From cdd8a26c53392c122b3a4a6f4e3028646c9ae452 Mon Sep 17 00:00:00 2001 From: aki-0421 <118268728+aki-0421@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:21:34 +0900 Subject: [PATCH] F: add incoming property context --- .../cfruntimecontext/cfruntimecontext.go | 4 +++- handler.go | 3 ++- internal/cfcontext/context.go | 22 +++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go index 41e7ea6..6371e78 100644 --- a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go +++ b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go @@ -4,6 +4,8 @@ import ( "context" "errors" "syscall/js" + + "github.com/syumai/workers/internal/cfcontext" ) /** @@ -47,7 +49,7 @@ 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.MustExtract(ctx) + runtimeCtxValue := cfcontext.MustExtractRuntimeContext(ctx) v := runtimeCtxValue.Get(key) if v.IsUndefined() { return js.Value{}, ErrValueNotFound diff --git a/handler.go b/handler.go index 396a166..d607d8a 100644 --- a/handler.go +++ b/handler.go @@ -7,6 +7,7 @@ import ( "net/http" "syscall/js" + "github.com/syumai/workers/internal/cfcontext" "github.com/syumai/workers/internal/jshttp" "github.com/syumai/workers/internal/jsutil" ) @@ -48,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) + ctx := cfcontext.New(context.Background(), runtimeCtxObj, reqObj.Get("cf")) req = req.WithContext(ctx) reader, writer := io.Pipe() w := &jshttp.ResponseWriter{ diff --git a/internal/cfcontext/context.go b/internal/cfcontext/context.go index b14b168..d2da914 100644 --- a/internal/cfcontext/context.go +++ b/internal/cfcontext/context.go @@ -7,19 +7,33 @@ import ( ) type runtimeCtxKey struct{} +type incomingPropertyKey struct{} -func New(ctx context.Context, runtimeCtxObj js.Value) context.Context { - return context.WithValue(ctx, runtimeCtxKey{}, runtimeCtxObj) +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") -// MustExtract extracts runtime context object from context. +// MustExtractRuntimeContext extracts runtime context object from context. // This function panics when runtime context object was not found. -func MustExtract(ctx context.Context) js.Value { +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 +}