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 Jul 13, 2022
1 parent fc5963a commit 4b65bf2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
25 changes: 25 additions & 0 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const (
WorkerServiceBindingType WorkerBindingType = "service"
// WorkerR2BucketBindingType is the type for R2 bucket bindings.
WorkerR2BucketBindingType WorkerBindingType = "r2_bucket"
// 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 @@ -355,6 +357,27 @@ func (b WorkerR2BucketBinding) 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, fmt.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 @@ -523,6 +546,8 @@ func (api *API) ListWorkerBindings(ctx context.Context, requestParams *WorkerReq
bindingListItem.Binding = WorkerR2BucketBinding{
BucketName: bucketName,
}
case WorkerDynamicDispatchBindingType:
bindingListItem.Binding = WorkerDynamicDispatchBinding{}
default:
bindingListItem.Binding = WorkerInheritBinding{}
}
Expand Down
55 changes: 50 additions & 5 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ const (
"service": "MY_SERVICE",
"environment": "MY_ENVIRONMENT"
},
{
"name": "MY_DISPATCHER",
"type": "dynamic_dispatch"
},
{
"name": "MY_NEW_BINDING",
"type": "some_imaginary_new_binding_type"
"type": "inherit"
},
{
"name": "MY_BUCKET",
Expand Down Expand Up @@ -820,6 +824,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 @@ -1087,7 +1125,7 @@ func TestWorkers_ListWorkerBindingsMultiScript(t *testing.T) {
assert.NoError(t, err)

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

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -1129,18 +1167,25 @@ 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())

assert.Equal(t, res.BindingList[6], WorkerBindingListItem{
assert.Equal(t, res.BindingList[7], WorkerBindingListItem{
Name: "MY_BUCKET",
Binding: WorkerR2BucketBinding{
BucketName: "bucket",
},
})
assert.Equal(t, WorkerR2BucketBindingType, res.BindingList[6].Binding.Type())
assert.Equal(t, WorkerR2BucketBindingType, res.BindingList[7].Binding.Type())
assert.Equal(t, WorkerInheritBindingType, res.BindingList[6].Binding.Type())
}

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

0 comments on commit 4b65bf2

Please sign in to comment.