R: refactor context

This commit is contained in:
aki-0421 2024-01-22 21:29:39 +09:00
parent 4035675827
commit 2eb402014f
No known key found for this signature in database
GPG Key ID: 64A8CF6D437D166A
4 changed files with 42 additions and 44 deletions

View File

@ -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
}

View File

@ -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{

View File

@ -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
}

View File

@ -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
}