From 33e4ad8f50d5e2f7c7b5bbb4dce55cc2ac15b16d Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:41:39 +0900 Subject: [PATCH 1/5] remove context.Context from existing RuntimeContext related func params --- .../cfruntimecontext/cfruntimecontext.go | 19 +++++++++---------- internal/runtimecontext/context.go | 10 ---------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go index b36d9cc..345dc4b 100644 --- a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go +++ b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go @@ -1,11 +1,10 @@ package cfruntimecontext import ( - "context" "errors" "syscall/js" - "github.com/syumai/workers/internal/runtimecontext" + "github.com/syumai/workers/internal/jsutil" ) /** @@ -23,21 +22,21 @@ import ( // MustGetRuntimeContextEnv gets object which holds environment variables bound to Cloudflare worker. // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L566 -func MustGetRuntimeContextEnv(ctx context.Context) js.Value { - return MustGetRuntimeContextValue(ctx, "env") +func MustGetRuntimeContextEnv() js.Value { + return MustGetRuntimeContextValue("env") } // MustGetExecutionContext gets ExecutionContext object from context. // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L567 // - see also: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L554 -func MustGetExecutionContext(ctx context.Context) js.Value { - return MustGetRuntimeContextValue(ctx, "ctx") +func MustGetExecutionContext() js.Value { + return MustGetRuntimeContextValue("ctx") } // MustGetRuntimeContextValue gets value for specified key from RuntimeContext. // - if the value is undefined, this function panics. -func MustGetRuntimeContextValue(ctx context.Context, key string) js.Value { - val, err := GetRuntimeContextValue(ctx, key) +func MustGetRuntimeContextValue(key string) js.Value { + val, err := GetRuntimeContextValue(key) if err != nil { panic(err) } @@ -48,8 +47,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) { - runtimeObj := runtimecontext.MustExtractRuntimeObj(ctx) +func GetRuntimeContextValue(key string) (js.Value, error) { + runtimeObj := jsutil.RuntimeContext v := runtimeObj.Get(key) if v.IsUndefined() { return js.Value{}, ErrValueNotFound diff --git a/internal/runtimecontext/context.go b/internal/runtimecontext/context.go index 288e0cd..4f5f668 100644 --- a/internal/runtimecontext/context.go +++ b/internal/runtimecontext/context.go @@ -25,13 +25,3 @@ func MustExtractTriggerObj(ctx context.Context) js.Value { } 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 -} From 627ed95530f6100118daa0c1ae3751506dfa6ce4 Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:52:30 +0900 Subject: [PATCH 2/5] remove dependencies on Context from cloudflare-related APIs --- cloudflare/d1/connector.go | 4 ++-- cloudflare/dostub.go | 5 ++--- cloudflare/env.go | 9 ++++----- cloudflare/fetchevent.go | 9 ++++----- cloudflare/kv.go | 5 ++--- cloudflare/r2bucket.go | 5 ++--- cloudflare/sockets/connect.go | 2 +- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/cloudflare/d1/connector.go b/cloudflare/d1/connector.go index 2ee1626..ce79ff0 100644 --- a/cloudflare/d1/connector.go +++ b/cloudflare/d1/connector.go @@ -18,8 +18,8 @@ var ( // OpenConnector returns Connector of D1. // This method checks DB existence. If DB was not found, this function returns error. -func OpenConnector(ctx context.Context, name string) (driver.Connector, error) { - v := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name) +func OpenConnector(name string) (driver.Connector, error) { + v := cfruntimecontext.MustGetRuntimeContextEnv().Get(name) if v.IsUndefined() { return nil, ErrDatabaseNotFound } diff --git a/cloudflare/dostub.go b/cloudflare/dostub.go index 2cb15c0..fd80f19 100644 --- a/cloudflare/dostub.go +++ b/cloudflare/dostub.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "fmt" "net/http" "syscall/js" @@ -20,8 +19,8 @@ type DurableObjectNamespace struct { // // This binding must be defined in the `wrangler.toml` file. The method will // return an `error` when there is no binding defined by `varName`. -func NewDurableObjectNamespace(ctx context.Context, varName string) (*DurableObjectNamespace, error) { - inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) +func NewDurableObjectNamespace(varName string) (*DurableObjectNamespace, error) { + inst := cfruntimecontext.MustGetRuntimeContextEnv().Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/env.go b/cloudflare/env.go index 9fb00a1..30b67b8 100644 --- a/cloudflare/env.go +++ b/cloudflare/env.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "syscall/js" "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" @@ -10,13 +9,13 @@ import ( // Getenv gets a value of an environment variable. // - https://developers.cloudflare.com/workers/platform/environment-variables/ // - This function panics when a runtime context is not found. -func Getenv(ctx context.Context, name string) string { - return cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name).String() +func Getenv(name string) string { + return cfruntimecontext.MustGetRuntimeContextEnv().Get(name).String() } // GetBinding gets a value of an environment binding. // - https://developers.cloudflare.com/workers/platform/bindings/about-service-bindings/ // - This function panics when a runtime context is not found. -func GetBinding(ctx context.Context, name string) js.Value { - return cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(name) +func GetBinding(name string) js.Value { + return cfruntimecontext.MustGetRuntimeContextEnv().Get(name) } diff --git a/cloudflare/fetchevent.go b/cloudflare/fetchevent.go index 5369c96..2c8ac0b 100644 --- a/cloudflare/fetchevent.go +++ b/cloudflare/fetchevent.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "syscall/js" "github.com/syumai/workers/cloudflare/internal/cfruntimecontext" @@ -11,8 +10,8 @@ import ( // WaitUntil extends the lifetime of the "fetch" event. // It accepts an asynchronous task which the Workers runtime will execute before the handler terminates but without blocking the response. // see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil -func WaitUntil(ctx context.Context, task func()) { - exCtx := cfruntimecontext.MustGetExecutionContext(ctx) +func WaitUntil(task func()) { + exCtx := cfruntimecontext.MustGetExecutionContext() exCtx.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { resolve := pArgs[0] go func() { @@ -26,8 +25,8 @@ func WaitUntil(ctx context.Context, task func()) { // PassThroughOnException prevents a runtime error response when the Worker script throws an unhandled exception. // Instead, the request forwards to the origin server as if it had not gone through the worker. // see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#passthroughonexception -func PassThroughOnException(ctx context.Context) { - exCtx := cfruntimecontext.MustGetExecutionContext(ctx) +func PassThroughOnException() { + exCtx := cfruntimecontext.MustGetExecutionContext() jsutil.AwaitPromise(jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { resolve := pArgs[0] go func() { diff --git a/cloudflare/kv.go b/cloudflare/kv.go index 6d3b936..ead0517 100644 --- a/cloudflare/kv.go +++ b/cloudflare/kv.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "fmt" "io" "syscall/js" @@ -21,8 +20,8 @@ type KVNamespace struct { // - variable name must be defined in wrangler.toml as kv_namespace's binding. // - if the given variable name doesn't exist on runtime context, returns error. // - This function panics when a runtime context is not found. -func NewKVNamespace(ctx context.Context, varName string) (*KVNamespace, error) { - inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) +func NewKVNamespace(varName string) (*KVNamespace, error) { + inst := cfruntimecontext.MustGetRuntimeContextEnv().Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/r2bucket.go b/cloudflare/r2bucket.go index cf9e2fc..e617f52 100644 --- a/cloudflare/r2bucket.go +++ b/cloudflare/r2bucket.go @@ -1,7 +1,6 @@ package cloudflare import ( - "context" "fmt" "io" "syscall/js" @@ -22,8 +21,8 @@ type R2Bucket struct { // - see example: https://github.com/syumai/workers/tree/main/_examples/r2-image-viewer // - if the given variable name doesn't exist on runtime context, returns error. // - This function panics when a runtime context is not found. -func NewR2Bucket(ctx context.Context, varName string) (*R2Bucket, error) { - inst := cfruntimecontext.MustGetRuntimeContextEnv(ctx).Get(varName) +func NewR2Bucket(varName string) (*R2Bucket, error) { + inst := cfruntimecontext.MustGetRuntimeContextEnv().Get(varName) if inst.IsUndefined() { return nil, fmt.Errorf("%s is undefined", varName) } diff --git a/cloudflare/sockets/connect.go b/cloudflare/sockets/connect.go index 7bb0798..c7b85d0 100644 --- a/cloudflare/sockets/connect.go +++ b/cloudflare/sockets/connect.go @@ -30,7 +30,7 @@ type SocketOptions struct { const defaultDeadline = 999999 * time.Hour func Connect(ctx context.Context, addr string, opts *SocketOptions) (net.Conn, error) { - connect, err := cfruntimecontext.GetRuntimeContextValue(ctx, "connect") + connect, err := cfruntimecontext.GetRuntimeContextValue("connect") if err != nil { return nil, err } From 52a78e178916cd081b8263dc9b66685ec7958cf4 Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:56:35 +0900 Subject: [PATCH 3/5] remove runtimeObj from runtimecontext.New --- cloudflare/cron/scheduler.go | 7 +++---- handler.go | 7 +++---- internal/runtimecontext/context.go | 4 +--- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/cloudflare/cron/scheduler.go b/cloudflare/cron/scheduler.go index d559568..ebad1c7 100644 --- a/cloudflare/cron/scheduler.go +++ b/cloudflare/cron/scheduler.go @@ -13,8 +13,8 @@ type Task func(ctx context.Context) error var scheduledTask Task -func runScheduler(eventObj js.Value, runtimeCtxObj js.Value) error { - ctx := runtimecontext.New(context.Background(), eventObj, runtimeCtxObj) +func runScheduler(eventObj js.Value) error { + ctx := runtimecontext.New(context.Background(), eventObj) if err := scheduledTask(ctx); err != nil { return err } @@ -27,13 +27,12 @@ func init() { panic(fmt.Errorf("invalid number of arguments given to runScheduler: %d", len(args))) } eventObj := args[0] - runtimeCtxObj := jsutil.RuntimeContext var cb js.Func cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { defer cb.Release() resolve := pArgs[0] go func() { - err := runScheduler(eventObj, runtimeCtxObj) + err := runScheduler(eventObj) if err != nil { panic(err) } diff --git a/handler.go b/handler.go index 8814f3b..61930e1 100644 --- a/handler.go +++ b/handler.go @@ -24,13 +24,12 @@ func init() { panic(fmt.Errorf("too many args given to handleRequest: %d", len(args))) } reqObj := args[0] - runtimeCtxObj := jsutil.RuntimeContext var cb js.Func cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { defer cb.Release() resolve := pArgs[0] go func() { - res, err := handleRequest(reqObj, runtimeCtxObj) + res, err := handleRequest(reqObj) if err != nil { panic(err) } @@ -53,7 +52,7 @@ func (c *appCloser) Close() error { } // handleRequest accepts a Request object and returns Response object. -func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) { +func handleRequest(reqObj js.Value) (js.Value, error) { if httpHandler == nil { return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.") } @@ -61,7 +60,7 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) { if err != nil { panic(err) } - ctx := runtimecontext.New(context.Background(), reqObj, runtimeCtxObj) + ctx := runtimecontext.New(context.Background(), reqObj) req = req.WithContext(ctx) reader, writer := io.Pipe() w := &jshttp.ResponseWriter{ diff --git a/internal/runtimecontext/context.go b/internal/runtimecontext/context.go index 4f5f668..f1d15e6 100644 --- a/internal/runtimecontext/context.go +++ b/internal/runtimecontext/context.go @@ -7,12 +7,10 @@ import ( type ( contextKeyTriggerObj struct{} - contextKeyRuntimeObj struct{} ) -func New(ctx context.Context, triggerObj, runtimeObj js.Value) context.Context { +func New(ctx context.Context, triggerObj js.Value) context.Context { ctx = context.WithValue(ctx, contextKeyTriggerObj{}, triggerObj) - ctx = context.WithValue(ctx, contextKeyRuntimeObj{}, runtimeObj) return ctx } From ca4bfba6cc88aed65ea172f140ab9c5bb51486a5 Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:56:47 +0900 Subject: [PATCH 4/5] fix examples --- _examples/cache/main.go | 3 +-- _examples/d1-blog-server/app/handler.go | 2 +- _examples/durable-object-counter/main.go | 5 ++--- _examples/env/main.go | 2 +- _examples/fetch-event/go.mod | 2 +- _examples/fetch-event/main.go | 6 ++---- _examples/kv-counter/main.go | 2 +- _examples/mysql-blog-server/app/handler.go | 6 +++--- _examples/r2-image-server/main.go | 19 +++++++++---------- _examples/r2-image-viewer/main.go | 2 +- _examples/service-bindings/go.mod | 2 +- _examples/service-bindings/main.go | 3 +-- 12 files changed, 24 insertions(+), 30 deletions(-) diff --git a/_examples/cache/main.go b/_examples/cache/main.go index f576a01..8f7c9fd 100644 --- a/_examples/cache/main.go +++ b/_examples/cache/main.go @@ -37,7 +37,6 @@ func (rw *responseWriter) ToHTTPResponse() *http.Response { } func handler(w http.ResponseWriter, req *http.Request) { - ctx := req.Context() rw := responseWriter{ResponseWriter: w} c := cache.New() @@ -64,7 +63,7 @@ func handler(w http.ResponseWriter, req *http.Request) { rw.Write([]byte(text)) // Create cache - cloudflare.WaitUntil(ctx, func() { + cloudflare.WaitUntil(func() { err := c.Put(req, rw.ToHTTPResponse()) if err != nil { fmt.Println(err) diff --git a/_examples/d1-blog-server/app/handler.go b/_examples/d1-blog-server/app/handler.go index 13aaaca..0251f72 100644 --- a/_examples/d1-blog-server/app/handler.go +++ b/_examples/d1-blog-server/app/handler.go @@ -25,7 +25,7 @@ func NewArticleHandler() http.Handler { func (h *articleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // initialize DB. // D1 connector requires request's context to initialize DB. - c, err := d1.OpenConnector(req.Context(), "BlogDB") + c, err := d1.OpenConnector("BlogDB") if err != nil { h.handleErr(w, http.StatusInternalServerError, fmt.Sprintf("failed to initialize DB: %v", err)) } diff --git a/_examples/durable-object-counter/main.go b/_examples/durable-object-counter/main.go index 336e2fa..c5ccb41 100644 --- a/_examples/durable-object-counter/main.go +++ b/_examples/durable-object-counter/main.go @@ -12,10 +12,10 @@ func main() { workers.Serve(&MyHandler{}) } -type MyHandler struct {} +type MyHandler struct{} func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - COUNTER, err := cloudflare.NewDurableObjectNamespace(req.Context(), "COUNTER") + COUNTER, err := cloudflare.NewDurableObjectNamespace("COUNTER") if err != nil { panic(err) } @@ -38,4 +38,3 @@ func (_ *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Durable object 'A' count: " + string(count))) } - diff --git a/_examples/env/main.go b/_examples/env/main.go index cf1e10d..bd8aa79 100644 --- a/_examples/env/main.go +++ b/_examples/env/main.go @@ -10,7 +10,7 @@ import ( func main() { handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - fmt.Fprintf(w, "MY_ENV: %s", cloudflare.Getenv(req.Context(), "MY_ENV")) + fmt.Fprintf(w, "MY_ENV: %s", cloudflare.Getenv("MY_ENV")) }) workers.Serve(handler) } diff --git a/_examples/fetch-event/go.mod b/_examples/fetch-event/go.mod index d698bb5..00d09bf 100644 --- a/_examples/fetch-event/go.mod +++ b/_examples/fetch-event/go.mod @@ -1,4 +1,4 @@ -module github.com/syumai/workers/_examples/hello +module github.com/syumai/workers/_examples/fetch-event go 1.21.3 diff --git a/_examples/fetch-event/main.go b/_examples/fetch-event/main.go index 3459ca6..8b1e827 100644 --- a/_examples/fetch-event/main.go +++ b/_examples/fetch-event/main.go @@ -12,12 +12,10 @@ import ( ) func handler(w http.ResponseWriter, req *http.Request) { - ctx := req.Context() - - cloudflare.PassThroughOnException(ctx) + cloudflare.PassThroughOnException() // logging after responding - cloudflare.WaitUntil(ctx, func() { + cloudflare.WaitUntil(func() { for i := 0; i < 5; i++ { time.Sleep(time.Second) } diff --git a/_examples/kv-counter/main.go b/_examples/kv-counter/main.go index b3af357..643926a 100644 --- a/_examples/kv-counter/main.go +++ b/_examples/kv-counter/main.go @@ -31,7 +31,7 @@ func main() { } // initialize KV namespace instance - kv, err := cloudflare.NewKVNamespace(req.Context(), counterNamespace) + kv, err := cloudflare.NewKVNamespace(counterNamespace) if err != nil { fmt.Fprintf(os.Stderr, "failed to init KV: %v", err) os.Exit(1) diff --git a/_examples/mysql-blog-server/app/handler.go b/_examples/mysql-blog-server/app/handler.go index aa4228a..132ddc1 100644 --- a/_examples/mysql-blog-server/app/handler.go +++ b/_examples/mysql-blog-server/app/handler.go @@ -27,13 +27,13 @@ func NewArticleHandler() http.Handler { func (h *articleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // initialize DB. - mysql.RegisterDialContext("tcp", func(_ context.Context, addr string) (net.Conn, error) { - return sockets.Connect(req.Context(), addr, &sockets.SocketOptions{ + mysql.RegisterDialContext("tcp", func(ctx context.Context, addr string) (net.Conn, error) { + return sockets.Connect(ctx, addr, &sockets.SocketOptions{ SecureTransport: sockets.SecureTransportOff, }) }) db, err := sql.Open("mysql", - cloudflare.Getenv(req.Context(), "MYSQL_DSN")) + cloudflare.Getenv("MYSQL_DSN")) if err != nil { log.Fatalf("failed to connect: %v", err) } diff --git a/_examples/r2-image-server/main.go b/_examples/r2-image-server/main.go index 9d4167c..60b5841 100644 --- a/_examples/r2-image-server/main.go +++ b/_examples/r2-image-server/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "io" "log" @@ -24,12 +23,12 @@ func handleErr(w http.ResponseWriter, msg string, err error) { type server struct{} -func (s *server) bucket(ctx context.Context) (*cloudflare.R2Bucket, error) { - return cloudflare.NewR2Bucket(ctx, bucketName) +func (s *server) bucket() (*cloudflare.R2Bucket, error) { + return cloudflare.NewR2Bucket(bucketName) } func (s *server) post(w http.ResponseWriter, req *http.Request, key string) { - bucket, err := s.bucket(req.Context()) + bucket, err := s.bucket() if err != nil { handleErr(w, "failed to initialize R2Bucket\n", err) return @@ -61,9 +60,9 @@ func (s *server) post(w http.ResponseWriter, req *http.Request, key string) { w.Write([]byte("successfully uploaded image")) } -func (s *server) get(w http.ResponseWriter, req *http.Request, key string) { +func (s *server) get(w http.ResponseWriter, key string) { // get image object from R2 - bucket, err := s.bucket(req.Context()) + bucket, err := s.bucket() if err != nil { handleErr(w, "failed to initialize R2Bucket\n", err) return @@ -88,9 +87,9 @@ func (s *server) get(w http.ResponseWriter, req *http.Request, key string) { io.Copy(w, imgObj.Body) } -func (s *server) delete(w http.ResponseWriter, req *http.Request, key string) { +func (s *server) delete(w http.ResponseWriter, key string) { // delete image object from R2 - bucket, err := s.bucket(req.Context()) + bucket, err := s.bucket() if err != nil { handleErr(w, "failed to initialize R2Bucket\n", err) return @@ -107,10 +106,10 @@ func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) { key := strings.TrimPrefix(req.URL.Path, "/") switch req.Method { case "GET": - s.get(w, req, key) + s.get(w, key) return case "DELETE": - s.delete(w, req, key) + s.delete(w, key) return case "POST": s.post(w, req, key) diff --git a/_examples/r2-image-viewer/main.go b/_examples/r2-image-viewer/main.go index cc897ab..924281a 100644 --- a/_examples/r2-image-viewer/main.go +++ b/_examples/r2-image-viewer/main.go @@ -23,7 +23,7 @@ func handleErr(w http.ResponseWriter, msg string, err error) { // This example is based on implementation in syumai/workers-playground // - https://github.com/syumai/workers-playground/blob/e32881648ccc055e3690a0d9c750a834261c333e/r2-image-viewer/src/index.ts#L30 func handler(w http.ResponseWriter, req *http.Request) { - bucket, err := cloudflare.NewR2Bucket(req.Context(), bucketName) + bucket, err := cloudflare.NewR2Bucket(bucketName) if err != nil { handleErr(w, "failed to get R2Bucket\n", err) return diff --git a/_examples/service-bindings/go.mod b/_examples/service-bindings/go.mod index 6ec9028..f055062 100644 --- a/_examples/service-bindings/go.mod +++ b/_examples/service-bindings/go.mod @@ -1,4 +1,4 @@ -module github.com/syumai/workers/_examples/fetch +module github.com/syumai/workers/_examples/service-bindings go 1.21.3 diff --git a/_examples/service-bindings/main.go b/_examples/service-bindings/main.go index ab9a23a..22de8d2 100644 --- a/_examples/service-bindings/main.go +++ b/_examples/service-bindings/main.go @@ -12,8 +12,7 @@ import ( func main() { handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - ctx := req.Context() - bind := cloudflare.GetBinding(ctx, "hello") + bind := cloudflare.GetBinding("hello") fc := fetch.NewClient(fetch.WithBinding(bind)) hc := fc.HTTPClient(fetch.RedirectModeFollow) From c19126a1cef9c0978a43297e6320ddccc5a5adc6 Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:59:25 +0900 Subject: [PATCH 5/5] fix test --- exp/hono/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exp/hono/context.go b/exp/hono/context.go index f9ccdcf..81ce831 100644 --- a/exp/hono/context.go +++ b/exp/hono/context.go @@ -26,7 +26,7 @@ func newContext(ctxObj js.Value) *Context { if err != nil { panic(err) } - ctx := runtimecontext.New(context.Background(), reqObj, jsutil.RuntimeContext) + ctx := runtimecontext.New(context.Background(), reqObj) req = req.WithContext(ctx) return req }),