graphql-upload 9.0.0 → 13.0.0
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/package.json +47 -41
- package/private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js +12 -0
- package/{ignoreStream.js → private/ignoreStream.js} +7 -5
- package/public/GraphQLUpload.js +90 -0
- package/public/Upload.js +75 -0
- package/{graphqlUploadExpress.js → public/graphqlUploadExpress.js} +41 -23
- package/{graphqlUploadKoa.js → public/graphqlUploadKoa.js} +32 -14
- package/public/index.js +75 -0
- package/public/index.mjs +5 -0
- package/public/processRequest.js +348 -0
- package/readme.md +216 -70
- package/GraphQLUpload.js +0 -67
- package/changelog.md +0 -441
- package/constants.js +0 -10
- package/index.js +0 -51
- package/isEnumerableObject.js +0 -11
- package/processRequest.js +0 -390
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const Busboy = require("busboy");
|
|
4
|
+
const { WriteStream } = require("fs-capacitor");
|
|
5
|
+
const createError = require("http-errors");
|
|
6
|
+
const objectPath = require("object-path");
|
|
7
|
+
const GRAPHQL_MULTIPART_REQUEST_SPEC_URL = require("../private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL");
|
|
8
|
+
const ignoreStream = require("../private/ignoreStream");
|
|
9
|
+
const Upload = require("./Upload");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Processes a
|
|
13
|
+
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
14
|
+
* It parses the `operations` and `map` fields to create an
|
|
15
|
+
* [`Upload`]{@link Upload} instance for each expected file upload, placing
|
|
16
|
+
* references wherever the file is expected in the
|
|
17
|
+
* [GraphQL operation]{@link GraphQLOperation} for the
|
|
18
|
+
* [`Upload`]{@link GraphQLUpload} scalar to derive it’s value. Errors are
|
|
19
|
+
* created with [`http-errors`](https://npm.im/http-errors) to assist in sending
|
|
20
|
+
* responses with appropriate HTTP status codes. Used in
|
|
21
|
+
* [`graphqlUploadExpress`]{@link graphqlUploadExpress} and
|
|
22
|
+
* [`graphqlUploadKoa`]{@link graphqlUploadKoa} and can be used to create custom
|
|
23
|
+
* middleware.
|
|
24
|
+
* @kind function
|
|
25
|
+
* @name processRequest
|
|
26
|
+
* @type {ProcessRequestFunction}
|
|
27
|
+
* @example <caption>Ways to `import`.</caption>
|
|
28
|
+
* ```js
|
|
29
|
+
* import { processRequest } from "graphql-upload";
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ```js
|
|
33
|
+
* import processRequest from "graphql-upload/public/processRequest.js";
|
|
34
|
+
* ```
|
|
35
|
+
* @example <caption>Ways to `require`.</caption>
|
|
36
|
+
* ```js
|
|
37
|
+
* const { processRequest } = require("graphql-upload");
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* ```js
|
|
41
|
+
* const processRequest = require("graphql-upload/public/processRequest.js");
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
module.exports = function processRequest(
|
|
45
|
+
request,
|
|
46
|
+
response,
|
|
47
|
+
{
|
|
48
|
+
maxFieldSize = 1000000, // 1 MB
|
|
49
|
+
maxFileSize = Infinity,
|
|
50
|
+
maxFiles = Infinity,
|
|
51
|
+
} = {}
|
|
52
|
+
) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
let released;
|
|
55
|
+
let exitError;
|
|
56
|
+
let lastFileStream;
|
|
57
|
+
let operations;
|
|
58
|
+
let operationsPath;
|
|
59
|
+
let map;
|
|
60
|
+
|
|
61
|
+
const parser = new Busboy({
|
|
62
|
+
headers: request.headers,
|
|
63
|
+
limits: {
|
|
64
|
+
fieldSize: maxFieldSize,
|
|
65
|
+
fields: 2, // Only operations and map.
|
|
66
|
+
fileSize: maxFileSize,
|
|
67
|
+
files: maxFiles,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Exits request processing with an error. Successive calls have no effect.
|
|
73
|
+
* @kind function
|
|
74
|
+
* @name processRequest~exit
|
|
75
|
+
* @param {object} error Error instance.
|
|
76
|
+
* @ignore
|
|
77
|
+
*/
|
|
78
|
+
const exit = (error) => {
|
|
79
|
+
// None of the tested scenarios cause multiple calls of this function, but
|
|
80
|
+
// it’t still good to guard against it happening in case it’s possible now
|
|
81
|
+
// or in the future.
|
|
82
|
+
// coverage ignore next line
|
|
83
|
+
if (exitError) return;
|
|
84
|
+
|
|
85
|
+
exitError = error;
|
|
86
|
+
|
|
87
|
+
reject(exitError);
|
|
88
|
+
|
|
89
|
+
parser.destroy();
|
|
90
|
+
|
|
91
|
+
if (
|
|
92
|
+
lastFileStream &&
|
|
93
|
+
!lastFileStream.readableEnded &&
|
|
94
|
+
!lastFileStream.destroyed
|
|
95
|
+
)
|
|
96
|
+
lastFileStream.destroy(exitError);
|
|
97
|
+
|
|
98
|
+
if (map)
|
|
99
|
+
for (const upload of map.values())
|
|
100
|
+
if (!upload.file) upload.reject(exitError);
|
|
101
|
+
|
|
102
|
+
request.unpipe(parser);
|
|
103
|
+
|
|
104
|
+
// With a sufficiently large request body, subsequent events in the same
|
|
105
|
+
// event frame cause the stream to pause after the parser is destroyed. To
|
|
106
|
+
// ensure that the request resumes, the call to .resume() is scheduled for
|
|
107
|
+
// later in the event loop.
|
|
108
|
+
setImmediate(() => {
|
|
109
|
+
request.resume();
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
parser.on(
|
|
114
|
+
"field",
|
|
115
|
+
(fieldName, value, fieldNameTruncated, valueTruncated) => {
|
|
116
|
+
if (valueTruncated)
|
|
117
|
+
return exit(
|
|
118
|
+
createError(
|
|
119
|
+
413,
|
|
120
|
+
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
switch (fieldName) {
|
|
125
|
+
case "operations":
|
|
126
|
+
try {
|
|
127
|
+
operations = JSON.parse(value);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return exit(
|
|
130
|
+
createError(
|
|
131
|
+
400,
|
|
132
|
+
`Invalid JSON in the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
133
|
+
)
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// `operations` should be an object or an array. Note that arrays
|
|
138
|
+
// and `null` have an `object` type.
|
|
139
|
+
if (typeof operations !== "object" || !operations)
|
|
140
|
+
return exit(
|
|
141
|
+
createError(
|
|
142
|
+
400,
|
|
143
|
+
`Invalid type for the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
144
|
+
)
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
operationsPath = objectPath(operations);
|
|
148
|
+
|
|
149
|
+
break;
|
|
150
|
+
case "map": {
|
|
151
|
+
if (!operations)
|
|
152
|
+
return exit(
|
|
153
|
+
createError(
|
|
154
|
+
400,
|
|
155
|
+
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
156
|
+
)
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
let parsedMap;
|
|
160
|
+
try {
|
|
161
|
+
parsedMap = JSON.parse(value);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return exit(
|
|
164
|
+
createError(
|
|
165
|
+
400,
|
|
166
|
+
`Invalid JSON in the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
167
|
+
)
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// `map` should be an object.
|
|
172
|
+
if (
|
|
173
|
+
typeof parsedMap !== "object" ||
|
|
174
|
+
!parsedMap ||
|
|
175
|
+
Array.isArray(parsedMap)
|
|
176
|
+
)
|
|
177
|
+
return exit(
|
|
178
|
+
createError(
|
|
179
|
+
400,
|
|
180
|
+
`Invalid type for the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
181
|
+
)
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const mapEntries = Object.entries(parsedMap);
|
|
185
|
+
|
|
186
|
+
// Check max files is not exceeded, even though the number of files
|
|
187
|
+
// to parse might not match the map provided by the client.
|
|
188
|
+
if (mapEntries.length > maxFiles)
|
|
189
|
+
return exit(
|
|
190
|
+
createError(413, `${maxFiles} max file uploads exceeded.`)
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
map = new Map();
|
|
194
|
+
for (const [fieldName, paths] of mapEntries) {
|
|
195
|
+
if (!Array.isArray(paths))
|
|
196
|
+
return exit(
|
|
197
|
+
createError(
|
|
198
|
+
400,
|
|
199
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
200
|
+
)
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
map.set(fieldName, new Upload());
|
|
204
|
+
|
|
205
|
+
for (const [index, path] of paths.entries()) {
|
|
206
|
+
if (typeof path !== "string")
|
|
207
|
+
return exit(
|
|
208
|
+
createError(
|
|
209
|
+
400,
|
|
210
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
operationsPath.set(path, map.get(fieldName));
|
|
216
|
+
} catch (error) {
|
|
217
|
+
return exit(
|
|
218
|
+
createError(
|
|
219
|
+
400,
|
|
220
|
+
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
221
|
+
)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
resolve(operations);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
parser.on("file", (fieldName, stream, filename, encoding, mimetype) => {
|
|
234
|
+
lastFileStream = stream;
|
|
235
|
+
|
|
236
|
+
if (!map) {
|
|
237
|
+
ignoreStream(stream);
|
|
238
|
+
return exit(
|
|
239
|
+
createError(
|
|
240
|
+
400,
|
|
241
|
+
`Misordered multipart fields; files should follow ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
242
|
+
)
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const upload = map.get(fieldName);
|
|
247
|
+
|
|
248
|
+
if (!upload) {
|
|
249
|
+
// The file is extraneous. As the rest can still be processed, just
|
|
250
|
+
// ignore it and don’t exit with an error.
|
|
251
|
+
ignoreStream(stream);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
let fileError;
|
|
256
|
+
const capacitor = new WriteStream();
|
|
257
|
+
|
|
258
|
+
capacitor.on("error", () => {
|
|
259
|
+
stream.unpipe();
|
|
260
|
+
stream.resume();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
stream.on("limit", () => {
|
|
264
|
+
fileError = createError(
|
|
265
|
+
413,
|
|
266
|
+
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
267
|
+
);
|
|
268
|
+
stream.unpipe();
|
|
269
|
+
capacitor.destroy(fileError);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
stream.on("error", (error) => {
|
|
273
|
+
fileError = error;
|
|
274
|
+
stream.unpipe();
|
|
275
|
+
capacitor.destroy(fileError);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const file = {
|
|
279
|
+
filename,
|
|
280
|
+
mimetype,
|
|
281
|
+
encoding,
|
|
282
|
+
createReadStream(options) {
|
|
283
|
+
const error = fileError || (released ? exitError : null);
|
|
284
|
+
if (error) throw error;
|
|
285
|
+
return capacitor.createReadStream(options);
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
Object.defineProperty(file, "capacitor", { value: capacitor });
|
|
290
|
+
|
|
291
|
+
stream.pipe(capacitor);
|
|
292
|
+
upload.resolve(file);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
parser.once("filesLimit", () =>
|
|
296
|
+
exit(createError(413, `${maxFiles} max file uploads exceeded.`))
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
parser.once("finish", () => {
|
|
300
|
+
request.unpipe(parser);
|
|
301
|
+
request.resume();
|
|
302
|
+
|
|
303
|
+
if (!operations)
|
|
304
|
+
return exit(
|
|
305
|
+
createError(
|
|
306
|
+
400,
|
|
307
|
+
`Missing multipart field ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
308
|
+
)
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
if (!map)
|
|
312
|
+
return exit(
|
|
313
|
+
createError(
|
|
314
|
+
400,
|
|
315
|
+
`Missing multipart field ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
316
|
+
)
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
for (const upload of map.values())
|
|
320
|
+
if (!upload.file)
|
|
321
|
+
upload.reject(createError(400, "File missing in the request."));
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
parser.once("error", exit);
|
|
325
|
+
|
|
326
|
+
response.once("close", () => {
|
|
327
|
+
released = true;
|
|
328
|
+
|
|
329
|
+
if (map)
|
|
330
|
+
for (const upload of map.values())
|
|
331
|
+
if (upload.file)
|
|
332
|
+
// Release resources and clean up temporary files.
|
|
333
|
+
upload.file.capacitor.release();
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
request.once("close", () => {
|
|
337
|
+
if (!request.readableEnded)
|
|
338
|
+
exit(
|
|
339
|
+
createError(
|
|
340
|
+
499,
|
|
341
|
+
"Request disconnected during file upload stream parsing."
|
|
342
|
+
)
|
|
343
|
+
);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
request.pipe(parser);
|
|
347
|
+
});
|
|
348
|
+
};
|