@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/types.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import type { components } from './generated/openapi.js';
|
|
2
|
-
|
|
3
|
-
/** Unmodified types generated from the reviewed public HTTP contract. */
|
|
4
|
-
export type { components, paths, operations } from './generated/openapi.js';
|
|
5
|
-
type Schemas = components['schemas'];
|
|
6
|
-
export type RawCreateSessionRequest = Schemas['CreateSessionRequest'];
|
|
7
|
-
export type RawPublishSessionRequest = Schemas['PatchSessionRequest'];
|
|
8
|
-
export type CreateSessionResponse = Schemas['CreateSessionResponse'];
|
|
9
|
-
export type DisplaySession = Schemas['SessionResponse'];
|
|
10
|
-
export type InteractiveSession = Schemas['InteractiveSessionResponse'];
|
|
11
|
-
export type Session = DisplaySession | InteractiveSession;
|
|
12
|
-
export type DisplaySurfaceRevision = Schemas['SessionResponse'];
|
|
13
|
-
export type InteractiveSurfaceRevision = Schemas['InteractiveSurfaceResponse'];
|
|
14
|
-
export type SurfaceRevision = DisplaySurfaceRevision | InteractiveSurfaceRevision;
|
|
15
|
-
export type AcceptedResponse = InteractiveSession['responses'][number];
|
|
16
|
-
export type SessionEvents = Schemas['SessionEventsResponse'];
|
|
17
|
-
export type SessionEvent = SessionEvents['events'][number];
|
|
18
|
-
/**
|
|
19
|
-
* The Sess link's state at the time of a Session read (Decision 0009 Amendment 10): `live` takes
|
|
20
|
-
* answers; `lapsed` is past its deadline but the person can still ask for more time, so the
|
|
21
|
-
* Session is still open; `expired` will never take an answer.
|
|
22
|
-
*/
|
|
23
|
-
export type LinkState = InteractiveSession['link_state'];
|
|
24
|
-
/**
|
|
25
|
-
* One accepted extension of the Sess link: on Session reads (`extensions`) and on the Session log
|
|
26
|
-
* (`link_extensions`). It carries deadlines only, never an answer value.
|
|
27
|
-
*/
|
|
28
|
-
export type LinkExtension = InteractiveSession['extensions'][number];
|
|
29
|
-
export type Deliveries = Schemas['DeliveriesResponse'];
|
|
30
|
-
export type WebhookRegisterRequest = Schemas['WebhookRegisterRequest'];
|
|
31
|
-
export type WebhookRegisterResponse = Schemas['WebhookRegisterResponse'];
|
|
32
|
-
export type WebhookResponse = Schemas['WebhookResponse'];
|
|
33
|
-
export type WebhookRotateResponse = Schemas['WebhookRotateResponse'];
|
|
34
|
-
export type WebhookVerifyResponse = Schemas['WebhookVerifyResponse'];
|
|
35
|
-
export type RedeliverResponse = Schemas['RedeliverResponse'];
|
|
36
|
-
export type WebhookDelivery = Schemas['WebhookDelivery'];
|
|
37
|
-
export type WebhookVerification = Schemas['WebhookVerification'];
|
|
38
|
-
export type DisplayInteraction = DisplaySession['interaction'];
|
|
39
|
-
export type Action = InteractiveSession['interaction']['actions'][number];
|
|
40
|
-
|
|
41
|
-
type SchemaDocumentation = { title?: string; description?: string };
|
|
42
|
-
/** The documented restricted field-schema subset; the runtime validates it. */
|
|
43
|
-
export type FieldSchema = SchemaDocumentation &
|
|
44
|
-
(
|
|
45
|
-
| { type: 'string'; minLength?: number; maxLength?: number; enum?: string[] }
|
|
46
|
-
| {
|
|
47
|
-
type: 'number' | 'integer';
|
|
48
|
-
minimum?: number;
|
|
49
|
-
maximum?: number;
|
|
50
|
-
exclusiveMinimum?: number;
|
|
51
|
-
exclusiveMaximum?: number;
|
|
52
|
-
}
|
|
53
|
-
| { type: 'boolean' }
|
|
54
|
-
| {
|
|
55
|
-
type: 'array';
|
|
56
|
-
items: { type: 'string'; enum: string[] };
|
|
57
|
-
minItems?: number;
|
|
58
|
-
maxItems?: number;
|
|
59
|
-
uniqueItems?: true;
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
type WireComponent = Schemas['SurfaceComponent'];
|
|
63
|
-
type Component<T extends WireComponent['type']> = Extract<WireComponent, { type: T }>;
|
|
64
|
-
/**
|
|
65
|
-
* One piece of an interactive Surface, shown in order. Lengths are Unicode code points; Sess
|
|
66
|
-
* never truncates: a value over its limit is a 400 INVALID_SCHEMA whose `field`, `limit` and
|
|
67
|
-
* `length` (on `SessError`) name the value and the most it may have.
|
|
68
|
-
*/
|
|
69
|
-
export type SurfaceComponent =
|
|
70
|
-
| (Component<'heading'> & {
|
|
71
|
-
/** A section heading: a few words; at most 60. Example: `Order details` */
|
|
72
|
-
text: string;
|
|
73
|
-
})
|
|
74
|
-
| (Component<'text'> & {
|
|
75
|
-
/**
|
|
76
|
-
* A paragraph of plain text (never markup): what the person needs to know; at most 4000.
|
|
77
|
-
* Example: `The refund goes back to the card used for the order.`
|
|
78
|
-
*/
|
|
79
|
-
text: string;
|
|
80
|
-
})
|
|
81
|
-
| (Component<'record'> & {
|
|
82
|
-
/** Optional caption above the record: a few words; at most 200. Example: `Order 1042` */
|
|
83
|
-
label?: string;
|
|
84
|
-
/**
|
|
85
|
-
* Rows shown verbatim, 1–50. A row's name: a word or two; at most 40 (example `Amount`).
|
|
86
|
-
* A row's value: a string of at most 200, or a number, boolean or null (example `$120.00`).
|
|
87
|
-
*/
|
|
88
|
-
fields: { [key: string]: string | number | boolean | null };
|
|
89
|
-
})
|
|
90
|
-
| (Omit<Component<'field'>, 'schema'> & {
|
|
91
|
-
/** What the field asks for: short, a few words; at most 60. Example: `Refund amount` */
|
|
92
|
-
label: string;
|
|
93
|
-
/**
|
|
94
|
-
* Optional help under the label: one hint; at most 160.
|
|
95
|
-
* Example: `Leave it as is to refund the full amount.`
|
|
96
|
-
*/
|
|
97
|
-
description?: string;
|
|
98
|
-
/** The words shown for one choice: short; at most 60. Example: `Next Monday` */
|
|
99
|
-
option_labels?: { [key: string]: string };
|
|
100
|
-
schema: FieldSchema;
|
|
101
|
-
});
|
|
102
|
-
type DocumentedAction = RawPublishSessionRequest['interaction']['actions'][number] & {
|
|
103
|
-
/** A button: a verb, 1–3 words; at most 24. Examples: `Approve`, `Send refund` */
|
|
104
|
-
label: string;
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* An interactive Surface: a title, plain-text components and typed fields, and 1–5 actions.
|
|
108
|
-
* Limits in Unicode code points: title 80, description 280, heading 60, text 4000, record
|
|
109
|
-
* caption 200, row name 40, row string value 200, field label 60, field help 160, option label
|
|
110
|
-
* 60, button 24; at most 5 actions.
|
|
111
|
-
*/
|
|
112
|
-
export type InteractiveInteraction = Omit<RawPublishSessionRequest['interaction'], 'source' | 'actions'> & {
|
|
113
|
-
/**
|
|
114
|
-
* The page's heading: a question or a task, about 40 characters; at most 80.
|
|
115
|
-
* Examples: `Approve the refund?`, `Pick a delivery date`
|
|
116
|
-
*/
|
|
117
|
-
title: string;
|
|
118
|
-
/**
|
|
119
|
-
* Optional context under the title: what the person decides and why, in one or two plain
|
|
120
|
-
* sentences; at most 280. Example: `The customer was charged twice for order 1042.`
|
|
121
|
-
*/
|
|
122
|
-
description?: string;
|
|
123
|
-
source: { kind: 'sess_surface'; components: SurfaceComponent[] };
|
|
124
|
-
/** The buttons, 1–5. A button: a verb, 1–3 words; at most 24. */
|
|
125
|
-
actions: DocumentedAction[];
|
|
126
|
-
};
|
|
127
|
-
export type CreateSessionRequest = {
|
|
128
|
-
interaction: DisplayInteraction | InteractiveInteraction;
|
|
129
|
-
/**
|
|
130
|
-
* Advanced: how long the human link stays open, in whole seconds from creation: from 30 up to
|
|
131
|
-
* the plan's link lifetime (the runtime checks the ceiling). Absent: the plan's lifetime. The
|
|
132
|
-
* create response's `action_expires_at` is the link's deadline. On an interactive link of 20
|
|
133
|
-
* hours or less the person may move it forward, up to the horizon a Session read reports
|
|
134
|
-
* (`action_expires_horizon`, `link_state`); nothing else moves it.
|
|
135
|
-
*/
|
|
136
|
-
expires_in?: number;
|
|
137
|
-
};
|
|
138
|
-
export type PublishSessionRequest = { interaction: InteractiveInteraction };
|
|
139
|
-
|
|
140
|
-
export interface ResponseMeta {
|
|
141
|
-
status: number;
|
|
142
|
-
requestId?: string;
|
|
143
|
-
etag?: string;
|
|
144
|
-
/** Authenticated resource workspace, when supplied by the runtime. */
|
|
145
|
-
workspaceId?: string;
|
|
146
|
-
attempts: number;
|
|
147
|
-
idempotencyKey?: string;
|
|
148
|
-
/**
|
|
149
|
-
* The response's HTTP `Date` in milliseconds since the epoch (whole seconds), when the runtime
|
|
150
|
-
* sent one: the runtime clock at or before the response. Used to observe link expiry.
|
|
151
|
-
*/
|
|
152
|
-
serverDate?: number;
|
|
153
|
-
}
|
|
154
|
-
export interface ApiResult<T> {
|
|
155
|
-
data: T;
|
|
156
|
-
meta: ResponseMeta;
|
|
157
|
-
}
|
|
158
|
-
export interface VersionedResult<T> extends ApiResult<T> {
|
|
159
|
-
meta: ResponseMeta & { etag: string };
|
|
160
|
-
}
|
|
161
|
-
export interface RequestEvent {
|
|
162
|
-
operation: string;
|
|
163
|
-
status?: number;
|
|
164
|
-
code?: string;
|
|
165
|
-
durationMs: number;
|
|
166
|
-
attempt: number;
|
|
167
|
-
requestId?: string;
|
|
168
|
-
}
|
|
169
|
-
export interface RequestOptions {
|
|
170
|
-
signal?: AbortSignal;
|
|
171
|
-
/** Deadline per attempt, including its response body. */
|
|
172
|
-
timeoutMs?: number;
|
|
173
|
-
/** Total logical-operation deadline, including credential resolution and retry delays. */
|
|
174
|
-
operationTimeoutMs?: number;
|
|
175
|
-
maxRetries?: number;
|
|
176
|
-
maxResponseBytes?: number;
|
|
177
|
-
}
|
|
178
|
-
export interface MutationOptions extends RequestOptions {
|
|
179
|
-
ifMatch: string;
|
|
180
|
-
/** Persist with the original body/If-Match. Primitive string matching ^[A-Za-z0-9_.:-]{1,128}$. */
|
|
181
|
-
idempotencyKey?: string;
|
|
182
|
-
}
|
|
183
|
-
export interface WaitOptions extends Omit<RequestOptions, 'timeoutMs'> {
|
|
184
|
-
revision: number;
|
|
185
|
-
/** Total human wait, default ten minutes. */
|
|
186
|
-
timeoutMs?: number;
|
|
187
|
-
intervalMs?: number;
|
|
188
|
-
/** Each poll inherits the client's logical-operation budget unless overridden, bounded by the remaining human wait. */
|
|
189
|
-
operationTimeoutMs?: number;
|
|
190
|
-
}
|
|
191
|
-
export interface WaitResult {
|
|
192
|
-
session: InteractiveSession;
|
|
193
|
-
response: AcceptedResponse;
|
|
194
|
-
/** The ETag is for this current snapshot, even when response is historical. */
|
|
195
|
-
meta: ResponseMeta & { etag: string };
|
|
196
|
-
}
|
|
197
|
-
export interface SessOptions {
|
|
198
|
-
apiKey: string | undefined | (() => string | Promise<string>);
|
|
199
|
-
baseUrl?: string;
|
|
200
|
-
fetch?: typeof globalThis.fetch;
|
|
201
|
-
timeoutMs?: number;
|
|
202
|
-
operationTimeoutMs?: number;
|
|
203
|
-
maxRetries?: number;
|
|
204
|
-
maxResponseBytes?: number;
|
|
205
|
-
receiptIssuer?: string;
|
|
206
|
-
expectedWorkspaceId?: string;
|
|
207
|
-
allowInsecureLocalhost?: boolean;
|
|
208
|
-
onRequestEvent?: (event: Readonly<RequestEvent>) => void;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/** Narrow a retrieved display/interactive Session without asserting business data. */
|
|
212
|
-
export function isInteractiveSession(session: Session): session is InteractiveSession {
|
|
213
|
-
return 'status' in session && session.interaction.mode === 'interactive';
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/** Narrow immutable content before accessing interactive-only schema or actions. */
|
|
217
|
-
export function isInteractiveSurface(surface: SurfaceRevision): surface is InteractiveSurfaceRevision {
|
|
218
|
-
return 'response_schema' in surface && surface.interaction.mode === 'interactive';
|
|
219
|
-
}
|
package/src/webhook-resource.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Transport } from './transport.js';
|
|
2
|
-
import {
|
|
3
|
-
decodeWebhook,
|
|
4
|
-
decodeWebhookRegister,
|
|
5
|
-
decodeWebhookRotate,
|
|
6
|
-
decodeWebhookVerify,
|
|
7
|
-
} from './internal/decode.js';
|
|
8
|
-
import type {
|
|
9
|
-
ApiResult,
|
|
10
|
-
RequestOptions,
|
|
11
|
-
WebhookRegisterRequest,
|
|
12
|
-
WebhookRegisterResponse,
|
|
13
|
-
WebhookResponse,
|
|
14
|
-
WebhookRotateResponse,
|
|
15
|
-
WebhookVerifyResponse,
|
|
16
|
-
} from './types.js';
|
|
17
|
-
|
|
18
|
-
export class WebhookResource {
|
|
19
|
-
#transport: Transport;
|
|
20
|
-
constructor(transport: Transport) {
|
|
21
|
-
this.#transport = transport;
|
|
22
|
-
}
|
|
23
|
-
/** Replaces the workspace endpoint. Store any returned signing secret privately. */
|
|
24
|
-
register(
|
|
25
|
-
body: WebhookRegisterRequest,
|
|
26
|
-
options?: RequestOptions,
|
|
27
|
-
): Promise<ApiResult<WebhookRegisterResponse>> {
|
|
28
|
-
return this.#transport.request(
|
|
29
|
-
{
|
|
30
|
-
name: 'webhooks.register',
|
|
31
|
-
method: 'PUT',
|
|
32
|
-
path: '/v1/webhook',
|
|
33
|
-
body,
|
|
34
|
-
status: 201,
|
|
35
|
-
retry: 'never',
|
|
36
|
-
parse: decodeWebhookRegister,
|
|
37
|
-
},
|
|
38
|
-
options,
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
retrieve(options?: RequestOptions): Promise<ApiResult<WebhookResponse>> {
|
|
42
|
-
return this.#transport.request(
|
|
43
|
-
{
|
|
44
|
-
name: 'webhooks.retrieve',
|
|
45
|
-
method: 'GET',
|
|
46
|
-
path: '/v1/webhook',
|
|
47
|
-
status: 200,
|
|
48
|
-
retry: 'safe',
|
|
49
|
-
parse: decodeWebhook,
|
|
50
|
-
},
|
|
51
|
-
options,
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
verifyEndpoint(options?: RequestOptions): Promise<ApiResult<WebhookVerifyResponse>> {
|
|
55
|
-
return this.#transport.request(
|
|
56
|
-
{
|
|
57
|
-
name: 'webhooks.verifyEndpoint',
|
|
58
|
-
method: 'POST',
|
|
59
|
-
path: '/v1/webhook/verify',
|
|
60
|
-
status: 200,
|
|
61
|
-
retry: 'never',
|
|
62
|
-
parse: decodeWebhookVerify,
|
|
63
|
-
},
|
|
64
|
-
options,
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
/** Once-returned secret; never automatically retried. */
|
|
68
|
-
rotateSecret(options?: RequestOptions): Promise<ApiResult<WebhookRotateResponse>> {
|
|
69
|
-
return this.#transport.request(
|
|
70
|
-
{
|
|
71
|
-
name: 'webhooks.rotateSecret',
|
|
72
|
-
method: 'POST',
|
|
73
|
-
path: '/v1/webhook/secrets/rotate',
|
|
74
|
-
status: 201,
|
|
75
|
-
retry: 'never',
|
|
76
|
-
parse: decodeWebhookRotate,
|
|
77
|
-
},
|
|
78
|
-
options,
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
}
|
package/src/webhooks/index.ts
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
import { Webhook } from 'standardwebhooks';
|
|
2
|
-
import { SessError } from '../errors.js';
|
|
3
|
-
import type { components } from '../generated/openapi.js';
|
|
4
|
-
import {
|
|
5
|
-
checkedInstant,
|
|
6
|
-
deepFreeze,
|
|
7
|
-
exactKeys,
|
|
8
|
-
isRecord,
|
|
9
|
-
snapshotJson,
|
|
10
|
-
wellFormedUnicode,
|
|
11
|
-
type DeepReadonly,
|
|
12
|
-
} from '../receipts/json.js';
|
|
13
|
-
|
|
14
|
-
export type WebhookEvent = components['schemas']['WebhookDelivery'];
|
|
15
|
-
export type WebhookChallenge = components['schemas']['WebhookVerification'];
|
|
16
|
-
export type VerifiedWebhook = DeepReadonly<WebhookEvent | WebhookChallenge>;
|
|
17
|
-
export type WebhookHeaders = Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
18
|
-
|
|
19
|
-
export interface VerifyWebhookOptions {
|
|
20
|
-
/** Workspace-specific secrets from registration/rotation; store outside source control. */
|
|
21
|
-
secret?: string;
|
|
22
|
-
/** During rotation, either trusted secret can authenticate the overlapping signatures. */
|
|
23
|
-
secrets?: readonly string[];
|
|
24
|
-
/** 0–300 seconds; the maintained Standard Webhooks verifier sets the outer 300-second bound. */
|
|
25
|
-
toleranceSeconds?: number;
|
|
26
|
-
/** Defaults to 1 MiB; can be tightened, never increased beyond 1 MiB. */
|
|
27
|
-
maxBodyBytes?: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const MAX_BODY_BYTES = 1024 * 1024;
|
|
31
|
-
const MAX_HEADER_BYTES = 8192;
|
|
32
|
-
const MAX_SIGNATURES = 8;
|
|
33
|
-
const MAX_SECRETS = 4;
|
|
34
|
-
const SECRET = /^whsec_[A-Za-z0-9+/]{43}=$/;
|
|
35
|
-
const EVENT_ID = /^evt_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
36
|
-
const SESSION_ID = /^ses_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
37
|
-
|
|
38
|
-
function failure(code: string, message: string): SessError {
|
|
39
|
-
return new SessError('protocol', message, { code, operation: 'webhooks.verify' });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function headersOf(headers: WebhookHeaders): Record<string, string> {
|
|
43
|
-
const wanted = new Set(['webhook-id', 'webhook-timestamp', 'webhook-signature']);
|
|
44
|
-
const result: Record<string, string> = {};
|
|
45
|
-
const seen = new Set<string>();
|
|
46
|
-
let bytes = 0;
|
|
47
|
-
const entries: Iterable<[string, unknown]> =
|
|
48
|
-
headers instanceof Headers ? headers.entries() : Object.entries(headers);
|
|
49
|
-
for (const [rawName, value] of entries) {
|
|
50
|
-
const name = rawName.toLowerCase();
|
|
51
|
-
if (!wanted.has(name)) continue; // sess-webhook-key-ids never grants authorization.
|
|
52
|
-
if (seen.has(name) || typeof value !== 'string' || /[\u0000-\u001f\u007f]/.test(value)) {
|
|
53
|
-
throw failure('WEBHOOK_INVALID_HEADERS', 'Webhook authentication headers are duplicated or malformed.');
|
|
54
|
-
}
|
|
55
|
-
seen.add(name);
|
|
56
|
-
bytes += Buffer.byteLength(name) + Buffer.byteLength(value);
|
|
57
|
-
if (bytes > MAX_HEADER_BYTES)
|
|
58
|
-
throw failure('WEBHOOK_INVALID_HEADERS', 'Webhook authentication headers exceed the safety limit.');
|
|
59
|
-
result[name] = value;
|
|
60
|
-
}
|
|
61
|
-
const id = result['webhook-id'];
|
|
62
|
-
const timestamp = result['webhook-timestamp'];
|
|
63
|
-
const signatures = result['webhook-signature'];
|
|
64
|
-
if (
|
|
65
|
-
!id ||
|
|
66
|
-
!/^[A-Za-z0-9._-]{1,128}$/.test(id) ||
|
|
67
|
-
!timestamp ||
|
|
68
|
-
!/^(?:0|[1-9][0-9]{0,11})$/.test(timestamp) ||
|
|
69
|
-
!signatures ||
|
|
70
|
-
signatures.length > 2048
|
|
71
|
-
) {
|
|
72
|
-
throw failure('WEBHOOK_INVALID_HEADERS', 'Webhook authentication headers are missing or malformed.');
|
|
73
|
-
}
|
|
74
|
-
const candidates = signatures.split(' ');
|
|
75
|
-
if (
|
|
76
|
-
candidates.length > MAX_SIGNATURES ||
|
|
77
|
-
candidates.some((candidate) => !/^v[1-9][0-9]*,[A-Za-z0-9+/]{43}=$/.test(candidate))
|
|
78
|
-
) {
|
|
79
|
-
throw failure(
|
|
80
|
-
'WEBHOOK_INVALID_HEADERS',
|
|
81
|
-
'Webhook signatures must use bounded, space-separated versioned signatures.',
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
// Headers combines repeated lines with a comma; exact grammar above rejects that form.
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Authenticates exact raw UTF-8 bytes before parsing JSON. This is transport authentication,
|
|
90
|
-
* not decision-receipt verification or authorization to perform application business work.
|
|
91
|
-
*/
|
|
92
|
-
export function verifyWebhook(
|
|
93
|
-
rawBody: Uint8Array | string,
|
|
94
|
-
headers: WebhookHeaders,
|
|
95
|
-
options: VerifyWebhookOptions,
|
|
96
|
-
): VerifiedWebhook {
|
|
97
|
-
if (!options || !headers)
|
|
98
|
-
throw new SessError('configuration', 'Webhook verification requires headers and trusted-secret options.');
|
|
99
|
-
const tolerance = options?.toleranceSeconds ?? 300;
|
|
100
|
-
const maxBytes = options?.maxBodyBytes ?? MAX_BODY_BYTES;
|
|
101
|
-
if (
|
|
102
|
-
!Number.isFinite(tolerance) ||
|
|
103
|
-
tolerance < 0 ||
|
|
104
|
-
tolerance > 300 ||
|
|
105
|
-
!Number.isSafeInteger(maxBytes) ||
|
|
106
|
-
maxBytes < 1 ||
|
|
107
|
-
maxBytes > MAX_BODY_BYTES
|
|
108
|
-
) {
|
|
109
|
-
throw new SessError(
|
|
110
|
-
'configuration',
|
|
111
|
-
'Webhook tolerance must be 0–300 seconds and its byte limit 1–1048576.',
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
if ((options.secret === undefined) === (options.secrets === undefined)) {
|
|
115
|
-
throw new SessError(
|
|
116
|
-
'configuration',
|
|
117
|
-
'Provide exactly one webhook secret or a trusted rotation secret list.',
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
const secrets = options.secrets === undefined ? [options.secret] : [...options.secrets];
|
|
121
|
-
if (
|
|
122
|
-
secrets.length === 0 ||
|
|
123
|
-
secrets.length > MAX_SECRETS ||
|
|
124
|
-
secrets.some(
|
|
125
|
-
(secret) =>
|
|
126
|
-
typeof secret !== 'string' ||
|
|
127
|
-
!SECRET.test(secret) ||
|
|
128
|
-
Buffer.from(secret.slice(6), 'base64').length !== 32 ||
|
|
129
|
-
Buffer.from(secret.slice(6), 'base64').toString('base64') !== secret.slice(6),
|
|
130
|
-
)
|
|
131
|
-
) {
|
|
132
|
-
throw new SessError('configuration', 'Use the webhook signing secret Sess returned for your workspace.');
|
|
133
|
-
}
|
|
134
|
-
let bytes: Buffer;
|
|
135
|
-
if (typeof rawBody === 'string') {
|
|
136
|
-
if (!wellFormedUnicode(rawBody))
|
|
137
|
-
throw failure('WEBHOOK_INVALID_ENCODING', 'Webhook bodies must be exact well-formed UTF-8.');
|
|
138
|
-
if (Buffer.byteLength(rawBody, 'utf8') > maxBytes)
|
|
139
|
-
throw failure('WEBHOOK_BODY_TOO_LARGE', 'Webhook body exceeds the configured byte limit.');
|
|
140
|
-
bytes = Buffer.from(rawBody, 'utf8');
|
|
141
|
-
} else if (rawBody instanceof Uint8Array) {
|
|
142
|
-
if (rawBody.byteLength > maxBytes)
|
|
143
|
-
throw failure('WEBHOOK_BODY_TOO_LARGE', 'Webhook body exceeds the configured byte limit.');
|
|
144
|
-
bytes = Buffer.from(rawBody);
|
|
145
|
-
} else {
|
|
146
|
-
throw failure('WEBHOOK_RAW_BODY_REQUIRED', 'Provide the original request bytes before JSON parsing.');
|
|
147
|
-
}
|
|
148
|
-
let body: string;
|
|
149
|
-
try {
|
|
150
|
-
body = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
151
|
-
} catch {
|
|
152
|
-
throw failure('WEBHOOK_INVALID_ENCODING', 'Webhook bodies must contain valid UTF-8 bytes.');
|
|
153
|
-
}
|
|
154
|
-
// Standard Webhooks accepts strings/Buffers and internally decodes text. This round trip
|
|
155
|
-
// guarantees that decoding cannot replace malformed bytes before signature verification.
|
|
156
|
-
if (!Buffer.from(body, 'utf8').equals(bytes))
|
|
157
|
-
throw failure('WEBHOOK_INVALID_ENCODING', 'Webhook UTF-8 decoding changed its signed bytes.');
|
|
158
|
-
const verifiedHeaders = headersOf(headers);
|
|
159
|
-
const timestamp = Number(verifiedHeaders['webhook-timestamp']);
|
|
160
|
-
if (Math.abs(Math.floor(Date.now() / 1000) - timestamp) > tolerance) {
|
|
161
|
-
throw failure(
|
|
162
|
-
'WEBHOOK_TIMESTAMP_OUT_OF_RANGE',
|
|
163
|
-
'Webhook timestamp is outside the configured replay tolerance.',
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
let authenticated = false;
|
|
167
|
-
for (const secret of secrets) {
|
|
168
|
-
try {
|
|
169
|
-
new Webhook(secret as string).verify(bytes, verifiedHeaders, { jsonParse: false });
|
|
170
|
-
authenticated = true;
|
|
171
|
-
break;
|
|
172
|
-
} catch {
|
|
173
|
-
// Neither dependency error messages nor secret/signature material reaches callers.
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
if (!authenticated)
|
|
177
|
-
throw failure(
|
|
178
|
-
'WEBHOOK_INVALID_SIGNATURE',
|
|
179
|
-
'Webhook authentication failed for the trusted workspace secrets.',
|
|
180
|
-
);
|
|
181
|
-
let parsed: unknown;
|
|
182
|
-
try {
|
|
183
|
-
parsed = JSON.parse(body);
|
|
184
|
-
} catch {
|
|
185
|
-
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The authenticated webhook body is not JSON.');
|
|
186
|
-
}
|
|
187
|
-
let value: unknown;
|
|
188
|
-
try {
|
|
189
|
-
value = snapshotJson(parsed);
|
|
190
|
-
} catch {
|
|
191
|
-
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The authenticated webhook body is not supported JSON.');
|
|
192
|
-
}
|
|
193
|
-
if (!isRecord(value))
|
|
194
|
-
throw failure('WEBHOOK_INVALID_PAYLOAD', 'The authenticated webhook payload must be an object.');
|
|
195
|
-
const id = verifiedHeaders['webhook-id'];
|
|
196
|
-
if (value.type === 'sess.webhook.verification') {
|
|
197
|
-
if (
|
|
198
|
-
!exactKeys(value, ['type', 'challenge']) ||
|
|
199
|
-
typeof value.challenge !== 'string' ||
|
|
200
|
-
Buffer.byteLength(value.challenge, 'utf8') > 1024
|
|
201
|
-
) {
|
|
202
|
-
throw failure(
|
|
203
|
-
'WEBHOOK_INVALID_CHALLENGE',
|
|
204
|
-
'The webhook challenge is malformed or exceeds its safety limit.',
|
|
205
|
-
);
|
|
206
|
-
}
|
|
207
|
-
return deepFreeze(value) as DeepReadonly<WebhookChallenge>;
|
|
208
|
-
}
|
|
209
|
-
if (
|
|
210
|
-
value.type !== 'response.accepted' ||
|
|
211
|
-
!exactKeys(value, [
|
|
212
|
-
'type',
|
|
213
|
-
'event_id',
|
|
214
|
-
'session_id',
|
|
215
|
-
'sequence',
|
|
216
|
-
'revision',
|
|
217
|
-
'action_id',
|
|
218
|
-
'data',
|
|
219
|
-
'accepted_at',
|
|
220
|
-
'receipt',
|
|
221
|
-
]) ||
|
|
222
|
-
typeof value.event_id !== 'string' ||
|
|
223
|
-
!EVENT_ID.test(value.event_id) ||
|
|
224
|
-
value.event_id !== id ||
|
|
225
|
-
typeof value.session_id !== 'string' ||
|
|
226
|
-
!SESSION_ID.test(value.session_id) ||
|
|
227
|
-
!Number.isSafeInteger(value.sequence) ||
|
|
228
|
-
(value.sequence as number) < 1 ||
|
|
229
|
-
!Number.isSafeInteger(value.revision) ||
|
|
230
|
-
(value.revision as number) < 1 ||
|
|
231
|
-
typeof value.action_id !== 'string' ||
|
|
232
|
-
!/^[a-z][a-z0-9_]{0,31}$/.test(value.action_id) ||
|
|
233
|
-
!isRecord(value.data) ||
|
|
234
|
-
checkedInstant(value.accepted_at) === undefined ||
|
|
235
|
-
(value.receipt !== null &&
|
|
236
|
-
(typeof value.receipt !== 'string' ||
|
|
237
|
-
value.receipt.length > 64 * 1024 ||
|
|
238
|
-
!/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(value.receipt)))
|
|
239
|
-
) {
|
|
240
|
-
throw failure(
|
|
241
|
-
'WEBHOOK_INVALID_PAYLOAD',
|
|
242
|
-
'The accepted webhook event is malformed or does not match its transport identity.',
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
return deepFreeze(value) as unknown as DeepReadonly<WebhookEvent>;
|
|
246
|
-
}
|