Browse Source

Send duplicate file SHAs with different paths

master
Tony Kovanen 9 years ago
parent
commit
bb22a4c542
  1. 13
      lib/hash.js
  2. 24
      lib/index.js

13
lib/hash.js

@ -9,11 +9,18 @@ import { readFile } from 'fs-promise';
*/ */
export default async function hashes (files) { export default async function hashes (files) {
const entries = await Promise.all(files.map(async (name) => { const map = new Map();
await Promise.all(files.map(async (name) => {
const data = await readFile(name); const data = await readFile(name);
return [hash(data), { name, data }]; const h = hash(data);
const entry = map.get(h);
if (entry) {
entry.names.push(name);
} else {
map.set(hash(data), { names: [name], data });
}
})); }));
return new Map(entries); return map;
} }
/** /**

24
lib/index.js

@ -82,13 +82,15 @@ export default class Now extends EventEmitter {
forceSync, forceSync,
name: pkg.name || basename(path), name: pkg.name || basename(path),
description: pkg.description, description: pkg.description,
files: Array.from(this._files).map(([sha, { data, name }]) => { files: [].concat.apply([], Array.from(this._files).map(([sha, { data, names }]) => {
return { return names.map((name) => {
sha, return {
size: data.length, sha,
file: toRelative(name, this._path) size: data.length,
}; file: toRelative(name, this._path)
}) };
});
}))
} }
}); });
if (this._debug) console.timeEnd('> [debug] /create'); if (this._debug) console.timeEnd('> [debug] /create');
@ -127,9 +129,9 @@ export default class Now extends EventEmitter {
const uploadChunk = () => { const uploadChunk = () => {
Promise.all(parts.shift().map((sha) => retry(async (bail) => { Promise.all(parts.shift().map((sha) => retry(async (bail) => {
const file = this._files.get(sha); const file = this._files.get(sha);
const { data, name } = file; const { data, names } = file;
if (this._debug) console.time(`> [debug] /sync ${name}`); if (this._debug) console.time(`> [debug] /sync ${names.join(' ')}`);
const stream = resumer().queue(data).end(); const stream = resumer().queue(data).end();
const res = await this._fetch('/sync', { const res = await this._fetch('/sync', {
@ -139,12 +141,12 @@ export default class Now extends EventEmitter {
'Content-Length': data.length, 'Content-Length': data.length,
'x-now-deployment-id': this._id, 'x-now-deployment-id': this._id,
'x-now-sha': sha, 'x-now-sha': sha,
'x-now-file': toRelative(name, this._path), 'x-now-file': names.map((name) => toRelative(name, this._path)).join(','),
'x-now-size': data.length 'x-now-size': data.length
}, },
body: stream body: stream
}); });
if (this._debug) console.timeEnd(`> [debug] /sync ${name}`); if (this._debug) console.timeEnd(`> [debug] /sync ${names.join(' ')}`);
// no retry on 4xx // no retry on 4xx
if (200 !== res.status && (400 <= res.status || 500 > res.status)) { if (200 !== res.status && (400 <= res.status || 500 > res.status)) {

Loading…
Cancel
Save