@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.
Files changed (139) hide show
  1. package/CHANGELOG.md +66 -9
  2. package/NOTICE +1 -1
  3. package/README.md +92 -16
  4. package/dist/ask.d.ts +0 -1
  5. package/dist/ask.js +0 -1
  6. package/dist/cli/main.d.ts +0 -1
  7. package/dist/cli/main.js +0 -1
  8. package/dist/cli/verify.d.ts +0 -1
  9. package/dist/cli/verify.js +0 -1
  10. package/dist/client.d.ts +0 -1
  11. package/dist/client.js +0 -1
  12. package/dist/commerce/builders.d.ts +99 -0
  13. package/dist/commerce/builders.js +208 -0
  14. package/dist/commerce/currency.d.ts +4 -0
  15. package/dist/commerce/currency.js +25 -0
  16. package/dist/commerce/items.d.ts +119 -0
  17. package/dist/commerce/items.js +280 -0
  18. package/dist/commerce/lint.d.ts +16 -0
  19. package/dist/commerce/lint.js +56 -0
  20. package/dist/errors.d.ts +5 -2
  21. package/dist/errors.js +15 -25
  22. package/dist/generated/openapi.d.ts +12 -13
  23. package/dist/generated/openapi.js +0 -1
  24. package/dist/index.d.ts +6 -1
  25. package/dist/index.js +3 -1
  26. package/dist/internal/decode.d.ts +0 -1
  27. package/dist/internal/decode.js +9 -2
  28. package/dist/internal/json.d.ts +0 -1
  29. package/dist/internal/json.js +0 -1
  30. package/dist/internal/time.d.ts +0 -1
  31. package/dist/internal/time.js +0 -1
  32. package/dist/internal/version.d.ts +2 -0
  33. package/dist/internal/version.js +2 -0
  34. package/dist/polling.d.ts +0 -1
  35. package/dist/polling.js +0 -1
  36. package/dist/qr/encoder.d.ts +0 -1
  37. package/dist/qr/encoder.js +0 -1
  38. package/dist/qr/index.d.ts +0 -1
  39. package/dist/qr/index.js +0 -1
  40. package/dist/receipts/bundle.d.ts +0 -1
  41. package/dist/receipts/bundle.js +0 -1
  42. package/dist/receipts/index.d.ts +0 -1
  43. package/dist/receipts/index.js +0 -1
  44. package/dist/receipts/json.d.ts +0 -1
  45. package/dist/receipts/json.js +0 -1
  46. package/dist/receipts/key-cache.d.ts +0 -1
  47. package/dist/receipts/key-cache.js +0 -1
  48. package/dist/receipts/renderers.d.ts +0 -1
  49. package/dist/receipts/renderers.js +1 -2
  50. package/dist/receipts/resource.d.ts +0 -1
  51. package/dist/receipts/resource.js +0 -1
  52. package/dist/receipts/verify.d.ts +0 -1
  53. package/dist/receipts/verify.js +0 -1
  54. package/dist/sessions.d.ts +0 -1
  55. package/dist/sessions.js +0 -1
  56. package/dist/transport.d.ts +0 -1
  57. package/dist/transport.js +35 -5
  58. package/dist/types.d.ts +56 -2
  59. package/dist/types.js +0 -1
  60. package/dist/webhook-resource.d.ts +0 -1
  61. package/dist/webhook-resource.js +0 -1
  62. package/dist/webhooks/index.d.ts +0 -1
  63. package/dist/webhooks/index.js +0 -1
  64. package/package.json +8 -3
  65. package/dist/ask.d.ts.map +0 -1
  66. package/dist/ask.js.map +0 -1
  67. package/dist/cli/main.d.ts.map +0 -1
  68. package/dist/cli/main.js.map +0 -1
  69. package/dist/cli/verify.d.ts.map +0 -1
  70. package/dist/cli/verify.js.map +0 -1
  71. package/dist/client.d.ts.map +0 -1
  72. package/dist/client.js.map +0 -1
  73. package/dist/errors.d.ts.map +0 -1
  74. package/dist/errors.js.map +0 -1
  75. package/dist/generated/openapi.d.ts.map +0 -1
  76. package/dist/generated/openapi.js.map +0 -1
  77. package/dist/index.d.ts.map +0 -1
  78. package/dist/index.js.map +0 -1
  79. package/dist/internal/decode.d.ts.map +0 -1
  80. package/dist/internal/decode.js.map +0 -1
  81. package/dist/internal/json.d.ts.map +0 -1
  82. package/dist/internal/json.js.map +0 -1
  83. package/dist/internal/time.d.ts.map +0 -1
  84. package/dist/internal/time.js.map +0 -1
  85. package/dist/polling.d.ts.map +0 -1
  86. package/dist/polling.js.map +0 -1
  87. package/dist/qr/encoder.d.ts.map +0 -1
  88. package/dist/qr/encoder.js.map +0 -1
  89. package/dist/qr/index.d.ts.map +0 -1
  90. package/dist/qr/index.js.map +0 -1
  91. package/dist/receipts/bundle.d.ts.map +0 -1
  92. package/dist/receipts/bundle.js.map +0 -1
  93. package/dist/receipts/index.d.ts.map +0 -1
  94. package/dist/receipts/index.js.map +0 -1
  95. package/dist/receipts/json.d.ts.map +0 -1
  96. package/dist/receipts/json.js.map +0 -1
  97. package/dist/receipts/key-cache.d.ts.map +0 -1
  98. package/dist/receipts/key-cache.js.map +0 -1
  99. package/dist/receipts/renderers.d.ts.map +0 -1
  100. package/dist/receipts/renderers.js.map +0 -1
  101. package/dist/receipts/resource.d.ts.map +0 -1
  102. package/dist/receipts/resource.js.map +0 -1
  103. package/dist/receipts/verify.d.ts.map +0 -1
  104. package/dist/receipts/verify.js.map +0 -1
  105. package/dist/sessions.d.ts.map +0 -1
  106. package/dist/sessions.js.map +0 -1
  107. package/dist/transport.d.ts.map +0 -1
  108. package/dist/transport.js.map +0 -1
  109. package/dist/types.d.ts.map +0 -1
  110. package/dist/types.js.map +0 -1
  111. package/dist/webhook-resource.d.ts.map +0 -1
  112. package/dist/webhook-resource.js.map +0 -1
  113. package/dist/webhooks/index.d.ts.map +0 -1
  114. package/dist/webhooks/index.js.map +0 -1
  115. package/src/ask.ts +0 -683
  116. package/src/cli/main.ts +0 -8
  117. package/src/cli/verify.ts +0 -349
  118. package/src/client.ts +0 -55
  119. package/src/errors.ts +0 -127
  120. package/src/generated/openapi.ts +0 -8586
  121. package/src/index.ts +0 -5
  122. package/src/internal/decode.ts +0 -348
  123. package/src/internal/json.ts +0 -64
  124. package/src/internal/time.ts +0 -85
  125. package/src/polling.ts +0 -173
  126. package/src/qr/encoder.ts +0 -345
  127. package/src/qr/index.ts +0 -203
  128. package/src/receipts/bundle.ts +0 -547
  129. package/src/receipts/index.ts +0 -29
  130. package/src/receipts/json.ts +0 -168
  131. package/src/receipts/key-cache.ts +0 -143
  132. package/src/receipts/renderers.ts +0 -220
  133. package/src/receipts/resource.ts +0 -264
  134. package/src/receipts/verify.ts +0 -554
  135. package/src/sessions.ts +0 -238
  136. package/src/transport.ts +0 -566
  137. package/src/types.ts +0 -219
  138. package/src/webhook-resource.ts +0 -81
  139. package/src/webhooks/index.ts +0 -246
@@ -1,264 +0,0 @@
1
- import { SessError } from '../errors.js';
2
- import { isInteractiveSurface } from '../types.js';
3
- import type {
4
- AcceptedResponse,
5
- ApiResult,
6
- InteractiveSurfaceRevision,
7
- Session,
8
- SurfaceRevision,
9
- VersionedResult,
10
- } from '../types.js';
11
- import { ReceiptKeyCache } from './key-cache.js';
12
- import {
13
- abortError,
14
- canonicalJson,
15
- deepFreeze,
16
- snapshotJson,
17
- verificationError,
18
- waitWithSignal,
19
- type DeepReadonly,
20
- } from './json.js';
21
- import {
22
- BUNDLED_RENDERER_IDENTITIES,
23
- MAX_RENDERER_IDENTITIES_BYTES,
24
- RENDERER_IDENTITIES_PATH,
25
- type RendererIdentitiesDocument,
26
- } from './renderers.js';
27
- import { MAX_RECEIPT_KEY_SET_BYTES, verifyDecisionReceipt, type ReceiptClaims } from './verify.js';
28
-
29
- /**
30
- * The renderer identity a receipt names, read without trusting it: only to decide whether the
31
- * issuer's signed list is needed (the verifier decides). Undefined when it cannot be read.
32
- */
33
- function namedRenderer(receipt: string): string | undefined {
34
- try {
35
- const claims: unknown = JSON.parse(
36
- Buffer.from(receipt.split('.')[1] ?? '', 'base64url').toString('utf8'),
37
- );
38
- const surface = (claims as { surface?: { renderer_version?: unknown } } | null)?.surface;
39
- return typeof surface?.renderer_version === 'string' ? surface.renderer_version : undefined;
40
- } catch {
41
- return undefined;
42
- }
43
- }
44
-
45
- export interface VerificationRequestOptions {
46
- signal?: AbortSignal;
47
- operationTimeoutMs?: number;
48
- timeoutMs?: number;
49
- maxRetries?: number;
50
- }
51
-
52
- interface Context {
53
- receiptIssuer: string;
54
- expectedWorkspaceId?: string;
55
- operationTimeoutMs: number;
56
- /** Stable configured API origin, used only to identify this trusted key source. */
57
- baseUrl?: string;
58
- retrieveSession: (id: string, options: VerificationRequestOptions) => Promise<VersionedResult<Session>>;
59
- retrieveSurface: (
60
- id: string,
61
- revision: number,
62
- options: VerificationRequestOptions,
63
- ) => Promise<ApiResult<SurfaceRevision>>;
64
- publicGet: (
65
- path: string,
66
- options: VerificationRequestOptions & { maxResponseBytes: number },
67
- ) => Promise<ApiResult<unknown>>;
68
- }
69
-
70
- export interface VerifyResponseInput {
71
- sessionId: string;
72
- response: AcceptedResponse;
73
- expectedWorkspaceId?: string;
74
- }
75
-
76
- export interface VerifiedResponse {
77
- claims: DeepReadonly<ReceiptClaims>;
78
- response: DeepReadonly<AcceptedResponse>;
79
- surface: DeepReadonly<InteractiveSurfaceRevision>;
80
- }
81
-
82
- export interface ReceiptResource {
83
- /** Full receipt verification with authorized current context and exact immutable revision. */
84
- verifyResponse(
85
- input: VerifyResponseInput,
86
- options?: VerificationRequestOptions,
87
- ): Promise<DeepReadonly<VerifiedResponse>>;
88
- }
89
-
90
- /** Internal bridge: all HTTP is supplied by the client's ordinary bounded transport. */
91
- export function createReceiptResource(context: Context): ReceiptResource {
92
- // Copy configuration/callbacks; mutation of an application-owned config object has no effect.
93
- const issuer = context.receiptIssuer;
94
- const configuredWorkspace = context.expectedWorkspaceId;
95
- const defaultBudget = context.operationTimeoutMs;
96
- const retrieve = context.retrieveSession;
97
- const surfaceRead = context.retrieveSurface;
98
- const publicGet = context.publicGet;
99
- const keyPath = '/.well-known/sess-receipt-keys.json';
100
- const cache = new ReceiptKeyCache({
101
- issuer,
102
- keySource: `${context.baseUrl ?? 'configured-api-origin'}${keyPath}`,
103
- load: async (options) =>
104
- (await publicGet(keyPath, { ...options, maxResponseBytes: MAX_RECEIPT_KEY_SET_BYTES })).data,
105
- });
106
- return Object.freeze({
107
- async verifyResponse(
108
- input: VerifyResponseInput,
109
- options: VerificationRequestOptions = {},
110
- ): Promise<DeepReadonly<VerifiedResponse>> {
111
- const sessionId = input.sessionId;
112
- const response = snapshotJson(input.response);
113
- const requestedWorkspace = input.expectedWorkspaceId;
114
- const timeoutMs = options.timeoutMs;
115
- const maxRetries = options.maxRetries;
116
- const originalSignal = options.signal;
117
- const budget = options.operationTimeoutMs ?? defaultBudget;
118
- if (!Number.isSafeInteger(budget) || budget <= 0 || budget > 2_147_483_647) {
119
- throw new SessError(
120
- 'configuration',
121
- 'Receipt verification requires a finite positive operation timeout.',
122
- );
123
- }
124
- if (!response || response.receipt === null) {
125
- throw verificationError(
126
- 'LEGACY_RECEIPT_UNAVAILABLE',
127
- 'This accepted response has no verifiable receipt.',
128
- );
129
- }
130
- if (requestedWorkspace && configuredWorkspace && requestedWorkspace !== configuredWorkspace) {
131
- throw verificationError(
132
- 'AUDIENCE_MISMATCH',
133
- 'The supplied workspace differs from this client’s trusted workspace.',
134
- );
135
- }
136
- if (originalSignal?.aborted) throw abortError(originalSignal);
137
- const controller = new AbortController();
138
- const abort = () => controller.abort(originalSignal?.reason);
139
- originalSignal?.addEventListener('abort', abort, { once: true });
140
- const started = performance.now();
141
- const timer = setTimeout(
142
- () =>
143
- controller.abort(new SessError('timeout', 'Receipt verification exceeded its overall deadline.')),
144
- budget,
145
- );
146
- const requestOptions = (): VerificationRequestOptions => {
147
- if (controller.signal.aborted) throw abortError(controller.signal);
148
- const remaining = Math.floor(budget - (performance.now() - started));
149
- if (remaining <= 0) {
150
- controller.abort(new SessError('timeout', 'Receipt verification exceeded its overall deadline.'));
151
- throw abortError(controller.signal);
152
- }
153
- return {
154
- signal: controller.signal,
155
- operationTimeoutMs: remaining,
156
- ...(timeoutMs !== undefined ? { timeoutMs } : {}),
157
- ...(maxRetries !== undefined ? { maxRetries } : {}),
158
- };
159
- };
160
- try {
161
- const current = await waitWithSignal(retrieve(sessionId, requestOptions()), controller.signal);
162
- // Copy at the read boundary; transport adapters must not be able to mutate verified data later.
163
- const currentSnapshot = snapshotJson(current);
164
- const trustedWorkspace =
165
- requestedWorkspace ?? configuredWorkspace ?? currentSnapshot.meta.workspaceId;
166
- if (typeof trustedWorkspace !== 'string' || trustedWorkspace.length === 0) {
167
- throw verificationError(
168
- 'VERIFICATION_CONTEXT_MISSING',
169
- 'Provide expectedWorkspaceId or use a runtime that returns authenticated Sess-Workspace-Id context.',
170
- );
171
- }
172
- if (
173
- currentSnapshot.meta.workspaceId !== undefined &&
174
- currentSnapshot.meta.workspaceId !== trustedWorkspace
175
- ) {
176
- throw verificationError(
177
- 'AUDIENCE_MISMATCH',
178
- 'The authorized resource belongs to a different workspace.',
179
- );
180
- }
181
- if (currentSnapshot.data.id !== sessionId || !('responses' in currentSnapshot.data)) {
182
- throw verificationError(
183
- 'RECORD_BINDING_MISMATCH',
184
- 'The authorized Session does not contain this accepted response.',
185
- );
186
- }
187
- const retained = currentSnapshot.data.responses.filter(
188
- (candidate) => candidate.event_id === response.event_id,
189
- );
190
- if (retained.length !== 1 || canonicalJson(retained[0]) !== canonicalJson(response)) {
191
- throw verificationError(
192
- 'RECORD_BINDING_MISMATCH',
193
- 'The supplied response does not match the authorized accepted record.',
194
- );
195
- }
196
- const retrievedSurface = await waitWithSignal(
197
- surfaceRead(sessionId, response.revision, requestOptions()),
198
- controller.signal,
199
- );
200
- const surface = snapshotJson(retrievedSurface.data);
201
- if (surface.id !== sessionId || surface.revision !== response.revision) {
202
- throw verificationError(
203
- 'RECORD_BINDING_MISMATCH',
204
- 'The retrieved Surface is not the requested immutable revision.',
205
- );
206
- }
207
- if (!isInteractiveSurface(surface)) {
208
- throw verificationError(
209
- 'RECORD_BINDING_MISMATCH',
210
- 'An accepted response requires an interactive immutable Surface.',
211
- );
212
- }
213
- // Decision 0009 Amendment 13: a renderer identity this release does not bundle is
214
- // accepted only on the issuer's signed list, fetched from the same API origin as the keys.
215
- const renderer = namedRenderer(response.receipt);
216
- let rendererIdentities: RendererIdentitiesDocument | undefined;
217
- if (renderer !== undefined && !BUNDLED_RENDERER_IDENTITIES.includes(renderer)) {
218
- const listed = await waitWithSignal(
219
- publicGet(RENDERER_IDENTITIES_PATH, {
220
- ...requestOptions(),
221
- maxResponseBytes: MAX_RENDERER_IDENTITIES_BYTES,
222
- }),
223
- controller.signal,
224
- );
225
- rendererIdentities = listed.data as RendererIdentitiesDocument;
226
- }
227
- const claims = await verifyDecisionReceipt(
228
- {
229
- receipt: response.receipt,
230
- ...(rendererIdentities !== undefined ? { rendererIdentities } : {}),
231
- issuer,
232
- audience: trustedWorkspace,
233
- expected: {
234
- sessionId,
235
- eventId: response.event_id,
236
- revision: response.revision,
237
- actionId: response.action_id,
238
- acceptedAt: response.accepted_at,
239
- },
240
- input: response.data,
241
- interaction: surface.interaction,
242
- keyResolver: (resolverContext) =>
243
- cache.resolve({
244
- ...resolverContext,
245
- ...(timeoutMs !== undefined ? { timeoutMs } : {}),
246
- ...(maxRetries !== undefined ? { maxRetries } : {}),
247
- }),
248
- },
249
- { signal: controller.signal },
250
- );
251
- if (response.actor.assurance !== 'link_holder') {
252
- throw verificationError(
253
- 'ACTOR_BINDING_MISMATCH',
254
- 'The accepted response has an unsupported actor assurance.',
255
- );
256
- }
257
- return deepFreeze({ claims, response, surface });
258
- } finally {
259
- clearTimeout(timer);
260
- originalSignal?.removeEventListener('abort', abort);
261
- }
262
- },
263
- });
264
- }