Node.js streams not flushing between uses. How to flush?
I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream
that I am piping to a custom write stream. The highWaterMark
for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.
On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:
The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).
Here is the code I've got:
return new Promise(async (resolve, reject) =>
const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).
const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );
let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
readFileStream.pipe(writeStream);
writeStream.on("finish", () =>
resolve(writeStream.urn);
);
writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
);
I've tried calling .destroy()
on both the read and write stream. I've tried calling .end()
, .unpipe()
. None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?
node.js stream buffer
add a comment |
I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream
that I am piping to a custom write stream. The highWaterMark
for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.
On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:
The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).
Here is the code I've got:
return new Promise(async (resolve, reject) =>
const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).
const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );
let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
readFileStream.pipe(writeStream);
writeStream.on("finish", () =>
resolve(writeStream.urn);
);
writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
);
I've tried calling .destroy()
on both the read and write stream. I've tried calling .end()
, .unpipe()
. None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?
node.js stream buffer
add a comment |
I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream
that I am piping to a custom write stream. The highWaterMark
for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.
On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:
The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).
Here is the code I've got:
return new Promise(async (resolve, reject) =>
const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).
const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );
let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
readFileStream.pipe(writeStream);
writeStream.on("finish", () =>
resolve(writeStream.urn);
);
writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
);
I've tried calling .destroy()
on both the read and write stream. I've tried calling .end()
, .unpipe()
. None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?
node.js stream buffer
I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream
that I am piping to a custom write stream. The highWaterMark
for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.
On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:
The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).
Here is the code I've got:
return new Promise(async (resolve, reject) =>
const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).
const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );
let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
readFileStream.pipe(writeStream);
writeStream.on("finish", () =>
resolve(writeStream.urn);
);
writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
);
I've tried calling .destroy()
on both the read and write stream. I've tried calling .end()
, .unpipe()
. None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?
node.js stream buffer
node.js stream buffer
asked Nov 13 '18 at 20:45
ReactingToAngularVuesReactingToAngularVues
2,2941356104
2,2941356104
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289234%2fnode-js-streams-not-flushing-between-uses-how-to-flush%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289234%2fnode-js-streams-not-flushing-between-uses-how-to-flush%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown