@di-framework/cli-plugin-platform 0.0.1 → 6.0.3
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/README.md +408 -0
- package/dist/actor-protocol.d.ts +4 -0
- package/dist/actor-protocol.js +162 -0
- package/dist/actors.d.ts +31 -0
- package/dist/actors.js +296 -0
- package/dist/args.d.ts +15 -0
- package/dist/args.js +66 -0
- package/dist/assets/http-adapter.js +876 -0
- package/dist/assets/platform/.gitignore.tmpl +6 -0
- package/dist/assets/platform/Pulumi.yaml.tmpl +6 -0
- package/dist/assets/platform/README.md +208 -0
- package/dist/assets/platform/index.ts.tmpl +2 -0
- package/dist/assets/platform/package.json.tmpl +18 -0
- package/dist/assets/platform/tsconfig.json +11 -0
- package/dist/assets/queue-adapter.js +60 -0
- package/dist/assets/sqlite/BUILD-INFO.txt +13 -0
- package/dist/assets/sqlite/SHA256SUMS +2 -0
- package/dist/assets/sqlite/di-framework-sqlite.wasm +0 -0
- package/dist/assets/sqlite/di-framework-sqlite.wit +337 -0
- package/dist/assets/wit/deps/LICENSE.md +8 -0
- package/dist/assets/wit/deps/di-framework-queues/package.wit +18 -0
- package/dist/assets/wit/deps/di-framework-sqlite/package.wit +211 -0
- package/dist/assets/wit/deps/wasi-cli/package.wit +70 -0
- package/dist/assets/wit/deps/wasi-clocks/package.wit +42 -0
- package/dist/assets/wit/deps/wasi-config/package.wit +15 -0
- package/dist/assets/wit/deps/wasi-filesystem/package.wit +184 -0
- package/dist/assets/wit/deps/wasi-http/package.wit +170 -0
- package/dist/assets/wit/deps/wasi-random/package.wit +17 -0
- package/dist/assets/wit/deps/wasi-sockets/package.wit +105 -0
- package/dist/assets/wit/deps/wasi-tls/package.wit +26 -0
- package/dist/assets/wit/deps/wasmcloud-blobstore/package.wit +153 -0
- package/dist/assets/wit/deps/wasmcloud-keyvalue/package.wit +370 -0
- package/dist/assets/wit/deps/wasmcloud-messaging/package.wit +125 -0
- package/dist/assets/wit/deps/wasmcloud-postgres/package.wit +435 -0
- package/dist/assets/wit/deps/wasmcloud-secrets/package.wit +80 -0
- package/dist/async-transform.d.ts +3 -0
- package/dist/async-transform.js +60 -0
- package/dist/bindings.d.ts +34 -0
- package/dist/bindings.js +307 -0
- package/dist/build.d.ts +29 -0
- package/dist/build.js +325 -0
- package/dist/command.d.ts +3 -0
- package/dist/command.js +141 -0
- package/dist/control/auth.d.ts +18 -0
- package/dist/control/auth.js +89 -0
- package/dist/control/cron.d.ts +7 -0
- package/dist/control/cron.js +86 -0
- package/dist/control/index.d.ts +4 -0
- package/dist/control/index.js +4 -0
- package/dist/control/network.d.ts +12 -0
- package/dist/control/network.js +49 -0
- package/dist/control/queues.d.ts +15 -0
- package/dist/control/queues.js +77 -0
- package/dist/cron.d.ts +26 -0
- package/dist/cron.js +239 -0
- package/dist/deploy.d.ts +29 -0
- package/dist/deploy.js +57 -0
- package/dist/deps.d.ts +96 -0
- package/dist/deps.js +303 -0
- package/dist/destroy.d.ts +3 -0
- package/dist/destroy.js +24 -0
- package/dist/dev-runner.d.ts +24 -0
- package/dist/dev-runner.js +83 -0
- package/dist/dev.d.ts +8 -0
- package/dist/dev.js +76 -0
- package/dist/discovery.d.ts +15 -0
- package/dist/discovery.js +182 -0
- package/dist/doctor.d.ts +8 -0
- package/dist/doctor.js +82 -0
- package/dist/guests.d.ts +9 -0
- package/dist/guests.js +56 -0
- package/dist/host-interface.d.ts +29 -0
- package/dist/host-interface.js +142 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +39 -0
- package/dist/kubernetes.d.ts +9 -0
- package/dist/kubernetes.js +17 -0
- package/dist/managed-bindings.d.ts +9 -0
- package/dist/managed-bindings.js +150 -0
- package/dist/manifest.d.ts +36 -0
- package/dist/manifest.js +257 -0
- package/dist/namespace.d.ts +2 -0
- package/dist/namespace.js +4 -0
- package/dist/node-compat/async-hooks.d.ts +29 -0
- package/dist/node-compat/async-hooks.js +80 -0
- package/dist/node-compat/bootstrap.d.ts +1 -0
- package/dist/node-compat/bootstrap.js +9 -0
- package/dist/node-compat/bytes.d.ts +7 -0
- package/dist/node-compat/bytes.js +41 -0
- package/dist/node-compat/crypto-hash.d.ts +27 -0
- package/dist/node-compat/crypto-hash.js +108 -0
- package/dist/node-compat/crypto-subtle.d.ts +59 -0
- package/dist/node-compat/crypto-subtle.js +288 -0
- package/dist/node-compat/crypto.d.ts +136 -0
- package/dist/node-compat/crypto.js +142 -0
- package/dist/node-compat/dgram.d.ts +49 -0
- package/dist/node-compat/dgram.js +197 -0
- package/dist/node-compat/env.d.ts +6 -0
- package/dist/node-compat/env.js +88 -0
- package/dist/node-compat/fetch-runtime.d.ts +80 -0
- package/dist/node-compat/fetch-runtime.js +314 -0
- package/dist/node-compat/fs.d.ts +84 -0
- package/dist/node-compat/fs.js +285 -0
- package/dist/node-compat/http-parser.d.ts +40 -0
- package/dist/node-compat/http-parser.js +185 -0
- package/dist/node-compat/http.d.ts +130 -0
- package/dist/node-compat/http.js +688 -0
- package/dist/node-compat/https.d.ts +28 -0
- package/dist/node-compat/https.js +88 -0
- package/dist/node-compat/module.d.ts +11 -0
- package/dist/node-compat/module.js +35 -0
- package/dist/node-compat/net.d.ts +119 -0
- package/dist/node-compat/net.js +441 -0
- package/dist/node-compat/process.d.ts +25 -0
- package/dist/node-compat/process.js +63 -0
- package/dist/node-compat/runtime-application.d.ts +4 -0
- package/dist/node-compat/runtime-application.js +24 -0
- package/dist/node-compat/seed-virtual.d.ts +6 -0
- package/dist/node-compat/seed-virtual.js +6 -0
- package/dist/node-compat/seed.d.ts +20 -0
- package/dist/node-compat/seed.js +106 -0
- package/dist/node-compat/socket-address.d.ts +35 -0
- package/dist/node-compat/socket-address.js +162 -0
- package/dist/node-compat/timers.d.ts +32 -0
- package/dist/node-compat/timers.js +96 -0
- package/dist/node-compat/tls.d.ts +57 -0
- package/dist/node-compat/tls.js +290 -0
- package/dist/node-compat/wasi-random.d.ts +2 -0
- package/dist/node-compat/wasi-random.js +25 -0
- package/dist/node-compat/wasi-sockets.d.ts +47 -0
- package/dist/node-compat/wasi-sockets.js +134 -0
- package/dist/oci.d.ts +9 -0
- package/dist/oci.js +9 -0
- package/dist/paths.d.ts +7 -0
- package/dist/paths.js +13 -0
- package/dist/platform-init.d.ts +13 -0
- package/dist/platform-init.js +265 -0
- package/dist/platform.d.ts +22 -0
- package/dist/platform.js +170 -0
- package/dist/project.d.ts +39 -0
- package/dist/project.js +130 -0
- package/dist/publish.d.ts +15 -0
- package/dist/publish.js +57 -0
- package/dist/pulumi.d.ts +4 -0
- package/dist/pulumi.js +27 -0
- package/dist/queues-module.d.ts +4 -0
- package/dist/queues-module.js +89 -0
- package/dist/queues.d.ts +16 -0
- package/dist/queues.js +167 -0
- package/dist/registry.d.ts +16 -0
- package/dist/registry.js +27 -0
- package/dist/service.d.ts +162 -0
- package/dist/service.js +678 -0
- package/dist/support.d.ts +14 -0
- package/dist/support.js +37 -0
- package/dist/target.d.ts +18 -0
- package/dist/target.js +45 -0
- package/dist/toml.d.ts +6 -0
- package/dist/toml.js +245 -0
- package/dist/wash-dev.d.ts +18 -0
- package/dist/wash-dev.js +80 -0
- package/dist/wit.d.ts +76 -0
- package/dist/wit.js +248 -0
- package/dist/workload-members.d.ts +12 -0
- package/dist/workload-members.js +136 -0
- package/dist/workload.d.ts +45 -0
- package/dist/workload.js +457 -0
- package/package.json +55 -8
- package/src/index.ts +0 -2
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type IpAddressFamily = 'ipv4' | 'ipv6';
|
|
2
|
+
export type Ipv4Address = [number, number, number, number];
|
|
3
|
+
export type Ipv6Address = [number, number, number, number, number, number, number, number];
|
|
4
|
+
export type IpSocketAddress = {
|
|
5
|
+
tag: 'ipv4';
|
|
6
|
+
val: {
|
|
7
|
+
port: number;
|
|
8
|
+
address: Ipv4Address;
|
|
9
|
+
};
|
|
10
|
+
} | {
|
|
11
|
+
tag: 'ipv6';
|
|
12
|
+
val: {
|
|
13
|
+
port: number;
|
|
14
|
+
address: Ipv6Address;
|
|
15
|
+
flowInfo: number;
|
|
16
|
+
scopeId: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare function isIPv4(host: string): boolean;
|
|
20
|
+
export declare function isIPv6(host: string): boolean;
|
|
21
|
+
export declare function isIP(host: string): 0 | 4 | 6;
|
|
22
|
+
export declare function stripIpv6Brackets(host: string): string;
|
|
23
|
+
export declare function parseIpv4(host: string): Ipv4Address | undefined;
|
|
24
|
+
export declare function parseIpv6(host: string): Ipv6Address | undefined;
|
|
25
|
+
export declare function formatIpv4(address: Ipv4Address): string;
|
|
26
|
+
export declare function formatIpv6(address: Ipv6Address): string;
|
|
27
|
+
export declare function parseIpSocketAddress(value: unknown): IpSocketAddress | undefined;
|
|
28
|
+
export declare function formatIpSocketAddress(address: IpSocketAddress): {
|
|
29
|
+
address: string;
|
|
30
|
+
family: 'IPv4' | 'IPv6';
|
|
31
|
+
port: number;
|
|
32
|
+
};
|
|
33
|
+
export declare function ipSocketAddress(host: string, port: number, family?: IpAddressFamily): IpSocketAddress | undefined;
|
|
34
|
+
export declare function unspecifiedAddress(family: IpAddressFamily, port: number): IpSocketAddress;
|
|
35
|
+
export declare function familyFromHost(host: string, fallback?: IpAddressFamily): IpAddressFamily;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
const IPV4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
|
2
|
+
export function isIPv4(host) {
|
|
3
|
+
return parseIpv4(host) !== undefined;
|
|
4
|
+
}
|
|
5
|
+
export function isIPv6(host) {
|
|
6
|
+
return parseIpv6(stripIpv6Brackets(host)) !== undefined;
|
|
7
|
+
}
|
|
8
|
+
export function isIP(host) {
|
|
9
|
+
if (isIPv4(host))
|
|
10
|
+
return 4;
|
|
11
|
+
if (isIPv6(host))
|
|
12
|
+
return 6;
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
export function stripIpv6Brackets(host) {
|
|
16
|
+
return host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
|
|
17
|
+
}
|
|
18
|
+
export function parseIpv4(host) {
|
|
19
|
+
const match = IPV4.exec(host);
|
|
20
|
+
if (match === null)
|
|
21
|
+
return undefined;
|
|
22
|
+
const parts = [];
|
|
23
|
+
for (let i = 1; i <= 4; i++) {
|
|
24
|
+
const value = Number(match[i]);
|
|
25
|
+
if (!Number.isInteger(value) || value > 255)
|
|
26
|
+
return undefined;
|
|
27
|
+
parts.push(value);
|
|
28
|
+
}
|
|
29
|
+
return parts;
|
|
30
|
+
}
|
|
31
|
+
export function parseIpv6(host) {
|
|
32
|
+
const input = stripIpv6Brackets(host);
|
|
33
|
+
if (input.includes('.'))
|
|
34
|
+
return undefined;
|
|
35
|
+
const halves = input.split('::');
|
|
36
|
+
if (halves.length > 2)
|
|
37
|
+
return undefined;
|
|
38
|
+
const parse = (part) => {
|
|
39
|
+
if (part === undefined || part === '')
|
|
40
|
+
return [];
|
|
41
|
+
const values = [];
|
|
42
|
+
for (const token of part.split(':')) {
|
|
43
|
+
if (token === '')
|
|
44
|
+
return undefined;
|
|
45
|
+
if (!/^[0-9A-Fa-f]{1,4}$/.test(token))
|
|
46
|
+
return undefined;
|
|
47
|
+
const value = Number.parseInt(token, 16);
|
|
48
|
+
if (Number.isNaN(value) || value > 0xffff)
|
|
49
|
+
return undefined;
|
|
50
|
+
values.push(value);
|
|
51
|
+
}
|
|
52
|
+
return values;
|
|
53
|
+
};
|
|
54
|
+
if (halves.length === 1) {
|
|
55
|
+
const parts = parse(halves[0]);
|
|
56
|
+
if (parts === undefined || parts.length !== 8)
|
|
57
|
+
return undefined;
|
|
58
|
+
return parts;
|
|
59
|
+
}
|
|
60
|
+
const left = parse(halves[0]);
|
|
61
|
+
const right = parse(halves[1]);
|
|
62
|
+
if (left === undefined || right === undefined)
|
|
63
|
+
return undefined;
|
|
64
|
+
const fill = 8 - left.length - right.length;
|
|
65
|
+
if (fill < 0)
|
|
66
|
+
return undefined;
|
|
67
|
+
return [...left, ...Array(fill).fill(0), ...right];
|
|
68
|
+
}
|
|
69
|
+
export function formatIpv4(address) {
|
|
70
|
+
return address.join('.');
|
|
71
|
+
}
|
|
72
|
+
export function formatIpv6(address) {
|
|
73
|
+
return address.map((part) => part.toString(16)).join(':');
|
|
74
|
+
}
|
|
75
|
+
function tuple4(value) {
|
|
76
|
+
const parts = Array.isArray(value) ? value : undefined;
|
|
77
|
+
if (parts === undefined || parts.length !== 4)
|
|
78
|
+
return undefined;
|
|
79
|
+
const numbers = parts.map(Number);
|
|
80
|
+
if (numbers.some((part) => !Number.isInteger(part) || part < 0 || part > 255))
|
|
81
|
+
return undefined;
|
|
82
|
+
return numbers;
|
|
83
|
+
}
|
|
84
|
+
function tuple8(value) {
|
|
85
|
+
const parts = Array.isArray(value) ? value : undefined;
|
|
86
|
+
if (parts === undefined || parts.length !== 8)
|
|
87
|
+
return undefined;
|
|
88
|
+
const numbers = parts.map(Number);
|
|
89
|
+
if (numbers.some((part) => !Number.isInteger(part) || part < 0 || part > 0xffff)) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
return numbers;
|
|
93
|
+
}
|
|
94
|
+
export function parseIpSocketAddress(value) {
|
|
95
|
+
if (value === null || typeof value !== 'object')
|
|
96
|
+
return undefined;
|
|
97
|
+
const tagged = value;
|
|
98
|
+
if (tagged.tag === 'ipv4') {
|
|
99
|
+
const record = tagged.val;
|
|
100
|
+
const address = tuple4(record?.address);
|
|
101
|
+
const port = Number(record?.port);
|
|
102
|
+
if (address === undefined || !Number.isInteger(port) || port < 0 || port > 65535) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
return { tag: 'ipv4', val: { port, address } };
|
|
106
|
+
}
|
|
107
|
+
if (tagged.tag === 'ipv6') {
|
|
108
|
+
const record = tagged.val;
|
|
109
|
+
const address = tuple8(record?.address);
|
|
110
|
+
const port = Number(record?.port);
|
|
111
|
+
if (address === undefined || !Number.isInteger(port) || port < 0 || port > 65535) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
tag: 'ipv6',
|
|
116
|
+
val: {
|
|
117
|
+
port,
|
|
118
|
+
address,
|
|
119
|
+
flowInfo: Number(record?.flowInfo ?? record?.['flow-info'] ?? 0),
|
|
120
|
+
scopeId: Number(record?.scopeId ?? record?.['scope-id'] ?? 0),
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
export function formatIpSocketAddress(address) {
|
|
127
|
+
if (address.tag === 'ipv4') {
|
|
128
|
+
return { address: formatIpv4(address.val.address), family: 'IPv4', port: address.val.port };
|
|
129
|
+
}
|
|
130
|
+
return { address: formatIpv6(address.val.address), family: 'IPv6', port: address.val.port };
|
|
131
|
+
}
|
|
132
|
+
export function ipSocketAddress(host, port, family = 'ipv4') {
|
|
133
|
+
const hostname = host === '' ? (family === 'ipv6' ? '::' : '0.0.0.0') : host;
|
|
134
|
+
if (hostname === 'localhost' || hostname === 'localhost.') {
|
|
135
|
+
return family === 'ipv6'
|
|
136
|
+
? {
|
|
137
|
+
tag: 'ipv6',
|
|
138
|
+
val: { port, address: [0, 0, 0, 0, 0, 0, 0, 1], flowInfo: 0, scopeId: 0 },
|
|
139
|
+
}
|
|
140
|
+
: { tag: 'ipv4', val: { port, address: [127, 0, 0, 1] } };
|
|
141
|
+
}
|
|
142
|
+
const v4 = parseIpv4(hostname);
|
|
143
|
+
if (v4 !== undefined)
|
|
144
|
+
return { tag: 'ipv4', val: { port, address: v4 } };
|
|
145
|
+
const v6 = parseIpv6(hostname);
|
|
146
|
+
if (v6 !== undefined) {
|
|
147
|
+
return { tag: 'ipv6', val: { port, address: v6, flowInfo: 0, scopeId: 0 } };
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
export function unspecifiedAddress(family, port) {
|
|
152
|
+
return family === 'ipv6'
|
|
153
|
+
? { tag: 'ipv6', val: { port, address: [0, 0, 0, 0, 0, 0, 0, 0], flowInfo: 0, scopeId: 0 } }
|
|
154
|
+
: { tag: 'ipv4', val: { port, address: [0, 0, 0, 0] } };
|
|
155
|
+
}
|
|
156
|
+
export function familyFromHost(host, fallback = 'ipv4') {
|
|
157
|
+
if (isIPv6(host) || (host === 'localhost' && fallback === 'ipv6'))
|
|
158
|
+
return 'ipv6';
|
|
159
|
+
if (isIPv4(host) || host === 'localhost')
|
|
160
|
+
return 'ipv4';
|
|
161
|
+
return fallback;
|
|
162
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare class Timeout {
|
|
2
|
+
private delay;
|
|
3
|
+
private repeat;
|
|
4
|
+
readonly id: number;
|
|
5
|
+
private active;
|
|
6
|
+
private referenced;
|
|
7
|
+
private generation;
|
|
8
|
+
private callback;
|
|
9
|
+
constructor(callback: (...args: unknown[]) => void, delay: number, repeat: boolean, args: unknown[]);
|
|
10
|
+
refresh(): this;
|
|
11
|
+
private wait;
|
|
12
|
+
close(): this;
|
|
13
|
+
ref(): this;
|
|
14
|
+
unref(): this;
|
|
15
|
+
hasRef(): boolean;
|
|
16
|
+
[Symbol.toPrimitive](): number;
|
|
17
|
+
}
|
|
18
|
+
export declare function setTimeout(callback: (...args: any[]) => void, delay?: number, ...args: unknown[]): Timeout;
|
|
19
|
+
export declare function setInterval(callback: (...args: any[]) => void, delay?: number, ...args: unknown[]): Timeout;
|
|
20
|
+
export declare function clearTimeout(timer?: Timeout | number | string | null): void;
|
|
21
|
+
export declare const clearInterval: typeof clearTimeout;
|
|
22
|
+
export declare function setImmediate(callback: (...args: any[]) => void, ...args: unknown[]): Timeout;
|
|
23
|
+
export declare const clearImmediate: typeof clearTimeout;
|
|
24
|
+
declare const _default: {
|
|
25
|
+
setTimeout: typeof setTimeout;
|
|
26
|
+
clearTimeout: typeof clearTimeout;
|
|
27
|
+
setInterval: typeof setInterval;
|
|
28
|
+
clearInterval: typeof clearTimeout;
|
|
29
|
+
setImmediate: typeof setImmediate;
|
|
30
|
+
clearImmediate: typeof clearTimeout;
|
|
31
|
+
};
|
|
32
|
+
export default _default;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { now, waitUntil } from 'wasi:clocks/monotonic-clock@0.3.0';
|
|
2
|
+
import { AsyncLocalStorage } from './async-hooks.js';
|
|
3
|
+
let nextId = 1;
|
|
4
|
+
const pending = new Map();
|
|
5
|
+
export class Timeout {
|
|
6
|
+
delay;
|
|
7
|
+
repeat;
|
|
8
|
+
id = nextId++;
|
|
9
|
+
active = true;
|
|
10
|
+
referenced = true;
|
|
11
|
+
generation = 0;
|
|
12
|
+
callback;
|
|
13
|
+
constructor(callback, delay, repeat, args) {
|
|
14
|
+
this.delay = delay;
|
|
15
|
+
this.repeat = repeat;
|
|
16
|
+
this.callback = AsyncLocalStorage.bind(() => callback(...args));
|
|
17
|
+
this.refresh();
|
|
18
|
+
}
|
|
19
|
+
refresh() {
|
|
20
|
+
this.active = true;
|
|
21
|
+
pending.set(this.id, this);
|
|
22
|
+
const generation = ++this.generation;
|
|
23
|
+
void this.wait(generation);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
async wait(generation) {
|
|
27
|
+
do {
|
|
28
|
+
const mark = now();
|
|
29
|
+
const deadline = BigInt(mark) + BigInt(this.delay) * BigInt(1_000_000);
|
|
30
|
+
// Bound cancellation latency: cancelled timers must not leave a long WASI
|
|
31
|
+
// task alive after the request has completed.
|
|
32
|
+
while (this.active && generation === this.generation && BigInt(now()) < deadline) {
|
|
33
|
+
const slice = BigInt(now()) + BigInt(10_000_000);
|
|
34
|
+
const when = slice < deadline ? slice : deadline;
|
|
35
|
+
await waitUntil(typeof mark === 'bigint' ? when : Number(when));
|
|
36
|
+
}
|
|
37
|
+
if (!this.active || generation !== this.generation)
|
|
38
|
+
return;
|
|
39
|
+
if (!this.repeat)
|
|
40
|
+
this.close();
|
|
41
|
+
this.callback();
|
|
42
|
+
} while (this.repeat && this.active && generation === this.generation);
|
|
43
|
+
}
|
|
44
|
+
close() {
|
|
45
|
+
this.active = false;
|
|
46
|
+
this.generation++;
|
|
47
|
+
pending.delete(this.id);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
ref() {
|
|
51
|
+
this.referenced = true;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
unref() {
|
|
55
|
+
this.referenced = false;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
hasRef() {
|
|
59
|
+
return this.referenced;
|
|
60
|
+
}
|
|
61
|
+
[Symbol.toPrimitive]() {
|
|
62
|
+
return this.id;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function schedule(callback, delay, repeat, args) {
|
|
66
|
+
if (typeof callback !== 'function')
|
|
67
|
+
throw new TypeError('callback must be a function');
|
|
68
|
+
const value = Number(delay);
|
|
69
|
+
const duration = !Number.isFinite(value) || value < 1 || value > 2_147_483_647 ? 1 : Math.trunc(value);
|
|
70
|
+
return new Timeout(callback, duration, repeat, args);
|
|
71
|
+
}
|
|
72
|
+
export function setTimeout(callback, delay, ...args) {
|
|
73
|
+
return schedule(callback, delay, false, args);
|
|
74
|
+
}
|
|
75
|
+
export function setInterval(callback, delay, ...args) {
|
|
76
|
+
return schedule(callback, delay, true, args);
|
|
77
|
+
}
|
|
78
|
+
export function clearTimeout(timer) {
|
|
79
|
+
if (timer instanceof Timeout)
|
|
80
|
+
timer.close();
|
|
81
|
+
else if (timer != null)
|
|
82
|
+
pending.get(Number(timer))?.close();
|
|
83
|
+
}
|
|
84
|
+
export const clearInterval = clearTimeout;
|
|
85
|
+
export function setImmediate(callback, ...args) {
|
|
86
|
+
return setTimeout(callback, 1, ...args);
|
|
87
|
+
}
|
|
88
|
+
export const clearImmediate = clearTimeout;
|
|
89
|
+
export default {
|
|
90
|
+
setTimeout,
|
|
91
|
+
clearTimeout,
|
|
92
|
+
setInterval,
|
|
93
|
+
clearInterval,
|
|
94
|
+
setImmediate,
|
|
95
|
+
clearImmediate,
|
|
96
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Socket } from './net.js';
|
|
2
|
+
export type ConnectionOptions = {
|
|
3
|
+
host?: string;
|
|
4
|
+
port?: number;
|
|
5
|
+
family?: number;
|
|
6
|
+
servername?: string;
|
|
7
|
+
socket?: Socket;
|
|
8
|
+
rejectUnauthorized?: boolean;
|
|
9
|
+
[option: string]: unknown;
|
|
10
|
+
};
|
|
11
|
+
export declare function unsupported(feature: string): never;
|
|
12
|
+
export declare function validateOptions(options: ConnectionOptions): void;
|
|
13
|
+
/** Client TLS using host verification and encryption. The raw socket carries ciphertext only. */
|
|
14
|
+
export declare class TLSSocket extends Socket {
|
|
15
|
+
readonly encrypted = true;
|
|
16
|
+
authorized: boolean;
|
|
17
|
+
authorizationError: Error | null;
|
|
18
|
+
readonly servername: string;
|
|
19
|
+
readonly alpnProtocol = false;
|
|
20
|
+
private readonly transport;
|
|
21
|
+
private readonly ciphertext;
|
|
22
|
+
private ended;
|
|
23
|
+
private handshakeStarted;
|
|
24
|
+
private timeoutMs;
|
|
25
|
+
private timer;
|
|
26
|
+
constructor(socket?: Socket, options?: ConnectionOptions);
|
|
27
|
+
private fail;
|
|
28
|
+
connect(..._args: Parameters<Socket['connect']>): this;
|
|
29
|
+
private observe;
|
|
30
|
+
private transform;
|
|
31
|
+
private sendCiphertext;
|
|
32
|
+
private handshake;
|
|
33
|
+
write(data: string | Uint8Array, encodingOrCallback?: string | ((error?: Error | null) => void), callback?: (error?: Error | null) => void): boolean;
|
|
34
|
+
end(data?: string | Uint8Array | (() => void), encodingOrCallback?: string | (() => void), callback?: () => void): this;
|
|
35
|
+
destroy(error?: Error): this;
|
|
36
|
+
setTimeout(timeout: number, callback?: () => void): this;
|
|
37
|
+
private refreshTimeout;
|
|
38
|
+
getPeerCertificate(): never;
|
|
39
|
+
getCipher(): never;
|
|
40
|
+
getProtocol(): never;
|
|
41
|
+
getSession(): never;
|
|
42
|
+
renegotiate(): never;
|
|
43
|
+
}
|
|
44
|
+
export declare function connect(portOrOptions: number | ConnectionOptions, hostOrOptionsOrCallback?: string | ConnectionOptions | (() => void), optionsOrCallback?: ConnectionOptions | (() => void), callback?: () => void): TLSSocket;
|
|
45
|
+
export declare class Server {
|
|
46
|
+
constructor(..._args: unknown[]);
|
|
47
|
+
}
|
|
48
|
+
export declare function createServer(..._args: unknown[]): never;
|
|
49
|
+
export declare function createSecureContext(..._args: unknown[]): never;
|
|
50
|
+
declare const _default: {
|
|
51
|
+
TLSSocket: typeof TLSSocket;
|
|
52
|
+
connect: typeof connect;
|
|
53
|
+
Server: typeof Server;
|
|
54
|
+
createServer: typeof createServer;
|
|
55
|
+
createSecureContext: typeof createSecureContext;
|
|
56
|
+
};
|
|
57
|
+
export default _default;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { Connector } from 'wasi:tls/client@0.3.0-draft';
|
|
2
|
+
import { createPushStream, Socket } from './net.js';
|
|
3
|
+
import { clearTimeout, setTimeout } from './timers.js';
|
|
4
|
+
import { asAsyncIterable, toBytes } from './wasi-sockets.js';
|
|
5
|
+
export function unsupported(feature) {
|
|
6
|
+
throw Object.assign(new Error(`${feature} is not supported by wasi:tls; configure TLS policy on the host`), {
|
|
7
|
+
code: 'ERR_TLS_UNSUPPORTED_OPTION',
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
export function validateOptions(options) {
|
|
11
|
+
if (options.rejectUnauthorized !== undefined && options.rejectUnauthorized !== true) {
|
|
12
|
+
unsupported('rejectUnauthorized');
|
|
13
|
+
}
|
|
14
|
+
for (const name of [
|
|
15
|
+
'ca',
|
|
16
|
+
'cert',
|
|
17
|
+
'key',
|
|
18
|
+
'pfx',
|
|
19
|
+
'passphrase',
|
|
20
|
+
'crl',
|
|
21
|
+
'secureContext',
|
|
22
|
+
'checkServerIdentity',
|
|
23
|
+
'ciphers',
|
|
24
|
+
'sigalgs',
|
|
25
|
+
'ecdhCurve',
|
|
26
|
+
'dhparam',
|
|
27
|
+
'minVersion',
|
|
28
|
+
'maxVersion',
|
|
29
|
+
'secureProtocol',
|
|
30
|
+
'secureOptions',
|
|
31
|
+
'ALPNProtocols',
|
|
32
|
+
'ALPNCallback',
|
|
33
|
+
'session',
|
|
34
|
+
'pskCallback',
|
|
35
|
+
'requestCert',
|
|
36
|
+
'isServer',
|
|
37
|
+
'SNICallback',
|
|
38
|
+
'enableTrace',
|
|
39
|
+
'allowPartialTrustChain',
|
|
40
|
+
'clientCertEngine',
|
|
41
|
+
'privateKeyEngine',
|
|
42
|
+
'privateKeyIdentifier',
|
|
43
|
+
]) {
|
|
44
|
+
if (options[name] !== undefined)
|
|
45
|
+
unsupported(name);
|
|
46
|
+
}
|
|
47
|
+
if (options.servername !== undefined && !options.servername)
|
|
48
|
+
unsupported('empty servername');
|
|
49
|
+
}
|
|
50
|
+
function tlsError(value) {
|
|
51
|
+
// Component bindings throw result errors as Error objects carrying the WIT resource.
|
|
52
|
+
if (value != null && typeof value === 'object' && 'payload' in value && value.payload !== value) {
|
|
53
|
+
return tlsError(value.payload);
|
|
54
|
+
}
|
|
55
|
+
if (value instanceof Error)
|
|
56
|
+
return value;
|
|
57
|
+
const detail = value != null && typeof value === 'object' && 'toDebugString' in value
|
|
58
|
+
? value.toDebugString()
|
|
59
|
+
: String(value);
|
|
60
|
+
return Object.assign(new Error(`TLS connection failed: ${detail}`), {
|
|
61
|
+
code: 'ERR_TLS_CONNECTION_FAILED',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function checkResult(value) {
|
|
65
|
+
if (value != null &&
|
|
66
|
+
typeof value === 'object' &&
|
|
67
|
+
'tag' in value &&
|
|
68
|
+
value.tag === 'err' &&
|
|
69
|
+
'val' in value) {
|
|
70
|
+
throw tlsError(value.val);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Client TLS using host verification and encryption. The raw socket carries ciphertext only. */
|
|
74
|
+
export class TLSSocket extends Socket {
|
|
75
|
+
encrypted = true;
|
|
76
|
+
authorized = false;
|
|
77
|
+
authorizationError = null;
|
|
78
|
+
servername;
|
|
79
|
+
alpnProtocol = false;
|
|
80
|
+
transport;
|
|
81
|
+
ciphertext = createPushStream();
|
|
82
|
+
ended = false;
|
|
83
|
+
handshakeStarted = false;
|
|
84
|
+
timeoutMs = 0;
|
|
85
|
+
timer;
|
|
86
|
+
constructor(socket, options = {}) {
|
|
87
|
+
super();
|
|
88
|
+
validateOptions(options);
|
|
89
|
+
this.servername = options.servername ?? options.host ?? socket?.remoteAddress ?? 'localhost';
|
|
90
|
+
this.transport = socket ?? new Socket();
|
|
91
|
+
this.connecting = true;
|
|
92
|
+
this.readyState = 'opening';
|
|
93
|
+
this.transport.on('data', (chunk) => this.ciphertext.push(toBytes(chunk)));
|
|
94
|
+
this.transport.on('end', () => this.ciphertext.close());
|
|
95
|
+
this.transport.on('error', (error) => this.fail(error));
|
|
96
|
+
this.transport.on('close', () => {
|
|
97
|
+
this.ciphertext.close();
|
|
98
|
+
if (this.connecting && !this.handshakeStarted && !this.destroyed)
|
|
99
|
+
this.fail(new Error('TLS socket closed before secure connection'));
|
|
100
|
+
});
|
|
101
|
+
queueMicrotask(() => {
|
|
102
|
+
if (this.destroyed)
|
|
103
|
+
return;
|
|
104
|
+
if (socket === undefined) {
|
|
105
|
+
this.transport.once('connect', () => void this.handshake());
|
|
106
|
+
this.transport.connect({
|
|
107
|
+
port: options.port ?? 443,
|
|
108
|
+
host: options.host ?? 'localhost',
|
|
109
|
+
family: options.family,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else if (socket.connecting)
|
|
113
|
+
socket.once('connect', () => void this.handshake());
|
|
114
|
+
else if (socket.destroyed || !socket.writable)
|
|
115
|
+
this.fail(new Error('TLS requires an open socket'));
|
|
116
|
+
else
|
|
117
|
+
void this.handshake();
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
fail(value) {
|
|
121
|
+
if (this.destroyed)
|
|
122
|
+
return;
|
|
123
|
+
const error = tlsError(value);
|
|
124
|
+
if (!this.authorized)
|
|
125
|
+
this.authorizationError = error;
|
|
126
|
+
this.destroy(error);
|
|
127
|
+
}
|
|
128
|
+
connect(..._args) {
|
|
129
|
+
return unsupported('TLSSocket.connect; use tls.connect(options)');
|
|
130
|
+
}
|
|
131
|
+
observe(value) {
|
|
132
|
+
void Promise.resolve(value)
|
|
133
|
+
.then(checkResult)
|
|
134
|
+
.catch((error) => this.fail(error));
|
|
135
|
+
}
|
|
136
|
+
transform(value) {
|
|
137
|
+
if (!Array.isArray(value) || value.length !== 2)
|
|
138
|
+
throw new Error('Invalid wasi:tls stream result');
|
|
139
|
+
this.observe(value[1]);
|
|
140
|
+
return asAsyncIterable(value[0]);
|
|
141
|
+
}
|
|
142
|
+
async sendCiphertext(stream) {
|
|
143
|
+
try {
|
|
144
|
+
for await (const chunk of stream) {
|
|
145
|
+
if (this.destroyed)
|
|
146
|
+
return;
|
|
147
|
+
this.transport.write(toBytes(chunk));
|
|
148
|
+
}
|
|
149
|
+
if (!this.destroyed)
|
|
150
|
+
this.transport.end();
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
this.fail(error);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async handshake() {
|
|
157
|
+
if (this.destroyed)
|
|
158
|
+
return;
|
|
159
|
+
this.handshakeStarted = true;
|
|
160
|
+
try {
|
|
161
|
+
this.emit('connect');
|
|
162
|
+
const connector = new Connector();
|
|
163
|
+
const incoming = this.transform(connector.receive(this.ciphertext.iterable));
|
|
164
|
+
const cleartext = createPushStream();
|
|
165
|
+
const outgoing = this.transform(connector.send(cleartext.iterable));
|
|
166
|
+
this.once('close', () => cleartext.close());
|
|
167
|
+
void this.sendCiphertext(outgoing);
|
|
168
|
+
checkResult(await Connector.connect(connector, this.servername));
|
|
169
|
+
if (this.destroyed)
|
|
170
|
+
return;
|
|
171
|
+
this.authorized = true;
|
|
172
|
+
this.connecting = false;
|
|
173
|
+
this.pending = false;
|
|
174
|
+
this.readyState = 'open';
|
|
175
|
+
this.outgoing = cleartext;
|
|
176
|
+
for (const chunk of this.buffered)
|
|
177
|
+
cleartext.push(chunk);
|
|
178
|
+
this.buffered = [];
|
|
179
|
+
if (this.ended)
|
|
180
|
+
cleartext.close();
|
|
181
|
+
this.localAddress = this.transport.localAddress;
|
|
182
|
+
this.localPort = this.transport.localPort;
|
|
183
|
+
this.remoteAddress = this.transport.remoteAddress;
|
|
184
|
+
this.remotePort = this.transport.remotePort;
|
|
185
|
+
this.remoteFamily = this.transport.remoteFamily;
|
|
186
|
+
this.refreshTimeout();
|
|
187
|
+
this.emit('secureConnect');
|
|
188
|
+
for await (const chunk of incoming) {
|
|
189
|
+
if (this.destroyed)
|
|
190
|
+
return;
|
|
191
|
+
const bytes = toBytes(chunk);
|
|
192
|
+
this.bytesRead += bytes.length;
|
|
193
|
+
this.refreshTimeout();
|
|
194
|
+
this.readableBuffer.push(bytes);
|
|
195
|
+
this.flushReadable();
|
|
196
|
+
}
|
|
197
|
+
if (!this.destroyed) {
|
|
198
|
+
this.readableEnded = true;
|
|
199
|
+
this.emit('end');
|
|
200
|
+
this.destroy();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
this.fail(error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
write(data, encodingOrCallback, callback) {
|
|
208
|
+
if (this.ended) {
|
|
209
|
+
const error = Object.assign(new Error('write after end'), {
|
|
210
|
+
code: 'ERR_STREAM_WRITE_AFTER_END',
|
|
211
|
+
});
|
|
212
|
+
const cb = typeof encodingOrCallback === 'function' ? encodingOrCallback : callback;
|
|
213
|
+
if (cb)
|
|
214
|
+
queueMicrotask(() => cb(error));
|
|
215
|
+
else
|
|
216
|
+
this.fail(error);
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
this.refreshTimeout();
|
|
220
|
+
return super.write(data, encodingOrCallback, callback);
|
|
221
|
+
}
|
|
222
|
+
end(data, encodingOrCallback, callback) {
|
|
223
|
+
super.end(data, encodingOrCallback, callback);
|
|
224
|
+
this.ended = true;
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
destroy(error) {
|
|
228
|
+
if (this.destroyed)
|
|
229
|
+
return this;
|
|
230
|
+
clearTimeout(this.timer);
|
|
231
|
+
this.ciphertext.close();
|
|
232
|
+
super.destroy(error);
|
|
233
|
+
this.transport.destroy();
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
setTimeout(timeout, callback) {
|
|
237
|
+
this.timeoutMs = timeout;
|
|
238
|
+
if (callback)
|
|
239
|
+
this.once('timeout', callback);
|
|
240
|
+
this.refreshTimeout();
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
refreshTimeout() {
|
|
244
|
+
clearTimeout(this.timer);
|
|
245
|
+
if (this.timeoutMs > 0 && !this.destroyed)
|
|
246
|
+
this.timer = setTimeout(() => this.emit('timeout'), this.timeoutMs);
|
|
247
|
+
}
|
|
248
|
+
getPeerCertificate() {
|
|
249
|
+
return unsupported('getPeerCertificate');
|
|
250
|
+
}
|
|
251
|
+
getCipher() {
|
|
252
|
+
return unsupported('getCipher');
|
|
253
|
+
}
|
|
254
|
+
getProtocol() {
|
|
255
|
+
return unsupported('getProtocol');
|
|
256
|
+
}
|
|
257
|
+
getSession() {
|
|
258
|
+
return unsupported('getSession');
|
|
259
|
+
}
|
|
260
|
+
renegotiate() {
|
|
261
|
+
return unsupported('renegotiate');
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
export function connect(portOrOptions, hostOrOptionsOrCallback, optionsOrCallback, callback) {
|
|
265
|
+
let options = typeof portOrOptions === 'number' ? { port: portOrOptions } : { ...portOrOptions };
|
|
266
|
+
for (const argument of [hostOrOptionsOrCallback, optionsOrCallback]) {
|
|
267
|
+
if (typeof argument === 'string')
|
|
268
|
+
options.host = argument;
|
|
269
|
+
else if (typeof argument === 'function')
|
|
270
|
+
callback = argument;
|
|
271
|
+
else if (argument)
|
|
272
|
+
options = { ...options, ...argument };
|
|
273
|
+
}
|
|
274
|
+
const socket = new TLSSocket(options.socket, options);
|
|
275
|
+
if (callback)
|
|
276
|
+
socket.once('secureConnect', callback);
|
|
277
|
+
return socket;
|
|
278
|
+
}
|
|
279
|
+
export class Server {
|
|
280
|
+
constructor(..._args) {
|
|
281
|
+
unsupported('TLS servers');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
export function createServer(..._args) {
|
|
285
|
+
return unsupported('TLS servers');
|
|
286
|
+
}
|
|
287
|
+
export function createSecureContext(..._args) {
|
|
288
|
+
return unsupported('createSecureContext');
|
|
289
|
+
}
|
|
290
|
+
export default { TLSSocket, connect, Server, createServer, createSecureContext };
|