mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
commit
c8dfc5eedf
@ -2,6 +2,7 @@ package cfruntimecontext
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"syscall/js"
|
"syscall/js"
|
||||||
|
|
||||||
"github.com/syumai/workers/internal/runtimecontext"
|
"github.com/syumai/workers/internal/runtimecontext"
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
* type RuntimeContext {
|
* type RuntimeContext {
|
||||||
* env: Env;
|
* env: Env;
|
||||||
* ctx: ExecutionContext;
|
* ctx: ExecutionContext;
|
||||||
|
* ...
|
||||||
* }
|
* }
|
||||||
* ```
|
* ```
|
||||||
* This type is based on the type definition of ExportedHandlerFetchHandler.
|
* This type is based on the type definition of ExportedHandlerFetchHandler.
|
||||||
@ -33,3 +35,16 @@ func GetExecutionContext(ctx context.Context) js.Value {
|
|||||||
runtimeCtxValue := runtimecontext.MustExtract(ctx)
|
runtimeCtxValue := runtimecontext.MustExtract(ctx)
|
||||||
return runtimeCtxValue.Get("ctx")
|
return runtimeCtxValue.Get("ctx")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrValueNotFound = errors.New("execution context value for specified key not found")
|
||||||
|
|
||||||
|
// 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 := runtimecontext.MustExtract(ctx)
|
||||||
|
v := runtimeCtxValue.Get(key)
|
||||||
|
if v.IsUndefined() {
|
||||||
|
return js.Value{}, ErrValueNotFound
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import "./polyfill_performance.js";
|
import "./polyfill_performance.js";
|
||||||
import "./wasm_exec.js";
|
import "./wasm_exec.js";
|
||||||
|
import { connect } from 'cloudflare:sockets';
|
||||||
|
|
||||||
const go = new Go();
|
const go = new Go();
|
||||||
|
|
||||||
@ -18,19 +19,27 @@ async function run() {
|
|||||||
await readyPromise;
|
await readyPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createRuntimeContext(env, ctx) {
|
||||||
|
return {
|
||||||
|
env,
|
||||||
|
ctx,
|
||||||
|
connect,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetch(req, env, ctx) {
|
export async function fetch(req, env, ctx) {
|
||||||
await run();
|
await run();
|
||||||
return handleRequest(req, { env, ctx });
|
return handleRequest(req, createRuntimeContext(env, ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function scheduled(event, env, ctx) {
|
export async function scheduled(event, env, ctx) {
|
||||||
await run();
|
await run();
|
||||||
return runScheduler(event, { env, ctx });
|
return runScheduler(event, createRuntimeContext(env, ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
// onRequest handles request to Cloudflare Pages
|
// onRequest handles request to Cloudflare Pages
|
||||||
export async function onRequest(ctx) {
|
export async function onRequest(ctx) {
|
||||||
await run();
|
await run();
|
||||||
const { request, env } = ctx;
|
const { request, env } = ctx;
|
||||||
return handleRequest(request, { env, ctx });
|
return handleRequest(request, createRuntimeContext(env, ctx));
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user