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