add MustGetRuntimeContextValue and changed to use it

This commit is contained in:
syumai 2024-01-03 23:32:24 +09:00
parent 115680fa0a
commit 400b51c6b8
7 changed files with 24 additions and 16 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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() {

View File

@ -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")

View File

@ -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)
}

View File

@ -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)
}