@sess.si/sdk 0.0.0-stage → 0.1.0-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +155 -2
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +8 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/cli/verify.d.ts +14 -0
- package/dist/cli/verify.d.ts.map +1 -0
- package/dist/cli/verify.js +277 -0
- package/dist/cli/verify.js.map +1 -0
- package/dist/client.d.ts +12 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +34 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +52 -0
- package/dist/errors.js.map +1 -0
- package/dist/generated/openapi.d.ts +2462 -0
- package/dist/generated/openapi.d.ts.map +1 -0
- package/dist/generated/openapi.js +2 -0
- package/dist/generated/openapi.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/decode.d.ts +21 -0
- package/dist/internal/decode.d.ts.map +1 -0
- package/dist/internal/decode.js +333 -0
- package/dist/internal/decode.js.map +1 -0
- package/dist/internal/json.d.ts +4 -0
- package/dist/internal/json.d.ts.map +1 -0
- package/dist/internal/json.js +75 -0
- package/dist/internal/json.js.map +1 -0
- package/dist/internal/time.d.ts +11 -0
- package/dist/internal/time.d.ts.map +1 -0
- package/dist/internal/time.js +71 -0
- package/dist/internal/time.js.map +1 -0
- package/dist/polling.d.ts +3 -0
- package/dist/polling.d.ts.map +1 -0
- package/dist/polling.js +61 -0
- package/dist/polling.js.map +1 -0
- package/dist/receipts/bundle.d.ts +124 -0
- package/dist/receipts/bundle.d.ts.map +1 -0
- package/dist/receipts/bundle.js +424 -0
- package/dist/receipts/bundle.js.map +1 -0
- package/dist/receipts/index.d.ts +7 -0
- package/dist/receipts/index.d.ts.map +1 -0
- package/dist/receipts/index.js +3 -0
- package/dist/receipts/index.js.map +1 -0
- package/dist/receipts/json.d.ts +22 -0
- package/dist/receipts/json.d.ts.map +1 -0
- package/dist/receipts/json.js +152 -0
- package/dist/receipts/json.js.map +1 -0
- package/dist/receipts/key-cache.d.ts +28 -0
- package/dist/receipts/key-cache.d.ts.map +1 -0
- package/dist/receipts/key-cache.js +110 -0
- package/dist/receipts/key-cache.js.map +1 -0
- package/dist/receipts/resource.d.ts +39 -0
- package/dist/receipts/resource.d.ts.map +1 -0
- package/dist/receipts/resource.js +119 -0
- package/dist/receipts/resource.js.map +1 -0
- package/dist/receipts/verify.d.ts +90 -0
- package/dist/receipts/verify.d.ts.map +1 -0
- package/dist/receipts/verify.js +294 -0
- package/dist/receipts/verify.js.map +1 -0
- package/dist/sessions.d.ts +19 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +145 -0
- package/dist/sessions.js.map +1 -0
- package/dist/transport.d.ts +29 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +447 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +149 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/webhook-resource.d.ts +13 -0
- package/dist/webhook-resource.d.ts.map +1 -0
- package/dist/webhook-resource.js +52 -0
- package/dist/webhook-resource.js.map +1 -0
- package/dist/webhooks/index.d.ts +22 -0
- package/dist/webhooks/index.d.ts.map +1 -0
- package/dist/webhooks/index.js +184 -0
- package/dist/webhooks/index.js.map +1 -0
- package/package.json +78 -4
- package/src/cli/main.ts +8 -0
- package/src/cli/verify.ts +291 -0
- package/src/client.ts +41 -0
- package/src/errors.ts +96 -0
- package/src/generated/openapi.ts +2578 -0
- package/src/index.ts +4 -0
- package/src/internal/decode.ts +317 -0
- package/src/internal/json.ts +64 -0
- package/src/internal/time.ts +85 -0
- package/src/polling.ts +77 -0
- package/src/receipts/bundle.ts +547 -0
- package/src/receipts/index.ts +27 -0
- package/src/receipts/json.ts +168 -0
- package/src/receipts/key-cache.ts +143 -0
- package/src/receipts/resource.ts +227 -0
- package/src/receipts/verify.ts +468 -0
- package/src/sessions.ts +227 -0
- package/src/transport.ts +524 -0
- package/src/types.ts +135 -0
- package/src/webhook-resource.ts +81 -0
- package/src/webhooks/index.ts +246 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// Decision bundles (bundle-v1, docs/spec/bundle-v1.md): a deliberate disclosure of one accepted
|
|
2
|
+
// decision that anyone can check. verifyDecisionBundle is the SDK face of the Sess verification
|
|
3
|
+
// core: the same check order, bounds, reasons, result object and key-trust rules, proven against
|
|
4
|
+
// the shared conformance corpus. Keys and expected context come only from the caller; nothing in
|
|
5
|
+
// the bundle is trusted. A valid result states what the issuer accepted from someone holding the
|
|
6
|
+
// Session's human link: never who that person is, nor that anything was authorized or executed.
|
|
7
|
+
//
|
|
8
|
+
// verifyDecisionReceipt is a separate, stricter record-bound interface and is unchanged by this
|
|
9
|
+
// module. This module shares its trusted-key validation and canonical JSON helpers.
|
|
10
|
+
import { createHash } from 'node:crypto';
|
|
11
|
+
import { decodeProtectedHeader, errors, importJWK, jwtVerify } from 'jose';
|
|
12
|
+
import { canonicalJson, exactKeys, isRecord, jsonDigest } from './json.js';
|
|
13
|
+
import { validateKeySet } from './verify.js';
|
|
14
|
+
const BUNDLE_TYPE = 'sess.decision-bundle';
|
|
15
|
+
const BUNDLE_VERSION = 1;
|
|
16
|
+
const RECEIPT_ALG = 'EdDSA';
|
|
17
|
+
const RECEIPT_TYP = 'sess-receipt+jwt';
|
|
18
|
+
/** Bounds of a bundle (bundle-v1 §4). A bundle beyond any of them is `oversized`. */
|
|
19
|
+
export const BUNDLE_LIMITS = Object.freeze({
|
|
20
|
+
/** RFC 8785 canonical UTF-8 bytes of the whole bundle. */
|
|
21
|
+
bytes: 1_048_576,
|
|
22
|
+
/** Nesting depth of any value (the bundle object itself is depth 1). */
|
|
23
|
+
depth: 32,
|
|
24
|
+
/** Items in any one array. */
|
|
25
|
+
arrayItems: 1_000,
|
|
26
|
+
/** Members of any one object. */
|
|
27
|
+
objectMembers: 1_000,
|
|
28
|
+
/** JSON values in the whole bundle. */
|
|
29
|
+
nodes: 100_000,
|
|
30
|
+
/** Characters of the receipt's compact JWS. */
|
|
31
|
+
receiptChars: 32_768,
|
|
32
|
+
/** Public keys in `keys.keys`. */
|
|
33
|
+
keys: 16,
|
|
34
|
+
});
|
|
35
|
+
/** How far in the future a receipt's `iat` may be before it is `not_yet_valid`, in seconds. */
|
|
36
|
+
const CLOCK_TOLERANCE_S = 60;
|
|
37
|
+
// ------------------------------------------------------------------------------------------------
|
|
38
|
+
// receipt-v1 §6, in its order and with its codes
|
|
39
|
+
// ------------------------------------------------------------------------------------------------
|
|
40
|
+
class ReceiptV1Failure extends Error {
|
|
41
|
+
code;
|
|
42
|
+
constructor(code) {
|
|
43
|
+
super(code);
|
|
44
|
+
this.code = code;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const DIGEST = /^sha256:[0-9a-f]{64}$/;
|
|
48
|
+
const nonEmpty = (v) => typeof v === 'string' && v.length > 0;
|
|
49
|
+
function receiptV1Claims(p) {
|
|
50
|
+
const top = [
|
|
51
|
+
'sess_receipt',
|
|
52
|
+
'iss',
|
|
53
|
+
'aud',
|
|
54
|
+
'jti',
|
|
55
|
+
'iat',
|
|
56
|
+
'exp',
|
|
57
|
+
'accepted_at',
|
|
58
|
+
'workspace_id',
|
|
59
|
+
'session_id',
|
|
60
|
+
'step_id',
|
|
61
|
+
'event_id',
|
|
62
|
+
'surface',
|
|
63
|
+
'action',
|
|
64
|
+
'input_hash',
|
|
65
|
+
'actor',
|
|
66
|
+
'return_binding',
|
|
67
|
+
];
|
|
68
|
+
const s = p.surface;
|
|
69
|
+
const a = p.action;
|
|
70
|
+
const act = p.actor;
|
|
71
|
+
const rb = p.return_binding;
|
|
72
|
+
const ok = exactKeys(p, top) &&
|
|
73
|
+
p.sess_receipt === 1 &&
|
|
74
|
+
[p.iss, p.aud, p.jti, p.accepted_at, p.workspace_id, p.session_id, p.step_id, p.event_id].every(nonEmpty) &&
|
|
75
|
+
p.jti === p.event_id &&
|
|
76
|
+
Number.isSafeInteger(p.iat) &&
|
|
77
|
+
Number.isSafeInteger(p.exp) &&
|
|
78
|
+
typeof p.input_hash === 'string' &&
|
|
79
|
+
DIGEST.test(p.input_hash) &&
|
|
80
|
+
isRecord(s) &&
|
|
81
|
+
exactKeys(s, ['revision', 'hash', 'renderer_version']) &&
|
|
82
|
+
Number.isSafeInteger(s.revision) &&
|
|
83
|
+
s.revision >= 1 &&
|
|
84
|
+
typeof s.hash === 'string' &&
|
|
85
|
+
DIGEST.test(s.hash) &&
|
|
86
|
+
nonEmpty(s.renderer_version) &&
|
|
87
|
+
isRecord(a) &&
|
|
88
|
+
exactKeys(a, ['id', 'label', 'summary']) &&
|
|
89
|
+
nonEmpty(a.id) &&
|
|
90
|
+
nonEmpty(a.label) &&
|
|
91
|
+
typeof a.summary === 'string' &&
|
|
92
|
+
isRecord(act) &&
|
|
93
|
+
exactKeys(act, ['assurance', 'subject']) &&
|
|
94
|
+
act.assurance === 'link_holder' &&
|
|
95
|
+
typeof act.subject === 'string' &&
|
|
96
|
+
act.subject.startsWith(`${p.workspace_id}:`) &&
|
|
97
|
+
isRecord(rb) &&
|
|
98
|
+
exactKeys(rb, ['recipient', 'return_channel_id']) &&
|
|
99
|
+
rb.recipient === p.aud &&
|
|
100
|
+
(rb.return_channel_id === null || nonEmpty(rb.return_channel_id));
|
|
101
|
+
if (!ok)
|
|
102
|
+
throw new ReceiptV1Failure('claims');
|
|
103
|
+
return p;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The receipt-v1 rules against trusted keys: compact JWS, pinned EdDSA, a kid the caller trusts
|
|
107
|
+
* (no fallback key), the receipt typ, the signature, the trusted issuer, the expected audience,
|
|
108
|
+
* current-time expiry, then the claims and their canonical serialization. Throws ReceiptV1Failure
|
|
109
|
+
* with the first failing code; any other error means the receipt could not be read at all.
|
|
110
|
+
*/
|
|
111
|
+
async function verifyReceiptV1(jws, keys, options) {
|
|
112
|
+
if (jws.split('.').length !== 3)
|
|
113
|
+
throw new ReceiptV1Failure('malformed');
|
|
114
|
+
let header;
|
|
115
|
+
try {
|
|
116
|
+
header = decodeProtectedHeader(jws);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
throw new ReceiptV1Failure('malformed');
|
|
120
|
+
}
|
|
121
|
+
if (header.alg !== RECEIPT_ALG)
|
|
122
|
+
throw new ReceiptV1Failure('algorithm');
|
|
123
|
+
if (typeof header.kid !== 'string' || header.kid.length === 0)
|
|
124
|
+
throw new ReceiptV1Failure('missing_kid');
|
|
125
|
+
const jwk = keys.keys.find((k) => k.kid === header.kid);
|
|
126
|
+
if (!jwk)
|
|
127
|
+
throw new ReceiptV1Failure('unknown_kid');
|
|
128
|
+
if (header.typ !== RECEIPT_TYP)
|
|
129
|
+
throw new ReceiptV1Failure('type');
|
|
130
|
+
const key = await importJWK({ kty: jwk.kty, crv: jwk.crv, x: jwk.x }, RECEIPT_ALG);
|
|
131
|
+
let payload;
|
|
132
|
+
try {
|
|
133
|
+
({ payload } = await jwtVerify(jws, key, {
|
|
134
|
+
algorithms: [RECEIPT_ALG],
|
|
135
|
+
typ: RECEIPT_TYP,
|
|
136
|
+
issuer: options.issuer,
|
|
137
|
+
audience: options.audience,
|
|
138
|
+
currentDate: options.now,
|
|
139
|
+
requiredClaims: ['iss', 'aud', 'iat', 'exp', 'jti'],
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
if (e instanceof errors.JWSSignatureVerificationFailed)
|
|
144
|
+
throw new ReceiptV1Failure('signature');
|
|
145
|
+
if (e instanceof errors.JWTExpired)
|
|
146
|
+
throw new ReceiptV1Failure('expired');
|
|
147
|
+
if (e instanceof errors.JOSEAlgNotAllowed)
|
|
148
|
+
throw new ReceiptV1Failure('algorithm');
|
|
149
|
+
if (e instanceof errors.JWTClaimValidationFailed) {
|
|
150
|
+
if (e.claim === 'aud')
|
|
151
|
+
throw new ReceiptV1Failure('audience');
|
|
152
|
+
if (e.claim === 'iss')
|
|
153
|
+
throw new ReceiptV1Failure('issuer');
|
|
154
|
+
throw new ReceiptV1Failure('claims');
|
|
155
|
+
}
|
|
156
|
+
throw new ReceiptV1Failure('malformed');
|
|
157
|
+
}
|
|
158
|
+
const claims = receiptV1Claims(payload);
|
|
159
|
+
// The signed bytes must be exactly the canonical form of the claims.
|
|
160
|
+
const signed = Buffer.from(jws.split('.')[1], 'base64url');
|
|
161
|
+
if (`sha256:${createHash('sha256').update(signed).digest('hex')}` !== jsonDigest(claims))
|
|
162
|
+
throw new ReceiptV1Failure('claims');
|
|
163
|
+
return claims;
|
|
164
|
+
}
|
|
165
|
+
// ------------------------------------------------------------------------------------------------
|
|
166
|
+
// bundle-v1 §2 and §4: shape and bounds
|
|
167
|
+
// ------------------------------------------------------------------------------------------------
|
|
168
|
+
/**
|
|
169
|
+
* Walks a value iteratively (nesting cannot exhaust the stack): 'oversized' for an exceeded
|
|
170
|
+
* bound, 'malformed' for a value with no JSON form, else null.
|
|
171
|
+
*/
|
|
172
|
+
function structuralProblem(value) {
|
|
173
|
+
const stack = [[value, 1]];
|
|
174
|
+
let nodes = 0;
|
|
175
|
+
let textBytes = 0;
|
|
176
|
+
while (stack.length > 0) {
|
|
177
|
+
const [v, depth] = stack.pop();
|
|
178
|
+
nodes += 1;
|
|
179
|
+
if (nodes > BUNDLE_LIMITS.nodes || depth > BUNDLE_LIMITS.depth)
|
|
180
|
+
return 'oversized';
|
|
181
|
+
if (v === null || typeof v === 'boolean')
|
|
182
|
+
continue;
|
|
183
|
+
if (typeof v === 'number') {
|
|
184
|
+
if (!Number.isFinite(v))
|
|
185
|
+
return 'malformed';
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (typeof v === 'string') {
|
|
189
|
+
// A lower bound of the UTF-8 size: stops a huge string before canonicalization.
|
|
190
|
+
textBytes += v.length;
|
|
191
|
+
if (textBytes > BUNDLE_LIMITS.bytes)
|
|
192
|
+
return 'oversized';
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (Array.isArray(v)) {
|
|
196
|
+
if (v.length > BUNDLE_LIMITS.arrayItems)
|
|
197
|
+
return 'oversized';
|
|
198
|
+
for (const item of v)
|
|
199
|
+
stack.push([item, depth + 1]);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype) {
|
|
203
|
+
const keys = Object.keys(v);
|
|
204
|
+
if (keys.length > BUNDLE_LIMITS.objectMembers)
|
|
205
|
+
return 'oversized';
|
|
206
|
+
for (const k of keys) {
|
|
207
|
+
textBytes += k.length;
|
|
208
|
+
stack.push([v[k], depth + 1]);
|
|
209
|
+
}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
// undefined, functions, symbols, bigint, class instances: no JSON form.
|
|
213
|
+
return 'malformed';
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
const COMPACT_JWS = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/;
|
|
218
|
+
const KID = /^[A-Za-z0-9._-]{1,64}$/;
|
|
219
|
+
const JWK_MEMBERS = new Set(['kty', 'crv', 'x', 'kid', 'alg', 'use']);
|
|
220
|
+
function publicJwkProblem(k) {
|
|
221
|
+
if (!isRecord(k))
|
|
222
|
+
return true;
|
|
223
|
+
// Only the public members: any other member (d, p, q, k, …) makes the bundle malformed, so a
|
|
224
|
+
// bundle never carries private key material.
|
|
225
|
+
if (Object.keys(k).some((m) => !JWK_MEMBERS.has(m)))
|
|
226
|
+
return true;
|
|
227
|
+
return !(k.kty === 'OKP' &&
|
|
228
|
+
k.crv === 'Ed25519' &&
|
|
229
|
+
typeof k.x === 'string' &&
|
|
230
|
+
/^[A-Za-z0-9_-]{43}$/.test(k.x) &&
|
|
231
|
+
typeof k.kid === 'string' &&
|
|
232
|
+
KID.test(k.kid) &&
|
|
233
|
+
(k.alg === undefined || k.alg === RECEIPT_ALG) &&
|
|
234
|
+
(k.use === undefined || k.use === 'sig'));
|
|
235
|
+
}
|
|
236
|
+
function shapeProblem(b) {
|
|
237
|
+
if (!isRecord(b))
|
|
238
|
+
return 'malformed';
|
|
239
|
+
const structural = structuralProblem(b);
|
|
240
|
+
if (structural)
|
|
241
|
+
return structural;
|
|
242
|
+
let size;
|
|
243
|
+
try {
|
|
244
|
+
size = Buffer.byteLength(canonicalJson(b), 'utf8');
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
return 'malformed'; // e.g. a lone surrogate
|
|
248
|
+
}
|
|
249
|
+
if (size > BUNDLE_LIMITS.bytes)
|
|
250
|
+
return 'oversized';
|
|
251
|
+
if (b.type !== BUNDLE_TYPE)
|
|
252
|
+
return 'malformed';
|
|
253
|
+
if (b.version !== BUNDLE_VERSION) {
|
|
254
|
+
return Number.isSafeInteger(b.version) && b.version > BUNDLE_VERSION
|
|
255
|
+
? 'unsupported_version'
|
|
256
|
+
: 'malformed';
|
|
257
|
+
}
|
|
258
|
+
if (!exactKeys(b, ['type', 'version', 'receipt', 'content', 'keys']))
|
|
259
|
+
return 'malformed';
|
|
260
|
+
if (typeof b.receipt !== 'string')
|
|
261
|
+
return 'malformed';
|
|
262
|
+
if (b.receipt.length > BUNDLE_LIMITS.receiptChars)
|
|
263
|
+
return 'oversized';
|
|
264
|
+
const c = b.content;
|
|
265
|
+
if (!isRecord(c) || !exactKeys(c, ['surface', 'input']))
|
|
266
|
+
return 'malformed';
|
|
267
|
+
if (!isRecord(c.surface) || !isRecord(c.input))
|
|
268
|
+
return 'malformed';
|
|
269
|
+
const k = b.keys;
|
|
270
|
+
if (!isRecord(k) || !exactKeys(k, ['keys']) || !Array.isArray(k.keys))
|
|
271
|
+
return 'malformed';
|
|
272
|
+
if (k.keys.length > BUNDLE_LIMITS.keys)
|
|
273
|
+
return 'oversized';
|
|
274
|
+
if (k.keys.some(publicJwkProblem))
|
|
275
|
+
return 'malformed';
|
|
276
|
+
const kids = k.keys.map((j) => j.kid);
|
|
277
|
+
if (new Set(kids).size !== kids.length)
|
|
278
|
+
return 'malformed';
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
/** The receipt's declared (unverified) `aud`, used only when the caller expects none. */
|
|
282
|
+
function declaredAudience(jws) {
|
|
283
|
+
if (!COMPACT_JWS.test(jws))
|
|
284
|
+
return undefined;
|
|
285
|
+
try {
|
|
286
|
+
const payload = JSON.parse(Buffer.from(jws.split('.')[1], 'base64url').toString('utf8'));
|
|
287
|
+
return isRecord(payload) && typeof payload.aud === 'string' && payload.aud.length > 0
|
|
288
|
+
? payload.aud
|
|
289
|
+
: undefined;
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
function checkOptions(o) {
|
|
296
|
+
if (typeof o?.issuer !== 'string' || o.issuer.length === 0)
|
|
297
|
+
throw new TypeError('verifyDecisionBundle needs a non-empty trusted issuer');
|
|
298
|
+
if (!o.trustedKeys)
|
|
299
|
+
throw new TypeError('verifyDecisionBundle needs trusted keys');
|
|
300
|
+
let keys;
|
|
301
|
+
try {
|
|
302
|
+
keys = validateKeySet(o.trustedKeys);
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
throw new TypeError('trustedKeys must be a JWK Set of public Ed25519 keys with unique kids');
|
|
306
|
+
}
|
|
307
|
+
if (o.now !== undefined && (!(o.now instanceof Date) || Number.isNaN(o.now.getTime())))
|
|
308
|
+
throw new TypeError('now must be a valid Date');
|
|
309
|
+
const e = o.expectedContext;
|
|
310
|
+
if (e === undefined)
|
|
311
|
+
return keys;
|
|
312
|
+
if (!isRecord(e))
|
|
313
|
+
throw new TypeError('expectedContext must be an object');
|
|
314
|
+
for (const k of ['audience', 'workspaceId', 'sessionId', 'eventId', 'actionId'])
|
|
315
|
+
if (e[k] !== undefined && (typeof e[k] !== 'string' || e[k].length === 0))
|
|
316
|
+
throw new TypeError(`expectedContext.${k} must be a non-empty string`);
|
|
317
|
+
if (e.revision !== undefined && (!Number.isSafeInteger(e.revision) || e.revision < 1))
|
|
318
|
+
throw new TypeError('expectedContext.revision must be a positive integer');
|
|
319
|
+
return keys;
|
|
320
|
+
}
|
|
321
|
+
function contextChecks(e, c) {
|
|
322
|
+
if (!e)
|
|
323
|
+
return [];
|
|
324
|
+
const all = [
|
|
325
|
+
['audience', e.audience, c?.aud],
|
|
326
|
+
['workspace_id', e.workspaceId, c?.workspace_id],
|
|
327
|
+
['session_id', e.sessionId, c?.session_id],
|
|
328
|
+
['event_id', e.eventId, c?.event_id],
|
|
329
|
+
['surface.revision', e.revision, c?.surface.revision],
|
|
330
|
+
['action.id', e.actionId, c?.action.id],
|
|
331
|
+
];
|
|
332
|
+
return all
|
|
333
|
+
.filter(([, expected]) => expected !== undefined)
|
|
334
|
+
.map(([field, expected, actual]) => ({ field, ok: expected === actual }));
|
|
335
|
+
}
|
|
336
|
+
// ------------------------------------------------------------------------------------------------
|
|
337
|
+
// Verification
|
|
338
|
+
// ------------------------------------------------------------------------------------------------
|
|
339
|
+
/**
|
|
340
|
+
* Verifies a bundle-v1 decision bundle offline against the issuer and keys the caller trusts.
|
|
341
|
+
* Every outcome about the bundle is a result, never an exception; only invalid options throw a
|
|
342
|
+
* TypeError. Order (bundle-v1 §5): shape and bounds; the receipt by the receipt-v1 rules; not
|
|
343
|
+
* issued in the future; the disclosed Surface and input against the receipt's hashes; then the
|
|
344
|
+
* caller's expected context.
|
|
345
|
+
*/
|
|
346
|
+
export async function verifyDecisionBundle(bundle, options) {
|
|
347
|
+
const trustedKeys = checkOptions(options);
|
|
348
|
+
const now = options.now ?? new Date();
|
|
349
|
+
const expected = options.expectedContext;
|
|
350
|
+
const supplied = contextChecks(expected, null).map((x) => x.field);
|
|
351
|
+
const invalid = (reason, over = {}) => ({
|
|
352
|
+
valid: false,
|
|
353
|
+
reason,
|
|
354
|
+
checked_at: now.toISOString(),
|
|
355
|
+
content: 'not_checked',
|
|
356
|
+
context: { status: 'not_checked', checked: supplied, mismatches: [] },
|
|
357
|
+
application_purpose: 'unchecked',
|
|
358
|
+
actor: null,
|
|
359
|
+
receipt: null,
|
|
360
|
+
...over,
|
|
361
|
+
});
|
|
362
|
+
const shape = shapeProblem(bundle);
|
|
363
|
+
if (shape)
|
|
364
|
+
return invalid(shape);
|
|
365
|
+
const b = bundle;
|
|
366
|
+
// Without an expected audience the receipt is checked against its own declared (signed) `aud`;
|
|
367
|
+
// the result then reports the context as unchecked.
|
|
368
|
+
const audience = expected?.audience ?? declaredAudience(b.receipt);
|
|
369
|
+
if (audience === undefined)
|
|
370
|
+
return invalid('receipt_malformed');
|
|
371
|
+
let claims;
|
|
372
|
+
try {
|
|
373
|
+
claims = await verifyReceiptV1(b.receipt, trustedKeys, { issuer: options.issuer, audience, now });
|
|
374
|
+
}
|
|
375
|
+
catch (e) {
|
|
376
|
+
if (!(e instanceof ReceiptV1Failure))
|
|
377
|
+
return invalid('receipt_malformed');
|
|
378
|
+
if (e.code === 'audience')
|
|
379
|
+
return invalid('audience', {
|
|
380
|
+
context: { status: 'mismatch', checked: supplied, mismatches: ['audience'] },
|
|
381
|
+
});
|
|
382
|
+
return invalid(e.code === 'malformed' ? 'receipt_malformed' : e.code);
|
|
383
|
+
}
|
|
384
|
+
if (claims.iat > Math.floor(now.getTime() / 1000) + CLOCK_TOLERANCE_S)
|
|
385
|
+
return invalid('not_yet_valid');
|
|
386
|
+
if (claims.surface.hash !== jsonDigest(b.content.surface))
|
|
387
|
+
return invalid('surface_mismatch', { content: 'mismatch' });
|
|
388
|
+
if (claims.input_hash !== jsonDigest(b.content.input))
|
|
389
|
+
return invalid('input_mismatch', { content: 'mismatch' });
|
|
390
|
+
const checks = contextChecks(expected, claims);
|
|
391
|
+
const mismatches = checks.filter((x) => !x.ok).map((x) => x.field);
|
|
392
|
+
if (mismatches.length > 0)
|
|
393
|
+
return invalid('context_mismatch', {
|
|
394
|
+
content: 'verified',
|
|
395
|
+
context: { status: 'mismatch', checked: supplied, mismatches },
|
|
396
|
+
});
|
|
397
|
+
const status = checks.length === 0 ? 'unchecked' : expected?.audience === undefined ? 'incomplete' : 'verified';
|
|
398
|
+
const kid = JSON.parse(Buffer.from(b.receipt.split('.')[0], 'base64url').toString('utf8'))
|
|
399
|
+
.kid;
|
|
400
|
+
return {
|
|
401
|
+
valid: true,
|
|
402
|
+
reason: null,
|
|
403
|
+
checked_at: now.toISOString(),
|
|
404
|
+
content: 'verified',
|
|
405
|
+
context: { status, checked: supplied, mismatches: [] },
|
|
406
|
+
application_purpose: status === 'verified' ? 'matched_caller_context' : 'unchecked',
|
|
407
|
+
actor: { assurance: claims.actor.assurance },
|
|
408
|
+
receipt: {
|
|
409
|
+
key_id: kid,
|
|
410
|
+
issuer: claims.iss,
|
|
411
|
+
audience: claims.aud,
|
|
412
|
+
event_id: claims.event_id,
|
|
413
|
+
session_id: claims.session_id,
|
|
414
|
+
workspace_id: claims.workspace_id,
|
|
415
|
+
step_id: claims.step_id,
|
|
416
|
+
surface: { ...claims.surface },
|
|
417
|
+
action: { ...claims.action },
|
|
418
|
+
input_hash: claims.input_hash,
|
|
419
|
+
accepted_at: claims.accepted_at,
|
|
420
|
+
expires_at: new Date(claims.exp * 1000).toISOString(),
|
|
421
|
+
},
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
//# sourceMappingURL=bundle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.js","sourceRoot":"","sources":["../../src/receipts/bundle.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,gGAAgG;AAChG,iGAAiG;AACjG,i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AE;QAC7B,OAAO,EAAE,UAAU;QACnB,OAAO,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE;QACtD,mBAAmB,EAAE,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,WAAW;QACnF,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE;QAC5C,OAAO,EAAE;YACP,MAAM,EAAE,GAAG;YACX,MAAM,EAAE,MAAM,CAAC,GAAG;YAClB,QAAQ,EAAE,MAAM,CAAC,GAAG;YACpB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,YAAY,EAAE,MAAM,CAAC,YAAY;YACjC,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,OAAO,EAAE,EAAE,GAAG,MAAM,CAAC,OAAO,EAAE;YAC9B,MAAM,EAAE,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE;YAC5B,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,UAAU,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,WAAW,EAAE;SACtD;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { verifyDecisionReceipt } from './verify.js';
|
|
2
|
+
export type { ReceiptClaims, ReceiptPublicKey, ReceiptKeySet, ReceiptKeyResolver, VerifyDecisionReceiptInput, VerifyDecisionReceiptOptions, } from './verify.js';
|
|
3
|
+
export type { DeepReadonly } from './json.js';
|
|
4
|
+
export { verifyDecisionBundle, BUNDLE_LIMITS } from './bundle.js';
|
|
5
|
+
export type { BundleContextField, BundleJwk, BundleRejection, DecisionBundle, DecisionBundleVerification, ExpectedBundleContext, VerifiedReceiptSummary, VerifyDecisionBundleOptions, } from './bundle.js';
|
|
6
|
+
export type { ReceiptResource, VerifyResponseInput, VerifiedResponse, VerificationRequestOptions, } from './resource.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/receipts/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,YAAY,EACV,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,kBAAkB,EAClB,0BAA0B,EAC1B,4BAA4B,GAC7B,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAClE,YAAY,EACV,kBAAkB,EAClB,SAAS,EACT,eAAe,EACf,cAAc,EACd,0BAA0B,EAC1B,qBAAqB,EACrB,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,GAC3B,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/receipts/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAUpD,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SessError } from '../errors.js';
|
|
2
|
+
/** JSON-shaped values returned by a verifier are copies, protected at every depth. */
|
|
3
|
+
export type DeepReadonly<T> = T extends readonly (infer U)[] ? readonly DeepReadonly<U>[] : T extends object ? {
|
|
4
|
+
readonly [P in keyof T]: DeepReadonly<T[P]>;
|
|
5
|
+
} : T;
|
|
6
|
+
export declare function verificationError(code: string, message: string): SessError;
|
|
7
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
8
|
+
export declare function wellFormedUnicode(value: string): boolean;
|
|
9
|
+
export declare function exactKeys(value: Record<string, unknown>, keys: readonly string[]): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Copy data without invoking accessors/toJSON or coercing non-JSON business values.
|
|
12
|
+
* This helper intentionally does not freeze data owned by the application.
|
|
13
|
+
*/
|
|
14
|
+
export declare function snapshotJson<T>(input: T): T;
|
|
15
|
+
export declare function deepFreeze<T>(value: T): DeepReadonly<T>;
|
|
16
|
+
export declare function canonicalJson(value: unknown): string;
|
|
17
|
+
export declare function jsonDigest(value: unknown): string;
|
|
18
|
+
export declare function checkedInstant(value: unknown): number | undefined;
|
|
19
|
+
export declare function abortError(signal: AbortSignal): SessError;
|
|
20
|
+
/** Race a promise without cancelling shared work; always remove this waiter's listener. */
|
|
21
|
+
export declare function waitWithSignal<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
22
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/receipts/json.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,sFAAsF;AACtF,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GACxD,SAAS,YAAY,CAAC,CAAC,CAAC,EAAE,GAC1B,CAAC,SAAS,MAAM,GACd;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAC/C,CAAC,CAAC;AAER,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAE1E;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEzE;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CASxD;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAE1F;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAmE3C;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAMvD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQpD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEjD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAKjE;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAIzD;AAED,2FAA2F;AAC3F,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAwB7F"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import canonicalize from 'canonicalize';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { SessError } from '../errors.js';
|
|
4
|
+
export function verificationError(code, message) {
|
|
5
|
+
return new SessError('receipt_verification', message, { code });
|
|
6
|
+
}
|
|
7
|
+
export function isRecord(value) {
|
|
8
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
export function wellFormedUnicode(value) {
|
|
11
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
12
|
+
const code = value.charCodeAt(index);
|
|
13
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
14
|
+
const next = value.charCodeAt(++index);
|
|
15
|
+
if (!(next >= 0xdc00 && next <= 0xdfff))
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
else if (code >= 0xdc00 && code <= 0xdfff)
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
export function exactKeys(value, keys) {
|
|
24
|
+
return Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Copy data without invoking accessors/toJSON or coercing non-JSON business values.
|
|
28
|
+
* This helper intentionally does not freeze data owned by the application.
|
|
29
|
+
*/
|
|
30
|
+
export function snapshotJson(input) {
|
|
31
|
+
const ancestors = new Set();
|
|
32
|
+
let remaining = 200_000;
|
|
33
|
+
const visit = (value, depth) => {
|
|
34
|
+
if (--remaining < 0 || depth > 128) {
|
|
35
|
+
throw verificationError('INVALID_JSON', 'Verification data exceeds the supported JSON complexity.');
|
|
36
|
+
}
|
|
37
|
+
if (value === null || typeof value === 'boolean')
|
|
38
|
+
return value;
|
|
39
|
+
if (typeof value === 'string') {
|
|
40
|
+
if (!wellFormedUnicode(value))
|
|
41
|
+
throw verificationError('INVALID_JSON', 'Verification data must contain well-formed Unicode.');
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
45
|
+
return value;
|
|
46
|
+
if (typeof value !== 'object' || value === null) {
|
|
47
|
+
throw verificationError('INVALID_JSON', 'Verification data must be JSON without coercion.');
|
|
48
|
+
}
|
|
49
|
+
if (ancestors.has(value) || Object.getOwnPropertySymbols(value).length !== 0) {
|
|
50
|
+
throw verificationError('INVALID_JSON', 'Verification data must be acyclic JSON.');
|
|
51
|
+
}
|
|
52
|
+
const array = Array.isArray(value);
|
|
53
|
+
const prototype = Object.getPrototypeOf(value);
|
|
54
|
+
if (array && prototype !== Array.prototype) {
|
|
55
|
+
throw verificationError('INVALID_JSON', 'Verification arrays must have ordinary JSON array semantics.');
|
|
56
|
+
}
|
|
57
|
+
if (!array && prototype !== Object.prototype && prototype !== null) {
|
|
58
|
+
throw verificationError('INVALID_JSON', 'Verification data must contain only plain JSON objects.');
|
|
59
|
+
}
|
|
60
|
+
ancestors.add(value);
|
|
61
|
+
try {
|
|
62
|
+
if (array) {
|
|
63
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
64
|
+
if (Object.keys(descriptors).some((key) => key !== 'length' && !/^(?:0|[1-9][0-9]*)$/.test(key))) {
|
|
65
|
+
throw verificationError('INVALID_JSON', 'Verification arrays cannot have named properties.');
|
|
66
|
+
}
|
|
67
|
+
return Array.from({ length: value.length }, (_, index) => {
|
|
68
|
+
const descriptor = descriptors[String(index)];
|
|
69
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
70
|
+
throw verificationError('INVALID_JSON', 'Verification arrays cannot be sparse or contain accessors.');
|
|
71
|
+
}
|
|
72
|
+
return visit(descriptor.value, depth + 1);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const result = {};
|
|
76
|
+
for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(value))) {
|
|
77
|
+
if (!wellFormedUnicode(key) || !descriptor.enumerable || !('value' in descriptor)) {
|
|
78
|
+
throw verificationError('INVALID_JSON', 'Verification objects cannot contain accessors or hidden properties.');
|
|
79
|
+
}
|
|
80
|
+
Object.defineProperty(result, key, {
|
|
81
|
+
value: visit(descriptor.value, depth + 1),
|
|
82
|
+
enumerable: true,
|
|
83
|
+
configurable: true,
|
|
84
|
+
writable: true,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
ancestors.delete(value);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
return visit(input, 0);
|
|
94
|
+
}
|
|
95
|
+
export function deepFreeze(value) {
|
|
96
|
+
if (value !== null && typeof value === 'object') {
|
|
97
|
+
for (const child of Object.values(value))
|
|
98
|
+
deepFreeze(child);
|
|
99
|
+
Object.freeze(value);
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
export function canonicalJson(value) {
|
|
104
|
+
try {
|
|
105
|
+
const result = canonicalize(value);
|
|
106
|
+
if (result !== undefined)
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// Dependencies' exceptions may contain caller data; expose only a fixed explanation.
|
|
111
|
+
}
|
|
112
|
+
throw verificationError('INVALID_JSON', 'Verification data is not canonicalizable JSON.');
|
|
113
|
+
}
|
|
114
|
+
export function jsonDigest(value) {
|
|
115
|
+
return `sha256:${createHash('sha256').update(canonicalJson(value), 'utf8').digest('hex')}`;
|
|
116
|
+
}
|
|
117
|
+
export function checkedInstant(value) {
|
|
118
|
+
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(value))
|
|
119
|
+
return undefined;
|
|
120
|
+
const millis = Date.parse(value);
|
|
121
|
+
return Number.isFinite(millis) && new Date(millis).toISOString() === value ? millis : undefined;
|
|
122
|
+
}
|
|
123
|
+
export function abortError(signal) {
|
|
124
|
+
return signal.reason instanceof SessError && signal.reason.kind === 'timeout'
|
|
125
|
+
? new SessError('timeout', 'Verification exceeded its deadline.')
|
|
126
|
+
: new SessError('aborted', 'Verification was cancelled.');
|
|
127
|
+
}
|
|
128
|
+
/** Race a promise without cancelling shared work; always remove this waiter's listener. */
|
|
129
|
+
export async function waitWithSignal(promise, signal) {
|
|
130
|
+
if (!signal)
|
|
131
|
+
return promise;
|
|
132
|
+
if (signal.aborted) {
|
|
133
|
+
void promise.catch(() => { });
|
|
134
|
+
throw abortError(signal);
|
|
135
|
+
}
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
const aborted = () => {
|
|
138
|
+
cleanup();
|
|
139
|
+
reject(abortError(signal));
|
|
140
|
+
};
|
|
141
|
+
const cleanup = () => signal.removeEventListener('abort', aborted);
|
|
142
|
+
signal.addEventListener('abort', aborted, { once: true });
|
|
143
|
+
promise.then((result) => {
|
|
144
|
+
cleanup();
|
|
145
|
+
resolve(result);
|
|
146
|
+
}, (error) => {
|
|
147
|
+
cleanup();
|
|
148
|
+
reject(error);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=json.js.map
|