-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.js
575 lines (498 loc) · 15.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
'use strict'
const Busboy = require('@fastify/busboy')
const os = require('node:os')
const fp = require('fastify-plugin')
const { createWriteStream } = require('node:fs')
const { unlink } = require('node:fs/promises')
const path = require('node:path')
const { generateId } = require('./lib/generateId')
const createError = require('@fastify/error')
const streamToNull = require('./lib/stream-consumer')
const deepmergeAll = require('@fastify/deepmerge')({ all: true })
const { PassThrough, Readable } = require('node:stream')
const { pipeline: pump } = require('node:stream/promises')
const secureJSON = require('secure-json-parse')
const kMultipart = Symbol('multipart')
const kMultipartHandler = Symbol('multipartHandler')
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', 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)
const InvalidJSONFieldError = createError('FST_INVALID_JSON_FIELD_ERROR', 'a request field is not a valid JSON as declared by its Content-Type', 406)
const FileBufferNotFoundError = createError('FST_FILE_BUFFER_NOT_FOUND', 'the file buffer was not found', 500)
const NoFormData = createError('FST_NO_FORM_DATA', 'FormData is not available', 500)
function setMultipart (req, payload, done) {
req[kMultipart] = true
done()
}
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) {
options.limits = {
...options.limits,
parts: options.limits?.parts || 1000,
fileSize: options.limits?.fileSize || fastify.initialConfig.bodyLimit
}
const attachFieldsToBody = options.attachFieldsToBody
if (attachFieldsToBody === true || attachFieldsToBody === 'keyValues') {
if (typeof options.sharedSchemaId === 'string' && attachFieldsToBody === true) {
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.call(req, part)
} else {
await part.toBuffer()
}
}
}
if (attachFieldsToBody === 'keyValues') {
const body = {}
if (req.body) {
const reqBodyKeys = Object.keys(req.body)
for (let i = 0; i < reqBodyKeys.length; ++i) {
const key = reqBodyKeys[i]
const field = req.body[key]
/* Don't modify the body if a field doesn't have a value or an attached buffer */
if (field.value !== undefined) {
body[key] = field.value
} else if (field._buf) {
body[key] = field._buf
} else if (Array.isArray(field)) {
const items = []
for (let i = 0; i < field.length; ++i) {
const item = field[i]
if (item.value !== undefined) {
items.push(item.value)
} else if (item._buf) {
items.push(item._buf)
}
}
if (items.length) {
body[key] = items
}
}
}
}
req.body = body
}
})
// The following is not available on old Node.js versions
// so we must skip it in the test coverage
/* istanbul ignore next */
if (globalThis.FormData && !fastify.hasRequestDecorator('formData')) {
fastify.decorateRequest('formData', async function () {
const formData = new FormData()
for (const key in this.body) {
const value = this.body[key]
if (Array.isArray(value)) {
for (const item of value) {
await append(key, item)
}
} else {
await append(key, value)
}
}
async function append (key, entry) {
/* c8 ignore next: Buffer.isBuffer is not covered and causing `npm test` to fail */
if (entry.type === 'file' || (attachFieldsToBody === 'keyValues' && Buffer.isBuffer(entry))) {
// TODO use File constructor with fs.openAsBlob()
// if attachFieldsToBody is not set
// https://nodejs.org/api/fs.html#fsopenasblobpath-options
formData.append(key, new Blob([await entry.toBuffer()], {
type: entry.mimetype
}), entry.filename)
} else {
formData.append(key, entry.value)
}
}
return formData
})
}
}
/* istanbul ignore next */
if (!fastify.hasRequestDecorator('formData')) {
fastify.decorateRequest('formData', async function () {
/* c8 ignore next: Next line is not covered and causing `npm test` to fail */
throw new NoFormData()
})
}
const defaultThrowFileSizeLimit = typeof options.throwFileSizeLimit === 'boolean'
? options.throwFileSizeLimit
: true
fastify.decorate('multipartErrors', {
PartsLimitError,
FilesLimitError,
FieldsLimitError,
PrototypeViolationError,
InvalidMultipartContentTypeError,
RequestFileTooLargeError,
FileBufferNotFoundError
})
fastify.addContentTypeParser('multipart/form-data', setMultipart)
fastify.decorateRequest(kMultipart, false)
fastify.decorateRequest(kMultipartHandler, handleMultipart)
fastify.decorateRequest('parts', getMultipartIterator)
fastify.decorateRequest('isMultipart', isMultipart)
fastify.decorateRequest('tmpUploads', null)
fastify.decorateRequest('savedRequestFiles', null)
// 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()
})
function isMultipart () {
return this[kMultipart]
}
function handleMultipart (opts = {}) {
if (!this.isMultipart()) {
throw new InvalidMultipartContentTypeError()
}
this.log.debug('starting multipart parsing')
let values = []
let pendingHandler = null
// only one file / field can be processed at a time
// "null" will close the consumer side
const ch = (val) => {
if (pendingHandler) {
pendingHandler(val)
pendingHandler = null
} else {
values.push(val)
}
}
const handle = (handler) => {
if (values.length > 0) {
const value = values[0]
values = values.slice(1)
handler(value)
} else {
pendingHandler = handler
}
}
const parts = () => {
return new Promise((resolve, reject) => {
handle((val) => {
if (val instanceof Error) {
if (val.message === 'Unexpected end of multipart data') {
// Stop parsing without throwing an error
resolve(null)
} else {
reject(val)
}
} else {
resolve(val)
}
})
})
}
const body = {}
let lastError = null
let currentFile = null
const request = this.raw
const busboyOptions = deepmergeAll(
{ headers: request.headers },
options,
opts
)
this.log.trace({ busboyOptions }, 'Providing options to busboy')
const bb = busboy(busboyOptions)
request.on('close', cleanup)
request.on('error', cleanup)
bb
.on('field', onField)
.on('file', onFile)
.on('end', cleanup)
.on('finish', cleanup)
.on('close', cleanup)
.on('error', cleanup)
bb.on('partsLimit', function () {
const err = new PartsLimitError()
onError(err)
process.nextTick(() => cleanup(err))
})
bb.on('filesLimit', function () {
const err = new FilesLimitError()
onError(err)
process.nextTick(() => cleanup(err))
})
bb.on('fieldsLimit', function () {
const err = new FieldsLimitError()
onError(err)
process.nextTick(() => cleanup(err))
})
request.pipe(bb)
function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
// don't overwrite prototypes
if (name in Object.prototype) {
onError(new PrototypeViolationError())
return
}
// If it is a JSON field, parse it
if (contentType.startsWith('application/json')) {
// If the value was truncated, it can never be a valid JSON. Don't even try to parse
if (valueTruncated) {
onError(new InvalidJSONFieldError())
return
}
try {
fieldValue = secureJSON.parse(fieldValue)
contentType = 'application/json'
} catch (e) {
onError(new InvalidJSONFieldError())
return
}
}
const value = {
type: 'field',
fieldname: name,
mimetype: contentType,
encoding,
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 (name in Object.prototype) {
// ensure that stream is consumed, any error is suppressed
streamToNull(file).catch(() => {})
onError(new PrototypeViolationError())
return
}
const throwFileSizeLimit = typeof opts.throwFileSizeLimit === 'boolean'
? opts.throwFileSizeLimit
: defaultThrowFileSizeLimit
const value = {
type: 'file',
fieldname: name,
filename,
encoding,
mimetype,
file,
fields: body,
_buf: null,
async toBuffer () {
if (this._buf) {
return this._buf
}
const fileChunks = []
let err
for await (const chunk of this.file) {
fileChunks.push(chunk)
if (throwFileSizeLimit && this.file.truncated) {
err = new RequestFileTooLargeError()
err.part = this
onError(err)
fileChunks.length = 0
}
}
if (err) {
// throwing in the async iterator will
// cause the file.destroy() to be called
// The stream has already been managed by
// busboy instead
throw err
}
this._buf = Buffer.concat(fileChunks)
return this._buf
}
}
if (throwFileSizeLimit) {
file.on('limit', function () {
const err = new RequestFileTooLargeError()
err.part = value
onError(err)
})
}
if (body[name] === undefined) {
body[name] = value
} else if (Array.isArray(body[name])) {
body[name].push(value)
} else {
body[name] = [body[name], value]
}
currentFile = file
ch(value)
}
function onError (err) {
lastError = err
currentFile = null
}
function cleanup (err) {
request.unpipe(bb)
if ((err || request.aborted) && currentFile) {
currentFile.destroy()
currentFile = null
}
ch(err || lastError || null)
}
return parts
}
async function saveRequestFiles (options) {
// Checks if this has already been run
if (this.savedRequestFiles) {
return this.savedRequestFiles
}
let files
if (attachFieldsToBody === true) {
// Skip the whole process if the body is empty
if (!this.body) {
return []
}
files = filesFromFields.call(this, this.body)
} else {
files = await this.files(options)
}
this.savedRequestFiles = []
const tmpdir = (options && options.tmpdir) || os.tmpdir()
this.tmpUploads = []
for await (const file of files) {
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename))
const target = createWriteStream(filepath)
try {
await pump(file.file, target)
this.savedRequestFiles.push({ ...file, filepath })
this.tmpUploads.push(filepath)
} catch (err) {
this.log.error({ err }, 'save request file')
throw err
}
}
return this.savedRequestFiles
}
function * filesFromFields (container) {
try {
const fields = Array.isArray(container) ? container : Object.values(container)
for (let i = 0; i < fields.length; ++i) {
const field = fields[i]
if (Array.isArray(field)) {
for (const subField of filesFromFields.call(this, field)) {
yield subField
}
}
if (!field.file) {
continue
}
if (!field._buf) {
throw new FileBufferNotFoundError()
}
field.file = Readable.from(field._buf)
yield field
}
} catch (err) {
this.log.error({ err }, 'save request file failed')
throw err
}
}
async function cleanRequestFiles () {
if (!this.tmpUploads) {
return
}
for (let i = 0; i < this.tmpUploads.length; ++i) {
const filepath = this.tmpUploads[i]
try {
await unlink(filepath)
} /* c8 ignore start */ catch (error) {
this.log.error(error, 'Could not delete file')
}
/* c8 ignore stop */
}
}
async function getMultipartFile (options) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
/* Only return a part if the file property exists */
if (part.file) {
return part
}
}
}
async function * getMultipartFiles (options) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
if (part.file) {
yield part
}
}
}
async function * getMultipartIterator (options) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
yield part
}
}
done()
}
/**
* Adds a new type `isFile` to help @fastify/swagger generate the correct schema.
*/
function ajvFilePlugin (ajv) {
return ajv.addKeyword({
keyword: 'isFile',
compile: (_schema, parent) => {
// Updates the schema to match the file type
parent.type = 'string'
parent.format = 'binary'
delete parent.isFile
return (field /* MultipartFile */) => !!field.file
},
error: {
message: 'should be a file'
}
})
}
/**
* These export configurations enable JS and TS developers
* to consumer fastify in whatever way best suits their needs.
*/
module.exports = fp(fastifyMultipart, {
fastify: '5.x',
name: '@fastify/multipart'
})
module.exports.default = fastifyMultipart
module.exports.fastifyMultipart = fastifyMultipart
module.exports.ajvFilePlugin = ajvFilePlugin