@halsp/core
Version:
面向云的现代渐进式轻量 Node.js 框架
148 lines • 3.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = exports.Response = exports.Request = void 0;
const utils_1 = require("./utils/index.cjs");
class Request {
ctx;
#body;
get body() {
return this.#body;
}
setBody(body) {
this.#body = body;
return this;
}
#path = "";
get path() {
return this.#path;
}
#originalPath;
get originalPath() {
return this.#originalPath;
}
setPath(path) {
this.#originalPath = path
?.replace(/\?.*$/, "")
?.replace(/^https?:\/{1,2}[^\/]+/, "");
this.#path = (0, utils_1.normalizePath)(this.#originalPath);
return this;
}
}
exports.Request = Request;
class Response {
ctx;
body;
setBody(body) {
this.body = body;
return this;
}
}
exports.Response = Response;
const singletonBagMap = new WeakMap();
class Context {
constructor(req) {
this.#req = req ?? new Request();
this.#res = new Response();
Object.defineProperty(this.#req, "ctx", {
configurable: true,
get: () => this,
});
Object.defineProperty(this.#res, "ctx", {
configurable: true,
get: () => this,
});
}
#req;
get req() {
return this.#req;
}
get request() {
return this.#req;
}
#res;
get res() {
return this.#res;
}
get response() {
return this.#res;
}
get logger() {
return this.startup.logger;
}
set logger(val) {
this.startup.logger = val;
}
startup;
get #singletonBag() {
if (!singletonBagMap.has(this.startup)) {
singletonBagMap.set(this.startup, {});
}
return singletonBagMap.get(this.startup);
}
#scopedBag = {};
#bag = {};
get(key) {
if (key in this.#singletonBag) {
return this.#getBagValue(key, this.#singletonBag[key]);
}
else if (key in this.#scopedBag) {
return this.#getBagValue(key, this.#scopedBag[key]);
}
else {
const result = this.#bag[key];
return this.#getBagValue(key, result);
}
}
set(key, arg1, arg2) {
if (!(0, utils_1.isUndefined)(arg2)) {
this.#bag[key] = {
type: arg1,
builder: arg2,
isBuilderBag: true,
};
return this;
}
else {
this.#bag[key] = arg1;
return this;
}
}
has(key) {
return key in this.#bag;
}
delete(key) {
const hasKey = this.has(key);
delete this.#bag[key];
delete this.#singletonBag[key];
delete this.#scopedBag[key];
return hasKey;
}
get length() {
return Object.keys(this.#bag).length;
}
#getBagValue(key, result) {
if ((0, utils_1.isPlainObject)(result) && result.isBuilderBag) {
if (result.type == "transient") {
return result.builder();
}
else {
let dict;
if (result.type == "singleton") {
dict = this.#singletonBag;
}
else {
dict = this.#scopedBag;
}
if (!(key in dict)) {
dict[key] = result.builder();
}
return dict[key];
}
}
else {
return result;
}
}
}
exports.Context = Context;
//# sourceMappingURL=context.js.map