cf-workers-native 0.1.5 → 0.1.7
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 +129 -59
- 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 +182 -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 +249 -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 +596 -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 +283 -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,121 @@
|
|
|
1
|
+
export const MAX_MESSAGE_BYTES = 32 * 1024 * 1024;
|
|
2
|
+
export function encodeFrame(opcode, payload, compressed = false) {
|
|
3
|
+
const length = payload.byteLength;
|
|
4
|
+
const extra = length < 126 ? 0 : length <= 65535 ? 2 : 8;
|
|
5
|
+
const frame = new Uint8Array(6 + extra + length);
|
|
6
|
+
frame[0] = 0x80 | opcode | (compressed ? 0x40 : 0);
|
|
7
|
+
frame[1] = 0x80 | (extra === 0 ? length : extra === 2 ? 126 : 127);
|
|
8
|
+
const view = new DataView(frame.buffer);
|
|
9
|
+
if (extra === 2)
|
|
10
|
+
view.setUint16(2, length);
|
|
11
|
+
if (extra === 8)
|
|
12
|
+
view.setBigUint64(2, BigInt(length));
|
|
13
|
+
const mask = crypto.getRandomValues(new Uint8Array(4));
|
|
14
|
+
frame.set(mask, 2 + extra);
|
|
15
|
+
for (let i = 0; i < length; i++)
|
|
16
|
+
frame[6 + extra + i] = payload[i] ^ mask[i % 4];
|
|
17
|
+
return frame;
|
|
18
|
+
}
|
|
19
|
+
/** Incremental parser: validate advertised lengths before allocating payloads. */
|
|
20
|
+
export class FrameReader {
|
|
21
|
+
allowCompression;
|
|
22
|
+
#header = new Uint8Array(10);
|
|
23
|
+
#headerBytes = 0;
|
|
24
|
+
#headerLength = 2;
|
|
25
|
+
#payload;
|
|
26
|
+
#payloadBytes = 0;
|
|
27
|
+
#opcode = 0;
|
|
28
|
+
#final = false;
|
|
29
|
+
#compressed = false;
|
|
30
|
+
constructor(allowCompression = false) {
|
|
31
|
+
this.allowCompression = allowCompression;
|
|
32
|
+
}
|
|
33
|
+
push(input, consume) {
|
|
34
|
+
let offset = 0;
|
|
35
|
+
while (offset < input.length) {
|
|
36
|
+
if (!this.#payload) {
|
|
37
|
+
const take = Math.min(this.#headerLength - this.#headerBytes, input.length - offset);
|
|
38
|
+
this.#header.set(input.subarray(offset, offset + take), this.#headerBytes);
|
|
39
|
+
this.#headerBytes += take;
|
|
40
|
+
offset += take;
|
|
41
|
+
if (this.#headerBytes < this.#headerLength)
|
|
42
|
+
continue;
|
|
43
|
+
const first = this.#header[0], second = this.#header[1];
|
|
44
|
+
const code = second & 127;
|
|
45
|
+
if (this.#headerLength === 2) {
|
|
46
|
+
if (first & 0x30 ||
|
|
47
|
+
(first & 0x40 &&
|
|
48
|
+
(!this.allowCompression ||
|
|
49
|
+
(first & 15) === 0 ||
|
|
50
|
+
(first & 15) >= 8)))
|
|
51
|
+
throw new Error("Unexpected WebSocket reserved bits.");
|
|
52
|
+
if (second & 0x80)
|
|
53
|
+
throw new Error("Server WebSocket frames must not be masked.");
|
|
54
|
+
this.#compressed = !!(first & 0x40);
|
|
55
|
+
this.#opcode = first & 15;
|
|
56
|
+
this.#final = !!(first & 128);
|
|
57
|
+
if (![0, 1, 2, 8, 9, 10].includes(this.#opcode))
|
|
58
|
+
throw new Error("Invalid WebSocket opcode.");
|
|
59
|
+
if (this.#opcode >= 8 && (!this.#final || code > 125))
|
|
60
|
+
throw new Error("Invalid WebSocket control frame.");
|
|
61
|
+
this.#headerLength = code === 126 ? 4 : code === 127 ? 10 : 2;
|
|
62
|
+
if (this.#headerBytes < this.#headerLength)
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const view = new DataView(this.#header.buffer);
|
|
66
|
+
const length = code < 126
|
|
67
|
+
? code
|
|
68
|
+
: code === 126
|
|
69
|
+
? view.getUint16(2)
|
|
70
|
+
: Number(view.getBigUint64(2));
|
|
71
|
+
if ((code === 126 && length < 126) || (code === 127 && length <= 65535))
|
|
72
|
+
throw new Error("Non-minimal WebSocket payload length.");
|
|
73
|
+
if (!Number.isSafeInteger(length) || length > MAX_MESSAGE_BYTES)
|
|
74
|
+
throw new Error("WebSocket message is too large.");
|
|
75
|
+
this.#payload = new Uint8Array(length);
|
|
76
|
+
this.#payloadBytes = 0;
|
|
77
|
+
}
|
|
78
|
+
const take = Math.min(this.#payload.length - this.#payloadBytes, input.length - offset);
|
|
79
|
+
this.#payload.set(input.subarray(offset, offset + take), this.#payloadBytes);
|
|
80
|
+
offset += take;
|
|
81
|
+
this.#payloadBytes += take;
|
|
82
|
+
if (this.#payloadBytes === this.#payload.length) {
|
|
83
|
+
const frame = {
|
|
84
|
+
opcode: this.#opcode,
|
|
85
|
+
final: this.#final,
|
|
86
|
+
payload: this.#payload,
|
|
87
|
+
...(this.#compressed ? { compressed: true } : {}),
|
|
88
|
+
};
|
|
89
|
+
this.#payload = undefined;
|
|
90
|
+
this.#headerBytes = 0;
|
|
91
|
+
this.#headerLength = 2;
|
|
92
|
+
consume(frame);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
get incomplete() {
|
|
97
|
+
return this.#headerBytes !== 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export function decodeClose(payload) {
|
|
101
|
+
if (payload.length === 1)
|
|
102
|
+
throw new Error("Invalid WebSocket close payload.");
|
|
103
|
+
if (payload.length === 0)
|
|
104
|
+
return { code: 1005, reason: "" };
|
|
105
|
+
const code = new DataView(payload.buffer, payload.byteOffset, payload.byteLength).getUint16(0);
|
|
106
|
+
if (code < 1000 || code >= 5000 || [1004, 1005, 1006, 1015].includes(code))
|
|
107
|
+
throw new Error("Invalid WebSocket close code.");
|
|
108
|
+
return {
|
|
109
|
+
code,
|
|
110
|
+
reason: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(payload.subarray(2)),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export function closePayload(code, reason) {
|
|
114
|
+
if (code === 1005)
|
|
115
|
+
return new Uint8Array();
|
|
116
|
+
const text = new TextEncoder().encode(reason);
|
|
117
|
+
const payload = new Uint8Array(2 + text.length);
|
|
118
|
+
new DataView(payload.buffer).setUint16(0, code);
|
|
119
|
+
payload.set(text, 2);
|
|
120
|
+
return payload;
|
|
121
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { connect } from "../../compat/net/sockets.js";
|
|
2
|
+
export interface WebSocketObserver {
|
|
3
|
+
open(protocol: string, extensions: string): void;
|
|
4
|
+
message(value: string | Uint8Array): void;
|
|
5
|
+
close(code: number, reason: string, clean: boolean): void;
|
|
6
|
+
error(error: Error): void;
|
|
7
|
+
}
|
|
8
|
+
export interface WebSocketTransport {
|
|
9
|
+
send(value: string | Uint8Array): void;
|
|
10
|
+
close(code: number, reason: string): void;
|
|
11
|
+
dispose(): void;
|
|
12
|
+
}
|
|
13
|
+
/** Native TCP/TLS keeps ws:// usable without a WebView mixed-content exception. */
|
|
14
|
+
export declare function nativeWebSocket(url: URL, protocols: readonly string[], observer: WebSocketObserver, dial?: typeof connect): WebSocketTransport;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { connect } from "../../compat/net/sockets.js";
|
|
2
|
+
import { socketReader } from "../../compat/net/reader.js";
|
|
3
|
+
import { closePayload, decodeClose, encodeFrame, FrameReader, MAX_MESSAGE_BYTES, } from "./frames.js";
|
|
4
|
+
import { negotiateCompression } from "./compression.js";
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
7
|
+
const guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
8
|
+
const base64 = (bytes) => btoa(String.fromCharCode(...bytes));
|
|
9
|
+
/** Native TCP/TLS keeps ws:// usable without a WebView mixed-content exception. */
|
|
10
|
+
export function nativeWebSocket(url, protocols, observer, dial = connect) {
|
|
11
|
+
let socket;
|
|
12
|
+
let writer;
|
|
13
|
+
let disposed = false, failed = false, sentClose = false, receivedClose = false, remoteInitiatedClose = false;
|
|
14
|
+
let compression;
|
|
15
|
+
let writes = Promise.resolve();
|
|
16
|
+
let fragments = [];
|
|
17
|
+
let fragmentBytes = 0, fragmentOpcode = 0;
|
|
18
|
+
let fragmentCompressed = false;
|
|
19
|
+
const dispose = () => {
|
|
20
|
+
if (disposed)
|
|
21
|
+
return;
|
|
22
|
+
disposed = true;
|
|
23
|
+
compression?.dispose();
|
|
24
|
+
void socket?.close().catch(() => { });
|
|
25
|
+
};
|
|
26
|
+
const fail = (cause) => {
|
|
27
|
+
if (disposed || failed)
|
|
28
|
+
return;
|
|
29
|
+
failed = true;
|
|
30
|
+
const error = cause instanceof Error ? cause : new Error(String(cause));
|
|
31
|
+
observer.error(error);
|
|
32
|
+
if (!receivedClose)
|
|
33
|
+
observer.close(1006, "WebSocket disconnected without sending Close frame.", false);
|
|
34
|
+
dispose();
|
|
35
|
+
};
|
|
36
|
+
const write = (opcode, bytes) => {
|
|
37
|
+
// Snapshot before queuing, including a caller-owned view.
|
|
38
|
+
const compress = !!compression && (opcode === 1 || opcode === 2);
|
|
39
|
+
const frame = encodeFrame(opcode, compress ? compression.encode(bytes) : bytes, compress);
|
|
40
|
+
writes = writes.then(async () => {
|
|
41
|
+
if (disposed)
|
|
42
|
+
return;
|
|
43
|
+
if (!writer)
|
|
44
|
+
throw new Error("WebSocket connection is not open.");
|
|
45
|
+
await writer.write(frame);
|
|
46
|
+
});
|
|
47
|
+
void writes.catch(fail);
|
|
48
|
+
};
|
|
49
|
+
const transport = {
|
|
50
|
+
send(value) {
|
|
51
|
+
write(typeof value === "string" ? 1 : 2, typeof value === "string" ? encoder.encode(value) : value);
|
|
52
|
+
},
|
|
53
|
+
close(code, reason) {
|
|
54
|
+
if (sentClose || disposed)
|
|
55
|
+
return;
|
|
56
|
+
sentClose = true;
|
|
57
|
+
write(8, closePayload(code, reason));
|
|
58
|
+
// A peer-initiated close still has a live read side. Let its actual EOF
|
|
59
|
+
// settle the transport; workerd reports that loss after the close event.
|
|
60
|
+
if (receivedClose && !remoteInitiatedClose)
|
|
61
|
+
void writes.then(dispose, fail);
|
|
62
|
+
},
|
|
63
|
+
dispose,
|
|
64
|
+
};
|
|
65
|
+
const frame = ({ opcode, final, payload, compressed }) => {
|
|
66
|
+
if (disposed || receivedClose)
|
|
67
|
+
return;
|
|
68
|
+
if (opcode === 9) {
|
|
69
|
+
write(10, payload);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (opcode === 10)
|
|
73
|
+
return;
|
|
74
|
+
if (opcode === 8) {
|
|
75
|
+
const close = decodeClose(payload);
|
|
76
|
+
remoteInitiatedClose = !sentClose;
|
|
77
|
+
receivedClose = true;
|
|
78
|
+
observer.close(close.code, close.reason, true);
|
|
79
|
+
if (sentClose && !remoteInitiatedClose)
|
|
80
|
+
void writes.then(dispose, fail);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (opcode === 0 ? fragmentOpcode === 0 : fragmentOpcode !== 0)
|
|
84
|
+
throw new Error("Invalid WebSocket continuation frame.");
|
|
85
|
+
if (opcode !== 0) {
|
|
86
|
+
fragmentOpcode = opcode;
|
|
87
|
+
fragmentCompressed = !!compressed;
|
|
88
|
+
}
|
|
89
|
+
fragmentBytes += payload.byteLength;
|
|
90
|
+
if (fragmentBytes > MAX_MESSAGE_BYTES)
|
|
91
|
+
throw new Error("WebSocket message is too large.");
|
|
92
|
+
fragments.push(payload);
|
|
93
|
+
if (!final)
|
|
94
|
+
return;
|
|
95
|
+
const message = new Uint8Array(fragmentBytes);
|
|
96
|
+
let offset = 0;
|
|
97
|
+
for (const part of fragments) {
|
|
98
|
+
message.set(part, offset);
|
|
99
|
+
offset += part.byteLength;
|
|
100
|
+
}
|
|
101
|
+
const text = fragmentOpcode === 1;
|
|
102
|
+
fragments = [];
|
|
103
|
+
fragmentBytes = 0;
|
|
104
|
+
fragmentOpcode = 0;
|
|
105
|
+
const delivered = fragmentCompressed
|
|
106
|
+
? compression.decode(message)
|
|
107
|
+
: message;
|
|
108
|
+
observer.message(text ? decoder.decode(delivered) : delivered);
|
|
109
|
+
};
|
|
110
|
+
void (async () => {
|
|
111
|
+
try {
|
|
112
|
+
const secure = url.protocol === "wss:";
|
|
113
|
+
socket = dial(`${url.hostname}:${url.port || (secure ? 443 : 80)}`, {
|
|
114
|
+
secureTransport: secure ? "on" : "off",
|
|
115
|
+
expectedServerHostname: url.hostname.replace(/^\[|\]$/g, ""),
|
|
116
|
+
});
|
|
117
|
+
await socket.opened;
|
|
118
|
+
if (disposed) {
|
|
119
|
+
await socket.close();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
writer = socket.writable.getWriter();
|
|
123
|
+
const reader = socketReader(socket.readable);
|
|
124
|
+
const key = base64(crypto.getRandomValues(new Uint8Array(16)));
|
|
125
|
+
const expected = base64(new Uint8Array(await crypto.subtle.digest("SHA-1", encoder.encode(key + guid))));
|
|
126
|
+
await writer.write(encoder.encode([
|
|
127
|
+
`GET ${url.pathname || "/"}${url.search} HTTP/1.1`,
|
|
128
|
+
`Host: ${url.host}`,
|
|
129
|
+
"Upgrade: websocket",
|
|
130
|
+
"Connection: Upgrade",
|
|
131
|
+
"Sec-WebSocket-Version: 13",
|
|
132
|
+
"Sec-WebSocket-Extensions: permessage-deflate",
|
|
133
|
+
`Sec-WebSocket-Key: ${key}`,
|
|
134
|
+
...(protocols.length
|
|
135
|
+
? [`Sec-WebSocket-Protocol: ${protocols.join(", ")}`]
|
|
136
|
+
: []),
|
|
137
|
+
"",
|
|
138
|
+
"",
|
|
139
|
+
].join("\r\n")));
|
|
140
|
+
let pending = new Uint8Array(0);
|
|
141
|
+
let ready = false;
|
|
142
|
+
let parser = new FrameReader();
|
|
143
|
+
while (!disposed) {
|
|
144
|
+
const next = await reader.read(new Uint8Array(65536));
|
|
145
|
+
if (next.done) {
|
|
146
|
+
if (!receivedClose)
|
|
147
|
+
throw new Error("WebSocket disconnected without sending Close frame.");
|
|
148
|
+
if (remoteInitiatedClose)
|
|
149
|
+
observer.error(new Error("Network connection lost."));
|
|
150
|
+
dispose();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
let bytes = next.value;
|
|
154
|
+
if (!ready) {
|
|
155
|
+
const combined = new Uint8Array(pending.length + bytes.length);
|
|
156
|
+
combined.set(pending);
|
|
157
|
+
combined.set(bytes, pending.length);
|
|
158
|
+
pending = combined;
|
|
159
|
+
let end = -1;
|
|
160
|
+
for (let i = 3; i < pending.length; i++) {
|
|
161
|
+
if (pending[i - 3] === 13 &&
|
|
162
|
+
pending[i - 2] === 10 &&
|
|
163
|
+
pending[i - 1] === 13 &&
|
|
164
|
+
pending[i] === 10) {
|
|
165
|
+
end = i + 1;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (end === -1) {
|
|
170
|
+
if (pending.length > 128 * 1024)
|
|
171
|
+
throw new Error("WebSocket response headers are too large.");
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (end > 128 * 1024)
|
|
175
|
+
throw new Error("WebSocket response headers are too large.");
|
|
176
|
+
const lines = new TextDecoder()
|
|
177
|
+
.decode(pending.subarray(0, end))
|
|
178
|
+
.split("\r\n");
|
|
179
|
+
const status = /^HTTP\/1\.[01] (\d{3})(?: |$)/.exec(lines.shift());
|
|
180
|
+
if (!status || status[1] !== "101")
|
|
181
|
+
throw new TypeError(`Failed to establish the WebSocket connection: expected server to reply with HTTP status code 101 (switching protocols), but received ${status?.[1] ?? 0} instead.`);
|
|
182
|
+
const headers = new Map();
|
|
183
|
+
for (const line of lines) {
|
|
184
|
+
if (!line)
|
|
185
|
+
continue;
|
|
186
|
+
const colon = line.indexOf(":");
|
|
187
|
+
if (colon < 1)
|
|
188
|
+
throw new Error("Invalid WebSocket response header.");
|
|
189
|
+
const name = line.slice(0, colon).toLowerCase();
|
|
190
|
+
const values = headers.get(name) ?? [];
|
|
191
|
+
values.push(line.slice(colon + 1).trim());
|
|
192
|
+
headers.set(name, values);
|
|
193
|
+
}
|
|
194
|
+
const tokens = (name) => (headers.get(name) ?? []).flatMap((value) => value
|
|
195
|
+
.toLowerCase()
|
|
196
|
+
.split(",")
|
|
197
|
+
.map((token) => token.trim()));
|
|
198
|
+
if (!tokens("upgrade").includes("websocket") ||
|
|
199
|
+
!tokens("connection").includes("upgrade") ||
|
|
200
|
+
headers.get("sec-websocket-accept")?.length !== 1 ||
|
|
201
|
+
headers.get("sec-websocket-accept")[0] !== expected)
|
|
202
|
+
throw new Error("Invalid WebSocket upgrade response.");
|
|
203
|
+
const selected = headers.get("sec-websocket-protocol") ?? [];
|
|
204
|
+
if (selected.length > 1 ||
|
|
205
|
+
(selected.length && !protocols.includes(selected[0])))
|
|
206
|
+
throw new Error("Invalid WebSocket response protocol.");
|
|
207
|
+
const extensions = headers.get("sec-websocket-extensions") ?? [];
|
|
208
|
+
if (extensions.length > 1)
|
|
209
|
+
throw new Error("Unexpected WebSocket extension.");
|
|
210
|
+
if (extensions.length)
|
|
211
|
+
compression = negotiateCompression(extensions[0]);
|
|
212
|
+
parser = new FrameReader(!!compression);
|
|
213
|
+
ready = true;
|
|
214
|
+
observer.open(selected[0] ?? "", extensions[0] ?? "");
|
|
215
|
+
bytes = pending.subarray(end);
|
|
216
|
+
pending = new Uint8Array(0);
|
|
217
|
+
}
|
|
218
|
+
parser.push(bytes, frame);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
fail(error);
|
|
223
|
+
}
|
|
224
|
+
})();
|
|
225
|
+
return transport;
|
|
226
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Headers } from "../../../browser/headers/index.js";
|
|
2
|
+
import { Response as WorkersResponse } from "../../../browser/response/index.js";
|
|
1
3
|
import { getHandler, kSetIncomingBody, } from "../../../compat/http/registry.js";
|
|
2
4
|
function validatePort(port) {
|
|
3
5
|
if (typeof port !== "number" ||
|
|
@@ -71,19 +73,21 @@ export async function handleAsNodeRequest(descriptor, request, env, ctx) {
|
|
|
71
73
|
reject(error);
|
|
72
74
|
res.destroy(error);
|
|
73
75
|
});
|
|
74
|
-
res.once("finish", () => {
|
|
75
|
-
request.signal.removeEventListener("abort", onAbort);
|
|
76
|
-
if (!req.complete)
|
|
77
|
-
req.destroy();
|
|
78
|
-
});
|
|
79
76
|
res.once("headers", () => {
|
|
80
77
|
try {
|
|
81
78
|
const headers = new Headers();
|
|
82
79
|
for (const [key, value] of Object.entries(res.getHeaders())) {
|
|
83
|
-
for (const item of Array.isArray(value)
|
|
80
|
+
for (const item of Array.isArray(value)
|
|
81
|
+
? key.toLowerCase() === "cookie" && value.length >= 2
|
|
82
|
+
? [value.join("; ")]
|
|
83
|
+
: value
|
|
84
|
+
: [value])
|
|
84
85
|
headers.append(key, String(item));
|
|
85
86
|
}
|
|
86
|
-
|
|
87
|
+
// This bridge exposes the standard Fetch contract. Its HTTP body chunks
|
|
88
|
+
// are ArrayBuffer-backed; owned Headers also implements the iterator
|
|
89
|
+
// protocol without depending on a particular TypeScript DOM lib version.
|
|
90
|
+
resolve(new WorkersResponse(request.method === "HEAD" ||
|
|
87
91
|
[204, 205, 304].includes(res.statusCode)
|
|
88
92
|
? null
|
|
89
93
|
: res.body, {
|
|
@@ -97,26 +101,10 @@ export async function handleAsNodeRequest(descriptor, request, env, ctx) {
|
|
|
97
101
|
res.destroy(error instanceof Error ? error : new Error(String(error)));
|
|
98
102
|
}
|
|
99
103
|
});
|
|
100
|
-
const onAbort = () => {
|
|
101
|
-
const reason = request.signal.reason;
|
|
102
|
-
const error = reason instanceof Error
|
|
103
|
-
? reason
|
|
104
|
-
: new Error("The operation was aborted", { cause: reason });
|
|
105
|
-
req.destroy(error);
|
|
106
|
-
res.destroy(error);
|
|
107
|
-
};
|
|
108
104
|
res.once("close", () => {
|
|
109
|
-
request.signal.removeEventListener("abort", onAbort);
|
|
110
|
-
if (!req.complete)
|
|
111
|
-
req.destroy();
|
|
112
105
|
if (!res.finished)
|
|
113
106
|
reject(new Error("Premature close"));
|
|
114
107
|
});
|
|
115
|
-
request.signal.addEventListener("abort", onAbort, { once: true });
|
|
116
|
-
if (request.signal.aborted) {
|
|
117
|
-
onAbort();
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
108
|
try {
|
|
121
109
|
server.emit("connection", server, req);
|
|
122
110
|
server.emit("request", req, res);
|
|
@@ -17,3 +17,23 @@ pinned workerd module; internal helpers are not added to the public facade.
|
|
|
17
17
|
X509 certificate wrappers and safe name rendering are adapted from
|
|
18
18
|
`src/node/internal/crypto_x509.ts` and `src/workerd/api/crypto/x509.c++` at
|
|
19
19
|
workerd commit `5358e3d337336eaf696a48bc1563d808b10feba5` (Apache-2.0).
|
|
20
|
+
|
|
21
|
+
Hash/HMAC and PBKDF2 frontends and digest/KDF test fixtures are adapted from
|
|
22
|
+
`src/node/internal/crypto_hash.ts`, `src/node/internal/crypto_pbkdf2.ts` and
|
|
23
|
+
`src/workerd/api/node/tests/crypto_{hash,hmac,pbkdf2,hkdf}-test.js` at the same
|
|
24
|
+
pinned workerd commit. The Apache-2.0 and Node.js MIT headers are retained.
|
|
25
|
+
Imports use the owned adapters. Latin-1 fixture expectations written as literal
|
|
26
|
+
caret notation upstream are decoded using independently verified digest bytes;
|
|
27
|
+
comments identify those adaptations. Float16 input-brand cases require the
|
|
28
|
+
engine's intrinsic; native evidence records its availability.
|
|
29
|
+
|
|
30
|
+
The Cipheriv/Decipheriv frontend is adapted from `src/node/internal/crypto_cipher.ts`
|
|
31
|
+
and lifecycle behavior from `src/workerd/api/node/crypto.c++` at that same workerd
|
|
32
|
+
revision. The cipher fixture retains the upstream `crypto_cipher-test.js` notices.
|
|
33
|
+
BoringSSL commit `427ec40cc8edc545253289232e885708c409e5ea` supplies the bundled cipher
|
|
34
|
+
primitives; its license and bundled third-party notices are in `LICENSE.boringssl`.
|
|
35
|
+
|
|
36
|
+
PEM/DER key parsing is adapted from `src/ncrypto.cpp` in Node.js ncrypto
|
|
37
|
+
`v1.1.4`, the version pinned by workerd. Copyright (c) 2025 Node.js; the MIT
|
|
38
|
+
license is retained in `LICENSE.ncrypto`. Parsing uses the bundled BoringSSL
|
|
39
|
+
backend before transferring owned key material to the OpenSSL operation backend.
|