simple-boot-http-server
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -0,0 +0,0 @@ export declare enum HttpHeaders { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare enum HttpMethod { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare enum HttpStatus { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare enum Mimes { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import 'reflect-metadata'; |
@@ -0,0 +0,0 @@ "use strict"; |
import { RequestResponse } from '../models/RequestResponse'; | ||
import { SimpleBootHttpServer } from '../SimpleBootHttpServer'; | ||
export interface EndPoint { | ||
endPoint(rr: RequestResponse, app: SimpleBootHttpServer): void; | ||
endPoint(rr: RequestResponse, app: SimpleBootHttpServer): Promise<any>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,0 +0,0 @@ import { HttpError } from './HttpError'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { HttpError } from './HttpError'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare class HttpError { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { HttpError } from './HttpError'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { HttpError } from './HttpError'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { HttpError } from './HttpError'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { SimpleBootHttpServer } from '../SimpleBootHttpServer'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,0 +0,0 @@ import { Filter } from '../filters/Filter'; |
@@ -47,4 +47,4 @@ "use strict"; | ||
return __generator(this, function (_a) { | ||
rr.res.statusCode = this.httpStatus; | ||
rr.res.end(); | ||
rr.resStatusCode(this.httpStatus); | ||
rr.resEnd(); | ||
return [2, false]; | ||
@@ -51,0 +51,0 @@ }); |
@@ -109,3 +109,3 @@ import { Filter } from './Filter'; | ||
}; | ||
servers: { | ||
servers?: { | ||
url: string; | ||
@@ -129,3 +129,3 @@ }[]; | ||
}[]; | ||
}, openApiConfig: OpenApi3FilterConfig); | ||
}, openApiConfig?: OpenApi3FilterConfig); | ||
before(rr: RequestResponse, app: SimpleBootHttpServer): Promise<boolean>; | ||
@@ -132,0 +132,0 @@ after(rr: RequestResponse, app: SimpleBootHttpServer): Promise<boolean>; |
@@ -52,3 +52,3 @@ "use strict"; | ||
this.config = config; | ||
this.openApiConfig = Object.assign(Object.assign({}, DefulatData), openApiConfig); | ||
this.openApiConfig = Object.assign(Object.assign({}, DefulatData), openApiConfig !== null && openApiConfig !== void 0 ? openApiConfig : {}); | ||
} | ||
@@ -55,0 +55,0 @@ OpenApi3Filter.prototype.before = function (rr, app) { |
@@ -0,0 +0,0 @@ import { Filter } from '../filters/Filter'; |
@@ -57,5 +57,5 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var url, sw, regExps, _i, regExps_1, it_1, resourcePath, header; | ||
var url, sw, regExps, _i, regExps_1, it_1, resourcePath; | ||
return __generator(this, function (_b) { | ||
url = ((_a = rr.req.url) !== null && _a !== void 0 ? _a : '').replace(/\.\./g, ''); | ||
url = ((_a = rr.reqUrl) !== null && _a !== void 0 ? _a : '').replace(/\.\./g, ''); | ||
sw = true; | ||
@@ -67,6 +67,5 @@ regExps = this.resourceRegex.filter(function (it) { return RegExp(it).test(url); }); | ||
if (fs_1.default.existsSync(resourcePath)) { | ||
header = {}; | ||
rr.res.statusCode = HttpStatus_1.HttpStatus.Ok; | ||
rr.res.setHeader(HttpHeaders_1.HttpHeaders.ContentType, mime_types_1.default.lookup(resourcePath).toString()); | ||
rr.res.end(fs_1.default.readFileSync(resourcePath)); | ||
rr.resStatusCode(HttpStatus_1.HttpStatus.Ok); | ||
rr.resSetHeader(HttpHeaders_1.HttpHeaders.ContentType, mime_types_1.default.lookup(resourcePath).toString()); | ||
rr.resEnd(fs_1.default.readFileSync(resourcePath)); | ||
sw = false; | ||
@@ -73,0 +72,0 @@ break; |
@@ -0,0 +0,0 @@ import { Filter } from '../filters/Filter'; |
@@ -0,0 +0,0 @@ "use strict"; |
export declare class ReqFormUrlBody { | ||
[key: string]: string | string[]; | ||
} |
@@ -0,0 +0,0 @@ "use strict"; |
export declare class ReqJsonBody { | ||
[key: string]: any; | ||
} |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export declare class HeaderData<T> { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { HeaderData } from './HeaderData'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ /// <reference types="node" /> |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { Operation as JsonOperation } from 'fast-json-patch'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -10,6 +10,10 @@ /// <reference types="node" /> | ||
import { ReqFormUrlBody } from './datas/body/ReqFormUrlBody'; | ||
import { ReqJsonBody } from './datas/body/ReqJsonBody'; | ||
import { ReqHeader } from './datas/ReqHeader'; | ||
import { ReqMultipartFormBody } from './datas/body/ReqMultipartFormBody'; | ||
export declare class RequestResponse { | ||
req: IncomingMessage; | ||
res: ServerResponse; | ||
private req; | ||
private res; | ||
protected resWriteChunk: any; | ||
protected reqBodyChunk?: Buffer; | ||
constructor(req: IncomingMessage, res: ServerResponse); | ||
@@ -27,2 +31,3 @@ get reqRemoteAddress(): string | undefined; | ||
get reqPathSearchParamUrl(): string; | ||
get reqReadable(): boolean; | ||
get reqIntent(): Intent<any, any>; | ||
@@ -37,4 +42,6 @@ reqHasContentTypeHeader(mime: Mimes | string): boolean; | ||
reqBodyReqFormUrlBody(): Promise<ReqFormUrlBody>; | ||
reqBodyReqJsonBody<T>(): Promise<T>; | ||
resBodyJsonData<T>(): Promise<T>; | ||
reqBodyReqJsonBody(): Promise<ReqJsonBody>; | ||
reqBodyReqMultipartFormBody(): Promise<ReqMultipartFormBody>; | ||
resBodyJsonData<T>(): Promise<T | null>; | ||
resBodyStringData(): Promise<string>; | ||
reqMethod(): string | undefined; | ||
@@ -55,9 +62,9 @@ reqHeader(key: HttpHeaders | string, defaultValue?: string): string | string[] | undefined; | ||
resSetStatusCode(statusCode: number): RequestResponseChain<number>; | ||
resWrite(data: string | Buffer, encoding?: BufferEncoding): RequestResponseChain<boolean>; | ||
resWriteJson(data: any, encoding?: BufferEncoding): RequestResponseChain<RequestResponseChain<boolean>>; | ||
resWrite(chunk: string | Buffer | any, encoding?: BufferEncoding): RequestResponseChain<any>; | ||
resWriteJson(chunk: any, encoding?: BufferEncoding): RequestResponseChain<any>; | ||
resSetHeader(key: HttpHeaders | string, value: string | string[]): RequestResponseChain<ServerResponse>; | ||
resSetHeaders(headers: { | ||
[key: string]: string | string[]; | ||
}): RequestResponseChain<unknown>; | ||
resEnd(chunk?: any): RequestResponseChain<unknown>; | ||
}): RequestResponseChain<any>; | ||
resEnd(chunk?: any): void; | ||
resWriteHead(statusCode: number, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | { | ||
@@ -67,2 +74,3 @@ [key: string]: string | string[]; | ||
resIsDone(): boolean; | ||
createRequestResponseChain<T = any>(data?: T): RequestResponseChain<T>; | ||
} | ||
@@ -69,0 +77,0 @@ export declare class RequestResponseChain<T> extends RequestResponse { |
@@ -17,2 +17,38 @@ "use strict"; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -29,2 +65,3 @@ exports.RequestResponseChain = exports.RequestResponse = void 0; | ||
var ReqHeader_1 = require("./datas/ReqHeader"); | ||
var ReqMultipartFormBody_1 = require("./datas/body/ReqMultipartFormBody"); | ||
var RequestResponse = (function () { | ||
@@ -96,2 +133,9 @@ function RequestResponse(req, res) { | ||
}); | ||
Object.defineProperty(RequestResponse.prototype, "reqReadable", { | ||
get: function () { | ||
return this.req.readable; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RequestResponse.prototype, "reqIntent", { | ||
@@ -115,6 +159,15 @@ get: function () { | ||
return new Promise(function (resolve, reject) { | ||
var data = []; | ||
_this.req.on('data', function (chunk) { return data.push(chunk); }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { return resolve(buffer_1.Buffer.concat(data)); }); | ||
var _a; | ||
if (_this.reqReadable) { | ||
var data_1 = []; | ||
_this.req.on('data', function (chunk) { return data_1.push(chunk); }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { | ||
_this.reqBodyChunk = buffer_1.Buffer.concat(data_1); | ||
resolve(_this.reqBodyChunk); | ||
}); | ||
} | ||
else { | ||
resolve((_a = _this.reqBodyChunk) !== null && _a !== void 0 ? _a : buffer_1.Buffer.alloc(0)); | ||
} | ||
}); | ||
@@ -160,41 +213,51 @@ }; | ||
RequestResponse.prototype.reqBodyStringData = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = ''; | ||
_this.req.on('data', function (chunk) { return data += chunk; }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { return resolve(data); }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.reqBodyData()]; | ||
case 1: | ||
data = (_a.sent()).toString(); | ||
return [2, data]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestResponse.prototype.reqBodyJsonData = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = ''; | ||
_this.req.on('data', function (chunk) { return data += chunk; }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { return resolve(JSON.parse(data)); }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_b = (_a = JSON).parse; | ||
return [4, this.reqBodyStringData()]; | ||
case 1: return [2, _b.apply(_a, [_c.sent()])]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestResponse.prototype.reqBodyFormUrlData = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = ''; | ||
_this.req.on('data', function (chunk) { return data += chunk; }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { | ||
var formData = {}; | ||
Array.from(new URLSearchParams(data).entries()).forEach(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
var target = formData[k]; | ||
if (Array.isArray(target)) { | ||
target.push(v); | ||
} | ||
else if (typeof target === 'string') { | ||
formData[k] = [target, v]; | ||
} | ||
else { | ||
formData[k] = v; | ||
} | ||
}); | ||
resolve(formData); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data, formData; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.reqBodyStringData()]; | ||
case 1: | ||
data = (_a.sent()); | ||
formData = {}; | ||
Array.from(new URLSearchParams(data).entries()).forEach(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
var target = formData[k]; | ||
if (Array.isArray(target)) { | ||
target.push(v); | ||
} | ||
else if (typeof target === 'string') { | ||
formData[k] = [target, v]; | ||
} | ||
else { | ||
formData[k] = v; | ||
} | ||
}); | ||
return [2, formData]; | ||
} | ||
}); | ||
@@ -204,23 +267,11 @@ }); | ||
RequestResponse.prototype.reqBodyReqFormUrlBody = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = ''; | ||
_this.req.on('data', function (chunk) { return data += chunk; }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { | ||
var formData = new ReqFormUrlBody_1.ReqFormUrlBody(); | ||
Array.from(new URLSearchParams(data).entries()).forEach(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
var target = formData[k]; | ||
if (Array.isArray(target)) { | ||
target.push(v); | ||
} | ||
else if (typeof target === 'string') { | ||
formData[k] = [target, v]; | ||
} | ||
else { | ||
formData[k] = v; | ||
} | ||
}); | ||
resolve(formData); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.reqBodyFormUrlData()]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2, Object.assign(new ReqFormUrlBody_1.ReqFormUrlBody(), data)]; | ||
} | ||
}); | ||
@@ -230,22 +281,40 @@ }); | ||
RequestResponse.prototype.reqBodyReqJsonBody = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = ''; | ||
_this.req.on('data', function (chunk) { return data += chunk; }); | ||
_this.req.on('error', function (err) { return reject(err); }); | ||
_this.req.on('end', function () { | ||
var parse = Object.assign(new ReqJsonBody_1.ReqJsonBody(), JSON.parse(data)); | ||
resolve(parse); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.reqBodyStringData()]; | ||
case 1: | ||
data = (_a.sent()); | ||
return [2, Object.assign(new ReqJsonBody_1.ReqJsonBody(), data ? JSON.parse(data) : {})]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestResponse.prototype.reqBodyReqMultipartFormBody = function () { | ||
return this.reqBodyMultipartFormData().then(function (it) { return new ReqMultipartFormBody_1.ReqMultipartFormBody(it); }); | ||
}; | ||
RequestResponse.prototype.resBodyJsonData = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = ''; | ||
_this.res.on('data', function (chunk) { return data += chunk; }); | ||
_this.res.on('error', function (err) { return reject(err); }); | ||
_this.res.on('end', function () { return resolve(JSON.parse(data)); }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.reqBodyData()]; | ||
case 1: | ||
data = (_a.sent()).toString(); | ||
return [2, data ? JSON.parse(data) : null]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestResponse.prototype.resBodyStringData = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.reqBodyData()]; | ||
case 1: return [2, (_a.sent()).toString()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestResponse.prototype.reqMethod = function () { | ||
@@ -323,14 +392,15 @@ var _a; | ||
this.res.statusCode = statusCode; | ||
return new RequestResponseChain(this.req, this.res, this.res.statusCode); | ||
return this.createRequestResponseChain(this.res.statusCode); | ||
}; | ||
RequestResponse.prototype.resWrite = function (data, encoding) { | ||
RequestResponse.prototype.resWrite = function (chunk, encoding) { | ||
if (encoding === void 0) { encoding = 'utf8'; } | ||
return new RequestResponseChain(this.req, this.res, this.res.write(data, encoding)); | ||
this.resWriteChunk = chunk; | ||
return this.createRequestResponseChain(this.resWriteChunk); | ||
}; | ||
RequestResponse.prototype.resWriteJson = function (data, encoding) { | ||
RequestResponse.prototype.resWriteJson = function (chunk, encoding) { | ||
if (encoding === void 0) { encoding = 'utf8'; } | ||
return new RequestResponseChain(this.req, this.res, this.resWrite(JSON.stringify(data), encoding)); | ||
return this.resWrite(this.resWrite(JSON.stringify(chunk), encoding)); | ||
}; | ||
RequestResponse.prototype.resSetHeader = function (key, value) { | ||
return new RequestResponseChain(this.req, this.res, this.res.setHeader(key.toLowerCase(), value)); | ||
return this.createRequestResponseChain(this.res.setHeader(key.toLowerCase(), value)); | ||
}; | ||
@@ -343,10 +413,18 @@ RequestResponse.prototype.resSetHeaders = function (headers) { | ||
}); | ||
return new RequestResponseChain(this.req, this.res); | ||
return this.createRequestResponseChain(); | ||
}; | ||
RequestResponse.prototype.resEnd = function (chunk) { | ||
this.res.end(chunk); | ||
return new RequestResponseChain(this.req, this.res); | ||
var _this = this; | ||
this.resWriteChunk = chunk !== null && chunk !== void 0 ? chunk : this.resWriteChunk; | ||
if (this.req.readable) { | ||
this.reqBodyData().then(function (it) { | ||
_this.res.end(_this.resWriteChunk); | ||
}); | ||
} | ||
else { | ||
this.res.end(this.resWriteChunk); | ||
} | ||
}; | ||
RequestResponse.prototype.resWriteHead = function (statusCode, headers) { | ||
return new RequestResponseChain(this.req, this.res, this.res.writeHead(statusCode, headers)); | ||
return this.createRequestResponseChain(this.res.writeHead(statusCode, headers)); | ||
}; | ||
@@ -356,2 +434,8 @@ RequestResponse.prototype.resIsDone = function () { | ||
}; | ||
RequestResponse.prototype.createRequestResponseChain = function (data) { | ||
var requestResponseChain = new RequestResponseChain(this.req, this.res, data); | ||
requestResponseChain.resWriteChunk = this.resWriteChunk; | ||
requestResponseChain.reqBodyChunk = this.reqBodyChunk; | ||
return requestResponseChain; | ||
}; | ||
return RequestResponse; | ||
@@ -358,0 +442,0 @@ }()); |
@@ -0,0 +0,0 @@ /// <reference types="node" /> |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "simple-boot-http-server", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"main": "SimpleBootHttpServer.js", | ||
@@ -83,4 +83,4 @@ "license": "MIT", | ||
"mime-types": "^2.1.34", | ||
"simple-boot-core": "^1.0.24" | ||
"simple-boot-core": "^1.0.26" | ||
} | ||
} |
@@ -36,4 +36,4 @@ SIMPLE-BOOT-HTTP-SERVER | ||
console.log('load Hello.get()', rr); | ||
rr.res.setHeader('Content-Type', 'application/json'); | ||
rr.res.end(JSON.stringify({ a: 1 })); | ||
rr.resSetHeader('Content-Type', 'application/json'); | ||
rr.resEnd(JSON.stringify({ a: 1 })); | ||
} | ||
@@ -44,12 +44,12 @@ } | ||
export class FirstFilter implements Filter { | ||
after(req: IncomingMessage, res: ServerResponse, app: SimpleBootHttpServer, sw: boolean): Promise<boolean> { | ||
async after(rr: RequestResponse, app: SimpleBootHttpServer, sw: boolean): Promise<boolean> { | ||
console.log('filter after') | ||
return Promise.resolve(true); | ||
return true; | ||
} | ||
before(req: IncomingMessage, res: ServerResponse, app: SimpleBootHttpServer): Promise<boolean> { | ||
async before(rr: RequestResponse, app: SimpleBootHttpServer): Promise<boolean> { | ||
console.log('filter before') | ||
return Promise.resolve(true); | ||
return true; | ||
} | ||
} | ||
``` |
@@ -0,0 +0,0 @@ import { RequestResponse } from '../models/RequestResponse'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,0 +0,0 @@ import { SimpleApplication } from 'simple-boot-core/SimpleApplication'; |
@@ -70,2 +70,6 @@ "use strict"; | ||
var RouterModule_1 = require("simple-boot-core/route/RouterModule"); | ||
var ReqMultipartFormBody_1 = require("./models/datas/body/ReqMultipartFormBody"); | ||
var Validation_1 = require("simple-boot-core/decorators/validate/Validation"); | ||
var ValidException_1 = require("simple-boot-core/errors/ValidException"); | ||
var HttpError_1 = require("./errors/HttpError"); | ||
var SimpleBootHttpServer = (function (_super) { | ||
@@ -88,8 +92,80 @@ __extends(SimpleBootHttpServer, _super); | ||
server.on('request', function (req, res) { return __awaiter(_this, void 0, void 0, function () { | ||
var rr, otherStorage, _i, _a, it_1, execute, filter, i, it_2, execute, sw, routerModule, moduleInstance, methods, it_3, paramTypes, injects, isJson, isFormUrl, ss, data_1, data_2, _b, paramTypes_1, paramType, _c, _d, _e, _f, _g, _h, data, execute, status, headers, _j, _k, it_4, _l, e_1, execute, target; | ||
var rr, otherStorage, _i, _a, it_1, execute, filter, i, it_2, execute, sw, routerModule, moduleInstance, methods, it_3, paramTypes, injects, validIndexs, isJson, isFormUrl, siturationContainers, data_1, inValid, data_2, inValid, _b, paramTypes_1, paramType, _c, _d, _e, _f, _g, _h, data, execute, status, headers, _j, _k, it_4, _l, e_1, execute, target; | ||
var _this = this; | ||
var _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5; | ||
return __generator(this, function (_6) { | ||
switch (_6.label) { | ||
var _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7; | ||
return __generator(this, function (_8) { | ||
switch (_8.label) { | ||
case 0: | ||
res.on('close', function () { return __awaiter(_this, void 0, void 0, function () { | ||
var _i, _a, it_5, execute, endPoint, e_2; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.option.closeEndPoints) return [3, 7]; | ||
_i = 0, _a = this.option.closeEndPoints; | ||
_b.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) return [3, 7]; | ||
it_5 = _a[_i]; | ||
_b.label = 2; | ||
case 2: | ||
_b.trys.push([2, 5, , 6]); | ||
execute = (typeof it_5 === 'function' ? this.simstanceManager.getOrNewSim(it_5) : it_5); | ||
endPoint = execute === null || execute === void 0 ? void 0 : execute.endPoint(rr, this); | ||
if (!(endPoint instanceof Promise)) return [3, 4]; | ||
return [4, endPoint]; | ||
case 3: | ||
_b.sent(); | ||
_b.label = 4; | ||
case 4: return [3, 6]; | ||
case 5: | ||
e_2 = _b.sent(); | ||
return [3, 6]; | ||
case 6: | ||
_i++; | ||
return [3, 1]; | ||
case 7: | ||
if (!rr.resIsDone()) { | ||
rr.resEnd(); | ||
} | ||
return [2]; | ||
} | ||
}); | ||
}); }); | ||
res.on('error', function () { return __awaiter(_this, void 0, void 0, function () { | ||
var _i, _a, it_6, execute, endPoint, e_3; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.option.errorEndPoints) return [3, 7]; | ||
_i = 0, _a = this.option.errorEndPoints; | ||
_b.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) return [3, 7]; | ||
it_6 = _a[_i]; | ||
_b.label = 2; | ||
case 2: | ||
_b.trys.push([2, 5, , 6]); | ||
execute = (typeof it_6 === 'function' ? this.simstanceManager.getOrNewSim(it_6) : it_6); | ||
endPoint = execute === null || execute === void 0 ? void 0 : execute.endPoint(rr, this); | ||
if (!(endPoint instanceof Promise)) return [3, 4]; | ||
return [4, endPoint]; | ||
case 3: | ||
_b.sent(); | ||
_b.label = 4; | ||
case 4: return [3, 6]; | ||
case 5: | ||
e_3 = _b.sent(); | ||
return [3, 6]; | ||
case 6: | ||
_i++; | ||
return [3, 1]; | ||
case 7: | ||
if (!rr.resIsDone()) { | ||
rr.resEnd(); | ||
} | ||
return [2]; | ||
} | ||
}); | ||
}); }); | ||
rr = new RequestResponse_1.RequestResponse(req, res); | ||
@@ -100,5 +176,5 @@ otherStorage = new Map(); | ||
otherStorage.set(http_1.ServerResponse, res); | ||
_6.label = 1; | ||
_8.label = 1; | ||
case 1: | ||
_6.trys.push([1, 30, , 33]); | ||
_8.trys.push([1, 30, , 33]); | ||
if (this.option.requestEndPoints) { | ||
@@ -117,3 +193,3 @@ for (_i = 0, _a = this.option.requestEndPoints; _i < _a.length; _i++) { | ||
i = 0; | ||
_6.label = 2; | ||
_8.label = 2; | ||
case 2: | ||
@@ -127,5 +203,5 @@ if (!(this.option.filters && i < this.option.filters.length)) return [3, 6]; | ||
case 3: | ||
sw = _6.sent(); | ||
sw = _8.sent(); | ||
filter.push({ filter: execute, sw: sw }); | ||
_6.label = 4; | ||
_8.label = 4; | ||
case 4: | ||
@@ -135,3 +211,3 @@ if (!sw) { | ||
} | ||
_6.label = 5; | ||
_8.label = 5; | ||
case 5: | ||
@@ -141,6 +217,6 @@ i++; | ||
case 6: | ||
if (!(!rr.res.finished || !rr.res.writableEnded)) return [3, 24]; | ||
if (!!rr.resIsDone()) return [3, 24]; | ||
return [4, this.routing(rr.reqIntent)]; | ||
case 7: | ||
routerModule = _6.sent(); | ||
routerModule = _8.sent(); | ||
otherStorage.set(RouterModule_1.RouterModule, routerModule); | ||
@@ -158,3 +234,2 @@ moduleInstance = (_m = routerModule === null || routerModule === void 0 ? void 0 : routerModule.getModuleInstance) === null || _m === void 0 ? void 0 : _m.call(routerModule); | ||
.filter(function (it) { var _a, _b, _c, _d, _e; return ((_b = (_a = it.config) === null || _a === void 0 ? void 0 : _a.req) === null || _b === void 0 ? void 0 : _b.accept) ? (!!((_e = (_d = (_c = it.config) === null || _c === void 0 ? void 0 : _c.req) === null || _d === void 0 ? void 0 : _d.accept) === null || _e === void 0 ? void 0 : _e.find(function (sit) { return rr.reqHasAcceptHeader(sit); }))) : true; }); | ||
console.dir(methods, { depth: 5 }); | ||
if (!methods[0]) return [3, 23]; | ||
@@ -164,12 +239,25 @@ it_3 = methods[0]; | ||
injects = (0, Inject_1.getInject)(moduleInstance, it_3.propertyKey); | ||
validIndexs = (0, Validation_1.getValidIndex)(moduleInstance, it_3.propertyKey); | ||
if (!injects) return [3, 12]; | ||
isJson = injects.find(function (it) { var _a; return ((_a = it.config) === null || _a === void 0 ? void 0 : _a.situationType) === MethodMapping_1.UrlMappingSituationType.REQ_JSON_BODY; }); | ||
isFormUrl = injects.find(function (it) { var _a; return ((_a = it.config) === null || _a === void 0 ? void 0 : _a.situationType) === MethodMapping_1.UrlMappingSituationType.REQ_FORM_URL_BODY; }); | ||
ss = new Inject_1.SituationTypeContainers(); | ||
siturationContainers = new Inject_1.SituationTypeContainers(); | ||
if (!isJson) return [3, 9]; | ||
return [4, rr.reqBodyJsonData()]; | ||
case 8: | ||
data_1 = _6.sent(); | ||
ss.push(new Inject_1.SituationTypeContainer({ situationType: MethodMapping_1.UrlMappingSituationType.REQ_JSON_BODY, data: data_1 })); | ||
_6.label = 9; | ||
data_1 = _8.sent(); | ||
if (isJson.type) { | ||
data_1 = Object.assign(new isJson.type(), data_1); | ||
} | ||
if (validIndexs.includes(isJson.index)) { | ||
inValid = (0, Validation_1.execValidationInValid)(data_1); | ||
if (((_p = inValid === null || inValid === void 0 ? void 0 : inValid.length) !== null && _p !== void 0 ? _p : 0) > 0) { | ||
throw new ValidException_1.ValidException(inValid); | ||
} | ||
} | ||
siturationContainers.push(new Inject_1.SituationTypeContainer({ | ||
situationType: MethodMapping_1.UrlMappingSituationType.REQ_JSON_BODY, | ||
data: data_1 | ||
})); | ||
_8.label = 9; | ||
case 9: | ||
@@ -179,13 +267,25 @@ if (!isFormUrl) return [3, 11]; | ||
case 10: | ||
data_2 = _6.sent(); | ||
ss.push(new Inject_1.SituationTypeContainer({ situationType: MethodMapping_1.UrlMappingSituationType.REQ_FORM_URL_BODY, data: data_2 })); | ||
_6.label = 11; | ||
data_2 = _8.sent(); | ||
if (isFormUrl.type) { | ||
data_2 = Object.assign(new isFormUrl.type(), data_2); | ||
} | ||
if (validIndexs.includes(isFormUrl.index)) { | ||
inValid = (0, Validation_1.execValidationInValid)(data_2); | ||
if (((_q = inValid === null || inValid === void 0 ? void 0 : inValid.length) !== null && _q !== void 0 ? _q : 0) > 0) { | ||
throw new ValidException_1.ValidException(inValid); | ||
} | ||
} | ||
siturationContainers.push(new Inject_1.SituationTypeContainer({ | ||
situationType: MethodMapping_1.UrlMappingSituationType.REQ_FORM_URL_BODY, | ||
data: data_2 | ||
})); | ||
_8.label = 11; | ||
case 11: | ||
if (ss.length) { | ||
otherStorage.set(Inject_1.SituationTypeContainers, ss); | ||
if (siturationContainers.length) { | ||
otherStorage.set(Inject_1.SituationTypeContainers, siturationContainers); | ||
} | ||
_6.label = 12; | ||
_8.label = 12; | ||
case 12: | ||
_b = 0, paramTypes_1 = paramTypes; | ||
_6.label = 13; | ||
_8.label = 13; | ||
case 13: | ||
@@ -199,3 +299,3 @@ if (!(_b < paramTypes_1.length)) return [3, 19]; | ||
case 14: | ||
_d.apply(_c, _e.concat([_6.sent()])); | ||
_d.apply(_c, _e.concat([_8.sent()])); | ||
return [3, 18]; | ||
@@ -208,3 +308,3 @@ case 15: | ||
case 16: | ||
_g.apply(_f, _h.concat([_6.sent()])); | ||
_g.apply(_f, _h.concat([_8.sent()])); | ||
return [3, 18]; | ||
@@ -215,6 +315,9 @@ case 17: | ||
} | ||
else if (paramType === ReqMultipartFormBody_1.ReqMultipartFormBody) { | ||
otherStorage.set(ReqMultipartFormBody_1.ReqMultipartFormBody, rr.reqBodyReqMultipartFormBody()); | ||
} | ||
else if (paramType === ReqHeader_1.ReqHeader) { | ||
otherStorage.set(ReqHeader_1.ReqHeader, rr.reqHeaderObj); | ||
} | ||
_6.label = 18; | ||
_8.label = 18; | ||
case 18: | ||
@@ -228,16 +331,16 @@ _b++; | ||
case 20: | ||
data = _6.sent(); | ||
if (!((_p = it_3.config) === null || _p === void 0 ? void 0 : _p.resolver)) return [3, 22]; | ||
data = _8.sent(); | ||
if (!((_r = it_3.config) === null || _r === void 0 ? void 0 : _r.resolver)) return [3, 22]; | ||
execute = typeof it_3.config.resolver === 'function' ? this.simstanceManager.getOrNewSim(it_3.config.resolver) : it_3.config.resolver; | ||
return [4, ((_q = execute === null || execute === void 0 ? void 0 : execute.resolve) === null || _q === void 0 ? void 0 : _q.call(execute, data, rr))]; | ||
return [4, ((_s = execute === null || execute === void 0 ? void 0 : execute.resolve) === null || _s === void 0 ? void 0 : _s.call(execute, data, rr))]; | ||
case 21: | ||
data = _6.sent(); | ||
_6.label = 22; | ||
data = _8.sent(); | ||
_8.label = 22; | ||
case 22: | ||
status = (_t = (_s = (_r = it_3.config) === null || _r === void 0 ? void 0 : _r.res) === null || _s === void 0 ? void 0 : _s.status) !== null && _t !== void 0 ? _t : HttpStatus_1.HttpStatus.Ok; | ||
headers = (_w = (_v = (_u = it_3.config) === null || _u === void 0 ? void 0 : _u.res) === null || _v === void 0 ? void 0 : _v.header) !== null && _w !== void 0 ? _w : {}; | ||
if ((_y = (_x = it_3.config) === null || _x === void 0 ? void 0 : _x.res) === null || _y === void 0 ? void 0 : _y.contentType) { | ||
headers[HttpHeaders_1.HttpHeaders.ContentType] = (_0 = (_z = it_3.config) === null || _z === void 0 ? void 0 : _z.res) === null || _0 === void 0 ? void 0 : _0.contentType; | ||
status = (_v = (_u = (_t = it_3.config) === null || _t === void 0 ? void 0 : _t.res) === null || _u === void 0 ? void 0 : _u.status) !== null && _v !== void 0 ? _v : HttpStatus_1.HttpStatus.Ok; | ||
headers = (_y = (_x = (_w = it_3.config) === null || _w === void 0 ? void 0 : _w.res) === null || _x === void 0 ? void 0 : _x.header) !== null && _y !== void 0 ? _y : {}; | ||
if ((_0 = (_z = it_3.config) === null || _z === void 0 ? void 0 : _z.res) === null || _0 === void 0 ? void 0 : _0.contentType) { | ||
headers[HttpHeaders_1.HttpHeaders.ContentType] = (_2 = (_1 = it_3.config) === null || _1 === void 0 ? void 0 : _1.res) === null || _2 === void 0 ? void 0 : _2.contentType; | ||
} | ||
if (((_4 = (_3 = (_2 = (_1 = it_3.config) === null || _1 === void 0 ? void 0 : _1.res) === null || _2 === void 0 ? void 0 : _2.contentType) === null || _3 === void 0 ? void 0 : _3.toLowerCase().indexOf(Mimes_1.Mimes.ApplicationJson.toLowerCase())) !== null && _4 !== void 0 ? _4 : -1) > -1) { | ||
if (((_6 = (_5 = (_4 = (_3 = it_3.config) === null || _3 === void 0 ? void 0 : _3.res) === null || _4 === void 0 ? void 0 : _4.contentType) === null || _5 === void 0 ? void 0 : _5.toLowerCase().indexOf(Mimes_1.Mimes.ApplicationJson.toLowerCase())) !== null && _6 !== void 0 ? _6 : -1) > -1) { | ||
data = JSON.stringify(data); | ||
@@ -250,4 +353,4 @@ } | ||
rr.resSetStatusCode(status); | ||
rr.resEnd(data); | ||
_6.label = 23; | ||
rr.resWrite(data); | ||
_8.label = 23; | ||
case 23: | ||
@@ -257,15 +360,15 @@ if (this.option.noSuchRouteEndPointMappingThrow && methods.length <= 0) { | ||
} | ||
_6.label = 24; | ||
_8.label = 24; | ||
case 24: | ||
_j = 0, _k = filter.reverse(); | ||
_6.label = 25; | ||
_8.label = 25; | ||
case 25: | ||
if (!(_j < _k.length)) return [3, 29]; | ||
it_4 = _k[_j]; | ||
_l = ((_5 = it_4.filter) === null || _5 === void 0 ? void 0 : _5.after); | ||
_l = ((_7 = it_4.filter) === null || _7 === void 0 ? void 0 : _7.after); | ||
if (!_l) return [3, 27]; | ||
return [4, it_4.filter.after(rr, this, it_4.sw)]; | ||
case 26: | ||
_l = !(_6.sent()); | ||
_6.label = 27; | ||
_l = !(_8.sent()); | ||
_8.label = 27; | ||
case 27: | ||
@@ -275,3 +378,3 @@ if (_l) { | ||
} | ||
_6.label = 28; | ||
_8.label = 28; | ||
case 28: | ||
@@ -282,6 +385,6 @@ _j++; | ||
case 30: | ||
e_1 = _6.sent(); | ||
e_1 = _8.sent(); | ||
rr.resStatusCode(e_1 instanceof HttpError_1.HttpError ? e_1.status : HttpStatus_1.HttpStatus.InternalServerError); | ||
execute = typeof this.option.globalAdvice === 'function' ? this.simstanceManager.getOrNewSim(this.option.globalAdvice) : this.option.globalAdvice; | ||
if (!execute) { | ||
rr.resWriteHead(HttpStatus_1.HttpStatus.InternalServerError); | ||
rr.resEnd(); | ||
@@ -292,3 +395,6 @@ return [2]; | ||
otherStorage.set(e_1.constructor, e_1); | ||
otherStorage.set(Inject_1.SituationTypeContainer, new Inject_1.SituationTypeContainer({ situationType: ExceptionDecorator_1.ExceptionHandlerSituationType.ERROR_OBJECT, data: e_1 })); | ||
otherStorage.set(Inject_1.SituationTypeContainer, new Inject_1.SituationTypeContainer({ | ||
situationType: ExceptionDecorator_1.ExceptionHandlerSituationType.ERROR_OBJECT, | ||
data: e_1 | ||
})); | ||
} | ||
@@ -302,36 +408,9 @@ target = (0, ExceptionDecorator_1.targetExceptionHandler)(execute, e_1); | ||
case 31: | ||
_6.sent(); | ||
_6.label = 32; | ||
case 32: | ||
_8.sent(); | ||
_8.label = 32; | ||
case 32: return [3, 33]; | ||
case 33: | ||
if (!rr.resIsDone()) { | ||
rr.resEnd(); | ||
} | ||
return [3, 33]; | ||
case 33: | ||
res.on('close', function () { | ||
if (_this.option.closeEndPoints) { | ||
for (var _i = 0, _a = _this.option.closeEndPoints; _i < _a.length; _i++) { | ||
var it_5 = _a[_i]; | ||
try { | ||
var execute = (typeof it_5 === 'function' ? _this.simstanceManager.getOrNewSim(it_5) : it_5); | ||
execute === null || execute === void 0 ? void 0 : execute.endPoint(rr, _this); | ||
} | ||
catch (e) { | ||
} | ||
} | ||
} | ||
}); | ||
res.on('error', function () { | ||
if (_this.option.errorEndPoints) { | ||
for (var _i = 0, _a = _this.option.errorEndPoints; _i < _a.length; _i++) { | ||
var it_6 = _a[_i]; | ||
try { | ||
var execute = (typeof it_6 === 'function' ? _this.simstanceManager.getOrNewSim(it_6) : it_6); | ||
execute === null || execute === void 0 ? void 0 : execute.endPoint(rr, _this); | ||
} | ||
catch (e) { | ||
} | ||
} | ||
} | ||
}); | ||
return [2]; | ||
@@ -338,0 +417,0 @@ } |
125044
58
2383
Updatedsimple-boot-core@^1.0.26