shared-hosting-sync 0.1.1 → 0.1.4

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/src/uploader.js CHANGED
@@ -2,6 +2,7 @@ import fs from 'node:fs'
2
2
  import path from 'node:path'
3
3
  import { validateRemotePath, relativeFile } from './paths.js'
4
4
  import { hashFile } from './checksum.js'
5
+ import { fingerprint, sameFile } from './core/fileFingerprint.js'
5
6
  import { isTransient } from './transport/reliable.js'
6
7
 
7
8
  const TMP_SUFFIX = '.devsync-uploading'
@@ -56,9 +57,9 @@ export function collectFiles(projectRoot, matcher, { includeIgnored = false, onE
56
57
 
57
58
  /**
58
59
  * Build the list of uploads without touching the network for writes.
59
- * Size differences upload immediately; equal sizes require a content match.
60
+ * Saved projects compare local metadata with SQLite. Explicit scans verify remote content.
60
61
  */
61
- export async function buildPlan(project, matcher, transport, { force = false, onProgress } = {}) {
62
+ export async function buildPlan(project, matcher, transport, { force = false, scan = false, onProgress } = {}) {
62
63
  const { localPath, remotePath } = project
63
64
  validateRemotePath(remotePath)
64
65
  if (!fs.statSync(localPath).isDirectory()) throw new Error('Local source must be a directory')
@@ -68,6 +69,9 @@ export async function buildPlan(project, matcher, transport, { force = false, on
68
69
  excludedCount++
69
70
  if (excluded.length < 200) excluded.push(entry)
70
71
  } })
72
+ const scope = project.syncScope
73
+ const saved = scope && !scan ? (await import('./core/syncState.js')).readSyncFiles(scope) : null
74
+ const matches = []
71
75
  const uploads = []
72
76
  const skipped = []
73
77
  let comparedBytes = 0
@@ -86,12 +90,22 @@ export async function buildPlan(project, matcher, transport, { force = false, on
86
90
 
87
91
  if (onProgress) onProgress({ phase: 'planning', item: { rel, remote, bytes: localStat.size }, index, total: files.length })
88
92
 
89
- const remoteStat = await transport.stat(remote)
90
- if (remoteStat?.isDir) throw new Error(`Remote file destination is a directory: ${remote}`)
91
- if (!force && remoteStat && remoteStat.size === localStat.size &&
92
- await transport.checksum(remote) === await hashFile(abs)) {
93
- skipped.push({ rel, bytes: localStat.size, reason: 'same content' })
94
- continue
93
+ const localFingerprint = fingerprint(localStat)
94
+ const item = { abs, rel, remote, bytes: localStat.size, dirs: ancestorsOf(remote), fingerprint: localFingerprint }
95
+ if (saved) {
96
+ if (!force && sameFile(saved.get(rel), localFingerprint)) {
97
+ skipped.push({ rel, bytes: localStat.size, reason: 'unchanged since last successful sync' })
98
+ continue
99
+ }
100
+ } else {
101
+ const remoteStat = await transport.stat(remote)
102
+ if (remoteStat?.isDir) throw new Error(`Remote file destination is a directory: ${remote}`)
103
+ if (!force && remoteStat && remoteStat.size === localStat.size &&
104
+ await transport.checksum(remote) === await hashFile(abs)) {
105
+ skipped.push({ rel, bytes: localStat.size, reason: 'same content' })
106
+ matches.push(item)
107
+ continue
108
+ }
95
109
  }
96
110
 
97
111
  uploads.push({
@@ -99,12 +113,22 @@ export async function buildPlan(project, matcher, transport, { force = false, on
99
113
  rel,
100
114
  remote,
101
115
  bytes: localStat.size,
116
+ fingerprint: localFingerprint,
102
117
  dirs: ancestorsOf(remote)
103
118
  })
104
119
  comparedBytes += localStat.size
105
120
  }
106
121
 
107
- return { uploads, skipped, excluded, excludedCount, totalFiles: files.length, comparedBytes }
122
+ return { uploads, skipped, excluded, excludedCount, totalFiles: files.length, comparedBytes, syncScope: scope, matches }
123
+ }
124
+
125
+ /** Scan server content without uploading; only confirmed matches become the baseline. */
126
+ export async function scanServer(project, matcher, transport, options = {}) {
127
+ if (!project.syncScope) throw new Error('A saved project is required for a server scan')
128
+ const plan = await buildPlan(project, matcher, transport, { ...options, force: false, scan: true })
129
+ const { replaceScannedFiles } = await import('./core/syncState.js')
130
+ replaceScannedFiles(project.syncScope, plan.matches)
131
+ return plan
108
132
  }
109
133
 
110
134
  /**
@@ -147,6 +171,10 @@ export async function runPlan(plan, transport, { dryRun = false, onProgress, onF
147
171
  }
148
172
  try {
149
173
  await uploadOne(transport, item)
174
+ if (plan.syncScope) {
175
+ const { recordUploadedFile } = await import('./core/syncState.js')
176
+ recordUploadedFile(plan.syncScope, item)
177
+ }
150
178
  done.push({ rel: item.rel, bytes: item.bytes })
151
179
  bytes += item.bytes
152
180
  if (onFileResult) onFileResult({ item, index: i, total: plan.uploads.length, success: true, dryRun: false, bytes })