mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
F: add incoming property context
This commit is contained in:
parent
bac0abd44b
commit
cdd8a26c53
@ -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
|
||||
|
@ -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{
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user