@madebyentropy/tnl 0.0.0-stage → 0.1.0
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 +28 -0
- package/NOTICE +5 -0
- package/THIRD_PARTY_LICENSES.txt +3297 -0
- package/dist/adapters/ioredis.d.ts +7 -0
- package/dist/adapters/ioredis.js +87 -0
- package/dist/adapters/node-postgres.d.ts +8 -0
- package/dist/adapters/node-postgres.js +41 -0
- package/dist/adapters/postgres-js.d.ts +7 -0
- package/dist/adapters/postgres-js.js +55 -0
- package/dist/bin/tnl.d.ts +2 -0
- package/dist/bin/tnl.js +16 -0
- package/dist/browser.d.ts +18 -0
- package/dist/browser.js +43 -0
- package/dist/bun.d.ts +9 -0
- package/dist/bun.js +6 -0
- package/dist/cloudflare.d.ts +9 -0
- package/dist/cloudflare.js +16 -0
- package/dist/config.d.ts +107 -0
- package/dist/config.gen.d.ts +1185 -0
- package/dist/config.gen.js +2 -0
- package/dist/config.js +3 -0
- package/dist/control.d.ts +5 -0
- package/dist/control.js +9 -0
- package/dist/durable-object.d.ts +51 -0
- package/dist/durable-object.js +374 -0
- package/dist/durable-object.worker.d.ts +5 -0
- package/dist/durable-object.worker.js +11 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +230 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +2 -0
- package/dist/internal/connector/cancellation.d.ts +7 -0
- package/dist/internal/connector/cancellation.js +30 -0
- package/dist/internal/connector/client.d.ts +62 -0
- package/dist/internal/connector/client.js +504 -0
- package/dist/internal/connector/client_factory.d.ts +6 -0
- package/dist/internal/connector/client_factory.js +123 -0
- package/dist/internal/connector/connection.d.ts +39 -0
- package/dist/internal/connector/connection.js +739 -0
- package/dist/internal/connector/connection_ipc.d.ts +12 -0
- package/dist/internal/connector/connection_ipc.js +198 -0
- package/dist/internal/connector/context.d.ts +28 -0
- package/dist/internal/connector/context.js +19 -0
- package/dist/internal/connector/contract.d.ts +392 -0
- package/dist/internal/connector/contract.js +56 -0
- package/dist/internal/connector/deployment_types.d.ts +27 -0
- package/dist/internal/connector/deployment_types.js +1 -0
- package/dist/internal/connector/driver_socket.d.ts +21 -0
- package/dist/internal/connector/driver_socket.js +129 -0
- package/dist/internal/connector/management.d.ts +29 -0
- package/dist/internal/connector/management.js +118 -0
- package/dist/internal/connector/native_operations.d.ts +16 -0
- package/dist/internal/connector/native_operations.js +30 -0
- package/dist/internal/connector/native_stream.d.ts +7 -0
- package/dist/internal/connector/native_stream.js +219 -0
- package/dist/internal/connector/operation_handles.d.ts +9 -0
- package/dist/internal/connector/operation_handles.js +18 -0
- package/dist/internal/connector/portable_operations.d.ts +13 -0
- package/dist/internal/connector/portable_operations.js +25 -0
- package/dist/internal/connector/snapshot.d.ts +183 -0
- package/dist/internal/connector/snapshot.js +47 -0
- package/dist/internal/connector/wasm_abi.d.ts +76 -0
- package/dist/internal/connector/wasm_abi.js +5 -0
- package/dist/internal/connector/wasm_client.d.ts +58 -0
- package/dist/internal/connector/wasm_client.js +545 -0
- package/dist/internal/connector/wasm_loader.d.ts +15 -0
- package/dist/internal/connector/wasm_loader.js +99 -0
- package/dist/internal/connector/wasm_stream.d.ts +4 -0
- package/dist/internal/connector/wasm_stream.js +177 -0
- package/dist/internal/connector/workload/contract.d.ts +54 -0
- package/dist/internal/connector/workload/contract.js +14 -0
- package/dist/internal/connector/workload/provider.d.ts +24 -0
- package/dist/internal/connector/workload/provider.js +130 -0
- package/dist/internal/enrollment_source.d.ts +4 -0
- package/dist/internal/enrollment_source.js +34 -0
- package/dist/internal/management/client/client.gen.d.ts +2 -0
- package/dist/internal/management/client/client.gen.js +216 -0
- package/dist/internal/management/client/index.d.ts +10 -0
- package/dist/internal/management/client/index.js +6 -0
- package/dist/internal/management/client/types.gen.d.ts +120 -0
- package/dist/internal/management/client/types.gen.js +2 -0
- package/dist/internal/management/client/utils.gen.d.ts +37 -0
- package/dist/internal/management/client/utils.gen.js +228 -0
- package/dist/internal/management/client.gen.d.ts +12 -0
- package/dist/internal/management/client.gen.js +3 -0
- package/dist/internal/management/core/auth.gen.d.ts +25 -0
- package/dist/internal/management/core/auth.gen.js +14 -0
- package/dist/internal/management/core/bodySerializer.gen.d.ts +25 -0
- package/dist/internal/management/core/bodySerializer.gen.js +57 -0
- package/dist/internal/management/core/params.gen.d.ts +43 -0
- package/dist/internal/management/core/params.gen.js +109 -0
- package/dist/internal/management/core/pathSerializer.gen.d.ts +33 -0
- package/dist/internal/management/core/pathSerializer.gen.js +106 -0
- package/dist/internal/management/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/internal/management/core/queryKeySerializer.gen.js +92 -0
- package/dist/internal/management/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/internal/management/core/serverSentEvents.gen.js +132 -0
- package/dist/internal/management/core/types.gen.d.ts +83 -0
- package/dist/internal/management/core/types.gen.js +2 -0
- package/dist/internal/management/core/utils.gen.d.ts +19 -0
- package/dist/internal/management/core/utils.gen.js +87 -0
- package/dist/internal/management/index.d.ts +2 -0
- package/dist/internal/management/index.js +2 -0
- package/dist/internal/management/sdk.gen.d.ts +73 -0
- package/dist/internal/management/sdk.gen.js +191 -0
- package/dist/internal/management/types.gen.d.ts +464 -0
- package/dist/internal/management/types.gen.js +2 -0
- package/dist/internal/management/zod.gen.d.ts +850 -0
- package/dist/internal/management/zod.gen.js +252 -0
- package/dist/internal/native/launcher.d.ts +8 -0
- package/dist/internal/native/launcher.js +75 -0
- package/dist/internal/native/native-targets.d.ts +9 -0
- package/dist/internal/native/native-targets.js +8 -0
- package/dist/internal/portable/cloudflare-publisher-client.d.ts +54 -0
- package/dist/internal/portable/cloudflare-publisher-client.js +91 -0
- package/dist/internal/portable/cloudflare-publisher.d.ts +19 -0
- package/dist/internal/portable/cloudflare-publisher.js +210 -0
- package/dist/internal/portable/durable-deployment.d.ts +10 -0
- package/dist/internal/portable/durable-deployment.js +22 -0
- package/dist/internal/portable/enrollment.d.ts +26 -0
- package/dist/internal/portable/enrollment.js +196 -0
- package/dist/internal/portable/publication.d.ts +54 -0
- package/dist/internal/portable/publication.js +304 -0
- package/dist/internal/portable/request.d.ts +1 -0
- package/dist/internal/portable/request.js +20 -0
- package/dist/internal/portable/storage-browser.d.ts +9 -0
- package/dist/internal/portable/storage-browser.js +115 -0
- package/dist/internal/portable/storage-durable-object.d.ts +11 -0
- package/dist/internal/portable/storage-durable-object.js +110 -0
- package/dist/internal/portable/storage-host.d.ts +8 -0
- package/dist/internal/portable/storage-host.js +142 -0
- package/dist/internal/portable/storage.d.ts +15 -0
- package/dist/internal/portable/storage.js +29 -0
- package/dist/internal/project/native_runtime.d.ts +24 -0
- package/dist/internal/project/native_runtime.js +23 -0
- package/dist/internal/project/runtime.d.ts +467 -0
- package/dist/internal/project/runtime.js +183 -0
- package/dist/internal/project/service_metadata.d.ts +3 -0
- package/dist/internal/project/service_metadata.js +12 -0
- package/dist/internal/publication_certificate.d.ts +28 -0
- package/dist/internal/publication_certificate.js +32 -0
- package/dist/internal/publication_run.d.ts +8 -0
- package/dist/internal/publication_run.js +19 -0
- package/dist/internal/publisher/app.d.ts +33 -0
- package/dist/internal/publisher/app.js +317 -0
- package/dist/internal/publisher/fetch.d.ts +6 -0
- package/dist/internal/publisher/fetch.js +135 -0
- package/dist/internal/publisher/fetch_types.d.ts +1 -0
- package/dist/internal/publisher/fetch_types.js +1 -0
- package/dist/internal/publisher/framework.d.ts +27 -0
- package/dist/internal/publisher/framework.js +82 -0
- package/dist/internal/publisher/local.d.ts +39 -0
- package/dist/internal/publisher/local.js +312 -0
- package/dist/internal/publisher/native_publisher.d.ts +25 -0
- package/dist/internal/publisher/native_publisher.js +228 -0
- package/dist/internal/publisher/port.d.ts +2 -0
- package/dist/internal/publisher/port.js +15 -0
- package/dist/internal/publisher/publication.d.ts +88 -0
- package/dist/internal/publisher/publication.js +627 -0
- package/dist/internal/publisher/register.d.ts +31 -0
- package/dist/internal/publisher/register.js +136 -0
- package/dist/internal/publisher/registration.d.ts +385 -0
- package/dist/internal/publisher/registration.js +223 -0
- package/dist/internal/publisher/status.d.ts +47 -0
- package/dist/internal/publisher/status.js +137 -0
- package/dist/internal/publisher/target.d.ts +6 -0
- package/dist/internal/publisher/target.js +44 -0
- package/dist/internal/runtime_resources.d.ts +38 -0
- package/dist/internal/runtime_resources.js +52 -0
- package/dist/internal/tracing.d.ts +10 -0
- package/dist/internal/tracing.js +78 -0
- package/dist/internal/validation.d.ts +3 -0
- package/dist/internal/validation.js +9 -0
- package/dist/internal/values.d.ts +27 -0
- package/dist/internal/values.js +54 -0
- package/dist/next.d.ts +12 -0
- package/dist/next.js +82 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.js +6 -0
- package/dist/project.d.ts +173 -0
- package/dist/project.js +1 -0
- package/dist/response.d.ts +1 -0
- package/dist/response.js +34 -0
- package/dist/third-party-licenses.txt +1537 -0
- package/dist/vite.d.ts +5 -0
- package/dist/vite.js +200 -0
- package/dist/wasm/build.json +16 -0
- package/dist/wasm/connector.wasm +0 -0
- package/dist/wasm/go-license.txt +27 -0
- package/dist/wasm/publisher.wasm +0 -0
- package/dist/wasm/third-party-licenses.txt +3297 -0
- package/dist/wasm/wasm_exec.d.ts +1 -0
- package/dist/wasm/wasm_exec.js +575 -0
- package/dist/workload.d.ts +11 -0
- package/dist/workload.js +4 -0
- package/package.json +159 -4
- package/readme.md +13 -0
- package/README.md +0 -3
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { TnlCleanupError, TnlError, safeTnlMessage } from "../../errors.js";
|
|
3
|
+
import { serializeRuntimePayload } from "../project/runtime.js";
|
|
4
|
+
import { parseValue } from "../validation.js";
|
|
5
|
+
import { prepareOptionsSchema, prepareService, reportFrameworkTarget, } from "./app.js";
|
|
6
|
+
const previewSchema = z.union([z.literal(false), z.enum(["auto", "required"])]);
|
|
7
|
+
const frameworkOptionsSchema = prepareOptionsSchema
|
|
8
|
+
.extend({
|
|
9
|
+
preview: previewSchema.exactOptional(),
|
|
10
|
+
})
|
|
11
|
+
.readonly();
|
|
12
|
+
export const localRuntimePayload = serializeRuntimePayload({ namespace: "", services: {} }, true);
|
|
13
|
+
export function frameworkOptions(options) {
|
|
14
|
+
const { preview = "auto", ...preparation } = parseValue(frameworkOptionsSchema, options, "sdk.configuration_invalid");
|
|
15
|
+
const override = process.env.TNL_PREVIEW;
|
|
16
|
+
return {
|
|
17
|
+
preview: override === undefined
|
|
18
|
+
? preview
|
|
19
|
+
: parseValue(previewSchema, override === "off" ? false : override, "sdk.configuration_invalid"),
|
|
20
|
+
preparation,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
// only expected preview availability failures are optional. invalid configuration,
|
|
24
|
+
// protocol mismatches, and programming errors must remain visible.
|
|
25
|
+
export async function frameworkOperation(preview, operation) {
|
|
26
|
+
if (preview === false)
|
|
27
|
+
return null;
|
|
28
|
+
const controller = new AbortController();
|
|
29
|
+
const timer = preview === "auto"
|
|
30
|
+
? setTimeout(() => controller.abort(new TnlError("sdk.dev_unavailable")), 3_000)
|
|
31
|
+
: undefined;
|
|
32
|
+
try {
|
|
33
|
+
return await operation(preview === "auto" ? controller.signal : undefined);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (preview !== "auto" ||
|
|
37
|
+
!(error instanceof TnlError) ||
|
|
38
|
+
![
|
|
39
|
+
"sdk.authentication_required",
|
|
40
|
+
"sdk.publisher_setup_required",
|
|
41
|
+
"sdk.dev_unavailable",
|
|
42
|
+
"sdk.registration_stale",
|
|
43
|
+
"sdk.owner_conflict",
|
|
44
|
+
"sdk.native_missing",
|
|
45
|
+
"sdk.native_unsupported",
|
|
46
|
+
"sdk.process_unavailable",
|
|
47
|
+
].includes(error.code))
|
|
48
|
+
throw error;
|
|
49
|
+
process.stderr.write(`tnl: preview unavailable [${error.code}]; continuing local development. ${safeTnlMessage(error)}\n`);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export async function prepareFramework(options, framework, target) {
|
|
57
|
+
const { preview, preparation } = frameworkOptions(options);
|
|
58
|
+
return frameworkOperation(preview, async (signal) => {
|
|
59
|
+
const prepared = await prepareService(preparation, framework, signal);
|
|
60
|
+
try {
|
|
61
|
+
if (target !== undefined)
|
|
62
|
+
await reportFrameworkTarget(prepared, target, signal);
|
|
63
|
+
return prepared;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
await closeFramework(prepared, error);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
export async function closeFramework(prepared, error) {
|
|
72
|
+
try {
|
|
73
|
+
await prepared.close(AbortSignal.timeout(3_000));
|
|
74
|
+
}
|
|
75
|
+
catch (cleanupError) {
|
|
76
|
+
// close also reports pending operation failures already observed by setup.
|
|
77
|
+
// the same error is not evidence that unregister failed independently.
|
|
78
|
+
if (cleanupError === error)
|
|
79
|
+
return;
|
|
80
|
+
throw new TnlCleanupError(error, cleanupError);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
declare const endpointSchema: z.ZodReadonly<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
3
|
+
version: z.ZodLiteral<1>;
|
|
4
|
+
url: z.ZodString;
|
|
5
|
+
capability: z.ZodString;
|
|
6
|
+
owner_id: z.ZodString;
|
|
7
|
+
scope: z.ZodLiteral<"app">;
|
|
8
|
+
service: z.ZodString;
|
|
9
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
10
|
+
version: z.ZodLiteral<1>;
|
|
11
|
+
url: z.ZodString;
|
|
12
|
+
capability: z.ZodString;
|
|
13
|
+
owner_id: z.ZodString;
|
|
14
|
+
scope: z.ZodLiteral<"invocation">;
|
|
15
|
+
invocation_id: z.core.$ZodBranded<z.ZodTemplateLiteral<`ivk_${string}`>, "InvocationID", "out">;
|
|
16
|
+
}, z.core.$strict>], "scope">>;
|
|
17
|
+
export type RuntimeEndpoint = z.output<typeof endpointSchema>;
|
|
18
|
+
declare const ownerEndpointSchema: z.ZodObject<{
|
|
19
|
+
version: z.ZodLiteral<1>;
|
|
20
|
+
url: z.ZodString;
|
|
21
|
+
capability: z.ZodString;
|
|
22
|
+
owner_id: z.ZodString;
|
|
23
|
+
scope: z.ZodLiteral<"owner">;
|
|
24
|
+
}, z.core.$strict>;
|
|
25
|
+
export type OwnerEndpoint = z.output<typeof ownerEndpointSchema>;
|
|
26
|
+
export declare function parseOwnerEndpoint(value: unknown): OwnerEndpoint;
|
|
27
|
+
export declare function publisherInvocationEndpoint(owner: OwnerEndpoint, invocationID: string, signal?: AbortSignal): Promise<RuntimeEndpoint>;
|
|
28
|
+
export interface EndpointSelection {
|
|
29
|
+
readonly service?: string | undefined;
|
|
30
|
+
readonly invocationID?: string | undefined;
|
|
31
|
+
readonly stateDir?: string;
|
|
32
|
+
readonly publications?: boolean;
|
|
33
|
+
readonly environment?: NodeJS.ProcessEnv;
|
|
34
|
+
}
|
|
35
|
+
export declare function parseEndpoint(value: unknown, selection?: EndpointSelection): RuntimeEndpoint;
|
|
36
|
+
export declare function startRuntime(directory: string, binary?: string, selection?: EndpointSelection, signal?: AbortSignal): Promise<RuntimeEndpoint>;
|
|
37
|
+
export declare function runtimeRequest(endpoint: RuntimeEndpoint, operation: string, body?: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
38
|
+
export declare function canceledRuntimeRequest(cause: unknown, signal: AbortSignal): boolean;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { lstat, open } from "node:fs/promises";
|
|
4
|
+
import * as http from "node:http";
|
|
5
|
+
import { isIP } from "node:net";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { promisify } from "node:util";
|
|
8
|
+
import { TnlError } from "../../errors.js";
|
|
9
|
+
import { resolveNativeBinary } from "../native/launcher.js";
|
|
10
|
+
import { launchEnvironment, traceHeaders, operation as traceOperation } from "../tracing.js";
|
|
11
|
+
import * as z from "zod";
|
|
12
|
+
import { parseValue } from "../validation.js";
|
|
13
|
+
import { digestSchema, invocationIDSchema, serviceNameSchema } from "../values.js";
|
|
14
|
+
const endpointFields = {
|
|
15
|
+
version: z.literal(1),
|
|
16
|
+
url: z
|
|
17
|
+
.string()
|
|
18
|
+
.max(128)
|
|
19
|
+
.refine((value) => {
|
|
20
|
+
const url = URL.parse(value);
|
|
21
|
+
if (url === null)
|
|
22
|
+
return false;
|
|
23
|
+
const hostname = url.hostname.replace(/^\[|\]$/g, "");
|
|
24
|
+
const port = value.match(/:([1-9][0-9]{0,4})$/)?.[1];
|
|
25
|
+
return (port !== undefined &&
|
|
26
|
+
Number(port) <= 65535 &&
|
|
27
|
+
value === `http://${url.hostname}:${port}` &&
|
|
28
|
+
((isIP(hostname) === 4 && hostname.startsWith("127.")) || hostname === "::1"));
|
|
29
|
+
}),
|
|
30
|
+
capability: digestSchema,
|
|
31
|
+
owner_id: z.string().regex(/^[a-f0-9]{32}$/),
|
|
32
|
+
};
|
|
33
|
+
const endpointSchema = z
|
|
34
|
+
.discriminatedUnion("scope", [
|
|
35
|
+
z.strictObject({ ...endpointFields, scope: z.literal("app"), service: serviceNameSchema }),
|
|
36
|
+
z.strictObject({
|
|
37
|
+
...endpointFields,
|
|
38
|
+
scope: z.literal("invocation"),
|
|
39
|
+
invocation_id: invocationIDSchema,
|
|
40
|
+
}),
|
|
41
|
+
])
|
|
42
|
+
.readonly();
|
|
43
|
+
const problemSchema = z.strictObject({ code: z.string(), message: z.string() });
|
|
44
|
+
const ownerEndpointSchema = z.strictObject({ ...endpointFields, scope: z.literal("owner") });
|
|
45
|
+
export function parseOwnerEndpoint(value) {
|
|
46
|
+
return parseValue(ownerEndpointSchema, value, "sdk.response_invalid");
|
|
47
|
+
}
|
|
48
|
+
export async function publisherInvocationEndpoint(owner, invocationID, signal) {
|
|
49
|
+
const value = await runtimeRequestUntraced(owner, "capabilities", { scope: "invocation", invocation_id: invocationID }, signal);
|
|
50
|
+
const endpoint = parseEndpoint(value, { invocationID });
|
|
51
|
+
if (endpoint.url !== owner.url || endpoint.owner_id !== owner.owner_id)
|
|
52
|
+
throw new TnlError("sdk.response_invalid");
|
|
53
|
+
return endpoint;
|
|
54
|
+
}
|
|
55
|
+
export function parseEndpoint(value, selection = {}) {
|
|
56
|
+
const endpoint = parseValue(endpointSchema, value, "sdk.response_invalid");
|
|
57
|
+
if (endpoint.scope === "app"
|
|
58
|
+
? selection.invocationID !== undefined ||
|
|
59
|
+
(selection.service !== undefined && selection.service !== endpoint.service)
|
|
60
|
+
: selection.service !== undefined || selection.invocationID !== endpoint.invocation_id) {
|
|
61
|
+
throw new TnlError("sdk.response_invalid");
|
|
62
|
+
}
|
|
63
|
+
return endpoint;
|
|
64
|
+
}
|
|
65
|
+
async function readEndpoint(file, selection) {
|
|
66
|
+
if (!path.isAbsolute(file) || file.includes("\0"))
|
|
67
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
68
|
+
const parent = await lstat(path.dirname(file));
|
|
69
|
+
if (!parent.isDirectory() || parent.mode & 0o077 || parent.uid !== process.getuid?.())
|
|
70
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
71
|
+
const handle = await open(file, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
72
|
+
try {
|
|
73
|
+
const info = await handle.stat();
|
|
74
|
+
if (!info.isFile() ||
|
|
75
|
+
info.mode & 0o077 ||
|
|
76
|
+
info.uid !== process.getuid?.() ||
|
|
77
|
+
info.size > 64 * 1024)
|
|
78
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
79
|
+
const buffer = Buffer.alloc(64 * 1024 + 1);
|
|
80
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
81
|
+
if (bytesRead > 64 * 1024)
|
|
82
|
+
throw new TnlError("sdk.response_invalid");
|
|
83
|
+
return parseEndpoint(JSON.parse(buffer.subarray(0, bytesRead).toString("utf8")), selection);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
await handle.close();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async function waitMountedEndpoint(file, selection, signal) {
|
|
90
|
+
const deadline = Date.now() + 30_000;
|
|
91
|
+
for (;;) {
|
|
92
|
+
signal?.throwIfAborted();
|
|
93
|
+
try {
|
|
94
|
+
const endpoint = await readEndpoint(file, selection);
|
|
95
|
+
await runtimeRequest(endpoint, "health", undefined, signal);
|
|
96
|
+
return endpoint;
|
|
97
|
+
}
|
|
98
|
+
catch (cause) {
|
|
99
|
+
const missing = typeof cause === "object" && cause !== null && "code" in cause && cause.code === "ENOENT";
|
|
100
|
+
if (!missing)
|
|
101
|
+
throw cause;
|
|
102
|
+
if (Date.now() >= deadline)
|
|
103
|
+
throw new TnlError("sdk.dev_unavailable");
|
|
104
|
+
await new Promise((resolve, reject) => {
|
|
105
|
+
const aborted = () => {
|
|
106
|
+
clearTimeout(timer);
|
|
107
|
+
reject(signal?.reason);
|
|
108
|
+
};
|
|
109
|
+
const timer = setTimeout(() => {
|
|
110
|
+
signal?.removeEventListener("abort", aborted);
|
|
111
|
+
resolve();
|
|
112
|
+
}, 200);
|
|
113
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
114
|
+
if (signal?.aborted)
|
|
115
|
+
aborted();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export async function startRuntime(directory, binary, selection = {}, signal) {
|
|
121
|
+
try {
|
|
122
|
+
signal?.throwIfAborted();
|
|
123
|
+
let endpoint;
|
|
124
|
+
const environment = selection.environment ?? process.env;
|
|
125
|
+
const mounted = selection.publications ? undefined : environment.TNL_APP_ENDPOINT;
|
|
126
|
+
if (mounted !== undefined) {
|
|
127
|
+
return await waitMountedEndpoint(mounted, selection, signal);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const args = ["--no-telemetry", "runtime", "endpoint", "--directory", directory];
|
|
131
|
+
const stateDir = selection.stateDir ?? environment.TNL_STATE_DIR;
|
|
132
|
+
if (stateDir !== undefined)
|
|
133
|
+
args.push("--state-dir", stateDir);
|
|
134
|
+
if (selection.service !== undefined)
|
|
135
|
+
args.push("--service", selection.service);
|
|
136
|
+
if (selection.invocationID !== undefined)
|
|
137
|
+
args.push("--invocation-id", selection.invocationID);
|
|
138
|
+
if (selection.publications)
|
|
139
|
+
args.push("--publications");
|
|
140
|
+
const launched = promisify(execFile)(binary ?? resolveNativeBinary(), args, {
|
|
141
|
+
timeout: 30_000,
|
|
142
|
+
maxBuffer: 64 * 1024,
|
|
143
|
+
encoding: "utf8",
|
|
144
|
+
env: launchEnvironment(environment),
|
|
145
|
+
...(signal === undefined ? {} : { signal }),
|
|
146
|
+
killSignal: "SIGKILL",
|
|
147
|
+
});
|
|
148
|
+
const exited = new Promise((resolve) => {
|
|
149
|
+
launched.child.once("close", () => resolve());
|
|
150
|
+
});
|
|
151
|
+
let stdout;
|
|
152
|
+
try {
|
|
153
|
+
({ stdout } = await launched);
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
await exited;
|
|
157
|
+
}
|
|
158
|
+
endpoint = parseEndpoint(JSON.parse(stdout), selection);
|
|
159
|
+
}
|
|
160
|
+
await runtimeRequest(endpoint, "health", undefined, signal);
|
|
161
|
+
return endpoint;
|
|
162
|
+
}
|
|
163
|
+
catch (cause) {
|
|
164
|
+
if (signal !== undefined && canceledRuntimeRequest(cause, signal))
|
|
165
|
+
throw signal.reason;
|
|
166
|
+
if (cause instanceof TnlError)
|
|
167
|
+
throw cause;
|
|
168
|
+
throw new TnlError("sdk.dev_unavailable");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
export function runtimeRequest(endpoint, operation, body, signal) {
|
|
172
|
+
return traceOperation("sdk.native.request", () => runtimeRequestUntraced(endpoint, operation, body, signal));
|
|
173
|
+
}
|
|
174
|
+
function runtimeRequestUntraced(endpoint, operation, body, signal) {
|
|
175
|
+
signal?.throwIfAborted();
|
|
176
|
+
if (endpoint.scope === "owner")
|
|
177
|
+
parseOwnerEndpoint(endpoint);
|
|
178
|
+
else
|
|
179
|
+
parseEndpoint(endpoint, endpoint.scope === "app"
|
|
180
|
+
? { service: endpoint.service }
|
|
181
|
+
: { invocationID: endpoint.invocation_id });
|
|
182
|
+
if (![
|
|
183
|
+
"health",
|
|
184
|
+
"status",
|
|
185
|
+
"prepare",
|
|
186
|
+
"register",
|
|
187
|
+
"renew",
|
|
188
|
+
"unregister",
|
|
189
|
+
"publications/register",
|
|
190
|
+
"publications/certificate",
|
|
191
|
+
"capabilities",
|
|
192
|
+
].includes(operation))
|
|
193
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
194
|
+
if ((operation === "capabilities") !== (endpoint.scope === "owner"))
|
|
195
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
196
|
+
const get = operation === "health" || operation === "status";
|
|
197
|
+
const returnsJSON = [
|
|
198
|
+
"prepare",
|
|
199
|
+
"status",
|
|
200
|
+
"publications/register",
|
|
201
|
+
"publications/certificate",
|
|
202
|
+
"capabilities",
|
|
203
|
+
].includes(operation);
|
|
204
|
+
const payload = get ? undefined : JSON.stringify(body);
|
|
205
|
+
if ((get && body !== undefined) ||
|
|
206
|
+
(payload !== undefined &&
|
|
207
|
+
Buffer.byteLength(payload) >
|
|
208
|
+
(operation === "publications/register" || operation === "renew" ? 256 : 64) * 1024))
|
|
209
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
210
|
+
return new Promise((resolve, reject) => {
|
|
211
|
+
const request = http.request(endpoint.url + `/v1/${operation}`, {
|
|
212
|
+
method: get ? "GET" : "POST",
|
|
213
|
+
agent: false,
|
|
214
|
+
...(signal === undefined ? {} : { signal }),
|
|
215
|
+
headers: {
|
|
216
|
+
...traceHeaders(),
|
|
217
|
+
Host: endpoint.url.slice("http://".length),
|
|
218
|
+
Authorization: `Bearer ${endpoint.capability}`,
|
|
219
|
+
...(payload === undefined
|
|
220
|
+
? {}
|
|
221
|
+
: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(payload) }),
|
|
222
|
+
},
|
|
223
|
+
}, (response) => {
|
|
224
|
+
const chunks = [];
|
|
225
|
+
let length = 0;
|
|
226
|
+
response.on("data", (chunk) => {
|
|
227
|
+
length += chunk.length;
|
|
228
|
+
if (length > (operation === "publications/certificate" ? 256 : 64) * 1024)
|
|
229
|
+
response.destroy(new TnlError("sdk.response_invalid"));
|
|
230
|
+
else
|
|
231
|
+
chunks.push(chunk);
|
|
232
|
+
});
|
|
233
|
+
response.on("error", (cause) => reject(cause instanceof TnlError ? cause : new TnlError("sdk.dev_unavailable")));
|
|
234
|
+
response.on("end", () => {
|
|
235
|
+
try {
|
|
236
|
+
if (response.statusCode === 401 &&
|
|
237
|
+
response.headers["content-type"]?.split(";", 1)[0] !== "application/json")
|
|
238
|
+
throw new TnlError("sdk.registration_stale");
|
|
239
|
+
if (response.statusCode === 204) {
|
|
240
|
+
if (length !== 0 || returnsJSON)
|
|
241
|
+
throw new TnlError("sdk.response_invalid");
|
|
242
|
+
resolve(undefined);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (response.headers["content-type"]?.split(";", 1)[0] !== "application/json")
|
|
246
|
+
throw new TnlError("sdk.response_invalid");
|
|
247
|
+
const value = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
248
|
+
if (response.statusCode !== 200) {
|
|
249
|
+
const problem = parseValue(problemSchema, value, "sdk.response_invalid");
|
|
250
|
+
if (problem.code === "runtime.publication_cleanup_failed") {
|
|
251
|
+
if (operation !== "unregister" && operation !== "publications/register")
|
|
252
|
+
throw new TnlError("sdk.response_invalid");
|
|
253
|
+
throw new TnlError("sdk.publication_cleanup_failed");
|
|
254
|
+
}
|
|
255
|
+
if (problem.code === "runtime.certificate_unavailable")
|
|
256
|
+
throw new TnlError("sdk.certificate_unavailable");
|
|
257
|
+
if (problem.code === "runtime.certificate_invalid")
|
|
258
|
+
throw new TnlError("sdk.certificate_invalid");
|
|
259
|
+
throw new TnlError(problem.code === "runtime.owner_conflict"
|
|
260
|
+
? "sdk.owner_conflict"
|
|
261
|
+
: problem.code === "runtime.registration_stale" ||
|
|
262
|
+
problem.code === "runtime.unauthorized"
|
|
263
|
+
? "sdk.registration_stale"
|
|
264
|
+
: problem.code === "runtime.authentication_required" ||
|
|
265
|
+
problem.code === "TNL_CLIENT_AUTHENTICATION_REQUIRED" ||
|
|
266
|
+
problem.code === "TNL_CLIENT_AUTH_RECOVERY_REQUIRED"
|
|
267
|
+
? "sdk.authentication_required"
|
|
268
|
+
: problem.code === "runtime.publisher_setup_required"
|
|
269
|
+
? "sdk.publisher_setup_required"
|
|
270
|
+
: problem.code === "runtime.publisher_credential_file_invalid"
|
|
271
|
+
? "sdk.publisher_credential_file_invalid"
|
|
272
|
+
: problem.code === "TNL_CLIENT_SERVER_UNAVAILABLE" ||
|
|
273
|
+
(response.statusCode !== undefined && response.statusCode >= 500)
|
|
274
|
+
? "sdk.dev_unavailable"
|
|
275
|
+
: "sdk.request_rejected");
|
|
276
|
+
}
|
|
277
|
+
if (!returnsJSON)
|
|
278
|
+
throw new TnlError("sdk.response_invalid");
|
|
279
|
+
resolve(value);
|
|
280
|
+
}
|
|
281
|
+
catch (cause) {
|
|
282
|
+
reject(cause instanceof TnlError ? cause : new TnlError("sdk.response_invalid"));
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
response.on("close", () => {
|
|
286
|
+
if (!response.complete)
|
|
287
|
+
reject(new TnlError("sdk.dev_unavailable"));
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
const deadline = setTimeout(() => request.destroy(new TnlError("sdk.dev_unavailable")), operation === "unregister" ? 10_000 : 30_000);
|
|
291
|
+
deadline.unref();
|
|
292
|
+
request.once("close", () => clearTimeout(deadline));
|
|
293
|
+
request.on("error", (cause) => {
|
|
294
|
+
if (signal !== undefined && canceledRuntimeRequest(cause, signal))
|
|
295
|
+
reject(signal.reason);
|
|
296
|
+
else
|
|
297
|
+
reject(cause instanceof TnlError ? cause : new TnlError("sdk.dev_unavailable"));
|
|
298
|
+
});
|
|
299
|
+
request.end(payload);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
// only the exact stop reason or Node's standard abort wrapper is an expected
|
|
303
|
+
// cancellation. an independent failure may also have cancellation as its cause.
|
|
304
|
+
export function canceledRuntimeRequest(cause, signal) {
|
|
305
|
+
return (signal.aborted &&
|
|
306
|
+
(cause === signal.reason ||
|
|
307
|
+
(cause instanceof Error &&
|
|
308
|
+
cause.name === "AbortError" &&
|
|
309
|
+
"code" in cause &&
|
|
310
|
+
cause.code === "ABORT_ERR" &&
|
|
311
|
+
cause.cause === signal.reason)));
|
|
312
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type PublisherResources } from "../runtime_resources.js";
|
|
2
|
+
import { type PublisherEnrollment } from "../enrollment_source.js";
|
|
3
|
+
import type { FetchHandler } from "./fetch_types.js";
|
|
4
|
+
import { type NamedPublicationOptions, type Publication } from "./publication.js";
|
|
5
|
+
import type { LocalHTTPServer } from "./register.js";
|
|
6
|
+
export interface PublisherOptions {
|
|
7
|
+
readonly resources?: PublisherResources;
|
|
8
|
+
readonly controlUrl?: string;
|
|
9
|
+
readonly instance?: string;
|
|
10
|
+
readonly enrollment?: PublisherEnrollment;
|
|
11
|
+
readonly signal?: AbortSignal;
|
|
12
|
+
}
|
|
13
|
+
declare const nativePublisher: unique symbol;
|
|
14
|
+
export interface Publisher {
|
|
15
|
+
readonly [nativePublisher]: true;
|
|
16
|
+
readonly publisherIdentityID: string;
|
|
17
|
+
readonly deploymentID: string;
|
|
18
|
+
readonly controlUrl: string;
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
type PublishService = (service: string, target: FetchHandler | LocalHTTPServer, options?: NamedPublicationOptions) => Promise<Publication>;
|
|
23
|
+
export declare function nativePublisherOperation(owner: object): PublishService | undefined;
|
|
24
|
+
export declare function publisher(options?: PublisherOptions): Promise<Publisher>;
|
|
25
|
+
export {};
|