@alpacachen/dsh-kanban 1.6.0 → 1.6.2

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.
Files changed (3) hide show
  1. package/index.js +32 -28
  2. package/lib/client.js +217 -134
  3. package/package.json +17 -16
package/index.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * - Workspace isolation: boards are keyed by workspaceId, one board per workspace.
9
9
  * - Persistence: ctx.fs writes <workspace.path>/.dsh-kanban.json.
10
10
  * - Agent tools: ctx.tools.register exposes 15 kanban_* tools.
11
- * - Browser API: ctx.get('webServer') registers routes under /api/kanban.
11
+ * - Browser API: ctx.get('connection') registers the authenticated /api/kanban route.
12
12
  *
13
13
  * Per-workspace data model (disk files include schemaVersion):
14
14
  * schemaVersion: 3
@@ -934,54 +934,58 @@ export function apply(ctx) {
934
934
  }
935
935
  }
936
936
 
937
- // ---- Browser API through the official webServer extension ----
937
+ // ---- Browser API through the authenticated Connection extension ----
938
938
  const MAX_HTTP_BODY = 1024 * 1024
939
- const sendJson = (res, status, value) => {
940
- res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
941
- res.end(JSON.stringify(value))
942
- }
943
- const httpHandler = async (req, res) => {
939
+ const sendJson = (status, value) => Response.json(value, { status })
940
+ const httpHandler = async (req) => {
944
941
  try {
945
- if (req.method !== 'POST') return sendJson(res, 405, { error: 'Method not allowed' })
946
- const contentType = String(req.headers && req.headers['content-type'] || '').toLowerCase()
947
- if (!contentType.startsWith('application/json')) return sendJson(res, 415, { error: 'Expected application/json' })
948
- const origin = req.headers && req.headers.origin
949
- const host = req.headers && req.headers.host
942
+ if (req.method !== 'POST') return sendJson(405, { error: 'Method not allowed' })
943
+ const chunks = []
944
+ let size = 0
945
+ for await (const chunk of req.body || []) {
946
+ size += chunk.length
947
+ // Drain rejected uploads without buffering them so Connection can send the error response.
948
+ if (size <= MAX_HTTP_BODY) chunks.push(chunk)
949
+ }
950
+ if (size > MAX_HTTP_BODY) return sendJson(413, { error: 'Request body too large' })
951
+ const contentType = (req.headers.get('content-type') || '').split(';', 1)[0].trim().toLowerCase()
952
+ if (contentType !== 'application/json') return sendJson(415, { error: 'Expected application/json' })
953
+ const origin = req.headers.get('origin')
954
+ const host = req.headers.get('host') || new URL(req.url).host
950
955
  if (origin && host) {
951
956
  let originHost = ''
952
957
  try { originHost = new URL(origin).host } catch {}
953
- if (originHost !== host) return sendJson(res, 403, { error: 'Cross-origin request denied' })
958
+ if (originHost !== host) return sendJson(403, { error: 'Cross-origin request denied' })
954
959
  }
955
960
 
956
- const chunks = []
957
- let size = 0
958
- for await (const chunk of req) {
959
- size += chunk.length
960
- if (size > MAX_HTTP_BODY) return sendJson(res, 413, { error: 'Request body too large' })
961
- chunks.push(chunk)
962
- }
963
961
  const raw = Buffer.concat(chunks).toString('utf8')
964
962
  const body = raw ? JSON.parse(raw) : {}
963
+ if (!isObj(body)) return sendJson(400, { error: 'Expected a JSON object' })
965
964
  const method = typeof body.method === 'string' ? body.method : 'get'
966
965
  const args = isObj(body.args) ? body.args : {}
967
966
  const workspaceId = typeof args.workspaceId === 'string' ? args.workspaceId : ''
968
967
  const workspace = workspaceId ? workspaceOfId(workspaceId) : undefined
969
- if (!workspace) return sendJson(res, 400, { error: 'Unknown workspace: ' + (workspaceId || '(missing)') })
968
+ if (!workspace) return sendJson(400, { error: 'Unknown workspace: ' + (workspaceId || '(missing)') })
970
969
  const result = await dispatch(workspace, method, args, 'human')
971
- return sendJson(res, result.error ? 400 : 200, result)
970
+ return sendJson(result.error ? 400 : 200, result)
972
971
  } catch (err) {
973
972
  const status = err instanceof SyntaxError ? 400 : 500
974
- return sendJson(res, status, { error: String((err && err.message) || err) })
973
+ return sendJson(status, { error: String((err && err.message) || err) })
975
974
  }
976
975
  }
977
976
 
978
977
  const routeState = { registered: false, timer: null, dispose: null, disposed: false, attempts: 0 }
979
978
  const registerRoute = () => {
980
979
  if (routeState.registered || routeState.disposed) return
981
- const webServer = ctx.get('webServer')
982
- if (webServer === undefined) return
980
+ const connection = ctx.get('connection')
981
+ if (connection === undefined) return
983
982
  try {
984
- const dispose = webServer.register({ kind: 'prefix', path: '/api/kanban', handler: httpHandler })
983
+ const dispose = connection.fetch.register({
984
+ path: '/api/kanban',
985
+ methods: ['POST'],
986
+ requestBody: 'streaming',
987
+ fetch: httpHandler,
988
+ })
985
989
  routeState.dispose = typeof dispose === 'function' ? dispose : null
986
990
  routeState.registered = true
987
991
  console.log('dsh-kanban: /api/kanban route registered')
@@ -1418,11 +1422,11 @@ export function apply(ctx) {
1418
1422
 
1419
1423
  for (const tool of tools) ctx.tools.register(tool)
1420
1424
 
1421
- return () => {
1425
+ return async () => {
1422
1426
  routeState.disposed = true
1423
1427
  if (routeState.timer) routeState.timer()
1424
1428
  routeState.timer = null
1425
- if (routeState.dispose) routeState.dispose()
1429
+ if (routeState.dispose) await routeState.dispose()
1426
1430
  routeState.dispose = null
1427
1431
  boards.clear()
1428
1432
  boardLoads.clear()