@lunora/container 1.0.0-alpha.11 → 1.0.0-alpha.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +201 -104
- package/README.md +32 -10
- package/dist/bridge.d.mts +58 -33
- package/dist/bridge.d.ts +58 -33
- package/dist/bridge.mjs +1 -79
- package/dist/do/index.d.mts +362 -78
- package/dist/do/index.d.ts +362 -78
- package/dist/do/index.mjs +1 -321
- package/dist/index.d.mts +590 -151
- package/dist/index.d.ts +590 -151
- package/dist/index.mjs +1 -2
- package/dist/otel.d.mts +146 -47
- package/dist/otel.d.ts +146 -47
- package/dist/otel.mjs +1 -220
- package/dist/packem_shared/CONTAINER_EXEC_PATH-7Hcd6-zs.mjs +1 -0
- package/dist/packem_shared/CUSTOM_INSTANCE_MIN_MEMORY_MIB_PER_VCPU-D8Hy9Rxv.mjs +1 -0
- package/dist/packem_shared/ContainerProxy-CfXjS6ZI.mjs +27 -0
- package/dist/packem_shared/DURABLE_OBJECT_POLICY_FORBIDDEN_KEYS-gYi8RBUO.mjs +1 -0
- package/dist/packem_shared/LunoraSandboxContainer-DPnhJUu1.mjs +1 -0
- package/dist/packem_shared/abort-deadline-jwbW9Ala.mjs +1 -0
- package/dist/packem_shared/createContainerContext-p6QxWjsm.mjs +1 -0
- package/dist/packem_shared/exec-BYLW70sR.mjs +1 -0
- package/dist/packem_shared/jurisdiction-DtE9s70w.mjs +1 -0
- package/dist/packem_shared/jurisdiction.d-BwpauCoI.d.mts +471 -0
- package/dist/packem_shared/jurisdiction.d-BwpauCoI.d.ts +471 -0
- package/dist/packem_shared/sampling-D21rpu_s.mjs +1 -0
- package/dist/packem_shared/sandbox-types.d-DXlHnsEg.d.mts +110 -0
- package/dist/packem_shared/sandbox-types.d-DXlHnsEg.d.ts +110 -0
- package/dist/sandbox/index.d.mts +66 -0
- package/dist/sandbox/index.d.ts +66 -0
- package/dist/sandbox/index.mjs +1 -0
- package/package.json +9 -3
- package/dist/packem_shared/ContainerProxy-DWqUX_re.mjs +0 -1474
- package/dist/packem_shared/containerBindingName-BiTrAF1J.mjs +0 -224
- package/dist/packem_shared/createContainerContext-Cg53QGdf.mjs +0 -223
- package/dist/packem_shared/jurisdiction-CuPNcLDt.mjs +0 -13
- package/dist/packem_shared/jurisdiction.d-TwTGkgTg.d.mts +0 -266
- package/dist/packem_shared/jurisdiction.d-TwTGkgTg.d.ts +0 -266
package/dist/bridge.d.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@lunora/container/bridge` — the container→Lunora client.
|
|
3
|
+
*
|
|
4
|
+
* Container code (any JS runtime — Node, Bun, Deno) uses this to call back
|
|
5
|
+
* into the app's Lunora functions over the Worker's HTTP RPC endpoint
|
|
6
|
+
* (`POST /_lunora/rpc`), so a container can read/write app state through the
|
|
7
|
+
* same queries/mutations/actions the browser uses instead of reaching into a
|
|
8
|
+
* database directly.
|
|
9
|
+
*
|
|
10
|
+
* Pure `fetch` over the documented wire contract — no Cloudflare imports — so
|
|
11
|
+
* it runs in any container and is unit-testable with an injected `fetch`. The
|
|
12
|
+
* transport is the public RPC endpoint authenticated with a bearer token your
|
|
13
|
+
* Worker's `resolveIdentity` recognizes (forward it as a container `secret`).
|
|
14
|
+
* Non-JS containers can implement the same one-line contract directly.
|
|
15
|
+
*/
|
|
1
16
|
import { LunoraError } from '@lunora/errors';
|
|
2
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* A `fetch` implementation — defaults to the runtime global.
|
|
19
|
+
*/
|
|
3
20
|
type FetchLike = (input: string, init: {
|
|
4
21
|
body: string;
|
|
5
22
|
headers: Record<string, string>;
|
|
@@ -10,33 +27,38 @@ type FetchLike = (input: string, init: {
|
|
|
10
27
|
status: number;
|
|
11
28
|
statusText?: string;
|
|
12
29
|
}>;
|
|
30
|
+
/**
|
|
31
|
+
* `ContainerBridgeOptions` is part of the experimental `@lunora/container` API and may change without a major version bump.
|
|
32
|
+
*/
|
|
13
33
|
interface ContainerBridgeOptions {
|
|
14
34
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
35
|
+
* Base URL of the deployed Lunora Worker (no trailing `/_lunora/rpc`), e.g.
|
|
36
|
+
* `https://my-app.workers.dev`. In a Lunora container, surface it as an
|
|
37
|
+
* `env` value on the definition.
|
|
38
|
+
*/
|
|
19
39
|
baseUrl: string;
|
|
20
40
|
/** Injectable `fetch` (tests / non-global runtimes). Defaults to `globalThis.fetch`. */
|
|
21
41
|
fetch?: FetchLike;
|
|
22
42
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
43
|
+
* Bearer token sent as `Authorization: Bearer <token>`. Your Worker's
|
|
44
|
+
* `resolveIdentity` maps it to the identity the called functions run as.
|
|
45
|
+
* Pass it to the container as a `secret`, never bake it into the image.
|
|
46
|
+
*/
|
|
27
47
|
token?: string;
|
|
28
48
|
}
|
|
29
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Thrown when a Lunora function returns an error envelope. A `LunoraError` subclass carrying the wire `code`.
|
|
51
|
+
*/
|
|
30
52
|
declare class ContainerBridgeError extends LunoraError {
|
|
31
53
|
constructor(code: string, message: string);
|
|
32
54
|
}
|
|
33
55
|
/**
|
|
34
|
-
* Structural mirror of `@lunora/client`'s `FunctionReference` — the typed
|
|
35
|
-
* handle the generated `_generated/api` object carries. Declared locally (not
|
|
36
|
-
* imported) so the bridge stays dependency-free and its `.d.ts` is
|
|
37
|
-
* self-contained; the `__lunoraPhantom` shape matches, so a real `api.x.y`
|
|
38
|
-
* reference is assignable and its arg/return types are inferable.
|
|
39
|
-
*/
|
|
56
|
+
* Structural mirror of `@lunora/client`'s `FunctionReference` — the typed
|
|
57
|
+
* handle the generated `_generated/api` object carries. Declared locally (not
|
|
58
|
+
* imported) so the bridge stays dependency-free and its `.d.ts` is
|
|
59
|
+
* self-contained; the `__lunoraPhantom` shape matches, so a real `api.x.y`
|
|
60
|
+
* reference is assignable and its arg/return types are inferable.
|
|
61
|
+
*/
|
|
40
62
|
interface BridgeFunctionReference<Args = unknown, Result = unknown> {
|
|
41
63
|
readonly __lunoraPhantom?: {
|
|
42
64
|
args: Args;
|
|
@@ -56,6 +78,9 @@ type ResultOfReference<Reference> = Reference extends {
|
|
|
56
78
|
returns: infer Result;
|
|
57
79
|
};
|
|
58
80
|
} ? Result : never;
|
|
81
|
+
/**
|
|
82
|
+
* `ContainerBridge` is part of the experimental `@lunora/container` API and may change without a major version bump.
|
|
83
|
+
*/
|
|
59
84
|
interface ContainerBridge {
|
|
60
85
|
/** Call an `action` by `namespace:fn` path. Alias of {@link ContainerBridge.call} for intent. */
|
|
61
86
|
action: <Result = unknown>(functionPath: string, args?: Record<string, unknown>, shardKey?: string) => Promise<Result>;
|
|
@@ -66,25 +91,25 @@ interface ContainerBridge {
|
|
|
66
91
|
/** Call a `query` by `namespace:fn` path. Alias of {@link ContainerBridge.call} for intent. */
|
|
67
92
|
query: <Result = unknown>(functionPath: string, args?: Record<string, unknown>, shardKey?: string) => Promise<Result>;
|
|
68
93
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
94
|
+
* Fully-typed call via a generated function reference. Pass a reference from
|
|
95
|
+
* the project's `_generated/api` (e.g. `api.messages.list`) and the args +
|
|
96
|
+
* result are inferred from it — the typed counterpart to {@link ContainerBridge.call}
|
|
97
|
+
* for JS/TS containers that can import the generated `api`.
|
|
98
|
+
*/
|
|
74
99
|
run: <Reference extends BridgeFunctionReference>(reference: Reference, args: ArgsOfReference<Reference>, shardKey?: string) => Promise<ResultOfReference<Reference>>;
|
|
75
100
|
}
|
|
76
101
|
/**
|
|
77
|
-
* Build a container→Lunora bridge bound to a Worker URL + token.
|
|
78
|
-
*
|
|
79
|
-
* ```ts
|
|
80
|
-
* const lunora = createContainerBridge({ baseUrl: process.env.LUNORA_URL!, token: process.env.LUNORA_TOKEN });
|
|
81
|
-
* const messages = await lunora.query("messages:list", { limit: 20 });
|
|
82
|
-
* await lunora.mutation("messages:markProcessed", { id });
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* `query`/`mutation`/`action` are intent-revealing aliases of one `call` — the
|
|
86
|
-
* wire is identical and the server dispatches by the function's registered
|
|
87
|
-
* kind, so a query path called via `.mutation(...)` still runs as a query.
|
|
88
|
-
*/
|
|
102
|
+
* Build a container→Lunora bridge bound to a Worker URL + token.
|
|
103
|
+
*
|
|
104
|
+
* ```ts
|
|
105
|
+
* const lunora = createContainerBridge({ baseUrl: process.env.LUNORA_URL!, token: process.env.LUNORA_TOKEN });
|
|
106
|
+
* const messages = await lunora.query("messages:list", { limit: 20 });
|
|
107
|
+
* await lunora.mutation("messages:markProcessed", { id });
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* `query`/`mutation`/`action` are intent-revealing aliases of one `call` — the
|
|
111
|
+
* wire is identical and the server dispatches by the function's registered
|
|
112
|
+
* kind, so a query path called via `.mutation(...)` still runs as a query.
|
|
113
|
+
*/
|
|
89
114
|
declare const createContainerBridge: (options: ContainerBridgeOptions) => ContainerBridge;
|
|
90
115
|
export { type BridgeFunctionReference, type ContainerBridge, ContainerBridgeError, type ContainerBridgeOptions, type FetchLike, createContainerBridge };
|
package/dist/bridge.mjs
CHANGED
|
@@ -1,79 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
const RPC_PATH = "/_lunora/rpc";
|
|
4
|
-
class ContainerBridgeError extends LunoraError {
|
|
5
|
-
constructor(code, message) {
|
|
6
|
-
super(code, message, { name: "ContainerBridgeError" });
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
const joinUrl = (baseUrl, path) => {
|
|
10
|
-
let base = baseUrl;
|
|
11
|
-
while (base.endsWith("/")) {
|
|
12
|
-
base = base.slice(0, -1);
|
|
13
|
-
}
|
|
14
|
-
return `${base}${path}`;
|
|
15
|
-
};
|
|
16
|
-
const statusError = (functionPath, response) => new Error(
|
|
17
|
-
`createContainerBridge: request to "${functionPath}" failed (status ${String(response.status)}${response.statusText ? ` ${response.statusText}` : ""})`
|
|
18
|
-
);
|
|
19
|
-
const parseResponseBody = async (response, functionPath) => {
|
|
20
|
-
try {
|
|
21
|
-
return await response.json();
|
|
22
|
-
} catch {
|
|
23
|
-
if (!response.ok) {
|
|
24
|
-
throw statusError(functionPath, response);
|
|
25
|
-
}
|
|
26
|
-
throw new LunoraError(
|
|
27
|
-
"INTERNAL",
|
|
28
|
-
`createContainerBridge: request to "${functionPath}" returned a non-JSON response (status ${String(response.status)})`
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const createContainerBridge = (options) => {
|
|
33
|
-
if (typeof options.baseUrl !== "string" || options.baseUrl.length === 0) {
|
|
34
|
-
throw new TypeError("createContainerBridge: `baseUrl` must be a non-empty Worker URL (e.g. https://my-app.workers.dev) — is the URL env var set?");
|
|
35
|
-
}
|
|
36
|
-
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
37
|
-
const call = async (functionPath, args = {}, shardKey) => {
|
|
38
|
-
if (typeof fetchImpl !== "function") {
|
|
39
|
-
throw new TypeError("createContainerBridge: no `fetch` available — pass `fetch` in options for this runtime.");
|
|
40
|
-
}
|
|
41
|
-
const headers = { "content-type": "application/json" };
|
|
42
|
-
if (options.token !== void 0) {
|
|
43
|
-
headers.authorization = `Bearer ${options.token}`;
|
|
44
|
-
}
|
|
45
|
-
const response = await fetchImpl(joinUrl(options.baseUrl, RPC_PATH), {
|
|
46
|
-
body: JSON.stringify({ args, functionPath, shardKey }),
|
|
47
|
-
headers,
|
|
48
|
-
method: "POST"
|
|
49
|
-
});
|
|
50
|
-
const body = await parseResponseBody(response, functionPath);
|
|
51
|
-
if (typeof body === "object" && body !== null && "error" in body) {
|
|
52
|
-
const { error } = body;
|
|
53
|
-
if (typeof error === "object" && error !== null) {
|
|
54
|
-
const { code, message } = error;
|
|
55
|
-
if (typeof code === "string" && typeof message === "string") {
|
|
56
|
-
throw new ContainerBridgeError(code, message);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
let detail;
|
|
60
|
-
try {
|
|
61
|
-
detail = JSON.stringify(error);
|
|
62
|
-
} catch {
|
|
63
|
-
detail = String(error);
|
|
64
|
-
}
|
|
65
|
-
throw new LunoraError(
|
|
66
|
-
"INTERNAL",
|
|
67
|
-
`createContainerBridge: request to "${functionPath}" returned a malformed error envelope (status ${String(response.status)}): ${detail}`
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
if (!response.ok) {
|
|
71
|
-
throw statusError(functionPath, response);
|
|
72
|
-
}
|
|
73
|
-
return body.result;
|
|
74
|
-
};
|
|
75
|
-
const run = async (reference, args, shardKey) => call(reference.__lunoraRef, args, shardKey);
|
|
76
|
-
return { action: call, call, mutation: call, query: call, run };
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export { ContainerBridgeError, createContainerBridge };
|
|
1
|
+
import{LunoraError as u}from"@lunora/errors";import{encodeWire as h,decodeWire as p}from"@lunora/wire";const w="/_lunora/rpc";class m extends u{constructor(r,t){super(r,t,{name:"ContainerBridgeError"})}}const b=(e,r)=>{let t=e;for(;t.endsWith("/");)t=t.slice(0,-1);return`${t}${r}`},y=(e,r)=>new Error(`createContainerBridge: request to "${e}" failed (status ${String(r.status)}${r.statusText?` ${r.statusText}`:""})`),$=async(e,r)=>{try{return await e.json()}catch{throw e.ok?new u("INTERNAL",`createContainerBridge: request to "${r}" returned a non-JSON response (status ${String(e.status)})`):y(r,e)}},T=e=>{if(typeof e.baseUrl!="string"||e.baseUrl.length===0)throw new TypeError("createContainerBridge: `baseUrl` must be a non-empty Worker URL (e.g. https://my-app.workers.dev) — is the URL env var set?");const r=e.fetch??globalThis.fetch,t=async(o,i={},c)=>{if(typeof r!="function")throw new TypeError("createContainerBridge: no `fetch` available — pass `fetch` in options for this runtime.");const d={"content-type":"application/json"};e.token!==void 0&&(d.authorization=`Bearer ${e.token}`);const s=await r(b(e.baseUrl,w),{body:JSON.stringify({args:h(i),functionPath:o,shardKey:c}),headers:d,method:"POST"}),n=await $(s,o);if(typeof n=="object"&&n!==null&&"error"in n){const{error:a}=n;if(typeof a=="object"&&a!==null){const{code:f,message:g}=a;if(typeof f=="string"&&typeof g=="string")throw new m(f,g)}let l;try{l=JSON.stringify(a)}catch{l=String(a)}throw new u("INTERNAL",`createContainerBridge: request to "${o}" returned a malformed error envelope (status ${String(s.status)}): ${l}`)}if(!s.ok)throw y(o,s);return p(n.result)};return{action:t,call:t,mutation:t,query:t,run:async(o,i,c)=>t(o.__lunoraRef,i,c)}};export{m as ContainerBridgeError,T as createContainerBridge};
|