Error: A ReadStream cannot be created from a destroyed WriteStream.
See original GitHub issueHi, I’ve got problem with reading contents from stream.
Either I get this error (👇) or writeStream hangs forever…
Error: A ReadStream cannot be created from a destroyed WriteStream.
at WriteStream.createReadStream (/var/bb/node_modules/fs-capacitor/dist/index.js:174:19)
at Object.createReadStream (/var/bb/node_modules/graphql-upload/lib/processRequest.js:297:28)
Example code:
class ImageStorageService {
async uploadImage(upload: FileUpload): Promise<string> {
const filename = `${cuid()}-${upload.filename}`;
const tempFile = `${tmpdir()}/${filename}`;
const readStream = upload.createReadStream();
const writeStream = createWriteStream(tempFile);
readStream.pipe(writeStream);
// hangs forever, no finish event emitted
const writePromise = new Promise((res) => {
writeStream.on("open", () => {
console.log("Stream opened");
});
writeStream.on("ready", () => {
console.log("Stream ready");
});
writeStream.on("finish", () => {
console.log("Stream finished");
res();
});
});
await writePromise;
// ...
}
}
Any ideas? Am I doing something wrong with streams?
Btw I had to use this fix to prevent maximum call stack size exceeded
error.
"resolutions": {
"**/**/fs-capacitor": "^5.0.0",
"**/graphql-upload": "^10.0.0"
}
Node: v14.2.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:23 (4 by maintainers)
Top Results From Across the Web
createReadStream is not present in uploaded image
Below is a single file upload example that uses the graphql-upload and apollo-server-express packages. Package versions:
Read more >fs-capacitor - npm
Once a WriteStream is fully destroyed, calling .createReadStream() will throw a ReadAfterDestroyedError error. As soon as a ReadStream ends or ...
Read more >fs-capacitor - npm Package Health Analysis - Snyk
To help you get started, we've collected the most common ways that fs-capacitor ... buffer stream const capacitor = new WriteStream(); capacitor.on("error", ...
Read more >node_modules/fs-capacitor · master - GitLab
If error is null or undefined, destruction of underlying resources is delayed until no ReadStream s are attached the WriteStream instance.
Read more >Stream | Node.js v19.3.0 Documentation
transform.destroy([error]) ... All streams created by Node.js APIs operate exclusively on strings and ... createReadStream('file.txt'); const z = zlib.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I suspect this is behaving correctly and that your
uploadImage
function is likely being called after all your resolvers have returned.Once the GraphQL operation has completed, the files backing these streams are flagged for deletion, and new read streams cannot be created from them. They will continue to serve existing read streams, though.
Essentially, it’s expected that you create all necessary streams before your resolver returns/resolves.
Hope you figured it out ok; we can reopen this issue if a specific
graphql-upload
bug is discovered.