From 2eb402014f636604846fe0ec12b1b67e14bedfc9 Mon Sep 17 00:00:00 2001 From: aki-0421 <118268728+aki-0421@users.noreply.github.com> Date: Mon, 22 Jan 2024 21:29:39 +0900 Subject: [PATCH] R: refactor context --- .../cfruntimecontext/cfruntimecontext.go | 6 +-- handler.go | 4 +- internal/cfcontext/context.go | 39 ------------------- internal/runtimecontext/context.go | 37 ++++++++++++++++++ 4 files changed, 42 insertions(+), 44 deletions(-) delete mode 100644 internal/cfcontext/context.go create mode 100644 internal/runtimecontext/context.go diff --git a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go index 6371e78..b36d9cc 100644 --- a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go +++ b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go @@ -5,7 +5,7 @@ import ( "errors" "syscall/js" - "github.com/syumai/workers/internal/cfcontext" + "github.com/syumai/workers/internal/runtimecontext" ) /** @@ -49,8 +49,8 @@ var ErrValueNotFound = errors.New("execution context value for specified key not // GetRuntimeContextValue gets value for specified key from RuntimeContext. // - if the value is undefined, return error. func GetRuntimeContextValue(ctx context.Context, key string) (js.Value, error) { - runtimeCtxValue := cfcontext.MustExtractRuntimeContext(ctx) - v := runtimeCtxValue.Get(key) + runtimeObj := runtimecontext.MustExtractRuntimeObj(ctx) + v := runtimeObj.Get(key) if v.IsUndefined() { return js.Value{}, ErrValueNotFound } diff --git a/handler.go b/handler.go index d607d8a..87398bb 100644 --- a/handler.go +++ b/handler.go @@ -7,9 +7,9 @@ import ( "net/http" "syscall/js" - "github.com/syumai/workers/internal/cfcontext" "github.com/syumai/workers/internal/jshttp" "github.com/syumai/workers/internal/jsutil" + "github.com/syumai/workers/internal/runtimecontext" ) var httpHandler http.Handler @@ -49,7 +49,7 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) { if err != nil { panic(err) } - ctx := cfcontext.New(context.Background(), runtimeCtxObj, reqObj.Get("cf")) + ctx := runtimecontext.New(context.Background(), reqObj, runtimeCtxObj) req = req.WithContext(ctx) reader, writer := io.Pipe() w := &jshttp.ResponseWriter{ diff --git a/internal/cfcontext/context.go b/internal/cfcontext/context.go deleted file mode 100644 index d2da914..0000000 --- a/internal/cfcontext/context.go +++ /dev/null @@ -1,39 +0,0 @@ -package cfcontext - -import ( - "context" - "errors" - "syscall/js" -) - -type runtimeCtxKey struct{} -type incomingPropertyKey struct{} - -func New(ctx context.Context, runtimeCtxObj, incomingPropertyObj js.Value) context.Context { - ctx = context.WithValue(ctx, runtimeCtxKey{}, runtimeCtxObj) - ctx = context.WithValue(ctx, incomingPropertyKey{}, incomingPropertyObj) - return ctx -} - -var ErrRuntimeContextNotFound = errors.New("runtime context was not found") -var ErrIncomingPropertyNotFound = errors.New("incoming property was not found") - -// MustExtractRuntimeContext extracts runtime context object from context. -// This function panics when runtime context object was not found. -func MustExtractRuntimeContext(ctx context.Context) js.Value { - v, ok := ctx.Value(runtimeCtxKey{}).(js.Value) - if !ok { - panic(ErrRuntimeContextNotFound) - } - return v -} - -// MustExtractIncomingProperty extracts incoming property object from context. -// This function panics when incoming property object was not found. -func MustExtractIncomingProperty(ctx context.Context) js.Value { - v, ok := ctx.Value(incomingPropertyKey{}).(js.Value) - if !ok { - panic(ErrIncomingPropertyNotFound) - } - return v -} diff --git a/internal/runtimecontext/context.go b/internal/runtimecontext/context.go new file mode 100644 index 0000000..288e0cd --- /dev/null +++ b/internal/runtimecontext/context.go @@ -0,0 +1,37 @@ +package runtimecontext + +import ( + "context" + "syscall/js" +) + +type ( + contextKeyTriggerObj struct{} + contextKeyRuntimeObj struct{} +) + +func New(ctx context.Context, triggerObj, runtimeObj js.Value) context.Context { + ctx = context.WithValue(ctx, contextKeyTriggerObj{}, triggerObj) + ctx = context.WithValue(ctx, contextKeyRuntimeObj{}, runtimeObj) + return ctx +} + +// MustExtractTriggerObj extracts trigger object from context. +// This function panics when trigger object was not found. +func MustExtractTriggerObj(ctx context.Context) js.Value { + v, ok := ctx.Value(contextKeyTriggerObj{}).(js.Value) + if !ok { + panic("trigger object was not found") + } + return v +} + +// MustExtractRuntimeObj extracts runtime object from context. +// This function panics when runtime object was not found. +func MustExtractRuntimeObj(ctx context.Context) js.Value { + v, ok := ctx.Value(contextKeyRuntimeObj{}).(js.Value) + if !ok { + panic("runtime object was not found") + } + return v +}