-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from auth0/add-hash-files
Add ability to generate digest of uploaded files (and upload them too)
- Loading branch information
Showing
29 changed files
with
2,413 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const walk = require('walk'); | ||
const path = require('path'); | ||
const Rx = require('rx'); | ||
|
||
module.exports = { | ||
write: (filePath, contents) => fs.writeFileSync(filePath, contents, { encoding:'utf8' }), | ||
walk: (directory) => { | ||
return Rx.Observable.create((observer) => { | ||
const walker = walk.walk(directory, { followLinks: false }); | ||
walker.on('file', (root, stats, next) => { | ||
const workingDir = process.cwd(); | ||
const localPath = path.relative(workingDir, path.resolve(root, stats.name)); | ||
observer.next(localPath); | ||
next(); | ||
}); | ||
walker.on('error', (root, stats, next) => { | ||
observer.onError(stats.error); | ||
next(); | ||
}); | ||
walker.on('end', () => { | ||
observer.onCompleted(); | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const Rx = require('rx'); | ||
const from = Rx.Observable.from; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const crypto = require('crypto'); | ||
|
||
const availableDigests = ['sha256', 'sha384', 'sha512']; | ||
|
||
const fromFile = (file, hash) => { | ||
return Rx.Observable.create((observer) => { | ||
const fd = fs.createReadStream(file); | ||
fd.on('end', function() { | ||
hash.end(); | ||
const value = hash.read(); | ||
observer.next(value); | ||
observer.onCompleted(); | ||
}); | ||
fd.on('error', (error) => observer.onError(error)); | ||
fd.pipe(hash); | ||
return () => {}; | ||
}); | ||
}; | ||
|
||
const digest = (method, file) => { | ||
const hash = crypto.createHash(method); | ||
hash.setEncoding('base64'); | ||
return fromFile(file, hash).map((value) => { | ||
return { file, digest: value, method }; | ||
}); | ||
}; | ||
|
||
module.exports = (file, options) => { | ||
const digests = availableDigests.filter((d) => options.hashes.length == 0 || options.hashes.indexOf(d) != -1); | ||
return Rx.Observable | ||
.from(digests) | ||
.flatMap((method) => digest(method, file)); | ||
}; | ||
|
||
module.exports.available = availableDigests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const Rx = require('rx'); | ||
const from = Rx.Observable.from; | ||
const files = require('../files'); | ||
const digest = require('./digest'); | ||
const path = require('path'); | ||
|
||
module.exports = function(options) { | ||
if (options.hashes.length == 0) { | ||
return Rx.Observable.empty(); | ||
} | ||
const logger = options.logger; | ||
const ignoredExtensions = digest.available.map((d) => `.${d}`); | ||
const hashOnly = options.hashOnly; | ||
logger.debug(`Checking for files to hash in ${options.localPaths}`); | ||
return from(options.localPaths).map(function (directoryPath) { | ||
logger.debug(`Starting to hash files in ${directoryPath}`); | ||
return files.walk(directoryPath) | ||
.filter((filePath) => { | ||
const parts = path.parse(filePath); | ||
return ignoredExtensions.indexOf(parts.ext) == -1 && (options.hashOnly.length == 0 || options.hashOnly.indexOf(parts.ext) != -1); | ||
}) | ||
.flatMap((localPath) => { | ||
return digest(localPath, options) | ||
}) | ||
}) | ||
.concatAll() | ||
.doOnNext((value) => { | ||
if (!options.dry) { | ||
files.write(`${value.file}.${value.method}`, value.digest); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.