@pplethai/cli 0.9.0 → 0.11.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/dist/bin.js +128 -125
- package/package.json +1 -1
- package/templates/golden-path/.dev.vars.example +4 -3
- package/templates/golden-path/CLAUDE.md +13 -6
- package/templates/golden-path/README.md +5 -4
- package/templates/golden-path/src/call-service.ts +17 -10
- package/templates/golden-path/src/dev-call-service.ts +21 -20
- package/templates/golden-path/src/dev-read-dataset.ts +55 -40
- package/templates/golden-path/src/dev-registry.ts +25 -7
- package/templates/golden-path/src/entry-name.ts +114 -0
- package/templates/golden-path/src/env.ts +2 -2
- package/templates/golden-path/src/read-dataset.ts +130 -16
- package/templates/golden-path/src/registry.ts +1 -1
- package/templates/golden-path/test/call-service.test.ts +118 -8
- package/templates/golden-path/test/dev-call-service.test.ts +201 -25
- package/templates/golden-path/test/dev-read-dataset.test.ts +122 -13
- package/templates/golden-path/test/dev-registry.test.ts +35 -6
- package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +8 -0
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +11 -0
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +7 -0
- package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +24 -0
- package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +1 -0
- package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +10 -0
- package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +7 -0
- package/templates/golden-path/test/fixtures/overlay/score/entry.md +10 -0
- package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +7 -0
- package/templates/golden-path/test/harness.ts +1 -1
- package/templates/golden-path/test/read-dataset.test.ts +187 -14
- package/templates/golden-path/test/fixtures/overlay/shop/catalog/entry.md +0 -8
- /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/entry.md +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/audience.json +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/named.json +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/offices.json +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/roster.json +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/summary.json +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{shop/catalog → catalog}/mocks/responses.json +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{shop/orders → orders}/entry.md +0 -0
- /package/templates/golden-path/test/fixtures/overlay/{shop/orders → orders}/mocks/responses.json +0 -0
package/package.json
CHANGED
|
@@ -14,16 +14,17 @@
|
|
|
14
14
|
# DEV_PERSONA="province-coordinator"
|
|
15
15
|
|
|
16
16
|
# Make offline callService return a platform refusal instead of a mock.
|
|
17
|
-
# A code applies to every entry.
|
|
17
|
+
# A code applies to every entry. name=code applies to one entry
|
|
18
18
|
# and wins over the global code. Separate several with commas.
|
|
19
19
|
# invalid_app_key, not_listed, not_granted, needs_beta, user_token_invalid,
|
|
20
20
|
# identity_unavailable, provider_unavailable, provider_timeout.
|
|
21
21
|
# An unknown code is ignored.
|
|
22
22
|
# DEV_REGISTRY_ERROR="needs_beta"
|
|
23
|
-
# DEV_REGISTRY_ERROR="not_granted,
|
|
23
|
+
# DEV_REGISTRY_ERROR="not_granted,orders=provider_timeout"
|
|
24
24
|
|
|
25
25
|
# Consumer slug reported on the emulated registry path. Defaults to
|
|
26
|
-
# "dev-consumer".
|
|
26
|
+
# "dev-consumer". `x-pple-entry` is the bare name;
|
|
27
|
+
# the dev Provider is dev-provider.
|
|
27
28
|
# DEV_CONSUMER="brave-purple-lion"
|
|
28
29
|
|
|
29
30
|
# Exercise the notification helper locally. These are normally injected by the
|
|
@@ -56,17 +56,22 @@ Where the token comes from:
|
|
|
56
56
|
- A Service handler lives on `registry` in `src/index.ts`. Locally, curl
|
|
57
57
|
`/.pple/registry/<name>/…`. The dev wrapper sets `x-pple-consumer`
|
|
58
58
|
(`DEV_CONSUMER`, default `dev-consumer`), `x-pple-entry`
|
|
59
|
-
(`dev-provider
|
|
59
|
+
(the bare `<name>`; the dev Provider is `dev-provider`), and `x-pple-identity` from the active Persona.
|
|
60
60
|
`DEV_PERSONA=none` or an unknown persona name omits the Identity (as
|
|
61
61
|
`userToken: none` does) and does not fall back to `hq-staff` on this path.
|
|
62
62
|
Read the caller with `serviceCall(request)`. Tests call `callRegistry` from
|
|
63
63
|
`test/harness.ts`. An entry with no handler throws `registry handler missing`.
|
|
64
64
|
The wrapper is compiled out of the deployed Worker.
|
|
65
|
-
- `callService`
|
|
66
|
-
|
|
65
|
+
- `callService` takes the entry's full name: a bare name such as `branch-list`,
|
|
66
|
+
a Namespace (กลุ่มชื่อ) name such as `election/2027/score`, or one of the
|
|
67
|
+
platform's own entries, which all sit under `platform`: `platform/sms`,
|
|
68
|
+
`platform/legal-identity`. A bare `sms` names nothing, and `platform` on its
|
|
69
|
+
own is refused. Locally it answers from
|
|
70
|
+
`.pple/registry/<full name>/mocks/responses.json` (nested folders for a
|
|
71
|
+
Namespace) as the active persona (`x-pple-mock` names the entry). A `userToken: required`
|
|
67
72
|
entry called with no token is `user_token_invalid`. Nothing matched is
|
|
68
73
|
`no_mock` (404), dev-only, with a Thai message that names `pple registry`.
|
|
69
|
-
`DEV_REGISTRY_ERROR` (`<code>` or `<
|
|
74
|
+
`DEV_REGISTRY_ERROR` (`<code>` or `<full name>=<code>`, commas) returns
|
|
70
75
|
that platform refusal instead. A persona added in `dev-personas.ts` only
|
|
71
76
|
hits cases that name no persona. `DEV_PERSONA=none` calls the real service.
|
|
72
77
|
Vitest uses the injected fetch and does not read the overlay. The overlay
|
|
@@ -77,10 +82,12 @@ Where the token comes from:
|
|
|
77
82
|
A collection page is `{ objects, next, sweptAt, snapshot }`; a singleton or
|
|
78
83
|
an id lookup is `{ object, sweptAt, snapshot }`. Pass `filter`, `sort` and
|
|
79
84
|
`limit`; pass `id` to look one object up; pass `follow: true` to walk
|
|
80
|
-
`next`.
|
|
85
|
+
`next`. Pass `ifNoneMatch: <etag>` on a page, singleton or lookup to poll:
|
|
86
|
+
a `304` is `{ ok: true, status: 304, etag, notModified: true }` with no `data`
|
|
87
|
+
(check `notModified` after `ok`); `follow: true` refuses it. The user token is `options.userToken` and is sent only when set.
|
|
81
88
|
Platform refusals are `x-pple-error` (`not_found`, `invalid_query`,
|
|
82
89
|
`level_not_met`, and the same codes as `callService`). Locally it reads
|
|
83
|
-
`.pple/registry/<
|
|
90
|
+
`.pple/registry/<full name>/mocks/<objectType>.json` through the same
|
|
84
91
|
query rules and `passesLevel` against the active Persona. `sweptAt` is the
|
|
85
92
|
mock file's write time. A level whose `subs` list is missing, including
|
|
86
93
|
`subs: swept`, admits nobody offline. `DEV_REGISTRY_ERROR` and `no_mock`
|
|
@@ -38,14 +38,15 @@ someone else, set `DEV_PERSONA` in `.dev.vars` (see `.dev.vars.example`) to
|
|
|
38
38
|
or add your own in `src/dev-personas.ts`.
|
|
39
39
|
|
|
40
40
|
`callService` on `npm run dev` answers from
|
|
41
|
-
`.pple/registry/<
|
|
41
|
+
`.pple/registry/<full name>/mocks/responses.json` (nested for a Namespace such as `election/2027/score`) as that persona. A
|
|
42
42
|
missing entry or route is `no_mock` (404) and names `pple registry`. Set
|
|
43
43
|
`DEV_REGISTRY_ERROR` to rehearse a platform refusal such as `not_granted` or
|
|
44
|
-
`provider_timeout
|
|
44
|
+
`provider_timeout`: `<code>` for every entry, or `<full name>=<code>` for one
|
|
45
|
+
(`orders=provider_timeout`). `DEV_PERSONA=none` calls the real service. This path is
|
|
45
46
|
compiled out of the deployed Worker.
|
|
46
47
|
|
|
47
48
|
`readDataset` on `npm run dev` reads
|
|
48
|
-
`.pple/registry/<
|
|
49
|
+
`.pple/registry/<full name>/mocks/<objectType>.json` the same way:
|
|
49
50
|
filters, sort and pages run locally, and the active persona has to pass the
|
|
50
51
|
Object Type's level. `sweptAt` is the time that mock file was written. A
|
|
51
52
|
level that only names people, or a swept list, admits nobody offline — those
|
|
@@ -56,7 +57,7 @@ compiled out of the deployed Worker.
|
|
|
56
57
|
To exercise a Service this app provides, curl
|
|
57
58
|
`/.pple/registry/<name>/…` on the dev server. That path is delivered to
|
|
58
59
|
`registry[name]` with the active Persona's Identity and a fake Consumer
|
|
59
|
-
(`dev-consumer`, or `DEV_CONSUMER`).
|
|
60
|
+
(`dev-consumer`, or `DEV_CONSUMER`). `x-pple-entry` is the bare name; the dev Provider is `dev-provider`.
|
|
60
61
|
`DEV_PERSONA=none`, or a persona name that is not defined, sends no
|
|
61
62
|
Identity — the same as an entry that declares `userToken: none`. An unknown
|
|
62
63
|
name does not fall back to `hq-staff` on this path. The wrapper is compiled
|
|
@@ -11,13 +11,18 @@
|
|
|
11
11
|
// CI does not have.
|
|
12
12
|
|
|
13
13
|
import { answerOfflineCall } from "./dev-call-service";
|
|
14
|
+
import { assertEntryName } from "./entry-name";
|
|
14
15
|
|
|
15
16
|
export const PLATFORM_ERROR_HEADER = "x-pple-error";
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Platform refusals, in the order a caller meets them.
|
|
19
|
-
* The first eight are ADR-0024 (a Service call). The
|
|
20
|
-
* (a Dataset read) and are not part of the Service check order.
|
|
20
|
+
* The first eight are ADR-0024 (a Service call). The next four are ADR-0030
|
|
21
|
+
* (a Dataset read) and are not part of the Service check order. `renamed` is
|
|
22
|
+
* ADR-0032: an admin renamed the entry's Namespace, so the old name is not
|
|
23
|
+
* served at all. It is a 410 for a Service call and a Dataset read alike, and
|
|
24
|
+
* the body is `{ error, renamed, renamedTo, message }` with the new name in
|
|
25
|
+
* `renamedTo`. It is answered before any grant or token is looked at.
|
|
21
26
|
*/
|
|
22
27
|
export const PLATFORM_ERROR_CODES = [
|
|
23
28
|
"invalid_app_key",
|
|
@@ -32,6 +37,7 @@ export const PLATFORM_ERROR_CODES = [
|
|
|
32
37
|
"not_found",
|
|
33
38
|
"invalid_query",
|
|
34
39
|
"level_not_met",
|
|
40
|
+
"renamed",
|
|
35
41
|
] as const;
|
|
36
42
|
|
|
37
43
|
export type PlatformErrorCode = (typeof PLATFORM_ERROR_CODES)[number];
|
|
@@ -46,8 +52,8 @@ export interface CallServiceEnv {
|
|
|
46
52
|
/** `"none"` uses the real service. Anything else, under `npm run dev`, uses the overlay. */
|
|
47
53
|
DEV_PERSONA?: string;
|
|
48
54
|
/**
|
|
49
|
-
* Offline only. `<code>` for every entry, or `<
|
|
50
|
-
* comma-separated. A per-entry code wins. Unknown codes are ignored.
|
|
55
|
+
* Offline only. `<code>` for every entry, or `<full name>=<code>`
|
|
56
|
+
* (`election/2027/score=503`), comma-separated. A per-entry code wins. Unknown codes are ignored.
|
|
51
57
|
*/
|
|
52
58
|
DEV_REGISTRY_ERROR?: string;
|
|
53
59
|
}
|
|
@@ -63,7 +69,10 @@ export interface CallServiceOptions {
|
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
/**
|
|
66
|
-
* `GET`/`POST`/… `services.<domain>/<
|
|
72
|
+
* `GET`/`POST`/… `services.<domain>/<full name>/<path>`. `entry` is the full
|
|
73
|
+
* name: a bare name such as `"branch-list"`, a Namespace and a path such as
|
|
74
|
+
* `"election/2027/score"`, or a platform entry such as `"platform/sms"`.
|
|
75
|
+
* Anything the shared validator refuses throws, the bare `"platform"` included.
|
|
67
76
|
*
|
|
68
77
|
* `path` `""` is the entry root. A path without a leading `/` or `?` gains
|
|
69
78
|
* one `/`. The App Key is always `Authorization: Bearer`. A `userToken` is
|
|
@@ -81,9 +90,7 @@ export async function callService(
|
|
|
81
90
|
init: RequestInit = {},
|
|
82
91
|
options: CallServiceOptions = {},
|
|
83
92
|
): Promise<Response> {
|
|
84
|
-
|
|
85
|
-
throw new Error('callService entry must be "<provider>/<name>"');
|
|
86
|
-
}
|
|
93
|
+
assertEntryName("callService", entry);
|
|
87
94
|
|
|
88
95
|
// `import.meta.env.DEV` is false in the production build, so this branch
|
|
89
96
|
// and `dev-call-service.ts` are absent from the deployed Worker. Vitest
|
|
@@ -108,8 +115,8 @@ export async function callService(
|
|
|
108
115
|
/**
|
|
109
116
|
* The platform refusal code on `response`, read from `x-pple-error`.
|
|
110
117
|
*
|
|
111
|
-
* Null when the header is absent or names a code outside ADR-0024
|
|
112
|
-
* ADR-
|
|
118
|
+
* Null when the header is absent or names a code outside ADR-0024, ADR-0030
|
|
119
|
+
* and ADR-0032. A null from an absent header means the Provider answered.
|
|
113
120
|
* `no_mock` is dev-only and stays null here. Callers that must not treat an
|
|
114
121
|
* unknown code as a Provider response check the header itself.
|
|
115
122
|
*/
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
// branch, so `vite build` drops it the same way it drops the Persona bypass
|
|
5
5
|
// (ADR-0011).
|
|
6
6
|
//
|
|
7
|
-
// The overlay is `.pple/registry/<
|
|
7
|
+
// The overlay is `.pple/registry/<full name>/`, written by the CLI (ADR-0031,
|
|
8
|
+
// ADR-0032: an entry is named by its full name, so `election/2027/score` is
|
|
9
|
+
// nested folders; the Provider is not part of the path).
|
|
8
10
|
// Vite's `import.meta.glob` skips a dot-folder unless the dot sits in the
|
|
9
11
|
// pattern's fixed base, so the glob below keeps `.pple/registry` in that
|
|
10
12
|
// base. CI has no overlay; tests pass a fixture glob to `answerFromOverlay`
|
|
@@ -16,6 +18,7 @@
|
|
|
16
18
|
// `callService`.
|
|
17
19
|
|
|
18
20
|
import type { PlatformErrorCode } from "./call-service";
|
|
21
|
+
import { isEntryName, entryNameError, overlayEntryFile } from "./entry-name";
|
|
19
22
|
import { DEV_PERSONAS, DEFAULT_DEV_PERSONA, resolveDevPersona } from "./dev-personas";
|
|
20
23
|
|
|
21
24
|
/** Statuses for every platform code. Keyed by the union, so a new code fails typecheck here. */
|
|
@@ -32,6 +35,7 @@ const PLATFORM_ERROR_STATUS: Record<PlatformErrorCode, number> = {
|
|
|
32
35
|
not_found: 404,
|
|
33
36
|
invalid_query: 400,
|
|
34
37
|
level_not_met: 403,
|
|
38
|
+
renamed: 410,
|
|
35
39
|
};
|
|
36
40
|
|
|
37
41
|
const MOCK_HEADER = "x-pple-mock";
|
|
@@ -58,6 +62,8 @@ export interface OverlayCall {
|
|
|
58
62
|
/** Active Persona name. A name the Builder added matches only unscoped cases. */
|
|
59
63
|
persona: string;
|
|
60
64
|
devRegistryError?: string;
|
|
65
|
+
/** Where the overlay starts in `files`' keys. Omitted: `.pple/registry/`. Tests with a fixture folder set it. */
|
|
66
|
+
overlayRoot?: string;
|
|
61
67
|
}
|
|
62
68
|
|
|
63
69
|
interface IndexedEntry {
|
|
@@ -101,7 +107,7 @@ export function answerFromOverlay(
|
|
|
101
107
|
const forced = forcedError(call.devRegistryError, call.entry);
|
|
102
108
|
if (forced) return platformRefusal(forced);
|
|
103
109
|
|
|
104
|
-
const entry = indexOverlay(files).get(call.entry);
|
|
110
|
+
const entry = indexOverlay(files, call.overlayRoot).get(call.entry);
|
|
105
111
|
if (!entry) return noMock(missingEntryMessage(call.entry));
|
|
106
112
|
|
|
107
113
|
if (entry.userToken === "required" && !hasToken(call.userToken)) {
|
|
@@ -152,27 +158,18 @@ function activePersonaName(configured: string | undefined): string {
|
|
|
152
158
|
return DEFAULT_DEV_PERSONA;
|
|
153
159
|
}
|
|
154
160
|
|
|
155
|
-
function indexOverlay(files: Readonly<Record<string, unknown
|
|
161
|
+
function indexOverlay(files: Readonly<Record<string, unknown>>, root?: string): Map<string, IndexedEntry> {
|
|
156
162
|
const found = new Map<string, { entryMd?: string; responses?: string }>();
|
|
157
163
|
for (const [file, value] of Object.entries(files)) {
|
|
158
164
|
const text = asText(value);
|
|
159
165
|
if (text === null) continue;
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
const responses = path.match(/(?:^|\/)([^/]+)\/([^/]+)\/mocks\/responses\.json$/);
|
|
170
|
-
if (responses?.[1] && responses[2]) {
|
|
171
|
-
const id = `${responses[1]}/${responses[2]}`;
|
|
172
|
-
const row = found.get(id) ?? {};
|
|
173
|
-
row.responses = text;
|
|
174
|
-
found.set(id, row);
|
|
175
|
-
}
|
|
166
|
+
const at = overlayEntryFile(file, root);
|
|
167
|
+
if (!at) continue;
|
|
168
|
+
const row = found.get(at.name) ?? {};
|
|
169
|
+
if (at.inside === "entry.md") row.entryMd = text;
|
|
170
|
+
else if (at.inside === "mocks/responses.json") row.responses = text;
|
|
171
|
+
else continue;
|
|
172
|
+
found.set(at.name, row);
|
|
176
173
|
}
|
|
177
174
|
|
|
178
175
|
const entries = new Map<string, IndexedEntry>();
|
|
@@ -226,7 +223,7 @@ function hasToken(userToken: string | undefined): boolean {
|
|
|
226
223
|
}
|
|
227
224
|
|
|
228
225
|
/**
|
|
229
|
-
* A per-entry `<
|
|
226
|
+
* A per-entry `<full name>=<code>` wins over a bare `<code>`. An
|
|
230
227
|
* unknown code is ignored, so a bad per-entry rule falls through to the
|
|
231
228
|
* global one. Checked before the missing-token refusal: `not_granted` and
|
|
232
229
|
* `needs_beta` are answers the platform gives before it looks at the token.
|
|
@@ -248,6 +245,10 @@ export function forcedError(spec: string | undefined, entry: string): PlatformEr
|
|
|
248
245
|
console.warn(`DEV_REGISTRY_ERROR ignores "${item}".`);
|
|
249
246
|
continue;
|
|
250
247
|
}
|
|
248
|
+
if (!isEntryName(id)) {
|
|
249
|
+
console.warn(`DEV_REGISTRY_ERROR ignores "${item}": ${entryNameError(id)}.`);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
251
252
|
rules.push({ entry: id, code });
|
|
252
253
|
}
|
|
253
254
|
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
// only from that branch, so `vite build` drops it the same way it drops the
|
|
5
5
|
// Persona bypass (ADR-0011).
|
|
6
6
|
//
|
|
7
|
-
// The overlay is `.pple/registry/<
|
|
7
|
+
// The overlay is `.pple/registry/<full name>/`, nested for a namespaced entry
|
|
8
|
+
// (`election/2027/score`, ADR-0032). Vite's `import.meta.glob`
|
|
8
9
|
// skips a dot-folder unless the dot sits in the pattern's fixed base, so both
|
|
9
10
|
// globs below keep `.pple/registry` in that base. CI has no overlay; tests
|
|
10
11
|
// pass a fixture glob to `answerOfflineDataset` and never read `.pple/`.
|
|
@@ -22,11 +23,16 @@
|
|
|
22
23
|
// overlay, and pages of one file must share a snapshot. A declared Object Type
|
|
23
24
|
// with no mock file is `no_mock`, not an empty page. Offline has no warehouse,
|
|
24
25
|
// and a missing mock is the dev-only miss (ADR-0026).
|
|
26
|
+
//
|
|
27
|
+
// ETags are `"<snapshot>-<query hash>"`, so they are stable already: both parts
|
|
28
|
+
// come from the mock text and the query, not from a clock. A matching
|
|
29
|
+
// `ifNoneMatch` answers `304`, after the refusals, as production does.
|
|
25
30
|
|
|
26
31
|
import { parse as parseYaml } from "yaml";
|
|
27
32
|
import { forcedError, platformRefusal } from "./dev-call-service";
|
|
28
33
|
import { resolveDevPersona } from "./dev-personas";
|
|
29
34
|
import { applyDatasetQuery, datasetOpaqueHash, datasetPageHash, parseDatasetQuery, type DatasetQuerySpec, type DatasetRow } from "./dataset-query";
|
|
35
|
+
import { overlayEntryFile, overlayRelative } from "./entry-name";
|
|
30
36
|
import { passesLevel, type AccessLevel } from "./levels";
|
|
31
37
|
import { isPlainObject } from "./dataset-schema";
|
|
32
38
|
|
|
@@ -40,9 +46,13 @@ export interface OfflineDatasetCall {
|
|
|
40
46
|
/** Query entries, in the order `readDataset` puts them on the URL. */
|
|
41
47
|
search?: readonly (readonly [string, string])[];
|
|
42
48
|
userToken?: string;
|
|
49
|
+
/** `If-None-Match` as the Builder sent it. A match with the ETag this read produces answers `304`. */
|
|
50
|
+
ifNoneMatch?: string;
|
|
43
51
|
/** Active Persona name. An unknown name warns and uses `hq-staff`. */
|
|
44
52
|
persona: string;
|
|
45
53
|
devRegistryError?: string;
|
|
54
|
+
/** Where the overlay starts in `files`' keys. Omitted: `.pple/registry/`. Tests with a fixture folder set it. */
|
|
55
|
+
overlayRoot?: string;
|
|
46
56
|
}
|
|
47
57
|
|
|
48
58
|
interface DeclaredType {
|
|
@@ -62,7 +72,7 @@ export function answerOfflineRead(
|
|
|
62
72
|
env: { DEV_PERSONA?: string; DEV_REGISTRY_ERROR?: string },
|
|
63
73
|
entry: string,
|
|
64
74
|
objectType: string,
|
|
65
|
-
request: { id?: string; search?: readonly (readonly [string, string])[]; userToken?: string },
|
|
75
|
+
request: { id?: string; search?: readonly (readonly [string, string])[]; userToken?: string; ifNoneMatch?: string },
|
|
66
76
|
): Response {
|
|
67
77
|
return answerOfflineDataset(overlayFiles(), overlayWrittenAt(), {
|
|
68
78
|
entry,
|
|
@@ -70,6 +80,7 @@ export function answerOfflineRead(
|
|
|
70
80
|
id: request.id,
|
|
71
81
|
search: request.search,
|
|
72
82
|
userToken: request.userToken,
|
|
83
|
+
ifNoneMatch: request.ifNoneMatch,
|
|
73
84
|
persona: env.DEV_PERSONA ?? "",
|
|
74
85
|
devRegistryError: env.DEV_REGISTRY_ERROR,
|
|
75
86
|
});
|
|
@@ -88,7 +99,7 @@ export function answerOfflineDataset(
|
|
|
88
99
|
const forced = forcedError(call.devRegistryError, call.entry);
|
|
89
100
|
if (forced) return platformRefusal(forced);
|
|
90
101
|
|
|
91
|
-
const indexed = indexOverlay(files);
|
|
102
|
+
const indexed = indexOverlay(files, call.overlayRoot);
|
|
92
103
|
const entry = indexed.get(call.entry);
|
|
93
104
|
if (!entry) return noMock(missingEntryMessage(call.entry));
|
|
94
105
|
|
|
@@ -110,7 +121,7 @@ export function answerOfflineDataset(
|
|
|
110
121
|
const mock = entry.mocks.get(call.objectType);
|
|
111
122
|
if (mock === undefined) return noMock(missingMockMessage(call.entry, call.objectType));
|
|
112
123
|
|
|
113
|
-
const sweptAt = writtenTime(writtenAt, call.entry, call.objectType);
|
|
124
|
+
const sweptAt = writtenTime(writtenAt, call.entry, call.objectType, call.overlayRoot);
|
|
114
125
|
const snapshot = snapshotOf(mock);
|
|
115
126
|
const search = call.search ?? [];
|
|
116
127
|
|
|
@@ -119,7 +130,7 @@ export function answerOfflineDataset(
|
|
|
119
130
|
if (search.length > 0) return platformRefusal("invalid_query");
|
|
120
131
|
const object = parseSingleton(call.entry, call.objectType, mock);
|
|
121
132
|
if (object === undefined) return noMock(unreadableMockMessage(call.entry, call.objectType));
|
|
122
|
-
return
|
|
133
|
+
return answer(call, { object, sweptAt, snapshot }, etag(snapshot, datasetOpaqueHash({ singleton: true })));
|
|
123
134
|
}
|
|
124
135
|
|
|
125
136
|
// A query on an id lookup is invalid even when the declaration cannot be
|
|
@@ -136,8 +147,8 @@ export function answerOfflineDataset(
|
|
|
136
147
|
const id = canonicalId(call.id, declared.spec.idType);
|
|
137
148
|
const found = id === null ? undefined : rows.find((row) => row.id === id);
|
|
138
149
|
if (!found) return platformRefusal("not_found");
|
|
139
|
-
return
|
|
140
|
-
call
|
|
150
|
+
return answer(
|
|
151
|
+
call,
|
|
141
152
|
{ object: found.object, sweptAt, snapshot },
|
|
142
153
|
etag(snapshot, datasetOpaqueHash({ lookup: id })),
|
|
143
154
|
);
|
|
@@ -148,8 +159,8 @@ export function answerOfflineDataset(
|
|
|
148
159
|
const parsed = parseDatasetQuery(params, declared.spec);
|
|
149
160
|
if (!parsed.ok) return platformRefusal("invalid_query");
|
|
150
161
|
const page = applyDatasetQuery(rows, parsed.query, declared.spec);
|
|
151
|
-
return
|
|
152
|
-
call
|
|
162
|
+
return answer(
|
|
163
|
+
call,
|
|
153
164
|
{ objects: page.objects, next: page.next, sweptAt, snapshot },
|
|
154
165
|
etag(snapshot, datasetPageHash(parsed.query)),
|
|
155
166
|
);
|
|
@@ -173,26 +184,17 @@ function overlayWrittenAt(): Record<string, unknown> {
|
|
|
173
184
|
});
|
|
174
185
|
}
|
|
175
186
|
|
|
176
|
-
function indexOverlay(files: Readonly<Record<string, unknown
|
|
187
|
+
function indexOverlay(files: Readonly<Record<string, unknown>>, root?: string): Map<string, IndexedEntry> {
|
|
177
188
|
const found = new Map<string, { entryMd?: string; mocks: Map<string, string> }>();
|
|
178
|
-
const touch = (id: string) => {
|
|
179
|
-
const row = found.get(id) ?? { mocks: new Map<string, string>() };
|
|
180
|
-
found.set(id, row);
|
|
181
|
-
return row;
|
|
182
|
-
};
|
|
183
189
|
for (const [file, value] of Object.entries(files)) {
|
|
184
190
|
const text = asText(value);
|
|
185
191
|
if (text === null) continue;
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const mock = path.match(/(?:^|\/)([^/]+)\/([^/]+)\/mocks\/([^/]+)\.json$/);
|
|
193
|
-
if (mock?.[1] && mock[2] && mock[3]) {
|
|
194
|
-
touch(`${mock[1]}/${mock[2]}`).mocks.set(mock[3], text);
|
|
195
|
-
}
|
|
192
|
+
const at = overlayEntryFile(file, root);
|
|
193
|
+
if (!at) continue;
|
|
194
|
+
const row = found.get(at.name) ?? { mocks: new Map<string, string>() };
|
|
195
|
+
found.set(at.name, row);
|
|
196
|
+
if (at.inside === "entry.md") row.entryMd = text;
|
|
197
|
+
else row.mocks.set(at.inside.slice("mocks/".length, -".json".length), text);
|
|
196
198
|
}
|
|
197
199
|
const entries = new Map<string, IndexedEntry>();
|
|
198
200
|
for (const [id, row] of found) {
|
|
@@ -347,25 +349,19 @@ function canonicalId(raw: string, idType: DatasetQuerySpec["idType"]): string |
|
|
|
347
349
|
return String(value);
|
|
348
350
|
}
|
|
349
351
|
|
|
350
|
-
function writtenTime(
|
|
352
|
+
function writtenTime(
|
|
353
|
+
writtenAt: Readonly<Record<string, unknown>>,
|
|
354
|
+
entry: string,
|
|
355
|
+
objectType: string,
|
|
356
|
+
root?: string,
|
|
357
|
+
): string | null {
|
|
351
358
|
const times = new Map<string, string>();
|
|
352
359
|
for (const [file, value] of Object.entries(writtenAt)) {
|
|
353
360
|
if (typeof value !== "string" || value === "") continue;
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
const mockSuffix = `${entry}/mocks/${objectType}.json`;
|
|
357
|
-
const entrySuffix = `${entry}/entry.md`;
|
|
358
|
-
for (const [path, iso] of times) {
|
|
359
|
-
if (pathEnds(path, mockSuffix)) return iso;
|
|
361
|
+
const relative = overlayRelative(file, root);
|
|
362
|
+
if (relative !== null) times.set(relative, value);
|
|
360
363
|
}
|
|
361
|
-
|
|
362
|
-
if (pathEnds(path, entrySuffix)) return iso;
|
|
363
|
-
}
|
|
364
|
-
return null;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
function pathEnds(path: string, suffix: string): boolean {
|
|
368
|
-
return path === suffix || path.endsWith(`/${suffix}`);
|
|
364
|
+
return times.get(`${entry}/mocks/${objectType}.json`) ?? times.get(`${entry}/entry.md`) ?? null;
|
|
369
365
|
}
|
|
370
366
|
|
|
371
367
|
/** Stable across pages of one file, and different after the mock text changes. */
|
|
@@ -406,6 +402,25 @@ function unreadableMockMessage(entry: string, objectType: string): string {
|
|
|
406
402
|
return `อ่าน .pple/registry/${entry}/mocks/${objectType}.json ไม่ได้ — รัน pple registry เพื่อรีเฟรช`;
|
|
407
403
|
}
|
|
408
404
|
|
|
405
|
+
/**
|
|
406
|
+
* The 200, or the `304` when the Builder's `If-None-Match` names this ETag.
|
|
407
|
+
* It comes after every refusal, as in production (`dataset-read.ts`): a bad
|
|
408
|
+
* query or an unknown id is refused even when the ETag is current. The match
|
|
409
|
+
* rule is the platform's: a comma list, `W/` and the quotes ignored.
|
|
410
|
+
*/
|
|
411
|
+
function answer(call: OfflineDatasetCall, body: unknown, tag: string): Response {
|
|
412
|
+
if (noneMatch(call.ifNoneMatch, tag)) {
|
|
413
|
+
return new Response(null, { status: 304, headers: { etag: tag, [MOCK_HEADER]: call.entry } });
|
|
414
|
+
}
|
|
415
|
+
return jsonAnswer(call.entry, body, tag);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function noneMatch(header: string | undefined, tag: string): boolean {
|
|
419
|
+
if (!header) return false;
|
|
420
|
+
const want = tag.replaceAll('"', "");
|
|
421
|
+
return header.split(",").some((part) => part.trim().replace(/^W\//i, "").replaceAll('"', "") === want);
|
|
422
|
+
}
|
|
423
|
+
|
|
409
424
|
function jsonAnswer(entry: string, body: unknown, tag: string): Response {
|
|
410
425
|
const headers = new Headers({
|
|
411
426
|
"content-type": "application/json; charset=utf-8",
|
|
@@ -17,9 +17,17 @@
|
|
|
17
17
|
// `userToken: none`. A name that is not a Persona does the same, and does
|
|
18
18
|
// not fall back to the default: a registry call must not invent a caller.
|
|
19
19
|
// `/api` still falls back, so a typo does not lock the Builder out of the
|
|
20
|
-
// app. The Consumer is `DEV_CONSUMER`, or `dev-consumer`.
|
|
21
|
-
// `
|
|
22
|
-
//
|
|
20
|
+
// app. The Consumer is `DEV_CONSUMER`, or `dev-consumer`. `x-pple-entry` is
|
|
21
|
+
// the `registry` key. For a bare entry that is the name the platform sends
|
|
22
|
+
// (ADR-0031). For a namespaced entry the key is its local path (`2027/score`),
|
|
23
|
+
// because the Provider never sees the Namespace (ADR-0032), so the platform's
|
|
24
|
+
// header carries the full name while this one carries the path. The Provider is
|
|
25
|
+
// not part of it: the local app is the dev Provider, `dev-provider`, because it
|
|
26
|
+
// has no App Slug until the platform creates it.
|
|
27
|
+
//
|
|
28
|
+
// The registry path names an entry by its local path, which may run several
|
|
29
|
+
// folders (`/.pple/registry/2027/score/<tail>`). No key is a segment-prefix of
|
|
30
|
+
// another in one app (ADR-0032), so the key that leads the path is the entry.
|
|
23
31
|
//
|
|
24
32
|
// A Dataset value is an object of page functions. `/.pple/registry/<name>/<objectType>`
|
|
25
33
|
// calls that function with `{ cursor }` and answers with its page. A value with
|
|
@@ -47,7 +55,7 @@ import {
|
|
|
47
55
|
|
|
48
56
|
export const DEFAULT_DEV_CONSUMER = "dev-consumer";
|
|
49
57
|
|
|
50
|
-
/** Local stand-in for the App Slug.
|
|
58
|
+
/** Local stand-in for the Provider's App Slug. It is never joined into the entry name. */
|
|
51
59
|
export const DEV_PROVIDER_SLUG = "dev-provider";
|
|
52
60
|
|
|
53
61
|
const REGISTRY_PATH_PREFIX = "/.pple/registry/";
|
|
@@ -111,7 +119,7 @@ export async function devWorkerFetch(
|
|
|
111
119
|
ctx: ExecutionContext,
|
|
112
120
|
): Promise<Response> {
|
|
113
121
|
const url = new URL(request.url);
|
|
114
|
-
const parsed = parseRegistryPath(url.pathname);
|
|
122
|
+
const parsed = parseRegistryPath(url.pathname, user.registry);
|
|
115
123
|
if (!parsed) return user.fetch(stripPlatformHeaders(request), env, ctx);
|
|
116
124
|
if (!parsed.name) return emptyRegistryResponse();
|
|
117
125
|
|
|
@@ -164,7 +172,7 @@ function registryFacts(
|
|
|
164
172
|
consumer: string,
|
|
165
173
|
identity: Identity | null,
|
|
166
174
|
): { consumer: string; entry: string; identity: Identity | null } {
|
|
167
|
-
return { consumer, entry:
|
|
175
|
+
return { consumer, entry: name, identity };
|
|
168
176
|
}
|
|
169
177
|
|
|
170
178
|
/** What the handler sees. Matches pple-services' header filter, minus the secret. */
|
|
@@ -234,11 +242,21 @@ function handlerFor(registry: RegistryMap | undefined, name: string): RegistryFe
|
|
|
234
242
|
throw new Error("registry handler missing");
|
|
235
243
|
}
|
|
236
244
|
|
|
237
|
-
function parseRegistryPath(
|
|
245
|
+
function parseRegistryPath(
|
|
246
|
+
pathname: string,
|
|
247
|
+
registry: RegistryMap | undefined,
|
|
248
|
+
): { name: string; tail: string } | null {
|
|
238
249
|
if (pathname !== "/.pple/registry" && !pathname.startsWith(REGISTRY_PATH_PREFIX)) return null;
|
|
239
250
|
const restPath = pathname.startsWith(REGISTRY_PATH_PREFIX)
|
|
240
251
|
? pathname.slice(REGISTRY_PATH_PREFIX.length)
|
|
241
252
|
: "";
|
|
253
|
+
// A nested local path (`2027/score`) is a key that leads the path at a
|
|
254
|
+
// segment boundary. Otherwise the first segment, so a missing handler
|
|
255
|
+
// throws as before.
|
|
256
|
+
const key = Object.keys(registry ?? {}).find(
|
|
257
|
+
(candidate) => candidate.includes("/") && (restPath === candidate || restPath.startsWith(`${candidate}/`)),
|
|
258
|
+
);
|
|
259
|
+
if (key) return { name: key, tail: restPath.slice(key.length) || "/" };
|
|
242
260
|
const slash = restPath.indexOf("/");
|
|
243
261
|
const name = slash === -1 ? restPath : restPath.slice(0, slash);
|
|
244
262
|
const tail = slash === -1 ? "/" : restPath.slice(slash) || "/";
|