Skip to content

Commit

Permalink
Account for buffered data when determining whether a readable has bee…
Browse files Browse the repository at this point in the history
…n read (parcel-bundler#3817)

* Account for read stream buffering when determining read streams

* Add test for large assets
  • Loading branch information
Will Binns-Smith authored Nov 21, 2019
1 parent 291f3e6 commit 146132d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/core/core/src/InternalAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export default class InternalAsset {
if (
// $FlowFixMe
typeof contentStream.bytesRead === 'number' &&
contentStream.bytesRead > 0
// If the amount of data read from this stream so far isn't exactly the amount
// of data that is available to be read, then it has been read from.
contentStream.bytesRead !== contentStream.readableLength
) {
throw new Error(
'Stream has already been read. This may happen if a plugin reads from a stream and does not replace it.'
Expand Down
36 changes: 36 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
bundler,
run,
assertBundles,
ncp,
overlayFS,
removeDistDirectory,
distDir,
outputFS,
Expand Down Expand Up @@ -797,6 +799,40 @@ describe('javascript', function() {
assert.equal(stats.size, 9);
});

it('should support importing a URL to a large raw asset', async function() {
// 6 megabytes, which exceeds the threshold in summarizeRequest for buffering
// entire contents into memory and should stream content instead
let assetSizeBytes = 6000000;

let distDir = '/dist';
let fixtureDir = path.join(__dirname, '/integration/import-raw');
let inputDir = path.join(__dirname, 'input');

await ncp(fixtureDir, inputDir);
await outputFS.writeFile(
path.join(inputDir, 'test.txt'),
Buffer.alloc(assetSizeBytes)
);

let b = await bundle(path.join(inputDir, 'index.js'), {inputFS: overlayFS});
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'test.txt.js']
},
{
type: 'txt',
assets: ['test.txt']
}
]);

let output = await run(b);
assert.equal(typeof output, 'function');
assert(/^\/test\.[0-9a-f]+\.txt$/.test(output()));
let stats = await outputFS.stat(path.join(distDir, output()));
assert.equal(stats.size, assetSizeBytes);
});

it('should minify JS in production mode', async function() {
let b = await bundle(path.join(__dirname, '/integration/uglify/index.js'), {
minify: true,
Expand Down

0 comments on commit 146132d

Please sign in to comment.