Skip to content

Commit

Permalink
Add Durable Object bindings to Workers upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sodabrew committed Jun 23, 2022
1 parent 31124cf commit fbd0322
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
33 changes: 33 additions & 0 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (b WorkerBindingType) String() string {
}

const (
// WorkerDurableObjectBindingType is the type for Durable Object bindings.
WorkerDurableObjectBindingType WorkerBindingType = "durable_object_namespace"
// WorkerInheritBindingType is the type for inherited bindings.
WorkerInheritBindingType WorkerBindingType = "inherit"
// WorkerKvNamespaceBindingType is the type for KV Namespace bindings.
Expand Down Expand Up @@ -188,6 +190,30 @@ func (b WorkerKvNamespaceBinding) serialize(bindingName string) (workerBindingMe
}, nil, nil
}

// WorkerDurableObjectBinding is a binding to a Workers DurableObject
type WorkerDurableObjectBinding struct {
ClassName string
ScriptName string
}

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

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

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

// WorkerWebAssemblyBinding is a binding to a WebAssembly module
//
// https://developers.cloudflare.com/workers/archive/api/resource-bindings/webassembly-modules/
Expand Down Expand Up @@ -426,6 +452,13 @@ func (api *API) ListWorkerBindings(ctx context.Context, requestParams *WorkerReq
}

switch WorkerBindingType(bType) {
case WorkerDurableObjectBindingType:
class_name := jsonBinding["class_name"].(string)
script_name := jsonBinding["script_name"].(string)
bindingListItem.Binding = WorkerDurableObjectBinding{
ClassName: class_name,
ScriptName: script_name,
}
case WorkerKvNamespaceBindingType:
namespaceID := jsonBinding["namespace_id"].(string)
bindingListItem.Binding = WorkerKvNamespaceBinding{
Expand Down
39 changes: 39 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,45 @@ func TestWorkers_UploadWorkerWithNameErrorsWithoutAccountId(t *testing.T) {
assert.Error(t, err)
}

func TestWorkers_UploadWorkerWithDurableObjectBinding(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": {
"name": "b1",
"type": "durable_object_namespace",
"class_name": "TheClass",
"script_name": "the_script",
},
}
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": WorkerDurableObjectBinding{
ClassName: "TheClass",
ScriptName: "the_script",
},
},
}
_, err := client.UploadWorkerWithBindings(context.Background(), &WorkerRequestParams{ScriptName: "bar"}, &scriptParams)
assert.NoError(t, err)
}

func TestWorkers_UploadWorkerWithInheritBinding(t *testing.T) {
setup(UsingAccount("foo"))
defer teardown()
Expand Down

0 comments on commit fbd0322

Please sign in to comment.