@ac-kit/format-netstring 0.1.0

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 ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @ac-kit/format-netstring
2
+
3
+ Netstring framing (`<length>:<data>,`): a `Decoder`, an `Encoder`, the codec
4
+ pairing them, and `TransformStream`s over both.
5
+
6
+ ```ts
7
+ import {
8
+ createNetstringCodec,
9
+ encodeNetstring,
10
+ NetstringDecodeStream,
11
+ } from "@ac-kit/format-netstring";
12
+
13
+ encodeNetstring("hello"); // 5:hello,
14
+ ```
15
+
16
+ A netstring announces its own length before sending any of it, so
17
+ `maxPayloadLength` (1 MiB by default) is checked against the declaration rather
18
+ than against bytes received: a hostile `999999999999:` is rejected immediately
19
+ instead of after the receive buffer fills.
20
+
21
+ A length-field violation is recoverable — the decoder knows exactly how many
22
+ bytes to discard, so framing resynchronises at the next frame. A missing comma
23
+ terminator is fatal: the declared length pointed somewhere it should not have,
24
+ so the length itself cannot be trusted and there is no position to resynchronise
25
+ from.
26
+
27
+ Spec: <https://cr.yp.to/proto/netstrings.txt\>
28
+
29
+ See the
30
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/format-netstring/)
31
+ for the full reference.
@@ -0,0 +1,5 @@
1
+ export * from "./netstring/netstring-codec.js";
2
+ export * from "./netstring/netstring-decoder.js";
3
+ export * from "./netstring/netstring-encoder.js";
4
+ export * from "./netstring/netstring-error.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./netstring/netstring-codec.js";
2
+ export * from "./netstring/netstring-decoder.js";
3
+ export * from "./netstring/netstring-encoder.js";
4
+ export * from "./netstring/netstring-error.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { Codec } from "@ac-kit/format-core";
2
+ import { type NetstringDecoderOptions } from "./netstring-decoder.js";
3
+ export type NetstringCodecOptions = NetstringDecoderOptions;
4
+ /**
5
+ * Creates the netstring codec: `length:payload,`.
6
+ *
7
+ * Use {@link createNetstringDecoder} or {@link createNetstringEncoder} directly
8
+ * where only one direction is needed; a `FrameLink` needs both.
9
+ *
10
+ * Spec: https://cr.yp.to/proto/netstrings.txt
11
+ */
12
+ export declare function createNetstringCodec(options?: NetstringCodecOptions): Codec<Uint8Array, Uint8Array | string>;
13
+ //# sourceMappingURL=netstring-codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-codec.d.ts","sourceRoot":"","sources":["../../src/netstring/netstring-codec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EAEN,KAAK,uBAAuB,EAC5B,MAAM,wBAAwB,CAAC;AAGhC,MAAM,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAE5D;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CACnC,OAAO,CAAC,EAAE,qBAAqB,GAC7B,KAAK,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAAC,CAQxC"}
@@ -0,0 +1,19 @@
1
+ import { createNetstringDecoder, } from "./netstring-decoder.js";
2
+ import { createNetstringEncoder } from "./netstring-encoder.js";
3
+ /**
4
+ * Creates the netstring codec: `length:payload,`.
5
+ *
6
+ * Use {@link createNetstringDecoder} or {@link createNetstringEncoder} directly
7
+ * where only one direction is needed; a `FrameLink` needs both.
8
+ *
9
+ * Spec: https://cr.yp.to/proto/netstrings.txt
10
+ */
11
+ export function createNetstringCodec(options) {
12
+ const decoder = createNetstringDecoder(options);
13
+ const encoder = createNetstringEncoder();
14
+ return {
15
+ decode: (view, context) => decoder.decode(view, context),
16
+ encode: (value) => encoder.encode(value),
17
+ };
18
+ }
19
+ //# sourceMappingURL=netstring-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-codec.js","sourceRoot":"","sources":["../../src/netstring/netstring-codec.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,sBAAsB,GAEtB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAIhE;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CACnC,OAA+B;IAE/B,MAAM,OAAO,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;IAChD,MAAM,OAAO,GAAG,sBAAsB,EAAE,CAAC;IAEzC,OAAO;QACN,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC;QACxD,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;KACxC,CAAC;AACH,CAAC"}
@@ -0,0 +1,54 @@
1
+ import { DecodeStream, type Decoder } from "@ac-kit/format-core";
2
+ /** Construction options for the netstring decoding half. */
3
+ export interface NetstringDecoderOptions {
4
+ /**
5
+ * Maximum declared payload length, in bytes.
6
+ *
7
+ * A netstring announces its own length before sending any of it, so this
8
+ * limit is checked against the declaration rather than against bytes
9
+ * received. That lets a hostile `999999999999:` be rejected immediately
10
+ * instead of after the receive buffer fills.
11
+ *
12
+ * Defaults to 1 MiB.
13
+ */
14
+ maxPayloadLength?: number;
15
+ }
16
+ /**
17
+ * Creates the netstring read half: `length:payload,` in, payload bytes out.
18
+ *
19
+ * Length-field violations are reported as recoverable errors — the decoder
20
+ * knows exactly how many bytes to discard, so framing resynchronises — while a
21
+ * missing terminator is fatal, because at that point the declared length cannot
22
+ * be trusted and frame synchronisation is lost.
23
+ *
24
+ * The single implementation of the netstring read grammar; every other read
25
+ * face in this package is driven by it.
26
+ *
27
+ * Spec: https://cr.yp.to/proto/netstrings.txt
28
+ */
29
+ export declare function createNetstringDecoder(options?: NetstringDecoderOptions): Decoder<Uint8Array, Uint8Array>;
30
+ export type NetstringDecodeStreamOptions = NetstringDecoderOptions & {
31
+ /**
32
+ * Hard ceiling on undecoded retained bytes. Distinct from
33
+ * {@link NetstringDecoderOptions.maxPayloadLength}, which bounds one declared
34
+ * frame; this bounds how much a producer may hand over before any of it is
35
+ * decodable. Defaults to 1 MiB.
36
+ */
37
+ maxBufferSize?: number;
38
+ /**
39
+ * Invoked for a recoverable protocol violation. Decoding continues afterward
40
+ * — the Streams Standard has no event for a transform stream to report an
41
+ * error without closing, so this callback is the substitute for the
42
+ * `'error'`-without-`destroy()` pattern a Node.js `Transform` would use.
43
+ */
44
+ onProtocolError?: (error: unknown) => void;
45
+ };
46
+ /**
47
+ * Decodes a byte stream of netstring-framed data into individual payloads.
48
+ *
49
+ * Spec: https://cr.yp.to/proto/netstrings.txt
50
+ */
51
+ export declare class NetstringDecodeStream extends DecodeStream<Uint8Array, Uint8Array, Uint8Array> {
52
+ constructor(options?: NetstringDecodeStreamOptions);
53
+ }
54
+ //# sourceMappingURL=netstring-decoder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-decoder.d.ts","sourceRoot":"","sources":["../../src/netstring/netstring-decoder.ts"],"names":[],"mappings":"AAQA,OAAO,EAGN,YAAY,EACZ,KAAK,OAAO,EAEZ,MAAM,qBAAqB,CAAC;AAO7B,4DAA4D;AAC5D,MAAM,WAAW,uBAAuB;IACvC;;;;;;;;;OASG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CACrC,OAAO,CAAC,EAAE,uBAAuB,GAC/B,OAAO,CAAC,UAAU,EAAE,UAAU,CAAC,CAyEjC;AAED,MAAM,MAAM,4BAA4B,GAAG,uBAAuB,GAAG;IACpE;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;;;OAKG;IACH,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAC3C,CAAC;AAEF;;;;GAIG;AACH,qBAAa,qBAAsB,SAAQ,YAAY,CACtD,UAAU,EACV,UAAU,EACV,UAAU,CACV;IACA,YAAY,OAAO,CAAC,EAAE,4BAA4B,EAOjD;CACD"}
@@ -0,0 +1,93 @@
1
+ import { ByteAccumulator, BYTES_PER_MIB, COLON, COMMA, DIGIT_ZERO, isAsciiDigit, } from "@ac-kit/core";
2
+ import { DECODE_INCOMPLETE, decodeIncomplete, DecodeStream, } from "@ac-kit/format-core";
3
+ import { NetstringProtocolError } from "./netstring-error.js";
4
+ const DEFAULT_MAX_PAYLOAD_LENGTH_ = BYTES_PER_MIB;
5
+ const DEFAULT_MAX_BUFFER_SIZE_ = BYTES_PER_MIB;
6
+ /**
7
+ * Creates the netstring read half: `length:payload,` in, payload bytes out.
8
+ *
9
+ * Length-field violations are reported as recoverable errors — the decoder
10
+ * knows exactly how many bytes to discard, so framing resynchronises — while a
11
+ * missing terminator is fatal, because at that point the declared length cannot
12
+ * be trusted and frame synchronisation is lost.
13
+ *
14
+ * The single implementation of the netstring read grammar; every other read
15
+ * face in this package is driven by it.
16
+ *
17
+ * Spec: https://cr.yp.to/proto/netstrings.txt
18
+ */
19
+ export function createNetstringDecoder(options) {
20
+ const maxPayloadLength = options?.maxPayloadLength ?? DEFAULT_MAX_PAYLOAD_LENGTH_;
21
+ return {
22
+ decode(view) {
23
+ // One pass validates the digits and accumulates the length. Parsing the
24
+ // field as a string would allocate once per frame for no benefit, and
25
+ // checking the ceiling as we go rejects an absurd declaration before the
26
+ // value can overflow.
27
+ let length = 0;
28
+ let colon = -1;
29
+ for (let index = 0; index < view.length; index++) {
30
+ const byte = view[index];
31
+ if (byte === COLON) {
32
+ colon = index;
33
+ break;
34
+ }
35
+ if (!isAsciiDigit(byte)) {
36
+ return {
37
+ status: "error",
38
+ error: new NetstringProtocolError(`Unexpected byte 0x${byte.toString(16).padStart(2, "0")} in netstring length field`),
39
+ consumed: index + 1,
40
+ };
41
+ }
42
+ length = length * 10 + (byte - DIGIT_ZERO);
43
+ if (length > maxPayloadLength) {
44
+ return {
45
+ status: "fatal",
46
+ error: new NetstringProtocolError(`Declared netstring payload exceeds the ${maxPayloadLength} byte limit`),
47
+ };
48
+ }
49
+ }
50
+ if (colon === -1) {
51
+ // Every buffered byte is a digit, so the length field is still arriving.
52
+ return DECODE_INCOMPLETE;
53
+ }
54
+ if (colon === 0) {
55
+ return {
56
+ status: "error",
57
+ error: new NetstringProtocolError("Empty netstring length field"),
58
+ consumed: 1,
59
+ };
60
+ }
61
+ const total = colon + 1 + length + 1;
62
+ if (view.length < total) {
63
+ return decodeIncomplete(total);
64
+ }
65
+ const terminator = view[total - 1];
66
+ if (terminator !== COMMA) {
67
+ return {
68
+ status: "fatal",
69
+ error: new NetstringProtocolError(`Expected netstring comma terminator, got 0x${(terminator ?? 0)
70
+ .toString(16)
71
+ .padStart(2, "0")}`),
72
+ };
73
+ }
74
+ // Copy: the view is invalidated as soon as the driver consumes.
75
+ const payload = view.slice(colon + 1, colon + 1 + length);
76
+ return { status: "decoded", value: payload, consumed: total };
77
+ },
78
+ };
79
+ }
80
+ /**
81
+ * Decodes a byte stream of netstring-framed data into individual payloads.
82
+ *
83
+ * Spec: https://cr.yp.to/proto/netstrings.txt
84
+ */
85
+ export class NetstringDecodeStream extends DecodeStream {
86
+ constructor(options) {
87
+ super(createNetstringDecoder(options), {
88
+ buffer: new ByteAccumulator(options?.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE_),
89
+ ...(options?.onProtocolError ? { onError: options.onProtocolError } : {}),
90
+ });
91
+ }
92
+ }
93
+ //# sourceMappingURL=netstring-decoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-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,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;QAC/D,CAAC;KACD,CAAC;AACH,CAAC;AAoBD;;;;GAIG;AACH,MAAM,OAAO,qBAAsB,SAAQ,YAI1C;IACA,YAAY,OAAsC;QACjD,KAAK,CAAC,sBAAsB,CAAC,OAAO,CAAC,EAAE;YACtC,MAAM,EAAE,IAAI,eAAe,CAC1B,OAAO,EAAE,aAAa,IAAI,wBAAwB,CAClD;YACD,GAAG,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACzE,CAAC,CAAC;IACJ,CAAC;CACD"}
@@ -0,0 +1,25 @@
1
+ import { EncodeStream, type Encoder } from "@ac-kit/format-core";
2
+ /**
3
+ * Creates the netstring write half: payload in, `length:payload,` out.
4
+ *
5
+ * Spec: https://cr.yp.to/proto/netstrings.txt
6
+ */
7
+ export declare function createNetstringEncoder(): Encoder<Uint8Array | string>;
8
+ /**
9
+ * Encodes a single netstring frame (`length:payload,`) into one contiguous
10
+ * buffer. Strings are treated as UTF-8.
11
+ *
12
+ * A utility over the same framing the encoder emits, not a second
13
+ * implementation: it only concatenates the vectored parts.
14
+ */
15
+ export declare function encodeNetstring(data: Uint8Array | string): Uint8Array;
16
+ /**
17
+ * Encodes `Uint8Array` or string payloads as netstring frames
18
+ * (`length:payload,`). Strings are treated as UTF-8.
19
+ *
20
+ * Spec: https://cr.yp.to/proto/netstrings.txt
21
+ */
22
+ export declare class NetstringEncodeStream extends EncodeStream<Uint8Array | string> {
23
+ constructor();
24
+ }
25
+ //# sourceMappingURL=netstring-encoder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-encoder.d.ts","sourceRoot":"","sources":["../../src/netstring/netstring-encoder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,KAAK,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAgBjE;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,OAAO,CAAC,UAAU,GAAG,MAAM,CAAC,CAErE;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,UAAU,CAErE;AAED;;;;;GAKG;AACH,qBAAa,qBAAsB,SAAQ,YAAY,CAAC,UAAU,GAAG,MAAM,CAAC;IAC3E,cAEC;CACD"}
@@ -0,0 +1,44 @@
1
+ import { concatBytes, encodeTextUtf8 } from "@ac-kit/core";
2
+ import { EncodeStream } from "@ac-kit/format-core";
3
+ const COMMA_BYTES_ = encodeTextUtf8(",");
4
+ /**
5
+ * Frames one payload as `length:payload,`, vectored so the payload itself is
6
+ * never copied — the length header and the terminator are the only
7
+ * allocations.
8
+ *
9
+ * The single implementation of the netstring write grammar.
10
+ */
11
+ function encodeParts(data) {
12
+ const payload = typeof data === "string" ? encodeTextUtf8(data) : data;
13
+ return [encodeTextUtf8(`${payload.length}:`), payload, COMMA_BYTES_];
14
+ }
15
+ /**
16
+ * Creates the netstring write half: payload in, `length:payload,` out.
17
+ *
18
+ * Spec: https://cr.yp.to/proto/netstrings.txt
19
+ */
20
+ export function createNetstringEncoder() {
21
+ return { encode: encodeParts };
22
+ }
23
+ /**
24
+ * Encodes a single netstring frame (`length:payload,`) into one contiguous
25
+ * buffer. Strings are treated as UTF-8.
26
+ *
27
+ * A utility over the same framing the encoder emits, not a second
28
+ * implementation: it only concatenates the vectored parts.
29
+ */
30
+ export function encodeNetstring(data) {
31
+ return concatBytes(...encodeParts(data));
32
+ }
33
+ /**
34
+ * Encodes `Uint8Array` or string payloads as netstring frames
35
+ * (`length:payload,`). Strings are treated as UTF-8.
36
+ *
37
+ * Spec: https://cr.yp.to/proto/netstrings.txt
38
+ */
39
+ export class NetstringEncodeStream extends EncodeStream {
40
+ constructor() {
41
+ super(createNetstringEncoder());
42
+ }
43
+ }
44
+ //# sourceMappingURL=netstring-encoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-encoder.js","sourceRoot":"","sources":["../../src/netstring/netstring-encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAgB,MAAM,qBAAqB,CAAC;AAEjE,MAAM,YAAY,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;AAEzC;;;;;;GAMG;AACH,SAAS,WAAW,CAAC,IAAyB;IAC7C,MAAM,OAAO,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvE,OAAO,CAAC,cAAc,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;AACtE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sBAAsB;IACrC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;AAChC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,IAAyB;IACxD,OAAO,WAAW,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,qBAAsB,SAAQ,YAAiC;IAC3E;QACC,KAAK,CAAC,sBAAsB,EAAE,CAAC,CAAC;IACjC,CAAC;CACD"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Thrown when a netstring frame cannot be decoded.
3
+ *
4
+ * Recoverable by default: the decoder knows how many bytes to discard, so
5
+ * framing resynchronises at the next length field.
6
+ */
7
+ export declare class NetstringProtocolError extends Error {
8
+ constructor(message: string, options?: ErrorOptions);
9
+ }
10
+ //# sourceMappingURL=netstring-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-error.d.ts","sourceRoot":"","sources":["../../src/netstring/netstring-error.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;IAChD,YAAY,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,EAElD;CACD"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Thrown when a netstring frame cannot be decoded.
3
+ *
4
+ * Recoverable by default: the decoder knows how many bytes to discard, so
5
+ * framing resynchronises at the next length field.
6
+ */
7
+ export class NetstringProtocolError extends Error {
8
+ constructor(message, options) {
9
+ super(message, options);
10
+ }
11
+ }
12
+ //# sourceMappingURL=netstring-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"netstring-error.js","sourceRoot":"","sources":["../../src/netstring/netstring-error.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAChD,YAAY,OAAe,EAAE,OAAsB;QAClD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACzB,CAAC;CACD"}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@ac-kit/format-netstring",
3
+ "version": "0.1.0",
4
+ "description": "Netstring framing: a decoder, an encoder, the codec pairing them, and TransformStreams over both.",
5
+ "keywords": [
6
+ "codec",
7
+ "framing",
8
+ "netstring",
9
+ "streams"
10
+ ],
11
+ "homepage": "https://anthochamp.github.io/node-essentials/api/format-netstring/",
12
+ "bugs": {
13
+ "url": "https://github.com/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
20
+ "directory": "packages/format-netstring"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "main": "./dist/index.js",
33
+ "exports": {
34
+ ".": "./dist/index.js"
35
+ },
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
40
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
41
+ "clean": "rimraf dist",
42
+ "build": "yarn clean && yarn compile",
43
+ "test": "yarn run -T vitest run",
44
+ "prepack": "yarn build"
45
+ },
46
+ "dependencies": {
47
+ "@ac-kit/core": "^0.1.0",
48
+ "@ac-kit/format-core": "^0.1.0"
49
+ },
50
+ "devDependencies": {
51
+ "@ac-kit/tsconfig": "0.1.0",
52
+ "@ac-kit/typedoc-config": "0.1.0",
53
+ "@ac-kit/vitest-config": "0.1.0",
54
+ "@types/node": "^26.5.1",
55
+ "@typescript/native": "typescript@7.0.2",
56
+ "rimraf": "6.1.3",
57
+ "typedoc": "0.28.20",
58
+ "typescript": "@typescript/typescript6@6.0.2"
59
+ },
60
+ "engines": {
61
+ "node": ">=24"
62
+ }
63
+ }