Skip to content

Commit

Permalink
Merge branch 'master' into use-tap
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Sep 28, 2023
2 parents d88fabc + 5f71e5f commit 59d0732
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 93 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
parserOptions: {
ecmaVersion: 2018
},
ignorePatterns: [
'bench',
'deps/encoding'
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Examples
* Parsing (multipart) with default options:

```javascript
const http = require('http');
const { inspect } = require('util');
const http = require('node:http');
const { inspect } = require('node:util');
const Busboy = require('busboy');

http.createServer((req, res) => {
Expand Down Expand Up @@ -103,10 +103,10 @@ http.createServer((req, res) => {
* Save all incoming files to disk:

```javascript
const http = require('http');
const path = require('path');
const os = require('os');
const fs = require('fs');
const http = require('node:http');
const path = require('node:path');
const os = require('node:os');
const fs = require('node:fs');

const Busboy = require('busboy');

Expand All @@ -133,8 +133,8 @@ http.createServer(function(req, res) {
* Parsing (urlencoded) with default options:

```javascript
const http = require('http');
const { inspect } = require('util');
const http = require('node:http');
const { inspect } = require('node:util');

const Busboy = require('busboy');

Expand Down
2 changes: 1 addition & 1 deletion bench/dicer/multiparty-bench-multipart-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var assert = require('assert'),
var assert = require('node:assert'),
Form = require('multiparty').Form,
boundary = '-----------------------------168072824752491622650073',
mb = 100,
Expand Down
2 changes: 1 addition & 1 deletion bench/dicer/parted-bench-multipart-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// because otherwise it attempts to do some things above and beyond just parsing
// -- like saving to disk and whatnot

var assert = require('assert');
var assert = require('node:assert');
var Parser = require('./parted-multipart'),
boundary = '-----------------------------168072824752491622650073',
parser = new Parser('boundary=' + boundary),
Expand Down
8 changes: 4 additions & 4 deletions bench/dicer/parted-multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
*/

var fs = require('fs')
, path = require('path')
, EventEmitter = require('events').EventEmitter
, StringDecoder = require('string_decoder').StringDecoder
var fs = require('node:fs')
, path = require('node:path')
, EventEmitter = require('node:events').EventEmitter
, StringDecoder = require('node:string_decoder').StringDecoder
, set = require('qs').set
, each = Array.prototype.forEach;

Expand Down
4 changes: 2 additions & 2 deletions benchmarks/common/resultsCombinator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const getopts = require('getopts')
const systemInformation = require('systeminformation')
const { loadResults } = require('photofinish')
Expand Down
4 changes: 2 additions & 2 deletions deps/dicer/lib/Dicer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const WritableStream = require('stream').Writable
const inherits = require('util').inherits
const WritableStream = require('node:stream').Writable
const inherits = require('node:util').inherits

const StreamSearch = require('../../streamsearch/sbmh')

Expand Down
4 changes: 2 additions & 2 deletions deps/dicer/lib/HeaderParser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const EventEmitter = require('node:events').EventEmitter
const inherits = require('node:util').inherits
const getLimit = require('../../../lib/utils/getLimit')

const StreamSearch = require('../../streamsearch/sbmh')
Expand Down
4 changes: 2 additions & 2 deletions deps/dicer/lib/PartStream.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const inherits = require('util').inherits
const ReadableStream = require('stream').Readable
const inherits = require('node:util').inherits
const ReadableStream = require('node:stream').Readable

function PartStream (opts) {
ReadableStream.call(this, opts)
Expand Down
4 changes: 2 additions & 2 deletions deps/streamsearch/sbmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
* by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
*/
const EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const EventEmitter = require('node:events').EventEmitter
const inherits = require('node:util').inherits

function SBMH (needle) {
if (typeof needle === 'string') {
Expand Down
6 changes: 3 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const WritableStream = require('stream').Writable
const { inherits } = require('util')
const WritableStream = require('node:stream').Writable
const { inherits } = require('node:util')
const Dicer = require('../deps/dicer/lib/Dicer')

const MultipartParser = require('./types/multipart')
Expand Down Expand Up @@ -41,7 +41,7 @@ inherits(Busboy, WritableStream)
Busboy.prototype.emit = function (ev) {
if (ev === 'finish') {
if (!this._done) {
this._parser && this._parser.end()
this._parser?.end()
return
} else if (this._finished) {
return
Expand Down
29 changes: 16 additions & 13 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// * support limits.fieldNameSize
// -- this will require modifications to utils.parseParams

const ReadableStream = require('stream').Readable
const inherits = require('util').inherits
const { Readable } = require('node:stream')
const { inherits } = require('node:util')

const Dicer = require('../../deps/dicer/lib/Dicer')

Expand Down Expand Up @@ -47,10 +47,7 @@ function Multipart (boy, cfg) {
function checkFinished () {
if (nends === 0 && finished && !boy._done) {
finished = false
process.nextTick(function () {
boy._done = true
boy.emit('finish')
})
self.end()
}
}

Expand Down Expand Up @@ -268,36 +265,42 @@ function Multipart (boy, cfg) {
}

Multipart.prototype.write = function (chunk, cb) {
let r
if ((r = this.parser.write(chunk)) && !this._pause) { cb() } else {
const r = this.parser.write(chunk)
if (r && !this._pause) {
cb()
} else {
this._needDrain = !r
this._cb = cb
}
}

Multipart.prototype.end = function () {
const self = this
if (this._nparts === 0 && !self._boy._done) {

if (self.parser.writable) {
self.parser.end()
} else if (!self._boy._done) {
process.nextTick(function () {
self._boy._done = true
self._boy.emit('finish')
})
} else if (this.parser.writable) { this.parser.end() }
}
}

function skipPart (part) {
part.resume()
}

function FileStream (opts) {
ReadableStream.call(this, opts)
Readable.call(this, opts)

this.bytesRead = 0

this.truncated = false
}
inherits(FileStream, ReadableStream)

FileStream.prototype._read = function (n) { }
inherits(FileStream, Readable)

FileStream.prototype._read = function (n) {}

module.exports = Multipart
46 changes: 6 additions & 40 deletions lib/utils/decodeText.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
'use strict'

let TextDecoder
try {
TextDecoder = require('util').TextDecoder
} catch (e) { }

const { TextDecoder: PolyfillTextDecoder, getEncoding } = require('text-decoding')

// Node has always utf-8
const textDecoders = new Map()
if (TextDecoder) {
textDecoders.set('utf-8', new TextDecoder('utf-8'))
} else {
textDecoders.set('utf-8', new PolyfillTextDecoder('utf-8'))
}
textDecoders.set('utf8', textDecoders.get('utf-8'))
const utf8Decoder = new TextDecoder('utf-8')
const textDecoders = new Map([
['utf-8', utf8Decoder],
['utf8', utf8Decoder]
])

function decodeText (text, textEncoding, destEncoding) {
if (text) {
Expand All @@ -26,35 +17,10 @@ function decodeText (text, textEncoding, destEncoding) {
try {
textDecoders.set(destEncoding, new TextDecoder(destEncoding))
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) {
if (getEncoding(destEncoding)) {
try {
textDecoders.set(destEncoding, new PolyfillTextDecoder(destEncoding))
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) { }
}
}
}
}
return text
}

function decodeTextPolyfill (text, textEncoding, destEncoding) {
if (text) {
if (textDecoders.has(destEncoding)) {
try {
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) { }
} else {
if (getEncoding(destEncoding)) {
try {
textDecoders.set(destEncoding, new PolyfillTextDecoder(destEncoding))
return textDecoders.get(destEncoding).decode(Buffer.from(text, textEncoding))
} catch (e) { }
}
}
}
return text
}

module.exports = TextDecoder ? decodeText : decodeTextPolyfill
module.exports = decodeText
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fastify/busboy",
"version": "1.2.1",
"version": "2.0.0",
"private": false,
"author": "Brian White <mscdex@mscdex.net>",
"contributors": [
Expand Down Expand Up @@ -34,17 +34,14 @@
"engines": {
"node": ">=14"
},
"dependencies": {
"text-decoding": "^1.0.0"
},
"devDependencies": {
"@types/node": "^20.1.0",
"busboy": "^1.0.0",
"photofinish": "^1.8.0",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.3.8",
"tsd": "^0.28.0",
"tsd": "^0.29.0",
"typescript": "^5.0.2"
},
"keywords": [
Expand Down
9 changes: 5 additions & 4 deletions test/dicer-multipart.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const Dicer = require('../deps/dicer/lib/Dicer')
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const inspect = require('util').inspect
const assert = require('node:assert')
const fs = require('node:fs')
const path = require('node:path')
const inspect = require('node:util').inspect
const { test } = require('tap')

const FIXTURES_ROOT = path.join(__dirname, 'fixtures/')

test('dicer-multipart', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/parse-params.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { inspect } = require('util')
const { inspect } = require('node:util')
const { test } = require('tap')
const parseParams = require('../lib/utils/parseParams')

Expand Down
21 changes: 21 additions & 0 deletions test/types-multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const tests = [
],
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
expected: [],
shouldError: 'Unexpected end of multipart data',
what: 'No fields and no files',
plan: 3
},
Expand Down Expand Up @@ -552,6 +553,26 @@ const tests = [
what: 'one empty part should get ignored',
plan: 7
},
{
source: [
' ------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
],
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
expected: [],
shouldError: 'Unexpected end of multipart data',
what: 'empty form with preceding whitespace',
plan: 3
},
{
source: [
'------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
],
boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhYY',
expected: [],
shouldError: 'Unexpected end of multipart data',
what: 'empty form with wrong boundary (extra Y)',
plan: 3
},
{
source: [[
'------WebKitFormBoundaryzca7IDMnT6QwqBp7',
Expand Down
Loading

0 comments on commit 59d0732

Please sign in to comment.