forked from fastify/fastify-multipart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
545 lines (467 loc) · 13.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'use strict'
const Busboy = require('busboy')
const os = require('os')
const fp = require('fastify-plugin')
const eos = require('end-of-stream')
const { createWriteStream } = require('fs')
const { unlink } = require('fs').promises
const path = require('path')
const hexoid = require('hexoid')
const util = require('util')
const createError = require('fastify-error')
const sendToWormhole = require('stream-wormhole')
const deepmerge = require('deepmerge')
const { PassThrough, pipeline } = require('stream')
const pump = util.promisify(pipeline)
const kMultipart = Symbol('multipart')
const kMultipartHandler = Symbol('multipartHandler')
const getDescriptor = Object.getOwnPropertyDescriptor
function setMultipart (req, payload, done) {
// nothing to do, it will be done by the Request.multipart object
req.raw[kMultipart] = true
done()
}
function attachToBody (options, req, reply, next) {
if (req.raw[kMultipart] !== true) {
next()
return
}
const consumerStream = options.onFile || defaultConsumer
const body = {}
const mp = req.multipart((field, file, filename, encoding, mimetype) => {
body[field] = body[field] || []
body[field].push({
data: [],
filename,
encoding,
mimetype,
limit: false
})
const result = consumerStream(field, file, filename, encoding, mimetype, body)
if (result && typeof result.then === 'function') {
result.catch((err) => {
// continue with the workflow
err.statusCode = 500
file.destroy(err)
})
}
}, function (err) {
if (!err) {
req.body = body
}
next(err)
}, options)
mp.on('field', (key, value) => {
if (key === '__proto__') {
mp.destroy(new Error('__proto__ is not allowed as field name'))
return
}
if (body[key] === undefined) {
body[key] = value
} else if (Array.isArray(body[key])) {
body[key].push(value)
} else {
body[key] = [body[key], value]
}
})
}
function defaultConsumer (field, file, filename, encoding, mimetype, body) {
const fileData = []
const lastFile = body[field][body[field].length - 1]
file.on('data', data => { if (!lastFile.limit) { fileData.push(data) } })
file.on('limit', () => { lastFile.limit = true })
file.on('end', () => {
if (!lastFile.limit) {
lastFile.data = Buffer.concat(fileData)
} else {
lastFile.data = undefined
}
})
}
function busboy (options) {
try {
return new Busboy(options)
} catch (error) {
const errorEmitter = new PassThrough()
process.nextTick(function () {
errorEmitter.emit('error', error)
})
return errorEmitter
}
}
function fastifyMultipart (fastify, options = {}, done) {
if (options.addToBody === true) {
if (typeof options.sharedSchemaId === 'string') {
fastify.addSchema({
$id: options.sharedSchemaId,
type: 'object',
properties: {
encoding: { type: 'string' },
filename: { type: 'string' },
limit: { type: 'boolean' },
mimetype: { type: 'string' }
}
})
}
fastify.addHook('preValidation', function (req, reply, next) {
attachToBody(options, req, reply, next)
})
}
if (options.attachFieldsToBody === true) {
if (typeof options.sharedSchemaId === 'string') {
fastify.addSchema({
$id: options.sharedSchemaId,
type: 'object',
properties: {
fieldname: { type: 'string' },
encoding: { type: 'string' },
filename: { type: 'string' },
mimetype: { type: 'string' }
}
})
}
fastify.addHook('preValidation', async function (req, reply) {
if (!req.isMultipart()) {
return
}
for await (const part of req.parts()) {
req.body = part.fields
if (part.file) {
if (options.onFile) {
await options.onFile(part)
} else {
await part.toBuffer()
}
}
}
})
}
const PartsLimitError = createError('FST_PARTS_LIMIT', 'reach parts limit', 413)
const FilesLimitError = createError('FST_FILES_LIMIT', 'reach files limit', 413)
const FieldsLimitError = createError('FST_FIELDS_LIMIT', 'reach fields limit', 413)
const RequestFileTooLargeError = createError('FST_REQ_FILE_TOO_LARGE', 'request file too large, please check multipart config', 413)
const PrototypeViolationError = createError('FST_PROTO_VIOLATION', 'prototype property is not allowed as field name', 400)
const InvalidMultipartContentTypeError = createError('FST_INVALID_MULTIPART_CONTENT_TYPE', 'the request is not multipart', 406)
fastify.decorate('multipartErrors', {
PartsLimitError,
FilesLimitError,
FieldsLimitError,
PrototypeViolationError,
InvalidMultipartContentTypeError,
RequestFileTooLargeError
})
fastify.addContentTypeParser('multipart', setMultipart)
fastify.decorateRequest(kMultipartHandler, handleMultipart)
fastify.decorateRequest('parts', getMultipartIterator)
// keeping multipartIterator to avoid bumping a major
// TODO remove on 4.x
fastify.decorateRequest('multipartIterator', getMultipartIterator)
fastify.decorateRequest('isMultipart', isMultipart)
fastify.decorateRequest('tmpUploads', null)
// legacy
fastify.decorateRequest('multipart', handleLegacyMultipartApi)
// Stream mode
fastify.decorateRequest('file', getMultipartFile)
fastify.decorateRequest('files', getMultipartFiles)
// Disk mode
fastify.decorateRequest('saveRequestFiles', saveRequestFiles)
fastify.decorateRequest('cleanRequestFiles', cleanRequestFiles)
fastify.addHook('onResponse', async (request, reply) => {
await request.cleanRequestFiles()
})
const toID = hexoid()
function isMultipart () {
return this.raw[kMultipart] || false
}
// handler definition is in multipart-readstream
// handler(field, file, filename, encoding, mimetype)
// opts is a per-request override for the options object
function handleLegacyMultipartApi (handler, done, opts) {
if (typeof handler !== 'function') {
throw new Error('handler must be a function')
}
if (typeof done !== 'function') {
throw new Error('the callback must be a function')
}
if (!this.isMultipart()) {
done(new Error('the request is not multipart'))
return
}
const log = this.log
log.warn('the multipart callback-based api is deprecated in favour of the new promise api')
log.debug('starting multipart parsing')
const req = this.raw
const busboyOptions = deepmerge.all([{ headers: req.headers }, options || {}, opts || {}])
const stream = busboy(busboyOptions)
var completed = false
var files = 0
var count = 0
var callDoneOnNextEos = false
req.on('error', function (err) {
stream.destroy()
if (!completed) {
completed = true
done(err)
}
})
stream.on('finish', function () {
log.debug('finished receiving stream, total %d files', files)
if (!completed && count === files) {
completed = true
setImmediate(done)
} else {
callDoneOnNextEos = true
}
})
stream.on('file', wrap)
req.pipe(stream)
.on('error', function (error) {
req.emit('error', error)
})
function wrap (field, file, filename, encoding, mimetype) {
log.debug({ field, filename, encoding, mimetype }, 'parsing part')
files++
eos(file, waitForFiles)
if (field === '__proto__') {
file.destroy(new Error('__proto__ is not allowed as field name'))
return
}
handler(field, file, filename, encoding, mimetype)
}
function waitForFiles (err) {
if (err) {
completed = true
done(err)
return
}
if (completed) {
return
}
++count
if (callDoneOnNextEos && count === files) {
completed = true
done()
}
}
return stream
}
function handleMultipart (opts = {}) {
if (!this.isMultipart()) {
throw new InvalidMultipartContentTypeError()
}
this.log.debug('starting multipart parsing')
let worker
let lastValue
// only one file / field can be processed at a time
// "null" will close the consumer side
const ch = (val) => {
if (typeof val === 'function') {
worker = val
} else {
lastValue = val
}
if (worker && lastValue !== undefined) {
worker(lastValue)
worker = undefined
lastValue = undefined
}
}
const parts = () => {
return new Promise((resolve, reject) => {
ch((val) => {
if (val instanceof Error) return reject(val)
resolve(val)
})
})
}
const body = {}
let lastError = null
const request = this.raw
const busboyOptions = deepmerge.all([
{ headers: request.headers },
options,
opts
])
const bb = busboy(busboyOptions)
request.on('close', cleanup)
bb
.on('field', onField)
.on('file', onFile)
.on('close', cleanup)
.on('error', onEnd)
.on('finish', onEnd)
bb.on('partsLimit', function () {
onError(new PartsLimitError())
})
bb.on('filesLimit', function () {
onError(new FilesLimitError())
})
bb.on('fieldsLimit', function () {
onError(new FieldsLimitError())
})
request.pipe(bb)
function onField (name, fieldValue, fieldnameTruncated, valueTruncated) {
// don't overwrite prototypes
if (getDescriptor(Object.prototype, name)) {
onError(new PrototypeViolationError())
return
}
const value = {
fieldname: name,
value: fieldValue,
fieldnameTruncated,
valueTruncated,
fields: body
}
if (body[name] === undefined) {
body[name] = value
} else if (Array.isArray(body[name])) {
body[name].push(value)
} else {
body[name] = [body[name], value]
}
ch(value)
}
function onFile (name, file, filename, encoding, mimetype) {
// don't overwrite prototypes
if (getDescriptor(Object.prototype, name)) {
// ensure that stream is consumed, any error is suppressed
sendToWormhole(file)
onError(new PrototypeViolationError())
return
}
const value = {
fieldname: name,
filename,
encoding,
mimetype,
file,
fields: body,
_buf: null,
async toBuffer () {
if (this._buf) {
return this._buf
}
const fileChunks = []
for await (const chunk of this.file) {
fileChunks.push(chunk)
}
this._buf = Buffer.concat(fileChunks)
return this._buf
}
}
if (body[name] === undefined) {
body[name] = value
} else if (Array.isArray(body[name])) {
body[name].push(value)
} else {
body[name] = [body[name], value]
}
ch(value)
}
function onError (err) {
lastError = err
}
function onEnd (error) {
cleanup()
bb.removeListener('finish', onEnd)
bb.removeListener('error', onEnd)
ch(error || lastError)
}
function cleanup () {
// keep finish listener to wait all data flushed
// keep error listener to wait stream error
request.removeListener('close', cleanup)
bb.removeListener('field', onField)
bb.removeListener('file', onFile)
bb.removeListener('close', cleanup)
}
return parts
}
async function handlePartFile (part, logger) {
const file = part.file
if (file.truncated) {
// ensure that stream is consumed, any error is suppressed
await sendToWormhole(file)
// throw on consumer side
return Promise.reject(new RequestFileTooLargeError())
}
file.once('limit', () => {
const err = new RequestFileTooLargeError()
if (file.listenerCount('error') > 0) {
file.emit('error', err)
logger.warn(err)
} else {
logger.error(err)
// ignore next error event
file.on('error', (err) => {
logger.error('fileLimit: suppressed file stream error, %s', err.messsage)
})
}
// ignore all data
file.resume()
})
return part
}
async function saveRequestFiles (options) {
const requestFiles = []
const files = await this.files(options)
this.tmpUploads = []
for await (const file of files) {
const filepath = path.join(os.tmpdir(), toID() + path.extname(file.filename))
const target = createWriteStream(filepath)
try {
await pump(file.file, target)
this.tmpUploads.push(filepath)
requestFiles.push({ ...file, filepath })
} catch (error) {
this.log.error(error)
await unlink(filepath)
}
}
return requestFiles
}
async function cleanRequestFiles () {
if (!this.tmpUploads) {
return
}
for (const filepath of this.tmpUploads) {
try {
await unlink(filepath)
} catch (error) {
this.log.error(error)
}
}
}
async function getMultipartFile (options) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
if (part.file) {
return handlePartFile(part, this.log)
}
}
}
async function * getMultipartFiles (options) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
if (part.file) {
part = await handlePartFile(part, this.log)
yield part
}
}
}
async function * getMultipartIterator (options) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
yield part
}
}
done()
}
module.exports = fp(fastifyMultipart, {
fastify: '>= 0.39.0',
name: 'fastify-multipart'
})