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

Fixes a race condition and conflict with form-data #168

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions changelog.md
@@ -1,5 +1,12 @@
# graphql-upload changelog

## 8.1.1

### Patch

- Fix race condition when trying to use fs-capacitor write stream before file created on disk, fixing [#167](https://github.com/jaydenseric/graphql-upload/issues/167).
- Fix conflict with fs-capacitor & form-data , fixing [form-data/form-data#394](https://github.com/form-data/form-data/issues/394#issuecomment-554195772).

## 8.1.0

### Minor
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "graphql-upload",
"version": "8.1.0",
"version": "8.1.1",
"description": "Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.",
"license": "MIT",
"author": {
Expand Down
15 changes: 10 additions & 5 deletions src/processRequest.mjs
Expand Up @@ -22,6 +22,7 @@ class Upload {
* @type {Promise<FileUpload>}
* @ignore
*/
this.hasFile = false;
this.promise = new Promise((resolve, reject) => {
/**
* Resolves the upload promise with the file upload details.
Expand Down Expand Up @@ -70,6 +71,7 @@ export const processRequest = (
request,
response,
{
waitForFileOnDisk = false,
maxFieldSize = 1000000, // 1 MB
maxFileSize = Infinity,
maxFiles = Infinity
Expand Down Expand Up @@ -302,6 +304,7 @@ export const processRequest = (
return
}

upload.hasFile = true;
const capacitor = new WriteStream()

capacitor.on('error', () => {
Expand All @@ -325,8 +328,6 @@ export const processRequest = (
capacitor.destroy(exitError || error)
})

stream.pipe(capacitor)

const file = {
filename,
mimetype,
Expand All @@ -337,6 +338,12 @@ export const processRequest = (
return capacitor.createReadStream()
}
}
if(waitForFileOnDisk) {
capacitor.once('finish',() => upload.resolve(file));
} else {
capacitor.once('ready',() => upload.resolve(file));
}
stream.pipe(capacitor)

let capacitorStream
Object.defineProperty(file, 'stream', {
Expand All @@ -347,8 +354,6 @@ export const processRequest = (
})

Object.defineProperty(file, 'capacitor', { value: capacitor })

upload.resolve(file)
})

parser.once('filesLimit', () =>
Expand All @@ -373,7 +378,7 @@ export const processRequest = (
)

for (const upload of map.values())
if (!upload.file)
if (!upload.hasFile)
upload.reject(createError(400, 'File missing in the request.'))
})

Expand Down