Skip to content

Commit

Permalink
add unsafe bindings to workers
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlisman committed Jul 7, 2023
1 parent cd0dbf2 commit 0de7841
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions workers_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@ func (b DispatchNamespaceBinding) serialize(bindingName string) (workerBindingMe
return meta, nil, nil
}

// UnsafeBinding is for experimental or deprecated bindings, and allows specifying any binding type or property

Check failure on line 410 in workers_bindings.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
type UnsafeBinding map[string]interface{}

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

func (b UnsafeBinding) serialize(bindingName string) (workerBindingMeta, workerBindingBodyWriter, error) {
b["name"] = bindingName
return b, 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
36 changes: 36 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,39 @@ func TestUploadWorker_ToDispatchNamespace(t *testing.T) {
})
assert.NoError(t, err)
}

func TestUploadWorker_UnsafeBinding(t *testing.T) {
setup()
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)

assert.Equal(t, workerScript, mpUpload.Script)

require.Contains(t, mpUpload.BindingMeta, "b1")
assert.Contains(t, mpUpload.BindingMeta["b1"], "name")
assert.Equal(t, "b1", mpUpload.BindingMeta["b1"]["name"])
assert.Contains(t, mpUpload.BindingMeta["b1"], "type")
assert.Equal(t, "dynamic_dispatch", mpUpload.BindingMeta["b1"]["type"])

w.Header().Set("content-type", "application/json")
fmt.Println(workersScriptResponse(t))
fmt.Fprint(w, workersScriptResponse(t))
}
mux.HandleFunc("/accounts/"+testAccountID+"/workers/scripts/bar", handler)

_, err := client.UploadWorker(context.Background(), AccountIdentifier(testAccountID), CreateWorkerParams{
ScriptName: "bar",
Script: workerScript,
Bindings: map[string]WorkerBinding{
"b1": UnsafeBinding{
"type": "dynamic_dispatch",
},
},
})
assert.NoError(t, err)
}

0 comments on commit 0de7841

Please sign in to comment.