Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Add dynamic dispatch #18

Merged
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ type WorkerScript struct {

// WorkerMetaData contains worker script information such as size, creation & modification dates.
type WorkerMetaData struct {
ID string `json:"id,omitempty"`
ETAG string `json:"etag,omitempty"`
Size int `json:"size,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
ID string `json:"id,omitempty"`
ETAG string `json:"etag,omitempty"`
ETAG_BYPASS string `json:"etag_bypass,omitempty"`
Size int `json:"size,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
}

// WorkerListResponse wrapper struct for API response to worker script list API call.
Expand Down Expand Up @@ -106,6 +107,8 @@ const (
WorkerPlainTextBindingType WorkerBindingType = "plain_text"
// WorkerServiceBindingType is the type for worker service bindings.
WorkerServiceBindingType WorkerBindingType = "service"
// WorkerDynamicDispatchBindingType is the type for dynamic dispatch bindings.
WorkerDynamicDispatchBindingType WorkerBindingType = "dynamic_dispatch"
)

// WorkerBindingListItem a struct representing an individual binding in a list of bindings.
Expand Down Expand Up @@ -304,6 +307,27 @@ func (b WorkerServiceBinding) serialize(bindingName string) (workerBindingMeta,
}, nil, nil
}

// WorkerDynamicDispatchBinding is a binding to a dispatcher than can dispatch another Worker
//
// https://developers.cloudflare.com/workers/tooling/api/scripts/#add-a-plain-text-binding
type WorkerDynamicDispatchBinding struct{}

// Type returns the type of the binding.
func (b WorkerDynamicDispatchBinding) Type() WorkerBindingType {
return WorkerDynamicDispatchBindingType
}

func (b WorkerDynamicDispatchBinding) serialize(bindingName string) (workerBindingMeta, workerBindingBodyWriter, error) {
if bindingName == "" {
return nil, nil, errors.Errorf(`Name for Dynamic Dispatch binding "%s" cannot be empty`, bindingName)
}

return workerBindingMeta{
"name": bindingName,
"type": b.Type(),
}, nil, nil
}

// Each binding that adds a part to the multipart form body will need
// a unique part name so we just generate a random 128bit hex string.
func getRandomPartName() string {
Expand Down
34 changes: 34 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,40 @@ func TestWorkers_UploadWorkerWithCompatibilityFlags(t *testing.T) {
assert.NoError(t, err)
}

func TestWorkers_UploadWorkerWithDynamicDispatch(t *testing.T) {
setup(UsingAccount("foo"))
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)

mpUpload, err := parseMultipartUpload(r)
assert.NoError(t, err)

expectedBindings := map[string]workerBindingMeta{
"b1": {
"type": "dynamic_dispatch",
"name": "b1",
},
}
assert.Equal(t, workerScript, mpUpload.Script)
assert.Equal(t, expectedBindings, mpUpload.BindingMeta)

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, uploadWorkerResponseData) //nolint
}
mux.HandleFunc("/accounts/foo/workers/scripts/bar", handler)

scriptParams := WorkerScriptParams{
Script: workerScript,
Bindings: map[string]WorkerBinding{
"b1": WorkerDynamicDispatchBinding{},
},
}
_, err := client.UploadWorkerWithBindings(context.Background(), &WorkerRequestParams{ScriptName: "bar"}, &scriptParams)
assert.NoError(t, err)
}

func TestWorkers_CreateWorkerRoute(t *testing.T) {
setup()
defer teardown()
Expand Down