@7h3/protocol 0.5.6 → 0.6.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/bin/7h3.js +74 -1
- package/cborCodec.d.ts +13 -0
- package/index.js +395 -376
- package/package.json +1 -1
- package/protocol.d.ts +10 -0
package/index.js
CHANGED
|
@@ -147,7 +147,7 @@ async function me(e, t) {
|
|
|
147
147
|
async function he(e, t, n) {
|
|
148
148
|
return !t || t.alg !== n.alg ? !1 : n.alg === "HS256" ? fe(e, t.value, n.secret) : x(e, t.value, n.publicKey);
|
|
149
149
|
}
|
|
150
|
-
var ge = 864e5;
|
|
150
|
+
var ge = 864e5, _e = 3e4;
|
|
151
151
|
function E(e, t = Date.now()) {
|
|
152
152
|
let n = [], r = e.header ?? {}, i = e.body ?? {}, a = typeof r.version == "string" ? r.version : "", o = typeof r.messageId == "string" ? r.messageId : "", s = typeof r.sender == "string" ? r.sender : "", c = typeof r.nonce == "string" ? r.nonce : "", l = typeof r.timestampMs == "number" && Number.isFinite(r.timestampMs) ? r.timestampMs : NaN, u = typeof r.ttlMs == "number" && Number.isFinite(r.ttlMs) ? r.ttlMs : NaN, d = typeof i.content == "string" ? i.content : "";
|
|
153
153
|
return a !== "7h3/0.1" && n.push({
|
|
@@ -174,6 +174,9 @@ function E(e, t = Date.now()) {
|
|
|
174
174
|
})) : n.push({
|
|
175
175
|
level: "error",
|
|
176
176
|
message: "ttlMs must be a finite number"
|
|
177
|
+
}), Number.isFinite(l) && l > t + 3e4 && n.push({
|
|
178
|
+
level: "error",
|
|
179
|
+
message: `timestampMs is more than ${_e} ms in the future`
|
|
177
180
|
}), Number.isFinite(l) && Number.isFinite(u) && l + u < t && n.push({
|
|
178
181
|
level: "error",
|
|
179
182
|
message: "Message TTL expired"
|
|
@@ -204,12 +207,12 @@ function D(e) {
|
|
|
204
207
|
}
|
|
205
208
|
//#endregion
|
|
206
209
|
//#region src/protocolBinary.ts
|
|
207
|
-
var
|
|
210
|
+
var ve = [
|
|
208
211
|
65,
|
|
209
212
|
73,
|
|
210
213
|
80,
|
|
211
214
|
66
|
|
212
|
-
],
|
|
215
|
+
], ye = Uint8Array.from(ve), be = 1, xe = 1024 * 1024, Se = 1, Ce = 2, we = 4, Te = 8, Ee = new TextEncoder(), De = new TextDecoder(), Oe = /* @__PURE__ */ new Set([
|
|
213
216
|
"PING",
|
|
214
217
|
"PONG",
|
|
215
218
|
"CAPS",
|
|
@@ -217,25 +220,25 @@ var _e = [
|
|
|
217
220
|
"RESULT",
|
|
218
221
|
"ERROR"
|
|
219
222
|
]);
|
|
220
|
-
function Oe(e) {
|
|
221
|
-
return Te.encode(e);
|
|
222
|
-
}
|
|
223
223
|
function ke(e) {
|
|
224
|
-
return Ee.
|
|
224
|
+
return Ee.encode(e);
|
|
225
225
|
}
|
|
226
226
|
function Ae(e) {
|
|
227
|
-
return e
|
|
227
|
+
return De.decode(e);
|
|
228
228
|
}
|
|
229
229
|
function je(e) {
|
|
230
|
+
return e === "ED25519" ? 2 : 1;
|
|
231
|
+
}
|
|
232
|
+
function Me(e) {
|
|
230
233
|
return e === 1 ? "HS256" : e === 2 ? "ED25519" : null;
|
|
231
234
|
}
|
|
232
235
|
function O(e) {
|
|
233
|
-
return { bytes:
|
|
236
|
+
return { bytes: ke(e) };
|
|
234
237
|
}
|
|
235
238
|
function k(e) {
|
|
236
239
|
return e ? 4 + e.bytes.byteLength : 0;
|
|
237
240
|
}
|
|
238
|
-
var
|
|
241
|
+
var Ne = class {
|
|
239
242
|
bytes;
|
|
240
243
|
view;
|
|
241
244
|
offset = 0;
|
|
@@ -260,7 +263,7 @@ var Me = class {
|
|
|
260
263
|
finish() {
|
|
261
264
|
return this.bytes;
|
|
262
265
|
}
|
|
263
|
-
},
|
|
266
|
+
}, Pe = class {
|
|
264
267
|
bytes;
|
|
265
268
|
offset = 0;
|
|
266
269
|
constructor(e) {
|
|
@@ -291,14 +294,14 @@ var Me = class {
|
|
|
291
294
|
}
|
|
292
295
|
readString() {
|
|
293
296
|
let e = this.readU32();
|
|
294
|
-
return
|
|
297
|
+
return Ae(this.readBytes(e));
|
|
295
298
|
}
|
|
296
299
|
};
|
|
297
|
-
function
|
|
298
|
-
let t = (e.header.recipient ?
|
|
299
|
-
return p.writeBytes(
|
|
300
|
+
function Fe(e) {
|
|
301
|
+
let t = (e.header.recipient ? Se : 0) | (e.body.capability ? Ce : 0) | (e.body.correlationId ? we : 0) | (e.signature ? Te : 0), n = O(e.header.version), r = O(e.header.messageId), i = O(e.header.sender), a = O(e.header.nonce), o = O(e.body.intent), s = O(e.body.content), c = e.header.recipient ? O(e.header.recipient) : null, l = e.body.capability ? O(e.body.capability) : null, u = e.body.correlationId ? O(e.body.correlationId) : null, d = e.signature ? O(e.signature.keyId) : null, f = e.signature ? O(e.signature.value) : null, p = new Ne(ye.byteLength + 1 + 1 + 8 + 4 + k(n) + k(r) + k(i) + k(a) + k(o) + k(s) + k(c) + k(l) + k(u) + +!!e.signature + k(d) + k(f));
|
|
302
|
+
return p.writeBytes(ye), p.writeU8(be), p.writeU8(t), p.writeU64(e.header.timestampMs), p.writeU32(e.header.ttlMs), p.writeString(n), p.writeString(r), p.writeString(i), p.writeString(a), p.writeString(o), p.writeString(s), c && p.writeString(c), l && p.writeString(l), u && p.writeString(u), e.signature && d && f && (p.writeU8(je(e.signature.alg)), p.writeString(d), p.writeString(f)), p.finish();
|
|
300
303
|
}
|
|
301
|
-
function
|
|
304
|
+
function Ie(e, t = {}) {
|
|
302
305
|
try {
|
|
303
306
|
let n = t.maxFrameBytes ?? 1048576;
|
|
304
307
|
if (e.byteLength > n) return {
|
|
@@ -309,13 +312,13 @@ function Fe(e, t = {}) {
|
|
|
309
312
|
}],
|
|
310
313
|
envelope: null
|
|
311
314
|
};
|
|
312
|
-
let r = new
|
|
315
|
+
let r = new Pe(e);
|
|
313
316
|
if ([
|
|
314
317
|
r.readU8(),
|
|
315
318
|
r.readU8(),
|
|
316
319
|
r.readU8(),
|
|
317
320
|
r.readU8()
|
|
318
|
-
].some((e, t) => e !==
|
|
321
|
+
].some((e, t) => e !== ve[t])) return {
|
|
319
322
|
ok: !1,
|
|
320
323
|
diagnostics: [{
|
|
321
324
|
level: "error",
|
|
@@ -324,7 +327,7 @@ function Fe(e, t = {}) {
|
|
|
324
327
|
envelope: null
|
|
325
328
|
};
|
|
326
329
|
let i = r.readU8();
|
|
327
|
-
if (i !==
|
|
330
|
+
if (i !== be) return {
|
|
328
331
|
ok: !1,
|
|
329
332
|
diagnostics: [{
|
|
330
333
|
level: "error",
|
|
@@ -354,7 +357,7 @@ function Fe(e, t = {}) {
|
|
|
354
357
|
}],
|
|
355
358
|
envelope: null
|
|
356
359
|
};
|
|
357
|
-
if (!
|
|
360
|
+
if (!Oe.has(c.body.intent)) return {
|
|
358
361
|
ok: !1,
|
|
359
362
|
diagnostics: [{
|
|
360
363
|
level: "error",
|
|
@@ -362,8 +365,8 @@ function Fe(e, t = {}) {
|
|
|
362
365
|
}],
|
|
363
366
|
envelope: null
|
|
364
367
|
};
|
|
365
|
-
if (a &
|
|
366
|
-
let e = r.readU8(), t =
|
|
368
|
+
if (a & Se && (c.header.recipient = r.readString()), a & Ce && (c.body.capability = r.readString()), a & we && (c.body.correlationId = r.readString()), a & Te) {
|
|
369
|
+
let e = r.readU8(), t = Me(e);
|
|
367
370
|
if (!t) return {
|
|
368
371
|
ok: !1,
|
|
369
372
|
diagnostics: [{
|
|
@@ -403,7 +406,7 @@ function Fe(e, t = {}) {
|
|
|
403
406
|
}
|
|
404
407
|
//#endregion
|
|
405
408
|
//#region src/protocolReplay.ts
|
|
406
|
-
var
|
|
409
|
+
var Le = class {
|
|
407
410
|
items = [];
|
|
408
411
|
peek() {
|
|
409
412
|
return this.items[0];
|
|
@@ -439,7 +442,7 @@ var Ie = class {
|
|
|
439
442
|
}, A = class {
|
|
440
443
|
entries = /* @__PURE__ */ new Map();
|
|
441
444
|
maxEntries;
|
|
442
|
-
expiries = new
|
|
445
|
+
expiries = new Le();
|
|
443
446
|
constructor(e = 1e4) {
|
|
444
447
|
this.maxEntries = e;
|
|
445
448
|
}
|
|
@@ -489,7 +492,7 @@ var Ie = class {
|
|
|
489
492
|
consumeMany(e, t = Date.now()) {
|
|
490
493
|
return e.map((e) => this.consume(e, t));
|
|
491
494
|
}
|
|
492
|
-
},
|
|
495
|
+
}, Re = class {
|
|
493
496
|
store;
|
|
494
497
|
constructor(e) {
|
|
495
498
|
this.store = e;
|
|
@@ -520,7 +523,7 @@ var Ie = class {
|
|
|
520
523
|
}
|
|
521
524
|
return Promise.all(e.map((e) => this.consume(e, t)));
|
|
522
525
|
}
|
|
523
|
-
},
|
|
526
|
+
}, ze = class {
|
|
524
527
|
entries = /* @__PURE__ */ new Map();
|
|
525
528
|
maxEntries;
|
|
526
529
|
constructor(e = 512) {
|
|
@@ -550,20 +553,20 @@ var Ie = class {
|
|
|
550
553
|
this.entries.delete(e);
|
|
551
554
|
}
|
|
552
555
|
};
|
|
553
|
-
function
|
|
556
|
+
function Be(e, t) {
|
|
554
557
|
return `${t}|${e.alg}|${e.keyId}`;
|
|
555
558
|
}
|
|
556
|
-
function
|
|
559
|
+
function Ve(e) {
|
|
557
560
|
if (!e || typeof e != "object") return !1;
|
|
558
561
|
let t = e;
|
|
559
562
|
return !!(t.header && t.body);
|
|
560
563
|
}
|
|
561
|
-
function
|
|
564
|
+
function He(e) {
|
|
562
565
|
if (!e || typeof e != "object") return !1;
|
|
563
566
|
let t = e;
|
|
564
567
|
return t.v === "7h3/0.1" && typeof t.mid == "string" && typeof t.i == "string";
|
|
565
568
|
}
|
|
566
|
-
function
|
|
569
|
+
function Ue(e) {
|
|
567
570
|
return {
|
|
568
571
|
header: {
|
|
569
572
|
version: e.v,
|
|
@@ -587,7 +590,7 @@ function He(e) {
|
|
|
587
590
|
} : void 0
|
|
588
591
|
};
|
|
589
592
|
}
|
|
590
|
-
function
|
|
593
|
+
function We(e) {
|
|
591
594
|
return {
|
|
592
595
|
v: e.header.version,
|
|
593
596
|
mid: e.header.messageId,
|
|
@@ -607,15 +610,15 @@ function Ue(e) {
|
|
|
607
610
|
} : void 0
|
|
608
611
|
};
|
|
609
612
|
}
|
|
610
|
-
function
|
|
611
|
-
if (e instanceof Uint8Array) return
|
|
613
|
+
function Ge(e) {
|
|
614
|
+
if (e instanceof Uint8Array) return Ie(e);
|
|
612
615
|
try {
|
|
613
616
|
let t = JSON.parse(e);
|
|
614
|
-
return
|
|
617
|
+
return He(t) ? {
|
|
615
618
|
ok: !0,
|
|
616
619
|
diagnostics: [],
|
|
617
|
-
envelope:
|
|
618
|
-
} :
|
|
620
|
+
envelope: Ue(t)
|
|
621
|
+
} : Ve(t) ? {
|
|
619
622
|
ok: !0,
|
|
620
623
|
diagnostics: [],
|
|
621
624
|
envelope: t
|
|
@@ -639,19 +642,19 @@ function We(e) {
|
|
|
639
642
|
}
|
|
640
643
|
}
|
|
641
644
|
function j(e, t = "json") {
|
|
642
|
-
return t === "binary" ?
|
|
645
|
+
return t === "binary" ? Fe(e) : JSON.stringify(t === "compact" ? We(e) : e);
|
|
643
646
|
}
|
|
644
|
-
function
|
|
645
|
-
return JSON.stringify(t === "compact" ? e.map(
|
|
647
|
+
function Ke(e, t = "compact") {
|
|
648
|
+
return JSON.stringify(t === "compact" ? e.map(We) : e);
|
|
646
649
|
}
|
|
647
|
-
function
|
|
650
|
+
function qe(e) {
|
|
648
651
|
try {
|
|
649
652
|
let t = JSON.parse(e);
|
|
650
|
-
return Array.isArray(t) ? t.map((e) =>
|
|
653
|
+
return Array.isArray(t) ? t.map((e) => He(e) ? {
|
|
651
654
|
ok: !0,
|
|
652
655
|
diagnostics: [],
|
|
653
|
-
envelope:
|
|
654
|
-
} :
|
|
656
|
+
envelope: Ue(e)
|
|
657
|
+
} : Ve(e) ? {
|
|
655
658
|
ok: !0,
|
|
656
659
|
diagnostics: [],
|
|
657
660
|
envelope: e
|
|
@@ -689,7 +692,7 @@ async function M(e, t = {}) {
|
|
|
689
692
|
...e
|
|
690
693
|
});
|
|
691
694
|
}
|
|
692
|
-
let o = typeof e == "string" || e instanceof Uint8Array ?
|
|
695
|
+
let o = typeof e == "string" || e instanceof Uint8Array ? Ge(e) : {
|
|
693
696
|
ok: !0,
|
|
694
697
|
diagnostics: [],
|
|
695
698
|
envelope: e
|
|
@@ -747,7 +750,7 @@ async function M(e, t = {}) {
|
|
|
747
750
|
diagnostics: s,
|
|
748
751
|
envelope: o.envelope
|
|
749
752
|
});
|
|
750
|
-
let c, l = o.envelope.signature, u =
|
|
753
|
+
let c, l = o.envelope.signature, u = Be(l, o.envelope.header.sender);
|
|
751
754
|
if (t.verificationMaterialCache && (c = t.verificationMaterialCache.get(u, n)), t.signatureResolver && (c ??= await t.signatureResolver(l, o.envelope.header.sender)), !c && l.alg === "HS256" && t.secretResolver) {
|
|
752
755
|
let e = await t.secretResolver(l.keyId, o.envelope.header.sender);
|
|
753
756
|
e && (c = {
|
|
@@ -814,7 +817,7 @@ async function M(e, t = {}) {
|
|
|
814
817
|
envelope: o.envelope
|
|
815
818
|
};
|
|
816
819
|
}
|
|
817
|
-
async function
|
|
820
|
+
async function Je(e, t = {}) {
|
|
818
821
|
let n = Math.max(1, Math.floor(t.batchConcurrency ?? Infinity));
|
|
819
822
|
return (async (e) => {
|
|
820
823
|
let r = Array(e.length), i = 0, a = Number.isFinite(n) ? Math.min(n, e.length) : e.length;
|
|
@@ -830,15 +833,15 @@ async function qe(e, t = {}) {
|
|
|
830
833
|
nowMs: t.nowMs ?? Date.now(),
|
|
831
834
|
reason: `items=${e.length};accepted=${r.filter((e) => e?.ok).length}`
|
|
832
835
|
}), r;
|
|
833
|
-
})(typeof e == "string" ?
|
|
836
|
+
})(typeof e == "string" ? qe(e).map((e) => e.ok && e.envelope ? e.envelope : e) : e);
|
|
834
837
|
}
|
|
835
|
-
var
|
|
838
|
+
var Ye = class {
|
|
836
839
|
options;
|
|
837
840
|
constructor(e = {}) {
|
|
838
841
|
this.options = {
|
|
839
842
|
...e,
|
|
840
843
|
replayCache: e.replayCache ?? new A(),
|
|
841
|
-
verificationMaterialCache: e.verificationMaterialCache ?? new
|
|
844
|
+
verificationMaterialCache: e.verificationMaterialCache ?? new ze()
|
|
842
845
|
};
|
|
843
846
|
}
|
|
844
847
|
receive(e, t = Date.now()) {
|
|
@@ -848,23 +851,23 @@ var Je = class {
|
|
|
848
851
|
});
|
|
849
852
|
}
|
|
850
853
|
receiveBatch(e, t = Date.now()) {
|
|
851
|
-
return
|
|
854
|
+
return Je(e, {
|
|
852
855
|
...this.options,
|
|
853
856
|
nowMs: t
|
|
854
857
|
});
|
|
855
858
|
}
|
|
856
859
|
};
|
|
857
|
-
async function
|
|
860
|
+
async function Xe(e) {
|
|
858
861
|
return S(D(e), e.secret, e.keyId);
|
|
859
862
|
}
|
|
860
863
|
//#endregion
|
|
861
864
|
//#region src/protocolCapabilities.ts
|
|
862
|
-
var
|
|
865
|
+
var Ze = [
|
|
863
866
|
"binary",
|
|
864
867
|
"compact",
|
|
865
868
|
"json"
|
|
866
|
-
],
|
|
867
|
-
function
|
|
869
|
+
], Qe = ["fast", "receipt"];
|
|
870
|
+
function $e(e) {
|
|
868
871
|
return {
|
|
869
872
|
agent: e.agent,
|
|
870
873
|
capabilities: e.capabilities ?? [],
|
|
@@ -873,10 +876,10 @@ function Qe(e) {
|
|
|
873
876
|
ackModes: e.ackModes ?? ["receipt"]
|
|
874
877
|
};
|
|
875
878
|
}
|
|
876
|
-
function
|
|
879
|
+
function et(e) {
|
|
877
880
|
return JSON.stringify(e);
|
|
878
881
|
}
|
|
879
|
-
function
|
|
882
|
+
function tt(e) {
|
|
880
883
|
try {
|
|
881
884
|
let t = JSON.parse(e);
|
|
882
885
|
return typeof t.agent != "string" || !Array.isArray(t.capabilities) || !Array.isArray(t.wireFormats) || !Array.isArray(t.ackModes) || typeof t.batchMax != "number" || t.batchMax < 1 ? null : {
|
|
@@ -890,8 +893,8 @@ function et(e) {
|
|
|
890
893
|
return null;
|
|
891
894
|
}
|
|
892
895
|
}
|
|
893
|
-
function
|
|
894
|
-
let n =
|
|
896
|
+
function nt(e, t) {
|
|
897
|
+
let n = Ze.find((n) => e.wireFormats.includes(n) && t.wireFormats.includes(n)) ?? "compact", r = Qe.find((n) => e.ackModes.includes(n) && t.ackModes.includes(n)) ?? "receipt";
|
|
895
898
|
return {
|
|
896
899
|
wireFormat: n,
|
|
897
900
|
batchMax: Math.max(1, Math.min(e.batchMax, t.batchMax)),
|
|
@@ -900,7 +903,7 @@ function tt(e, t) {
|
|
|
900
903
|
}
|
|
901
904
|
//#endregion
|
|
902
905
|
//#region src/keyRotation.ts
|
|
903
|
-
var
|
|
906
|
+
var rt = class {
|
|
904
907
|
records;
|
|
905
908
|
constructor(e = []) {
|
|
906
909
|
this.records = [...e];
|
|
@@ -919,12 +922,12 @@ var nt = class {
|
|
|
919
922
|
return this.records.filter((r) => r.sender === t && r.keyId === e.keyId && r.alg === e.alg && r.status !== "revoked" && r.notBeforeMs <= n && n < r.notAfterMs).sort((e, t) => t.notBeforeMs - e.notBeforeMs)[0]?.material;
|
|
920
923
|
}
|
|
921
924
|
};
|
|
922
|
-
function
|
|
925
|
+
function it(e, t = () => Date.now()) {
|
|
923
926
|
return async (n, r) => e.resolveVerificationMaterial(n, r, t());
|
|
924
927
|
}
|
|
925
928
|
//#endregion
|
|
926
929
|
//#region src/protocolAgent.ts
|
|
927
|
-
var
|
|
930
|
+
var at = class {
|
|
928
931
|
agentId;
|
|
929
932
|
outboundSecret;
|
|
930
933
|
keyId;
|
|
@@ -989,7 +992,7 @@ var it = class {
|
|
|
989
992
|
if (e.body.intent === "CAPS") return this.createSignedIntent({
|
|
990
993
|
recipient: e.header.sender,
|
|
991
994
|
intent: "RESULT",
|
|
992
|
-
content: $e(
|
|
995
|
+
content: et($e({
|
|
993
996
|
agent: this.agentId,
|
|
994
997
|
capabilities: this.capabilities,
|
|
995
998
|
wireFormats: this.supportedWireFormats,
|
|
@@ -1017,11 +1020,11 @@ var it = class {
|
|
|
1017
1020
|
}
|
|
1018
1021
|
return null;
|
|
1019
1022
|
}
|
|
1020
|
-
},
|
|
1023
|
+
}, ot = class {
|
|
1021
1024
|
session;
|
|
1022
1025
|
wireFormat;
|
|
1023
1026
|
constructor(e) {
|
|
1024
|
-
this.session = new
|
|
1027
|
+
this.session = new at(e), this.wireFormat = e.wireFormat ?? "compact";
|
|
1025
1028
|
}
|
|
1026
1029
|
toRaw(e) {
|
|
1027
1030
|
return j(e, this.wireFormat);
|
|
@@ -1038,12 +1041,12 @@ var it = class {
|
|
|
1038
1041
|
return r.response && await t(this.toRaw(r.response), r.response), r;
|
|
1039
1042
|
}
|
|
1040
1043
|
};
|
|
1041
|
-
function
|
|
1042
|
-
return new
|
|
1044
|
+
function st(e) {
|
|
1045
|
+
return new ot(e);
|
|
1043
1046
|
}
|
|
1044
1047
|
//#endregion
|
|
1045
1048
|
//#region src/frameworkAdapters.ts
|
|
1046
|
-
async function
|
|
1049
|
+
async function ct(e, t, n = {}) {
|
|
1047
1050
|
return e.createRawIntent({
|
|
1048
1051
|
recipient: n.recipient,
|
|
1049
1052
|
intent: "TASK",
|
|
@@ -1052,7 +1055,7 @@ async function st(e, t, n = {}) {
|
|
|
1052
1055
|
correlationId: n.correlationId
|
|
1053
1056
|
});
|
|
1054
1057
|
}
|
|
1055
|
-
function
|
|
1058
|
+
function lt(e) {
|
|
1056
1059
|
return {
|
|
1057
1060
|
content: e.body.content,
|
|
1058
1061
|
name: e.body.intent,
|
|
@@ -1066,7 +1069,7 @@ function ct(e) {
|
|
|
1066
1069
|
}
|
|
1067
1070
|
};
|
|
1068
1071
|
}
|
|
1069
|
-
async function
|
|
1072
|
+
async function ut(e, t, n = {}) {
|
|
1070
1073
|
return e.createRawIntent({
|
|
1071
1074
|
recipient: n.recipient,
|
|
1072
1075
|
intent: "TASK",
|
|
@@ -1075,7 +1078,7 @@ async function lt(e, t, n = {}) {
|
|
|
1075
1078
|
correlationId: n.correlationId
|
|
1076
1079
|
});
|
|
1077
1080
|
}
|
|
1078
|
-
function
|
|
1081
|
+
function dt(e) {
|
|
1079
1082
|
return {
|
|
1080
1083
|
role: e.body.intent === "TASK" ? "user" : "assistant",
|
|
1081
1084
|
content: e.body.content,
|
|
@@ -1088,15 +1091,15 @@ function ut(e) {
|
|
|
1088
1091
|
}
|
|
1089
1092
|
};
|
|
1090
1093
|
}
|
|
1091
|
-
function
|
|
1094
|
+
function ft(e) {
|
|
1092
1095
|
return `mcp.${e}`;
|
|
1093
1096
|
}
|
|
1094
|
-
async function
|
|
1097
|
+
async function pt(e, t, n = {}) {
|
|
1095
1098
|
async function r(e) {
|
|
1096
1099
|
n.onPolicyEvent && await n.onPolicyEvent(e);
|
|
1097
1100
|
}
|
|
1098
1101
|
if (n.allowedMethods && !n.allowedMethods.includes(t.method)) {
|
|
1099
|
-
let e = n.methodToCapability ? n.methodToCapability(t.method) :
|
|
1102
|
+
let e = n.methodToCapability ? n.methodToCapability(t.method) : ft(t.method), i = {
|
|
1100
1103
|
intent: "TASK",
|
|
1101
1104
|
recipient: n.recipient,
|
|
1102
1105
|
capability: e,
|
|
@@ -1111,7 +1114,7 @@ async function ft(e, t, n = {}) {
|
|
|
1111
1114
|
reason: "method_not_allowed"
|
|
1112
1115
|
}), Error(`JSON-RPC method '${t.method}' is not allowed`);
|
|
1113
1116
|
}
|
|
1114
|
-
let i = n.methodToCapability ? n.methodToCapability(t.method) :
|
|
1117
|
+
let i = n.methodToCapability ? n.methodToCapability(t.method) : ft(t.method), a = {
|
|
1115
1118
|
intent: "TASK",
|
|
1116
1119
|
recipient: n.recipient,
|
|
1117
1120
|
capability: i,
|
|
@@ -1165,7 +1168,7 @@ async function ft(e, t, n = {}) {
|
|
|
1165
1168
|
})
|
|
1166
1169
|
});
|
|
1167
1170
|
}
|
|
1168
|
-
function
|
|
1171
|
+
function mt(e, t, n = {}) {
|
|
1169
1172
|
if (e.body.intent === "ERROR") return {
|
|
1170
1173
|
jsonrpc: "2.0",
|
|
1171
1174
|
id: t,
|
|
@@ -1192,16 +1195,16 @@ function pt(e, t, n = {}) {
|
|
|
1192
1195
|
}
|
|
1193
1196
|
//#endregion
|
|
1194
1197
|
//#region src/runtimePolicy.ts
|
|
1195
|
-
function
|
|
1198
|
+
function ht(e) {
|
|
1196
1199
|
return typeof e == "object" && !!e && !Array.isArray(e);
|
|
1197
1200
|
}
|
|
1198
|
-
function
|
|
1201
|
+
function gt(e, t) {
|
|
1199
1202
|
if (typeof e != "string" || e.trim().length === 0) throw Error(`Invalid runtime policy: '${t}' must be a non-empty string`);
|
|
1200
1203
|
return e;
|
|
1201
1204
|
}
|
|
1202
|
-
function
|
|
1203
|
-
if (!
|
|
1204
|
-
let t =
|
|
1205
|
+
function _t(e) {
|
|
1206
|
+
if (!ht(e)) throw Error("Invalid runtime policy: root must be an object");
|
|
1207
|
+
let t = gt(e.version, "version"), n = gt(e.name, "name"), r = gt(e.status, "status");
|
|
1205
1208
|
return {
|
|
1206
1209
|
...e,
|
|
1207
1210
|
version: t,
|
|
@@ -1209,7 +1212,7 @@ function gt(e) {
|
|
|
1209
1212
|
status: r
|
|
1210
1213
|
};
|
|
1211
1214
|
}
|
|
1212
|
-
function
|
|
1215
|
+
function vt(e) {
|
|
1213
1216
|
let t;
|
|
1214
1217
|
try {
|
|
1215
1218
|
t = JSON.parse(e);
|
|
@@ -1217,9 +1220,9 @@ function _t(e) {
|
|
|
1217
1220
|
let t = e instanceof Error ? e.message : String(e);
|
|
1218
1221
|
throw Error(`Invalid runtime policy JSON: ${t}`, { cause: e });
|
|
1219
1222
|
}
|
|
1220
|
-
return
|
|
1223
|
+
return _t(t);
|
|
1221
1224
|
}
|
|
1222
|
-
async function
|
|
1225
|
+
async function yt(e) {
|
|
1223
1226
|
if (typeof fetch == "function") {
|
|
1224
1227
|
let t = await fetch(e);
|
|
1225
1228
|
if (!t.ok) throw Error(`Failed to fetch runtime policy at '${e}': ${t.status} ${t.statusText}`);
|
|
@@ -1227,26 +1230,26 @@ async function vt(e) {
|
|
|
1227
1230
|
}
|
|
1228
1231
|
throw Error("No default runtime file reader available in this environment. Provide 'readTextFile' in loadRuntimePolicy(options).");
|
|
1229
1232
|
}
|
|
1230
|
-
async function
|
|
1233
|
+
async function bt(e = {}) {
|
|
1231
1234
|
let t = e.path ?? "AI_RUNTIME_POLICY.json";
|
|
1232
|
-
return
|
|
1235
|
+
return vt(await (e.readTextFile ?? yt)(t));
|
|
1233
1236
|
}
|
|
1234
1237
|
//#endregion
|
|
1235
1238
|
//#region src/policyEnforcer.ts
|
|
1236
|
-
function
|
|
1239
|
+
function xt(e, t) {
|
|
1237
1240
|
if (!Array.isArray(e) || e.length !== 2) throw Error(`Invalid policy tuning range '${t}': expected [min,max]`);
|
|
1238
1241
|
let n = Number(e[0]), r = Number(e[1]);
|
|
1239
1242
|
if (!Number.isFinite(n) || !Number.isFinite(r) || n <= 0 || r < n) throw Error(`Invalid policy tuning range '${t}': expected positive ascending numbers`);
|
|
1240
1243
|
return [Math.floor(n), Math.floor(r)];
|
|
1241
1244
|
}
|
|
1242
|
-
function
|
|
1245
|
+
function St(e) {
|
|
1243
1246
|
return e === "http" || e === "ws" || e === "http-binary" || e === "http-batch" || e === "http-binary-batch" || e === "ws-batch" || e === "ws-binary" || e === "ws-binary-batch";
|
|
1244
1247
|
}
|
|
1245
|
-
function
|
|
1248
|
+
function Ct(e) {
|
|
1246
1249
|
let t = Number((e.slo_defaults ?? {}).drop_pct_max ?? .1), n = Number((e.slo_defaults ?? {}).p99_ms_max ?? 20), r = Number((e.tuning ?? {}).max_retry_attempts ?? 3), i = Array.isArray((e.tuning ?? {}).retriable_status_codes) ? (e.tuning ?? {}).retriable_status_codes.map((e) => Number(e)).filter((e) => Number.isFinite(e)) : [503], a = e.tuning ?? {}, o = a.batch_size, s = a.inflight_cap, c = a.retry_backoff_ms, l = (e) => ({
|
|
1247
|
-
batchSizeRange:
|
|
1248
|
-
inflightCapRange:
|
|
1249
|
-
retryBackoffMsRange:
|
|
1250
|
+
batchSizeRange: xt(o?.[`${e}_traffic`], `batch_size.${e}_traffic`),
|
|
1251
|
+
inflightCapRange: xt(s?.[`${e}_traffic`], `inflight_cap.${e}_traffic`),
|
|
1252
|
+
retryBackoffMsRange: xt(c?.[`${e}_traffic`], `retry_backoff_ms.${e}_traffic`)
|
|
1250
1253
|
});
|
|
1251
1254
|
function u(e) {
|
|
1252
1255
|
return e.concurrency >= 100 ? "http-binary-batch" : e.latencySensitive ? "ws-batch" : e.compatibilityFirst ? "http" : "ws-batch";
|
|
@@ -1284,27 +1287,27 @@ function St(e) {
|
|
|
1284
1287
|
};
|
|
1285
1288
|
}
|
|
1286
1289
|
async function N(e = {}) {
|
|
1287
|
-
let t = await
|
|
1290
|
+
let t = await bt(e);
|
|
1288
1291
|
return {
|
|
1289
1292
|
policy: t,
|
|
1290
|
-
enforcer:
|
|
1293
|
+
enforcer: Ct(t)
|
|
1291
1294
|
};
|
|
1292
1295
|
}
|
|
1293
|
-
function
|
|
1294
|
-
return
|
|
1296
|
+
function wt(e) {
|
|
1297
|
+
return St(e);
|
|
1295
1298
|
}
|
|
1296
1299
|
//#endregion
|
|
1297
1300
|
//#region src/mcpGateway.ts
|
|
1298
|
-
var
|
|
1301
|
+
var Tt = [
|
|
1299
1302
|
"tools/call",
|
|
1300
1303
|
"resources/read",
|
|
1301
1304
|
"prompts/get"
|
|
1302
1305
|
];
|
|
1303
|
-
function
|
|
1306
|
+
function Et(e) {
|
|
1304
1307
|
return JSON.stringify(e);
|
|
1305
1308
|
}
|
|
1306
|
-
function
|
|
1307
|
-
return
|
|
1309
|
+
function Dt(e, t, n) {
|
|
1310
|
+
return Et({
|
|
1308
1311
|
jsonrpc: "2.0",
|
|
1309
1312
|
id: e,
|
|
1310
1313
|
error: {
|
|
@@ -1313,7 +1316,7 @@ function Et(e, t, n) {
|
|
|
1313
1316
|
}
|
|
1314
1317
|
});
|
|
1315
1318
|
}
|
|
1316
|
-
function
|
|
1319
|
+
function Ot(e) {
|
|
1317
1320
|
let t = JSON.parse(e);
|
|
1318
1321
|
return t.jsonrpc !== "2.0" || typeof t.method != "string" || t.method.length === 0 || typeof t.id != "string" && typeof t.id != "number" ? null : {
|
|
1319
1322
|
jsonrpc: "2.0",
|
|
@@ -1322,30 +1325,30 @@ function Dt(e) {
|
|
|
1322
1325
|
params: t.params
|
|
1323
1326
|
};
|
|
1324
1327
|
}
|
|
1325
|
-
function
|
|
1328
|
+
function kt(e) {
|
|
1326
1329
|
return e.includes("not allowed") ? -32601 : e.includes("not authorized") ? -32001 : e.includes("rate-limited") ? -32002 : -32603;
|
|
1327
1330
|
}
|
|
1328
|
-
function
|
|
1329
|
-
let t = e.gatewayAgentId ?? "agent.gateway", n = e.workerAgentId ?? "agent.worker", r = e.allowedMethods ??
|
|
1331
|
+
function At(e) {
|
|
1332
|
+
let t = e.gatewayAgentId ?? "agent.gateway", n = e.workerAgentId ?? "agent.worker", r = e.allowedMethods ?? Tt, i = e.workerCapabilityPrefix ?? "mcp.", a = e.gatewayWireFormat ?? "compact", o = e.workerWireFormat ?? "compact";
|
|
1330
1333
|
async function s(t) {
|
|
1331
1334
|
e.onAuditEvent && await e.onAuditEvent({
|
|
1332
1335
|
...t,
|
|
1333
1336
|
timestampMs: Date.now()
|
|
1334
1337
|
});
|
|
1335
1338
|
}
|
|
1336
|
-
let c =
|
|
1339
|
+
let c = st({
|
|
1337
1340
|
agentId: t,
|
|
1338
1341
|
outboundSecret: e.sharedSecret,
|
|
1339
1342
|
sharedSecrets: { [n]: e.sharedSecret },
|
|
1340
1343
|
wireFormat: a
|
|
1341
|
-
}), l =
|
|
1344
|
+
}), l = st({
|
|
1342
1345
|
agentId: n,
|
|
1343
1346
|
outboundSecret: e.sharedSecret,
|
|
1344
1347
|
sharedSecrets: { [t]: e.sharedSecret },
|
|
1345
1348
|
wireFormat: o,
|
|
1346
1349
|
onTask: async (e) => ({
|
|
1347
1350
|
intent: "RESULT",
|
|
1348
|
-
content:
|
|
1351
|
+
content: Et({
|
|
1349
1352
|
ok: !0,
|
|
1350
1353
|
capability: e.body.capability,
|
|
1351
1354
|
content: e.body.content
|
|
@@ -1358,12 +1361,12 @@ function kt(e) {
|
|
|
1358
1361
|
if (!i) return null;
|
|
1359
1362
|
let a = null;
|
|
1360
1363
|
try {
|
|
1361
|
-
let t =
|
|
1364
|
+
let t = Ot(i);
|
|
1362
1365
|
if (!t) return await s({
|
|
1363
1366
|
phase: "request_error",
|
|
1364
1367
|
code: -32600,
|
|
1365
1368
|
message: "Invalid Request"
|
|
1366
|
-
}),
|
|
1369
|
+
}), Dt(null, -32600, "Invalid Request");
|
|
1367
1370
|
a = t.id, await s({
|
|
1368
1371
|
phase: "request_received",
|
|
1369
1372
|
id: t.id,
|
|
@@ -1384,7 +1387,7 @@ function kt(e) {
|
|
|
1384
1387
|
policy: e
|
|
1385
1388
|
});
|
|
1386
1389
|
}
|
|
1387
|
-
}, u = await
|
|
1390
|
+
}, u = await pt(c, t, o), d = null;
|
|
1388
1391
|
if (await l.handleRaw(u, async (e) => {
|
|
1389
1392
|
d = e;
|
|
1390
1393
|
}), !d) return await s({
|
|
@@ -1393,7 +1396,7 @@ function kt(e) {
|
|
|
1393
1396
|
method: t.method,
|
|
1394
1397
|
code: -32603,
|
|
1395
1398
|
message: "AIP worker did not produce a response"
|
|
1396
|
-
}),
|
|
1399
|
+
}), Dt(a, -32603, "AIP worker did not produce a response");
|
|
1397
1400
|
let f = await c.receiveRaw(d);
|
|
1398
1401
|
return !f.ok || !f.received ? (await s({
|
|
1399
1402
|
phase: "verification_failed",
|
|
@@ -1401,24 +1404,24 @@ function kt(e) {
|
|
|
1401
1404
|
method: t.method,
|
|
1402
1405
|
code: -32603,
|
|
1403
1406
|
message: "AIP verification failed"
|
|
1404
|
-
}),
|
|
1407
|
+
}), Dt(a, -32603, "AIP verification failed")) : (await s({
|
|
1405
1408
|
phase: "request_success",
|
|
1406
1409
|
id: t.id,
|
|
1407
1410
|
method: t.method
|
|
1408
|
-
}),
|
|
1411
|
+
}), Et(mt(f.received, t.id)));
|
|
1409
1412
|
} catch (e) {
|
|
1410
|
-
let t = e instanceof Error ? e.message : "Internal error", n =
|
|
1413
|
+
let t = e instanceof Error ? e.message : "Internal error", n = kt(t);
|
|
1411
1414
|
return await s({
|
|
1412
1415
|
phase: "request_error",
|
|
1413
1416
|
id: a,
|
|
1414
1417
|
code: n,
|
|
1415
1418
|
message: t
|
|
1416
|
-
}),
|
|
1419
|
+
}), Dt(a, n, t);
|
|
1417
1420
|
}
|
|
1418
1421
|
} };
|
|
1419
1422
|
}
|
|
1420
|
-
async function
|
|
1421
|
-
if (!e.runtimePolicy?.enabled) return
|
|
1423
|
+
async function jt(e) {
|
|
1424
|
+
if (!e.runtimePolicy?.enabled) return At(e);
|
|
1422
1425
|
let { enforcer: t } = await N(e.runtimePolicy.options);
|
|
1423
1426
|
t.assertInvariant({
|
|
1424
1427
|
signatureVerification: !0,
|
|
@@ -1431,7 +1434,7 @@ async function At(e) {
|
|
|
1431
1434
|
latencySensitive: e.runtimePolicy.latencySensitive,
|
|
1432
1435
|
compatibilityFirst: e.runtimePolicy.compatibilityFirst
|
|
1433
1436
|
}), r = n.includes("binary") ? "binary" : "compact";
|
|
1434
|
-
return
|
|
1437
|
+
return At({
|
|
1435
1438
|
...e,
|
|
1436
1439
|
gatewayWireFormat: r,
|
|
1437
1440
|
workerWireFormat: r,
|
|
@@ -1447,7 +1450,7 @@ async function At(e) {
|
|
|
1447
1450
|
}
|
|
1448
1451
|
//#endregion
|
|
1449
1452
|
//#region src/runtimePolicyManager.ts
|
|
1450
|
-
var
|
|
1453
|
+
var Mt = class {
|
|
1451
1454
|
snapshot = null;
|
|
1452
1455
|
options;
|
|
1453
1456
|
constructor(e = {}) {
|
|
@@ -1477,7 +1480,7 @@ var jt = class {
|
|
|
1477
1480
|
throw t;
|
|
1478
1481
|
}
|
|
1479
1482
|
}
|
|
1480
|
-
},
|
|
1483
|
+
}, Nt = {
|
|
1481
1484
|
dev: {
|
|
1482
1485
|
environment: "dev",
|
|
1483
1486
|
mode: "ws-batch",
|
|
@@ -1503,12 +1506,12 @@ var jt = class {
|
|
|
1503
1506
|
retryMaxAttempts: 3
|
|
1504
1507
|
}
|
|
1505
1508
|
};
|
|
1506
|
-
function
|
|
1507
|
-
return
|
|
1509
|
+
function Pt(e) {
|
|
1510
|
+
return Nt[e];
|
|
1508
1511
|
}
|
|
1509
1512
|
//#endregion
|
|
1510
1513
|
//#region src/policyTelemetryFeedback.ts
|
|
1511
|
-
function
|
|
1514
|
+
function Ft(e) {
|
|
1512
1515
|
let t = [], n = "normal";
|
|
1513
1516
|
return e.dropPct > .1 && (t.push("reduce inflight cap"), t.push("reduce batch size"), t.push("increase retry backoff"), e.mode === "http" && e.concurrency >= 100 && t.push("switch to http-binary-batch"), n = e.dropPct > 2 ? "critical" : "warn"), e.p99Ms > 20 && (t.push("reduce batch size"), t.push("run adaptive benchmark and re-baseline"), n === "normal" && (n = "warn"), e.p99Ms > 100 && (n = "critical")), {
|
|
1514
1517
|
severity: n,
|
|
@@ -1517,7 +1520,7 @@ function Pt(e) {
|
|
|
1517
1520
|
}
|
|
1518
1521
|
//#endregion
|
|
1519
1522
|
//#region src/redisClient.ts
|
|
1520
|
-
var
|
|
1523
|
+
var It = class {
|
|
1521
1524
|
entries = /* @__PURE__ */ new Map();
|
|
1522
1525
|
now;
|
|
1523
1526
|
constructor(e = {}) {
|
|
@@ -1563,13 +1566,13 @@ var Ft = class {
|
|
|
1563
1566
|
};
|
|
1564
1567
|
return t;
|
|
1565
1568
|
}
|
|
1566
|
-
},
|
|
1569
|
+
}, Lt = class {
|
|
1567
1570
|
keyPrefix;
|
|
1568
1571
|
client;
|
|
1569
1572
|
constructor(e = {}) {
|
|
1570
1573
|
if (this.keyPrefix = e.keyPrefix ?? "7h3:nonce:", e.client) this.client = e.client;
|
|
1571
1574
|
else {
|
|
1572
|
-
let e = new
|
|
1575
|
+
let e = new It();
|
|
1573
1576
|
this.client = {
|
|
1574
1577
|
set: (t, n, r) => e.set(t, n, {
|
|
1575
1578
|
nx: r?.nx,
|
|
@@ -1588,7 +1591,7 @@ var Ft = class {
|
|
|
1588
1591
|
px: Math.max(1, t)
|
|
1589
1592
|
}) === null;
|
|
1590
1593
|
}
|
|
1591
|
-
},
|
|
1594
|
+
}, Rt = class {
|
|
1592
1595
|
nodes;
|
|
1593
1596
|
constructor(e) {
|
|
1594
1597
|
this.nodes = e;
|
|
@@ -1597,17 +1600,17 @@ var Ft = class {
|
|
|
1597
1600
|
return (await Promise.all(this.nodes.map((n) => n.check(e, t)))).some((e) => e);
|
|
1598
1601
|
}
|
|
1599
1602
|
};
|
|
1600
|
-
function
|
|
1601
|
-
return
|
|
1603
|
+
function zt(e, t = {}) {
|
|
1604
|
+
return Bt(e) ? Ht(e, t) : new Lt(e);
|
|
1602
1605
|
}
|
|
1603
|
-
function
|
|
1606
|
+
function Bt(e) {
|
|
1604
1607
|
return typeof e == "object" && !!e && typeof e.set == "function" && (typeof e.pipeline == "function" || !("quit" in e));
|
|
1605
1608
|
}
|
|
1606
|
-
function
|
|
1607
|
-
return new
|
|
1609
|
+
function Vt(e) {
|
|
1610
|
+
return new Rt(e.map((e) => new Lt({ redisUrl: e })));
|
|
1608
1611
|
}
|
|
1609
|
-
function
|
|
1610
|
-
let n = t.keyPrefix ?? "aip:replay:", r = t.errorBehavior ?? "fallback", i = t.onDegraded, a = t.fallback ?? (r === "fallback" ?
|
|
1612
|
+
function Ht(e, t = {}) {
|
|
1613
|
+
let n = t.keyPrefix ?? "aip:replay:", r = t.errorBehavior ?? "fallback", i = t.onDegraded, a = t.fallback ?? (r === "fallback" ? Ht(new It(), {
|
|
1611
1614
|
keyPrefix: n,
|
|
1612
1615
|
errorBehavior: "reject"
|
|
1613
1616
|
}) : void 0);
|
|
@@ -1647,7 +1650,7 @@ function Vt(e, t = {}) {
|
|
|
1647
1650
|
}
|
|
1648
1651
|
//#endregion
|
|
1649
1652
|
//#region src/revocation.ts
|
|
1650
|
-
var
|
|
1653
|
+
var Ut = class {
|
|
1651
1654
|
revoked = /* @__PURE__ */ new Map();
|
|
1652
1655
|
now;
|
|
1653
1656
|
constructor(e = {}) {
|
|
@@ -1664,7 +1667,7 @@ var Ht = class {
|
|
|
1664
1667
|
this.revoked.set(this.makeKey(e, t), n.untilMs ?? Infinity);
|
|
1665
1668
|
}
|
|
1666
1669
|
};
|
|
1667
|
-
function
|
|
1670
|
+
function Wt(e, t = {}) {
|
|
1668
1671
|
let n = t.keyPrefix ?? "aip:revoked:", r = t.errorBehavior ?? "reject", i = t.cacheTtlMs ?? 5e3, a = t.onDegraded, o = t.now ?? (() => Date.now()), s = /* @__PURE__ */ new Map();
|
|
1669
1672
|
function c(e, t) {
|
|
1670
1673
|
return `${n}${e}${t}`;
|
|
@@ -1696,7 +1699,7 @@ function Ut(e, t = {}) {
|
|
|
1696
1699
|
}
|
|
1697
1700
|
};
|
|
1698
1701
|
}
|
|
1699
|
-
function
|
|
1702
|
+
function Gt(e, t, n = {}) {
|
|
1700
1703
|
let r = n.nowMsProvider ?? (() => Date.now());
|
|
1701
1704
|
return async (n, i) => {
|
|
1702
1705
|
if (!await t.isRevoked(i, n.keyId, r())) return e(n, i);
|
|
@@ -1704,7 +1707,7 @@ function Wt(e, t, n = {}) {
|
|
|
1704
1707
|
}
|
|
1705
1708
|
//#endregion
|
|
1706
1709
|
//#region src/mcpWrapper.ts
|
|
1707
|
-
function
|
|
1710
|
+
function Kt(e, t, n) {
|
|
1708
1711
|
return {
|
|
1709
1712
|
jsonrpc: "2.0",
|
|
1710
1713
|
id: e,
|
|
@@ -1714,15 +1717,15 @@ function Gt(e, t, n) {
|
|
|
1714
1717
|
}
|
|
1715
1718
|
};
|
|
1716
1719
|
}
|
|
1717
|
-
function
|
|
1720
|
+
function qt(e, t) {
|
|
1718
1721
|
return {
|
|
1719
1722
|
...e.receive,
|
|
1720
1723
|
requireSignature: !0,
|
|
1721
1724
|
replayCache: t
|
|
1722
1725
|
};
|
|
1723
1726
|
}
|
|
1724
|
-
function
|
|
1725
|
-
let n = t.wireFormat ?? "compact", r = t.ttlMs ?? 6e4, i =
|
|
1727
|
+
function Jt(e, t) {
|
|
1728
|
+
let n = t.wireFormat ?? "compact", r = t.ttlMs ?? 6e4, i = qt(t, t.receive?.replayCache ?? new A());
|
|
1726
1729
|
async function a(e) {
|
|
1727
1730
|
return j(await t.sign(e), n);
|
|
1728
1731
|
}
|
|
@@ -1738,25 +1741,25 @@ function qt(e, t) {
|
|
|
1738
1741
|
}
|
|
1739
1742
|
return async (n) => {
|
|
1740
1743
|
let r = await M(n, i), a = r.envelope?.header.sender, s = r.envelope?.header.messageId;
|
|
1741
|
-
if (!r.ok || !r.envelope) return o(a, "ERROR",
|
|
1742
|
-
if (r.envelope.header.recipient !== t.selfAgentId) return o(a, "ERROR",
|
|
1744
|
+
if (!r.ok || !r.envelope) return o(a, "ERROR", Kt(null, -32600, r.diagnostics.find((e) => e.level === "error")?.message ?? "AIP verification failed"), s);
|
|
1745
|
+
if (r.envelope.header.recipient !== t.selfAgentId) return o(a, "ERROR", Kt(null, -32600, "Recipient mismatch: envelope not addressed to this agent"), s);
|
|
1743
1746
|
let c;
|
|
1744
1747
|
try {
|
|
1745
1748
|
c = JSON.parse(r.envelope.body.content);
|
|
1746
1749
|
} catch {
|
|
1747
|
-
return o(a, "ERROR",
|
|
1750
|
+
return o(a, "ERROR", Kt(null, -32700, "Parse error: envelope body is not JSON-RPC"), s);
|
|
1748
1751
|
}
|
|
1749
1752
|
let l;
|
|
1750
1753
|
try {
|
|
1751
1754
|
l = await e(c);
|
|
1752
1755
|
} catch (e) {
|
|
1753
|
-
l =
|
|
1756
|
+
l = Kt(c.id ?? null, -32603, e instanceof Error ? e.message : "Internal error");
|
|
1754
1757
|
}
|
|
1755
1758
|
return o(r.envelope.header.sender, "RESULT", l, s);
|
|
1756
1759
|
};
|
|
1757
1760
|
}
|
|
1758
|
-
function
|
|
1759
|
-
let t = e.wireFormat ?? "compact", n = e.ttlMs ?? 6e4, r =
|
|
1761
|
+
function Yt(e) {
|
|
1762
|
+
let t = e.wireFormat ?? "compact", n = e.ttlMs ?? 6e4, r = qt(e, e.receive?.replayCache ?? new A());
|
|
1760
1763
|
return {
|
|
1761
1764
|
async encodeRequest(r) {
|
|
1762
1765
|
let i = D({
|
|
@@ -1784,8 +1787,8 @@ function Jt(e) {
|
|
|
1784
1787
|
}
|
|
1785
1788
|
};
|
|
1786
1789
|
}
|
|
1787
|
-
function
|
|
1788
|
-
let n =
|
|
1790
|
+
function Xt(e, t) {
|
|
1791
|
+
let n = Yt(t);
|
|
1789
1792
|
return async (t) => {
|
|
1790
1793
|
let { raw: r, messageId: i } = await n.encodeRequest(t);
|
|
1791
1794
|
return n.decodeResponse(await e(r), { expectCorrelationId: i });
|
|
@@ -1793,11 +1796,11 @@ function Yt(e, t) {
|
|
|
1793
1796
|
}
|
|
1794
1797
|
//#endregion
|
|
1795
1798
|
//#region src/mcpTransports.ts
|
|
1796
|
-
function
|
|
1799
|
+
function Zt(e) {
|
|
1797
1800
|
if (typeof e != "string") throw Error("stdio adapter supports json/compact (string) wire formats; use the HTTP adapter for binary");
|
|
1798
1801
|
return e;
|
|
1799
1802
|
}
|
|
1800
|
-
function
|
|
1803
|
+
function Qt(t, n = {}) {
|
|
1801
1804
|
let r = n.input ?? process.stdin, i = n.output ?? process.stdout, a = e.createInterface({
|
|
1802
1805
|
input: r,
|
|
1803
1806
|
crlfDelay: Infinity
|
|
@@ -1806,7 +1809,7 @@ function Zt(t, n = {}) {
|
|
|
1806
1809
|
r && (o = o.then(async () => {
|
|
1807
1810
|
try {
|
|
1808
1811
|
let e = await t(r);
|
|
1809
|
-
i.write(`${
|
|
1812
|
+
i.write(`${Zt(e)}\n`);
|
|
1810
1813
|
} catch (e) {
|
|
1811
1814
|
n.onError?.(e);
|
|
1812
1815
|
}
|
|
@@ -1816,7 +1819,7 @@ function Zt(t, n = {}) {
|
|
|
1816
1819
|
a.off("line", s), a.close();
|
|
1817
1820
|
} };
|
|
1818
1821
|
}
|
|
1819
|
-
function
|
|
1822
|
+
function $t(t) {
|
|
1820
1823
|
let n = e.createInterface({
|
|
1821
1824
|
input: t.input,
|
|
1822
1825
|
crlfDelay: Infinity
|
|
@@ -1828,7 +1831,7 @@ function Qt(t) {
|
|
|
1828
1831
|
};
|
|
1829
1832
|
return n.on("line", i), {
|
|
1830
1833
|
send(e) {
|
|
1831
|
-
let n =
|
|
1834
|
+
let n = Zt(e);
|
|
1832
1835
|
return new Promise((e) => {
|
|
1833
1836
|
r.push(e), t.output.write(`${n}\n`);
|
|
1834
1837
|
});
|
|
@@ -1838,7 +1841,7 @@ function Qt(t) {
|
|
|
1838
1841
|
}
|
|
1839
1842
|
};
|
|
1840
1843
|
}
|
|
1841
|
-
function
|
|
1844
|
+
function en(e, t = {}) {
|
|
1842
1845
|
return (n, r) => {
|
|
1843
1846
|
let i = [];
|
|
1844
1847
|
n.on("data", (e) => i.push(e)), n.on("error", () => {
|
|
@@ -1862,7 +1865,7 @@ function $t(e, t = {}) {
|
|
|
1862
1865
|
});
|
|
1863
1866
|
};
|
|
1864
1867
|
}
|
|
1865
|
-
function
|
|
1868
|
+
function tn(e) {
|
|
1866
1869
|
let t = e.fetchImpl ?? fetch, n = e.binary ? "application/octet-stream" : "application/json";
|
|
1867
1870
|
return { async send(r) {
|
|
1868
1871
|
let i = await t(e.url, {
|
|
@@ -1878,12 +1881,12 @@ function en(e) {
|
|
|
1878
1881
|
}
|
|
1879
1882
|
//#endregion
|
|
1880
1883
|
//#region src/keyRegistry.ts
|
|
1881
|
-
function
|
|
1884
|
+
function nn(e) {
|
|
1882
1885
|
return { async getPublicKey(t) {
|
|
1883
1886
|
return e[t] ?? null;
|
|
1884
1887
|
} };
|
|
1885
1888
|
}
|
|
1886
|
-
function
|
|
1889
|
+
function rn(...e) {
|
|
1887
1890
|
return {
|
|
1888
1891
|
async getPublicKey(t) {
|
|
1889
1892
|
for (let n of e) {
|
|
@@ -1902,7 +1905,7 @@ function nn(...e) {
|
|
|
1902
1905
|
}
|
|
1903
1906
|
};
|
|
1904
1907
|
}
|
|
1905
|
-
function
|
|
1908
|
+
function an(e, t) {
|
|
1906
1909
|
let n = t?.ttlMs ?? 6e4, r = /* @__PURE__ */ new Map();
|
|
1907
1910
|
async function i(e, t) {
|
|
1908
1911
|
let i = Date.now(), a = r.get(e);
|
|
@@ -1924,18 +1927,18 @@ function rn(e, t) {
|
|
|
1924
1927
|
}
|
|
1925
1928
|
//#endregion
|
|
1926
1929
|
//#region src/cborCodec.ts
|
|
1927
|
-
var P = new TextEncoder(),
|
|
1930
|
+
var P = new TextEncoder(), on = new TextDecoder(), F = 0, sn = 1, cn = 2, I = 3, ln = 4, un = 5, dn = 7, fn = 244, pn = 245, mn = 246, hn = 24, gn = 25, _n = 26, vn = 27, yn = 27, bn = class {
|
|
1928
1931
|
chunks = [];
|
|
1929
1932
|
encode(e) {
|
|
1930
1933
|
return this.chunks = [], this._encode(e), this._concat();
|
|
1931
1934
|
}
|
|
1932
1935
|
_encode(e) {
|
|
1933
1936
|
if (e == null) {
|
|
1934
|
-
this.chunks.push(new Uint8Array([
|
|
1937
|
+
this.chunks.push(new Uint8Array([mn]));
|
|
1935
1938
|
return;
|
|
1936
1939
|
}
|
|
1937
1940
|
if (typeof e == "boolean") {
|
|
1938
|
-
this.chunks.push(new Uint8Array([e ?
|
|
1941
|
+
this.chunks.push(new Uint8Array([e ? pn : fn]));
|
|
1939
1942
|
return;
|
|
1940
1943
|
}
|
|
1941
1944
|
if (typeof e == "number") {
|
|
@@ -1973,7 +1976,7 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
1973
1976
|
new DataView(t).setFloat64(0, e, !1), this.chunks.push(new Uint8Array(t));
|
|
1974
1977
|
return;
|
|
1975
1978
|
}
|
|
1976
|
-
e >= 0 ? this._encodeHead(F, e) : this._encodeHead(
|
|
1979
|
+
e >= 0 ? this._encodeHead(F, e) : this._encodeHead(sn, -1 - e);
|
|
1977
1980
|
}
|
|
1978
1981
|
_float64Header() {
|
|
1979
1982
|
return new Uint8Array([251]);
|
|
@@ -1983,10 +1986,10 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
1983
1986
|
this._encodeHead(I, t.length), this.chunks.push(t);
|
|
1984
1987
|
}
|
|
1985
1988
|
_encodeByteString(e) {
|
|
1986
|
-
this._encodeHead(
|
|
1989
|
+
this._encodeHead(cn, e.length), this.chunks.push(e);
|
|
1987
1990
|
}
|
|
1988
1991
|
_encodeArray(e) {
|
|
1989
|
-
this._encodeHead(
|
|
1992
|
+
this._encodeHead(ln, e.length);
|
|
1990
1993
|
for (let t of e) this._encode(t);
|
|
1991
1994
|
}
|
|
1992
1995
|
_encodeMapFromAnyEntries(e) {
|
|
@@ -2003,7 +2006,7 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
2003
2006
|
value: t
|
|
2004
2007
|
};
|
|
2005
2008
|
});
|
|
2006
|
-
t.sort((e, t) => this._compareBytes(e.keyEncoded, t.keyEncoded)), this._encodeHead(
|
|
2009
|
+
t.sort((e, t) => this._compareBytes(e.keyEncoded, t.keyEncoded)), this._encodeHead(un, t.length);
|
|
2007
2010
|
for (let { key: e, value: n } of t) {
|
|
2008
2011
|
if (typeof e == "number" && Number.isInteger(e)) this._encodeHead(F, e);
|
|
2009
2012
|
else {
|
|
@@ -2022,7 +2025,7 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
2022
2025
|
value: t
|
|
2023
2026
|
};
|
|
2024
2027
|
});
|
|
2025
|
-
t.sort((e, t) => this._compareBytes(e.keyEncoded, t.keyEncoded)), this._encodeHead(
|
|
2028
|
+
t.sort((e, t) => this._compareBytes(e.keyEncoded, t.keyEncoded)), this._encodeHead(un, t.length);
|
|
2026
2029
|
for (let { keyBytes: e, value: n } of t) this._encodeHead(I, e.length), this.chunks.push(e), this._encode(n);
|
|
2027
2030
|
}
|
|
2028
2031
|
_compareBytes(e, t) {
|
|
@@ -2043,48 +2046,58 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
2043
2046
|
_encodeHeadBytes(e, t) {
|
|
2044
2047
|
let n = e << 5;
|
|
2045
2048
|
if (t <= 23) return new Uint8Array([n | t]);
|
|
2046
|
-
if (t <= 255) return new Uint8Array([n |
|
|
2049
|
+
if (t <= 255) return new Uint8Array([n | hn, t]);
|
|
2047
2050
|
if (t <= 65535) {
|
|
2048
2051
|
let e = /* @__PURE__ */ new ArrayBuffer(3), r = new DataView(e);
|
|
2049
|
-
return r.setUint8(0, n |
|
|
2052
|
+
return r.setUint8(0, n | gn), r.setUint16(1, t, !1), new Uint8Array(e);
|
|
2050
2053
|
}
|
|
2051
2054
|
if (t <= 4294967295) {
|
|
2052
2055
|
let e = /* @__PURE__ */ new ArrayBuffer(5), r = new DataView(e);
|
|
2053
|
-
return r.setUint8(0, n |
|
|
2056
|
+
return r.setUint8(0, n | _n), r.setUint32(1, t, !1), new Uint8Array(e);
|
|
2054
2057
|
}
|
|
2055
2058
|
let r = /* @__PURE__ */ new ArrayBuffer(9), i = new DataView(r);
|
|
2056
|
-
return i.setUint8(0, n |
|
|
2059
|
+
return i.setUint8(0, n | vn), i.setBigUint64(1, BigInt(t), !1), new Uint8Array(r);
|
|
2057
2060
|
}
|
|
2058
2061
|
_concat() {
|
|
2059
2062
|
let e = this.chunks.reduce((e, t) => e + t.length, 0), t = new Uint8Array(e), n = 0;
|
|
2060
2063
|
for (let e of this.chunks) t.set(e, n), n += e.length;
|
|
2061
2064
|
return t;
|
|
2062
2065
|
}
|
|
2063
|
-
},
|
|
2066
|
+
}, xn = 64, Sn = class {
|
|
2064
2067
|
data;
|
|
2065
2068
|
offset = 0;
|
|
2069
|
+
depth = 0;
|
|
2066
2070
|
decode(e) {
|
|
2067
|
-
return this.data = e, this.offset = 0, this._decode();
|
|
2071
|
+
return this.data = e, this.offset = 0, this.depth = 0, this._decode();
|
|
2068
2072
|
}
|
|
2069
2073
|
_decode() {
|
|
2074
|
+
if (this.depth >= 64) throw Error("CborDecoder: nesting depth exceeds maximum of 64");
|
|
2075
|
+
this.depth++;
|
|
2076
|
+
try {
|
|
2077
|
+
return this._decodeItem();
|
|
2078
|
+
} finally {
|
|
2079
|
+
this.depth--;
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
_decodeItem() {
|
|
2070
2083
|
let e = this._readByte(), t = e >> 5 & 7, n = e & 31;
|
|
2071
2084
|
switch (t) {
|
|
2072
2085
|
case F: return this._decodeUint(n);
|
|
2073
|
-
case
|
|
2074
|
-
case
|
|
2086
|
+
case sn: return -1 - this._decodeUint(n);
|
|
2087
|
+
case cn: {
|
|
2075
2088
|
let e = this._decodeUint(n);
|
|
2076
2089
|
return this._readBytes(e);
|
|
2077
2090
|
}
|
|
2078
2091
|
case I: {
|
|
2079
2092
|
let e = this._decodeUint(n), t = this._readBytes(e);
|
|
2080
|
-
return
|
|
2093
|
+
return on.decode(t);
|
|
2081
2094
|
}
|
|
2082
|
-
case
|
|
2095
|
+
case ln: {
|
|
2083
2096
|
let e = this._decodeUint(n), t = [];
|
|
2084
2097
|
for (let n = 0; n < e; n++) t.push(this._decode());
|
|
2085
2098
|
return t;
|
|
2086
2099
|
}
|
|
2087
|
-
case
|
|
2100
|
+
case un: {
|
|
2088
2101
|
let e = this._decodeUint(n), t = Object.create(null);
|
|
2089
2102
|
for (let n = 0; n < e; n++) {
|
|
2090
2103
|
let e = this._decode();
|
|
@@ -2092,11 +2105,11 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
2092
2105
|
}
|
|
2093
2106
|
return t;
|
|
2094
2107
|
}
|
|
2095
|
-
case
|
|
2108
|
+
case dn:
|
|
2096
2109
|
if (n === 20) return !1;
|
|
2097
2110
|
if (n === 21) return !0;
|
|
2098
2111
|
if (n === 22) return null;
|
|
2099
|
-
if (n ===
|
|
2112
|
+
if (n === yn) {
|
|
2100
2113
|
let e = this._readBytes(8);
|
|
2101
2114
|
return new DataView(e.buffer, e.byteOffset, 8).getFloat64(0, !1);
|
|
2102
2115
|
}
|
|
@@ -2106,16 +2119,16 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
2106
2119
|
}
|
|
2107
2120
|
_decodeUint(e) {
|
|
2108
2121
|
if (e <= 23) return e;
|
|
2109
|
-
if (e ===
|
|
2110
|
-
if (e ===
|
|
2122
|
+
if (e === hn) return this._readByte();
|
|
2123
|
+
if (e === gn) {
|
|
2111
2124
|
let e = this._readBytes(2);
|
|
2112
2125
|
return new DataView(e.buffer, e.byteOffset, 2).getUint16(0, !1);
|
|
2113
2126
|
}
|
|
2114
|
-
if (e ===
|
|
2127
|
+
if (e === _n) {
|
|
2115
2128
|
let e = this._readBytes(4);
|
|
2116
2129
|
return new DataView(e.buffer, e.byteOffset, 4).getUint32(0, !1);
|
|
2117
2130
|
}
|
|
2118
|
-
if (e ===
|
|
2131
|
+
if (e === vn) {
|
|
2119
2132
|
let e = this._readBytes(8);
|
|
2120
2133
|
return Number(new DataView(e.buffer, e.byteOffset, 8).getBigUint64(0, !1));
|
|
2121
2134
|
}
|
|
@@ -2131,16 +2144,16 @@ var P = new TextEncoder(), an = new TextDecoder(), F = 0, on = 1, sn = 2, I = 3,
|
|
|
2131
2144
|
return this.offset += e, t;
|
|
2132
2145
|
}
|
|
2133
2146
|
};
|
|
2134
|
-
function
|
|
2135
|
-
return new
|
|
2147
|
+
function Cn(e) {
|
|
2148
|
+
return new bn().encode(e);
|
|
2136
2149
|
}
|
|
2137
|
-
function
|
|
2138
|
-
return new
|
|
2150
|
+
function wn(e) {
|
|
2151
|
+
return new Sn().decode(e);
|
|
2139
2152
|
}
|
|
2140
2153
|
//#endregion
|
|
2141
2154
|
//#region src/envelopeCbor.ts
|
|
2142
|
-
var
|
|
2143
|
-
function
|
|
2155
|
+
var Tn = "application/7h3-cbor";
|
|
2156
|
+
function En(e) {
|
|
2144
2157
|
let t = /* @__PURE__ */ new Map();
|
|
2145
2158
|
t.set(1, e.header.version), t.set(2, e.header.messageId), t.set(3, e.header.timestampMs), t.set(4, e.header.ttlMs), t.set(5, e.header.sender), e.header.recipient !== void 0 && t.set(6, e.header.recipient), t.set(7, e.header.nonce);
|
|
2146
2159
|
let n = /* @__PURE__ */ new Map();
|
|
@@ -2152,21 +2165,21 @@ function wn(e) {
|
|
|
2152
2165
|
let e = /* @__PURE__ */ new Map();
|
|
2153
2166
|
e.set(1, i.alg), e.set(2, i.keyId), e.set(3, i.value), r.set(3, e);
|
|
2154
2167
|
}
|
|
2155
|
-
return
|
|
2168
|
+
return Cn(r);
|
|
2156
2169
|
}
|
|
2157
2170
|
function L(e, t) {
|
|
2158
2171
|
if (typeof e != "string") throw Error(`decodeEnvelopeCbor: ${t} must be a string`);
|
|
2159
2172
|
return e;
|
|
2160
2173
|
}
|
|
2161
|
-
function
|
|
2174
|
+
function Dn(e, t) {
|
|
2162
2175
|
if (e !== void 0) return L(e, t);
|
|
2163
2176
|
}
|
|
2164
|
-
function
|
|
2177
|
+
function On(e, t) {
|
|
2165
2178
|
if (typeof e != "number" || !Number.isFinite(e)) throw Error(`decodeEnvelopeCbor: ${t} must be a finite number`);
|
|
2166
2179
|
return e;
|
|
2167
2180
|
}
|
|
2168
|
-
function
|
|
2169
|
-
let t =
|
|
2181
|
+
function kn(e) {
|
|
2182
|
+
let t = wn(e);
|
|
2170
2183
|
if (!t || typeof t != "object" || Array.isArray(t)) throw Error("decodeEnvelopeCbor: expected a map at top level");
|
|
2171
2184
|
let n = t, r = n[1];
|
|
2172
2185
|
if (!r || typeof r != "object" || Array.isArray(r)) throw Error("decodeEnvelopeCbor: missing header map (key 1)");
|
|
@@ -2176,8 +2189,8 @@ function Dn(e) {
|
|
|
2176
2189
|
header: {
|
|
2177
2190
|
version: L(i[1], "header.version"),
|
|
2178
2191
|
messageId: L(i[2], "header.messageId"),
|
|
2179
|
-
timestampMs:
|
|
2180
|
-
ttlMs:
|
|
2192
|
+
timestampMs: On(i[3], "header.timestampMs"),
|
|
2193
|
+
ttlMs: On(i[4], "header.ttlMs"),
|
|
2181
2194
|
sender: L(i[5], "header.sender"),
|
|
2182
2195
|
nonce: L(i[7], "header.nonce")
|
|
2183
2196
|
},
|
|
@@ -2185,11 +2198,11 @@ function Dn(e) {
|
|
|
2185
2198
|
intent: L(o[1], "body.intent"),
|
|
2186
2199
|
content: L(o[2], "body.content")
|
|
2187
2200
|
}
|
|
2188
|
-
}, c =
|
|
2201
|
+
}, c = Dn(i[6], "header.recipient");
|
|
2189
2202
|
c !== void 0 && (s.header.recipient = c);
|
|
2190
|
-
let l =
|
|
2203
|
+
let l = Dn(o[3], "body.capability");
|
|
2191
2204
|
l !== void 0 && (s.body.capability = l);
|
|
2192
|
-
let u =
|
|
2205
|
+
let u = Dn(o[4], "body.correlationId");
|
|
2193
2206
|
u !== void 0 && (s.body.correlationId = u);
|
|
2194
2207
|
let d = n[3];
|
|
2195
2208
|
if (d != null) {
|
|
@@ -2205,11 +2218,11 @@ function Dn(e) {
|
|
|
2205
2218
|
}
|
|
2206
2219
|
//#endregion
|
|
2207
2220
|
//#region src/httpBinding.ts
|
|
2208
|
-
var
|
|
2209
|
-
async function
|
|
2221
|
+
var An = "x-7h3-envelope";
|
|
2222
|
+
async function jn(e, t, n) {
|
|
2210
2223
|
let r = t.headerName ?? "x-7h3-envelope", i = (Array.isArray(e["content-type"]) ? e["content-type"][0] : e["content-type"]) ?? "", a;
|
|
2211
2224
|
if (i.includes("7h3-cbor") && n instanceof Uint8Array) try {
|
|
2212
|
-
a =
|
|
2225
|
+
a = kn(n);
|
|
2213
2226
|
} catch (e) {
|
|
2214
2227
|
return {
|
|
2215
2228
|
ok: !1,
|
|
@@ -2247,6 +2260,11 @@ async function kn(e, t, n) {
|
|
|
2247
2260
|
};
|
|
2248
2261
|
}
|
|
2249
2262
|
let o = a.header.sender, s = a.signature.alg;
|
|
2263
|
+
if (!t.keyRegistry) return {
|
|
2264
|
+
ok: !1,
|
|
2265
|
+
reason: "unknown-sender",
|
|
2266
|
+
detail: "no key registry configured"
|
|
2267
|
+
};
|
|
2250
2268
|
if (s === "ED25519") {
|
|
2251
2269
|
let e = await t.keyRegistry.getPublicKey(o);
|
|
2252
2270
|
if (!e) return {
|
|
@@ -2279,24 +2297,24 @@ async function kn(e, t, n) {
|
|
|
2279
2297
|
envelope: a
|
|
2280
2298
|
};
|
|
2281
2299
|
}
|
|
2282
|
-
async function
|
|
2300
|
+
async function Mn(e, t, n) {
|
|
2283
2301
|
let r = await w(e, t);
|
|
2284
2302
|
if (n?.format === "cbor") {
|
|
2285
|
-
let e =
|
|
2303
|
+
let e = En(r);
|
|
2286
2304
|
return {
|
|
2287
|
-
headers: { "content-type":
|
|
2305
|
+
headers: { "content-type": Tn },
|
|
2288
2306
|
body: e
|
|
2289
2307
|
};
|
|
2290
2308
|
}
|
|
2291
2309
|
return { headers: { [n?.headerName ?? "x-7h3-envelope"]: JSON.stringify(r) } };
|
|
2292
2310
|
}
|
|
2293
|
-
async function
|
|
2311
|
+
async function Nn(e, t, n, r) {
|
|
2294
2312
|
let i = await S(e, t, n);
|
|
2295
2313
|
return { headers: { [r?.headerName ?? "x-7h3-envelope"]: JSON.stringify(i) } };
|
|
2296
2314
|
}
|
|
2297
|
-
function
|
|
2315
|
+
function Pn(e) {
|
|
2298
2316
|
return async function(t, n, r) {
|
|
2299
|
-
let i = await
|
|
2317
|
+
let i = await jn(t.headers, e);
|
|
2300
2318
|
if (!i.ok) {
|
|
2301
2319
|
n.status(401).json({
|
|
2302
2320
|
error: "7h3: request verification failed",
|
|
@@ -2308,13 +2326,13 @@ function Mn(e) {
|
|
|
2308
2326
|
t["7h3Envelope"] = i.envelope, r();
|
|
2309
2327
|
};
|
|
2310
2328
|
}
|
|
2311
|
-
async function
|
|
2312
|
-
let { headers: i } = await
|
|
2329
|
+
async function Fn(e, t, n, r) {
|
|
2330
|
+
let { headers: i } = await Mn(t, n, r), a = new Headers(e.headers);
|
|
2313
2331
|
for (let [e, t] of Object.entries(i)) a.set(e, t);
|
|
2314
2332
|
return new Request(e, { headers: a });
|
|
2315
2333
|
}
|
|
2316
|
-
async function
|
|
2317
|
-
return
|
|
2334
|
+
async function In(e, t) {
|
|
2335
|
+
return Fn(e, D({
|
|
2318
2336
|
sender: t.sender,
|
|
2319
2337
|
recipient: t.recipient,
|
|
2320
2338
|
ttlMs: t.ttlMs ?? 6e4,
|
|
@@ -2324,7 +2342,7 @@ async function Pn(e, t) {
|
|
|
2324
2342
|
}
|
|
2325
2343
|
//#endregion
|
|
2326
2344
|
//#region src/webhookBinding.ts
|
|
2327
|
-
var R = "x-7h3-sig", z = "x-7h3-ts",
|
|
2345
|
+
var R = "x-7h3-sig", z = "x-7h3-ts", Ln = 3e5, Rn = class {
|
|
2328
2346
|
seen = /* @__PURE__ */ new Map();
|
|
2329
2347
|
maxEntries;
|
|
2330
2348
|
constructor(e = 1e4) {
|
|
@@ -2347,21 +2365,21 @@ var R = "x-7h3-sig", z = "x-7h3-ts", Fn = 3e5, In = class {
|
|
|
2347
2365
|
function B(e, t) {
|
|
2348
2366
|
return `${e}.${t}`;
|
|
2349
2367
|
}
|
|
2350
|
-
async function
|
|
2368
|
+
async function zn(e, t) {
|
|
2351
2369
|
let n = typeof e == "string" ? e : new TextDecoder().decode(e), r = Date.now(), i = await b(B(r, n), t.privateKey);
|
|
2352
2370
|
return {
|
|
2353
2371
|
[R]: i,
|
|
2354
2372
|
[z]: String(r)
|
|
2355
2373
|
};
|
|
2356
2374
|
}
|
|
2357
|
-
async function
|
|
2375
|
+
async function Bn(e, t) {
|
|
2358
2376
|
let n = typeof e == "string" ? e : new TextDecoder().decode(e), r = Date.now(), i = await de(B(r, n), t.secret);
|
|
2359
2377
|
return {
|
|
2360
2378
|
[R]: i,
|
|
2361
2379
|
[z]: String(r)
|
|
2362
2380
|
};
|
|
2363
2381
|
}
|
|
2364
|
-
async function
|
|
2382
|
+
async function Vn(e, t, n) {
|
|
2365
2383
|
let r = typeof e == "string" ? e : new TextDecoder().decode(e), i = t[R], a = t[z];
|
|
2366
2384
|
if (!i || !a) return !1;
|
|
2367
2385
|
let o = Number(a);
|
|
@@ -2369,7 +2387,7 @@ async function zn(e, t, n) {
|
|
|
2369
2387
|
let s = n.maxAgeMs ?? 3e5, c = Date.now();
|
|
2370
2388
|
return !(c - o > s || !await x(B(o, r), i, n.publicKey) || n.replayCache && !await n.replayCache.consume(i, o + s, c));
|
|
2371
2389
|
}
|
|
2372
|
-
async function
|
|
2390
|
+
async function Hn(e, t, n) {
|
|
2373
2391
|
let r = typeof e == "string" ? e : new TextDecoder().decode(e), i = t[R], a = t[z];
|
|
2374
2392
|
if (!i || !a) return !1;
|
|
2375
2393
|
let o = Number(a);
|
|
@@ -2377,13 +2395,13 @@ async function Bn(e, t, n) {
|
|
|
2377
2395
|
let s = n.maxAgeMs ?? 3e5, c = Date.now();
|
|
2378
2396
|
return !(c - o > s || !await fe(B(o, r), i, n.secret) || n.replayCache && !await n.replayCache.consume(i, o + s, c));
|
|
2379
2397
|
}
|
|
2380
|
-
async function
|
|
2381
|
-
if (!await
|
|
2398
|
+
async function Un(e, t, n) {
|
|
2399
|
+
if (!await Vn(e, t, n)) throw Error("7h3: webhook signature verification failed");
|
|
2382
2400
|
return JSON.parse(e);
|
|
2383
2401
|
}
|
|
2384
2402
|
//#endregion
|
|
2385
2403
|
//#region src/queueBinding.ts
|
|
2386
|
-
async function
|
|
2404
|
+
async function Wn(e, t) {
|
|
2387
2405
|
let n = t.ttlMs ?? 36e5, r = typeof e == "string" ? e : JSON.stringify(e), i = {
|
|
2388
2406
|
envelope: await w(D({
|
|
2389
2407
|
sender: t.sender,
|
|
@@ -2396,7 +2414,7 @@ async function Hn(e, t) {
|
|
|
2396
2414
|
};
|
|
2397
2415
|
return JSON.stringify(i);
|
|
2398
2416
|
}
|
|
2399
|
-
async function
|
|
2417
|
+
async function Gn(e, t) {
|
|
2400
2418
|
let n;
|
|
2401
2419
|
try {
|
|
2402
2420
|
n = JSON.parse(e);
|
|
@@ -2414,15 +2432,16 @@ async function Un(e, t) {
|
|
|
2414
2432
|
let e = await t.replayCache.consume(n.envelope, r);
|
|
2415
2433
|
if (!e.ok) throw Error(e.reason ?? "Queue message replay detected");
|
|
2416
2434
|
}
|
|
2435
|
+
if ((typeof n.payload == "string" ? n.payload : JSON.stringify(n.payload)) !== n.envelope.body.content) throw Error("Queue message payload does not match the signed content — the payload field was modified in transit");
|
|
2417
2436
|
return {
|
|
2418
2437
|
payload: n.payload,
|
|
2419
2438
|
envelope: n.envelope
|
|
2420
2439
|
};
|
|
2421
2440
|
}
|
|
2422
|
-
async function
|
|
2441
|
+
async function Kn(e, t) {
|
|
2423
2442
|
return Promise.all(e.map(async (e) => {
|
|
2424
2443
|
try {
|
|
2425
|
-
let { payload: n, envelope: r } = await
|
|
2444
|
+
let { payload: n, envelope: r } = await Gn(e, t);
|
|
2426
2445
|
return {
|
|
2427
2446
|
ok: !0,
|
|
2428
2447
|
payload: n,
|
|
@@ -2439,20 +2458,20 @@ async function Wn(e, t) {
|
|
|
2439
2458
|
}
|
|
2440
2459
|
//#endregion
|
|
2441
2460
|
//#region src/stream.ts
|
|
2442
|
-
var
|
|
2461
|
+
var qn = "x-7h3-stream", V = new TextEncoder();
|
|
2443
2462
|
function H() {
|
|
2444
2463
|
if (typeof crypto > "u" || !crypto.subtle) throw Error("Web Crypto API is not available in this runtime");
|
|
2445
2464
|
return crypto.subtle;
|
|
2446
2465
|
}
|
|
2447
|
-
function
|
|
2466
|
+
function Jn() {
|
|
2448
2467
|
return globalThis.Buffer;
|
|
2449
2468
|
}
|
|
2450
2469
|
function U(e) {
|
|
2451
|
-
let t =
|
|
2470
|
+
let t = Jn();
|
|
2452
2471
|
return (t ? t.from(e).toString("base64") : btoa(String.fromCharCode(...e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
2453
2472
|
}
|
|
2454
2473
|
function W(e) {
|
|
2455
|
-
let t = e.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(e.length / 4) * 4, "="), n =
|
|
2474
|
+
let t = e.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(e.length / 4) * 4, "="), n = Jn();
|
|
2456
2475
|
if (n) return new Uint8Array(n.from(t, "base64"));
|
|
2457
2476
|
let r = atob(t), i = new Uint8Array(r.length);
|
|
2458
2477
|
for (let e = 0; e < r.length; e++) i[e] = r.charCodeAt(e);
|
|
@@ -2461,15 +2480,15 @@ function W(e) {
|
|
|
2461
2480
|
function G(e) {
|
|
2462
2481
|
return e.buffer.slice(e.byteOffset, e.byteOffset + e.byteLength);
|
|
2463
2482
|
}
|
|
2464
|
-
function
|
|
2483
|
+
function Yn() {
|
|
2465
2484
|
let e = /* @__PURE__ */ new Uint8Array(16);
|
|
2466
2485
|
return crypto.getRandomValues(e), U(e);
|
|
2467
2486
|
}
|
|
2468
|
-
var K = /* @__PURE__ */ new Map(),
|
|
2469
|
-
function
|
|
2487
|
+
var K = /* @__PURE__ */ new Map(), Xn = 128;
|
|
2488
|
+
function Zn(e, t) {
|
|
2470
2489
|
let n = `${e}:${t}`, r = K.get(n);
|
|
2471
2490
|
if (r) return r;
|
|
2472
|
-
K.size >=
|
|
2491
|
+
K.size >= Xn && K.clear();
|
|
2473
2492
|
let i = H(), a = W(e), o = (async () => {
|
|
2474
2493
|
let e = await i.importKey("raw", G(a), "HKDF", !1, ["deriveBits"]), n = await i.deriveBits({
|
|
2475
2494
|
name: "HKDF",
|
|
@@ -2484,42 +2503,42 @@ function Yn(e, t) {
|
|
|
2484
2503
|
})();
|
|
2485
2504
|
return o.catch(() => K.delete(n)), K.set(n, o), o;
|
|
2486
2505
|
}
|
|
2487
|
-
var q = /* @__PURE__ */ new Map(),
|
|
2488
|
-
function
|
|
2506
|
+
var q = /* @__PURE__ */ new Map(), Qn = 64;
|
|
2507
|
+
function $n(e) {
|
|
2489
2508
|
let t = q.get(e);
|
|
2490
2509
|
if (t) return t;
|
|
2491
|
-
q.size >=
|
|
2510
|
+
q.size >= Qn && q.clear();
|
|
2492
2511
|
let n = H().importKey("pkcs8", G(W(e)), { name: "Ed25519" }, !1, ["sign"]).catch((t) => {
|
|
2493
2512
|
throw q.delete(e), t;
|
|
2494
2513
|
});
|
|
2495
2514
|
return q.set(e, n), n;
|
|
2496
2515
|
}
|
|
2497
|
-
var J = /* @__PURE__ */ new Map(),
|
|
2498
|
-
function
|
|
2516
|
+
var J = /* @__PURE__ */ new Map(), er = 64;
|
|
2517
|
+
function tr(e) {
|
|
2499
2518
|
let t = J.get(e);
|
|
2500
2519
|
if (t) return t;
|
|
2501
|
-
J.size >=
|
|
2520
|
+
J.size >= er && J.clear();
|
|
2502
2521
|
let n = H().importKey("spki", G(W(e)), { name: "Ed25519" }, !1, ["verify"]).catch((t) => {
|
|
2503
2522
|
throw J.delete(e), t;
|
|
2504
2523
|
});
|
|
2505
2524
|
return J.set(e, n), n;
|
|
2506
2525
|
}
|
|
2507
|
-
function
|
|
2526
|
+
function nr(e) {
|
|
2508
2527
|
let t = e.map((e) => `${e.i}:${e.d}`);
|
|
2509
2528
|
return V.encode(t.join("\n"));
|
|
2510
2529
|
}
|
|
2511
|
-
async function
|
|
2530
|
+
async function rr(e) {
|
|
2512
2531
|
let t = await H().digest("SHA-256", G(e));
|
|
2513
2532
|
return new Uint8Array(t);
|
|
2514
2533
|
}
|
|
2515
|
-
var
|
|
2534
|
+
var ir = class {
|
|
2516
2535
|
opts;
|
|
2517
2536
|
seq = 0;
|
|
2518
2537
|
accum = [];
|
|
2519
2538
|
hmacKeyPromise = null;
|
|
2520
2539
|
constructor(e) {
|
|
2521
2540
|
this.opts = {
|
|
2522
|
-
nonce: e.nonce ??
|
|
2541
|
+
nonce: e.nonce ?? Yn(),
|
|
2523
2542
|
keyId: e.keyId ?? "stream-key",
|
|
2524
2543
|
...e
|
|
2525
2544
|
};
|
|
@@ -2528,7 +2547,7 @@ var nr = class {
|
|
|
2528
2547
|
return this.opts.nonce;
|
|
2529
2548
|
}
|
|
2530
2549
|
getHmacKey() {
|
|
2531
|
-
return this.hmacKeyPromise ||=
|
|
2550
|
+
return this.hmacKeyPromise ||= Zn(this.opts.privateKey, this.opts.nonce), this.hmacKeyPromise;
|
|
2532
2551
|
}
|
|
2533
2552
|
async computeChunkHmac(e, t) {
|
|
2534
2553
|
let n = H(), r = await this.getHmacKey(), i = `${this.opts.nonce}:${e}:${t}`, a = await n.sign("HMAC", r, V.encode(i));
|
|
@@ -2547,7 +2566,7 @@ var nr = class {
|
|
|
2547
2566
|
};
|
|
2548
2567
|
}
|
|
2549
2568
|
async finalize() {
|
|
2550
|
-
let e = H(), t = await
|
|
2569
|
+
let e = H(), t = await $n(this.opts.privateKey), n = await rr(nr(this.accum)), r = await e.sign("Ed25519", t, G(n)), i = U(new Uint8Array(r)), a = this.seq;
|
|
2551
2570
|
return {
|
|
2552
2571
|
i: a,
|
|
2553
2572
|
d: "",
|
|
@@ -2557,7 +2576,7 @@ var nr = class {
|
|
|
2557
2576
|
kid: this.opts.keyId
|
|
2558
2577
|
};
|
|
2559
2578
|
}
|
|
2560
|
-
},
|
|
2579
|
+
}, ar = class {
|
|
2561
2580
|
opts;
|
|
2562
2581
|
expectedSeq = 0;
|
|
2563
2582
|
accum = [];
|
|
@@ -2594,14 +2613,14 @@ var nr = class {
|
|
|
2594
2613
|
};
|
|
2595
2614
|
let t = H(), n;
|
|
2596
2615
|
try {
|
|
2597
|
-
n = await
|
|
2616
|
+
n = await tr(this.opts.publicKey);
|
|
2598
2617
|
} catch {
|
|
2599
2618
|
return {
|
|
2600
2619
|
ok: !1,
|
|
2601
2620
|
reason: "invalid public key"
|
|
2602
2621
|
};
|
|
2603
2622
|
}
|
|
2604
|
-
let r = await
|
|
2623
|
+
let r = await rr(nr(this.accum)), i = W(e.sig);
|
|
2605
2624
|
return await t.verify("Ed25519", n, G(i), G(r)) ? {
|
|
2606
2625
|
ok: !0,
|
|
2607
2626
|
totalBytes: this.totalBytes,
|
|
@@ -2612,20 +2631,20 @@ var nr = class {
|
|
|
2612
2631
|
};
|
|
2613
2632
|
}
|
|
2614
2633
|
};
|
|
2615
|
-
function
|
|
2616
|
-
return new
|
|
2634
|
+
function or(e) {
|
|
2635
|
+
return new ir(e);
|
|
2617
2636
|
}
|
|
2618
|
-
function
|
|
2619
|
-
return new
|
|
2637
|
+
function sr(e) {
|
|
2638
|
+
return new ar(e);
|
|
2620
2639
|
}
|
|
2621
|
-
async function
|
|
2622
|
-
let n = new
|
|
2640
|
+
async function cr(e, t) {
|
|
2641
|
+
let n = new ir(t), r = [];
|
|
2623
2642
|
if (Array.isArray(e)) for (let t of e) r.push(await n.writeChunk(t));
|
|
2624
2643
|
else for await (let t of e) r.push(await n.writeChunk(t));
|
|
2625
2644
|
return r.push(await n.finalize()), r;
|
|
2626
2645
|
}
|
|
2627
|
-
async function
|
|
2628
|
-
let n = new
|
|
2646
|
+
async function lr(e, t) {
|
|
2647
|
+
let n = new ar(t), r = e.slice(0, -1), i = e[e.length - 1];
|
|
2629
2648
|
if (e.length === 0) return {
|
|
2630
2649
|
ok: !1,
|
|
2631
2650
|
reason: "empty chunk array — missing final frame"
|
|
@@ -2651,10 +2670,10 @@ async function sr(e, t) {
|
|
|
2651
2670
|
}
|
|
2652
2671
|
return n.finalize(i);
|
|
2653
2672
|
}
|
|
2654
|
-
function
|
|
2673
|
+
function ur(e) {
|
|
2655
2674
|
return JSON.stringify(e);
|
|
2656
2675
|
}
|
|
2657
|
-
function
|
|
2676
|
+
function dr(e) {
|
|
2658
2677
|
let t;
|
|
2659
2678
|
try {
|
|
2660
2679
|
t = JSON.parse(e);
|
|
@@ -2681,7 +2700,7 @@ function lr(e) {
|
|
|
2681
2700
|
}
|
|
2682
2701
|
//#endregion
|
|
2683
2702
|
//#region src/wsBinding.ts
|
|
2684
|
-
function
|
|
2703
|
+
function fr(e, t) {
|
|
2685
2704
|
let n = 0, r = [], i = [];
|
|
2686
2705
|
function a(e, n) {
|
|
2687
2706
|
t.onVerifyFail?.(e, n);
|
|
@@ -2752,28 +2771,28 @@ function ur(e, t) {
|
|
|
2752
2771
|
}
|
|
2753
2772
|
};
|
|
2754
2773
|
}
|
|
2755
|
-
function
|
|
2756
|
-
let n = new
|
|
2774
|
+
function pr(e, t) {
|
|
2775
|
+
let n = new ir(t), r = async (t) => {
|
|
2757
2776
|
let r = typeof t.data == "string" ? t.data : t.data.toString("utf8"), i = await n.writeChunk(r);
|
|
2758
|
-
e.send(
|
|
2777
|
+
e.send(ur(i));
|
|
2759
2778
|
}, i = async () => {
|
|
2760
2779
|
e.removeEventListener("message", r), e.removeEventListener("close", i);
|
|
2761
2780
|
let t = await n.finalize();
|
|
2762
|
-
e.send(
|
|
2781
|
+
e.send(ur(t));
|
|
2763
2782
|
};
|
|
2764
2783
|
return e.addEventListener("message", r), e.addEventListener("close", i), n;
|
|
2765
2784
|
}
|
|
2766
|
-
function
|
|
2785
|
+
function mr(e, t) {
|
|
2767
2786
|
return new Promise((n, r) => {
|
|
2768
2787
|
let i = [], a = async (e) => {
|
|
2769
2788
|
let a = typeof e.data == "string" ? e.data : e.data.toString("utf8"), o;
|
|
2770
2789
|
try {
|
|
2771
|
-
o =
|
|
2790
|
+
o = dr(a);
|
|
2772
2791
|
} catch (e) {
|
|
2773
2792
|
s(), r(e);
|
|
2774
2793
|
return;
|
|
2775
2794
|
}
|
|
2776
|
-
i.push(o), o.f && (s(), n(await
|
|
2795
|
+
i.push(o), o.f && (s(), n(await lr(i, t)));
|
|
2777
2796
|
}, o = () => {
|
|
2778
2797
|
s(), n({
|
|
2779
2798
|
ok: !1,
|
|
@@ -2788,8 +2807,8 @@ function fr(e, t) {
|
|
|
2788
2807
|
}
|
|
2789
2808
|
//#endregion
|
|
2790
2809
|
//#region src/grpcBinding.ts
|
|
2791
|
-
var
|
|
2792
|
-
async function
|
|
2810
|
+
var hr = "7h3-envelope-bin";
|
|
2811
|
+
async function gr(e) {
|
|
2793
2812
|
let t = await w(D({
|
|
2794
2813
|
sender: e.sender,
|
|
2795
2814
|
recipient: e.recipient,
|
|
@@ -2799,7 +2818,7 @@ async function mr(e) {
|
|
|
2799
2818
|
}), e.privateKey);
|
|
2800
2819
|
return { [e.metadataKey ?? "7h3-envelope-bin"]: JSON.stringify(t) };
|
|
2801
2820
|
}
|
|
2802
|
-
async function
|
|
2821
|
+
async function _r(e, t) {
|
|
2803
2822
|
let n = e[t.metadataKey ?? "7h3-envelope-bin"], r = Array.isArray(n) ? n[0] : n instanceof Uint8Array ? new TextDecoder().decode(n) : n;
|
|
2804
2823
|
if (!r) return {
|
|
2805
2824
|
ok: !1,
|
|
@@ -2861,39 +2880,39 @@ async function hr(e, t) {
|
|
|
2861
2880
|
envelope: i
|
|
2862
2881
|
};
|
|
2863
2882
|
}
|
|
2864
|
-
function
|
|
2883
|
+
function vr(e, t) {
|
|
2865
2884
|
return async (n) => {
|
|
2866
|
-
let r = await
|
|
2885
|
+
let r = await _r(n.metadata, t);
|
|
2867
2886
|
if (!r.ok) throw Object.assign(Error(r.message), { code: r.code });
|
|
2868
2887
|
return n["7h3Envelope"] = r.envelope, e(n);
|
|
2869
2888
|
};
|
|
2870
2889
|
}
|
|
2871
2890
|
//#endregion
|
|
2872
2891
|
//#region src/keyInfra.ts
|
|
2873
|
-
function
|
|
2892
|
+
function yr(e) {
|
|
2874
2893
|
return JSON.stringify(e);
|
|
2875
2894
|
}
|
|
2876
|
-
function
|
|
2895
|
+
function br(e) {
|
|
2877
2896
|
let t = JSON.parse(e);
|
|
2878
2897
|
if (t.version !== "7h3/0.1") throw Error(`unsupported version: ${t.version}`);
|
|
2879
2898
|
return t;
|
|
2880
2899
|
}
|
|
2881
|
-
async function
|
|
2900
|
+
async function xr(e, t) {
|
|
2882
2901
|
let n = e.replace(/\/$/, "") + "/.well-known/7h3-keys", r = new AbortController(), i = t?.timeout ? setTimeout(() => r.abort(), t.timeout) : void 0;
|
|
2883
2902
|
try {
|
|
2884
2903
|
let e = await fetch(n, { signal: r.signal });
|
|
2885
2904
|
if (!e.ok) throw Error(`HTTP ${e.status} from ${n}`);
|
|
2886
|
-
return
|
|
2905
|
+
return br(await e.text());
|
|
2887
2906
|
} finally {
|
|
2888
2907
|
i !== void 0 && clearTimeout(i);
|
|
2889
2908
|
}
|
|
2890
2909
|
}
|
|
2891
|
-
function
|
|
2910
|
+
function Sr(e) {
|
|
2892
2911
|
let t = /* @__PURE__ */ new Map(), n = e?.cacheMs ?? 6e4, r = e?.timeout ?? 5e3;
|
|
2893
2912
|
async function i(e) {
|
|
2894
2913
|
let i = Date.now(), a = t.get(e);
|
|
2895
2914
|
if (a && a.expiresAt > i) return a.doc;
|
|
2896
|
-
let o = await
|
|
2915
|
+
let o = await xr(e, { timeout: r });
|
|
2897
2916
|
return t.set(e, {
|
|
2898
2917
|
doc: o,
|
|
2899
2918
|
expiresAt: i + n
|
|
@@ -2910,7 +2929,7 @@ function br(e) {
|
|
|
2910
2929
|
}
|
|
2911
2930
|
} };
|
|
2912
2931
|
}
|
|
2913
|
-
var
|
|
2932
|
+
var Cr = class {
|
|
2914
2933
|
maxAgeMs;
|
|
2915
2934
|
overlapMs;
|
|
2916
2935
|
keys = [];
|
|
@@ -2960,7 +2979,7 @@ var xr = class {
|
|
|
2960
2979
|
return this.keys.find((n) => n.id === e && (!n.expiresAt || n.expiresAt > t))?.publicKey ?? null;
|
|
2961
2980
|
} };
|
|
2962
2981
|
}
|
|
2963
|
-
},
|
|
2982
|
+
}, wr = class {
|
|
2964
2983
|
revoked = /* @__PURE__ */ new Map();
|
|
2965
2984
|
revoke(e, t) {
|
|
2966
2985
|
this.revoked.set(e, {
|
|
@@ -2988,7 +3007,7 @@ var xr = class {
|
|
|
2988
3007
|
getSharedSecret: e.getSharedSecret ? async (t, n) => this.isRevoked(t) || this.isRevoked(n) ? null : e.getSharedSecret(t, n) : void 0
|
|
2989
3008
|
};
|
|
2990
3009
|
}
|
|
2991
|
-
},
|
|
3010
|
+
}, Tr = class {
|
|
2992
3011
|
windows = /* @__PURE__ */ new Map();
|
|
2993
3012
|
maxKeys;
|
|
2994
3013
|
constructor(e = {}) {
|
|
@@ -3022,10 +3041,10 @@ var xr = class {
|
|
|
3022
3041
|
clear() {
|
|
3023
3042
|
this.windows.clear();
|
|
3024
3043
|
}
|
|
3025
|
-
},
|
|
3044
|
+
}, Er = new Tr();
|
|
3026
3045
|
//#endregion
|
|
3027
3046
|
//#region src/routePolicy.ts
|
|
3028
|
-
function
|
|
3047
|
+
function Dr(e, t) {
|
|
3029
3048
|
let n = "^", r = 0;
|
|
3030
3049
|
for (; r < e.length;) {
|
|
3031
3050
|
let t = e[r];
|
|
@@ -3033,17 +3052,17 @@ function Tr(e, t) {
|
|
|
3033
3052
|
}
|
|
3034
3053
|
return n += "$", new RegExp(n).test(t);
|
|
3035
3054
|
}
|
|
3036
|
-
function
|
|
3037
|
-
for (let n of e) if (
|
|
3055
|
+
function Or(e, t) {
|
|
3056
|
+
for (let n of e) if (Dr(n.path, t)) return n;
|
|
3038
3057
|
return null;
|
|
3039
3058
|
}
|
|
3040
|
-
function
|
|
3059
|
+
function kr(e, t) {
|
|
3041
3060
|
return !e.allowedSenders || e.allowedSenders.length === 0 || e.allowedSenders.includes(t);
|
|
3042
3061
|
}
|
|
3043
3062
|
//#endregion
|
|
3044
3063
|
//#region src/signedResponse.ts
|
|
3045
|
-
var
|
|
3046
|
-
async function
|
|
3064
|
+
var Ar = "x-7h3-response";
|
|
3065
|
+
async function jr(e, t) {
|
|
3047
3066
|
let n = await w(D({
|
|
3048
3067
|
sender: t.sender,
|
|
3049
3068
|
recipient: t.recipient,
|
|
@@ -3052,10 +3071,10 @@ async function kr(e, t) {
|
|
|
3052
3071
|
correlationId: t.correlationId,
|
|
3053
3072
|
ttlMs: t.ttlMs ?? 6e4
|
|
3054
3073
|
}), t.privateKey);
|
|
3055
|
-
return { headers: { [
|
|
3074
|
+
return { headers: { [Ar]: JSON.stringify(n) } };
|
|
3056
3075
|
}
|
|
3057
|
-
async function
|
|
3058
|
-
let r = t[
|
|
3076
|
+
async function Mr(e, t, n) {
|
|
3077
|
+
let r = t[Ar], i = Array.isArray(r) ? r[0] : r;
|
|
3059
3078
|
if (!i) return {
|
|
3060
3079
|
ok: !1,
|
|
3061
3080
|
reason: "missing-header"
|
|
@@ -3089,21 +3108,21 @@ async function Ar(e, t, n) {
|
|
|
3089
3108
|
}
|
|
3090
3109
|
//#endregion
|
|
3091
3110
|
//#region src/telemetry.ts
|
|
3092
|
-
function
|
|
3111
|
+
function Nr(e) {
|
|
3093
3112
|
return !e || Object.keys(e).length === 0 ? "__default__" : Object.entries(e).sort(([e], [t]) => e.localeCompare(t)).map(([e, t]) => `${e}=${t}`).join(",");
|
|
3094
3113
|
}
|
|
3095
|
-
function
|
|
3114
|
+
function Pr(e) {
|
|
3096
3115
|
return !e || Object.keys(e).length === 0 ? "" : `{${Object.entries(e).sort(([e], [t]) => e.localeCompare(t)).map(([e, t]) => `${e}="${t}"`).join(",")}}`;
|
|
3097
3116
|
}
|
|
3098
3117
|
var Y = class {
|
|
3099
3118
|
counts = /* @__PURE__ */ new Map();
|
|
3100
3119
|
labelSets = /* @__PURE__ */ new Map();
|
|
3101
3120
|
increment(e) {
|
|
3102
|
-
let t =
|
|
3121
|
+
let t = Nr(e);
|
|
3103
3122
|
this.counts.set(t, (this.counts.get(t) ?? 0) + 1), this.labelSets.has(t) || this.labelSets.set(t, e);
|
|
3104
3123
|
}
|
|
3105
3124
|
value(e) {
|
|
3106
|
-
return this.counts.get(
|
|
3125
|
+
return this.counts.get(Nr(e)) ?? 0;
|
|
3107
3126
|
}
|
|
3108
3127
|
entries() {
|
|
3109
3128
|
let e = [];
|
|
@@ -3113,7 +3132,7 @@ var Y = class {
|
|
|
3113
3132
|
});
|
|
3114
3133
|
return e;
|
|
3115
3134
|
}
|
|
3116
|
-
},
|
|
3135
|
+
}, Fr = class {
|
|
3117
3136
|
bucketBounds;
|
|
3118
3137
|
states = /* @__PURE__ */ new Map();
|
|
3119
3138
|
labelSets = /* @__PURE__ */ new Map();
|
|
@@ -3133,7 +3152,7 @@ var Y = class {
|
|
|
3133
3152
|
this.bucketBounds = [...e].sort((e, t) => e - t);
|
|
3134
3153
|
}
|
|
3135
3154
|
observe(e, t) {
|
|
3136
|
-
let n =
|
|
3155
|
+
let n = Nr(t);
|
|
3137
3156
|
this.states.has(n) || (this.states.set(n, {
|
|
3138
3157
|
count: 0,
|
|
3139
3158
|
sum: 0,
|
|
@@ -3143,7 +3162,7 @@ var Y = class {
|
|
|
3143
3162
|
r.count++, r.sum += e, r.observations.push(e);
|
|
3144
3163
|
}
|
|
3145
3164
|
snapshot(e) {
|
|
3146
|
-
let t =
|
|
3165
|
+
let t = Nr(e), n = this.states.get(t) ?? {
|
|
3147
3166
|
count: 0,
|
|
3148
3167
|
sum: 0,
|
|
3149
3168
|
observations: []
|
|
@@ -3169,9 +3188,9 @@ var Y = class {
|
|
|
3169
3188
|
getBounds() {
|
|
3170
3189
|
return [...this.bucketBounds];
|
|
3171
3190
|
}
|
|
3172
|
-
},
|
|
3191
|
+
}, Ir = class {
|
|
3173
3192
|
verifications_total = new Y();
|
|
3174
|
-
verification_duration_ms = new
|
|
3193
|
+
verification_duration_ms = new Fr([
|
|
3175
3194
|
.1,
|
|
3176
3195
|
.5,
|
|
3177
3196
|
1,
|
|
@@ -3185,15 +3204,15 @@ var Y = class {
|
|
|
3185
3204
|
replay_detections_total = new Y();
|
|
3186
3205
|
audit_entries_total = new Y();
|
|
3187
3206
|
active_connections = new Y();
|
|
3188
|
-
}, X = new
|
|
3189
|
-
function
|
|
3207
|
+
}, X = new Ir();
|
|
3208
|
+
function Lr(e, t = "7h3") {
|
|
3190
3209
|
let n = [];
|
|
3191
3210
|
function r(e, r, i) {
|
|
3192
3211
|
let a = `${t}_${e}`;
|
|
3193
3212
|
n.push(`# HELP ${a} ${r}`), n.push(`# TYPE ${a} counter`);
|
|
3194
3213
|
let o = i.entries();
|
|
3195
3214
|
if (o.length === 0) n.push(`${a} 0`);
|
|
3196
|
-
else for (let { labels: e, value: t } of o) e && Object.keys(e).length > 0 ? n.push(`${a}${
|
|
3215
|
+
else for (let { labels: e, value: t } of o) e && Object.keys(e).length > 0 ? n.push(`${a}${Pr(e)} ${t}`) : n.push(`${a} ${t}`);
|
|
3197
3216
|
}
|
|
3198
3217
|
function i(e, r, i) {
|
|
3199
3218
|
let a = `${t}_${e}`;
|
|
@@ -3203,7 +3222,7 @@ function Fr(e, t = "7h3") {
|
|
|
3203
3222
|
for (let e of i.getBounds()) n.push(`${a}_bucket{le="${e}"} 0`);
|
|
3204
3223
|
n.push(`${a}_bucket{le="+Inf"} 0`), n.push(`${a}_sum 0`), n.push(`${a}_count 0`);
|
|
3205
3224
|
} else for (let { labels: e, snapshot: t } of o) {
|
|
3206
|
-
let r = e && Object.keys(e).length > 0 ?
|
|
3225
|
+
let r = e && Object.keys(e).length > 0 ? Pr(e) : "";
|
|
3207
3226
|
for (let [e, i] of Object.entries(t.buckets)) {
|
|
3208
3227
|
if (e === "+Inf") continue;
|
|
3209
3228
|
let t = r ? r.slice(0, -1) + `,le="${e}"}` : `{le="${e}"}`;
|
|
@@ -3215,28 +3234,28 @@ function Fr(e, t = "7h3") {
|
|
|
3215
3234
|
}
|
|
3216
3235
|
return r("verifications_total", "Total number of AIP envelope verifications, by result, algorithm, and transport", e.verifications_total), i("verification_duration_ms", "Verification latency in milliseconds", e.verification_duration_ms), r("rate_limit_hits_total", "Total number of rate-limit rejections, by sender and path", e.rate_limit_hits_total), r("sender_denials_total", "Total number of sender-denied (403) rejections, by sender and path", e.sender_denials_total), r("replay_detections_total", "Total number of replay-detected events, by transport", e.replay_detections_total), r("audit_entries_total", "Total number of audit log entries written, by type", e.audit_entries_total), r("active_connections", "Current active connections, by transport (WS/gRPC)", e.active_connections), n.join("\n") + "\n";
|
|
3217
3236
|
}
|
|
3218
|
-
function
|
|
3237
|
+
function Rr(e = "/metrics", t = X, n = "7h3") {
|
|
3219
3238
|
return (r, i, a) => {
|
|
3220
3239
|
if (r.url === e && (r.method === "GET" || r.method === void 0)) {
|
|
3221
|
-
let e =
|
|
3240
|
+
let e = Lr(t, n);
|
|
3222
3241
|
i.writeHead(200, { "content-type": "text/plain; version=0.0.4; charset=utf-8" }), i.end(e);
|
|
3223
3242
|
} else a();
|
|
3224
3243
|
};
|
|
3225
3244
|
}
|
|
3226
3245
|
//#endregion
|
|
3227
3246
|
//#region src/capability.ts
|
|
3228
|
-
var
|
|
3229
|
-
function
|
|
3247
|
+
var zr = "x-7h3-capability";
|
|
3248
|
+
function Br(e) {
|
|
3230
3249
|
let t = [];
|
|
3231
3250
|
return e.maxDelegations !== void 0 && t.push(`"maxDelegations":${e.maxDelegations}`), e.methods !== void 0 && t.push(`"methods":${JSON.stringify(e.methods)}`), t.push(`"pathGlob":${JSON.stringify(e.pathGlob)}`), `{${t.join(",")}}`;
|
|
3232
3251
|
}
|
|
3233
3252
|
function Z(e) {
|
|
3234
3253
|
let t = [];
|
|
3235
3254
|
t.push(`"delegationDepth":${e.delegationDepth}`), t.push(`"expiresAt":${e.expiresAt}`), t.push(`"id":${JSON.stringify(e.id)}`), t.push(`"issuedAt":${e.issuedAt}`), t.push(`"issuer":${JSON.stringify(e.issuer)}`), t.push(`"keyId":${JSON.stringify(e.keyId)}`), e.maxDelegations !== void 0 && t.push(`"maxDelegations":${e.maxDelegations}`), e.parentTokenId !== void 0 && t.push(`"parentTokenId":${JSON.stringify(e.parentTokenId)}`);
|
|
3236
|
-
let n = `[${e.scopes.map(
|
|
3255
|
+
let n = `[${e.scopes.map(Br).join(",")}]`;
|
|
3237
3256
|
return t.push(`"scopes":${n}`), t.push(`"subject":${JSON.stringify(e.subject)}`), t.push(`"version":${JSON.stringify(e.version)}`), `{${t.join(",")}}`;
|
|
3238
3257
|
}
|
|
3239
|
-
async function
|
|
3258
|
+
async function Vr(e) {
|
|
3240
3259
|
let t = Date.now(), n = `cap-${t}-${y(6)}`, r = e.keyId ?? `${e.issuerId}-key`, i = e.maxDelegations ?? 0, a = {
|
|
3241
3260
|
id: n,
|
|
3242
3261
|
version: "7h3-cap/1",
|
|
@@ -3254,40 +3273,40 @@ async function zr(e) {
|
|
|
3254
3273
|
signature: o
|
|
3255
3274
|
};
|
|
3256
3275
|
}
|
|
3257
|
-
function
|
|
3276
|
+
function Hr(e, t) {
|
|
3258
3277
|
return e === t || t === "*" && e !== "**";
|
|
3259
3278
|
}
|
|
3260
|
-
function
|
|
3279
|
+
function Ur(e, t) {
|
|
3261
3280
|
let n = e.split("/"), r = t.split("/"), i = 0;
|
|
3262
3281
|
for (let e of r) {
|
|
3263
|
-
if (e === "**") return
|
|
3282
|
+
if (e === "**") return i < n.length;
|
|
3264
3283
|
if (i >= n.length) return !1;
|
|
3265
3284
|
let t = n[i];
|
|
3266
|
-
if (t === "**" || !
|
|
3285
|
+
if (t === "**" || !Hr(t, e)) return !1;
|
|
3267
3286
|
i += 1;
|
|
3268
3287
|
}
|
|
3269
3288
|
return i === n.length;
|
|
3270
3289
|
}
|
|
3271
|
-
function
|
|
3272
|
-
if (!
|
|
3290
|
+
function Wr(e, t) {
|
|
3291
|
+
if (!Ur(e.pathGlob, t.pathGlob)) return !1;
|
|
3273
3292
|
if (t.methods !== void 0) {
|
|
3274
3293
|
if (e.methods === void 0) return !1;
|
|
3275
3294
|
for (let n of e.methods) if (!t.methods.includes(n)) return !1;
|
|
3276
3295
|
}
|
|
3277
3296
|
return !0;
|
|
3278
3297
|
}
|
|
3279
|
-
function
|
|
3280
|
-
for (let n of e) if (!t.some((e) =>
|
|
3298
|
+
function Gr(e, t) {
|
|
3299
|
+
for (let n of e) if (!t.some((e) => Wr(n, e))) return !1;
|
|
3281
3300
|
return !0;
|
|
3282
3301
|
}
|
|
3283
|
-
async function
|
|
3302
|
+
async function Kr(e) {
|
|
3284
3303
|
let { parentToken: t, delegatorId: n, newSubject: r } = e;
|
|
3285
3304
|
if (n !== t.subject) throw Error(`delegatorId '${n}' does not match parentToken.subject '${t.subject}'`);
|
|
3286
3305
|
let i = Date.now();
|
|
3287
3306
|
if (i >= t.expiresAt) throw Error("Parent token is expired");
|
|
3288
3307
|
if (t.maxDelegations !== void 0 && t.maxDelegations <= 0) throw Error("Parent token does not allow further delegation (maxDelegations=0)");
|
|
3289
3308
|
let a = e.scopes ?? t.scopes;
|
|
3290
|
-
if (!
|
|
3309
|
+
if (!Gr(a, t.scopes)) throw Error("Delegated scopes exceed parent token scopes");
|
|
3291
3310
|
let o = t.expiresAt - i;
|
|
3292
3311
|
if (e.ttlMs > o) throw Error(`Delegated ttlMs (${e.ttlMs}) exceeds parent token remaining TTL (${o})`);
|
|
3293
3312
|
let s = `cap-${i}-${y(6)}`, c = e.keyId ?? `${n}-key`, l = t.maxDelegations === void 0 ? void 0 : t.maxDelegations - 1, u = {
|
|
@@ -3308,12 +3327,12 @@ async function Wr(e) {
|
|
|
3308
3327
|
signature: d
|
|
3309
3328
|
};
|
|
3310
3329
|
}
|
|
3311
|
-
async function
|
|
3330
|
+
async function qr(e, t, n) {
|
|
3312
3331
|
if ((n?.now ?? Date.now()) >= e.expiresAt) return !1;
|
|
3313
3332
|
let { signature: r, ...i } = e;
|
|
3314
3333
|
return x(Z(i), r, t);
|
|
3315
3334
|
}
|
|
3316
|
-
async function
|
|
3335
|
+
async function Jr(e, t, n) {
|
|
3317
3336
|
if (e.length === 0) return {
|
|
3318
3337
|
ok: !1,
|
|
3319
3338
|
reason: "empty-chain"
|
|
@@ -3347,7 +3366,7 @@ async function Kr(e, t, n) {
|
|
|
3347
3366
|
ok: !1,
|
|
3348
3367
|
reason: `chain-broken: token[${n}].expiresAt exceeds token[${n - 1}].expiresAt`
|
|
3349
3368
|
};
|
|
3350
|
-
if (!
|
|
3369
|
+
if (!Gr(i.scopes, t.scopes)) return {
|
|
3351
3370
|
ok: !1,
|
|
3352
3371
|
reason: `chain-broken: token[${n}].scopes exceed token[${n - 1}].scopes`
|
|
3353
3372
|
};
|
|
@@ -3368,7 +3387,7 @@ async function Kr(e, t, n) {
|
|
|
3368
3387
|
};
|
|
3369
3388
|
}
|
|
3370
3389
|
let i = e[e.length - 1];
|
|
3371
|
-
return n?.requiredPathGlob !== void 0 && !
|
|
3390
|
+
return n?.requiredPathGlob !== void 0 && !Yr(i, n.requiredPathGlob, n.requiredMethod) ? {
|
|
3372
3391
|
ok: !1,
|
|
3373
3392
|
reason: "leaf-token-does-not-cover-required-scope"
|
|
3374
3393
|
} : {
|
|
@@ -3377,19 +3396,19 @@ async function Kr(e, t, n) {
|
|
|
3377
3396
|
chain: e
|
|
3378
3397
|
};
|
|
3379
3398
|
}
|
|
3380
|
-
function
|
|
3381
|
-
for (let r of e.scopes) if (
|
|
3399
|
+
function Yr(e, t, n) {
|
|
3400
|
+
for (let r of e.scopes) if (Dr(r.pathGlob, t) && (r.methods === void 0 || n !== void 0 && r.methods.includes(n.toUpperCase()))) return !0;
|
|
3382
3401
|
return !1;
|
|
3383
3402
|
}
|
|
3384
|
-
function
|
|
3403
|
+
function Xr(e) {
|
|
3385
3404
|
return JSON.stringify(e);
|
|
3386
3405
|
}
|
|
3387
|
-
function
|
|
3406
|
+
function Zr(e) {
|
|
3388
3407
|
return JSON.parse(e);
|
|
3389
3408
|
}
|
|
3390
3409
|
//#endregion
|
|
3391
3410
|
//#region src/gateway.ts
|
|
3392
|
-
function
|
|
3411
|
+
function Qr(e) {
|
|
3393
3412
|
if (!e.startsWith("/")) return null;
|
|
3394
3413
|
let t = e;
|
|
3395
3414
|
for (let e = 0; e < 5; e++) {
|
|
@@ -3414,14 +3433,14 @@ function Xr(e) {
|
|
|
3414
3433
|
}
|
|
3415
3434
|
return "/" + r.join("/");
|
|
3416
3435
|
}
|
|
3417
|
-
var
|
|
3436
|
+
var $r = class {
|
|
3418
3437
|
config;
|
|
3419
3438
|
rateLimiter;
|
|
3420
3439
|
constructor(e) {
|
|
3421
|
-
this.config = e, this.rateLimiter = new
|
|
3440
|
+
this.config = e, this.rateLimiter = new Tr(), !e.replayStore && (e.policies ?? []).some((e) => e.require !== "none") && console.warn("[7h3-protocol] createGateway(): no replayStore configured with signature-requiring policies. Nonce/replay protection will not survive multiple gateway instances or restarts. See docs/GATEWAY.md#production-safety.");
|
|
3422
3441
|
}
|
|
3423
3442
|
async checkSenderAndRateLimit(e, t, n, r, i) {
|
|
3424
|
-
if (e && !
|
|
3443
|
+
if (e && !kr(e, t)) {
|
|
3425
3444
|
let e = performance.now() - i;
|
|
3426
3445
|
return X.verifications_total.increment({
|
|
3427
3446
|
result: "fail",
|
|
@@ -3454,13 +3473,13 @@ var Zr = class {
|
|
|
3454
3473
|
return null;
|
|
3455
3474
|
}
|
|
3456
3475
|
async verify(e) {
|
|
3457
|
-
let t = performance.now(), n =
|
|
3476
|
+
let t = performance.now(), n = Qr(e.path);
|
|
3458
3477
|
if (n === null) return {
|
|
3459
3478
|
ok: !1,
|
|
3460
3479
|
status: 400,
|
|
3461
3480
|
reason: "invalid-path"
|
|
3462
3481
|
};
|
|
3463
|
-
let r =
|
|
3482
|
+
let r = Or(this.config.policies ?? [], n);
|
|
3464
3483
|
if (r?.require === "none" || !r && (this.config.defaultPolicy ?? "allow") === "allow") {
|
|
3465
3484
|
let e = performance.now() - t;
|
|
3466
3485
|
return X.verifications_total.increment({
|
|
@@ -3473,13 +3492,13 @@ var Zr = class {
|
|
|
3473
3492
|
};
|
|
3474
3493
|
}
|
|
3475
3494
|
if (this.config.capabilityRegistry) {
|
|
3476
|
-
let n = e.headers[
|
|
3495
|
+
let n = e.headers[zr], i = Array.isArray(n) ? n[0] : n;
|
|
3477
3496
|
if (i) try {
|
|
3478
|
-
let n = await
|
|
3497
|
+
let n = await Jr(Zr(i), this.config.capabilityRegistry, {
|
|
3479
3498
|
requiredPathGlob: e.path,
|
|
3480
3499
|
requiredMethod: e.method
|
|
3481
3500
|
});
|
|
3482
|
-
if (n.ok &&
|
|
3501
|
+
if (n.ok && Yr(n.token, e.path, e.method)) {
|
|
3483
3502
|
let i = n.token.subject, a = await this.checkSenderAndRateLimit(r, i, "ED25519", e, t);
|
|
3484
3503
|
if (a) return a;
|
|
3485
3504
|
let o = performance.now() - t;
|
|
@@ -3530,7 +3549,7 @@ var Zr = class {
|
|
|
3530
3549
|
reason: "no-matching-policy"
|
|
3531
3550
|
};
|
|
3532
3551
|
}
|
|
3533
|
-
let i = await
|
|
3552
|
+
let i = await jn(e.headers, {
|
|
3534
3553
|
keyRegistry: this.config.keyRegistry,
|
|
3535
3554
|
headerName: this.config.headerName
|
|
3536
3555
|
});
|
|
@@ -3587,7 +3606,7 @@ var Zr = class {
|
|
|
3587
3606
|
headers: { "content-type": "application/json" },
|
|
3588
3607
|
body: JSON.stringify({ error: t.reason })
|
|
3589
3608
|
};
|
|
3590
|
-
let n =
|
|
3609
|
+
let n = Qr(e.path), r = this.config.upstream.replace(/\/$/, "") + n, i = {};
|
|
3591
3610
|
for (let [t, n] of Object.entries(e.headers)) i[t] = Array.isArray(n) ? n[0] : n;
|
|
3592
3611
|
t.sender && (i["x-7h3-sender"] = t.sender, i["x-7h3-verified"] = "true");
|
|
3593
3612
|
let a = await fetch(r, {
|
|
@@ -3596,7 +3615,7 @@ var Zr = class {
|
|
|
3596
3615
|
body: e.body
|
|
3597
3616
|
}), o = await a.text(), s = { "content-type": a.headers.get("content-type") ?? "application/json" };
|
|
3598
3617
|
if (this.config.privateKey !== void 0 && (this.config.signResponses ?? !0) && this.config.sender !== void 0) {
|
|
3599
|
-
let e = await
|
|
3618
|
+
let e = await jr(o, {
|
|
3600
3619
|
privateKey: this.config.privateKey,
|
|
3601
3620
|
sender: this.config.sender,
|
|
3602
3621
|
recipient: t.sender || void 0
|
|
@@ -3613,17 +3632,17 @@ var Zr = class {
|
|
|
3613
3632
|
return this.rateLimiter;
|
|
3614
3633
|
}
|
|
3615
3634
|
};
|
|
3616
|
-
function
|
|
3617
|
-
return new
|
|
3635
|
+
function ei(e) {
|
|
3636
|
+
return new $r(e);
|
|
3618
3637
|
}
|
|
3619
|
-
function
|
|
3638
|
+
function ti(e) {
|
|
3620
3639
|
if (e.defaultPolicy !== "deny") throw Error("createProductionGateway(): defaultPolicy must be explicitly 'deny'. Unmatched routes must never be forwarded unverified in production.");
|
|
3621
3640
|
if (!e.replayStore) throw Error("createProductionGateway(): replayStore is required. Use a shared store (Redis/KV/Durable Object) so nonce replay protection survives multiple instances and restarts.");
|
|
3622
|
-
return new
|
|
3641
|
+
return new $r(e);
|
|
3623
3642
|
}
|
|
3624
3643
|
//#endregion
|
|
3625
3644
|
//#region src/auditLog.ts
|
|
3626
|
-
function
|
|
3645
|
+
function ni(e) {
|
|
3627
3646
|
let t = [
|
|
3628
3647
|
`"id":${JSON.stringify(e.id)}`,
|
|
3629
3648
|
`"timestampMs":${e.timestampMs}`,
|
|
@@ -3631,7 +3650,7 @@ function ei(e) {
|
|
|
3631
3650
|
];
|
|
3632
3651
|
return e.sender !== void 0 && t.push(`"sender":${JSON.stringify(e.sender)}`), e.path !== void 0 && t.push(`"path":${JSON.stringify(e.path)}`), e.method !== void 0 && t.push(`"method":${JSON.stringify(e.method)}`), e.envelopeId !== void 0 && t.push(`"envelopeId":${JSON.stringify(e.envelopeId)}`), e.failReason !== void 0 && t.push(`"failReason":${JSON.stringify(e.failReason)}`), e.upstream !== void 0 && t.push(`"upstream":${JSON.stringify(e.upstream)}`), e.responseStatus !== void 0 && t.push(`"responseStatus":${e.responseStatus}`), `{${t.join(",")}}`;
|
|
3633
3652
|
}
|
|
3634
|
-
var
|
|
3653
|
+
var ri = class {
|
|
3635
3654
|
entries = [];
|
|
3636
3655
|
privateKey;
|
|
3637
3656
|
maxEntries;
|
|
@@ -3643,7 +3662,7 @@ var ti = class {
|
|
|
3643
3662
|
id: `audit-${Date.now()}-${y(5)}`,
|
|
3644
3663
|
timestampMs: Date.now(),
|
|
3645
3664
|
...e
|
|
3646
|
-
}, n = await b(
|
|
3665
|
+
}, n = await b(ni(t), this.privateKey), r = {
|
|
3647
3666
|
...t,
|
|
3648
3667
|
entrySignature: n
|
|
3649
3668
|
};
|
|
@@ -3654,7 +3673,7 @@ var ti = class {
|
|
|
3654
3673
|
return e?.type !== void 0 && (t = t.filter((t) => t.type === e.type)), e?.sender !== void 0 && (t = t.filter((t) => t.sender === e.sender)), e?.since !== void 0 && (t = t.filter((t) => t.timestampMs >= e.since)), e?.limit !== void 0 && (t = t.slice(-e.limit)), t;
|
|
3655
3674
|
}
|
|
3656
3675
|
async verify(e, t) {
|
|
3657
|
-
return x(
|
|
3676
|
+
return x(ni({
|
|
3658
3677
|
id: e.id,
|
|
3659
3678
|
timestampMs: e.timestampMs,
|
|
3660
3679
|
type: e.type,
|
|
@@ -3670,7 +3689,7 @@ var ti = class {
|
|
|
3670
3689
|
size() {
|
|
3671
3690
|
return this.entries.length;
|
|
3672
3691
|
}
|
|
3673
|
-
},
|
|
3692
|
+
}, ii = class {
|
|
3674
3693
|
async log(e) {}
|
|
3675
3694
|
async query(e) {
|
|
3676
3695
|
return [];
|
|
@@ -3682,25 +3701,25 @@ var ti = class {
|
|
|
3682
3701
|
return 0;
|
|
3683
3702
|
}
|
|
3684
3703
|
};
|
|
3685
|
-
function
|
|
3686
|
-
return new
|
|
3704
|
+
function ai(e, t) {
|
|
3705
|
+
return new ri(e, t?.maxEntries);
|
|
3687
3706
|
}
|
|
3688
3707
|
//#endregion
|
|
3689
3708
|
//#region src/otel.ts
|
|
3690
|
-
var
|
|
3691
|
-
function
|
|
3692
|
-
|
|
3709
|
+
var oi = null;
|
|
3710
|
+
function si(e) {
|
|
3711
|
+
oi = e;
|
|
3693
3712
|
}
|
|
3694
|
-
function
|
|
3695
|
-
if (
|
|
3713
|
+
function ci() {
|
|
3714
|
+
if (oi === null) return null;
|
|
3696
3715
|
try {
|
|
3697
|
-
return
|
|
3716
|
+
return oi.getTracer("7h3/protocol");
|
|
3698
3717
|
} catch {
|
|
3699
3718
|
return null;
|
|
3700
3719
|
}
|
|
3701
3720
|
}
|
|
3702
|
-
async function
|
|
3703
|
-
let n =
|
|
3721
|
+
async function li(e, t) {
|
|
3722
|
+
let n = ci();
|
|
3704
3723
|
if (n === null) return e(null);
|
|
3705
3724
|
let r = n.startSpan("7h3.verify", t);
|
|
3706
3725
|
try {
|
|
@@ -3711,8 +3730,8 @@ async function si(e, t) {
|
|
|
3711
3730
|
r.end();
|
|
3712
3731
|
}
|
|
3713
3732
|
}
|
|
3714
|
-
async function
|
|
3715
|
-
let t =
|
|
3733
|
+
async function ui(e) {
|
|
3734
|
+
let t = ci();
|
|
3716
3735
|
if (t === null) return e(null);
|
|
3717
3736
|
let n = t.startSpan("7h3.audit.write");
|
|
3718
3737
|
try {
|
|
@@ -3731,39 +3750,39 @@ function Q(e) {
|
|
|
3731
3750
|
function $(e) {
|
|
3732
3751
|
return Buffer.from(e, "base64url");
|
|
3733
3752
|
}
|
|
3734
|
-
var
|
|
3735
|
-
function
|
|
3753
|
+
var di = Buffer.from("302e020100300506032b656e04220420", "hex"), fi = Buffer.from("302a300506032b656e032100", "hex");
|
|
3754
|
+
function pi(e) {
|
|
3736
3755
|
let t = $(e);
|
|
3737
3756
|
return r({
|
|
3738
|
-
key: Buffer.concat([
|
|
3757
|
+
key: Buffer.concat([di, t]),
|
|
3739
3758
|
format: "der",
|
|
3740
3759
|
type: "pkcs8"
|
|
3741
3760
|
});
|
|
3742
3761
|
}
|
|
3743
|
-
function
|
|
3762
|
+
function mi(e) {
|
|
3744
3763
|
let t = $(e);
|
|
3745
3764
|
return i({
|
|
3746
|
-
key: Buffer.concat([
|
|
3765
|
+
key: Buffer.concat([fi, t]),
|
|
3747
3766
|
format: "der",
|
|
3748
3767
|
type: "spki"
|
|
3749
3768
|
});
|
|
3750
3769
|
}
|
|
3751
|
-
function
|
|
3770
|
+
function hi() {
|
|
3752
3771
|
let { privateKey: e, publicKey: t } = o("x25519"), n = e.export({ format: "jwk" });
|
|
3753
3772
|
return {
|
|
3754
3773
|
publicKey: t.export({ format: "jwk" }).x,
|
|
3755
3774
|
privateKey: n.d
|
|
3756
3775
|
};
|
|
3757
3776
|
}
|
|
3758
|
-
function
|
|
3777
|
+
function gi(e, t, n) {
|
|
3759
3778
|
let r = s("sha256", a({
|
|
3760
|
-
privateKey:
|
|
3761
|
-
publicKey:
|
|
3779
|
+
privateKey: pi(e),
|
|
3780
|
+
publicKey: mi(t)
|
|
3762
3781
|
}), $(n), Buffer.from("7h3-enc/1", "utf8"), 32);
|
|
3763
3782
|
return Buffer.from(r);
|
|
3764
3783
|
}
|
|
3765
|
-
function
|
|
3766
|
-
let r =
|
|
3784
|
+
function _i(e, n) {
|
|
3785
|
+
let r = hi(), i = c(12), a = Q(i), o = gi(r.privateKey, n, a), s = Buffer.from(JSON.stringify(e), "utf8"), l = t("chacha20-poly1305", o, i, { authTagLength: 16 }), u = Buffer.concat([l.update(s), l.final()]), d = l.getAuthTag(), f = {
|
|
3767
3786
|
ephemeralPublic: r.publicKey,
|
|
3768
3787
|
nonce: a,
|
|
3769
3788
|
ciphertext: Q(u),
|
|
@@ -3774,14 +3793,14 @@ function hi(e, n) {
|
|
|
3774
3793
|
ephemeralPublic: r.publicKey
|
|
3775
3794
|
};
|
|
3776
3795
|
}
|
|
3777
|
-
function
|
|
3778
|
-
let r = $(e).toString("utf8"), { ephemeralPublic: i, nonce: a, ciphertext: o, tag: s } = JSON.parse(r), c =
|
|
3796
|
+
function vi(e, t) {
|
|
3797
|
+
let r = $(e).toString("utf8"), { ephemeralPublic: i, nonce: a, ciphertext: o, tag: s } = JSON.parse(r), c = gi(t, i, a), l = $(a), u = $(o), d = $(s), f = n("chacha20-poly1305", c, l, { authTagLength: 16 });
|
|
3779
3798
|
f.setAuthTag(d);
|
|
3780
3799
|
let p = Buffer.concat([f.update(u), f.final()]);
|
|
3781
3800
|
return JSON.parse(p.toString("utf8"));
|
|
3782
3801
|
}
|
|
3783
|
-
async function
|
|
3784
|
-
let { encryptedContent: n } =
|
|
3802
|
+
async function yi(e, t) {
|
|
3803
|
+
let { encryptedContent: n } = _i(e.body, t.recipientX25519PublicKey);
|
|
3785
3804
|
return w({
|
|
3786
3805
|
header: e.header,
|
|
3787
3806
|
body: {
|
|
@@ -3792,12 +3811,12 @@ async function _i(e, t) {
|
|
|
3792
3811
|
}
|
|
3793
3812
|
}, t.senderEd25519PrivateKey);
|
|
3794
3813
|
}
|
|
3795
|
-
async function
|
|
3814
|
+
async function bi(e, t) {
|
|
3796
3815
|
if (!await T(e, t.senderEd25519PublicKey)) throw Error("7h3/encryption: Ed25519 signature verification failed");
|
|
3797
3816
|
return {
|
|
3798
3817
|
envelope: e,
|
|
3799
|
-
body:
|
|
3818
|
+
body: vi(e.body.content, t.recipientX25519PrivateKey)
|
|
3800
3819
|
};
|
|
3801
3820
|
}
|
|
3802
3821
|
//#endregion
|
|
3803
|
-
export {
|
|
3822
|
+
export { at as AgentSession, ot as AipAgentAdapter, zr as CAP_HEADER, Tn as CBOR_CONTENT_TYPE, Sn as CborDecoder, bn as CborEncoder, Rt as ClusterRedisReplayStore, An as DEFAULT_HEADER, xe as DEFAULT_MAX_BINARY_ENVELOPE_BYTES, Re as DistributedReplayCache, hr as GRPC_METADATA_KEY, ri as InMemoryAuditLog, It as InMemoryRedisLikeClient, A as InMemoryReplayCache, Ut as InMemoryRevocationStore, ze as InMemoryVerificationMaterialCache, Rn as InMemoryWebhookReplayCache, Cr as KeyRotationManager, xn as MAX_CBOR_DEPTH, _e as MAX_CLOCK_SKEW_MS, ge as MAX_TTL_MS, ii as NoopAuditLog, $r as Protocol7h3Gateway, Ir as Protocol7h3Metrics, Ar as RESPONSE_HEADER, Lt as RedisReplayStore, wr as RevocationRegistry, rt as RollingKeyring, Mt as RuntimePolicyManager, qn as STREAM_HEADER, Ye as SessionTransport, ar as SignedStreamReader, ir as SignedStreamWriter, Y as SimpleCounter, Fr as SimpleHistogram, Tr as SlidingWindowRateLimiter, Ln as WEBHOOK_DEFAULT_TTL_MS, R as WEBHOOK_SIG_HEADER, z as WEBHOOK_TS_HEADER, N as bootstrapRuntimePolicyEnforcer, Z as canonicalizeCapabilityToken, _ as canonicalizeEnvelope, Un as consumeWebhook, st as createAipAgentAdapter, $e as createAipCapabilities, At as createAipMcpGatewayRuntime, jt as createAipMcpGatewayRuntimeWithPolicy, ai as createAuditLog, an as createCachingKeyRegistry, Vt as createClusterReplayStore, rn as createCompositeKeyRegistry, D as createEnvelope, ei as createGateway, Sr as createHttpKeyRegistry, tn as createHttpMcpClient, en as createHttpMcpHandler, Pn as createHttpMiddleware, it as createKeyringSignatureResolver, Yt as createMcpClientCodec, Rr as createMetricsMiddleware, Ct as createPolicyEnforcer, ti as createProductionGateway, pt as createRawTaskFromJsonRpc, ct as createRawTaskFromLangChain, ut as createRawTaskFromLlamaIndex, zt as createRedisReplayStore, Wt as createRedisRevocationStore, In as createSignedFetchRequest, Xe as createSignedMessage, or as createSignedStream, pr as createSignedWebSocketStream, nn as createStaticKeyRegistry, $t as createStdioMcpClient, sr as createStreamVerifier, wn as decodeCbor, Ge as decodeEnvelope, qe as decodeEnvelopeBatch, Ie as decodeEnvelopeBinary, kn as decodeEnvelopeCbor, dr as decodeStreamChunk, vi as decryptBody, Kr as delegateCapabilityToken, gi as deriveEncryptionKey, et as encodeAipCapabilities, Cn as encodeCbor, j as encodeEnvelope, Ke as encodeEnvelopeBatch, Fe as encodeEnvelopeBinary, En as encodeEnvelopeCbor, ur as encodeStreamChunk, _i as encryptBody, xr as fetchWellKnownKeys, pe as generateEd25519KeypairBase64Url, hi as generateX25519KeyPair, ci as getOtelTracer, Pt as getRuntimeTuningPreset, Er as globalRateLimiter, kr as isAllowedSender, wt as isRuntimeMode, Vr as issueCapabilityToken, bt as loadRuntimePolicy, Dr as matchGlob, Or as matchPolicy, X as metrics, nt as negotiateAipCapabilities, Qr as normalizeGatewayPath, bi as openEnvelope, tt as parseAipCapabilities, Zr as parseCapabilityChain, vt as parseRuntimePolicyJson, br as parseWellKnownKeys, y as randomHex, M as receiveEnvelope, Je as receiveEnvelopeBatch, mr as receiveSignedWebSocketStream, Ft as recommendPolicyAdjustments, Lr as renderPrometheusText, yi as sealEnvelope, Xr as serializeCapabilityChain, Qt as serveMcpOverStdio, yr as serveWellKnownKeys, si as setOtelProvider, b as signCanonicalPayloadEd25519, de as signCanonicalPayloadHmac, w as signEnvelopeEd25519, S as signEnvelopeHmac, Fn as signFetchRequest, gr as signGrpcCall, Mn as signHttpRequest, Nn as signHttpRequestHmac, Wn as signQueueMessage, jr as signResponse, cr as signStream, zn as signWebhook, Bn as signWebhookHmac, mt as toJsonRpcResponse, lt as toLangChainMessage, dt as toLlamaIndexMessage, Yr as tokenMatchesScope, E as validateEnvelope, _t as validateRuntimePolicy, x as verifyCanonicalPayloadEd25519, fe as verifyCanonicalPayloadHmac, he as verifyCanonicalPayloadSignature, Jr as verifyCapabilityChain, qr as verifyCapabilityToken, T as verifyEnvelopeEd25519, C as verifyEnvelopeHmac, me as verifyEnvelopeSignature, _r as verifyGrpcCall, jn as verifyHttpEnvelope, Kn as verifyQueueBatch, Gn as verifyQueueMessage, Mr as verifyResponse, lr as verifyStream, Vn as verifyWebhook, Hn as verifyWebhookHmac, ui as withAuditSpan, vr as withGrpcVerification, Gt as withRevocationCheck, li as withVerificationSpan, Xt as wrapMcpClient, Jt as wrapMcpServer, fr as wrapWebSocket };
|