@sess.si/sdk 0.1.0-rc.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -9
- package/NOTICE +1 -1
- package/README.md +92 -16
- package/dist/ask.d.ts +0 -1
- package/dist/ask.js +0 -1
- package/dist/cli/main.d.ts +0 -1
- package/dist/cli/main.js +0 -1
- package/dist/cli/verify.d.ts +0 -1
- package/dist/cli/verify.js +0 -1
- package/dist/client.d.ts +0 -1
- package/dist/client.js +0 -1
- package/dist/commerce/builders.d.ts +99 -0
- package/dist/commerce/builders.js +208 -0
- package/dist/commerce/currency.d.ts +4 -0
- package/dist/commerce/currency.js +25 -0
- package/dist/commerce/items.d.ts +119 -0
- package/dist/commerce/items.js +280 -0
- package/dist/commerce/lint.d.ts +16 -0
- package/dist/commerce/lint.js +56 -0
- package/dist/errors.d.ts +5 -2
- package/dist/errors.js +15 -25
- package/dist/generated/openapi.d.ts +12 -13
- package/dist/generated/openapi.js +0 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/internal/decode.d.ts +0 -1
- package/dist/internal/decode.js +9 -2
- package/dist/internal/json.d.ts +0 -1
- package/dist/internal/json.js +0 -1
- package/dist/internal/time.d.ts +0 -1
- package/dist/internal/time.js +0 -1
- package/dist/internal/version.d.ts +2 -0
- package/dist/internal/version.js +2 -0
- package/dist/polling.d.ts +0 -1
- package/dist/polling.js +0 -1
- package/dist/qr/encoder.d.ts +0 -1
- package/dist/qr/encoder.js +0 -1
- package/dist/qr/index.d.ts +0 -1
- package/dist/qr/index.js +0 -1
- package/dist/receipts/bundle.d.ts +0 -1
- package/dist/receipts/bundle.js +0 -1
- package/dist/receipts/index.d.ts +0 -1
- package/dist/receipts/index.js +0 -1
- package/dist/receipts/json.d.ts +0 -1
- package/dist/receipts/json.js +0 -1
- package/dist/receipts/key-cache.d.ts +0 -1
- package/dist/receipts/key-cache.js +0 -1
- package/dist/receipts/renderers.d.ts +0 -1
- package/dist/receipts/renderers.js +1 -2
- package/dist/receipts/resource.d.ts +0 -1
- package/dist/receipts/resource.js +0 -1
- package/dist/receipts/verify.d.ts +0 -1
- package/dist/receipts/verify.js +0 -1
- package/dist/sessions.d.ts +0 -1
- package/dist/sessions.js +0 -1
- package/dist/transport.d.ts +0 -1
- package/dist/transport.js +35 -5
- package/dist/types.d.ts +56 -2
- package/dist/types.js +0 -1
- package/dist/webhook-resource.d.ts +0 -1
- package/dist/webhook-resource.js +0 -1
- package/dist/webhooks/index.d.ts +0 -1
- package/dist/webhooks/index.js +0 -1
- package/package.json +8 -3
- package/dist/ask.d.ts.map +0 -1
- package/dist/ask.js.map +0 -1
- package/dist/cli/main.d.ts.map +0 -1
- package/dist/cli/main.js.map +0 -1
- package/dist/cli/verify.d.ts.map +0 -1
- package/dist/cli/verify.js.map +0 -1
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/generated/openapi.d.ts.map +0 -1
- package/dist/generated/openapi.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/internal/decode.d.ts.map +0 -1
- package/dist/internal/decode.js.map +0 -1
- package/dist/internal/json.d.ts.map +0 -1
- package/dist/internal/json.js.map +0 -1
- package/dist/internal/time.d.ts.map +0 -1
- package/dist/internal/time.js.map +0 -1
- package/dist/polling.d.ts.map +0 -1
- package/dist/polling.js.map +0 -1
- package/dist/qr/encoder.d.ts.map +0 -1
- package/dist/qr/encoder.js.map +0 -1
- package/dist/qr/index.d.ts.map +0 -1
- package/dist/qr/index.js.map +0 -1
- package/dist/receipts/bundle.d.ts.map +0 -1
- package/dist/receipts/bundle.js.map +0 -1
- package/dist/receipts/index.d.ts.map +0 -1
- package/dist/receipts/index.js.map +0 -1
- package/dist/receipts/json.d.ts.map +0 -1
- package/dist/receipts/json.js.map +0 -1
- package/dist/receipts/key-cache.d.ts.map +0 -1
- package/dist/receipts/key-cache.js.map +0 -1
- package/dist/receipts/renderers.d.ts.map +0 -1
- package/dist/receipts/renderers.js.map +0 -1
- package/dist/receipts/resource.d.ts.map +0 -1
- package/dist/receipts/resource.js.map +0 -1
- package/dist/receipts/verify.d.ts.map +0 -1
- package/dist/receipts/verify.js.map +0 -1
- package/dist/sessions.d.ts.map +0 -1
- package/dist/sessions.js.map +0 -1
- package/dist/transport.d.ts.map +0 -1
- package/dist/transport.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/webhook-resource.d.ts.map +0 -1
- package/dist/webhook-resource.js.map +0 -1
- package/dist/webhooks/index.d.ts.map +0 -1
- package/dist/webhooks/index.js.map +0 -1
- package/src/ask.ts +0 -683
- package/src/cli/main.ts +0 -8
- package/src/cli/verify.ts +0 -349
- package/src/client.ts +0 -55
- package/src/errors.ts +0 -127
- package/src/generated/openapi.ts +0 -8586
- package/src/index.ts +0 -5
- package/src/internal/decode.ts +0 -348
- package/src/internal/json.ts +0 -64
- package/src/internal/time.ts +0 -85
- package/src/polling.ts +0 -173
- package/src/qr/encoder.ts +0 -345
- package/src/qr/index.ts +0 -203
- package/src/receipts/bundle.ts +0 -547
- package/src/receipts/index.ts +0 -29
- package/src/receipts/json.ts +0 -168
- package/src/receipts/key-cache.ts +0 -143
- package/src/receipts/renderers.ts +0 -220
- package/src/receipts/resource.ts +0 -264
- package/src/receipts/verify.ts +0 -554
- package/src/sessions.ts +0 -238
- package/src/transport.ts +0 -566
- package/src/types.ts +0 -219
- package/src/webhook-resource.ts +0 -81
- package/src/webhooks/index.ts +0 -246
package/src/index.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { Sess } from './client.js';
|
|
2
|
-
export { SessError } from './errors.js';
|
|
3
|
-
export type { SessErrorKind, SessErrorDetails, WriteOutcome } from './errors.js';
|
|
4
|
-
export type { AskAnswer, AskAnswerOptions, AskField, AskHandle, AskInput, AskOptions } from './ask.js';
|
|
5
|
-
export * from './types.js';
|
package/src/internal/decode.ts
DELETED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
import { SessError } from '../errors.js';
|
|
2
|
-
import type {
|
|
3
|
-
AcceptedResponse,
|
|
4
|
-
CreateSessionResponse,
|
|
5
|
-
Deliveries,
|
|
6
|
-
InteractiveSession,
|
|
7
|
-
Session,
|
|
8
|
-
SessionEvents,
|
|
9
|
-
SurfaceRevision,
|
|
10
|
-
WebhookRegisterResponse,
|
|
11
|
-
WebhookResponse,
|
|
12
|
-
WebhookRotateResponse,
|
|
13
|
-
WebhookVerifyResponse,
|
|
14
|
-
RedeliverResponse,
|
|
15
|
-
} from '../types.js';
|
|
16
|
-
|
|
17
|
-
export const ulid = '[0-9A-HJKMNP-TV-Z]{26}';
|
|
18
|
-
export const sessionPattern = new RegExp(`^ses_${ulid}$`);
|
|
19
|
-
const eventPattern = new RegExp(`^evt_${ulid}$`);
|
|
20
|
-
const actionPattern = /^[a-z][a-z0-9_]{0,31}$/;
|
|
21
|
-
const receiptPattern = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
|
|
22
|
-
export function protocol(): never {
|
|
23
|
-
throw new SessError('protocol', 'The response does not match the Sess API this SDK supports.', {
|
|
24
|
-
code: 'INVALID_RESPONSE',
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
export function object(value: unknown): Record<string, unknown> {
|
|
28
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) protocol();
|
|
29
|
-
return value as Record<string, unknown>;
|
|
30
|
-
}
|
|
31
|
-
export function string(value: unknown, pattern?: RegExp, max = 4096): string {
|
|
32
|
-
// The wire contract uses Unicode code points, independently of UTF-8 body caps.
|
|
33
|
-
if (
|
|
34
|
-
typeof value !== 'string' ||
|
|
35
|
-
!value.length ||
|
|
36
|
-
[...value].length > max ||
|
|
37
|
-
(pattern && !pattern.test(value))
|
|
38
|
-
)
|
|
39
|
-
protocol();
|
|
40
|
-
return value;
|
|
41
|
-
}
|
|
42
|
-
export function integer(value: unknown, minimum = 1): number {
|
|
43
|
-
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < minimum) protocol();
|
|
44
|
-
return value;
|
|
45
|
-
}
|
|
46
|
-
function date(value: unknown): void {
|
|
47
|
-
const text = string(value, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/, 32);
|
|
48
|
-
if (!Number.isFinite(Date.parse(text))) protocol();
|
|
49
|
-
}
|
|
50
|
-
function nullableDate(value: unknown): void {
|
|
51
|
-
if (value !== null) date(value);
|
|
52
|
-
}
|
|
53
|
-
function array(value: unknown): unknown[] {
|
|
54
|
-
if (!Array.isArray(value)) protocol();
|
|
55
|
-
return value;
|
|
56
|
-
}
|
|
57
|
-
function oneOf(value: unknown, values: readonly string[]): void {
|
|
58
|
-
if (typeof value !== 'string' || !values.includes(value)) protocol();
|
|
59
|
-
}
|
|
60
|
-
function receipt(value: unknown): void {
|
|
61
|
-
if (value !== null) string(value, receiptPattern, 65536);
|
|
62
|
-
}
|
|
63
|
-
function interaction(value: unknown, mode: 'display' | 'interactive'): void {
|
|
64
|
-
const v = object(value);
|
|
65
|
-
if (v.mode !== mode) protocol();
|
|
66
|
-
string(v.title, undefined, 200);
|
|
67
|
-
if (v.description !== undefined && typeof v.description !== 'string') protocol();
|
|
68
|
-
const source = object(v.source);
|
|
69
|
-
if (mode === 'display') {
|
|
70
|
-
if (source.kind !== 'data') protocol();
|
|
71
|
-
object(source.data);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (source.kind !== 'sess_surface' || v.response_policy !== 'first') protocol();
|
|
75
|
-
for (const item of array(source.components)) {
|
|
76
|
-
const c = object(item);
|
|
77
|
-
oneOf(c.type, ['heading', 'text', 'record', 'field']);
|
|
78
|
-
if (c.type === 'heading') string(c.text, undefined, 200);
|
|
79
|
-
if (c.type === 'text') string(c.text, undefined, 4000);
|
|
80
|
-
if (c.type === 'record') object(c.fields);
|
|
81
|
-
if (c.type === 'field') {
|
|
82
|
-
string(c.name, /^[a-z][a-z0-9_]{0,63}$/);
|
|
83
|
-
string(c.label, undefined, 200);
|
|
84
|
-
oneOf(object(c.schema).type, ['string', 'number', 'integer', 'boolean', 'array']);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const seen = new Set<string>();
|
|
88
|
-
for (const item of array(v.actions)) {
|
|
89
|
-
const a = object(item);
|
|
90
|
-
const id = string(a.id, actionPattern);
|
|
91
|
-
if (seen.has(id)) protocol();
|
|
92
|
-
seen.add(id);
|
|
93
|
-
string(a.label, undefined, 200);
|
|
94
|
-
if (a.intent !== undefined)
|
|
95
|
-
oneOf(a.intent, ['submit', 'continue', 'approve', 'reject', 'request_change']);
|
|
96
|
-
}
|
|
97
|
-
if (!seen.size) protocol();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Keeps all additive JSON fields intact; this is a boundary decoder, not normalization. */
|
|
101
|
-
export function decodeAccepted(value: unknown): AcceptedResponse {
|
|
102
|
-
const v = object(value);
|
|
103
|
-
string(v.event_id, eventPattern);
|
|
104
|
-
integer(v.sequence);
|
|
105
|
-
integer(v.revision);
|
|
106
|
-
string(v.action_id, actionPattern);
|
|
107
|
-
object(v.data);
|
|
108
|
-
if (object(v.actor).assurance !== 'link_holder') protocol();
|
|
109
|
-
date(v.accepted_at);
|
|
110
|
-
receipt(v.receipt);
|
|
111
|
-
return value as AcceptedResponse;
|
|
112
|
-
}
|
|
113
|
-
export function decodeCreate(value: unknown, allowInsecureLocalhost = false): CreateSessionResponse {
|
|
114
|
-
const v = object(value);
|
|
115
|
-
string(v.id, sessionPattern);
|
|
116
|
-
integer(v.revision);
|
|
117
|
-
// The link's expiry; checked when present (a runtime before the link-lifetime slice omits it).
|
|
118
|
-
if (v.action_expires_at !== undefined) date(v.action_expires_at);
|
|
119
|
-
const text = string(v.human_url, /^[^\s\u0000-\u001f\u007f]+$/, 4096);
|
|
120
|
-
let url: URL;
|
|
121
|
-
try {
|
|
122
|
-
url = new URL(text);
|
|
123
|
-
} catch {
|
|
124
|
-
return protocol();
|
|
125
|
-
}
|
|
126
|
-
if (
|
|
127
|
-
url.username ||
|
|
128
|
-
url.password ||
|
|
129
|
-
url.hash ||
|
|
130
|
-
url.search ||
|
|
131
|
-
!(
|
|
132
|
-
url.protocol === 'https:' ||
|
|
133
|
-
(allowInsecureLocalhost && url.protocol === 'http:' && isLoopback(url.hostname))
|
|
134
|
-
)
|
|
135
|
-
)
|
|
136
|
-
protocol();
|
|
137
|
-
return value as CreateSessionResponse;
|
|
138
|
-
}
|
|
139
|
-
export function isLoopback(host: string): boolean {
|
|
140
|
-
return host === 'localhost' || host === '[::1]' || /^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(host);
|
|
141
|
-
}
|
|
142
|
-
/** One accepted extension of the Sess link (Decision 0009 Amendment 10), numbered in order. */
|
|
143
|
-
function linkExtension(value: unknown, number: number): void {
|
|
144
|
-
const v = object(value);
|
|
145
|
-
if (integer(v.number) !== number) protocol();
|
|
146
|
-
date(v.previous_action_expires_at);
|
|
147
|
-
date(v.action_expires_at);
|
|
148
|
-
if (typeof v.from_lapsed !== 'boolean') protocol();
|
|
149
|
-
date(v.extended_at);
|
|
150
|
-
}
|
|
151
|
-
/** The extension history: at most ten records, numbered 1, 2, … in order. */
|
|
152
|
-
function linkExtensions(value: unknown): void {
|
|
153
|
-
const items = array(value);
|
|
154
|
-
if (items.length > 10) protocol();
|
|
155
|
-
items.forEach((item, i) => linkExtension(item, i + 1));
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* A Session read's link timing (Decision 0009 Amendment 10), checked when present. A runtime
|
|
159
|
-
* before link timing omits it; the wait then keeps its earlier rule (the deadline is final).
|
|
160
|
-
*/
|
|
161
|
-
function linkTiming(v: Record<string, unknown>): void {
|
|
162
|
-
if (v.link_state !== undefined) oneOf(v.link_state, ['live', 'lapsed', 'expired']);
|
|
163
|
-
if (v.action_expires_horizon !== undefined) nullableDate(v.action_expires_horizon);
|
|
164
|
-
if (v.extensions_used !== undefined && integer(v.extensions_used, 0) > 10) protocol();
|
|
165
|
-
if (v.extensions !== undefined) linkExtensions(v.extensions);
|
|
166
|
-
}
|
|
167
|
-
export function decodeSession(value: unknown, expectedId: string): Session {
|
|
168
|
-
const v = object(value);
|
|
169
|
-
if (string(v.id, sessionPattern) !== expectedId) protocol();
|
|
170
|
-
const rev = integer(v.revision);
|
|
171
|
-
linkTiming(v);
|
|
172
|
-
const mode = object(v.interaction).mode;
|
|
173
|
-
if (mode === 'display') {
|
|
174
|
-
interaction(v.interaction, 'display');
|
|
175
|
-
if ('status' in v || 'responses' in v) protocol();
|
|
176
|
-
return value as Session;
|
|
177
|
-
}
|
|
178
|
-
interaction(v.interaction, 'interactive');
|
|
179
|
-
oneOf(v.status, ['active', 'responded', 'completed']);
|
|
180
|
-
object(v.response_schema);
|
|
181
|
-
const seen = new Set<string>();
|
|
182
|
-
const revisions = new Set<number>();
|
|
183
|
-
let previousSequence = 0;
|
|
184
|
-
let previousRevision = 0;
|
|
185
|
-
for (const item of array(v.responses)) {
|
|
186
|
-
const r = decodeAccepted(item);
|
|
187
|
-
if (
|
|
188
|
-
r.revision > rev ||
|
|
189
|
-
r.sequence <= previousSequence ||
|
|
190
|
-
r.revision <= previousRevision ||
|
|
191
|
-
seen.has(r.event_id) ||
|
|
192
|
-
revisions.has(r.revision)
|
|
193
|
-
)
|
|
194
|
-
protocol();
|
|
195
|
-
seen.add(r.event_id);
|
|
196
|
-
revisions.add(r.revision);
|
|
197
|
-
previousSequence = r.sequence;
|
|
198
|
-
previousRevision = r.revision;
|
|
199
|
-
}
|
|
200
|
-
if (v.status === 'responded' && !revisions.has(rev)) protocol();
|
|
201
|
-
if (v.status === 'active' && revisions.has(rev)) protocol();
|
|
202
|
-
return value as InteractiveSession;
|
|
203
|
-
}
|
|
204
|
-
export function decodeSurface(value: unknown, expectedId: string, revision: number): SurfaceRevision {
|
|
205
|
-
const v = object(value);
|
|
206
|
-
if (string(v.id, sessionPattern) !== expectedId || integer(v.revision) !== revision) protocol();
|
|
207
|
-
if (object(v.interaction).mode === 'display') {
|
|
208
|
-
interaction(v.interaction, 'display');
|
|
209
|
-
if ('response_schema' in v || 'status' in v || 'responses' in v) protocol();
|
|
210
|
-
return value as SurfaceRevision;
|
|
211
|
-
}
|
|
212
|
-
interaction(v.interaction, 'interactive');
|
|
213
|
-
object(v.response_schema);
|
|
214
|
-
return value as SurfaceRevision;
|
|
215
|
-
}
|
|
216
|
-
export function decodeEvents(value: unknown, expectedId: string): SessionEvents {
|
|
217
|
-
const v = object(value);
|
|
218
|
-
if (string(v.id, sessionPattern) !== expectedId) protocol();
|
|
219
|
-
const seen = new Set<string>();
|
|
220
|
-
let seq = 0;
|
|
221
|
-
for (const item of array(v.events)) {
|
|
222
|
-
const e = object(item);
|
|
223
|
-
const id = string(e.event_id, eventPattern);
|
|
224
|
-
const next = integer(e.sequence);
|
|
225
|
-
if (seen.has(id) || next <= seq) protocol();
|
|
226
|
-
seen.add(id);
|
|
227
|
-
seq = next;
|
|
228
|
-
integer(e.revision);
|
|
229
|
-
date(e.created_at);
|
|
230
|
-
oneOf(e.type, ['response.accepted', 'revision.published', 'session.completed']);
|
|
231
|
-
const actor = object(e.actor).assurance;
|
|
232
|
-
if (e.type === 'response.accepted') {
|
|
233
|
-
if (actor !== 'link_holder') protocol();
|
|
234
|
-
string(e.action_id, actionPattern);
|
|
235
|
-
object(e.data);
|
|
236
|
-
receipt(e.receipt);
|
|
237
|
-
} else if (actor !== 'owner') protocol();
|
|
238
|
-
}
|
|
239
|
-
// Decision 0009 Amendment 10: the link's extensions are listed beside the Events (never as an
|
|
240
|
-
// Event type), only where the deployment enables them.
|
|
241
|
-
if (v.link_extensions !== undefined) linkExtensions(v.link_extensions);
|
|
242
|
-
return value as SessionEvents;
|
|
243
|
-
}
|
|
244
|
-
function endpoint(value: unknown): void {
|
|
245
|
-
const v = object(value);
|
|
246
|
-
string(v.id, new RegExp(`^whe_${ulid}$`));
|
|
247
|
-
string(v.url);
|
|
248
|
-
oneOf(v.status, ['pending', 'verified']);
|
|
249
|
-
date(v.created_at);
|
|
250
|
-
nullableDate(v.verified_at);
|
|
251
|
-
nullableDate(v.verification_expires_at);
|
|
252
|
-
}
|
|
253
|
-
function secret(value: unknown): void {
|
|
254
|
-
const v = object(value);
|
|
255
|
-
string(v.kid, new RegExp(`^wk_${ulid}$`));
|
|
256
|
-
string(v.secret, /^whsec_[A-Za-z0-9+/]{43}=$/);
|
|
257
|
-
}
|
|
258
|
-
export function decodeWebhookRegister(value: unknown): WebhookRegisterResponse {
|
|
259
|
-
const v = object(value);
|
|
260
|
-
endpoint(v.endpoint);
|
|
261
|
-
if (v.signing_secret !== null) secret(v.signing_secret);
|
|
262
|
-
return value as WebhookRegisterResponse;
|
|
263
|
-
}
|
|
264
|
-
export function decodeWebhookVerify(value: unknown): WebhookVerifyResponse {
|
|
265
|
-
const v = object(value);
|
|
266
|
-
endpoint(v.endpoint);
|
|
267
|
-
if (object(v.endpoint).status !== 'verified' || object(v.endpoint).verified_at === null) protocol();
|
|
268
|
-
return value as WebhookVerifyResponse;
|
|
269
|
-
}
|
|
270
|
-
export function decodeWebhook(value: unknown): WebhookResponse {
|
|
271
|
-
const v = object(value);
|
|
272
|
-
if (v.endpoint !== null) endpoint(v.endpoint);
|
|
273
|
-
for (const item of array(v.signing_secrets)) {
|
|
274
|
-
const s = object(item);
|
|
275
|
-
string(s.kid);
|
|
276
|
-
if (typeof s.active !== 'boolean') protocol();
|
|
277
|
-
nullableDate(s.retire_at);
|
|
278
|
-
}
|
|
279
|
-
const d = object(v.deliveries);
|
|
280
|
-
integer(d.pending, 0);
|
|
281
|
-
integer(d.failed, 0);
|
|
282
|
-
integer(d.delivered, 0);
|
|
283
|
-
if (d.oldest_pending_age_seconds !== null) integer(d.oldest_pending_age_seconds, 0);
|
|
284
|
-
return value as WebhookResponse;
|
|
285
|
-
}
|
|
286
|
-
export function decodeWebhookRotate(value: unknown): WebhookRotateResponse {
|
|
287
|
-
const v = object(value);
|
|
288
|
-
secret(v.signing_secret);
|
|
289
|
-
if (v.previous !== null) {
|
|
290
|
-
const p = object(v.previous);
|
|
291
|
-
string(p.kid);
|
|
292
|
-
date(p.retire_at);
|
|
293
|
-
}
|
|
294
|
-
return value as WebhookRotateResponse;
|
|
295
|
-
}
|
|
296
|
-
const outcomes = [
|
|
297
|
-
'started',
|
|
298
|
-
'delivered',
|
|
299
|
-
'server_error',
|
|
300
|
-
'rate_limited',
|
|
301
|
-
'timeout',
|
|
302
|
-
'connection',
|
|
303
|
-
'dns',
|
|
304
|
-
'signing_unavailable',
|
|
305
|
-
'request_timeout',
|
|
306
|
-
'too_early',
|
|
307
|
-
'rejected',
|
|
308
|
-
'redirect',
|
|
309
|
-
'egress_refused',
|
|
310
|
-
'endpoint_unavailable',
|
|
311
|
-
'exhausted',
|
|
312
|
-
];
|
|
313
|
-
export function decodeDeliveries(value: unknown, expectedId: string): Deliveries {
|
|
314
|
-
const v = object(value);
|
|
315
|
-
if (string(v.id) !== expectedId) protocol();
|
|
316
|
-
for (const item of array(v.deliveries)) decodeDelivery(item);
|
|
317
|
-
return value as Deliveries;
|
|
318
|
-
}
|
|
319
|
-
function decodeDelivery(item: unknown): void {
|
|
320
|
-
const d = object(item);
|
|
321
|
-
string(d.delivery_id, new RegExp(`^obx_${ulid}$`));
|
|
322
|
-
string(d.event_id, eventPattern);
|
|
323
|
-
string(d.endpoint_id);
|
|
324
|
-
oneOf(d.kind, ['accepted', 'redelivery']);
|
|
325
|
-
oneOf(d.status, ['pending', 'delivered', 'failed']);
|
|
326
|
-
nullableDate(d.next_attempt_at);
|
|
327
|
-
date(d.deadline_at);
|
|
328
|
-
date(d.created_at);
|
|
329
|
-
nullableDate(d.finished_at);
|
|
330
|
-
if (d.last_outcome !== null) oneOf(d.last_outcome, outcomes);
|
|
331
|
-
for (const item of array(d.attempts)) {
|
|
332
|
-
const a = object(item);
|
|
333
|
-
integer(a.attempt);
|
|
334
|
-
date(a.started_at);
|
|
335
|
-
nullableDate(a.finished_at);
|
|
336
|
-
oneOf(a.outcome, outcomes);
|
|
337
|
-
if (a.http_status !== null) {
|
|
338
|
-
const status = integer(a.http_status, 100);
|
|
339
|
-
if (status > 599) protocol();
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
export function decodeRedelivery(value: unknown, expectedEvent: string): RedeliverResponse {
|
|
344
|
-
const v = object(value);
|
|
345
|
-
decodeDelivery(v.delivery);
|
|
346
|
-
if (object(v.delivery).event_id !== expectedEvent) protocol();
|
|
347
|
-
return value as RedeliverResponse;
|
|
348
|
-
}
|
package/src/internal/json.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { SessError } from '../errors.js';
|
|
2
|
-
|
|
3
|
-
/** Copy only ordinary JSON, never invoke accessors or toJSON coercions. */
|
|
4
|
-
export function snapshotJson<T>(input: T): T {
|
|
5
|
-
const active = new Set<object>();
|
|
6
|
-
function copy(value: unknown, depth: number): unknown {
|
|
7
|
-
if (depth > 128) throw new Error();
|
|
8
|
-
if (value === null || typeof value === 'string' || typeof value === 'boolean') return value;
|
|
9
|
-
if (typeof value === 'number') {
|
|
10
|
-
if (!Number.isFinite(value) || Object.is(value, -0)) throw new Error();
|
|
11
|
-
return value;
|
|
12
|
-
}
|
|
13
|
-
if (typeof value !== 'object' || active.has(value)) throw new Error();
|
|
14
|
-
if (Object.getOwnPropertySymbols(value).length) throw new Error();
|
|
15
|
-
// Hidden toJSON and inherited Array-subclass semantics must not be silently erased.
|
|
16
|
-
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
17
|
-
for (const [name, descriptor] of Object.entries(descriptors)) {
|
|
18
|
-
if (!('value' in descriptor) || (name === 'toJSON' && typeof descriptor.value === 'function'))
|
|
19
|
-
throw new Error();
|
|
20
|
-
}
|
|
21
|
-
active.add(value);
|
|
22
|
-
try {
|
|
23
|
-
if (Array.isArray(value)) {
|
|
24
|
-
if (Object.getPrototypeOf(value) !== Array.prototype) throw new Error();
|
|
25
|
-
if (Object.keys(value).length !== value.length) throw new Error();
|
|
26
|
-
return Array.from({ length: value.length }, (_, i) => {
|
|
27
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, String(i));
|
|
28
|
-
if (!descriptor || !('value' in descriptor)) throw new Error();
|
|
29
|
-
return copy(descriptor.value, depth + 1);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null)
|
|
33
|
-
throw new Error();
|
|
34
|
-
const result: Record<string, unknown> = Object.create(null) as Record<string, unknown>;
|
|
35
|
-
for (const key of Object.keys(value)) {
|
|
36
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, key)!;
|
|
37
|
-
if (!('value' in descriptor)) throw new Error();
|
|
38
|
-
if (descriptor.value !== undefined) result[key] = copy(descriptor.value, depth + 1);
|
|
39
|
-
}
|
|
40
|
-
return result;
|
|
41
|
-
} finally {
|
|
42
|
-
active.delete(value);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
return copy(input, 0) as T;
|
|
47
|
-
} catch {
|
|
48
|
-
throw new SessError('configuration', 'Request must contain ordinary JSON values without coercion.', {
|
|
49
|
-
code: 'INVALID_JSON',
|
|
50
|
-
outcome: 'not_sent',
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function serializeBody(input: unknown): string {
|
|
56
|
-
const serialized = JSON.stringify(snapshotJson(input));
|
|
57
|
-
if (Buffer.byteLength(serialized, 'utf8') > 256 * 1024) {
|
|
58
|
-
throw new SessError('configuration', 'Request body exceeds the 256 KiB limit.', {
|
|
59
|
-
code: 'PAYLOAD_TOO_LARGE',
|
|
60
|
-
outcome: 'not_sent',
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
return serialized;
|
|
64
|
-
}
|
package/src/internal/time.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { SessError } from '../errors.js';
|
|
2
|
-
|
|
3
|
-
export function boundedNumber(
|
|
4
|
-
value: number | undefined,
|
|
5
|
-
fallback: number,
|
|
6
|
-
name: string,
|
|
7
|
-
min = 1,
|
|
8
|
-
max = 2_147_483_647,
|
|
9
|
-
): number {
|
|
10
|
-
const result = value ?? fallback;
|
|
11
|
-
if (!Number.isSafeInteger(result) || result < min || result > max) {
|
|
12
|
-
throw new SessError('configuration', `${name} is outside its supported range.`, { outcome: 'not_sent' });
|
|
13
|
-
}
|
|
14
|
-
return result;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function deadline(ms: number, parent?: AbortSignal): { signal: AbortSignal; dispose(): void } {
|
|
18
|
-
const controller = new AbortController();
|
|
19
|
-
const abort = () => controller.abort(parent?.reason);
|
|
20
|
-
if (parent?.aborted) abort();
|
|
21
|
-
else parent?.addEventListener('abort', abort, { once: true });
|
|
22
|
-
const timer = setTimeout(
|
|
23
|
-
() => controller.abort(new SessError('timeout', 'Operation deadline exceeded.')),
|
|
24
|
-
ms,
|
|
25
|
-
);
|
|
26
|
-
return {
|
|
27
|
-
signal: controller.signal,
|
|
28
|
-
dispose() {
|
|
29
|
-
clearTimeout(timer);
|
|
30
|
-
parent?.removeEventListener('abort', abort);
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function abortError(signal: AbortSignal): SessError {
|
|
36
|
-
return signal.reason instanceof SessError && signal.reason.kind === 'timeout'
|
|
37
|
-
? new SessError('timeout', 'Operation deadline exceeded.')
|
|
38
|
-
: new SessError('aborted', 'Operation was aborted. Remote work is not cancelled.');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Enforce deadlines even when a custom fetch/provider fails to observe its signal. */
|
|
42
|
-
export function withSignal<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
43
|
-
return new Promise<T>((resolve, reject) => {
|
|
44
|
-
const cleanup = () => signal.removeEventListener('abort', abort);
|
|
45
|
-
const abort = () => {
|
|
46
|
-
cleanup();
|
|
47
|
-
reject(abortError(signal));
|
|
48
|
-
};
|
|
49
|
-
if (signal.aborted) {
|
|
50
|
-
promise.catch(() => {});
|
|
51
|
-
abort();
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
signal.addEventListener('abort', abort, { once: true });
|
|
55
|
-
promise.then(
|
|
56
|
-
(value) => {
|
|
57
|
-
cleanup();
|
|
58
|
-
resolve(value);
|
|
59
|
-
},
|
|
60
|
-
(error: unknown) => {
|
|
61
|
-
cleanup();
|
|
62
|
-
reject(error);
|
|
63
|
-
},
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function delay(ms: number, signal: AbortSignal): Promise<void> {
|
|
69
|
-
return new Promise((resolve, reject) => {
|
|
70
|
-
if (signal.aborted) {
|
|
71
|
-
reject(abortError(signal));
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
const abort = () => {
|
|
75
|
-
clearTimeout(timer);
|
|
76
|
-
signal.removeEventListener('abort', abort);
|
|
77
|
-
reject(abortError(signal));
|
|
78
|
-
};
|
|
79
|
-
const timer = setTimeout(() => {
|
|
80
|
-
signal.removeEventListener('abort', abort);
|
|
81
|
-
resolve();
|
|
82
|
-
}, ms);
|
|
83
|
-
signal.addEventListener('abort', abort, { once: true });
|
|
84
|
-
});
|
|
85
|
-
}
|
package/src/polling.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { SessError } from './errors.js';
|
|
2
|
-
import { abortError, boundedNumber, deadline, delay } from './internal/time.js';
|
|
3
|
-
import { isInteractiveSession } from './types.js';
|
|
4
|
-
import type {
|
|
5
|
-
InteractiveSession,
|
|
6
|
-
RequestOptions,
|
|
7
|
-
Session,
|
|
8
|
-
VersionedResult,
|
|
9
|
-
WaitOptions,
|
|
10
|
-
WaitResult,
|
|
11
|
-
} from './types.js';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Once a Session's link has reached its end (its deadline, `action_expires_at`, or while the
|
|
15
|
-
* person can still extend it, its horizon), the wait reads it once more when the runtime clock is
|
|
16
|
-
* this far past that end, then ends: an answer accepted before the end is returned; otherwise the
|
|
17
|
-
* wait ends with `LINK_EXPIRED`. Two seconds covers the one-second
|
|
18
|
-
* resolution of the HTTP `Date` header plus an answer accepted just before the expiry whose write
|
|
19
|
-
* is still landing, and keeps the end prompt.
|
|
20
|
-
*/
|
|
21
|
-
export const LINK_EXPIRY_GRACE_MS = 2000;
|
|
22
|
-
|
|
23
|
-
/** An RFC 3339 UTC instant in milliseconds since the epoch, when the value is a valid one. */
|
|
24
|
-
function instant(value: unknown): number | undefined {
|
|
25
|
-
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/.test(value))
|
|
26
|
-
return undefined;
|
|
27
|
-
const ms = Date.parse(value);
|
|
28
|
-
return Number.isFinite(ms) ? ms : undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The link's state as read and the instant it ends for good as far as this read shows: its
|
|
33
|
-
* horizon while the person can still extend it (an extension left), else its deadline
|
|
34
|
-
* (`action_expires_at`). No state from a runtime before link timing (Decision 0009 Amendment 10).
|
|
35
|
-
*/
|
|
36
|
-
function linkEnd(session: InteractiveSession): { state: string | undefined; endsAt: number | undefined } {
|
|
37
|
-
const v = session as {
|
|
38
|
-
action_expires_at?: unknown;
|
|
39
|
-
action_expires_horizon?: unknown;
|
|
40
|
-
extensions_used?: unknown;
|
|
41
|
-
link_state?: unknown;
|
|
42
|
-
};
|
|
43
|
-
const deadline = instant(v.action_expires_at);
|
|
44
|
-
const horizon = instant(v.action_expires_horizon);
|
|
45
|
-
const extendable = horizon !== undefined && typeof v.extensions_used === 'number' && v.extensions_used < 10;
|
|
46
|
-
return {
|
|
47
|
-
state: typeof v.link_state === 'string' ? v.link_state : undefined,
|
|
48
|
-
endsAt: extendable && deadline !== undefined ? Math.max(horizon, deadline) : deadline,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function waitForResponse(
|
|
53
|
-
retrieve: (options: RequestOptions) => Promise<VersionedResult<Session>>,
|
|
54
|
-
id: string,
|
|
55
|
-
input: WaitOptions,
|
|
56
|
-
clientOperationTimeoutMs: number,
|
|
57
|
-
): Promise<WaitResult> {
|
|
58
|
-
if (!input || typeof input !== 'object')
|
|
59
|
-
throw new SessError('configuration', 'waitForResponse requires an exact revision.');
|
|
60
|
-
const options = { ...input };
|
|
61
|
-
const revision = boundedNumber(options.revision, 0, 'revision');
|
|
62
|
-
const timeoutMs = boundedNumber(options.timeoutMs, 10 * 60_000, 'timeoutMs');
|
|
63
|
-
const intervalMs = boundedNumber(options.intervalMs, 1500, 'intervalMs', 1000);
|
|
64
|
-
if (options.operationTimeoutMs !== undefined && typeof options.operationTimeoutMs !== 'number')
|
|
65
|
-
throw new SessError('configuration', 'operationTimeoutMs must be a positive integer duration.', {
|
|
66
|
-
outcome: 'not_sent',
|
|
67
|
-
});
|
|
68
|
-
// Validate before clamping so malformed overrides cannot become usable durations.
|
|
69
|
-
const operationTimeoutMs = boundedNumber(
|
|
70
|
-
options.operationTimeoutMs,
|
|
71
|
-
clientOperationTimeoutMs,
|
|
72
|
-
'operationTimeoutMs',
|
|
73
|
-
);
|
|
74
|
-
const budget = deadline(timeoutMs, options.signal);
|
|
75
|
-
const expires = performance.now() + timeoutMs;
|
|
76
|
-
/**
|
|
77
|
-
* The first read in a row that showed the link expired with no answer at an instant the clocks
|
|
78
|
-
* cannot place past its end: its runtime clock, that clock's resolution, and whether the clock
|
|
79
|
-
* was the runtime's (HTTP Date) or this machine's. Such a read is final only once a later one,
|
|
80
|
-
* at least LINK_EXPIRY_GRACE_MS (plus the resolution) after it on the same clock, still shows it.
|
|
81
|
-
*/
|
|
82
|
-
let expiredSince: { at: number; resolutionMs: number; server: boolean } | undefined;
|
|
83
|
-
const ended = (code: string, message: string) =>
|
|
84
|
-
new SessError('wait_ended', message, { code, sessionId: id, revision });
|
|
85
|
-
try {
|
|
86
|
-
for (;;) {
|
|
87
|
-
if (budget.signal.aborted) throw abortError(budget.signal);
|
|
88
|
-
const remainingMs = Math.ceil(expires - performance.now());
|
|
89
|
-
if (remainingMs <= 0)
|
|
90
|
-
throw ended('WAIT_TIMEOUT', 'Human wait timed out. Resume using the same Session and revision.');
|
|
91
|
-
const current = await retrieve({
|
|
92
|
-
signal: budget.signal,
|
|
93
|
-
operationTimeoutMs: Math.min(operationTimeoutMs, remainingMs),
|
|
94
|
-
maxRetries: options.maxRetries,
|
|
95
|
-
maxResponseBytes: options.maxResponseBytes,
|
|
96
|
-
});
|
|
97
|
-
if (!isInteractiveSession(current.data))
|
|
98
|
-
throw ended('NOT_INTERACTIVE', 'A display Session has no accepted human responses.');
|
|
99
|
-
const session = current.data;
|
|
100
|
-
const response = session.responses.find((item) => item.revision === revision);
|
|
101
|
-
// Retained history remains readable after a later revision or completion.
|
|
102
|
-
if (response) return { session, response, meta: current.meta };
|
|
103
|
-
if (session.status === 'completed')
|
|
104
|
-
throw ended(
|
|
105
|
-
'CLOSED_WITHOUT_RESPONSE',
|
|
106
|
-
'Session completed without an accepted response for this revision.',
|
|
107
|
-
);
|
|
108
|
-
if (session.revision > revision)
|
|
109
|
-
throw ended('REVISION_SUPERSEDED', 'The requested unanswered revision was superseded.');
|
|
110
|
-
// The link's end (Decision 0009 Amendments 7 and 10): an answer accepted while the link
|
|
111
|
-
// was live is in every later read. The runtime clock is the response's HTTP Date (at or
|
|
112
|
-
// before the read), else this machine's. Nothing is completed here.
|
|
113
|
-
// - live or lapsed: the Session is still open (a lapsed link takes no answer, but the
|
|
114
|
-
// person can still ask for more time until the horizon). The wait never ends on the
|
|
115
|
-
// link; it reads again soon after the link could end.
|
|
116
|
-
// - expired: final once a read made at least LINK_EXPIRY_GRACE_MS past the link's end, or a
|
|
117
|
-
// read at least the grace after the first expired read on the same clock (an end the
|
|
118
|
-
// clocks cannot place, such as a revocation), still shows it expired with no answer. A
|
|
119
|
-
// short polling interval reads again sooner but never ends the wait inside the grace.
|
|
120
|
-
// - no link_state (a runtime before link timing): the deadline is final, as before.
|
|
121
|
-
let untilFinalReadMs = Number.POSITIVE_INFINITY;
|
|
122
|
-
const link = linkEnd(session);
|
|
123
|
-
const observedAt = current.meta.serverDate ?? Date.now();
|
|
124
|
-
// A Date header has whole seconds: one more second puts the next read past the instant.
|
|
125
|
-
const resolutionMs = current.meta.serverDate === undefined ? 0 : 1000;
|
|
126
|
-
const server = current.meta.serverDate !== undefined;
|
|
127
|
-
if (link.state !== 'expired' || (expiredSince !== undefined && expiredSince.server !== server))
|
|
128
|
-
expiredSince = undefined;
|
|
129
|
-
if (link.state === 'live' || link.state === 'lapsed') {
|
|
130
|
-
if (link.endsAt !== undefined && link.endsAt + LINK_EXPIRY_GRACE_MS > observedAt)
|
|
131
|
-
untilFinalReadMs = link.endsAt + LINK_EXPIRY_GRACE_MS - observedAt + resolutionMs;
|
|
132
|
-
} else if (link.state === 'expired' || link.endsAt !== undefined) {
|
|
133
|
-
const finalAt = (link.endsAt ?? observedAt) + LINK_EXPIRY_GRACE_MS;
|
|
134
|
-
// Whole-second Date headers: a full grace plus one second between the two headers puts
|
|
135
|
-
// more than the grace between the two reads.
|
|
136
|
-
const graceSeenAt =
|
|
137
|
-
expiredSince === undefined
|
|
138
|
-
? Number.POSITIVE_INFINITY
|
|
139
|
-
: expiredSince.at + LINK_EXPIRY_GRACE_MS + expiredSince.resolutionMs;
|
|
140
|
-
if (observedAt >= finalAt || observedAt >= graceSeenAt)
|
|
141
|
-
throw ended(
|
|
142
|
-
'LINK_EXPIRED',
|
|
143
|
-
'The Sess link expired with no accepted response for this revision. Nothing was completed.',
|
|
144
|
-
);
|
|
145
|
-
untilFinalReadMs = finalAt - observedAt + resolutionMs;
|
|
146
|
-
if (link.state === 'expired') {
|
|
147
|
-
expiredSince ??= { at: observedAt, resolutionMs, server };
|
|
148
|
-
const graceEndsAt = expiredSince.at + LINK_EXPIRY_GRACE_MS + expiredSince.resolutionMs;
|
|
149
|
-
untilFinalReadMs = Math.min(untilFinalReadMs, Math.max(1, graceEndsAt - observedAt));
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
await delay(
|
|
153
|
-
Math.min(
|
|
154
|
-
intervalMs + Math.floor(Math.random() * 150),
|
|
155
|
-
untilFinalReadMs,
|
|
156
|
-
Math.max(1, Math.ceil(expires - performance.now())),
|
|
157
|
-
),
|
|
158
|
-
budget.signal,
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
} catch (error) {
|
|
162
|
-
if (options.signal?.aborted)
|
|
163
|
-
throw new SessError('aborted', 'Human wait was aborted. The Session remains available.', {
|
|
164
|
-
sessionId: id,
|
|
165
|
-
revision,
|
|
166
|
-
});
|
|
167
|
-
if (budget.signal.aborted || performance.now() >= expires)
|
|
168
|
-
throw ended('WAIT_TIMEOUT', 'Human wait timed out. Resume using the same Session and revision.');
|
|
169
|
-
throw error;
|
|
170
|
-
} finally {
|
|
171
|
-
budget.dispose();
|
|
172
|
-
}
|
|
173
|
-
}
|