cf-workers-native 0.0.0-stage → 0.1.2-beta.local.sha2f5e4e6956f4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Package.swift +25 -0
- package/README.md +92 -2
- package/dist/android.d.ts +2 -0
- package/dist/android.js +1 -0
- package/dist/browser/abort/index.d.ts +13 -0
- package/dist/browser/abort/index.js +66 -0
- package/dist/browser/body/index.d.ts +24 -0
- package/dist/browser/body/index.js +116 -0
- package/dist/browser/body/stream-body.d.ts +13 -0
- package/dist/browser/body/stream-body.js +84 -0
- package/dist/browser/documents/body.d.ts +3 -0
- package/dist/browser/documents/body.js +48 -0
- package/dist/browser/documents/form-body.d.ts +1 -0
- package/dist/browser/documents/form-body.js +54 -0
- package/dist/browser/documents/host.d.ts +11 -0
- package/dist/browser/documents/host.js +256 -0
- package/dist/browser/documents/links-source.d.ts +1 -0
- package/dist/browser/documents/links-source.js +1 -0
- package/dist/browser/documents/links.d.ts +1 -0
- package/dist/browser/documents/links.js +33 -0
- package/dist/browser/documents/page-config.d.ts +5 -0
- package/dist/browser/documents/page-config.js +5 -0
- package/dist/browser/documents/page-source.d.ts +1 -0
- package/dist/browser/documents/page-source.js +1 -0
- package/dist/browser/documents/page.d.ts +2 -0
- package/dist/browser/documents/page.js +153 -0
- package/dist/browser/documents/registration.d.ts +5 -0
- package/dist/browser/documents/registration.js +75 -0
- package/dist/browser/documents/runtime.d.ts +7 -0
- package/dist/browser/documents/runtime.js +161 -0
- package/dist/browser/documents/service-worker.js +285 -0
- package/dist/browser/encoding/index.d.ts +7 -0
- package/dist/browser/encoding/index.js +3 -0
- package/dist/browser/fetch/LICENSE-3RD-PARTY.md +9 -0
- package/dist/browser/fetch/LICENSE-APACHE +177 -0
- package/dist/browser/fetch/host-call.d.ts +3 -0
- package/dist/browser/fetch/host-call.js +67 -0
- package/dist/browser/fetch/index.d.ts +5 -0
- package/dist/browser/fetch/index.js +96 -0
- package/dist/browser/fetch/interop.d.ts +6 -0
- package/dist/browser/fetch/interop.js +48 -0
- package/dist/browser/fetch/native-network.d.ts +20 -0
- package/dist/browser/fetch/native-network.js +233 -0
- package/dist/browser/fetch/outbound.d.ts +2 -0
- package/dist/browser/fetch/outbound.js +11 -0
- package/dist/browser/fetch/relay.d.ts +7 -0
- package/dist/browser/fetch/relay.js +100 -0
- package/dist/browser/fetch/scope.d.ts +8 -0
- package/dist/browser/fetch/scope.js +108 -0
- package/dist/browser/fetch/transport.d.ts +39 -0
- package/dist/browser/fetch/transport.js +136 -0
- package/dist/browser/headers/LICENSE-3RD-PARTY.md +9 -0
- package/dist/browser/headers/LICENSE-APACHE +177 -0
- package/dist/browser/headers/index.d.ts +20 -0
- package/dist/browser/headers/index.js +194 -0
- package/dist/browser/isolated/bridge.d.ts +8 -0
- package/dist/browser/isolated/bridge.js +267 -0
- package/dist/browser/isolated/protocol.d.ts +40 -0
- package/dist/browser/isolated/protocol.js +1 -0
- package/dist/browser/isolated/transport.d.ts +2 -0
- package/dist/browser/isolated/transport.js +157 -0
- package/dist/browser/request/LICENSE-3RD-PARTY.md +10 -0
- package/dist/browser/request/LICENSE-APACHE +177 -0
- package/dist/browser/request/index.d.ts +48 -0
- package/dist/browser/request/index.js +182 -0
- package/dist/browser/response/LICENSE-3RD-PARTY.md +10 -0
- package/dist/browser/response/LICENSE-APACHE +177 -0
- package/dist/browser/response/index.d.ts +43 -0
- package/dist/browser/response/index.js +192 -0
- package/dist/browser/response/status.d.ts +1 -0
- package/dist/browser/response/status.js +75 -0
- package/dist/browser/structured-clone/index.d.ts +1 -0
- package/dist/browser/structured-clone/index.js +479 -0
- package/dist/cloudflare/apis/cloudflare-node/index.d.ts +18 -0
- package/dist/cloudflare/apis/cloudflare-node/index.js +150 -0
- package/dist/cloudflare/apis/cloudflare-workers/index.d.ts +8 -0
- package/dist/cloudflare/apis/cloudflare-workers/index.js +37 -0
- package/dist/cloudflare/apis/types.d.ts +7 -0
- package/dist/cloudflare/apis/types.js +1 -0
- package/dist/cloudflare/bindings/config.d.ts +42 -0
- package/dist/cloudflare/bindings/config.js +280 -0
- package/dist/cloudflare/bindings/kv/contract.d.ts +6 -0
- package/dist/cloudflare/bindings/kv/contract.js +181 -0
- package/dist/cloudflare/bindings/kv/index.d.ts +18 -0
- package/dist/cloudflare/bindings/kv/index.js +61 -0
- package/dist/cloudflare/bindings/kv/storage.d.ts +25 -0
- package/dist/cloudflare/bindings/kv/storage.js +320 -0
- package/dist/cloudflare/bindings/kv/types.d.ts +50 -0
- package/dist/cloudflare/bindings/kv/types.js +1 -0
- package/dist/cloudflare/bindings/remote.d.ts +8 -0
- package/dist/cloudflare/bindings/remote.js +261 -0
- package/dist/cloudflare/workers/contract.d.ts +8 -0
- package/dist/cloudflare/workers/contract.js +65 -0
- package/dist/cloudflare/workers/feature.d.ts +10 -0
- package/dist/cloudflare/workers/feature.js +28 -0
- package/dist/cloudflare/workers/runtime.d.ts +23 -0
- package/dist/cloudflare/workers/runtime.js +121 -0
- package/dist/cloudflare/workers/types.d.ts +49 -0
- package/dist/cloudflare/workers/types.js +1 -0
- package/dist/cloudflare/workers/worker-runtime.d.ts +44 -0
- package/dist/cloudflare/workers/worker-runtime.js +267 -0
- package/dist/compat/assert/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/assert/LICENSE-APACHE +177 -0
- package/dist/compat/assert/assertion-error.d.ts +26 -0
- package/dist/compat/assert/assertion-error.js +468 -0
- package/dist/compat/assert/diff.d.ts +26 -0
- package/dist/compat/assert/diff.js +329 -0
- package/dist/compat/assert/errors.d.ts +11 -0
- package/dist/compat/assert/errors.js +55 -0
- package/dist/compat/assert/index.cjs +24 -0
- package/dist/compat/assert/index.d.cts +43 -0
- package/dist/compat/assert/index.d.ts +50 -0
- package/dist/compat/assert/index.js +858 -0
- package/dist/compat/async-hooks/LICENSE-APACHE +177 -0
- package/dist/compat/async-hooks/NOTICE.md +8 -0
- package/dist/compat/async-hooks/callbacks.d.ts +7 -0
- package/dist/compat/async-hooks/callbacks.js +18 -0
- package/dist/compat/async-hooks/hooks.d.ts +10 -0
- package/dist/compat/async-hooks/hooks.js +111 -0
- package/dist/compat/async-hooks/index.d.ts +41 -0
- package/dist/compat/async-hooks/index.js +298 -0
- package/dist/compat/buffer/LICENSE-3RD-PARTY.md +15 -0
- package/dist/compat/buffer/LICENSE-APACHE +177 -0
- package/dist/compat/buffer/blob.d.ts +22 -0
- package/dist/compat/buffer/blob.js +210 -0
- package/dist/compat/buffer/bytes.d.ts +34 -0
- package/dist/compat/buffer/bytes.js +228 -0
- package/dist/compat/buffer/errors.d.ts +41 -0
- package/dist/compat/buffer/errors.js +265 -0
- package/dist/compat/buffer/index.d.ts +25 -0
- package/dist/compat/buffer/index.js +24 -0
- package/dist/compat/buffer/internal-buffer.d.ts +148 -0
- package/dist/compat/buffer/internal-buffer.js +1809 -0
- package/dist/compat/buffer/support.d.ts +13 -0
- package/dist/compat/buffer/support.js +65 -0
- package/dist/compat/child-process/LICENSE-3RD-PARTY.md +8 -0
- package/dist/compat/child-process/LICENSE-APACHE +177 -0
- package/dist/compat/child-process/errors.d.ts +4 -0
- package/dist/compat/child-process/errors.js +6 -0
- package/dist/compat/child-process/index.d.ts +49 -0
- package/dist/compat/child-process/index.js +96 -0
- package/dist/compat/cluster/LICENSE-3RD-PARTY.md +40 -0
- package/dist/compat/cluster/LICENSE-APACHE +177 -0
- package/dist/compat/cluster/errors.d.ts +4 -0
- package/dist/compat/cluster/errors.js +6 -0
- package/dist/compat/cluster/index.d.ts +47 -0
- package/dist/compat/cluster/index.js +88 -0
- package/dist/compat/constants/LICENSE-3RD-PARTY.md +6 -0
- package/dist/compat/constants/LICENSE-APACHE +177 -0
- package/dist/compat/constants/index.d.ts +481 -0
- package/dist/compat/constants/index.js +488 -0
- package/dist/compat/dgram/LICENSE-3RD-PARTY.md +8 -0
- package/dist/compat/dgram/LICENSE-APACHE +177 -0
- package/dist/compat/dgram/index.d.ts +10 -0
- package/dist/compat/dgram/index.js +132 -0
- package/dist/compat/diagnostics-channel/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/diagnostics-channel/LICENSE-APACHE +177 -0
- package/dist/compat/diagnostics-channel/index.d.ts +73 -0
- package/dist/compat/diagnostics-channel/index.js +261 -0
- package/dist/compat/diagnostics-channel/native.d.ts +26 -0
- package/dist/compat/diagnostics-channel/native.js +154 -0
- package/dist/compat/dns/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/dns/LICENSE-APACHE +177 -0
- package/dist/compat/dns/client.d.ts +72 -0
- package/dist/compat/dns/client.js +161 -0
- package/dist/compat/dns/constants.d.ts +27 -0
- package/dist/compat/dns/constants.js +31 -0
- package/dist/compat/dns/errors.d.ts +8 -0
- package/dist/compat/dns/errors.js +12 -0
- package/dist/compat/dns/index.d.ts +97 -0
- package/dist/compat/dns/index.js +127 -0
- package/dist/compat/dns/parser.d.ts +18 -0
- package/dist/compat/dns/parser.js +144 -0
- package/dist/compat/dns/promise-resolver.d.ts +82 -0
- package/dist/compat/dns/promise-resolver.js +113 -0
- package/dist/compat/dns/promises.d.ts +50 -0
- package/dist/compat/dns/promises.js +53 -0
- package/dist/compat/dns/resolver.d.ts +34 -0
- package/dist/compat/dns/resolver.js +415 -0
- package/dist/compat/domain/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/domain/LICENSE-APACHE +177 -0
- package/dist/compat/domain/index.d.ts +22 -0
- package/dist/compat/domain/index.js +79 -0
- package/dist/compat/fs/index.d.ts +85 -0
- package/dist/compat/fs/index.js +255 -0
- package/dist/compat/fs/promises/index.d.ts +3 -0
- package/dist/compat/fs/promises/index.js +3 -0
- package/dist/compat/http/index.d.ts +322 -0
- package/dist/compat/http/index.js +1356 -0
- package/dist/compat/http/registry.d.ts +19 -0
- package/dist/compat/http/registry.js +13 -0
- package/dist/compat/http-agent/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/http-agent/LICENSE-APACHE +177 -0
- package/dist/compat/http-agent/index.d.ts +7 -0
- package/dist/compat/http-agent/index.js +10 -0
- package/dist/compat/http-client/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/http-client/LICENSE-APACHE +177 -0
- package/dist/compat/http-client/index.d.ts +6 -0
- package/dist/compat/http-client/index.js +7 -0
- package/dist/compat/http-incoming/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/http-incoming/LICENSE-APACHE +177 -0
- package/dist/compat/http-incoming/index.d.ts +6 -0
- package/dist/compat/http-incoming/index.js +9 -0
- package/dist/compat/http2/LICENSE-3RD-PARTY.md +29 -0
- package/dist/compat/http2/LICENSE-APACHE +177 -0
- package/dist/compat/http2/constants.d.ts +255 -0
- package/dist/compat/http2/constants.js +515 -0
- package/dist/compat/http2/errors.d.ts +4 -0
- package/dist/compat/http2/errors.js +6 -0
- package/dist/compat/http2/index.d.ts +283 -0
- package/dist/compat/http2/index.js +68 -0
- package/dist/compat/https/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/https/LICENSE-APACHE +177 -0
- package/dist/compat/https/agent.d.ts +10 -0
- package/dist/compat/https/agent.js +99 -0
- package/dist/compat/https/errors.d.ts +4 -0
- package/dist/compat/https/errors.js +6 -0
- package/dist/compat/https/index.d.ts +20 -0
- package/dist/compat/https/index.js +50 -0
- package/dist/compat/https/server.d.ts +8 -0
- package/dist/compat/https/server.js +27 -0
- package/dist/compat/immediate/index.d.ts +11 -0
- package/dist/compat/immediate/index.js +58 -0
- package/dist/compat/inspector/LICENSE-3RD-PARTY.md +34 -0
- package/dist/compat/inspector/LICENSE-APACHE +177 -0
- package/dist/compat/inspector/errors.d.ts +4 -0
- package/dist/compat/inspector/errors.js +6 -0
- package/dist/compat/inspector/index.d.ts +18 -0
- package/dist/compat/inspector/index.js +117 -0
- package/dist/compat/inspector/promises.d.ts +18 -0
- package/dist/compat/inspector/promises.js +114 -0
- package/dist/compat/module/index.cjs +2 -0
- package/dist/compat/module/index.d.cts +2 -0
- package/dist/compat/module/index.d.ts +11 -0
- package/dist/compat/module/index.js +44 -0
- package/dist/compat/module/resolve-unavailable.d.ts +1 -0
- package/dist/compat/module/resolve-unavailable.js +3 -0
- package/dist/compat/net/LICENSE-3RD-PARTY.md +15 -0
- package/dist/compat/net/LICENSE-APACHE +177 -0
- package/dist/compat/net/error-base.d.ts +14 -0
- package/dist/compat/net/error-base.js +62 -0
- package/dist/compat/net/errors.d.ts +76 -0
- package/dist/compat/net/errors.js +333 -0
- package/dist/compat/net/index.d.ts +23 -0
- package/dist/compat/net/index.js +46 -0
- package/dist/compat/net/internal-net.d.ts +236 -0
- package/dist/compat/net/internal-net.js +2292 -0
- package/dist/compat/net/ports.d.ts +32 -0
- package/dist/compat/net/ports.js +149 -0
- package/dist/compat/net/reader.d.ts +8 -0
- package/dist/compat/net/reader.js +36 -0
- package/dist/compat/net/sockets.d.ts +20 -0
- package/dist/compat/net/sockets.js +195 -0
- package/dist/compat/net/validators.d.ts +18 -0
- package/dist/compat/net/validators.js +154 -0
- package/dist/compat/os/LICENSE-3RD-PARTY.md +8 -0
- package/dist/compat/os/LICENSE-APACHE +177 -0
- package/dist/compat/os/index.d.ts +329 -0
- package/dist/compat/os/index.js +278 -0
- package/dist/compat/path/LICENSE-3RD-PARTY.md +35 -0
- package/dist/compat/path/LICENSE-APACHE +177 -0
- package/dist/compat/path/index.d.ts +4 -0
- package/dist/compat/path/index.js +11 -0
- package/dist/compat/path/internal-path.d.ts +95 -0
- package/dist/compat/path/internal-path.js +1542 -0
- package/dist/compat/path/posix/index.d.ts +4 -0
- package/dist/compat/path/posix/index.js +5 -0
- package/dist/compat/path/win32/index.d.ts +4 -0
- package/dist/compat/path/win32/index.js +5 -0
- package/dist/compat/perf-hooks/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/perf-hooks/LICENSE-APACHE +177 -0
- package/dist/compat/perf-hooks/index.d.ts +85 -0
- package/dist/compat/perf-hooks/index.js +79 -0
- package/dist/compat/perf-hooks/performance.d.ts +96 -0
- package/dist/compat/perf-hooks/performance.js +445 -0
- package/dist/compat/punycode/LICENSE-3RD-PARTY.md +7 -0
- package/dist/compat/punycode/LICENSE-APACHE +177 -0
- package/dist/compat/punycode/index.d.ts +93 -0
- package/dist/compat/punycode/index.js +410 -0
- package/dist/compat/querystring/LICENSE-3RD-PARTY.md +35 -0
- package/dist/compat/querystring/LICENSE-APACHE +177 -0
- package/dist/compat/querystring/index.d.ts +14 -0
- package/dist/compat/querystring/index.js +18 -0
- package/dist/compat/querystring/internal-querystring.d.ts +22 -0
- package/dist/compat/querystring/internal-querystring.js +498 -0
- package/dist/compat/readline/LICENSE-3RD-PARTY.md +37 -0
- package/dist/compat/readline/LICENSE-APACHE +177 -0
- package/dist/compat/readline/index.d.ts +21 -0
- package/dist/compat/readline/index.js +56 -0
- package/dist/compat/readline/interface.d.ts +22 -0
- package/dist/compat/readline/interface.js +85 -0
- package/dist/compat/readline/internal-promises.d.ts +32 -0
- package/dist/compat/readline/internal-promises.js +104 -0
- package/dist/compat/readline/promises.d.ts +8 -0
- package/dist/compat/readline/promises.js +32 -0
- package/dist/compat/repl/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/repl/LICENSE-APACHE +177 -0
- package/dist/compat/repl/builtin-modules.d.ts +1 -0
- package/dist/compat/repl/builtin-modules.js +101 -0
- package/dist/compat/repl/errors.d.ts +4 -0
- package/dist/compat/repl/errors.js +6 -0
- package/dist/compat/repl/index.d.ts +53 -0
- package/dist/compat/repl/index.js +106 -0
- package/dist/compat/sqlite/LICENSE-3RD-PARTY.md +6 -0
- package/dist/compat/sqlite/LICENSE-APACHE +177 -0
- package/dist/compat/sqlite/index.d.ts +12 -0
- package/dist/compat/sqlite/index.js +101 -0
- package/dist/compat/sqlite/native.d.ts +16 -0
- package/dist/compat/sqlite/native.js +33 -0
- package/dist/compat/stream/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream/LICENSE-APACHE +177 -0
- package/dist/compat/stream/add-abort-signal.d.ts +4 -0
- package/dist/compat/stream/add-abort-signal.js +73 -0
- package/dist/compat/stream/compose.d.ts +1 -0
- package/dist/compat/stream/compose.js +313 -0
- package/dist/compat/stream/destroy.d.ts +5 -0
- package/dist/compat/stream/destroy.js +315 -0
- package/dist/compat/stream/duplex.d.ts +48 -0
- package/dist/compat/stream/duplex.js +789 -0
- package/dist/compat/stream/end-of-stream.d.ts +16 -0
- package/dist/compat/stream/end-of-stream.js +269 -0
- package/dist/compat/stream/errors.d.ts +40 -0
- package/dist/compat/stream/errors.js +86 -0
- package/dist/compat/stream/events.d.ts +6 -0
- package/dist/compat/stream/events.js +28 -0
- package/dist/compat/stream/index.cjs +27 -0
- package/dist/compat/stream/index.d.cts +25 -0
- package/dist/compat/stream/index.d.ts +20 -0
- package/dist/compat/stream/index.js +55 -0
- package/dist/compat/stream/legacy.d.ts +8 -0
- package/dist/compat/stream/legacy.js +105 -0
- package/dist/compat/stream/once.d.ts +7 -0
- package/dist/compat/stream/once.js +13 -0
- package/dist/compat/stream/pipeline.d.ts +5 -0
- package/dist/compat/stream/pipeline.js +567 -0
- package/dist/compat/stream/process.d.ts +1 -0
- package/dist/compat/stream/process.js +4 -0
- package/dist/compat/stream/promises.d.ts +7 -0
- package/dist/compat/stream/promises.js +60 -0
- package/dist/compat/stream/readable.d.ts +29 -0
- package/dist/compat/stream/readable.js +2267 -0
- package/dist/compat/stream/state.d.ts +8 -0
- package/dist/compat/stream/state.js +63 -0
- package/dist/compat/stream/transform.d.ts +2 -0
- package/dist/compat/stream/transform.js +202 -0
- package/dist/compat/stream/util.d.ts +55 -0
- package/dist/compat/stream/util.js +345 -0
- package/dist/compat/stream/validators.d.ts +2 -0
- package/dist/compat/stream/validators.js +14 -0
- package/dist/compat/stream/writable.d.ts +21 -0
- package/dist/compat/stream/writable.js +1325 -0
- package/dist/compat/stream-consumers/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/stream-consumers/LICENSE-APACHE +177 -0
- package/dist/compat/stream-consumers/index.d.ts +14 -0
- package/dist/compat/stream-consumers/index.js +62 -0
- package/dist/compat/stream-consumers/iterable.d.ts +1 -0
- package/dist/compat/stream-consumers/iterable.js +34 -0
- package/dist/compat/stream-duplex/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream-duplex/LICENSE-APACHE +177 -0
- package/dist/compat/stream-duplex/index.cjs +3 -0
- package/dist/compat/stream-duplex/index.d.cts +2 -0
- package/dist/compat/stream-duplex/index.d.ts +3 -0
- package/dist/compat/stream-duplex/index.js +8 -0
- package/dist/compat/stream-passthrough/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream-passthrough/LICENSE-APACHE +177 -0
- package/dist/compat/stream-passthrough/index.cjs +3 -0
- package/dist/compat/stream-passthrough/index.d.cts +2 -0
- package/dist/compat/stream-passthrough/index.d.ts +3 -0
- package/dist/compat/stream-passthrough/index.js +8 -0
- package/dist/compat/stream-promises/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream-promises/LICENSE-APACHE +177 -0
- package/dist/compat/stream-promises/index.d.ts +1 -0
- package/dist/compat/stream-promises/index.js +1 -0
- package/dist/compat/stream-readable/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream-readable/LICENSE-APACHE +177 -0
- package/dist/compat/stream-readable/index.cjs +3 -0
- package/dist/compat/stream-readable/index.d.cts +2 -0
- package/dist/compat/stream-readable/index.d.ts +3 -0
- package/dist/compat/stream-readable/index.js +8 -0
- package/dist/compat/stream-transform/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream-transform/LICENSE-APACHE +177 -0
- package/dist/compat/stream-transform/index.cjs +3 -0
- package/dist/compat/stream-transform/index.d.cts +2 -0
- package/dist/compat/stream-transform/index.d.ts +3 -0
- package/dist/compat/stream-transform/index.js +8 -0
- package/dist/compat/stream-web/LICENSE-MIT +22 -0
- package/dist/compat/stream-web/NOTICE.md +3 -0
- package/dist/compat/stream-web/context.d.ts +2 -0
- package/dist/compat/stream-web/context.js +35 -0
- package/dist/compat/stream-web/index.d.ts +80 -0
- package/dist/compat/stream-web/index.js +129 -0
- package/dist/compat/stream-web/interop.d.ts +6 -0
- package/dist/compat/stream-web/interop.js +72 -0
- package/dist/compat/stream-web/ponyfill.d.ts +1 -0
- package/dist/compat/stream-web/ponyfill.js +2093 -0
- package/dist/compat/stream-web/runtime.d.ts +16 -0
- package/dist/compat/stream-web/runtime.js +51 -0
- package/dist/compat/stream-writable/LICENSE-3RD-PARTY.md +13 -0
- package/dist/compat/stream-writable/LICENSE-APACHE +177 -0
- package/dist/compat/stream-writable/index.cjs +3 -0
- package/dist/compat/stream-writable/index.d.cts +2 -0
- package/dist/compat/stream-writable/index.d.ts +3 -0
- package/dist/compat/stream-writable/index.js +8 -0
- package/dist/compat/string-decoder/LICENSE-3RD-PARTY.md +38 -0
- package/dist/compat/string-decoder/LICENSE-APACHE +177 -0
- package/dist/compat/string-decoder/index.d.ts +33 -0
- package/dist/compat/string-decoder/index.js +518 -0
- package/dist/compat/test/LICENSE-3RD-PARTY.md +37 -0
- package/dist/compat/test/LICENSE-APACHE +177 -0
- package/dist/compat/test/index.d.ts +9 -0
- package/dist/compat/test/index.js +11 -0
- package/dist/compat/test/mock.d.ts +103 -0
- package/dist/compat/test/mock.js +397 -0
- package/dist/compat/test/validators.d.ts +2 -0
- package/dist/compat/test/validators.js +14 -0
- package/dist/compat/timers/LICENSE-3RD-PARTY.md +7 -0
- package/dist/compat/timers/LICENSE-APACHE +177 -0
- package/dist/compat/timers/errors.d.ts +28 -0
- package/dist/compat/timers/errors.js +206 -0
- package/dist/compat/timers/host.d.ts +11 -0
- package/dist/compat/timers/host.js +60 -0
- package/dist/compat/timers/index.cjs +3 -0
- package/dist/compat/timers/index.d.cts +23 -0
- package/dist/compat/timers/index.d.ts +36 -0
- package/dist/compat/timers/index.js +20 -0
- package/dist/compat/timers/installation.d.ts +4 -0
- package/dist/compat/timers/installation.js +61 -0
- package/dist/compat/timers/internal-promises.d.ts +19 -0
- package/dist/compat/timers/internal-promises.js +173 -0
- package/dist/compat/timers/promises.cjs +2 -0
- package/dist/compat/timers/promises.d.cts +12 -0
- package/dist/compat/timers/promises.d.ts +14 -0
- package/dist/compat/timers/promises.js +3 -0
- package/dist/compat/timers/timers.d.ts +32 -0
- package/dist/compat/timers/timers.js +147 -0
- package/dist/compat/timers/validation.d.ts +5 -0
- package/dist/compat/timers/validation.js +22 -0
- package/dist/compat/timers/wait.d.ts +4 -0
- package/dist/compat/timers/wait.js +91 -0
- package/dist/compat/tls-common/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls-common/LICENSE-APACHE +177 -0
- package/dist/compat/tls-common/index.d.ts +14 -0
- package/dist/compat/tls-common/index.js +88 -0
- package/dist/compat/trace-events/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/trace-events/LICENSE-APACHE +177 -0
- package/dist/compat/trace-events/index.d.ts +18 -0
- package/dist/compat/trace-events/index.js +88 -0
- package/dist/compat/tty/LICENSE-3RD-PARTY.md +8 -0
- package/dist/compat/tty/LICENSE-APACHE +177 -0
- package/dist/compat/tty/index.d.ts +17 -0
- package/dist/compat/tty/index.js +101 -0
- package/dist/compat/url/LICENSE-3RD-PARTY.md +63 -0
- package/dist/compat/url/LICENSE-ADA +18 -0
- package/dist/compat/url/LICENSE-APACHE +177 -0
- package/dist/compat/url/LICENSE-UNICODE +39 -0
- package/dist/compat/url/LICENSE-WHATWG-URL +21 -0
- package/dist/compat/url/brand.d.ts +4 -0
- package/dist/compat/url/brand.js +10 -0
- package/dist/compat/url/constants.d.ts +27 -0
- package/dist/compat/url/constants.js +28 -0
- package/dist/compat/url/errors.d.ts +44 -0
- package/dist/compat/url/errors.js +248 -0
- package/dist/compat/url/idna-data.d.ts +3958 -0
- package/dist/compat/url/idna-data.js +22 -0
- package/dist/compat/url/idna.d.ts +9 -0
- package/dist/compat/url/idna.js +47 -0
- package/dist/compat/url/index.d.ts +48 -0
- package/dist/compat/url/index.js +39 -0
- package/dist/compat/url/installation.d.ts +4 -0
- package/dist/compat/url/installation.js +61 -0
- package/dist/compat/url/internal-url.d.ts +34 -0
- package/dist/compat/url/internal-url.js +264 -0
- package/dist/compat/url/legacy-url.d.ts +28 -0
- package/dist/compat/url/legacy-url.js +870 -0
- package/dist/compat/url/parser-platform.d.ts +3 -0
- package/dist/compat/url/parser-platform.js +26 -0
- package/dist/compat/url/standard-url.d.ts +2 -0
- package/dist/compat/url/standard-url.js +3189 -0
- package/dist/compat/url/unicode.d.ts +2 -0
- package/dist/compat/url/unicode.js +119 -0
- package/dist/compat/url/validation.d.ts +2 -0
- package/dist/compat/url/validation.js +9 -0
- package/dist/compat/url/web-url.d.ts +27 -0
- package/dist/compat/url/web-url.js +119 -0
- package/dist/compat/util/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/util/LICENSE-APACHE +177 -0
- package/dist/compat/util/additional.d.ts +11 -0
- package/dist/compat/util/additional.js +121 -0
- package/dist/compat/util/comparison-types.d.ts +16 -0
- package/dist/compat/util/comparison-types.js +50 -0
- package/dist/compat/util/comparisons.d.ts +1 -0
- package/dist/compat/util/comparisons.js +527 -0
- package/dist/compat/util/core.d.ts +18 -0
- package/dist/compat/util/core.js +265 -0
- package/dist/compat/util/debuglog.d.ts +1 -0
- package/dist/compat/util/debuglog.js +78 -0
- package/dist/compat/util/index.d.ts +35 -0
- package/dist/compat/util/index.js +77 -0
- package/dist/compat/util/inspect-binding.d.ts +13 -0
- package/dist/compat/util/inspect-binding.js +39 -0
- package/dist/compat/util/inspect-types.d.ts +13 -0
- package/dist/compat/util/inspect-types.js +30 -0
- package/dist/compat/util/inspect.d.ts +152 -0
- package/dist/compat/util/inspect.js +2503 -0
- package/dist/compat/util/mime.d.ts +24 -0
- package/dist/compat/util/mime.js +284 -0
- package/dist/compat/util/properties.d.ts +9 -0
- package/dist/compat/util/properties.js +76 -0
- package/dist/compat/v8/LICENSE-3RD-PARTY.md +6 -0
- package/dist/compat/v8/LICENSE-APACHE +177 -0
- package/dist/compat/v8/errors.d.ts +5 -0
- package/dist/compat/v8/errors.js +7 -0
- package/dist/compat/v8/index.d.ts +106 -0
- package/dist/compat/v8/index.js +280 -0
- package/dist/compat/vm/LICENSE-3RD-PARTY.md +39 -0
- package/dist/compat/vm/LICENSE-APACHE +177 -0
- package/dist/compat/vm/errors.d.ts +5 -0
- package/dist/compat/vm/errors.js +7 -0
- package/dist/compat/vm/index.d.ts +47 -0
- package/dist/compat/vm/index.js +231 -0
- package/dist/compat/vm/validation.d.ts +3 -0
- package/dist/compat/vm/validation.js +14 -0
- package/dist/compat/wasi/LICENSE-3RD-PARTY.md +8 -0
- package/dist/compat/wasi/LICENSE-APACHE +177 -0
- package/dist/compat/wasi/errors.d.ts +4 -0
- package/dist/compat/wasi/errors.js +6 -0
- package/dist/compat/wasi/index.d.ts +15 -0
- package/dist/compat/wasi/index.js +44 -0
- package/dist/compat/worker-threads/LICENSE-3RD-PARTY.md +5 -0
- package/dist/compat/worker-threads/LICENSE-APACHE +177 -0
- package/dist/compat/worker-threads/errors.d.ts +4 -0
- package/dist/compat/worker-threads/errors.js +6 -0
- package/dist/compat/worker-threads/index.d.ts +71 -0
- package/dist/compat/worker-threads/index.js +138 -0
- package/dist/compat/worker-threads/message-channel.d.ts +18 -0
- package/dist/compat/worker-threads/message-channel.js +157 -0
- package/dist/configured.d.ts +3 -0
- package/dist/configured.js +4 -0
- package/dist/environment.d.ts +4 -0
- package/dist/environment.js +18 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +107 -0
- package/dist/ios.d.ts +2 -0
- package/dist/ios.js +1 -0
- package/dist/native-entry.d.ts +8 -0
- package/dist/native-entry.js +67 -0
- package/dist/shared/brotli/LICENSE-APACHE +201 -0
- package/dist/shared/brotli/LICENSE-MIT +9 -0
- package/dist/shared/brotli/NOTICE +3 -0
- package/dist/shared/brotli/index.d.ts +2 -0
- package/dist/shared/brotli/index.js +288 -0
- package/dist/shared/contracts/index.d.ts +42 -0
- package/dist/shared/contracts/index.js +88 -0
- package/dist/shared/contracts/testing.d.ts +22 -0
- package/dist/shared/contracts/testing.js +57 -0
- package/dist/shared/globals/index.d.ts +3 -0
- package/dist/shared/globals/index.js +87 -0
- package/dist/shared/globals/inject.d.ts +2 -0
- package/dist/shared/globals/inject.js +3 -0
- package/dist/shared/globals/install.d.ts +1 -0
- package/dist/shared/globals/install.js +15 -0
- package/dist/shared/globals/node-install.d.ts +1 -0
- package/dist/shared/globals/node-install.js +4 -0
- package/dist/shared/globals/platform.d.ts +104 -0
- package/dist/shared/globals/platform.js +37 -0
- package/dist/shared/globals/runtime.d.ts +6 -0
- package/dist/shared/globals/runtime.js +8 -0
- package/dist/shared/globals/symbols.d.ts +4 -0
- package/dist/shared/globals/symbols.js +35 -0
- package/dist/shared/globals/web.d.ts +4 -0
- package/dist/shared/globals/web.js +108 -0
- package/dist/shared/http-wire/LICENSE.llhttp +22 -0
- package/dist/shared/http-wire/LICENSE.undici +21 -0
- package/dist/shared/http-wire/NOTICE.md +14 -0
- package/dist/shared/http-wire/index.d.ts +27 -0
- package/dist/shared/http-wire/index.js +132 -0
- package/dist/shared/http-wire/wasm.d.ts +2 -0
- package/dist/shared/http-wire/wasm.js +2 -0
- package/dist/tooling/async.d.ts +7 -0
- package/dist/tooling/async.js +99 -0
- package/dist/tooling/build-isolation.d.ts +4 -0
- package/dist/tooling/build-isolation.js +34 -0
- package/dist/tooling/documents.d.ts +7 -0
- package/dist/tooling/documents.js +26 -0
- package/dist/tooling/esbuild.d.ts +15 -0
- package/dist/tooling/esbuild.js +193 -0
- package/dist/tooling/files.d.ts +2 -0
- package/dist/tooling/files.js +18 -0
- package/dist/tooling/opennext/build.d.ts +1 -0
- package/dist/tooling/opennext/build.js +121 -0
- package/dist/tooling/opennext/index.d.ts +3 -0
- package/dist/tooling/opennext/index.js +213 -0
- package/dist/tooling/opennext/plan.d.ts +9 -0
- package/dist/tooling/opennext/plan.js +36 -0
- package/dist/tooling/opennext/safety.d.ts +4 -0
- package/dist/tooling/opennext/safety.js +36 -0
- package/dist/tooling/route-permissions.d.ts +3 -0
- package/dist/tooling/route-permissions.js +15 -0
- package/dist/tooling/server-modules.d.ts +30 -0
- package/dist/tooling/server-modules.js +98 -0
- package/dist/tooling/server-only-html.d.ts +1 -0
- package/dist/tooling/server-only-html.js +66 -0
- package/dist/tooling/server-only.d.ts +6 -0
- package/dist/tooling/server-only.js +53 -0
- package/dist/tooling/server.d.ts +25 -0
- package/dist/tooling/server.js +981 -0
- package/dist/tooling/shared.d.ts +13 -0
- package/dist/tooling/shared.js +130 -0
- package/dist/tooling/vite.d.ts +18 -0
- package/dist/tooling/vite.js +448 -0
- package/dist/tooling/void/index.d.ts +3 -0
- package/dist/tooling/void/index.js +205 -0
- package/dist/tooling/void/plan.d.ts +17 -0
- package/dist/tooling/void/plan.js +288 -0
- package/native/android/build.gradle +68 -0
- package/native/android/consumer-rules.pro +2 -0
- package/native/android/src/main/AndroidManifest.xml +6 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +202 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +256 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersNative.java +22 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersNativeActivity.java +178 -0
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersNativeBridge.java +45 -0
- package/native/ios/DocumentBridge.swift +330 -0
- package/native/ios/NetworkBridge.swift +51 -0
- package/native/ios/SocketBridge.swift +180 -0
- package/native/ios/WorkersCapacitorPlugin.swift +152 -0
- package/native/ios-host/WorkersView.swift +194 -0
- package/package.json +154 -4
|
@@ -0,0 +1,2292 @@
|
|
|
1
|
+
import { socketReader } from "./reader.js";
|
|
2
|
+
// Copyright (c) 2017-2022 Cloudflare, Inc.
|
|
3
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
4
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
5
|
+
//
|
|
6
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
7
|
+
//
|
|
8
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
9
|
+
// copy of this software and associated documentation files (the
|
|
10
|
+
// "Software"), to deal in the Software without restriction, including
|
|
11
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
12
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
13
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
14
|
+
// following conditions:
|
|
15
|
+
//
|
|
16
|
+
// The above copyright notice and this permission notice shall be included
|
|
17
|
+
// in all copies or substantial portions of the Software.
|
|
18
|
+
//
|
|
19
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
20
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
22
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
23
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
24
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
25
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
27
|
+
import inner from "./sockets.js";
|
|
28
|
+
import { tcpPorts, HOST_ADDRESS, GATEWAY_ADDRESS, } from "./ports.js";
|
|
29
|
+
import { EventEmitter } from "node:events";
|
|
30
|
+
import { AbortError, ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, ERR_MISSING_ARGS, ERR_OUT_OF_RANGE, ERR_OPTION_NOT_IMPLEMENTED, ERR_SOCKET_CLOSED, ERR_SOCKET_CLOSED_BEFORE_CONNECTION, ERR_SOCKET_CONNECTING, ERR_SOCKET_HANDLE_ADOPTED, ERR_SERVER_ALREADY_LISTEN, ERR_SERVER_NOT_RUNNING, ERR_INVALID_IP_ADDRESS, ERR_INVALID_ADDRESS, EADDRINUSE, EADDRNOTAVAIL, EPIPE, ENOBUFS, } from "./errors.js";
|
|
31
|
+
import { validateAbortSignal, validateArray, validateFunction, validateInt32, validateNumber, validatePort, validateObject, validateOneOf, validateBoolean, validateString, validateUint32, } from "./validators.js";
|
|
32
|
+
const isUint8Array = (value) => Object.prototype.toString.call(value) === "[object Uint8Array]";
|
|
33
|
+
const isArrayBufferView = ArrayBuffer.isView;
|
|
34
|
+
// Use the same scheduler as the bundled Duplex: error must precede close.
|
|
35
|
+
import { nextTick } from "node:process";
|
|
36
|
+
import { Duplex } from "node:stream";
|
|
37
|
+
import { Buffer } from "node:buffer";
|
|
38
|
+
import { inspect } from "node:util";
|
|
39
|
+
import { asyncDispose, dispose } from "../../shared/globals/symbols.js";
|
|
40
|
+
const kInspect = inspect.custom;
|
|
41
|
+
const kLastWriteQueueSize = Symbol("kLastWriteQueueSize");
|
|
42
|
+
const kTimeout = Symbol("kTimeout");
|
|
43
|
+
const kBuffer = Symbol("kBuffer");
|
|
44
|
+
const kBufferCb = Symbol("kBufferCb");
|
|
45
|
+
const kBufferGen = Symbol("kBufferGen");
|
|
46
|
+
const kBufferContinuations = Symbol("kBufferContinuations");
|
|
47
|
+
const kBytesRead = Symbol("kBytesRead");
|
|
48
|
+
const kBytesWritten = Symbol("kBytesWritten");
|
|
49
|
+
const kUpdateTimer = Symbol("kUpdateTimer");
|
|
50
|
+
export const normalizedArgsSymbol = Symbol("normalizedArgs");
|
|
51
|
+
export const kReinitializeHandle = Symbol("kReinitializeHandle");
|
|
52
|
+
// Once the socket has been opened, the socket info provided by the
|
|
53
|
+
// socket.opened promise will be stored here.
|
|
54
|
+
const kSocketInfo = Symbol("kSocketInfo");
|
|
55
|
+
// The local address of a Socket, either adopted from a BoundSocket or autobound
|
|
56
|
+
// on connect. kBoundReserved marks a port table reservation to be released on
|
|
57
|
+
// destroy; false when the address is only a label.
|
|
58
|
+
const kBoundSource = Symbol("kBoundSource");
|
|
59
|
+
const kBoundReserved = Symbol("kBoundReserved");
|
|
60
|
+
const kBoundSocketConsume = Symbol("kBoundSocketConsume");
|
|
61
|
+
const DEFAULT_IPV4_ADDR = "0.0.0.0";
|
|
62
|
+
const DEFAULT_IPV6_ADDR = "::";
|
|
63
|
+
function isWildcard(address) {
|
|
64
|
+
return address === DEFAULT_IPV4_ADDR || address === DEFAULT_IPV6_ADDR;
|
|
65
|
+
}
|
|
66
|
+
// A synthetic IPv4 address in the form the given family reports it.
|
|
67
|
+
function forFamily(address, family) {
|
|
68
|
+
return family === "IPv6" ? `::ffff:${address}` : address;
|
|
69
|
+
}
|
|
70
|
+
// The concrete address a socket bound to the wildcard reports once connected
|
|
71
|
+
// or accepted: the isolate's host address, as Linux reports the interface the
|
|
72
|
+
// connection uses.
|
|
73
|
+
function concreteLocal(bound) {
|
|
74
|
+
if (!isWildcard(bound.address))
|
|
75
|
+
return bound;
|
|
76
|
+
return { ...bound, address: forFamily(HOST_ADDRESS, bound.family) };
|
|
77
|
+
}
|
|
78
|
+
// Reserves port in the isolate's table (shared with http servers), allocating
|
|
79
|
+
// an ephemeral port for 0. Throws EADDRINUSE on conflict. Binding is
|
|
80
|
+
// role-neutral, so a declared listener port is never taken here;
|
|
81
|
+
// Server.listen() claims one.
|
|
82
|
+
export function bindPort(address, port, reusePort = false) {
|
|
83
|
+
if (port === 0) {
|
|
84
|
+
port = tcpPorts.ephemeral();
|
|
85
|
+
if (port === 0)
|
|
86
|
+
throw new EADDRINUSE(address, port);
|
|
87
|
+
}
|
|
88
|
+
if (!tcpPorts.tryBind(port, reusePort)) {
|
|
89
|
+
throw new EADDRINUSE(address, port);
|
|
90
|
+
}
|
|
91
|
+
return port;
|
|
92
|
+
}
|
|
93
|
+
function releaseBoundSource(socket) {
|
|
94
|
+
if (socket[kBoundReserved]) {
|
|
95
|
+
tcpPorts.unregister(socket);
|
|
96
|
+
tcpPorts.release(socket[kBoundSource].port);
|
|
97
|
+
socket[kBoundReserved] = false;
|
|
98
|
+
}
|
|
99
|
+
socket[kBoundSource] = null;
|
|
100
|
+
}
|
|
101
|
+
// IPv4 Segment
|
|
102
|
+
const v4Seg = "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])";
|
|
103
|
+
const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`;
|
|
104
|
+
const IPv4Reg = new RegExp(`^${v4Str}$`);
|
|
105
|
+
// IPv6 Segment
|
|
106
|
+
const v6Seg = "(?:[0-9a-fA-F]{1,4})";
|
|
107
|
+
const IPv6Reg = new RegExp("^(?:" +
|
|
108
|
+
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
|
|
109
|
+
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
|
|
110
|
+
`(?:${v6Seg}:){5}(?::${v4Str}|(?::${v6Seg}){1,2}|:)|` +
|
|
111
|
+
`(?:${v6Seg}:){4}(?:(?::${v6Seg}){0,1}:${v4Str}|(?::${v6Seg}){1,3}|:)|` +
|
|
112
|
+
`(?:${v6Seg}:){3}(?:(?::${v6Seg}){0,2}:${v4Str}|(?::${v6Seg}){1,4}|:)|` +
|
|
113
|
+
`(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` +
|
|
114
|
+
`(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` +
|
|
115
|
+
`(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
|
|
116
|
+
")(?:%[0-9a-zA-Z-.:]{1,})?$");
|
|
117
|
+
const TIMEOUT_MAX = 2 ** 31 - 1;
|
|
118
|
+
// Reserves a port in the virtual port table; the adopting Socket reports the
|
|
119
|
+
// bound address as its local address.
|
|
120
|
+
export class BoundSocket {
|
|
121
|
+
#address;
|
|
122
|
+
// Bound with port 0, so a server adopting it may re-home it onto a declared
|
|
123
|
+
// listener port.
|
|
124
|
+
#ephemeral;
|
|
125
|
+
static isBoundSocket(value) {
|
|
126
|
+
return typeof value === "object" && value !== null && #address in value;
|
|
127
|
+
}
|
|
128
|
+
constructor(options = {}) {
|
|
129
|
+
validateObject(options, "options");
|
|
130
|
+
if (options.path !== undefined) {
|
|
131
|
+
throw new ERR_INVALID_ARG_VALUE("options.path", options.path, "is not supported");
|
|
132
|
+
}
|
|
133
|
+
const port = validatePort(options.port ?? 0, "options.port");
|
|
134
|
+
this.#ephemeral = port === 0;
|
|
135
|
+
const ipv6Only = options.ipv6Only ?? false;
|
|
136
|
+
validateBoolean(ipv6Only, "options.ipv6Only");
|
|
137
|
+
const reusePort = options.reusePort ?? false;
|
|
138
|
+
validateBoolean(reusePort, "options.reusePort");
|
|
139
|
+
let { host } = options;
|
|
140
|
+
let addressType;
|
|
141
|
+
if (host === undefined || host === null) {
|
|
142
|
+
host = ipv6Only ? DEFAULT_IPV6_ADDR : DEFAULT_IPV4_ADDR;
|
|
143
|
+
addressType = ipv6Only ? 6 : 4;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
validateString(host, "options.host");
|
|
147
|
+
addressType = isIP(host);
|
|
148
|
+
if (addressType === 0) {
|
|
149
|
+
throw new ERR_INVALID_ARG_VALUE("options.host", host, "must be a numeric IP address; net.BoundSocket does not perform DNS resolution");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
this.#address = {
|
|
153
|
+
address: host,
|
|
154
|
+
family: addressType === 6 ? "IPv6" : "IPv4",
|
|
155
|
+
port: bindPort(host, port, reusePort),
|
|
156
|
+
};
|
|
157
|
+
tcpPorts.register(this, this.#address.port);
|
|
158
|
+
}
|
|
159
|
+
address() {
|
|
160
|
+
if (this.#address === null) {
|
|
161
|
+
throw new ERR_SOCKET_HANDLE_ADOPTED();
|
|
162
|
+
}
|
|
163
|
+
return this.#address;
|
|
164
|
+
}
|
|
165
|
+
// Workers have no file descriptors; -1 matches Node.js on platforms without
|
|
166
|
+
// socket fds.
|
|
167
|
+
fd() {
|
|
168
|
+
if (this.#address === null) {
|
|
169
|
+
throw new ERR_SOCKET_HANDLE_ADOPTED();
|
|
170
|
+
}
|
|
171
|
+
return -1;
|
|
172
|
+
}
|
|
173
|
+
close() {
|
|
174
|
+
if (this.#address === null) {
|
|
175
|
+
throw new ERR_SOCKET_HANDLE_ADOPTED();
|
|
176
|
+
}
|
|
177
|
+
tcpPorts.unregister(this);
|
|
178
|
+
tcpPorts.release(this.#address.port);
|
|
179
|
+
this.#address = null;
|
|
180
|
+
}
|
|
181
|
+
[dispose]() {
|
|
182
|
+
if (this.#address !== null) {
|
|
183
|
+
this.close();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// Transfers the reservation to the adopter.
|
|
187
|
+
[kBoundSocketConsume]() {
|
|
188
|
+
if (this.#address === null) {
|
|
189
|
+
throw new ERR_SOCKET_HANDLE_ADOPTED();
|
|
190
|
+
}
|
|
191
|
+
tcpPorts.unregister(this);
|
|
192
|
+
const address = this.#address;
|
|
193
|
+
this.#address = null;
|
|
194
|
+
return { address, ephemeral: this.#ephemeral };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function familyOf(address) {
|
|
198
|
+
switch (isIP(address)) {
|
|
199
|
+
case 4:
|
|
200
|
+
return "IPv4";
|
|
201
|
+
case 6:
|
|
202
|
+
return "IPv6";
|
|
203
|
+
default:
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Splits a "host:port" / "[v6]:port" authority; "*" is the wildcard.
|
|
208
|
+
function parseAuthority(authority) {
|
|
209
|
+
if (typeof authority !== "string")
|
|
210
|
+
return null;
|
|
211
|
+
const m = /^\[?([^\]]*?)\]?:(\d+)$/.exec(authority);
|
|
212
|
+
if (m === null)
|
|
213
|
+
return null;
|
|
214
|
+
const address = m[1] === "*" ? DEFAULT_IPV4_ADDR : m[1];
|
|
215
|
+
const info = { address, port: Number(m[2]) };
|
|
216
|
+
const family = familyOf(address);
|
|
217
|
+
if (family !== undefined)
|
|
218
|
+
info.family = family;
|
|
219
|
+
return info;
|
|
220
|
+
}
|
|
221
|
+
// A listening server reserves its port in the isolate's TCP port table and
|
|
222
|
+
// installs a connect handler there. Inbound platform sockets are routed to it
|
|
223
|
+
// by local port (see connectHandler in cloudflare:node) and wrapped as
|
|
224
|
+
// net.Sockets; the platform owns the local endpoint, so nothing is released
|
|
225
|
+
// per connection.
|
|
226
|
+
export class Server extends EventEmitter {
|
|
227
|
+
#address = null;
|
|
228
|
+
#connections = 0;
|
|
229
|
+
#closing = false;
|
|
230
|
+
#handler = {
|
|
231
|
+
connect: (socket) => this.#onConnection(socket),
|
|
232
|
+
};
|
|
233
|
+
allowHalfOpen;
|
|
234
|
+
pauseOnConnect;
|
|
235
|
+
noDelay;
|
|
236
|
+
keepAlive;
|
|
237
|
+
keepAliveInitialDelay;
|
|
238
|
+
highWaterMark;
|
|
239
|
+
maxConnections = undefined;
|
|
240
|
+
constructor(options, connectionListener) {
|
|
241
|
+
super();
|
|
242
|
+
if (typeof options === "function") {
|
|
243
|
+
connectionListener = options;
|
|
244
|
+
options = {};
|
|
245
|
+
}
|
|
246
|
+
else if (options == null) {
|
|
247
|
+
options = {};
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
validateObject(options, "options");
|
|
251
|
+
}
|
|
252
|
+
if (connectionListener !== undefined) {
|
|
253
|
+
validateFunction(connectionListener, "connectionListener");
|
|
254
|
+
this.on("connection", connectionListener);
|
|
255
|
+
}
|
|
256
|
+
let keepAliveInitialDelay = options.keepAliveInitialDelay;
|
|
257
|
+
if (keepAliveInitialDelay !== undefined) {
|
|
258
|
+
validateNumber(keepAliveInitialDelay, "options.keepAliveInitialDelay");
|
|
259
|
+
if (keepAliveInitialDelay < 0)
|
|
260
|
+
keepAliveInitialDelay = 0;
|
|
261
|
+
}
|
|
262
|
+
this.allowHalfOpen = options.allowHalfOpen ?? false;
|
|
263
|
+
this.pauseOnConnect = Boolean(options.pauseOnConnect);
|
|
264
|
+
this.noDelay = Boolean(options.noDelay);
|
|
265
|
+
this.keepAlive = Boolean(options.keepAlive);
|
|
266
|
+
this.keepAliveInitialDelay = ~~((keepAliveInitialDelay ?? 0) / 1000);
|
|
267
|
+
this.highWaterMark = options.highWaterMark;
|
|
268
|
+
}
|
|
269
|
+
listen(...args) {
|
|
270
|
+
const [options, cb] = _normalizeArgs(args);
|
|
271
|
+
if (this.#address !== null) {
|
|
272
|
+
throw new ERR_SERVER_ALREADY_LISTEN();
|
|
273
|
+
}
|
|
274
|
+
if (cb !== null) {
|
|
275
|
+
this.once("listening", cb);
|
|
276
|
+
}
|
|
277
|
+
let bound = null;
|
|
278
|
+
if (BoundSocket.isBoundSocket(options)) {
|
|
279
|
+
bound = options;
|
|
280
|
+
}
|
|
281
|
+
else if (BoundSocket.isBoundSocket(options.handle)) {
|
|
282
|
+
bound = options.handle;
|
|
283
|
+
}
|
|
284
|
+
const fail = (err) => {
|
|
285
|
+
queueMicrotask(() => {
|
|
286
|
+
this.emit("error", err);
|
|
287
|
+
});
|
|
288
|
+
return this;
|
|
289
|
+
};
|
|
290
|
+
let address;
|
|
291
|
+
if (bound !== null) {
|
|
292
|
+
// Adoption transfers the reservation to the server.
|
|
293
|
+
let ephemeral;
|
|
294
|
+
({ address, ephemeral } = bound[kBoundSocketConsume]());
|
|
295
|
+
if (tcpPorts.hasDeclared() && !tcpPorts.isDeclared(address.port)) {
|
|
296
|
+
tcpPorts.release(address.port);
|
|
297
|
+
if (!ephemeral) {
|
|
298
|
+
return fail(new EADDRNOTAVAIL(address.address, address.port));
|
|
299
|
+
}
|
|
300
|
+
// A socket bound with port 0 becomes a listener on a declared port,
|
|
301
|
+
// so new BoundSocket() + listen(bound) lands where connections arrive.
|
|
302
|
+
const declared = tcpPorts.unclaimedDeclared();
|
|
303
|
+
if (declared === 0 || !tcpPorts.tryBind(declared)) {
|
|
304
|
+
return fail(new EADDRINUSE(address.address, 0));
|
|
305
|
+
}
|
|
306
|
+
address = { ...address, port: declared };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
const opts = options;
|
|
311
|
+
if (opts.path != null) {
|
|
312
|
+
throw new ERR_INVALID_ARG_VALUE("options.path", opts.path, "is not supported");
|
|
313
|
+
}
|
|
314
|
+
if (opts.handle != null || opts.fd != null) {
|
|
315
|
+
throw new ERR_INVALID_ARG_VALUE("options", options, "only a net.BoundSocket handle is supported");
|
|
316
|
+
}
|
|
317
|
+
let port = opts.port == null ? 0 : validatePort(opts.port, "options.port");
|
|
318
|
+
const reusePort = opts.reusePort ?? false;
|
|
319
|
+
validateBoolean(reusePort, "options.reusePort");
|
|
320
|
+
let host = opts.host;
|
|
321
|
+
if (host === undefined) {
|
|
322
|
+
host = opts.ipv6Only ? DEFAULT_IPV6_ADDR : DEFAULT_IPV4_ADDR;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
validateString(host, "options.host");
|
|
326
|
+
if (host === "localhost")
|
|
327
|
+
host = "127.0.0.1";
|
|
328
|
+
}
|
|
329
|
+
// Where the platform declares the ports it delivers connections on, a
|
|
330
|
+
// server can only listen on one of them, and port 0 takes the first
|
|
331
|
+
// unclaimed one.
|
|
332
|
+
if (tcpPorts.hasDeclared()) {
|
|
333
|
+
if (port === 0) {
|
|
334
|
+
port = tcpPorts.unclaimedDeclared();
|
|
335
|
+
if (port === 0)
|
|
336
|
+
return fail(new EADDRINUSE(host, 0));
|
|
337
|
+
}
|
|
338
|
+
else if (!tcpPorts.isDeclared(port)) {
|
|
339
|
+
return fail(new EADDRNOTAVAIL(host, port));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
let boundPort;
|
|
343
|
+
try {
|
|
344
|
+
boundPort = bindPort(host, port, reusePort);
|
|
345
|
+
}
|
|
346
|
+
catch (err) {
|
|
347
|
+
return fail(err);
|
|
348
|
+
}
|
|
349
|
+
address = { address: host, port: boundPort };
|
|
350
|
+
const family = familyOf(host);
|
|
351
|
+
if (family !== undefined)
|
|
352
|
+
address.family = family;
|
|
353
|
+
}
|
|
354
|
+
this.#address = address;
|
|
355
|
+
// A pending close from a previous listen is cancelled.
|
|
356
|
+
this.#closing = false;
|
|
357
|
+
tcpPorts.setHandler(address.port, this.#handler);
|
|
358
|
+
queueMicrotask(() => {
|
|
359
|
+
this.emit("listening");
|
|
360
|
+
});
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
address() {
|
|
364
|
+
return this.#address === null ? null : { ...this.#address };
|
|
365
|
+
}
|
|
366
|
+
get listening() {
|
|
367
|
+
return this.#address !== null;
|
|
368
|
+
}
|
|
369
|
+
close(cb) {
|
|
370
|
+
if (typeof cb === "function") {
|
|
371
|
+
if (this.#address === null) {
|
|
372
|
+
this.once("close", () => {
|
|
373
|
+
cb(new ERR_SERVER_NOT_RUNNING());
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
this.once("close", cb);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (this.#address !== null) {
|
|
381
|
+
tcpPorts.clearHandler(this.#address.port, this.#handler);
|
|
382
|
+
tcpPorts.release(this.#address.port);
|
|
383
|
+
this.#address = null;
|
|
384
|
+
}
|
|
385
|
+
this.#closing = true;
|
|
386
|
+
this.#maybeEmitClose();
|
|
387
|
+
return this;
|
|
388
|
+
}
|
|
389
|
+
// 'close' fires once the server is not listening and no connection remains.
|
|
390
|
+
#maybeEmitClose() {
|
|
391
|
+
if (this.#closing && this.#address === null && this.#connections === 0) {
|
|
392
|
+
this.#closing = false;
|
|
393
|
+
queueMicrotask(() => {
|
|
394
|
+
this.emit("close");
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
getConnections(cb) {
|
|
399
|
+
validateFunction(cb, "cb");
|
|
400
|
+
const count = this.#connections;
|
|
401
|
+
queueMicrotask(() => {
|
|
402
|
+
cb(null, count);
|
|
403
|
+
});
|
|
404
|
+
return this;
|
|
405
|
+
}
|
|
406
|
+
ref() {
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
unref() {
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
async [asyncDispose]() {
|
|
413
|
+
if (this.#address === null)
|
|
414
|
+
return;
|
|
415
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
416
|
+
const { promise, resolve } = deferred();
|
|
417
|
+
this.close(() => {
|
|
418
|
+
resolve();
|
|
419
|
+
});
|
|
420
|
+
await promise;
|
|
421
|
+
}
|
|
422
|
+
async #onConnection(handle) {
|
|
423
|
+
const info = await handle.opened;
|
|
424
|
+
if (this.#address === null) {
|
|
425
|
+
await handle.close();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
// The accepted socket's local endpoint is the server's bound address, as
|
|
429
|
+
// with an accepted fd; the CONNECT authority only routed it here. A peer
|
|
430
|
+
// the platform does not report (a service binding's) appears behind the
|
|
431
|
+
// gateway address with its own port, since a connected socket always has
|
|
432
|
+
// a distinct peer.
|
|
433
|
+
const localAddress = concreteLocal(this.#address);
|
|
434
|
+
const remoteAddress = parseAuthority(info.remoteAddress) ?? {
|
|
435
|
+
address: forFamily(GATEWAY_ADDRESS, localAddress.family),
|
|
436
|
+
family: localAddress.family ?? "IPv4",
|
|
437
|
+
port: tcpPorts.ephemeral(),
|
|
438
|
+
};
|
|
439
|
+
if (this.maxConnections !== undefined &&
|
|
440
|
+
this.#connections >= this.maxConnections) {
|
|
441
|
+
this.emit("drop", {
|
|
442
|
+
localAddress: localAddress.address,
|
|
443
|
+
localPort: localAddress.port,
|
|
444
|
+
localFamily: localAddress.family,
|
|
445
|
+
remoteAddress: remoteAddress.address,
|
|
446
|
+
remotePort: remoteAddress.port,
|
|
447
|
+
remoteFamily: remoteAddress.family,
|
|
448
|
+
});
|
|
449
|
+
await handle.close();
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const socket = new Socket({
|
|
453
|
+
allowHalfOpen: this.allowHalfOpen,
|
|
454
|
+
highWaterMark: this.highWaterMark,
|
|
455
|
+
});
|
|
456
|
+
socket._handle = {
|
|
457
|
+
socket: handle,
|
|
458
|
+
writer: handle.writable.getWriter(),
|
|
459
|
+
reader: socketReader(handle.readable),
|
|
460
|
+
bytesRead: 0,
|
|
461
|
+
bytesWritten: 0,
|
|
462
|
+
reading: false,
|
|
463
|
+
options: {
|
|
464
|
+
host: remoteAddress.address,
|
|
465
|
+
port: remoteAddress.port,
|
|
466
|
+
addressType: isIP(remoteAddress.address),
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
socket[kSocketInfo] = { remoteAddress: { ...remoteAddress } };
|
|
470
|
+
socket[kBoundSource] = localAddress;
|
|
471
|
+
socket.server = this;
|
|
472
|
+
socket._server = this;
|
|
473
|
+
socket.connecting = false;
|
|
474
|
+
socket._undestroy();
|
|
475
|
+
this.#connections++;
|
|
476
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
477
|
+
const { promise: closed, resolve } = deferred();
|
|
478
|
+
socket.once("close", () => {
|
|
479
|
+
resolve();
|
|
480
|
+
});
|
|
481
|
+
handle.closed.then(onConnectionClosed.bind(socket), (err) => {
|
|
482
|
+
socket.destroy(err);
|
|
483
|
+
});
|
|
484
|
+
if (this.pauseOnConnect) {
|
|
485
|
+
socket.pause();
|
|
486
|
+
}
|
|
487
|
+
this.emit("connection", socket);
|
|
488
|
+
if (!socket.isPaused() && !socket._handle.reading) {
|
|
489
|
+
tryReadStart(socket);
|
|
490
|
+
}
|
|
491
|
+
await closed;
|
|
492
|
+
this.#connections--;
|
|
493
|
+
this.#maybeEmitClose();
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
// @ts-expect-error TS2323 Redeclare error.
|
|
497
|
+
export function Socket(options) {
|
|
498
|
+
if (!(this instanceof Socket)) {
|
|
499
|
+
return new Socket(options);
|
|
500
|
+
}
|
|
501
|
+
if (options?.objectMode) {
|
|
502
|
+
throw new ERR_INVALID_ARG_VALUE("options.objectMode", options.objectMode, "is not supported");
|
|
503
|
+
}
|
|
504
|
+
else if (options?.readableObjectMode || options?.writableObjectMode) {
|
|
505
|
+
throw new ERR_INVALID_ARG_VALUE(`options.${options.readableObjectMode ? "readableObjectMode" : "writableObjectMode"}`, options.readableObjectMode || options.writableObjectMode, "is not supported");
|
|
506
|
+
}
|
|
507
|
+
if (options?.keepAliveInitialDelay !== undefined) {
|
|
508
|
+
validateNumber(options.keepAliveInitialDelay, "options.keepAliveInitialDelay");
|
|
509
|
+
if (options.keepAliveInitialDelay < 0) {
|
|
510
|
+
options.keepAliveInitialDelay = 0;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (typeof options === "number") {
|
|
514
|
+
options = { fd: options };
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
options = { ...options };
|
|
518
|
+
}
|
|
519
|
+
if (options.fd !== undefined) {
|
|
520
|
+
// We are not supporting the options.fd option for now. This is the option
|
|
521
|
+
// that allows users to pass in a file descriptor to an existing socket.
|
|
522
|
+
// Workers doesn't have file descriptors and does not use them in any way.
|
|
523
|
+
throw new ERR_OPTION_NOT_IMPLEMENTED("options.fd");
|
|
524
|
+
}
|
|
525
|
+
// We do not support the noDelay and keepAlive options at this
|
|
526
|
+
// time and will just ignore them if they are passed.
|
|
527
|
+
//
|
|
528
|
+
// We shouldn't throw an error for the following validations,
|
|
529
|
+
// because it breaks packages such as redis.
|
|
530
|
+
//
|
|
531
|
+
// if (options.noDelay) {
|
|
532
|
+
// throw new ERR_OPTION_NOT_IMPLEMENTED('truthy options.noDelay');
|
|
533
|
+
// }
|
|
534
|
+
//
|
|
535
|
+
// if (options.keepAlive) {
|
|
536
|
+
// throw new ERR_OPTION_NOT_IMPLEMENTED('truthy options.keepAlive');
|
|
537
|
+
// }
|
|
538
|
+
options.allowHalfOpen = Boolean(options.allowHalfOpen);
|
|
539
|
+
// TODO(now): Match behavior with Node.js
|
|
540
|
+
// In Node.js, emitClose and autoDestroy are false by default so that
|
|
541
|
+
// the socket must handle those itself, including emitting the close
|
|
542
|
+
// event with the hadError argument. We should match that behavior.
|
|
543
|
+
options.emitClose = false;
|
|
544
|
+
options.autoDestroy = true;
|
|
545
|
+
options.decodeStrings = false;
|
|
546
|
+
// In Node.js, these are meaningful when the options.fd is used.
|
|
547
|
+
// We do not support options.fd so we just ignore whatever value
|
|
548
|
+
// is given and always pass true.
|
|
549
|
+
options.readable = true;
|
|
550
|
+
options.writable = true;
|
|
551
|
+
this._handle = null;
|
|
552
|
+
this.connecting = false;
|
|
553
|
+
this._hadError = false;
|
|
554
|
+
this._parent = null;
|
|
555
|
+
this._parentWrap = null;
|
|
556
|
+
this._host = null;
|
|
557
|
+
this[kLastWriteQueueSize] = 0;
|
|
558
|
+
this[kTimeout] = null;
|
|
559
|
+
this[kBuffer] = null;
|
|
560
|
+
this[kBufferCb] = null;
|
|
561
|
+
this[kBufferGen] = null;
|
|
562
|
+
this[kBufferContinuations] = null;
|
|
563
|
+
this[kSocketInfo] = null;
|
|
564
|
+
this[kBytesRead] = 0;
|
|
565
|
+
this[kBytesWritten] = 0;
|
|
566
|
+
this[kBoundSource] = null;
|
|
567
|
+
this[kBoundReserved] = false;
|
|
568
|
+
this.server = null;
|
|
569
|
+
this._server = null;
|
|
570
|
+
this._closeAfterHandlingError = false;
|
|
571
|
+
// @ts-expect-error TS2540 Required due to types
|
|
572
|
+
this.autoSelectFamilyAttemptedAddresses = [];
|
|
573
|
+
this._undestroy();
|
|
574
|
+
this._pendingData = null;
|
|
575
|
+
this._pendingEncoding = "";
|
|
576
|
+
// Call Duplex constructor before setting up the abort signal
|
|
577
|
+
// This ensures the stream methods are properly set up before
|
|
578
|
+
// any abort handling that might call destroy()
|
|
579
|
+
Duplex.call(this, options);
|
|
580
|
+
if (options.handle) {
|
|
581
|
+
validateObject(options.handle, "options.handle");
|
|
582
|
+
if (BoundSocket.isBoundSocket(options.handle)) {
|
|
583
|
+
const { address } = options.handle[kBoundSocketConsume]();
|
|
584
|
+
this[kBoundSource] = address;
|
|
585
|
+
this[kBoundReserved] = true;
|
|
586
|
+
tcpPorts.register(this, address.port);
|
|
587
|
+
}
|
|
588
|
+
else {
|
|
589
|
+
this._handle = options.handle;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
// We explicitly listen for all 'end' events, not only for once
|
|
593
|
+
// because Socket class supports reconnection through s.connect();
|
|
594
|
+
this.on("end", onReadableStreamEnd);
|
|
595
|
+
const onread = options.onread;
|
|
596
|
+
if (onread != null &&
|
|
597
|
+
typeof onread === "object" &&
|
|
598
|
+
// The onread.buffer can either be a Uint8Array or a function that returns
|
|
599
|
+
// a Uint8Array.
|
|
600
|
+
(isUint8Array(onread.buffer) || typeof onread.buffer === "function") &&
|
|
601
|
+
// The onread.callback is the function used to deliver the read buffer to
|
|
602
|
+
// the application.
|
|
603
|
+
typeof onread.callback === "function") {
|
|
604
|
+
if (typeof onread.buffer === "function") {
|
|
605
|
+
this[kBufferGen] = onread.buffer;
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
const { buffer } = onread;
|
|
609
|
+
this[kBufferGen] = () => buffer;
|
|
610
|
+
}
|
|
611
|
+
this[kBuffer] = true;
|
|
612
|
+
// The read loop's BYOB reads transfer the buffers they are handed, so a
|
|
613
|
+
// caller reusing one — a fixed buffer, or a generator handing out the
|
|
614
|
+
// same buffers again — is continued over the store it moved to.
|
|
615
|
+
this[kBufferContinuations] = new WeakMap();
|
|
616
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
617
|
+
this[kBufferCb] = onread.callback;
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
this[kBuffer] = true;
|
|
621
|
+
this[kBufferGen] = () => new Uint8Array(4096);
|
|
622
|
+
this[kBufferCb] = undefined;
|
|
623
|
+
}
|
|
624
|
+
// Now set up abort signal handling after the Duplex constructor
|
|
625
|
+
if (options.signal) {
|
|
626
|
+
addClientAbortSignalOption(this, options.signal);
|
|
627
|
+
}
|
|
628
|
+
this[kBytesRead] = 0;
|
|
629
|
+
this[kBytesWritten] = 0;
|
|
630
|
+
// TODO(soon): Enable this once blockList is implemented.
|
|
631
|
+
// if (options.blockList) {
|
|
632
|
+
// if (!BlockList.isBlockList(options.blockList)) {
|
|
633
|
+
// throw new ERR_INVALID_ARG_TYPE('options.blockList', 'net.BlockList', options.blockList);
|
|
634
|
+
// }
|
|
635
|
+
// this.blockList = options.blockList;
|
|
636
|
+
// }
|
|
637
|
+
return this;
|
|
638
|
+
}
|
|
639
|
+
Object.setPrototypeOf(Socket.prototype, Duplex.prototype);
|
|
640
|
+
Object.setPrototypeOf(Socket, Duplex);
|
|
641
|
+
// Restarts the idle timer (of this socket and of any wrapping TLS socket)
|
|
642
|
+
// after activity, keeping the 'timeout' listeners as they are. A socket
|
|
643
|
+
// whose timeout is disabled holds no timer (kTimeout is null, see
|
|
644
|
+
// clearSocketTimers), so there is nothing to restart.
|
|
645
|
+
Socket.prototype._unrefTimer = function _unrefTimer() {
|
|
646
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
647
|
+
for (let s = this; s != null; s = s._parentWrap) {
|
|
648
|
+
if (s[kTimeout] != null) {
|
|
649
|
+
clearTimeout(s[kTimeout]);
|
|
650
|
+
s[kTimeout] = setTimeout(() => {
|
|
651
|
+
s._onTimeout();
|
|
652
|
+
}, s.timeout);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
// Disables the idle timer of this socket and of any wrapping TLS socket.
|
|
657
|
+
// kTimeout is cleared as well as the timer, so that later activity
|
|
658
|
+
// (_unrefTimer) does not arm a new one.
|
|
659
|
+
function clearSocketTimers(socket) {
|
|
660
|
+
for (let s = socket; s != null; s = s._parentWrap) {
|
|
661
|
+
clearTimeout(s[kTimeout]);
|
|
662
|
+
s[kTimeout] = null;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
Socket.prototype.setTimeout = function (msecs, callback) {
|
|
666
|
+
if (this.destroyed)
|
|
667
|
+
return this;
|
|
668
|
+
this.timeout = msecs;
|
|
669
|
+
// Type checking identical to timers.enroll()
|
|
670
|
+
msecs = getTimerDuration(msecs, "msecs");
|
|
671
|
+
// Attempt to clear an existing timer in both cases -
|
|
672
|
+
// even if it will be rescheduled we don't want to leak an existing timer.
|
|
673
|
+
clearTimeout(this[kTimeout]);
|
|
674
|
+
this[kTimeout] = null;
|
|
675
|
+
if (msecs === 0) {
|
|
676
|
+
if (callback !== undefined) {
|
|
677
|
+
validateFunction(callback, "callback");
|
|
678
|
+
this.removeListener("timeout", callback);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
// @ts-expect-error TS2740 Required to not overcomplicate types
|
|
683
|
+
this[kTimeout] = setTimeout(() => {
|
|
684
|
+
this._onTimeout();
|
|
685
|
+
}, msecs);
|
|
686
|
+
if (callback !== undefined) {
|
|
687
|
+
validateFunction(callback, "callback");
|
|
688
|
+
this.once("timeout", callback);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
return this;
|
|
692
|
+
};
|
|
693
|
+
Socket.prototype._onTimeout = function () {
|
|
694
|
+
const handle = this._handle;
|
|
695
|
+
const lastWriteQueueSize = this[kLastWriteQueueSize];
|
|
696
|
+
if (lastWriteQueueSize > 0 && handle) {
|
|
697
|
+
// `lastWriteQueueSize !== writeQueueSize` means there is
|
|
698
|
+
// an active write in progress, so we suppress the timeout.
|
|
699
|
+
const { writeQueueSize } = handle;
|
|
700
|
+
if (lastWriteQueueSize !== writeQueueSize) {
|
|
701
|
+
this[kLastWriteQueueSize] = writeQueueSize;
|
|
702
|
+
this._unrefTimer();
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
this.emit("timeout");
|
|
707
|
+
};
|
|
708
|
+
Socket.prototype._getpeername = function () {
|
|
709
|
+
if (this._handle == null || this[kSocketInfo] == null) {
|
|
710
|
+
return {};
|
|
711
|
+
}
|
|
712
|
+
return { ...this[kSocketInfo].remoteAddress };
|
|
713
|
+
};
|
|
714
|
+
Socket.prototype._getsockname = function () {
|
|
715
|
+
if (this[kBoundSource] != null) {
|
|
716
|
+
return this[kBoundSource];
|
|
717
|
+
}
|
|
718
|
+
// A wrapping socket (TLSSocket) shares the local endpoint of the socket it
|
|
719
|
+
// wraps.
|
|
720
|
+
if (this._parentWrap != null) {
|
|
721
|
+
return this._parentWrap._getsockname();
|
|
722
|
+
}
|
|
723
|
+
return {};
|
|
724
|
+
};
|
|
725
|
+
Socket.prototype.address = function () {
|
|
726
|
+
return this._getsockname();
|
|
727
|
+
};
|
|
728
|
+
// ======================================================================================
|
|
729
|
+
// Writable side ...
|
|
730
|
+
Socket.prototype._writeGeneric = function (writev, data, encoding, cb) {
|
|
731
|
+
// If we are still connecting, buffer this for later.
|
|
732
|
+
// The writable logic will buffer up any more writes while
|
|
733
|
+
// waiting for this one to be done.
|
|
734
|
+
try {
|
|
735
|
+
if (this.connecting) {
|
|
736
|
+
this._pendingData = data;
|
|
737
|
+
this._pendingEncoding = encoding;
|
|
738
|
+
function onClose() {
|
|
739
|
+
cb(new ERR_SOCKET_CLOSED_BEFORE_CONNECTION());
|
|
740
|
+
}
|
|
741
|
+
this.once("connect", () => {
|
|
742
|
+
this.off("close", onClose);
|
|
743
|
+
this._writeGeneric(writev, data, encoding, cb);
|
|
744
|
+
});
|
|
745
|
+
this.once("close", onClose);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
this._pendingData = null;
|
|
749
|
+
this._pendingEncoding = "";
|
|
750
|
+
if (this._handle?.writer === undefined) {
|
|
751
|
+
cb(new ERR_SOCKET_CLOSED());
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
this._unrefTimer();
|
|
755
|
+
let lastWriteSize = 0;
|
|
756
|
+
if (writev) {
|
|
757
|
+
// data is an array of strings or ArrayBufferViews. We're going to concat
|
|
758
|
+
// them all together into a single buffer so we can write all at once. This
|
|
759
|
+
// trades higher memory use by copying the input buffers for fewer round trips
|
|
760
|
+
// through the write loop in the stream. Since the write loops involve bouncing
|
|
761
|
+
// back and forth across the kj event loop boundary and requires reacquiring the
|
|
762
|
+
// isolate lock after each write, this should be more efficient in the long run.
|
|
763
|
+
const buffers = [];
|
|
764
|
+
for (const d of data) {
|
|
765
|
+
if (typeof d.chunk === "string") {
|
|
766
|
+
const buf = Buffer.from(d.chunk, d.encoding);
|
|
767
|
+
buffers.push(buf);
|
|
768
|
+
lastWriteSize += buf.byteLength;
|
|
769
|
+
}
|
|
770
|
+
else if (isArrayBufferView(d.chunk)) {
|
|
771
|
+
buffers.push(new Uint8Array(d.chunk.buffer, d.chunk.byteOffset, d.chunk.byteLength));
|
|
772
|
+
lastWriteSize += d.chunk.byteLength;
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
throw new ERR_INVALID_ARG_TYPE("chunk", ["string", "ArrayBufferView"], d.chunk);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
this._unrefTimer();
|
|
779
|
+
this._handle.writer.write(Buffer.concat(buffers)).then(() => {
|
|
780
|
+
if (this._handle != null) {
|
|
781
|
+
this._handle.bytesWritten += this[kLastWriteQueueSize] ?? 0;
|
|
782
|
+
}
|
|
783
|
+
else {
|
|
784
|
+
this[kBytesWritten] += this[kLastWriteQueueSize] ?? 0;
|
|
785
|
+
}
|
|
786
|
+
this[kLastWriteQueueSize] = 0;
|
|
787
|
+
this._unrefTimer();
|
|
788
|
+
cb();
|
|
789
|
+
}, (err) => {
|
|
790
|
+
this[kLastWriteQueueSize] = 0;
|
|
791
|
+
this._unrefTimer();
|
|
792
|
+
cb(err);
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
else {
|
|
796
|
+
let bufferData;
|
|
797
|
+
if (typeof data === "string") {
|
|
798
|
+
bufferData = Buffer.from(data, encoding);
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
801
|
+
bufferData = data;
|
|
802
|
+
}
|
|
803
|
+
this._handle.writer.write(bufferData).then(() => {
|
|
804
|
+
if (this._handle != null) {
|
|
805
|
+
this._handle.bytesWritten += this[kLastWriteQueueSize] ?? 0;
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
this[kBytesWritten] += this[kLastWriteQueueSize] ?? 0;
|
|
809
|
+
}
|
|
810
|
+
this[kLastWriteQueueSize] = 0;
|
|
811
|
+
this._unrefTimer();
|
|
812
|
+
cb();
|
|
813
|
+
}, (err) => {
|
|
814
|
+
this[kLastWriteQueueSize] = 0;
|
|
815
|
+
this._unrefTimer();
|
|
816
|
+
cb(err);
|
|
817
|
+
});
|
|
818
|
+
lastWriteSize = bufferData.byteLength;
|
|
819
|
+
}
|
|
820
|
+
this[kLastWriteQueueSize] = lastWriteSize;
|
|
821
|
+
}
|
|
822
|
+
catch (err) {
|
|
823
|
+
this.destroy(err);
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
Socket.prototype._writev = function (chunks, cb) {
|
|
827
|
+
this._writeGeneric(true, chunks, "", cb);
|
|
828
|
+
};
|
|
829
|
+
Socket.prototype._write = function (data, encoding, cb) {
|
|
830
|
+
this._writeGeneric(false, data, encoding, cb);
|
|
831
|
+
};
|
|
832
|
+
Socket.prototype._final = function (cb) {
|
|
833
|
+
if (this.connecting) {
|
|
834
|
+
this.once("connect", () => {
|
|
835
|
+
this._final(cb);
|
|
836
|
+
});
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
// If there is no writer, then there's really nothing left to do here.
|
|
840
|
+
if (this._handle == null) {
|
|
841
|
+
cb();
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
this._handle.writer.close().then(() => {
|
|
845
|
+
cb();
|
|
846
|
+
}, (err) => {
|
|
847
|
+
cb(err);
|
|
848
|
+
});
|
|
849
|
+
};
|
|
850
|
+
Socket.prototype.end = function (
|
|
851
|
+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
852
|
+
data, encoding, cb) {
|
|
853
|
+
// @ts-expect-error this fails after upgrading to @types/node@22.14
|
|
854
|
+
Duplex.prototype.end.call(this, data, encoding, cb);
|
|
855
|
+
return this;
|
|
856
|
+
};
|
|
857
|
+
// ======================================================================================
|
|
858
|
+
// Readable side
|
|
859
|
+
Socket.prototype.pause = function () {
|
|
860
|
+
if (this[kBuffer] && !this.connecting && this._handle?.reading) {
|
|
861
|
+
// If the read loop is already running, setting reading to false
|
|
862
|
+
// will interrupt it after the current read completes (if any)
|
|
863
|
+
if (!this.destroyed) {
|
|
864
|
+
this._handle.reading = false;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
return Duplex.prototype.pause.call(this);
|
|
868
|
+
};
|
|
869
|
+
Socket.prototype.resume = function () {
|
|
870
|
+
if (this[kBuffer] &&
|
|
871
|
+
!this.connecting &&
|
|
872
|
+
this._handle &&
|
|
873
|
+
!this._handle.reading) {
|
|
874
|
+
tryReadStart(this);
|
|
875
|
+
}
|
|
876
|
+
return Duplex.prototype.resume.call(this);
|
|
877
|
+
};
|
|
878
|
+
Socket.prototype.read = function (n) {
|
|
879
|
+
if (this[kBuffer] &&
|
|
880
|
+
!this.connecting &&
|
|
881
|
+
this._handle &&
|
|
882
|
+
!this._handle.reading) {
|
|
883
|
+
tryReadStart(this);
|
|
884
|
+
}
|
|
885
|
+
return Duplex.prototype.read.call(this, n);
|
|
886
|
+
};
|
|
887
|
+
Socket.prototype._read = function (n) {
|
|
888
|
+
if (this.connecting || !this._handle) {
|
|
889
|
+
this.once("connect", () => {
|
|
890
|
+
this._read(n);
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
else if (!this._handle.reading) {
|
|
894
|
+
tryReadStart(this);
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
// ======================================================================================
|
|
898
|
+
// Destroy and reset
|
|
899
|
+
Socket.prototype._reset = function () {
|
|
900
|
+
this.resetAndClosing = true;
|
|
901
|
+
return this.destroy();
|
|
902
|
+
};
|
|
903
|
+
Socket.prototype.resetAndDestroy = function () {
|
|
904
|
+
// In Node.js, the resetAndDestroy method is used to "[close] the TCP connection by
|
|
905
|
+
// sending an RST packet and destroy the stream. If this TCP socket is in connecting
|
|
906
|
+
// status, it will send an RST packet and destroy this TCP socket once it is connected.
|
|
907
|
+
// Otherwise, it will call socket.destroy with an ERR_SOCKET_CLOSED Error. If this is
|
|
908
|
+
// not a TCP socket (for example, a pipe), calling this method will immediately throw
|
|
909
|
+
// an ERR_INVALID_HANDLE_TYPE Error." In our implementation we really don't have a way
|
|
910
|
+
// of ensuring whether or not an RST packet is actually sent so this is largely an
|
|
911
|
+
// alias for the existing destroy. If the socket is still connecting, it will be
|
|
912
|
+
// destroyed immediately after the connection is established.
|
|
913
|
+
if (this._handle) {
|
|
914
|
+
if (this.connecting) {
|
|
915
|
+
this.once("connect", () => {
|
|
916
|
+
this._reset();
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
else {
|
|
920
|
+
this._reset();
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
else {
|
|
924
|
+
this.destroy(new ERR_SOCKET_CLOSED());
|
|
925
|
+
}
|
|
926
|
+
return this;
|
|
927
|
+
};
|
|
928
|
+
Socket.prototype.destroySoon = function () {
|
|
929
|
+
if (this.writable) {
|
|
930
|
+
this.end();
|
|
931
|
+
}
|
|
932
|
+
if (this.writableFinished) {
|
|
933
|
+
this.destroy();
|
|
934
|
+
}
|
|
935
|
+
else {
|
|
936
|
+
this.once("finish", () => {
|
|
937
|
+
// Do not call this.destroy.bind(this) since user can override it.
|
|
938
|
+
this.destroy();
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
Socket.prototype._destroy = function (exception, cb) {
|
|
943
|
+
this.connecting = false;
|
|
944
|
+
clearSocketTimers(this);
|
|
945
|
+
releaseBoundSource(this);
|
|
946
|
+
if (this._handle != null) {
|
|
947
|
+
this._handle.socket.close().then(() => {
|
|
948
|
+
cleanupAfterDestroy(this, cb, exception);
|
|
949
|
+
}, (err) => {
|
|
950
|
+
cleanupAfterDestroy(this, cb, (err || exception));
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
else {
|
|
954
|
+
cleanupAfterDestroy(this, cb, exception);
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
// ======================================================================================
|
|
958
|
+
// Connection
|
|
959
|
+
// @ts-expect-error TS2322 Type inconsistencies between types/node
|
|
960
|
+
Socket.prototype.connect = function (...args) {
|
|
961
|
+
let normalized;
|
|
962
|
+
// @ts-expect-error TS7015 Required not to overcomplicate types
|
|
963
|
+
if (Array.isArray(args[0]) && args[0][normalizedArgsSymbol]) {
|
|
964
|
+
normalized = args[0];
|
|
965
|
+
}
|
|
966
|
+
else {
|
|
967
|
+
normalized = _normalizeArgs(args);
|
|
968
|
+
}
|
|
969
|
+
const options = normalized[0];
|
|
970
|
+
const cb = normalized[1];
|
|
971
|
+
if (this.connecting) {
|
|
972
|
+
throw new ERR_SOCKET_CONNECTING();
|
|
973
|
+
}
|
|
974
|
+
if (this._aborted) {
|
|
975
|
+
if (cb) {
|
|
976
|
+
cb(new AbortError());
|
|
977
|
+
}
|
|
978
|
+
else {
|
|
979
|
+
throw new AbortError();
|
|
980
|
+
}
|
|
981
|
+
return undefined;
|
|
982
|
+
}
|
|
983
|
+
if (cb !== null) {
|
|
984
|
+
this.once("connect", cb);
|
|
985
|
+
}
|
|
986
|
+
if (this._parentWrap?.connecting) {
|
|
987
|
+
return this;
|
|
988
|
+
}
|
|
989
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
990
|
+
if (options.port === undefined && options.path == null) {
|
|
991
|
+
throw new ERR_MISSING_ARGS(["options", "port", "path"]);
|
|
992
|
+
}
|
|
993
|
+
if (this.write !== Socket.prototype.write) {
|
|
994
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
995
|
+
this.write = Socket.prototype.write;
|
|
996
|
+
}
|
|
997
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
998
|
+
if (options.path != null) {
|
|
999
|
+
throw new ERR_INVALID_ARG_VALUE("path", options.path, "is not supported");
|
|
1000
|
+
}
|
|
1001
|
+
this[kBytesRead] = 0;
|
|
1002
|
+
this[kBytesWritten] = 0;
|
|
1003
|
+
// This is required for "reconnection".
|
|
1004
|
+
// Previous connected handle needs to emit "close" event.
|
|
1005
|
+
// This ensures that the previous handle is closed before initializing a new one.
|
|
1006
|
+
if (this._handle) {
|
|
1007
|
+
// A reconnect is a new underlying socket, so the previous local endpoint
|
|
1008
|
+
// is dropped and the new connection autobinds. The old handle is detached
|
|
1009
|
+
// first so that its EOF and close do not end this socket; writes buffer
|
|
1010
|
+
// until the new connection opens.
|
|
1011
|
+
releaseBoundSource(this);
|
|
1012
|
+
const old = this._handle;
|
|
1013
|
+
this._handle = null;
|
|
1014
|
+
old.reading = false;
|
|
1015
|
+
this.connecting = true;
|
|
1016
|
+
old.socket.close().then(() => {
|
|
1017
|
+
initializeConnection(this, options);
|
|
1018
|
+
}, () => {
|
|
1019
|
+
initializeConnection(this, options);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
else {
|
|
1023
|
+
initializeConnection(this, options);
|
|
1024
|
+
}
|
|
1025
|
+
return this;
|
|
1026
|
+
};
|
|
1027
|
+
Socket.prototype[kReinitializeHandle] = function reinitializeHandle(handle) {
|
|
1028
|
+
this._handle?.socket.close().then(() => {
|
|
1029
|
+
cleanupAfterDestroy(this);
|
|
1030
|
+
}, (err) => {
|
|
1031
|
+
cleanupAfterDestroy(this, null, err);
|
|
1032
|
+
});
|
|
1033
|
+
this._handle = handle;
|
|
1034
|
+
this._undestroy();
|
|
1035
|
+
};
|
|
1036
|
+
// ======================================================================================
|
|
1037
|
+
// Socket methods that are not no-ops or nominal impls
|
|
1038
|
+
Socket.prototype.setNoDelay = function (_enable) {
|
|
1039
|
+
// Ignore this for now.
|
|
1040
|
+
// Cloudflare connect() does not support this.
|
|
1041
|
+
return this;
|
|
1042
|
+
};
|
|
1043
|
+
Socket.prototype.setKeepAlive = function (_enable, _initialDelay) {
|
|
1044
|
+
// Ignore this for now.
|
|
1045
|
+
// This is used by services like mySQL.
|
|
1046
|
+
// TODO(soon): Investigate supporting this.
|
|
1047
|
+
return this;
|
|
1048
|
+
};
|
|
1049
|
+
// @ts-expect-error TS2322 Intentionally no-op
|
|
1050
|
+
Socket.prototype.ref = function () {
|
|
1051
|
+
// Intentional no-op
|
|
1052
|
+
};
|
|
1053
|
+
// @ts-expect-error TS2322 Intentionally no-op
|
|
1054
|
+
Socket.prototype.unref = function () {
|
|
1055
|
+
// Intentional no-op
|
|
1056
|
+
};
|
|
1057
|
+
Object.defineProperties(Socket.prototype, {
|
|
1058
|
+
_connecting: {
|
|
1059
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1060
|
+
__proto__: null,
|
|
1061
|
+
get() {
|
|
1062
|
+
return this.connecting;
|
|
1063
|
+
},
|
|
1064
|
+
},
|
|
1065
|
+
pending: {
|
|
1066
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1067
|
+
__proto__: null,
|
|
1068
|
+
get() {
|
|
1069
|
+
return !this._handle || this.connecting;
|
|
1070
|
+
},
|
|
1071
|
+
configurable: true,
|
|
1072
|
+
},
|
|
1073
|
+
readyState: {
|
|
1074
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1075
|
+
__proto__: null,
|
|
1076
|
+
get() {
|
|
1077
|
+
if (this.connecting) {
|
|
1078
|
+
return "opening";
|
|
1079
|
+
}
|
|
1080
|
+
else if (this.readable && this.writable) {
|
|
1081
|
+
return "open";
|
|
1082
|
+
}
|
|
1083
|
+
else if (this.readable && !this.writable) {
|
|
1084
|
+
return "readOnly";
|
|
1085
|
+
}
|
|
1086
|
+
else if (!this.readable && this.writable) {
|
|
1087
|
+
return "writeOnly";
|
|
1088
|
+
}
|
|
1089
|
+
return "closed";
|
|
1090
|
+
},
|
|
1091
|
+
},
|
|
1092
|
+
bufferSize: {
|
|
1093
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1094
|
+
__proto__: null,
|
|
1095
|
+
get() {
|
|
1096
|
+
if (this._handle) {
|
|
1097
|
+
return this.writableLength;
|
|
1098
|
+
}
|
|
1099
|
+
return undefined;
|
|
1100
|
+
},
|
|
1101
|
+
},
|
|
1102
|
+
[kUpdateTimer]: {
|
|
1103
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1104
|
+
__proto__: null,
|
|
1105
|
+
get() {
|
|
1106
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
1107
|
+
return this._unrefTimer;
|
|
1108
|
+
},
|
|
1109
|
+
},
|
|
1110
|
+
bytesRead: {
|
|
1111
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1112
|
+
__proto__: null,
|
|
1113
|
+
configurable: false,
|
|
1114
|
+
enumerable: true,
|
|
1115
|
+
get() {
|
|
1116
|
+
return this._handle ? this._handle.bytesRead : this[kBytesRead];
|
|
1117
|
+
},
|
|
1118
|
+
},
|
|
1119
|
+
_bytesDispatched: {
|
|
1120
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1121
|
+
__proto__: null,
|
|
1122
|
+
configurable: false,
|
|
1123
|
+
enumerable: true,
|
|
1124
|
+
get() {
|
|
1125
|
+
return this._handle ? this._handle.bytesWritten : this[kBytesWritten];
|
|
1126
|
+
},
|
|
1127
|
+
},
|
|
1128
|
+
bytesWritten: {
|
|
1129
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1130
|
+
__proto__: null,
|
|
1131
|
+
configurable: false,
|
|
1132
|
+
enumerable: true,
|
|
1133
|
+
get() {
|
|
1134
|
+
let bytes = this._bytesDispatched;
|
|
1135
|
+
const data = this._pendingData;
|
|
1136
|
+
const encoding = this._pendingEncoding;
|
|
1137
|
+
const writableBuffer = this.writableBuffer;
|
|
1138
|
+
if (!writableBuffer)
|
|
1139
|
+
return undefined;
|
|
1140
|
+
if (Array.isArray(data)) {
|
|
1141
|
+
// Was a writev, iterate over chunks to get total length
|
|
1142
|
+
for (let i = 0; i < data.length; i++) {
|
|
1143
|
+
const chunk = data[i];
|
|
1144
|
+
if (chunk == null) {
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
// @ts-expect-error TS2339 allBuffers doesn't exist on type.
|
|
1148
|
+
if (chunk instanceof Buffer || data.allBuffers)
|
|
1149
|
+
bytes += chunk.length;
|
|
1150
|
+
else {
|
|
1151
|
+
bytes += Buffer.byteLength(
|
|
1152
|
+
// @ts-expect-error TS2339 TODO(soon): Use correct type here.
|
|
1153
|
+
chunk.chunk,
|
|
1154
|
+
// @ts-expect-error TS2339 TODO(soon): Use correct type here.
|
|
1155
|
+
chunk.encoding);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
else if (data) {
|
|
1160
|
+
// Writes are either a string or a Buffer.
|
|
1161
|
+
if (typeof data !== "string") {
|
|
1162
|
+
bytes += data.length;
|
|
1163
|
+
}
|
|
1164
|
+
else {
|
|
1165
|
+
bytes += Buffer.byteLength(data, encoding);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
else {
|
|
1169
|
+
const flushed = this._handle != null
|
|
1170
|
+
? this._handle.bytesWritten
|
|
1171
|
+
: this[kBytesWritten];
|
|
1172
|
+
return this.writableLength + flushed;
|
|
1173
|
+
}
|
|
1174
|
+
return bytes;
|
|
1175
|
+
},
|
|
1176
|
+
},
|
|
1177
|
+
remoteAddress: {
|
|
1178
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1179
|
+
__proto__: null,
|
|
1180
|
+
configurable: false,
|
|
1181
|
+
enumerable: true,
|
|
1182
|
+
get() {
|
|
1183
|
+
return this._getpeername().address;
|
|
1184
|
+
},
|
|
1185
|
+
},
|
|
1186
|
+
remoteFamily: {
|
|
1187
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1188
|
+
__proto__: null,
|
|
1189
|
+
configurable: false,
|
|
1190
|
+
enumerable: true,
|
|
1191
|
+
get() {
|
|
1192
|
+
return this._getpeername().family;
|
|
1193
|
+
},
|
|
1194
|
+
},
|
|
1195
|
+
remotePort: {
|
|
1196
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1197
|
+
__proto__: null,
|
|
1198
|
+
configurable: false,
|
|
1199
|
+
enumerable: true,
|
|
1200
|
+
get() {
|
|
1201
|
+
return this._getpeername().port;
|
|
1202
|
+
},
|
|
1203
|
+
},
|
|
1204
|
+
localAddress: {
|
|
1205
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1206
|
+
__proto__: null,
|
|
1207
|
+
configurable: false,
|
|
1208
|
+
enumerable: true,
|
|
1209
|
+
get() {
|
|
1210
|
+
return this._getsockname().address;
|
|
1211
|
+
},
|
|
1212
|
+
},
|
|
1213
|
+
localPort: {
|
|
1214
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1215
|
+
__proto__: null,
|
|
1216
|
+
configurable: false,
|
|
1217
|
+
enumerable: true,
|
|
1218
|
+
get() {
|
|
1219
|
+
return this._getsockname().port;
|
|
1220
|
+
},
|
|
1221
|
+
},
|
|
1222
|
+
localFamily: {
|
|
1223
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1224
|
+
__proto__: null,
|
|
1225
|
+
configurable: false,
|
|
1226
|
+
enumerable: true,
|
|
1227
|
+
get() {
|
|
1228
|
+
return this._getsockname().family;
|
|
1229
|
+
},
|
|
1230
|
+
},
|
|
1231
|
+
});
|
|
1232
|
+
// ======================================================================================
|
|
1233
|
+
// Helper/utility methods
|
|
1234
|
+
function cleanupAfterDestroy(socket, cb, error) {
|
|
1235
|
+
const isException = error != null;
|
|
1236
|
+
if (socket._handle != null) {
|
|
1237
|
+
socket[kBytesRead] = socket._handle.bytesRead;
|
|
1238
|
+
socket[kBytesWritten] = socket._handle.bytesWritten;
|
|
1239
|
+
socket.resetAndClosing = false;
|
|
1240
|
+
}
|
|
1241
|
+
socket[kLastWriteQueueSize] = 0;
|
|
1242
|
+
socket[kSocketInfo] = null;
|
|
1243
|
+
if (cb != null) {
|
|
1244
|
+
// The Duplex destroy machinery emits 'error' from its callback on the
|
|
1245
|
+
// next tick. emitClose is off for sockets, so 'close' (with hadError)
|
|
1246
|
+
// is emitted here, queued behind that 'error'.
|
|
1247
|
+
cb(error);
|
|
1248
|
+
nextTick(() => socket.emit("close", isException));
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
// No destroy callback (a handle being replaced): emit both directly.
|
|
1252
|
+
if (error != null) {
|
|
1253
|
+
socket.emit("error", error);
|
|
1254
|
+
}
|
|
1255
|
+
socket.emit("close", isException);
|
|
1256
|
+
}
|
|
1257
|
+
function initializeConnection(socket, options) {
|
|
1258
|
+
const { host = "localhost", family, hints, autoSelectFamily, autoSelectFamilyAttemptTimeout, lookup, localAddress, localPort, } = options;
|
|
1259
|
+
let { port } = options;
|
|
1260
|
+
if (socket[kBoundSource] != null &&
|
|
1261
|
+
(localAddress !== undefined || localPort !== undefined)) {
|
|
1262
|
+
throw new ERR_INVALID_ARG_VALUE("options", options, "localAddress and localPort cannot be used with an adopted bound socket");
|
|
1263
|
+
}
|
|
1264
|
+
if (localAddress && !isIP(localAddress)) {
|
|
1265
|
+
throw new ERR_INVALID_IP_ADDRESS(localAddress);
|
|
1266
|
+
}
|
|
1267
|
+
if (localPort) {
|
|
1268
|
+
validateNumber(localPort, "options.localPort");
|
|
1269
|
+
}
|
|
1270
|
+
if (typeof port !== "number" && typeof port !== "string") {
|
|
1271
|
+
throw new ERR_INVALID_ARG_TYPE("options.port", ["number", "string"], port);
|
|
1272
|
+
}
|
|
1273
|
+
validatePort(port);
|
|
1274
|
+
port |= 0;
|
|
1275
|
+
if (autoSelectFamily != null) {
|
|
1276
|
+
// We don't support this option.
|
|
1277
|
+
// We shouldn't throw this because services like mongodb depends on it.
|
|
1278
|
+
// TODO(soon): Investigate supporting this.
|
|
1279
|
+
validateBoolean(autoSelectFamily, "options.autoSelectFamily");
|
|
1280
|
+
}
|
|
1281
|
+
if (autoSelectFamilyAttemptTimeout != null) {
|
|
1282
|
+
// We don't support this option.
|
|
1283
|
+
// We shouldn't throw this because services like mongodb depends on it.
|
|
1284
|
+
// TODO(soon): Investigate supporting this.
|
|
1285
|
+
validateInt32(autoSelectFamilyAttemptTimeout, "options.autoSelectFamilyAttemptTimeout", 1);
|
|
1286
|
+
}
|
|
1287
|
+
socket._unrefTimer();
|
|
1288
|
+
socket.connecting = true;
|
|
1289
|
+
const continueConnection = (host, port, family) => {
|
|
1290
|
+
socket[kSocketInfo] = {
|
|
1291
|
+
remoteAddress: {
|
|
1292
|
+
address: host,
|
|
1293
|
+
port,
|
|
1294
|
+
family: family === 4 ? "IPv4" : family === 6 ? "IPv6" : undefined,
|
|
1295
|
+
},
|
|
1296
|
+
};
|
|
1297
|
+
if (family === 6) {
|
|
1298
|
+
// The host is an IPv6 address. We need to wrap it in square brackets.
|
|
1299
|
+
host = `[${host}]`;
|
|
1300
|
+
}
|
|
1301
|
+
// @ts-expect-error TS2540 Unnecessary error due to using @types/node
|
|
1302
|
+
socket.autoSelectFamilyAttemptedAddresses = [`${host}:${port}`];
|
|
1303
|
+
socket.emit("connectionAttempt", host, port, addressType);
|
|
1304
|
+
socket._host = `${host}`;
|
|
1305
|
+
try {
|
|
1306
|
+
// Autobind the local endpoint, as connect(2) does, unless one was
|
|
1307
|
+
// adopted from a BoundSocket.
|
|
1308
|
+
if (socket[kBoundSource] == null) {
|
|
1309
|
+
const address = localAddress ??
|
|
1310
|
+
(family === 6 ? DEFAULT_IPV6_ADDR : DEFAULT_IPV4_ADDR);
|
|
1311
|
+
// Only a concrete localPort claims a table entry; localAddress alone
|
|
1312
|
+
// (and localPort 0) autobinds, with the address kept as the label.
|
|
1313
|
+
const reserved = localPort != null && localPort !== 0;
|
|
1314
|
+
socket[kBoundSource] = {
|
|
1315
|
+
address,
|
|
1316
|
+
family: isIP(address) === 6 ? "IPv6" : "IPv4",
|
|
1317
|
+
port: reserved ? bindPort(address, localPort) : tcpPorts.ephemeral(),
|
|
1318
|
+
};
|
|
1319
|
+
if (reserved) {
|
|
1320
|
+
socket[kBoundReserved] = true;
|
|
1321
|
+
tcpPorts.register(socket, socket[kBoundSource].port);
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
// connect(2) resolves a wildcard bind to the source address actually
|
|
1325
|
+
// used. The platform does not report an outbound socket's source
|
|
1326
|
+
// (SocketInfo.localAddress is empty for outbound sockets); if it did, it
|
|
1327
|
+
// would replace the synthetic host address here, from handle.opened.
|
|
1328
|
+
socket[kBoundSource] = concreteLocal(socket[kBoundSource]);
|
|
1329
|
+
const handle = inner.connect(`${host}:${port}`, {
|
|
1330
|
+
// The Duplex owns the half-open policy: with allowHalfOpen false,
|
|
1331
|
+
// Readable's endReadableNT schedules endWritableNT, which calls end()
|
|
1332
|
+
// a tick after 'end' and so flushes whatever was queued (the socket's
|
|
1333
|
+
// own 'end' listener, onReadableStreamEnd, only installs writeAfterFIN
|
|
1334
|
+
// to refuse later writes). The connect() socket must not race it by
|
|
1335
|
+
// closing the writable half on EOF as well.
|
|
1336
|
+
allowHalfOpen: true,
|
|
1337
|
+
// A Node.js socket is always capable of being upgraded to the TLS socket.
|
|
1338
|
+
secureTransport: socket.encrypted ? "on" : "starttls",
|
|
1339
|
+
// We are not going to pass the high water-mark here. The outer Node.js
|
|
1340
|
+
// stream will implement the appropriate backpressure for us.
|
|
1341
|
+
});
|
|
1342
|
+
// Our version of the socket._handle is necessarily different from Node.js'.
|
|
1343
|
+
// It serves the same purpose but any code that may exist that is depending
|
|
1344
|
+
// on `_handle` being a particular type (which it shouldn't be) will fail.
|
|
1345
|
+
socket._handle = {
|
|
1346
|
+
socket: handle,
|
|
1347
|
+
writer: handle.writable.getWriter(),
|
|
1348
|
+
reader: socketReader(handle.readable),
|
|
1349
|
+
bytesRead: 0,
|
|
1350
|
+
bytesWritten: 0,
|
|
1351
|
+
reading: false,
|
|
1352
|
+
options: {
|
|
1353
|
+
host: socket._host,
|
|
1354
|
+
port,
|
|
1355
|
+
addressType,
|
|
1356
|
+
},
|
|
1357
|
+
};
|
|
1358
|
+
// We need to undestroy the stream to connect to it.
|
|
1359
|
+
socket._undestroy();
|
|
1360
|
+
handle.opened.then(() => {
|
|
1361
|
+
if (socket._handle?.socket === handle)
|
|
1362
|
+
onConnectionOpened.call(socket);
|
|
1363
|
+
}, (err) => {
|
|
1364
|
+
if (socket._handle?.socket !== handle)
|
|
1365
|
+
return;
|
|
1366
|
+
socket.emit("connectionAttemptFailed", host, port, addressType, err);
|
|
1367
|
+
socket.destroy(err);
|
|
1368
|
+
});
|
|
1369
|
+
handle.closed.then(() => {
|
|
1370
|
+
// A reconnect may have replaced the handle by the time the previous
|
|
1371
|
+
// one reports closed.
|
|
1372
|
+
if (socket._handle?.socket === handle) {
|
|
1373
|
+
onConnectionClosed.call(socket);
|
|
1374
|
+
}
|
|
1375
|
+
}, (error) => {
|
|
1376
|
+
// Do not call socket.destroy.bind(socket) since user can override it.
|
|
1377
|
+
if (socket._handle?.socket === handle)
|
|
1378
|
+
socket.destroy(error);
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
catch (err) {
|
|
1382
|
+
socket.destroy(err);
|
|
1383
|
+
}
|
|
1384
|
+
};
|
|
1385
|
+
if (lookup != null) {
|
|
1386
|
+
validateFunction(lookup, "options.lookup");
|
|
1387
|
+
}
|
|
1388
|
+
const addressType = isIP(host);
|
|
1389
|
+
// The host is not an IP address. That's allowed in our implementation, but let's
|
|
1390
|
+
// see if the user provided a lookup function. If not, we'll skip.
|
|
1391
|
+
if (addressType === 0 && lookup != null) {
|
|
1392
|
+
// Looks like we have a lookup function! Let's call it. The expectation is that
|
|
1393
|
+
// the lookup function will produce a good IP address from the non-IP address
|
|
1394
|
+
// that is given. How that is done is left entirely up to the application code.
|
|
1395
|
+
// The connection attempt will continue once the lookup function invokes the
|
|
1396
|
+
// given callback.
|
|
1397
|
+
const lookupOptions = { family: family || addressType, hints };
|
|
1398
|
+
lookup(host, lookupOptions, (err, address, family) => {
|
|
1399
|
+
socket.emit("lookup", err, address, family, host);
|
|
1400
|
+
if (err) {
|
|
1401
|
+
socket.destroy(err);
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
if (isIP(address) === 0) {
|
|
1405
|
+
throw new ERR_INVALID_IP_ADDRESS(address);
|
|
1406
|
+
}
|
|
1407
|
+
if (family !== 4 &&
|
|
1408
|
+
family !== 6 &&
|
|
1409
|
+
family !== "IPv4" &&
|
|
1410
|
+
family !== "IPv6") {
|
|
1411
|
+
throw new ERR_INVALID_ARG_VALUE("family", family, "must be 4 or 6");
|
|
1412
|
+
}
|
|
1413
|
+
continueConnection(address, port, family);
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
else {
|
|
1417
|
+
continueConnection(host, port, addressType);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
export function onConnectionOpened() {
|
|
1421
|
+
// Callback may come after call to destroy
|
|
1422
|
+
if (this.destroyed) {
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
this.connecting = false;
|
|
1426
|
+
this._unrefTimer();
|
|
1427
|
+
this.emit("connect");
|
|
1428
|
+
this.emit("ready");
|
|
1429
|
+
if (!this.isPaused()) {
|
|
1430
|
+
tryReadStart(this);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
export function onConnectionClosed() {
|
|
1434
|
+
const handle = this._handle;
|
|
1435
|
+
if (handle?.socket.upgraded) {
|
|
1436
|
+
// The socket is being upgraded from insecure to TLS.
|
|
1437
|
+
// No need to handle this particular close event.
|
|
1438
|
+
return;
|
|
1439
|
+
}
|
|
1440
|
+
clearSocketTimers(this);
|
|
1441
|
+
if (this.destroyed || handle == null || handle.readLoopActive) {
|
|
1442
|
+
// A pending read reports the connection's outcome itself: EOF as its
|
|
1443
|
+
// done result, a failure (a peer's reset) as a rejection that destroys
|
|
1444
|
+
// the socket with the error. Ending the readable here as well would
|
|
1445
|
+
// announce EOF for a connection that failed, and with allowHalfOpen
|
|
1446
|
+
// false go on to end the writable side before the failure is reported.
|
|
1447
|
+
return;
|
|
1448
|
+
}
|
|
1449
|
+
// Nobody is reading (the socket is paused or stopped on backpressure), so
|
|
1450
|
+
// no read will observe the connection's end: end the readable here.
|
|
1451
|
+
// push(null) is a no-op if it already has; read(0) lets 'end' fire on a
|
|
1452
|
+
// socket nobody is reading, as in Node.
|
|
1453
|
+
this.push(null);
|
|
1454
|
+
Duplex.prototype.read.call(this, 0);
|
|
1455
|
+
}
|
|
1456
|
+
async function startRead(socket) {
|
|
1457
|
+
const handle = socket._handle;
|
|
1458
|
+
if (!handle || handle.readLoopActive)
|
|
1459
|
+
return;
|
|
1460
|
+
handle.readLoopActive = true;
|
|
1461
|
+
const reader = handle.reader;
|
|
1462
|
+
try {
|
|
1463
|
+
while (handle.reading && socket._handle === handle) {
|
|
1464
|
+
const generatedBuffer = socket[kBufferGen]?.();
|
|
1465
|
+
if (!isUint8Array(generatedBuffer)) {
|
|
1466
|
+
throw new ERR_INVALID_ARG_TYPE("onread.buffer", ["Buffer", "Uint8Array"], generatedBuffer);
|
|
1467
|
+
}
|
|
1468
|
+
// A view the loop has read into before was detached by that read's
|
|
1469
|
+
// transfer of its buffer. Handed out again — a fixed onread buffer, or a
|
|
1470
|
+
// generator reusing its buffers, as Node's in-place fills allow — it
|
|
1471
|
+
// means the same range of the store the buffer moved to, which the
|
|
1472
|
+
// loop kept as the view's continuation.
|
|
1473
|
+
const view = socket[kBufferContinuations]?.get(generatedBuffer) ?? generatedBuffer;
|
|
1474
|
+
if (view.byteLength === 0) {
|
|
1475
|
+
throw new ENOBUFS();
|
|
1476
|
+
}
|
|
1477
|
+
// The view's range within its buffer, taken before the read: a BYOB
|
|
1478
|
+
// read transfers the buffer and detaches the view, which then reports
|
|
1479
|
+
// neither.
|
|
1480
|
+
const { byteOffset: viewOffset, byteLength: viewLength } = view;
|
|
1481
|
+
const { value, done } = await reader.read(view);
|
|
1482
|
+
// Make sure the socket was not destroyed or reconnected while we were
|
|
1483
|
+
// waiting. If it was, we're going to throw away the chunk of data we
|
|
1484
|
+
// just read.
|
|
1485
|
+
if (socket.destroyed || socket._handle !== handle) {
|
|
1486
|
+
// Doh! Well, this is awkward. Let's just stop reading and return.
|
|
1487
|
+
// There's really nothing else we should try to do here.
|
|
1488
|
+
break;
|
|
1489
|
+
}
|
|
1490
|
+
// Reset the timeout timer since we received data.
|
|
1491
|
+
socket._unrefTimer();
|
|
1492
|
+
if (done) {
|
|
1493
|
+
// All done! If allowHalfOpen is true, then this will just end the
|
|
1494
|
+
// readable side of the socket. If allowHalfOpen is false, then this
|
|
1495
|
+
// should allow the current write queue to drain but not allow any
|
|
1496
|
+
// further writes to be queued.
|
|
1497
|
+
socket.push(null);
|
|
1498
|
+
// As in Node's onStreamRead: with nothing buffered, EOF must surface
|
|
1499
|
+
// as 'end' right away, even when nobody is consuming the socket. The
|
|
1500
|
+
// Readable's own read(0) does that; the socket's read() override
|
|
1501
|
+
// would try to start reading again.
|
|
1502
|
+
Duplex.prototype.read.call(socket, 0);
|
|
1503
|
+
break;
|
|
1504
|
+
}
|
|
1505
|
+
// The BYOB read transferred the view's buffer. Continue the caller's
|
|
1506
|
+
// view over the transferred backing store as a view of the same range
|
|
1507
|
+
// — the caller's offset and capacity, not the whole allocation — so a
|
|
1508
|
+
// read into it again fills exactly the region the caller handed out. A
|
|
1509
|
+
// store the read left too small for that range (detached or shrunk
|
|
1510
|
+
// while the read was pending, which only a non-transferring read can
|
|
1511
|
+
// see) is continued as an empty view, so reading into it is ENOBUFS.
|
|
1512
|
+
socket[kBufferContinuations]?.set(generatedBuffer, viewOffset + viewLength <= value.buffer.byteLength
|
|
1513
|
+
? new Uint8Array(value.buffer, viewOffset, viewLength)
|
|
1514
|
+
: new Uint8Array(0));
|
|
1515
|
+
// If the byteLength is zero, skip the push.
|
|
1516
|
+
if (value.byteLength === 0) {
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1519
|
+
handle.bytesRead += value.byteLength;
|
|
1520
|
+
// The socket API is expected to produce Buffer instances, not Uint8Arrays
|
|
1521
|
+
const buffer = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
1522
|
+
if (typeof socket[kBufferCb] === "function") {
|
|
1523
|
+
if (socket[kBufferCb](buffer.byteLength, buffer) === false) {
|
|
1524
|
+
// If the callback returns explicitly false (not falsy) then
|
|
1525
|
+
// we're being asked to stop reading for now.
|
|
1526
|
+
break;
|
|
1527
|
+
}
|
|
1528
|
+
continue;
|
|
1529
|
+
}
|
|
1530
|
+
// Because we're pushing the buffer onto the stream we can't use the shared
|
|
1531
|
+
// buffer here or the next read will overwrite it! We need to copy. For the
|
|
1532
|
+
// more efficient version, use onread.
|
|
1533
|
+
if (!socket.push(Buffer.from(buffer))) {
|
|
1534
|
+
// If push returns false, we've hit the high water mark and should stop
|
|
1535
|
+
// reading until the stream requests to start reading again.
|
|
1536
|
+
break;
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
catch (err) {
|
|
1541
|
+
// A pending read rejects when the socket is destroyed (its stream is
|
|
1542
|
+
// closing) or when a TLS upgrade releases this loop's reader (the
|
|
1543
|
+
// handle is handed over; the parent socket keeps pointing at it):
|
|
1544
|
+
// neither is news. Anything else — a failing generator or buffer
|
|
1545
|
+
// (above), a read the runtime refuses — destroys the socket with the
|
|
1546
|
+
// error rather than leaving it open and silent.
|
|
1547
|
+
if (socket._handle === handle && !handle.handedOver && !socket.destroyed) {
|
|
1548
|
+
socket.destroy(err);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
finally {
|
|
1552
|
+
handle.readLoopActive = false;
|
|
1553
|
+
handle.reading = false;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
export function tryReadStart(socket) {
|
|
1557
|
+
if (socket._handle != null) {
|
|
1558
|
+
socket._handle.reading = true;
|
|
1559
|
+
}
|
|
1560
|
+
startRead(socket).catch((err) => socket.destroy(err));
|
|
1561
|
+
}
|
|
1562
|
+
function writeAfterFIN(chunk, encoding, cb) {
|
|
1563
|
+
if (!this.writableEnded) {
|
|
1564
|
+
// @ts-expect-error TS2554 Required due to @types/node
|
|
1565
|
+
return Duplex.prototype.write.call(this, chunk, encoding, cb);
|
|
1566
|
+
}
|
|
1567
|
+
if (typeof encoding === "function") {
|
|
1568
|
+
cb = encoding;
|
|
1569
|
+
encoding = null;
|
|
1570
|
+
}
|
|
1571
|
+
const er = new EPIPE();
|
|
1572
|
+
if (cb != null && typeof cb === "function") {
|
|
1573
|
+
queueMicrotask(() => {
|
|
1574
|
+
cb(er);
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
this.destroy(er);
|
|
1578
|
+
return false;
|
|
1579
|
+
}
|
|
1580
|
+
function onReadableStreamEnd() {
|
|
1581
|
+
if (!this.allowHalfOpen) {
|
|
1582
|
+
this.write = writeAfterFIN;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
export function getTimerDuration(msecs, name) {
|
|
1586
|
+
validateNumber(msecs, name);
|
|
1587
|
+
if (msecs < 0 || !Number.isFinite(msecs)) {
|
|
1588
|
+
throw new ERR_OUT_OF_RANGE(name, "a non-negative finite number", msecs);
|
|
1589
|
+
}
|
|
1590
|
+
// Ensure that msecs fits into signed int32
|
|
1591
|
+
if (msecs > TIMEOUT_MAX) {
|
|
1592
|
+
return TIMEOUT_MAX;
|
|
1593
|
+
}
|
|
1594
|
+
return msecs;
|
|
1595
|
+
}
|
|
1596
|
+
export function toNumber(x) {
|
|
1597
|
+
return (x = Number(x)) >= 0 ? x : false;
|
|
1598
|
+
}
|
|
1599
|
+
export function isPipeName(s) {
|
|
1600
|
+
return typeof s === "string" && toNumber(s) === false;
|
|
1601
|
+
}
|
|
1602
|
+
export function _normalizeArgs(args) {
|
|
1603
|
+
let arr;
|
|
1604
|
+
if (args.length === 0) {
|
|
1605
|
+
arr = [{}, null];
|
|
1606
|
+
// @ts-expect-error TS2554 Required due to @types/node
|
|
1607
|
+
arr[normalizedArgsSymbol] = true;
|
|
1608
|
+
return arr;
|
|
1609
|
+
}
|
|
1610
|
+
const arg0 = args[0];
|
|
1611
|
+
let options = {};
|
|
1612
|
+
if (typeof arg0 === "object" && arg0 !== null) {
|
|
1613
|
+
// (options[...][, cb])
|
|
1614
|
+
options = arg0;
|
|
1615
|
+
}
|
|
1616
|
+
else if (isPipeName(arg0)) {
|
|
1617
|
+
// (path[...][, cb])
|
|
1618
|
+
options.path = arg0;
|
|
1619
|
+
}
|
|
1620
|
+
else {
|
|
1621
|
+
// ([port][, host][...][, cb])
|
|
1622
|
+
options.port = arg0;
|
|
1623
|
+
if (args.length > 1 && typeof args[1] === "string") {
|
|
1624
|
+
options.host = args[1];
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
const cb = args[args.length - 1];
|
|
1628
|
+
if (typeof cb !== "function")
|
|
1629
|
+
arr = [options, null];
|
|
1630
|
+
else
|
|
1631
|
+
arr = [options, cb];
|
|
1632
|
+
// @ts-expect-error TS2554 Required due to @types/node
|
|
1633
|
+
arr[normalizedArgsSymbol] = true;
|
|
1634
|
+
return arr;
|
|
1635
|
+
}
|
|
1636
|
+
function addClientAbortSignalOption(self, signal) {
|
|
1637
|
+
validateAbortSignal(signal, "options.signal");
|
|
1638
|
+
let disposable;
|
|
1639
|
+
function onAbort() {
|
|
1640
|
+
disposable?.[dispose]();
|
|
1641
|
+
self._aborted = true;
|
|
1642
|
+
}
|
|
1643
|
+
if (signal.aborted) {
|
|
1644
|
+
queueMicrotask(onAbort);
|
|
1645
|
+
}
|
|
1646
|
+
else {
|
|
1647
|
+
queueMicrotask(() => {
|
|
1648
|
+
disposable = addAbortListener(signal, onAbort);
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
function addAbortListener(signal, listener) {
|
|
1653
|
+
if (signal === undefined) {
|
|
1654
|
+
throw new ERR_INVALID_ARG_TYPE("signal", "AbortSignal", signal);
|
|
1655
|
+
}
|
|
1656
|
+
validateAbortSignal(signal, "signal");
|
|
1657
|
+
validateFunction(listener, "listener");
|
|
1658
|
+
let removeEventListener;
|
|
1659
|
+
if (signal.aborted) {
|
|
1660
|
+
queueMicrotask(() => {
|
|
1661
|
+
listener();
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
else {
|
|
1665
|
+
signal.addEventListener("abort", listener, { once: true });
|
|
1666
|
+
removeEventListener = () => {
|
|
1667
|
+
signal.removeEventListener("abort", listener);
|
|
1668
|
+
};
|
|
1669
|
+
}
|
|
1670
|
+
return {
|
|
1671
|
+
// @ts-expect-error TS2353 Required for __proto__
|
|
1672
|
+
__proto__: null,
|
|
1673
|
+
[dispose]() {
|
|
1674
|
+
removeEventListener?.();
|
|
1675
|
+
},
|
|
1676
|
+
};
|
|
1677
|
+
}
|
|
1678
|
+
// ======================================================================================
|
|
1679
|
+
// The rest of the exports
|
|
1680
|
+
export function connect(...args) {
|
|
1681
|
+
const normalized = _normalizeArgs(args);
|
|
1682
|
+
const options = normalized[0];
|
|
1683
|
+
const socket = new Socket(options);
|
|
1684
|
+
if (options.timeout) {
|
|
1685
|
+
socket.setTimeout(options.timeout);
|
|
1686
|
+
}
|
|
1687
|
+
if (socket.destroyed) {
|
|
1688
|
+
return socket;
|
|
1689
|
+
}
|
|
1690
|
+
return socket.connect(normalized);
|
|
1691
|
+
}
|
|
1692
|
+
export const createConnection = connect;
|
|
1693
|
+
export function createServer(options, connectionListener) {
|
|
1694
|
+
return new Server(options, connectionListener);
|
|
1695
|
+
}
|
|
1696
|
+
export function getDefaultAutoSelectFamily() {
|
|
1697
|
+
// This is the only value we support.
|
|
1698
|
+
return false;
|
|
1699
|
+
}
|
|
1700
|
+
export function setDefaultAutoSelectFamily(val) {
|
|
1701
|
+
if (!val)
|
|
1702
|
+
return;
|
|
1703
|
+
throw new ERR_INVALID_ARG_VALUE("val", val);
|
|
1704
|
+
}
|
|
1705
|
+
// We don't actually make use of this. It's here only for compatibility.
|
|
1706
|
+
// The value is not used anywhere.
|
|
1707
|
+
let autoSelectFamilyAttemptTimeout = 10;
|
|
1708
|
+
export function getDefaultAutoSelectFamilyAttemptTimeout() {
|
|
1709
|
+
return autoSelectFamilyAttemptTimeout;
|
|
1710
|
+
}
|
|
1711
|
+
export function setDefaultAutoSelectFamilyAttemptTimeout(val) {
|
|
1712
|
+
validateInt32(val, "val", 1);
|
|
1713
|
+
if (val < 10)
|
|
1714
|
+
val = 10;
|
|
1715
|
+
autoSelectFamilyAttemptTimeout = val;
|
|
1716
|
+
}
|
|
1717
|
+
export function isIP(input) {
|
|
1718
|
+
if (isIPv4(input))
|
|
1719
|
+
return 4;
|
|
1720
|
+
if (isIPv6(input))
|
|
1721
|
+
return 6;
|
|
1722
|
+
return 0;
|
|
1723
|
+
}
|
|
1724
|
+
export function isIPv4(input) {
|
|
1725
|
+
input = typeof input !== "string" ? `${input}` : input;
|
|
1726
|
+
return IPv4Reg.test(input);
|
|
1727
|
+
}
|
|
1728
|
+
export function isIPv6(input) {
|
|
1729
|
+
input = typeof input !== "string" ? `${input}` : input;
|
|
1730
|
+
return IPv6Reg.test(input);
|
|
1731
|
+
}
|
|
1732
|
+
// ======================================================================================
|
|
1733
|
+
export class SocketAddress {
|
|
1734
|
+
#address;
|
|
1735
|
+
#port;
|
|
1736
|
+
#family;
|
|
1737
|
+
#flowlabel;
|
|
1738
|
+
static isSocketAddress(value) {
|
|
1739
|
+
return value instanceof SocketAddress;
|
|
1740
|
+
}
|
|
1741
|
+
constructor(options = {}) {
|
|
1742
|
+
validateObject(options, "options");
|
|
1743
|
+
this.#family = options.family || "ipv4";
|
|
1744
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
1745
|
+
if (typeof this.#family?.toLowerCase === "function")
|
|
1746
|
+
this.#family = this.#family.toLowerCase();
|
|
1747
|
+
validateOneOf(this.#family, "options.family", ["ipv4", "ipv6"]);
|
|
1748
|
+
const { address = this.#family === "ipv4" ? "127.0.0.1" : "::", port = 0, flowlabel = 0, } = options;
|
|
1749
|
+
validateString(address, "options.address");
|
|
1750
|
+
const _port = validatePort(port, "options.port");
|
|
1751
|
+
validateUint32(flowlabel, "options.flowlabel", false);
|
|
1752
|
+
switch (this.#family) {
|
|
1753
|
+
case "ipv4":
|
|
1754
|
+
if (!isIPv4(address)) {
|
|
1755
|
+
throw new ERR_INVALID_ADDRESS();
|
|
1756
|
+
}
|
|
1757
|
+
break;
|
|
1758
|
+
case "ipv6":
|
|
1759
|
+
if (!isIPv6(address)) {
|
|
1760
|
+
throw new ERR_INVALID_ADDRESS();
|
|
1761
|
+
}
|
|
1762
|
+
break;
|
|
1763
|
+
}
|
|
1764
|
+
// Node.js' implementation is a bit more complicated since it is backed
|
|
1765
|
+
// by a C++ class wrapping an actual socket address structure. We don't
|
|
1766
|
+
// need that here, so we keep things simple.
|
|
1767
|
+
this.#address = address;
|
|
1768
|
+
this.#port = _port;
|
|
1769
|
+
this.#flowlabel = flowlabel;
|
|
1770
|
+
}
|
|
1771
|
+
get address() {
|
|
1772
|
+
return this.#address;
|
|
1773
|
+
}
|
|
1774
|
+
get port() {
|
|
1775
|
+
return this.#port;
|
|
1776
|
+
}
|
|
1777
|
+
get family() {
|
|
1778
|
+
return this.#family;
|
|
1779
|
+
}
|
|
1780
|
+
get flowlabel() {
|
|
1781
|
+
return this.#flowlabel;
|
|
1782
|
+
}
|
|
1783
|
+
[kInspect](depth, options) {
|
|
1784
|
+
if (depth < 0)
|
|
1785
|
+
return this;
|
|
1786
|
+
const opts = {
|
|
1787
|
+
...options,
|
|
1788
|
+
depth: options.depth == null ? null : options.depth - 1,
|
|
1789
|
+
};
|
|
1790
|
+
return `SocketAddress ${inspect(this.toJSON(), opts)}`;
|
|
1791
|
+
}
|
|
1792
|
+
toJSON() {
|
|
1793
|
+
return {
|
|
1794
|
+
address: this.address,
|
|
1795
|
+
port: this.port,
|
|
1796
|
+
family: this.family,
|
|
1797
|
+
flowlabel: this.flowlabel,
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
static parse(input) {
|
|
1801
|
+
validateString(input, "input");
|
|
1802
|
+
try {
|
|
1803
|
+
const parsed = URL.parse(`http://${input}`);
|
|
1804
|
+
if (parsed == null) {
|
|
1805
|
+
return undefined;
|
|
1806
|
+
}
|
|
1807
|
+
const { hostname: address, port } = parsed;
|
|
1808
|
+
if (address.startsWith("[") && address.endsWith("]")) {
|
|
1809
|
+
return new SocketAddress({
|
|
1810
|
+
address: address.slice(1, -1),
|
|
1811
|
+
// @ts-expect-error TS2362 port will be a string, this converts it
|
|
1812
|
+
port: port | 0,
|
|
1813
|
+
family: "ipv6",
|
|
1814
|
+
});
|
|
1815
|
+
}
|
|
1816
|
+
// @ts-expect-error TS2362 port will be a string, this converts it
|
|
1817
|
+
return new SocketAddress({ address, port: port | 0 });
|
|
1818
|
+
}
|
|
1819
|
+
catch {
|
|
1820
|
+
// Ignore errors here. Return undefined if the input cannot
|
|
1821
|
+
// be successfully parsed or is not a proper socket address.
|
|
1822
|
+
}
|
|
1823
|
+
return undefined;
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
// ======================================================================================
|
|
1827
|
+
// Note: the bulk of the following BlockList related code was authored by claude...
|
|
1828
|
+
// The implementation in Node.js is split between javascript and C++.
|
|
1829
|
+
// Here we do the entire implementation as TypeScript simply because
|
|
1830
|
+
// we don't need the C++ parts at all.
|
|
1831
|
+
const kIpv6Regex = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i;
|
|
1832
|
+
const kIpv6RangeRegex = /Range: IPv6 ([0-9a-fA-F:]{1,39})-([0-9a-fA-F:]{1,39})/i;
|
|
1833
|
+
const kIpv6AddressRegex = /Address: IPv6 ([0-9a-fA-F:]{1,39})/i;
|
|
1834
|
+
const kIpv6SubnetRegex = /Subnet: IPv6 ([0-9a-fA-F:]{1,39})\/(\d{1,3})/i;
|
|
1835
|
+
const kIpv4RangeRegex = /Range: IPv4 (\d{1,3}(?:\.\d{1,3}){3})-(\d{1,3}(?:\.\d{1,3}){3})/;
|
|
1836
|
+
const kIpv4AddressRegex = /Address: IPv4 (\d{1,3}(?:\.\d{1,3}){3})/;
|
|
1837
|
+
const kIpv4SubnetRegex = /Subnet: IPv4 (\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})/;
|
|
1838
|
+
// IPv4/IPv6 CIDR network utilities
|
|
1839
|
+
function parseIPv4(ip) {
|
|
1840
|
+
// When the input is not a valid IPv4 address, return an empty array.
|
|
1841
|
+
const parts = ip.split(".");
|
|
1842
|
+
if (parts.length !== 4)
|
|
1843
|
+
return undefined;
|
|
1844
|
+
const nums = [0, 0, 0, 0];
|
|
1845
|
+
for (let n = 0; n < parts.length; n++) {
|
|
1846
|
+
const part = parts[n];
|
|
1847
|
+
if (part === undefined)
|
|
1848
|
+
return undefined;
|
|
1849
|
+
const num = parseInt(part, 10);
|
|
1850
|
+
if (isNaN(num) || num < 0 || num > 255)
|
|
1851
|
+
return undefined;
|
|
1852
|
+
nums[n] = num;
|
|
1853
|
+
}
|
|
1854
|
+
return nums;
|
|
1855
|
+
}
|
|
1856
|
+
function parseIPv6(ip) {
|
|
1857
|
+
// Handle IPv4-mapped IPv6 addresses
|
|
1858
|
+
if (ip.includes(".")) {
|
|
1859
|
+
const match = ip.match(kIpv6Regex);
|
|
1860
|
+
if (match != null && match[1]) {
|
|
1861
|
+
const ipv4Parts = parseIPv4(match[1]);
|
|
1862
|
+
if (ipv4Parts !== undefined) {
|
|
1863
|
+
return [
|
|
1864
|
+
0,
|
|
1865
|
+
0,
|
|
1866
|
+
0,
|
|
1867
|
+
0,
|
|
1868
|
+
0,
|
|
1869
|
+
0xffff,
|
|
1870
|
+
(ipv4Parts[0] << 8) | ipv4Parts[1],
|
|
1871
|
+
(ipv4Parts[2] << 8) | ipv4Parts[3],
|
|
1872
|
+
];
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
// Expand :: notation
|
|
1877
|
+
let expanded = ip;
|
|
1878
|
+
if (ip.includes("::")) {
|
|
1879
|
+
const parts = ip.split("::");
|
|
1880
|
+
if (parts.length === 2) {
|
|
1881
|
+
const left = parts[0]?.split(":") ?? [];
|
|
1882
|
+
const right = parts[1]?.split(":") ?? [];
|
|
1883
|
+
const missing = 8 - left.length - right.length;
|
|
1884
|
+
const middle = Array.from({ length: missing }).fill("0");
|
|
1885
|
+
expanded = [...left, ...middle, ...right].join(":");
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
const parts = expanded.split(":");
|
|
1889
|
+
if (parts.length !== 8)
|
|
1890
|
+
return undefined;
|
|
1891
|
+
const nums = parts.map((p) => {
|
|
1892
|
+
const num = parseInt(p || "0", 16);
|
|
1893
|
+
return num >= 0 && num <= 0xffff ? num : -1;
|
|
1894
|
+
});
|
|
1895
|
+
return nums.includes(-1) ? undefined : nums;
|
|
1896
|
+
}
|
|
1897
|
+
function ipv4ToNumber(ip) {
|
|
1898
|
+
const parts = parseIPv4(ip);
|
|
1899
|
+
if (parts === undefined)
|
|
1900
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv4 address");
|
|
1901
|
+
return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
|
|
1902
|
+
}
|
|
1903
|
+
function ipv6ToBigInt(ip) {
|
|
1904
|
+
const parts = parseIPv6(ip);
|
|
1905
|
+
if (parts === undefined)
|
|
1906
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv6 address");
|
|
1907
|
+
let result = 0n;
|
|
1908
|
+
for (let i = 0; i < 8; i++) {
|
|
1909
|
+
const part = parts[i];
|
|
1910
|
+
result = (result << 16n) | BigInt(part);
|
|
1911
|
+
}
|
|
1912
|
+
return result;
|
|
1913
|
+
}
|
|
1914
|
+
function isInIPv4Subnet(ip, network, prefix) {
|
|
1915
|
+
if (prefix < 0 || prefix > 32)
|
|
1916
|
+
return false;
|
|
1917
|
+
if (prefix === 0)
|
|
1918
|
+
return true;
|
|
1919
|
+
const ipNum = ipv4ToNumber(ip);
|
|
1920
|
+
const netNum = ipv4ToNumber(network);
|
|
1921
|
+
// Create a subnet mask by shifting all 1s left by (32 - prefix) bits
|
|
1922
|
+
// This creates a mask with 'prefix' number of leading 1s followed by 0s
|
|
1923
|
+
// For example, prefix=24 creates mask 0xffffff00 (255.255.255.0)
|
|
1924
|
+
const mask = (0xffffffff << (32 - prefix)) >>> 0;
|
|
1925
|
+
return (ipNum & mask) === (netNum & mask);
|
|
1926
|
+
}
|
|
1927
|
+
function isInIPv6Subnet(ip, network, prefix) {
|
|
1928
|
+
if (prefix < 0 || prefix > 128)
|
|
1929
|
+
return false;
|
|
1930
|
+
if (prefix === 0)
|
|
1931
|
+
return true;
|
|
1932
|
+
const ipBig = ipv6ToBigInt(ip);
|
|
1933
|
+
const netBig = ipv6ToBigInt(network);
|
|
1934
|
+
// Create a 128-bit subnet mask for IPv6 by shifting all 1s left by (128 - prefix) bits
|
|
1935
|
+
// This creates a mask with 'prefix' number of leading 1s followed by 0s
|
|
1936
|
+
// The AND with 0xfff...f ensures we stay within 128 bits
|
|
1937
|
+
// For example, prefix=64 creates a mask with 64 leading 1s and 64 trailing 0s
|
|
1938
|
+
const mask = (0xffffffffffffffffffffffffffffffffn << BigInt(128 - prefix)) &
|
|
1939
|
+
0xffffffffffffffffffffffffffffffffn;
|
|
1940
|
+
return (ipBig & mask) === (netBig & mask);
|
|
1941
|
+
}
|
|
1942
|
+
function compareIPv4(a, b) {
|
|
1943
|
+
const aNum = ipv4ToNumber(a);
|
|
1944
|
+
const bNum = ipv4ToNumber(b);
|
|
1945
|
+
return aNum - bNum;
|
|
1946
|
+
}
|
|
1947
|
+
function compareIPv6(a, b) {
|
|
1948
|
+
const aBig = ipv6ToBigInt(a);
|
|
1949
|
+
const bBig = ipv6ToBigInt(b);
|
|
1950
|
+
return aBig < bBig ? -1 : aBig > bBig ? 1 : 0;
|
|
1951
|
+
}
|
|
1952
|
+
function formatIPFamily(family) {
|
|
1953
|
+
return family === "ipv4" ? "IPv4" : "IPv6";
|
|
1954
|
+
}
|
|
1955
|
+
class AddressRule {
|
|
1956
|
+
type = "address";
|
|
1957
|
+
address;
|
|
1958
|
+
family;
|
|
1959
|
+
constructor(address, family) {
|
|
1960
|
+
this.address = address;
|
|
1961
|
+
this.family = family;
|
|
1962
|
+
}
|
|
1963
|
+
toString() {
|
|
1964
|
+
return `Address: ${formatIPFamily(this.family)} ${this.address}`;
|
|
1965
|
+
}
|
|
1966
|
+
check(address, family) {
|
|
1967
|
+
if (this.family !== family) {
|
|
1968
|
+
// Handle IPv4-mapped IPv6 addresses
|
|
1969
|
+
if (this.family === "ipv4" &&
|
|
1970
|
+
family === "ipv6" &&
|
|
1971
|
+
address.startsWith("::ffff:")) {
|
|
1972
|
+
const ipv4Part = address.substring(7);
|
|
1973
|
+
return this.address === ipv4Part;
|
|
1974
|
+
}
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1977
|
+
return this.address === address;
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
class RangeRule {
|
|
1981
|
+
type = "range";
|
|
1982
|
+
start;
|
|
1983
|
+
end;
|
|
1984
|
+
family;
|
|
1985
|
+
constructor(start, end, family) {
|
|
1986
|
+
this.start = start;
|
|
1987
|
+
this.end = end;
|
|
1988
|
+
this.family = family;
|
|
1989
|
+
}
|
|
1990
|
+
toString() {
|
|
1991
|
+
return `Range: ${formatIPFamily(this.family)} ${this.start}-${this.end}`;
|
|
1992
|
+
}
|
|
1993
|
+
check(address, family) {
|
|
1994
|
+
if (this.family !== family) {
|
|
1995
|
+
// Handle IPv4-mapped IPv6 for IPv4 ranges
|
|
1996
|
+
if (this.family === "ipv4" &&
|
|
1997
|
+
family === "ipv6" &&
|
|
1998
|
+
address.startsWith("::ffff:")) {
|
|
1999
|
+
const ipv4Part = address.substring(7);
|
|
2000
|
+
const cmpStart = compareIPv4(ipv4Part, this.start);
|
|
2001
|
+
const cmpEnd = compareIPv4(ipv4Part, this.end);
|
|
2002
|
+
return cmpStart >= 0 && cmpEnd <= 0;
|
|
2003
|
+
}
|
|
2004
|
+
return false;
|
|
2005
|
+
}
|
|
2006
|
+
if (family === "ipv4") {
|
|
2007
|
+
const cmpStart = compareIPv4(address, this.start);
|
|
2008
|
+
const cmpEnd = compareIPv4(address, this.end);
|
|
2009
|
+
return cmpStart >= 0 && cmpEnd <= 0;
|
|
2010
|
+
}
|
|
2011
|
+
else {
|
|
2012
|
+
const cmpStart = compareIPv6(address, this.start);
|
|
2013
|
+
const cmpEnd = compareIPv6(address, this.end);
|
|
2014
|
+
return cmpStart >= 0 && cmpEnd <= 0;
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
class SubnetRule {
|
|
2019
|
+
type = "subnet";
|
|
2020
|
+
network;
|
|
2021
|
+
prefix;
|
|
2022
|
+
family;
|
|
2023
|
+
constructor(network, prefix, family) {
|
|
2024
|
+
this.network = network;
|
|
2025
|
+
this.prefix = prefix;
|
|
2026
|
+
this.family = family;
|
|
2027
|
+
}
|
|
2028
|
+
toString() {
|
|
2029
|
+
return `Subnet: ${formatIPFamily(this.family)} ${this.network}/${this.prefix}`;
|
|
2030
|
+
}
|
|
2031
|
+
check(address, family) {
|
|
2032
|
+
if (this.family !== family) {
|
|
2033
|
+
// Handle IPv4-mapped IPv6 for IPv4 subnets
|
|
2034
|
+
if (this.family === "ipv4" &&
|
|
2035
|
+
family === "ipv6" &&
|
|
2036
|
+
address.startsWith("::ffff:")) {
|
|
2037
|
+
const ipv4Part = address.substring(7);
|
|
2038
|
+
return isInIPv4Subnet(ipv4Part, this.network, this.prefix);
|
|
2039
|
+
}
|
|
2040
|
+
return false;
|
|
2041
|
+
}
|
|
2042
|
+
if (family === "ipv4") {
|
|
2043
|
+
return isInIPv4Subnet(address, this.network, this.prefix);
|
|
2044
|
+
}
|
|
2045
|
+
else {
|
|
2046
|
+
return isInIPv6Subnet(address, this.network, this.prefix);
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
export class BlockList {
|
|
2051
|
+
#rules = [];
|
|
2052
|
+
static isBlockList(value) {
|
|
2053
|
+
return value instanceof BlockList;
|
|
2054
|
+
}
|
|
2055
|
+
addAddress(address, family = "ipv4") {
|
|
2056
|
+
if (SocketAddress.isSocketAddress(address)) {
|
|
2057
|
+
this.#rules.push(new AddressRule(address.address, address.family));
|
|
2058
|
+
}
|
|
2059
|
+
else {
|
|
2060
|
+
validateString(address, "address");
|
|
2061
|
+
validateOneOf(family, "family", ["ipv4", "ipv6"]);
|
|
2062
|
+
// Validate the address format
|
|
2063
|
+
if (family === "ipv4" && !isIPv4(address)) {
|
|
2064
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv4 address");
|
|
2065
|
+
}
|
|
2066
|
+
if (family === "ipv6" && !isIPv6(address)) {
|
|
2067
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv6 address");
|
|
2068
|
+
}
|
|
2069
|
+
this.#rules.push(new AddressRule(address, family));
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
addRange(start, end, family = "ipv4") {
|
|
2073
|
+
let startAddr;
|
|
2074
|
+
let endAddr;
|
|
2075
|
+
let addrFamily;
|
|
2076
|
+
if (SocketAddress.isSocketAddress(start)) {
|
|
2077
|
+
startAddr = start.address;
|
|
2078
|
+
addrFamily = start.family;
|
|
2079
|
+
}
|
|
2080
|
+
else {
|
|
2081
|
+
validateString(start, "start");
|
|
2082
|
+
validateOneOf(family, "family", ["ipv4", "ipv6"]);
|
|
2083
|
+
startAddr = start;
|
|
2084
|
+
addrFamily = family;
|
|
2085
|
+
}
|
|
2086
|
+
if (SocketAddress.isSocketAddress(end)) {
|
|
2087
|
+
endAddr = end.address;
|
|
2088
|
+
if (SocketAddress.isSocketAddress(start) && end.family !== addrFamily) {
|
|
2089
|
+
throw new ERR_INVALID_ARG_VALUE("end", end, "must be same family as start");
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
else {
|
|
2093
|
+
validateString(end, "end");
|
|
2094
|
+
endAddr = end;
|
|
2095
|
+
}
|
|
2096
|
+
// Validate addresses
|
|
2097
|
+
if (addrFamily === "ipv4") {
|
|
2098
|
+
if (!isIPv4(startAddr) || !isIPv4(endAddr)) {
|
|
2099
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv4 address");
|
|
2100
|
+
}
|
|
2101
|
+
if (compareIPv4(startAddr, endAddr) > 0) {
|
|
2102
|
+
throw new ERR_INVALID_ARG_VALUE("start", startAddr, "must come before end");
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
else {
|
|
2106
|
+
if (!isIPv6(startAddr) || !isIPv6(endAddr)) {
|
|
2107
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv6 address");
|
|
2108
|
+
}
|
|
2109
|
+
if (compareIPv6(startAddr, endAddr) > 0) {
|
|
2110
|
+
throw new ERR_INVALID_ARG_VALUE("start", startAddr, "must come before end");
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
this.#rules.push(new RangeRule(startAddr, endAddr, addrFamily));
|
|
2114
|
+
}
|
|
2115
|
+
addSubnet(network, prefix, family = "ipv4") {
|
|
2116
|
+
let networkAddr;
|
|
2117
|
+
let addrFamily;
|
|
2118
|
+
if (SocketAddress.isSocketAddress(network)) {
|
|
2119
|
+
networkAddr = network.address;
|
|
2120
|
+
addrFamily = network.family;
|
|
2121
|
+
}
|
|
2122
|
+
else {
|
|
2123
|
+
validateString(network, "network");
|
|
2124
|
+
validateOneOf(family, "family", ["ipv4", "ipv6"]);
|
|
2125
|
+
networkAddr = network;
|
|
2126
|
+
addrFamily = family;
|
|
2127
|
+
}
|
|
2128
|
+
validateInt32(prefix, "prefix");
|
|
2129
|
+
// Validate prefix range
|
|
2130
|
+
if (addrFamily === "ipv4") {
|
|
2131
|
+
if (prefix < 0 || prefix > 32) {
|
|
2132
|
+
throw new ERR_OUT_OF_RANGE("prefix", "between 0 and 32", prefix);
|
|
2133
|
+
}
|
|
2134
|
+
if (!isIPv4(networkAddr)) {
|
|
2135
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv4 address");
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
else {
|
|
2139
|
+
if (prefix < 0 || prefix > 128) {
|
|
2140
|
+
throw new ERR_OUT_OF_RANGE("prefix", "between 0 and 128", prefix);
|
|
2141
|
+
}
|
|
2142
|
+
if (!isIPv6(networkAddr)) {
|
|
2143
|
+
throw new ERR_INVALID_IP_ADDRESS("Invalid IPv6 address");
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
this.#rules.push(new SubnetRule(networkAddr, prefix, addrFamily));
|
|
2147
|
+
}
|
|
2148
|
+
check(address, family = "ipv4") {
|
|
2149
|
+
let checkAddr;
|
|
2150
|
+
let checkFamily;
|
|
2151
|
+
if (SocketAddress.isSocketAddress(address)) {
|
|
2152
|
+
checkAddr = address.address;
|
|
2153
|
+
checkFamily = address.family;
|
|
2154
|
+
}
|
|
2155
|
+
else {
|
|
2156
|
+
validateString(address, "address");
|
|
2157
|
+
validateOneOf(family, "family", ["ipv4", "ipv6"]);
|
|
2158
|
+
checkAddr = address;
|
|
2159
|
+
checkFamily = family.toLowerCase();
|
|
2160
|
+
}
|
|
2161
|
+
// Validate address format
|
|
2162
|
+
try {
|
|
2163
|
+
if (checkFamily === "ipv4" && !isIPv4(checkAddr)) {
|
|
2164
|
+
return false;
|
|
2165
|
+
}
|
|
2166
|
+
if (checkFamily === "ipv6" && !isIPv6(checkAddr)) {
|
|
2167
|
+
return false;
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
catch {
|
|
2171
|
+
return false;
|
|
2172
|
+
}
|
|
2173
|
+
// Check against all rules
|
|
2174
|
+
for (const rule of this.#rules) {
|
|
2175
|
+
if (rule.check(checkAddr, checkFamily)) {
|
|
2176
|
+
return true;
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
return false;
|
|
2180
|
+
}
|
|
2181
|
+
get rules() {
|
|
2182
|
+
return this.#rules.map((rule) => rule.toString());
|
|
2183
|
+
}
|
|
2184
|
+
[kInspect](depth, options) {
|
|
2185
|
+
if (depth < 0)
|
|
2186
|
+
return "[BlockList]";
|
|
2187
|
+
const opts = {
|
|
2188
|
+
...options,
|
|
2189
|
+
depth: options.depth == null ? null : options.depth - 1,
|
|
2190
|
+
};
|
|
2191
|
+
return `BlockList {\n rules: ${inspect(this.rules, opts)}\n}`;
|
|
2192
|
+
}
|
|
2193
|
+
toJSON() {
|
|
2194
|
+
return this.rules;
|
|
2195
|
+
}
|
|
2196
|
+
fromJSON(data) {
|
|
2197
|
+
let rules;
|
|
2198
|
+
if (typeof data === "string") {
|
|
2199
|
+
try {
|
|
2200
|
+
rules = JSON.parse(data);
|
|
2201
|
+
}
|
|
2202
|
+
catch {
|
|
2203
|
+
throw new ERR_INVALID_ARG_VALUE("data", data, "must be valid JSON");
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
else {
|
|
2207
|
+
rules = data;
|
|
2208
|
+
}
|
|
2209
|
+
validateArray(rules, "data");
|
|
2210
|
+
for (const item of data) {
|
|
2211
|
+
if (item.includes("IPv4")) {
|
|
2212
|
+
// IPv4 subnet pattern
|
|
2213
|
+
const subnetMatch = item.match(kIpv4SubnetRegex);
|
|
2214
|
+
if (subnetMatch) {
|
|
2215
|
+
const [, network, prefix] = subnetMatch;
|
|
2216
|
+
if (network === undefined || prefix === undefined) {
|
|
2217
|
+
// Skip the rule if parsing failed.
|
|
2218
|
+
continue;
|
|
2219
|
+
}
|
|
2220
|
+
this.addSubnet(network, parseInt(prefix, 10));
|
|
2221
|
+
continue;
|
|
2222
|
+
}
|
|
2223
|
+
// IPv4 address pattern
|
|
2224
|
+
const addressMatch = item.match(kIpv4AddressRegex);
|
|
2225
|
+
if (addressMatch) {
|
|
2226
|
+
const [, address] = addressMatch;
|
|
2227
|
+
if (address === undefined) {
|
|
2228
|
+
// Skip the rule if parsing failed.
|
|
2229
|
+
continue;
|
|
2230
|
+
}
|
|
2231
|
+
this.addAddress(address);
|
|
2232
|
+
continue;
|
|
2233
|
+
}
|
|
2234
|
+
// IPv4 range pattern
|
|
2235
|
+
const rangeMatch = item.match(kIpv4RangeRegex);
|
|
2236
|
+
if (rangeMatch) {
|
|
2237
|
+
const [, start, end] = rangeMatch;
|
|
2238
|
+
if (start === undefined || end === undefined) {
|
|
2239
|
+
// Skip the rule if parsing failed.
|
|
2240
|
+
continue;
|
|
2241
|
+
}
|
|
2242
|
+
this.addRange(start, end);
|
|
2243
|
+
continue;
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
if (item.includes("IPv6")) {
|
|
2247
|
+
// IPv6 subnet pattern
|
|
2248
|
+
const ipv6SubnetMatch = item.match(kIpv6SubnetRegex);
|
|
2249
|
+
if (ipv6SubnetMatch) {
|
|
2250
|
+
const [, network, prefix] = ipv6SubnetMatch;
|
|
2251
|
+
if (network === undefined || prefix === undefined) {
|
|
2252
|
+
// Skip the rule if parsing failed.
|
|
2253
|
+
continue;
|
|
2254
|
+
}
|
|
2255
|
+
this.addSubnet(network, parseInt(prefix, 10), "ipv6");
|
|
2256
|
+
continue;
|
|
2257
|
+
}
|
|
2258
|
+
// IPv6 address pattern
|
|
2259
|
+
const ipv6AddressMatch = item.match(kIpv6AddressRegex);
|
|
2260
|
+
if (ipv6AddressMatch) {
|
|
2261
|
+
const [, address] = ipv6AddressMatch;
|
|
2262
|
+
if (address === undefined) {
|
|
2263
|
+
// Skip the rule if parsing failed.
|
|
2264
|
+
continue;
|
|
2265
|
+
}
|
|
2266
|
+
this.addAddress(address, "ipv6");
|
|
2267
|
+
continue;
|
|
2268
|
+
}
|
|
2269
|
+
// IPv6 range pattern
|
|
2270
|
+
const ipv6RangeMatch = item.match(kIpv6RangeRegex);
|
|
2271
|
+
if (ipv6RangeMatch) {
|
|
2272
|
+
const [, start, end] = ipv6RangeMatch;
|
|
2273
|
+
if (start === undefined || end === undefined) {
|
|
2274
|
+
// Skip the rule if parsing failed.
|
|
2275
|
+
continue;
|
|
2276
|
+
}
|
|
2277
|
+
this.addRange(start, end, "ipv6");
|
|
2278
|
+
continue;
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
function deferred() {
|
|
2285
|
+
let resolve;
|
|
2286
|
+
let reject;
|
|
2287
|
+
const promise = new Promise((yes, no) => {
|
|
2288
|
+
resolve = yes;
|
|
2289
|
+
reject = no;
|
|
2290
|
+
});
|
|
2291
|
+
return { promise, resolve, reject };
|
|
2292
|
+
}
|