@sendproof/core 0.1.0-alpha.1 → 0.1.0-alpha.2
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/assets/api.js +2 -1
- package/assets/app.css +612 -1
- package/assets/app.js +89 -13
- package/assets/bar-fit.js +91 -0
- package/assets/commands.js +27 -4
- package/assets/dialog.css +125 -0
- package/assets/dialog.js +191 -0
- package/assets/fields/lookup-source.js +5 -2
- package/assets/fields/lookup.js +3 -2
- package/assets/i18n.js +13 -2
- package/assets/icons.js +18 -0
- package/assets/index.html +1 -0
- package/assets/journeys/compare.js +225 -0
- package/assets/journeys/expressions.js +89 -0
- package/assets/journeys/index.js +7 -0
- package/assets/journeys/run.js +152 -0
- package/assets/journeys/steps.js +163 -0
- package/assets/journeys-code.js +193 -0
- package/assets/journeys-dialog-model.js +172 -0
- package/assets/journeys-dialogs.js +301 -0
- package/assets/journeys-model.js +259 -0
- package/assets/journeys-runs.js +109 -0
- package/assets/journeys-session.js +135 -0
- package/assets/journeys-view.js +961 -0
- package/assets/journeys.js +962 -0
- package/assets/locales/ar-journeys.js +468 -0
- package/assets/locales/ar.js +2 -0
- package/assets/locales/cs-journeys.js +468 -0
- package/assets/locales/cs.js +2 -0
- package/assets/locales/da-journeys.js +465 -0
- package/assets/locales/da.js +2 -0
- package/assets/locales/de-journeys.js +465 -0
- package/assets/locales/de.js +2 -0
- package/assets/locales/el-journeys.js +465 -0
- package/assets/locales/el.js +2 -0
- package/assets/locales/en-journeys.js +467 -0
- package/assets/locales/en.js +2 -0
- package/assets/locales/es-journeys.js +467 -0
- package/assets/locales/es.js +2 -0
- package/assets/locales/fa-journeys.js +465 -0
- package/assets/locales/fa.js +2 -0
- package/assets/locales/fi-journeys.js +465 -0
- package/assets/locales/fi.js +2 -0
- package/assets/locales/fr-journeys.js +467 -0
- package/assets/locales/fr.js +2 -0
- package/assets/locales/he-journeys.js +468 -0
- package/assets/locales/he.js +2 -0
- package/assets/locales/hi-journeys.js +465 -0
- package/assets/locales/hi.js +2 -0
- package/assets/locales/id-journeys.js +466 -0
- package/assets/locales/id.js +2 -0
- package/assets/locales/it-journeys.js +467 -0
- package/assets/locales/it.js +2 -0
- package/assets/locales/ja-journeys.js +466 -0
- package/assets/locales/ja.js +2 -0
- package/assets/locales/ko-journeys.js +465 -0
- package/assets/locales/ko.js +2 -0
- package/assets/locales/nb-journeys.js +465 -0
- package/assets/locales/nb.js +2 -0
- package/assets/locales/nl-journeys.js +465 -0
- package/assets/locales/nl.js +2 -0
- package/assets/locales/pl-journeys.js +468 -0
- package/assets/locales/pl.js +2 -0
- package/assets/locales/pt-BR-journeys.js +467 -0
- package/assets/locales/pt-BR.js +2 -0
- package/assets/locales/pt-PT-journeys.js +467 -0
- package/assets/locales/pt-PT.js +2 -0
- package/assets/locales/ru-journeys.js +468 -0
- package/assets/locales/ru.js +2 -0
- package/assets/locales/sv-journeys.js +465 -0
- package/assets/locales/sv.js +2 -0
- package/assets/locales/th-journeys.js +466 -0
- package/assets/locales/th.js +2 -0
- package/assets/locales/tr-journeys.js +468 -0
- package/assets/locales/tr.js +2 -0
- package/assets/locales/uk-journeys.js +468 -0
- package/assets/locales/uk.js +2 -0
- package/assets/locales/vi-journeys.js +466 -0
- package/assets/locales/vi.js +2 -0
- package/assets/locales/zh-Hans-journeys.js +466 -0
- package/assets/locales/zh-Hans.js +2 -0
- package/assets/locales/zh-Hant-journeys.js +466 -0
- package/assets/locales/zh-Hant.js +2 -0
- package/assets/manifest.json +349 -104
- package/assets/markdown.js +27 -2
- package/assets/request-editor.js +7 -1
- package/assets/result-states.js +2 -2
- package/assets/results.js +41 -14
- package/assets/router.js +25 -1
- package/assets/seeded-menu.js +2 -1
- package/assets/shortcuts-sheet.js +3 -1
- package/assets/shortcuts.js +6 -0
- package/assets/sidebar.js +54 -5
- package/assets/signin.js +6 -3
- package/assets/styles.css +8 -2
- package/assets/toast.js +29 -0
- package/assets/vendor/arazzo-runtime-expression.js +136 -0
- package/assets/vendor/json-pointer.js +48 -0
- package/assets/vendor/jsonpath-rfc9535.js +15 -0
- package/assets/view.js +11 -3
- package/assets/views.css +7 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +217 -26
- package/dist/journey-builder.d.ts +178 -0
- package/dist/journey-builder.js +169 -0
- package/dist/journeys.d.ts +24 -0
- package/dist/journeys.js +157 -0
- package/dist/node-http.d.ts +2 -0
- package/dist/node-http.js +17 -0
- package/dist/sessions.d.ts +43 -0
- package/dist/sessions.js +145 -0
- package/dist/statements.js +2 -2
- package/dist/stores/tables.d.ts +213 -0
- package/dist/stores/tables.js +213 -0
- package/dist/types.d.ts +34 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,6 +6,10 @@ import { fileURLToPath } from 'node:url';
|
|
|
6
6
|
import { attributeHttpRequests, Capture, closePreview, openPreview } from './capture.js';
|
|
7
7
|
import { hasPreference, localPath, requireLocalPath, requestsRollback, sameOrigin } from './paths.js';
|
|
8
8
|
import { writeReason } from './stores/guard.js';
|
|
9
|
+
import { journeyPrefix, registerJourneys } from './journeys.js';
|
|
10
|
+
import { failed, notFound, PreviewSessionEndedError, SessionProblem, Sessions } from './sessions.js';
|
|
11
|
+
export { defineJourney, match } from './journey-builder.js';
|
|
12
|
+
export { PreviewSessionEndedError, PreviewStoreBusyError } from './sessions.js';
|
|
9
13
|
export { attributeRequest, Capture, currentCapture, currentPreview, deliverEffect, detached } from './capture.js';
|
|
10
14
|
export { looksSecret, redacted, redactDeep, redactRow } from './secrets.js';
|
|
11
15
|
export { classifyDynamo, classifyMongo, classifyRedis, partiqlVerb } from './stores/classify.js';
|
|
@@ -15,6 +19,7 @@ export { cassandraTable, cosmosdbTable, dynamodbTable, elasticsearchTable, fires
|
|
|
15
19
|
export { controlsTransaction, endsTransaction, isRollback, mayWrite, previewSavepoint, previewSavepointHeld, rollsBackTransaction, setPreviewSavepoint, refusedStatement, safeProcedureNames, splitStatements, } from './statements.js';
|
|
16
20
|
const prefix = '/sendproof/_preview';
|
|
17
21
|
const tracePrefix = '/sendproof/_api/traces/';
|
|
22
|
+
const sessionsPath = '/sendproof/_api/sessions';
|
|
18
23
|
const assets = fileURLToPath(new URL('../assets/', import.meta.url));
|
|
19
24
|
const noStore = { 'Cache-Control': 'no-store' };
|
|
20
25
|
const privateHeaders = new Set([
|
|
@@ -26,6 +31,7 @@ const json = (status, value, headers = {}) => ({
|
|
|
26
31
|
body: Buffer.from(JSON.stringify(value)),
|
|
27
32
|
});
|
|
28
33
|
const problem = (status, title) => json(status, { status, title }, { 'Content-Type': 'application/problem+json' });
|
|
34
|
+
const noContent = () => ({ status: 204, headers: noStore, body: Buffer.alloc(0) });
|
|
29
35
|
/**
|
|
30
36
|
* The blocked-Preview response of spec/data-stores.md: 409 problem+json, never a confirmation.
|
|
31
37
|
* It carries nothing the handler produced, and names the command, never its arguments.
|
|
@@ -133,7 +139,7 @@ function databaseOf(label, provider) {
|
|
|
133
139
|
* capabilities, so the workbench writes guidance such as the `preview: false` snippet in its idiom.
|
|
134
140
|
*/
|
|
135
141
|
export function createBench(options = {}, registered = () => [], binding) {
|
|
136
|
-
const { apiName = 'API', environment = process.env.NODE_ENV ?? 'production', environments = ['development'], openApiUrl = '/openapi.json', documents = [], fieldModules = [], database = null, authProviders = [], resolveIdentity = () => null, authorize = () => true, discoverAuth, previewProvider: sqlProvider, maxResponseBytes = 1024 * 1024, captureLimits = {}, traceTtlMs = 300000, traceClock = () => performance.now(), includeSqlParameterValues = environment.toLowerCase() === 'development', dataStores = [], onError = () => { }, } = options;
|
|
142
|
+
const { apiName = 'API', environment = process.env.NODE_ENV ?? 'production', environments = ['development'], openApiUrl = '/openapi.json', openApiDocument, documents = [], journeys = [], journeyDocument, fieldModules = [], database = null, authProviders = [], resolveIdentity = () => null, authorize = () => true, discoverAuth, previewProvider: sqlProvider, previewSessions = {}, maxResponseBytes = 1024 * 1024, captureLimits = {}, traceTtlMs = 300000, traceClock = () => performance.now(), includeSqlParameterValues = environment.toLowerCase() === 'development', dataStores = [], onError = () => { }, } = options;
|
|
137
143
|
if (!Array.isArray(dataStores) || dataStores.some(store => typeof store?.system !== 'string' || typeof store.describe !== 'function')) {
|
|
138
144
|
throw new TypeError('dataStores takes stores made by redisStore, dynamoStore, mongoStore, mongooseStore, cassandraStore, elasticsearchStore, opensearchStore, cosmosStore or firestoreStore.');
|
|
139
145
|
}
|
|
@@ -163,8 +169,104 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
163
169
|
if (dataStores.length)
|
|
164
170
|
attributeHttpRequests();
|
|
165
171
|
const store = new TraceStore(traceTtlMs, traceClock);
|
|
172
|
+
const journeyDocuments = registerJourneys(journeys, {
|
|
173
|
+
apiName, openApiUrl, openApiDocument, document: journeyDocument, enabled,
|
|
174
|
+
development: environment.toLowerCase() === 'development',
|
|
175
|
+
});
|
|
176
|
+
const sessions = previewSessions === false ? undefined
|
|
177
|
+
: new Sessions(previewSessions.idleTimeoutSeconds ?? 300, previewSessions.maxLifetimeSeconds ?? 1800);
|
|
178
|
+
const hasSessions = (stores) => Boolean(sessions && previewProvider && (!sqlProvider || sqlProvider.openSession)
|
|
179
|
+
&& stores.every(({ mode }, index) => mode === 'guard' || dataStores[index].openSession));
|
|
180
|
+
const describeStores = () => Promise.all(dataStores.map(store => store.describe()));
|
|
166
181
|
let manifest;
|
|
167
|
-
|
|
182
|
+
function sessionError(error, context) {
|
|
183
|
+
if (error instanceof PreviewSessionEndedError)
|
|
184
|
+
error = failed();
|
|
185
|
+
if (error instanceof SessionProblem) {
|
|
186
|
+
const { status, code, message: title } = error;
|
|
187
|
+
const idle = sessions?.idleTimeoutSeconds;
|
|
188
|
+
return json(status, {
|
|
189
|
+
type: 'about:blank', status, code, title,
|
|
190
|
+
...(code === 'store_busy' ? {
|
|
191
|
+
detail: `That session ends by itself after its idle timeout (${idle} seconds), or when it is ended.`,
|
|
192
|
+
idleTimeoutSeconds: idle,
|
|
193
|
+
} : {}),
|
|
194
|
+
}, { 'Content-Type': 'application/problem+json' });
|
|
195
|
+
}
|
|
196
|
+
onError(error, context);
|
|
197
|
+
return problem(500, 'The preview session could not be completed safely.');
|
|
198
|
+
}
|
|
199
|
+
async function sessionRoute(context, subject, readBody) {
|
|
200
|
+
const { pathname, method } = context;
|
|
201
|
+
const resource = pathname.slice(sessionsPath.length).match(/^\/([^/]+)(\/rewind)?$/);
|
|
202
|
+
if (pathname === sessionsPath && method === 'POST') {
|
|
203
|
+
if (!sameOrigin(context))
|
|
204
|
+
return problem(403, 'Preview sessions must be opened from the application origin.');
|
|
205
|
+
if (!subject)
|
|
206
|
+
return problem(403, 'A Preview session needs one stable subject for the signed-in caller.');
|
|
207
|
+
if (!sessions)
|
|
208
|
+
return problem(409, 'Preview sessions are unavailable.');
|
|
209
|
+
try {
|
|
210
|
+
const stores = await describeStores();
|
|
211
|
+
if (!hasSessions(stores))
|
|
212
|
+
return problem(409, 'Preview sessions are unavailable.');
|
|
213
|
+
const guards = dataStores.filter((store, index) => stores[index].mode === 'guard' && store.around);
|
|
214
|
+
const opened = await sessions.open(subject, guards, async (add) => {
|
|
215
|
+
if (sqlProvider?.openSession)
|
|
216
|
+
await add(await sqlProvider.openSession());
|
|
217
|
+
for (const [index, dataStore] of dataStores.entries()) {
|
|
218
|
+
if (stores[index].mode === 'preview')
|
|
219
|
+
await add(await dataStore.openSession());
|
|
220
|
+
}
|
|
221
|
+
}, error => onError(error, context));
|
|
222
|
+
return json(201, opened);
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
if (!(error instanceof SessionProblem))
|
|
226
|
+
onError(error, context);
|
|
227
|
+
return sessionError(error instanceof SessionProblem ? error : failed(), context);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (!resource)
|
|
231
|
+
return pathname === sessionsPath ? problem(405, 'Use POST to open a Preview session.') : sessionError(notFound(), context);
|
|
232
|
+
const [, id, rewind] = resource;
|
|
233
|
+
if (!sessions || !sameOrigin(context))
|
|
234
|
+
return sessionError(notFound(), context);
|
|
235
|
+
if (method !== (rewind ? 'POST' : 'DELETE'))
|
|
236
|
+
return problem(405, 'Use POST to rewind or DELETE to end a Preview session.');
|
|
237
|
+
try {
|
|
238
|
+
const session = sessions.enter({ id, owner: subject });
|
|
239
|
+
try {
|
|
240
|
+
if (!rewind) {
|
|
241
|
+
await sessions.close(session);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
const input = await readBody?.().catch(() => null);
|
|
245
|
+
const step = input && typeof input === 'object' && 'step' in input ? input.step : undefined;
|
|
246
|
+
if (typeof step !== 'number' || !Number.isSafeInteger(step) || step < 1 || step > session.nextStep) {
|
|
247
|
+
return problem(400, 'Choose a step from 1 through the next Preview step.');
|
|
248
|
+
}
|
|
249
|
+
if (step < session.nextStep) {
|
|
250
|
+
for (const part of session.parts.toReversed())
|
|
251
|
+
await part.rewind(step);
|
|
252
|
+
session.nextStep = step;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return noContent();
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
await sessions.close(session).catch(error => onError(error, context));
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
await sessions.exit(session);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
return sessionError(error, context);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async function lookup(context, readBody) {
|
|
168
270
|
const { pathname, query = '', basePath = '', method, headers } = context;
|
|
169
271
|
const prefer = headers.prefer;
|
|
170
272
|
const isPreview = under(pathname, prefix);
|
|
@@ -177,17 +279,28 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
177
279
|
const identity = await resolveIdentity(context);
|
|
178
280
|
if (!await authorize(context, identity))
|
|
179
281
|
return problem(identity ? 403 : 401, 'Workbench access denied.');
|
|
282
|
+
const subject = () => identity ? owner(identity, basePath) : JSON.stringify([basePath]);
|
|
283
|
+
if (under(pathname, sessionsPath))
|
|
284
|
+
return sessionRoute(context, subject(), readBody);
|
|
180
285
|
if (isPreview) {
|
|
181
286
|
if (!hasPreference(prefer, 'tx=rollback'))
|
|
182
287
|
return problem(400, 'A Sendproof preview requires Prefer: tx=rollback.');
|
|
183
288
|
if (method === 'HEAD')
|
|
184
289
|
return problem(405, 'A preview requires a response body.');
|
|
290
|
+
const sessionId = headers['sendproof-session'];
|
|
291
|
+
if (sessionId !== undefined && (!sessions || !sameOrigin(context)))
|
|
292
|
+
return sessionError(notFound(), context);
|
|
185
293
|
if (!sameOrigin(context))
|
|
186
294
|
return problem(403, 'Preview is restricted to the application origin.');
|
|
187
295
|
const target = pathname.slice(prefix.length);
|
|
188
296
|
if (!localPath(target) || under(decodeURIComponent(target).toLowerCase(), '/sendproof')) {
|
|
189
297
|
return problem(400, 'The Sendproof preview target is invalid.');
|
|
190
298
|
}
|
|
299
|
+
if (sessionId !== undefined)
|
|
300
|
+
return {
|
|
301
|
+
target: target + query,
|
|
302
|
+
session: { id: typeof sessionId === 'string' ? sessionId : '', owner: subject() },
|
|
303
|
+
};
|
|
191
304
|
if (!previewProvider)
|
|
192
305
|
return problem(503, 'Preview requires an available provider.');
|
|
193
306
|
return { target: target + query };
|
|
@@ -197,6 +310,7 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
197
310
|
if (pathname === '/sendproof/_api/capabilities') {
|
|
198
311
|
const addBase = (url) => url === null ? null : basePath + requireLocalPath(url);
|
|
199
312
|
const providers = [...authProviders, ...((await discoverAuth?.(context)) ?? [])];
|
|
313
|
+
const stores = await describeStores();
|
|
200
314
|
return json(200, {
|
|
201
315
|
protocolVersion: 1, apiName, environment, ...(binding ? { binding: { language: binding.language, framework: binding.framework } } : {}),
|
|
202
316
|
database: databaseOf(database, sqlProvider), identity: publicIdentity(identity),
|
|
@@ -206,7 +320,9 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
206
320
|
authProviders: providers.map(({ kind, label, url }) => ({ kind, label, url: addBase(url) })),
|
|
207
321
|
previewOperations: previewProvider ? operations() : [],
|
|
208
322
|
...(previewProvider && unavailable().length ? { previewUnavailable: unavailable() } : {}),
|
|
209
|
-
...(dataStores.length ? { dataStores:
|
|
323
|
+
...(dataStores.length ? { dataStores: stores } : {}),
|
|
324
|
+
previewSessions: hasSessions(stores),
|
|
325
|
+
...(journeyDocuments.size ? { journeys: journeyDocuments.discover(basePath) } : {}),
|
|
210
326
|
observedSend: Boolean(previewProvider),
|
|
211
327
|
});
|
|
212
328
|
}
|
|
@@ -219,6 +335,11 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
219
335
|
}
|
|
220
336
|
if (pathname === '/sendproof')
|
|
221
337
|
return { status: 302, headers: { ...noStore, Location: basePath + '/sendproof/' + query }, body: Buffer.alloc(0) };
|
|
338
|
+
if (pathname.startsWith(journeyPrefix)) {
|
|
339
|
+
const document = journeyDocuments.document(pathname, basePath);
|
|
340
|
+
if (document !== undefined)
|
|
341
|
+
return json(200, document, { 'X-Content-Type-Options': 'nosniff' });
|
|
342
|
+
}
|
|
222
343
|
const asset = pathname.slice('/sendproof/'.length) || 'index.html';
|
|
223
344
|
manifest ??= JSON.parse(readFileSync(join(assets, 'manifest.json'), 'utf8'));
|
|
224
345
|
const entry = Object.hasOwn(manifest.files, asset) && manifest.files[asset];
|
|
@@ -232,9 +353,98 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
232
353
|
const body = unchanged ? Buffer.alloc(0) : readFileSync(join(assets, asset));
|
|
233
354
|
return { status: unchanged ? 304 : 200, headers: headersOut, body };
|
|
234
355
|
}
|
|
235
|
-
|
|
356
|
+
function refusal(capture, response) {
|
|
357
|
+
if (capture.fault)
|
|
358
|
+
return problem(500, 'The preview could not be completed safely.');
|
|
359
|
+
if (capture.blocked)
|
|
360
|
+
return blockedPreview(capture.blocked);
|
|
361
|
+
if (response.status === 401 || response.status === 403)
|
|
362
|
+
return problem(response.status, 'Preview access denied.');
|
|
363
|
+
if (response.status >= 500)
|
|
364
|
+
return problem(500, 'The preview operation did not complete safely.');
|
|
365
|
+
if (response.exceeded || response.body.length > maxResponseBytes)
|
|
366
|
+
return problem(413, 'The preview response exceeds the configured size limit.');
|
|
367
|
+
}
|
|
368
|
+
const unmatched = () => problem(409, 'This operation is unavailable for preview.');
|
|
369
|
+
function confirmed(capture, response, session) {
|
|
370
|
+
const headers = Object.create(null);
|
|
371
|
+
const connectionTokens = Object.entries(response.headers)
|
|
372
|
+
.filter(([name]) => name.toLowerCase() === 'connection')
|
|
373
|
+
.flatMap(([, value]) => String(value ?? '').toLowerCase().split(',').map(token => token.trim()));
|
|
374
|
+
for (const [name, value] of Object.entries(response.headers).slice(0, 64)) {
|
|
375
|
+
const lower = name.toLowerCase();
|
|
376
|
+
if (value !== undefined && !privateHeaders.has(lower) && !connectionTokens.includes(lower))
|
|
377
|
+
headers[lower] = String(value).slice(0, 2048);
|
|
378
|
+
}
|
|
379
|
+
return json(200, {
|
|
380
|
+
status: response.status, headers, body: responseBody(response.body, headers['content-type']),
|
|
381
|
+
...capture.finish().capture, rolledBack: true, ...(session ? { session } : {}),
|
|
382
|
+
}, { 'Preference-Applied': 'tx=rollback' });
|
|
383
|
+
}
|
|
384
|
+
async function sessionPreview(context, access, dispatch) {
|
|
385
|
+
if (!sessions)
|
|
386
|
+
return sessionError(notFound(), context);
|
|
387
|
+
try {
|
|
388
|
+
const session = sessions.enter(access);
|
|
389
|
+
const step = session.nextStep;
|
|
390
|
+
const capture = new Capture('preview', captureLimits, includeSqlParameterValues);
|
|
391
|
+
openPreview(capture);
|
|
392
|
+
try {
|
|
393
|
+
for (const part of session.parts)
|
|
394
|
+
await part.checkpoint(step);
|
|
395
|
+
const guarded = session.guards.reduceRight((next, store) => () => store.around(capture, next, 'guard'), dispatch);
|
|
396
|
+
const handler = session.parts.reduceRight((next, part) => () => part.run(capture, next), guarded);
|
|
397
|
+
let rejected;
|
|
398
|
+
try {
|
|
399
|
+
const response = await capture.run(handler);
|
|
400
|
+
if ('unmatched' in response) {
|
|
401
|
+
rejected = capture.blocked ? blockedPreview(capture.blocked) : unmatched();
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
const refused = refusal(capture, response);
|
|
405
|
+
if (!refused) {
|
|
406
|
+
for (const part of session.parts)
|
|
407
|
+
await part.verify(step);
|
|
408
|
+
const rendered = confirmed(capture, response, { id: session.id, step });
|
|
409
|
+
session.nextStep++;
|
|
410
|
+
return rendered;
|
|
411
|
+
}
|
|
412
|
+
rejected = refused;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
if (error instanceof PreviewSessionEndedError)
|
|
417
|
+
throw error;
|
|
418
|
+
onError(error, context);
|
|
419
|
+
rejected = capture.blocked && !capture.fault
|
|
420
|
+
? blockedPreview(capture.blocked) : problem(500, 'The preview could not be completed safely.');
|
|
421
|
+
}
|
|
422
|
+
if (capture.fault)
|
|
423
|
+
throw capture.fault;
|
|
424
|
+
for (const part of session.parts.toReversed())
|
|
425
|
+
await part.rewind(step);
|
|
426
|
+
return rejected;
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
await sessions.close(session).catch(error => onError(error, context));
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
finally {
|
|
433
|
+
if (capture.active)
|
|
434
|
+
capture.finish();
|
|
435
|
+
closePreview(capture);
|
|
436
|
+
await sessions.exit(session);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
catch (error) {
|
|
440
|
+
return sessionError(error, context);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
async function preview(context, dispatch, session) {
|
|
236
444
|
if (!enabled)
|
|
237
445
|
return problem(404, 'Sendproof is unavailable.');
|
|
446
|
+
if (session)
|
|
447
|
+
return sessionPreview(context, session, dispatch);
|
|
238
448
|
if (!previewProvider)
|
|
239
449
|
return problem(503, 'Preview requires an available provider.');
|
|
240
450
|
const capture = new Capture('preview', captureLimits, includeSqlParameterValues);
|
|
@@ -273,27 +483,8 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
273
483
|
if (capture.blocked)
|
|
274
484
|
return blockedPreview(capture.blocked);
|
|
275
485
|
if ('unmatched' in response)
|
|
276
|
-
return
|
|
277
|
-
|
|
278
|
-
return problem(response.status, 'Preview access denied.');
|
|
279
|
-
if (response.status >= 500)
|
|
280
|
-
return problem(500, 'The preview operation did not complete safely.');
|
|
281
|
-
if (response.exceeded || response.body.length > maxResponseBytes) {
|
|
282
|
-
return problem(413, 'The preview response exceeds the configured size limit.');
|
|
283
|
-
}
|
|
284
|
-
const headers = Object.create(null);
|
|
285
|
-
const connectionTokens = Object.entries(response.headers)
|
|
286
|
-
.filter(([name]) => name.toLowerCase() === 'connection')
|
|
287
|
-
.flatMap(([, value]) => String(value ?? '').toLowerCase().split(',').map(token => token.trim()));
|
|
288
|
-
for (const [name, value] of Object.entries(response.headers).slice(0, 64)) {
|
|
289
|
-
const lower = name.toLowerCase();
|
|
290
|
-
if (value !== undefined && !privateHeaders.has(lower) && !connectionTokens.includes(lower))
|
|
291
|
-
headers[lower] = String(value).slice(0, 2048);
|
|
292
|
-
}
|
|
293
|
-
return json(200, {
|
|
294
|
-
status: response.status, headers, body: responseBody(response.body, headers['content-type']),
|
|
295
|
-
...capture.finish().capture, rolledBack: true,
|
|
296
|
-
}, { 'Preference-Applied': 'tx=rollback' });
|
|
486
|
+
return unmatched();
|
|
487
|
+
return refusal(capture, response) ?? confirmed(capture, response);
|
|
297
488
|
}
|
|
298
489
|
catch (error) {
|
|
299
490
|
onError(error, context);
|
|
@@ -332,5 +523,5 @@ export function createBench(options = {}, registered = () => [], binding) {
|
|
|
332
523
|
},
|
|
333
524
|
};
|
|
334
525
|
}
|
|
335
|
-
return { lookup, preview, observation, maxResponseBytes };
|
|
526
|
+
return { lookup, preview, observation, maxResponseBytes, close: async () => sessions?.shutdown() };
|
|
336
527
|
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
declare const refValue: unique symbol;
|
|
2
|
+
declare const buildJourney: unique symbol;
|
|
3
|
+
/** A value bound when a journey runs. The type follows the input or the declared output. */
|
|
4
|
+
export type Ref<T> = JourneyRef<T>;
|
|
5
|
+
declare class JourneyRef<T> {
|
|
6
|
+
readonly expression: string;
|
|
7
|
+
readonly owner: symbol;
|
|
8
|
+
readonly [refValue]: T;
|
|
9
|
+
constructor(expression: string, owner: symbol);
|
|
10
|
+
}
|
|
11
|
+
type Scalar = string | number | boolean | null;
|
|
12
|
+
type Literal = Scalar | readonly Literal[] | {
|
|
13
|
+
readonly [key: string]: Literal;
|
|
14
|
+
};
|
|
15
|
+
type Value = Scalar | Ref<unknown> | readonly Value[] | {
|
|
16
|
+
readonly [key: string]: Value;
|
|
17
|
+
};
|
|
18
|
+
type Binding<T> = unknown extends T ? Value : Ref<T> | (T extends object ? {
|
|
19
|
+
readonly [K in keyof T]: Binding<T[K]>;
|
|
20
|
+
} : T);
|
|
21
|
+
type InputSchema = {
|
|
22
|
+
readonly type: 'object';
|
|
23
|
+
readonly properties: Readonly<Record<string, unknown>>;
|
|
24
|
+
} & Readonly<Record<string, unknown>>;
|
|
25
|
+
type JsonType = {
|
|
26
|
+
string: string;
|
|
27
|
+
number: number;
|
|
28
|
+
integer: number;
|
|
29
|
+
boolean: boolean;
|
|
30
|
+
null: null;
|
|
31
|
+
};
|
|
32
|
+
type InputType<S> = S extends {
|
|
33
|
+
readonly type: infer T;
|
|
34
|
+
} ? T extends keyof JsonType ? JsonType[T] : T extends 'array' ? S extends {
|
|
35
|
+
readonly items: infer I;
|
|
36
|
+
} ? InputType<I>[] : unknown[] : T extends 'object' ? Record<string, unknown> : unknown : unknown;
|
|
37
|
+
type ParameterLocation = 'path' | 'query' | 'header' | 'cookie';
|
|
38
|
+
type JourneyRequest = Partial<Record<ParameterLocation | 'body', unknown>>;
|
|
39
|
+
type OpenRequest = Record<ParameterLocation, Record<string, unknown>> & {
|
|
40
|
+
body: unknown;
|
|
41
|
+
};
|
|
42
|
+
type WriteFields = Readonly<Record<string, Value>>;
|
|
43
|
+
type WriteTarget = {
|
|
44
|
+
table: string;
|
|
45
|
+
key?: WriteFields;
|
|
46
|
+
} | {
|
|
47
|
+
collection: string;
|
|
48
|
+
key?: WriteFields;
|
|
49
|
+
} | {
|
|
50
|
+
key: string | Ref<string>;
|
|
51
|
+
};
|
|
52
|
+
export type ExpectedWrite = WriteTarget & {
|
|
53
|
+
/** Use the system ID advertised in capabilities, such as sqlite, mongodb or redis. */
|
|
54
|
+
store: string;
|
|
55
|
+
op: string;
|
|
56
|
+
values?: WriteFields;
|
|
57
|
+
};
|
|
58
|
+
export type ExpectedEffect = {
|
|
59
|
+
kind: string;
|
|
60
|
+
readonly [field: string]: Value;
|
|
61
|
+
};
|
|
62
|
+
type StepExpectations = {
|
|
63
|
+
persona?: string;
|
|
64
|
+
writes?: readonly ExpectedWrite[];
|
|
65
|
+
effects?: readonly ExpectedEffect[];
|
|
66
|
+
unlisted?: 'fail' | 'warn' | 'ignore';
|
|
67
|
+
};
|
|
68
|
+
export interface ApprovedDifference {
|
|
69
|
+
id: string;
|
|
70
|
+
step: string;
|
|
71
|
+
target: {
|
|
72
|
+
store: string;
|
|
73
|
+
field?: string;
|
|
74
|
+
} & ({
|
|
75
|
+
table: string;
|
|
76
|
+
} | {
|
|
77
|
+
collection: string;
|
|
78
|
+
} | {
|
|
79
|
+
key: string;
|
|
80
|
+
});
|
|
81
|
+
kind: 'value' | 'missing' | 'extra';
|
|
82
|
+
expected?: Literal;
|
|
83
|
+
actual?: Literal;
|
|
84
|
+
reason: string;
|
|
85
|
+
approvedBy: string;
|
|
86
|
+
approvedOn: string;
|
|
87
|
+
expires?: string;
|
|
88
|
+
link?: string;
|
|
89
|
+
}
|
|
90
|
+
type MatchType = 'any' | 'date-time' | 'uuid' | 'string' | 'number' | 'integer' | 'boolean';
|
|
91
|
+
export declare const match: {
|
|
92
|
+
any: (type?: MatchType) => {
|
|
93
|
+
type: MatchType;
|
|
94
|
+
};
|
|
95
|
+
pattern: (pattern: string) => {
|
|
96
|
+
pattern: string;
|
|
97
|
+
};
|
|
98
|
+
changed: (from: Value, to: Value) => {
|
|
99
|
+
from: Value;
|
|
100
|
+
to: Value;
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
export interface JourneyOptions<S extends InputSchema = InputSchema> {
|
|
104
|
+
id: string;
|
|
105
|
+
name: string;
|
|
106
|
+
summary?: string;
|
|
107
|
+
description?: string;
|
|
108
|
+
group?: string;
|
|
109
|
+
runAs?: string;
|
|
110
|
+
/** The reason this journey requires Send. Redis write expectations also imply Send. */
|
|
111
|
+
sendOnly?: string;
|
|
112
|
+
definedIn?: {
|
|
113
|
+
type: string;
|
|
114
|
+
file?: string;
|
|
115
|
+
};
|
|
116
|
+
inputs?: S;
|
|
117
|
+
}
|
|
118
|
+
type Step = {
|
|
119
|
+
stepId: string;
|
|
120
|
+
operationId: string;
|
|
121
|
+
parameters?: {
|
|
122
|
+
in: ParameterLocation;
|
|
123
|
+
name: string;
|
|
124
|
+
value: unknown;
|
|
125
|
+
}[];
|
|
126
|
+
requestBody?: {
|
|
127
|
+
contentType: string;
|
|
128
|
+
payload: unknown;
|
|
129
|
+
};
|
|
130
|
+
successCriteria?: {
|
|
131
|
+
condition: string;
|
|
132
|
+
}[];
|
|
133
|
+
outputs?: Record<string, string>;
|
|
134
|
+
};
|
|
135
|
+
export declare class JourneyStep<R extends JourneyRequest> {
|
|
136
|
+
private readonly step;
|
|
137
|
+
private readonly expectations;
|
|
138
|
+
private readonly owner;
|
|
139
|
+
private readonly problems;
|
|
140
|
+
/** @internal Created by the journey builder. */
|
|
141
|
+
constructor(step: Step, expectations: StepExpectations, owner: symbol, problems: string[]);
|
|
142
|
+
set<L extends ParameterLocation, K extends keyof NonNullable<R[L]> & string>(location: L, name: K, value: Binding<NonNullable<R[L]>[K]>): this;
|
|
143
|
+
body(payload: Binding<R['body']>, contentType?: string): this;
|
|
144
|
+
succeedsWith(status: number): this;
|
|
145
|
+
succeedsWhen(condition: string): this;
|
|
146
|
+
/** Declare the response expression and, for typed consumers, its resolved type. */
|
|
147
|
+
output<T = unknown>(name: string, expression: string): Ref<T>;
|
|
148
|
+
runAs(persona: string): this;
|
|
149
|
+
/** Use an empty writes array to assert that a step writes nothing. */
|
|
150
|
+
expectWrites(expectations: Omit<StepExpectations, 'persona'>): this;
|
|
151
|
+
}
|
|
152
|
+
export interface JourneyBuilder<S extends InputSchema> {
|
|
153
|
+
input<K extends keyof S['properties'] & string>(name: K): Ref<InputType<S['properties'][K]>>;
|
|
154
|
+
step<R extends JourneyRequest = OpenRequest>(stepId: string, operationId: string): JourneyStep<R>;
|
|
155
|
+
output(name: string, value: Ref<unknown>): void;
|
|
156
|
+
approveDifference(record: ApprovedDifference): void;
|
|
157
|
+
}
|
|
158
|
+
interface CompiledJourney {
|
|
159
|
+
workflow: {
|
|
160
|
+
workflowId: string;
|
|
161
|
+
};
|
|
162
|
+
expectations: unknown;
|
|
163
|
+
differences: (ApprovedDifference & {
|
|
164
|
+
journey: string;
|
|
165
|
+
})[];
|
|
166
|
+
problems: string[];
|
|
167
|
+
}
|
|
168
|
+
export interface CodeJourney {
|
|
169
|
+
readonly id: string;
|
|
170
|
+
readonly [buildJourney]: (operationIds: ReadonlySet<string>) => CompiledJourney;
|
|
171
|
+
}
|
|
172
|
+
/** Define a workflow once. The adapter evaluates its definition during startup. */
|
|
173
|
+
export declare function defineJourney<const S extends InputSchema = {
|
|
174
|
+
type: 'object';
|
|
175
|
+
properties: Record<never, never>;
|
|
176
|
+
}>(options: JourneyOptions<S>, define: (journey: JourneyBuilder<S>) => void): CodeJourney;
|
|
177
|
+
export declare function compileJourney(journey: CodeJourney, operationIds: ReadonlySet<string>): CompiledJourney;
|
|
178
|
+
export {};
|