@labacacia/nps-sdk 1.0.0-alpha.3 → 1.0.0-alpha.5
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/CHANGELOG.cn.md +73 -0
- package/CHANGELOG.md +82 -0
- package/README.cn.md +17 -4
- package/README.md +17 -4
- package/dist/core/anchor-cache.js +104 -0
- package/dist/core/anchor-cache.js.map +1 -0
- package/dist/core/cache.js +80 -0
- package/dist/core/cache.js.map +1 -0
- package/dist/core/canonical-json.js +44 -0
- package/dist/core/canonical-json.js.map +1 -0
- package/dist/core/codec.js +119 -0
- package/dist/core/codec.js.map +1 -0
- package/dist/core/codecs/index.js +6 -0
- package/dist/core/codecs/index.js.map +1 -0
- package/dist/core/codecs/ncp-codec.js +93 -0
- package/dist/core/codecs/ncp-codec.js.map +1 -0
- package/dist/core/codecs/tier1-json-codec.js +28 -0
- package/dist/core/codecs/tier1-json-codec.js.map +1 -0
- package/dist/core/codecs/tier2-msgpack-codec.js +26 -0
- package/dist/core/codecs/tier2-msgpack-codec.js.map +1 -0
- package/dist/core/crypto-provider.js +10 -0
- package/dist/core/crypto-provider.js.map +1 -0
- package/dist/core/exceptions.js +52 -0
- package/dist/core/exceptions.js.map +1 -0
- package/dist/core/frame-header.js +185 -0
- package/dist/core/frame-header.js.map +1 -0
- package/dist/core/frame-registry.js +63 -0
- package/dist/core/frame-registry.js.map +1 -0
- package/dist/core/frames.js +154 -0
- package/dist/core/frames.js.map +1 -0
- package/dist/core/index.js +21 -405
- package/dist/core/index.js.map +1 -1
- package/dist/core/registry.js +17 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/status-codes.d.ts +1 -0
- package/dist/core/status-codes.d.ts.map +1 -1
- package/dist/core/status-codes.js +39 -0
- package/dist/core/status-codes.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/dist/ncp/frames/anchor-frame.js +54 -0
- package/dist/ncp/frames/anchor-frame.js.map +1 -0
- package/dist/ncp/frames/caps-frame.js +29 -0
- package/dist/ncp/frames/caps-frame.js.map +1 -0
- package/dist/ncp/frames/diff-frame.js +37 -0
- package/dist/ncp/frames/diff-frame.js.map +1 -0
- package/dist/ncp/frames/error-frame.js +13 -0
- package/dist/ncp/frames/error-frame.js.map +1 -0
- package/dist/ncp/frames/hello-frame.js +25 -0
- package/dist/ncp/frames/hello-frame.js.map +1 -0
- package/dist/ncp/frames/stream-frame.js +18 -0
- package/dist/ncp/frames/stream-frame.js.map +1 -0
- package/dist/ncp/frames.js +192 -0
- package/dist/ncp/frames.js.map +1 -0
- package/dist/ncp/handshake.js +80 -0
- package/dist/ncp/handshake.js.map +1 -0
- package/dist/ncp/index.d.ts +1 -0
- package/dist/ncp/index.d.ts.map +1 -1
- package/dist/ncp/index.js +13 -368
- package/dist/ncp/index.js.map +1 -1
- package/dist/ncp/ncp-error-codes.d.ts +1 -0
- package/dist/ncp/ncp-error-codes.d.ts.map +1 -1
- package/dist/ncp/ncp-error-codes.js +34 -0
- package/dist/ncp/ncp-error-codes.js.map +1 -0
- package/dist/ncp/ncp-patch-format.js +13 -0
- package/dist/ncp/ncp-patch-format.js.map +1 -0
- package/dist/ncp/preamble.d.ts +47 -0
- package/dist/ncp/preamble.d.ts.map +1 -0
- package/dist/ncp/preamble.js +74 -0
- package/dist/ncp/preamble.js.map +1 -0
- package/dist/ncp/registry.js +13 -0
- package/dist/ncp/registry.js.map +1 -0
- package/dist/ncp/stream-manager.js +163 -0
- package/dist/ncp/stream-manager.js.map +1 -0
- package/dist/ndp/dns-txt.d.ts +35 -0
- package/dist/ndp/dns-txt.d.ts.map +1 -0
- package/dist/ndp/dns-txt.js +67 -0
- package/dist/ndp/dns-txt.js.map +1 -0
- package/dist/ndp/frames.js +87 -0
- package/dist/ndp/frames.js.map +1 -0
- package/dist/ndp/index.d.ts +1 -0
- package/dist/ndp/index.d.ts.map +1 -1
- package/dist/ndp/index.js +7 -223
- package/dist/ndp/index.js.map +1 -1
- package/dist/ndp/ndp-registry.d.ts +2 -0
- package/dist/ndp/ndp-registry.d.ts.map +1 -1
- package/dist/ndp/ndp-registry.js +104 -0
- package/dist/ndp/ndp-registry.js.map +1 -0
- package/dist/ndp/registry.js +10 -0
- package/dist/ndp/registry.js.map +1 -0
- package/dist/ndp/validator.js +48 -0
- package/dist/ndp/validator.js.map +1 -0
- package/dist/nip/acme/client.d.ts +31 -0
- package/dist/nip/acme/client.d.ts.map +1 -0
- package/dist/nip/acme/client.js +136 -0
- package/dist/nip/acme/client.js.map +1 -0
- package/dist/nip/acme/index.d.ts +6 -0
- package/dist/nip/acme/index.d.ts.map +1 -0
- package/dist/nip/acme/index.js +8 -0
- package/dist/nip/acme/index.js.map +1 -0
- package/dist/nip/acme/jws.d.ts +31 -0
- package/dist/nip/acme/jws.d.ts.map +1 -0
- package/dist/nip/acme/jws.js +76 -0
- package/dist/nip/acme/jws.js.map +1 -0
- package/dist/nip/acme/messages.d.ts +71 -0
- package/dist/nip/acme/messages.d.ts.map +1 -0
- package/dist/nip/acme/messages.js +4 -0
- package/dist/nip/acme/messages.js.map +1 -0
- package/dist/nip/acme/server.d.ts +41 -0
- package/dist/nip/acme/server.d.ts.map +1 -0
- package/dist/nip/acme/server.js +458 -0
- package/dist/nip/acme/server.js.map +1 -0
- package/dist/nip/acme/wire.d.ts +19 -0
- package/dist/nip/acme/wire.d.ts.map +1 -0
- package/dist/nip/acme/wire.js +21 -0
- package/dist/nip/acme/wire.js.map +1 -0
- package/dist/nip/assurance-level.d.ts +19 -0
- package/dist/nip/assurance-level.d.ts.map +1 -0
- package/dist/nip/assurance-level.js +38 -0
- package/dist/nip/assurance-level.js.map +1 -0
- package/dist/nip/cert-format.d.ts +5 -0
- package/dist/nip/cert-format.d.ts.map +1 -0
- package/dist/nip/cert-format.js +6 -0
- package/dist/nip/cert-format.js.map +1 -0
- package/dist/nip/error-codes.d.ts +25 -0
- package/dist/nip/error-codes.d.ts.map +1 -0
- package/dist/nip/error-codes.js +32 -0
- package/dist/nip/error-codes.js.map +1 -0
- package/dist/nip/frames.d.ts +10 -1
- package/dist/nip/frames.d.ts.map +1 -1
- package/dist/nip/frames.js +106 -0
- package/dist/nip/frames.js.map +1 -0
- package/dist/nip/identity.js +94 -0
- package/dist/nip/identity.js.map +1 -0
- package/dist/nip/index.d.ts +6 -0
- package/dist/nip/index.d.ts.map +1 -1
- package/dist/nip/index.js +12 -187
- package/dist/nip/index.js.map +1 -1
- package/dist/nip/registry.js +10 -0
- package/dist/nip/registry.js.map +1 -0
- package/dist/nip/verifier.d.ts +23 -0
- package/dist/nip/verifier.d.ts.map +1 -0
- package/dist/nip/verifier.js +90 -0
- package/dist/nip/verifier.js.map +1 -0
- package/dist/nip/x509/builder.d.ts +35 -0
- package/dist/nip/x509/builder.d.ts.map +1 -0
- package/dist/nip/x509/builder.js +59 -0
- package/dist/nip/x509/builder.js.map +1 -0
- package/dist/nip/x509/index.d.ts +4 -0
- package/dist/nip/x509/index.d.ts.map +1 -0
- package/dist/nip/x509/index.js +6 -0
- package/dist/nip/x509/index.js.map +1 -0
- package/dist/nip/x509/oids.d.ts +17 -0
- package/dist/nip/x509/oids.d.ts.map +1 -0
- package/dist/nip/x509/oids.js +23 -0
- package/dist/nip/x509/oids.js.map +1 -0
- package/dist/nip/x509/verifier.d.ts +26 -0
- package/dist/nip/x509/verifier.d.ts.map +1 -0
- package/dist/nip/x509/verifier.js +171 -0
- package/dist/nip/x509/verifier.js.map +1 -0
- package/dist/nop/client.js +90 -0
- package/dist/nop/client.js.map +1 -0
- package/dist/nop/frames.js +148 -0
- package/dist/nop/frames.js.map +1 -0
- package/dist/nop/index.js +6 -789
- package/dist/nop/index.js.map +1 -1
- package/dist/nop/models.js +50 -0
- package/dist/nop/models.js.map +1 -0
- package/dist/nop/nop-types.js +44 -0
- package/dist/nop/nop-types.js.map +1 -0
- package/dist/nop/registry.js +11 -0
- package/dist/nop/registry.js.map +1 -0
- package/dist/nwp/client.js +101 -0
- package/dist/nwp/client.js.map +1 -0
- package/dist/nwp/error-codes.d.ts +42 -0
- package/dist/nwp/error-codes.d.ts.map +1 -0
- package/dist/nwp/error-codes.js +53 -0
- package/dist/nwp/error-codes.js.map +1 -0
- package/dist/nwp/frames.js +81 -0
- package/dist/nwp/frames.js.map +1 -0
- package/dist/nwp/index.d.ts +1 -0
- package/dist/nwp/index.d.ts.map +1 -1
- package/dist/nwp/index.js +6 -693
- package/dist/nwp/index.js.map +1 -1
- package/dist/nwp/registry.js +9 -0
- package/dist/nwp/registry.js.map +1 -0
- package/dist/setup.js +29 -0
- package/dist/setup.js.map +1 -0
- package/doc/nps-sdk.nip.cn.md +30 -0
- package/doc/nps-sdk.nip.md +30 -0
- package/doc/nps-sdk.nwp.cn.md +71 -0
- package/doc/nps-sdk.nwp.md +71 -0
- package/package.json +2 -1
- package/src/core/status-codes.ts +1 -0
- package/src/index.ts +1 -1
- package/src/ncp/index.ts +1 -0
- package/src/ncp/ncp-error-codes.ts +2 -0
- package/src/ncp/preamble.ts +79 -0
- package/src/ndp/dns-txt.ts +86 -0
- package/src/ndp/index.ts +1 -0
- package/src/ndp/ndp-registry.ts +34 -0
- package/src/nip/acme/client.ts +185 -0
- package/src/nip/acme/index.ts +8 -0
- package/src/nip/acme/jws.ts +109 -0
- package/src/nip/acme/messages.ts +85 -0
- package/src/nip/acme/server.ts +480 -0
- package/src/nip/acme/wire.ts +24 -0
- package/src/nip/assurance-level.ts +40 -0
- package/src/nip/cert-format.ts +9 -0
- package/src/nip/error-codes.ts +38 -0
- package/src/nip/frames.ts +35 -3
- package/src/nip/index.ts +8 -0
- package/src/nip/verifier.ts +122 -0
- package/src/nip/x509/builder.ts +91 -0
- package/src/nip/x509/index.ts +6 -0
- package/src/nip/x509/oids.ts +28 -0
- package/src/nip/x509/verifier.ts +214 -0
- package/src/nop/client.ts +1 -1
- package/src/nwp/client.ts +4 -4
- package/src/nwp/error-codes.ts +62 -0
- package/src/nwp/index.ts +1 -0
- package/tests/_rfc0002-keys.ts +57 -0
- package/tests/ncp/preamble.test.ts +93 -0
- package/tests/ndp.test.ts +106 -0
- package/tests/nip-acme-agent01.test.ts +192 -0
- package/tests/nip-x509.test.ts +280 -0
- package/dist/core/index.cjs +0 -452
- package/dist/core/index.cjs.map +0 -1
- package/dist/index.cjs +0 -8
- package/dist/index.cjs.map +0 -1
- package/dist/ncp/index.cjs +0 -388
- package/dist/ncp/index.cjs.map +0 -1
- package/dist/ndp/index.cjs +0 -252
- package/dist/ndp/index.cjs.map +0 -1
- package/dist/nip/index.cjs +0 -214
- package/dist/nip/index.cjs.map +0 -1
- package/dist/nop/index.cjs +0 -823
- package/dist/nop/index.cjs.map +0 -1
- package/dist/nwp/index.cjs +0 -720
- package/dist/nwp/index.cjs.map +0 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Copyright 2026 INNO LOTUS PTY LTD
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* NCP native-mode connection preamble — the 8-byte ASCII constant
|
|
5
|
+
* `"NPS/1.0\n"` that every native-mode client MUST emit immediately
|
|
6
|
+
* after the transport handshake and before its first HelloFrame.
|
|
7
|
+
* Defined by NPS-RFC-0001 and NPS-1 NCP §2.6.1.
|
|
8
|
+
*
|
|
9
|
+
* HTTP-mode connections do not use the preamble.
|
|
10
|
+
*/
|
|
11
|
+
export const PREAMBLE_LITERAL = "NPS/1.0\n";
|
|
12
|
+
export const PREAMBLE_LENGTH = 8;
|
|
13
|
+
export const PREAMBLE_BYTES = new TextEncoder().encode(PREAMBLE_LITERAL);
|
|
14
|
+
/** Validation timeout in milliseconds (NPS-RFC-0001 §4.1). */
|
|
15
|
+
export const PREAMBLE_READ_TIMEOUT_MS = 10_000;
|
|
16
|
+
/** Maximum delay before closing after a mismatch, in milliseconds. */
|
|
17
|
+
export const PREAMBLE_CLOSE_DEADLINE_MS = 500;
|
|
18
|
+
export const PREAMBLE_ERROR_CODE = "NCP-PREAMBLE-INVALID";
|
|
19
|
+
export const PREAMBLE_STATUS_CODE = "NPS-PROTO-PREAMBLE-INVALID";
|
|
20
|
+
export class NcpPreambleInvalidError extends Error {
|
|
21
|
+
errorCode = PREAMBLE_ERROR_CODE;
|
|
22
|
+
statusCode = PREAMBLE_STATUS_CODE;
|
|
23
|
+
constructor(reason) {
|
|
24
|
+
super(reason);
|
|
25
|
+
this.name = "NcpPreambleInvalidError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns `true` iff `buf` starts with the 8-byte NPS/1.0 preamble.
|
|
30
|
+
* Safe to call with shorter buffers.
|
|
31
|
+
*/
|
|
32
|
+
export function preambleMatches(buf) {
|
|
33
|
+
if (buf.length < PREAMBLE_LENGTH)
|
|
34
|
+
return false;
|
|
35
|
+
for (let i = 0; i < PREAMBLE_LENGTH; i++) {
|
|
36
|
+
if (buf[i] !== PREAMBLE_BYTES[i])
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validates a presumed-preamble buffer.
|
|
43
|
+
* Returns `{ valid: true, reason: "" }` on success or `{ valid: false, reason }` on failure.
|
|
44
|
+
*/
|
|
45
|
+
export function tryValidatePreamble(buf) {
|
|
46
|
+
if (buf.length < PREAMBLE_LENGTH) {
|
|
47
|
+
return { valid: false, reason: `short read (${buf.length}/${PREAMBLE_LENGTH} bytes); peer is not speaking NCP` };
|
|
48
|
+
}
|
|
49
|
+
if (!preambleMatches(buf)) {
|
|
50
|
+
// "NPS/" = 0x4E 0x50 0x53 0x2F
|
|
51
|
+
const isNps = buf[0] === 0x4e && buf[1] === 0x50 && buf[2] === 0x53 && buf[3] === 0x2f;
|
|
52
|
+
if (isNps) {
|
|
53
|
+
return { valid: false, reason: "future-major-version NPS preamble; close with NPS-PREAMBLE-UNSUPPORTED-VERSION diagnostic" };
|
|
54
|
+
}
|
|
55
|
+
return { valid: false, reason: "preamble mismatch; peer is not speaking NPS/1.x" };
|
|
56
|
+
}
|
|
57
|
+
return { valid: true, reason: "" };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validates a presumed-preamble buffer, throwing {@link NcpPreambleInvalidError} on mismatch.
|
|
61
|
+
*/
|
|
62
|
+
export function validatePreamble(buf) {
|
|
63
|
+
const { valid, reason } = tryValidatePreamble(buf);
|
|
64
|
+
if (!valid)
|
|
65
|
+
throw new NcpPreambleInvalidError(reason);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Writes the preamble bytes to `writer`.
|
|
69
|
+
* `writer` must expose a `write(buf: Uint8Array): void` method (e.g. Node.js `net.Socket`).
|
|
70
|
+
*/
|
|
71
|
+
export function writePreamble(writer) {
|
|
72
|
+
writer.write(PREAMBLE_BYTES);
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=preamble.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preamble.js","sourceRoot":"","sources":["../../src/ncp/preamble.ts"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,sCAAsC;AAEtC;;;;;;;GAOG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAC5C,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AACjC,MAAM,CAAC,MAAM,cAAc,GAAe,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;AACrF,8DAA8D;AAC9D,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAC/C,sEAAsE;AACtE,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,CAAC;AAE9C,MAAM,CAAC,MAAM,mBAAmB,GAAI,sBAAsB,CAAC;AAC3D,MAAM,CAAC,MAAM,oBAAoB,GAAG,4BAA4B,CAAC;AAEjE,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IACvC,SAAS,GAAI,mBAAmB,CAAC;IACjC,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AE,EAAE,CAAC;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAe;IAC9C,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;IACnD,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,uBAAuB,CAAC,MAAM,CAAC,CAAC;AACxD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,MAAwC;IACpE,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2026 INNO LOTUS PTY LTD
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { FrameType } from "../core/frames.js";
|
|
4
|
+
import { AnchorFrame, CapsFrame, DiffFrame, ErrorFrame, HelloFrame, StreamFrame } from "./frames.js";
|
|
5
|
+
export function registerNcpFrames(registry) {
|
|
6
|
+
registry.register(FrameType.ANCHOR, AnchorFrame);
|
|
7
|
+
registry.register(FrameType.DIFF, DiffFrame);
|
|
8
|
+
registry.register(FrameType.STREAM, StreamFrame);
|
|
9
|
+
registry.register(FrameType.CAPS, CapsFrame);
|
|
10
|
+
registry.register(FrameType.HELLO, HelloFrame);
|
|
11
|
+
registry.register(FrameType.ERROR, ErrorFrame);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/ncp/registry.ts"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,sCAAsC;AAGtC,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAErG,MAAM,UAAU,iBAAiB,CAAC,QAAuB;IACvD,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACjD,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,EAAI,SAAS,CAAC,CAAC;IAC/C,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACjD,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,EAAI,SAAS,CAAC,CAAC;IAC/C,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAG,UAAU,CAAC,CAAC;IAChD,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAG,UAAU,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
|
|
3
|
+
//
|
|
4
|
+
// StreamManager — Concurrent stream tracking, sequence validation, flow control
|
|
5
|
+
// NPS-1 §4.3, §7.3
|
|
6
|
+
import { NcpError } from "../core/frame-header.js";
|
|
7
|
+
import { NCP_ERROR_CODES } from "./ncp-error-codes.js";
|
|
8
|
+
/**
|
|
9
|
+
* Manages concurrent StreamFrame streams.
|
|
10
|
+
*
|
|
11
|
+
* - Tracks active streams by stream_id
|
|
12
|
+
* - Validates sequential seq numbers
|
|
13
|
+
* - Enforces max concurrent stream limit (NPS-1 §7.3, default 32)
|
|
14
|
+
* - Detects early termination via error_code
|
|
15
|
+
* - Enforces window-based flow control on outgoing streams (NCP-S-07–11)
|
|
16
|
+
*/
|
|
17
|
+
export class StreamManager {
|
|
18
|
+
streams = new Map();
|
|
19
|
+
outgoing = new Map();
|
|
20
|
+
maxConcurrent;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.maxConcurrent = options?.maxConcurrent ?? 32;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Receive a StreamFrame chunk.
|
|
26
|
+
*
|
|
27
|
+
* @returns true if stream is complete (is_last=true or error_code set).
|
|
28
|
+
* @throws {NcpError} NCP-STREAM-LIMIT-EXCEEDED if too many concurrent streams.
|
|
29
|
+
* @throws {NcpError} NCP-STREAM-NOT-FOUND if frame.seq > 0 for a stream that was never opened.
|
|
30
|
+
* @throws {NcpError} NPS-CLIENT-CONFLICT if the stream_id was already completed (stream-id reuse; see test_cases NCP-S-04).
|
|
31
|
+
* @throws {NcpError} NCP-STREAM-SEQ-GAP if sequence number is not expected.
|
|
32
|
+
*/
|
|
33
|
+
receive(frame) {
|
|
34
|
+
let stream = this.streams.get(frame.stream_id);
|
|
35
|
+
if (!stream) {
|
|
36
|
+
// A stream is opened only by seq=0. Any other seq on an unknown stream_id
|
|
37
|
+
// means the opener was never seen (NCP-S-13: unknown stream_id).
|
|
38
|
+
if (frame.seq !== 0) {
|
|
39
|
+
throw new NcpError(NCP_ERROR_CODES.NCP_STREAM_NOT_FOUND, `Unknown stream_id ${frame.stream_id} — first frame must have seq=0`);
|
|
40
|
+
}
|
|
41
|
+
// New stream — check concurrent limit
|
|
42
|
+
if (this.streams.size >= this.maxConcurrent) {
|
|
43
|
+
throw new NcpError(NCP_ERROR_CODES.NCP_STREAM_LIMIT_EXCEEDED, `Max concurrent streams (${this.maxConcurrent}) exceeded`);
|
|
44
|
+
}
|
|
45
|
+
stream = {
|
|
46
|
+
streamId: frame.stream_id,
|
|
47
|
+
expectedSeq: 0,
|
|
48
|
+
chunks: [],
|
|
49
|
+
completed: false,
|
|
50
|
+
};
|
|
51
|
+
this.streams.set(frame.stream_id, stream);
|
|
52
|
+
}
|
|
53
|
+
// Reject writes to completed streams. Per test_cases NCP-S-04, the spec does
|
|
54
|
+
// not assign a dedicated NCP code for stream-id reuse; interim mapping uses
|
|
55
|
+
// the NPS-level NPS-CLIENT-CONFLICT until a spec-side code is added.
|
|
56
|
+
if (stream.completed) {
|
|
57
|
+
throw new NcpError("NPS-CLIENT-CONFLICT", `Stream ${frame.stream_id} is already completed — cannot reuse stream_id`);
|
|
58
|
+
}
|
|
59
|
+
// Sequence validation
|
|
60
|
+
if (frame.seq !== stream.expectedSeq) {
|
|
61
|
+
// Duplicate detection — same seq as last accepted
|
|
62
|
+
if (frame.seq < stream.expectedSeq) {
|
|
63
|
+
// Ignore duplicate (idempotent)
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
throw new NcpError(NCP_ERROR_CODES.NCP_STREAM_SEQ_GAP, `Expected seq ${stream.expectedSeq}, got ${frame.seq} on stream ${frame.stream_id}`);
|
|
67
|
+
}
|
|
68
|
+
stream.chunks.push(frame.data);
|
|
69
|
+
stream.expectedSeq = frame.seq + 1;
|
|
70
|
+
// Early termination via error_code
|
|
71
|
+
if (frame.error_code) {
|
|
72
|
+
stream.completed = true;
|
|
73
|
+
stream.errorCode = frame.error_code;
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
// Normal completion
|
|
77
|
+
if (frame.is_last) {
|
|
78
|
+
stream.completed = true;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Send a StreamFrame on an outgoing stream, enforcing window-based flow control.
|
|
85
|
+
*
|
|
86
|
+
* - seq=0 with window_size initialises remainingWindow (no decrement for opening frame).
|
|
87
|
+
* - Subsequent sends decrement remainingWindow when flow control is active.
|
|
88
|
+
* - Throws NCP-STREAM-WINDOW-OVERFLOW when remainingWindow === 0.
|
|
89
|
+
*
|
|
90
|
+
* @throws {NcpError} NCP-STREAM-WINDOW-OVERFLOW if window is exhausted.
|
|
91
|
+
*/
|
|
92
|
+
send(frame) {
|
|
93
|
+
let out = this.outgoing.get(frame.stream_id);
|
|
94
|
+
if (!out) {
|
|
95
|
+
out = {
|
|
96
|
+
streamId: frame.stream_id,
|
|
97
|
+
remainingWindow: undefined,
|
|
98
|
+
paused: false,
|
|
99
|
+
};
|
|
100
|
+
this.outgoing.set(frame.stream_id, out);
|
|
101
|
+
}
|
|
102
|
+
// Opening frame: initialise window from window_size if provided.
|
|
103
|
+
if (frame.seq === 0 && frame.window_size !== undefined) {
|
|
104
|
+
out.remainingWindow = frame.window_size;
|
|
105
|
+
return; // opening frame does not consume a window slot
|
|
106
|
+
}
|
|
107
|
+
// Flow control check for subsequent frames.
|
|
108
|
+
if (out.remainingWindow !== undefined) {
|
|
109
|
+
if (out.remainingWindow === 0) {
|
|
110
|
+
throw new NcpError(NCP_ERROR_CODES.NCP_STREAM_WINDOW_OVERFLOW, `Window exhausted on stream ${frame.stream_id}`);
|
|
111
|
+
}
|
|
112
|
+
out.remainingWindow -= 1;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Update the send window for a stream.
|
|
117
|
+
*
|
|
118
|
+
* Called when a reverse-direction StreamFrame arrives with data=[] and window_size set.
|
|
119
|
+
* Replaces remainingWindow with new_size. Sets paused=true when new_size === 0.
|
|
120
|
+
*/
|
|
121
|
+
updateWindow(streamId, newSize) {
|
|
122
|
+
let out = this.outgoing.get(streamId);
|
|
123
|
+
if (!out) {
|
|
124
|
+
out = {
|
|
125
|
+
streamId,
|
|
126
|
+
remainingWindow: newSize,
|
|
127
|
+
paused: newSize === 0,
|
|
128
|
+
};
|
|
129
|
+
this.outgoing.set(streamId, out);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
out.remainingWindow = newSize;
|
|
133
|
+
out.paused = newSize === 0;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns true when the outgoing stream is paused (window=0 was received).
|
|
137
|
+
* Resumes (returns false) once a non-zero window update arrives.
|
|
138
|
+
*/
|
|
139
|
+
isPaused(streamId) {
|
|
140
|
+
return this.outgoing.get(streamId)?.paused ?? false;
|
|
141
|
+
}
|
|
142
|
+
/** Get reassembled data for a completed stream. */
|
|
143
|
+
getData(streamId) {
|
|
144
|
+
const stream = this.streams.get(streamId);
|
|
145
|
+
if (!stream || !stream.completed)
|
|
146
|
+
return null;
|
|
147
|
+
return stream.chunks.flat();
|
|
148
|
+
}
|
|
149
|
+
/** Get error code if stream terminated with error. */
|
|
150
|
+
getError(streamId) {
|
|
151
|
+
return this.streams.get(streamId)?.errorCode;
|
|
152
|
+
}
|
|
153
|
+
/** Number of active (non-completed) streams. */
|
|
154
|
+
get activeCount() {
|
|
155
|
+
let count = 0;
|
|
156
|
+
for (const s of this.streams.values()) {
|
|
157
|
+
if (!s.completed)
|
|
158
|
+
count++;
|
|
159
|
+
}
|
|
160
|
+
return count;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=stream-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-manager.js","sourceRoot":"","sources":["../../src/ncp/stream-manager.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,oDAAoD;AACpD,EAAE;AACF,gFAAgF;AAChF,mBAAmB;AAEnB,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAiBvD;;;;;;;;GAQG;AACH,MAAM,OAAO,aAAa;IACP,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IAC1C,QAAQ,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC7C,aAAa,CAAS;IAEvC,YAAY,OAAoC;QAC9C,IAAI,CAAC,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,EAAE,CAAC;IACpD,CAAC;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,KAAkB;QACxB,IAAI,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAE/C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,0EAA0E;YAC1E,iEAAiE;YACjE,IAAI,KAAK,CAAC,GAAG,KAAK,CAAC,EAAE,CAAC;gBACpB,MAAM,IAAI,Q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KAAK,EAAE,CAAC;QAC5B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { NdpResolveResult } from "./frames.js";
|
|
2
|
+
export declare const DNS_TXT_DEFAULT_TTL = 300;
|
|
3
|
+
/**
|
|
4
|
+
* Extract the hostname from an NWP target URI.
|
|
5
|
+
* e.g. "nwp://api.example.com/products" → "api.example.com"
|
|
6
|
+
*/
|
|
7
|
+
export declare function extractHostFromTarget(target: string): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Parse one TXT record (array of string chunks) into an NdpResolveResult.
|
|
10
|
+
*
|
|
11
|
+
* Expected format (chunks joined with a single space):
|
|
12
|
+
* v=nps1 type=<type> port=<port> nid=<nid> fp=sha256:<fingerprint>
|
|
13
|
+
*
|
|
14
|
+
* Rules:
|
|
15
|
+
* - `v` MUST be present and equal to "nps1"
|
|
16
|
+
* - `nid` MUST be present
|
|
17
|
+
* - `port` defaults to 17433 when absent
|
|
18
|
+
* - `fp` is mapped to certFingerprint
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseNpsTxtRecord(parts: string[], host: string): NdpResolveResult | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Abstraction over DNS TXT lookups.
|
|
23
|
+
* The default implementation uses Node's `dns.promises`; pass a mock in tests.
|
|
24
|
+
*/
|
|
25
|
+
export interface DnsTxtLookup {
|
|
26
|
+
resolveTxt(hostname: string): Promise<string[][]>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* System DNS TXT resolver backed by Node's built-in `dns.promises`.
|
|
30
|
+
* The dynamic `import()` keeps browser bundles from failing at parse time.
|
|
31
|
+
*/
|
|
32
|
+
export declare class SystemDnsTxtLookup implements DnsTxtLookup {
|
|
33
|
+
resolveTxt(hostname: string): Promise<string[][]>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=dns-txt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dns-txt.d.ts","sourceRoot":"","sources":["../../src/ndp/dns-txt.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAEvC;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAMxE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,MAAM,EAAE,EACf,IAAI,EAAE,MAAM,GACX,gBAAgB,GAAG,SAAS,CAgC9B;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;CACnD;AAED;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IAC/C,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;CAIxD"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Copyright 2026 INNO LOTUS PTY LTD
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export const DNS_TXT_DEFAULT_TTL = 300;
|
|
4
|
+
/**
|
|
5
|
+
* Extract the hostname from an NWP target URI.
|
|
6
|
+
* e.g. "nwp://api.example.com/products" → "api.example.com"
|
|
7
|
+
*/
|
|
8
|
+
export function extractHostFromTarget(target) {
|
|
9
|
+
if (!target.startsWith("nwp://"))
|
|
10
|
+
return undefined;
|
|
11
|
+
const rest = target.slice("nwp://".length);
|
|
12
|
+
const slashIdx = rest.indexOf("/");
|
|
13
|
+
const host = slashIdx === -1 ? rest : rest.slice(0, slashIdx);
|
|
14
|
+
return host.length > 0 ? host : undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Parse one TXT record (array of string chunks) into an NdpResolveResult.
|
|
18
|
+
*
|
|
19
|
+
* Expected format (chunks joined with a single space):
|
|
20
|
+
* v=nps1 type=<type> port=<port> nid=<nid> fp=sha256:<fingerprint>
|
|
21
|
+
*
|
|
22
|
+
* Rules:
|
|
23
|
+
* - `v` MUST be present and equal to "nps1"
|
|
24
|
+
* - `nid` MUST be present
|
|
25
|
+
* - `port` defaults to 17433 when absent
|
|
26
|
+
* - `fp` is mapped to certFingerprint
|
|
27
|
+
*/
|
|
28
|
+
export function parseNpsTxtRecord(parts, host) {
|
|
29
|
+
const joined = parts.join(" ");
|
|
30
|
+
const kv = new Map();
|
|
31
|
+
for (const token of joined.split(/\s+/)) {
|
|
32
|
+
const eq = token.indexOf("=");
|
|
33
|
+
if (eq === -1)
|
|
34
|
+
continue;
|
|
35
|
+
const key = token.slice(0, eq);
|
|
36
|
+
const val = token.slice(eq + 1);
|
|
37
|
+
kv.set(key, val);
|
|
38
|
+
}
|
|
39
|
+
if (kv.get("v") !== "nps1")
|
|
40
|
+
return undefined;
|
|
41
|
+
const nid = kv.get("nid");
|
|
42
|
+
if (!nid)
|
|
43
|
+
return undefined;
|
|
44
|
+
const rawPort = kv.get("port");
|
|
45
|
+
const port = rawPort !== undefined ? Number(rawPort) : 17433;
|
|
46
|
+
const fp = kv.get("fp");
|
|
47
|
+
const result = {
|
|
48
|
+
host,
|
|
49
|
+
port,
|
|
50
|
+
ttl: DNS_TXT_DEFAULT_TTL,
|
|
51
|
+
};
|
|
52
|
+
if (fp !== undefined) {
|
|
53
|
+
result.certFingerprint = fp;
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* System DNS TXT resolver backed by Node's built-in `dns.promises`.
|
|
59
|
+
* The dynamic `import()` keeps browser bundles from failing at parse time.
|
|
60
|
+
*/
|
|
61
|
+
export class SystemDnsTxtLookup {
|
|
62
|
+
async resolveTxt(hostname) {
|
|
63
|
+
const { promises: dns } = await import("node:dns");
|
|
64
|
+
return dns.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} from "../core/frames.js";
|
|
4
|
+
export class AnnounceFrame {
|
|
5
|
+
nid;
|
|
6
|
+
addresses;
|
|
7
|
+
capabilities;
|
|
8
|
+
ttl;
|
|
9
|
+
timestamp;
|
|
10
|
+
signature;
|
|
11
|
+
nodeType;
|
|
12
|
+
frameType = FrameType.ANNOUNCE;
|
|
13
|
+
preferredTier = EncodingTier.MSGPACK;
|
|
14
|
+
constructor(nid, addresses, capabilities, ttl, timestamp, signature, nodeType) {
|
|
15
|
+
this.nid = nid;
|
|
16
|
+
this.addresses = addresses;
|
|
17
|
+
this.capabilities = capabilities;
|
|
18
|
+
this.ttl = ttl;
|
|
19
|
+
this.timestamp = timestamp;
|
|
20
|
+
this.signature = signature;
|
|
21
|
+
this.nodeType = nodeType;
|
|
22
|
+
}
|
|
23
|
+
unsignedDict() {
|
|
24
|
+
return {
|
|
25
|
+
nid: this.nid,
|
|
26
|
+
addresses: this.addresses,
|
|
27
|
+
capabilities: this.capabilities,
|
|
28
|
+
ttl: this.ttl,
|
|
29
|
+
timestamp: this.timestamp,
|
|
30
|
+
node_type: this.nodeType ?? null,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
toDict() {
|
|
34
|
+
return { ...this.unsignedDict(), signature: this.signature };
|
|
35
|
+
}
|
|
36
|
+
static fromDict(data) {
|
|
37
|
+
return new AnnounceFrame(data["nid"], data["addresses"], data["capabilities"], data["ttl"], data["timestamp"], data["signature"], data["node_type"] ?? undefined);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export class ResolveFrame {
|
|
41
|
+
target;
|
|
42
|
+
requesterNid;
|
|
43
|
+
resolved;
|
|
44
|
+
frameType = FrameType.RESOLVE;
|
|
45
|
+
preferredTier = EncodingTier.MSGPACK;
|
|
46
|
+
constructor(target, requesterNid, resolved) {
|
|
47
|
+
this.target = target;
|
|
48
|
+
this.requesterNid = requesterNid;
|
|
49
|
+
this.resolved = resolved;
|
|
50
|
+
}
|
|
51
|
+
toDict() {
|
|
52
|
+
return {
|
|
53
|
+
target: this.target,
|
|
54
|
+
requester_nid: this.requesterNid ?? null,
|
|
55
|
+
resolved: this.resolved ?? null,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static fromDict(data) {
|
|
59
|
+
return new ResolveFrame(data["target"], data["requester_nid"] ?? undefined, data["resolved"] ?? undefined);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export class GraphFrame {
|
|
63
|
+
seq;
|
|
64
|
+
initialSync;
|
|
65
|
+
nodes;
|
|
66
|
+
patch;
|
|
67
|
+
frameType = FrameType.GRAPH;
|
|
68
|
+
preferredTier = EncodingTier.MSGPACK;
|
|
69
|
+
constructor(seq, initialSync, nodes, patch) {
|
|
70
|
+
this.seq = seq;
|
|
71
|
+
this.initialSync = initialSync;
|
|
72
|
+
this.nodes = nodes;
|
|
73
|
+
this.patch = patch;
|
|
74
|
+
}
|
|
75
|
+
toDict() {
|
|
76
|
+
return {
|
|
77
|
+
seq: this.seq,
|
|
78
|
+
initial_sync: this.initialSync,
|
|
79
|
+
nodes: this.nodes ?? null,
|
|
80
|
+
patch: this.patch ?? null,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
static fromDict(data) {
|
|
84
|
+
return new GraphFrame(data["seq"], data["initial_sync"], data["nodes"] ?? undefined, data["patch"] ?? undefined);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=frames.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frames.js","sourceRoot":"","sources":["../../src/ndp/frames.ts"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,sCAAsC;AAEtC,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAuB5D,MAAM,OAAO,aAAa;IAKN;IACA;IACA;IACA;IACA;IACA;IACA;IAVT,SAAS,GAAO,SAAS,CAAC,QAAQ,CAAC;IACnC,aAAa,GAAG,YAAY,CAAC,OAAO,CA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,CAAC,OAAO,CAAsC,IAAI,SAAS,CACjE,CAAC;IACJ,CAAC;CACF"}
|
package/dist/ndp/index.d.ts
CHANGED
package/dist/ndp/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ndp/index.ts"],"names":[],"mappings":"AAGA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ndp/index.ts"],"names":[],"mappings":"AAGA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
|