F: add incoming property context

This commit is contained in:
aki-0421 2024-01-17 17:21:34 +09:00
parent bac0abd44b
commit cdd8a26c53
No known key found for this signature in database
GPG Key ID: 64A8CF6D437D166A
3 changed files with 23 additions and 6 deletions

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"errors" "errors"
"syscall/js" "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. // GetRuntimeContextValue gets value for specified key from RuntimeContext.
// - if the value is undefined, return error. // - if the value is undefined, return error.
func GetRuntimeContextValue(ctx context.Context, key string) (js.Value, error) { func GetRuntimeContextValue(ctx context.Context, key string) (js.Value, error) {
runtimeCtxValue := cfcontext.MustExtract(ctx) runtimeCtxValue := cfcontext.MustExtractRuntimeContext(ctx)
v := runtimeCtxValue.Get(key) v := runtimeCtxValue.Get(key)
if v.IsUndefined() { if v.IsUndefined() {
return js.Value{}, ErrValueNotFound return js.Value{}, ErrValueNotFound

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"syscall/js" "syscall/js"
"github.com/syumai/workers/internal/cfcontext"
"github.com/syumai/workers/internal/jshttp" "github.com/syumai/workers/internal/jshttp"
"github.com/syumai/workers/internal/jsutil" "github.com/syumai/workers/internal/jsutil"
) )
@ -48,7 +49,7 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
ctx := cfcontext.New(context.Background(), runtimeCtxObj) ctx := cfcontext.New(context.Background(), runtimeCtxObj, reqObj.Get("cf"))
req = req.WithContext(ctx) req = req.WithContext(ctx)
reader, writer := io.Pipe() reader, writer := io.Pipe()
w := &jshttp.ResponseWriter{ w := &jshttp.ResponseWriter{

View File

@ -7,19 +7,33 @@ import (
) )
type runtimeCtxKey struct{} type runtimeCtxKey struct{}
type incomingPropertyKey struct{}
func New(ctx context.Context, runtimeCtxObj js.Value) context.Context { func New(ctx context.Context, runtimeCtxObj, incomingPropertyObj js.Value) context.Context {
return context.WithValue(ctx, runtimeCtxKey{}, runtimeCtxObj) ctx = context.WithValue(ctx, runtimeCtxKey{}, runtimeCtxObj)
ctx = context.WithValue(ctx, incomingPropertyKey{}, incomingPropertyObj)
return ctx
} }
var ErrRuntimeContextNotFound = errors.New("runtime context was not found") 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. // 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) v, ok := ctx.Value(runtimeCtxKey{}).(js.Value)
if !ok { if !ok {
panic(ErrRuntimeContextNotFound) panic(ErrRuntimeContextNotFound)
} }
return v 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
}