icex-dsh-fixer 0.2.3 → 0.2.5
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/index.js +74 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
* key events and native window bounds are not available to a browser plugin.
|
|
7
7
|
*/
|
|
8
8
|
import { spawn } from 'node:child_process'
|
|
9
|
-
import { existsSync } from 'node:fs'
|
|
9
|
+
import { existsSync, mkdirSync, readdirSync, renameSync } from 'node:fs'
|
|
10
|
+
import { homedir } from 'node:os'
|
|
10
11
|
import { dirname, join } from 'node:path'
|
|
11
12
|
import { fileURLToPath } from 'node:url'
|
|
12
13
|
|
|
@@ -18,6 +19,7 @@ const helper = join(here, 'native', 'icex-summon')
|
|
|
18
19
|
* @param ctx - owning Cordis context.
|
|
19
20
|
*/
|
|
20
21
|
export function apply(ctx) {
|
|
22
|
+
registerSessionDeleteRoute(ctx)
|
|
21
23
|
if (process.platform !== 'darwin') return
|
|
22
24
|
if (!existsSync(helper)) {
|
|
23
25
|
console.warn('[icex-dsh-fixer] native summon helper is missing: ' + helper)
|
|
@@ -54,3 +56,74 @@ export function apply(ctx) {
|
|
|
54
56
|
}
|
|
55
57
|
}, 'icex-dsh-fixer: native summon helper')
|
|
56
58
|
}
|
|
59
|
+
|
|
60
|
+
/** Session logs live under the DSH home; deleted logs move to a trash folder. */
|
|
61
|
+
const DSH_HOME = process.env.DSH_HOME && process.env.DSH_HOME !== '' ? process.env.DSH_HOME : join(homedir(), '.dsh')
|
|
62
|
+
const SESSIONS_ROOT = join(DSH_HOME, 'sessions')
|
|
63
|
+
const TRASH_ROOT = join(DSH_HOME, 'trash', 'sessions')
|
|
64
|
+
|
|
65
|
+
/** JSON response helper for the session-delete route. */
|
|
66
|
+
function jsonResponse(body, status = 200) {
|
|
67
|
+
return new Response(JSON.stringify(body), {
|
|
68
|
+
status,
|
|
69
|
+
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Every on-disk directory for one session id, across project slug directories. */
|
|
74
|
+
function sessionDirectories(id) {
|
|
75
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,200}$/.test(id)) return []
|
|
76
|
+
if (!existsSync(SESSIONS_ROOT)) return []
|
|
77
|
+
const found = []
|
|
78
|
+
for (const entry of readdirSync(SESSIONS_ROOT, { withFileTypes: true })) {
|
|
79
|
+
if (!entry.isDirectory() || entry.name.startsWith('.')) continue
|
|
80
|
+
const dir = join(SESSIONS_ROOT, entry.name, id)
|
|
81
|
+
if (existsSync(dir)) found.push(dir)
|
|
82
|
+
}
|
|
83
|
+
return found
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Move a session's logs to the trash; the row disappears from the next list read. */
|
|
87
|
+
async function handleDeleteSession(request) {
|
|
88
|
+
let body
|
|
89
|
+
try {
|
|
90
|
+
body = await request.json()
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return jsonResponse({ ok: false, error: 'body is not JSON' }, 400)
|
|
93
|
+
}
|
|
94
|
+
const id = typeof body?.id === 'string' ? body.id.trim() : ''
|
|
95
|
+
if (id === '') return jsonResponse({ ok: false, error: 'id is required' }, 400)
|
|
96
|
+
|
|
97
|
+
const sessions = request.ctx?.get?.('sessions')
|
|
98
|
+
try {
|
|
99
|
+
if (sessions !== undefined && typeof sessions.get === 'function' && sessions.get(id) !== undefined) {
|
|
100
|
+
return jsonResponse({ ok: false, error: 'session is open; close it first', code: 'live' }, 409)
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
// A registry probe must never block deletion of a closed session.
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const dirs = sessionDirectories(id)
|
|
107
|
+
if (dirs.length === 0) return jsonResponse({ ok: false, error: 'session log not found', code: 'missing' }, 404)
|
|
108
|
+
|
|
109
|
+
mkdirSync(TRASH_ROOT, { recursive: true })
|
|
110
|
+
const moved = []
|
|
111
|
+
for (const dir of dirs) {
|
|
112
|
+
const target = join(TRASH_ROOT, id + '-' + Date.now() + '-' + moved.length)
|
|
113
|
+
renameSync(dir, target)
|
|
114
|
+
moved.push(target)
|
|
115
|
+
}
|
|
116
|
+
return jsonResponse({ ok: true, moved })
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Claim the session-delete route when this host carries a browser connection. */
|
|
120
|
+
function registerSessionDeleteRoute(ctx) {
|
|
121
|
+
const connection = ctx.get('connection')
|
|
122
|
+
if (connection === undefined || connection.fetch === undefined || typeof connection.fetch.register !== 'function') return
|
|
123
|
+
ctx.effect(() => connection.fetch.register({
|
|
124
|
+
path: '/api/icex-fixer/delete-session',
|
|
125
|
+
methods: ['POST'],
|
|
126
|
+
requestBody: 'buffered',
|
|
127
|
+
fetch: (request) => handleDeleteSession(request),
|
|
128
|
+
}), 'icex-dsh-fixer: session delete route')
|
|
129
|
+
}
|
package/package.json
CHANGED