graphql-upload 14.0.0 → 15.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/graphqlUploadKoa.js +1 -1
- package/package.json +7 -7
- package/processRequest.js +176 -187
package/graphqlUploadKoa.js
CHANGED
|
@@ -42,7 +42,7 @@ function graphqlUploadKoa({
|
|
|
42
42
|
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
|
|
43
43
|
* the request `body` to be similar to a conventional GraphQL POST request for
|
|
44
44
|
* following GraphQL middleware to consume.
|
|
45
|
-
* @param {import("koa").
|
|
45
|
+
* @param {import("koa").ParameterizedContext} ctx
|
|
46
46
|
* @param {() => Promise<unknown>} next
|
|
47
47
|
*/
|
|
48
48
|
async function graphqlUploadKoaMiddleware(ctx, next) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-upload",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "15.0.2",
|
|
4
4
|
"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.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -61,10 +61,10 @@
|
|
|
61
61
|
}
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@types/busboy": "^
|
|
64
|
+
"@types/busboy": "^1.5.0",
|
|
65
65
|
"@types/node": "*",
|
|
66
66
|
"@types/object-path": "^0.11.1",
|
|
67
|
-
"busboy": "^
|
|
67
|
+
"busboy": "^1.6.0",
|
|
68
68
|
"fs-capacitor": "^6.2.0",
|
|
69
69
|
"http-errors": "^2.0.0",
|
|
70
70
|
"object-path": "^0.11.8"
|
|
@@ -73,17 +73,17 @@
|
|
|
73
73
|
"@types/express": "^4.17.13",
|
|
74
74
|
"@types/koa": "^2.13.4",
|
|
75
75
|
"coverage-node": "^6.1.0",
|
|
76
|
-
"eslint": "^8.
|
|
76
|
+
"eslint": "^8.18.0",
|
|
77
77
|
"eslint-plugin-simple-import-sort": "^7.0.0",
|
|
78
|
-
"express": "^4.18.
|
|
78
|
+
"express": "^4.18.1",
|
|
79
79
|
"form-data-encoder": "^2.0.1",
|
|
80
80
|
"graphql": "^16.5.0",
|
|
81
81
|
"koa": "^2.13.4",
|
|
82
82
|
"node-abort-controller": "^3.0.1",
|
|
83
|
-
"node-fetch": "^3.2.
|
|
83
|
+
"node-fetch": "^3.2.6",
|
|
84
84
|
"prettier": "^2.6.2",
|
|
85
85
|
"test-director": "^8.0.2",
|
|
86
|
-
"typescript": "^4.7.
|
|
86
|
+
"typescript": "^4.7.4"
|
|
87
87
|
},
|
|
88
88
|
"scripts": {
|
|
89
89
|
"eslint": "eslint .",
|
package/processRequest.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
"use strict";
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const busboy = require("busboy");
|
|
6
6
|
const { WriteStream } = require("fs-capacitor");
|
|
7
7
|
const createError = require("http-errors");
|
|
8
8
|
const objectPath = require("object-path");
|
|
@@ -11,8 +11,6 @@ const ignoreStream = require("./ignoreStream.js");
|
|
|
11
11
|
const Upload = require("./Upload.js");
|
|
12
12
|
|
|
13
13
|
/** @typedef {import("./GraphQLUpload.js")} GraphQLUpload */
|
|
14
|
-
/** @typedef {import("./graphqlUploadExpress.js")} graphqlUploadExpress */
|
|
15
|
-
/** @typedef {import("./graphqlUploadKoa.js")} graphqlUploadKoa */
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
16
|
* Processes an incoming
|
|
@@ -22,8 +20,7 @@ const Upload = require("./Upload.js");
|
|
|
22
20
|
* is expected in the GraphQL operation for the {@linkcode GraphQLUpload} scalar
|
|
23
21
|
* to derive it’s value. Errors are created with
|
|
24
22
|
* [`http-errors`](https://npm.im/http-errors) to assist in sending responses
|
|
25
|
-
* with appropriate HTTP status codes. Used to create custom middleware
|
|
26
|
-
* used by {@linkcode graphqlUploadExpress} and {@linkcode graphqlUploadKoa}.
|
|
23
|
+
* with appropriate HTTP status codes. Used to create custom middleware.
|
|
27
24
|
* @type {ProcessRequestFunction}
|
|
28
25
|
*/
|
|
29
26
|
function processRequest(
|
|
@@ -42,9 +39,6 @@ function processRequest(
|
|
|
42
39
|
/** @type {Error} */
|
|
43
40
|
let exitError;
|
|
44
41
|
|
|
45
|
-
/** @type {import("stream").Readable} */
|
|
46
|
-
let lastFileStream;
|
|
47
|
-
|
|
48
42
|
/**
|
|
49
43
|
* @type {{ [key: string]: unknown } | Array<
|
|
50
44
|
* { [key: string]: unknown }
|
|
@@ -62,7 +56,7 @@ function processRequest(
|
|
|
62
56
|
/** @type {Map<string, Upload>} */
|
|
63
57
|
let map;
|
|
64
58
|
|
|
65
|
-
const parser =
|
|
59
|
+
const parser = busboy({
|
|
66
60
|
// @ts-ignore This is about to change with `busboy` v1 types.
|
|
67
61
|
headers: request.headers,
|
|
68
62
|
limits: {
|
|
@@ -76,31 +70,20 @@ function processRequest(
|
|
|
76
70
|
/**
|
|
77
71
|
* Exits request processing with an error. Successive calls have no effect.
|
|
78
72
|
* @param {Error} error Error instance.
|
|
73
|
+
* @param {boolean} [isParserError] Is the error from the parser.
|
|
79
74
|
*/
|
|
80
|
-
|
|
81
|
-
// None of the tested scenarios cause multiple calls of this function, but
|
|
82
|
-
// it’s still good to guard against it happening in case it’s possible now
|
|
83
|
-
// or in the future.
|
|
84
|
-
// coverage ignore next line
|
|
75
|
+
function exit(error, isParserError = false) {
|
|
85
76
|
if (exitError) return;
|
|
86
77
|
|
|
87
78
|
exitError = error;
|
|
88
79
|
|
|
89
|
-
reject(exitError);
|
|
90
|
-
|
|
91
|
-
parser.destroy();
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
lastFileStream &&
|
|
95
|
-
!lastFileStream.readableEnded &&
|
|
96
|
-
!lastFileStream.destroyed
|
|
97
|
-
)
|
|
98
|
-
lastFileStream.destroy(exitError);
|
|
99
|
-
|
|
100
80
|
if (map)
|
|
101
81
|
for (const upload of map.values())
|
|
102
82
|
if (!upload.file) upload.reject(exitError);
|
|
103
83
|
|
|
84
|
+
// If the error came from the parser, don’t cause it to be emitted again.
|
|
85
|
+
isParserError ? parser.destroy() : parser.destroy(exitError);
|
|
86
|
+
|
|
104
87
|
request.unpipe(parser);
|
|
105
88
|
|
|
106
89
|
// With a sufficiently large request body, subsequent events in the same
|
|
@@ -110,197 +93,197 @@ function processRequest(
|
|
|
110
93
|
setImmediate(() => {
|
|
111
94
|
request.resume();
|
|
112
95
|
});
|
|
113
|
-
};
|
|
114
96
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
(fieldName, value, fieldNameTruncated, valueTruncated) => {
|
|
118
|
-
if (valueTruncated)
|
|
119
|
-
return exit(
|
|
120
|
-
createError(
|
|
121
|
-
413,
|
|
122
|
-
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
123
|
-
)
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
switch (fieldName) {
|
|
127
|
-
case "operations":
|
|
128
|
-
try {
|
|
129
|
-
operations = JSON.parse(value);
|
|
130
|
-
} catch (error) {
|
|
131
|
-
return exit(
|
|
132
|
-
createError(
|
|
133
|
-
400,
|
|
134
|
-
`Invalid JSON in the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
135
|
-
)
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// `operations` should be an object or an array. Note that arrays
|
|
140
|
-
// and `null` have an `object` type.
|
|
141
|
-
if (typeof operations !== "object" || !operations)
|
|
142
|
-
return exit(
|
|
143
|
-
createError(
|
|
144
|
-
400,
|
|
145
|
-
`Invalid type for the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
146
|
-
)
|
|
147
|
-
);
|
|
97
|
+
reject(exitError);
|
|
98
|
+
}
|
|
148
99
|
|
|
149
|
-
|
|
100
|
+
parser.on("field", (fieldName, value, { valueTruncated }) => {
|
|
101
|
+
if (valueTruncated)
|
|
102
|
+
return exit(
|
|
103
|
+
createError(
|
|
104
|
+
413,
|
|
105
|
+
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
106
|
+
)
|
|
107
|
+
);
|
|
150
108
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
109
|
+
switch (fieldName) {
|
|
110
|
+
case "operations":
|
|
111
|
+
try {
|
|
112
|
+
operations = JSON.parse(value);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
return exit(
|
|
115
|
+
createError(
|
|
116
|
+
400,
|
|
117
|
+
`Invalid JSON in the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
}
|
|
160
121
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
122
|
+
// `operations` should be an object or an array. Note that arrays
|
|
123
|
+
// and `null` have an `object` type.
|
|
124
|
+
if (typeof operations !== "object" || !operations)
|
|
125
|
+
return exit(
|
|
126
|
+
createError(
|
|
127
|
+
400,
|
|
128
|
+
`Invalid type for the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
129
|
+
)
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
operationsPath = objectPath(operations);
|
|
133
|
+
|
|
134
|
+
break;
|
|
135
|
+
case "map": {
|
|
136
|
+
if (!operations)
|
|
137
|
+
return exit(
|
|
138
|
+
createError(
|
|
139
|
+
400,
|
|
140
|
+
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
141
|
+
)
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
let parsedMap;
|
|
145
|
+
try {
|
|
146
|
+
parsedMap = JSON.parse(value);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
return exit(
|
|
149
|
+
createError(
|
|
150
|
+
400,
|
|
151
|
+
`Invalid JSON in the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
152
|
+
)
|
|
153
|
+
);
|
|
154
|
+
}
|
|
172
155
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
156
|
+
// `map` should be an object.
|
|
157
|
+
if (
|
|
158
|
+
typeof parsedMap !== "object" ||
|
|
159
|
+
!parsedMap ||
|
|
160
|
+
Array.isArray(parsedMap)
|
|
161
|
+
)
|
|
162
|
+
return exit(
|
|
163
|
+
createError(
|
|
164
|
+
400,
|
|
165
|
+
`Invalid type for the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
166
|
+
)
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const mapEntries = Object.entries(parsedMap);
|
|
170
|
+
|
|
171
|
+
// Check max files is not exceeded, even though the number of files
|
|
172
|
+
// to parse might not match the map provided by the client.
|
|
173
|
+
if (mapEntries.length > maxFiles)
|
|
174
|
+
return exit(
|
|
175
|
+
createError(413, `${maxFiles} max file uploads exceeded.`)
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
map = new Map();
|
|
179
|
+
for (const [fieldName, paths] of mapEntries) {
|
|
180
|
+
if (!Array.isArray(paths))
|
|
179
181
|
return exit(
|
|
180
182
|
createError(
|
|
181
183
|
400,
|
|
182
|
-
`Invalid type for the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
184
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
183
185
|
)
|
|
184
186
|
);
|
|
185
187
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
// Check max files is not exceeded, even though the number of files
|
|
189
|
-
// to parse might not match the map provided by the client.
|
|
190
|
-
if (mapEntries.length > maxFiles)
|
|
191
|
-
return exit(
|
|
192
|
-
createError(413, `${maxFiles} max file uploads exceeded.`)
|
|
193
|
-
);
|
|
188
|
+
map.set(fieldName, new Upload());
|
|
194
189
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (!Array.isArray(paths))
|
|
190
|
+
for (const [index, path] of paths.entries()) {
|
|
191
|
+
if (typeof path !== "string")
|
|
198
192
|
return exit(
|
|
199
193
|
createError(
|
|
200
194
|
400,
|
|
201
|
-
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
195
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
202
196
|
)
|
|
203
197
|
);
|
|
204
198
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
);
|
|
215
|
-
|
|
216
|
-
try {
|
|
217
|
-
operationsPath.set(path, map.get(fieldName));
|
|
218
|
-
} catch (error) {
|
|
219
|
-
return exit(
|
|
220
|
-
createError(
|
|
221
|
-
400,
|
|
222
|
-
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
223
|
-
)
|
|
224
|
-
);
|
|
225
|
-
}
|
|
199
|
+
try {
|
|
200
|
+
operationsPath.set(path, map.get(fieldName));
|
|
201
|
+
} catch (error) {
|
|
202
|
+
return exit(
|
|
203
|
+
createError(
|
|
204
|
+
400,
|
|
205
|
+
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
206
|
+
)
|
|
207
|
+
);
|
|
226
208
|
}
|
|
227
209
|
}
|
|
228
|
-
|
|
229
|
-
resolve(operations);
|
|
230
210
|
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
);
|
|
234
|
-
|
|
235
|
-
parser.on("file", (fieldName, stream, filename, encoding, mimetype) => {
|
|
236
|
-
lastFileStream = stream;
|
|
237
|
-
|
|
238
|
-
if (!map) {
|
|
239
|
-
ignoreStream(stream);
|
|
240
|
-
return exit(
|
|
241
|
-
createError(
|
|
242
|
-
400,
|
|
243
|
-
`Misordered multipart fields; files should follow ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
244
|
-
)
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const upload = map.get(fieldName);
|
|
249
211
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
// ignore it and don’t exit with an error.
|
|
253
|
-
ignoreStream(stream);
|
|
254
|
-
return;
|
|
212
|
+
resolve(operations);
|
|
213
|
+
}
|
|
255
214
|
}
|
|
215
|
+
});
|
|
256
216
|
|
|
257
|
-
|
|
258
|
-
|
|
217
|
+
parser.on(
|
|
218
|
+
"file",
|
|
219
|
+
(fieldName, stream, { filename, encoding, mimeType: mimetype }) => {
|
|
220
|
+
if (!map) {
|
|
221
|
+
ignoreStream(stream);
|
|
222
|
+
return exit(
|
|
223
|
+
createError(
|
|
224
|
+
400,
|
|
225
|
+
`Misordered multipart fields; files should follow ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
226
|
+
)
|
|
227
|
+
);
|
|
228
|
+
}
|
|
259
229
|
|
|
260
|
-
|
|
230
|
+
const upload = map.get(fieldName);
|
|
261
231
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
232
|
+
if (!upload) {
|
|
233
|
+
// The file is extraneous. As the rest can still be processed, just
|
|
234
|
+
// ignore it and don’t exit with an error.
|
|
235
|
+
ignoreStream(stream);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
266
238
|
|
|
267
|
-
|
|
268
|
-
fileError
|
|
269
|
-
413,
|
|
270
|
-
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
271
|
-
);
|
|
272
|
-
stream.unpipe();
|
|
273
|
-
capacitor.destroy(fileError);
|
|
274
|
-
});
|
|
239
|
+
/** @type {Error} */
|
|
240
|
+
let fileError;
|
|
275
241
|
|
|
276
|
-
|
|
277
|
-
fileError = error;
|
|
278
|
-
stream.unpipe();
|
|
279
|
-
capacitor.destroy(fileError);
|
|
280
|
-
});
|
|
242
|
+
const capacitor = new WriteStream();
|
|
281
243
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
encoding,
|
|
287
|
-
createReadStream(options) {
|
|
288
|
-
const error = fileError || (released ? exitError : null);
|
|
289
|
-
if (error) throw error;
|
|
290
|
-
return capacitor.createReadStream(options);
|
|
291
|
-
},
|
|
292
|
-
capacitor,
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
Object.defineProperty(file, "capacitor", {
|
|
296
|
-
enumerable: false,
|
|
297
|
-
configurable: false,
|
|
298
|
-
writable: false,
|
|
299
|
-
});
|
|
244
|
+
capacitor.on("error", () => {
|
|
245
|
+
stream.unpipe();
|
|
246
|
+
stream.resume();
|
|
247
|
+
});
|
|
300
248
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
249
|
+
stream.on("limit", () => {
|
|
250
|
+
fileError = createError(
|
|
251
|
+
413,
|
|
252
|
+
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
253
|
+
);
|
|
254
|
+
stream.unpipe();
|
|
255
|
+
capacitor.destroy(fileError);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
stream.on("error", (error) => {
|
|
259
|
+
fileError = error;
|
|
260
|
+
stream.unpipe();
|
|
261
|
+
capacitor.destroy(fileError);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
/** @type {FileUpload} */
|
|
265
|
+
const file = {
|
|
266
|
+
filename,
|
|
267
|
+
mimetype,
|
|
268
|
+
encoding,
|
|
269
|
+
createReadStream(options) {
|
|
270
|
+
const error = fileError || (released ? exitError : null);
|
|
271
|
+
if (error) throw error;
|
|
272
|
+
return capacitor.createReadStream(options);
|
|
273
|
+
},
|
|
274
|
+
capacitor,
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
Object.defineProperty(file, "capacitor", {
|
|
278
|
+
enumerable: false,
|
|
279
|
+
configurable: false,
|
|
280
|
+
writable: false,
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
stream.pipe(capacitor);
|
|
284
|
+
upload.resolve(file);
|
|
285
|
+
}
|
|
286
|
+
);
|
|
304
287
|
|
|
305
288
|
parser.once("filesLimit", () =>
|
|
306
289
|
exit(createError(413, `${maxFiles} max file uploads exceeded.`))
|
|
@@ -331,7 +314,13 @@ function processRequest(
|
|
|
331
314
|
upload.reject(createError(400, "File missing in the request."));
|
|
332
315
|
});
|
|
333
316
|
|
|
334
|
-
|
|
317
|
+
// Use the `on` method instead of `once` as in edge cases the same parser
|
|
318
|
+
// could have multiple `error` events and all must be handled to prevent the
|
|
319
|
+
// Node.js process exiting with an error. One edge case is if there is a
|
|
320
|
+
// malformed part header as well as an unexpected end of the form.
|
|
321
|
+
parser.on("error", (/** @type {Error} */ error) => {
|
|
322
|
+
exit(error, true);
|
|
323
|
+
});
|
|
335
324
|
|
|
336
325
|
response.once("close", () => {
|
|
337
326
|
released = true;
|