一大波更新

v2
yutent 2023-10-26 19:02:46 +08:00
parent 6b3a44d387
commit 94a997bb8a
8 changed files with 360 additions and 462 deletions

308
index.js
View File

@ -8,15 +8,15 @@ import 'es.shim'
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 fs from 'iofs' import fs from 'iofs'
import URL from 'url' import { fileURLToPath, parse } from 'node:url'
import QS from 'querystring' import QS from 'node:querystring'
import PATH from 'path' import { dirname, resolve } from 'node:path'
const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded' const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded'
const __dirname = PATH.dirname(URL.fileURLToPath(import.meta.url)) const __dirname = dirname(fileURLToPath(import.meta.url))
const tmpdir = PATH.resolve(__dirname, '.tmp/') const tmpdir = resolve(__dirname, '.tmp/')
const encode = encodeURIComponent const encode = encodeURIComponent
const decode = decodeURIComponent const decode = decodeURIComponent
@ -36,33 +36,46 @@ function hideProperty(host, name, value) {
} }
export default class Request { export default class Request {
#req = null
#res = null
#query = null
#body = null
#cookies = Object.create(null)
method = 'GET'
path = []
url = ''
host = '127.0.0.1'
constructor(req, res) { constructor(req, res) {
this.method = req.method.toUpperCase() this.method = req.method.toUpperCase()
this.params = {}
hideProperty(this, 'origin', { req, res }) this.#req = req
hideProperty(this, '__GET__', null) this.#res = res
hideProperty(this, '__POST__', null)
hideProperty(this, '__COOKIE__', parseCookie(this.header('cookie') || '')) this.host = req.headers['host']
this.__fixUrl() this.#cookies = parseCookie(this.headers['cookie'] || '')
this.#init()
} }
// 修正请求的url // 修正请求的url
__fixUrl() { #init() {
let _url = URL.parse(this.origin.req.url) let _url = parse(this.#req.url)
.pathname.slice(1) .pathname.slice(1)
.replace(/[\/]+$/, '') .replace(/[\/]+$/, '')
let app = '' // 将作为主控制器(即apps目录下的应用) let app = '' // 将作为主控制器(即apps目录下的应用)
let pathArr = [] let pathArr = []
let tmpArr = []
// URL上不允许有非法字符 // URL上不允许有非法字符
if (/[^\w-/.@~!$&:+'=]/.test(decode(_url))) { if (/[^\w-/.,@~!$&:+'"=]/.test(decode(_url))) {
this.origin.res.rendered = true this.#res.rendered = true
this.origin.res.writeHead(400, { this.#res.writeHead(400, {
'X-debug': `url [/${encode(_url)}] contains invalid characters` 'X-debug': `url [/${encode(_url)}] contains invalid characters`
}) })
return this.origin.res.end(`Invalid characters: /${_url}`) return this.#res.end(`Invalid characters: /${_url}`)
} }
// 修正url中可能出现的"多斜杠" // 修正url中可能出现的"多斜杠"
@ -85,200 +98,153 @@ export default class Request {
pathArr.shift() pathArr.shift()
// 将path第3段之后的部分, 每2个一组转为key-val数据对象, 存入params中
tmpArr = pathArr.slice(1).concat()
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.app = app
this.url = _url this.url = _url
this.path = pathArr this.path = pathArr
} }
/** /**
* [get 同php的$_GET] * [解析请求体, 需要 await ]
*/
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 [字段]
*/ */
post(key = '', xss = true) { #parseBody() {
let para = {}
let out = Promise.defer() let out = Promise.defer()
let form, contentType let form, contentType
xss = !!xss this.#body = {}
//如果之前已经缓存过,则直接从缓存读取
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() form = new Parser(this.#req, { uploadDir: tmpdir })
form.uploadDir = tmpdir
form.parse(this.origin.req)
form.on('field', (name, value) => { form
if (name === false) { .on('field', (name, value) => {
para = value if (name === false) {
return this.#body = value
} return
if (~contentType.indexOf('urlencoded')) {
if (
name.slice(0, 2) === '{"' &&
(name.slice(-2) === '"}' || value.slice(-2) === '"}')
) {
name = name.replace(/\s/g, '+')
if (value.slice(0, 1) === '=') value = '=' + value
return Object.assign(para, JSON.parse(name + value))
} }
} if (~contentType.indexOf('urlencoded')) {
if (
name.slice(0, 2) === '{"' &&
(name.slice(-2) === '"}' || value.slice(-2) === '"}')
) {
name = name.replace(/\s/g, '+')
if (typeof value === 'string') { if (value.slice(0, 1) === '=') value = '=' + value
value = xss ? value.xss() : value
}
if (name.slice(-2) === '[]') { return Object.assign(this.#body, JSON.parse(name + value))
name = name.slice(0, -2) }
if (typeof value === 'string') {
value = [value]
} }
} else if (name.slice(-1) === ']') {
let key = name.slice(name.lastIndexOf('[') + 1, -1)
name = name.slice(0, name.lastIndexOf('['))
//多解析一层对象(也仅支持到这一层) if (name.slice(-2) === '[]') {
if (name.slice(-1) === ']') { name = name.slice(0, -2)
let pkey = name.slice(name.lastIndexOf('[') + 1, -1) if (typeof value === 'string') {
value = [value]
}
} else if (name.slice(-1) === ']') {
let key = name.slice(name.lastIndexOf('[') + 1, -1)
name = name.slice(0, name.lastIndexOf('[')) name = name.slice(0, name.lastIndexOf('['))
if (!para.hasOwnProperty(name)) { //多解析一层对象(也仅支持到这一层)
para[name] = {} if (name.slice(-1) === ']') {
} let pkey = name.slice(name.lastIndexOf('[') + 1, -1)
name = name.slice(0, name.lastIndexOf('['))
if (!para[name].hasOwnProperty(pkey)) { if (!this.#body.hasOwnProperty(name)) {
para[name][pkey] = {} this.#body[name] = {}
} }
para[name][pkey][key] = value if (!this.#body[name].hasOwnProperty(pkey)) {
} else { this.#body[name][pkey] = {}
if (!para.hasOwnProperty(name)) { }
para[name] = {}
} this.#body[name][pkey][key] = value
} else {
para[name][key] = value if (!this.#body.hasOwnProperty(name)) {
} this.#body[name] = {}
return }
}
this.#body[name][key] = value
para[name] = value }
}) return
}
form.on('file', (name, file) => {
if (name.slice(-2) === '[]') { this.#body[name] = value
name = name.slice(0, -2) })
} .on('file', (name, file) => {
if (!para.hasOwnProperty(name)) { if (name.slice(-2) === '[]') {
para[name] = file name = name.slice(0, -2)
} else { }
if (!Array.isArray(para[name])) { if (!this.#body.hasOwnProperty(name)) {
para[name] = [para[name]] this.#body[name] = file
} } else {
para[name].push(file) if (!Array.isArray(this.#body[name])) {
} this.#body[name] = [this.#body[name]]
}) }
this.#body[name].push(file)
form.on('error', out.reject) }
})
form.on('end', err => { .on('error', out.reject)
if (~contentType.indexOf('urlencoded')) { .on('end', err => {
for (let i in para) { if (~contentType.indexOf('urlencoded')) {
if (typeof para[i] === 'string') { for (let i in this.#body) {
if (!para[i]) { if (typeof this.#body[i] === 'string') {
continue if (!this.#body[i]) {
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
} }
//获取响应头 //获取响应头
header(key = '') { header(key = '') {
key = key ? (key + '').toLowerCase() : null key = key ? (key + '').toLowerCase() : null
return !!key ? this.origin.req.headers[key] : this.origin.req.headers return !!key ? this.#req.headers[key] : this.#req.headers
} }
// 读取cookie // 读取cookie
cookie(key) { cookie(key) {
if (key) { if (key) {
return this.__COOKIE__[key] return this.#cookies[key]
} }
return this.__COOKIE__ return this.#cookies
}
get query() {
if (!this.#query) {
let para = parse(this.#req.url).query
this.#query = {}
para = Object.assign(this.#query, QS.parse(para))
}
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
ip() { get ip() {
return ( return (
this.header('x-real-ip') || this.headers['x-real-ip'] ||
this.header('x-forwarded-for') || this.headers['x-forwarded-for'] ||
this.origin.req.connection.remoteAddress.replace('::ffff:', '') this.#req.connection.remoteAddress.replace('::ffff:', '')
) )
} }
} }

View File

@ -1,9 +1,7 @@
import { WriteStream } from 'node:fs' import { WriteStream } from 'node:fs'
import { EventEmitter } from 'node:events' import { EventEmitter } from 'node:events'
export default class File extends EventEmitter { export default class File extends EventEmitter {
#stream = null #stream = null
size = 0 size = 0
@ -12,7 +10,7 @@ export default class File extends EventEmitter {
type = null type = null
lastModifiedDate = null lastModifiedDate = null
constructor(props = {}){ constructor(props = {}) {
super() super()
for (var key in props) { for (var key in props) {
@ -23,7 +21,7 @@ export default class File extends EventEmitter {
open() { open() {
this.#stream = new WriteStream(this.path) this.#stream = new WriteStream(this.path)
} }
toJSON() { toJSON() {
return { return {
size: this.size, size: this.size,
@ -36,26 +34,20 @@ export default class File extends EventEmitter {
mime: this.mime mime: this.mime
} }
} }
write(buffer, cb) {
this.#stream.write(buffer, _ =>{ write(buffer, cb) {
this.#stream.write(buffer, _ => {
this.lastModifiedDate = new Date() this.lastModifiedDate = new Date()
this.size += buffer.length this.size += buffer.length
this.emit('progress', this.size) this.emit('progress', this.size)
cb() cb()
}) })
} }
end(cb) { end(cb) {
this.#stream.end(() => { this.#stream.end(() => {
this.emit('end') this.emit('end')
cb() cb()
}) })
} }
} }

View File

@ -1,21 +1,19 @@
import crypto from 'node:crypto' import crypto from 'node:crypto'
import fs from 'node:fs' import fs from 'node:fs'
import util from 'node:util'
import path from 'node:path' import path from 'node:path'
import File from './file.js'
import { EventEmitter } from 'node:events' import { EventEmitter } from 'node:events'
import { Stream } from 'node:stream' import { Stream } from 'node:stream'
import { StringDecoder } from 'node:string_decoder' import { StringDecoder } from 'node:string_decoder'
import File from './file.js'
import { MultipartParser } from './multipart_parser.js' import { MultipartParser } from './multipart_parser.js'
import { QuerystringParser } from './querystring_parser.js' import { QuerystringParser } from './querystring_parser.js'
import { OctetParser } from './octet_parser.js' import { OctetParser } from './octet_parser.js'
import { JSONParser } from './json_parser.js' import { JSONParser } from './json_parser.js'
function dummyParser(self) { function dummyParser(self) {
return { return {
end: function() { end: function () {
self.ended = true self.ended = true
self._maybeEnd() self._maybeEnd()
return null return null
@ -23,132 +21,66 @@ function dummyParser(self) {
} }
} }
export default class IncomingForm{ export default class IncomingForm extends EventEmitter {
#req = null
constructor(opts = {}) { error = null
ended = false
headers = null
type = null
bytesReceived = null
bytesExpected = null
_parser = null
_flushing = 0
_fieldsSize = 0
openedFiles = []
constructor(req, opts = {}) {
super()
this.#req = req
this.error = null
this.ended = false
this.maxFields = opts.maxFields || 1000 this.maxFields = opts.maxFields || 1000
this.maxFieldsSize = opts.maxFieldsSize || 2 * 1024 * 1024 this.maxFieldsSize = opts.maxFieldsSize || 2 * 1024 * 1024
this.keepExtensions = opts.keepExtensions || false 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.multiples = opts.multiples || false
this.bytesReceived = null
this.bytesExpected = null
this._parser = null
this._flushing = 0
this._fieldsSize = 0
this.openedFiles = []
}
parse(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. // Parse headers and setup the parser, ready to start listening for data.
this.writeHeaders(req.headers) this.writeHeaders(req.headers)
// Start listening for data.
var self = this
req req
.on('error', function(err) { .on('error', err => {
self._error(err) this._error(err)
}) })
.on('aborted', function() { .on('aborted', () => {
self.emit('aborted') this.emit('aborted')
self._error(new Error('Request aborted')) this._error(new Error('Request aborted'))
}) })
.on('data', function(buffer) { .on('data', buffer => {
self.write(buffer) this.write(buffer)
}) })
.on('end', function() { .on('end', () => {
if (self.error) { if (this.error) {
return return
} }
var err = self._parser.end() var err = this._parser.end()
if (err) { if (err) {
self._error(err) this._error(err)
} }
}) })
return this
} }
writeHeaders(headers) { writeHeaders(headers) {
this.headers = headers this.headers = headers
this._parseContentLength() this.#parseContentLength()
this._parseContentType() this.#parseContentType()
} }
write(buffer) { write(buffer) {
if (this.error) { if (this.error) {
return return
@ -157,10 +89,10 @@ export default class IncomingForm{
this._error(new Error('uninitialized parser')) this._error(new Error('uninitialized parser'))
return return
} }
this.bytesReceived += buffer.length this.bytesReceived += buffer.length
this.emit('progress', this.bytesReceived, this.bytesExpected) this.emit('progress', this.bytesReceived, this.bytesExpected)
var bytesParsed = this._parser.write(buffer) var bytesParsed = this._parser.write(buffer)
if (bytesParsed !== buffer.length) { if (bytesParsed !== buffer.length) {
this._error( this._error(
@ -173,33 +105,52 @@ export default class IncomingForm{
) )
) )
} }
return bytesParsed return bytesParsed
} }
pause() { pause() {
// this does nothing, unless overwritten in IncomingForm.parse try {
return false this.#req.pause()
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err)
}
return false
}
return true
} }
resume() { resume() {
// this does nothing, unless overwritten in IncomingForm.parse try {
return false this.#req.resume()
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err)
}
return false
}
return true
} }
onPart(part) { onPart(part) {
// this method can be overwritten by the user // this method can be overwritten by the user
this.handlePart(part) this.handlePart(part)
} }
handlePart(part) { handlePart(part) {
var self = this var self = this
if (part.filename === undefined) { if (part.filename === undefined) {
var value = '', var value = '',
decoder = new StringDecoder(this.encoding) decoder = new StringDecoder(this.encoding)
part.on('data', function(buffer) { part.on('data', function (buffer) {
self._fieldsSize += buffer.length self._fieldsSize += buffer.length
if (self._fieldsSize > self.maxFieldsSize) { if (self._fieldsSize > self.maxFieldsSize) {
self._error( self._error(
@ -213,68 +164,67 @@ export default class IncomingForm{
} }
value += decoder.write(buffer) value += decoder.write(buffer)
}) })
part.on('end', function() { part.on('end', function () {
self.emit('field', part.name, value) self.emit('field', part.name, value)
}) })
return return
} }
this._flushing++ this._flushing++
var file = new File({ var file = new File({
path: this._uploadPath(part.filename), path: this._uploadPath(part.filename),
name: part.filename, name: part.filename,
type: part.mime, type: part.mime,
hash: self.hash hash: self.hash
}) })
this.emit('fileBegin', part.name, file) this.emit('fileBegin', part.name, file)
file.open() file.open()
this.openedFiles.push(file) this.openedFiles.push(file)
part.on('data', function(buffer) { part.on('data', function (buffer) {
if (buffer.length == 0) { if (buffer.length == 0) {
return return
} }
self.pause() self.pause()
file.write(buffer, function() { file.write(buffer, function () {
self.resume() self.resume()
}) })
}) })
part.on('end', function() { part.on('end', function () {
file.end(function() { file.end(function () {
self._flushing-- self._flushing--
self.emit('file', part.name, file) self.emit('file', part.name, file)
self._maybeEnd() self._maybeEnd()
}) })
}) })
} }
#parseContentType() {
_parseContentType() {
if (this.bytesExpected === 0) { if (this.bytesExpected === 0) {
this._parser = dummyParser(this) this._parser = dummyParser(this)
return return
} }
if (!this.headers['content-type']) { if (!this.headers['content-type']) {
this._error(new Error('bad content-type header, no content-type')) this._error(new Error('bad content-type header, no content-type'))
return return
} }
if (this.headers['content-type'].match(/octet-stream/i)) { if (this.headers['content-type'].match(/octet-stream/i)) {
this._initOctetStream() this._initOctetStream()
return return
} }
if (this.headers['content-type'].match(/urlencoded/i)) { if (this.headers['content-type'].match(/urlencoded/i)) {
this._initUrlencoded() this._initUrlencoded()
return return
} }
if (this.headers['content-type'].match(/multipart/i)) { if (this.headers['content-type'].match(/multipart/i)) {
var m = this.headers['content-type'].match( var m = this.headers['content-type'].match(
/boundary=(?:"([^"]+)"|([^;]+))/i /boundary=(?:"([^"]+)"|([^;]+))/i
@ -286,12 +236,12 @@ export default class IncomingForm{
} }
return return
} }
if (this.headers['content-type'].match(/json|appliation|plain|text/i)) { if (this.headers['content-type'].match(/json|appliation|plain|text/i)) {
this._initJSONencoded() this._initJSONencoded()
return return
} }
this._error( this._error(
new Error( new Error(
'bad content-type header, unknown content-type: ' + 'bad content-type header, unknown content-type: ' +
@ -299,113 +249,113 @@ export default class IncomingForm{
) )
) )
} }
_error(err) { _error(err) {
if (this.error || this.ended) { if (this.error || this.ended) {
return return
} }
this.error = err this.error = err
this.emit('error', err) this.emit('error', err)
if (Array.isArray(this.openedFiles)) { if (Array.isArray(this.openedFiles)) {
this.openedFiles.forEach(function(file) { this.openedFiles.forEach(function (file) {
file._writeStream.destroy() file._writeStream.destroy()
setTimeout(fs.unlink, 0, file.path, function(error) {}) setTimeout(fs.unlink, 0, file.path, function (error) {})
}) })
} }
} }
_parseContentLength() { #parseContentLength() {
this.bytesReceived = 0 this.bytesReceived = 0
if (this.headers['content-length']) { if (this.headers['content-length']) {
this.bytesExpected = parseInt(this.headers['content-length'], 10) this.bytesExpected = +this.headers['content-length']
} else if (this.headers['transfer-encoding'] === undefined) { } else if (this.headers['transfer-encoding'] === undefined) {
this.bytesExpected = 0 this.bytesExpected = 0
} }
if (this.bytesExpected !== null) { if (this.bytesExpected !== null) {
this.emit('progress', this.bytesReceived, this.bytesExpected) this.emit('progress', this.bytesReceived, this.bytesExpected)
} }
} }
_newParser() { _newParser() {
return new MultipartParser() return new MultipartParser()
} }
_initMultipart(boundary) { _initMultipart(boundary) {
this.type = 'multipart' this.type = 'multipart'
var parser = new MultipartParser(), var parser = new MultipartParser(),
self = this, self = this,
headerField, headerField,
headerValue, headerValue,
part part
parser.initWithBoundary(boundary) parser.initWithBoundary(boundary)
parser.onPartBegin = function() { parser.onPartBegin = function () {
part = new Stream() part = new Stream()
part.readable = true part.readable = true
part.headers = {} part.headers = {}
part.name = null part.name = null
part.filename = null part.filename = null
part.mime = null part.mime = null
part.transferEncoding = 'binary' part.transferEncoding = 'binary'
part.transferBuffer = '' part.transferBuffer = ''
headerField = '' headerField = ''
headerValue = '' headerValue = ''
} }
parser.onHeaderField = function(b, start, end) { parser.onHeaderField = function (b, start, end) {
headerField += b.toString(self.encoding, start, end) headerField += b.toString(self.encoding, start, end)
} }
parser.onHeaderValue = function(b, start, end) { parser.onHeaderValue = function (b, start, end) {
headerValue += b.toString(self.encoding, start, end) headerValue += b.toString(self.encoding, start, end)
} }
parser.onHeaderEnd = function() { parser.onHeaderEnd = function () {
headerField = headerField.toLowerCase() headerField = headerField.toLowerCase()
part.headers[headerField] = headerValue part.headers[headerField] = headerValue
var m = headerValue.match(/\bname="([^"]+)"/i) var m = headerValue.match(/\bname="([^"]+)"/i)
if (headerField == 'content-disposition') { if (headerField == 'content-disposition') {
if (m) { if (m) {
part.name = m[1] part.name = m[1]
} }
part.filename = self._fileName(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') {
part.transferEncoding = headerValue.toLowerCase() part.transferEncoding = headerValue.toLowerCase()
} }
headerField = '' headerField = ''
headerValue = '' headerValue = ''
} }
parser.onHeadersEnd = function() { parser.onHeadersEnd = function () {
switch (part.transferEncoding) { switch (part.transferEncoding) {
case 'binary': case 'binary':
case '7bit': case '7bit':
case '8bit': case '8bit':
parser.onPartData = function(b, start, end) { parser.onPartData = function (b, start, end) {
part.emit('data', b.slice(start, end)) part.emit('data', b.slice(start, end))
} }
parser.onPartEnd = function() { parser.onPartEnd = function () {
part.emit('end') part.emit('end')
} }
break break
case 'base64': case 'base64':
parser.onPartData = function(b, start, end) { parser.onPartData = function (b, start, end) {
part.transferBuffer += b.slice(start, end).toString('ascii') part.transferBuffer += b.slice(start, end).toString('ascii')
/* /*
four bytes (chars) in base64 converts to three bytes in binary four bytes (chars) in base64 converts to three bytes in binary
encoding. So we should always work with a number of bytes that encoding. So we should always work with a number of bytes that
@ -419,105 +369,105 @@ export default class IncomingForm{
) )
part.transferBuffer = part.transferBuffer.substring(offset) part.transferBuffer = part.transferBuffer.substring(offset)
} }
parser.onPartEnd = 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 self._error(new Error('unknown transfer-encoding')) return self._error(new Error('unknown transfer-encoding'))
} }
self.onPart(part) self.onPart(part)
} }
parser.onEnd = function() { parser.onEnd = function () {
self.ended = true self.ended = true
self._maybeEnd() self._maybeEnd()
} }
this._parser = parser this._parser = parser
} }
_fileName(headerValue) { _fileName(headerValue) {
var m = headerValue.match(/\bfilename="(.*?)"($|; )/i) var m = headerValue.match(/\bfilename="(.*?)"($|; )/i)
if (!m) return if (!m) return
var filename = m[1].substr(m[1].lastIndexOf('\\') + 1) var filename = m[1].substr(m[1].lastIndexOf('\\') + 1)
filename = filename.replace(/%22/g, '"') filename = filename.replace(/%22/g, '"')
filename = filename.replace(/&#([\d]{4});/g, function(m, code) { filename = filename.replace(/&#([\d]{4});/g, function (m, code) {
return String.fromCharCode(code) return String.fromCharCode(code)
}) })
return filename return filename
} }
_initUrlencoded() { _initUrlencoded() {
this.type = 'urlencoded' this.type = 'urlencoded'
var parser = new QuerystringParser(this.maxFields) var parser = new QuerystringParser(this.maxFields)
parser.onField = (key, val) => { parser.onField = (key, val) => {
this.emit('field', key, val) this.emit('field', key, val)
} }
parser.onEnd = () => { parser.onEnd = () => {
this.ended = true this.ended = true
this._maybeEnd() this._maybeEnd()
} }
this._parser = parser this._parser = parser
} }
_initOctetStream() { _initOctetStream() {
this.type = 'octet-stream' this.type = 'octet-stream'
var filename = this.headers['x-file-name'] var filename = this.headers['x-file-name']
var mime = this.headers['content-type'] var mime = this.headers['content-type']
var file = new File({ var file = new File({
path: this._uploadPath(filename), path: this._uploadPath(filename),
name: filename, name: filename,
type: mime type: mime
}) })
this.emit('fileBegin', filename, file) this.emit('fileBegin', filename, file)
file.open() file.open()
this._flushing++ this._flushing++
var self = this var self = this
self._parser = new OctetParser() self._parser = new OctetParser()
//Keep track of writes that haven't finished so we don't emit the file before it's done being written //Keep track of writes that haven't finished so we don't emit the file before it's done being written
var outstandingWrites = 0 var outstandingWrites = 0
self._parser.on('data', function(buffer) { self._parser.on('data', function (buffer) {
self.pause() self.pause()
outstandingWrites++ outstandingWrites++
file.write(buffer, function() { file.write(buffer, function () {
outstandingWrites-- outstandingWrites--
self.resume() self.resume()
if (self.ended) { if (self.ended) {
self._parser.emit('doneWritingFile') self._parser.emit('doneWritingFile')
} }
}) })
}) })
self._parser.on('end', function() { self._parser.on('end', function () {
self._flushing-- self._flushing--
self.ended = true self.ended = true
var done = function() { var done = function () {
file.end(function() { file.end(function () {
self.emit('file', 'file', file) self.emit('file', 'file', file)
self._maybeEnd() self._maybeEnd()
}) })
} }
if (outstandingWrites === 0) { if (outstandingWrites === 0) {
done() done()
} else { } else {
@ -525,53 +475,51 @@ export default class IncomingForm{
} }
}) })
} }
_initJSONencoded() { _initJSONencoded() {
this.type = 'json' this.type = 'json'
var parser = new JSONParser(), var parser = new JSONParser(),
self = this self = this
if (this.bytesExpected) { if (this.bytesExpected) {
parser.initWithLength(this.bytesExpected) parser.initWithLength(this.bytesExpected)
} }
parser.onField = function(key, val) { parser.onField = function (key, val) {
self.emit('field', key, val) self.emit('field', key, val)
} }
parser.onEnd = function() { parser.onEnd = function () {
self.ended = true self.ended = true
self._maybeEnd() self._maybeEnd()
} }
this._parser = parser this._parser = parser
} }
_uploadPath(filename) { _uploadPath(filename) {
var name = 'upload_' var name = 'upload_'
var buf = crypto.randomBytes(16) var buf = crypto.randomBytes(16)
for (var i = 0; i < buf.length; ++i) { for (var i = 0; i < buf.length; ++i) {
name += ('0' + buf[i].toString(16)).slice(-2) name += ('0' + buf[i].toString(16)).slice(-2)
} }
if (this.keepExtensions) { if (this.keepExtensions) {
var ext = path.extname(filename) var ext = path.extname(filename)
ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1') ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1')
name += ext name += ext
} }
return path.join(this.uploadDir, name) return path.join(this.uploadDir, name)
} }
_maybeEnd() { _maybeEnd() {
if (!this.ended || this._flushing || this.error) { if (!this.ended || this._flushing || this.error) {
return return
} }
this.emit('end') this.emit('end')
} }
} }

View File

@ -1,12 +1,11 @@
export class JSONParser { export class JSONParser {
data = Buffer.from('') data = Buffer.from('')
bytesWritten = 0 bytesWritten = 0
initWithLength(length) { initWithLength(length) {
this.data = Buffer.alloc(length) this.data = Buffer.alloc(length)
} }
write(buffer) { write(buffer) {
if (this.data.length >= this.bytesWritten + buffer.length) { if (this.data.length >= this.bytesWritten + buffer.length) {
buffer.copy(this.data, this.bytesWritten) buffer.copy(this.data, this.bytesWritten)
@ -16,7 +15,7 @@ export class JSONParser {
this.bytesWritten += buffer.length this.bytesWritten += buffer.length
return buffer.length return buffer.length
} }
end() { end() {
var data = this.data.toString('utf8') var data = this.data.toString('utf8')
var fields var fields
@ -25,12 +24,10 @@ export class JSONParser {
} catch (e) { } catch (e) {
fields = Function(`try{return ${data}}catch(e){}`)() || data fields = Function(`try{return ${data}}catch(e){}`)() || data
} }
this.onField(false, fields) this.onField(false, fields)
this.data = null this.data = null
this.onEnd() this.onEnd()
} }
} }

View File

@ -26,7 +26,7 @@ var s = 0,
COLON = 58, COLON = 58,
A = 97, A = 97,
Z = 122, Z = 122,
lower = function(c) { lower = function (c) {
return c | 0x20 return c | 0x20
} }
@ -39,28 +39,26 @@ export class MultipartParser {
index = null index = null
flags = 0 flags = 0
static stateToString(stateNumber) {
static stateToString(stateNumber) {
for (var state in S) { for (var state in S) {
var number = S[state] var number = S[state]
if (number === stateNumber) return state if (number === stateNumber) return state
} }
} }
initWithBoundary(str) { initWithBoundary(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 = S.START this.state = S.START
this.boundaryChars = {} this.boundaryChars = {}
for (var 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
} }
} }
write(buffer) { write(buffer) {
var self = this, var self = this,
i = 0, i = 0,
@ -77,29 +75,29 @@ export class MultipartParser {
bufferLength = buffer.length, bufferLength = buffer.length,
c, c,
cl, cl,
mark = function(name) { mark = function (name) {
self[name + 'Mark'] = i self[name + 'Mark'] = i
}, },
clear = function(name) { clear = function (name) {
delete self[name + 'Mark'] delete self[name + 'Mark']
}, },
callback = function(name, buffer, start, end) { callback = function (name, buffer, start, end) {
if (start !== undefined && start === end) { if (start !== undefined && start === end) {
return return
} }
var callbackSymbol = var callbackSymbol =
'on' + name.substr(0, 1).toUpperCase() + name.substr(1) 'on' + name.substr(0, 1).toUpperCase() + name.substr(1)
if (callbackSymbol in self) { if (callbackSymbol in self) {
self[callbackSymbol](buffer, start, end) self[callbackSymbol](buffer, start, end)
} }
}, },
dataCallback = function(name, clear) { dataCallback = function (name, clear) {
var markSymbol = name + 'Mark' var markSymbol = name + 'Mark'
if (!(markSymbol in self)) { if (!(markSymbol in self)) {
return return
} }
if (!clear) { if (!clear) {
callback(name, buffer, self[markSymbol], buffer.length) callback(name, buffer, self[markSymbol], buffer.length)
self[markSymbol] = 0 self[markSymbol] = 0
@ -108,7 +106,7 @@ export class MultipartParser {
delete self[markSymbol] delete self[markSymbol]
} }
} }
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
c = buffer[i] c = buffer[i]
switch (state) { switch (state) {
@ -140,7 +138,7 @@ export class MultipartParser {
} }
break break
} }
if (c != boundary[index + 2]) { if (c != boundary[index + 2]) {
index = -2 index = -2
} }
@ -158,12 +156,12 @@ export class MultipartParser {
state = S.HEADERS_ALMOST_DONE state = S.HEADERS_ALMOST_DONE
break break
} }
index++ index++
if (c == HYPHEN) { if (c == HYPHEN) {
break break
} }
if (c == COLON) { if (c == COLON) {
if (index == 1) { if (index == 1) {
// empty header field // empty header field
@ -173,7 +171,7 @@ export class MultipartParser {
state = S.HEADER_VALUE_START state = S.HEADER_VALUE_START
break break
} }
cl = lower(c) cl = lower(c)
if (cl < A || cl > Z) { if (cl < A || cl > Z) {
return i return i
@ -183,7 +181,7 @@ export class MultipartParser {
if (c == SPACE) { if (c == SPACE) {
break break
} }
mark('headerValue') mark('headerValue')
state = S.HEADER_VALUE state = S.HEADER_VALUE
case S.HEADER_VALUE: case S.HEADER_VALUE:
@ -203,7 +201,7 @@ export class MultipartParser {
if (c != LF) { if (c != LF) {
return i return i
} }
callback('headersEnd') callback('headersEnd')
state = S.PART_DATA_START state = S.PART_DATA_START
break break
@ -212,7 +210,7 @@ export class MultipartParser {
mark('partData') mark('partData')
case S.PART_DATA: case S.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
i += boundaryEnd i += boundaryEnd
@ -222,7 +220,7 @@ export class MultipartParser {
i -= boundaryEnd i -= boundaryEnd
c = buffer[i] 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) {
@ -267,7 +265,7 @@ export class MultipartParser {
index = 0 index = 0
} }
} }
if (index > 0) { if (index > 0) {
// when matching a possible boundary, keep a lookbehind reference // when matching a possible boundary, keep a lookbehind reference
// in case it turns out to be a false lead // in case it turns out to be a false lead
@ -278,12 +276,12 @@ export class MultipartParser {
callback('partData', lookbehind, 0, prevIndex) callback('partData', lookbehind, 0, prevIndex)
prevIndex = 0 prevIndex = 0
mark('partData') 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
i-- i--
} }
break break
case S.END: case S.END:
break break
@ -291,21 +289,22 @@ export class MultipartParser {
return i return i
} }
} }
dataCallback('headerField') dataCallback('headerField')
dataCallback('headerValue') dataCallback('headerValue')
dataCallback('partData') dataCallback('partData')
this.index = index this.index = index
this.state = state this.state = state
this.flags = flags this.flags = flags
return len return len
} }
end() { end() {
var callback = function(self, name) { var callback = function (self, name) {
var callbackSymbol = 'on' + name.substr(0, 1).toUpperCase() + name.substr(1) var callbackSymbol =
'on' + name.substr(0, 1).toUpperCase() + name.substr(1)
if (callbackSymbol in self) { if (callbackSymbol in self) {
self[callbackSymbol]() self[callbackSymbol]()
} }
@ -322,10 +321,8 @@ export class MultipartParser {
) )
} }
} }
explain() { explain() {
return 'state = ' + MultipartParser.stateToString(this.state) return 'state = ' + MultipartParser.stateToString(this.state)
} }
} }

View File

@ -1,15 +1,12 @@
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
export class OctetParser extends EventEmitter { export class OctetParser extends EventEmitter {
write(buffer) { write(buffer) {
this.emit('data', buffer) this.emit('data', buffer)
return buffer.length return buffer.length
} }
end () { end() {
this.emit('end') this.emit('end')
}
} }
}

View File

@ -1,6 +1,6 @@
// This is a buffering parser, not quite as nice as the multipart one. // 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 // If I find time I'll rewrite this to be fully streaming as well
import {parse} from 'node:querystring' import { parse } from 'node:querystring'
export class QuerystringParser { export class QuerystringParser {
constructor(maxKeys) { constructor(maxKeys) {

View File

@ -1,13 +1,14 @@
{ {
"name": "@gm5/request", "name": "@gm5/request",
"version": "1.2.8", "version": "2.0.0",
"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",
"node-five", "gmf",
"gm5",
"five.js", "five.js",
"fivejs", "fivejs",
"request", "request",