@cloudhal/comments 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +86 -2
- package/dist/src/client/comments.browser.js +245 -0
- package/dist/src/client/index.d.ts +19 -0
- package/dist/src/client/index.js +39 -0
- package/dist/src/client/solver.browser.js +118 -0
- package/dist/src/crypto.d.ts +64 -0
- package/dist/src/crypto.js +170 -0
- package/dist/src/errors.d.ts +38 -0
- package/dist/src/errors.js +61 -0
- package/dist/src/fixtures/comment-text.json +335 -0
- package/dist/src/hashes.d.ts +70 -0
- package/dist/src/hashes.js +191 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +24 -0
- package/dist/src/limits.d.ts +73 -0
- package/dist/src/limits.js +227 -0
- package/dist/src/linkify.d.ts +53 -0
- package/dist/src/linkify.js +142 -0
- package/dist/src/model.d.ts +287 -0
- package/dist/src/model.js +341 -0
- package/dist/src/normalize.d.ts +17 -0
- package/dist/src/normalize.js +125 -0
- package/dist/src/pipeline.d.ts +176 -0
- package/dist/src/pipeline.js +898 -0
- package/dist/src/pow.d.ts +86 -0
- package/dist/src/pow.js +239 -0
- package/dist/src/purge.d.ts +87 -0
- package/dist/src/purge.js +186 -0
- package/dist/src/retention.d.ts +32 -0
- package/dist/src/retention.js +106 -0
- package/dist/src/rules.d.ts +68 -0
- package/dist/src/rules.js +218 -0
- package/dist/src/settings.d.ts +108 -0
- package/dist/src/settings.js +347 -0
- package/dist/src/sql/index.d.ts +7 -0
- package/dist/src/sql/index.js +29 -0
- package/dist/src/sql/portability.d.ts +113 -0
- package/dist/src/sql/portability.js +514 -0
- package/dist/src/sql/rows.d.ts +54 -0
- package/dist/src/sql/rows.js +145 -0
- package/dist/src/sql/store.d.ts +312 -0
- package/dist/src/sql/store.js +2111 -0
- package/dist/src/thread.d.ts +35 -0
- package/dist/src/thread.js +64 -0
- package/package.json +61 -3
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The proof-of-work solver (RFC-0007 §7.2–§7.3), a dedicated Web Worker served as
|
|
3
|
+
* `/_cloudhal/cw.<hash>.js`. It receives `{ c: challenge, b: bits }` and answers with the smallest
|
|
4
|
+
* decimal counter `x` such that `SHA-256(c ":" x)` starts with `b` zero bits — the same search as
|
|
5
|
+
* `solveChallenge` in `@cloudhal/comments`.
|
|
6
|
+
*
|
|
7
|
+
* Pure-JS SHA-256 (no WebAssembly, no `crypto.subtle` round trip per hash): the state after the
|
|
8
|
+
* challenge's whole 64-byte blocks is computed once, so each try hashes one or two blocks. The
|
|
9
|
+
* round constants are derived from the primes, as the standard defines them, to keep the file
|
|
10
|
+
* small. Plain JavaScript because it runs in the browser as built.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** @type {number[]} */
|
|
14
|
+
const K = [];
|
|
15
|
+
/** @type {number[]} */
|
|
16
|
+
const H = [];
|
|
17
|
+
for (let n = 2; K.length < 64; n++) {
|
|
18
|
+
let prime = true;
|
|
19
|
+
for (let d = 2; d * d <= n; d++) if (n % d === 0) prime = false;
|
|
20
|
+
if (!prime) continue;
|
|
21
|
+
if (H.length < 8) H.push(((Math.sqrt(n) % 1) * 2 ** 32) | 0);
|
|
22
|
+
K.push(((Math.cbrt(n) % 1) * 2 ** 32) | 0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const w = new Int32Array(64);
|
|
26
|
+
const rot = (x, n) => (x >>> n) | (x << (32 - n));
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* One compression of a 16-word block into a copy of `state`.
|
|
30
|
+
* @param {number[]} state
|
|
31
|
+
* @param {Int32Array} block
|
|
32
|
+
*/
|
|
33
|
+
const compress = (state, block) => {
|
|
34
|
+
for (let i = 0; i < 64; i++) {
|
|
35
|
+
if (i < 16) w[i] = block[i];
|
|
36
|
+
else {
|
|
37
|
+
const a = w[i - 15];
|
|
38
|
+
const b = w[i - 2];
|
|
39
|
+
w[i] =
|
|
40
|
+
(rot(a, 7) ^ rot(a, 18) ^ (a >>> 3)) +
|
|
41
|
+
w[i - 16] +
|
|
42
|
+
(rot(b, 17) ^ rot(b, 19) ^ (b >>> 10)) +
|
|
43
|
+
w[i - 7];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
let [a, b, c, d, e, f, g, h] = state;
|
|
47
|
+
for (let i = 0; i < 64; i++) {
|
|
48
|
+
const t1 = (h + (rot(e, 6) ^ rot(e, 11) ^ rot(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]) | 0;
|
|
49
|
+
const t2 = ((rot(a, 2) ^ rot(a, 13) ^ rot(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
50
|
+
h = g;
|
|
51
|
+
g = f;
|
|
52
|
+
f = e;
|
|
53
|
+
e = (d + t1) | 0;
|
|
54
|
+
d = c;
|
|
55
|
+
c = b;
|
|
56
|
+
b = a;
|
|
57
|
+
a = (t1 + t2) | 0;
|
|
58
|
+
}
|
|
59
|
+
return [a, b, c, d, e, f, g, h].map((v, i) => (v + state[i]) | 0);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Leading zero bits of a digest given as eight 32-bit words. */
|
|
63
|
+
const zeros = (digest) => {
|
|
64
|
+
let n = 0;
|
|
65
|
+
for (const word of digest) {
|
|
66
|
+
if (word) return n + Math.clz32(word);
|
|
67
|
+
n += 32;
|
|
68
|
+
}
|
|
69
|
+
return n;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} challenge ASCII (base64url segments and dots)
|
|
74
|
+
* @param {number} bits
|
|
75
|
+
*/
|
|
76
|
+
export const solve = (challenge, bits) => {
|
|
77
|
+
const prefix = `${challenge}:`;
|
|
78
|
+
const whole = prefix.length - (prefix.length % 64);
|
|
79
|
+
const block = new Int32Array(16);
|
|
80
|
+
let mid = H;
|
|
81
|
+
for (let at = 0; at < whole; at += 64) {
|
|
82
|
+
block.fill(0);
|
|
83
|
+
for (let i = 0; i < 64; i++) block[i >> 2] |= prefix.charCodeAt(at + i) << (24 - (i % 4) * 8);
|
|
84
|
+
mid = compress(mid, block);
|
|
85
|
+
}
|
|
86
|
+
const rest = prefix.slice(whole);
|
|
87
|
+
const bytes = new Uint8Array(128);
|
|
88
|
+
for (let x = 0; ; x++) {
|
|
89
|
+
const tail = rest + x;
|
|
90
|
+
const length = whole + tail.length;
|
|
91
|
+
const blocks = tail.length + 9 > 64 ? 2 : 1;
|
|
92
|
+
bytes.fill(0);
|
|
93
|
+
for (let i = 0; i < tail.length; i++) bytes[i] = tail.charCodeAt(i);
|
|
94
|
+
bytes[tail.length] = 0x80;
|
|
95
|
+
// the message length in bits fits in 32 bits (challenges are ≤ 512 characters)
|
|
96
|
+
const end = blocks * 64;
|
|
97
|
+
const bitLength = length * 8;
|
|
98
|
+
bytes[end - 4] = bitLength >>> 24;
|
|
99
|
+
bytes[end - 3] = bitLength >>> 16;
|
|
100
|
+
bytes[end - 2] = bitLength >>> 8;
|
|
101
|
+
bytes[end - 1] = bitLength;
|
|
102
|
+
let state = mid;
|
|
103
|
+
for (let k = 0; k < blocks; k++) {
|
|
104
|
+
for (let i = 0; i < 16; i++) {
|
|
105
|
+
const o = k * 64 + i * 4;
|
|
106
|
+
block[i] = (bytes[o] << 24) | (bytes[o + 1] << 16) | (bytes[o + 2] << 8) | bytes[o + 3];
|
|
107
|
+
}
|
|
108
|
+
state = compress(state, block);
|
|
109
|
+
}
|
|
110
|
+
if (zeros(state) >= bits) return String(x);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (typeof WorkerGlobalScope !== "undefined")
|
|
115
|
+
self.onmessage = (event) => {
|
|
116
|
+
const { c, b } = event.data;
|
|
117
|
+
postMessage(solve(String(c), Number(b)));
|
|
118
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebCrypto helpers shared by the proof of work, the hashes and the encrypted columns (RFC-0007
|
|
3
|
+
* §4.1, §7.2, §9.2). Everything runs on Node and on Workers; nothing here imports a Node module.
|
|
4
|
+
*
|
|
5
|
+
* One deployment secret, `CLOUDHAL_COMMENTS_KEY`, is stretched with HKDF-SHA-256 into separate
|
|
6
|
+
* keys per purpose (`cloudhal/comments/<purpose>/v1`), so a key used to sign challenges can never
|
|
7
|
+
* decrypt an email and rotating one purpose is a new info string, not a new secret.
|
|
8
|
+
*/
|
|
9
|
+
/** Spelled through the `crypto.subtle` signatures so it resolves with Node's and Workers' types. */
|
|
10
|
+
export type HmacKey = Parameters<typeof crypto.subtle.sign>[1];
|
|
11
|
+
export type AesKey = Parameters<typeof crypto.subtle.encrypt>[1];
|
|
12
|
+
export declare const utf8: (text: string) => Uint8Array<ArrayBuffer>;
|
|
13
|
+
export declare function toHex(bytes: Uint8Array): string;
|
|
14
|
+
export declare function toBase64Url(bytes: Uint8Array): string;
|
|
15
|
+
/** Decodes base64url without padding; `undefined` for anything else. */
|
|
16
|
+
export declare function fromBase64Url(text: string): Uint8Array<ArrayBuffer> | undefined;
|
|
17
|
+
export declare function randomBytes(n: number): Uint8Array<ArrayBuffer>;
|
|
18
|
+
/** Lower-case hex SHA-256 of a string. */
|
|
19
|
+
export declare function sha256Hex(text: string): Promise<string>;
|
|
20
|
+
export declare function hmac(key: HmacKey, data: Uint8Array<ArrayBuffer>): Promise<Uint8Array>;
|
|
21
|
+
/** HMAC-SHA-256 under a raw key (the monthly IP salt). */
|
|
22
|
+
export declare function hmacRaw(rawKey: Uint8Array<ArrayBuffer>, data: string): Promise<Uint8Array>;
|
|
23
|
+
/** Compares two byte arrays without an early exit. */
|
|
24
|
+
export declare function constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean;
|
|
25
|
+
/** The per-purpose keys derived from `CLOUDHAL_COMMENTS_KEY`. */
|
|
26
|
+
export interface CommentKeys {
|
|
27
|
+
/** Signs proof-of-work challenges (`K_pow`, §7.2). */
|
|
28
|
+
readonly pow: HmacKey;
|
|
29
|
+
/** Keyed hash of lower-cased email addresses: lookups and bans (§4.1 `email_hmac`). */
|
|
30
|
+
readonly emailHmac: HmacKey;
|
|
31
|
+
/** AES-GCM for `commenter_emails.email_ciphertext`. */
|
|
32
|
+
readonly email: AesKey;
|
|
33
|
+
/** AES-GCM for `comment_secrets` (the owner's Turnstile secret). */
|
|
34
|
+
readonly secrets: AesKey;
|
|
35
|
+
/** Keys the optional `COMMENTS_RL` Rate Limiting binding by client network (§6.5). */
|
|
36
|
+
readonly rateLimit: HmacKey;
|
|
37
|
+
}
|
|
38
|
+
export declare const KEY_INFO: {
|
|
39
|
+
readonly pow: "cloudhal/comments/pow/v1";
|
|
40
|
+
readonly emailHmac: "cloudhal/comments/email-hmac/v1";
|
|
41
|
+
readonly email: "cloudhal/comments/email/v1";
|
|
42
|
+
readonly secrets: "cloudhal/comments/secrets/v1";
|
|
43
|
+
readonly rateLimit: "cloudhal/comments/rl/v1";
|
|
44
|
+
};
|
|
45
|
+
/** The deployment secret must carry at least 128 bits; generated ones carry 256. */
|
|
46
|
+
export declare const MIN_SECRET_LENGTH = 32;
|
|
47
|
+
/**
|
|
48
|
+
* Derives the comment keys from the deployment secret (HKDF-SHA-256, empty salt, one info string
|
|
49
|
+
* per purpose). Throws when the secret is shorter than 32 characters.
|
|
50
|
+
*/
|
|
51
|
+
export declare function deriveCommentKeys(secret: string): Promise<CommentKeys>;
|
|
52
|
+
export interface Sealed {
|
|
53
|
+
/** base64url AES-GCM ciphertext with its tag. */
|
|
54
|
+
readonly ciphertext: string;
|
|
55
|
+
/** base64url 96-bit IV, fresh per encryption. */
|
|
56
|
+
readonly iv: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* AES-GCM with a fresh 96-bit IV. `context` is bound as additional data (the site id, and the
|
|
60
|
+
* secret's name for secrets), so a ciphertext copied to another site's row does not decrypt.
|
|
61
|
+
*/
|
|
62
|
+
export declare function seal(key: AesKey, plaintext: string, context: string): Promise<Sealed>;
|
|
63
|
+
/** Opens what `seal` produced; throws when the key, context or bytes do not match. */
|
|
64
|
+
export declare function open(key: AesKey, sealed: Sealed, context: string): Promise<string>;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebCrypto helpers shared by the proof of work, the hashes and the encrypted columns (RFC-0007
|
|
3
|
+
* §4.1, §7.2, §9.2). Everything runs on Node and on Workers; nothing here imports a Node module.
|
|
4
|
+
*
|
|
5
|
+
* One deployment secret, `CLOUDHAL_COMMENTS_KEY`, is stretched with HKDF-SHA-256 into separate
|
|
6
|
+
* keys per purpose (`cloudhal/comments/<purpose>/v1`), so a key used to sign challenges can never
|
|
7
|
+
* decrypt an email and rotating one purpose is a new info string, not a new secret.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Spelled through the `crypto.subtle` signatures so it resolves with Node's and Workers' types. */
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const encoder = new TextEncoder();
|
|
15
|
+
|
|
16
|
+
export const utf8 = (text ) =>
|
|
17
|
+
encoder.encode(text) ;
|
|
18
|
+
|
|
19
|
+
export function toHex(bytes ) {
|
|
20
|
+
let out = "";
|
|
21
|
+
for (const b of bytes) out += b.toString(16).padStart(2, "0");
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function toBase64Url(bytes ) {
|
|
26
|
+
let bin = "";
|
|
27
|
+
for (const b of bytes) bin += String.fromCharCode(b);
|
|
28
|
+
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Decodes base64url without padding; `undefined` for anything else. */
|
|
32
|
+
export function fromBase64Url(text ) {
|
|
33
|
+
if (!/^[A-Za-z0-9_-]*$/.test(text) || text.length % 4 === 1) return undefined;
|
|
34
|
+
const b64 = text.replace(/-/g, "+").replace(/_/g, "/") + "===".slice((text.length + 3) % 4);
|
|
35
|
+
try {
|
|
36
|
+
const bin = atob(b64);
|
|
37
|
+
const out = new Uint8Array(bin.length);
|
|
38
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
39
|
+
return out;
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function randomBytes(n ) {
|
|
46
|
+
return crypto.getRandomValues(new Uint8Array(n));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Lower-case hex SHA-256 of a string. */
|
|
50
|
+
export async function sha256Hex(text ) {
|
|
51
|
+
return toHex(new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(text))));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function hmac(key , data ) {
|
|
55
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", key, data));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** HMAC-SHA-256 under a raw key (the monthly IP salt). */
|
|
59
|
+
export async function hmacRaw(rawKey , data ) {
|
|
60
|
+
const key = (await crypto.subtle.importKey(
|
|
61
|
+
"raw",
|
|
62
|
+
rawKey,
|
|
63
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
64
|
+
false,
|
|
65
|
+
["sign"],
|
|
66
|
+
)) ;
|
|
67
|
+
return hmac(key, utf8(data));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Compares two byte arrays without an early exit. */
|
|
71
|
+
export function constantTimeEqual(a , b ) {
|
|
72
|
+
if (a.length !== b.length) return false;
|
|
73
|
+
let diff = 0;
|
|
74
|
+
for (let i = 0; i < a.length; i++) diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
75
|
+
return diff === 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The per-purpose keys derived from `CLOUDHAL_COMMENTS_KEY`. */
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
export const KEY_INFO = {
|
|
93
|
+
pow: "cloudhal/comments/pow/v1",
|
|
94
|
+
emailHmac: "cloudhal/comments/email-hmac/v1",
|
|
95
|
+
email: "cloudhal/comments/email/v1",
|
|
96
|
+
secrets: "cloudhal/comments/secrets/v1",
|
|
97
|
+
rateLimit: "cloudhal/comments/rl/v1",
|
|
98
|
+
} ;
|
|
99
|
+
|
|
100
|
+
/** The deployment secret must carry at least 128 bits; generated ones carry 256. */
|
|
101
|
+
export const MIN_SECRET_LENGTH = 32;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Derives the comment keys from the deployment secret (HKDF-SHA-256, empty salt, one info string
|
|
105
|
+
* per purpose). Throws when the secret is shorter than 32 characters.
|
|
106
|
+
*/
|
|
107
|
+
export async function deriveCommentKeys(secret ) {
|
|
108
|
+
if (typeof secret !== "string" || secret.length < MIN_SECRET_LENGTH)
|
|
109
|
+
throw new Error(`CLOUDHAL_COMMENTS_KEY must be at least ${MIN_SECRET_LENGTH} characters`);
|
|
110
|
+
const ikm = await crypto.subtle.importKey("raw", utf8(secret), "HKDF", false, ["deriveKey"]);
|
|
111
|
+
|
|
112
|
+
const derive = (info , algorithm , usages ) =>
|
|
113
|
+
crypto.subtle.deriveKey(
|
|
114
|
+
{ name: "HKDF", hash: "SHA-256", salt: new Uint8Array(0), info: utf8(info) },
|
|
115
|
+
ikm,
|
|
116
|
+
algorithm,
|
|
117
|
+
false,
|
|
118
|
+
usages,
|
|
119
|
+
);
|
|
120
|
+
const hmacAlg = { name: "HMAC", hash: "SHA-256", length: 256 };
|
|
121
|
+
const aesAlg = { name: "AES-GCM", length: 256 };
|
|
122
|
+
const [pow, emailHmac, email, secrets, rateLimit] = await Promise.all([
|
|
123
|
+
derive(KEY_INFO.pow, hmacAlg, ["sign"]),
|
|
124
|
+
derive(KEY_INFO.emailHmac, hmacAlg, ["sign"]),
|
|
125
|
+
derive(KEY_INFO.email, aesAlg, ["encrypt", "decrypt"]),
|
|
126
|
+
derive(KEY_INFO.secrets, aesAlg, ["encrypt", "decrypt"]),
|
|
127
|
+
derive(KEY_INFO.rateLimit, hmacAlg, ["sign"]),
|
|
128
|
+
]);
|
|
129
|
+
return {
|
|
130
|
+
pow: pow ,
|
|
131
|
+
emailHmac: emailHmac ,
|
|
132
|
+
email: email ,
|
|
133
|
+
secrets: secrets ,
|
|
134
|
+
rateLimit: rateLimit ,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* AES-GCM with a fresh 96-bit IV. `context` is bound as additional data (the site id, and the
|
|
147
|
+
* secret's name for secrets), so a ciphertext copied to another site's row does not decrypt.
|
|
148
|
+
*/
|
|
149
|
+
export async function seal(key , plaintext , context ) {
|
|
150
|
+
const iv = randomBytes(12);
|
|
151
|
+
const ct = await crypto.subtle.encrypt(
|
|
152
|
+
{ name: "AES-GCM", iv, additionalData: utf8(context) },
|
|
153
|
+
key,
|
|
154
|
+
utf8(plaintext),
|
|
155
|
+
);
|
|
156
|
+
return { ciphertext: toBase64Url(new Uint8Array(ct)), iv: toBase64Url(iv) };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Opens what `seal` produced; throws when the key, context or bytes do not match. */
|
|
160
|
+
export async function open(key , sealed , context ) {
|
|
161
|
+
const iv = fromBase64Url(sealed.iv);
|
|
162
|
+
const ct = fromBase64Url(sealed.ciphertext);
|
|
163
|
+
if (!iv || !ct || iv.length !== 12) throw new Error("malformed ciphertext");
|
|
164
|
+
const pt = await crypto.subtle.decrypt(
|
|
165
|
+
{ name: "AES-GCM", iv, additionalData: utf8(context) },
|
|
166
|
+
key,
|
|
167
|
+
ct,
|
|
168
|
+
);
|
|
169
|
+
return new TextDecoder().decode(pt);
|
|
170
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors of the comments layer. Each carries a stable `code` the API maps to a status:
|
|
3
|
+
* `invalid_settings` / `invalid_input` 400, `not_found` 404, `conflict` 409 (CAS on `rev`),
|
|
4
|
+
* `forbidden` 403 (an agent asked for what only a person may do, RFC-0007 §0.1 decision 3).
|
|
5
|
+
*/
|
|
6
|
+
export type CommentsErrorCode = "invalid_settings" | "invalid_input" | "not_found" | "conflict" | "forbidden";
|
|
7
|
+
export declare class CommentsError extends Error {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly code: CommentsErrorCode;
|
|
10
|
+
constructor(code: CommentsErrorCode, message: string, options?: {
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/** A settings or thread value outside its allowed set (400). */
|
|
15
|
+
export declare class CommentSettingsError extends CommentsError {
|
|
16
|
+
readonly name = "CommentSettingsError";
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
/** A write with a stale `rev` (409). */
|
|
20
|
+
export declare class CommentConflictError extends CommentsError {
|
|
21
|
+
readonly name = "CommentConflictError";
|
|
22
|
+
constructor(message?: string);
|
|
23
|
+
}
|
|
24
|
+
/** Input a person or agent can fix (400). */
|
|
25
|
+
export declare class CommentInputError extends CommentsError {
|
|
26
|
+
readonly name = "CommentInputError";
|
|
27
|
+
constructor(message: string);
|
|
28
|
+
}
|
|
29
|
+
/** The comment, thread or commenter does not exist on this site (404). */
|
|
30
|
+
export declare class CommentNotFoundError extends CommentsError {
|
|
31
|
+
readonly name = "CommentNotFoundError";
|
|
32
|
+
constructor(message?: string);
|
|
33
|
+
}
|
|
34
|
+
/** Only a person may do this (approve, restore, reply, erase), in every mode (403). */
|
|
35
|
+
export declare class CommentForbiddenError extends CommentsError {
|
|
36
|
+
readonly name = "CommentForbiddenError";
|
|
37
|
+
constructor(message: string);
|
|
38
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors of the comments layer. Each carries a stable `code` the API maps to a status:
|
|
3
|
+
* `invalid_settings` / `invalid_input` 400, `not_found` 404, `conflict` 409 (CAS on `rev`),
|
|
4
|
+
* `forbidden` 403 (an agent asked for what only a person may do, RFC-0007 §0.1 decision 3).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export class CommentsError extends Error {
|
|
14
|
+
name = "CommentsError";
|
|
15
|
+
code ;
|
|
16
|
+
|
|
17
|
+
constructor(code , message , options = {}) {
|
|
18
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A settings or thread value outside its allowed set (400). */
|
|
24
|
+
export class CommentSettingsError extends CommentsError {
|
|
25
|
+
name = "CommentSettingsError";
|
|
26
|
+
constructor(message ) {
|
|
27
|
+
super("invalid_settings", message);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A write with a stale `rev` (409). */
|
|
32
|
+
export class CommentConflictError extends CommentsError {
|
|
33
|
+
name = "CommentConflictError";
|
|
34
|
+
constructor(message = "comments changed somewhere else; reload and retry") {
|
|
35
|
+
super("conflict", message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Input a person or agent can fix (400). */
|
|
40
|
+
export class CommentInputError extends CommentsError {
|
|
41
|
+
name = "CommentInputError";
|
|
42
|
+
constructor(message ) {
|
|
43
|
+
super("invalid_input", message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The comment, thread or commenter does not exist on this site (404). */
|
|
48
|
+
export class CommentNotFoundError extends CommentsError {
|
|
49
|
+
name = "CommentNotFoundError";
|
|
50
|
+
constructor(message = "not found") {
|
|
51
|
+
super("not_found", message);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Only a person may do this (approve, restore, reply, erase), in every mode (403). */
|
|
56
|
+
export class CommentForbiddenError extends CommentsError {
|
|
57
|
+
name = "CommentForbiddenError";
|
|
58
|
+
constructor(message ) {
|
|
59
|
+
super("forbidden", message);
|
|
60
|
+
}
|
|
61
|
+
}
|