graphql-upload 11.0.0 → 14.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.
@@ -0,0 +1,435 @@
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
+ /** @typedef {import("./graphqlUploadExpress.js")} graphqlUploadExpress */
15
+ /** @typedef {import("./graphqlUploadKoa.js")} graphqlUploadKoa */
16
+
17
+ /**
18
+ * Processes an incoming
19
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
20
+ * It parses the `operations` and `map` fields to create an {@linkcode Upload}
21
+ * instance for each expected file upload, placing references wherever the file
22
+ * is expected in the GraphQL operation for the {@linkcode GraphQLUpload} scalar
23
+ * to derive it’s value. Errors are created with
24
+ * [`http-errors`](https://npm.im/http-errors) to assist in sending responses
25
+ * with appropriate HTTP status codes. Used to create custom middleware and is
26
+ * used by {@linkcode graphqlUploadExpress} and {@linkcode graphqlUploadKoa}.
27
+ * @type {ProcessRequestFunction}
28
+ */
29
+ function processRequest(
30
+ request,
31
+ response,
32
+ {
33
+ maxFieldSize = 1000000, // 1 MB
34
+ maxFileSize = Infinity,
35
+ maxFiles = Infinity,
36
+ } = {}
37
+ ) {
38
+ return new Promise((resolve, reject) => {
39
+ /** @type {boolean} */
40
+ let released;
41
+
42
+ /** @type {Error} */
43
+ let exitError;
44
+
45
+ /** @type {import("stream").Readable} */
46
+ let lastFileStream;
47
+
48
+ /**
49
+ * @type {{ [key: string]: unknown } | Array<
50
+ * { [key: string]: unknown }
51
+ * >}
52
+ */
53
+ let operations;
54
+
55
+ /**
56
+ * @type {import("object-path").ObjectPathBound<
57
+ * { [key: string]: unknown } | Array<{ [key: string]: unknown }>
58
+ * >}
59
+ */
60
+ let operationsPath;
61
+
62
+ /** @type {Map<string, Upload>} */
63
+ let map;
64
+
65
+ const parser = new Busboy({
66
+ // @ts-ignore This is about to change with `busboy` v1 types.
67
+ headers: request.headers,
68
+ limits: {
69
+ fieldSize: maxFieldSize,
70
+ fields: 2, // Only operations and map.
71
+ fileSize: maxFileSize,
72
+ files: maxFiles,
73
+ },
74
+ });
75
+
76
+ /**
77
+ * Exits request processing with an error. Successive calls have no effect.
78
+ * @param {Error} error Error instance.
79
+ */
80
+ const exit = (error) => {
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
85
+ if (exitError) return;
86
+
87
+ exitError = error;
88
+
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
+ if (map)
101
+ for (const upload of map.values())
102
+ if (!upload.file) upload.reject(exitError);
103
+
104
+ request.unpipe(parser);
105
+
106
+ // With a sufficiently large request body, subsequent events in the same
107
+ // event frame cause the stream to pause after the parser is destroyed. To
108
+ // ensure that the request resumes, the call to .resume() is scheduled for
109
+ // later in the event loop.
110
+ setImmediate(() => {
111
+ request.resume();
112
+ });
113
+ };
114
+
115
+ parser.on(
116
+ "field",
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
+ );
148
+
149
+ operationsPath = objectPath(operations);
150
+
151
+ break;
152
+ case "map": {
153
+ if (!operations)
154
+ return exit(
155
+ createError(
156
+ 400,
157
+ `Misordered multipart fields; ‘map’ should follow ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
158
+ )
159
+ );
160
+
161
+ let parsedMap;
162
+ try {
163
+ parsedMap = JSON.parse(value);
164
+ } catch (error) {
165
+ return exit(
166
+ createError(
167
+ 400,
168
+ `Invalid JSON in the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
169
+ )
170
+ );
171
+ }
172
+
173
+ // `map` should be an object.
174
+ if (
175
+ typeof parsedMap !== "object" ||
176
+ !parsedMap ||
177
+ Array.isArray(parsedMap)
178
+ )
179
+ return exit(
180
+ createError(
181
+ 400,
182
+ `Invalid type for the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
183
+ )
184
+ );
185
+
186
+ const mapEntries = Object.entries(parsedMap);
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
+ );
194
+
195
+ map = new Map();
196
+ for (const [fieldName, paths] of mapEntries) {
197
+ if (!Array.isArray(paths))
198
+ return exit(
199
+ createError(
200
+ 400,
201
+ `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
202
+ )
203
+ );
204
+
205
+ map.set(fieldName, new Upload());
206
+
207
+ for (const [index, path] of paths.entries()) {
208
+ if (typeof path !== "string")
209
+ return exit(
210
+ createError(
211
+ 400,
212
+ `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
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
+ }
226
+ }
227
+ }
228
+
229
+ resolve(operations);
230
+ }
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
+
250
+ if (!upload) {
251
+ // The file is extraneous. As the rest can still be processed, just
252
+ // ignore it and don’t exit with an error.
253
+ ignoreStream(stream);
254
+ return;
255
+ }
256
+
257
+ /** @type {Error} */
258
+ let fileError;
259
+
260
+ const capacitor = new WriteStream();
261
+
262
+ capacitor.on("error", () => {
263
+ stream.unpipe();
264
+ stream.resume();
265
+ });
266
+
267
+ stream.on("limit", () => {
268
+ fileError = createError(
269
+ 413,
270
+ `File truncated as it exceeds the ${maxFileSize} byte size limit.`
271
+ );
272
+ stream.unpipe();
273
+ capacitor.destroy(fileError);
274
+ });
275
+
276
+ stream.on("error", (error) => {
277
+ fileError = error;
278
+ stream.unpipe();
279
+ capacitor.destroy(fileError);
280
+ });
281
+
282
+ /** @type {FileUpload} */
283
+ const file = {
284
+ filename,
285
+ mimetype,
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
+ });
300
+
301
+ stream.pipe(capacitor);
302
+ upload.resolve(file);
303
+ });
304
+
305
+ parser.once("filesLimit", () =>
306
+ exit(createError(413, `${maxFiles} max file uploads exceeded.`))
307
+ );
308
+
309
+ parser.once("finish", () => {
310
+ request.unpipe(parser);
311
+ request.resume();
312
+
313
+ if (!operations)
314
+ return exit(
315
+ createError(
316
+ 400,
317
+ `Missing multipart field ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
318
+ )
319
+ );
320
+
321
+ if (!map)
322
+ return exit(
323
+ createError(
324
+ 400,
325
+ `Missing multipart field ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
326
+ )
327
+ );
328
+
329
+ for (const upload of map.values())
330
+ if (!upload.file)
331
+ upload.reject(createError(400, "File missing in the request."));
332
+ });
333
+
334
+ parser.once("error", exit);
335
+
336
+ response.once("close", () => {
337
+ released = true;
338
+
339
+ if (map)
340
+ for (const upload of map.values())
341
+ if (upload.file)
342
+ // Release resources and clean up temporary files.
343
+ upload.file.capacitor.release();
344
+ });
345
+
346
+ request.once("close", () => {
347
+ if (!request.readableEnded)
348
+ exit(
349
+ createError(
350
+ 499,
351
+ "Request disconnected during file upload stream parsing."
352
+ )
353
+ );
354
+ });
355
+
356
+ request.pipe(parser);
357
+ });
358
+ }
359
+
360
+ module.exports = processRequest;
361
+
362
+ /**
363
+ * File upload details that are only available after the file’s field in the
364
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec)
365
+ * has begun streaming in.
366
+ * @typedef {object} FileUpload
367
+ * @prop {string} filename File name.
368
+ * @prop {string} mimetype File MIME type. Provided by the client and can’t be
369
+ * trusted.
370
+ * @prop {string} encoding File stream transfer encoding.
371
+ * @prop {import("fs-capacitor").WriteStream} capacitor A private implementation
372
+ * detail that shouldn’t be used outside
373
+ * [`graphql-upload`](https://npm.im/graphql-upload).
374
+ * @prop {FileUploadCreateReadStream} createReadStream Creates a
375
+ * [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
376
+ * of the file’s contents, for processing and storage.
377
+ */
378
+
379
+ /**
380
+ * Creates a
381
+ * [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
382
+ * of an {@link FileUpload uploading file’s} contents, for processing and
383
+ * storage. Multiple calls create independent streams. Throws if called after
384
+ * all resolvers have resolved, or after an error has interrupted the request.
385
+ * @callback FileUploadCreateReadStream
386
+ * @param {FileUploadCreateReadStreamOptions} [options] Options.
387
+ * @returns {import("stream").Readable}
388
+ * [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
389
+ * of the file’s contents.
390
+ * @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#new-streamreadableoptions).
391
+ * @see [Node.js stream backpressure guide](https://nodejs.org/en/docs/guides/backpressuring-in-streams).
392
+ */
393
+
394
+ /**
395
+ * {@linkcode FileUploadCreateReadStream} options.
396
+ * @typedef {object} FileUploadCreateReadStreamOptions
397
+ * @prop {string} [options.encoding] Specify an encoding for the
398
+ * [`data`](https://nodejs.org/api/stream.html#event-data) chunks to be
399
+ * strings (without splitting multi-byte characters across chunks) instead of
400
+ * Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer) instances.
401
+ * Supported values depend on the
402
+ * [`Buffer` implementation](https://github.com/nodejs/node/blob/v18.1.0/lib/buffer.js#L590-L680)
403
+ * and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`,
404
+ * `base64url`, or `hex`. Defaults to `utf8`.
405
+ * @prop {number} [options.highWaterMark] Maximum number of bytes to store in
406
+ * the internal buffer before ceasing to read from the underlying resource.
407
+ * Defaults to `16384`.
408
+ */
409
+
410
+ /**
411
+ * Processes an incoming
412
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
413
+ * @callback ProcessRequestFunction
414
+ * @param {import("http").IncomingMessage} request
415
+ * [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
416
+ * @param {import("http").ServerResponse} response
417
+ * [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse).
418
+ * @param {ProcessRequestOptions} [options] Options.
419
+ * @returns {Promise<
420
+ * { [key: string]: unknown } | Array<{ [key: string]: unknown }>
421
+ * >} GraphQL operation or batch of operations for a GraphQL server to consume
422
+ * (usually as the request body). A GraphQL operation typically has the
423
+ * properties `query` and `variables`.
424
+ */
425
+
426
+ /**
427
+ * {@linkcode ProcessRequestFunction} options.
428
+ * @typedef {object} ProcessRequestOptions
429
+ * @prop {number} [maxFieldSize] Maximum allowed non file multipart form field
430
+ * size in bytes; enough for your queries. Defaults to `1000000` (1 MB).
431
+ * @prop {number} [maxFileSize] Maximum allowed file size in bytes. Defaults to
432
+ * `Infinity`.
433
+ * @prop {number} [maxFiles] Maximum allowed number of files. Defaults to
434
+ * `Infinity`.
435
+ */