Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve File hashing logic in bundler.js. #10050

Merged
merged 2 commits into from Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions History.md
Expand Up @@ -10,6 +10,11 @@
`uglify-es` that appears to be (more actively) maintained.
[Issue #10042](https://github.com/meteor/meteor/issues/10042)

* Sub-resource integrity hashes (sha512) can now be enabled for static CSS
and JS assets by calling `WebAppInternals.enableSubresourceIntegrity()`.
[PR #9933](https://github.com/meteor/meteor/pull/9933)
[PR #10050](https://github.com/meteor/meteor/pull/10050)

## v1.7.0.3, 2018-06-13

* Fixed [Issue #9991](https://github.com/meteor/meteor/issues/9991),
Expand Down
10 changes: 5 additions & 5 deletions tools/fs/watch.js
Expand Up @@ -261,19 +261,19 @@ export function readFile(absPath) {
};

export function sha1(...args) {
return Profile("sha1", function () {
return Profile.run("sha1", function () {
var hash = createHash('sha1');
args.forEach(arg => hash.update(arg));
return hash.digest('hex');
})();
});
}

export function sri(...args) {
return Profile("sri", function () {
export function sha512(...args) {
return Profile.run("sha512", function () {
var hash = createHash('sha512');
args.forEach(arg => hash.update(arg));
return hash.digest('base64');
})();
});
}

export function readDirectory({absPath, include, exclude, names}) {
Expand Down
22 changes: 7 additions & 15 deletions tools/isobuild/bundler.js
Expand Up @@ -529,8 +529,6 @@ export class NodeModulesDirectory {
// Allowed options:
// - sourcePath: path to file on disk that will provide our contents
// - data: contents of the file as a Buffer
// - hash: optional, sha1 hash of the file contents, if known
// - sri: sha512 hash of file contents in base64 encoding
// - sourceMap: if 'data' is given, can be given instead of
// sourcePath. a string or a JS Object. Will be stored as Object.
// - cacheable
Expand Down Expand Up @@ -599,7 +597,6 @@ class File {
this.assets = null;

this._contents = options.data || null; // contents, if known, as a Buffer
this._hashOfContents = options.hash || null;
this._hash = null;
this._sri = null;
}
Expand All @@ -615,13 +612,9 @@ class File {

hash() {
if (! this._hash) {
if (! this._hashOfContents) {
this._hashOfContents = watch.sha1(this.contents());
}

this._hash = watch.sha1(
String(File._salt()),
this._hashOfContents,
this.sri(),
);
}

Expand All @@ -630,10 +623,11 @@ class File {

sri() {
if (! this._sri) {
this._sri = watch.sri(this.contents());
this._sri = watch.sha512(this.contents());
}

return this._sri;
}
}

// Omit encoding to get a buffer, or provide something like 'utf8'
// to get a string
Expand All @@ -650,12 +644,10 @@ class File {
}

setContents(b) {
if (!(b instanceof Buffer)) {
throw new Error("Must set contents to a Buffer");
}
assert.ok(Buffer.isBuffer(b), "Must pass Buffer to File#setContents");
this._contents = b;
// Un-cache hash.
this._hashOfContents = this._hash = null;
// Bust the hash cache.
this._hash = this._sri = null;
}

size() {
Expand Down