@lunora/container 1.0.0-alpha.4 → 1.0.0-alpha.41
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 +26 -0
- package/README.md +29 -3
- package/dist/bridge.d.mts +45 -35
- package/dist/bridge.d.ts +45 -35
- package/dist/bridge.mjs +1 -76
- package/dist/do/index.d.mts +562 -62
- package/dist/do/index.d.ts +562 -62
- package/dist/do/index.mjs +1 -256
- package/dist/index.d.mts +400 -136
- package/dist/index.d.ts +400 -136
- package/dist/index.mjs +1 -2
- package/dist/otel.d.mts +161 -0
- package/dist/otel.d.ts +161 -0
- package/dist/otel.mjs +1 -0
- package/dist/packem_shared/CONTAINER_EXEC_PATH-CAqlIhLq.mjs +1 -0
- package/dist/packem_shared/ContainerProxy-fTW_pXWY.mjs +27 -0
- package/dist/packem_shared/abort-deadline-jwbW9Ala.mjs +1 -0
- package/dist/packem_shared/containerBindingName-D01FopQt.mjs +1 -0
- package/dist/packem_shared/createContainerContext-IYvMMEfq.mjs +1 -0
- package/dist/packem_shared/exec-KlR4eMip.mjs +1 -0
- package/dist/packem_shared/jurisdiction-DtE9s70w.mjs +1 -0
- package/dist/packem_shared/jurisdiction.d-KcQu2ZT_.d.mts +269 -0
- package/dist/packem_shared/jurisdiction.d-KcQu2ZT_.d.ts +269 -0
- package/package.json +6 -2
- package/dist/packem_shared/containerBindingName-C7Lic2ET.mjs +0 -200
- package/dist/packem_shared/createContainerContext-DBp8oJOr.mjs +0 -158
- package/dist/packem_shared/types.d-DAOLRiZQ.d.mts +0 -234
- package/dist/packem_shared/types.d-DAOLRiZQ.d.ts +0 -234
package/LICENSE.md
CHANGED
|
@@ -103,3 +103,29 @@ Unless required by applicable law or agreed to in writing, software distributed
|
|
|
103
103
|
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
104
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
105
|
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
|
|
109
|
+
# Licenses of bundled dependencies
|
|
110
|
+
The published @lunora/container artifact additionally contains code with the following licenses:
|
|
111
|
+
MIT OR Apache-2.0
|
|
112
|
+
|
|
113
|
+
# Bundled dependencies:
|
|
114
|
+
## @cloudflare/containers
|
|
115
|
+
License: MIT OR Apache-2.0
|
|
116
|
+
Repository: git+https://github.com/cloudflare/containers.git
|
|
117
|
+
|
|
118
|
+
<!-- /DEPENDENCIES -->
|
|
119
|
+
|
|
120
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
121
|
+
|
|
122
|
+
# Licenses of bundled types
|
|
123
|
+
The published @lunora/container artifact additionally contains code with the following licenses:
|
|
124
|
+
MIT OR Apache-2.0
|
|
125
|
+
|
|
126
|
+
# Bundled types:
|
|
127
|
+
## @cloudflare/containers
|
|
128
|
+
License: MIT OR Apache-2.0
|
|
129
|
+
Repository: git+https://github.com/cloudflare/containers.git
|
|
130
|
+
|
|
131
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
<!-- END_PACKAGE_OG_IMAGE_PLACEHOLDER -->
|
|
12
12
|
|
|
13
|
+
> **Experimental** — this package is outside the Lunora 1.0 stability promise: its API may change in any release, without a major version bump.
|
|
14
|
+
|
|
13
15
|
<br />
|
|
14
16
|
|
|
15
17
|
<div align="center">
|
|
@@ -90,7 +92,9 @@ export const transcode = action.input({ videoId: v.id("videos") }).action(async
|
|
|
90
92
|
});
|
|
91
93
|
```
|
|
92
94
|
|
|
93
|
-
`
|
|
95
|
+
`.get()` and `.any()` retry the **same** instance through a cold start — when a request lands while Cloudflare is still provisioning (a `503` "no instance", `500` "Failed to start", `429`, or "not listening"), they back off and retry (default 3 attempts) so the provisioning race never reaches your handler. Genuine app `5xx`s pass straight through. Tune or disable per call with `.get(id, { attempts, backoffMs })` (a pre-built `Request` is sent once and not retried, since its body may not be replayable).
|
|
96
|
+
|
|
97
|
+
`ctx.containers` is action-only (container calls are external I/O, like `ctx.fetch`); `.get(name)` handles also expose `start`/`stop`/`destroy`/`getState` lifecycle control plus `renewActivityTimeout()` and `egress.*` (adjust the allow/deny lists at runtime). HTTP requests and WebSocket frames already keep a busy container awake automatically; `renewActivityTimeout()` is the escape hatch for non-HTTP/non-WS activity.
|
|
94
98
|
|
|
95
99
|
The config layer (`lunora dev` / `lunora deploy`) reconciles the wrangler `containers[]` entry, the `CONTAINER_*` Durable Object binding, and the SQLite-class migration automatically; `wrangler deploy` builds the Dockerfile with local Docker and pushes it to the Cloudflare Registry.
|
|
96
100
|
|
|
@@ -121,6 +125,18 @@ export const worker = defineContainer({
|
|
|
121
125
|
});
|
|
122
126
|
```
|
|
123
127
|
|
|
128
|
+
### Secrets and Secrets Store
|
|
129
|
+
|
|
130
|
+
`secrets` forwards plain Worker secrets into the container env; `secretsStore` maps a _container env-var name → Cloudflare [Secrets Store](https://developers.cloudflare.com/secrets-store/) binding name_ and resolves each with its async `.get()` at first start (memoised). A collision with `env`/`secrets` is rejected at authoring time; a missing binding fails the start — the same fail-closed stance as `secrets`. Like `env`/`secrets`, these injected values only apply to implicit starts or a bare `start()`; a per-instance `start({ envVars })` replaces the env set wholesale (and skips Secrets Store resolution entirely).
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
export const worker = defineContainer({
|
|
134
|
+
image: "./containers/worker",
|
|
135
|
+
secrets: ["TRANSCODER_API_KEY"], // plain Worker secret → same-named env var
|
|
136
|
+
secretsStore: { STRIPE_KEY: "STRIPE_SECRET" }, // env.STRIPE_SECRET.get() → STRIPE_KEY
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
124
140
|
### Egress firewall
|
|
125
141
|
|
|
126
142
|
Pair `enableInternet: false` with an `allowedHosts` allow-list (or layer a `deniedHosts` deny-list that overrides everything) to constrain a container's outbound traffic; `interceptHttps: true` extends the lists to TLS connections (the image must trust the Cloudflare CA). Codegen re-exports the `ContainerProxy` worker entrypoint the interception path needs automatically.
|
|
@@ -180,7 +196,7 @@ await lunora.mutation("jobs:markDone", { id: pending[0].id });
|
|
|
180
196
|
|
|
181
197
|
The token is a bearer your Worker's `resolveIdentity` recognizes — pass it to the container as a `secret`. Non-JS containers can `POST /_lunora/rpc` with `{ functionPath, args }` directly.
|
|
182
198
|
|
|
183
|
-
Secure the bridge in `resolveIdentity`: read `request.headers.get("authorization")`, strip the `Bearer ` prefix, and compare the token against a Worker secret (e.g. `env.LUNORA_CONTAINER_TOKEN`) you also forward to the container. Return a `{ userId }` identity only on a match and `null` otherwise — an unrecognised request then runs anonymously and is rejected by your functions' own authorization checks. See [Securing the bridge](https://lunora.sh/docs/
|
|
199
|
+
Secure the bridge in `resolveIdentity`: read `request.headers.get("authorization")`, strip the `Bearer ` prefix, and compare the token against a Worker secret (e.g. `env.LUNORA_CONTAINER_TOKEN`) you also forward to the container. Return a `{ userId }` identity only on a match and `null` otherwise — an unrecognised request then runs anonymously and is rejected by your functions' own authorization checks. See [Securing the bridge](https://lunora.sh/docs/packages/container#securing-the-bridge) for the full example.
|
|
184
200
|
|
|
185
201
|
### Entry points
|
|
186
202
|
|
|
@@ -188,7 +204,17 @@ Secure the bridge in `resolveIdentity`: read `request.headers.get("authorization
|
|
|
188
204
|
- `@lunora/container/do` — workerd-only: the `LunoraContainer` base class the generated DO classes extend (pulls in `@cloudflare/containers`).
|
|
189
205
|
- `@lunora/container/bridge` — runtime-agnostic: `createContainerBridge` for calling Lunora functions from inside a container.
|
|
190
206
|
|
|
191
|
-
> This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/
|
|
207
|
+
> This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/packages/container)**.
|
|
208
|
+
|
|
209
|
+
### Known platform limitations
|
|
210
|
+
|
|
211
|
+
Some constraints live in Cloudflare Containers itself (open issues on [`cloudflare/containers`](https://github.com/cloudflare/containers/issues)). Lunora papers over what it can — cold-start retry and WebSocket keep-alive — and surfaces the rest:
|
|
212
|
+
|
|
213
|
+
- **No autoscaling / location-aware routing** — pools are fixed-size and pick uniformly at random ([#226](https://github.com/cloudflare/containers/issues/226)).
|
|
214
|
+
- **Ephemeral disk; no FUSE / tmpfs / some `node:net` modes** — persist to [`@lunora/storage`](https://www.npmjs.com/package/@lunora/storage) (R2) ([#112](https://github.com/cloudflare/containers/issues/112), [#160](https://github.com/cloudflare/containers/issues/160), [#67](https://github.com/cloudflare/containers/issues/67)).
|
|
215
|
+
- **Egress interception is HTTP-first** — HTTPS needs `interceptHttps`; raw gRPC isn't interceptable yet ([#195](https://github.com/cloudflare/containers/issues/195)).
|
|
216
|
+
- **Long jobs can be terminated on rollout** — use `hardTimeout` and make work resumable ([#138](https://github.com/cloudflare/containers/issues/138)).
|
|
217
|
+
- **Local dev can't pull from the Cloudflare Registry** — build from a local Dockerfile ([#155](https://github.com/cloudflare/containers/issues/155)).
|
|
192
218
|
|
|
193
219
|
## Related
|
|
194
220
|
|
package/dist/bridge.d.mts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { LunoraError } from '@lunora/errors';
|
|
2
|
+
/**
|
|
3
|
+
* A `fetch` implementation — defaults to the runtime global.
|
|
4
|
+
*/
|
|
2
5
|
type FetchLike = (input: string, init: {
|
|
3
6
|
body: string;
|
|
4
7
|
headers: Record<string, string>;
|
|
@@ -9,34 +12,38 @@ type FetchLike = (input: string, init: {
|
|
|
9
12
|
status: number;
|
|
10
13
|
statusText?: string;
|
|
11
14
|
}>;
|
|
15
|
+
/**
|
|
16
|
+
* `ContainerBridgeOptions` is part of the experimental `@lunora/container` API and may change without a major version bump.
|
|
17
|
+
*/
|
|
12
18
|
interface ContainerBridgeOptions {
|
|
13
19
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
* Base URL of the deployed Lunora Worker (no trailing `/_lunora/rpc`), e.g.
|
|
21
|
+
* `https://my-app.workers.dev`. In a Lunora container, surface it as an
|
|
22
|
+
* `env` value on the definition.
|
|
23
|
+
*/
|
|
18
24
|
baseUrl: string;
|
|
19
25
|
/** Injectable `fetch` (tests / non-global runtimes). Defaults to `globalThis.fetch`. */
|
|
20
26
|
fetch?: FetchLike;
|
|
21
27
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
* Bearer token sent as `Authorization: Bearer <token>`. Your Worker's
|
|
29
|
+
* `resolveIdentity` maps it to the identity the called functions run as.
|
|
30
|
+
* Pass it to the container as a `secret`, never bake it into the image.
|
|
31
|
+
*/
|
|
26
32
|
token?: string;
|
|
27
33
|
}
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Thrown when a Lunora function returns an error envelope. A `LunoraError` subclass carrying the wire `code`.
|
|
36
|
+
*/
|
|
37
|
+
declare class ContainerBridgeError extends LunoraError {
|
|
31
38
|
constructor(code: string, message: string);
|
|
32
39
|
}
|
|
33
40
|
/**
|
|
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
|
-
*/
|
|
41
|
+
* Structural mirror of `@lunora/client`'s `FunctionReference` — the typed
|
|
42
|
+
* handle the generated `_generated/api` object carries. Declared locally (not
|
|
43
|
+
* imported) so the bridge stays dependency-free and its `.d.ts` is
|
|
44
|
+
* self-contained; the `__lunoraPhantom` shape matches, so a real `api.x.y`
|
|
45
|
+
* reference is assignable and its arg/return types are inferable.
|
|
46
|
+
*/
|
|
40
47
|
interface BridgeFunctionReference<Args = unknown, Result = unknown> {
|
|
41
48
|
readonly __lunoraPhantom?: {
|
|
42
49
|
args: Args;
|
|
@@ -56,6 +63,9 @@ type ResultOfReference<Reference> = Reference extends {
|
|
|
56
63
|
returns: infer Result;
|
|
57
64
|
};
|
|
58
65
|
} ? Result : never;
|
|
66
|
+
/**
|
|
67
|
+
* `ContainerBridge` is part of the experimental `@lunora/container` API and may change without a major version bump.
|
|
68
|
+
*/
|
|
59
69
|
interface ContainerBridge {
|
|
60
70
|
/** Call an `action` by `namespace:fn` path. Alias of {@link ContainerBridge.call} for intent. */
|
|
61
71
|
action: <Result = unknown>(functionPath: string, args?: Record<string, unknown>, shardKey?: string) => Promise<Result>;
|
|
@@ -66,25 +76,25 @@ interface ContainerBridge {
|
|
|
66
76
|
/** Call a `query` by `namespace:fn` path. Alias of {@link ContainerBridge.call} for intent. */
|
|
67
77
|
query: <Result = unknown>(functionPath: string, args?: Record<string, unknown>, shardKey?: string) => Promise<Result>;
|
|
68
78
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
* Fully-typed call via a generated function reference. Pass a reference from
|
|
80
|
+
* the project's `_generated/api` (e.g. `api.messages.list`) and the args +
|
|
81
|
+
* result are inferred from it — the typed counterpart to {@link ContainerBridge.call}
|
|
82
|
+
* for JS/TS containers that can import the generated `api`.
|
|
83
|
+
*/
|
|
74
84
|
run: <Reference extends BridgeFunctionReference>(reference: Reference, args: ArgsOfReference<Reference>, shardKey?: string) => Promise<ResultOfReference<Reference>>;
|
|
75
85
|
}
|
|
76
86
|
/**
|
|
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
|
-
*/
|
|
87
|
+
* Build a container→Lunora bridge bound to a Worker URL + token.
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* const lunora = createContainerBridge({ baseUrl: process.env.LUNORA_URL!, token: process.env.LUNORA_TOKEN });
|
|
91
|
+
* const messages = await lunora.query("messages:list", { limit: 20 });
|
|
92
|
+
* await lunora.mutation("messages:markProcessed", { id });
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* `query`/`mutation`/`action` are intent-revealing aliases of one `call` — the
|
|
96
|
+
* wire is identical and the server dispatches by the function's registered
|
|
97
|
+
* kind, so a query path called via `.mutation(...)` still runs as a query.
|
|
98
|
+
*/
|
|
89
99
|
declare const createContainerBridge: (options: ContainerBridgeOptions) => ContainerBridge;
|
|
90
100
|
export { type BridgeFunctionReference, type ContainerBridge, ContainerBridgeError, type ContainerBridgeOptions, type FetchLike, createContainerBridge };
|
package/dist/bridge.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { LunoraError } from '@lunora/errors';
|
|
2
|
+
/**
|
|
3
|
+
* A `fetch` implementation — defaults to the runtime global.
|
|
4
|
+
*/
|
|
2
5
|
type FetchLike = (input: string, init: {
|
|
3
6
|
body: string;
|
|
4
7
|
headers: Record<string, string>;
|
|
@@ -9,34 +12,38 @@ type FetchLike = (input: string, init: {
|
|
|
9
12
|
status: number;
|
|
10
13
|
statusText?: string;
|
|
11
14
|
}>;
|
|
15
|
+
/**
|
|
16
|
+
* `ContainerBridgeOptions` is part of the experimental `@lunora/container` API and may change without a major version bump.
|
|
17
|
+
*/
|
|
12
18
|
interface ContainerBridgeOptions {
|
|
13
19
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
* Base URL of the deployed Lunora Worker (no trailing `/_lunora/rpc`), e.g.
|
|
21
|
+
* `https://my-app.workers.dev`. In a Lunora container, surface it as an
|
|
22
|
+
* `env` value on the definition.
|
|
23
|
+
*/
|
|
18
24
|
baseUrl: string;
|
|
19
25
|
/** Injectable `fetch` (tests / non-global runtimes). Defaults to `globalThis.fetch`. */
|
|
20
26
|
fetch?: FetchLike;
|
|
21
27
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
* Bearer token sent as `Authorization: Bearer <token>`. Your Worker's
|
|
29
|
+
* `resolveIdentity` maps it to the identity the called functions run as.
|
|
30
|
+
* Pass it to the container as a `secret`, never bake it into the image.
|
|
31
|
+
*/
|
|
26
32
|
token?: string;
|
|
27
33
|
}
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Thrown when a Lunora function returns an error envelope. A `LunoraError` subclass carrying the wire `code`.
|
|
36
|
+
*/
|
|
37
|
+
declare class ContainerBridgeError extends LunoraError {
|
|
31
38
|
constructor(code: string, message: string);
|
|
32
39
|
}
|
|
33
40
|
/**
|
|
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
|
-
*/
|
|
41
|
+
* Structural mirror of `@lunora/client`'s `FunctionReference` — the typed
|
|
42
|
+
* handle the generated `_generated/api` object carries. Declared locally (not
|
|
43
|
+
* imported) so the bridge stays dependency-free and its `.d.ts` is
|
|
44
|
+
* self-contained; the `__lunoraPhantom` shape matches, so a real `api.x.y`
|
|
45
|
+
* reference is assignable and its arg/return types are inferable.
|
|
46
|
+
*/
|
|
40
47
|
interface BridgeFunctionReference<Args = unknown, Result = unknown> {
|
|
41
48
|
readonly __lunoraPhantom?: {
|
|
42
49
|
args: Args;
|
|
@@ -56,6 +63,9 @@ type ResultOfReference<Reference> = Reference extends {
|
|
|
56
63
|
returns: infer Result;
|
|
57
64
|
};
|
|
58
65
|
} ? Result : never;
|
|
66
|
+
/**
|
|
67
|
+
* `ContainerBridge` is part of the experimental `@lunora/container` API and may change without a major version bump.
|
|
68
|
+
*/
|
|
59
69
|
interface ContainerBridge {
|
|
60
70
|
/** Call an `action` by `namespace:fn` path. Alias of {@link ContainerBridge.call} for intent. */
|
|
61
71
|
action: <Result = unknown>(functionPath: string, args?: Record<string, unknown>, shardKey?: string) => Promise<Result>;
|
|
@@ -66,25 +76,25 @@ interface ContainerBridge {
|
|
|
66
76
|
/** Call a `query` by `namespace:fn` path. Alias of {@link ContainerBridge.call} for intent. */
|
|
67
77
|
query: <Result = unknown>(functionPath: string, args?: Record<string, unknown>, shardKey?: string) => Promise<Result>;
|
|
68
78
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
* Fully-typed call via a generated function reference. Pass a reference from
|
|
80
|
+
* the project's `_generated/api` (e.g. `api.messages.list`) and the args +
|
|
81
|
+
* result are inferred from it — the typed counterpart to {@link ContainerBridge.call}
|
|
82
|
+
* for JS/TS containers that can import the generated `api`.
|
|
83
|
+
*/
|
|
74
84
|
run: <Reference extends BridgeFunctionReference>(reference: Reference, args: ArgsOfReference<Reference>, shardKey?: string) => Promise<ResultOfReference<Reference>>;
|
|
75
85
|
}
|
|
76
86
|
/**
|
|
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
|
-
*/
|
|
87
|
+
* Build a container→Lunora bridge bound to a Worker URL + token.
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* const lunora = createContainerBridge({ baseUrl: process.env.LUNORA_URL!, token: process.env.LUNORA_TOKEN });
|
|
91
|
+
* const messages = await lunora.query("messages:list", { limit: 20 });
|
|
92
|
+
* await lunora.mutation("messages:markProcessed", { id });
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* `query`/`mutation`/`action` are intent-revealing aliases of one `call` — the
|
|
96
|
+
* wire is identical and the server dispatches by the function's registered
|
|
97
|
+
* kind, so a query path called via `.mutation(...)` still runs as a query.
|
|
98
|
+
*/
|
|
89
99
|
declare const createContainerBridge: (options: ContainerBridgeOptions) => ContainerBridge;
|
|
90
100
|
export { type BridgeFunctionReference, type ContainerBridge, ContainerBridgeError, type ContainerBridgeOptions, type FetchLike, createContainerBridge };
|
package/dist/bridge.mjs
CHANGED
|
@@ -1,76 +1 @@
|
|
|
1
|
-
const
|
|
2
|
-
class ContainerBridgeError extends Error {
|
|
3
|
-
code;
|
|
4
|
-
constructor(code, message) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = "ContainerBridgeError";
|
|
7
|
-
this.code = code;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
const joinUrl = (baseUrl, path) => {
|
|
11
|
-
let base = baseUrl;
|
|
12
|
-
while (base.endsWith("/")) {
|
|
13
|
-
base = base.slice(0, -1);
|
|
14
|
-
}
|
|
15
|
-
return `${base}${path}`;
|
|
16
|
-
};
|
|
17
|
-
const statusError = (functionPath, response) => new Error(
|
|
18
|
-
`createContainerBridge: request to "${functionPath}" failed (status ${String(response.status)}${response.statusText ? ` ${response.statusText}` : ""})`
|
|
19
|
-
);
|
|
20
|
-
const parseResponseBody = async (response, functionPath) => {
|
|
21
|
-
try {
|
|
22
|
-
return await response.json();
|
|
23
|
-
} catch {
|
|
24
|
-
if (!response.ok) {
|
|
25
|
-
throw statusError(functionPath, response);
|
|
26
|
-
}
|
|
27
|
-
throw new Error(`createContainerBridge: request to "${functionPath}" returned a non-JSON response (status ${String(response.status)})`);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
const createContainerBridge = (options) => {
|
|
31
|
-
if (typeof options.baseUrl !== "string" || options.baseUrl.length === 0) {
|
|
32
|
-
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?");
|
|
33
|
-
}
|
|
34
|
-
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
35
|
-
const call = async (functionPath, args = {}, shardKey) => {
|
|
36
|
-
if (typeof fetchImpl !== "function") {
|
|
37
|
-
throw new TypeError("createContainerBridge: no `fetch` available — pass `fetch` in options for this runtime.");
|
|
38
|
-
}
|
|
39
|
-
const headers = { "content-type": "application/json" };
|
|
40
|
-
if (options.token !== void 0) {
|
|
41
|
-
headers.authorization = `Bearer ${options.token}`;
|
|
42
|
-
}
|
|
43
|
-
const response = await fetchImpl(joinUrl(options.baseUrl, RPC_PATH), {
|
|
44
|
-
body: JSON.stringify({ args, functionPath, shardKey }),
|
|
45
|
-
headers,
|
|
46
|
-
method: "POST"
|
|
47
|
-
});
|
|
48
|
-
const body = await parseResponseBody(response, functionPath);
|
|
49
|
-
if (typeof body === "object" && body !== null && "error" in body) {
|
|
50
|
-
const { error } = body;
|
|
51
|
-
if (typeof error === "object" && error !== null) {
|
|
52
|
-
const { code, message } = error;
|
|
53
|
-
if (typeof code === "string" && typeof message === "string") {
|
|
54
|
-
throw new ContainerBridgeError(code, message);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
let detail;
|
|
58
|
-
try {
|
|
59
|
-
detail = JSON.stringify(error);
|
|
60
|
-
} catch {
|
|
61
|
-
detail = String(error);
|
|
62
|
-
}
|
|
63
|
-
throw new Error(
|
|
64
|
-
`createContainerBridge: request to "${functionPath}" returned a malformed error envelope (status ${String(response.status)}): ${detail}`
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
if (!response.ok) {
|
|
68
|
-
throw statusError(functionPath, response);
|
|
69
|
-
}
|
|
70
|
-
return body.result;
|
|
71
|
-
};
|
|
72
|
-
const run = async (reference, args, shardKey) => call(reference.__lunoraRef, args, shardKey);
|
|
73
|
-
return { action: call, call, mutation: call, query: call, run };
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export { ContainerBridgeError, createContainerBridge };
|
|
1
|
+
import{LunoraError as u}from"@lunora/errors";const h="/_lunora/rpc";class w extends u{constructor(r,e){super(r,e,{name:"ContainerBridgeError"})}}const p=(t,r)=>{let e=t;for(;e.endsWith("/");)e=e.slice(0,-1);return`${e}${r}`},g=(t,r)=>new Error(`createContainerBridge: request to "${t}" failed (status ${String(r.status)}${r.statusText?` ${r.statusText}`:""})`),m=async(t,r)=>{try{return await t.json()}catch{throw t.ok?new u("INTERNAL",`createContainerBridge: request to "${r}" returned a non-JSON response (status ${String(t.status)})`):g(r,t)}},B=t=>{if(typeof t.baseUrl!="string"||t.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=t.fetch??globalThis.fetch,e=async(n,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"};t.token!==void 0&&(d.authorization=`Bearer ${t.token}`);const s=await r(p(t.baseUrl,h),{body:JSON.stringify({args:i,functionPath:n,shardKey:c}),headers:d,method:"POST"}),o=await m(s,n);if(typeof o=="object"&&o!==null&&"error"in o){const{error:a}=o;if(typeof a=="object"&&a!==null){const{code:f,message:y}=a;if(typeof f=="string"&&typeof y=="string")throw new w(f,y)}let l;try{l=JSON.stringify(a)}catch{l=String(a)}throw new u("INTERNAL",`createContainerBridge: request to "${n}" returned a malformed error envelope (status ${String(s.status)}): ${l}`)}if(!s.ok)throw g(n,s);return o.result};return{action:e,call:e,mutation:e,query:e,run:async(n,i,c)=>e(n.__lunoraRef,i,c)}};export{w as ContainerBridgeError,B as createContainerBridge};
|