Skip to content

Commit

Permalink
[feat] Adds support for large assets (> 100 MB) (#3194)
Browse files Browse the repository at this point in the history
* Updates crypto library

* missed one

* Using charCodeAt instead of TextEncoder

* updated lock

* Using hash.js for the browser build

* review
  • Loading branch information
Sebastian Niemann authored and lukastaegert committed Oct 27, 2019
1 parent b123169 commit b4c8b43
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
6 changes: 3 additions & 3 deletions LICENSE.md
Expand Up @@ -150,13 +150,13 @@ Repository: jonschlinkert/fill-range
## hash.js
License: MIT
By: Fedor Indutny
Repository: git@github.com:indutny/hash.js
Repository: git+ssh://git@github.com/indutny/hash.js.git

---------------------------------------

## inherits
License: ISC
Repository: git://github.com/isaacs/inherits
Repository: git://github.com/isaacs/inherits.git

> The ISC License
>
Expand Down Expand Up @@ -265,7 +265,7 @@ Repository: micromatch/micromatch

## minimalistic-assert
License: ISC
Repository: https://github.com/calvinmetcalf/minimalistic-assert.git
Repository: git+https://github.com/calvinmetcalf/minimalistic-assert.git

> Copyright 2015 Calvin Metcalf
>
Expand Down
3 changes: 3 additions & 0 deletions browser/crypto.ts
@@ -0,0 +1,3 @@
import sha256 from 'hash.js/lib/hash/sha/256';

export const createHash = () => sha256();
42 changes: 15 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rollup.config.js
Expand Up @@ -171,7 +171,7 @@ export default command => {
!command.configTest && license({ thirdParty: generateLicenseFile })
],
// acorn needs to be external as some plugins rely on a shared acorn instance
external: ['acorn', 'assert', 'events', 'fs', 'module', 'path', 'util'],
external: ['acorn', 'assert', 'crypto', 'events', 'fs', 'module', 'path', 'util'],
treeshake,
output: {
banner,
Expand Down Expand Up @@ -205,6 +205,7 @@ export default command => {
json(),
{
load: id => {
if (~id.indexOf('crypto.ts')) return fs.readFileSync('browser/crypto.ts', 'utf-8');
if (~id.indexOf('fs.ts')) return fs.readFileSync('browser/fs.ts', 'utf-8');
if (~id.indexOf('path.ts')) return fs.readFileSync('browser/path.ts', 'utf-8');
}
Expand Down
8 changes: 4 additions & 4 deletions src/Chunk.ts
@@ -1,5 +1,5 @@
import sha256 from 'hash.js/lib/hash/sha/256';
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import { createHash } from '../browser/crypto';
import { relative } from '../browser/path';
import { createInclusionContext } from './ast/ExecutionContext';
import ExportDefaultDeclaration from './ast/nodes/ExportDefaultDeclaration';
Expand Down Expand Up @@ -366,7 +366,7 @@ export default class Chunk {
getRenderedHash(): string {
if (this.renderedHash) return this.renderedHash;
if (!this.renderedSource) return '';
const hash = sha256();
const hash = createHash();
const hashAugmentation = this.calculateHashAugmentation();
hash.update(hashAugmentation);
hash.update(this.renderedSource.toString());
Expand Down Expand Up @@ -860,11 +860,11 @@ export default class Chunk {
options: OutputOptions,
existingNames: Record<string, any>
): string {
const hash = sha256();
const hash = createHash();
hash.update(
[addons.intro, addons.outro, addons.banner, addons.footer].map(addon => addon || '').join(':')
);
hash.update(options.format);
hash.update(options.format as string);
this.visitDependencies(dep => {
if (dep instanceof ExternalModule) {
hash.update(':' + dep.renderPath);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/FileEmitter.ts
@@ -1,9 +1,9 @@
import sha256 from 'hash.js/lib/hash/sha/256';
import Chunk from '../Chunk';
import Graph from '../Graph';
import Module from '../Module';
import { FilePlaceholder, OutputBundleWithPlaceholders } from '../rollup/types';
import { BuildPhase } from './buildPhase';
import { createHash } from './crypto';
import {
errAssetNotFinalisedForFileName,
errAssetReferenceIdNotFoundForSetSource,
Expand Down Expand Up @@ -33,7 +33,7 @@ function generateAssetFileName(
return makeUnique(
renderNamePattern(output.assetFileNames, 'output.assetFileNames', {
hash() {
const hash = sha256();
const hash = createHash();
hash.update(emittedName);
hash.update(':');
hash.update(source);
Expand Down Expand Up @@ -290,7 +290,7 @@ export class FileEmitter {
private assignReferenceId(file: ConsumedFile, idBase: string): string {
let referenceId: string | undefined;
do {
const hash = sha256();
const hash = createHash();
if (referenceId) {
hash.update(referenceId);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/crypto.ts
@@ -0,0 +1,3 @@
import { createHash as cryptoCreateHash } from 'crypto';

export const createHash = () => cryptoCreateHash('sha256');
2 changes: 1 addition & 1 deletion typings/Chunk.d.ts → typings/hash.js.d.ts
@@ -1,6 +1,6 @@
declare module 'hash.js/lib/hash/sha/256' {
export default function sha256(): {
update: (data: any) => void;
digest: (format: string) => string;
update: (data: any) => void;
};
}

0 comments on commit b4c8b43

Please sign in to comment.