@hansenexus/hud 0.3.0 → 0.5.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 +782 -6
- package/dist/a11y-B5DUaiCs.js +246 -0
- package/dist/a11y-panel-p_t4eD7p.js +207 -0
- package/dist/access-C7zlaqj6.js +100 -0
- package/dist/access-D_ewAEJG.d.ts +61 -0
- package/dist/access-panel-CEfTr9ZI.js +123 -0
- package/dist/adapter-BYoxPYTI.d.ts +33 -0
- package/dist/adapter-hcwmJtNx.js +65 -0
- package/dist/auto/runtime.d.ts +65 -0
- package/dist/auto/runtime.js +50 -0
- package/dist/auto.d.ts +15 -0
- package/dist/auto.js +33 -0
- package/dist/capabilities-rDSKjXpq.js +110 -0
- package/dist/capture-CwrIwLqi.js +300 -0
- package/dist/capture-buffer-Bbbo6omF.d.ts +35 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +275 -0
- package/dist/data-panel-DhxAmP91.js +817 -0
- package/dist/data-store-ChF1td_P.js +164 -0
- package/dist/define-hud-CHPedaQ5.d.ts +50 -0
- package/dist/design-D0kGgPoP.js +87 -0
- package/dist/detect-BxHZ0pxW.d.ts +25 -0
- package/dist/detect-CHZiSeZs.js +63 -0
- package/dist/dispatch-panel-B-xr_Giy.js +279 -0
- package/dist/env-CApnbI0r.d.ts +19 -0
- package/dist/env-CaiFPIhF.js +31 -0
- package/dist/file-xdSaQ2uR.js +103 -0
- package/dist/flags-DVtaJ8pQ.d.ts +58 -0
- package/dist/flags.d.ts +2 -0
- package/dist/flags.js +82 -0
- package/dist/force-state-BaqWsmrf.js +153 -0
- package/dist/force-state-CCqoTfVB.d.ts +23 -0
- package/dist/force-state-panel-_otVYh9i.js +97 -0
- package/dist/generate-C8lQl3Eh.js +53 -0
- package/dist/handler-BpVhxEmU.js +106 -0
- package/dist/handler-DtYVCZY6.d.ts +39 -0
- package/dist/index-BAV_1W8a.d.ts +43 -0
- package/dist/index-BJ26pse9.d.ts +70 -0
- package/dist/index-BnQ0dTZf.d.ts +19 -0
- package/dist/index-CwfX-W87.d.ts +72 -0
- package/dist/index-DcH9V3cU.d.ts +48 -0
- package/dist/index-DqOyNf1u.d.ts +65 -0
- package/dist/index-DstI_ike.d.ts +66 -0
- package/dist/index-Dxz4fn4j.d.ts +42 -0
- package/dist/index-Hhh7OXzU.d.ts +129 -0
- package/dist/index-ReFbhQ9p.d.ts +25 -0
- package/dist/index-RgXfMBuW.d.ts +48 -0
- package/dist/index-gCZBO53s.d.ts +73 -0
- package/dist/index.d.ts +6 -36
- package/dist/index.js +798 -240
- package/dist/inspect-C9Ft8fqn.js +142 -0
- package/dist/inspect-root-BkaupBMp.js +429 -0
- package/dist/marker-C66LxOhJ.d.ts +9 -0
- package/dist/marker-CMrvxPmS.d.ts +12 -0
- package/dist/marker-DjeGvL-F.js +9 -0
- package/dist/marker-uZYPOJ4M.js +12 -0
- package/dist/markers.d.ts +3 -0
- package/dist/markers.js +3 -0
- package/dist/meta-Bv0sEao3.js +6 -0
- package/dist/meta-C_G7MB5w.d.ts +49 -0
- package/dist/mocks-CA-wM2SB.d.ts +109 -0
- package/dist/mocks-ESiRqk9o.js +150 -0
- package/dist/msw-browser-wjaYgkB7.d.ts +31 -0
- package/dist/next/config.d.ts +52 -0
- package/dist/next/config.js +78 -0
- package/dist/next.d.ts +14 -19
- package/dist/next.js +7 -30
- package/dist/observe-CdDnDBRP.js +89 -0
- package/dist/observe-panel-Cgp1M8oT.js +347 -0
- package/dist/ops-panel-CEqDe_UT.js +234 -0
- package/dist/payload-MUQ9ciER.d.ts +92 -0
- package/dist/plugins/a11y.d.ts +2 -0
- package/dist/plugins/a11y.js +2 -0
- package/dist/plugins/access/server.d.ts +122 -0
- package/dist/plugins/access/server.js +340 -0
- package/dist/plugins/access.d.ts +41 -0
- package/dist/plugins/access.js +51 -0
- package/dist/plugins/agent/server.d.ts +94 -0
- package/dist/plugins/agent/server.js +455 -0
- package/dist/plugins/agent.d.ts +37 -0
- package/dist/plugins/agent.js +69 -0
- package/dist/plugins/capture/server.d.ts +92 -0
- package/dist/plugins/capture/server.js +248 -0
- package/dist/plugins/capture.d.ts +24 -0
- package/dist/plugins/capture.js +70 -0
- package/dist/plugins/data/msw-browser.d.ts +2 -0
- package/dist/plugins/data/msw-browser.js +39 -0
- package/dist/plugins/data/msw-node.d.ts +36 -0
- package/dist/plugins/data/msw-node.js +68 -0
- package/dist/plugins/data/server.d.ts +257 -0
- package/dist/plugins/data/server.js +717 -0
- package/dist/plugins/data.d.ts +6 -0
- package/dist/plugins/data.js +51 -0
- package/dist/plugins/design/server.d.ts +54 -0
- package/dist/plugins/design/server.js +198 -0
- package/dist/plugins/design.d.ts +5 -0
- package/dist/plugins/design.js +4 -0
- package/dist/plugins/inspect/convex.d.ts +2 -0
- package/dist/plugins/inspect/convex.js +486 -0
- package/dist/plugins/inspect/react-query.d.ts +2 -0
- package/dist/plugins/inspect/react-query.js +225 -0
- package/dist/plugins/inspect/sockets.d.ts +51 -0
- package/dist/plugins/inspect/sockets.js +397 -0
- package/dist/plugins/inspect/swr.d.ts +2 -0
- package/dist/plugins/inspect/swr.js +218 -0
- package/dist/plugins/inspect.d.ts +4 -0
- package/dist/plugins/inspect.js +3 -0
- package/dist/plugins/observe.d.ts +4 -0
- package/dist/plugins/observe.js +2 -0
- package/dist/plugins/ops/server.d.ts +82 -0
- package/dist/plugins/ops/server.js +381 -0
- package/dist/plugins/ops.d.ts +27 -0
- package/dist/plugins/ops.js +41 -0
- package/dist/plugins/routes/server.d.ts +88 -0
- package/dist/plugins/routes/server.js +62 -0
- package/dist/plugins/routes.d.ts +3 -0
- package/dist/plugins/routes.js +216 -0
- package/dist/plugins/seo.d.ts +2 -0
- package/dist/plugins/seo.js +2 -0
- package/dist/plugins/vitals.d.ts +2 -25
- package/dist/plugins/vitals.js +4 -86
- package/dist/plugins-BZvDqGx5.d.ts +144 -0
- package/dist/{plugins-CzUiTn_r.js → plugins-DVy_vaYr.js} +3 -1
- package/dist/poll-CjKsWZi8.js +89 -0
- package/dist/requests-BwD6MKEq.js +282 -0
- package/dist/requests-CJrUf465.d.ts +50 -0
- package/dist/review.d.ts +181 -0
- package/dist/review.js +1017 -0
- package/dist/same-origin-BGet22CC.js +21 -0
- package/dist/scan-DzJNXqOk.js +165 -0
- package/dist/selector-NSYSe7AZ.js +28 -0
- package/dist/seo-Dq9q_QkO.js +551 -0
- package/dist/seo-panel-CfiY-TnT.js +124 -0
- package/dist/server.d.ts +3 -39
- package/dist/server.js +2 -105
- package/dist/sessions-panel-COvhPAzv.js +98 -0
- package/dist/shortcut-s_mjGeU8.js +179 -0
- package/dist/state-CjjZPjPJ.js +40 -0
- package/dist/state-boundary.d.ts +26 -0
- package/dist/state-boundary.js +61 -0
- package/dist/store/convex.d.ts +33 -0
- package/dist/store/convex.js +52 -0
- package/dist/store-CQqIIKTc.js +59 -0
- package/dist/store-Ync9eb1G.js +67 -0
- package/dist/store.d.ts +76 -0
- package/dist/store.js +188 -0
- package/dist/styles-BA8ueHoV.js +342 -0
- package/dist/styles-DKU_P8MH.js +139 -0
- package/dist/styles-DPpDPmzi.js +68 -0
- package/dist/tokens-panel-CbMQfasZ.js +142 -0
- package/dist/types-B-ulf2Jp.js +8 -0
- package/dist/types-BIgGf-0C.d.ts +25 -0
- package/dist/types-BIkI-qN8.js +30 -0
- package/dist/types-Cxh4twqc.d.ts +50 -0
- package/dist/types-DIdPfjtV.js +6 -0
- package/dist/types-DaJcsEjO.d.ts +44 -0
- package/dist/types-Dn68psVz.d.ts +118 -0
- package/dist/types-f3jlNxzw.d.ts +92 -0
- package/dist/types-szkr2QOF.d.ts +39 -0
- package/dist/unlock-store-4V3vR4Eg.js +96 -0
- package/dist/variant-groups-TIfO3NIj.js +137 -0
- package/dist/variant-groups-aAIrcNK5.d.ts +36 -0
- package/dist/variants-panel-BDoSkwBJ.js +457 -0
- package/dist/variants-types-CcRf119M.d.ts +63 -0
- package/dist/variants.d.ts +43 -0
- package/dist/variants.js +83 -0
- package/dist/vitals-panel-BCmMUntW.js +30 -0
- package/dist/vite.d.ts +37 -0
- package/dist/vite.js +134 -0
- package/package.json +85 -5
- package/dist/define-hud-BHQR8Lbv.d.ts +0 -31
- package/dist/plugins-1QmTmZyL.d.ts +0 -76
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
import { onCLS, onFCP, onINP, onLCP, onTTFB } from "web-vitals";
|
|
3
|
+
//#region src/plugins/vitals/store.ts
|
|
4
|
+
/** Display order: the three Core Web Vitals first, then the diagnostics. */
|
|
5
|
+
const VITAL_ORDER = [
|
|
6
|
+
"LCP",
|
|
7
|
+
"INP",
|
|
8
|
+
"CLS",
|
|
9
|
+
"FCP",
|
|
10
|
+
"TTFB"
|
|
11
|
+
];
|
|
12
|
+
let snapshot = {};
|
|
13
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
14
|
+
let started = false;
|
|
15
|
+
function record(metric) {
|
|
16
|
+
snapshot = {
|
|
17
|
+
...snapshot,
|
|
18
|
+
[metric.name]: {
|
|
19
|
+
name: metric.name,
|
|
20
|
+
value: metric.value,
|
|
21
|
+
rating: metric.rating
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
for (const listener of listeners) listener();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Starts collection once per page. Called when the HUD mounts, not when the
|
|
28
|
+
* panel opens: LCP and FCP are only reported for the initial load, so a lazy
|
|
29
|
+
* start would miss them.
|
|
30
|
+
*/
|
|
31
|
+
function startVitals() {
|
|
32
|
+
if (started) return;
|
|
33
|
+
started = true;
|
|
34
|
+
const opts = { reportAllChanges: true };
|
|
35
|
+
try {
|
|
36
|
+
onLCP(record, opts);
|
|
37
|
+
onINP(record, opts);
|
|
38
|
+
onCLS(record, opts);
|
|
39
|
+
onFCP(record, opts);
|
|
40
|
+
onTTFB(record, opts);
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
function getVitals() {
|
|
44
|
+
return snapshot;
|
|
45
|
+
}
|
|
46
|
+
function subscribeVitals(listener) {
|
|
47
|
+
listeners.add(listener);
|
|
48
|
+
return () => listeners.delete(listener);
|
|
49
|
+
}
|
|
50
|
+
function useVitals() {
|
|
51
|
+
return useSyncExternalStore(subscribeVitals, getVitals, getVitals);
|
|
52
|
+
}
|
|
53
|
+
function formatVital(name, value) {
|
|
54
|
+
if (name === "CLS") return value.toFixed(2);
|
|
55
|
+
if (value >= 1e3) return `${(value / 1e3).toFixed(1)} s`;
|
|
56
|
+
return `${Math.round(value)} ms`;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { useVitals as i, formatVital as n, startVitals as r, VITAL_ORDER as t };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
//#region src/core/grabber/store.ts
|
|
3
|
+
const IDLE = {
|
|
4
|
+
grabbing: false,
|
|
5
|
+
payload: null,
|
|
6
|
+
highlight: null
|
|
7
|
+
};
|
|
8
|
+
function createGrabber() {
|
|
9
|
+
let state = IDLE;
|
|
10
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
11
|
+
const set = (next) => {
|
|
12
|
+
if (next.grabbing === state.grabbing && next.payload === state.payload && next.highlight === state.highlight) return;
|
|
13
|
+
state = next;
|
|
14
|
+
for (const listener of listeners) listener();
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
start: () => set({
|
|
18
|
+
...state,
|
|
19
|
+
grabbing: true,
|
|
20
|
+
highlight: null
|
|
21
|
+
}),
|
|
22
|
+
cancel: () => set({
|
|
23
|
+
...state,
|
|
24
|
+
grabbing: false
|
|
25
|
+
}),
|
|
26
|
+
clear: () => set({
|
|
27
|
+
...state,
|
|
28
|
+
payload: null
|
|
29
|
+
}),
|
|
30
|
+
amend: (patch) => {
|
|
31
|
+
if (state.payload) set({
|
|
32
|
+
...state,
|
|
33
|
+
payload: {
|
|
34
|
+
...state.payload,
|
|
35
|
+
...patch
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
highlight: (element, label = element.tagName.toLowerCase()) => set({
|
|
40
|
+
...state,
|
|
41
|
+
highlight: {
|
|
42
|
+
element,
|
|
43
|
+
label
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
clearHighlight: () => set({
|
|
47
|
+
...state,
|
|
48
|
+
highlight: null
|
|
49
|
+
}),
|
|
50
|
+
complete: (payload) => set({
|
|
51
|
+
...state,
|
|
52
|
+
grabbing: false,
|
|
53
|
+
payload
|
|
54
|
+
}),
|
|
55
|
+
getState: () => state,
|
|
56
|
+
subscribe: (listener) => {
|
|
57
|
+
listeners.add(listener);
|
|
58
|
+
return () => listeners.delete(listener);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Subscribes a component to a HUD's grab state: `useGrab(ctx.grab)`. */
|
|
63
|
+
function useGrab(grab) {
|
|
64
|
+
return useSyncExternalStore(grab.subscribe, grab.getState, grab.getState);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { useGrab as n, createGrabber as t };
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { d as HudRoutes, r as HudCapability } from "./plugins-BZvDqGx5.js";
|
|
2
|
+
import { a as STORE_COLLECTION, c as assertId, i as HudStoreValue, l as assertValue, n as HudStoreEntry, o as STORE_ID, r as HudStoreError, s as assertCollection, t as HudStore, u as isStoreValue } from "./types-DaJcsEjO.js";
|
|
3
|
+
//#region src/store/file.d.ts
|
|
4
|
+
interface FileStoreOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Directory the collections live in. Default `<cwd>/.hud`, the dev
|
|
7
|
+
* server's root, so `.hud/variants.json` sits next to the app and an agent
|
|
8
|
+
* in the same checkout can edit it.
|
|
9
|
+
*/
|
|
10
|
+
dir?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The default store: one JSON file per collection under `.hud/`, keyed by id,
|
|
14
|
+
* so a human or an agent can read and edit it (`.hud/variants.json`:
|
|
15
|
+
* `{ "hero:b": { "score": 8, "rationale": "…", "scoredBy": "claude" } }`).
|
|
16
|
+
*
|
|
17
|
+
* Server side only: the browser reaches it through `storeRoutes()` or a
|
|
18
|
+
* plugin's own routes. The collection name is validated and the resolved
|
|
19
|
+
* path checked against `dir`, so no request can name a file outside it.
|
|
20
|
+
* Writes go to a temporary file and are renamed into place, one at a time
|
|
21
|
+
* per collection.
|
|
22
|
+
*/
|
|
23
|
+
export declare function file({ dir }?: FileStoreOptions): HudStore;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/store/http.d.ts
|
|
26
|
+
interface HttpStoreOptions {
|
|
27
|
+
/** Where `storeRoutes()` is mounted, e.g. `/api/hud/store`. */
|
|
28
|
+
baseUrl: string;
|
|
29
|
+
/** Default: the global `fetch`. */
|
|
30
|
+
fetch?: typeof fetch;
|
|
31
|
+
/** Extra headers on every request, such as a token for a remote store. */
|
|
32
|
+
headers?: HeadersInit;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A store over HTTP, against `storeRoutes()` (or anything speaking the same
|
|
36
|
+
* three routes): `GET <base>/<collection>` lists, `GET <base>/<collection>/<id>`
|
|
37
|
+
* reads (404 reads as null), `PUT <base>/<collection>/<id>` writes a JSON
|
|
38
|
+
* object. Runs in a browser or on a server; it is how a client panel reaches
|
|
39
|
+
* the dev server's `file()`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function http({ baseUrl, fetch: doFetch, headers }: HttpStoreOptions): HudStore;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/store/memory.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* An in-memory store: tests, the playground (where MSW runs the server half
|
|
46
|
+
* in the browser) and anything that may forget on restart. `seed` maps
|
|
47
|
+
* collection to documents by id.
|
|
48
|
+
*/
|
|
49
|
+
export declare function memory(seed?: Record<string, Record<string, HudStoreValue>>): HudStore;
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/store/routes.d.ts
|
|
52
|
+
/**
|
|
53
|
+
* The three routes `http()` speaks, over any store. Mount them in a plugin's
|
|
54
|
+
* `routes` (`storeServer()` is the ready-made one) and the browser's
|
|
55
|
+
* `http({ baseUrl })` becomes a view onto the server's `file()`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function storeRoutes(store: HudStore): HudRoutes;
|
|
58
|
+
interface StoreServerOptions {
|
|
59
|
+
store: HudStore;
|
|
60
|
+
/** Plugin id, and therefore the URL segment. Default `store`. */
|
|
61
|
+
id?: string;
|
|
62
|
+
/** Default `data.write`: the deployed-tier matrix never grants it, so a deployed HUD cannot write. */
|
|
63
|
+
capability?: HudCapability;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A plugin that exposes a store over HTTP at `<apiBase>/<id>/…`, for
|
|
67
|
+
* `http({ baseUrl: "<apiBase>/<id>" })` in the browser. Carries `data.write`
|
|
68
|
+
* by default, which `access` denies on every deployed tier.
|
|
69
|
+
*/
|
|
70
|
+
export declare function storeServer({ store, id, capability }: StoreServerOptions): {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly capability: string;
|
|
73
|
+
readonly routes: HudRoutes;
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
export { type FileStoreOptions, type HttpStoreOptions, type HudStore, type HudStoreEntry, HudStoreError, type HudStoreValue, STORE_COLLECTION, STORE_ID, type StoreServerOptions, assertCollection, assertId, assertValue, isStoreValue };
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { i as definePlugin } from "./plugins-DVy_vaYr.js";
|
|
2
|
+
import { t as refuseCrossSite } from "./same-origin-BGet22CC.js";
|
|
3
|
+
import { a as assertId, i as assertCollection, n as STORE_COLLECTION, o as assertValue, r as STORE_ID, s as isStoreValue, t as HudStoreError } from "./types-BIkI-qN8.js";
|
|
4
|
+
import { t as file } from "./file-xdSaQ2uR.js";
|
|
5
|
+
//#region src/store/http.ts
|
|
6
|
+
function errorOf(body) {
|
|
7
|
+
if (isStoreValue(body) && typeof body.error === "string") return body.error;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A store over HTTP, against `storeRoutes()` (or anything speaking the same
|
|
11
|
+
* three routes): `GET <base>/<collection>` lists, `GET <base>/<collection>/<id>`
|
|
12
|
+
* reads (404 reads as null), `PUT <base>/<collection>/<id>` writes a JSON
|
|
13
|
+
* object. Runs in a browser or on a server; it is how a client panel reaches
|
|
14
|
+
* the dev server's `file()`.
|
|
15
|
+
*/
|
|
16
|
+
function http({ baseUrl, fetch: doFetch, headers }) {
|
|
17
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
18
|
+
const call = doFetch ?? ((input, init) => fetch(input, init));
|
|
19
|
+
async function request(url, init = {}) {
|
|
20
|
+
const merged = new Headers(headers);
|
|
21
|
+
for (const [key, value] of new Headers(init.headers)) merged.set(key, value);
|
|
22
|
+
if (!merged.has("accept")) merged.set("accept", "application/json");
|
|
23
|
+
return call(url, {
|
|
24
|
+
...init,
|
|
25
|
+
headers: merged
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async function body(res) {
|
|
29
|
+
const text = await res.text();
|
|
30
|
+
if (!text) return void 0;
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(text);
|
|
33
|
+
} catch {
|
|
34
|
+
return text;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function fail(res, what) {
|
|
38
|
+
const parsed = await body(res);
|
|
39
|
+
throw new HudStoreError(`hud store: ${what} answered ${res.status}${errorOf(parsed) ? `: ${errorOf(parsed)}` : ""}`, res.status);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
async get(collection, id) {
|
|
43
|
+
assertCollection(collection);
|
|
44
|
+
assertId(id);
|
|
45
|
+
const res = await request(`${base}/${collection}/${encodeURIComponent(id)}`);
|
|
46
|
+
if (res.status === 404) return null;
|
|
47
|
+
if (!res.ok) return fail(res, `GET ${collection}/${id}`);
|
|
48
|
+
const parsed = await body(res);
|
|
49
|
+
return isStoreValue(parsed) && isStoreValue(parsed.value) ? parsed.value : null;
|
|
50
|
+
},
|
|
51
|
+
async set(collection, id, value) {
|
|
52
|
+
assertCollection(collection);
|
|
53
|
+
assertId(id);
|
|
54
|
+
assertValue(value);
|
|
55
|
+
const res = await request(`${base}/${collection}/${encodeURIComponent(id)}`, {
|
|
56
|
+
method: "PUT",
|
|
57
|
+
headers: { "content-type": "application/json" },
|
|
58
|
+
body: JSON.stringify(value)
|
|
59
|
+
});
|
|
60
|
+
if (!res.ok) return fail(res, `PUT ${collection}/${id}`);
|
|
61
|
+
},
|
|
62
|
+
async list(collection) {
|
|
63
|
+
assertCollection(collection);
|
|
64
|
+
const res = await request(`${base}/${collection}`);
|
|
65
|
+
if (res.status === 404) return [];
|
|
66
|
+
if (!res.ok) return fail(res, `GET ${collection}`);
|
|
67
|
+
const parsed = await body(res);
|
|
68
|
+
return (isStoreValue(parsed) && Array.isArray(parsed.entries) ? parsed.entries : []).filter((entry) => isStoreValue(entry) && typeof entry.id === "string" && isStoreValue(entry.value));
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/store/memory.ts
|
|
74
|
+
/**
|
|
75
|
+
* An in-memory store: tests, the playground (where MSW runs the server half
|
|
76
|
+
* in the browser) and anything that may forget on restart. `seed` maps
|
|
77
|
+
* collection to documents by id.
|
|
78
|
+
*/
|
|
79
|
+
function memory(seed = {}) {
|
|
80
|
+
const collections = /* @__PURE__ */ new Map();
|
|
81
|
+
for (const [collection, documents] of Object.entries(seed)) {
|
|
82
|
+
assertCollection(collection);
|
|
83
|
+
collections.set(collection, new Map(Object.entries(documents)));
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
async get(collection, id) {
|
|
87
|
+
assertCollection(collection);
|
|
88
|
+
assertId(id);
|
|
89
|
+
return collections.get(collection)?.get(id) ?? null;
|
|
90
|
+
},
|
|
91
|
+
async set(collection, id, value) {
|
|
92
|
+
assertCollection(collection);
|
|
93
|
+
assertId(id);
|
|
94
|
+
assertValue(value);
|
|
95
|
+
let documents = collections.get(collection);
|
|
96
|
+
if (!documents) {
|
|
97
|
+
documents = /* @__PURE__ */ new Map();
|
|
98
|
+
collections.set(collection, documents);
|
|
99
|
+
}
|
|
100
|
+
documents.set(id, structuredClone(value));
|
|
101
|
+
},
|
|
102
|
+
async list(collection) {
|
|
103
|
+
assertCollection(collection);
|
|
104
|
+
const documents = collections.get(collection);
|
|
105
|
+
if (!documents) return [];
|
|
106
|
+
return [...documents].map(([id, value]) => ({
|
|
107
|
+
id,
|
|
108
|
+
value
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/store/routes.ts
|
|
115
|
+
const NO_STORE = { "cache-control": "no-store" };
|
|
116
|
+
function failed(error) {
|
|
117
|
+
if (error instanceof HudStoreError) return Response.json({
|
|
118
|
+
ok: false,
|
|
119
|
+
error: error.message
|
|
120
|
+
}, { status: error.status });
|
|
121
|
+
console.error("hud store: route failed", error);
|
|
122
|
+
return Response.json({
|
|
123
|
+
ok: false,
|
|
124
|
+
error: "store failed"
|
|
125
|
+
}, { status: 500 });
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The three routes `http()` speaks, over any store. Mount them in a plugin's
|
|
129
|
+
* `routes` (`storeServer()` is the ready-made one) and the browser's
|
|
130
|
+
* `http({ baseUrl })` becomes a view onto the server's `file()`.
|
|
131
|
+
*/
|
|
132
|
+
function storeRoutes(store) {
|
|
133
|
+
return {
|
|
134
|
+
"/:collection": { GET: async (_request, { params }) => {
|
|
135
|
+
try {
|
|
136
|
+
const entries = await store.list(params.collection ?? "");
|
|
137
|
+
return Response.json({
|
|
138
|
+
ok: true,
|
|
139
|
+
entries
|
|
140
|
+
}, { headers: NO_STORE });
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return failed(error);
|
|
143
|
+
}
|
|
144
|
+
} },
|
|
145
|
+
"/:collection/:id": {
|
|
146
|
+
GET: async (_request, { params }) => {
|
|
147
|
+
try {
|
|
148
|
+
const value = await store.get(params.collection ?? "", params.id ?? "");
|
|
149
|
+
if (value === null) return Response.json({
|
|
150
|
+
ok: false,
|
|
151
|
+
error: "not found"
|
|
152
|
+
}, { status: 404 });
|
|
153
|
+
return Response.json({
|
|
154
|
+
ok: true,
|
|
155
|
+
value
|
|
156
|
+
}, { headers: NO_STORE });
|
|
157
|
+
} catch (error) {
|
|
158
|
+
return failed(error);
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
PUT: async (request, { params }) => {
|
|
162
|
+
const refusal = refuseCrossSite(request);
|
|
163
|
+
if (refusal) return refusal;
|
|
164
|
+
const value = await request.json().catch(() => void 0);
|
|
165
|
+
try {
|
|
166
|
+
await store.set(params.collection ?? "", params.id ?? "", value);
|
|
167
|
+
return Response.json({ ok: true });
|
|
168
|
+
} catch (error) {
|
|
169
|
+
return failed(error);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* A plugin that exposes a store over HTTP at `<apiBase>/<id>/…`, for
|
|
177
|
+
* `http({ baseUrl: "<apiBase>/<id>" })` in the browser. Carries `data.write`
|
|
178
|
+
* by default, which `access` denies on every deployed tier.
|
|
179
|
+
*/
|
|
180
|
+
function storeServer({ store, id = "store", capability = "data.write" }) {
|
|
181
|
+
return definePlugin({
|
|
182
|
+
id,
|
|
183
|
+
capability,
|
|
184
|
+
routes: storeRoutes(store)
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
//#endregion
|
|
188
|
+
export { HudStoreError, STORE_COLLECTION, STORE_ID, assertCollection, assertId, assertValue, file, http, isStoreValue, memory, storeRoutes, storeServer };
|