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

Commit

Permalink
Add dynamic dispatch #18
Browse files Browse the repository at this point in the history
  • Loading branch information
myakhnis-shopify authored and yknx4 committed May 30, 2022
1 parent 00f8635 commit a330f4e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
25 changes: 25 additions & 0 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,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 @@ -305,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 Expand Up @@ -459,6 +482,8 @@ func (api *API) ListWorkerBindings(ctx context.Context, requestParams *WorkerReq
Service: jsonBinding["service"].(string),
Environment: jsonBinding["environment"].(string),
}
case WorkerDynamicDispatchBindingType:
bindingListItem.Binding = WorkerDynamicDispatchBinding{}
default:
bindingListItem.Binding = WorkerInheritBinding{}
}
Expand Down
48 changes: 46 additions & 2 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ const (
"service": "test_service",
"environment": "production"
},
{
"name": "MY_DISPATCHER",
"type": "dynamic_dispatch"
},
{
"name": "MY_NEW_BINDING",
"type": "some_imaginary_new_binding_type"
Expand Down Expand Up @@ -767,6 +771,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 Expand Up @@ -1034,7 +1072,7 @@ func TestWorkers_ListWorkerBindingsMultiScript(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, successResponse, res.Response)
assert.Equal(t, 6, len(res.BindingList))
assert.Equal(t, 7, len(res.BindingList))

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -1075,10 +1113,16 @@ func TestWorkers_ListWorkerBindingsMultiScript(t *testing.T) {
assert.Equal(t, WorkerServiceBindingType, res.BindingList[4].Binding.Type())

assert.Equal(t, res.BindingList[5], WorkerBindingListItem{
Name: "MY_DISPATCHER",
Binding: WorkerDynamicDispatchBinding{},
})
assert.Equal(t, WorkerDynamicDispatchBindingType, res.BindingList[5].Binding.Type())

assert.Equal(t, res.BindingList[6], WorkerBindingListItem{
Name: "MY_NEW_BINDING",
Binding: WorkerInheritBinding{},
})
assert.Equal(t, WorkerInheritBindingType, res.BindingList[5].Binding.Type())
assert.Equal(t, WorkerInheritBindingType, res.BindingList[6].Binding.Type())
}

func TestWorkers_UpdateWorkerRouteErrorsWhenMixingSingleAndMultiScriptProperties(t *testing.T) {
Expand Down

0 comments on commit a330f4e

Please sign in to comment.