@bowl42/keywire 0.0.0-stage → 0.3.1
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/README.md +118 -2
- package/dist/app.d.ts +2 -0
- package/dist/app.js +13 -0
- package/dist/app.js.map +1 -0
- package/dist/bytes.d.ts +1 -0
- package/dist/bytes.js +7 -0
- package/dist/bytes.js.map +1 -0
- package/dist/client.d.ts +66 -0
- package/dist/client.js +846 -0
- package/dist/client.js.map +1 -0
- package/dist/compress/block.d.ts +51 -0
- package/dist/compress/block.js +667 -0
- package/dist/compress/block.js.map +1 -0
- package/dist/compress/deflate.d.ts +7 -0
- package/dist/compress/deflate.js +603 -0
- package/dist/compress/deflate.js.map +1 -0
- package/dist/compress/index.d.ts +6 -0
- package/dist/compress/index.js +13 -0
- package/dist/compress/index.js.map +1 -0
- package/dist/compress/inflate.d.ts +21 -0
- package/dist/compress/inflate.js +380 -0
- package/dist/compress/inflate.js.map +1 -0
- package/dist/compress/wasm.d.ts +50 -0
- package/dist/compress/wasm.js +192 -0
- package/dist/compress/wasm.js.map +1 -0
- package/dist/compress/zlib-wasm.d.ts +15 -0
- package/dist/compress/zlib-wasm.js +18 -0
- package/dist/compress/zlib-wasm.js.map +1 -0
- package/dist/conn.d.ts +126 -0
- package/dist/conn.js +685 -0
- package/dist/conn.js.map +1 -0
- package/dist/dial/index.d.ts +3 -0
- package/dist/dial/index.js +10 -0
- package/dist/dial/index.js.map +1 -0
- package/dist/discovery/index.d.ts +16 -0
- package/dist/discovery/index.js +173 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/reach.d.ts +4 -0
- package/dist/discovery/reach.js +36 -0
- package/dist/discovery/reach.js.map +1 -0
- package/dist/domain.d.ts +3 -0
- package/dist/domain.js +12 -0
- package/dist/domain.js.map +1 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +49 -0
- package/dist/errors.js.map +1 -0
- package/dist/extension.d.ts +53 -0
- package/dist/extension.js +158 -0
- package/dist/extension.js.map +1 -0
- package/dist/forward.d.ts +11 -0
- package/dist/forward.js +125 -0
- package/dist/forward.js.map +1 -0
- package/dist/framed-stream.d.ts +14 -0
- package/dist/framed-stream.js +113 -0
- package/dist/framed-stream.js.map +1 -0
- package/dist/gen/keywire/discovery/v1/discovery_pb.d.ts +105 -0
- package/dist/gen/keywire/discovery/v1/discovery_pb.js +57 -0
- package/dist/gen/keywire/discovery/v1/discovery_pb.js.map +1 -0
- package/dist/gen/keywire/state/v1/state_pb.d.ts +988 -0
- package/dist/gen/keywire/state/v1/state_pb.js +184 -0
- package/dist/gen/keywire/state/v1/state_pb.js.map +1 -0
- package/dist/gen/keywire/v1/forward_pb.d.ts +53 -0
- package/dist/gen/keywire/v1/forward_pb.js +58 -0
- package/dist/gen/keywire/v1/forward_pb.js.map +1 -0
- package/dist/gen/keywire/v1/guest_pb.d.ts +322 -0
- package/dist/gen/keywire/v1/guest_pb.js +100 -0
- package/dist/gen/keywire/v1/guest_pb.js.map +1 -0
- package/dist/gen/keywire/v1/pair_pb.d.ts +106 -0
- package/dist/gen/keywire/v1/pair_pb.js +45 -0
- package/dist/gen/keywire/v1/pair_pb.js.map +1 -0
- package/dist/gen/keywire/v1/relay_pb.d.ts +116 -0
- package/dist/gen/keywire/v1/relay_pb.js +20 -0
- package/dist/gen/keywire/v1/relay_pb.js.map +1 -0
- package/dist/gen/keywire/v1/roster_pb.d.ts +213 -0
- package/dist/gen/keywire/v1/roster_pb.js +77 -0
- package/dist/gen/keywire/v1/roster_pb.js.map +1 -0
- package/dist/gen/keywire/v1/sync_pb.d.ts +68 -0
- package/dist/gen/keywire/v1/sync_pb.js +24 -0
- package/dist/gen/keywire/v1/sync_pb.js.map +1 -0
- package/dist/guest/equal-bytes.d.ts +1 -0
- package/dist/guest/equal-bytes.js +10 -0
- package/dist/guest/equal-bytes.js.map +1 -0
- package/dist/guest/hello.d.ts +4 -0
- package/dist/guest/hello.js +42 -0
- package/dist/guest/hello.js.map +1 -0
- package/dist/guest/invite-frame.d.ts +8 -0
- package/dist/guest/invite-frame.js +44 -0
- package/dist/guest/invite-frame.js.map +1 -0
- package/dist/guest/invite.d.ts +17 -0
- package/dist/guest/invite.js +86 -0
- package/dist/guest/invite.js.map +1 -0
- package/dist/guest/relay-hello.d.ts +2 -0
- package/dist/guest/relay-hello.js +38 -0
- package/dist/guest/relay-hello.js.map +1 -0
- package/dist/guest/revocations.d.ts +12 -0
- package/dist/guest/revocations.js +134 -0
- package/dist/guest/revocations.js.map +1 -0
- package/dist/guest/roster.d.ts +2 -0
- package/dist/guest/roster.js +14 -0
- package/dist/guest/roster.js.map +1 -0
- package/dist/guest/token-payload.d.ts +2 -0
- package/dist/guest/token-payload.js +71 -0
- package/dist/guest/token-payload.js.map +1 -0
- package/dist/guest-conn.d.ts +20 -0
- package/dist/guest-conn.js +99 -0
- package/dist/guest-conn.js.map +1 -0
- package/dist/guest-relay.d.ts +7 -0
- package/dist/guest-relay.js +45 -0
- package/dist/guest-relay.js.map +1 -0
- package/dist/guest.d.ts +20 -0
- package/dist/guest.js +99 -0
- package/dist/guest.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/msg-path.d.ts +9 -0
- package/dist/msg-path.js +2 -0
- package/dist/msg-path.js.map +1 -0
- package/dist/mux/byte-queue.d.ts +9 -0
- package/dist/mux/byte-queue.js +62 -0
- package/dist/mux/byte-queue.js.map +1 -0
- package/dist/mux/codec.d.ts +30 -0
- package/dist/mux/codec.js +23 -0
- package/dist/mux/codec.js.map +1 -0
- package/dist/mux/errors.d.ts +23 -0
- package/dist/mux/errors.js +47 -0
- package/dist/mux/errors.js.map +1 -0
- package/dist/mux/frame.d.ts +44 -0
- package/dist/mux/frame.js +161 -0
- package/dist/mux/frame.js.map +1 -0
- package/dist/mux/nexttask.d.ts +1 -0
- package/dist/mux/nexttask.js +19 -0
- package/dist/mux/nexttask.js.map +1 -0
- package/dist/mux/session.d.ts +69 -0
- package/dist/mux/session.js +586 -0
- package/dist/mux/session.js.map +1 -0
- package/dist/mux/signal.d.ts +5 -0
- package/dist/mux/signal.js +14 -0
- package/dist/mux/signal.js.map +1 -0
- package/dist/mux/stream.d.ts +34 -0
- package/dist/mux/stream.js +620 -0
- package/dist/mux/stream.js.map +1 -0
- package/dist/nodekit/device.d.ts +40 -0
- package/dist/nodekit/device.js +188 -0
- package/dist/nodekit/device.js.map +1 -0
- package/dist/nodekit/index.d.ts +5 -0
- package/dist/nodekit/index.js +8 -0
- package/dist/nodekit/index.js.map +1 -0
- package/dist/nodekit/indexeddb.d.ts +2 -0
- package/dist/nodekit/indexeddb.js +114 -0
- package/dist/nodekit/indexeddb.js.map +1 -0
- package/dist/nodekit/pair-device.d.ts +27 -0
- package/dist/nodekit/pair-device.js +44 -0
- package/dist/nodekit/pair-device.js.map +1 -0
- package/dist/nodekit/sealed-signer.d.ts +22 -0
- package/dist/nodekit/sealed-signer.js +74 -0
- package/dist/nodekit/sealed-signer.js.map +1 -0
- package/dist/nodekit/store.d.ts +22 -0
- package/dist/nodekit/store.js +21 -0
- package/dist/nodekit/store.js.map +1 -0
- package/dist/noise/backend.d.ts +25 -0
- package/dist/noise/backend.js +2 -0
- package/dist/noise/backend.js.map +1 -0
- package/dist/noise/backends.d.ts +4 -0
- package/dist/noise/backends.js +19 -0
- package/dist/noise/backends.js.map +1 -0
- package/dist/noise/cipher.d.ts +18 -0
- package/dist/noise/cipher.js +69 -0
- package/dist/noise/cipher.js.map +1 -0
- package/dist/noise/crypto.d.ts +11 -0
- package/dist/noise/crypto.js +47 -0
- package/dist/noise/crypto.js.map +1 -0
- package/dist/noise/handshake.d.ts +28 -0
- package/dist/noise/handshake.js +183 -0
- package/dist/noise/handshake.js.map +1 -0
- package/dist/noise/import.d.ts +2 -0
- package/dist/noise/import.js +16 -0
- package/dist/noise/import.js.map +1 -0
- package/dist/noise/noble.d.ts +2 -0
- package/dist/noise/noble.js +51 -0
- package/dist/noise/noble.js.map +1 -0
- package/dist/noise/pair-code.d.ts +2 -0
- package/dist/noise/pair-code.js +11 -0
- package/dist/noise/pair-code.js.map +1 -0
- package/dist/noise/pkcs8.d.ts +3 -0
- package/dist/noise/pkcs8.js +18 -0
- package/dist/noise/pkcs8.js.map +1 -0
- package/dist/noise/sealed.d.ts +21 -0
- package/dist/noise/sealed.js +40 -0
- package/dist/noise/sealed.js.map +1 -0
- package/dist/noise/symmetric.d.ts +17 -0
- package/dist/noise/symmetric.js +56 -0
- package/dist/noise/symmetric.js.map +1 -0
- package/dist/noise/webcrypto.d.ts +3 -0
- package/dist/noise/webcrypto.js +98 -0
- package/dist/noise/webcrypto.js.map +1 -0
- package/dist/pair-relay.d.ts +4 -0
- package/dist/pair-relay.js +46 -0
- package/dist/pair-relay.js.map +1 -0
- package/dist/pair.d.ts +7 -0
- package/dist/pair.js +50 -0
- package/dist/pair.js.map +1 -0
- package/dist/paths/link.d.ts +33 -0
- package/dist/paths/link.js +132 -0
- package/dist/paths/link.js.map +1 -0
- package/dist/paths/node-paths.d.ts +10 -0
- package/dist/paths/node-paths.js +41 -0
- package/dist/paths/node-paths.js.map +1 -0
- package/dist/paths/path.d.ts +11 -0
- package/dist/paths/path.js +25 -0
- package/dist/paths/path.js.map +1 -0
- package/dist/paths/race.d.ts +18 -0
- package/dist/paths/race.js +157 -0
- package/dist/paths/race.js.map +1 -0
- package/dist/paths/relay-sessions.d.ts +11 -0
- package/dist/paths/relay-sessions.js +93 -0
- package/dist/paths/relay-sessions.js.map +1 -0
- package/dist/paths/upgrades.d.ts +12 -0
- package/dist/paths/upgrades.js +58 -0
- package/dist/paths/upgrades.js.map +1 -0
- package/dist/relay-server/admission.d.ts +24 -0
- package/dist/relay-server/admission.js +140 -0
- package/dist/relay-server/admission.js.map +1 -0
- package/dist/relay-server/config.d.ts +28 -0
- package/dist/relay-server/config.js +47 -0
- package/dist/relay-server/config.js.map +1 -0
- package/dist/relay-server/discovery/addr.d.ts +18 -0
- package/dist/relay-server/discovery/addr.js +211 -0
- package/dist/relay-server/discovery/addr.js.map +1 -0
- package/dist/relay-server/discovery/directory.d.ts +42 -0
- package/dist/relay-server/discovery/directory.js +101 -0
- package/dist/relay-server/discovery/directory.js.map +1 -0
- package/dist/relay-server/discovery/http.d.ts +10 -0
- package/dist/relay-server/discovery/http.js +172 -0
- package/dist/relay-server/discovery/http.js.map +1 -0
- package/dist/relay-server/discovery/index.d.ts +6 -0
- package/dist/relay-server/discovery/index.js +15 -0
- package/dist/relay-server/discovery/index.js.map +1 -0
- package/dist/relay-server/discovery/listing.d.ts +24 -0
- package/dist/relay-server/discovery/listing.js +115 -0
- package/dist/relay-server/discovery/listing.js.map +1 -0
- package/dist/relay-server/discovery/options.d.ts +39 -0
- package/dist/relay-server/discovery/options.js +97 -0
- package/dist/relay-server/discovery/options.js.map +1 -0
- package/dist/relay-server/discovery/service.d.ts +21 -0
- package/dist/relay-server/discovery/service.js +409 -0
- package/dist/relay-server/discovery/service.js.map +1 -0
- package/dist/relay-server/forward.d.ts +3 -0
- package/dist/relay-server/forward.js +97 -0
- package/dist/relay-server/forward.js.map +1 -0
- package/dist/relay-server/handshake.d.ts +10 -0
- package/dist/relay-server/handshake.js +36 -0
- package/dist/relay-server/handshake.js.map +1 -0
- package/dist/relay-server/index.d.ts +6 -0
- package/dist/relay-server/index.js +10 -0
- package/dist/relay-server/index.js.map +1 -0
- package/dist/relay-server/relay.d.ts +25 -0
- package/dist/relay-server/relay.js +392 -0
- package/dist/relay-server/relay.js.map +1 -0
- package/dist/relay-server/service-read.d.ts +4 -0
- package/dist/relay-server/service-read.js +16 -0
- package/dist/relay-server/service-read.js.map +1 -0
- package/dist/relay-server/service.d.ts +25 -0
- package/dist/relay-server/service.js +8 -0
- package/dist/relay-server/service.js.map +1 -0
- package/dist/relay-server/session.d.ts +60 -0
- package/dist/relay-server/session.js +35 -0
- package/dist/relay-server/session.js.map +1 -0
- package/dist/relay-server/url.d.ts +1 -0
- package/dist/relay-server/url.js +8 -0
- package/dist/relay-server/url.js.map +1 -0
- package/dist/relay-url.d.ts +2 -0
- package/dist/relay-url.js +11 -0
- package/dist/relay-url.js.map +1 -0
- package/dist/relay.d.ts +5 -0
- package/dist/relay.js +60 -0
- package/dist/relay.js.map +1 -0
- package/dist/roster/admission.d.ts +17 -0
- package/dist/roster/admission.js +85 -0
- package/dist/roster/admission.js.map +1 -0
- package/dist/roster/credentials.d.ts +5 -0
- package/dist/roster/credentials.js +72 -0
- package/dist/roster/credentials.js.map +1 -0
- package/dist/roster/digest.d.ts +3 -0
- package/dist/roster/digest.js +23 -0
- package/dist/roster/digest.js.map +1 -0
- package/dist/roster/envelope.d.ts +11 -0
- package/dist/roster/envelope.js +35 -0
- package/dist/roster/envelope.js.map +1 -0
- package/dist/roster/errors.d.ts +6 -0
- package/dist/roster/errors.js +11 -0
- package/dist/roster/errors.js.map +1 -0
- package/dist/roster/limits.d.ts +25 -0
- package/dist/roster/limits.js +26 -0
- package/dist/roster/limits.js.map +1 -0
- package/dist/roster/merge.d.ts +10 -0
- package/dist/roster/merge.js +101 -0
- package/dist/roster/merge.js.map +1 -0
- package/dist/roster/name.d.ts +1 -0
- package/dist/roster/name.js +7 -0
- package/dist/roster/name.js.map +1 -0
- package/dist/roster/order.d.ts +5 -0
- package/dist/roster/order.js +22 -0
- package/dist/roster/order.js.map +1 -0
- package/dist/roster/ownership.d.ts +9 -0
- package/dist/roster/ownership.js +42 -0
- package/dist/roster/ownership.js.map +1 -0
- package/dist/roster/retain.d.ts +2 -0
- package/dist/roster/retain.js +55 -0
- package/dist/roster/retain.js.map +1 -0
- package/dist/roster/revocations.d.ts +5 -0
- package/dist/roster/revocations.js +37 -0
- package/dist/roster/revocations.js.map +1 -0
- package/dist/roster/scan.d.ts +16 -0
- package/dist/roster/scan.js +194 -0
- package/dist/roster/scan.js.map +1 -0
- package/dist/roster/state.d.ts +34 -0
- package/dist/roster/state.js +204 -0
- package/dist/roster/state.js.map +1 -0
- package/dist/roster/statement.d.ts +38 -0
- package/dist/roster/statement.js +176 -0
- package/dist/roster/statement.js.map +1 -0
- package/dist/roster/types.d.ts +35 -0
- package/dist/roster/types.js +2 -0
- package/dist/roster/types.js.map +1 -0
- package/dist/roster/verify.d.ts +3 -0
- package/dist/roster/verify.js +63 -0
- package/dist/roster/verify.js.map +1 -0
- package/dist/roster-sync/apply.d.ts +2 -0
- package/dist/roster-sync/apply.js +23 -0
- package/dist/roster-sync/apply.js.map +1 -0
- package/dist/roster-sync/exchange.d.ts +3 -0
- package/dist/roster-sync/exchange.js +25 -0
- package/dist/roster-sync/exchange.js.map +1 -0
- package/dist/roster-sync/frame.d.ts +17 -0
- package/dist/roster-sync/frame.js +66 -0
- package/dist/roster-sync/frame.js.map +1 -0
- package/dist/roster-sync/records.d.ts +8 -0
- package/dist/roster-sync/records.js +78 -0
- package/dist/roster-sync/records.js.map +1 -0
- package/dist/roster-sync/sync.d.ts +12 -0
- package/dist/roster-sync/sync.js +69 -0
- package/dist/roster-sync/sync.js.map +1 -0
- package/dist/rtc.d.ts +15 -0
- package/dist/rtc.js +275 -0
- package/dist/rtc.js.map +1 -0
- package/dist/state/errors.d.ts +30 -0
- package/dist/state/errors.js +72 -0
- package/dist/state/errors.js.map +1 -0
- package/dist/state/index.d.ts +6 -0
- package/dist/state/index.js +11 -0
- package/dist/state/index.js.map +1 -0
- package/dist/state/limits.d.ts +34 -0
- package/dist/state/limits.js +115 -0
- package/dist/state/limits.js.map +1 -0
- package/dist/state/remote.d.ts +217 -0
- package/dist/state/remote.js +1934 -0
- package/dist/state/remote.js.map +1 -0
- package/dist/state/shared.d.ts +56 -0
- package/dist/state/shared.js +300 -0
- package/dist/state/shared.js.map +1 -0
- package/dist/state/state.d.ts +118 -0
- package/dist/state/state.js +1048 -0
- package/dist/state/state.js.map +1 -0
- package/dist/state/store.d.ts +31 -0
- package/dist/state/store.js +375 -0
- package/dist/state/store.js.map +1 -0
- package/dist/state/wire.d.ts +43 -0
- package/dist/state/wire.js +179 -0
- package/dist/state/wire.js.map +1 -0
- package/dist/strict-proto.d.ts +10 -0
- package/dist/strict-proto.js +167 -0
- package/dist/strict-proto.js.map +1 -0
- package/dist/token.d.ts +18 -0
- package/dist/token.js +60 -0
- package/dist/token.js.map +1 -0
- package/dist/urls.d.ts +7 -0
- package/dist/urls.js +117 -0
- package/dist/urls.js.map +1 -0
- package/dist/ws.d.ts +19 -0
- package/dist/ws.js +143 -0
- package/dist/ws.js.map +1 -0
- package/package.json +72 -3
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
// Blocks of a deflate stream (spec/compression.md; Go: internal/compress):
|
|
2
|
+
// a direction is a sequence of blocks of at most 64 KiB, each deflated with
|
|
3
|
+
// a sync flush in the direction's context, or plain, outside it.
|
|
4
|
+
import { CompressionMemoryError, } from "../mux/codec.js";
|
|
5
|
+
import { Deflater, ENCODER_BYTES } from "./deflate.js";
|
|
6
|
+
import { CompressionError, Context, Inflater, MAX_BLOCK, MAX_BODY, SLACK, SYNC_MARKER, WINDOW, } from "./inflate.js";
|
|
7
|
+
import { WASM_DECODER_BYTES, WASM_ENCODER_BYTES, WasmEncoder, wasmAvailable, wasmDecoder, wasmEncoder, } from "./wasm.js";
|
|
8
|
+
export { CompressionError, ENCODER_BYTES, MAX_BLOCK, MAX_BODY, SLACK, WINDOW };
|
|
9
|
+
// WINDOW_BYTES is what a writer whose encoder went to another keeps: its
|
|
10
|
+
// context.
|
|
11
|
+
export const WINDOW_BYTES = WINDOW;
|
|
12
|
+
// MAX_UVARINT bounds the head (MAX_BLOCK*2+1) and length uvarints.
|
|
13
|
+
const MAX_UVARINT = 3;
|
|
14
|
+
// maxEncoded is the longest block carrying size bytes.
|
|
15
|
+
export const maxEncoded = (size) => 2 * MAX_UVARINT + size + SLACK;
|
|
16
|
+
function putUvarint(out, off, v) {
|
|
17
|
+
while (v >= 0x80) {
|
|
18
|
+
out[off++] = (v & 0x7f) | 0x80;
|
|
19
|
+
v = Math.floor(v / 128);
|
|
20
|
+
}
|
|
21
|
+
out[off++] = v;
|
|
22
|
+
return off;
|
|
23
|
+
}
|
|
24
|
+
function uvarintLen(v) {
|
|
25
|
+
let n = 1;
|
|
26
|
+
while (v >= 0x80) {
|
|
27
|
+
v = Math.floor(v / 128);
|
|
28
|
+
n++;
|
|
29
|
+
}
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
32
|
+
// appendPlain returns a plain block of data, 1 to MAX_BLOCK bytes.
|
|
33
|
+
// plainHead is the head of a plain block of size bytes.
|
|
34
|
+
export function plainHead(size) {
|
|
35
|
+
const head = size * 2 + 1;
|
|
36
|
+
const out = new Uint8Array(uvarintLen(head));
|
|
37
|
+
putUvarint(out, 0, head);
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
export function plainBlock(data) {
|
|
41
|
+
const head = plainHead(data.length);
|
|
42
|
+
const out = new Uint8Array(head.length + data.length);
|
|
43
|
+
out.set(head);
|
|
44
|
+
out.set(data, head.length);
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
// Encoder compresses one direction of a stream: its context runs from one
|
|
48
|
+
// block to the next.
|
|
49
|
+
export class Encoder {
|
|
50
|
+
#d;
|
|
51
|
+
constructor(d = new Deflater()) {
|
|
52
|
+
this.#d = d;
|
|
53
|
+
}
|
|
54
|
+
// reset sets the context to the last WINDOW bytes of dict, as after an
|
|
55
|
+
// encoder passed to another stream and back.
|
|
56
|
+
reset(dict) {
|
|
57
|
+
this.#d.reset(dict);
|
|
58
|
+
}
|
|
59
|
+
// block returns a deflate block of data, 1 to MAX_BLOCK bytes; fast,
|
|
60
|
+
// made with the fast path.
|
|
61
|
+
block(data, fast = false) {
|
|
62
|
+
return frame(this.#d, data, fast);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// frame encodes data with d, framed as a deflate block.
|
|
66
|
+
function frame(d, data, fast) {
|
|
67
|
+
const body = d.block(data, fast);
|
|
68
|
+
if (body.length < 1 || body.length > data.length + SLACK)
|
|
69
|
+
throw new Error("keywire: deflate output out of bounds");
|
|
70
|
+
const head = data.length * 2;
|
|
71
|
+
const out = new Uint8Array(uvarintLen(head) + uvarintLen(body.length) + body.length);
|
|
72
|
+
out.set(body, putUvarint(out, putUvarint(out, 0, head), body.length));
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
// encodeBlocks encodes msgs as the deflate blocks of one stream direction,
|
|
76
|
+
// one each, the encoder first reset with the last WINDOW bytes of what it
|
|
77
|
+
// encoded before each message whose index is in resets, and each whose
|
|
78
|
+
// index is in fast made with the fast path: the blocks Go's encoder
|
|
79
|
+
// writes for the same (keywire-peer -deflate-oracle).
|
|
80
|
+
export function encodeBlocks(msgs, resets = [], fast = []) {
|
|
81
|
+
const d = new Deflater();
|
|
82
|
+
d.reset();
|
|
83
|
+
const at = new Set(resets);
|
|
84
|
+
const quick = new Set(fast);
|
|
85
|
+
const blocks = [];
|
|
86
|
+
let sent = new Uint8Array(0);
|
|
87
|
+
for (let i = 0; i < msgs.length; i++) {
|
|
88
|
+
if (at.has(i))
|
|
89
|
+
d.reset(sent);
|
|
90
|
+
blocks.push(frame(d, msgs[i], quick.has(i)));
|
|
91
|
+
const next = new Uint8Array(Math.min(WINDOW, sent.length + msgs[i].length));
|
|
92
|
+
const tail = msgs[i].subarray(Math.max(0, msgs[i].length - next.length));
|
|
93
|
+
next.set(sent.subarray(sent.length - (next.length - tail.length)));
|
|
94
|
+
next.set(tail, next.length - tail.length);
|
|
95
|
+
sent = next;
|
|
96
|
+
}
|
|
97
|
+
return blocks;
|
|
98
|
+
}
|
|
99
|
+
// Spare encoders, given back by streams that ended or lent theirs; at most
|
|
100
|
+
// a few are kept.
|
|
101
|
+
const spare = [];
|
|
102
|
+
const SPARE = 4;
|
|
103
|
+
// Budget bounds the memory the writers given it hold, all together: an
|
|
104
|
+
// encoder (ENCODER_BYTES) for each writing with one, the context
|
|
105
|
+
// (WINDOW_BYTES) for each that had its encoder taken, and the stage
|
|
106
|
+
// (MAX_BLOCK) of each stream gathering writes. A writer short of
|
|
107
|
+
// room takes it from those used longest ago: first their encoders,
|
|
108
|
+
// keeping their contexts, then their contexts, which they then start
|
|
109
|
+
// again without. Neither changes what the peer decodes; a writer that
|
|
110
|
+
// finds no room at all writes plain blocks.
|
|
111
|
+
export class Budget {
|
|
112
|
+
limit;
|
|
113
|
+
used = 0;
|
|
114
|
+
#tick = 0;
|
|
115
|
+
holders = new Set();
|
|
116
|
+
#idleMs;
|
|
117
|
+
#sweep;
|
|
118
|
+
// A writer that has not written for idleMs gives its encoder up and
|
|
119
|
+
// keeps its context; never if idleMs is 0.
|
|
120
|
+
constructor(limit, idleMs = 0) {
|
|
121
|
+
this.limit = limit;
|
|
122
|
+
this.#idleMs = idleMs;
|
|
123
|
+
}
|
|
124
|
+
// grow gives w need more bytes, taking them from others if it must.
|
|
125
|
+
grow(w, need) {
|
|
126
|
+
w.lastUse = ++this.#tick;
|
|
127
|
+
w.at = Date.now();
|
|
128
|
+
if (this.used + need > this.limit && !this.#take(w, this.used + need - this.limit))
|
|
129
|
+
return false;
|
|
130
|
+
this.used += need;
|
|
131
|
+
w.held += need;
|
|
132
|
+
this.holders.add(w);
|
|
133
|
+
this.#arm();
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
// reserve takes n bytes for a stage if they are free with room for an
|
|
137
|
+
// encoder besides: a stage is worth less than an encoder or a context,
|
|
138
|
+
// and never takes one's place.
|
|
139
|
+
reserve(n) {
|
|
140
|
+
if (this.used + n + ENCODER_BYTES > this.limit)
|
|
141
|
+
return false;
|
|
142
|
+
this.used += n;
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
release(n) {
|
|
146
|
+
this.used -= n;
|
|
147
|
+
}
|
|
148
|
+
// reserveDecode takes n bytes for a decoding direction, from writers if
|
|
149
|
+
// it must, as grow does for an encoder: a block that arrived must be
|
|
150
|
+
// decoded, while an encoder or a context given up costs only ratio.
|
|
151
|
+
reserveDecode(n) {
|
|
152
|
+
if (this.used + n > this.limit && !this.#take(undefined, this.used + n - this.limit))
|
|
153
|
+
return false;
|
|
154
|
+
this.used += n;
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
touch(w) {
|
|
158
|
+
w.lastUse = ++this.#tick;
|
|
159
|
+
w.at = Date.now();
|
|
160
|
+
}
|
|
161
|
+
// #arm has idle encoders swept once idleMs has passed. The timer keeps
|
|
162
|
+
// no Node process alive.
|
|
163
|
+
#arm() {
|
|
164
|
+
if (this.#idleMs <= 0 || this.#sweep !== undefined)
|
|
165
|
+
return;
|
|
166
|
+
this.#sweep = setTimeout(() => this.#sweepIdle(), this.#idleMs);
|
|
167
|
+
this.#sweep.unref?.();
|
|
168
|
+
}
|
|
169
|
+
// #sweepIdle takes the encoders of writers idle for idleMs, and looks
|
|
170
|
+
// again later while any encoder is held. Encoding is synchronous, so no
|
|
171
|
+
// writer is in the middle of a block here.
|
|
172
|
+
#sweepIdle() {
|
|
173
|
+
this.#sweep = undefined;
|
|
174
|
+
const now = Date.now();
|
|
175
|
+
let held = false;
|
|
176
|
+
for (const w of this.holders) {
|
|
177
|
+
if (!w.hasEncoder)
|
|
178
|
+
continue;
|
|
179
|
+
if (now - w.at >= this.#idleMs)
|
|
180
|
+
w.shrink(true);
|
|
181
|
+
else
|
|
182
|
+
held = true;
|
|
183
|
+
}
|
|
184
|
+
if (held)
|
|
185
|
+
this.#arm();
|
|
186
|
+
}
|
|
187
|
+
// #take frees at least n bytes from writers other than w, least
|
|
188
|
+
// recently used first: encoders, then contexts.
|
|
189
|
+
#take(w, n) {
|
|
190
|
+
const others = [...this.holders].filter((v) => v !== w).sort((x, y) => x.lastUse - y.lastUse);
|
|
191
|
+
let freed = 0;
|
|
192
|
+
for (const encoders of [true, false]) {
|
|
193
|
+
for (const v of others) {
|
|
194
|
+
if (freed >= n)
|
|
195
|
+
return true;
|
|
196
|
+
freed += v.shrink(encoders);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return freed >= n;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// BlockWriter encodes one direction of a stream, a block at a time: plan
|
|
203
|
+
// takes an encoder from the budget if it needs one and says the most the
|
|
204
|
+
// block can take, before block encodes anything. Without room, or with
|
|
205
|
+
// compression off, blocks go plain, outside the context.
|
|
206
|
+
// Data that does not compress is not worth an encoder's time, as in Go: a
|
|
207
|
+
// writer samples what its deflate blocks make of SKIP_SAMPLE bytes, and
|
|
208
|
+
// where they come to SKIP_RATIO of the data or more, sends the next
|
|
209
|
+
// SKIP_FIRST bytes plain, then probes again; each failed probe doubles the
|
|
210
|
+
// plain run, up to SKIP_MOST.
|
|
211
|
+
// A deflate block of FAST_FROM bytes or more whose credit was there at
|
|
212
|
+
// once is made with the encoder's fast path, as in Go: so large a block is
|
|
213
|
+
// a burst's, and credit at once means the link keeps up with the encoder.
|
|
214
|
+
const FAST_FROM = 32 << 10;
|
|
215
|
+
const SKIP_SAMPLE = 16 << 10;
|
|
216
|
+
const SKIP_RATIO = 0.95;
|
|
217
|
+
const SKIP_FIRST = 64 << 10;
|
|
218
|
+
const SKIP_MOST = 1 << 20;
|
|
219
|
+
export class BlockWriter {
|
|
220
|
+
// compress is the mode encode uses.
|
|
221
|
+
compress = true;
|
|
222
|
+
#budget;
|
|
223
|
+
#enc;
|
|
224
|
+
// #encBytes is what the budget counts for the encoder, by its engine.
|
|
225
|
+
#encBytes = 0;
|
|
226
|
+
// #ctx is the context while the writer has no encoder: undefined also
|
|
227
|
+
// when it never had one, or gave it up.
|
|
228
|
+
#ctx;
|
|
229
|
+
#closed = false;
|
|
230
|
+
#deflate = false;
|
|
231
|
+
// held is what the budget counts for the writer; lastUse when it last
|
|
232
|
+
// wrote.
|
|
233
|
+
held = 0;
|
|
234
|
+
lastUse = 0;
|
|
235
|
+
// at is when it last wrote, in ms.
|
|
236
|
+
at = 0;
|
|
237
|
+
// Deflated data since the last decision and what it came to; data still
|
|
238
|
+
// to go plain, and the last plain run.
|
|
239
|
+
#sampled = 0;
|
|
240
|
+
#made = 0;
|
|
241
|
+
#plain = 0;
|
|
242
|
+
#run = 0;
|
|
243
|
+
// noSkip, set only by measurements, deflates whatever the data.
|
|
244
|
+
noSkip = false;
|
|
245
|
+
constructor(budget) {
|
|
246
|
+
this.#budget = budget;
|
|
247
|
+
}
|
|
248
|
+
plan(size, compress) {
|
|
249
|
+
if (size < 1 || size > MAX_BLOCK)
|
|
250
|
+
throw new RangeError("keywire: block size out of range");
|
|
251
|
+
this.#deflate =
|
|
252
|
+
compress && !this.#closed && (this.#plain <= 0 || this.noSkip) && this.#encoder();
|
|
253
|
+
return this.#deflate ? maxEncoded(size) : MAX_UVARINT + size;
|
|
254
|
+
}
|
|
255
|
+
// block encodes chunk as plan last decided: a deflate block, whole, or a
|
|
256
|
+
// plain block's head and the chunk itself, uncopied.
|
|
257
|
+
block(chunk, creditAtOnce = false) {
|
|
258
|
+
if (this.#deflate && this.#enc) {
|
|
259
|
+
const b = frame(this.#enc, chunk, creditAtOnce && chunk.length >= FAST_FROM);
|
|
260
|
+
this.#sample(chunk.length, b.length);
|
|
261
|
+
return [b];
|
|
262
|
+
}
|
|
263
|
+
// Only plain data sent counts against the plain run: block is called
|
|
264
|
+
// once the block is committed, plan perhaps more often.
|
|
265
|
+
if (this.#plain > 0)
|
|
266
|
+
this.#plain -= chunk.length;
|
|
267
|
+
return [plainHead(chunk.length), chunk];
|
|
268
|
+
}
|
|
269
|
+
// encode encodes data whole, for tests and measurements.
|
|
270
|
+
encode(data) {
|
|
271
|
+
const parts = [];
|
|
272
|
+
for (let off = 0; off < data.length; off += MAX_BLOCK) {
|
|
273
|
+
const chunk = data.subarray(off, Math.min(data.length, off + MAX_BLOCK));
|
|
274
|
+
this.plan(chunk.length, this.compress);
|
|
275
|
+
parts.push(...this.block(chunk));
|
|
276
|
+
}
|
|
277
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
278
|
+
let off = 0;
|
|
279
|
+
for (const p of parts) {
|
|
280
|
+
out.set(p, off);
|
|
281
|
+
off += p.length;
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
// #sample counts a deflate block of n bytes of data that came to made,
|
|
286
|
+
// and decides once it has seen SKIP_SAMPLE bytes.
|
|
287
|
+
#sample(n, made) {
|
|
288
|
+
this.#sampled += n;
|
|
289
|
+
this.#made += made;
|
|
290
|
+
if (this.#sampled < SKIP_SAMPLE)
|
|
291
|
+
return;
|
|
292
|
+
if (this.#made >= SKIP_RATIO * this.#sampled) {
|
|
293
|
+
this.#run = Math.min(Math.max(2 * this.#run, SKIP_FIRST), SKIP_MOST);
|
|
294
|
+
this.#plain = this.#run;
|
|
295
|
+
}
|
|
296
|
+
else
|
|
297
|
+
this.#run = 0;
|
|
298
|
+
this.#sampled = 0;
|
|
299
|
+
this.#made = 0;
|
|
300
|
+
}
|
|
301
|
+
// #encoder makes sure the writer has an encoder, set to its context; it
|
|
302
|
+
// returns false where the budget has no room for one.
|
|
303
|
+
// The WebAssembly engine's, where it loads, has a free encoder, and the
|
|
304
|
+
// budget room for one; else the JS engine's, smaller.
|
|
305
|
+
#encoder() {
|
|
306
|
+
if (this.#enc) {
|
|
307
|
+
this.#budget?.touch(this);
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
const kept = this.#ctx ? WINDOW_BYTES : 0;
|
|
311
|
+
if (wasmAvailable() && this.#grow(WASM_ENCODER_BYTES - kept)) {
|
|
312
|
+
const w = wasmEncoder();
|
|
313
|
+
if (w) {
|
|
314
|
+
w.reset(this.#ctx);
|
|
315
|
+
this.#take(w, WASM_ENCODER_BYTES);
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
this.#ungrow(WASM_ENCODER_BYTES - kept);
|
|
319
|
+
}
|
|
320
|
+
if (!this.#grow(ENCODER_BYTES - kept))
|
|
321
|
+
return false;
|
|
322
|
+
const d = spare.pop() ?? new Deflater();
|
|
323
|
+
d.reset(this.#ctx);
|
|
324
|
+
this.#take(d, ENCODER_BYTES);
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
#grow(n) {
|
|
328
|
+
return !this.#budget || this.#budget.grow(this, n);
|
|
329
|
+
}
|
|
330
|
+
#ungrow(n) {
|
|
331
|
+
const b = this.#budget;
|
|
332
|
+
if (!b)
|
|
333
|
+
return;
|
|
334
|
+
b.used -= n;
|
|
335
|
+
this.held -= n;
|
|
336
|
+
if (this.held === 0)
|
|
337
|
+
b.holders.delete(this);
|
|
338
|
+
}
|
|
339
|
+
#take(enc, bytes) {
|
|
340
|
+
this.#enc = enc;
|
|
341
|
+
this.#encBytes = bytes;
|
|
342
|
+
this.#ctx = undefined;
|
|
343
|
+
}
|
|
344
|
+
get hasEncoder() {
|
|
345
|
+
return this.#enc !== undefined;
|
|
346
|
+
}
|
|
347
|
+
// shrink gives up the encoder, keeping the context, or with encoders
|
|
348
|
+
// false the context; it returns the bytes freed.
|
|
349
|
+
shrink(encoders) {
|
|
350
|
+
const b = this.#budget;
|
|
351
|
+
if (encoders && this.#enc) {
|
|
352
|
+
// The context it keeps is its encoder's window; an encoder that
|
|
353
|
+
// never wrote leaves none, and then no charge.
|
|
354
|
+
const ctx = this.#enc.context();
|
|
355
|
+
this.#ctx = ctx.length > 0 ? ctx : undefined;
|
|
356
|
+
giveBack(this.#enc);
|
|
357
|
+
this.#enc = undefined;
|
|
358
|
+
const freed = this.#encBytes - (this.#ctx ? WINDOW_BYTES : 0);
|
|
359
|
+
b.used -= freed;
|
|
360
|
+
this.held -= freed;
|
|
361
|
+
if (this.held === 0)
|
|
362
|
+
b.holders.delete(this);
|
|
363
|
+
return freed;
|
|
364
|
+
}
|
|
365
|
+
if (!encoders && !this.#enc && this.#ctx) {
|
|
366
|
+
this.#ctx = undefined;
|
|
367
|
+
b.used -= WINDOW_BYTES;
|
|
368
|
+
this.held -= WINDOW_BYTES;
|
|
369
|
+
b.holders.delete(this);
|
|
370
|
+
return WINDOW_BYTES;
|
|
371
|
+
}
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
close() {
|
|
375
|
+
if (this.#closed)
|
|
376
|
+
return;
|
|
377
|
+
this.#closed = true;
|
|
378
|
+
this.#ctx = undefined;
|
|
379
|
+
if (this.#enc) {
|
|
380
|
+
giveBack(this.#enc);
|
|
381
|
+
this.#enc = undefined;
|
|
382
|
+
}
|
|
383
|
+
const b = this.#budget;
|
|
384
|
+
if (b) {
|
|
385
|
+
b.used -= this.held;
|
|
386
|
+
this.held = 0;
|
|
387
|
+
b.holders.delete(this);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function giveBack(d) {
|
|
392
|
+
if (d instanceof WasmEncoder)
|
|
393
|
+
d.release();
|
|
394
|
+
else if (spare.length < SPARE)
|
|
395
|
+
spare.push(d);
|
|
396
|
+
}
|
|
397
|
+
var Phase;
|
|
398
|
+
(function (Phase) {
|
|
399
|
+
Phase[Phase["Head"] = 0] = "Head";
|
|
400
|
+
Phase[Phase["Length"] = 1] = "Length";
|
|
401
|
+
Phase[Phase["Body"] = 2] = "Body";
|
|
402
|
+
Phase[Phase["Plain"] = 3] = "Plain";
|
|
403
|
+
})(Phase || (Phase = {}));
|
|
404
|
+
// One inflater, and its tables, decodes every stream: decoding is
|
|
405
|
+
// synchronous.
|
|
406
|
+
const inflater = new Inflater();
|
|
407
|
+
// BlockReader decodes one direction of a stream. It takes raw data from its
|
|
408
|
+
// source only as it needs it; a CompressionError is final.
|
|
409
|
+
export class BlockReader {
|
|
410
|
+
#src;
|
|
411
|
+
#chunk;
|
|
412
|
+
#off = 0;
|
|
413
|
+
#phase = Phase.Head;
|
|
414
|
+
#size = 0;
|
|
415
|
+
#left = 0;
|
|
416
|
+
#body;
|
|
417
|
+
#bodyN = 0;
|
|
418
|
+
// #ctx is the context, made at the first deflate block.
|
|
419
|
+
#ctx;
|
|
420
|
+
#err;
|
|
421
|
+
// #budget, if set, counts what the reader holds: the context from the
|
|
422
|
+
// first deflate block on (#ctxHeld), a block's body while it is read
|
|
423
|
+
// (#bodyHeld bytes). The output is the caller's once returned.
|
|
424
|
+
#budget;
|
|
425
|
+
#ctxHeld = false;
|
|
426
|
+
#bodyHeld = 0;
|
|
427
|
+
// #ended is set once endIfIdle gave the state up: reads end.
|
|
428
|
+
#ended = false;
|
|
429
|
+
// #reads counts the reads under way.
|
|
430
|
+
#reads = 0;
|
|
431
|
+
// #wasm is the WebAssembly engine's decoder, where the reader took one
|
|
432
|
+
// at its first deflate block; it then holds the context. #ctxBytes is
|
|
433
|
+
// what the budget counts for the context, by the engine.
|
|
434
|
+
#wasm;
|
|
435
|
+
#engine;
|
|
436
|
+
#ctxBytes = WINDOW;
|
|
437
|
+
constructor(src, budget) {
|
|
438
|
+
this.#src = src;
|
|
439
|
+
this.#budget = budget;
|
|
440
|
+
}
|
|
441
|
+
#fail(why) {
|
|
442
|
+
this.#release();
|
|
443
|
+
const err = new CompressionError(why);
|
|
444
|
+
this.#err = err;
|
|
445
|
+
throw err;
|
|
446
|
+
}
|
|
447
|
+
#release() {
|
|
448
|
+
this.#chunk = undefined;
|
|
449
|
+
this.#dropBody();
|
|
450
|
+
this.#ctx = undefined;
|
|
451
|
+
this.#wasm?.release();
|
|
452
|
+
this.#wasm = undefined;
|
|
453
|
+
if (this.#ctxHeld) {
|
|
454
|
+
this.#ctxHeld = false;
|
|
455
|
+
this.#budget?.release(this.#ctxBytes);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
#dropBody() {
|
|
459
|
+
this.#body = undefined;
|
|
460
|
+
if (this.#bodyHeld) {
|
|
461
|
+
this.#budget?.release(this.#bodyHeld);
|
|
462
|
+
this.#bodyHeld = 0;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
// #hold has the budget count a block's body of n bytes, the context too
|
|
466
|
+
// at the first deflate block; without room, the stream is refused.
|
|
467
|
+
#hold(n) {
|
|
468
|
+
// The engine, at the first deflate block: the WebAssembly one's
|
|
469
|
+
// decoder where there is one free and room for it and this block's
|
|
470
|
+
// body together, else the JS one, which holds less.
|
|
471
|
+
if (this.#engine === undefined) {
|
|
472
|
+
const w = wasmDecoder();
|
|
473
|
+
if (w && (!this.#budget || this.#budget.reserveDecode(WASM_DECODER_BYTES + n))) {
|
|
474
|
+
this.#engine = "wasm";
|
|
475
|
+
this.#wasm = w;
|
|
476
|
+
this.#ctxBytes = WASM_DECODER_BYTES;
|
|
477
|
+
if (this.#budget) {
|
|
478
|
+
this.#ctxHeld = true;
|
|
479
|
+
this.#bodyHeld = n;
|
|
480
|
+
}
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
w?.release();
|
|
484
|
+
this.#engine = "js";
|
|
485
|
+
}
|
|
486
|
+
if (!this.#budget)
|
|
487
|
+
return;
|
|
488
|
+
const need = n + (this.#ctxHeld ? 0 : this.#ctxBytes);
|
|
489
|
+
if (!this.#budget.reserveDecode(need)) {
|
|
490
|
+
this.#release();
|
|
491
|
+
const err = new CompressionMemoryError();
|
|
492
|
+
this.#err = err;
|
|
493
|
+
throw err;
|
|
494
|
+
}
|
|
495
|
+
this.#ctxHeld = true;
|
|
496
|
+
this.#bodyHeld = n;
|
|
497
|
+
}
|
|
498
|
+
// close gives up reading and frees the reader's buffers.
|
|
499
|
+
close() {
|
|
500
|
+
this.#err ??= new Error("keywire: reader closed");
|
|
501
|
+
this.#release();
|
|
502
|
+
}
|
|
503
|
+
// endIfIdle frees the reader's state if it is between blocks with no
|
|
504
|
+
// raw data at hand: the caller knows no more can come, and reads end.
|
|
505
|
+
// A read under way may be about to take a block the caller no longer
|
|
506
|
+
// sees queued: it is left to end, or take the block, itself.
|
|
507
|
+
endIfIdle() {
|
|
508
|
+
if (this.#reads > 0 || this.#phase !== Phase.Head || this.#have() || this.#ended)
|
|
509
|
+
return;
|
|
510
|
+
this.#ended = true;
|
|
511
|
+
this.#release();
|
|
512
|
+
}
|
|
513
|
+
// #have says raw data is at hand, without waiting.
|
|
514
|
+
#have() {
|
|
515
|
+
return this.#chunk !== undefined && this.#off < this.#chunk.length;
|
|
516
|
+
}
|
|
517
|
+
// #more makes raw data available; false at the end of the stream.
|
|
518
|
+
async #more() {
|
|
519
|
+
while (!this.#chunk || this.#off === this.#chunk.length) {
|
|
520
|
+
const c = await this.#src();
|
|
521
|
+
// Closed while waiting: nothing more is taken, or counted.
|
|
522
|
+
if (this.#err)
|
|
523
|
+
throw this.#err;
|
|
524
|
+
if (c === null)
|
|
525
|
+
return false;
|
|
526
|
+
this.#chunk = c;
|
|
527
|
+
this.#off = 0;
|
|
528
|
+
}
|
|
529
|
+
return true;
|
|
530
|
+
}
|
|
531
|
+
// read resolves with the next data, or null at the end of the stream.
|
|
532
|
+
// An idle reader holds its context only.
|
|
533
|
+
async read() {
|
|
534
|
+
this.#reads++;
|
|
535
|
+
try {
|
|
536
|
+
return await this.#read();
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
this.#reads--;
|
|
540
|
+
if (this.#chunk && this.#off === this.#chunk.length)
|
|
541
|
+
this.#chunk = undefined;
|
|
542
|
+
// Closed, or failed, under the read: nothing it took stays counted.
|
|
543
|
+
if (this.#err)
|
|
544
|
+
this.#release();
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
// #live throws where the reader was closed while a read waited: after
|
|
548
|
+
// every await, before the read touches its state or reserves.
|
|
549
|
+
#live() {
|
|
550
|
+
if (this.#err)
|
|
551
|
+
throw this.#err;
|
|
552
|
+
}
|
|
553
|
+
async #read() {
|
|
554
|
+
for (;;) {
|
|
555
|
+
if (this.#err)
|
|
556
|
+
throw this.#err;
|
|
557
|
+
if (this.#ended)
|
|
558
|
+
return null;
|
|
559
|
+
switch (this.#phase) {
|
|
560
|
+
case Phase.Head: {
|
|
561
|
+
const v = await this.#uvarint(true);
|
|
562
|
+
this.#live();
|
|
563
|
+
if (v === null) {
|
|
564
|
+
this.#release();
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
if (v < 2 || v > MAX_BLOCK * 2 + 1)
|
|
568
|
+
this.#fail("head");
|
|
569
|
+
this.#size = Math.floor(v / 2);
|
|
570
|
+
if (v % 2 === 1) {
|
|
571
|
+
this.#phase = Phase.Plain;
|
|
572
|
+
this.#left = this.#size;
|
|
573
|
+
}
|
|
574
|
+
else
|
|
575
|
+
this.#phase = Phase.Length;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
case Phase.Length: {
|
|
579
|
+
const v = (await this.#uvarint(false));
|
|
580
|
+
this.#live();
|
|
581
|
+
if (v < 1 || v > this.#size + SLACK)
|
|
582
|
+
this.#fail("length");
|
|
583
|
+
this.#hold(v + SYNC_MARKER.length);
|
|
584
|
+
this.#left = v;
|
|
585
|
+
this.#bodyN = 0;
|
|
586
|
+
this.#body = new Uint8Array(v + SYNC_MARKER.length);
|
|
587
|
+
this.#phase = Phase.Body;
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
case Phase.Body: {
|
|
591
|
+
if (!(this.#have() || (await this.#more())))
|
|
592
|
+
this.#fail("truncated body");
|
|
593
|
+
this.#live();
|
|
594
|
+
const c = this.#chunk;
|
|
595
|
+
const n = Math.min(c.length - this.#off, this.#left - this.#bodyN);
|
|
596
|
+
this.#body.set(c.subarray(this.#off, this.#off + n), this.#bodyN);
|
|
597
|
+
this.#off += n;
|
|
598
|
+
this.#bodyN += n;
|
|
599
|
+
if (this.#bodyN === this.#left)
|
|
600
|
+
return this.#decode();
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
case Phase.Plain: {
|
|
604
|
+
if (!(this.#have() || (await this.#more())))
|
|
605
|
+
this.#fail("plain truncated");
|
|
606
|
+
this.#live();
|
|
607
|
+
const c = this.#chunk;
|
|
608
|
+
const n = Math.min(c.length - this.#off, this.#left);
|
|
609
|
+
const out = c.subarray(this.#off, this.#off + n);
|
|
610
|
+
this.#off += n;
|
|
611
|
+
this.#left -= n;
|
|
612
|
+
if (this.#left === 0)
|
|
613
|
+
this.#phase = Phase.Head;
|
|
614
|
+
return out;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
#decode() {
|
|
620
|
+
const body = this.#body;
|
|
621
|
+
body.set(SYNC_MARKER, this.#left);
|
|
622
|
+
this.#dropBody();
|
|
623
|
+
if (this.#wasm) {
|
|
624
|
+
const out = this.#wasm.block(body, this.#size);
|
|
625
|
+
if (!out)
|
|
626
|
+
this.#fail("deflate");
|
|
627
|
+
this.#phase = Phase.Head;
|
|
628
|
+
return out;
|
|
629
|
+
}
|
|
630
|
+
this.#ctx ??= new Context();
|
|
631
|
+
// The output is the caller's: a fresh array, the block's size.
|
|
632
|
+
const out = new Uint8Array(this.#size);
|
|
633
|
+
try {
|
|
634
|
+
inflater.inflate(this.#ctx, out, this.#size, body);
|
|
635
|
+
}
|
|
636
|
+
catch (e) {
|
|
637
|
+
if (e instanceof CompressionError)
|
|
638
|
+
this.#fail(e.why);
|
|
639
|
+
throw e;
|
|
640
|
+
}
|
|
641
|
+
this.#ctx.add(out);
|
|
642
|
+
this.#phase = Phase.Head;
|
|
643
|
+
return out;
|
|
644
|
+
}
|
|
645
|
+
// #uvarint reads a shortest-form uvarint of at most MAX_UVARINT bytes;
|
|
646
|
+
// at a block boundary (first), the end of the stream is null.
|
|
647
|
+
async #uvarint(first) {
|
|
648
|
+
let v = 0;
|
|
649
|
+
for (let i = 0;; i++) {
|
|
650
|
+
if (!(this.#have() || (await this.#more()))) {
|
|
651
|
+
if (first && i === 0)
|
|
652
|
+
return null;
|
|
653
|
+
this.#fail("truncated uvarint");
|
|
654
|
+
}
|
|
655
|
+
const b = this.#chunk[this.#off++];
|
|
656
|
+
if (i === MAX_UVARINT)
|
|
657
|
+
this.#fail("uvarint long");
|
|
658
|
+
v += (b & 0x7f) * 2 ** (7 * i);
|
|
659
|
+
if (b < 0x80) {
|
|
660
|
+
if (i > 0 && b === 0)
|
|
661
|
+
this.#fail("uvarint form");
|
|
662
|
+
return v;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
//# sourceMappingURL=block.js.map
|