cf-workers-native 0.1.5 → 0.1.6
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 +2 -2
- package/dist/browser/documents/page-source.js +1 -1
- package/dist/browser/headers/index.d.ts +2 -0
- package/dist/browser/headers/index.js +5 -0
- package/dist/browser/response/index.d.ts +2 -0
- package/dist/browser/response/index.js +11 -3
- package/dist/browser/structured-clone/index.d.ts +3 -0
- package/dist/browser/structured-clone/index.js +75 -7
- package/dist/browser/websocket/attachment-size.d.ts +3 -0
- package/dist/browser/websocket/attachment-size.js +336 -0
- package/dist/browser/websocket/brand.d.ts +4 -0
- package/dist/browser/websocket/brand.js +16 -0
- package/dist/browser/websocket/close-event.d.ts +7 -0
- package/dist/browser/websocket/close-event.js +30 -0
- package/dist/browser/websocket/compression.d.ts +5 -0
- package/dist/browser/websocket/compression.js +124 -0
- package/dist/browser/websocket/conversion.d.ts +2 -0
- package/dist/browser/websocket/conversion.js +13 -0
- package/dist/browser/websocket/endpoint.d.ts +39 -0
- package/dist/browser/websocket/endpoint.js +379 -0
- package/dist/browser/websocket/frames.d.ts +21 -0
- package/dist/browser/websocket/frames.js +121 -0
- package/dist/browser/websocket/native.d.ts +14 -0
- package/dist/browser/websocket/native.js +226 -0
- package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
- package/dist/compat/crypto/LICENSE-3RD-PARTY.md +20 -0
- package/dist/compat/crypto/LICENSE.boringssl +238 -0
- package/dist/compat/crypto/LICENSE.ncrypto +21 -0
- package/dist/compat/crypto/asymmetric.js +141 -68
- package/dist/compat/crypto/cipher-handle.d.ts +27 -0
- package/dist/compat/crypto/cipher-handle.js +239 -0
- package/dist/compat/crypto/cipher-info.d.ts +12 -0
- package/dist/compat/crypto/cipher-info.js +324 -0
- package/dist/compat/crypto/constants.d.ts +62 -0
- package/dist/compat/crypto/constants.js +68 -0
- package/dist/compat/crypto/dh-handle.d.ts +17 -0
- package/dist/compat/crypto/dh-handle.js +180 -0
- package/dist/compat/crypto/dh.d.ts +53 -0
- package/dist/compat/crypto/dh.js +138 -0
- package/dist/compat/crypto/digest-stream.d.ts +15 -0
- package/dist/compat/crypto/digest-stream.js +213 -0
- package/dist/compat/crypto/digests.d.ts +3 -0
- package/dist/compat/crypto/digests.js +32 -0
- package/dist/compat/crypto/ecdh.d.ts +16 -6
- package/dist/compat/crypto/hash-handle.d.ts +23 -0
- package/dist/compat/crypto/hash-handle.js +70 -0
- package/dist/compat/crypto/hash.d.ts +37 -0
- package/dist/compat/crypto/hash.js +218 -0
- package/dist/compat/crypto/hkdf.js +40 -14
- package/dist/compat/crypto/index.d.cts +168 -1
- package/dist/compat/crypto/index.d.ts +191 -8
- package/dist/compat/crypto/index.js +36 -23
- package/dist/compat/crypto/key-errors.d.ts +10 -0
- package/dist/compat/crypto/key-errors.js +17 -0
- package/dist/compat/crypto/key-export.d.ts +8 -0
- package/dist/compat/crypto/key-export.js +49 -0
- package/dist/compat/crypto/key-formats.d.ts +2 -1
- package/dist/compat/crypto/key-formats.js +162 -31
- package/dist/compat/crypto/key-options.d.ts +1 -0
- package/dist/compat/crypto/key-options.js +74 -0
- package/dist/compat/crypto/key-state.d.ts +1 -0
- package/dist/compat/crypto/keys.d.ts +20 -12
- package/dist/compat/crypto/keys.js +125 -56
- package/dist/compat/crypto/metadata.d.ts +9 -0
- package/dist/compat/crypto/metadata.js +89 -2
- package/dist/compat/crypto/native-cipher.d.ts +17 -0
- package/dist/compat/crypto/native-cipher.js +192 -0
- package/dist/compat/crypto/native-crypto.d.ts +14 -1
- package/dist/compat/crypto/native-crypto.js +118 -6
- package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
- package/dist/compat/crypto/pbkdf2-handle.js +26 -0
- package/dist/compat/crypto/pbkdf2.d.ts +5 -0
- package/dist/compat/crypto/pbkdf2.js +60 -0
- package/dist/compat/crypto/prime.js +4 -4
- package/dist/compat/crypto/random.d.ts +11 -0
- package/dist/compat/crypto/random.js +86 -6
- package/dist/compat/crypto/rsa.d.ts +17 -1
- package/dist/compat/crypto/rsa.js +8 -4
- package/dist/compat/crypto/scrypt.d.ts +3 -3
- package/dist/compat/crypto/scrypt.js +25 -23
- package/dist/compat/crypto/sign-handle.d.ts +3 -3
- package/dist/compat/crypto/sign-handle.js +55 -11
- package/dist/compat/crypto/signatures.d.ts +17 -11
- package/dist/compat/crypto/subtle-digest.d.ts +1 -0
- package/dist/compat/crypto/subtle-digest.js +56 -0
- package/dist/compat/crypto/symmetric.d.ts +40 -24
- package/dist/compat/crypto/symmetric.js +229 -94
- package/dist/compat/crypto/validation.d.ts +4 -0
- package/dist/compat/crypto/validation.js +29 -2
- package/dist/compat/crypto/webcrypto.d.ts +13 -0
- package/dist/compat/crypto/webcrypto.js +51 -0
- package/dist/compat/crypto/x509.js +2 -1
- package/dist/compat/events/index.js +1 -1
- package/dist/compat/fs/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/fs/LICENSE-APACHE +177 -0
- package/dist/compat/fs/directory-backend.d.ts +6 -0
- package/dist/compat/fs/directory-backend.js +51 -0
- package/dist/compat/fs/directory.d.ts +37 -0
- package/dist/compat/fs/directory.js +137 -0
- package/dist/compat/fs/embedded.d.ts +1 -1
- package/dist/compat/fs/embedded.js +7 -2
- package/dist/compat/fs/file-handle.d.ts +75 -0
- package/dist/compat/fs/file-handle.js +226 -0
- package/dist/compat/fs/glob-internal.d.ts +15 -0
- package/dist/compat/fs/glob-internal.js +417 -0
- package/dist/compat/fs/glob.d.ts +10 -0
- package/dist/compat/fs/glob.js +71 -0
- package/dist/compat/fs/index.d.ts +6 -3
- package/dist/compat/fs/index.js +582 -50
- package/dist/compat/fs/io-validation.d.ts +14 -0
- package/dist/compat/fs/io-validation.js +130 -0
- package/dist/compat/fs/io.d.ts +1 -0
- package/dist/compat/fs/io.js +282 -0
- package/dist/compat/fs/promises/index.d.ts +3 -1
- package/dist/compat/fs/public-types.d.ts +120 -0
- package/dist/compat/fs/public-types.js +1 -0
- package/dist/compat/fs/stream-errors.d.ts +6 -0
- package/dist/compat/fs/stream-errors.js +8 -0
- package/dist/compat/fs/stream-utils.d.ts +16 -0
- package/dist/compat/fs/stream-utils.js +29 -0
- package/dist/compat/fs/streams.d.ts +104 -0
- package/dist/compat/fs/streams.js +794 -0
- package/dist/compat/fs/writable.d.ts +8 -0
- package/dist/compat/fs/writable.js +412 -22
- package/dist/compat/http/index.d.ts +11 -28
- package/dist/compat/http/index.js +81 -125
- package/dist/compat/http/server-options.d.ts +1 -0
- package/dist/compat/http/server-options.js +87 -0
- package/dist/compat/http/unique-headers.d.ts +1 -0
- package/dist/compat/http/unique-headers.js +21 -0
- package/dist/compat/http-outgoing/index.d.ts +2 -1
- package/dist/compat/http-outgoing/index.js +2 -17
- package/dist/compat/http-server/index.d.ts +2 -2
- package/dist/compat/http-server/index.js +4 -85
- package/dist/compat/https/index.d.ts +4 -2
- package/dist/compat/net/sockets.js +98 -20
- package/dist/compat/net/validators.d.ts +1 -0
- package/dist/compat/net/validators.js +8 -0
- package/dist/compat/process/configuration.d.ts +113 -0
- package/dist/compat/process/configuration.js +115 -0
- package/dist/compat/process/directory.d.ts +4 -0
- package/dist/compat/process/directory.js +28 -0
- package/dist/compat/process/files.d.ts +3 -0
- package/dist/compat/process/files.js +10 -0
- package/dist/compat/process/index.d.ts +15 -13
- package/dist/compat/process/index.js +47 -74
- package/dist/compat/process/scheduling.d.ts +2 -0
- package/dist/compat/process/scheduling.js +60 -0
- package/dist/compat/process/state.js +1 -0
- package/dist/compat/process/stdio.d.ts +18 -0
- package/dist/compat/process/stdio.js +66 -0
- package/dist/compat/stream/destroy.d.ts +1 -0
- package/dist/compat/stream/destroy.js +21 -1
- package/dist/compat/stream/readable.js +10 -2
- package/dist/compat/tls/jsstream.js +16 -6
- package/dist/compat/tls/wrap.d.ts +1 -1
- package/dist/compat/tls/wrap.js +0 -3
- package/dist/compat/util/inspect.js +5 -1
- package/dist/compat/zlib/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/zlib/LICENSE-APACHE +177 -0
- package/dist/compat/zlib/LICENSE.zlib +22 -0
- package/dist/compat/zlib/classic-base.cjs +600 -0
- package/dist/compat/zlib/classic-base.d.cts +39 -0
- package/dist/compat/zlib/classic-binding.d.ts +28 -0
- package/dist/compat/zlib/classic-binding.js +105 -0
- package/dist/compat/zlib/classic.d.ts +44 -0
- package/dist/compat/zlib/classic.js +199 -0
- package/dist/compat/zlib/codec-binding.d.ts +33 -0
- package/dist/compat/zlib/codec-binding.js +141 -0
- package/dist/compat/zlib/compression.d.ts +22 -51
- package/dist/compat/zlib/compression.js +129 -218
- package/dist/compat/zlib/errors.d.ts +21 -0
- package/dist/compat/zlib/errors.js +32 -0
- package/dist/compat/zlib/index.d.ts +367 -3
- package/dist/compat/zlib/index.js +28 -11
- package/dist/compat/zlib/input.d.ts +16 -0
- package/dist/compat/zlib/input.js +132 -0
- package/dist/compat/zlib/native-codecs.d.ts +7 -0
- package/dist/compat/zlib/native-codecs.js +22 -10
- package/dist/compat/zlib/native-zlib.d.ts +5 -0
- package/dist/compat/zlib/native-zlib.js +52 -0
- package/dist/compat/zlib/public-types.d.ts +48 -0
- package/dist/compat/zlib/public-types.js +1 -0
- package/dist/compat/zlib/validators.d.ts +3 -0
- package/dist/compat/zlib/validators.js +18 -0
- package/dist/shared/buffer-source.d.ts +5 -0
- package/dist/shared/buffer-source.js +70 -0
- package/dist/shared/globals/platform.d.ts +8 -0
- package/dist/shared/globals/platform.js +1 -0
- package/dist/shared/globals/web.js +5 -0
- package/dist/shared/request-context.d.ts +2 -0
- package/dist/shared/request-context.js +11 -0
- package/dist/shared/request-scope.js +4 -1
- package/dist/tooling/shared.js +6 -0
- package/dist/tooling/vite.js +4 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +38 -7
- package/package.json +2 -2
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { library, message } from "../../compat/zlib/native-zlib.js";
|
|
2
|
+
import { MAX_MESSAGE_BYTES } from "./frames.js";
|
|
3
|
+
/** RFC 7692 raw deflate stream. Context survives message boundaries unless the
|
|
4
|
+
* negotiated endpoint explicitly disables takeover. */
|
|
5
|
+
class MessageCodec {
|
|
6
|
+
encode;
|
|
7
|
+
noTakeover;
|
|
8
|
+
api = library();
|
|
9
|
+
context;
|
|
10
|
+
constructor(encode, bits, noTakeover) {
|
|
11
|
+
this.encode = encode;
|
|
12
|
+
this.noTakeover = noTakeover;
|
|
13
|
+
this.context = this.api.cz_create(encode ? 5 : 6, bits, -1, 8, 0, 0, 0);
|
|
14
|
+
if (!this.context)
|
|
15
|
+
throw new Error("Unable to initialize WebSocket compression.");
|
|
16
|
+
}
|
|
17
|
+
run(input) {
|
|
18
|
+
if (!this.context)
|
|
19
|
+
throw new Error("WebSocket compression is closed.");
|
|
20
|
+
// An empty message is an empty stored block. Repeating Z_SYNC_FLUSH on
|
|
21
|
+
// an idle deflater produces no bytes and cannot supply a message boundary.
|
|
22
|
+
if (this.encode && input.length === 0)
|
|
23
|
+
return Uint8Array.of(0);
|
|
24
|
+
const api = this.api;
|
|
25
|
+
const bytes = this.encode ? input : new Uint8Array(input.length + 4);
|
|
26
|
+
if (!this.encode) {
|
|
27
|
+
bytes.set(input);
|
|
28
|
+
bytes.set([0, 0, 255, 255], input.length);
|
|
29
|
+
}
|
|
30
|
+
const capacity = 16384;
|
|
31
|
+
const allocation = api.malloc(Math.max(1, bytes.length) + capacity + 8);
|
|
32
|
+
if (!allocation)
|
|
33
|
+
throw new RangeError("Memory limit exceeded");
|
|
34
|
+
const output = allocation + Math.max(1, bytes.length), state = output + capacity;
|
|
35
|
+
const chunks = [];
|
|
36
|
+
let remaining = bytes.length, offset = 0, length = 0;
|
|
37
|
+
try {
|
|
38
|
+
new Uint8Array(api.memory.buffer).set(bytes, allocation);
|
|
39
|
+
for (;;) {
|
|
40
|
+
const result = api.cz_process(this.context, allocation + offset, remaining, output, capacity, 2, state);
|
|
41
|
+
const view = new DataView(api.memory.buffer);
|
|
42
|
+
const available = view.getUint32(state, true), left = view.getUint32(state + 4, true);
|
|
43
|
+
if (result !== 0 && result !== -5)
|
|
44
|
+
throw new Error(message(api, api.cz_message(this.context)));
|
|
45
|
+
const written = capacity - available;
|
|
46
|
+
length += written;
|
|
47
|
+
if (!this.encode && length > MAX_MESSAGE_BYTES)
|
|
48
|
+
throw new Error("WebSocket message is too large.");
|
|
49
|
+
if (written)
|
|
50
|
+
chunks.push(new Uint8Array(api.memory.buffer, output, written).slice());
|
|
51
|
+
offset += remaining - left;
|
|
52
|
+
remaining = left;
|
|
53
|
+
if (remaining === 0 && available > 0)
|
|
54
|
+
break;
|
|
55
|
+
if (!written && result === -5)
|
|
56
|
+
throw new Error("Invalid WebSocket compressed message.");
|
|
57
|
+
}
|
|
58
|
+
const merged = new Uint8Array(length);
|
|
59
|
+
let position = 0;
|
|
60
|
+
for (const chunk of chunks) {
|
|
61
|
+
merged.set(chunk, position);
|
|
62
|
+
position += chunk.length;
|
|
63
|
+
}
|
|
64
|
+
if (this.noTakeover && api.cz_reset(this.context) !== 0)
|
|
65
|
+
throw new Error("Unable to reset WebSocket compression.");
|
|
66
|
+
if (this.encode) {
|
|
67
|
+
if (length < 4 ||
|
|
68
|
+
![0, 0, 255, 255].every((byte, i) => merged[length - 4 + i] === byte))
|
|
69
|
+
throw new Error("Invalid WebSocket compression boundary.");
|
|
70
|
+
return merged.subarray(0, length - 4);
|
|
71
|
+
}
|
|
72
|
+
return merged;
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
api.free(allocation);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
dispose() {
|
|
79
|
+
if (this.context) {
|
|
80
|
+
this.api.cz_free(this.context);
|
|
81
|
+
this.context = 0;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export function negotiateCompression(header) {
|
|
86
|
+
const parts = header.split(";").map((part) => part.trim());
|
|
87
|
+
if (parts.shift() !== "permessage-deflate")
|
|
88
|
+
throw new Error("Unexpected WebSocket extension.");
|
|
89
|
+
const parameters = new Map();
|
|
90
|
+
for (const part of parts) {
|
|
91
|
+
const match = /^([a-z_]+)(?:=(\d+))?$/.exec(part);
|
|
92
|
+
if (!match || parameters.has(match[1]))
|
|
93
|
+
throw new Error("Invalid WebSocket extension parameters.");
|
|
94
|
+
const [, name, value] = match;
|
|
95
|
+
if (![
|
|
96
|
+
"server_no_context_takeover",
|
|
97
|
+
"client_no_context_takeover",
|
|
98
|
+
"server_max_window_bits",
|
|
99
|
+
].includes(name))
|
|
100
|
+
throw new Error("Unexpected WebSocket extension parameter.");
|
|
101
|
+
if (name.endsWith("no_context_takeover")
|
|
102
|
+
? value !== undefined
|
|
103
|
+
: value === undefined || +value < 8 || +value > 15)
|
|
104
|
+
throw new Error("Invalid WebSocket extension parameters.");
|
|
105
|
+
parameters.set(name, value);
|
|
106
|
+
}
|
|
107
|
+
const encoder = new MessageCodec(true, 15, parameters.has("client_no_context_takeover"));
|
|
108
|
+
let decoder;
|
|
109
|
+
try {
|
|
110
|
+
decoder = new MessageCodec(false, Number(parameters.get("server_max_window_bits") ?? 15), parameters.has("server_no_context_takeover"));
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
encoder.dispose();
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
encode: (bytes) => encoder.run(bytes),
|
|
118
|
+
decode: (bytes) => decoder.run(bytes),
|
|
119
|
+
dispose() {
|
|
120
|
+
encoder.dispose();
|
|
121
|
+
decoder.dispose();
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** JSG signed integer conversion checks the range before truncating. */
|
|
2
|
+
export function int32(value) {
|
|
3
|
+
if (typeof value === "bigint")
|
|
4
|
+
throw new TypeError("Cannot convert a BigInt value to a number");
|
|
5
|
+
if (typeof value === "symbol")
|
|
6
|
+
throw new TypeError("Cannot convert a Symbol value to a number");
|
|
7
|
+
const number = +value;
|
|
8
|
+
if (!Number.isFinite(number))
|
|
9
|
+
return 0;
|
|
10
|
+
if (number < -2147483648 || number > 2147483647)
|
|
11
|
+
throw new TypeError("Value out of range. Must be between -2147483648 and 2147483647 (inclusive).");
|
|
12
|
+
return Math.trunc(number);
|
|
13
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Workers endpoint lifecycle shared by public globals and node:http. */
|
|
2
|
+
export declare class WebSocket extends EventTarget {
|
|
3
|
+
readonly CONNECTING: 0;
|
|
4
|
+
readonly READY_STATE_CONNECTING: 0;
|
|
5
|
+
readonly OPEN: 1;
|
|
6
|
+
readonly READY_STATE_OPEN: 1;
|
|
7
|
+
readonly CLOSING: 2;
|
|
8
|
+
readonly READY_STATE_CLOSING: 2;
|
|
9
|
+
readonly CLOSED: 3;
|
|
10
|
+
readonly READY_STATE_CLOSED: 3;
|
|
11
|
+
static readonly CONNECTING: 0;
|
|
12
|
+
static readonly READY_STATE_CONNECTING: 0;
|
|
13
|
+
static readonly OPEN: 1;
|
|
14
|
+
static readonly READY_STATE_OPEN: 1;
|
|
15
|
+
static readonly CLOSING: 2;
|
|
16
|
+
static readonly READY_STATE_CLOSING: 2;
|
|
17
|
+
static readonly CLOSED: 3;
|
|
18
|
+
static readonly READY_STATE_CLOSED: 3;
|
|
19
|
+
constructor(url: string | URL, protocols?: string | string[]);
|
|
20
|
+
get readyState(): number;
|
|
21
|
+
get url(): string | null;
|
|
22
|
+
get protocol(): string | null;
|
|
23
|
+
get extensions(): string | null;
|
|
24
|
+
get binaryType(): "blob" | "arraybuffer";
|
|
25
|
+
set binaryType(value: string);
|
|
26
|
+
accept(options?: {
|
|
27
|
+
allowHalfOpen?: boolean;
|
|
28
|
+
}): void;
|
|
29
|
+
send(value: unknown): void;
|
|
30
|
+
serializeAttachment(value: unknown): void;
|
|
31
|
+
deserializeAttachment(): unknown;
|
|
32
|
+
close(code?: number, reason?: string): void;
|
|
33
|
+
}
|
|
34
|
+
export declare class WebSocketPair {
|
|
35
|
+
readonly 0: WebSocket;
|
|
36
|
+
readonly 1: WebSocket;
|
|
37
|
+
constructor();
|
|
38
|
+
[Symbol.iterator](): Iterator<WebSocket>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { int32 } from "./conversion.js";
|
|
2
|
+
import { getNativeCall } from "../fetch/native-network.js";
|
|
3
|
+
import { platform } from "../../shared/globals/platform.js";
|
|
4
|
+
import { isAnyBuffer, bufferBytes, viewBytes, } from "../../shared/buffer-source.js";
|
|
5
|
+
import { Blob } from "../../compat/buffer/blob.js";
|
|
6
|
+
import { dictionary, webString } from "../body/index.js";
|
|
7
|
+
import { structuredClone, cloneAttachment } from "../structured-clone/index.js";
|
|
8
|
+
import { markWebSocket, markWebSocketPair } from "./brand.js";
|
|
9
|
+
import { CloseEvent } from "./close-event.js";
|
|
10
|
+
import { attachmentSize } from "./attachment-size.js";
|
|
11
|
+
import { nativeWebSocket, } from "./native.js";
|
|
12
|
+
const pairToken = Symbol("WebSocketPair endpoint");
|
|
13
|
+
const acceptMessage = "You must call one of accept() or state.acceptWebSocket() on this WebSocket before sending messages.";
|
|
14
|
+
const states = new WeakMap();
|
|
15
|
+
function state(socket) {
|
|
16
|
+
const result = states.get(socket);
|
|
17
|
+
if (!result)
|
|
18
|
+
throw new TypeError("Illegal invocation");
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
function syntax(message) {
|
|
22
|
+
throw new DOMException(`WebSocket Constructor: ${message}`, "SyntaxError");
|
|
23
|
+
}
|
|
24
|
+
function validate(input, protocols) {
|
|
25
|
+
const original = webString(input);
|
|
26
|
+
let url;
|
|
27
|
+
try {
|
|
28
|
+
url = new URL(original);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return syntax("The url is invalid.");
|
|
32
|
+
}
|
|
33
|
+
if (!url.hostname)
|
|
34
|
+
syntax("The url is invalid.");
|
|
35
|
+
if (!["ws:", "wss:"].includes(url.protocol))
|
|
36
|
+
syntax("The url scheme must be ws or wss.");
|
|
37
|
+
if (original.includes("#"))
|
|
38
|
+
syntax("The url fragment must be empty.");
|
|
39
|
+
const list = [];
|
|
40
|
+
if (Array.isArray(protocols)) {
|
|
41
|
+
// JSG converts arrays by indexed access, without consulting the iterator.
|
|
42
|
+
const length = protocols.length;
|
|
43
|
+
for (let index = 0; index < length; index++)
|
|
44
|
+
list.push(webString(protocols[index]));
|
|
45
|
+
}
|
|
46
|
+
else if (protocols !== undefined)
|
|
47
|
+
list.push(webString(protocols));
|
|
48
|
+
const seen = new Set();
|
|
49
|
+
for (const token of list) {
|
|
50
|
+
if (!/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(token))
|
|
51
|
+
syntax(Array.isArray(protocols)
|
|
52
|
+
? "One of the protocol header tokens is invalid."
|
|
53
|
+
: "The protocol header token is invalid.");
|
|
54
|
+
if (seen.has(token))
|
|
55
|
+
syntax("The protocols header cannot have repeating values.");
|
|
56
|
+
seen.add(token);
|
|
57
|
+
}
|
|
58
|
+
url.username = "";
|
|
59
|
+
url.password = "";
|
|
60
|
+
return { url, original, protocols: list };
|
|
61
|
+
}
|
|
62
|
+
function errorEvent(error) {
|
|
63
|
+
const message = `Uncaught ${String(error)}`;
|
|
64
|
+
if (typeof ErrorEvent === "function")
|
|
65
|
+
return new ErrorEvent("error", { error, message });
|
|
66
|
+
// Node's unit-test host has Event but no ErrorEvent. WebViews take the branch above.
|
|
67
|
+
const event = new Event("error");
|
|
68
|
+
Object.defineProperties(event, {
|
|
69
|
+
error: { value: error, enumerable: true, configurable: true },
|
|
70
|
+
message: { value: message, enumerable: true, configurable: true },
|
|
71
|
+
});
|
|
72
|
+
return event;
|
|
73
|
+
}
|
|
74
|
+
function enqueue(socket, delivery) {
|
|
75
|
+
const s = state(socket);
|
|
76
|
+
s.queue.push(delivery);
|
|
77
|
+
if (!s.accepted || s.scheduled)
|
|
78
|
+
return;
|
|
79
|
+
s.scheduled = true;
|
|
80
|
+
queueMicrotask(() => {
|
|
81
|
+
s.scheduled = false;
|
|
82
|
+
while (s.queue.length) {
|
|
83
|
+
const item = s.queue.shift();
|
|
84
|
+
if ("message" in item) {
|
|
85
|
+
if (s.receivedClose || s.failed)
|
|
86
|
+
continue;
|
|
87
|
+
const data = typeof item.message === "string"
|
|
88
|
+
? item.message
|
|
89
|
+
: s.binaryType === "blob"
|
|
90
|
+
? new Blob([item.message])
|
|
91
|
+
: item.message.slice().buffer;
|
|
92
|
+
socket.dispatchEvent(new MessageEvent("message", { data }));
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
if (s.receivedClose)
|
|
96
|
+
continue;
|
|
97
|
+
s.receivedClose = true;
|
|
98
|
+
if (!s.allowHalfOpen && !s.sentClose && item.clean)
|
|
99
|
+
sendClose(socket, item.code, item.reason);
|
|
100
|
+
const event = new CloseEvent("close", {
|
|
101
|
+
code: item.code,
|
|
102
|
+
reason: item.reason,
|
|
103
|
+
wasClean: item.clean,
|
|
104
|
+
});
|
|
105
|
+
socket.dispatchEvent(event);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function sendClose(socket, code, reason) {
|
|
111
|
+
const s = state(socket);
|
|
112
|
+
s.sentClose = true;
|
|
113
|
+
if (s.peer)
|
|
114
|
+
enqueue(s.peer, { code, reason, clean: true });
|
|
115
|
+
else
|
|
116
|
+
s.transport?.close(code, reason);
|
|
117
|
+
}
|
|
118
|
+
function hostTransport(url, protocols, observer) {
|
|
119
|
+
const host = new platform.WebSocket(url.href, [...protocols]);
|
|
120
|
+
host.binaryType = "arraybuffer";
|
|
121
|
+
host.addEventListener("open", () => observer.open(host.protocol, host.extensions));
|
|
122
|
+
host.addEventListener("message", (event) => observer.message(typeof event.data === "string" ? event.data : new Uint8Array(event.data)));
|
|
123
|
+
host.addEventListener("error", () => observer.error(new Error("WebSocket connection failed.")));
|
|
124
|
+
host.addEventListener("close", (event) => observer.close(event.code, event.reason, event.wasClean));
|
|
125
|
+
return {
|
|
126
|
+
send(value) {
|
|
127
|
+
host.send(value);
|
|
128
|
+
},
|
|
129
|
+
close(code, reason) {
|
|
130
|
+
if (code === 1005)
|
|
131
|
+
host.close();
|
|
132
|
+
else
|
|
133
|
+
host.close(code, reason);
|
|
134
|
+
},
|
|
135
|
+
dispose() {
|
|
136
|
+
host.close();
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/** Workers endpoint lifecycle shared by public globals and node:http. */
|
|
141
|
+
export class WebSocket extends EventTarget {
|
|
142
|
+
constructor(url, protocols) {
|
|
143
|
+
super();
|
|
144
|
+
if (!arguments.length)
|
|
145
|
+
throw new TypeError("Failed to construct 'WebSocket': constructor parameter 1 is not of type 'string'.");
|
|
146
|
+
const paired = url === pairToken;
|
|
147
|
+
const validated = paired ? undefined : validate(url, protocols);
|
|
148
|
+
const s = {
|
|
149
|
+
url: validated?.original ?? null,
|
|
150
|
+
protocol: "",
|
|
151
|
+
extensions: "",
|
|
152
|
+
connecting: !paired,
|
|
153
|
+
accepted: false,
|
|
154
|
+
failed: false,
|
|
155
|
+
sentClose: false,
|
|
156
|
+
receivedClose: false,
|
|
157
|
+
allowHalfOpen: false,
|
|
158
|
+
binaryType: "blob",
|
|
159
|
+
queue: [],
|
|
160
|
+
scheduled: false,
|
|
161
|
+
};
|
|
162
|
+
states.set(this, s);
|
|
163
|
+
markWebSocket(this);
|
|
164
|
+
if (paired)
|
|
165
|
+
return;
|
|
166
|
+
const observer = {
|
|
167
|
+
open: (protocol, extensions) => {
|
|
168
|
+
if (s.failed)
|
|
169
|
+
return;
|
|
170
|
+
s.connecting = false;
|
|
171
|
+
s.accepted = true;
|
|
172
|
+
s.protocol = protocol || null;
|
|
173
|
+
s.extensions = extensions || null;
|
|
174
|
+
this.dispatchEvent(new Event("open"));
|
|
175
|
+
},
|
|
176
|
+
message: (message) => enqueue(this, { message }),
|
|
177
|
+
close: (code, reason, clean) => {
|
|
178
|
+
s.accepted = true;
|
|
179
|
+
enqueue(this, { code, reason, clean });
|
|
180
|
+
},
|
|
181
|
+
error: (error) => {
|
|
182
|
+
if (s.failed)
|
|
183
|
+
return;
|
|
184
|
+
s.failed = true;
|
|
185
|
+
s.connecting = false;
|
|
186
|
+
const event = errorEvent(error);
|
|
187
|
+
this.dispatchEvent(event);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
s.transport = getNativeCall()
|
|
191
|
+
? nativeWebSocket(validated.url, validated.protocols, observer)
|
|
192
|
+
: hostTransport(validated.url, validated.protocols, observer);
|
|
193
|
+
}
|
|
194
|
+
get readyState() {
|
|
195
|
+
const s = state(this);
|
|
196
|
+
return s.failed || (s.sentClose && s.receivedClose)
|
|
197
|
+
? 3
|
|
198
|
+
: s.sentClose || s.receivedClose
|
|
199
|
+
? 2
|
|
200
|
+
: s.connecting
|
|
201
|
+
? 0
|
|
202
|
+
: 1;
|
|
203
|
+
}
|
|
204
|
+
get url() {
|
|
205
|
+
return state(this).url;
|
|
206
|
+
}
|
|
207
|
+
get protocol() {
|
|
208
|
+
return state(this).protocol;
|
|
209
|
+
}
|
|
210
|
+
get extensions() {
|
|
211
|
+
return state(this).extensions;
|
|
212
|
+
}
|
|
213
|
+
get binaryType() {
|
|
214
|
+
return state(this).binaryType;
|
|
215
|
+
}
|
|
216
|
+
set binaryType(value) {
|
|
217
|
+
const s = state(this);
|
|
218
|
+
value = webString(value);
|
|
219
|
+
if (value === "blob" || value === "arraybuffer")
|
|
220
|
+
s.binaryType = value;
|
|
221
|
+
}
|
|
222
|
+
accept(options) {
|
|
223
|
+
const s = state(this);
|
|
224
|
+
const allowHalfOpen = dictionary(options ?? {}).allowHalfOpen;
|
|
225
|
+
if (!s.peer)
|
|
226
|
+
throw new TypeError("Websockets obtained from the 'new WebSocket()' constructor cannot call accept");
|
|
227
|
+
if (s.accepted)
|
|
228
|
+
return;
|
|
229
|
+
if (allowHalfOpen !== undefined)
|
|
230
|
+
s.allowHalfOpen = Boolean(allowHalfOpen);
|
|
231
|
+
s.accepted = true;
|
|
232
|
+
if (s.queue.length) {
|
|
233
|
+
const queued = s.queue.splice(0);
|
|
234
|
+
for (const item of queued)
|
|
235
|
+
enqueue(this, item);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
send(value) {
|
|
239
|
+
const s = state(this);
|
|
240
|
+
if (!arguments.length)
|
|
241
|
+
throw new TypeError("Failed to execute 'send' on 'WebSocket': parameter 1 is not of type 'ArrayBuffer or ArrayBufferView or string'.");
|
|
242
|
+
const message = isAnyBuffer(value)
|
|
243
|
+
? bufferBytes(value).slice()
|
|
244
|
+
: ArrayBuffer.isView(value)
|
|
245
|
+
? viewBytes(value).slice()
|
|
246
|
+
: webString(value);
|
|
247
|
+
if (s.sentClose)
|
|
248
|
+
throw new TypeError("Can't call WebSocket send() after close().");
|
|
249
|
+
if (s.failed)
|
|
250
|
+
return;
|
|
251
|
+
if (!s.accepted)
|
|
252
|
+
throw new TypeError(acceptMessage);
|
|
253
|
+
if (s.peer)
|
|
254
|
+
enqueue(s.peer, { message });
|
|
255
|
+
else
|
|
256
|
+
s.transport.send(message);
|
|
257
|
+
}
|
|
258
|
+
serializeAttachment(value) {
|
|
259
|
+
const s = state(this);
|
|
260
|
+
if (!arguments.length)
|
|
261
|
+
throw new TypeError("Failed to execute 'serializeAttachment' on 'WebSocket': parameter 1 is not of type 'Value'.");
|
|
262
|
+
const snapshot = cloneAttachment(value);
|
|
263
|
+
const externals = { shared: false };
|
|
264
|
+
const bytes = attachmentSize(snapshot, externals);
|
|
265
|
+
if (bytes > 16384)
|
|
266
|
+
throw new Error(`A WebSocket 'attachment' cannot be larger than 16384 bytes.'attachment' was ${bytes} bytes.`);
|
|
267
|
+
// Commit only after cloning and accounting succeed, keeping the prior value
|
|
268
|
+
// intact on both unsupported input and oversize writes.
|
|
269
|
+
s.attachment = {
|
|
270
|
+
value: externals.shared ? undefined : snapshot,
|
|
271
|
+
shared: externals.shared,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
deserializeAttachment() {
|
|
275
|
+
const attachment = state(this).attachment;
|
|
276
|
+
if (!attachment)
|
|
277
|
+
return null;
|
|
278
|
+
if (attachment.shared)
|
|
279
|
+
throw new Error("Unable to deserialize cloned data.");
|
|
280
|
+
return structuredClone(attachment.value);
|
|
281
|
+
}
|
|
282
|
+
close(code, reason) {
|
|
283
|
+
const s = state(this);
|
|
284
|
+
const number = code === undefined ? undefined : int32(code);
|
|
285
|
+
const text = reason === undefined
|
|
286
|
+
? ""
|
|
287
|
+
: new TextDecoder("utf-8", { ignoreBOM: true }).decode(new TextEncoder().encode(webString(reason)));
|
|
288
|
+
if (number !== undefined &&
|
|
289
|
+
(number < 1000 ||
|
|
290
|
+
number >= 5000 ||
|
|
291
|
+
[1004, 1005, 1006, 1015].includes(number)))
|
|
292
|
+
throw new DOMException(`Invalid WebSocket close code: ${number}.`, "InvalidAccessError");
|
|
293
|
+
if (new TextEncoder().encode(text).length > 123)
|
|
294
|
+
throw new DOMException("WebSocket close reason must not be longer than 123 bytes when UTF-8 encoded.", "SyntaxError");
|
|
295
|
+
if (reason !== undefined && number === undefined)
|
|
296
|
+
throw new DOMException("If you specify a WebSocket close reason, you must also specify a code.", "InvalidAccessError");
|
|
297
|
+
if (s.sentClose || s.failed)
|
|
298
|
+
return;
|
|
299
|
+
if (s.connecting) {
|
|
300
|
+
// Workers releases the pending connection synchronously, then reports
|
|
301
|
+
// cancellation as an asynchronous network error/abnormal close.
|
|
302
|
+
s.connecting = false;
|
|
303
|
+
s.transport?.dispose();
|
|
304
|
+
queueMicrotask(() => {
|
|
305
|
+
if (s.failed)
|
|
306
|
+
return;
|
|
307
|
+
s.failed = true;
|
|
308
|
+
s.accepted = true;
|
|
309
|
+
const event = errorEvent(new Error("Network connection lost."));
|
|
310
|
+
this.dispatchEvent(event);
|
|
311
|
+
enqueue(this, {
|
|
312
|
+
code: 1006,
|
|
313
|
+
reason: "WebSocket disconnected without sending Close frame.",
|
|
314
|
+
clean: false,
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (!s.accepted)
|
|
320
|
+
throw new TypeError(acceptMessage);
|
|
321
|
+
sendClose(this, number ?? 1005, text);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
export class WebSocketPair {
|
|
325
|
+
constructor() {
|
|
326
|
+
markWebSocketPair(this);
|
|
327
|
+
const a = new WebSocket(pairToken), b = new WebSocket(pairToken);
|
|
328
|
+
state(a).peer = b;
|
|
329
|
+
state(b).peer = a;
|
|
330
|
+
Object.defineProperties(this, {
|
|
331
|
+
0: { get: () => a, enumerable: true, configurable: true },
|
|
332
|
+
1: { get: () => b, enumerable: true, configurable: true },
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
*[Symbol.iterator]() {
|
|
336
|
+
yield this[0];
|
|
337
|
+
yield this[1];
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
for (const [name, value] of Object.entries({
|
|
341
|
+
CONNECTING: 0,
|
|
342
|
+
OPEN: 1,
|
|
343
|
+
CLOSING: 2,
|
|
344
|
+
CLOSED: 3,
|
|
345
|
+
})) {
|
|
346
|
+
for (const key of [name, `READY_STATE_${name}`]) {
|
|
347
|
+
const descriptor = {
|
|
348
|
+
value,
|
|
349
|
+
enumerable: true,
|
|
350
|
+
configurable: true,
|
|
351
|
+
writable: false,
|
|
352
|
+
};
|
|
353
|
+
Object.defineProperty(WebSocket, key, descriptor);
|
|
354
|
+
Object.defineProperty(WebSocket.prototype, key, descriptor);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
for (const key of Object.getOwnPropertyNames(WebSocket.prototype)) {
|
|
358
|
+
if (key !== "constructor")
|
|
359
|
+
Object.defineProperty(WebSocket.prototype, key, { enumerable: true });
|
|
360
|
+
}
|
|
361
|
+
for (const [Constructor, name] of [
|
|
362
|
+
[WebSocket, "WebSocket"],
|
|
363
|
+
[WebSocketPair, "WebSocketPair"],
|
|
364
|
+
]) {
|
|
365
|
+
Object.defineProperty(Constructor, "name", { value: name });
|
|
366
|
+
Object.defineProperty(Constructor, "length", { value: 0 });
|
|
367
|
+
Object.defineProperty(Constructor.prototype, Symbol.toStringTag, {
|
|
368
|
+
value: name,
|
|
369
|
+
configurable: true,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
for (const name of [
|
|
373
|
+
"accept",
|
|
374
|
+
"send",
|
|
375
|
+
"close",
|
|
376
|
+
"serializeAttachment",
|
|
377
|
+
"deserializeAttachment",
|
|
378
|
+
])
|
|
379
|
+
Object.defineProperty(WebSocket.prototype[name], "length", { value: 0 });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type Frame = {
|
|
2
|
+
opcode: number;
|
|
3
|
+
final: boolean;
|
|
4
|
+
payload: Uint8Array;
|
|
5
|
+
compressed?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare const MAX_MESSAGE_BYTES: number;
|
|
8
|
+
export declare function encodeFrame(opcode: number, payload: Uint8Array, compressed?: boolean): Uint8Array;
|
|
9
|
+
/** Incremental parser: validate advertised lengths before allocating payloads. */
|
|
10
|
+
export declare class FrameReader {
|
|
11
|
+
#private;
|
|
12
|
+
private allowCompression;
|
|
13
|
+
constructor(allowCompression?: boolean);
|
|
14
|
+
push(input: Uint8Array, consume: (frame: Frame) => void): void;
|
|
15
|
+
get incomplete(): boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function decodeClose(payload: Uint8Array): {
|
|
18
|
+
code: number;
|
|
19
|
+
reason: string;
|
|
20
|
+
};
|
|
21
|
+
export declare function closePayload(code: number, reason: string): Uint8Array;
|