diff --git a/cloudflare/env.go b/cloudflare/env.go index 75c09e6..85a28dd 100644 --- a/cloudflare/env.go +++ b/cloudflare/env.go @@ -2,11 +2,13 @@ package cloudflare import ( "context" + + "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" ) // 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 getRuntimeContextEnv(ctx).Get(name).String() + return cfruntimecontext.GetRuntimeContextEnv(ctx).Get(name).String() } diff --git a/cloudflare/runtimecontext.go b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go similarity index 79% rename from cloudflare/runtimecontext.go rename to cloudflare/internal/cfruntimecontext/cfruntimecontext.go index bc3fd2d..cbc2228 100644 --- a/cloudflare/runtimecontext.go +++ b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go @@ -1,4 +1,4 @@ -package cloudflare +package cfruntimecontext import ( "context" @@ -19,17 +19,17 @@ 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. +// GetRuntimeContextEnv 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 { +func GetRuntimeContextEnv(ctx context.Context) js.Value { runtimeCtxValue := runtimecontext.MustExtract(ctx) return runtimeCtxValue.Get("env") } -// getExecutionContext gets ExecutionContext object from context. +// GetExecutionContext 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 { +func GetExecutionContext(ctx context.Context) js.Value { runtimeCtxValue := runtimecontext.MustExtract(ctx) return runtimeCtxValue.Get("ctx") } diff --git a/cloudflare/kv.go b/cloudflare/kv.go index 13da052..fd1e935 100644 --- a/cloudflare/kv.go +++ b/cloudflare/kv.go @@ -6,6 +6,8 @@ import ( "io" "syscall/js" + "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" + "github.com/syumai/workers/internal/jsutil" ) @@ -21,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 := getRuntimeContextEnv(ctx).Get(varName) + inst := cfruntimecontext.GetRuntimeContextEnv(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 3697d04..e1d1add 100644 --- a/cloudflare/r2bucket.go +++ b/cloudflare/r2bucket.go @@ -6,6 +6,7 @@ import ( "io" "syscall/js" + "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" "github.com/syumai/workers/internal/jsutil" ) @@ -22,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 := getRuntimeContextEnv(ctx).Get(varName) + inst := cfruntimecontext.GetRuntimeContextEnv(ctx).Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) }