dsh-reading-inbox 0.2.1 → 0.4.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.en.md +214 -208
- package/README.md +214 -208
- package/VERIFY.md +139 -126
- package/index.js +81 -80
- package/package.json +83 -83
- package/src/config.js +44 -44
- package/src/fetch.js +173 -173
- package/src/markdown.js +316 -309
- package/src/routes.js +314 -254
- package/src/search.js +109 -0
- package/src/store.js +1000 -647
- package/src/tools.js +488 -438
- package/web/app.js +1014 -785
- package/web/index.html +102 -74
- package/web/style.css +865 -739
package/src/routes.js
CHANGED
|
@@ -1,254 +1,314 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
import { join } from 'node:path'
|
|
3
|
-
|
|
4
|
-
export const PREFIX = '/dsh-reading-inbox'
|
|
5
|
-
const API_PREFIX = `${PREFIX}/api`
|
|
6
|
-
|
|
7
|
-
const STATIC_FILES = new Map([
|
|
8
|
-
['/', ['index.html', 'text/html; charset=utf-8']],
|
|
9
|
-
['/index.html', ['index.html', 'text/html; charset=utf-8']],
|
|
10
|
-
['/app.js', ['app.js', 'text/javascript; charset=utf-8']],
|
|
11
|
-
['/style.css', ['style.css', 'text/css; charset=utf-8']],
|
|
12
|
-
['/favicon.svg', ['favicon.svg', 'image/svg+xml']],
|
|
13
|
-
])
|
|
14
|
-
|
|
15
|
-
const CSP = [
|
|
16
|
-
"default-src 'self'",
|
|
17
|
-
"img-src 'self' https: data:",
|
|
18
|
-
"style-src 'self' 'unsafe-inline'",
|
|
19
|
-
"script-src 'self'",
|
|
20
|
-
"connect-src 'self'",
|
|
21
|
-
"frame-ancestors 'self'",
|
|
22
|
-
"base-uri 'none'",
|
|
23
|
-
"form-action 'self'",
|
|
24
|
-
].join('; ')
|
|
25
|
-
|
|
26
|
-
function isLoopbackRequest(req) {
|
|
27
|
-
const address = req.socket?.remoteAddress
|
|
28
|
-
if (address !== '127.0.0.1' && address !== '::1' && address !== '::ffff:127.0.0.1') return false
|
|
29
|
-
const host = req.headers?.host
|
|
30
|
-
if (typeof host !== 'string') return false
|
|
31
|
-
let parsedHost
|
|
32
|
-
try {
|
|
33
|
-
parsedHost = new URL(`http://${host}`)
|
|
34
|
-
} catch {
|
|
35
|
-
return false
|
|
36
|
-
}
|
|
37
|
-
if (!['127.0.0.1', 'localhost', '[::1]'].includes(parsedHost.hostname)) return false
|
|
38
|
-
if (req.headers['sec-fetch-site'] === 'cross-site') return false
|
|
39
|
-
const origin = req.headers.origin
|
|
40
|
-
if (origin === undefined) return true
|
|
41
|
-
try {
|
|
42
|
-
return new URL(origin).host === parsedHost.host
|
|
43
|
-
} catch {
|
|
44
|
-
return false
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function commonHeaders(contentType) {
|
|
49
|
-
return {
|
|
50
|
-
'content-type': contentType,
|
|
51
|
-
'cache-control': 'no-store',
|
|
52
|
-
'referrer-policy': 'no-referrer',
|
|
53
|
-
'x-content-type-options': 'nosniff',
|
|
54
|
-
'content-security-policy': CSP,
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function writeJson(res, status, body) {
|
|
59
|
-
res.writeHead(status, commonHeaders('application/json; charset=utf-8'))
|
|
60
|
-
res.end(JSON.stringify(body))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function writeError(res, status, message) {
|
|
64
|
-
writeJson(res, status, { ok: false, error: message })
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async function readJsonBody(req, maxBytes = 64 * 1024) {
|
|
68
|
-
const chunks = []
|
|
69
|
-
let size = 0
|
|
70
|
-
for await (const chunk of req) {
|
|
71
|
-
size += chunk.length
|
|
72
|
-
if (size > maxBytes) throw new Error('请求体过大')
|
|
73
|
-
chunks.push(chunk)
|
|
74
|
-
}
|
|
75
|
-
if (chunks.length === 0) return {}
|
|
76
|
-
try {
|
|
77
|
-
const parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
78
|
-
return parsed !== null && typeof parsed === 'object' ? parsed : {}
|
|
79
|
-
} catch {
|
|
80
|
-
throw new Error('请求体不是合法 JSON')
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function decodeSegment(value) {
|
|
85
|
-
try {
|
|
86
|
-
return decodeURIComponent(value)
|
|
87
|
-
} catch {
|
|
88
|
-
return value
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function serveStatic(res, webDir, rest) {
|
|
93
|
-
const file = STATIC_FILES.get(rest)
|
|
94
|
-
if (file === undefined) {
|
|
95
|
-
writeError(res, 404, 'not found')
|
|
96
|
-
return
|
|
97
|
-
}
|
|
98
|
-
const [name, contentType] = file
|
|
99
|
-
try {
|
|
100
|
-
const body = await readFile(join(webDir, name))
|
|
101
|
-
res.writeHead(200, commonHeaders(contentType))
|
|
102
|
-
res.end(body)
|
|
103
|
-
} catch (error) {
|
|
104
|
-
writeError(res, 500, `读取界面文件失败:${error instanceof Error ? error.message : String(error)}`)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function handleApi(req, res, url, inbox) {
|
|
109
|
-
const rest = url.pathname.slice(API_PREFIX.length)
|
|
110
|
-
const segments = rest.split('/').filter(Boolean)
|
|
111
|
-
const method = req.method ?? 'GET'
|
|
112
|
-
|
|
113
|
-
if (segments.length === 0) {
|
|
114
|
-
writeError(res, 404, 'not found')
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (segments[0] === 'health' && segments.length === 1) {
|
|
119
|
-
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
120
|
-
return writeJson(res, 200, {
|
|
121
|
-
ok: true,
|
|
122
|
-
name: 'dsh-reading-inbox',
|
|
123
|
-
dir: inbox.dir,
|
|
124
|
-
warnings: inbox.warnings,
|
|
125
|
-
})
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (segments[0] === 'stats' && segments.length === 1) {
|
|
129
|
-
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
130
|
-
return writeJson(res, 200, { ok: true, ...(await inbox.stats()) })
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (segments[0] === '
|
|
134
|
-
if (method !== '
|
|
135
|
-
const
|
|
136
|
-
if (
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
return
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
res.
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
export const PREFIX = '/dsh-reading-inbox'
|
|
5
|
+
const API_PREFIX = `${PREFIX}/api`
|
|
6
|
+
|
|
7
|
+
const STATIC_FILES = new Map([
|
|
8
|
+
['/', ['index.html', 'text/html; charset=utf-8']],
|
|
9
|
+
['/index.html', ['index.html', 'text/html; charset=utf-8']],
|
|
10
|
+
['/app.js', ['app.js', 'text/javascript; charset=utf-8']],
|
|
11
|
+
['/style.css', ['style.css', 'text/css; charset=utf-8']],
|
|
12
|
+
['/favicon.svg', ['favicon.svg', 'image/svg+xml']],
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
const CSP = [
|
|
16
|
+
"default-src 'self'",
|
|
17
|
+
"img-src 'self' https: data:",
|
|
18
|
+
"style-src 'self' 'unsafe-inline'",
|
|
19
|
+
"script-src 'self'",
|
|
20
|
+
"connect-src 'self'",
|
|
21
|
+
"frame-ancestors 'self'",
|
|
22
|
+
"base-uri 'none'",
|
|
23
|
+
"form-action 'self'",
|
|
24
|
+
].join('; ')
|
|
25
|
+
|
|
26
|
+
function isLoopbackRequest(req) {
|
|
27
|
+
const address = req.socket?.remoteAddress
|
|
28
|
+
if (address !== '127.0.0.1' && address !== '::1' && address !== '::ffff:127.0.0.1') return false
|
|
29
|
+
const host = req.headers?.host
|
|
30
|
+
if (typeof host !== 'string') return false
|
|
31
|
+
let parsedHost
|
|
32
|
+
try {
|
|
33
|
+
parsedHost = new URL(`http://${host}`)
|
|
34
|
+
} catch {
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
if (!['127.0.0.1', 'localhost', '[::1]'].includes(parsedHost.hostname)) return false
|
|
38
|
+
if (req.headers['sec-fetch-site'] === 'cross-site') return false
|
|
39
|
+
const origin = req.headers.origin
|
|
40
|
+
if (origin === undefined) return true
|
|
41
|
+
try {
|
|
42
|
+
return new URL(origin).host === parsedHost.host
|
|
43
|
+
} catch {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function commonHeaders(contentType) {
|
|
49
|
+
return {
|
|
50
|
+
'content-type': contentType,
|
|
51
|
+
'cache-control': 'no-store',
|
|
52
|
+
'referrer-policy': 'no-referrer',
|
|
53
|
+
'x-content-type-options': 'nosniff',
|
|
54
|
+
'content-security-policy': CSP,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function writeJson(res, status, body) {
|
|
59
|
+
res.writeHead(status, commonHeaders('application/json; charset=utf-8'))
|
|
60
|
+
res.end(JSON.stringify(body))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function writeError(res, status, message) {
|
|
64
|
+
writeJson(res, status, { ok: false, error: message })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function readJsonBody(req, maxBytes = 64 * 1024) {
|
|
68
|
+
const chunks = []
|
|
69
|
+
let size = 0
|
|
70
|
+
for await (const chunk of req) {
|
|
71
|
+
size += chunk.length
|
|
72
|
+
if (size > maxBytes) throw new Error('请求体过大')
|
|
73
|
+
chunks.push(chunk)
|
|
74
|
+
}
|
|
75
|
+
if (chunks.length === 0) return {}
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
78
|
+
return parsed !== null && typeof parsed === 'object' ? parsed : {}
|
|
79
|
+
} catch {
|
|
80
|
+
throw new Error('请求体不是合法 JSON')
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function decodeSegment(value) {
|
|
85
|
+
try {
|
|
86
|
+
return decodeURIComponent(value)
|
|
87
|
+
} catch {
|
|
88
|
+
return value
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function serveStatic(res, webDir, rest) {
|
|
93
|
+
const file = STATIC_FILES.get(rest)
|
|
94
|
+
if (file === undefined) {
|
|
95
|
+
writeError(res, 404, 'not found')
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
const [name, contentType] = file
|
|
99
|
+
try {
|
|
100
|
+
const body = await readFile(join(webDir, name))
|
|
101
|
+
res.writeHead(200, commonHeaders(contentType))
|
|
102
|
+
res.end(body)
|
|
103
|
+
} catch (error) {
|
|
104
|
+
writeError(res, 500, `读取界面文件失败:${error instanceof Error ? error.message : String(error)}`)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function handleApi(req, res, url, inbox) {
|
|
109
|
+
const rest = url.pathname.slice(API_PREFIX.length)
|
|
110
|
+
const segments = rest.split('/').filter(Boolean)
|
|
111
|
+
const method = req.method ?? 'GET'
|
|
112
|
+
|
|
113
|
+
if (segments.length === 0) {
|
|
114
|
+
writeError(res, 404, 'not found')
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (segments[0] === 'health' && segments.length === 1) {
|
|
119
|
+
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
120
|
+
return writeJson(res, 200, {
|
|
121
|
+
ok: true,
|
|
122
|
+
name: 'dsh-reading-inbox',
|
|
123
|
+
dir: inbox.dir,
|
|
124
|
+
warnings: inbox.warnings,
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (segments[0] === 'stats' && segments.length === 1) {
|
|
129
|
+
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
130
|
+
return writeJson(res, 200, { ok: true, ...(await inbox.stats()) })
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (segments[0] === 'export' && segments.length === 1) {
|
|
134
|
+
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
135
|
+
const format = url.searchParams.get('format') || 'json'
|
|
136
|
+
if (format === 'markdown') {
|
|
137
|
+
const markdown = await inbox.exportAllMarkdown()
|
|
138
|
+
res.writeHead(200, {
|
|
139
|
+
...commonHeaders('text/markdown; charset=utf-8'),
|
|
140
|
+
'content-disposition': 'attachment; filename="dsh-reading-inbox.md"',
|
|
141
|
+
})
|
|
142
|
+
res.end(markdown)
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
if (format !== 'json') return writeError(res, 400, 'format 只支持 json 或 markdown')
|
|
146
|
+
const bundle = await inbox.exportBundle({
|
|
147
|
+
includeOriginal: url.searchParams.get('originals') === '1',
|
|
148
|
+
})
|
|
149
|
+
res.writeHead(200, {
|
|
150
|
+
...commonHeaders('application/json; charset=utf-8'),
|
|
151
|
+
'content-disposition': 'attachment; filename="dsh-reading-inbox.json"',
|
|
152
|
+
})
|
|
153
|
+
res.end(JSON.stringify(bundle, null, 2))
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (segments[0] === 'import' && segments.length === 1) {
|
|
158
|
+
if (method !== 'POST') return writeError(res, 405, 'method not allowed')
|
|
159
|
+
const body = await readJsonBody(req, 20 * 1024 * 1024)
|
|
160
|
+
const result = await inbox.importBundle(body)
|
|
161
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (segments[0] === 'save-text' && segments.length === 1) {
|
|
165
|
+
if (method !== 'POST') return writeError(res, 405, 'method not allowed')
|
|
166
|
+
const body = await readJsonBody(req, 2 * 1024 * 1024)
|
|
167
|
+
const result = await inbox.saveText(body)
|
|
168
|
+
return writeJson(res, 200, { ok: true, clip: result })
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (segments[0] === 'views') {
|
|
172
|
+
if (segments.length === 1) {
|
|
173
|
+
if (method === 'GET') {
|
|
174
|
+
return writeJson(res, 200, { ok: true, views: await inbox.listViews() })
|
|
175
|
+
}
|
|
176
|
+
if (method === 'POST') {
|
|
177
|
+
const body = await readJsonBody(req)
|
|
178
|
+
if (typeof body.name !== 'string' || typeof body.query !== 'string') {
|
|
179
|
+
return writeError(res, 400, '缺少 name 或 query')
|
|
180
|
+
}
|
|
181
|
+
const view = await inbox.saveView(body.name, body.query)
|
|
182
|
+
return writeJson(res, 200, { ok: true, view })
|
|
183
|
+
}
|
|
184
|
+
return writeError(res, 405, 'method not allowed')
|
|
185
|
+
}
|
|
186
|
+
if (segments.length === 2 && method === 'DELETE') {
|
|
187
|
+
const result = await inbox.deleteView(decodeSegment(segments[1]))
|
|
188
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
189
|
+
}
|
|
190
|
+
return writeError(res, 405, 'method not allowed')
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (segments[0] === 'clip' && segments.length === 1) {
|
|
194
|
+
if (method !== 'POST') return writeError(res, 405, 'method not allowed')
|
|
195
|
+
const body = await readJsonBody(req)
|
|
196
|
+
if (typeof body.url !== 'string' || body.url.trim() === '') {
|
|
197
|
+
return writeError(res, 400, '缺少 url')
|
|
198
|
+
}
|
|
199
|
+
const tags = Array.isArray(body.tags) ? body.tags : []
|
|
200
|
+
const result = await inbox.clip(body.url, {
|
|
201
|
+
tags,
|
|
202
|
+
force: body.force === true,
|
|
203
|
+
})
|
|
204
|
+
return writeJson(res, 200, { ok: true, clip: result })
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (segments[0] === 'items') {
|
|
208
|
+
if (segments.length === 1) {
|
|
209
|
+
if (method === 'GET') {
|
|
210
|
+
const result = await inbox.list({
|
|
211
|
+
q: url.searchParams.get('q') ?? '',
|
|
212
|
+
status: url.searchParams.get('status') ?? 'all',
|
|
213
|
+
tag: url.searchParams.get('tag') ?? '',
|
|
214
|
+
limit: url.searchParams.get('limit') ?? undefined,
|
|
215
|
+
offset: url.searchParams.get('offset') ?? undefined,
|
|
216
|
+
})
|
|
217
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
218
|
+
}
|
|
219
|
+
return writeError(res, 405, 'method not allowed')
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const id = decodeSegment(segments[1])
|
|
223
|
+
if (segments.length === 2) {
|
|
224
|
+
if (method === 'GET') {
|
|
225
|
+
return writeJson(res, 200, { ok: true, item: await inbox.get(id) })
|
|
226
|
+
}
|
|
227
|
+
if (method === 'PATCH') {
|
|
228
|
+
const body = await readJsonBody(req)
|
|
229
|
+
const patch = {}
|
|
230
|
+
if (body.status !== undefined) patch.status = body.status
|
|
231
|
+
if (body.tags !== undefined) patch.tags = body.tags
|
|
232
|
+
return writeJson(res, 200, { ok: true, item: await inbox.update(id, patch) })
|
|
233
|
+
}
|
|
234
|
+
return writeError(res, 405, 'method not allowed')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (segments.length === 3 && segments[2] === 'annotations') {
|
|
238
|
+
if (method === 'GET') {
|
|
239
|
+
const annotations = await inbox.listAnnotations(id)
|
|
240
|
+
return writeJson(res, 200, { ok: true, annotations, count: annotations.length })
|
|
241
|
+
}
|
|
242
|
+
if (method === 'POST') {
|
|
243
|
+
const body = await readJsonBody(req)
|
|
244
|
+
const annotation = await inbox.addAnnotation(id, body)
|
|
245
|
+
const annotations = await inbox.listAnnotations(id)
|
|
246
|
+
return writeJson(res, 200, { ok: true, annotation, annotations, count: annotations.length })
|
|
247
|
+
}
|
|
248
|
+
return writeError(res, 405, 'method not allowed')
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (segments.length === 4 && segments[2] === 'annotations') {
|
|
252
|
+
const annotationId = decodeSegment(segments[3])
|
|
253
|
+
if (method === 'PATCH') {
|
|
254
|
+
const body = await readJsonBody(req)
|
|
255
|
+
const annotation = await inbox.updateAnnotation(id, annotationId, body)
|
|
256
|
+
return writeJson(res, 200, { ok: true, annotation })
|
|
257
|
+
}
|
|
258
|
+
if (method === 'DELETE') {
|
|
259
|
+
const result = await inbox.deleteAnnotation(id, annotationId)
|
|
260
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
261
|
+
}
|
|
262
|
+
return writeError(res, 405, 'method not allowed')
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (segments.length === 3 && segments[2] === 'export') {
|
|
266
|
+
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
267
|
+
const result = await inbox.export(id)
|
|
268
|
+
res.writeHead(200, {
|
|
269
|
+
...commonHeaders('text/markdown; charset=utf-8'),
|
|
270
|
+
'content-disposition': `attachment; filename="${result.meta.id}.md"`,
|
|
271
|
+
})
|
|
272
|
+
res.end(result.markdown)
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
writeError(res, 404, 'not found')
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function createRoutes({ inbox, webDir }) {
|
|
281
|
+
return [{
|
|
282
|
+
kind: 'prefix',
|
|
283
|
+
path: PREFIX,
|
|
284
|
+
handler: async (req, res) => {
|
|
285
|
+
if (!isLoopbackRequest(req)) {
|
|
286
|
+
writeError(res, 403, 'forbidden: loopback-only')
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
290
|
+
|
|
291
|
+
if (url.pathname === API_PREFIX || url.pathname.startsWith(`${API_PREFIX}/`)) {
|
|
292
|
+
try {
|
|
293
|
+
await handleApi(req, res, url, inbox)
|
|
294
|
+
} catch (error) {
|
|
295
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
296
|
+
writeError(res, 500, message)
|
|
297
|
+
}
|
|
298
|
+
return
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Relative assets and API calls in web/index.html assume the trailing
|
|
302
|
+
// slash. Redirect the bare prefix instead of serving a page that would
|
|
303
|
+
// resolve ./style.css and ./api against the DSH root.
|
|
304
|
+
if (url.pathname === PREFIX) {
|
|
305
|
+
res.writeHead(302, { location: `${PREFIX}/${url.search}` })
|
|
306
|
+
res.end()
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const rest = url.pathname.slice(PREFIX.length) || '/'
|
|
311
|
+
await serveStatic(res, webDir, rest)
|
|
312
|
+
},
|
|
313
|
+
}]
|
|
314
|
+
}
|