From 627ed95530f6100118daa0c1ae3751506dfa6ce4 Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:52:30 +0900 Subject: [PATCH] remove dependencies on Context from cloudflare-related APIs --- cloudflare/d1/connector.go | 4 ++-- cloudflare/dostub.go | 5 ++--- cloudflare/env.go | 9 ++++----- cloudflare/fetchevent.go | 9 ++++----- cloudflare/kv.go | 5 ++--- cloudflare/r2bucket.go | 5 ++--- cloudflare/sockets/connect.go | 2 +- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/cloudflare/d1/connector.go b/cloudflare/d1/connector.go index 2ee1626..ce79ff0 100644 --- a/cloudflare/d1/connector.go +++ b/cloudflare/d1/connector.go @@ -18,8 +18,8 @@ var ( // OpenConnector returns Connector of D1. // This method checks DB existence. If DB was not found, this function returns error. -func OpenConnector(ctx context.Context, name string) (driver.Connector, error) { - v := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name) +func OpenConnector(name string) (driver.Connector, error) { + v := cfruntimecontext.MustGetRuntimeContextEnv().Get(name) if v.IsUndefined() { return nil, ErrDatabaseNotFound } diff --git a/cloudflare/dostub.go b/cloudflare/dostub.go index 2cb15c0..fd80f19 100644 --- a/cloudflare/dostub.go +++ b/cloudflare/dostub.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "fmt" "net/http" "syscall/js" @@ -20,8 +19,8 @@ type DurableObjectNamespace struct { // // This binding must be defined in the `wrangler.toml` file. The method will // return an `error` when there is no binding defined by `varName`. -func NewDurableObjectNamespace(ctx context.Context, varName string) (*DurableObjectNamespace, error) { - inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) +func NewDurableObjectNamespace(varName string) (*DurableObjectNamespace, error) { + inst := cfruntimecontext.MustGetRuntimeContextEnv().Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/env.go b/cloudflare/env.go index 9fb00a1..30b67b8 100644 --- a/cloudflare/env.go +++ b/cloudflare/env.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "syscall/js" "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" @@ -10,13 +9,13 @@ import ( // Getenv gets a value of an environment variable. // - https://developers.cloudflare.com/workers/platform/environment-variables/ // - This function panics when a runtime context is not found. -func Getenv(ctx context.Context, name string) string { - return cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name).String() +func Getenv(name string) string { + return cfruntimecontext.MustGetRuntimeContextEnv().Get(name).String() } // GetBinding gets a value of an environment binding. // - https://developers.cloudflare.com/workers/platform/bindings/about-service-bindings/ // - This function panics when a runtime context is not found. -func GetBinding(ctx context.Context, name string) js.Value { - return cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name) +func GetBinding(name string) js.Value { + return cfruntimecontext.MustGetRuntimeContextEnv().Get(name) } diff --git a/cloudflare/fetchevent.go b/cloudflare/fetchevent.go index 5369c96..2c8ac0b 100644 --- a/cloudflare/fetchevent.go +++ b/cloudflare/fetchevent.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "syscall/js" "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" @@ -11,8 +10,8 @@ import ( // WaitUntil extends the lifetime of the "fetch" event. // It accepts an asynchronous task which the Workers runtime will execute before the handler terminates but without blocking the response. // see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil -func WaitUntil(ctx context.Context, task func()) { - exCtx := cfruntimecontext.MustGetExecutionContext(ctx) +func WaitUntil(task func()) { + exCtx := cfruntimecontext.MustGetExecutionContext() exCtx.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { resolve := pArgs[0] go func() { @@ -26,8 +25,8 @@ func WaitUntil(ctx context.Context, task func()) { // PassThroughOnException prevents a runtime error response when the Worker script throws an unhandled exception. // Instead, the request forwards to the origin server as if it had not gone through the worker. // see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#passthroughonexception -func PassThroughOnException(ctx context.Context) { - exCtx := cfruntimecontext.MustGetExecutionContext(ctx) +func PassThroughOnException() { + exCtx := cfruntimecontext.MustGetExecutionContext() jsutil.AwaitPromise(jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { resolve := pArgs[0] go func() { diff --git a/cloudflare/kv.go b/cloudflare/kv.go index 6d3b936..ead0517 100644 --- a/cloudflare/kv.go +++ b/cloudflare/kv.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "fmt" "io" "syscall/js" @@ -21,8 +20,8 @@ type KVNamespace struct { // - variable name must be defined in wrangler.toml as kv_namespace's binding. // - if the given variable name doesn't exist on runtime context, returns error. // - This function panics when a runtime context is not found. -func NewKVNamespace(ctx context.Context, varName string) (*KVNamespace, error) { - inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) +func NewKVNamespace(varName string) (*KVNamespace, error) { + inst := cfruntimecontext.MustGetRuntimeContextEnv().Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/r2bucket.go b/cloudflare/r2bucket.go index cf9e2fc..e617f52 100644 --- a/cloudflare/r2bucket.go +++ b/cloudflare/r2bucket.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "fmt" "io" "syscall/js" @@ -22,8 +21,8 @@ type R2Bucket struct { // - see example: https://github.com/syumai/workers/tree/main/_examples/r2-image-viewer // - if the given variable name doesn't exist on runtime context, returns error. // - This function panics when a runtime context is not found. -func NewR2Bucket(ctx context.Context, varName string) (*R2Bucket, error) { - inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) +func NewR2Bucket(varName string) (*R2Bucket, error) { + inst := cfruntimecontext.MustGetRuntimeContextEnv().Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/sockets/connect.go b/cloudflare/sockets/connect.go index 7bb0798..c7b85d0 100644 --- a/cloudflare/sockets/connect.go +++ b/cloudflare/sockets/connect.go @@ -30,7 +30,7 @@ type SocketOptions struct { const defaultDeadline = 999999 * time.Hour func Connect(ctx context.Context, addr string, opts *SocketOptions) (net.Conn, error) { - connect, err := cfruntimecontext.GetRuntimeContextValue(ctx, "connect") + connect, err := cfruntimecontext.GetRuntimeContextValue("connect") if err != nil { return nil, err }