调整API, 修改已弃用API的使用

master
yutent 2025-01-07 14:45:22 +08:00
parent f433720268
commit 642738790a
2 changed files with 31 additions and 26 deletions

View File

@ -5,7 +5,7 @@
import 'es.shim'
import { fileURLToPath, parse } from 'node:url'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import fs from 'iofs'
@ -27,13 +27,8 @@ if (fs.isdir(tmpdir)) {
fs.mkdir(tmpdir)
function hideProperty(host, name, value) {
Object.defineProperty(host, name, {
value: value,
writable: true,
enumerable: false,
configurable: true
})
function urlParse(url) {
return new URL(url, 'http://127.0.0.1')
}
export default class Request {
@ -46,11 +41,12 @@ export default class Request {
#body = null
#cookies = Object.create(null)
controller = 'index'
method = 'GET'
path = []
url = ''
controller = 'index'
actions = []
pathname = ''
host = '127.0.0.1'
hostname = '127.0.0.1'
protocol = 'http'
@ -74,11 +70,11 @@ export default class Request {
// 修正请求的url
#init() {
let url = parse(this.#req.url)
let url = urlParse(this.#req.url)
.pathname.slice(1)
.replace(/[\/]+$/, '')
let controller = '' // 将作为主控制器(即apps目录下的应用)
let path = []
let actions = []
// URL上不允许有非法字符
if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) {
@ -92,26 +88,26 @@ export default class Request {
// 修正url中可能出现的"多斜杠"
url = url.replace(/[\/]+/g, '/').replace(/^\//, '')
path = url.split('/')
if (!path[0] || path[0] === '') {
path[0] = 'index'
actions = url.split('/')
if (!actions[0] || actions[0] === '') {
actions[0] = 'index'
}
if (path[0].includes('.')) {
controller = path[0].slice(0, path[0].indexOf('.'))
if (actions[0].includes('.')) {
controller = actions[0].slice(0, actions[0].indexOf('.'))
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
if (!controller || controller === '') {
controller = 'index'
}
} else {
controller = path[0]
controller = actions[0]
}
path.shift()
actions.shift()
this.controller = controller
this.url = url
this.path = path
this.pathname = url
this.actions = actions
}
/**
@ -225,8 +221,8 @@ export default class Request {
get query() {
if (!this.#query) {
let data = parse(this.#req.url).query
this.#query = querystring(data)
let { search } = urlParse(this.#req.url)
this.#query = querystring(search.slice(1))
}
return this.#query
}

View File

@ -7,13 +7,22 @@
import { parse } from 'node:querystring'
export function querystring(str) {
let query = parse(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)
if (query.hasOwnProperty(_k)) {
let old = query[_k]
if (!Array.isArray(old)) {
old = [old]
}
query[_k] = old.concat(val)
} else {
query[_k] = val
}
delete query[k]
} else if (k.endsWith(']')) {
let idx = k.lastIndexOf('[')