dsh-matrix-todo 1.0.1 → 1.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/CHANGELOG.md +19 -0
- package/LICENSE +1 -1
- package/README.md +101 -159
- package/client.js +47 -4
- package/cordis.patch.yml +4 -4
- package/icon.svg +7 -7
- package/index.js +123 -122
- package/lib/store.js +511 -469
- package/locale/en.json +5 -5
- package/locale/zh.json +5 -5
- package/package.json +5 -5
package/index.js
CHANGED
|
@@ -1,122 +1,123 @@
|
|
|
1
|
-
/** Host half of the matrix-todo bundle: one JSON route over the file-backed store. */
|
|
2
|
-
|
|
3
|
-
import { createStore, resolveDataFile } from './lib/store.js'
|
|
4
|
-
|
|
5
|
-
export const name = 'dsh-matrix-todo'
|
|
6
|
-
export const inject = ['webServer']
|
|
7
|
-
|
|
8
|
-
const PREFIX = '/matrix-todo/api/'
|
|
9
|
-
const STATUS = { 'invalid-input': 400, 'not-found': 404, 'io-error': 500 }
|
|
10
|
-
|
|
11
|
-
function sendJson(res, status, body, extraHeaders) {
|
|
12
|
-
const text = JSON.stringify(body)
|
|
13
|
-
res.writeHead(status, {
|
|
14
|
-
'content-type': 'application/json; charset=utf-8',
|
|
15
|
-
'cache-control': 'no-store',
|
|
16
|
-
...extraHeaders,
|
|
17
|
-
})
|
|
18
|
-
res.end(text)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Body is JSON; an empty body and malformed JSON both count as {}. */
|
|
22
|
-
async function readJson(req) {
|
|
23
|
-
const chunks = []
|
|
24
|
-
for await (const chunk of req) chunks.push(chunk)
|
|
25
|
-
if (chunks.length === 0) return {}
|
|
26
|
-
try {
|
|
27
|
-
const parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
28
|
-
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
|
|
29
|
-
return parsed
|
|
30
|
-
} catch {
|
|
31
|
-
return {}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function isLoopbackPeer(address) {
|
|
36
|
-
if (address === undefined) return false
|
|
37
|
-
const plain = address.startsWith('::ffff:') ? address.slice('::ffff:'.length) : address
|
|
38
|
-
return plain === '::1' || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(plain)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Same-origin / loopback fence. The connection service is authoritative when present;
|
|
43
|
-
* otherwise fall back to the same checks the harness engine uses for local routes.
|
|
44
|
-
* Returns undefined when allowed, or the HTTP status of the refusal.
|
|
45
|
-
*/
|
|
46
|
-
function refusalOf(ctx, req) {
|
|
47
|
-
const connection = ctx.get('connection')
|
|
48
|
-
if (typeof connection?.requestRejection === 'function') return connection.requestRejection(req)
|
|
49
|
-
if (!isLoopbackPeer(req.socket?.remoteAddress)) return 403
|
|
50
|
-
const host = req.headers.host
|
|
51
|
-
if (host !== undefined) {
|
|
52
|
-
if (!URL.canParse(`http://${host}`)) return 403
|
|
53
|
-
const name = new URL(`http://${host}`).hostname
|
|
54
|
-
if (name !== 'localhost' && name !== '[::1]' && !/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(name)) return 403
|
|
55
|
-
}
|
|
56
|
-
const site = req.headers['sec-fetch-site']
|
|
57
|
-
if (site !== undefined && site !== 'same-origin' && site !== 'none') return 403
|
|
58
|
-
const origin = req.headers.origin
|
|
59
|
-
if (origin === undefined) return undefined
|
|
60
|
-
if (!URL.canParse(origin)) return 403
|
|
61
|
-
return new URL(origin).host === host ? undefined : 403
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const ACTIONS = {
|
|
65
|
-
list: (store) => store.list(),
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
1
|
+
/** Host half of the matrix-todo bundle: one JSON route over the file-backed store. */
|
|
2
|
+
|
|
3
|
+
import { createStore, resolveDataFile } from './lib/store.js'
|
|
4
|
+
|
|
5
|
+
export const name = 'dsh-matrix-todo'
|
|
6
|
+
export const inject = ['webServer']
|
|
7
|
+
|
|
8
|
+
const PREFIX = '/matrix-todo/api/'
|
|
9
|
+
const STATUS = { 'invalid-input': 400, 'not-found': 404, 'io-error': 500 }
|
|
10
|
+
|
|
11
|
+
function sendJson(res, status, body, extraHeaders) {
|
|
12
|
+
const text = JSON.stringify(body)
|
|
13
|
+
res.writeHead(status, {
|
|
14
|
+
'content-type': 'application/json; charset=utf-8',
|
|
15
|
+
'cache-control': 'no-store',
|
|
16
|
+
...extraHeaders,
|
|
17
|
+
})
|
|
18
|
+
res.end(text)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Body is JSON; an empty body and malformed JSON both count as {}. */
|
|
22
|
+
async function readJson(req) {
|
|
23
|
+
const chunks = []
|
|
24
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
25
|
+
if (chunks.length === 0) return {}
|
|
26
|
+
try {
|
|
27
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
28
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
|
|
29
|
+
return parsed
|
|
30
|
+
} catch {
|
|
31
|
+
return {}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isLoopbackPeer(address) {
|
|
36
|
+
if (address === undefined) return false
|
|
37
|
+
const plain = address.startsWith('::ffff:') ? address.slice('::ffff:'.length) : address
|
|
38
|
+
return plain === '::1' || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(plain)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Same-origin / loopback fence. The connection service is authoritative when present;
|
|
43
|
+
* otherwise fall back to the same checks the harness engine uses for local routes.
|
|
44
|
+
* Returns undefined when allowed, or the HTTP status of the refusal.
|
|
45
|
+
*/
|
|
46
|
+
function refusalOf(ctx, req) {
|
|
47
|
+
const connection = ctx.get('connection')
|
|
48
|
+
if (typeof connection?.requestRejection === 'function') return connection.requestRejection(req)
|
|
49
|
+
if (!isLoopbackPeer(req.socket?.remoteAddress)) return 403
|
|
50
|
+
const host = req.headers.host
|
|
51
|
+
if (host !== undefined) {
|
|
52
|
+
if (!URL.canParse(`http://${host}`)) return 403
|
|
53
|
+
const name = new URL(`http://${host}`).hostname
|
|
54
|
+
if (name !== 'localhost' && name !== '[::1]' && !/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(name)) return 403
|
|
55
|
+
}
|
|
56
|
+
const site = req.headers['sec-fetch-site']
|
|
57
|
+
if (site !== undefined && site !== 'same-origin' && site !== 'none') return 403
|
|
58
|
+
const origin = req.headers.origin
|
|
59
|
+
if (origin === undefined) return undefined
|
|
60
|
+
if (!URL.canParse(origin)) return 403
|
|
61
|
+
return new URL(origin).host === host ? undefined : 403
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const ACTIONS = {
|
|
65
|
+
list: (store) => store.list(),
|
|
66
|
+
seed: (store, body) => store.seed(body),
|
|
67
|
+
create: (store, body) => store.create(body),
|
|
68
|
+
update: (store, body) => store.update(body),
|
|
69
|
+
toggle: (store, body) => store.toggle(body),
|
|
70
|
+
remove: (store, body) => store.remove(body),
|
|
71
|
+
clearCompleted: (store, body) => store.clearCompleted(body),
|
|
72
|
+
reorder: (store, body) => store.reorder(body),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Every response carries the full list plus the layout preferences. */
|
|
76
|
+
function resultOf(store) {
|
|
77
|
+
return { todos: store.list(), settings: store.settings(), seeded: store.seeded() }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function apply(ctx, config = {}) {
|
|
81
|
+
// config.dataPath: optional override for the data file
|
|
82
|
+
// (default: %DSH_PROFILE_DIR%/data/dsh-matrix-todo/todos.json).
|
|
83
|
+
const store = createStore({ file: resolveDataFile(config.dataPath) })
|
|
84
|
+
const ready = store.load()
|
|
85
|
+
void ready.catch(() => {}) // keep load failures out of the unhandled-rejection path; callers still await it
|
|
86
|
+
|
|
87
|
+
ctx.effect(() => ctx.webServer.register({
|
|
88
|
+
kind: 'prefix',
|
|
89
|
+
path: '/matrix-todo/api',
|
|
90
|
+
handler: async (req, res) => {
|
|
91
|
+
try {
|
|
92
|
+
if (refusalOf(ctx, req) !== undefined) {
|
|
93
|
+
sendJson(res, 403, { ok: false, error: { code: 'forbidden', message: '请求来源不受信任' } })
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
if (req.method !== 'POST') {
|
|
97
|
+
sendJson(res, 405, { ok: false, error: { code: 'method', message: '只支持 POST' } }, { allow: 'POST' })
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
const pathname = new URL(req.url ?? '/', 'http://dsh.internal').pathname
|
|
101
|
+
const action = pathname.startsWith(PREFIX) ? pathname.slice(PREFIX.length) : ''
|
|
102
|
+
const known = action === 'saveSettings' || Object.hasOwn(ACTIONS, action)
|
|
103
|
+
if (action === '' || action.includes('/') || !known) {
|
|
104
|
+
sendJson(res, 404, { ok: false, error: { code: 'not-found', message: '未知接口' } })
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
await ready
|
|
108
|
+
const body = await readJson(req)
|
|
109
|
+
if (action === 'saveSettings') await store.saveSettings(body)
|
|
110
|
+
else await ACTIONS[action](store, body)
|
|
111
|
+
sendJson(res, 200, { ok: true, result: resultOf(store) })
|
|
112
|
+
} catch (err) {
|
|
113
|
+
const code = typeof err?.code === 'string' && err.code !== '' ? err.code : 'internal'
|
|
114
|
+
sendJson(res, STATUS[code] ?? 500, {
|
|
115
|
+
ok: false,
|
|
116
|
+
error: { code, message: err?.message ?? '待办操作失败' },
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
}), 'matrix-todo: http')
|
|
121
|
+
|
|
122
|
+
ctx.effect(() => () => { void store.close() }, 'matrix-todo: store')
|
|
123
|
+
}
|