Compare commits

..

No commits in common. "master" and "v1" have entirely different histories.
master ... v1

13 changed files with 1295 additions and 1076 deletions

1
.gitignore vendored
View File

@ -9,5 +9,4 @@
.vscode .vscode
.tmp/
node_modules/ node_modules/

110
Readme.md
View File

@ -1,6 +1,4 @@
![module info](https://nodei.co/npm/@gm5/request.png?downloads=true&downloadRank=true&stars=true)
![downloads](https://img.shields.io/npm/dt/@gm5/request.svg)
![version](https://img.shields.io/npm/v/@gm5/request.svg)
# @gm5/equest # @gm5/equest
> 对Http的request进一步封装, 提供常用的API. > 对Http的request进一步封装, 提供常用的API.
@ -21,13 +19,115 @@ http
.createServer((req, res) => { .createServer((req, res) => {
let request = new Request(req, res) let request = new Request(req, res)
console.log(request.origin) // {req, res}
// print the fixed url // print the fixed url
console.log(request.url) console.log(request.url)
request.ip // get client ip address request.ip() // get client ip address
// http://test.com/?foo=bar // http://test.com/?foo=bar
request.query['foo'] // bar request.get('foo') // bar
}) })
.listen(3000) .listen(3000)
``` ```
## API
### origin
> 返回原始的response & request对象
```js
console.log(request.origin) // {req: request, res: response}
```
### app
> 返回一级路由的名字
```js
// abc.com/foo/bar
console.log(request.app) // foo
```
### path
> 以数组形式,返回除一级路由之外剩下的路径
```js
// abc.com/foo/bar/aa/bb
console.log(request.path) // ['bar', 'aa', 'bb']
```
### url
> 返回修正过的url路径
```js
// abc.com/foo/bar/aa/bb
// abc.com////foo///bar/aa/bb
console.log(request.url) // foo/bar/aa/bb
```
### get([key[,xss]])
* key `<String>` 字段名 [可选], 不则返回全部参数
* xss `<Boolean>` 是否进行xss过滤 [可选], 默认为ture
> 返回URL上的query参数, 类似于`$_GET[]`;
```javascript
// http://test.com?name=foo&age=18
request.get('name') // foo
request.get('age') // 18
request.get() // {name: 'foo', age: 18}
request.get('weight') // return null if not exists
```
### post([key[,xss]])
* key `<String>` optional
* xss `<Boolean>` optional
> 读取post请求的body, 类似于 `$_POST[]`.
> **该方法返回的是Promise对象**
```javascript
// http://test.com
await request.post('name') // foo
await request.post('age') // 18
// return all if not yet argument given
await request.post() // {name: 'foo', age: 18}
await request.post('weight') // return null if not exists
```
### header([key])
* key `<String>` 字段名[可选], 不传则返回全部
> 返回请求头
```javascript
request.header('user-agent') // Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ...
// return all if not yet argument given
request.header() // {'user-agent': '...'[, ...]}
```
### ip()
> 获取客户端IP地址.
>
> It would return '127.0.0.1' maybe if in local area network.
### cookie(key)
> 获取客户端带上的cookie.
> 不传key时返回所有的

300
index.js
View File

@ -5,21 +5,20 @@
import 'es.shim' import 'es.shim'
import { fileURLToPath, parse } from 'node:url'
import { dirname, resolve } from 'node:path'
import fs from 'iofs'
import Parser from './lib/index.js' import Parser from './lib/index.js'
import { parseCookie } from './lib/cookie.js' import { parseCookie } from './lib/cookie.js'
import { querystring } from './lib/helper.js' import fs from 'iofs'
import URL from 'url'
import QS from 'querystring'
import PATH from 'path'
const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded' const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded'
const __dirname = dirname(fileURLToPath(import.meta.url)) var __dirname = PATH.dirname(URL.fileURLToPath(import.meta.url))
const tmpdir = resolve(__dirname, '.tmp/') var tmpdir = PATH.resolve(__dirname, '.tmp/')
const encode = encodeURIComponent var encode = encodeURIComponent
const decode = decodeURIComponent var decode = decodeURIComponent
if (fs.isdir(tmpdir)) { if (fs.isdir(tmpdir)) {
fs.rm(tmpdir, true) fs.rm(tmpdir, true)
@ -37,174 +36,226 @@ function hideProperty(host, name, value) {
} }
export default class Request { export default class Request {
#req = null constructor(req, res) {
#res = null
#opts = {}
#query = null
#body = null
#cookies = Object.create(null)
controller = 'index'
method = 'GET'
path = []
url = ''
host = '127.0.0.1'
hostname = '127.0.0.1'
protocol = 'http'
constructor(req, res, opts = {}) {
this.method = req.method.toUpperCase() this.method = req.method.toUpperCase()
this.params = {}
this.#req = req hideProperty(this, 'origin', { req, res })
this.#res = res hideProperty(this, '__GET__', null)
hideProperty(this, '__POST__', null)
hideProperty(this, '__COOKIE__', parseCookie(this.header('cookie') || ''))
this.host = req.headers['host'] || '127.0.0.1' this.__fixUrl()
this.hostname = this.host.split(':')[0]
this.protocol = req.headers['x-forwarded-proto'] || 'http'
this.#cookies = parseCookie(this.headers['cookie'] || '')
Object.assign(this.#opts, opts)
this.#init()
} }
// 修正请求的url // 修正请求的url
#init() { __fixUrl() {
let url = parse(this.#req.url) let _url = URL.parse(this.origin.req.url)
.pathname.slice(1) .pathname.slice(1)
.replace(/[\/]+$/, '') .replace(/[\/]+$/, '')
let controller = '' // 将作为主控制器(即apps目录下的应用) let app = '' // 将作为主控制器(即apps目录下的应用)
let path = [] let pathArr = []
let tmpArr = []
// URL上不允许有非法字符 // URL上不允许有非法字符
if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) { if (/[^\w-/.@~!$&:+'=]/.test(decode(_url))) {
this.#res.rendered = true this.origin.res.rendered = true
this.#res.writeHead(400, { this.origin.res.writeHead(400, {
'X-debug': `url [/${encode(url)}] contains invalid characters` 'X-debug': `url [/${encode(_url)}] contains invalid characters`
}) })
return this.#res.end(`Invalid characters: /${url}`) return this.origin.res.end(`Invalid characters: /${_url}`)
} }
// 修正url中可能出现的"多斜杠" // 修正url中可能出现的"多斜杠"
url = url.replace(/[\/]+/g, '/').replace(/^\//, '') _url = _url.replace(/[\/]+/g, '/').replace(/^\//, '')
path = url.split('/') pathArr = _url.split('/')
if (!path[0] || path[0] === '') { if (!pathArr[0] || pathArr[0] === '') {
path[0] = 'index' pathArr[0] = 'index'
} }
if (path[0].includes('.')) { if (pathArr[0].indexOf('.') !== -1) {
controller = path[0].slice(0, path[0].indexOf('.')) app = pathArr[0].slice(0, pathArr[0].indexOf('.'))
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index // 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
if (!controller || controller === '') { if (!app || app === '') {
controller = 'index' app = 'index'
} }
} else { } else {
controller = path[0] app = pathArr[0]
} }
path.shift() pathArr.shift()
this.controller = controller // 将path第3段之后的部分, 每2个一组转为key-val数据对象, 存入params中
this.url = url tmpArr = pathArr.slice(1).concat()
this.path = path while (tmpArr.length) {
this.params[tmpArr.shift()] = tmpArr.shift() || null
}
tmpArr = undefined
for (let i in this.params) {
if (!this.params[i]) {
continue
}
// 修正数字类型,把符合条件的数字字符串转为数字(也许会误转, 但总的来说是利大弊)
this.params[i] = Number.parse(this.params[i])
}
this.app = app
this.url = _url
this.path = pathArr
} }
/** /**
* [解析请求体, 需要 await ] * [get 同php的$_GET]
*/
get(key = '', xss = true) {
xss = !!xss
if (!this.__GET__) {
let para = URL.parse(this.origin.req.url).query
para = Object.assign({}, QS.parse(para))
if (xss) {
for (let i in para) {
if (!para[i]) {
continue
}
if (Array.isArray(para[i])) {
para[i] = para[i].map(it => {
it = Number.parse(it.trim().xss())
return it
})
} else {
para[i] = Number.parse(para[i].trim().xss())
}
}
}
this.__GET__ = para
}
return key
? this.__GET__.hasOwnProperty(key)
? this.__GET__[key]
: null
: this.__GET__
}
/**
* [post 接收post, 需要 await ]
* @param {Str} key [字段] * @param {Str} key [字段]
*/ */
#parseBody() { post(key = '', xss = true) {
let para = {}
let out = Promise.defer() let out = Promise.defer()
let form, contentType let form, contentType
this.#body = {} xss = !!xss
//如果之前已经缓存过,则直接从缓存读取
if (this.__POST__) {
if (key) {
return this.__POST__.hasOwnProperty(key) ? this.__POST__[key] : null
} else {
return this.__POST__
}
}
contentType = this.header('content-type') || DEFAULT_FORM_TYPE contentType = this.header('content-type') || DEFAULT_FORM_TYPE
form = new Parser(this.#req, { ...this.#opts, uploadDir: tmpdir }) form = new Parser()
form.uploadDir = tmpdir
form.parse(this.origin.req)
form form.on('field', (name, value) => {
.on('field', (name, value) => {
if (name === false) { if (name === false) {
this.#body = value para = value
return return
} }
if (~contentType.indexOf('urlencoded')) {
if (
name.slice(0, 2) === '{"' &&
(name.slice(-2) === '"}' || value.slice(-2) === '"}')
) {
name = name.replace(/\s/g, '+')
if (name.endsWith('[]')) { if (value.slice(0, 1) === '=') value = '=' + value
return Object.assign(para, JSON.parse(name + value))
}
}
if (typeof value === 'string') {
value = xss ? value.xss() : value
}
if (name.slice(-2) === '[]') {
name = name.slice(0, -2) name = name.slice(0, -2)
if (typeof value === 'string') { if (typeof value === 'string') {
value = [value] value = [value]
} }
} else if (name.slice(-1) === ']') { } else if (name.slice(-1) === ']') {
let idx = name.lastIndexOf('[') let key = name.slice(name.lastIndexOf('[') + 1, -1)
let key = name.slice(idx + 1, -1) name = name.slice(0, name.lastIndexOf('['))
name = name.slice(0, idx)
//多解析一层对象(也仅支持到这一层) //多解析一层对象(也仅支持到这一层)
if (name.slice(-1) === ']') { if (name.slice(-1) === ']') {
idx = name.lastIndexOf('[') let pkey = name.slice(name.lastIndexOf('[') + 1, -1)
let pkey = name.slice(idx + 1, -1) name = name.slice(0, name.lastIndexOf('['))
name = name.slice(0, idx)
if (!this.#body.hasOwnProperty(name)) { if (!para.hasOwnProperty(name)) {
this.#body[name] = {} para[name] = {}
} }
if (!this.#body[name].hasOwnProperty(pkey)) { if (!para[name].hasOwnProperty(pkey)) {
this.#body[name][pkey] = {} para[name][pkey] = {}
} }
this.#body[name][pkey][key] = value para[name][pkey][key] = value
} else { } else {
if (!this.#body.hasOwnProperty(name)) { if (!para.hasOwnProperty(name)) {
this.#body[name] = {} para[name] = {}
} }
this.#body[name][key] = value para[name][key] = value
} }
return return
} }
this.#body[name] = value para[name] = value
}) })
.on('file', (name, file) => {
if (name === false) { form.on('file', (name, file) => {
this.#body = file
} else {
if (name.slice(-2) === '[]') { if (name.slice(-2) === '[]') {
name = name.slice(0, -2) name = name.slice(0, -2)
} }
if (!this.#body.hasOwnProperty(name)) { if (!para.hasOwnProperty(name)) {
this.#body[name] = file para[name] = file
} else { } else {
if (!Array.isArray(this.#body[name])) { if (!Array.isArray(para[name])) {
this.#body[name] = [this.#body[name]] para[name] = [para[name]]
}
this.#body[name].push(file)
} }
para[name].push(file)
} }
}) })
.on('buffer', buf => {
this.#body = buf form.on('error', out.reject)
})
.on('error', out.reject) form.on('end', err => {
.on('end', _ => { if (~contentType.indexOf('urlencoded')) {
if (contentType.includes('urlencoded')) { for (let i in para) {
for (let i in this.#body) { if (typeof para[i] === 'string') {
if (typeof this.#body[i] === 'string') { if (!para[i]) {
if (!this.#body[i]) {
continue continue
} }
this.#body[i] = Number.parse(this.#body[i]) para[i] = Number.parse(para[i])
} }
} }
} }
this._postParam = para
out.resolve(this.#body) if (key) {
return out.resolve(para.hasOwnProperty(key) ? para[key] : null)
} else {
return out.resolve(para)
}
}) })
return out.promise return out.promise
} }
@ -212,46 +263,23 @@ export default class Request {
//获取响应头 //获取响应头
header(key = '') { header(key = '') {
key = key ? (key + '').toLowerCase() : null key = key ? (key + '').toLowerCase() : null
return !!key ? this.#req.headers[key] : this.#req.headers return !!key ? this.origin.req.headers[key] : this.origin.req.headers
} }
// 读取cookie // 读取cookie
cookie(key) { cookie(key) {
if (key) { if (key) {
return this.#cookies[key] return this.__COOKIE__[key]
} }
return this.#cookies return this.__COOKIE__
}
get query() {
if (!this.#query) {
let data = parse(this.#req.url).query
this.#query = querystring(data)
}
return this.#query
}
get body() {
if (this.#body) {
return this.#body
}
return this.#parseBody()
}
get cookies() {
return this.#cookies
}
get headers() {
return this.#req.headers
} }
//获取客户端IP //获取客户端IP
get ip() { ip() {
return ( return (
this.headers['x-real-ip'] || this.header('x-real-ip') ||
this.headers['x-forwarded-for'] || this.header('x-forwarded-for') ||
this.#req.connection.remoteAddress.replace('::ffff:', '') this.origin.req.connection.remoteAddress.replace('::ffff:', '')
) )
} }
} }

View File

@ -4,16 +4,16 @@
*/ */
// var KEY_REGEXP = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/ // var KEY_REGEXP = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
const SPLIT_REGEXP = /; */ var SPLIT_REGEXP = /; */
// var encode = encodeURIComponent // var encode = encodeURIComponent
const decode = decodeURIComponent var decode = decodeURIComponent
/** /**
* [parse 格式化字符串] * [parse 格式化字符串]
*/ */
export function parseCookie(str) { export function parseCookie(str) {
let obj = {} var obj = {}
let pairs var pairs
if (typeof str !== 'string') { if (typeof str !== 'string') {
return {} return {}
@ -27,8 +27,8 @@ export function parseCookie(str) {
continue continue
} }
let key = item[0].trim() var key = item[0].trim()
let val = item[1].trim() var val = item[1].trim()
obj[key] = decode(val) obj[key] = decode(val)
} }

View File

@ -1,28 +1,38 @@
import { WriteStream } from 'node:fs' import util from 'util'
import { EventEmitter } from 'node:events' import { WriteStream } from 'fs'
import { EventEmitter } from 'events'
import crypto from 'crypto'
export default class File extends EventEmitter { export default function File(properties) {
#stream = null EventEmitter.call(this)
size = 0 this.size = 0
path = null this.path = null
name = null this.name = null
type = null this.type = null
lastModifiedDate = null this.hash = null
this.lastModifiedDate = null
constructor(props = {}) { this._writeStream = null
super()
for (let key in props) { for (var key in properties) {
this[key] = props[key] this[key] = properties[key]
}
} }
open() { if (typeof this.hash === 'string') {
this.#stream = new WriteStream(this.path) this.hash = crypto.createHash(properties.hash)
} else {
this.hash = null
} }
}
toJSON() { util.inherits(File, EventEmitter)
File.prototype.open = function() {
this._writeStream = new WriteStream(this.path)
}
File.prototype.toJSON = function() {
return { return {
size: this.size, size: this.size,
path: this.path, path: this.path,
@ -30,19 +40,31 @@ export default class File extends EventEmitter {
type: this.type, type: this.type,
mtime: this.lastModifiedDate, mtime: this.lastModifiedDate,
length: this.length, length: this.length,
filename: this.name, filename: this.filename,
mime: this.type mime: this.mime
}
}
write(buffer) {
this.#stream.write(buffer, _ => {
this.size += buffer.length
})
}
end(callback) {
this.lastModifiedDate = new Date()
this.#stream.end(callback)
} }
} }
File.prototype.write = function(buffer, cb) {
var self = this
if (self.hash) {
self.hash.update(buffer)
}
this._writeStream.write(buffer, function() {
self.lastModifiedDate = new Date()
self.size += buffer.length
self.emit('progress', self.size)
cb()
})
}
File.prototype.end = function(cb) {
var self = this
if (self.hash) {
self.hash = self.hash.digest('hex')
}
this._writeStream.end(function() {
self.emit('end')
cb()
})
}

View File

@ -1,32 +0,0 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2024/07/16 17:01:43
*/
import { parse } from 'node:querystring'
export function querystring(str) {
let query = parse(str)
for (let k of Object.keys(query)) {
let val = query[k]
if (k.endsWith('[]')) {
let _k = k.slice(0, -2)
query[_k] = val
delete query[k]
} else if (k.endsWith(']')) {
let idx = k.lastIndexOf('[')
let _pk = k.slice(0, idx)
let _k = k.slice(idx + 1, -1)
if (query[_pk]) {
query[_pk][_k] = val
} else {
query[_pk] = { [_k]: val }
}
delete query[k]
}
}
return query
}

View File

@ -1,191 +1,348 @@
import crypto from 'node:crypto' import crypto from 'crypto'
import fs from 'node:fs' import fs from 'fs'
import { join } from 'node:path' import util from 'util'
import { EventEmitter } from 'node:events' import path from 'path'
import File from './file.js' import File from './file.js'
import { EventEmitter } from 'events'
import { Stream } from 'stream'
import { StringDecoder } from 'string_decoder'
import { MultipartParser } from './multipart_parser.js' import { MultipartParser } from './multipart_parser.js'
import { UrlencodedParser } from './urlencoded_parser.js' import { QuerystringParser } from './querystring_parser.js'
import { OctetParser, BufferParser, EmptyParser } from './octet_parser.js' import { OctetParser } from './octet_parser.js'
import { JSONParser } from './json_parser.js' import { JSONParser } from './json_parser.js'
function randomPath(uploadDir) { export default function IncomingForm(opts) {
var name = 'upload_' + crypto.randomBytes(16).toString('hex') EventEmitter.call(this)
return join(uploadDir, name)
}
function parseFilename(headerValue) { opts = opts || {}
let matches = headerValue.match(/\bfilename="(.*?)"($|; )/i)
if (!matches) {
return
}
let filename = matches[1].slice(matches[1].lastIndexOf('\\') + 1) this.error = null
filename = filename.replace(/%22/g, '"') this.ended = false
filename = filename.replace(/&#([\d]{4});/g, function (m, code) {
return String.fromCharCode(code)
})
return filename
}
/* ------------------------------------- */
export default class IncomingForm extends EventEmitter {
#req = null
#error = false
#ended = false
ended = false
headers = null
bytesReceived = null
bytesExpected = null
#parser = null
#pending = 0
#openedFiles = []
constructor(req, opts = {}) {
super()
this.#req = req
this.maxFields = opts.maxFields || 1000
this.maxFieldsSize = opts.maxFieldsSize || 2 * 1024 * 1024
this.keepExtensions = opts.keepExtensions || false
this.uploadDir = opts.uploadDir this.uploadDir = opts.uploadDir
this.encoding = opts.encoding || 'utf-8' this.encoding = opts.encoding || 'utf-8'
this.headers = null
this.type = null
this.hash = opts.hash || false
this.multiples = opts.multiples || false
this.headers = req.headers this.bytesReceived = null
this.#parseContentLength() this.bytesExpected = null
this.#parseContentType()
this._parser = null
this._flushing = 0
this._fieldsSize = 0
this.openedFiles = []
}
util.inherits(IncomingForm, EventEmitter)
IncomingForm.prototype.parse = function(req, cb) {
this.pause = function() {
try {
req.pause()
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err)
}
return false
}
return true
}
this.resume = function() {
try {
req.resume()
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err)
}
return false
}
return true
}
// Setup callback first, so we don't miss anything from data events emitted
// immediately.
if (cb) {
var fields = {},
files = {}
this.on('field', function(name, value) {
fields[name] = value
})
.on('file', function(name, file) {
if (this.multiples) {
if (files[name]) {
if (!Array.isArray(files[name])) {
files[name] = [files[name]]
}
files[name].push(file)
} else {
files[name] = file
}
} else {
files[name] = file
}
})
.on('error', function(err) {
cb(err, fields, files)
})
.on('end', function() {
cb(null, fields, files)
})
}
// Parse headers and setup the parser, ready to start listening for data.
this.writeHeaders(req.headers)
// Start listening for data.
var self = this
req req
.on('error', err => { .on('error', function(err) {
this.#handleError(err) self._error(err)
this.#clearUploads()
}) })
.on('aborted', () => { .on('aborted', function() {
this.emit('aborted') self.emit('aborted')
this.#clearUploads() self._error(new Error('Request aborted'))
}) })
.on('data', buffer => this.#write(buffer)) .on('data', function(buffer) {
.on('end', () => { self.write(buffer)
if (this.#error) { })
.on('end', function() {
if (self.error) {
return return
} }
let err = this.#parser.end()
if (err) {
this.#handleError(err)
}
})
}
#write(buffer) { var err = self._parser.end()
if (this.#error) { if (err) {
self._error(err)
}
})
return this
}
IncomingForm.prototype.writeHeaders = function(headers) {
this.headers = headers
this._parseContentLength()
this._parseContentType()
}
IncomingForm.prototype.write = function(buffer) {
if (this.error) {
return return
} }
if (!this.#parser) { if (!this._parser) {
return this.#handleError(new Error('uninitialized parser')) this._error(new Error('uninitialized parser'))
return
} }
this.bytesReceived += buffer.length this.bytesReceived += buffer.length
this.emit('progress', this.bytesReceived, this.bytesExpected) this.emit('progress', this.bytesReceived, this.bytesExpected)
this.#parser.write(buffer) var bytesParsed = this._parser.write(buffer)
if (bytesParsed !== buffer.length) {
this._error(
new Error(
'parser error, ' +
bytesParsed +
' of ' +
buffer.length +
' bytes parsed'
)
)
} }
#handlePart(part) { return bytesParsed
if (part.filename === undefined) { }
let value = Buffer.from('')
part IncomingForm.prototype.pause = function() {
.on('data', buf => { // this does nothing, unless overwritten in IncomingForm.parse
value = Buffer.concat([value, buf]) return false
}
IncomingForm.prototype.resume = function() {
// this does nothing, unless overwritten in IncomingForm.parse
return false
}
IncomingForm.prototype.onPart = function(part) {
// this method can be overwritten by the user
this.handlePart(part)
}
IncomingForm.prototype.handlePart = function(part) {
var self = this
if (part.filename === undefined) {
var value = '',
decoder = new StringDecoder(this.encoding)
part.on('data', function(buffer) {
self._fieldsSize += buffer.length
if (self._fieldsSize > self.maxFieldsSize) {
self._error(
new Error(
'maxFieldsSize exceeded, received ' +
self._fieldsSize +
' bytes of field data'
)
)
return
}
value += decoder.write(buffer)
}) })
.on('end', () => {
this.emit('field', part.name, value.toString(this.encoding)) part.on('end', function() {
self.emit('field', part.name, value)
}) })
} else { return
let file = new File({ }
path: randomPath(this.uploadDir),
this._flushing++
var file = new File({
path: this._uploadPath(part.filename),
name: part.filename, name: part.filename,
type: part.mime type: part.mime,
hash: self.hash
}) })
this.emit('fileBegin', part.name, file)
file.open() file.open()
this.openedFiles.push(file)
this.#openedFiles.push(file) part.on('data', function(buffer) {
// 表单解析完的时候文件写入不一定完成了, 所以需要加入pending计数
this.#pending++
part
.on('data', buffer => {
if (buffer.length == 0) { if (buffer.length == 0) {
return return
} }
file.write(buffer) self.pause()
}) file.write(buffer, function() {
.on('end', () => { self.resume()
if (part.ended) {
return
}
part.ended = true
file.end(() => {
this.emit('file', part.name, file)
this.#pending--
}) })
}) })
}
}
#parseContentType() { part.on('end', function() {
let contentType = this.headers['content-type'] || '' file.end(function() {
let lower = contentType.toLowerCase() self._flushing--
self.emit('file', part.name, file)
self._maybeEnd()
})
})
}
function dummyParser(self) {
return {
end: function() {
self.ended = true
self._maybeEnd()
return null
}
}
}
IncomingForm.prototype._parseContentType = function() {
if (this.bytesExpected === 0) { if (this.bytesExpected === 0) {
return (this.#parser = new EmptyParser()) this._parser = dummyParser(this)
return
} }
if (lower.includes('octet-stream')) { if (!this.headers['content-type']) {
return this.#createStreamParser() this._error(new Error('bad content-type header, no content-type'))
return
} }
if (lower.includes('urlencoded')) { if (this.headers['content-type'].match(/octet-stream/i)) {
return this.#createUrlencodedParser() this._initOctetStream()
return
} }
if (lower.includes('multipart')) { if (this.headers['content-type'].match(/urlencoded/i)) {
let matches = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/) this._initUrlencoded()
if (matches) { return
this.#createMultipartParser(matches[1] || matches[2]) }
if (this.headers['content-type'].match(/multipart/i)) {
var m = this.headers['content-type'].match(
/boundary=(?:"([^"]+)"|([^;]+))/i
)
if (m) {
this._initMultipart(m[1] || m[2])
} else { } else {
this.#handleError(new TypeError('unknow multipart boundary')) this._error(new Error('bad content-type header, no multipart boundary'))
} }
return return
} }
if (lower.match(/json|appliation|plain|text/)) { if (this.headers['content-type'].match(/json|appliation|plain|text/i)) {
return this.#createJsonParser() this._initJSONencoded()
return
} }
this.#createBufferParser() this._error(
new Error(
'bad content-type header, unknown content-type: ' +
this.headers['content-type']
)
)
}
IncomingForm.prototype._error = function(err) {
if (this.error || this.ended) {
return
} }
#parseContentLength() { this.error = err
this.emit('error', err)
if (Array.isArray(this.openedFiles)) {
this.openedFiles.forEach(function(file) {
file._writeStream.destroy()
setTimeout(fs.unlink, 0, file.path, function(error) {})
})
}
}
IncomingForm.prototype._parseContentLength = function() {
this.bytesReceived = 0 this.bytesReceived = 0
if (this.headers['content-length']) { if (this.headers['content-length']) {
this.bytesExpected = +this.headers['content-length'] this.bytesExpected = parseInt(this.headers['content-length'], 10)
} else if (this.headers['transfer-encoding'] === undefined) { } else if (this.headers['transfer-encoding'] === undefined) {
this.bytesExpected = 0 this.bytesExpected = 0
} }
if (this.bytesExpected !== null) {
this.emit('progress', this.bytesReceived, this.bytesExpected)
} }
}
#createMultipartParser(boundary) { IncomingForm.prototype._newParser = function() {
let headerField, headerValue, part return new MultipartParser()
}
this.#parser = new MultipartParser(boundary) IncomingForm.prototype._initMultipart = function(boundary) {
this.type = 'multipart'
this.#parser.$partBegin = function () { var parser = new MultipartParser(),
part = new EventEmitter() self = this,
headerField,
headerValue,
part
parser.initWithBoundary(boundary)
parser.onPartBegin = function() {
part = new Stream()
part.readable = true part.readable = true
part.headers = {} part.headers = {}
part.name = null part.name = null
@ -199,25 +356,25 @@ export default class IncomingForm extends EventEmitter {
headerValue = '' headerValue = ''
} }
this.#parser.$headerField = b => { parser.onHeaderField = function(b, start, end) {
headerField += b.toString(this.encoding) headerField += b.toString(self.encoding, start, end)
} }
this.#parser.$headerValue = b => { parser.onHeaderValue = function(b, start, end) {
headerValue += b.toString(this.encoding) headerValue += b.toString(self.encoding, start, end)
} }
this.#parser.$headerEnd = () => { parser.onHeaderEnd = function() {
headerField = headerField.toLowerCase() headerField = headerField.toLowerCase()
part.headers[headerField] = headerValue part.headers[headerField] = headerValue
let matches = headerValue.match(/\bname="([^"]+)"/i) var m = headerValue.match(/\bname="([^"]+)"/i)
if (headerField == 'content-disposition') { if (headerField == 'content-disposition') {
if (matches) { if (m) {
part.name = matches[1] part.name = m[1]
} }
part.filename = parseFilename(headerValue) part.filename = self._fileName(headerValue)
} else if (headerField == 'content-type') { } else if (headerField == 'content-type') {
part.mime = headerValue part.mime = headerValue
} else if (headerField == 'content-transfer-encoding') { } else if (headerField == 'content-transfer-encoding') {
@ -228,138 +385,187 @@ export default class IncomingForm extends EventEmitter {
headerValue = '' headerValue = ''
} }
this.#parser.$headersEnd = () => { parser.onHeadersEnd = function() {
switch (part.transferEncoding) { switch (part.transferEncoding) {
case 'binary': case 'binary':
case '7bit': case '7bit':
case '8bit': case '8bit':
this.#parser.$partData = function (b) { parser.onPartData = function(b, start, end) {
part.emit('data', b) part.emit('data', b.slice(start, end))
} }
this.#parser.$partEnd = function () {
parser.onPartEnd = function() {
part.emit('end') part.emit('end')
} }
break break
case 'base64': case 'base64':
this.#parser.$partData = function (b) { parser.onPartData = function(b, start, end) {
part.transferBuffer += b.toString('ascii') part.transferBuffer += b.slice(start, end).toString('ascii')
// 确保offset的值能被4整除 /*
let offset = ~~(part.transferBuffer.length / 4) * 4 four bytes (chars) in base64 converts to three bytes in binary
encoding. So we should always work with a number of bytes that
can be divided by 4, it will result in a number of buytes that
can be divided vy 3.
*/
var offset = parseInt(part.transferBuffer.length / 4, 10) * 4
part.emit( part.emit(
'data', 'data',
Buffer.from(part.transferBuffer.slice(0, offset), 'base64') Buffer.from(part.transferBuffer.substring(0, offset), 'base64')
) )
part.transferBuffer = part.transferBuffer.slice(offset) part.transferBuffer = part.transferBuffer.substring(offset)
} }
this.#parser.$partEnd = function () {
parser.onPartEnd = function() {
part.emit('data', Buffer.from(part.transferBuffer, 'base64')) part.emit('data', Buffer.from(part.transferBuffer, 'base64'))
part.emit('end') part.emit('end')
} }
break break
default: default:
return this.#handleError(new Error('unknown transfer-encoding')) return self._error(new Error('unknown transfer-encoding'))
} }
this.#handlePart(part) self.onPart(part)
} }
this.#parser.$end = () => { parser.onEnd = function() {
if (this.#pending > 0) { self.ended = true
setTimeout(_ => this.#parser.$end()) self._maybeEnd()
} else {
this.#handleEnd()
}
}
} }
#createUrlencodedParser() { this._parser = parser
this.#parser = new UrlencodedParser() }
if (this.bytesExpected) { IncomingForm.prototype._fileName = function(headerValue) {
this.#parser.initLength(this.bytesExpected) var m = headerValue.match(/\bfilename="(.*?)"($|; )/i)
} if (!m) return
this.#parser var filename = m[1].substr(m[1].lastIndexOf('\\') + 1)
.on('field', fields => this.emit('field', false, fields)) filename = filename.replace(/%22/g, '"')
.on('end', () => this.#handleEnd()) filename = filename.replace(/&#([\d]{4});/g, function(m, code) {
} return String.fromCharCode(code)
})
#createStreamParser() { return filename
let filename = this.headers['x-file-name'] }
let mime = this.headers['x-file-type']
IncomingForm.prototype._initUrlencoded = function() {
this.#parser = new OctetParser(filename, mime, randomPath(this.uploadDir)) this.type = 'urlencoded'
if (this.bytesExpected) { var parser = new QuerystringParser(this.maxFields)
this.#parser.initLength(this.bytesExpected)
} parser.onField = (key, val) => {
this.emit('field', key, val)
this.#parser }
.on('file', file => {
this.emit('file', false, file) parser.onEnd = () => {
}) this.ended = true
.on('end', () => this.#handleEnd()) this._maybeEnd()
.on('error', err => this.#handleError(err)) }
}
this._parser = parser
#createJsonParser() { }
this.#parser = new JSONParser()
IncomingForm.prototype._initOctetStream = function() {
if (this.bytesExpected) { this.type = 'octet-stream'
this.#parser.initLength(this.bytesExpected) var filename = this.headers['x-file-name']
} var mime = this.headers['content-type']
this.#parser var file = new File({
.on('field', (key, val) => { path: this._uploadPath(filename),
this.emit('field', key, val) name: filename,
}) type: mime
.on('end', () => this.#handleEnd()) })
.on('error', err => this.#handleError(err))
} this.emit('fileBegin', filename, file)
file.open()
#createBufferParser() {
this.#parser = new BufferParser() this._flushing++
if (this.bytesExpected) { var self = this
this.#parser.initLength(this.bytesExpected)
} self._parser = new OctetParser()
this.#parser //Keep track of writes that haven't finished so we don't emit the file before it's done being written
.on('buffer', buf => { var outstandingWrites = 0
this.emit('buffer', buf)
}) self._parser.on('data', function(buffer) {
.on('end', () => this.#handleEnd()) self.pause()
.on('error', err => this.#handleError(err)) outstandingWrites++
}
file.write(buffer, function() {
#clearUploads() { outstandingWrites--
while (this.#openedFiles.length) { self.resume()
let file = this.#openedFiles.pop()
file._writeStream.destroy() if (self.ended) {
setTimeout(_ => { self._parser.emit('doneWritingFile')
try { }
fs.unlink(file.path) })
} catch (e) {} })
})
} self._parser.on('end', function() {
} self._flushing--
self.ended = true
#handleError(err) {
if (this.#error || this.#ended) { var done = function() {
return file.end(function() {
} self.emit('file', 'file', file)
this.error = true self._maybeEnd()
this.emit('error', err) })
} }
#handleEnd() { if (outstandingWrites === 0) {
if (this.#ended || this.#error) { done()
return } else {
} self._parser.once('doneWritingFile', done)
this.#ended = true }
this.emit('end') })
} }
IncomingForm.prototype._initJSONencoded = function() {
this.type = 'json'
var parser = new JSONParser(),
self = this
if (this.bytesExpected) {
parser.initWithLength(this.bytesExpected)
}
parser.onField = function(key, val) {
self.emit('field', key, val)
}
parser.onEnd = function() {
self.ended = true
self._maybeEnd()
}
this._parser = parser
}
IncomingForm.prototype._uploadPath = function(filename) {
var name = 'upload_'
var buf = crypto.randomBytes(16)
for (var i = 0; i < buf.length; ++i) {
name += ('0' + buf[i].toString(16)).slice(-2)
}
if (this.keepExtensions) {
var ext = path.extname(filename)
ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1')
name += ext
}
return path.join(this.uploadDir, name)
}
IncomingForm.prototype._maybeEnd = function() {
if (!this.ended || this._flushing || this.error) {
return
}
this.emit('end')
} }

View File

@ -1,43 +1,33 @@
import { EventEmitter } from 'node:events' export function JSONParser() {
this.data = Buffer.from('')
this.bytesWritten = 0
}
export class JSONParser extends EventEmitter { JSONParser.prototype.initWithLength = function(length) {
#buf = Buffer.from('') this.data = Buffer.alloc(length)
#byteLen = 0 }
initLength(length) { JSONParser.prototype.write = function(buffer) {
this.#byteLen = length if (this.data.length >= this.bytesWritten + buffer.length) {
buffer.copy(this.data, this.bytesWritten)
} else {
this.data = Buffer.concat([this.data, buffer])
} }
this.bytesWritten += buffer.length
return buffer.length
}
write(buffer) { JSONParser.prototype.end = function() {
this.#buf = Buffer.concat([this.#buf, buffer]) var data = this.data.toString('utf8')
} var fields
end() {
if (this.#buf.length === this.#byteLen) {
let data = this.#buf.toString()
let fields = data
try { try {
fields = JSON.parse(data) fields = JSON.parse(data)
} catch (e) { } catch (e) {
try { fields = Function(`try{return ${data}}catch(e){}`)() || data
// 非标准的json语法,尝试用 Function 解析
fields = Function(`try{return ${data}}catch(e){}`)()
} catch (err) {}
} }
this.emit('field', false, fields) this.onField(false, fields)
this.emit('end') this.data = null
this.#buf = null this.onEnd()
} else {
this.emit(
'error',
new Error(
`The uploaded data is incomplete. Expected ${
this.#byteLen
}, Received ${this.#buf.length} .`
)
)
}
}
} }

View File

@ -1,11 +1,5 @@
/** var s = 0,
* {} S = {
* @author yutent<yutent.io@gmail.com>
* @date 2023/10/30 16:41:59
*/
let s = 0
const STATE_DICT = {
PARSER_UNINITIALIZED: s++, PARSER_UNINITIALIZED: s++,
START: s++, START: s++,
START_BOUNDARY: s++, START_BOUNDARY: s++,
@ -19,82 +13,56 @@ const STATE_DICT = {
PART_DATA: s++, PART_DATA: s++,
PART_END: s++, PART_END: s++,
END: s++ END: s++
} },
let f = 1 f = 1,
const FLAG_DICT = { F = {
PART_BOUNDARY: f, PART_BOUNDARY: f,
LAST_BOUNDARY: (f *= 2) LAST_BOUNDARY: (f *= 2)
} },
LF = 10,
const LF = 10 CR = 13,
const CR = 13 SPACE = 32,
const SPACE = 32 HYPHEN = 45,
const HYPHEN = 45 COLON = 58,
const COLON = 58 A = 97,
const LETTER_A = 97 Z = 122,
const LETTER_Z = 122 lower = function(c) {
function lower(c) {
return c | 0x20 return c | 0x20
}
export function MultipartParser() {
this.boundary = null
this.boundaryChars = null
this.lookbehind = null
this.state = S.PARSER_UNINITIALIZED
this.index = null
this.flags = 0
} }
function stateToString(value) { MultipartParser.stateToString = function(stateNumber) {
for (let key in STATE_DICT) { for (var state in S) {
let number = STATE_DICT[key] var number = S[state]
if (number === value) { if (number === stateNumber) return state
return key
}
} }
} }
export class MultipartParser { MultipartParser.prototype.initWithBoundary = function(str) {
boundary = null
boundaryChars = null
lookbehind = null
state = STATE_DICT.PARSER_UNINITIALIZED
index = null
flags = 0
constructor(str) {
this.boundary = Buffer.alloc(str.length + 4) this.boundary = Buffer.alloc(str.length + 4)
this.boundary.write('\r\n--', 0) this.boundary.write('\r\n--', 0)
this.boundary.write(str, 4) this.boundary.write(str, 4)
this.lookbehind = Buffer.alloc(this.boundary.length + 8) this.lookbehind = Buffer.alloc(this.boundary.length + 8)
this.state = STATE_DICT.START this.state = S.START
this.boundaryChars = {} this.boundaryChars = {}
for (let i = 0; i < this.boundary.length; i++) { for (var i = 0; i < this.boundary.length; i++) {
this.boundaryChars[this.boundary[i]] = true this.boundaryChars[this.boundary[i]] = true
} }
} }
#mark(k, v) { MultipartParser.prototype.write = function(buffer) {
this[k + 'Mark'] = v var self = this,
} i = 0,
#emit(name, buf, idx, cleanup) {
let mark = name + 'Mark'
if (this[mark] !== void 0) {
let start = this[mark]
let end = buf.length
if (cleanup) {
end = idx
delete this[mark]
} else {
this[mark] = 0
}
if (start === end) {
return
}
this['$' + name](buf.slice(start, end))
}
}
write(buffer) {
let idx = 0,
len = buffer.length, len = buffer.length,
prevIndex = this.index, prevIndex = this.index,
index = this.index, index = this.index,
@ -107,39 +75,67 @@ export class MultipartParser {
boundaryEnd = boundaryLength - 1, boundaryEnd = boundaryLength - 1,
bufferLength = buffer.length, bufferLength = buffer.length,
c, c,
cl cl,
mark = function(name) {
for (idx = 0; idx < len; idx++) { self[name + 'Mark'] = i
c = buffer[idx] },
clear = function(name) {
switch (state) { delete self[name + 'Mark']
case STATE_DICT.PARSER_UNINITIALIZED: },
callback = function(name, buffer, start, end) {
if (start !== undefined && start === end) {
return return
}
case STATE_DICT.START: var callbackSymbol =
'on' + name.substr(0, 1).toUpperCase() + name.substr(1)
if (callbackSymbol in self) {
self[callbackSymbol](buffer, start, end)
}
},
dataCallback = function(name, clear) {
var markSymbol = name + 'Mark'
if (!(markSymbol in self)) {
return
}
if (!clear) {
callback(name, buffer, self[markSymbol], buffer.length)
self[markSymbol] = 0
} else {
callback(name, buffer, self[markSymbol], i)
delete self[markSymbol]
}
}
for (i = 0; i < len; i++) {
c = buffer[i]
switch (state) {
case S.PARSER_UNINITIALIZED:
return i
case S.START:
index = 0 index = 0
state = STATE_DICT.START_BOUNDARY state = S.START_BOUNDARY
case S.START_BOUNDARY:
case STATE_DICT.START_BOUNDARY:
if (index == boundary.length - 2) { if (index == boundary.length - 2) {
if (c == HYPHEN) { if (c == HYPHEN) {
flags |= FLAG_DICT.LAST_BOUNDARY flags |= F.LAST_BOUNDARY
} else if (c != CR) { } else if (c != CR) {
return return i
} }
index++ index++
break break
} else if (index - 1 == boundary.length - 2) { } else if (index - 1 == boundary.length - 2) {
if (flags & FLAG_DICT.LAST_BOUNDARY && c == HYPHEN) { if (flags & F.LAST_BOUNDARY && c == HYPHEN) {
this.$end() callback('end')
state = STATE_DICT.END state = S.END
flags = 0 flags = 0
} else if (!(flags & FLAG_DICT.LAST_BOUNDARY) && c == LF) { } else if (!(flags & F.LAST_BOUNDARY) && c == LF) {
index = 0 index = 0
this.$partBegin() callback('partBegin')
state = STATE_DICT.HEADER_FIELD_START state = S.HEADER_FIELD_START
} else { } else {
return return i
} }
break break
} }
@ -151,16 +147,14 @@ export class MultipartParser {
index++ index++
} }
break break
case S.HEADER_FIELD_START:
case STATE_DICT.HEADER_FIELD_START: state = S.HEADER_FIELD
state = STATE_DICT.HEADER_FIELD mark('headerField')
this.#mark('headerField', idx)
index = 0 index = 0
case S.HEADER_FIELD:
case STATE_DICT.HEADER_FIELD:
if (c == CR) { if (c == CR) {
delete this.headerFieldMark clear('headerField')
state = STATE_DICT.HEADERS_ALMOST_DONE state = S.HEADERS_ALMOST_DONE
break break
} }
@ -172,72 +166,66 @@ export class MultipartParser {
if (c == COLON) { if (c == COLON) {
if (index == 1) { if (index == 1) {
// empty header field // empty header field
return return i
} }
this.#emit('headerField', buffer, idx, true) dataCallback('headerField', true)
state = STATE_DICT.HEADER_VALUE_START state = S.HEADER_VALUE_START
break break
} }
cl = lower(c) cl = lower(c)
if (cl < LETTER_A || cl > LETTER_Z) { if (cl < A || cl > Z) {
return return i
} }
break break
case S.HEADER_VALUE_START:
case STATE_DICT.HEADER_VALUE_START:
if (c == SPACE) { if (c == SPACE) {
break break
} }
this.#mark('headerValue', idx) mark('headerValue')
state = STATE_DICT.HEADER_VALUE state = S.HEADER_VALUE
case S.HEADER_VALUE:
case STATE_DICT.HEADER_VALUE:
if (c == CR) { if (c == CR) {
this.#emit('headerValue', buffer, idx, true) dataCallback('headerValue', true)
this.$headerEnd() callback('headerEnd')
state = STATE_DICT.HEADER_VALUE_ALMOST_DONE state = S.HEADER_VALUE_ALMOST_DONE
} }
break break
case S.HEADER_VALUE_ALMOST_DONE:
case STATE_DICT.HEADER_VALUE_ALMOST_DONE:
if (c != LF) { if (c != LF) {
return return i
} }
state = STATE_DICT.HEADER_FIELD_START state = S.HEADER_FIELD_START
break break
case S.HEADERS_ALMOST_DONE:
case STATE_DICT.HEADERS_ALMOST_DONE:
if (c != LF) { if (c != LF) {
return return i
} }
this.$headersEnd() callback('headersEnd')
state = STATE_DICT.PART_DATA_START state = S.PART_DATA_START
break break
case S.PART_DATA_START:
case STATE_DICT.PART_DATA_START: state = S.PART_DATA
state = STATE_DICT.PART_DATA mark('partData')
this.#mark('partData', idx) case S.PART_DATA:
case STATE_DICT.PART_DATA:
prevIndex = index prevIndex = index
if (index === 0) { if (index === 0) {
// boyer-moore derrived algorithm to safely skip non-boundary data // boyer-moore derrived algorithm to safely skip non-boundary data
idx += boundaryEnd i += boundaryEnd
while (idx < bufferLength && !(buffer[idx] in boundaryChars)) { while (i < bufferLength && !(buffer[i] in boundaryChars)) {
idx += boundaryLength i += boundaryLength
} }
idx -= boundaryEnd i -= boundaryEnd
c = buffer[idx] c = buffer[i]
} }
if (index < boundary.length) { if (index < boundary.length) {
if (boundary[index] == c) { if (boundary[index] == c) {
if (index === 0) { if (index === 0) {
this.#emit('partData', buffer, idx, true) dataCallback('partData', true)
} }
index++ index++
} else { } else {
@ -247,29 +235,29 @@ export class MultipartParser {
index++ index++
if (c == CR) { if (c == CR) {
// CR = part boundary // CR = part boundary
flags |= FLAG_DICT.PART_BOUNDARY flags |= F.PART_BOUNDARY
} else if (c == HYPHEN) { } else if (c == HYPHEN) {
// HYPHEN = end boundary // HYPHEN = end boundary
flags |= FLAG_DICT.LAST_BOUNDARY flags |= F.LAST_BOUNDARY
} else { } else {
index = 0 index = 0
} }
} else if (index - 1 == boundary.length) { } else if (index - 1 == boundary.length) {
if (flags & FLAG_DICT.PART_BOUNDARY) { if (flags & F.PART_BOUNDARY) {
index = 0 index = 0
if (c == LF) { if (c == LF) {
// unset the PART_BOUNDARY flag // unset the PART_BOUNDARY flag
flags &= ~FLAG_DICT.PART_BOUNDARY flags &= ~F.PART_BOUNDARY
this.$partEnd() callback('partEnd')
this.$partBegin() callback('partBegin')
state = STATE_DICT.HEADER_FIELD_START state = S.HEADER_FIELD_START
break break
} }
} else if (flags & FLAG_DICT.LAST_BOUNDARY) { } else if (flags & F.LAST_BOUNDARY) {
if (c == HYPHEN) { if (c == HYPHEN) {
this.$partEnd() callback('partEnd')
this.$end() callback('end')
state = STATE_DICT.END state = S.END
flags = 0 flags = 0
} else { } else {
index = 0 index = 0
@ -286,50 +274,54 @@ export class MultipartParser {
} else if (prevIndex > 0) { } else if (prevIndex > 0) {
// if our boundary turned out to be rubbish, the captured lookbehind // if our boundary turned out to be rubbish, the captured lookbehind
// belongs to partData // belongs to partData
callback('partData', lookbehind, 0, prevIndex)
this.$partData(lookbehind.slice(0, prevIndex))
prevIndex = 0 prevIndex = 0
this.#mark('partData', idx) mark('partData')
// reconsider the current character even so it interrupted the sequence // reconsider the current character even so it interrupted the sequence
// it could be the beginning of a new sequence // it could be the beginning of a new sequence
idx-- i--
} }
break break
case S.END:
case STATE_DICT.END:
break break
default: default:
return return i
} }
} }
this.#emit('headerField', buffer, idx) dataCallback('headerField')
this.#emit('headerValue', buffer, idx) dataCallback('headerValue')
this.#emit('partData', buffer, idx) dataCallback('partData')
this.index = index this.index = index
this.state = state this.state = state
this.flags = flags this.flags = flags
}
end() { return len
}
MultipartParser.prototype.end = function() {
var callback = function(self, name) {
var callbackSymbol = 'on' + name.substr(0, 1).toUpperCase() + name.substr(1)
if (callbackSymbol in self) {
self[callbackSymbol]()
}
}
if ( if (
(this.state === STATE_DICT.HEADER_FIELD_START && this.index === 0) || (this.state == S.HEADER_FIELD_START && this.index === 0) ||
(this.state === STATE_DICT.PART_DATA && (this.state == S.PART_DATA && this.index == this.boundary.length)
this.index == this.boundary.length)
) { ) {
this.$end() callback(this, 'partEnd')
} else if (this.state !== STATE_DICT.END) { callback(this, 'end')
} else if (this.state != S.END) {
return new Error( return new Error(
'MultipartParser.end(): stream ended unexpectedly: ' + this.explain() 'MultipartParser.end(): stream ended unexpectedly: ' + this.explain()
) )
} }
} }
explain() { MultipartParser.prototype.explain = function() {
return 'state = ' + stateToString(this.state) return 'state = ' + MultipartParser.stateToString(this.state)
}
} }

View File

@ -1,86 +1,17 @@
/** import { EventEmitter } from 'events'
* {} import util from 'util'
* @author yutent<yutent.io@gmail.com>
* @date 2023/10/27 14:23:22
*/
import { EventEmitter } from 'node:events' export function OctetParser() {
import File from './file.js' EventEmitter.call(this)
export class OctetParser extends EventEmitter {
#file = null
#byteLen = 0
#wroteLen = 0
constructor(name, type, path) {
super()
this.#file = new File({ path, name, type })
this.#file.open()
}
initLength(length) {
this.#byteLen = length
}
write(buffer) {
this.#file.write(buffer)
this.#wroteLen += buffer.length
}
end() {
this.#file.end(_ => {
if (this.#wroteLen === this.#byteLen) {
this.emit('file', this.#file)
this.emit('end')
} else {
this.emit(
'error',
new Error(
`The uploaded data is incomplete. Expected ${
this.#byteLen
}, Received ${this.#wroteLen} .`
)
)
}
})
}
} }
export class BufferParser extends EventEmitter { util.inherits(OctetParser, EventEmitter)
#buf = Buffer.from('')
#byteLen = 0
initLength(length) { OctetParser.prototype.write = function(buffer) {
this.#byteLen = length this.emit('data', buffer)
} return buffer.length
write(buffer) {
this.#buf = Buffer.concat([this.#buf, buffer])
}
end() {
if (this.#buf.length === this.#byteLen) {
this.emit('buffer', this.#buf)
this.emit('end')
this.#buf = null
} else {
this.emit(
'error',
new Error(
`The uploaded data is incomplete. Expected ${
this.#byteLen
}, Received ${this.#buf.length} .`
)
)
}
}
} }
export class EmptyParser extends EventEmitter { OctetParser.prototype.end = function() {
write() {}
end() {
this.emit('end') this.emit('end')
}
} }

27
lib/querystring_parser.js Normal file
View File

@ -0,0 +1,27 @@
// This is a buffering parser, not quite as nice as the multipart one.
// If I find time I'll rewrite this to be fully streaming as well
import querystring from 'querystring'
export class QuerystringParser {
constructor(maxKeys) {
this.maxKeys = maxKeys
this.buffer = ''
}
write(buffer) {
this.buffer += buffer.toString('ascii')
return buffer.length
}
end() {
var fields = querystring.parse(this.buffer, '&', '=', {
maxKeys: this.maxKeys
})
for (var field in fields) {
this.onField(field, fields[field])
}
this.buffer = ''
this.onEnd()
}
}

View File

@ -1,43 +0,0 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2023/10/27 12:14:05
*/
import { EventEmitter } from 'node:events'
import { querystring } from './helper.js'
export class UrlencodedParser extends EventEmitter {
#buf = Buffer.from('')
#byteLen = 0
initLength(length) {
this.#byteLen = length
}
write(buffer) {
this.#buf = Buffer.concat([this.#buf, buffer])
}
end() {
if (this.#buf.length === this.#byteLen) {
let data = this.#buf.toString()
let fields = querystring(data)
this.#buf = null
this.emit('field', fields)
this.emit('end')
this.#buf = null
} else {
this.emit(
'error',
new Error(
`The uploaded data is incomplete. Expected ${
this.#byteLen
}, Received ${this.#buf.length} .`
)
)
}
}
}

View File

@ -1,23 +1,22 @@
{ {
"name": "@gm5/request", "name": "@gm5/request",
"version": "2.0.7", "version": "1.2.8",
"description": "对Http的Request进一步封装, 提供常用的API", "description": "对Http的request进一步封装, 提供常用的API",
"main": "index.js", "main": "index.js",
"author": "yutent", "author": "yutent",
"type": "module", "type": "module",
"keywords": [ "keywords": [
"five", "five",
"gmf", "node-five",
"gm5",
"five.js", "five.js",
"fivejs", "fivejs",
"request", "request",
"http" "http"
], ],
"dependencies": { "dependencies": {
"es.shim": "^2.2.0", "es.shim": "^2.0.1",
"iofs": "^1.5.3" "iofs": "^1.5.0"
}, },
"repository": "https://git.wkit.fun/gm5/request.git", "repository": "https://github.com/bytedo/gmf.request.git",
"license": "MIT" "license": "MIT"
} }