@anatta.io/tva 0.0.0-stage → 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/README.md +50 -2
- package/dist/actions/action-consumer.d.ts +45 -0
- package/dist/actions/action-consumer.js +90 -0
- package/dist/actions/action.d.ts +67 -0
- package/dist/actions/action.js +12 -0
- package/dist/actions/index.d.ts +3 -0
- package/dist/actions/index.js +4 -0
- package/dist/actions/run-action.d.ts +31 -0
- package/dist/actions/run-action.js +113 -0
- package/dist/adapters/cloudflare/bindings.d.ts +10 -0
- package/dist/adapters/cloudflare/bindings.js +1 -0
- package/dist/adapters/cloudflare/blob-store.d.ts +39 -0
- package/dist/adapters/cloudflare/blob-store.js +95 -0
- package/dist/adapters/cloudflare/cache.d.ts +14 -0
- package/dist/adapters/cloudflare/cache.js +48 -0
- package/dist/adapters/cloudflare/faults.d.ts +7 -0
- package/dist/adapters/cloudflare/faults.js +39 -0
- package/dist/adapters/cloudflare/index.d.ts +15 -0
- package/dist/adapters/cloudflare/index.js +16 -0
- package/dist/adapters/cloudflare/jobs.d.ts +97 -0
- package/dist/adapters/cloudflare/jobs.js +218 -0
- package/dist/adapters/cloudflare/journal.d.ts +107 -0
- package/dist/adapters/cloudflare/journal.js +305 -0
- package/dist/adapters/cloudflare/ledger.d.ts +14 -0
- package/dist/adapters/cloudflare/ledger.js +108 -0
- package/dist/adapters/cloudflare/logger.d.ts +22 -0
- package/dist/adapters/cloudflare/logger.js +49 -0
- package/dist/adapters/cloudflare/manifest.d.ts +81 -0
- package/dist/adapters/cloudflare/manifest.js +81 -0
- package/dist/adapters/cloudflare/queue.d.ts +80 -0
- package/dist/adapters/cloudflare/queue.js +177 -0
- package/dist/adapters/cloudflare/retention.d.ts +24 -0
- package/dist/adapters/cloudflare/retention.js +103 -0
- package/dist/adapters/cloudflare/session-storage.d.ts +27 -0
- package/dist/adapters/cloudflare/session-storage.js +78 -0
- package/dist/adapters/cloudflare/toolkit.d.ts +16 -0
- package/dist/adapters/cloudflare/toolkit.js +57 -0
- package/dist/adapters/cloudflare/workflow-jobs.d.ts +57 -0
- package/dist/adapters/cloudflare/workflow-jobs.js +319 -0
- package/dist/adapters/journal-records.d.ts +79 -0
- package/dist/adapters/journal-records.js +74 -0
- package/dist/adapters/memory/blob-store.d.ts +9 -0
- package/dist/adapters/memory/blob-store.js +19 -0
- package/dist/adapters/memory/cache.d.ts +13 -0
- package/dist/adapters/memory/cache.js +36 -0
- package/dist/adapters/memory/clock.d.ts +1 -0
- package/dist/adapters/memory/clock.js +4 -0
- package/dist/adapters/memory/index.d.ts +11 -0
- package/dist/adapters/memory/index.js +11 -0
- package/dist/adapters/memory/jobs.d.ts +62 -0
- package/dist/adapters/memory/jobs.js +219 -0
- package/dist/adapters/memory/journal.d.ts +34 -0
- package/dist/adapters/memory/journal.js +135 -0
- package/dist/adapters/memory/ledger.d.ts +9 -0
- package/dist/adapters/memory/ledger.js +33 -0
- package/dist/adapters/memory/logger.d.ts +16 -0
- package/dist/adapters/memory/logger.js +21 -0
- package/dist/adapters/memory/manifest.d.ts +59 -0
- package/dist/adapters/memory/manifest.js +59 -0
- package/dist/adapters/memory/queue.d.ts +20 -0
- package/dist/adapters/memory/queue.js +102 -0
- package/dist/adapters/memory/session-storage.d.ts +20 -0
- package/dist/adapters/memory/session-storage.js +33 -0
- package/dist/adapters/memory/toolkit.d.ts +8 -0
- package/dist/adapters/memory/toolkit.js +31 -0
- package/dist/adapters/postgres/cache.d.ts +17 -0
- package/dist/adapters/postgres/cache.js +69 -0
- package/dist/adapters/postgres/config.d.ts +27 -0
- package/dist/adapters/postgres/config.js +45 -0
- package/dist/adapters/postgres/faults.d.ts +14 -0
- package/dist/adapters/postgres/faults.js +47 -0
- package/dist/adapters/postgres/index.d.ts +12 -0
- package/dist/adapters/postgres/index.js +12 -0
- package/dist/adapters/postgres/journal.d.ts +83 -0
- package/dist/adapters/postgres/journal.js +314 -0
- package/dist/adapters/postgres/ledger.d.ts +16 -0
- package/dist/adapters/postgres/ledger.js +97 -0
- package/dist/adapters/postgres/logger.d.ts +19 -0
- package/dist/adapters/postgres/logger.js +37 -0
- package/dist/adapters/postgres/manifest.d.ts +52 -0
- package/dist/adapters/postgres/manifest.js +53 -0
- package/dist/adapters/postgres/migrate.d.ts +20 -0
- package/dist/adapters/postgres/migrate.js +96 -0
- package/dist/adapters/postgres/queue.d.ts +64 -0
- package/dist/adapters/postgres/queue.js +333 -0
- package/dist/adapters/postgres/retention.d.ts +18 -0
- package/dist/adapters/postgres/retention.js +104 -0
- package/dist/adapters/postgres/session-storage.d.ts +20 -0
- package/dist/adapters/postgres/session-storage.js +71 -0
- package/dist/adapters/postgres/toolkit.d.ts +25 -0
- package/dist/adapters/postgres/toolkit.js +58 -0
- package/dist/adapters/postgres/with-client.d.ts +11 -0
- package/dist/adapters/postgres/with-client.js +29 -0
- package/dist/adapters/reference-executor.d.ts +171 -0
- package/dist/adapters/reference-executor.js +564 -0
- package/dist/adapters/retention.d.ts +18 -0
- package/dist/adapters/retention.js +13 -0
- package/dist/adapters/s3/blob-store.d.ts +35 -0
- package/dist/adapters/s3/blob-store.js +92 -0
- package/dist/adapters/s3/faults.d.ts +8 -0
- package/dist/adapters/s3/faults.js +34 -0
- package/dist/adapters/s3/index.d.ts +2 -0
- package/dist/adapters/s3/index.js +2 -0
- package/dist/adapters/session-envelope.d.ts +21 -0
- package/dist/adapters/session-envelope.js +122 -0
- package/dist/adapters/system-clock.d.ts +8 -0
- package/dist/adapters/system-clock.js +8 -0
- package/dist/contracts/blob-store.d.ts +11 -0
- package/dist/contracts/blob-store.js +1 -0
- package/dist/contracts/cache.d.ts +27 -0
- package/dist/contracts/cache.js +1 -0
- package/dist/contracts/duration.d.ts +23 -0
- package/dist/contracts/duration.js +27 -0
- package/dist/contracts/faults.d.ts +28 -0
- package/dist/contracts/faults.js +29 -0
- package/dist/contracts/identity.d.ts +12 -0
- package/dist/contracts/identity.js +1 -0
- package/dist/contracts/index.d.ts +14 -0
- package/dist/contracts/index.js +15 -0
- package/dist/contracts/instant.d.ts +17 -0
- package/dist/contracts/instant.js +12 -0
- package/dist/contracts/jobs.d.ts +335 -0
- package/dist/contracts/jobs.js +1 -0
- package/dist/contracts/journal.d.ts +186 -0
- package/dist/contracts/journal.js +10 -0
- package/dist/contracts/ledger.d.ts +34 -0
- package/dist/contracts/ledger.js +1 -0
- package/dist/contracts/logger.d.ts +11 -0
- package/dist/contracts/logger.js +1 -0
- package/dist/contracts/manifest.d.ts +37 -0
- package/dist/contracts/manifest.js +30 -0
- package/dist/contracts/queue.d.ts +55 -0
- package/dist/contracts/queue.js +1 -0
- package/dist/contracts/session-storage.d.ts +18 -0
- package/dist/contracts/session-storage.js +1 -0
- package/dist/contracts/toolkit.d.ts +21 -0
- package/dist/contracts/toolkit.js +1 -0
- package/dist/schedule/schedule.d.ts +40 -0
- package/dist/schedule/schedule.js +135 -0
- package/dist/webhooks/create-webhook-handler.d.ts +63 -0
- package/dist/webhooks/create-webhook-handler.js +246 -0
- package/dist/webhooks/dead-letter-consumer.d.ts +14 -0
- package/dist/webhooks/dead-letter-consumer.js +42 -0
- package/dist/webhooks/define-registry.d.ts +42 -0
- package/dist/webhooks/define-registry.js +56 -0
- package/dist/webhooks/once.d.ts +15 -0
- package/dist/webhooks/once.js +29 -0
- package/dist/webhooks/redact-customer-bodies.d.ts +29 -0
- package/dist/webhooks/redact-customer-bodies.js +45 -0
- package/dist/webhooks/replay-webhooks.d.ts +39 -0
- package/dist/webhooks/replay-webhooks.js +79 -0
- package/dist/webhooks/sign-shopify-webhook.d.ts +14 -0
- package/dist/webhooks/sign-shopify-webhook.js +31 -0
- package/dist/webhooks/webhook-consumer.d.ts +51 -0
- package/dist/webhooks/webhook-consumer.js +127 -0
- package/migrations/0001_create_ledger_claims.sql +23 -0
- package/migrations/0002_create_journal.sql +76 -0
- package/migrations/0003_create_shopify_sessions.sql +30 -0
- package/migrations/0004_create_journal_steps.sql +34 -0
- package/migrations/0007_create_job_tables.sql +71 -0
- package/migrations/0008_add_ledger_completed_at.sql +7 -0
- package/migrations/0009_create_webhook_receipts.sql +34 -0
- package/migrations/0010_index_webhook_deliveries_received_at.sql +2 -0
- package/migrations/0011_add_webhook_task_reason_code.sql +4 -0
- package/migrations/postgres/0001_create_ledger_claims.sql +13 -0
- package/migrations/postgres/0002_create_journal.sql +48 -0
- package/migrations/postgres/0003_create_shopify_sessions.sql +12 -0
- package/migrations/postgres/0004_create_journal_steps.sql +19 -0
- package/migrations/postgres/0005_create_cache_entries.sql +14 -0
- package/migrations/postgres/0006_create_queue.sql +42 -0
- package/migrations/postgres/0007_create_job_tables.sql +40 -0
- package/migrations/postgres/0008_add_ledger_completed_at.sql +3 -0
- package/migrations/postgres/0009_create_webhook_receipts.sql +23 -0
- package/migrations/postgres/0010_index_webhook_deliveries_received_at.sql +2 -0
- package/migrations/postgres/0011_add_webhook_task_reason_code.sql +3 -0
- package/package.json +137 -4
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { DeleteObjectCommand, GetObjectCommand, paginateListObjectsV2, PutObjectCommand, } from '@aws-sdk/client-s3';
|
|
2
|
+
import { LimitExceeded } from '../../contracts/index.js';
|
|
3
|
+
import { toFault } from './faults.js';
|
|
4
|
+
/** The name this tool answers to in a fault message and a log line. */
|
|
5
|
+
const TOOL = 'blobs';
|
|
6
|
+
/**
|
|
7
|
+
* The largest single PUT S3 accepts. The adapter does no multipart upload, so
|
|
8
|
+
* this is the most it can honestly promise. docs/platform-facts.md, S3.
|
|
9
|
+
*/
|
|
10
|
+
export const S3_MAX_PUT_BYTES = 5_368_709_120;
|
|
11
|
+
/**
|
|
12
|
+
* BlobStore over any S3-compatible bucket.
|
|
13
|
+
*
|
|
14
|
+
* S3 gives away what the contract asks for: a prefix is a string prefix, and
|
|
15
|
+
* deleting a missing key succeeds. Only `list` needs work, because a page
|
|
16
|
+
* holds at most 1,000 keys.
|
|
17
|
+
*/
|
|
18
|
+
export class S3BlobStore {
|
|
19
|
+
o;
|
|
20
|
+
constructor(o) {
|
|
21
|
+
this.o = o;
|
|
22
|
+
}
|
|
23
|
+
async put(key, body, options) {
|
|
24
|
+
// Encoded before the size check, because the limit is bytes.
|
|
25
|
+
const bytes = typeof body === 'string' ? new TextEncoder().encode(body) : body;
|
|
26
|
+
if (bytes.byteLength > this.o.maxBytes) {
|
|
27
|
+
throw new LimitExceeded(TOOL, `body of ${bytes.byteLength} bytes is above blobMaxBytes ${this.o.maxBytes}`);
|
|
28
|
+
}
|
|
29
|
+
await this.#call('put', async () => this.o.client.send(new PutObjectCommand({
|
|
30
|
+
Bucket: this.o.bucket,
|
|
31
|
+
Key: key,
|
|
32
|
+
Body: bytes,
|
|
33
|
+
ContentType: options?.contentType,
|
|
34
|
+
})));
|
|
35
|
+
}
|
|
36
|
+
async get(key) {
|
|
37
|
+
return this.#call('get', async () => {
|
|
38
|
+
const object = await this.#getObject(key);
|
|
39
|
+
return object === undefined
|
|
40
|
+
? undefined
|
|
41
|
+
: ((await object.Body?.transformToByteArray()) ?? new Uint8Array());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async getText(key) {
|
|
45
|
+
return this.#call('getText', async () => {
|
|
46
|
+
const object = await this.#getObject(key);
|
|
47
|
+
return object === undefined
|
|
48
|
+
? undefined
|
|
49
|
+
: ((await object.Body?.transformToString('utf-8')) ?? '');
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async delete(key) {
|
|
53
|
+
await this.#call('delete', async () => this.o.client.send(new DeleteObjectCommand({ Bucket: this.o.bucket, Key: key })));
|
|
54
|
+
}
|
|
55
|
+
/** Pages to completion. One unpaged call would return 1,000 keys and no error. */
|
|
56
|
+
async list(prefix) {
|
|
57
|
+
return this.#call('list', async () => {
|
|
58
|
+
const keys = [];
|
|
59
|
+
const pages = paginateListObjectsV2({ client: this.o.client }, { Bucket: this.o.bucket, Prefix: prefix });
|
|
60
|
+
for await (const page of pages) {
|
|
61
|
+
for (const object of page.Contents ?? []) {
|
|
62
|
+
if (object.Key !== undefined) {
|
|
63
|
+
keys.push(object.Key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// S3 orders by UTF-8 bytes, which is not JavaScript's string order
|
|
68
|
+
// outside the BMP, so the contract's sort is applied here.
|
|
69
|
+
return keys.sort();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** A missing key is an outcome, not a fault, so it is caught before #call. */
|
|
73
|
+
async #getObject(key) {
|
|
74
|
+
try {
|
|
75
|
+
return await this.o.client.send(new GetObjectCommand({ Bucket: this.o.bucket, Key: key }));
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
if (e instanceof Error && e.name === 'NoSuchKey') {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async #call(operation, f) {
|
|
85
|
+
try {
|
|
86
|
+
return await f();
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
throw toFault(TOOL, operation, e);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ToolFault } from '../../contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Unknown failures, including a refused connection, which has no HTTP status,
|
|
4
|
+
* are Unavailable and so retryable. A retryable answer to something permanent
|
|
5
|
+
* costs a few attempts; a permanent answer to something transient loses the
|
|
6
|
+
* work.
|
|
7
|
+
*/
|
|
8
|
+
export declare function toFault(tool: string, operation: string, e: unknown): ToolFault;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LimitExceeded, Misconfigured, Throttled, Unavailable, } from '../../contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* An S3 client error carries the service's error code as `name` and the HTTP
|
|
4
|
+
* status in `$metadata`. Both are read here, in one place, and the vendor
|
|
5
|
+
* error is kept as `cause`, never in the caller's message.
|
|
6
|
+
*
|
|
7
|
+
* https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
|
|
8
|
+
*/
|
|
9
|
+
const statusOf = (e) => e?.$metadata
|
|
10
|
+
?.httpStatusCode;
|
|
11
|
+
const nameOf = (e) => e instanceof Error ? e.name : undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Unknown failures, including a refused connection, which has no HTTP status,
|
|
14
|
+
* are Unavailable and so retryable. A retryable answer to something permanent
|
|
15
|
+
* costs a few attempts; a permanent answer to something transient loses the
|
|
16
|
+
* work.
|
|
17
|
+
*/
|
|
18
|
+
export function toFault(tool, operation, e) {
|
|
19
|
+
const status = statusOf(e);
|
|
20
|
+
const where = `${operation} failed`;
|
|
21
|
+
const options = { cause: e };
|
|
22
|
+
// 503 SlowDown is how S3 asks a client to back off. A 503 with another
|
|
23
|
+
// code is an outage, which is Unavailable below.
|
|
24
|
+
if (status === 429 || nameOf(e) === 'SlowDown') {
|
|
25
|
+
return new Throttled(tool, where, options);
|
|
26
|
+
}
|
|
27
|
+
// A missing key never reaches here, so a 404 is a missing bucket.
|
|
28
|
+
if (status === 400 || status === 401 || status === 403 || status === 404) {
|
|
29
|
+
return nameOf(e) === 'EntityTooLarge'
|
|
30
|
+
? new LimitExceeded(tool, where, options)
|
|
31
|
+
: new Misconfigured(tool, where, options);
|
|
32
|
+
}
|
|
33
|
+
return new Unavailable(tool, where, options);
|
|
34
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Session } from '@shopify/shopify-api';
|
|
2
|
+
/** What a session becomes at rest: the columns of `shopify_sessions`. */
|
|
3
|
+
export type SealedSession = {
|
|
4
|
+
readonly version: number;
|
|
5
|
+
readonly iv: Uint8Array<ArrayBuffer>;
|
|
6
|
+
readonly ciphertext: Uint8Array<ArrayBuffer>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The encryption at rest of decision 0005, shared by every SQL adapter so a
|
|
10
|
+
* session sealed on one database opens on another with the same key.
|
|
11
|
+
*
|
|
12
|
+
* Takes the raw secret, base64 decoding to 32 bytes, not a CryptoKey:
|
|
13
|
+
* importing is async and the adapters' constructors stay synchronous.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SessionEnvelope {
|
|
16
|
+
#private;
|
|
17
|
+
private readonly key;
|
|
18
|
+
constructor(key: string);
|
|
19
|
+
seal(session: Session): Promise<SealedSession>;
|
|
20
|
+
open(id: string, shop: string, iv: Uint8Array<ArrayBuffer>, ciphertext: Uint8Array<ArrayBuffer>): Promise<Session>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Session } from '@shopify/shopify-api';
|
|
2
|
+
import { Misconfigured } from '../contracts/index.js';
|
|
3
|
+
const TOOL = 'sessions';
|
|
4
|
+
/** AES-256-GCM. 32-byte key, 12-byte IV — the standard WebCrypto nonce size. */
|
|
5
|
+
const KEY_BYTES = 32;
|
|
6
|
+
const IV_BYTES = 12;
|
|
7
|
+
/**
|
|
8
|
+
* Schema/envelope version. Bump this, not the column shape, on a future key
|
|
9
|
+
* rotation scheme — see #36's non-goals: rotation itself is out of scope, but
|
|
10
|
+
* a version with no reader for "2" yet is what makes rotation possible to
|
|
11
|
+
* write later without a blind migration over every existing row.
|
|
12
|
+
*/
|
|
13
|
+
const ENVELOPE_VERSION = 1;
|
|
14
|
+
/**
|
|
15
|
+
* exactOptionalPropertyTypes draws a line between "absent" and "present and
|
|
16
|
+
* undefined". Keyed on `undefined` rather than journal-records.ts's `null`:
|
|
17
|
+
* the omission happens on a plain object field, not on a column read back.
|
|
18
|
+
*/
|
|
19
|
+
const optional = (key, value) => value === undefined ? {} : { [key]: value };
|
|
20
|
+
const toSecret = (s) => ({
|
|
21
|
+
state: s.state,
|
|
22
|
+
isOnline: s.isOnline,
|
|
23
|
+
...optional('scope', s.scope),
|
|
24
|
+
...optional('expires', s.expires?.toISOString()),
|
|
25
|
+
...optional('accessToken', s.accessToken),
|
|
26
|
+
...optional('refreshToken', s.refreshToken),
|
|
27
|
+
...optional('refreshTokenExpires', s.refreshTokenExpires?.toISOString()),
|
|
28
|
+
...optional('onlineAccessInfo', s.onlineAccessInfo),
|
|
29
|
+
});
|
|
30
|
+
const toSession = (id, shop, secret) => new Session({
|
|
31
|
+
id,
|
|
32
|
+
shop,
|
|
33
|
+
state: secret.state,
|
|
34
|
+
isOnline: secret.isOnline,
|
|
35
|
+
...optional('scope', secret.scope),
|
|
36
|
+
...optional('expires', secret.expires === undefined ? undefined : new Date(secret.expires)),
|
|
37
|
+
...optional('accessToken', secret.accessToken),
|
|
38
|
+
...optional('refreshToken', secret.refreshToken),
|
|
39
|
+
...optional('refreshTokenExpires', secret.refreshTokenExpires === undefined
|
|
40
|
+
? undefined
|
|
41
|
+
: new Date(secret.refreshTokenExpires)),
|
|
42
|
+
...optional('onlineAccessInfo', secret.onlineAccessInfo),
|
|
43
|
+
});
|
|
44
|
+
function decodeBase64(s) {
|
|
45
|
+
const binary = atob(s);
|
|
46
|
+
const bytes = new Uint8Array(binary.length);
|
|
47
|
+
for (let i = 0; i < binary.length; i++) {
|
|
48
|
+
bytes[i] = binary.charCodeAt(i);
|
|
49
|
+
}
|
|
50
|
+
return bytes;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The encryption at rest of decision 0005, shared by every SQL adapter so a
|
|
54
|
+
* session sealed on one database opens on another with the same key.
|
|
55
|
+
*
|
|
56
|
+
* Takes the raw secret, base64 decoding to 32 bytes, not a CryptoKey:
|
|
57
|
+
* importing is async and the adapters' constructors stay synchronous.
|
|
58
|
+
*/
|
|
59
|
+
export class SessionEnvelope {
|
|
60
|
+
key;
|
|
61
|
+
#keyPromise;
|
|
62
|
+
constructor(key) {
|
|
63
|
+
this.key = key;
|
|
64
|
+
}
|
|
65
|
+
async seal(session) {
|
|
66
|
+
return {
|
|
67
|
+
version: ENVELOPE_VERSION,
|
|
68
|
+
...(await this.#encrypt(toSecret(session))),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async open(id, shop, iv, ciphertext) {
|
|
72
|
+
return toSession(id, shop, await this.#decrypt(iv, ciphertext));
|
|
73
|
+
}
|
|
74
|
+
async #encrypt(secret) {
|
|
75
|
+
const key = await this.#getKey();
|
|
76
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES));
|
|
77
|
+
const plaintext = new TextEncoder().encode(JSON.stringify(secret));
|
|
78
|
+
const ciphertext = new Uint8Array(await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, plaintext));
|
|
79
|
+
return { iv, ciphertext };
|
|
80
|
+
}
|
|
81
|
+
async #decrypt(iv, ciphertext) {
|
|
82
|
+
const key = await this.#getKey();
|
|
83
|
+
let plaintext;
|
|
84
|
+
try {
|
|
85
|
+
plaintext = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext);
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
// GCM refuses a wrong key and a changed row alike. WebCrypto's own
|
|
89
|
+
// OperationError names neither the tool nor the key.
|
|
90
|
+
throw new Misconfigured(TOOL, 'a stored session did not decrypt with SESSION_ENC_KEY', { cause: e });
|
|
91
|
+
}
|
|
92
|
+
return JSON.parse(new TextDecoder().decode(plaintext));
|
|
93
|
+
}
|
|
94
|
+
/** Imported once, on first use, and memoized — including a failure, which
|
|
95
|
+
* stays failed, because a key that doesn't decode to 32 bytes never will. */
|
|
96
|
+
async #getKey() {
|
|
97
|
+
return await (this.#keyPromise ??= this.#importKey());
|
|
98
|
+
}
|
|
99
|
+
async #importKey() {
|
|
100
|
+
let raw;
|
|
101
|
+
try {
|
|
102
|
+
raw = decodeBase64(this.key);
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
throw new Misconfigured(TOOL, 'SESSION_ENC_KEY is not valid base64', {
|
|
106
|
+
cause: e,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (raw.byteLength !== KEY_BYTES) {
|
|
110
|
+
throw new Misconfigured(TOOL, `SESSION_ENC_KEY must decode to ${KEY_BYTES} bytes for AES-256-GCM, got ${raw.byteLength}`);
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return await crypto.subtle.importKey('raw', raw, 'AES-GCM', false, [
|
|
114
|
+
'encrypt',
|
|
115
|
+
'decrypt',
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
throw new Misconfigured(TOOL, 'SESSION_ENC_KEY is not usable as an AES-256-GCM key', { cause: e });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Clock } from '../contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* The one implementation of Clock that reads the wall clock. It lives above
|
|
4
|
+
* every adapter rather than inside one, because a real adapter needs it too
|
|
5
|
+
* and importing it from `adapters/memory` would make a real adapter depend on
|
|
6
|
+
* the fake one.
|
|
7
|
+
*/
|
|
8
|
+
export declare const systemClock: () => Clock;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { fromEpochMs } from '../contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* The one implementation of Clock that reads the wall clock. It lives above
|
|
4
|
+
* every adapter rather than inside one, because a real adapter needs it too
|
|
5
|
+
* and importing it from `adapters/memory` would make a real adapter depend on
|
|
6
|
+
* the fake one.
|
|
7
|
+
*/
|
|
8
|
+
export const systemClock = () => ({ now: () => fromEpochMs(Date.now()) });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type BlobKey = string;
|
|
2
|
+
export type PutOptions = {
|
|
3
|
+
readonly contentType?: string;
|
|
4
|
+
};
|
|
5
|
+
export interface BlobStore {
|
|
6
|
+
put(key: BlobKey, body: Uint8Array | string, options?: PutOptions): Promise<void>;
|
|
7
|
+
get(key: BlobKey): Promise<Uint8Array | undefined>;
|
|
8
|
+
getText(key: BlobKey): Promise<string | undefined>;
|
|
9
|
+
delete(key: BlobKey): Promise<void>;
|
|
10
|
+
list(prefix: string): Promise<readonly BlobKey[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type CacheKey = string;
|
|
2
|
+
/**
|
|
3
|
+
* Text in, text out. What is cached is a Shopify fact with a staleness budget,
|
|
4
|
+
* and it arrives and leaves as JSON. Bytes belong in BlobStore.
|
|
5
|
+
*
|
|
6
|
+
* Expiry is INCLUSIVE at the ttl: at exactly `ttlSeconds` after the write, the
|
|
7
|
+
* value is already gone. Stated here because every platform otherwise picks its
|
|
8
|
+
* own answer.
|
|
9
|
+
*/
|
|
10
|
+
export interface Cache {
|
|
11
|
+
/** `undefined` is a miss. The empty string is a hit. */
|
|
12
|
+
get(key: CacheKey): Promise<string | undefined>;
|
|
13
|
+
/**
|
|
14
|
+
* A second write replaces the value AND the ttl.
|
|
15
|
+
* A ttl below the adapter's declared cacheMinTtlSeconds is rejected with
|
|
16
|
+
* LimitExceeded — never clamped — and leaves no value behind.
|
|
17
|
+
*
|
|
18
|
+
* `ttlSeconds` must be a finite, non-negative number. Anything else — `NaN`,
|
|
19
|
+
* `Infinity`, a negative — is a caller bug rather than a target condition,
|
|
20
|
+
* and is rejected with a **TypeError, not a ToolFault**, so it escapes the
|
|
21
|
+
* retry envelope instead of being counted as something worth retrying. A
|
|
22
|
+
* rejected write leaves no value behind. See `checkDuration`.
|
|
23
|
+
*/
|
|
24
|
+
put(key: CacheKey, value: string, ttlSeconds: number): Promise<void>;
|
|
25
|
+
/** Idempotent. Deleting a key that does not exist is not an error. */
|
|
26
|
+
delete(key: CacheKey): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every duration an interface accepts passes through here, before any manifest
|
|
3
|
+
* check, because a manifest check cannot catch this: `NaN < 60` is false and
|
|
4
|
+
* `NaN > 86_400` is false, so a `NaN` passes a ceiling and a floor alike. The
|
|
5
|
+
* write then succeeds, the expiry is `NaN`, and the value never expires. The
|
|
6
|
+
* message is never visible, so it is never delivered. **The caller is told
|
|
7
|
+
* nothing.**
|
|
8
|
+
*
|
|
9
|
+
* THIS THROWS A TypeError, AND DELIBERATELY NOT A ToolFault.
|
|
10
|
+
*
|
|
11
|
+
* A `ToolFault` describes something the TARGET did — it was unavailable, it
|
|
12
|
+
* throttled us, the value was past a limit it publishes. Every one of them is
|
|
13
|
+
* something a caller may reasonably catch and retry, and `retryable` exists to
|
|
14
|
+
* say which. A `NaN` is none of that. It never reaches the target, retrying
|
|
15
|
+
* produces the same `NaN` for ever, and no value of `retryable` is honest
|
|
16
|
+
* about it.
|
|
17
|
+
*
|
|
18
|
+
* So it is a caller bug, and it escapes the retry envelope on purpose. A job
|
|
19
|
+
* that computes a ttl from a missing config field should crash where it did
|
|
20
|
+
* that, not accumulate silently-immortal cache entries while its error handler
|
|
21
|
+
* counts retryable faults.
|
|
22
|
+
*/
|
|
23
|
+
export declare function checkDuration(tool: string, field: string, value: number): void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every duration an interface accepts passes through here, before any manifest
|
|
3
|
+
* check, because a manifest check cannot catch this: `NaN < 60` is false and
|
|
4
|
+
* `NaN > 86_400` is false, so a `NaN` passes a ceiling and a floor alike. The
|
|
5
|
+
* write then succeeds, the expiry is `NaN`, and the value never expires. The
|
|
6
|
+
* message is never visible, so it is never delivered. **The caller is told
|
|
7
|
+
* nothing.**
|
|
8
|
+
*
|
|
9
|
+
* THIS THROWS A TypeError, AND DELIBERATELY NOT A ToolFault.
|
|
10
|
+
*
|
|
11
|
+
* A `ToolFault` describes something the TARGET did — it was unavailable, it
|
|
12
|
+
* throttled us, the value was past a limit it publishes. Every one of them is
|
|
13
|
+
* something a caller may reasonably catch and retry, and `retryable` exists to
|
|
14
|
+
* say which. A `NaN` is none of that. It never reaches the target, retrying
|
|
15
|
+
* produces the same `NaN` for ever, and no value of `retryable` is honest
|
|
16
|
+
* about it.
|
|
17
|
+
*
|
|
18
|
+
* So it is a caller bug, and it escapes the retry envelope on purpose. A job
|
|
19
|
+
* that computes a ttl from a missing config field should crash where it did
|
|
20
|
+
* that, not accumulate silently-immortal cache entries while its error handler
|
|
21
|
+
* counts retryable faults.
|
|
22
|
+
*/
|
|
23
|
+
export function checkDuration(tool, field, value) {
|
|
24
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
25
|
+
throw new TypeError(`${tool}: ${field} must be a finite, non-negative number, not ${String(value)}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An adapter converts every platform failure into one of these, so a caller never
|
|
3
|
+
* pattern-matches on a vendor error string.
|
|
4
|
+
*
|
|
5
|
+
* NOT represented here: an ambiguous write — one where the call may or may not have
|
|
6
|
+
* landed. That is an OUTCOME the caller records, never a fault it retries.
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class ToolFault extends Error {
|
|
9
|
+
readonly tool: string;
|
|
10
|
+
abstract readonly retryable: boolean;
|
|
11
|
+
constructor(tool: string, message: string, options?: {
|
|
12
|
+
cause?: unknown;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** The call did not happen. Distinguish from "I do not know whether it happened". */
|
|
16
|
+
export declare class Unavailable extends ToolFault {
|
|
17
|
+
readonly retryable = true;
|
|
18
|
+
}
|
|
19
|
+
export declare class Throttled extends ToolFault {
|
|
20
|
+
readonly retryable = true;
|
|
21
|
+
}
|
|
22
|
+
/** Too large, too long, out of bounds. It will be out of bounds next time too. */
|
|
23
|
+
export declare class LimitExceeded extends ToolFault {
|
|
24
|
+
readonly retryable = false;
|
|
25
|
+
}
|
|
26
|
+
export declare class Misconfigured extends ToolFault {
|
|
27
|
+
readonly retryable = false;
|
|
28
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An adapter converts every platform failure into one of these, so a caller never
|
|
3
|
+
* pattern-matches on a vendor error string.
|
|
4
|
+
*
|
|
5
|
+
* NOT represented here: an ambiguous write — one where the call may or may not have
|
|
6
|
+
* landed. That is an OUTCOME the caller records, never a fault it retries.
|
|
7
|
+
*/
|
|
8
|
+
export class ToolFault extends Error {
|
|
9
|
+
tool;
|
|
10
|
+
constructor(tool, message, options) {
|
|
11
|
+
super(`${tool}: ${message}`, options);
|
|
12
|
+
this.tool = tool;
|
|
13
|
+
this.name = new.target.name;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** The call did not happen. Distinguish from "I do not know whether it happened". */
|
|
17
|
+
export class Unavailable extends ToolFault {
|
|
18
|
+
retryable = true;
|
|
19
|
+
}
|
|
20
|
+
export class Throttled extends ToolFault {
|
|
21
|
+
retryable = true;
|
|
22
|
+
}
|
|
23
|
+
/** Too large, too long, out of bounds. It will be out of bounds next time too. */
|
|
24
|
+
export class LimitExceeded extends ToolFault {
|
|
25
|
+
retryable = false;
|
|
26
|
+
}
|
|
27
|
+
export class Misconfigured extends ToolFault {
|
|
28
|
+
retryable = false;
|
|
29
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Carried on every message, every journal row, every log line.
|
|
3
|
+
* It exists to answer one question that is otherwise unanswerable in an incident:
|
|
4
|
+
* WHICH DEPLOYMENT PROCESSED THIS RECORD? `NODE_ENV` cannot answer it.
|
|
5
|
+
*/
|
|
6
|
+
export type DeploymentIdentity = {
|
|
7
|
+
readonly application: string;
|
|
8
|
+
readonly environment: 'local' | 'preview' | 'staging' | 'production';
|
|
9
|
+
readonly deployment: string;
|
|
10
|
+
readonly gitSha: string;
|
|
11
|
+
readonly pullRequest?: number;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from './instant.js';
|
|
2
|
+
export * from './duration.js';
|
|
3
|
+
export * from './faults.js';
|
|
4
|
+
export * from './identity.js';
|
|
5
|
+
export * from './manifest.js';
|
|
6
|
+
export * from './queue.js';
|
|
7
|
+
export * from './ledger.js';
|
|
8
|
+
export * from './cache.js';
|
|
9
|
+
export * from './blob-store.js';
|
|
10
|
+
export * from './journal.js';
|
|
11
|
+
export * from './jobs.js';
|
|
12
|
+
export * from './logger.js';
|
|
13
|
+
export * from './session-storage.js';
|
|
14
|
+
export * from './toolkit.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The package entry point. Not a barrel inside src — modules import each other directly.
|
|
2
|
+
export * from './instant.js';
|
|
3
|
+
export * from './duration.js';
|
|
4
|
+
export * from './faults.js';
|
|
5
|
+
export * from './identity.js';
|
|
6
|
+
export * from './manifest.js';
|
|
7
|
+
export * from './queue.js';
|
|
8
|
+
export * from './ledger.js';
|
|
9
|
+
export * from './cache.js';
|
|
10
|
+
export * from './blob-store.js';
|
|
11
|
+
export * from './journal.js';
|
|
12
|
+
export * from './jobs.js';
|
|
13
|
+
export * from './logger.js';
|
|
14
|
+
export * from './session-storage.js';
|
|
15
|
+
export * from './toolkit.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Epoch milliseconds, UTC. Branded so a raw number cannot be passed by mistake. */
|
|
2
|
+
export type Instant = number & {
|
|
3
|
+
readonly __instant: unique symbol;
|
|
4
|
+
};
|
|
5
|
+
export declare const instant: (iso: string) => Instant;
|
|
6
|
+
export declare const fromEpochMs: (ms: number) => Instant;
|
|
7
|
+
export declare const plusSeconds: (a: Instant, s: number) => Instant;
|
|
8
|
+
export declare const isBefore: (a: Instant, b: Instant) => boolean;
|
|
9
|
+
export declare const isAfter: (a: Instant, b: Instant) => boolean;
|
|
10
|
+
export declare const toIso: (a: Instant) => string;
|
|
11
|
+
/**
|
|
12
|
+
* The only thing permitted to read the clock. Everything else takes `now: Instant`
|
|
13
|
+
* as an argument, so a boundary case can be a row in a test table.
|
|
14
|
+
*/
|
|
15
|
+
export interface Clock {
|
|
16
|
+
now(): Instant;
|
|
17
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const instant = (iso) => {
|
|
2
|
+
const ms = Date.parse(iso);
|
|
3
|
+
if (Number.isNaN(ms)) {
|
|
4
|
+
throw new Error(`Not an ISO-8601 instant: ${iso}`);
|
|
5
|
+
}
|
|
6
|
+
return ms;
|
|
7
|
+
};
|
|
8
|
+
export const fromEpochMs = (ms) => ms;
|
|
9
|
+
export const plusSeconds = (a, s) => (a + s * 1000);
|
|
10
|
+
export const isBefore = (a, b) => a < b;
|
|
11
|
+
export const isAfter = (a, b) => a > b;
|
|
12
|
+
export const toIso = (a) => new Date(a).toISOString();
|