shared-hosting-sync 0.0.0-stage → 0.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.
Files changed (46) hide show
  1. package/.env.example +23 -0
  2. package/CHANGELOG.md +24 -0
  3. package/LICENSE +21 -0
  4. package/PHASES.md +147 -0
  5. package/PRODUCT.md +90 -0
  6. package/README.md +398 -2
  7. package/RELEASING.md +79 -0
  8. package/package.json +68 -4
  9. package/src/arguments.js +30 -0
  10. package/src/bin.js +8 -0
  11. package/src/checksum.js +18 -0
  12. package/src/cli.js +511 -0
  13. package/src/config.js +98 -0
  14. package/src/core/db.js +61 -0
  15. package/src/core/deployment.js +22 -0
  16. package/src/core/envResolver.js +98 -0
  17. package/src/core/envSchema.js +160 -0
  18. package/src/core/migrate.js +75 -0
  19. package/src/core/migrations/001_init.sql +67 -0
  20. package/src/core/migrations/002_operation_locks.sql +9 -0
  21. package/src/core/operationLock.js +54 -0
  22. package/src/core/projects.js +261 -0
  23. package/src/core/secrets.js +75 -0
  24. package/src/core/v1Migrate.js +82 -0
  25. package/src/ignore.js +148 -0
  26. package/src/metadata.js +3 -0
  27. package/src/onboarding.js +117 -0
  28. package/src/paths.js +23 -0
  29. package/src/prompt.js +54 -0
  30. package/src/server/browse.js +46 -0
  31. package/src/server/deployments.js +209 -0
  32. package/src/server/index.js +79 -0
  33. package/src/server/local.js +23 -0
  34. package/src/server/onboarding.js +203 -0
  35. package/src/server/routes.js +307 -0
  36. package/src/server/session.js +58 -0
  37. package/src/transport/ftp.js +100 -0
  38. package/src/transport/index.js +12 -0
  39. package/src/transport/reliable.js +50 -0
  40. package/src/transport/sftp.js +99 -0
  41. package/src/ui/deployments.js +129 -0
  42. package/src/ui/index.html +17 -0
  43. package/src/ui/style.css +129 -0
  44. package/src/ui/ui.js +289 -0
  45. package/src/uploader.js +160 -0
  46. package/src/watcher.js +122 -0
@@ -0,0 +1,307 @@
1
+ // routes.js — REST handlers, mounted by server/index.js.
2
+ //
3
+ // All mutating routes require `X-Devsync-Session`. GET /api/session does
4
+ // not (that's how the UI bootstraps).
5
+ //
6
+ // Errors are returned as { error: 'message' } with appropriate status.
7
+
8
+ import path from 'node:path'
9
+ import { name, version } from '../metadata.js'
10
+ import { mutateProject } from '../core/operationLock.js'
11
+ import { ENV_SCHEMA } from '../core/envSchema.js'
12
+ import { validateRemotePath } from '../paths.js'
13
+ import {
14
+ listProjects,
15
+ getProject,
16
+ createProject,
17
+ deleteProject,
18
+ listEnvOverrides,
19
+ setEnvOverrides,
20
+ setSecrets,
21
+ getSecrets,
22
+ hasSecrets
23
+ } from '../core/projects.js'
24
+ import { checkSession, readSessionToken } from './session.js'
25
+ import { browseRemote, normaliseRemotePath } from './browse.js'
26
+ import { credsFromResolved, resolveEnv } from '../core/envResolver.js'
27
+ import { createTransport } from '../transport/index.js'
28
+ import { browseLocal } from './local.js'
29
+ import { configurationView, validateConnection, safeConnectionError } from './onboarding.js'
30
+
31
+ function sessionOk(req, res) {
32
+ const tok = req.headers['x-devsync-session']
33
+ if (!checkSession(tok)) {
34
+ res.statusCode = 401
35
+ res.setHeader('content-type', 'application/json')
36
+ res.end(JSON.stringify({ error: 'session token required' }))
37
+ return false
38
+ }
39
+ return true
40
+ }
41
+
42
+ function json(res, status, obj) {
43
+ res.statusCode = status
44
+ res.setHeader('content-type', 'application/json; charset=utf-8')
45
+ res.end(JSON.stringify(obj))
46
+ }
47
+
48
+ function readJson(req) {
49
+ return new Promise((resolve, reject) => {
50
+ const chunks = []
51
+ let bytes = 0
52
+ req.on('data', (c) => {
53
+ bytes += c.length
54
+ if (bytes > 1024 * 1024) reject(new Error('Request body is too large'))
55
+ else chunks.push(c)
56
+ })
57
+ req.on('end', () => {
58
+ const buf = Buffer.concat(chunks)
59
+ if (!buf.length) return resolve({})
60
+ try { resolve(JSON.parse(buf.toString('utf8'))) }
61
+ catch (e) { reject(new Error('invalid JSON body')) }
62
+ })
63
+ req.on('error', reject)
64
+ })
65
+ }
66
+
67
+ /** Strip isSecret flags; redact secret values to a presence bool. */
68
+ function shapeOverrides(rows) {
69
+ return rows.map((r) => ({
70
+ key: r.key,
71
+ value: r.is_secret ? null : r.value,
72
+ isSecret: !!r.is_secret,
73
+ present: r.is_secret ? !!r.value || undefined : true
74
+ }))
75
+ }
76
+
77
+ export async function handle(req, res, { onboarding, deployments } = {}) {
78
+ const url = new URL(req.url, 'http://127.0.0.1')
79
+ const route = `${req.method} ${url.pathname}`
80
+
81
+ // ---------- public ----------
82
+ if (route === 'GET /api/health') {
83
+ return json(res, 200, { ok: true, name, version })
84
+ }
85
+
86
+ if (route === 'GET /api/session') {
87
+ // Loopback-only + same-origin = the browser is trusted. We hand back
88
+ // the session token so the UI can attach it to mutating requests.
89
+ // The token is also stored on disk at ~/.devsync/session.token.
90
+ const tok = readSessionToken()
91
+ if (!tok) return json(res, 503, { error: 'session token not initialised' })
92
+ return json(res, 200, { ok: true, token: tok })
93
+ }
94
+
95
+ if (route === 'GET /api/schema') {
96
+ return json(res, 200, {
97
+ keys: ENV_SCHEMA.map((s) => ({
98
+ key: s.key,
99
+ type: s.type,
100
+ required: !!s.required,
101
+ default: s.default !== undefined ? s.default : (s.defaultBy === 'transport' ? '__transport_default__' : undefined),
102
+ values: s.values,
103
+ min: s.min,
104
+ max: s.max,
105
+ label: s.label,
106
+ help: s.help,
107
+ secret: s.type === 'secret'
108
+ }))
109
+ })
110
+ }
111
+
112
+ // All routes below need auth.
113
+ if (!sessionOk(req, res)) return
114
+
115
+ const deploymentMatch = url.pathname.match(/^\/api\/projects\/(\d+)\/(activity|preview|deploy|sync|watch)(\/stop)?$/)
116
+ if (deploymentMatch) {
117
+ if (!deployments) return json(res, 503, { error: 'Deployment service is unavailable' })
118
+ const [, id, action, stop] = deploymentMatch
119
+ try {
120
+ if (req.method === 'GET' && action === 'activity' && !stop) return json(res, 200, deployments.activity(id))
121
+ if (req.method === 'POST' && action === 'watch' && stop) return json(res, 202, deployments.stop(id))
122
+ if (req.method === 'POST' && !stop && action !== 'activity') return json(res, 202, deployments.start(id, action, await readJson(req)))
123
+ return json(res, 405, { error: 'Method not allowed' })
124
+ } catch (error) { return json(res, error.statusCode || 400, { error: error.message }) }
125
+ }
126
+
127
+ if (route === 'GET /api/local') {
128
+ try { return json(res, 200, await browseLocal(url.searchParams.get('path'))) }
129
+ catch (error) { return json(res, 400, { error: error.message }) }
130
+ }
131
+
132
+ if (url.pathname === '/api/onboarding' || url.pathname.startsWith('/api/onboarding/')) {
133
+ if (!onboarding) return json(res, 503, { error: 'Setup service is unavailable' })
134
+ try {
135
+ if (route === 'POST /api/onboarding') return json(res, 201, onboarding.load(await readJson(req)))
136
+ const match = url.pathname.match(/^\/api\/onboarding\/([a-f0-9-]+)(?:\/(test|browse|save))?$/)
137
+ if (!match) return json(res, 404, { error: 'Setup route not found' })
138
+ const [, id, action] = match
139
+ if (req.method === 'PUT' && !action) return json(res, 200, onboarding.update(id, (await readJson(req)).entries))
140
+ if (req.method === 'DELETE' && !action) { onboarding.remove(id); return json(res, 200, { ok: true }) }
141
+ if (req.method === 'POST' && action === 'test') return json(res, 200, await onboarding.test(id))
142
+ if (req.method === 'POST' && action === 'browse') return json(res, 200, await onboarding.browse(id, (await readJson(req)).path || '/'))
143
+ if (req.method === 'POST' && action === 'save') return json(res, 200, await onboarding.save(id, (await readJson(req)).label))
144
+ return json(res, 405, { error: 'Method not allowed' })
145
+ } catch (error) { return json(res, error.statusCode || 400, { error: error.message }) }
146
+ }
147
+
148
+ if (route === 'GET /api/projects') {
149
+ const all = listProjects()
150
+ return json(res, 200, {
151
+ projects: all.map((p) => ({
152
+ id: p.id,
153
+ label: p.label,
154
+ localPath: p.localPath,
155
+ enabled: p.enabled,
156
+ lastStatus: p.lastStatus,
157
+ lastError: p.lastError,
158
+ lastSyncAt: p.lastSyncAt,
159
+ credsSet: hasSecrets(p.id)
160
+ }))
161
+ })
162
+ }
163
+
164
+ // ---------- project CRUD ----------
165
+ if (route === 'POST /api/projects' && req.method === 'POST') {
166
+ let body
167
+ try { body = await readJson(req) } catch (e) { return json(res, 400, { error: e.message }) }
168
+ if (!body.label || !body.localPath) return json(res, 400, { error: 'label and localPath required' })
169
+ try {
170
+ const p = createProject({ label: body.label, localPath: path.resolve(body.localPath) })
171
+ return json(res, 201, { id: p.id, label: p.label, localPath: p.localPath })
172
+ } catch (e) {
173
+ return json(res, 400, { error: e.message })
174
+ }
175
+ }
176
+
177
+ if (url.pathname.startsWith('/api/projects/') && req.method === 'DELETE') {
178
+ const id = Number(url.pathname.split('/').pop())
179
+ if (!Number.isFinite(id)) return json(res, 400, { error: 'bad id' })
180
+ try { deployments?.ensureEditable(id) } catch (error) { return json(res, 409, { error: error.message }) }
181
+ let ok
182
+ try { ok = mutateProject(id, () => deleteProject(id)) }
183
+ catch (error) { return json(res, error.statusCode || 400, { error: error.message }) }
184
+ return json(res, ok ? 200 : 404, { ok })
185
+ }
186
+
187
+ // ---------- project env (the main UI form) ----------
188
+ const envMatch = url.pathname.match(/^\/api\/projects\/(\d+)\/env$/)
189
+ if (envMatch) {
190
+ const id = Number(envMatch[1])
191
+ const proj = getProject(id)
192
+ if (!proj) return json(res, 404, { error: 'project not found' })
193
+
194
+ if (req.method === 'GET') {
195
+ const rows = listEnvOverrides(id)
196
+ const e = resolveEnv(id, { useEnvFileEnvironment: false })
197
+ return json(res, 200, {
198
+ label: proj.label,
199
+ localPath: proj.localPath,
200
+ overrides: shapeOverrides(rows),
201
+ effective: { ...e.values, DEVSYNC_PASSWORD: e.values.DEVSYNC_PASSWORD ? '***' : null, DEVSYNC_PRIVATE_KEY: e.values.DEVSYNC_PRIVATE_KEY ? '***' : null },
202
+ credsSet: hasSecrets(id),
203
+ configuration: configurationView(e)
204
+ })
205
+ }
206
+
207
+ if (req.method === 'PUT') {
208
+ try { deployments?.ensureEditable(id) } catch (error) { return json(res, 409, { error: error.message }) }
209
+ let body
210
+ try { body = await readJson(req) } catch (e) { return json(res, 400, { error: e.message }) }
211
+ // body = { entries: [{ key, value, isSecret }] }
212
+ if (!Array.isArray(body.entries)) return json(res, 400, { error: 'entries array required' })
213
+ // Separate secrets from overrides.
214
+ const overrides = []
215
+ const secrets = {}
216
+ let transport = 'sftp'
217
+ for (const e of body.entries) {
218
+ if (!e.key || !e.key.startsWith('DEVSYNC_')) return json(res, 400, { error: `bad key: ${e.key}` })
219
+ if (e.key === 'DEVSYNC_TRANSPORT') transport = e.value || 'sftp'
220
+ if (e.isSecret || e.key === 'DEVSYNC_PASSWORD') {
221
+ // Stored as a secret — only set if a non-empty value was supplied
222
+ if (e.value && e.value !== '***') secrets[e.key] = e.value
223
+ } else {
224
+ overrides.push({ key: e.key, value: e.value, isSecret: false })
225
+ }
226
+ }
227
+ // Validate entries
228
+ const errors = []
229
+ for (const e of body.entries) {
230
+ const s = ENV_SCHEMA.find((x) => x.key === e.key)
231
+ if (!s) { errors.push(`unknown key: ${e.key}`); continue }
232
+ const v = e.value
233
+ if (e.key === 'DEVSYNC_REMOTE_PATH') {
234
+ try { validateRemotePath(v) } catch (err) { errors.push(err.message) }
235
+ }
236
+ if (s.required && (!v || v === '')) errors.push(`${e.key} is required`)
237
+ if (s.type === 'enum' && v && !s.values.includes(v)) errors.push(`${e.key} must be one of ${s.values.join(', ')}`)
238
+ if (s.type === 'int' && v) {
239
+ const n = Number(v)
240
+ if (!Number.isInteger(n)) errors.push(`${e.key} must be an integer`)
241
+ else if (s.min != null && n < s.min) errors.push(`${e.key} must be >= ${s.min}`)
242
+ else if (s.max != null && n > s.max) errors.push(`${e.key} must be <= ${s.max}`)
243
+ }
244
+ }
245
+ if (errors.length) return json(res, 400, { error: errors.join('; ') })
246
+
247
+ // Preserve existing secrets we weren't given (so the UI doesn't have to
248
+ // re-enter the password to change other fields).
249
+ const existing = getSecrets(id) || {}
250
+ const merged = { ...existing, ...secrets }
251
+ // Strip out empty secrets we explicitly cleared
252
+ for (const e of body.entries) {
253
+ if ((e.isSecret || e.key === 'DEVSYNC_PASSWORD') && (!e.value || e.value === '')) {
254
+ delete merged[e.key]
255
+ }
256
+ }
257
+
258
+ try {
259
+ mutateProject(id, () => { setEnvOverrides(id, overrides); setSecrets(id, merged) })
260
+ } catch (error) { return json(res, error.statusCode || 400, { error: error.message }) }
261
+ return json(res, 200, { ok: true, credsSet: Object.keys(merged).length > 0 })
262
+ }
263
+ }
264
+
265
+ // ---------- browse ----------
266
+ if (route === 'GET /api/browse' || (url.pathname === '/api/browse' && req.method === 'GET')) {
267
+ const id = Number(url.searchParams.get('projectId'))
268
+ const sub = url.searchParams.get('path') || '/'
269
+ if (!Number.isFinite(id)) return json(res, 400, { error: 'projectId required' })
270
+ const proj = getProject(id)
271
+ if (!proj) return json(res, 404, { error: 'project not found' })
272
+ const r = resolveEnv(id, { useEnvFileEnvironment: false })
273
+ try { validateConnection(r.values) } catch (error) { return json(res, 400, { error: error.message }) }
274
+ const creds = credsFromResolved(r.values)
275
+ try {
276
+ const entries = await browseRemote(creds, normaliseRemotePath(sub))
277
+ return json(res, 200, { path: normaliseRemotePath(sub), entries })
278
+ } catch (e) {
279
+ return json(res, 502, { error: safeConnectionError(e, r.values) })
280
+ }
281
+ }
282
+
283
+ // ---------- test connection ----------
284
+ if (url.pathname === '/api/test' && req.method === 'POST') {
285
+ let body
286
+ try { body = await readJson(req) } catch (e) { return json(res, 400, { error: e.message }) }
287
+ const id = Number(body.projectId)
288
+ const proj = getProject(id)
289
+ if (!proj) return json(res, 404, { error: 'project not found' })
290
+ const r = resolveEnv(id, { useEnvFileEnvironment: false })
291
+ try { validateConnection(r.values) } catch (error) { return json(res, 400, { error: error.message }) }
292
+ const creds = credsFromResolved(r.values)
293
+ let t
294
+ try {
295
+ t = await createTransport(creds, creds.transport)
296
+ await t.list('/')
297
+ return json(res, 200, { ok: true, transport: creds.transport, host: creds.host })
298
+ } catch (e) {
299
+ return json(res, 502, { error: safeConnectionError(e, r.values) })
300
+ } finally {
301
+ if (t) await t.close()
302
+ }
303
+ }
304
+
305
+ // 404
306
+ json(res, 404, { error: `no route for ${route}` })
307
+ }
@@ -0,0 +1,58 @@
1
+ // session.js — single-use-on-bootstrap session token file.
2
+ //
3
+ // On `devsync ui` start we write a random 32-byte hex token to
4
+ // ~/.devsync/session.token (mode 0600). The UI fetches the token via
5
+ // GET /api/session (same-origin, loopback-only), then sends it as
6
+ // `X-Devsync-Session` on every mutating request.
7
+ //
8
+ // This is enough to keep the door closed against:
9
+ //
10
+ // - Other local processes on non-loopback hosts (server binds 127.0.0.1)
11
+ // - Cross-origin pages (no CORS headers)
12
+ // - Casual snooping (token is needed for POST/PUT/DELETE)
13
+ //
14
+ // It is NOT enough to keep out a determined attacker who can run code as
15
+ // the same user — that's the obfuscation-not-KMS trade-off and is
16
+ // documented.
17
+
18
+ import fs from 'node:fs'
19
+ import path from 'node:path'
20
+ import crypto from 'node:crypto'
21
+ import { HOME } from '../config.js'
22
+
23
+ const TOKEN_FILE = path.join(HOME, 'session.token')
24
+
25
+ function ensureHome() {
26
+ if (!fs.existsSync(HOME)) fs.mkdirSync(HOME, { recursive: true })
27
+ return HOME
28
+ }
29
+
30
+ export function writeSessionToken() {
31
+ ensureHome()
32
+ const tok = crypto.randomBytes(32).toString('hex')
33
+ fs.writeFileSync(TOKEN_FILE, tok, { mode: 0o600 })
34
+ try { fs.chmodSync(TOKEN_FILE, 0o600) } catch {}
35
+ return tok
36
+ }
37
+
38
+ export function readSessionToken() {
39
+ if (!fs.existsSync(TOKEN_FILE)) return null
40
+ return fs.readFileSync(TOKEN_FILE, 'utf8').trim()
41
+ }
42
+
43
+ export function checkSession(provided) {
44
+ const expected = readSessionToken()
45
+ if (!expected) return false
46
+ if (!provided) return false
47
+ // Both are hex strings of the same length — constant-time compare
48
+ if (provided.length !== expected.length) return false
49
+ try {
50
+ return crypto.timingSafeEqual(Buffer.from(provided, 'hex'), Buffer.from(expected, 'hex'))
51
+ } catch {
52
+ return false
53
+ }
54
+ }
55
+
56
+ export function sessionTokenPath() {
57
+ return TOKEN_FILE
58
+ }
@@ -0,0 +1,100 @@
1
+ import { Client } from 'basic-ftp'
2
+ import { hashSink } from '../checksum.js'
3
+
4
+ /**
5
+ * FTP/FTPS adapter.
6
+ * Note: the FTP protocol does not allow multiple requests in parallel, so
7
+ * transfers on a single control connection are inherently serial.
8
+ */
9
+ export class FtpTransport {
10
+ constructor(creds) {
11
+ this.creds = creds
12
+ this.client = null
13
+ }
14
+
15
+ async connect() {
16
+ const c = new Client(this.creds.timeoutMs)
17
+ try {
18
+ await c.access({
19
+ host: this.creds.host,
20
+ port: this.creds.port || 21,
21
+ user: this.creds.username,
22
+ password: this.creds.password,
23
+ secure: this.creds.secure,
24
+ secureOptions: this.creds.secureOptions
25
+ })
26
+ } catch (err) {
27
+ c.close()
28
+ throw err
29
+ }
30
+ this.client = c
31
+ return this
32
+ }
33
+
34
+ async list(remotePath) {
35
+ const items = await this.client.list(remotePath || '/')
36
+ return items.map((e) => ({
37
+ name: e.name,
38
+ type: e.isDirectory ? 'dir' : 'file',
39
+ size: e.size ?? 0,
40
+ mtime: e.modifyTime ? new Date(e.modifyTime) : null
41
+ }))
42
+ }
43
+
44
+ async ensureDir(remotePath) {
45
+ if (!remotePath) return
46
+ // cd() creates nothing; uploadToDir() does not create intermediate dirs.
47
+ // Walk up so each level exists before the next.
48
+ const parts = remotePath.split('/').filter(Boolean)
49
+ let acc = remotePath.startsWith('/') ? '' : '.'
50
+ for (const part of parts) {
51
+ acc = acc === '' || acc === '.' ? `${acc}/${part}` : `${acc}/${part}`
52
+ const current = acc.startsWith('/') ? acc : `/${acc}`
53
+ await this.client.ensureDir(current)
54
+ }
55
+ }
56
+
57
+ async exists(remotePath) {
58
+ try {
59
+ const size = await this.client.size(remotePath)
60
+ return size !== -1
61
+ } catch {
62
+ return false
63
+ }
64
+ }
65
+
66
+ async stat(remotePath) {
67
+ try {
68
+ const size = await this.client.size(remotePath)
69
+ return size === -1 ? null : { size, isDir: false }
70
+ } catch (err) {
71
+ if (err.code === 550) return null
72
+ throw err
73
+ }
74
+ }
75
+
76
+ async checksum(remotePath) {
77
+ const sink = hashSink()
78
+ await this.client.downloadTo(sink.stream, remotePath)
79
+ return sink.digest()
80
+ }
81
+
82
+ async put(localPath, remotePath) {
83
+ await this.client.uploadFrom(localPath, remotePath)
84
+ }
85
+
86
+ async rename(from, to) {
87
+ await this.client.rename(from, to)
88
+ }
89
+
90
+ async delete(remotePath) {
91
+ await this.client.remove(remotePath)
92
+ }
93
+
94
+ async close() {
95
+ if (this.client) {
96
+ this.client.close()
97
+ this.client = null
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,12 @@
1
+ import { SftpTransport } from './sftp.js'
2
+ import { FtpTransport } from './ftp.js'
3
+ import { ReliableTransport } from './reliable.js'
4
+
5
+ export async function createTransport(creds, override, options = {}) {
6
+ const kind = (override || creds.transport || 'sftp').toLowerCase()
7
+ const Adapter = kind === 'sftp' ? SftpTransport : ['ftp', 'ftps'].includes(kind) ? FtpTransport : null
8
+ if (!Adapter) throw new Error(`Unknown transport "${kind}". Use sftp, ftp or ftps.`)
9
+ return new ReliableTransport(() => new Adapter(creds).connect(), options).connect()
10
+ }
11
+
12
+ export { SftpTransport, FtpTransport }
@@ -0,0 +1,50 @@
1
+ import { setTimeout as delay } from 'node:timers/promises'
2
+
3
+ export function isTransient(error) {
4
+ if (['ECONNRESET', 'ECONNREFUSED', 'ECONNABORTED', 'ETIMEDOUT', 'EPIPE', 'ENOTCONN', 'EAI_AGAIN'].includes(error?.code)) return true
5
+ if ([421, 425, 426].includes(Number(error?.code))) return true
6
+ return /connection (?:closed|lost|reset|ended)|not connected|no sftp connection|socket (?:closed|hang up|not writable)|timed? ?out|client is closed/i.test(error?.message || '')
7
+ }
8
+
9
+ /** Retry reads and complete atomic uploads, never an ambiguous rename alone. */
10
+ export class ReliableTransport {
11
+ constructor(connect, { retries = 2, retryDelayMs = 250, onRetry = () => {} } = {}) {
12
+ this.connectRaw = connect
13
+ this.retries = retries
14
+ this.retryDelayMs = retryDelayMs
15
+ this.onRetry = onRetry
16
+ this.raw = null
17
+ this.closed = false
18
+ }
19
+
20
+ async connect() { await this.transaction(async () => {}); return this }
21
+
22
+ async transaction(action) {
23
+ for (let attempt = 0; ; attempt++) {
24
+ if (this.closed) throw new Error('Transport has been closed')
25
+ try {
26
+ this.raw ||= await this.connectRaw()
27
+ return await action(this.raw)
28
+ } catch (error) {
29
+ if (!isTransient(error) || attempt >= this.retries || this.closed) throw error
30
+ this.onRetry({ attempt: attempt + 1, retries: this.retries })
31
+ if (this.raw) await this.raw.close().catch(() => {})
32
+ this.raw = null
33
+ await delay(this.retryDelayMs * 2 ** attempt)
34
+ }
35
+ }
36
+ }
37
+
38
+ async close() {
39
+ this.closed = true
40
+ if (this.raw) await this.raw.close()
41
+ this.raw = null
42
+ }
43
+ }
44
+
45
+ for (const method of ['list', 'stat', 'checksum', 'exists', 'ensureDir', 'delete']) {
46
+ ReliableTransport.prototype[method] = function (...args) { return this.transaction((raw) => raw[method](...args)) }
47
+ }
48
+ for (const method of ['put', 'rename']) {
49
+ ReliableTransport.prototype[method] = function (...args) { return this.raw[method](...args) }
50
+ }
@@ -0,0 +1,99 @@
1
+ import fs from 'node:fs'
2
+ import SftpClient from 'ssh2-sftp-client'
3
+ import { hashSink } from '../checksum.js'
4
+ import { finished } from 'node:stream/promises'
5
+
6
+ /** SFTP adapter. Parallel-friendly — ftp() does not have the FTP serialisation limit. */
7
+ export class SftpTransport {
8
+ constructor(creds) {
9
+ this.creds = creds
10
+ this.client = null
11
+ }
12
+
13
+ async connect() {
14
+ const c = new SftpClient()
15
+ const opts = {
16
+ host: this.creds.host,
17
+ port: this.creds.port || 22,
18
+ username: this.creds.username,
19
+ readyTimeout: this.creds.timeoutMs
20
+ }
21
+ if (this.creds.privateKeyPath && fs.existsSync(this.creds.privateKeyPath)) {
22
+ opts.privateKey = fs.readFileSync(this.creds.privateKeyPath)
23
+ if (this.creds.password) opts.passphrase = this.creds.password
24
+ } else {
25
+ opts.password = this.creds.password
26
+ }
27
+ try {
28
+ await c.connect(opts)
29
+ } catch (err) {
30
+ await c.end().catch(() => {})
31
+ throw err
32
+ }
33
+ this.client = c
34
+ return this
35
+ }
36
+
37
+ async list(remotePath) {
38
+ const raw = await this.client.list(remotePath)
39
+ return raw
40
+ .filter((e) => e.name !== '.' && e.name !== '..')
41
+ .map((e) => ({
42
+ name: e.name,
43
+ type: e.type === 'd' ? 'dir' : 'file',
44
+ size: e.size ?? 0,
45
+ mtime: e.modifyTime ? new Date(e.modifyTime) : null
46
+ }))
47
+ }
48
+
49
+ async ensureDir(remotePath) {
50
+ if (!remotePath) return
51
+ const type = await this.client.exists(remotePath)
52
+ if (type && type !== 'd') throw new Error(`Remote directory path is occupied by a file: ${remotePath}`)
53
+ if (!type) await this.client.mkdir(remotePath, true)
54
+ }
55
+
56
+ async exists(remotePath) {
57
+ return this.client.exists(remotePath)
58
+ }
59
+
60
+ /** Returns a stats object when present, else null. */
61
+ async stat(remotePath) {
62
+ try {
63
+ const s = await this.client.stat(remotePath)
64
+ return { size: s.size, isDir: s.isDirectory }
65
+ } catch (err) {
66
+ if (err.code === 2 || err.code === 'ENOENT') return null
67
+ throw err
68
+ }
69
+ }
70
+
71
+ async checksum(remotePath) {
72
+ const sink = hashSink()
73
+ await this.client.get(remotePath, sink.stream)
74
+ await finished(sink.stream)
75
+ return sink.digest()
76
+ }
77
+
78
+ async put(localPath, remotePath) {
79
+ await this.client.fastPut(localPath, remotePath, {
80
+ concurrency: 4,
81
+ chunkSize: 32768
82
+ })
83
+ }
84
+
85
+ async rename(from, to) {
86
+ await this.client.rename(from, to)
87
+ }
88
+
89
+ async delete(remotePath) {
90
+ await this.client.delete(remotePath, true)
91
+ }
92
+
93
+ async close() {
94
+ if (this.client) {
95
+ await this.client.end().catch(() => {})
96
+ this.client = null
97
+ }
98
+ }
99
+ }