@pplethai/cli 0.21.0 → 0.23.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 +12 -14
- package/dist/bin.js +102 -102
- package/package.json +3 -6
- package/templates/golden-path/.dev.vars.example +0 -33
- package/templates/golden-path/.env.example +0 -10
- package/templates/golden-path/.github/workflows/ci.yml +0 -22
- package/templates/golden-path/AGENTS.md +0 -1
- package/templates/golden-path/CLAUDE.md +0 -187
- package/templates/golden-path/README.md +0 -118
- package/templates/golden-path/gitignore +0 -17
- package/templates/golden-path/index.html +0 -16
- package/templates/golden-path/migrations/0001_init.sql +0 -10
- package/templates/golden-path/overlay-mtime.mjs +0 -30
- package/templates/golden-path/package-lock.json +0 -4318
- package/templates/golden-path/package.json +0 -41
- package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
- package/templates/golden-path/shared/api.ts +0 -325
- package/templates/golden-path/src/auth.ts +0 -230
- package/templates/golden-path/src/call-service.ts +0 -147
- package/templates/golden-path/src/dataset-query.ts +0 -470
- package/templates/golden-path/src/dataset-schema.ts +0 -398
- package/templates/golden-path/src/db.ts +0 -32
- package/templates/golden-path/src/dev-call-service.ts +0 -392
- package/templates/golden-path/src/dev-personas.ts +0 -186
- package/templates/golden-path/src/dev-read-dataset.ts +0 -444
- package/templates/golden-path/src/dev-registry.ts +0 -294
- package/templates/golden-path/src/email.ts +0 -224
- package/templates/golden-path/src/entry-name.ts +0 -114
- package/templates/golden-path/src/env.ts +0 -38
- package/templates/golden-path/src/files.ts +0 -38
- package/templates/golden-path/src/identity-header.ts +0 -17
- package/templates/golden-path/src/index.ts +0 -276
- package/templates/golden-path/src/legal-identity.ts +0 -180
- package/templates/golden-path/src/levels.ts +0 -132
- package/templates/golden-path/src/notification-response.ts +0 -55
- package/templates/golden-path/src/notify.ts +0 -311
- package/templates/golden-path/src/payment.ts +0 -554
- package/templates/golden-path/src/read-dataset.ts +0 -490
- package/templates/golden-path/src/registry.ts +0 -128
- package/templates/golden-path/src/vite-env.d.ts +0 -27
- package/templates/golden-path/test/apply-migrations.ts +0 -4
- package/templates/golden-path/test/call-service.test.ts +0 -288
- package/templates/golden-path/test/dev-call-service.test.ts +0 -455
- package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
- package/templates/golden-path/test/dev-registry.test.ts +0 -404
- package/templates/golden-path/test/effective-sub.test.ts +0 -101
- package/templates/golden-path/test/email-offline.test.ts +0 -51
- package/templates/golden-path/test/email.test.ts +0 -117
- package/templates/golden-path/test/env.d.ts +0 -17
- package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
- package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
- package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -71
- package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/harness.ts +0 -153
- package/templates/golden-path/test/interaction.test.ts +0 -230
- package/templates/golden-path/test/legal-identity.test.ts +0 -323
- package/templates/golden-path/test/membership.test.ts +0 -105
- package/templates/golden-path/test/notification-response.test.ts +0 -79
- package/templates/golden-path/test/payment-app.test.ts +0 -210
- package/templates/golden-path/test/payment-offline.test.ts +0 -91
- package/templates/golden-path/test/payment.test.ts +0 -427
- package/templates/golden-path/test/payments-offline.ts +0 -33
- package/templates/golden-path/test/read-dataset.test.ts +0 -387
- package/templates/golden-path/test/service-call.test.ts +0 -166
- package/templates/golden-path/test/smoke.test.ts +0 -464
- package/templates/golden-path/tsconfig.json +0 -20
- package/templates/golden-path/tsconfig.worker.json +0 -21
- package/templates/golden-path/vite.config.ts +0 -12
- package/templates/golden-path/vitest.config.ts +0 -28
- package/templates/golden-path/web/App.tsx +0 -478
- package/templates/golden-path/web/app.css +0 -83
- package/templates/golden-path/web/main.tsx +0 -51
- package/templates/golden-path/web/pple.ts +0 -124
- package/templates/golden-path/web/vite-env.d.ts +0 -10
- package/templates/golden-path/wrangler.jsonc +0 -89
- package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
- package/templates/payment-bridge/CLAUDE.md +0 -9
- package/templates/payment-bridge/README.md +0 -57
- package/templates/payment-bridge/docs/verify-webhook.md +0 -80
- package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
- package/templates/payment-bridge/gitignore +0 -5
- package/templates/payment-bridge/package-lock.json +0 -4198
- package/templates/payment-bridge/package.json +0 -21
- package/templates/payment-bridge/src/bridge-key.ts +0 -18
- package/templates/payment-bridge/src/env.ts +0 -14
- package/templates/payment-bridge/src/index.ts +0 -39
- package/templates/payment-bridge/src/platform.ts +0 -49
- package/templates/payment-bridge/src/webhook.ts +0 -86
- package/templates/payment-bridge/test/docs.test.ts +0 -9
- package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -71
- package/templates/payment-bridge/test/harness.ts +0 -78
- package/templates/payment-bridge/test/inbound.test.ts +0 -138
- package/templates/payment-bridge/test/webhook.test.ts +0 -174
- package/templates/payment-bridge/tsconfig.json +0 -22
- package/templates/payment-bridge/vitest.config.ts +0 -8
- package/templates/payment-bridge/wrangler.jsonc +0 -15
- package/templates/payment-bridge.version +0 -1
|
@@ -1,490 +0,0 @@
|
|
|
1
|
-
// A Consumer's read of one Dataset Object Type (ADR-0030). The Builder names
|
|
2
|
-
// the entry and the Object Type. This module adds the services address, the
|
|
3
|
-
// App Key, and — only when the caller passes one — the user token. It builds
|
|
4
|
-
// the filter, sort and limit query, looks an object up by id, and follows
|
|
5
|
-
// `next` when asked.
|
|
6
|
-
//
|
|
7
|
-
// Under `npm run dev`, while `DEV_PERSONA` is not `"none"`, the answer comes
|
|
8
|
-
// from the workspace overlay (ADR-0026, `dev-read-dataset.ts`). That branch
|
|
9
|
-
// is compiled out of the production build. Under vitest the fetch is the one
|
|
10
|
-
// the test injected: this module does not read the overlay, which CI does
|
|
11
|
-
// not have.
|
|
12
|
-
|
|
13
|
-
import { callService, PLATFORM_ERROR_HEADER, type CallServiceEnv } from "./call-service";
|
|
14
|
-
import { answerOfflineRead } from "./dev-read-dataset";
|
|
15
|
-
import { assertEntryName } from "./entry-name";
|
|
16
|
-
|
|
17
|
-
/** One page of a collection. `next` is null on the last page. */
|
|
18
|
-
export interface DatasetPage<T = Record<string, unknown>> {
|
|
19
|
-
objects: T[];
|
|
20
|
-
next: string | null;
|
|
21
|
-
sweptAt: string | null;
|
|
22
|
-
snapshot: number | null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** A singleton, or one object looked up by id. `object` is null when the sweep held none. */
|
|
26
|
-
export interface DatasetObject<T = Record<string, unknown>> {
|
|
27
|
-
object: T | null;
|
|
28
|
-
sweptAt: string | null;
|
|
29
|
-
snapshot: number | null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface DatasetRange {
|
|
33
|
-
gt?: string | number | boolean;
|
|
34
|
-
gte?: string | number | boolean;
|
|
35
|
-
lt?: string | number | boolean;
|
|
36
|
-
lte?: string | number | boolean;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Equality, a list (`in`), or a range on one indexed field. */
|
|
40
|
-
export type DatasetFilterValue =
|
|
41
|
-
| string
|
|
42
|
-
| number
|
|
43
|
-
| boolean
|
|
44
|
-
| readonly (string | number | boolean)[]
|
|
45
|
-
| DatasetRange;
|
|
46
|
-
|
|
47
|
-
interface ReadDatasetCommon {
|
|
48
|
-
/** End-user PPLE ID token. Sent as `x-pple-user-token` only when set. */
|
|
49
|
-
userToken?: string;
|
|
50
|
-
/**
|
|
51
|
-
* Test seam. Under vitest this is required: `readDataset` does not fall
|
|
52
|
-
* back to `globalThis.fetch`. Production omits it.
|
|
53
|
-
*/
|
|
54
|
-
fetchImpl?: typeof fetch;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface ReadDatasetPageOptions extends ReadDatasetCommon {
|
|
58
|
-
shape?: "collection";
|
|
59
|
-
id?: undefined;
|
|
60
|
-
ifNoneMatch?: undefined;
|
|
61
|
-
/** Equality, a repeated value (`in`), or `{ gt, gte, lt, lte }`. */
|
|
62
|
-
filter?: Record<string, DatasetFilterValue>;
|
|
63
|
-
/** `<index>` or `-<index>`. Omitted sorts by id ascending. */
|
|
64
|
-
sort?: string;
|
|
65
|
-
limit?: number;
|
|
66
|
-
/** Opaque cursor from a previous page. Bound to the filters and the sort. */
|
|
67
|
-
next?: string;
|
|
68
|
-
/**
|
|
69
|
-
* Walk `next` until the last page and return every object. `data.next` is
|
|
70
|
-
* null. If a later page's `snapshot` differs, the walk starts over so one
|
|
71
|
-
* result does not mix two sweeps. After three such starts the result is
|
|
72
|
-
* `{ ok: false, error: null }`.
|
|
73
|
-
*/
|
|
74
|
-
follow?: boolean;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface ReadDatasetSingletonOptions extends ReadDatasetCommon {
|
|
78
|
-
shape: "singleton";
|
|
79
|
-
id?: undefined;
|
|
80
|
-
ifNoneMatch?: undefined;
|
|
81
|
-
/** Sent as-is. The platform answers `invalid_query` for any query on a singleton. */
|
|
82
|
-
filter?: Record<string, DatasetFilterValue>;
|
|
83
|
-
sort?: string;
|
|
84
|
-
limit?: number;
|
|
85
|
-
next?: string;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export interface ReadDatasetLookupOptions extends ReadDatasetCommon {
|
|
89
|
-
/** Look up this id. A query string as well is `invalid_query`. */
|
|
90
|
-
id: string;
|
|
91
|
-
shape?: "collection" | "singleton";
|
|
92
|
-
ifNoneMatch?: undefined;
|
|
93
|
-
filter?: Record<string, DatasetFilterValue>;
|
|
94
|
-
sort?: string;
|
|
95
|
-
limit?: number;
|
|
96
|
-
next?: string;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* The conditional forms: the same three single-request shapes, plus an
|
|
101
|
-
* `ifNoneMatch` that is sent as `If-None-Match`. Passing one widens the result
|
|
102
|
-
* to include `DatasetNotModified`. `follow: true` is not accepted with it:
|
|
103
|
-
* a walk has one ETag per page, so one conditional request does not apply. It
|
|
104
|
-
* is a type error, and `readDataset` throws if it gets one anyway.
|
|
105
|
-
*/
|
|
106
|
-
interface Conditional {
|
|
107
|
-
/**
|
|
108
|
-
* An `etag` from an earlier success or not-modified result. Sent as
|
|
109
|
-
* `If-None-Match`. When the data has not changed the platform answers `304`
|
|
110
|
-
* and `readDataset` returns `{ ok: true, status: 304, etag, notModified: true }`
|
|
111
|
-
* with no `data`. Otherwise the answer is the usual success or refusal.
|
|
112
|
-
* `undefined` (a first read, with nothing cached yet) sends no header.
|
|
113
|
-
*/
|
|
114
|
-
ifNoneMatch: string | undefined;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export type ReadDatasetConditionalPageOptions = Omit<ReadDatasetPageOptions, "ifNoneMatch" | "follow"> &
|
|
118
|
-
Conditional & { follow?: false };
|
|
119
|
-
export type ReadDatasetConditionalSingletonOptions = Omit<ReadDatasetSingletonOptions, "ifNoneMatch"> &
|
|
120
|
-
Conditional;
|
|
121
|
-
export type ReadDatasetConditionalLookupOptions = Omit<ReadDatasetLookupOptions, "ifNoneMatch"> & Conditional;
|
|
122
|
-
|
|
123
|
-
type AnyReadOptions =
|
|
124
|
-
| ReadDatasetPageOptions
|
|
125
|
-
| ReadDatasetSingletonOptions
|
|
126
|
-
| ReadDatasetLookupOptions
|
|
127
|
-
| ReadDatasetConditionalPageOptions
|
|
128
|
-
| ReadDatasetConditionalSingletonOptions
|
|
129
|
-
| ReadDatasetConditionalLookupOptions;
|
|
130
|
-
|
|
131
|
-
export type DatasetSuccess<T> = {
|
|
132
|
-
ok: true;
|
|
133
|
-
status: number;
|
|
134
|
-
etag: string | null;
|
|
135
|
-
data: T;
|
|
136
|
-
notModified?: undefined;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The answer to a conditional read (`ifNoneMatch`) when the platform said
|
|
141
|
-
* `304`: what the Builder already holds is still current. There is no `data`.
|
|
142
|
-
* `etag` is the platform's `ETag`, the same value that was sent.
|
|
143
|
-
*
|
|
144
|
-
* It only comes back from a read that passed `ifNoneMatch`. Narrow it with
|
|
145
|
-
* `result.ok && result.notModified`: on `true` the result is this type, and
|
|
146
|
-
* after `if (result.ok && !result.notModified)` it is a `DatasetSuccess` with
|
|
147
|
-
* `data`. A refusal has `ok: false`, so it never has `notModified`.
|
|
148
|
-
*/
|
|
149
|
-
export type DatasetNotModified = {
|
|
150
|
-
ok: true;
|
|
151
|
-
status: 304;
|
|
152
|
-
etag: string | null;
|
|
153
|
-
notModified: true;
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* A platform or dev refusal. `error` is the `x-pple-error` header, including
|
|
158
|
-
* the dev-only `no_mock`. `platformError` reads the same header and returns
|
|
159
|
-
* null for `no_mock` and for any code it does not know. Null `error` means
|
|
160
|
-
* the header was absent.
|
|
161
|
-
*/
|
|
162
|
-
export type DatasetFailure = {
|
|
163
|
-
ok: false;
|
|
164
|
-
status: number;
|
|
165
|
-
error: string | null;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
export type DatasetResult<T> = DatasetSuccess<T> | DatasetFailure;
|
|
169
|
-
|
|
170
|
-
/** What a conditional read returns: a `DatasetResult`, or `DatasetNotModified`. */
|
|
171
|
-
export type DatasetConditionalResult<T> = DatasetResult<T> | DatasetNotModified;
|
|
172
|
-
|
|
173
|
-
const RANGE_OPS = ["gt", "gte", "lt", "lte"] as const;
|
|
174
|
-
const FOLLOW_ATTEMPTS = 3;
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* `GET services.<domain>/<full name>/<objectType>[/<id>]`. `entry` is the full
|
|
178
|
-
* name: a bare name such as `"branch-list"`, a Namespace and a path such as
|
|
179
|
-
* `"election/score"`, or a platform entry such as `"platform/<name>"`. A name
|
|
180
|
-
* the shared validator refuses throws, the bare `"platform"` included.
|
|
181
|
-
*
|
|
182
|
-
* A collection (the default) is a page. `shape: "singleton"` or `id` is one
|
|
183
|
-
* object. `follow: true` walks a collection to the end. The App Key is always
|
|
184
|
-
* `Authorization: Bearer`.
|
|
185
|
-
*
|
|
186
|
-
* Polling: pass `ifNoneMatch: <the last etag>` on a page, a singleton or an id
|
|
187
|
-
* lookup. It is sent as `If-None-Match`. A `304` comes back as
|
|
188
|
-
* `{ ok: true, status: 304, etag, notModified: true }` with no `data`
|
|
189
|
-
* (`DatasetNotModified`), so keep what you read last time. A `200` with a
|
|
190
|
-
* different ETag is the usual success, with the new `etag`. `ifNoneMatch` with
|
|
191
|
-
* `follow: true` is refused: it is a type error, and `readDataset` throws.
|
|
192
|
-
* Offline answers the same way, from the mock's content.
|
|
193
|
-
*/
|
|
194
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
195
|
-
env: CallServiceEnv,
|
|
196
|
-
entry: string,
|
|
197
|
-
objectType: string,
|
|
198
|
-
options: ReadDatasetConditionalLookupOptions,
|
|
199
|
-
): Promise<DatasetConditionalResult<DatasetObject<T>>>;
|
|
200
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
201
|
-
env: CallServiceEnv,
|
|
202
|
-
entry: string,
|
|
203
|
-
objectType: string,
|
|
204
|
-
options: ReadDatasetConditionalSingletonOptions,
|
|
205
|
-
): Promise<DatasetConditionalResult<DatasetObject<T>>>;
|
|
206
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
207
|
-
env: CallServiceEnv,
|
|
208
|
-
entry: string,
|
|
209
|
-
objectType: string,
|
|
210
|
-
options: ReadDatasetConditionalPageOptions,
|
|
211
|
-
): Promise<DatasetConditionalResult<DatasetPage<T>>>;
|
|
212
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
213
|
-
env: CallServiceEnv,
|
|
214
|
-
entry: string,
|
|
215
|
-
objectType: string,
|
|
216
|
-
options: ReadDatasetLookupOptions,
|
|
217
|
-
): Promise<DatasetResult<DatasetObject<T>>>;
|
|
218
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
219
|
-
env: CallServiceEnv,
|
|
220
|
-
entry: string,
|
|
221
|
-
objectType: string,
|
|
222
|
-
options: ReadDatasetSingletonOptions,
|
|
223
|
-
): Promise<DatasetResult<DatasetObject<T>>>;
|
|
224
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
225
|
-
env: CallServiceEnv,
|
|
226
|
-
entry: string,
|
|
227
|
-
objectType: string,
|
|
228
|
-
options?: ReadDatasetPageOptions,
|
|
229
|
-
): Promise<DatasetResult<DatasetPage<T>>>;
|
|
230
|
-
export async function readDataset<T extends Record<string, unknown> = Record<string, unknown>>(
|
|
231
|
-
env: CallServiceEnv,
|
|
232
|
-
entry: string,
|
|
233
|
-
objectType: string,
|
|
234
|
-
options: AnyReadOptions = {},
|
|
235
|
-
): Promise<DatasetConditionalResult<DatasetPage<T> | DatasetObject<T>>> {
|
|
236
|
-
assertEntryName("readDataset", entry);
|
|
237
|
-
if (objectType === "" || /[/\s?#]/.test(objectType)) {
|
|
238
|
-
throw new Error("readDataset objectType must be one path segment");
|
|
239
|
-
}
|
|
240
|
-
if ("id" in options && options.id === "") {
|
|
241
|
-
throw new Error("readDataset id must not be empty");
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// The types refuse this pair. A caller that got past them (JS, a cast) throws.
|
|
245
|
-
if (options.ifNoneMatch !== undefined && (options as { follow?: boolean }).follow === true) {
|
|
246
|
-
throw new Error("readDataset ifNoneMatch does not apply with follow: a walk has one ETag per page");
|
|
247
|
-
}
|
|
248
|
-
if (isPageRequest(options) && options.follow === true) {
|
|
249
|
-
return followPages(env, entry, objectType, options);
|
|
250
|
-
}
|
|
251
|
-
return readOnce(env, entry, objectType, options);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
async function followPages<T extends Record<string, unknown>>(
|
|
255
|
-
env: CallServiceEnv,
|
|
256
|
-
entry: string,
|
|
257
|
-
objectType: string,
|
|
258
|
-
options: ReadDatasetPageOptions,
|
|
259
|
-
): Promise<DatasetResult<DatasetPage<T>>> {
|
|
260
|
-
let cursor = options.next;
|
|
261
|
-
for (let attempt = 0; attempt < FOLLOW_ATTEMPTS; attempt++) {
|
|
262
|
-
const walked = await walkPages<T>(env, entry, objectType, options, cursor);
|
|
263
|
-
if (walked.outcome !== "moved") return walked.result;
|
|
264
|
-
cursor = undefined;
|
|
265
|
-
}
|
|
266
|
-
// The snapshot kept moving. There is no platform code for that.
|
|
267
|
-
return { ok: false, status: 200, error: null };
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
async function walkPages<T extends Record<string, unknown>>(
|
|
271
|
-
env: CallServiceEnv,
|
|
272
|
-
entry: string,
|
|
273
|
-
objectType: string,
|
|
274
|
-
options: ReadDatasetPageOptions,
|
|
275
|
-
cursor: string | undefined,
|
|
276
|
-
): Promise<{ outcome: "done"; result: DatasetResult<DatasetPage<T>> } | { outcome: "moved" }> {
|
|
277
|
-
const first = await readPage<T>(env, entry, objectType, pageOptions(options, cursor));
|
|
278
|
-
if (!first.ok) return { outcome: "done", result: first };
|
|
279
|
-
if (!isPage<T>(first.data)) return { outcome: "done", result: { ok: false, status: first.status, error: null } };
|
|
280
|
-
|
|
281
|
-
const objects = [...first.data.objects];
|
|
282
|
-
let next = first.data.next;
|
|
283
|
-
const snapshot = first.data.snapshot;
|
|
284
|
-
let sweptAt = first.data.sweptAt;
|
|
285
|
-
let etag = first.etag;
|
|
286
|
-
let status = first.status;
|
|
287
|
-
const seen = new Set<string>();
|
|
288
|
-
while (next !== null) {
|
|
289
|
-
if (seen.has(next)) return { outcome: "done", result: { ok: false, status: 400, error: null } };
|
|
290
|
-
seen.add(next);
|
|
291
|
-
const page = await readPage<T>(env, entry, objectType, pageOptions(options, next));
|
|
292
|
-
if (!page.ok) return { outcome: "done", result: page };
|
|
293
|
-
if (!isPage<T>(page.data)) return { outcome: "done", result: { ok: false, status: page.status, error: null } };
|
|
294
|
-
if (page.data.snapshot !== snapshot) return { outcome: "moved" };
|
|
295
|
-
objects.push(...page.data.objects);
|
|
296
|
-
next = page.data.next;
|
|
297
|
-
sweptAt = page.data.sweptAt;
|
|
298
|
-
etag = page.etag;
|
|
299
|
-
status = page.status;
|
|
300
|
-
}
|
|
301
|
-
return {
|
|
302
|
-
outcome: "done",
|
|
303
|
-
result: { ok: true, status, etag, data: { objects, next: null, sweptAt, snapshot } },
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function pageOptions(options: ReadDatasetPageOptions, next: string | undefined): ReadDatasetPageOptions {
|
|
308
|
-
const rest: ReadDatasetPageOptions = { ...options, follow: false };
|
|
309
|
-
if (next === undefined) {
|
|
310
|
-
delete rest.next;
|
|
311
|
-
return rest;
|
|
312
|
-
}
|
|
313
|
-
return { ...rest, next };
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/** One page of a walk. A walk never sends `ifNoneMatch`, so it is never `304`. */
|
|
317
|
-
async function readPage<T extends Record<string, unknown>>(
|
|
318
|
-
env: CallServiceEnv,
|
|
319
|
-
entry: string,
|
|
320
|
-
objectType: string,
|
|
321
|
-
options: ReadDatasetPageOptions,
|
|
322
|
-
): Promise<DatasetResult<DatasetPage<T> | DatasetObject<T>>> {
|
|
323
|
-
const result = await readOnce<T>(env, entry, objectType, options);
|
|
324
|
-
if (result.ok && result.notModified) return { ok: false, status: result.status, error: null };
|
|
325
|
-
return result;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
async function readOnce<T extends Record<string, unknown>>(
|
|
329
|
-
env: CallServiceEnv,
|
|
330
|
-
entry: string,
|
|
331
|
-
objectType: string,
|
|
332
|
-
options: AnyReadOptions,
|
|
333
|
-
): Promise<DatasetConditionalResult<DatasetPage<T> | DatasetObject<T>>> {
|
|
334
|
-
const response = await load(env, entry, objectType, options);
|
|
335
|
-
const error = response.headers.get(PLATFORM_ERROR_HEADER);
|
|
336
|
-
if (error !== null) return { ok: false, status: response.status, error };
|
|
337
|
-
const etag = response.headers.get("etag");
|
|
338
|
-
// Only a conditional read can be answered 304. Without `ifNoneMatch` a 304
|
|
339
|
-
// is not something the platform sends, and stays a failure below.
|
|
340
|
-
if (response.status === 304 && sentIfNoneMatch(options)) {
|
|
341
|
-
return { ok: true, status: 304, etag, notModified: true };
|
|
342
|
-
}
|
|
343
|
-
if (!response.ok) return { ok: false, status: response.status, error: null };
|
|
344
|
-
|
|
345
|
-
const body = (await response.json().catch(() => null)) as unknown;
|
|
346
|
-
if (isObjectRequest(options)) {
|
|
347
|
-
const data = asObject<T>(body);
|
|
348
|
-
if (!data) return { ok: false, status: response.status, error: null };
|
|
349
|
-
return { ok: true, status: response.status, etag, data };
|
|
350
|
-
}
|
|
351
|
-
const data = asPage<T>(body);
|
|
352
|
-
if (!data) return { ok: false, status: response.status, error: null };
|
|
353
|
-
return { ok: true, status: response.status, etag, data };
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
function load(
|
|
357
|
-
env: CallServiceEnv,
|
|
358
|
-
entry: string,
|
|
359
|
-
objectType: string,
|
|
360
|
-
options: AnyReadOptions,
|
|
361
|
-
): Promise<Response> {
|
|
362
|
-
const search = datasetSearch(options);
|
|
363
|
-
const ifNoneMatch = sentIfNoneMatch(options) ? options.ifNoneMatch : undefined;
|
|
364
|
-
// `import.meta.env.DEV` is false in the production build, so this branch
|
|
365
|
-
// and `dev-read-dataset.ts` are absent from the deployed Worker. Vitest
|
|
366
|
-
// keeps the injected fetch even when a Persona is set.
|
|
367
|
-
if (import.meta.env.DEV && env.DEV_PERSONA !== "none" && !underVitest()) {
|
|
368
|
-
return Promise.resolve(answerOfflineRead(env, entry, objectType, {
|
|
369
|
-
id: "id" in options ? options.id : undefined,
|
|
370
|
-
search,
|
|
371
|
-
userToken: options.userToken,
|
|
372
|
-
ifNoneMatch,
|
|
373
|
-
}));
|
|
374
|
-
}
|
|
375
|
-
const id = "id" in options ? options.id : undefined;
|
|
376
|
-
const init: RequestInit = { method: "GET" };
|
|
377
|
-
if (ifNoneMatch !== undefined) init.headers = { "if-none-match": ifNoneMatch };
|
|
378
|
-
return callService(env, entry, datasetPath(objectType, id, search), init, {
|
|
379
|
-
userToken: options.userToken,
|
|
380
|
-
fetchImpl: options.fetchImpl,
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
function datasetPath(
|
|
385
|
-
objectType: string,
|
|
386
|
-
id: string | undefined,
|
|
387
|
-
search: readonly (readonly [string, string])[],
|
|
388
|
-
): string {
|
|
389
|
-
const head = id !== undefined
|
|
390
|
-
? `${encodeURIComponent(objectType)}/${encodeURIComponent(id)}`
|
|
391
|
-
: encodeURIComponent(objectType);
|
|
392
|
-
if (search.length === 0) return head;
|
|
393
|
-
const params = new URLSearchParams();
|
|
394
|
-
for (const [key, value] of search) params.append(key, value);
|
|
395
|
-
return `${head}?${params.toString()}`;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function datasetSearch(
|
|
399
|
-
options: AnyReadOptions,
|
|
400
|
-
): [string, string][] {
|
|
401
|
-
const entries: [string, string][] = [];
|
|
402
|
-
const filter = "filter" in options ? options.filter : undefined;
|
|
403
|
-
if (filter) {
|
|
404
|
-
for (const [field, value] of Object.entries(filter)) {
|
|
405
|
-
if (Array.isArray(value)) {
|
|
406
|
-
for (const item of value) entries.push([field, String(item)]);
|
|
407
|
-
continue;
|
|
408
|
-
}
|
|
409
|
-
if (isRange(value)) {
|
|
410
|
-
for (const op of RANGE_OPS) {
|
|
411
|
-
const bound = value[op];
|
|
412
|
-
if (bound !== undefined) entries.push([`${field}[${op}]`, String(bound)]);
|
|
413
|
-
}
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
entries.push([field, String(value)]);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
if (options.sort !== undefined) entries.push(["sort", options.sort]);
|
|
420
|
-
if (options.limit !== undefined) entries.push(["limit", String(options.limit)]);
|
|
421
|
-
if (options.next !== undefined) entries.push(["next", options.next]);
|
|
422
|
-
return entries;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/** True when the caller gave a non-empty `ifNoneMatch`. An empty string sends no header. */
|
|
426
|
-
function sentIfNoneMatch(options: AnyReadOptions): options is AnyReadOptions & { ifNoneMatch: string } {
|
|
427
|
-
return typeof options.ifNoneMatch === "string" && options.ifNoneMatch !== "";
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
function isRange(value: DatasetFilterValue): value is DatasetRange {
|
|
431
|
-
if (typeof value !== "object") return false;
|
|
432
|
-
const keys = Object.keys(value);
|
|
433
|
-
return keys.length > 0 && keys.every((key) => key === "gt" || key === "gte" || key === "lt" || key === "lte");
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
function isPageRequest(
|
|
437
|
-
options: AnyReadOptions,
|
|
438
|
-
): options is ReadDatasetPageOptions {
|
|
439
|
-
return !("id" in options && options.id !== undefined) && options.shape !== "singleton";
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
function isObjectRequest(
|
|
443
|
-
options: AnyReadOptions,
|
|
444
|
-
): boolean {
|
|
445
|
-
return ("id" in options && options.id !== undefined) || options.shape === "singleton";
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
function isPage<T>(data: DatasetPage<T> | DatasetObject<T>): data is DatasetPage<T> {
|
|
449
|
-
return "objects" in data;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
function asPage<T extends Record<string, unknown>>(body: unknown): DatasetPage<T> | null {
|
|
453
|
-
if (!isRecord(body) || !Array.isArray(body.objects)) return null;
|
|
454
|
-
if (!body.objects.every((item) => isRecord(item))) return null;
|
|
455
|
-
if (!(body.next === null || typeof body.next === "string")) return null;
|
|
456
|
-
const sweptAt = asSweptAt(body.sweptAt);
|
|
457
|
-
const snapshot = asSnapshot(body.snapshot);
|
|
458
|
-
if (sweptAt === undefined || snapshot === undefined) return null;
|
|
459
|
-
return { objects: body.objects as T[], next: body.next, sweptAt, snapshot };
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function asObject<T extends Record<string, unknown>>(body: unknown): DatasetObject<T> | null {
|
|
463
|
-
if (!isRecord(body)) return null;
|
|
464
|
-
if (!(body.object === null || isRecord(body.object))) return null;
|
|
465
|
-
const sweptAt = asSweptAt(body.sweptAt);
|
|
466
|
-
const snapshot = asSnapshot(body.snapshot);
|
|
467
|
-
if (sweptAt === undefined || snapshot === undefined) return null;
|
|
468
|
-
return { object: body.object as T | null, sweptAt, snapshot };
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
function asSweptAt(value: unknown): string | null | undefined {
|
|
472
|
-
if (value === null) return null;
|
|
473
|
-
if (typeof value === "string") return value;
|
|
474
|
-
return undefined;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function asSnapshot(value: unknown): number | null | undefined {
|
|
478
|
-
if (value === null) return null;
|
|
479
|
-
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
480
|
-
return undefined;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
484
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
function underVitest(): boolean {
|
|
488
|
-
const flag = import.meta.env.VITEST;
|
|
489
|
-
return import.meta.env.MODE === "test" || flag === true || flag === "true";
|
|
490
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
// A Builder Service or Dataset (ADR-0024, ADR-0027). The platform calls
|
|
2
|
-
// `registry[name]` on the Worker default export at `/.pple/registry/<name>/…`.
|
|
3
|
-
// `serviceCall` reads who is calling from the headers the platform set. It
|
|
4
|
-
// returns null for any other request, including public traffic: the wrapper
|
|
5
|
-
// strips `x-pple-*` before app code sees it. Under `npm run dev` the template
|
|
6
|
-
// emulates that path and those headers; that emulation is compiled out of
|
|
7
|
-
// production.
|
|
8
|
-
//
|
|
9
|
-
// The Identity is one header, `x-pple-identity`: base64url (RFC 4648 §5, no
|
|
10
|
-
// padding) of the UTF-8 JSON `{ actor, effective }`. A Thai name is not a
|
|
11
|
-
// reliable raw header value. The header is absent when the entry declares
|
|
12
|
-
// `userToken: none`, and then `identity` is null. The user's token is never
|
|
13
|
-
// one of these headers.
|
|
14
|
-
|
|
15
|
-
import { IdentitySchema, type Identity } from "../shared/api";
|
|
16
|
-
import type { Env } from "./env";
|
|
17
|
-
import type { NotificationResponseHandler } from "./notification-response";
|
|
18
|
-
import type { PaymentHandler } from "./payment";
|
|
19
|
-
|
|
20
|
-
export const CONSUMER_HEADER = "x-pple-consumer";
|
|
21
|
-
export const ENTRY_HEADER = "x-pple-entry";
|
|
22
|
-
export const IDENTITY_HEADER = "x-pple-identity";
|
|
23
|
-
|
|
24
|
-
export interface ServiceCall {
|
|
25
|
-
/** The Consumer, named from its App Key. */
|
|
26
|
-
consumer: string;
|
|
27
|
-
/** The entry's full name (`sms`, `election/2027/score`), from `x-pple-entry`. */
|
|
28
|
-
entry: string;
|
|
29
|
-
/**
|
|
30
|
-
* Effective (including `sub`), Actor, and `effective.isDelegated`. Null
|
|
31
|
-
* when the entry declares `userToken: none`: nothing about the user was
|
|
32
|
-
* passed.
|
|
33
|
-
*/
|
|
34
|
-
identity: Identity | null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Fetch-shaped, so a Hono app (which has `.fetch`) plugs straight in. */
|
|
38
|
-
export type RegistryFetch = (
|
|
39
|
-
request: Request,
|
|
40
|
-
env: Env,
|
|
41
|
-
ctx: ExecutionContext,
|
|
42
|
-
) => Response | Promise<Response>;
|
|
43
|
-
|
|
44
|
-
export type RegistryHandler = RegistryFetch | { fetch: RegistryFetch };
|
|
45
|
-
|
|
46
|
-
/** What a Dataset page function is called with. `cursor` is null on the first page. */
|
|
47
|
-
export interface PageInput {
|
|
48
|
-
cursor: string | null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* One page of a collection. `next` is the Provider's own cursor, or null on
|
|
53
|
-
* the last page. `subs` is the swept audience, and only on the last page.
|
|
54
|
-
*/
|
|
55
|
-
export interface CollectionPage<T = unknown> {
|
|
56
|
-
objects: T[];
|
|
57
|
-
next: string | null;
|
|
58
|
-
subs?: string[];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** A singleton. `object` is null when the Dataset currently holds none. `subs` sits beside it. */
|
|
62
|
-
export interface SingletonPage<T = unknown> {
|
|
63
|
-
object: T | null;
|
|
64
|
-
subs?: string[];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export type CollectionPageFunction<T = unknown> = (
|
|
68
|
-
input: PageInput,
|
|
69
|
-
env: Env,
|
|
70
|
-
ctx: ExecutionContext,
|
|
71
|
-
) => CollectionPage<T> | Promise<CollectionPage<T>>;
|
|
72
|
-
|
|
73
|
-
export type SingletonPageFunction<T = unknown> = (
|
|
74
|
-
input: PageInput,
|
|
75
|
-
env: Env,
|
|
76
|
-
ctx: ExecutionContext,
|
|
77
|
-
) => SingletonPage<T> | Promise<SingletonPage<T>>;
|
|
78
|
-
|
|
79
|
-
/** One Object Type's page function. The declared shape says which return to write. */
|
|
80
|
-
export type ObjectTypePageFunction<T = unknown> = CollectionPageFunction<T> | SingletonPageFunction<T>;
|
|
81
|
-
|
|
82
|
-
/** Page functions for one Dataset, keyed by Object Type name. */
|
|
83
|
-
export type DatasetPages = Record<string, ObjectTypePageFunction>;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Service handlers and Dataset page functions, keyed by the entry name.
|
|
87
|
-
* A Service value is fetch-shaped. A Dataset value is {@link DatasetPages}.
|
|
88
|
-
*/
|
|
89
|
-
export type RegistryMap = Record<string, RegistryHandler | DatasetPages>;
|
|
90
|
-
|
|
91
|
-
export interface PpleWorker {
|
|
92
|
-
fetch(request: Request, env: Env, ctx: ExecutionContext): Response | Promise<Response>;
|
|
93
|
-
registry?: RegistryMap;
|
|
94
|
-
/** Answers to your Interactive Notifications. See `./notification-response`. */
|
|
95
|
-
notificationResponse?: NotificationResponseHandler;
|
|
96
|
-
/** What happened to a Payment you created. See `./payment`. */
|
|
97
|
-
payment?: PaymentHandler;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function decodeIdentity(value: string): Identity | null {
|
|
101
|
-
try {
|
|
102
|
-
const pad = value.length % 4 === 0 ? "" : "=".repeat(4 - (value.length % 4));
|
|
103
|
-
const binary = atob(value.replace(/-/g, "+").replace(/_/g, "/") + pad);
|
|
104
|
-
const bytes = new Uint8Array(binary.length);
|
|
105
|
-
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
106
|
-
const parsed: unknown = JSON.parse(new TextDecoder().decode(bytes));
|
|
107
|
-
const identity = IdentitySchema.safeParse(parsed);
|
|
108
|
-
return identity.success ? identity.data : null;
|
|
109
|
-
} catch {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Who is calling this Service, or null when the request is not a platform
|
|
116
|
-
* registry call. Never includes the user's token.
|
|
117
|
-
*/
|
|
118
|
-
export function serviceCall(request: Request): ServiceCall | null {
|
|
119
|
-
const consumer = (request.headers.get(CONSUMER_HEADER) ?? "").trim();
|
|
120
|
-
const entry = (request.headers.get(ENTRY_HEADER) ?? "").trim();
|
|
121
|
-
if (!consumer || !entry) return null;
|
|
122
|
-
const encoded = request.headers.get(IDENTITY_HEADER);
|
|
123
|
-
return {
|
|
124
|
-
consumer,
|
|
125
|
-
entry,
|
|
126
|
-
identity: encoded ? decodeIdentity(encoded) : null,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
// The Worker is built by Vite (via @cloudflare/vite-plugin), so `import.meta.env`
|
|
2
|
-
// is available here just as it is in the browser bundle. Vite replaces
|
|
3
|
-
// `import.meta.env.DEV` with a literal: `true` under `vite dev` and `vitest`,
|
|
4
|
-
// `false` in `vite build` — which is what lets the local auth bypass in
|
|
5
|
-
// src/auth.ts be dead-code-eliminated from the deployed Worker.
|
|
6
|
-
interface ImportMetaEnv {
|
|
7
|
-
readonly DEV: boolean;
|
|
8
|
-
readonly PROD: boolean;
|
|
9
|
-
readonly MODE: string;
|
|
10
|
-
/** Set by vitest. Absent from `vite dev` and the production build. */
|
|
11
|
-
readonly VITEST?: boolean | "true";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface ImportMeta {
|
|
15
|
-
readonly env: ImportMetaEnv;
|
|
16
|
-
/**
|
|
17
|
-
* Vite loads the matched files at build time. The offline overlay glob
|
|
18
|
-
* keeps `.pple/registry` in the fixed base and sits in an
|
|
19
|
-
* `import.meta.env.DEV` branch, so the production Worker does not contain it.
|
|
20
|
-
* `?mtime` is the overlay file's write time (`overlay-mtime.mjs`), used as
|
|
21
|
-
* `sweptAt` by offline `readDataset`.
|
|
22
|
-
*/
|
|
23
|
-
glob(
|
|
24
|
-
pattern: string,
|
|
25
|
-
options: { eager: true; query: string; import: string },
|
|
26
|
-
): Record<string, unknown>;
|
|
27
|
-
}
|