inkwire-receiver-core 0.2.0 → 1.0.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 +135 -17
- package/dist/auth.d.ts +18 -1
- package/dist/auth.js +30 -4
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +13 -2
- package/dist/files.d.ts +33 -0
- package/dist/files.js +210 -0
- package/dist/handle.d.ts +135 -10
- package/dist/handle.js +482 -51
- package/dist/index.d.ts +17 -4
- package/dist/index.js +10 -3
- package/dist/list.d.ts +19 -0
- package/dist/list.js +66 -0
- package/dist/lock.d.ts +6 -0
- package/dist/lock.js +17 -0
- package/dist/media-meta.d.ts +22 -0
- package/dist/media-meta.js +64 -0
- package/dist/media.d.ts +20 -0
- package/dist/media.js +125 -0
- package/dist/render.d.ts +18 -0
- package/dist/render.js +53 -4
- package/dist/schema.d.ts +214 -9
- package/dist/schema.js +86 -9
- package/dist/seo.d.ts +9 -0
- package/dist/seo.js +39 -0
- package/dist/store.d.ts +97 -34
- package/dist/store.js +1 -18
- package/dist/stores/file.d.ts +67 -0
- package/dist/stores/file.js +227 -0
- package/dist/stores/github.d.ts +94 -0
- package/dist/stores/github.js +334 -0
- package/dist/stores/memory.d.ts +27 -0
- package/dist/stores/memory.js +65 -0
- package/dist/testing.d.ts +49 -0
- package/dist/testing.js +223 -0
- package/package.json +47 -9
package/dist/handle.js
CHANGED
|
@@ -1,58 +1,489 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { authorize } from "./auth.js";
|
|
3
|
-
import { renderMarkdown, slugify } from "./render.js";
|
|
1
|
+
import { authorize, requireWrite } from "./auth.js";
|
|
4
2
|
import { InkwireError } from "./errors.js";
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { withLock } from "./lock.js";
|
|
4
|
+
import { assertImage, parseMultipart, safeFilename } from "./media.js";
|
|
5
|
+
import { renderMarkdown, slugify } from "./render.js";
|
|
6
|
+
import { ContentInput, MediaPatchSchema, MediaUrlInput, PatchSchema } from "./schema.js";
|
|
7
|
+
import { cleanOverrides, effectiveSeo, mergeOverrides } from "./seo.js";
|
|
8
|
+
export const INKWIRE_VERSION = "1";
|
|
9
|
+
export const MAX_PAGE = 100;
|
|
10
|
+
export const DEFAULT_PAGE = 20;
|
|
11
|
+
const DEFAULT_LIMITS = { markdown_bytes: 1_000_000, media_bytes: 10_000_000 };
|
|
12
|
+
const base = (extra = {}) => ({
|
|
13
|
+
"Inkwire-Version": INKWIRE_VERSION,
|
|
14
|
+
"Content-Type": "application/json",
|
|
15
|
+
...extra,
|
|
16
|
+
});
|
|
17
|
+
const ok = (body, extra = {}) => ({ status: 200, headers: base(extra), body });
|
|
18
|
+
function fail(e) {
|
|
19
|
+
if (e instanceof InkwireError) {
|
|
20
|
+
return { status: e.httpStatus, headers: base(), body: { error: { code: e.code, message: e.message || e.code } } };
|
|
21
|
+
}
|
|
22
|
+
// Anything else is a receiver/storage fault: say so without leaking what it was.
|
|
23
|
+
console.error("[inkwire] internal error:", e instanceof Error ? e.message : "unknown");
|
|
24
|
+
return { status: 500, headers: base(), body: { error: { code: "internal", message: "internal error" } } };
|
|
25
|
+
}
|
|
26
|
+
async function guarded(fn) {
|
|
7
27
|
try {
|
|
8
|
-
|
|
9
|
-
const parsed = PostInput.safeParse(input.rawBody);
|
|
10
|
-
if (!parsed.success) {
|
|
11
|
-
const first = parsed.error.issues[0];
|
|
12
|
-
throw new InkwireError("invalid_payload", `${first.path.join(".") || "body"}: ${first.message}`);
|
|
13
|
-
}
|
|
14
|
-
const p = parsed.data;
|
|
15
|
-
// Slug resolution + explicit-collision guard.
|
|
16
|
-
let slug;
|
|
17
|
-
if (p.slug) {
|
|
18
|
-
const owner = await input.store.findBySlug(key, p.slug);
|
|
19
|
-
if (owner && owner.external_id !== p.external_id)
|
|
20
|
-
throw new InkwireError("conflict", `slug '${p.slug}' is taken`);
|
|
21
|
-
slug = p.slug;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
const existing = await input.store.findByExternalId(key, p.external_id);
|
|
25
|
-
slug = existing?.slug ?? await dedupeSlug(input.store, key, slugify(p.title));
|
|
26
|
-
}
|
|
27
|
-
const html = renderMarkdown(p.markdown);
|
|
28
|
-
const seo = {
|
|
29
|
-
title: p.seo?.title ?? p.title,
|
|
30
|
-
description: p.seo?.description ?? (p.excerpt || p.title),
|
|
31
|
-
image_url: p.seo?.image_url ?? p.cover_image_url,
|
|
32
|
-
noindex: p.seo?.noindex ?? false,
|
|
33
|
-
};
|
|
34
|
-
const result = await input.store.upsert(key, {
|
|
35
|
-
external_id: p.external_id, title: p.title, html, markdown: p.markdown, slug,
|
|
36
|
-
excerpt: p.excerpt, tags: p.tags, cover_image_url: p.cover_image_url,
|
|
37
|
-
canonical_url: p.canonical_url, seo, author: p.author, status: p.status,
|
|
38
|
-
published_at: p.published_at ?? new Date().toISOString(),
|
|
39
|
-
});
|
|
40
|
-
return { status: 200, headers: HEADERS, body: {
|
|
41
|
-
id: result.id, external_id: p.external_id, url: result.url,
|
|
42
|
-
slug: result.slug, status: p.status, created: result.created,
|
|
43
|
-
} };
|
|
28
|
+
return await fn();
|
|
44
29
|
}
|
|
45
|
-
catch (
|
|
46
|
-
|
|
47
|
-
return { status: e.httpStatus, headers: HEADERS, body: { error: { code: e.code, message: e.message } } };
|
|
30
|
+
catch (e) {
|
|
31
|
+
return fail(e);
|
|
48
32
|
}
|
|
49
33
|
}
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
let
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
const limitsOf = (config) => ({ ...DEFAULT_LIMITS, ...(config?.limits ?? {}) });
|
|
35
|
+
function parseJson(rawBody) {
|
|
36
|
+
let v;
|
|
37
|
+
try {
|
|
38
|
+
v = JSON.parse(typeof rawBody === "string" ? rawBody : (rawBody ?? Buffer.alloc(0)).toString("utf8"));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw new InkwireError("invalid_payload", "the body is not valid JSON");
|
|
42
|
+
}
|
|
43
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) {
|
|
44
|
+
throw new InkwireError("invalid_payload", "the body must be a JSON object");
|
|
56
45
|
}
|
|
57
|
-
return
|
|
46
|
+
return v;
|
|
47
|
+
}
|
|
48
|
+
function zodMessage(e) {
|
|
49
|
+
return e.issues
|
|
50
|
+
.slice(0, 3)
|
|
51
|
+
.map((i) => {
|
|
52
|
+
const where = i.path.join(".");
|
|
53
|
+
if (i.code === "unrecognized_keys")
|
|
54
|
+
return `${where ? `${where}: ` : ""}unknown field ${(i.keys ?? []).join(", ")}`;
|
|
55
|
+
return where ? `${where}: ${i.message}` : i.message;
|
|
56
|
+
})
|
|
57
|
+
.join("; ");
|
|
58
|
+
}
|
|
59
|
+
function validate(schema, v) {
|
|
60
|
+
const r = schema.safeParse(v);
|
|
61
|
+
if (!r.success)
|
|
62
|
+
throw new InkwireError("invalid_payload", zodMessage(r.error));
|
|
63
|
+
return r.data;
|
|
64
|
+
}
|
|
65
|
+
const bytesOf = (s) => Buffer.byteLength(s, "utf8");
|
|
66
|
+
const quote = (etag) => `"${etag}"`;
|
|
67
|
+
function matchesIfMatch(header, etag) {
|
|
68
|
+
if (header === null || header === undefined)
|
|
69
|
+
return true;
|
|
70
|
+
const tokens = header.split(",").map((t) => t.trim().replace(/^W\//, "").replace(/^"(.*)"$/, "$1"));
|
|
71
|
+
return tokens.includes("*") || tokens.includes(etag);
|
|
72
|
+
}
|
|
73
|
+
function checkIfMatch(header, entry) {
|
|
74
|
+
if (!matchesIfMatch(header, entry.etag)) {
|
|
75
|
+
throw new InkwireError("stale", "the entry changed since you read it (If-Match no longer matches); read it again and retry");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function summary(e) {
|
|
79
|
+
return {
|
|
80
|
+
id: e.id,
|
|
81
|
+
external_id: e.external_id,
|
|
82
|
+
kind: e.kind,
|
|
83
|
+
title: e.title,
|
|
84
|
+
slug: e.slug,
|
|
85
|
+
url: e.url,
|
|
86
|
+
status: e.status,
|
|
87
|
+
...(e.excerpt !== undefined ? { excerpt: e.excerpt } : {}),
|
|
88
|
+
...(e.tags !== undefined ? { tags: e.tags } : {}),
|
|
89
|
+
...(e.categories !== undefined ? { categories: e.categories } : {}),
|
|
90
|
+
published_at: e.published_at,
|
|
91
|
+
updated_at: e.updated_at,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function full(e) {
|
|
95
|
+
return {
|
|
96
|
+
...summary(e),
|
|
97
|
+
markdown: e.markdown,
|
|
98
|
+
...(e.cover_image_url !== undefined ? { cover_image_url: e.cover_image_url } : {}),
|
|
99
|
+
...(e.cover_image_alt !== undefined ? { cover_image_alt: e.cover_image_alt } : {}),
|
|
100
|
+
...(e.canonical_url !== undefined ? { canonical_url: e.canonical_url } : {}),
|
|
101
|
+
...(e.author !== undefined ? { author: e.author } : {}),
|
|
102
|
+
seo: effectiveSeo(e),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const etagHeader = (e) => ({ ETag: quote(e.etag) });
|
|
106
|
+
function requireKind(config, kind) {
|
|
107
|
+
if (!config.kinds.includes(kind)) {
|
|
108
|
+
throw new InkwireError("invalid_payload", `kind "${kind}" is not held by this receiver (it holds: ${config.kinds.join(", ")})`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function slugOwner(store, scope, kind, slug) {
|
|
112
|
+
return (await store.list(scope, { kind, slug, limit: 1 })).items[0];
|
|
113
|
+
}
|
|
114
|
+
/** `base`, `base-2`, `base-3`… the first one no other entry of that kind owns. */
|
|
115
|
+
async function freeSlug(store, scope, kind, wanted, selfId) {
|
|
116
|
+
for (let n = 1; n <= 500; n++) {
|
|
117
|
+
const suffix = n === 1 ? "" : `-${n}`;
|
|
118
|
+
const candidate = wanted.slice(0, 255 - suffix.length).replace(/-+$/g, "") + suffix;
|
|
119
|
+
const owner = await slugOwner(store, scope, kind, candidate);
|
|
120
|
+
if (!owner || owner.id === selfId)
|
|
121
|
+
return candidate;
|
|
122
|
+
}
|
|
123
|
+
return `${wanted.slice(0, 240).replace(/-+$/g, "")}-${Math.random().toString(36).slice(2, 8)}`;
|
|
124
|
+
}
|
|
125
|
+
async function assertSlugFree(store, scope, kind, slug, selfId) {
|
|
126
|
+
const owner = await slugOwner(store, scope, kind, slug);
|
|
127
|
+
if (owner && owner.id !== selfId) {
|
|
128
|
+
throw new InkwireError("conflict", `slug "${slug}" is already used by another ${kind}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function parseLimit(query, fallback, max) {
|
|
132
|
+
const raw = query.get("limit");
|
|
133
|
+
if (raw === null || raw === "")
|
|
134
|
+
return fallback;
|
|
135
|
+
const n = Number(raw);
|
|
136
|
+
if (!Number.isInteger(n) || n < 1)
|
|
137
|
+
throw new InkwireError("invalid_payload", "limit must be a positive integer");
|
|
138
|
+
return Math.min(n, max);
|
|
139
|
+
}
|
|
140
|
+
const authedWrite = (authHeader, keys) => {
|
|
141
|
+
const key = authorize(authHeader, keys);
|
|
142
|
+
requireWrite(key);
|
|
143
|
+
return key;
|
|
144
|
+
};
|
|
145
|
+
export function handleUpsert({ authHeader, rawBody, keys, store, config }) {
|
|
146
|
+
return guarded(async () => {
|
|
147
|
+
const key = authedWrite(authHeader, keys);
|
|
148
|
+
const limits = limitsOf(config);
|
|
149
|
+
// JSON escapes can make a body up to ~6x its text: refuse the absurd before parsing it.
|
|
150
|
+
if (rawBody.length > limits.markdown_bytes * 6 + 100_000) {
|
|
151
|
+
throw new InkwireError("too_large", `the body is over the limit (markdown_bytes ${limits.markdown_bytes})`);
|
|
152
|
+
}
|
|
153
|
+
const input = validate(ContentInput, parseJson(rawBody));
|
|
154
|
+
requireKind(config, input.kind);
|
|
155
|
+
if (bytesOf(input.markdown) > limits.markdown_bytes) {
|
|
156
|
+
throw new InkwireError("too_large", `markdown is over the limit (markdown_bytes ${limits.markdown_bytes})`);
|
|
157
|
+
}
|
|
158
|
+
const scope = key.scope;
|
|
159
|
+
return withLock(scope, async () => {
|
|
160
|
+
const existing = (await store.list(scope, { kind: input.kind, external_id: input.external_id, limit: 1 })).items[0];
|
|
161
|
+
let slug;
|
|
162
|
+
if (input.slug) {
|
|
163
|
+
await assertSlugFree(store, scope, input.kind, input.slug, existing?.id);
|
|
164
|
+
slug = input.slug;
|
|
165
|
+
}
|
|
166
|
+
else if (existing) {
|
|
167
|
+
slug = existing.slug;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
slug = await freeSlug(store, scope, input.kind, slugify(input.title));
|
|
171
|
+
}
|
|
172
|
+
const now = new Date().toISOString();
|
|
173
|
+
const entry = {
|
|
174
|
+
...(existing ? { id: existing.id } : {}),
|
|
175
|
+
key: scope,
|
|
176
|
+
external_id: input.external_id,
|
|
177
|
+
kind: input.kind,
|
|
178
|
+
title: input.title,
|
|
179
|
+
slug,
|
|
180
|
+
markdown: input.markdown,
|
|
181
|
+
html: renderMarkdown(input.markdown),
|
|
182
|
+
...(input.excerpt !== undefined ? { excerpt: input.excerpt } : {}),
|
|
183
|
+
...(input.tags !== undefined ? { tags: input.tags } : {}),
|
|
184
|
+
...(input.categories !== undefined ? { categories: input.categories } : {}),
|
|
185
|
+
...(input.cover_image_url !== undefined ? { cover_image_url: input.cover_image_url } : {}),
|
|
186
|
+
...(input.cover_image_alt !== undefined ? { cover_image_alt: input.cover_image_alt } : {}),
|
|
187
|
+
...(input.canonical_url !== undefined ? { canonical_url: input.canonical_url } : {}),
|
|
188
|
+
seo: cleanOverrides(input.seo),
|
|
189
|
+
...(input.author !== undefined ? { author: input.author } : {}),
|
|
190
|
+
status: input.status,
|
|
191
|
+
published_at: input.published_at ?? existing?.published_at ?? now,
|
|
192
|
+
updated_at: now,
|
|
193
|
+
version: (existing?.version ?? 0) + 1,
|
|
194
|
+
};
|
|
195
|
+
const saved = await store.save(scope, entry);
|
|
196
|
+
return ok({
|
|
197
|
+
id: saved.id,
|
|
198
|
+
external_id: saved.external_id,
|
|
199
|
+
url: saved.url,
|
|
200
|
+
slug: saved.slug,
|
|
201
|
+
kind: saved.kind,
|
|
202
|
+
status: saved.status,
|
|
203
|
+
created: !existing,
|
|
204
|
+
}, etagHeader(saved));
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
export function handleList({ authHeader, query, keys, store }) {
|
|
209
|
+
return guarded(async () => {
|
|
210
|
+
const key = authorize(authHeader, keys);
|
|
211
|
+
const status = query.get("status") || undefined;
|
|
212
|
+
if (status !== undefined && status !== "draft" && status !== "published") {
|
|
213
|
+
throw new InkwireError("invalid_payload", 'status must be "draft" or "published"');
|
|
214
|
+
}
|
|
215
|
+
const limit = parseLimit(query, DEFAULT_PAGE, MAX_PAGE);
|
|
216
|
+
const result = await store.list(key.scope, {
|
|
217
|
+
...(status ? { status } : {}),
|
|
218
|
+
...(query.get("kind") ? { kind: query.get("kind") } : {}),
|
|
219
|
+
...(query.get("tag") ? { tag: query.get("tag") } : {}),
|
|
220
|
+
...(query.get("category") ? { category: query.get("category") } : {}),
|
|
221
|
+
...(query.get("search") ? { search: query.get("search") } : {}),
|
|
222
|
+
...(query.get("external_id") ? { external_id: query.get("external_id") } : {}),
|
|
223
|
+
...(query.get("slug") ? { slug: query.get("slug") } : {}),
|
|
224
|
+
limit,
|
|
225
|
+
...(query.get("cursor") ? { cursor: query.get("cursor") } : {}),
|
|
226
|
+
});
|
|
227
|
+
return ok({ items: result.items.map(summary), next_cursor: result.next_cursor });
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
export function handleGet({ authHeader, id, keys, store }) {
|
|
231
|
+
return guarded(async () => {
|
|
232
|
+
const key = authorize(authHeader, keys);
|
|
233
|
+
const entry = await store.get(key.scope, id);
|
|
234
|
+
if (!entry)
|
|
235
|
+
throw new InkwireError("not_found", `no entry with id "${id}"`);
|
|
236
|
+
return ok(full(entry), etagHeader(entry));
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
export function handlePatch({ authHeader, id, rawBody, ifMatchHeader, keys, store, config }) {
|
|
240
|
+
return guarded(async () => {
|
|
241
|
+
const key = authedWrite(authHeader, keys);
|
|
242
|
+
const limits = limitsOf(config);
|
|
243
|
+
if (rawBody.length > limits.markdown_bytes * 6 + 100_000) {
|
|
244
|
+
throw new InkwireError("too_large", `the body is over the limit (markdown_bytes ${limits.markdown_bytes})`);
|
|
245
|
+
}
|
|
246
|
+
const raw = parseJson(rawBody);
|
|
247
|
+
if ("external_id" in raw)
|
|
248
|
+
throw new InkwireError("invalid_payload", "external_id cannot be changed");
|
|
249
|
+
const patch = validate(PatchSchema, raw);
|
|
250
|
+
if (patch.kind !== undefined)
|
|
251
|
+
requireKind(config, patch.kind);
|
|
252
|
+
if (patch.markdown !== undefined && bytesOf(patch.markdown) > limits.markdown_bytes) {
|
|
253
|
+
throw new InkwireError("too_large", `markdown is over the limit (markdown_bytes ${limits.markdown_bytes})`);
|
|
254
|
+
}
|
|
255
|
+
const scope = key.scope;
|
|
256
|
+
return withLock(scope, async () => {
|
|
257
|
+
const current = await store.get(scope, id);
|
|
258
|
+
if (!current)
|
|
259
|
+
throw new InkwireError("not_found", `no entry with id "${id}"`);
|
|
260
|
+
checkIfMatch(ifMatchHeader, current);
|
|
261
|
+
const now = new Date().toISOString();
|
|
262
|
+
const kind = patch.kind ?? current.kind;
|
|
263
|
+
let slug = patch.slug ?? current.slug;
|
|
264
|
+
if (patch.slug !== undefined) {
|
|
265
|
+
if (patch.slug !== current.slug || kind !== current.kind)
|
|
266
|
+
await assertSlugFree(store, scope, kind, patch.slug, current.id);
|
|
267
|
+
}
|
|
268
|
+
else if (kind !== current.kind) {
|
|
269
|
+
slug = await freeSlug(store, scope, kind, current.slug, current.id);
|
|
270
|
+
}
|
|
271
|
+
const next = {
|
|
272
|
+
id: current.id,
|
|
273
|
+
key: scope,
|
|
274
|
+
external_id: current.external_id,
|
|
275
|
+
kind,
|
|
276
|
+
title: patch.title ?? current.title,
|
|
277
|
+
slug,
|
|
278
|
+
markdown: patch.markdown ?? current.markdown,
|
|
279
|
+
html: patch.markdown !== undefined ? renderMarkdown(patch.markdown) : current.html,
|
|
280
|
+
seo: patch.seo !== undefined ? mergeOverrides(current.seo, patch.seo) : current.seo,
|
|
281
|
+
status: patch.status ?? current.status,
|
|
282
|
+
published_at: patch.published_at === null ? now : (patch.published_at ?? current.published_at),
|
|
283
|
+
updated_at: now,
|
|
284
|
+
version: current.version + 1,
|
|
285
|
+
};
|
|
286
|
+
const optional = ["excerpt", "tags", "categories", "cover_image_url", "cover_image_alt", "canonical_url", "author"];
|
|
287
|
+
for (const f of optional) {
|
|
288
|
+
const v = f in patch ? patch[f] : current[f];
|
|
289
|
+
if (v !== null && v !== undefined)
|
|
290
|
+
next[f] = v;
|
|
291
|
+
}
|
|
292
|
+
const saved = await store.save(scope, next);
|
|
293
|
+
return ok(full(saved), etagHeader(saved));
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
export function handleDelete({ authHeader, id, ifMatchHeader, keys, store }) {
|
|
298
|
+
return guarded(async () => {
|
|
299
|
+
const key = authedWrite(authHeader, keys);
|
|
300
|
+
return withLock(key.scope, async () => {
|
|
301
|
+
const current = await store.get(key.scope, id);
|
|
302
|
+
if (!current)
|
|
303
|
+
throw new InkwireError("not_found", `no entry with id "${id}"`);
|
|
304
|
+
checkIfMatch(ifMatchHeader, current);
|
|
305
|
+
const removed = await store.remove(key.scope, current.id);
|
|
306
|
+
if (!removed)
|
|
307
|
+
throw new InkwireError("not_found", `no entry with id "${id}"`);
|
|
308
|
+
return ok({ id: current.id, deleted: true });
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
export function handleMedia({ authHeader, contentType, rawBody, keys, config, store }) {
|
|
313
|
+
return guarded(async () => {
|
|
314
|
+
const key = authedWrite(authHeader, keys);
|
|
315
|
+
if (!store.saveMedia)
|
|
316
|
+
throw new InkwireError("not_found", "this receiver does not accept media uploads");
|
|
317
|
+
const limit = limitsOf(config).media_bytes;
|
|
318
|
+
const body = typeof rawBody === "string" ? Buffer.from(rawBody, "utf8") : Buffer.from(rawBody);
|
|
319
|
+
const type = (contentType ?? "").toLowerCase();
|
|
320
|
+
const tooLarge = () => new InkwireError("too_large", `the file is over the limit (media_bytes ${limit})`);
|
|
321
|
+
let bytes;
|
|
322
|
+
let declaredType;
|
|
323
|
+
let filename;
|
|
324
|
+
let alt;
|
|
325
|
+
if (type.startsWith("multipart/form-data")) {
|
|
326
|
+
if (body.length > limit + 65_536)
|
|
327
|
+
throw tooLarge();
|
|
328
|
+
const parts = parseMultipart(body, contentType);
|
|
329
|
+
const file = parts.find((p) => p.name === "file" && p.filename !== undefined);
|
|
330
|
+
if (!file)
|
|
331
|
+
throw new InkwireError("invalid_payload", 'the multipart body needs a "file" part');
|
|
332
|
+
bytes = file.data;
|
|
333
|
+
declaredType = file.contentType;
|
|
334
|
+
filename = file.filename;
|
|
335
|
+
const altPart = parts.find((p) => p.name === "alt" && p.filename === undefined);
|
|
336
|
+
if (altPart)
|
|
337
|
+
alt = altPart.data.toString("utf8");
|
|
338
|
+
}
|
|
339
|
+
else if (type.startsWith("application/json")) {
|
|
340
|
+
const input = validate(MediaUrlInput, parseJson(body));
|
|
341
|
+
if (!config.fetchMedia) {
|
|
342
|
+
throw new InkwireError("invalid_payload", "fetching media from a URL is not enabled on this receiver; upload the file instead");
|
|
343
|
+
}
|
|
344
|
+
const got = await config.fetchMedia(input.url);
|
|
345
|
+
bytes = Buffer.from(got.bytes);
|
|
346
|
+
declaredType = got.contentType;
|
|
347
|
+
filename = got.filename ?? decodeURIComponent(new URL(input.url).pathname.split("/").pop() ?? "");
|
|
348
|
+
alt = input.alt;
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
throw new InkwireError("invalid_payload", "send multipart/form-data with a file part, or JSON {url, alt}");
|
|
352
|
+
}
|
|
353
|
+
if (bytes.length > limit)
|
|
354
|
+
throw tooLarge();
|
|
355
|
+
if (bytes.length === 0)
|
|
356
|
+
throw new InkwireError("invalid_payload", "the file is empty");
|
|
357
|
+
if (alt !== undefined && alt.length > 1024)
|
|
358
|
+
throw new InkwireError("invalid_payload", "alt is over 1024 characters");
|
|
359
|
+
const imageType = assertImage(bytes, declaredType, filename);
|
|
360
|
+
const saved = await store.saveMedia(key.scope, {
|
|
361
|
+
bytes,
|
|
362
|
+
contentType: imageType,
|
|
363
|
+
filename: safeFilename(filename, imageType),
|
|
364
|
+
...(alt !== undefined && alt !== "" ? { alt } : {}),
|
|
365
|
+
});
|
|
366
|
+
return ok(mediaItem(saved));
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
const mediaItem = (m) => ({
|
|
370
|
+
id: m.id,
|
|
371
|
+
url: m.url,
|
|
372
|
+
...(m.alt !== undefined ? { alt: m.alt } : {}),
|
|
373
|
+
...(m.title !== undefined ? { title: m.title } : {}),
|
|
374
|
+
mime_type: m.mime_type,
|
|
375
|
+
created_at: m.created_at,
|
|
376
|
+
});
|
|
377
|
+
export function handleMediaList({ authHeader, query, keys, store }) {
|
|
378
|
+
return guarded(async () => {
|
|
379
|
+
const key = authorize(authHeader, keys);
|
|
380
|
+
if (!store.listMedia)
|
|
381
|
+
throw new InkwireError("not_found", "this receiver does not list its media library");
|
|
382
|
+
const limit = parseLimit(query, DEFAULT_PAGE, MAX_PAGE);
|
|
383
|
+
const result = await store.listMedia(key.scope, {
|
|
384
|
+
...(query.get("search") ? { search: query.get("search") } : {}),
|
|
385
|
+
limit,
|
|
386
|
+
...(query.get("cursor") ? { cursor: query.get("cursor") } : {}),
|
|
387
|
+
});
|
|
388
|
+
return ok({ items: result.items.map(mediaItem), next_cursor: result.next_cursor });
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
export function handleMediaUpdate({ authHeader, id, rawBody, keys, store }) {
|
|
392
|
+
return guarded(async () => {
|
|
393
|
+
const key = authedWrite(authHeader, keys);
|
|
394
|
+
if (!store.updateMedia)
|
|
395
|
+
throw new InkwireError("not_found", "this receiver does not edit its media library");
|
|
396
|
+
if (rawBody.length > 100_000)
|
|
397
|
+
throw new InkwireError("too_large", "the body is over the limit");
|
|
398
|
+
const patch = validate(MediaPatchSchema, parseJson(rawBody));
|
|
399
|
+
const saved = await store.updateMedia(key.scope, id, patch);
|
|
400
|
+
if (!saved)
|
|
401
|
+
throw new InkwireError("not_found", `no media with id "${id}"`);
|
|
402
|
+
return ok(mediaItem(saved));
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
406
|
+
const MAX_TERMS = 500;
|
|
407
|
+
const DEFAULT_TERMS = 100;
|
|
408
|
+
/** Entries read to count terms; a site past this reports counts for its newest entries. */
|
|
409
|
+
const TERMS_SCAN_CAP = 10_000;
|
|
410
|
+
export function handleTerms({ authHeader, query, keys, store }) {
|
|
411
|
+
return guarded(async () => {
|
|
412
|
+
const key = authorize(authHeader, keys);
|
|
413
|
+
const kind = query.get("kind") || undefined;
|
|
414
|
+
if (kind !== undefined && kind !== "tag" && kind !== "category") {
|
|
415
|
+
throw new InkwireError("invalid_payload", 'kind must be "tag" or "category"');
|
|
416
|
+
}
|
|
417
|
+
const limit = parseLimit(query, DEFAULT_TERMS, MAX_TERMS);
|
|
418
|
+
const search = (query.get("search") ?? "").toLowerCase();
|
|
419
|
+
// name (lowercased) -> total, and how often each spelling appears.
|
|
420
|
+
const seen = new Map();
|
|
421
|
+
const add = (k, names) => {
|
|
422
|
+
const byLower = new Map();
|
|
423
|
+
for (const n of names ?? [])
|
|
424
|
+
if (!byLower.has(n.toLowerCase()))
|
|
425
|
+
byLower.set(n.toLowerCase(), n);
|
|
426
|
+
for (const name of byLower.values()) {
|
|
427
|
+
const id = `${k}:${name.toLowerCase()}`;
|
|
428
|
+
const t = seen.get(id) ?? { kind: k, total: 0, spellings: new Map() };
|
|
429
|
+
t.total++;
|
|
430
|
+
t.spellings.set(name, (t.spellings.get(name) ?? 0) + 1);
|
|
431
|
+
seen.set(id, t);
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
let cursor;
|
|
435
|
+
let scanned = 0;
|
|
436
|
+
do {
|
|
437
|
+
const page = await store.list(key.scope, { limit: MAX_PAGE, ...(cursor ? { cursor } : {}) });
|
|
438
|
+
for (const e of page.items) {
|
|
439
|
+
if (kind !== "category")
|
|
440
|
+
add("tag", e.tags);
|
|
441
|
+
if (kind !== "tag")
|
|
442
|
+
add("category", e.categories);
|
|
443
|
+
}
|
|
444
|
+
scanned += page.items.length;
|
|
445
|
+
cursor = page.next_cursor ?? undefined;
|
|
446
|
+
} while (cursor && scanned < TERMS_SCAN_CAP);
|
|
447
|
+
const items = [...seen.values()]
|
|
448
|
+
.map((t) => {
|
|
449
|
+
const name = [...t.spellings.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))[0][0];
|
|
450
|
+
return { kind: t.kind, name, slug: slugify(name), count: t.total };
|
|
451
|
+
})
|
|
452
|
+
.filter((t) => !search || t.name.toLowerCase().includes(search))
|
|
453
|
+
.sort((a, b) => b.count - a.count || cmp(a.name.toLowerCase(), b.name.toLowerCase()) || cmp(a.name, b.name))
|
|
454
|
+
.slice(0, limit);
|
|
455
|
+
return ok({ items });
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
// ---------------------------------------------------------------------------------------------
|
|
459
|
+
// GET /api/inkwire
|
|
460
|
+
export function discoveryBody(config, store) {
|
|
461
|
+
const derived = [
|
|
462
|
+
"upsert",
|
|
463
|
+
"list",
|
|
464
|
+
"get",
|
|
465
|
+
"update",
|
|
466
|
+
"delete",
|
|
467
|
+
"terms",
|
|
468
|
+
...(config.schedule ? ["schedule"] : []),
|
|
469
|
+
...(store?.saveMedia ? ["media"] : []),
|
|
470
|
+
...(store?.saveMedia && config.fetchMedia ? ["media_url"] : []),
|
|
471
|
+
...(store?.listMedia ? ["media_list"] : []),
|
|
472
|
+
...(store?.updateMedia ? ["media_update"] : []),
|
|
473
|
+
];
|
|
474
|
+
const features = [...new Set(["upsert", ...(config.features ?? derived)])];
|
|
475
|
+
return {
|
|
476
|
+
inkwire: 1,
|
|
477
|
+
site: config.site,
|
|
478
|
+
features,
|
|
479
|
+
kinds: config.kinds,
|
|
480
|
+
limits: limitsOf(config),
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
/** The authenticated discovery response. Adapters may also call `discoveryBody` directly. */
|
|
484
|
+
export function handleDiscovery({ authHeader, keys, config, store }) {
|
|
485
|
+
return guarded(async () => {
|
|
486
|
+
authorize(authHeader, keys);
|
|
487
|
+
return ok(discoveryBody(config, store));
|
|
488
|
+
});
|
|
58
489
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
1
|
+
export { handleUpsert, handleList, handleGet, handlePatch, handleDelete, handleMedia, handleMediaList, handleMediaUpdate, handleTerms, handleDiscovery, discoveryBody, INKWIRE_VERSION, } from "./handle.js";
|
|
2
|
+
export type { InkwireConfig, HandlerResult, FetchedMedia, UpsertArgs, ListArgs, GetArgs, PatchArgs, DeleteArgs, MediaArgs, MediaListArgs, MediaUpdateArgs, TermsArgs, DiscoveryArgs, } from "./handle.js";
|
|
3
|
+
export { parseApiKeys, authorize, requireWrite, DEFAULT_SCOPE } from "./auth.js";
|
|
4
|
+
export type { ApiKey } from "./auth.js";
|
|
4
5
|
export { InkwireError } from "./errors.js";
|
|
5
|
-
export {
|
|
6
|
+
export type { InkwireCode } from "./errors.js";
|
|
7
|
+
export { renderMarkdown, slugify, edits, ALLOWED_TAGS, ALLOWED_ATTRIBUTES, ALLOWED_SCHEMES } from "./render.js";
|
|
8
|
+
export { ContentInput, PatchSchema, MediaPatchSchema } from "./schema.js";
|
|
9
|
+
export { sniffImage, assertImage, parseMultipart, safeFilename } from "./media.js";
|
|
10
|
+
export { effectiveSeo } from "./seo.js";
|
|
11
|
+
export type { ContentStore, StoredContent, SaveEntry, UpsertInput, PatchInput, ListFilter, ListResult, SeoMetadata, SeoOverrides, StoredMedia, MediaInput, MediaListFilter, MediaListResult, } from "./store.js";
|
|
12
|
+
export { matches, mediaMatches, pageMedia, pageOf, recencyKey, encodeCursor, decodeCursor } from "./list.js";
|
|
13
|
+
export { MemoryStore } from "./stores/memory.js";
|
|
14
|
+
export type { MemoryStoreOptions } from "./stores/memory.js";
|
|
15
|
+
export { FileStore } from "./stores/file.js";
|
|
16
|
+
export type { FileStoreOptions } from "./stores/file.js";
|
|
17
|
+
export { GitHubStore } from "./stores/github.js";
|
|
18
|
+
export type { GitHubStoreOptions } from "./stores/github.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { handleUpsert, handleList, handleGet, handlePatch, handleDelete, handleMedia, handleMediaList, handleMediaUpdate, handleTerms, handleDiscovery, discoveryBody, INKWIRE_VERSION, } from "./handle.js";
|
|
2
|
+
export { parseApiKeys, authorize, requireWrite, DEFAULT_SCOPE } from "./auth.js";
|
|
3
3
|
export { InkwireError } from "./errors.js";
|
|
4
|
-
export { renderMarkdown, slugify } from "./render.js";
|
|
4
|
+
export { renderMarkdown, slugify, edits, ALLOWED_TAGS, ALLOWED_ATTRIBUTES, ALLOWED_SCHEMES } from "./render.js";
|
|
5
|
+
export { ContentInput, PatchSchema, MediaPatchSchema } from "./schema.js";
|
|
6
|
+
export { sniffImage, assertImage, parseMultipart, safeFilename } from "./media.js";
|
|
7
|
+
export { effectiveSeo } from "./seo.js";
|
|
8
|
+
export { matches, mediaMatches, pageMedia, pageOf, recencyKey, encodeCursor, decodeCursor } from "./list.js";
|
|
9
|
+
export { MemoryStore } from "./stores/memory.js";
|
|
10
|
+
export { FileStore } from "./stores/file.js";
|
|
11
|
+
export { GitHubStore } from "./stores/github.js";
|
package/dist/list.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ListFilter, MediaListFilter, MediaListResult, StoredContent, StoredMedia } from "./store.js";
|
|
2
|
+
/** The filter semantics every store implements (status, kind, tag, search, external_id, slug). */
|
|
3
|
+
export declare function matches(e: StoredContent, f: ListFilter): boolean;
|
|
4
|
+
/** The media filter semantics: alt, title and filename, case-insensitive. */
|
|
5
|
+
export declare function mediaMatches(m: StoredMedia, search: string | undefined): boolean;
|
|
6
|
+
export declare function encodeCursor(k: string): string;
|
|
7
|
+
export declare function decodeCursor(cursor: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Keyset page over `items` already sorted ascending by `keyOf`: the page starts after the cursor's
|
|
10
|
+
* key, so an entry added or removed between pages never repeats or skips its neighbours.
|
|
11
|
+
*/
|
|
12
|
+
export declare function pageOf<T>(items: T[], keyOf: (t: T) => string, limit: number, cursor?: string): {
|
|
13
|
+
items: T[];
|
|
14
|
+
next_cursor: string | null;
|
|
15
|
+
};
|
|
16
|
+
/** Newest first, then id: a sort key that works as a plain ascending string. */
|
|
17
|
+
export declare function recencyKey(e: Pick<StoredContent, "updated_at" | "id"> | Pick<StoredMedia, "created_at" | "id">): string;
|
|
18
|
+
/** Filter, order (newest first) and page an in-memory set of media: what stores without a query language use. */
|
|
19
|
+
export declare function pageMedia(all: StoredMedia[], f: MediaListFilter): MediaListResult;
|
package/dist/list.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { InkwireError } from "./errors.js";
|
|
2
|
+
/** The filter semantics every store implements (status, kind, tag, search, external_id, slug). */
|
|
3
|
+
export function matches(e, f) {
|
|
4
|
+
if (f.status && e.status !== f.status)
|
|
5
|
+
return false;
|
|
6
|
+
if (f.kind && e.kind !== f.kind)
|
|
7
|
+
return false;
|
|
8
|
+
if (f.slug && e.slug !== f.slug)
|
|
9
|
+
return false;
|
|
10
|
+
if (f.external_id !== undefined && e.external_id !== f.external_id)
|
|
11
|
+
return false;
|
|
12
|
+
if (f.tag && !(e.tags ?? []).includes(f.tag))
|
|
13
|
+
return false;
|
|
14
|
+
if (f.category && !(e.categories ?? []).includes(f.category))
|
|
15
|
+
return false;
|
|
16
|
+
if (f.search) {
|
|
17
|
+
const q = f.search.toLowerCase();
|
|
18
|
+
const hay = `${e.title}\n${e.excerpt ?? ""}\n${e.markdown}`.toLowerCase();
|
|
19
|
+
if (!hay.includes(q))
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
/** The media filter semantics: alt, title and filename, case-insensitive. */
|
|
25
|
+
export function mediaMatches(m, search) {
|
|
26
|
+
if (!search)
|
|
27
|
+
return true;
|
|
28
|
+
const q = search.toLowerCase();
|
|
29
|
+
const filename = decodeURIComponent(m.url.split("/").pop() ?? "");
|
|
30
|
+
return `${m.alt ?? ""}\n${m.title ?? ""}\n${filename}`.toLowerCase().includes(q);
|
|
31
|
+
}
|
|
32
|
+
export function encodeCursor(k) {
|
|
33
|
+
return Buffer.from(JSON.stringify({ k })).toString("base64url");
|
|
34
|
+
}
|
|
35
|
+
export function decodeCursor(cursor) {
|
|
36
|
+
try {
|
|
37
|
+
const v = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
38
|
+
if (typeof v?.k === "string")
|
|
39
|
+
return v.k;
|
|
40
|
+
}
|
|
41
|
+
catch { }
|
|
42
|
+
throw new InkwireError("invalid_payload", "cursor: not a cursor this receiver issued");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Keyset page over `items` already sorted ascending by `keyOf`: the page starts after the cursor's
|
|
46
|
+
* key, so an entry added or removed between pages never repeats or skips its neighbours.
|
|
47
|
+
*/
|
|
48
|
+
export function pageOf(items, keyOf, limit, cursor) {
|
|
49
|
+
const after = cursor ? decodeCursor(cursor) : null;
|
|
50
|
+
const rest = after === null ? items : items.filter((t) => keyOf(t) > after);
|
|
51
|
+
const page = rest.slice(0, limit);
|
|
52
|
+
const more = rest.length > page.length;
|
|
53
|
+
return { items: page, next_cursor: more && page.length ? encodeCursor(keyOf(page[page.length - 1])) : null };
|
|
54
|
+
}
|
|
55
|
+
/** Newest first, then id: a sort key that works as a plain ascending string. */
|
|
56
|
+
export function recencyKey(e) {
|
|
57
|
+
const t = Date.parse("updated_at" in e ? e.updated_at : e.created_at);
|
|
58
|
+
const inverted = (8.64e15 - (Number.isNaN(t) ? 0 : t)).toString().padStart(17, "0");
|
|
59
|
+
return `${inverted}|${e.id}`;
|
|
60
|
+
}
|
|
61
|
+
/** Filter, order (newest first) and page an in-memory set of media: what stores without a query language use. */
|
|
62
|
+
export function pageMedia(all, f) {
|
|
63
|
+
const found = all.filter((m) => mediaMatches(m, f.search));
|
|
64
|
+
found.sort((a, b) => (recencyKey(a) < recencyKey(b) ? -1 : 1));
|
|
65
|
+
return pageOf(found, recencyKey, f.limit, f.cursor);
|
|
66
|
+
}
|
package/dist/lock.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` after every earlier call with the same `key` has finished, so a read-check-write (an
|
|
3
|
+
* If-Match test, a slug check) cannot interleave with another write in this process. One process
|
|
4
|
+
* only: a store shared by several processes needs its own conditional write.
|
|
5
|
+
*/
|
|
6
|
+
export declare function withLock<T>(key: string, fn: () => Promise<T>): Promise<T>;
|