Leo Lamprecht
8 years ago
No known key found for this signature in database
GPG Key ID: EF804E3FF4BBA8AB
2 changed files with
30 additions and
9 deletions
-
lib/hash.js
-
lib/index.js
|
@ -1,20 +1,41 @@ |
|
|
// Native
|
|
|
// Native
|
|
|
import {createHash} from 'crypto' |
|
|
import {createHash} from 'crypto' |
|
|
|
|
|
import path from 'path' |
|
|
|
|
|
|
|
|
// Packages
|
|
|
// Packages
|
|
|
import {readFile} from 'fs-promise' |
|
|
import {readFile} from 'fs-promise' |
|
|
|
|
|
|
|
|
/** |
|
|
const listPackage = { |
|
|
|
|
|
name: 'static', |
|
|
|
|
|
version: '0.0.0', |
|
|
|
|
|
scripts: { |
|
|
|
|
|
start: 'list ./content' |
|
|
|
|
|
}, |
|
|
|
|
|
dependencies: { |
|
|
|
|
|
list: 'latest' |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
* Computes hashes for the contents of each file given. |
|
|
* Computes hashes for the contents of each file given. |
|
|
* |
|
|
* |
|
|
* @param {Array} of {String} full paths |
|
|
* @param {Array} of {String} full paths |
|
|
* @return {Map} |
|
|
* @return {Map} |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
export default async function hashes(files) { |
|
|
export default async function hashes(files, isStatic) { |
|
|
const map = new Map() |
|
|
const map = new Map() |
|
|
await Promise.all(files.map(async name => { |
|
|
await Promise.all(files.map(async name => { |
|
|
const data = await readFile(name) |
|
|
const filename = path.basename(name) |
|
|
|
|
|
let data |
|
|
|
|
|
|
|
|
|
|
|
if (isStatic && filename === 'package.json') { |
|
|
|
|
|
const packageString = JSON.stringify(listPackage, null, 2) |
|
|
|
|
|
data = Buffer.from(packageString) |
|
|
|
|
|
} else { |
|
|
|
|
|
data = await readFile(name) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const h = hash(data) |
|
|
const h = hash(data) |
|
|
const entry = map.get(h) |
|
|
const entry = map.get(h) |
|
|
if (entry) { |
|
|
if (entry) { |
|
|
|
@ -102,7 +102,7 @@ export default class Now extends EventEmitter { |
|
|
console.time('> [debug] Computing hashes') |
|
|
console.time('> [debug] Computing hashes') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const hashes = await hash(files) |
|
|
const hashes = await hash(files, isStatic) |
|
|
|
|
|
|
|
|
if (this._debug) { |
|
|
if (this._debug) { |
|
|
console.timeEnd('> [debug] Computing hashes') |
|
|
console.timeEnd('> [debug] Computing hashes') |
|
|