Skip to content

Commit

Permalink
fix: Remove dicer dependency to libs/utils.js (#105)
Browse files Browse the repository at this point in the history
* fix: Remove dicer dependecny to libs/utils.js

* fix

* more changes

Co-authored-by: Lahiru Maramba <llahiru@gmail.com>
  • Loading branch information
Uzlopak and lahirumaramba authored Jan 6, 2023
1 parent 63a7075 commit f7aef85
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 210 deletions.
2 changes: 2 additions & 0 deletions deps/dicer/lib/Dicer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

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

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

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions deps/streamsearch/sbmh.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

/**
* Copyright Brian White. All rights reserved.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'

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

const MultipartParser = require('./types/multipart')
const UrlencodedParser = require('./types/urlencoded')
const parseParams = require('./utils').parseParams
const parseParams = require('./utils/parseParams')

function Busboy (opts) {
if (!(this instanceof Busboy)) { return new Busboy(opts) }
Expand Down
10 changes: 6 additions & 4 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

// TODO:
// * support 1 nested multipart level
// (see second multipart example here:
Expand All @@ -10,10 +12,10 @@ const inherits = require('util').inherits

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

const parseParams = require('../utils').parseParams
const decodeText = require('../utils').decodeText
const basename = require('../utils').basename
const getLimit = require('../utils').getLimit
const parseParams = require('../utils/parseParams')
const decodeText = require('../utils/decodeText')
const basename = require('../utils/basename')
const getLimit = require('../utils/getLimit')

const RE_BOUNDARY = /^boundary$/i
const RE_FIELD = /^form-data$/i
Expand Down
8 changes: 5 additions & 3 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Decoder = require('../utils').Decoder
const decodeText = require('../utils').decodeText
const getLimit = require('../utils').getLimit
'use strict'

const Decoder = require('../utils/Decoder')
const decodeText = require('../utils/decodeText')
const getLimit = require('../utils/getLimit')

const RE_CHARSET = /^charset$/i

Expand Down
198 changes: 0 additions & 198 deletions lib/utils.js

This file was deleted.

54 changes: 54 additions & 0 deletions lib/utils/Decoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict'

const RE_PLUS = /\+/g

const HEX = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]

function Decoder () {
this.buffer = undefined
}
Decoder.prototype.write = function (str) {
// Replace '+' with ' ' before decoding
str = str.replace(RE_PLUS, ' ')
let res = ''
let i = 0; let p = 0; const len = str.length
for (; i < len; ++i) {
if (this.buffer !== undefined) {
if (!HEX[str.charCodeAt(i)]) {
res += '%' + this.buffer
this.buffer = undefined
--i // retry character
} else {
this.buffer += str[i]
++p
if (this.buffer.length === 2) {
res += String.fromCharCode(parseInt(this.buffer, 16))
this.buffer = undefined
}
}
} else if (str[i] === '%') {
if (i > p) {
res += str.substring(p, i)
p = i
}
this.buffer = ''
++p
}
}
if (p < len && this.buffer === undefined) { res += str.substring(p) }
return res
}
Decoder.prototype.reset = function () {
this.buffer = undefined
}

module.exports = Decoder
14 changes: 14 additions & 0 deletions lib/utils/basename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

module.exports = function basename (path) {
if (typeof path !== 'string') { return '' }
for (var i = path.length - 1; i >= 0; --i) { // eslint-disable-line no-var
switch (path.charCodeAt(i)) {
case 0x2F: // '/'
case 0x5C: // '\'
path = path.slice(i + 1)
return (path === '..' || path === '.' ? '' : path)
}
}
return (path === '..' || path === '.' ? '' : path)
}
Loading

0 comments on commit f7aef85

Please sign in to comment.