remove dependencies on Context from cloudflare-related APIs

This commit is contained in:
syumai 2024-04-17 00:52:30 +09:00
parent 33e4ad8f50
commit 627ed95530
7 changed files with 17 additions and 22 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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