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