parent
8afca9afaa
commit
a395c87101
39
index.js
39
index.js
|
@ -5,12 +5,13 @@
|
||||||
|
|
||||||
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 fs from 'iofs'
|
import { querystring } from './lib/helper.js'
|
||||||
import { fileURLToPath, parse } from 'node:url'
|
|
||||||
import QS from 'node:querystring'
|
|
||||||
import { dirname, resolve } from 'node:path'
|
|
||||||
|
|
||||||
const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded'
|
const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded'
|
||||||
|
|
||||||
|
@ -96,7 +97,7 @@ export default class Request {
|
||||||
path[0] = 'index'
|
path[0] = 'index'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path[0].indexOf('.') !== -1) {
|
if (path[0].includes('.')) {
|
||||||
controller = path[0].slice(0, path[0].indexOf('.'))
|
controller = path[0].slice(0, path[0].indexOf('.'))
|
||||||
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
|
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
|
||||||
if (!controller || controller === '') {
|
if (!controller || controller === '') {
|
||||||
|
@ -132,32 +133,22 @@ export default class Request {
|
||||||
this.#body = value
|
this.#body = value
|
||||||
return
|
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
|
if (name.endsWith('[]')) {
|
||||||
|
|
||||||
return Object.assign(this.#body, JSON.parse(name + 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 key = name.slice(name.lastIndexOf('[') + 1, -1)
|
let idx = name.lastIndexOf('[')
|
||||||
name = name.slice(0, name.lastIndexOf('['))
|
let key = name.slice(idx + 1, -1)
|
||||||
|
name = name.slice(0, idx)
|
||||||
|
|
||||||
//多解析一层对象(也仅支持到这一层)
|
//多解析一层对象(也仅支持到这一层)
|
||||||
if (name.slice(-1) === ']') {
|
if (name.slice(-1) === ']') {
|
||||||
let pkey = name.slice(name.lastIndexOf('[') + 1, -1)
|
idx = name.lastIndexOf('[')
|
||||||
name = name.slice(0, name.lastIndexOf('['))
|
let pkey = name.slice(idx + 1, -1)
|
||||||
|
name = name.slice(0, idx)
|
||||||
|
|
||||||
if (!this.#body.hasOwnProperty(name)) {
|
if (!this.#body.hasOwnProperty(name)) {
|
||||||
this.#body[name] = {}
|
this.#body[name] = {}
|
||||||
|
@ -234,8 +225,8 @@ export default class Request {
|
||||||
|
|
||||||
get query() {
|
get query() {
|
||||||
if (!this.#query) {
|
if (!this.#query) {
|
||||||
let para = parse(this.#req.url).query
|
let data = parse(this.#req.url).query
|
||||||
this.#query = QS.parse(para)
|
this.#query = querystring(data)
|
||||||
}
|
}
|
||||||
return this.#query
|
return this.#query
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
/**
|
||||||
|
* {}
|
||||||
|
* @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
|
||||||
|
}
|
|
@ -4,23 +4,40 @@
|
||||||
* @date 2023/10/27 12:14:05
|
* @date 2023/10/27 12:14:05
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { parse } from 'node:querystring'
|
|
||||||
import { EventEmitter } from 'node:events'
|
import { EventEmitter } from 'node:events'
|
||||||
|
import { querystring } from './helper.js'
|
||||||
|
|
||||||
export class UrlencodedParser extends EventEmitter {
|
export class UrlencodedParser extends EventEmitter {
|
||||||
#buf = Buffer.from('')
|
#buf = Buffer.from('')
|
||||||
|
#byteLen = 0
|
||||||
|
|
||||||
|
initLength(length) {
|
||||||
|
this.#byteLen = length
|
||||||
|
}
|
||||||
|
|
||||||
write(buffer) {
|
write(buffer) {
|
||||||
this.#buf = Buffer.concat([this.#buf, buffer])
|
this.#buf = Buffer.concat([this.#buf, buffer])
|
||||||
}
|
}
|
||||||
|
|
||||||
end() {
|
end() {
|
||||||
|
if (this.#buf.length === this.#byteLen) {
|
||||||
let data = this.#buf.toString()
|
let data = this.#buf.toString()
|
||||||
let fields = parse(data)
|
let fields = querystring(data)
|
||||||
|
|
||||||
this.#buf = null
|
this.#buf = null
|
||||||
|
|
||||||
this.emit('field', fields)
|
this.emit('field', fields)
|
||||||
this.emit('end')
|
this.emit('end')
|
||||||
|
this.#buf = null
|
||||||
|
} else {
|
||||||
|
this.emit(
|
||||||
|
'error',
|
||||||
|
new Error(
|
||||||
|
`The uploaded data is incomplete. Expected ${
|
||||||
|
this.#byteLen
|
||||||
|
}, Received ${this.#buf.length} .`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@gm5/request",
|
"name": "@gm5/request",
|
||||||
"version": "2.0.5",
|
"version": "2.0.6",
|
||||||
"description": "对Http的Request进一步封装, 提供常用的API",
|
"description": "对Http的Request进一步封装, 提供常用的API",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "yutent",
|
"author": "yutent",
|
||||||
|
|
Loading…
Reference in New Issue