graphql-upload 13.0.0 → 15.0.1

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.
@@ -0,0 +1,424 @@
1
+ // @ts-check
2
+
3
+ "use strict";
4
+
5
+ const busboy = require("busboy");
6
+ const { WriteStream } = require("fs-capacitor");
7
+ const createError = require("http-errors");
8
+ const objectPath = require("object-path");
9
+ const GRAPHQL_MULTIPART_REQUEST_SPEC_URL = require("./GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js");
10
+ const ignoreStream = require("./ignoreStream.js");
11
+ const Upload = require("./Upload.js");
12
+
13
+ /** @typedef {import("./GraphQLUpload.js")} GraphQLUpload */
14
+
15
+ /**
16
+ * Processes an incoming
17
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
18
+ * It parses the `operations` and `map` fields to create an {@linkcode Upload}
19
+ * instance for each expected file upload, placing references wherever the file
20
+ * is expected in the GraphQL operation for the {@linkcode GraphQLUpload} scalar
21
+ * to derive it’s value. Errors are created with
22
+ * [`http-errors`](https://npm.im/http-errors) to assist in sending responses
23
+ * with appropriate HTTP status codes. Used to create custom middleware.
24
+ * @type {ProcessRequestFunction}
25
+ */
26
+ function processRequest(
27
+ request,
28
+ response,
29
+ {
30
+ maxFieldSize = 1000000, // 1 MB
31
+ maxFileSize = Infinity,
32
+ maxFiles = Infinity,
33
+ } = {}
34
+ ) {
35
+ return new Promise((resolve, reject) => {
36
+ /** @type {boolean} */
37
+ let released;
38
+
39
+ /** @type {Error} */
40
+ let exitError;
41
+
42
+ /**
43
+ * @type {{ [key: string]: unknown } | Array<
44
+ * { [key: string]: unknown }
45
+ * >}
46
+ */
47
+ let operations;
48
+
49
+ /**
50
+ * @type {import("object-path").ObjectPathBound<
51
+ * { [key: string]: unknown } | Array<{ [key: string]: unknown }>
52
+ * >}
53
+ */
54
+ let operationsPath;
55
+
56
+ /** @type {Map<string, Upload>} */
57
+ let map;
58
+
59
+ const parser = busboy({
60
+ // @ts-ignore This is about to change with `busboy` v1 types.
61
+ headers: request.headers,
62
+ limits: {
63
+ fieldSize: maxFieldSize,
64
+ fields: 2, // Only operations and map.
65
+ fileSize: maxFileSize,
66
+ files: maxFiles,
67
+ },
68
+ });
69
+
70
+ /**
71
+ * Exits request processing with an error. Successive calls have no effect.
72
+ * @param {Error} error Error instance.
73
+ * @param {boolean} [isParserError] Is the error from the parser.
74
+ */
75
+ function exit(error, isParserError = false) {
76
+ if (exitError) return;
77
+
78
+ exitError = error;
79
+
80
+ if (map)
81
+ for (const upload of map.values())
82
+ if (!upload.file) upload.reject(exitError);
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
+
87
+ request.unpipe(parser);
88
+
89
+ // With a sufficiently large request body, subsequent events in the same
90
+ // event frame cause the stream to pause after the parser is destroyed. To
91
+ // ensure that the request resumes, the call to .resume() is scheduled for
92
+ // later in the event loop.
93
+ setImmediate(() => {
94
+ request.resume();
95
+ });
96
+
97
+ reject(exitError);
98
+ }
99
+
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
+ );
108
+
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
+ }
121
+
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
+ }
155
+
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))
181
+ return exit(
182
+ createError(
183
+ 400,
184
+ `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
185
+ )
186
+ );
187
+
188
+ map.set(fieldName, new Upload());
189
+
190
+ for (const [index, path] of paths.entries()) {
191
+ if (typeof path !== "string")
192
+ return exit(
193
+ createError(
194
+ 400,
195
+ `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
196
+ )
197
+ );
198
+
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
+ );
208
+ }
209
+ }
210
+ }
211
+
212
+ resolve(operations);
213
+ }
214
+ }
215
+ });
216
+
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
+ }
229
+
230
+ const upload = map.get(fieldName);
231
+
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
+ }
238
+
239
+ /** @type {Error} */
240
+ let fileError;
241
+
242
+ const capacitor = new WriteStream();
243
+
244
+ capacitor.on("error", () => {
245
+ stream.unpipe();
246
+ stream.resume();
247
+ });
248
+
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
+ );
287
+
288
+ parser.once("filesLimit", () =>
289
+ exit(createError(413, `${maxFiles} max file uploads exceeded.`))
290
+ );
291
+
292
+ parser.once("finish", () => {
293
+ request.unpipe(parser);
294
+ request.resume();
295
+
296
+ if (!operations)
297
+ return exit(
298
+ createError(
299
+ 400,
300
+ `Missing multipart field ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
301
+ )
302
+ );
303
+
304
+ if (!map)
305
+ return exit(
306
+ createError(
307
+ 400,
308
+ `Missing multipart field ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
309
+ )
310
+ );
311
+
312
+ for (const upload of map.values())
313
+ if (!upload.file)
314
+ upload.reject(createError(400, "File missing in the request."));
315
+ });
316
+
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
+ });
324
+
325
+ response.once("close", () => {
326
+ released = true;
327
+
328
+ if (map)
329
+ for (const upload of map.values())
330
+ if (upload.file)
331
+ // Release resources and clean up temporary files.
332
+ upload.file.capacitor.release();
333
+ });
334
+
335
+ request.once("close", () => {
336
+ if (!request.readableEnded)
337
+ exit(
338
+ createError(
339
+ 499,
340
+ "Request disconnected during file upload stream parsing."
341
+ )
342
+ );
343
+ });
344
+
345
+ request.pipe(parser);
346
+ });
347
+ }
348
+
349
+ module.exports = processRequest;
350
+
351
+ /**
352
+ * File upload details that are only available after the file’s field in the
353
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec)
354
+ * has begun streaming in.
355
+ * @typedef {object} FileUpload
356
+ * @prop {string} filename File name.
357
+ * @prop {string} mimetype File MIME type. Provided by the client and can’t be
358
+ * trusted.
359
+ * @prop {string} encoding File stream transfer encoding.
360
+ * @prop {import("fs-capacitor").WriteStream} capacitor A private implementation
361
+ * detail that shouldn’t be used outside
362
+ * [`graphql-upload`](https://npm.im/graphql-upload).
363
+ * @prop {FileUploadCreateReadStream} createReadStream Creates a
364
+ * [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
365
+ * of the file’s contents, for processing and storage.
366
+ */
367
+
368
+ /**
369
+ * Creates a
370
+ * [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
371
+ * of an {@link FileUpload uploading file’s} contents, for processing and
372
+ * storage. Multiple calls create independent streams. Throws if called after
373
+ * all resolvers have resolved, or after an error has interrupted the request.
374
+ * @callback FileUploadCreateReadStream
375
+ * @param {FileUploadCreateReadStreamOptions} [options] Options.
376
+ * @returns {import("stream").Readable}
377
+ * [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
378
+ * of the file’s contents.
379
+ * @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#new-streamreadableoptions).
380
+ * @see [Node.js stream backpressure guide](https://nodejs.org/en/docs/guides/backpressuring-in-streams).
381
+ */
382
+
383
+ /**
384
+ * {@linkcode FileUploadCreateReadStream} options.
385
+ * @typedef {object} FileUploadCreateReadStreamOptions
386
+ * @prop {string} [options.encoding] Specify an encoding for the
387
+ * [`data`](https://nodejs.org/api/stream.html#event-data) chunks to be
388
+ * strings (without splitting multi-byte characters across chunks) instead of
389
+ * Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer) instances.
390
+ * Supported values depend on the
391
+ * [`Buffer` implementation](https://github.com/nodejs/node/blob/v18.1.0/lib/buffer.js#L590-L680)
392
+ * and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`,
393
+ * `base64url`, or `hex`. Defaults to `utf8`.
394
+ * @prop {number} [options.highWaterMark] Maximum number of bytes to store in
395
+ * the internal buffer before ceasing to read from the underlying resource.
396
+ * Defaults to `16384`.
397
+ */
398
+
399
+ /**
400
+ * Processes an incoming
401
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
402
+ * @callback ProcessRequestFunction
403
+ * @param {import("http").IncomingMessage} request
404
+ * [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
405
+ * @param {import("http").ServerResponse} response
406
+ * [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse).
407
+ * @param {ProcessRequestOptions} [options] Options.
408
+ * @returns {Promise<
409
+ * { [key: string]: unknown } | Array<{ [key: string]: unknown }>
410
+ * >} GraphQL operation or batch of operations for a GraphQL server to consume
411
+ * (usually as the request body). A GraphQL operation typically has the
412
+ * properties `query` and `variables`.
413
+ */
414
+
415
+ /**
416
+ * {@linkcode ProcessRequestFunction} options.
417
+ * @typedef {object} ProcessRequestOptions
418
+ * @prop {number} [maxFieldSize] Maximum allowed non file multipart form field
419
+ * size in bytes; enough for your queries. Defaults to `1000000` (1 MB).
420
+ * @prop {number} [maxFileSize] Maximum allowed file size in bytes. Defaults to
421
+ * `Infinity`.
422
+ * @prop {number} [maxFiles] Maximum allowed number of files. Defaults to
423
+ * `Infinity`.
424
+ */