@mccaskie-labs/workbench-library 0.0.0-stage → 0.2.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/CHANGELOG.md +11 -0
- package/README.md +62 -2
- package/dist/access.d.ts +7 -0
- package/dist/access.js +46 -0
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +142 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +84 -0
- package/dist/embed.d.ts +6 -0
- package/dist/embed.js +53 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +11 -0
- package/dist/feed.d.ts +14 -0
- package/dist/feed.js +74 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +16 -0
- package/dist/library.d.ts +156 -0
- package/dist/library.js +422 -0
- package/dist/links.d.ts +37 -0
- package/dist/links.js +68 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +619 -0
- package/dist/sponsors.d.ts +3 -0
- package/dist/sponsors.js +8 -0
- package/dist/starters.d.ts +20 -0
- package/dist/starters.js +16 -0
- package/dist/styles.d.ts +33 -0
- package/dist/styles.js +86 -0
- package/dist/text.d.ts +3 -0
- package/dist/text.js +26 -0
- package/dist/time.d.ts +6 -0
- package/dist/time.js +36 -0
- package/dist/types.d.ts +245 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +42 -0
- package/dist/ui.js +148 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +128 -0
- package/package.json +52 -4
package/dist/links.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Short-lived signed links to gated files, and serving a file from the app's
|
|
2
|
+
// own R2 bucket with Range support (so audio can be skipped through).
|
|
3
|
+
//
|
|
4
|
+
// A link is <files path>/<file id>?e=<unix seconds>&s=<signature>, signed
|
|
5
|
+
// with HMAC-SHA-256 and LIBRARY_SECRET over the id and the expiry. A file on
|
|
6
|
+
// an item everyone may see has no e or s; serveFile then asks the database
|
|
7
|
+
// whether it is public at that moment.
|
|
8
|
+
const enc = new TextEncoder();
|
|
9
|
+
async function hmac(secret, message) {
|
|
10
|
+
const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
11
|
+
const bytes = new Uint8Array(await crypto.subtle.sign("HMAC", key, enc.encode(message)));
|
|
12
|
+
let out = "";
|
|
13
|
+
for (const b of bytes.slice(0, 24))
|
|
14
|
+
out += String.fromCharCode(b);
|
|
15
|
+
return btoa(out).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
16
|
+
}
|
|
17
|
+
function same(a, b) {
|
|
18
|
+
if (a.length !== b.length)
|
|
19
|
+
return false;
|
|
20
|
+
let d = 0;
|
|
21
|
+
for (let i = 0; i < a.length; i++)
|
|
22
|
+
d |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
23
|
+
return d === 0;
|
|
24
|
+
}
|
|
25
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
26
|
+
/** The signed path for a file, valid until `until`. */
|
|
27
|
+
export async function signedPath(secret, filesPath, file, until) {
|
|
28
|
+
const e = Math.floor(until.getTime() / 1000);
|
|
29
|
+
return `${filesPath}/${file}?e=${e}&s=${await hmac(secret, `wb-library:file:${file}:${e}`)}`;
|
|
30
|
+
}
|
|
31
|
+
/** Reads a request's path: which file, and whether its signature holds. */
|
|
32
|
+
export async function readLink(secret, filesPath, url, now) {
|
|
33
|
+
if (!url.pathname.startsWith(`${filesPath}/`))
|
|
34
|
+
return { refused: "bad_link" };
|
|
35
|
+
const file = url.pathname.slice(filesPath.length + 1);
|
|
36
|
+
if (!UUID.test(file))
|
|
37
|
+
return { refused: "bad_link" };
|
|
38
|
+
const e = url.searchParams.get("e");
|
|
39
|
+
const s = url.searchParams.get("s");
|
|
40
|
+
if (e === null && s === null)
|
|
41
|
+
return { file, signed: false };
|
|
42
|
+
if (e === null || s === null || !/^\d{1,12}$/.test(e))
|
|
43
|
+
return { refused: "bad_link" };
|
|
44
|
+
if (!same(s, await hmac(secret, `wb-library:file:${file}:${e}`)))
|
|
45
|
+
return { refused: "bad_link" };
|
|
46
|
+
if (Number(e) * 1000 < now.getTime())
|
|
47
|
+
return { refused: "expired" };
|
|
48
|
+
return { file, signed: true };
|
|
49
|
+
}
|
|
50
|
+
/** One range of bytes asked for, or null for the whole file; "bad" when it can't be met. */
|
|
51
|
+
export function parseRange(header, size) {
|
|
52
|
+
if (!header)
|
|
53
|
+
return null;
|
|
54
|
+
const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
55
|
+
if (!m || (m[1] === "" && m[2] === ""))
|
|
56
|
+
return "bad";
|
|
57
|
+
if (m[1] === "") {
|
|
58
|
+
const n = Math.min(Number(m[2]), size);
|
|
59
|
+
if (n === 0)
|
|
60
|
+
return "bad";
|
|
61
|
+
return { offset: size - n, length: n };
|
|
62
|
+
}
|
|
63
|
+
const start = Number(m[1]);
|
|
64
|
+
const end = m[2] === "" ? size - 1 : Math.min(Number(m[2]), size - 1);
|
|
65
|
+
if (start >= size || end < start)
|
|
66
|
+
return "bad";
|
|
67
|
+
return { offset: start, length: end - start + 1 };
|
|
68
|
+
}
|