cursedbelt-server 4.49.0 → 4.51.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/server/dropzone/index.d.ts +5 -1
- package/dist/server/dropzone/index.js +5 -1
- package/dist/server/dropzone/links.d.ts +79 -0
- package/dist/server/dropzone/links.js +190 -0
- package/dist/server/dropzone/scan.d.ts +63 -3
- package/dist/server/dropzone/scan.js +105 -30
- package/dist/server/maps-budget/mapsBudget.d.ts +20 -0
- package/dist/server/maps-budget/mapsBudget.js +57 -0
- package/dist/server/maps-static/staticMap.d.ts +103 -0
- package/dist/server/maps-static/staticMap.js +171 -0
- package/package.json +7 -1
- package/src/server/dropzone/index.ts +21 -0
- package/src/server/dropzone/links.spec.ts +176 -0
- package/src/server/dropzone/links.ts +219 -0
- package/src/server/dropzone/scan.spec.ts +32 -1
- package/src/server/dropzone/scan.ts +142 -30
- package/src/server/maps-budget/mapsBudget.ts +76 -0
- package/src/server/maps-budget/metaTableBudget.spec.ts +34 -0
- package/src/server/maps-static/staticMap.spec.ts +47 -0
- package/src/server/maps-static/staticMap.ts +197 -0
- package/src/server/serveBunOverload.spec.ts +24 -10
- package/src/specsWriteNothingIntoSrc.spec.ts +146 -0
|
@@ -75,6 +75,7 @@
|
|
|
75
75
|
* exists to fix. Each app keeps its own ONE DOOR (`maps.ts` in family, pinned by
|
|
76
76
|
* a test that scans for Google hosts outside it) and both doors consume this.
|
|
77
77
|
*/
|
|
78
|
+
import type { D1LikeDatabase } from '../d1/types.js';
|
|
78
79
|
/** Where the counter lives. A tiny KV, so an app can back it with whatever
|
|
79
80
|
* durable store it already has — `apps/orch` uses its own settings store,
|
|
80
81
|
* `apps/family` uses the `meta` table of the database it already opens. */
|
|
@@ -192,3 +193,22 @@ export interface MapsBudgetOptions<Api extends string> {
|
|
|
192
193
|
now?: () => number;
|
|
193
194
|
}
|
|
194
195
|
export declare function createMapsBudget<Api extends string>(options: MapsBudgetOptions<Api>): MapsBudget<Api>;
|
|
196
|
+
/** What {@link createMetaTableBudget} hands back: the same check-and-count, awaited. */
|
|
197
|
+
export interface AsyncMapsBudget<Api extends string> {
|
|
198
|
+
consume(api: Api, calls?: number): Promise<BudgetVerdict>;
|
|
199
|
+
/** Every API in the ceilings, for a health surface — a roster read, so an API added to
|
|
200
|
+
* the ceilings cannot be missed by a surface that asks for each one by name. */
|
|
201
|
+
usage(): Promise<ReturnType<MapsBudget<Api>['usage']>[]>;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The ceiling over an app's own two-column `meta (key, value)` table on D1 or bun:sqlite —
|
|
205
|
+
* family's adapter, lifted when photos became the second app to need it (forge task 3700).
|
|
206
|
+
*
|
|
207
|
+
* 🔴 The package's store is SYNCHRONOUS by contract and D1 is not. So the counters are read
|
|
208
|
+
* into a snapshot before each decision (one query for every `maps-budget.*` row) and the
|
|
209
|
+
* ceiling decides over the snapshot. The write goes back as a DELTA (`value + n`), never the
|
|
210
|
+
* absolute number: two requests in two isolates each read 10 and each count one, and an
|
|
211
|
+
* absolute write would leave 11 where 12 is true. A spend ceiling that can lose increments
|
|
212
|
+
* under load is the one kind that cannot be trusted.
|
|
213
|
+
*/
|
|
214
|
+
export declare function createMetaTableBudget<Api extends string>(db: Pick<D1LikeDatabase, 'prepare' | 'batch'>, options: Omit<MapsBudgetOptions<Api>, 'store'>): AsyncMapsBudget<Api>;
|
|
@@ -191,3 +191,60 @@ export function createMapsBudget(options) {
|
|
|
191
191
|
},
|
|
192
192
|
};
|
|
193
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* The ceiling over an app's own two-column `meta (key, value)` table on D1 or bun:sqlite —
|
|
196
|
+
* family's adapter, lifted when photos became the second app to need it (forge task 3700).
|
|
197
|
+
*
|
|
198
|
+
* 🔴 The package's store is SYNCHRONOUS by contract and D1 is not. So the counters are read
|
|
199
|
+
* into a snapshot before each decision (one query for every `maps-budget.*` row) and the
|
|
200
|
+
* ceiling decides over the snapshot. The write goes back as a DELTA (`value + n`), never the
|
|
201
|
+
* absolute number: two requests in two isolates each read 10 and each count one, and an
|
|
202
|
+
* absolute write would leave 11 where 12 is true. A spend ceiling that can lose increments
|
|
203
|
+
* under load is the one kind that cannot be trusted.
|
|
204
|
+
*/
|
|
205
|
+
export function createMetaTableBudget(db, options) {
|
|
206
|
+
const snapshot = new Map();
|
|
207
|
+
const loaded = new Map();
|
|
208
|
+
const sync = async () => {
|
|
209
|
+
snapshot.clear();
|
|
210
|
+
loaded.clear();
|
|
211
|
+
const rows = await db.prepare("SELECT key, value FROM meta WHERE key LIKE 'maps-budget.%'").all();
|
|
212
|
+
for (const row of rows.results) {
|
|
213
|
+
snapshot.set(row.key, row.value);
|
|
214
|
+
loaded.set(row.key, Number(row.value) || 0);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
const flush = async () => {
|
|
218
|
+
const writes = [...snapshot]
|
|
219
|
+
.map(([key, value]) => ({ key, delta: (Number(value) || 0) - (loaded.get(key) ?? 0) }))
|
|
220
|
+
.filter((entry) => entry.delta !== 0);
|
|
221
|
+
if (writes.length === 0)
|
|
222
|
+
return;
|
|
223
|
+
await db.batch(writes.map(({ key, delta }) => db
|
|
224
|
+
.prepare(`INSERT INTO meta (key, value) VALUES (?1, CAST(?2 AS TEXT))
|
|
225
|
+
ON CONFLICT(key) DO UPDATE SET value = CAST(CAST(value AS INTEGER) + ?2 AS TEXT)`)
|
|
226
|
+
.bind(key, delta)));
|
|
227
|
+
};
|
|
228
|
+
const budget = createMapsBudget({
|
|
229
|
+
...options,
|
|
230
|
+
store: {
|
|
231
|
+
read: (key) => snapshot.get(key) ?? null,
|
|
232
|
+
write: (key, value) => {
|
|
233
|
+
snapshot.set(key, value);
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
const apis = Object.keys(options.ceilings);
|
|
238
|
+
return {
|
|
239
|
+
consume: async (api, calls) => {
|
|
240
|
+
await sync();
|
|
241
|
+
const verdict = budget.consume(api, calls);
|
|
242
|
+
await flush();
|
|
243
|
+
return verdict;
|
|
244
|
+
},
|
|
245
|
+
usage: async () => {
|
|
246
|
+
await sync();
|
|
247
|
+
return apis.map((api) => budget.usage(api));
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cursedbelt-server/maps-static — the shared half of an app's `/api/map` proxy: Google Static
|
|
3
|
+
* Maps imagery under a `cursedbelt/react/map-frame` overlay.
|
|
4
|
+
*
|
|
5
|
+
* Lifted from apps/family's `maps.ts` when photos became the second app to draw a map (forge
|
|
6
|
+
* task 3700). What is here is everything about a render that is not the network call: the
|
|
7
|
+
* request a query string may ask for (clamped, so the cache key space stays tiny), the URL with
|
|
8
|
+
* the fleet's map styling, the cache key, and an LRU for the bytes.
|
|
9
|
+
*
|
|
10
|
+
* 🔴 The FETCH is not here, on purpose. Each app keeps its own ONE DOOR to Google — the place
|
|
11
|
+
* its `cursedbelt-server/maps-budget` ceiling is consumed immediately before the request — so
|
|
12
|
+
* the ceiling and the call can never be separated. Build the URL here; spend it there.
|
|
13
|
+
*
|
|
14
|
+
* 🔴 The key NEVER reaches a browser: `GOOGLE_MAPS_API_KEY` is restricted by API and not by
|
|
15
|
+
* HTTP referrer, so the imagery is proxied and the frame's overlay is our own SVG.
|
|
16
|
+
*/
|
|
17
|
+
/** The one Static Maps endpoint. An app's door checks a URL starts with it. */
|
|
18
|
+
export declare const STATIC_MAP_HOST = "https://maps.googleapis.com/maps/api/staticmap";
|
|
19
|
+
/** A lat/lng. Structural, so the server tier needs nothing from the React package. */
|
|
20
|
+
export interface StaticMapCenter {
|
|
21
|
+
lat: number;
|
|
22
|
+
lng: number;
|
|
23
|
+
}
|
|
24
|
+
export interface StaticMapRequest {
|
|
25
|
+
center: StaticMapCenter;
|
|
26
|
+
zoom: number;
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
/** `dark` renders the muted night styling the app's dark theme wants. */
|
|
30
|
+
theme: "light" | "dark";
|
|
31
|
+
/** 2 on a retina screen. Google caps this at 2. */
|
|
32
|
+
scale?: 1 | 2;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The map styling, as Google's `style=` parameters.
|
|
36
|
+
*
|
|
37
|
+
* Deliberately DESATURATED and label-light in both themes. The imagery here is a
|
|
38
|
+
* backdrop for our own pins and arcs; a full-color road map competes with them
|
|
39
|
+
* and turns a family's migration story into a picture of motorways. Points of
|
|
40
|
+
* interest and transit are off for the same reason — a 1889 birthplace does not
|
|
41
|
+
* need the nearest pharmacy marked.
|
|
42
|
+
*
|
|
43
|
+
* ── 🔴 The designer's direction, taken literally (2026-08-24) ───────────────
|
|
44
|
+
* *"The current basemap looks like a generic open-source canvas — switch to a
|
|
45
|
+
* premium vector style (a rich dark canvas, or a clean minimalist cream) that
|
|
46
|
+
* makes your data points pop."* Both, one per theme, because `MapFrame` already
|
|
47
|
+
* takes a theme and half of this app is read at night:
|
|
48
|
+
*
|
|
49
|
+
* · LIGHT is CREAM (`0xf7f3ea`), not the cool gray-green it was. Gray-green sat
|
|
50
|
+
* at almost exactly the chroma of the `.ft-viz` palette's teal and blue slots,
|
|
51
|
+
* so a pin over land was a saturated dot on a slightly-less-saturated field.
|
|
52
|
+
* A warm neutral is the complement of every cool hue in that palette at once,
|
|
53
|
+
* which is the whole reason a cream basemap is the classic choice.
|
|
54
|
+
* · DARK is a deeper, bluer canvas (`0x0a1120`) with more separation between
|
|
55
|
+
* land and water than the old pair had. The pins draw at full chroma, so what
|
|
56
|
+
* makes them pop is the backdrop's LIGHTNESS distance from them rather than
|
|
57
|
+
* any change to the marks.
|
|
58
|
+
* · ROAD GEOMETRY is now tinted to sit just off the landscape instead of being
|
|
59
|
+
* left at Google's default. Untinted roads are the single loudest thing in
|
|
60
|
+
* the old picture and they say nothing about a family — but turning them off
|
|
61
|
+
* entirely takes away the only cue that a place is a town rather than a field.
|
|
62
|
+
* · The road CASING (`geometry.stroke`) is off. It is a second line drawn beside
|
|
63
|
+
* every first one, at the width our own migration arcs are drawn at.
|
|
64
|
+
*
|
|
65
|
+
* 🔴 **A style change invalidates every cached render**, because `staticMapKey`
|
|
66
|
+
* includes the theme and the bytes are keyed on it — which is correct, and is why
|
|
67
|
+
* this is landed in ONE pass rather than iterated against the ceiling in this
|
|
68
|
+
* file. Steady state is unchanged: a town does not move, so each place re-renders
|
|
69
|
+
* once and then never again.
|
|
70
|
+
*/
|
|
71
|
+
export declare function styleParams(theme: "light" | "dark"): string;
|
|
72
|
+
/** The cache key for a render. Everything that changes the bytes, and nothing
|
|
73
|
+
* else — so two viewers looking at the same place share one image. */
|
|
74
|
+
export declare const staticMapKey: (request: StaticMapRequest) => string;
|
|
75
|
+
/**
|
|
76
|
+
* A render's URL, key included — for the app's door to spend, never for a browser to see.
|
|
77
|
+
*
|
|
78
|
+
* Google caps a single render at 640×640 before `scale`; asking for more is a 400 rather than
|
|
79
|
+
* a bigger picture, so the size is clamped here.
|
|
80
|
+
*/
|
|
81
|
+
export declare function staticMapUrl(request: StaticMapRequest, apiKey: string): string;
|
|
82
|
+
/**
|
|
83
|
+
* The render a `/api/map?lat=&lng=&z=&w=&h=&theme=` query asks for, or null when it names no
|
|
84
|
+
* place. The clamps are what keep the cache useful: a free-form size or zoom would make every
|
|
85
|
+
* page view a new billable render.
|
|
86
|
+
*/
|
|
87
|
+
export declare function readStaticMapRequest(query: (name: string) => string | undefined): StaticMapRequest | null;
|
|
88
|
+
/**
|
|
89
|
+
* A tiny LRU for rendered map bytes — in process (or in one Worker isolate), never on disk or
|
|
90
|
+
* in a database: the bytes are a third party's disposable cache, so a restart costs a handful
|
|
91
|
+
* of re-renders, bounded by the app's ceiling, and a long browser cache header does the rest.
|
|
92
|
+
*
|
|
93
|
+
* Bounded by COUNT rather than by bytes because every entry is the same
|
|
94
|
+
* dimensions by construction (the viewport sizes are fixed by the layout), so
|
|
95
|
+
* counting images is counting megabytes with less arithmetic. 160 entries at a
|
|
96
|
+
* 640×360@2x PNG is roughly 12 MB — small enough to be invisible next to the
|
|
97
|
+
* process, large enough that a family's whole atlas stays resident.
|
|
98
|
+
*/
|
|
99
|
+
export declare function createImageCache(max?: number): {
|
|
100
|
+
get(key: string): Uint8Array | null;
|
|
101
|
+
set(key: string, value: Uint8Array): void;
|
|
102
|
+
readonly size: number;
|
|
103
|
+
};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cursedbelt-server/maps-static — the shared half of an app's `/api/map` proxy: Google Static
|
|
3
|
+
* Maps imagery under a `cursedbelt/react/map-frame` overlay.
|
|
4
|
+
*
|
|
5
|
+
* Lifted from apps/family's `maps.ts` when photos became the second app to draw a map (forge
|
|
6
|
+
* task 3700). What is here is everything about a render that is not the network call: the
|
|
7
|
+
* request a query string may ask for (clamped, so the cache key space stays tiny), the URL with
|
|
8
|
+
* the fleet's map styling, the cache key, and an LRU for the bytes.
|
|
9
|
+
*
|
|
10
|
+
* 🔴 The FETCH is not here, on purpose. Each app keeps its own ONE DOOR to Google — the place
|
|
11
|
+
* its `cursedbelt-server/maps-budget` ceiling is consumed immediately before the request — so
|
|
12
|
+
* the ceiling and the call can never be separated. Build the URL here; spend it there.
|
|
13
|
+
*
|
|
14
|
+
* 🔴 The key NEVER reaches a browser: `GOOGLE_MAPS_API_KEY` is restricted by API and not by
|
|
15
|
+
* HTTP referrer, so the imagery is proxied and the frame's overlay is our own SVG.
|
|
16
|
+
*/
|
|
17
|
+
/** The one Static Maps endpoint. An app's door checks a URL starts with it. */
|
|
18
|
+
export const STATIC_MAP_HOST = "https://maps.googleapis.com/maps/api/staticmap";
|
|
19
|
+
/**
|
|
20
|
+
* The map styling, as Google's `style=` parameters.
|
|
21
|
+
*
|
|
22
|
+
* Deliberately DESATURATED and label-light in both themes. The imagery here is a
|
|
23
|
+
* backdrop for our own pins and arcs; a full-color road map competes with them
|
|
24
|
+
* and turns a family's migration story into a picture of motorways. Points of
|
|
25
|
+
* interest and transit are off for the same reason — a 1889 birthplace does not
|
|
26
|
+
* need the nearest pharmacy marked.
|
|
27
|
+
*
|
|
28
|
+
* ── 🔴 The designer's direction, taken literally (2026-08-24) ───────────────
|
|
29
|
+
* *"The current basemap looks like a generic open-source canvas — switch to a
|
|
30
|
+
* premium vector style (a rich dark canvas, or a clean minimalist cream) that
|
|
31
|
+
* makes your data points pop."* Both, one per theme, because `MapFrame` already
|
|
32
|
+
* takes a theme and half of this app is read at night:
|
|
33
|
+
*
|
|
34
|
+
* · LIGHT is CREAM (`0xf7f3ea`), not the cool gray-green it was. Gray-green sat
|
|
35
|
+
* at almost exactly the chroma of the `.ft-viz` palette's teal and blue slots,
|
|
36
|
+
* so a pin over land was a saturated dot on a slightly-less-saturated field.
|
|
37
|
+
* A warm neutral is the complement of every cool hue in that palette at once,
|
|
38
|
+
* which is the whole reason a cream basemap is the classic choice.
|
|
39
|
+
* · DARK is a deeper, bluer canvas (`0x0a1120`) with more separation between
|
|
40
|
+
* land and water than the old pair had. The pins draw at full chroma, so what
|
|
41
|
+
* makes them pop is the backdrop's LIGHTNESS distance from them rather than
|
|
42
|
+
* any change to the marks.
|
|
43
|
+
* · ROAD GEOMETRY is now tinted to sit just off the landscape instead of being
|
|
44
|
+
* left at Google's default. Untinted roads are the single loudest thing in
|
|
45
|
+
* the old picture and they say nothing about a family — but turning them off
|
|
46
|
+
* entirely takes away the only cue that a place is a town rather than a field.
|
|
47
|
+
* · The road CASING (`geometry.stroke`) is off. It is a second line drawn beside
|
|
48
|
+
* every first one, at the width our own migration arcs are drawn at.
|
|
49
|
+
*
|
|
50
|
+
* 🔴 **A style change invalidates every cached render**, because `staticMapKey`
|
|
51
|
+
* includes the theme and the bytes are keyed on it — which is correct, and is why
|
|
52
|
+
* this is landed in ONE pass rather than iterated against the ceiling in this
|
|
53
|
+
* file. Steady state is unchanged: a town does not move, so each place re-renders
|
|
54
|
+
* once and then never again.
|
|
55
|
+
*/
|
|
56
|
+
export function styleParams(theme) {
|
|
57
|
+
const common = [
|
|
58
|
+
"feature:poi|visibility:off",
|
|
59
|
+
"feature:transit|visibility:off",
|
|
60
|
+
"feature:road|element:labels|visibility:off",
|
|
61
|
+
// A casing is a second line beside every road, at the width our own arcs
|
|
62
|
+
// are drawn at — the exact competition this styling exists to remove.
|
|
63
|
+
"feature:road|element:geometry.stroke|visibility:off",
|
|
64
|
+
];
|
|
65
|
+
const themed = theme === "dark"
|
|
66
|
+
? [
|
|
67
|
+
"element:geometry|color:0x0a1120",
|
|
68
|
+
"element:labels.text.fill|color:0x94a6c4",
|
|
69
|
+
"element:labels.text.stroke|color:0x0a1120",
|
|
70
|
+
"feature:water|element:geometry|color:0x050a14",
|
|
71
|
+
"feature:administrative|element:geometry.stroke|color:0x33456a",
|
|
72
|
+
"feature:landscape|element:geometry|color:0x121c30",
|
|
73
|
+
"feature:road|element:geometry|color:0x1b2740",
|
|
74
|
+
]
|
|
75
|
+
: [
|
|
76
|
+
"element:geometry|color:0xf7f3ea",
|
|
77
|
+
"element:labels.text.fill|color:0x6b6257",
|
|
78
|
+
"element:labels.text.stroke|color:0xfbf8f2",
|
|
79
|
+
"feature:water|element:geometry|color:0xdae3e4",
|
|
80
|
+
"feature:administrative|element:geometry.stroke|color:0xd8cfbc",
|
|
81
|
+
"feature:landscape|element:geometry|color:0xf2ece0",
|
|
82
|
+
"feature:road|element:geometry|color:0xeae2d4",
|
|
83
|
+
];
|
|
84
|
+
return [...common, ...themed].map((style) => `&style=${encodeURIComponent(style)}`).join("");
|
|
85
|
+
}
|
|
86
|
+
/** The cache key for a render. Everything that changes the bytes, and nothing
|
|
87
|
+
* else — so two viewers looking at the same place share one image. */
|
|
88
|
+
export const staticMapKey = (request) => [
|
|
89
|
+
request.center.lat.toFixed(4),
|
|
90
|
+
request.center.lng.toFixed(4),
|
|
91
|
+
request.zoom,
|
|
92
|
+
request.width,
|
|
93
|
+
request.height,
|
|
94
|
+
request.theme,
|
|
95
|
+
request.scale ?? 2,
|
|
96
|
+
].join(":");
|
|
97
|
+
/**
|
|
98
|
+
* A render's URL, key included — for the app's door to spend, never for a browser to see.
|
|
99
|
+
*
|
|
100
|
+
* Google caps a single render at 640×640 before `scale`; asking for more is a 400 rather than
|
|
101
|
+
* a bigger picture, so the size is clamped here.
|
|
102
|
+
*/
|
|
103
|
+
export function staticMapUrl(request, apiKey) {
|
|
104
|
+
const scale = request.scale ?? 2;
|
|
105
|
+
const width = Math.min(640, Math.round(request.width));
|
|
106
|
+
const height = Math.min(640, Math.round(request.height));
|
|
107
|
+
return (`${STATIC_MAP_HOST}?center=${request.center.lat},${request.center.lng}` +
|
|
108
|
+
`&zoom=${request.zoom}&size=${width}x${height}&scale=${scale}&format=png` +
|
|
109
|
+
`&maptype=roadmap${styleParams(request.theme)}` +
|
|
110
|
+
`&key=${encodeURIComponent(apiKey)}`);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The render a `/api/map?lat=&lng=&z=&w=&h=&theme=` query asks for, or null when it names no
|
|
114
|
+
* place. The clamps are what keep the cache useful: a free-form size or zoom would make every
|
|
115
|
+
* page view a new billable render.
|
|
116
|
+
*/
|
|
117
|
+
export function readStaticMapRequest(query) {
|
|
118
|
+
const lat = Number(query("lat"));
|
|
119
|
+
const lng = Number(query("lng"));
|
|
120
|
+
// `Number(undefined)` is NaN and `Number("")` is 0 — an absent coordinate must not become
|
|
121
|
+
// the Gulf of Guinea.
|
|
122
|
+
if (!query("lat") || !query("lng") || !Number.isFinite(lat) || !Number.isFinite(lng) || Math.abs(lat) > 90 || Math.abs(lng) > 180) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
center: { lat, lng },
|
|
127
|
+
zoom: Math.min(16, Math.max(1, Math.round(Number(query("z")) || 4))),
|
|
128
|
+
width: Math.min(640, Math.max(120, Math.round(Number(query("w")) || 640))),
|
|
129
|
+
height: Math.min(640, Math.max(80, Math.round(Number(query("h")) || 360))),
|
|
130
|
+
theme: query("theme") === "dark" ? "dark" : "light",
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* A tiny LRU for rendered map bytes — in process (or in one Worker isolate), never on disk or
|
|
135
|
+
* in a database: the bytes are a third party's disposable cache, so a restart costs a handful
|
|
136
|
+
* of re-renders, bounded by the app's ceiling, and a long browser cache header does the rest.
|
|
137
|
+
*
|
|
138
|
+
* Bounded by COUNT rather than by bytes because every entry is the same
|
|
139
|
+
* dimensions by construction (the viewport sizes are fixed by the layout), so
|
|
140
|
+
* counting images is counting megabytes with less arithmetic. 160 entries at a
|
|
141
|
+
* 640×360@2x PNG is roughly 12 MB — small enough to be invisible next to the
|
|
142
|
+
* process, large enough that a family's whole atlas stays resident.
|
|
143
|
+
*/
|
|
144
|
+
export function createImageCache(max = 160) {
|
|
145
|
+
const entries = new Map();
|
|
146
|
+
return {
|
|
147
|
+
get(key) {
|
|
148
|
+
const hit = entries.get(key);
|
|
149
|
+
if (!hit)
|
|
150
|
+
return null;
|
|
151
|
+
// Re-insert to mark it as recently used — Map preserves insertion order,
|
|
152
|
+
// which is the whole LRU here.
|
|
153
|
+
entries.delete(key);
|
|
154
|
+
entries.set(key, hit);
|
|
155
|
+
return hit;
|
|
156
|
+
},
|
|
157
|
+
set(key, value) {
|
|
158
|
+
entries.delete(key);
|
|
159
|
+
entries.set(key, value);
|
|
160
|
+
while (entries.size > max) {
|
|
161
|
+
const oldest = entries.keys().next().value;
|
|
162
|
+
if (oldest === undefined)
|
|
163
|
+
break;
|
|
164
|
+
entries.delete(oldest);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
get size() {
|
|
168
|
+
return entries.size;
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedbelt-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.51.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The app-facing Bun/Hono server tier of the cursedbelt split — storage, sharing, activity, guard, sync. React-free; cursedbelt-core below it.",
|
|
@@ -210,6 +210,12 @@
|
|
|
210
210
|
"source": "./src/server/maps-budget/mapsBudget.ts",
|
|
211
211
|
"import": "./dist/server/maps-budget/mapsBudget.js"
|
|
212
212
|
},
|
|
213
|
+
"./maps-static": {
|
|
214
|
+
"types": "./dist/server/maps-static/staticMap.d.ts",
|
|
215
|
+
"bun": "./src/server/maps-static/staticMap.ts",
|
|
216
|
+
"source": "./src/server/maps-static/staticMap.ts",
|
|
217
|
+
"import": "./dist/server/maps-static/staticMap.js"
|
|
218
|
+
},
|
|
213
219
|
"./master-lock": {
|
|
214
220
|
"types": "./dist/server/master-lock/index.d.ts",
|
|
215
221
|
"bun": "./src/server/master-lock/index.ts",
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Consumers (2026-08-13): `apps/music`, `apps/family`, `apps/collections` in
|
|
10
10
|
* cursed-satellites. Each supplies a `DropHandler` and nothing else.
|
|
11
|
+
*
|
|
12
|
+
* A symlink in a drop folder is a LINKED source when the sweep is asked to follow it
|
|
13
|
+
* (`followLinks`): ingested in place, never deleted (`links.ts`, forge task 3752).
|
|
11
14
|
*/
|
|
12
15
|
export {
|
|
13
16
|
describeSweep,
|
|
@@ -16,12 +19,30 @@ export {
|
|
|
16
19
|
pruneEmptyDirs,
|
|
17
20
|
scanDropzone,
|
|
18
21
|
sweepDropzone,
|
|
22
|
+
walkDropzone,
|
|
19
23
|
type DropHandler,
|
|
24
|
+
type DropzoneScan,
|
|
20
25
|
type DropOutcome,
|
|
21
26
|
type DroppedFile,
|
|
22
27
|
type ScanOptions,
|
|
23
28
|
type SweepReport,
|
|
29
|
+
type UnreachableLink,
|
|
24
30
|
} from "./scan.js";
|
|
31
|
+
export {
|
|
32
|
+
adoptFinderAliases,
|
|
33
|
+
DROPZONE_LINK,
|
|
34
|
+
ensureDropzoneLink,
|
|
35
|
+
isFinderAlias,
|
|
36
|
+
isMountPoint,
|
|
37
|
+
LINKED_FREE_FLOOR_BYTES,
|
|
38
|
+
LINKED_OWED_WINDOW_MS,
|
|
39
|
+
linkedRoomBytes,
|
|
40
|
+
linkSource,
|
|
41
|
+
linkTarget,
|
|
42
|
+
resolveFinderAlias,
|
|
43
|
+
type AliasAdoption,
|
|
44
|
+
type LinkSource,
|
|
45
|
+
} from "./links.js";
|
|
25
46
|
export {
|
|
26
47
|
askBinaryServerAboutDuplicate,
|
|
27
48
|
hashFile,
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { adoptFinderAliases, DROPZONE_LINK, ensureDropzoneLink, isFinderAlias, linkSource } from "./links.js";
|
|
6
|
+
import { scanDropzone, sweepDropzone, walkDropzone, type DropOutcome } from "./scan.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* LINKED sources (forge task 3752). Every test here is about the owner's OWN drive: a link in a
|
|
10
|
+
* drop folder points at folders and files that are not the sweep's to delete, prune or move.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
let base: string;
|
|
14
|
+
/** The drop folder. */
|
|
15
|
+
let root: string;
|
|
16
|
+
/** Stands in for the owner's drive — everything here must survive every sweep. */
|
|
17
|
+
let drive: string;
|
|
18
|
+
|
|
19
|
+
const write = (at: string, bytes = 8): string => {
|
|
20
|
+
mkdirSync(join(at, ".."), { recursive: true });
|
|
21
|
+
writeFileSync(at, Buffer.alloc(bytes, 7));
|
|
22
|
+
return at;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
base = mkdtempSync(join(tmpdir(), "dropzone-links-spec-"));
|
|
27
|
+
root = join(base, "drop");
|
|
28
|
+
drive = join(base, "drive");
|
|
29
|
+
mkdirSync(root);
|
|
30
|
+
mkdirSync(drive);
|
|
31
|
+
});
|
|
32
|
+
afterEach(() => rmSync(base, { recursive: true, force: true }));
|
|
33
|
+
|
|
34
|
+
describe("walkDropzone with followLinks", () => {
|
|
35
|
+
test("a link is skipped unless asked — no consumer changes behavior unasked", async () => {
|
|
36
|
+
write(join(drive, "a.mp3"));
|
|
37
|
+
symlinkSync(drive, join(root, "linked"));
|
|
38
|
+
expect(await scanDropzone(root)).toEqual([]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("a linked folder is walked, a linked file is one file, and each carries its real path", async () => {
|
|
42
|
+
write(join(drive, "Album", "a.mp3"));
|
|
43
|
+
const loose = write(join(base, "loose.mp3"));
|
|
44
|
+
write(join(root, "dropped.mp3"));
|
|
45
|
+
symlinkSync(drive, join(root, "Drive"));
|
|
46
|
+
symlinkSync(loose, join(root, "single.mp3"));
|
|
47
|
+
|
|
48
|
+
const { files, unreachable } = await walkDropzone(root, { followLinks: true });
|
|
49
|
+
const byPath = Object.fromEntries(files.map((f) => [f.relativePath, f.linked?.realPath ?? null]));
|
|
50
|
+
expect(byPath).toEqual({
|
|
51
|
+
"Drive/Album/a.mp3": realpathSync(join(drive, "Album", "a.mp3")),
|
|
52
|
+
"single.mp3": realpathSync(loose),
|
|
53
|
+
"dropped.mp3": null,
|
|
54
|
+
});
|
|
55
|
+
expect(unreachable).toEqual([]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("🔴 a loop, two links to one folder, and a link back into the drop folder are each walked once", async () => {
|
|
59
|
+
write(join(drive, "a.mp3"));
|
|
60
|
+
symlinkSync(drive, join(drive, "loop"));
|
|
61
|
+
symlinkSync(drive, join(root, "first"));
|
|
62
|
+
symlinkSync(drive, join(root, "second"));
|
|
63
|
+
write(join(root, "Own", "b.mp3"));
|
|
64
|
+
symlinkSync(join(root, "Own"), join(root, "back"));
|
|
65
|
+
|
|
66
|
+
const { files } = await walkDropzone(root, { followLinks: true });
|
|
67
|
+
const reals = files.map((f) => f.linked?.realPath ?? realpathSync(f.path));
|
|
68
|
+
expect(reals.filter((p) => p.endsWith("a.mp3"))).toHaveLength(1);
|
|
69
|
+
expect(reals.filter((p) => p.endsWith("b.mp3"))).toHaveLength(1);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("an unplugged drive and a deleted folder are told apart, and only the drop folder's own links are its", async () => {
|
|
73
|
+
symlinkSync("/Volumes/no-such-drive-3752/tod", join(root, "unplugged"));
|
|
74
|
+
symlinkSync(join(base, "gone"), join(root, "deleted"));
|
|
75
|
+
symlinkSync(join(base, "also-gone"), join(drive, "dangling"));
|
|
76
|
+
symlinkSync(drive, join(root, "Drive"));
|
|
77
|
+
|
|
78
|
+
const { unreachable } = await walkDropzone(root, { followLinks: true });
|
|
79
|
+
const by = Object.fromEntries(unreachable.map((u) => [u.relativePath, [u.source.state, u.inDropzone]]));
|
|
80
|
+
expect(by).toEqual({
|
|
81
|
+
unplugged: ["unmounted", true],
|
|
82
|
+
deleted: ["deleted", true],
|
|
83
|
+
"Drive/dangling": ["deleted", false],
|
|
84
|
+
});
|
|
85
|
+
expect(unreachable.find((u) => u.relativePath === "unplugged")?.source).toMatchObject({ volume: "/Volumes/no-such-drive-3752" });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("skipTopDirs skips a folder only directly under the root, case as APFS compares it", async () => {
|
|
89
|
+
write(join(root, "Dups", "x.mp3"));
|
|
90
|
+
write(join(drive, "dups", "y.mp3"));
|
|
91
|
+
symlinkSync(drive, join(root, "Drive"));
|
|
92
|
+
const { files } = await walkDropzone(root, { followLinks: true, skipTopDirs: ["dups"] });
|
|
93
|
+
expect(files.map((f) => f.relativePath)).toEqual(["Drive/dups/y.mp3"]);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("sweepDropzone never deletes a linked file", () => {
|
|
98
|
+
for (const kind of ["stored", "duplicate", "discard"] as const)
|
|
99
|
+
test(`🔴 "${kind}" leaves the owner's copy where it lives`, async () => {
|
|
100
|
+
const owned = write(join(drive, "Album", "a.mp3"));
|
|
101
|
+
write(join(root, "dropped.mp3"));
|
|
102
|
+
symlinkSync(drive, join(root, "Drive"));
|
|
103
|
+
|
|
104
|
+
const report = await sweepDropzone(root, async (): Promise<DropOutcome> => ({ kind }), { stabilityMs: 0, followLinks: true });
|
|
105
|
+
|
|
106
|
+
expect(existsSync(owned)).toBe(true);
|
|
107
|
+
expect(existsSync(join(root, "dropped.mp3"))).toBe(false); // a dropped file still goes
|
|
108
|
+
expect(report.leftInPlace).toBe(1);
|
|
109
|
+
expect(report.bytesRemoved).toBe(8);
|
|
110
|
+
expect(lstatSync(join(root, "Drive")).isSymbolicLink()).toBe(true);
|
|
111
|
+
expect(report.remaining).toBe(0); // a link not followed is not "left in the folder"
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("a sweep reports the links it could not reach", async () => {
|
|
115
|
+
symlinkSync("/Volumes/no-such-drive-3752/tod", join(root, "unplugged"));
|
|
116
|
+
const report = await sweepDropzone(root, async () => ({ kind: "stored" }), { stabilityMs: 0, followLinks: true });
|
|
117
|
+
expect(report.unreachable.map((u) => u.source.state)).toEqual(["unmounted"]);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("Finder aliases", () => {
|
|
122
|
+
const alias = (at: string): string => {
|
|
123
|
+
mkdirSync(join(at, ".."), { recursive: true });
|
|
124
|
+
writeFileSync(at, Buffer.concat([Buffer.from("book\0\0\0\0mark\0\0\0\0", "latin1"), Buffer.alloc(400, 1)]));
|
|
125
|
+
return at;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
test("the bookmark magic is an alias; content is not", async () => {
|
|
129
|
+
expect(await isFinderAlias(alias(join(root, "tod alias")), 416)).toBe(true);
|
|
130
|
+
expect(await isFinderAlias(write(join(root, "a.mp3"), 416), 416)).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("each alias becomes a symlink to its target; one that will not resolve, and a skipped folder, are left alone", async () => {
|
|
134
|
+
alias(join(root, "tod alias"));
|
|
135
|
+
alias(join(root, "nested", "offline alias"));
|
|
136
|
+
alias(join(root, "done", "old alias"));
|
|
137
|
+
const resolve = (path: string): string | null => (path.endsWith("tod alias") ? drive : null);
|
|
138
|
+
|
|
139
|
+
const result = await adoptFinderAliases(root, { resolve, skipTopDirs: ["done"] });
|
|
140
|
+
|
|
141
|
+
expect(result).toEqual({ linked: [`tod alias → ${drive}`], unresolved: ["nested/offline alias"] });
|
|
142
|
+
expect(readlinkSync(join(root, "tod alias"))).toBe(drive);
|
|
143
|
+
expect(lstatSync(join(root, "done", "old alias")).isFile()).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("a dry run says what it would do and changes nothing", async () => {
|
|
147
|
+
alias(join(root, "tod alias"));
|
|
148
|
+
const result = await adoptFinderAliases(root, { dryRun: true, resolve: () => drive });
|
|
149
|
+
expect(result.linked).toHaveLength(1);
|
|
150
|
+
expect(lstatSync(join(root, "tod alias")).isFile()).toBe(true);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("linkSource and the link beside the code", () => {
|
|
155
|
+
test("missing under a mounted volume is deleted; under an unmounted one, unmounted; off /Volumes, deleted", () => {
|
|
156
|
+
expect(linkSource(drive).state).toBe("ok");
|
|
157
|
+
expect(linkSource("/Volumes/X/a", () => true).state).toBe("deleted");
|
|
158
|
+
expect(linkSource("/Volumes/X/a", () => false)).toEqual({ state: "unmounted", target: "/Volumes/X/a", volume: "/Volumes/X" });
|
|
159
|
+
expect(linkSource(join(base, "gone")).state).toBe("deleted");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("ensureDropzoneLink makes the folder and the link, is quiet when right, and never replaces a real file", async () => {
|
|
163
|
+
const app = join(base, "app");
|
|
164
|
+
mkdirSync(app);
|
|
165
|
+
const target = join(base, "state", "inbox");
|
|
166
|
+
expect(await ensureDropzoneLink(app, target)).toContain("linked");
|
|
167
|
+
expect(readlinkSync(join(app, DROPZONE_LINK))).toBe(target);
|
|
168
|
+
expect(existsSync(target)).toBe(true);
|
|
169
|
+
expect(await ensureDropzoneLink(app, target)).toBeNull();
|
|
170
|
+
|
|
171
|
+
const other = join(base, "other");
|
|
172
|
+
mkdirSync(other);
|
|
173
|
+
write(join(other, DROPZONE_LINK));
|
|
174
|
+
expect(await ensureDropzoneLink(other, target)).toContain("left alone");
|
|
175
|
+
});
|
|
176
|
+
});
|