一大波更新

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

220
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,88 +98,28 @@ 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
.on('field', (name, value) => {
if (name === false) { if (name === false) {
para = value this.#body = value
return return
} }
if (~contentType.indexOf('urlencoded')) { if (~contentType.indexOf('urlencoded')) {
@ -178,14 +131,10 @@ export default class Request {
if (value.slice(0, 1) === '=') value = '=' + value if (value.slice(0, 1) === '=') value = '=' + value
return Object.assign(para, JSON.parse(name + value)) return Object.assign(this.#body, JSON.parse(name + value))
} }
} }
if (typeof value === 'string') {
value = xss ? value.xss() : value
}
if (name.slice(-2) === '[]') { if (name.slice(-2) === '[]') {
name = name.slice(0, -2) name = name.slice(0, -2)
if (typeof value === 'string') { if (typeof value === 'string') {
@ -200,61 +149,54 @@ export default class Request {
let pkey = name.slice(name.lastIndexOf('[') + 1, -1) let pkey = name.slice(name.lastIndexOf('[') + 1, -1)
name = name.slice(0, name.lastIndexOf('[')) name = name.slice(0, name.lastIndexOf('['))
if (!para.hasOwnProperty(name)) { if (!this.#body.hasOwnProperty(name)) {
para[name] = {} this.#body[name] = {}
} }
if (!para[name].hasOwnProperty(pkey)) { if (!this.#body[name].hasOwnProperty(pkey)) {
para[name][pkey] = {} this.#body[name][pkey] = {}
} }
para[name][pkey][key] = value this.#body[name][pkey][key] = value
} else { } else {
if (!para.hasOwnProperty(name)) { if (!this.#body.hasOwnProperty(name)) {
para[name] = {} this.#body[name] = {}
} }
para[name][key] = value this.#body[name][key] = value
} }
return return
} }
para[name] = value this.#body[name] = value
}) })
.on('file', (name, file) => {
form.on('file', (name, file) => {
if (name.slice(-2) === '[]') { if (name.slice(-2) === '[]') {
name = name.slice(0, -2) name = name.slice(0, -2)
} }
if (!para.hasOwnProperty(name)) { if (!this.#body.hasOwnProperty(name)) {
para[name] = file this.#body[name] = file
} else { } else {
if (!Array.isArray(para[name])) { if (!Array.isArray(this.#body[name])) {
para[name] = [para[name]] this.#body[name] = [this.#body[name]]
} }
para[name].push(file) this.#body[name].push(file)
} }
}) })
.on('error', out.reject)
form.on('error', out.reject) .on('end', err => {
form.on('end', err => {
if (~contentType.indexOf('urlencoded')) { if (~contentType.indexOf('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
} }
para[i] = Number.parse(para[i]) this.#body[i] = Number.parse(this.#body[i])
} }
} }
} }
this._postParam = para
if (key) { out.resolve(this.#body)
return out.resolve(para.hasOwnProperty(key) ? para[key] : null)
} else {
return out.resolve(para)
}
}) })
return out.promise return out.promise
} }
@ -262,23 +204,47 @@ export default class Request {
//获取响应头 //获取响应头
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
@ -38,8 +36,6 @@ export default class File extends EventEmitter {
} }
write(buffer, cb) { write(buffer, cb) {
this.#stream.write(buffer, _ => { this.#stream.write(buffer, _ => {
this.lastModifiedDate = new Date() this.lastModifiedDate = new Date()
this.size += buffer.length this.size += buffer.length
@ -49,13 +45,9 @@ export default class File extends EventEmitter {
} }
end(cb) { end(cb) {
this.#stream.end(() => { this.#stream.end(() => {
this.emit('end') this.emit('end')
cb() cb()
}) })
} }
} }

View File

@ -1,18 +1,16 @@
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 () {
@ -23,130 +21,64 @@ 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
this.error = null bytesReceived = null
this.ended = false bytesExpected = null
_parser = null
_flushing = 0
_fieldsSize = 0
openedFiles = []
constructor(req, opts = {}) {
super()
this.#req = req
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) {
@ -178,15 +110,34 @@ export default class IncomingForm{
} }
pause() { pause() {
// this does nothing, unless overwritten in IncomingForm.parse try {
this.#req.pause()
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err)
}
return false return false
} }
return true
}
resume() { resume() {
// this does nothing, unless overwritten in IncomingForm.parse try {
this.#req.resume()
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err)
}
return false 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)
@ -253,8 +204,7 @@ export default class IncomingForm{
}) })
} }
#parseContentType() {
_parseContentType() {
if (this.bytesExpected === 0) { if (this.bytesExpected === 0) {
this._parser = dummyParser(this) this._parser = dummyParser(this)
return return
@ -316,10 +266,10 @@ export default class IncomingForm{
} }
} }
_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
} }
@ -572,6 +522,4 @@ export default class IncomingForm{
this.emit('end') this.emit('end')
} }
} }

View File

@ -1,5 +1,4 @@
export class JSONParser { export class JSONParser {
data = Buffer.from('') data = Buffer.from('')
bytesWritten = 0 bytesWritten = 0
@ -31,6 +30,4 @@ export class JSONParser {
this.onEnd() this.onEnd()
} }
} }

View File

@ -39,7 +39,6 @@ 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]
@ -47,7 +46,6 @@ export class MultipartParser {
} }
} }
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)
@ -305,7 +303,8 @@ export class MultipartParser {
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]()
} }
@ -327,5 +326,3 @@ export class MultipartParser {
return 'state = ' + MultipartParser.stateToString(this.state) return 'state = ' + MultipartParser.stateToString(this.state)
} }
} }

View File

@ -1,6 +1,5 @@
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)
@ -11,5 +10,3 @@ end () {
this.emit('end') this.emit('end')
} }
} }

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",