From 400b51c6b841a3385c5cd9cb787c00accc73a1b9 Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 3 Jan 2024 23:32:24 +0900 Subject: [PATCH] add MustGetRuntimeContextValue and changed to use it --- cloudflare/d1/connector.go | 2 +- cloudflare/dostub.go | 2 +- cloudflare/env.go | 4 ++-- cloudflare/fetchevent.go | 4 ++-- .../cfruntimecontext/cfruntimecontext.go | 24 ++++++++++++------- cloudflare/kv.go | 2 +- cloudflare/r2bucket.go | 2 +- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cloudflare/d1/connector.go b/cloudflare/d1/connector.go index eb10df5..2ee1626 100644 --- a/cloudflare/d1/connector.go +++ b/cloudflare/d1/connector.go @@ -19,7 +19,7 @@ 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.GetRuntimeContextEnv(ctx).Get(name) + v := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name) if v.IsUndefined() { return nil, ErrDatabaseNotFound } diff --git a/cloudflare/dostub.go b/cloudflare/dostub.go index 7200050..2cb15c0 100644 --- a/cloudflare/dostub.go +++ b/cloudflare/dostub.go @@ -21,7 +21,7 @@ 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.GetRuntimeContextEnv(ctx).Get(varName) + inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/env.go b/cloudflare/env.go index 82e8f48..9fb00a1 100644 --- a/cloudflare/env.go +++ b/cloudflare/env.go @@ -11,12 +11,12 @@ import ( // - 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.GetRuntimeContextEnv(ctx).Get(name).String() + return cfruntimecontext.MustGetRuntimeContextEnv(ctx).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.GetRuntimeContextEnv(ctx).Get(name) + return cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name) } diff --git a/cloudflare/fetchevent.go b/cloudflare/fetchevent.go index 2c63db7..5369c96 100644 --- a/cloudflare/fetchevent.go +++ b/cloudflare/fetchevent.go @@ -12,7 +12,7 @@ import ( // 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.GetExecutionContext(ctx) + exCtx := cfruntimecontext.MustGetExecutionContext(ctx) exCtx.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { resolve := pArgs[0] go func() { @@ -27,7 +27,7 @@ func WaitUntil(ctx context.Context, task func()) { // 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.GetExecutionContext(ctx) + exCtx := cfruntimecontext.MustGetExecutionContext(ctx) jsutil.AwaitPromise(jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { resolve := pArgs[0] go func() { diff --git a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go index 69d076f..731190d 100644 --- a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go +++ b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go @@ -21,19 +21,27 @@ import ( * - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#LL564 */ -// GetRuntimeContextEnv gets object which holds environment variables bound to Cloudflare worker. +// MustGetRuntimeContextEnv gets object which holds environment variables bound to Cloudflare worker. // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L566 -func GetRuntimeContextEnv(ctx context.Context) js.Value { - runtimeCtxValue := runtimecontext.MustExtract(ctx) - return runtimeCtxValue.Get("env") +func MustGetRuntimeContextEnv(ctx context.Context) js.Value { + return MustGetRuntimeContextValue(ctx, "env") } -// GetExecutionContext gets ExecutionContext object from context. +// MustGetExecutionContext gets ExecutionContext object from context. // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L567 // - see also: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L554 -func GetExecutionContext(ctx context.Context) js.Value { - runtimeCtxValue := runtimecontext.MustExtract(ctx) - return runtimeCtxValue.Get("ctx") +func MustGetExecutionContext(ctx context.Context) js.Value { + return MustGetRuntimeContextValue(ctx, "ctx") +} + +// MustGetRuntimeContextValue gets value for specified key from RuntimeContext. +// - if the value is undefined, this function panics. +func MustGetRuntimeContextValue(ctx context.Context, key string) js.Value { + val, err := GetRuntimeContextValue(ctx, key) + if err != nil { + panic(err) + } + return val } var ErrValueNotFound = errors.New("execution context value for specified key not found") diff --git a/cloudflare/kv.go b/cloudflare/kv.go index fd1e935..9cb6665 100644 --- a/cloudflare/kv.go +++ b/cloudflare/kv.go @@ -23,7 +23,7 @@ type KVNamespace struct { // - 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.GetRuntimeContextEnv(ctx).Get(varName) + inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/r2bucket.go b/cloudflare/r2bucket.go index e581734..cf9e2fc 100644 --- a/cloudflare/r2bucket.go +++ b/cloudflare/r2bucket.go @@ -23,7 +23,7 @@ type R2Bucket struct { // - 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.GetRuntimeContextEnv(ctx).Get(varName) + inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) }