dsh-reading-inbox 0.2.1 → 0.3.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 +211 -208
- package/README.md +211 -208
- package/VERIFY.md +133 -126
- package/index.js +80 -80
- package/package.json +83 -83
- package/src/config.js +44 -44
- package/src/fetch.js +173 -173
- package/src/markdown.js +309 -309
- package/src/routes.js +276 -254
- package/src/search.js +109 -0
- package/src/store.js +741 -647
- package/src/tools.js +441 -438
- package/web/app.js +912 -785
- package/web/index.html +83 -74
- package/web/style.css +814 -739
package/src/routes.js
CHANGED
|
@@ -1,254 +1,276 @@
|
|
|
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 (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (segments.length ===
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return writeError(res,
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const
|
|
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
|
-
|
|
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
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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] === 'views') {
|
|
134
|
+
if (segments.length === 1) {
|
|
135
|
+
if (method === 'GET') {
|
|
136
|
+
return writeJson(res, 200, { ok: true, views: await inbox.listViews() })
|
|
137
|
+
}
|
|
138
|
+
if (method === 'POST') {
|
|
139
|
+
const body = await readJsonBody(req)
|
|
140
|
+
if (typeof body.name !== 'string' || typeof body.query !== 'string') {
|
|
141
|
+
return writeError(res, 400, '缺少 name 或 query')
|
|
142
|
+
}
|
|
143
|
+
const view = await inbox.saveView(body.name, body.query)
|
|
144
|
+
return writeJson(res, 200, { ok: true, view })
|
|
145
|
+
}
|
|
146
|
+
return writeError(res, 405, 'method not allowed')
|
|
147
|
+
}
|
|
148
|
+
if (segments.length === 2 && method === 'DELETE') {
|
|
149
|
+
const result = await inbox.deleteView(decodeSegment(segments[1]))
|
|
150
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
151
|
+
}
|
|
152
|
+
return writeError(res, 405, 'method not allowed')
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (segments[0] === 'clip' && segments.length === 1) {
|
|
156
|
+
if (method !== 'POST') return writeError(res, 405, 'method not allowed')
|
|
157
|
+
const body = await readJsonBody(req)
|
|
158
|
+
if (typeof body.url !== 'string' || body.url.trim() === '') {
|
|
159
|
+
return writeError(res, 400, '缺少 url')
|
|
160
|
+
}
|
|
161
|
+
const tags = Array.isArray(body.tags) ? body.tags : []
|
|
162
|
+
const result = await inbox.clip(body.url, {
|
|
163
|
+
tags,
|
|
164
|
+
force: body.force === true,
|
|
165
|
+
})
|
|
166
|
+
return writeJson(res, 200, { ok: true, clip: result })
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (segments[0] === 'items') {
|
|
170
|
+
if (segments.length === 1) {
|
|
171
|
+
if (method === 'GET') {
|
|
172
|
+
const result = await inbox.list({
|
|
173
|
+
q: url.searchParams.get('q') ?? '',
|
|
174
|
+
status: url.searchParams.get('status') ?? 'all',
|
|
175
|
+
tag: url.searchParams.get('tag') ?? '',
|
|
176
|
+
limit: url.searchParams.get('limit') ?? undefined,
|
|
177
|
+
offset: url.searchParams.get('offset') ?? undefined,
|
|
178
|
+
})
|
|
179
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
180
|
+
}
|
|
181
|
+
return writeError(res, 405, 'method not allowed')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const id = decodeSegment(segments[1])
|
|
185
|
+
if (segments.length === 2) {
|
|
186
|
+
if (method === 'GET') {
|
|
187
|
+
return writeJson(res, 200, { ok: true, item: await inbox.get(id) })
|
|
188
|
+
}
|
|
189
|
+
if (method === 'PATCH') {
|
|
190
|
+
const body = await readJsonBody(req)
|
|
191
|
+
const patch = {}
|
|
192
|
+
if (body.status !== undefined) patch.status = body.status
|
|
193
|
+
if (body.tags !== undefined) patch.tags = body.tags
|
|
194
|
+
return writeJson(res, 200, { ok: true, item: await inbox.update(id, patch) })
|
|
195
|
+
}
|
|
196
|
+
return writeError(res, 405, 'method not allowed')
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (segments.length === 3 && segments[2] === 'annotations') {
|
|
200
|
+
if (method === 'GET') {
|
|
201
|
+
const annotations = await inbox.listAnnotations(id)
|
|
202
|
+
return writeJson(res, 200, { ok: true, annotations, count: annotations.length })
|
|
203
|
+
}
|
|
204
|
+
if (method === 'POST') {
|
|
205
|
+
const body = await readJsonBody(req)
|
|
206
|
+
const annotation = await inbox.addAnnotation(id, body)
|
|
207
|
+
const annotations = await inbox.listAnnotations(id)
|
|
208
|
+
return writeJson(res, 200, { ok: true, annotation, annotations, count: annotations.length })
|
|
209
|
+
}
|
|
210
|
+
return writeError(res, 405, 'method not allowed')
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (segments.length === 4 && segments[2] === 'annotations') {
|
|
214
|
+
const annotationId = decodeSegment(segments[3])
|
|
215
|
+
if (method === 'PATCH') {
|
|
216
|
+
const body = await readJsonBody(req)
|
|
217
|
+
const annotation = await inbox.updateAnnotation(id, annotationId, body)
|
|
218
|
+
return writeJson(res, 200, { ok: true, annotation })
|
|
219
|
+
}
|
|
220
|
+
if (method === 'DELETE') {
|
|
221
|
+
const result = await inbox.deleteAnnotation(id, annotationId)
|
|
222
|
+
return writeJson(res, 200, { ok: true, ...result })
|
|
223
|
+
}
|
|
224
|
+
return writeError(res, 405, 'method not allowed')
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (segments.length === 3 && segments[2] === 'export') {
|
|
228
|
+
if (method !== 'GET') return writeError(res, 405, 'method not allowed')
|
|
229
|
+
const result = await inbox.export(id)
|
|
230
|
+
res.writeHead(200, {
|
|
231
|
+
...commonHeaders('text/markdown; charset=utf-8'),
|
|
232
|
+
'content-disposition': `attachment; filename="${result.meta.id}.md"`,
|
|
233
|
+
})
|
|
234
|
+
res.end(result.markdown)
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
writeError(res, 404, 'not found')
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function createRoutes({ inbox, webDir }) {
|
|
243
|
+
return [{
|
|
244
|
+
kind: 'prefix',
|
|
245
|
+
path: PREFIX,
|
|
246
|
+
handler: async (req, res) => {
|
|
247
|
+
if (!isLoopbackRequest(req)) {
|
|
248
|
+
writeError(res, 403, 'forbidden: loopback-only')
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
252
|
+
|
|
253
|
+
if (url.pathname === API_PREFIX || url.pathname.startsWith(`${API_PREFIX}/`)) {
|
|
254
|
+
try {
|
|
255
|
+
await handleApi(req, res, url, inbox)
|
|
256
|
+
} catch (error) {
|
|
257
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
258
|
+
writeError(res, 500, message)
|
|
259
|
+
}
|
|
260
|
+
return
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Relative assets and API calls in web/index.html assume the trailing
|
|
264
|
+
// slash. Redirect the bare prefix instead of serving a page that would
|
|
265
|
+
// resolve ./style.css and ./api against the DSH root.
|
|
266
|
+
if (url.pathname === PREFIX) {
|
|
267
|
+
res.writeHead(302, { location: `${PREFIX}/${url.search}` })
|
|
268
|
+
res.end()
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const rest = url.pathname.slice(PREFIX.length) || '/'
|
|
273
|
+
await serveStatic(res, webDir, rest)
|
|
274
|
+
},
|
|
275
|
+
}]
|
|
276
|
+
}
|
package/src/search.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
const STATUS_VALUES = new Set(['unread', 'reading', 'read', 'archived'])
|
|
2
|
+
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/
|
|
3
|
+
|
|
4
|
+
function cleanValue(value) {
|
|
5
|
+
return String(value ?? '').replace(/^["']|["']$/g, '').trim()
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Split a search string into free text and field filters:
|
|
10
|
+
* tag:security is:unread site:example.com after:2026-01-01
|
|
11
|
+
* before:2026-12-31 has:highlight has:annotation
|
|
12
|
+
*
|
|
13
|
+
* Unknown `field:value` tokens stay part of the free-text query so a URL or
|
|
14
|
+
* colon in normal prose is not silently swallowed.
|
|
15
|
+
*/
|
|
16
|
+
export function parseQuery(raw) {
|
|
17
|
+
const parsed = {
|
|
18
|
+
text: '',
|
|
19
|
+
statuses: [],
|
|
20
|
+
tags: [],
|
|
21
|
+
sites: [],
|
|
22
|
+
after: '',
|
|
23
|
+
before: '',
|
|
24
|
+
hasHighlight: false,
|
|
25
|
+
}
|
|
26
|
+
const tokens = String(raw ?? '').match(/(?:[^\s"]+|"[^"]*")+/g) ?? []
|
|
27
|
+
const text = []
|
|
28
|
+
|
|
29
|
+
for (const token of tokens) {
|
|
30
|
+
const tag = /^tag:(.+)$/i.exec(token)
|
|
31
|
+
if (tag !== null) {
|
|
32
|
+
const value = cleanValue(tag[1]).toLowerCase()
|
|
33
|
+
if (value !== '') parsed.tags.push(value)
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
const is = /^is:(.+)$/i.exec(token)
|
|
37
|
+
if (is !== null) {
|
|
38
|
+
const value = cleanValue(is[1]).toLowerCase()
|
|
39
|
+
if (STATUS_VALUES.has(value)) {
|
|
40
|
+
parsed.statuses.push(value)
|
|
41
|
+
} else if (value === 'highlight' || value === 'annotated' || value === 'annotation') {
|
|
42
|
+
parsed.hasHighlight = true
|
|
43
|
+
} else {
|
|
44
|
+
text.push(token)
|
|
45
|
+
}
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
const site = /^site:(.+)$/i.exec(token)
|
|
49
|
+
if (site !== null) {
|
|
50
|
+
const value = cleanValue(site[1]).toLowerCase()
|
|
51
|
+
if (value !== '') parsed.sites.push(value)
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
const after = /^after:(.+)$/i.exec(token)
|
|
55
|
+
if (after !== null && DATE_RE.test(cleanValue(after[1]))) {
|
|
56
|
+
parsed.after = cleanValue(after[1])
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
const before = /^before:(.+)$/i.exec(token)
|
|
60
|
+
if (before !== null && DATE_RE.test(cleanValue(before[1]))) {
|
|
61
|
+
parsed.before = cleanValue(before[1])
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
const has = /^has:(highlight|annotation|annotations)$/i.exec(token)
|
|
65
|
+
if (has !== null) {
|
|
66
|
+
parsed.hasHighlight = true
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
text.push(token)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
parsed.text = text.join(' ').trim()
|
|
73
|
+
return parsed
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function queryTerms(parsed) {
|
|
77
|
+
const terms = []
|
|
78
|
+
for (const term of String(parsed?.text ?? '').split(/\s+/)) {
|
|
79
|
+
const value = term.replace(/^["']|["']$/g, '').trim()
|
|
80
|
+
if (value !== '' && !terms.includes(value)) terms.push(value)
|
|
81
|
+
}
|
|
82
|
+
return terms
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Return a short plain-text window around the first matched term. The UI
|
|
87
|
+
* escapes this text; it is not HTML and must never be injected as markup.
|
|
88
|
+
*/
|
|
89
|
+
export function makeSnippet(text, terms, maxLength = 180) {
|
|
90
|
+
const normalized = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
91
|
+
if (normalized === '') return ''
|
|
92
|
+
const haystack = normalized.toLowerCase()
|
|
93
|
+
let index = -1
|
|
94
|
+
for (const term of terms) {
|
|
95
|
+
const candidate = String(term ?? '').toLowerCase()
|
|
96
|
+
if (candidate === '') continue
|
|
97
|
+
const found = haystack.indexOf(candidate)
|
|
98
|
+
if (found !== -1 && (index === -1 || found < index)) index = found
|
|
99
|
+
}
|
|
100
|
+
if (index === -1) {
|
|
101
|
+
return normalized.length > maxLength ? `${normalized.slice(0, maxLength).trimEnd()}…` : normalized
|
|
102
|
+
}
|
|
103
|
+
const before = Math.floor(maxLength * 0.38)
|
|
104
|
+
const start = Math.max(0, index - before)
|
|
105
|
+
const end = Math.min(normalized.length, start + maxLength)
|
|
106
|
+
const prefix = start > 0 ? '…' : ''
|
|
107
|
+
const suffix = end < normalized.length ? '…' : ''
|
|
108
|
+
return `${prefix}${normalized.slice(start, end)}${suffix}`
|
|
109
|
+
}
|