@pleasenobugs/server 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +50 -2
- package/dist/app.d.ts +27 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +43 -0
- package/dist/app.js.map +1 -0
- package/dist/auth.d.ts +37 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +108 -0
- package/dist/auth.js.map +1 -0
- package/dist/board.d.ts +23 -0
- package/dist/board.d.ts.map +1 -0
- package/dist/board.js +45 -0
- package/dist/board.js.map +1 -0
- package/dist/bootstrap.d.ts +42 -0
- package/dist/bootstrap.d.ts.map +1 -0
- package/dist/bootstrap.js +68 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/importer.d.ts +50 -0
- package/dist/importer.d.ts.map +1 -0
- package/dist/importer.js +85 -0
- package/dist/importer.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/limit.d.ts +14 -0
- package/dist/limit.d.ts.map +1 -0
- package/dist/limit.js +32 -0
- package/dist/limit.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +164 -0
- package/dist/main.js.map +1 -0
- package/dist/memory-store.d.ts +59 -0
- package/dist/memory-store.d.ts.map +1 -0
- package/dist/memory-store.js +177 -0
- package/dist/memory-store.js.map +1 -0
- package/dist/projects.d.ts +64 -0
- package/dist/projects.d.ts.map +1 -0
- package/dist/projects.js +163 -0
- package/dist/projects.js.map +1 -0
- package/dist/router.d.ts +28 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +391 -0
- package/dist/router.js.map +1 -0
- package/dist/sqlite-store.d.ts +61 -0
- package/dist/sqlite-store.d.ts.map +1 -0
- package/dist/sqlite-store.js +290 -0
- package/dist/sqlite-store.js.map +1 -0
- package/dist/store.d.ts +108 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +2 -0
- package/dist/store.js.map +1 -0
- package/dist/telemetry.d.ts +83 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +174 -0
- package/dist/telemetry.js.map +1 -0
- package/package.json +53 -4
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type Project, type Rules } from '@pleasenobugs/core';
|
|
2
|
+
/**
|
|
3
|
+
* Making and changing projects, shared by the dashboard routes and the CLI so
|
|
4
|
+
* the two cannot come to disagree about what a valid project is.
|
|
5
|
+
*
|
|
6
|
+
* Every validator answers the cleaned value or null, and null always means
|
|
7
|
+
* "refuse the request": a project is configuration, and quietly dropping half
|
|
8
|
+
* of somebody's origin list would be a widget that mysteriously does not work.
|
|
9
|
+
*/
|
|
10
|
+
export declare const PROJECT_NAME_MAX = 60;
|
|
11
|
+
export declare const MAX_ORIGINS = 10;
|
|
12
|
+
export declare const MAX_OWNERS = 10;
|
|
13
|
+
/** A fresh project id. 31^10 is about 8e14, so a collision is a curiosity. */
|
|
14
|
+
export declare function newProjectId(): string;
|
|
15
|
+
/** A project's name: trimmed, 1 to 60 characters. */
|
|
16
|
+
export declare function cleanName(v: unknown): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* An origin the widget may run on, or null.
|
|
19
|
+
*
|
|
20
|
+
* Only http and https. `http` is allowed because `http://localhost:5173` is
|
|
21
|
+
* where every developer tries it first, and refusing that would make the first
|
|
22
|
+
* five minutes the worst ones.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* parseOrigin('https://Shitti.me/board') // → 'https://shitti.me'
|
|
26
|
+
* parseOrigin('javascript:alert(1)') // → null
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseOrigin(v: unknown): string | null;
|
|
29
|
+
export declare function cleanOrigins(v: unknown): string[] | null;
|
|
30
|
+
/** Owners: valid addresses, deduplicated, and never none. */
|
|
31
|
+
export declare function cleanOwners(v: unknown): string[] | null;
|
|
32
|
+
export declare const MAX_LOGIN_MESSAGES = 3;
|
|
33
|
+
export declare const LOGIN_MESSAGE_MAX = 200;
|
|
34
|
+
/**
|
|
35
|
+
* The app logins a project accepts, or null. An empty array is valid and
|
|
36
|
+
* turns the feature off.
|
|
37
|
+
*
|
|
38
|
+
* **Never trimmed or normalised**: the signature is over exact bytes, and a
|
|
39
|
+
* message that differs from what the app signs by one trailing space would be
|
|
40
|
+
* a setting that silently never works. Our own message format is refused, so
|
|
41
|
+
* a scoped, expiring proof can never be turned into a permanent one by listing
|
|
42
|
+
* it here.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* cleanLoginMessages(['Sign in to Shitti']) // → ['Sign in to Shitti']
|
|
46
|
+
* cleanLoginMessages(['PleaseNoBugs login…']) // → null
|
|
47
|
+
*/
|
|
48
|
+
export declare function cleanLoginMessages(v: unknown): string[] | null;
|
|
49
|
+
/**
|
|
50
|
+
* A partial change to a project's rules, merged over what it has, or null if
|
|
51
|
+
* any field is out of range. The ranges are generous: they exist to stop a
|
|
52
|
+
* typo, not to have opinions.
|
|
53
|
+
*/
|
|
54
|
+
export declare function mergeRules(current: Rules, v: unknown): Rules | null;
|
|
55
|
+
/** A new project owned by one wallet, on the default rules. */
|
|
56
|
+
export declare function newProject(args: {
|
|
57
|
+
name: string;
|
|
58
|
+
origins: string[];
|
|
59
|
+
owner: string;
|
|
60
|
+
now?: number;
|
|
61
|
+
id?: string;
|
|
62
|
+
loginMessages?: string[];
|
|
63
|
+
}): Project;
|
|
64
|
+
//# sourceMappingURL=projects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projects.d.ts","sourceRoot":"","sources":["../src/projects.ts"],"names":[],"mappings":"AACA,OAAO,EAOL,KAAK,OAAO,EACZ,KAAK,KAAK,EACX,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;GAOG;AAEH,eAAO,MAAM,gBAAgB,KAAK,CAAC;AACnC,eAAO,MAAM,WAAW,KAAK,CAAC;AAC9B,eAAO,MAAM,UAAU,KAAK,CAAC;AAS7B,8EAA8E;AAC9E,wBAAgB,YAAY,IAAI,MAAM,CAIrC;AAED,qDAAqD;AACrD,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAInD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAQrD;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CASxD;AAED,6DAA6D;AAC7D,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CASvD;AAED,eAAO,MAAM,kBAAkB,IAAI,CAAC;AACpC,eAAO,MAAM,iBAAiB,MAAM,CAAC;AAErC;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CAS9D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,GAAG,KAAK,GAAG,IAAI,CA+BnE;AAED,+DAA+D;AAC/D,wBAAgB,UAAU,CAAC,IAAI,EAAE;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,GAAG,OAAO,CAUV"}
|
package/dist/projects.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { randomInt } from 'node:crypto';
|
|
2
|
+
import { DEFAULT_RULES, POST_KINDS, isNimiqAddress, isPostKind, normalizeAddress, } from '@pleasenobugs/core';
|
|
3
|
+
/**
|
|
4
|
+
* Making and changing projects, shared by the dashboard routes and the CLI so
|
|
5
|
+
* the two cannot come to disagree about what a valid project is.
|
|
6
|
+
*
|
|
7
|
+
* Every validator answers the cleaned value or null, and null always means
|
|
8
|
+
* "refuse the request": a project is configuration, and quietly dropping half
|
|
9
|
+
* of somebody's origin list would be a widget that mysteriously does not work.
|
|
10
|
+
*/
|
|
11
|
+
export const PROJECT_NAME_MAX = 60;
|
|
12
|
+
export const MAX_ORIGINS = 10;
|
|
13
|
+
export const MAX_OWNERS = 10;
|
|
14
|
+
/**
|
|
15
|
+
* Lower case and digits with the lookalikes left out (0/o, 1/l/i), since a
|
|
16
|
+
* project id gets read off one screen and typed into another.
|
|
17
|
+
*/
|
|
18
|
+
const ID_ALPHABET = '23456789abcdefghjkmnpqrstuvwxyz';
|
|
19
|
+
const ID_LENGTH = 10;
|
|
20
|
+
/** A fresh project id. 31^10 is about 8e14, so a collision is a curiosity. */
|
|
21
|
+
export function newProjectId() {
|
|
22
|
+
let id = '';
|
|
23
|
+
for (let i = 0; i < ID_LENGTH; i++)
|
|
24
|
+
id += ID_ALPHABET[randomInt(ID_ALPHABET.length)];
|
|
25
|
+
return id;
|
|
26
|
+
}
|
|
27
|
+
/** A project's name: trimmed, 1 to 60 characters. */
|
|
28
|
+
export function cleanName(v) {
|
|
29
|
+
if (typeof v !== 'string')
|
|
30
|
+
return null;
|
|
31
|
+
const name = v.trim();
|
|
32
|
+
return name && name.length <= PROJECT_NAME_MAX ? name : null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* An origin the widget may run on, or null.
|
|
36
|
+
*
|
|
37
|
+
* Only http and https. `http` is allowed because `http://localhost:5173` is
|
|
38
|
+
* where every developer tries it first, and refusing that would make the first
|
|
39
|
+
* five minutes the worst ones.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* parseOrigin('https://Shitti.me/board') // → 'https://shitti.me'
|
|
43
|
+
* parseOrigin('javascript:alert(1)') // → null
|
|
44
|
+
*/
|
|
45
|
+
export function parseOrigin(v) {
|
|
46
|
+
if (typeof v !== 'string')
|
|
47
|
+
return null;
|
|
48
|
+
try {
|
|
49
|
+
const url = new URL(v.trim());
|
|
50
|
+
return url.protocol === 'https:' || url.protocol === 'http:' ? url.origin : null;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function cleanOrigins(v) {
|
|
57
|
+
if (!Array.isArray(v) || v.length === 0 || v.length > MAX_ORIGINS)
|
|
58
|
+
return null;
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const raw of v) {
|
|
61
|
+
const origin = parseOrigin(raw);
|
|
62
|
+
if (!origin)
|
|
63
|
+
return null;
|
|
64
|
+
if (!out.includes(origin))
|
|
65
|
+
out.push(origin);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/** Owners: valid addresses, deduplicated, and never none. */
|
|
70
|
+
export function cleanOwners(v) {
|
|
71
|
+
if (!Array.isArray(v) || v.length === 0 || v.length > MAX_OWNERS)
|
|
72
|
+
return null;
|
|
73
|
+
const out = [];
|
|
74
|
+
for (const raw of v) {
|
|
75
|
+
if (!isNimiqAddress(raw))
|
|
76
|
+
return null;
|
|
77
|
+
const a = normalizeAddress(raw);
|
|
78
|
+
if (!out.includes(a))
|
|
79
|
+
out.push(a);
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
export const MAX_LOGIN_MESSAGES = 3;
|
|
84
|
+
export const LOGIN_MESSAGE_MAX = 200;
|
|
85
|
+
/**
|
|
86
|
+
* The app logins a project accepts, or null. An empty array is valid and
|
|
87
|
+
* turns the feature off.
|
|
88
|
+
*
|
|
89
|
+
* **Never trimmed or normalised**: the signature is over exact bytes, and a
|
|
90
|
+
* message that differs from what the app signs by one trailing space would be
|
|
91
|
+
* a setting that silently never works. Our own message format is refused, so
|
|
92
|
+
* a scoped, expiring proof can never be turned into a permanent one by listing
|
|
93
|
+
* it here.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* cleanLoginMessages(['Sign in to Shitti']) // → ['Sign in to Shitti']
|
|
97
|
+
* cleanLoginMessages(['PleaseNoBugs login…']) // → null
|
|
98
|
+
*/
|
|
99
|
+
export function cleanLoginMessages(v) {
|
|
100
|
+
if (!Array.isArray(v) || v.length > MAX_LOGIN_MESSAGES)
|
|
101
|
+
return null;
|
|
102
|
+
const out = [];
|
|
103
|
+
for (const m of v) {
|
|
104
|
+
if (typeof m !== 'string' || !m.trim() || m.length > LOGIN_MESSAGE_MAX)
|
|
105
|
+
return null;
|
|
106
|
+
if (m.startsWith('PleaseNoBugs login'))
|
|
107
|
+
return null;
|
|
108
|
+
if (!out.includes(m))
|
|
109
|
+
out.push(m);
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A partial change to a project's rules, merged over what it has, or null if
|
|
115
|
+
* any field is out of range. The ranges are generous: they exist to stop a
|
|
116
|
+
* typo, not to have opinions.
|
|
117
|
+
*/
|
|
118
|
+
export function mergeRules(current, v) {
|
|
119
|
+
if (!v || typeof v !== 'object' || Array.isArray(v))
|
|
120
|
+
return null;
|
|
121
|
+
const change = v;
|
|
122
|
+
const next = { ...current, kinds: [...current.kinds] };
|
|
123
|
+
const int = (x, min, max) => typeof x === 'number' && Number.isInteger(x) && x >= min && x <= max ? x : null;
|
|
124
|
+
if (change.perDay !== undefined) {
|
|
125
|
+
const n = int(change.perDay, 1, 100);
|
|
126
|
+
if (n === null)
|
|
127
|
+
return null;
|
|
128
|
+
next.perDay = n;
|
|
129
|
+
}
|
|
130
|
+
if (change.textMax !== undefined) {
|
|
131
|
+
const n = int(change.textMax, 20, 2000);
|
|
132
|
+
if (n === null)
|
|
133
|
+
return null;
|
|
134
|
+
next.textMax = n;
|
|
135
|
+
}
|
|
136
|
+
if (change.replyMax !== undefined) {
|
|
137
|
+
const n = int(change.replyMax, 20, 2000);
|
|
138
|
+
if (n === null)
|
|
139
|
+
return null;
|
|
140
|
+
next.replyMax = n;
|
|
141
|
+
}
|
|
142
|
+
if (change.kinds !== undefined) {
|
|
143
|
+
if (!Array.isArray(change.kinds) || !change.kinds.length || !change.kinds.every(isPostKind)) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
// In the order core lists them, so the widget's chips never shuffle.
|
|
147
|
+
next.kinds = POST_KINDS.filter((k) => change.kinds.includes(k));
|
|
148
|
+
}
|
|
149
|
+
return next;
|
|
150
|
+
}
|
|
151
|
+
/** A new project owned by one wallet, on the default rules. */
|
|
152
|
+
export function newProject(args) {
|
|
153
|
+
return {
|
|
154
|
+
id: args.id ?? newProjectId(),
|
|
155
|
+
name: args.name,
|
|
156
|
+
owners: [normalizeAddress(args.owner)],
|
|
157
|
+
origins: args.origins,
|
|
158
|
+
rules: { ...DEFAULT_RULES, kinds: [...DEFAULT_RULES.kinds] },
|
|
159
|
+
...(args.loginMessages?.length ? { loginMessages: args.loginMessages } : {}),
|
|
160
|
+
createdAt: args.now ?? Date.now(),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=projects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projects.js","sourceRoot":"","sources":["../src/projects.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EACL,aAAa,EACb,UAAU,EACV,cAAc,EACd,UAAU,EACV,gBAAgB,GAIjB,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;GAOG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AACnC,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,CAAC;AAC9B,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAC;AAE7B;;;GAGG;AACH,MAAM,WAAW,GAAG,iCAAiC,CAAC;A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,EAAE,GAAG,aAAa,EAAE,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE;QAC5D,GAAG,CAAC,IAAI,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5E,SAAS,EAAE,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE;KAClC,CAAC;AACJ,CAAC"}
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import type { Store } from './store.js';
|
|
3
|
+
export interface RouterOptions {
|
|
4
|
+
store: Store;
|
|
5
|
+
/**
|
|
6
|
+
* Where the dashboard runs. A proof signed for `Project: dashboard` is only
|
|
7
|
+
* accepted from these origins, and only on the project routes. Empty means
|
|
8
|
+
* no dashboard at all, which is the right default for a self-hoster, who
|
|
9
|
+
* creates projects with the CLI.
|
|
10
|
+
*/
|
|
11
|
+
dashboardOrigins?: string[];
|
|
12
|
+
/** The clock, so tests can move it. */
|
|
13
|
+
now?: () => number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Everything under `/v1`, as one Express router a developer can mount wherever
|
|
17
|
+
* they like:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* app.use('/feedback', createRouter({ store })); // → /feedback/v1/...
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* It brings its own CORS and body parsing, so it works mounted into an app
|
|
24
|
+
* that has neither. The routes and what they answer are `docs/api-v1.md`; the
|
|
25
|
+
* comments here are about *why* each one is the way it is.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createRouter(options: RouterOptions): express.Router;
|
|
28
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,SAAS,CAAC;AA4B9B,OAAO,KAAK,EAAa,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnD,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,uCAAuC;IACvC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAKD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAuZnE"}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import cors from 'cors';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { DASHBOARD_PROJECT, DAY_MS, NAME_MAX, POST_ID_MAX, cleanContext, decodeProof, isPostKind, isPostStatus, normalizeAddress, normalizeOrigin, } from '@pleasenobugs/core';
|
|
4
|
+
import { checkProof } from './auth.js';
|
|
5
|
+
import { publicProject, toVisible } from './board.js';
|
|
6
|
+
import { rateLimit } from './limit.js';
|
|
7
|
+
import { cleanLoginMessages, cleanName, cleanOrigins, cleanOwners, mergeRules, newProject, newProjectId, } from './projects.js';
|
|
8
|
+
/** How many answers `GET /answers` hands back. More than anybody will have. */
|
|
9
|
+
const ANSWERS_MAX = 10;
|
|
10
|
+
/**
|
|
11
|
+
* Everything under `/v1`, as one Express router a developer can mount wherever
|
|
12
|
+
* they like:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* app.use('/feedback', createRouter({ store })); // → /feedback/v1/...
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* It brings its own CORS and body parsing, so it works mounted into an app
|
|
19
|
+
* that has neither. The routes and what they answer are `docs/api-v1.md`; the
|
|
20
|
+
* comments here are about *why* each one is the way it is.
|
|
21
|
+
*/
|
|
22
|
+
export function createRouter(options) {
|
|
23
|
+
const { store } = options;
|
|
24
|
+
const now = options.now ?? Date.now;
|
|
25
|
+
const dashboardOrigins = (options.dashboardOrigins ?? []).map(normalizeOrigin);
|
|
26
|
+
const router = express.Router();
|
|
27
|
+
// Any origin may call, and that is safe for one reason: nothing here uses
|
|
28
|
+
// cookies. Who you are is the proof in the Authorization header, and a page
|
|
29
|
+
// cannot borrow somebody else's. The origin rules that *do* matter (which
|
|
30
|
+
// sites may post on which board) are enforced on the proof itself.
|
|
31
|
+
router.use(cors({ origin: true, allowedHeaders: ['content-type', 'authorization'], maxAge: 600 }));
|
|
32
|
+
router.use(express.json({ limit: '32kb' }));
|
|
33
|
+
/** Express 4 does not catch a rejected promise; this hands it to the error handler. */
|
|
34
|
+
const h = (fn) => (req, res, next) => {
|
|
35
|
+
fn(req, res).catch(next);
|
|
36
|
+
};
|
|
37
|
+
const fail = (res, status, error, extra) => res.status(status).json({ error, ...extra });
|
|
38
|
+
/** The project in the URL, or a 404 already sent. */
|
|
39
|
+
async function project(req, res) {
|
|
40
|
+
const p = await store.getProject(String(req.params.pid));
|
|
41
|
+
if (!p)
|
|
42
|
+
fail(res, 404, 'no_project');
|
|
43
|
+
return p;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Who is reading this project's board.
|
|
47
|
+
*
|
|
48
|
+
* No header is a stranger. A header that does not hold up is a refusal, even
|
|
49
|
+
* on a route a stranger could use: the client sent it believing it worked,
|
|
50
|
+
* and silently treating them as a stranger would show them a board where
|
|
51
|
+
* their own posts are not theirs.
|
|
52
|
+
*/
|
|
53
|
+
async function reader(req, p) {
|
|
54
|
+
const check = await checkProof(req.header('authorization'), p.id, req.header('origin'), now(), p.loginMessages ?? []);
|
|
55
|
+
if (!check.ok) {
|
|
56
|
+
return check.reason === 'missing'
|
|
57
|
+
? { kind: 'stranger' }
|
|
58
|
+
: { kind: 'refused', status: 401, error: 'unauthorized' };
|
|
59
|
+
}
|
|
60
|
+
if (!p.origins.includes(check.origin)) {
|
|
61
|
+
return { kind: 'refused', status: 403, error: 'origin_not_allowed' };
|
|
62
|
+
}
|
|
63
|
+
return { kind: 'wallet', address: check.address, owner: p.owners.includes(check.address) };
|
|
64
|
+
}
|
|
65
|
+
/** A signed-in reader, or an error already sent. */
|
|
66
|
+
async function wallet(req, res, p) {
|
|
67
|
+
const r = await reader(req, p);
|
|
68
|
+
if (r.kind === 'wallet')
|
|
69
|
+
return r;
|
|
70
|
+
if (r.kind === 'stranger')
|
|
71
|
+
fail(res, 401, 'unauthorized');
|
|
72
|
+
else
|
|
73
|
+
fail(res, r.status, r.error);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
/** The dashboard's wallet, or an error already sent. */
|
|
77
|
+
async function dashboard(req, res) {
|
|
78
|
+
if (!dashboardOrigins.length) {
|
|
79
|
+
fail(res, 401, 'unauthorized');
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
const check = await checkProof(req.header('authorization'), DASHBOARD_PROJECT, req.header('origin'), now());
|
|
83
|
+
if (!check.ok) {
|
|
84
|
+
fail(res, 401, 'unauthorized');
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
if (!dashboardOrigins.includes(check.origin)) {
|
|
88
|
+
fail(res, 403, 'origin_not_allowed');
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
return check.address;
|
|
92
|
+
}
|
|
93
|
+
/** What one wallet needs to know about itself on one board. */
|
|
94
|
+
async function me(p, address, owner) {
|
|
95
|
+
const used = await store.countByAuthorSince(p.id, address, now() - DAY_MS);
|
|
96
|
+
const out = { owner, left: Math.max(0, p.rules.perDay - used) };
|
|
97
|
+
const name = (await store.names(p.id, [address])).get(address);
|
|
98
|
+
if (name)
|
|
99
|
+
out.name = name;
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
// ---- Reading ----
|
|
103
|
+
router.get('/v1/projects/:pid', h(async (req, res) => {
|
|
104
|
+
const p = await project(req, res);
|
|
105
|
+
if (p)
|
|
106
|
+
res.json(publicProject(p));
|
|
107
|
+
}));
|
|
108
|
+
router.get('/v1/projects/:pid/posts', h(async (req, res) => {
|
|
109
|
+
const p = await project(req, res);
|
|
110
|
+
if (!p)
|
|
111
|
+
return;
|
|
112
|
+
const r = await reader(req, p);
|
|
113
|
+
if (r.kind === 'refused')
|
|
114
|
+
return fail(res, r.status, r.error);
|
|
115
|
+
const address = r.kind === 'wallet' ? r.address : null;
|
|
116
|
+
const owner = r.kind === 'wallet' && r.owner;
|
|
117
|
+
const rows = await store.posts(p.id, address);
|
|
118
|
+
// Hidden posts reach owners, marked, so hiding can be undone. Everybody
|
|
119
|
+
// else gets a board without them, their authors included.
|
|
120
|
+
const shown = owner ? rows : rows.filter((row) => !row.hidden);
|
|
121
|
+
// One lookup for every author who let their name show, rather than one
|
|
122
|
+
// per post: a board is mostly a handful of people saying several things.
|
|
123
|
+
const named = [...new Set(shown.filter((row) => !row.anon).map((row) => row.author))];
|
|
124
|
+
const names = await store.names(p.id, named);
|
|
125
|
+
res.json({
|
|
126
|
+
project: publicProject(p),
|
|
127
|
+
posts: shown.map((row) => toVisible(row, address, names.get(row.author), owner)),
|
|
128
|
+
...(r.kind === 'wallet' ? { me: await me(p, r.address, r.owner) } : {}),
|
|
129
|
+
});
|
|
130
|
+
}));
|
|
131
|
+
router.get('/v1/projects/:pid/me', h(async (req, res) => {
|
|
132
|
+
const p = await project(req, res);
|
|
133
|
+
if (!p)
|
|
134
|
+
return;
|
|
135
|
+
const w = await wallet(req, res, p);
|
|
136
|
+
if (w)
|
|
137
|
+
res.json(await me(p, w.address, w.owner));
|
|
138
|
+
}));
|
|
139
|
+
router.get('/v1/projects/:pid/answers', h(async (req, res) => {
|
|
140
|
+
const p = await project(req, res);
|
|
141
|
+
if (!p)
|
|
142
|
+
return;
|
|
143
|
+
const w = await wallet(req, res, p);
|
|
144
|
+
if (w)
|
|
145
|
+
res.json({ answers: await store.answersFor(p.id, w.address, ANSWERS_MAX) });
|
|
146
|
+
}));
|
|
147
|
+
// ---- Writing ----
|
|
148
|
+
router.put('/v1/projects/:pid/me', h(async (req, res) => {
|
|
149
|
+
const p = await project(req, res);
|
|
150
|
+
if (!p)
|
|
151
|
+
return;
|
|
152
|
+
const w = await wallet(req, res, p);
|
|
153
|
+
if (!w)
|
|
154
|
+
return;
|
|
155
|
+
const raw = (req.body ?? {}).name;
|
|
156
|
+
if (typeof raw !== 'string' || raw.trim().length > NAME_MAX)
|
|
157
|
+
return fail(res, 400, 'bad_request');
|
|
158
|
+
const name = raw.trim() || undefined;
|
|
159
|
+
await store.setName(p.id, w.address, name);
|
|
160
|
+
res.json({ ok: true, ...(name ? { name } : {}) });
|
|
161
|
+
}));
|
|
162
|
+
router.post('/v1/projects/:pid/posts', rateLimit({
|
|
163
|
+
windowMs: 15 * 60_000,
|
|
164
|
+
max: 10,
|
|
165
|
+
name: 'post',
|
|
166
|
+
// Per wallet on top of the per-IP window. The daily allowance is the
|
|
167
|
+
// real rule; this is the brake on somebody hammering the route.
|
|
168
|
+
by: (req) => decodeProof(req.header('authorization'))?.address,
|
|
169
|
+
}), h(async (req, res) => {
|
|
170
|
+
const p = await project(req, res);
|
|
171
|
+
if (!p)
|
|
172
|
+
return;
|
|
173
|
+
const w = await wallet(req, res, p);
|
|
174
|
+
if (!w)
|
|
175
|
+
return;
|
|
176
|
+
const body = (req.body ?? {});
|
|
177
|
+
const text = typeof body.text === 'string' ? body.text.trim() : '';
|
|
178
|
+
const context = body.context === undefined ? {} : cleanContext(body.context);
|
|
179
|
+
if (typeof body.id !== 'string' ||
|
|
180
|
+
!body.id ||
|
|
181
|
+
body.id.length > POST_ID_MAX ||
|
|
182
|
+
!isPostKind(body.kind) ||
|
|
183
|
+
!p.rules.kinds.includes(body.kind) ||
|
|
184
|
+
!text ||
|
|
185
|
+
text.length > p.rules.textMax ||
|
|
186
|
+
context === null) {
|
|
187
|
+
return fail(res, 400, 'bad_request');
|
|
188
|
+
}
|
|
189
|
+
const at = now();
|
|
190
|
+
const used = await store.countByAuthorSince(p.id, w.address, at - DAY_MS);
|
|
191
|
+
if (used >= p.rules.perDay) {
|
|
192
|
+
console.warn(`[post] ${p.id} too_many ${tag(w.address)} today=${used}`);
|
|
193
|
+
return fail(res, 429, 'too_many', { perDay: p.rules.perDay });
|
|
194
|
+
}
|
|
195
|
+
const created = await store.createPost({
|
|
196
|
+
projectId: p.id,
|
|
197
|
+
id: body.id,
|
|
198
|
+
author: w.address,
|
|
199
|
+
// Anonymous unless the author explicitly asked to be named. Getting
|
|
200
|
+
// this wrong in the private direction costs a name on a card; getting
|
|
201
|
+
// it wrong the other way puts somebody's name where they did not want it.
|
|
202
|
+
anon: body.anon !== false,
|
|
203
|
+
kind: body.kind,
|
|
204
|
+
text,
|
|
205
|
+
at,
|
|
206
|
+
status: 'open',
|
|
207
|
+
...(Object.keys(context).length ? { context } : {}),
|
|
208
|
+
});
|
|
209
|
+
if (!created)
|
|
210
|
+
return fail(res, 409, 'duplicate');
|
|
211
|
+
console.info(`[post] ${p.id} ${tag(w.address)} posted ${body.kind} ${body.id}`);
|
|
212
|
+
res.json({ ok: true, left: Math.max(0, p.rules.perDay - used - 1) });
|
|
213
|
+
}));
|
|
214
|
+
router.post('/v1/projects/:pid/posts/:id/vote', h(async (req, res) => {
|
|
215
|
+
const p = await project(req, res);
|
|
216
|
+
if (!p)
|
|
217
|
+
return;
|
|
218
|
+
const w = await wallet(req, res, p);
|
|
219
|
+
if (!w)
|
|
220
|
+
return;
|
|
221
|
+
const post = await store.getPost(p.id, String(req.params.id));
|
|
222
|
+
if (!post || (post.hidden && !w.owner))
|
|
223
|
+
return fail(res, 404, 'no_post');
|
|
224
|
+
// Stated rather than toggled, so a retry lands where the tap meant it to.
|
|
225
|
+
const up = (req.body ?? {}).up !== false;
|
|
226
|
+
const votes = await store.vote(p.id, post.id, w.address, up);
|
|
227
|
+
res.json({ ok: true, votes, voted: up });
|
|
228
|
+
}));
|
|
229
|
+
router.delete('/v1/projects/:pid/posts/:id', h(async (req, res) => {
|
|
230
|
+
const p = await project(req, res);
|
|
231
|
+
if (!p)
|
|
232
|
+
return;
|
|
233
|
+
const w = await wallet(req, res, p);
|
|
234
|
+
if (!w)
|
|
235
|
+
return;
|
|
236
|
+
const post = await store.getPost(p.id, String(req.params.id));
|
|
237
|
+
if (!post)
|
|
238
|
+
return fail(res, 404, 'no_post');
|
|
239
|
+
if (post.author !== w.address && !w.owner)
|
|
240
|
+
return fail(res, 403, 'not_yours');
|
|
241
|
+
await store.deletePost(p.id, post.id);
|
|
242
|
+
console.info(`[post] ${p.id} ${tag(w.address)} deleted ${post.id}`);
|
|
243
|
+
res.json({ ok: true });
|
|
244
|
+
}));
|
|
245
|
+
// ---- Owners ----
|
|
246
|
+
router.post('/v1/projects/:pid/posts/:id/manage', h(async (req, res) => {
|
|
247
|
+
const p = await project(req, res);
|
|
248
|
+
if (!p)
|
|
249
|
+
return;
|
|
250
|
+
const w = await wallet(req, res, p);
|
|
251
|
+
if (!w)
|
|
252
|
+
return;
|
|
253
|
+
if (!w.owner)
|
|
254
|
+
return fail(res, 403, 'not_owner');
|
|
255
|
+
const post = await store.getPost(p.id, String(req.params.id));
|
|
256
|
+
if (!post)
|
|
257
|
+
return fail(res, 404, 'no_post');
|
|
258
|
+
const { status, reply, hidden } = (req.body ?? {});
|
|
259
|
+
const patch = {};
|
|
260
|
+
// Whether this call was an *answer*, which is what the author is told
|
|
261
|
+
// about. Hiding is not: telling somebody their post was taken down is
|
|
262
|
+
// the start of an argument the board has nowhere to hold. Nor is saving
|
|
263
|
+
// the same words twice.
|
|
264
|
+
let answered = false;
|
|
265
|
+
if (status !== undefined) {
|
|
266
|
+
if (!isPostStatus(status))
|
|
267
|
+
return fail(res, 400, 'bad_request');
|
|
268
|
+
if (status !== post.status)
|
|
269
|
+
answered = true;
|
|
270
|
+
patch.status = status;
|
|
271
|
+
}
|
|
272
|
+
if (reply !== undefined) {
|
|
273
|
+
if (typeof reply !== 'string' || reply.trim().length > p.rules.replyMax) {
|
|
274
|
+
return fail(res, 400, 'bad_request');
|
|
275
|
+
}
|
|
276
|
+
const said = reply.trim();
|
|
277
|
+
if (said !== (post.reply?.text ?? ''))
|
|
278
|
+
answered = true;
|
|
279
|
+
// An empty reply is how one is taken back, rather than a route of its own.
|
|
280
|
+
patch.reply = said ? { text: said, at: now() } : null;
|
|
281
|
+
}
|
|
282
|
+
if (hidden !== undefined) {
|
|
283
|
+
if (typeof hidden !== 'boolean')
|
|
284
|
+
return fail(res, 400, 'bad_request');
|
|
285
|
+
patch.hidden = hidden;
|
|
286
|
+
}
|
|
287
|
+
if (answered)
|
|
288
|
+
patch.answeredAt = now();
|
|
289
|
+
await store.updatePost(p.id, post.id, patch);
|
|
290
|
+
console.info(`[post] ${p.id} owner managed ${post.id}`);
|
|
291
|
+
res.json({ ok: true });
|
|
292
|
+
}));
|
|
293
|
+
// ---- Projects (dashboard) ----
|
|
294
|
+
router.post('/v1/projects', rateLimit({ windowMs: 60 * 60_000, max: 5, name: 'create-project' }), h(async (req, res) => {
|
|
295
|
+
const owner = await dashboard(req, res);
|
|
296
|
+
if (!owner)
|
|
297
|
+
return;
|
|
298
|
+
const body = (req.body ?? {});
|
|
299
|
+
const name = cleanName(body.name);
|
|
300
|
+
const origins = cleanOrigins(body.origins);
|
|
301
|
+
if (!name || !origins)
|
|
302
|
+
return fail(res, 400, 'bad_request');
|
|
303
|
+
let id = newProjectId();
|
|
304
|
+
while (await store.getProject(id))
|
|
305
|
+
id = newProjectId();
|
|
306
|
+
const p = newProject({ id, name, origins, owner, now: now() });
|
|
307
|
+
await store.putProject(p);
|
|
308
|
+
console.info(`[project] ${tag(owner)} created ${p.id}`);
|
|
309
|
+
res.json(p);
|
|
310
|
+
}));
|
|
311
|
+
router.get('/v1/projects', h(async (req, res) => {
|
|
312
|
+
const owner = await dashboard(req, res);
|
|
313
|
+
if (!owner)
|
|
314
|
+
return;
|
|
315
|
+
const projects = await store.projectsOwnedBy(owner);
|
|
316
|
+
res.json({
|
|
317
|
+
projects: await Promise.all(projects.map(async (p) => ({
|
|
318
|
+
...p,
|
|
319
|
+
unseen: await store.countSince(p.id, await store.getSeen(p.id, owner)),
|
|
320
|
+
}))),
|
|
321
|
+
});
|
|
322
|
+
}));
|
|
323
|
+
router.patch('/v1/projects/:pid', h(async (req, res) => {
|
|
324
|
+
const owner = await dashboard(req, res);
|
|
325
|
+
if (!owner)
|
|
326
|
+
return;
|
|
327
|
+
const p = await project(req, res);
|
|
328
|
+
if (!p)
|
|
329
|
+
return;
|
|
330
|
+
if (!p.owners.includes(owner))
|
|
331
|
+
return fail(res, 403, 'not_owner');
|
|
332
|
+
const body = (req.body ?? {});
|
|
333
|
+
const next = { ...p };
|
|
334
|
+
if (body.name !== undefined) {
|
|
335
|
+
const name = cleanName(body.name);
|
|
336
|
+
if (!name)
|
|
337
|
+
return fail(res, 400, 'bad_request');
|
|
338
|
+
next.name = name;
|
|
339
|
+
}
|
|
340
|
+
if (body.origins !== undefined) {
|
|
341
|
+
const origins = cleanOrigins(body.origins);
|
|
342
|
+
if (!origins)
|
|
343
|
+
return fail(res, 400, 'bad_request');
|
|
344
|
+
next.origins = origins;
|
|
345
|
+
}
|
|
346
|
+
if (body.owners !== undefined) {
|
|
347
|
+
// Never empty: a project nobody owns is a project nobody can answer.
|
|
348
|
+
const owners = cleanOwners(body.owners);
|
|
349
|
+
if (!owners)
|
|
350
|
+
return fail(res, 400, 'bad_request');
|
|
351
|
+
next.owners = owners;
|
|
352
|
+
}
|
|
353
|
+
if (body.rules !== undefined) {
|
|
354
|
+
const rules = mergeRules(p.rules, body.rules);
|
|
355
|
+
if (!rules)
|
|
356
|
+
return fail(res, 400, 'bad_request');
|
|
357
|
+
next.rules = rules;
|
|
358
|
+
}
|
|
359
|
+
if (body.loginMessages !== undefined) {
|
|
360
|
+
const messages = cleanLoginMessages(body.loginMessages);
|
|
361
|
+
if (!messages)
|
|
362
|
+
return fail(res, 400, 'bad_request');
|
|
363
|
+
if (messages.length)
|
|
364
|
+
next.loginMessages = messages;
|
|
365
|
+
else
|
|
366
|
+
delete next.loginMessages;
|
|
367
|
+
}
|
|
368
|
+
await store.putProject(next);
|
|
369
|
+
console.info(`[project] ${tag(owner)} changed ${p.id}`);
|
|
370
|
+
res.json(next);
|
|
371
|
+
}));
|
|
372
|
+
router.post('/v1/projects/:pid/seen', h(async (req, res) => {
|
|
373
|
+
const owner = await dashboard(req, res);
|
|
374
|
+
if (!owner)
|
|
375
|
+
return;
|
|
376
|
+
const p = await project(req, res);
|
|
377
|
+
if (!p)
|
|
378
|
+
return;
|
|
379
|
+
if (!p.owners.includes(owner))
|
|
380
|
+
return fail(res, 403, 'not_owner');
|
|
381
|
+
await store.setSeen(p.id, owner, now());
|
|
382
|
+
res.json({ ok: true });
|
|
383
|
+
}));
|
|
384
|
+
return router;
|
|
385
|
+
}
|
|
386
|
+
/** A wallet, short enough for a log line and not enough to identify anyone at a glance. */
|
|
387
|
+
function tag(address) {
|
|
388
|
+
const a = normalizeAddress(address);
|
|
389
|
+
return `${a.slice(0, 4)}…${a.slice(-4)}`;
|
|
390
|
+
}
|
|
391
|
+
//# sourceMappingURL=router.js.map
|