graphql-upload 8.1.0 → 12.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.
@@ -1,318 +0,0 @@
1
- 'use strict'
2
-
3
- exports.__esModule = true
4
- exports.processRequest = void 0
5
-
6
- var _util = _interopRequireDefault(require('util'))
7
-
8
- var _busboy = _interopRequireDefault(require('busboy'))
9
-
10
- var _fsCapacitor = require('fs-capacitor')
11
-
12
- var _httpErrors = _interopRequireDefault(require('http-errors'))
13
-
14
- var _objectPath = _interopRequireDefault(require('object-path'))
15
-
16
- var _constants = require('./constants')
17
-
18
- var _ignoreStream = require('./ignoreStream')
19
-
20
- var _isEnumerableObject = require('./isEnumerableObject')
21
-
22
- // istanbul ignore next
23
- function _interopRequireDefault(obj) {
24
- return obj && obj.__esModule ? obj : { default: obj }
25
- }
26
-
27
- class Upload {
28
- constructor() {
29
- this.promise = new Promise((resolve, reject) => {
30
- this.resolve = file => {
31
- this.file = file
32
- resolve(file)
33
- }
34
-
35
- this.reject = reject
36
- })
37
- this.promise.catch(() => {})
38
- }
39
- }
40
-
41
- const processRequest = (
42
- request,
43
- response,
44
- { maxFieldSize = 1000000, maxFileSize = Infinity, maxFiles = Infinity } = {}
45
- ) =>
46
- new Promise((resolve, reject) => {
47
- let released
48
- let exitError
49
- let currentStream
50
- let operations
51
- let operationsPath
52
- let map
53
- const parser = new _busboy.default({
54
- headers: request.headers,
55
- limits: {
56
- fieldSize: maxFieldSize,
57
- fields: 2,
58
- fileSize: maxFileSize,
59
- files: maxFiles
60
- }
61
- })
62
-
63
- const exit = error => {
64
- if (exitError) return
65
- exitError = error
66
- reject(exitError)
67
- parser.destroy()
68
- if (currentStream) currentStream.destroy(exitError)
69
- if (map)
70
- for (const upload of map.values())
71
- if (!upload.file) upload.reject(exitError)
72
- request.unpipe(parser)
73
- setImmediate(() => {
74
- request.resume()
75
- })
76
- }
77
-
78
- const release = () => {
79
- // istanbul ignore next
80
- if (released) return
81
- released = true
82
- if (map)
83
- for (const upload of map.values())
84
- if (upload.file) upload.file.capacitor.destroy()
85
- }
86
-
87
- const abort = () => {
88
- exit(
89
- (0, _httpErrors.default)(
90
- 499,
91
- 'Request disconnected during file upload stream parsing.'
92
- )
93
- )
94
- }
95
-
96
- parser.on(
97
- 'field',
98
- (fieldName, value, fieldNameTruncated, valueTruncated) => {
99
- if (exitError) return
100
- if (valueTruncated)
101
- return exit(
102
- (0, _httpErrors.default)(
103
- 413,
104
- `The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
105
- )
106
- )
107
-
108
- switch (fieldName) {
109
- case 'operations':
110
- try {
111
- operations = JSON.parse(value)
112
- } catch (error) {
113
- return exit(
114
- (0, _httpErrors.default)(
115
- 400,
116
- `Invalid JSON in the ‘operations’ multipart field (${_constants.SPEC_URL}).`
117
- )
118
- )
119
- }
120
-
121
- if (
122
- !(0, _isEnumerableObject.isEnumerableObject)(operations) &&
123
- !Array.isArray(operations)
124
- )
125
- return exit(
126
- (0, _httpErrors.default)(
127
- 400,
128
- `Invalid type for the ‘operations’ multipart field (${_constants.SPEC_URL}).`
129
- )
130
- )
131
- operationsPath = (0, _objectPath.default)(operations)
132
- break
133
-
134
- case 'map': {
135
- if (!operations)
136
- return exit(
137
- (0, _httpErrors.default)(
138
- 400,
139
- `Misordered multipart fields; ‘map’ should follow ‘operations’ (${_constants.SPEC_URL}).`
140
- )
141
- )
142
- let parsedMap
143
-
144
- try {
145
- parsedMap = JSON.parse(value)
146
- } catch (error) {
147
- return exit(
148
- (0, _httpErrors.default)(
149
- 400,
150
- `Invalid JSON in the ‘map’ multipart field (${_constants.SPEC_URL}).`
151
- )
152
- )
153
- }
154
-
155
- if (!(0, _isEnumerableObject.isEnumerableObject)(parsedMap))
156
- return exit(
157
- (0, _httpErrors.default)(
158
- 400,
159
- `Invalid type for the ‘map’ multipart field (${_constants.SPEC_URL}).`
160
- )
161
- )
162
- const mapEntries = Object.entries(parsedMap)
163
- if (mapEntries.length > maxFiles)
164
- return exit(
165
- (0, _httpErrors.default)(
166
- 413,
167
- `${maxFiles} max file uploads exceeded.`
168
- )
169
- )
170
- map = new Map()
171
-
172
- for (const [fieldName, paths] of mapEntries) {
173
- if (!Array.isArray(paths))
174
- return exit(
175
- (0, _httpErrors.default)(
176
- 400,
177
- `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${_constants.SPEC_URL}).`
178
- )
179
- )
180
- map.set(fieldName, new Upload())
181
-
182
- for (const [index, path] of paths.entries()) {
183
- if (typeof path !== 'string')
184
- return exit(
185
- (0, _httpErrors.default)(
186
- 400,
187
- `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${_constants.SPEC_URL}).`
188
- )
189
- )
190
-
191
- try {
192
- operationsPath.set(path, map.get(fieldName).promise)
193
- } catch (error) {
194
- return exit(
195
- (0, _httpErrors.default)(
196
- 400,
197
- `Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${_constants.SPEC_URL}).`
198
- )
199
- )
200
- }
201
- }
202
- }
203
-
204
- resolve(operations)
205
- }
206
- }
207
- }
208
- )
209
- parser.on('file', (fieldName, stream, filename, encoding, mimetype) => {
210
- if (exitError) {
211
- ;(0, _ignoreStream.ignoreStream)(stream)
212
- return
213
- }
214
-
215
- if (!map) {
216
- ;(0, _ignoreStream.ignoreStream)(stream)
217
- return exit(
218
- (0, _httpErrors.default)(
219
- 400,
220
- `Misordered multipart fields; files should follow ‘map’ (${_constants.SPEC_URL}).`
221
- )
222
- )
223
- }
224
-
225
- currentStream = stream
226
- stream.on('end', () => {
227
- currentStream = null
228
- })
229
- const upload = map.get(fieldName)
230
-
231
- if (!upload) {
232
- ;(0, _ignoreStream.ignoreStream)(stream)
233
- return
234
- }
235
-
236
- const capacitor = new _fsCapacitor.WriteStream()
237
- capacitor.on('error', () => {
238
- stream.unpipe()
239
- stream.resume()
240
- })
241
- stream.on('limit', () => {
242
- stream.unpipe()
243
- capacitor.destroy(
244
- (0, _httpErrors.default)(
245
- 413,
246
- `File truncated as it exceeds the ${maxFileSize} byte size limit.`
247
- )
248
- )
249
- })
250
- stream.on('error', error => {
251
- stream.unpipe() // istanbul ignore next
252
-
253
- capacitor.destroy(exitError || error)
254
- })
255
- stream.pipe(capacitor)
256
- const file = {
257
- filename,
258
- mimetype,
259
- encoding,
260
-
261
- createReadStream() {
262
- const error = capacitor.error || (released ? exitError : null)
263
- if (error) throw error
264
- return capacitor.createReadStream()
265
- }
266
- }
267
- let capacitorStream
268
- Object.defineProperty(file, 'stream', {
269
- get: _util.default.deprecate(function() {
270
- if (!capacitorStream) capacitorStream = this.createReadStream()
271
- return capacitorStream
272
- }, 'File upload property ‘stream’ is deprecated. Use ‘createReadStream()’ instead.')
273
- })
274
- Object.defineProperty(file, 'capacitor', {
275
- value: capacitor
276
- })
277
- upload.resolve(file)
278
- })
279
- parser.once('filesLimit', () =>
280
- exit(
281
- (0, _httpErrors.default)(413, `${maxFiles} max file uploads exceeded.`)
282
- )
283
- )
284
- parser.once('finish', () => {
285
- request.unpipe(parser)
286
- request.resume()
287
- if (!operations)
288
- return exit(
289
- (0, _httpErrors.default)(
290
- 400,
291
- `Missing multipart field ‘operations’ (${_constants.SPEC_URL}).`
292
- )
293
- )
294
- if (!map)
295
- return exit(
296
- (0, _httpErrors.default)(
297
- 400,
298
- `Missing multipart field ‘map’ (${_constants.SPEC_URL}).`
299
- )
300
- )
301
-
302
- for (const upload of map.values())
303
- if (!upload.file)
304
- upload.reject(
305
- (0, _httpErrors.default)(400, 'File missing in the request.')
306
- )
307
- })
308
- parser.once('error', exit)
309
- response.once('finish', release)
310
- response.once('close', release)
311
- request.once('close', abort)
312
- request.once('end', () => {
313
- request.removeListener('close', abort)
314
- })
315
- request.pipe(parser)
316
- })
317
-
318
- exports.processRequest = processRequest
@@ -1,286 +0,0 @@
1
- import util from 'util'
2
- import Busboy from 'busboy'
3
- import { WriteStream } from 'fs-capacitor'
4
- import createError from 'http-errors'
5
- import objectPath from 'object-path'
6
- import { SPEC_URL } from './constants'
7
- import { ignoreStream } from './ignoreStream'
8
- import { isEnumerableObject } from './isEnumerableObject'
9
-
10
- class Upload {
11
- constructor() {
12
- this.promise = new Promise((resolve, reject) => {
13
- this.resolve = file => {
14
- this.file = file
15
- resolve(file)
16
- }
17
-
18
- this.reject = reject
19
- })
20
- this.promise.catch(() => {})
21
- }
22
- }
23
-
24
- export const processRequest = (
25
- request,
26
- response,
27
- { maxFieldSize = 1000000, maxFileSize = Infinity, maxFiles = Infinity } = {}
28
- ) =>
29
- new Promise((resolve, reject) => {
30
- let released
31
- let exitError
32
- let currentStream
33
- let operations
34
- let operationsPath
35
- let map
36
- const parser = new Busboy({
37
- headers: request.headers,
38
- limits: {
39
- fieldSize: maxFieldSize,
40
- fields: 2,
41
- fileSize: maxFileSize,
42
- files: maxFiles
43
- }
44
- })
45
-
46
- const exit = error => {
47
- if (exitError) return
48
- exitError = error
49
- reject(exitError)
50
- parser.destroy()
51
- if (currentStream) currentStream.destroy(exitError)
52
- if (map)
53
- for (const upload of map.values())
54
- if (!upload.file) upload.reject(exitError)
55
- request.unpipe(parser)
56
- setImmediate(() => {
57
- request.resume()
58
- })
59
- }
60
-
61
- const release = () => {
62
- // istanbul ignore next
63
- if (released) return
64
- released = true
65
- if (map)
66
- for (const upload of map.values())
67
- if (upload.file) upload.file.capacitor.destroy()
68
- }
69
-
70
- const abort = () => {
71
- exit(
72
- createError(
73
- 499,
74
- 'Request disconnected during file upload stream parsing.'
75
- )
76
- )
77
- }
78
-
79
- parser.on(
80
- 'field',
81
- (fieldName, value, fieldNameTruncated, valueTruncated) => {
82
- if (exitError) return
83
- if (valueTruncated)
84
- return exit(
85
- createError(
86
- 413,
87
- `The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
88
- )
89
- )
90
-
91
- switch (fieldName) {
92
- case 'operations':
93
- try {
94
- operations = JSON.parse(value)
95
- } catch (error) {
96
- return exit(
97
- createError(
98
- 400,
99
- `Invalid JSON in the ‘operations’ multipart field (${SPEC_URL}).`
100
- )
101
- )
102
- }
103
-
104
- if (!isEnumerableObject(operations) && !Array.isArray(operations))
105
- return exit(
106
- createError(
107
- 400,
108
- `Invalid type for the ‘operations’ multipart field (${SPEC_URL}).`
109
- )
110
- )
111
- operationsPath = objectPath(operations)
112
- break
113
-
114
- case 'map': {
115
- if (!operations)
116
- return exit(
117
- createError(
118
- 400,
119
- `Misordered multipart fields; ‘map’ should follow ‘operations’ (${SPEC_URL}).`
120
- )
121
- )
122
- let parsedMap
123
-
124
- try {
125
- parsedMap = JSON.parse(value)
126
- } catch (error) {
127
- return exit(
128
- createError(
129
- 400,
130
- `Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`
131
- )
132
- )
133
- }
134
-
135
- if (!isEnumerableObject(parsedMap))
136
- return exit(
137
- createError(
138
- 400,
139
- `Invalid type for the ‘map’ multipart field (${SPEC_URL}).`
140
- )
141
- )
142
- const mapEntries = Object.entries(parsedMap)
143
- if (mapEntries.length > maxFiles)
144
- return exit(
145
- createError(413, `${maxFiles} max file uploads exceeded.`)
146
- )
147
- map = new Map()
148
-
149
- for (const [fieldName, paths] of mapEntries) {
150
- if (!Array.isArray(paths))
151
- return exit(
152
- createError(
153
- 400,
154
- `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${SPEC_URL}).`
155
- )
156
- )
157
- map.set(fieldName, new Upload())
158
-
159
- for (const [index, path] of paths.entries()) {
160
- if (typeof path !== 'string')
161
- return exit(
162
- createError(
163
- 400,
164
- `Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${SPEC_URL}).`
165
- )
166
- )
167
-
168
- try {
169
- operationsPath.set(path, map.get(fieldName).promise)
170
- } catch (error) {
171
- return exit(
172
- createError(
173
- 400,
174
- `Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${SPEC_URL}).`
175
- )
176
- )
177
- }
178
- }
179
- }
180
-
181
- resolve(operations)
182
- }
183
- }
184
- }
185
- )
186
- parser.on('file', (fieldName, stream, filename, encoding, mimetype) => {
187
- if (exitError) {
188
- ignoreStream(stream)
189
- return
190
- }
191
-
192
- if (!map) {
193
- ignoreStream(stream)
194
- return exit(
195
- createError(
196
- 400,
197
- `Misordered multipart fields; files should follow ‘map’ (${SPEC_URL}).`
198
- )
199
- )
200
- }
201
-
202
- currentStream = stream
203
- stream.on('end', () => {
204
- currentStream = null
205
- })
206
- const upload = map.get(fieldName)
207
-
208
- if (!upload) {
209
- ignoreStream(stream)
210
- return
211
- }
212
-
213
- const capacitor = new WriteStream()
214
- capacitor.on('error', () => {
215
- stream.unpipe()
216
- stream.resume()
217
- })
218
- stream.on('limit', () => {
219
- stream.unpipe()
220
- capacitor.destroy(
221
- createError(
222
- 413,
223
- `File truncated as it exceeds the ${maxFileSize} byte size limit.`
224
- )
225
- )
226
- })
227
- stream.on('error', error => {
228
- stream.unpipe() // istanbul ignore next
229
-
230
- capacitor.destroy(exitError || error)
231
- })
232
- stream.pipe(capacitor)
233
- const file = {
234
- filename,
235
- mimetype,
236
- encoding,
237
-
238
- createReadStream() {
239
- const error = capacitor.error || (released ? exitError : null)
240
- if (error) throw error
241
- return capacitor.createReadStream()
242
- }
243
- }
244
- let capacitorStream
245
- Object.defineProperty(file, 'stream', {
246
- get: util.deprecate(function() {
247
- if (!capacitorStream) capacitorStream = this.createReadStream()
248
- return capacitorStream
249
- }, 'File upload property ‘stream’ is deprecated. Use ‘createReadStream()’ instead.')
250
- })
251
- Object.defineProperty(file, 'capacitor', {
252
- value: capacitor
253
- })
254
- upload.resolve(file)
255
- })
256
- parser.once('filesLimit', () =>
257
- exit(createError(413, `${maxFiles} max file uploads exceeded.`))
258
- )
259
- parser.once('finish', () => {
260
- request.unpipe(parser)
261
- request.resume()
262
- if (!operations)
263
- return exit(
264
- createError(
265
- 400,
266
- `Missing multipart field ‘operations’ (${SPEC_URL}).`
267
- )
268
- )
269
- if (!map)
270
- return exit(
271
- createError(400, `Missing multipart field ‘map’ (${SPEC_URL}).`)
272
- )
273
-
274
- for (const upload of map.values())
275
- if (!upload.file)
276
- upload.reject(createError(400, 'File missing in the request.'))
277
- })
278
- parser.once('error', exit)
279
- response.once('finish', release)
280
- response.once('close', release)
281
- request.once('close', abort)
282
- request.once('end', () => {
283
- request.removeListener('close', abort)
284
- })
285
- request.pipe(parser)
286
- })