@aomex/web
Advanced tools
Comparing version 0.0.20 to 0.0.21
# @aomex/web | ||
## 0.0.21 | ||
### Patch Changes | ||
- [`a8dbaf3`](https://github.com/aomex/aomex/commit/a8dbaf38524f5a5d58337bc617175115742f9e1a) Thanks [@geekact](https://github.com/geekact)! - feat(web): remove readonly modifier from request.method and request.url | ||
- Updated dependencies [[`ddcec91`](https://github.com/aomex/aomex/commit/ddcec91803b00f35f7c55964d1a2d97c40a22c6e)]: | ||
- @aomex/core@0.0.20 | ||
## 0.0.20 | ||
@@ -4,0 +13,0 @@ |
@@ -13,2 +13,3 @@ import { Chain, PureChain, PureMiddlewareToken, Next, Middleware, OpenAPI, Validator, TransformedValidator, magistrate } from '@aomex/core'; | ||
import { File } from 'formidable'; | ||
import { RequestListener as RequestListener$1, Server as Server$1 } from 'http'; | ||
export { default as statuses } from 'statuses'; | ||
@@ -79,2 +80,5 @@ | ||
} | ||
declare module WebResponse { | ||
type FakeType = typeof ServerResponse & (new (req: IncomingMessage) => ServerResponse<IncomingMessage>); | ||
} | ||
declare class WebResponse<Request extends WebRequest = WebRequest> extends ServerResponse<Request> { | ||
@@ -145,2 +149,18 @@ app: WebApp; | ||
declare const getMimeType: (filenameOrExt: string) => string | false; | ||
declare enum METHOD { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
PATCH = "PATCH", | ||
DELETE = "DELETE", | ||
OPTIONS = "OPTIONS", | ||
HEAD = "HEAD" | ||
} | ||
declare const createHttpServer: (listener: RequestListener$1<typeof WebRequest, | ||
/** @ts-ignore */ | ||
typeof WebResponse<WebRequest>>) => Server$1<any, any>; | ||
declare class WebRequest extends IncomingMessage { | ||
@@ -150,4 +170,4 @@ app: WebApp; | ||
ctx: WebContext; | ||
readonly method: string; | ||
readonly url: string; | ||
method: METHOD; | ||
url: string; | ||
params: Record<string, unknown>; | ||
@@ -241,14 +261,2 @@ protected _query?: any; | ||
declare const getMimeType: (filenameOrExt: string) => string | false; | ||
declare enum METHOD { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
PATCH = "PATCH", | ||
DELETE = "DELETE", | ||
OPTIONS = "OPTIONS", | ||
HEAD = "HEAD" | ||
} | ||
declare module '@aomex/core' { | ||
@@ -445,2 +453,2 @@ interface MiddlewarePlatform { | ||
export { Body, FileValidator, METHOD, WebApp, WebAppOption, WebBodyMiddleware, WebChain, WebContext, WebMiddleware, WebMiddlewareSkipOptions, WebMiddlewareToDocument, WebMiddlewareToken, WebParamMiddleware, WebQueryMiddleware, WebRequest, WebResponse, WebResponseMiddleware, WebResponseOptions, body, getMimeType, params, query, response, skip }; | ||
export { Body, FileValidator, METHOD, WebApp, WebAppOption, WebBodyMiddleware, WebChain, WebContext, WebMiddleware, WebMiddlewareSkipOptions, WebMiddlewareToDocument, WebMiddlewareToken, WebParamMiddleware, WebQueryMiddleware, WebRequest, WebResponse, WebResponseMiddleware, WebResponseOptions, body, createHttpServer, getMimeType, params, query, response, skip }; |
@@ -156,3 +156,2 @@ // src/override/middleware.ts | ||
// src/app/app.ts | ||
import { createServer } from "node:http"; | ||
import EventEmitter from "node:events"; | ||
@@ -163,2 +162,32 @@ import { EOL } from "node:os"; | ||
// src/util/get-content-type.ts | ||
import mimeTypes2 from "mime-types"; | ||
import { MemoryCache } from "@aomex/core"; | ||
var cache = new MemoryCache({ | ||
maxItems: 100 | ||
}); | ||
var getMimeType = (filenameOrExt) => { | ||
let mimeType = cache.lru.get(filenameOrExt); | ||
if (!mimeType) { | ||
mimeType = mimeTypes2.contentType(filenameOrExt); | ||
cache.lru.set(filenameOrExt, mimeType); | ||
} | ||
return mimeType; | ||
}; | ||
// src/util/method.ts | ||
var METHOD = /* @__PURE__ */ ((METHOD2) => { | ||
METHOD2["GET"] = "GET"; | ||
METHOD2["POST"] = "POST"; | ||
METHOD2["PUT"] = "PUT"; | ||
METHOD2["PATCH"] = "PATCH"; | ||
METHOD2["DELETE"] = "DELETE"; | ||
METHOD2["OPTIONS"] = "OPTIONS"; | ||
METHOD2["HEAD"] = "HEAD"; | ||
return METHOD2; | ||
})(METHOD || {}); | ||
// src/util/create-http-server.ts | ||
import { createServer } from "http"; | ||
// src/app/request.ts | ||
@@ -296,2 +325,61 @@ import { IncomingMessage } from "node:http"; | ||
// src/util/create-http-server.ts | ||
var createHttpServer = (listener) => { | ||
return createServer( | ||
{ | ||
IncomingMessage: WebRequest, | ||
// @ts-ignore | ||
ServerResponse: WebResponse | ||
}, | ||
listener | ||
); | ||
}; | ||
// src/app/app.ts | ||
var WebApp = class extends EventEmitter { | ||
constructor(options = {}) { | ||
super(); | ||
this.options = options; | ||
} | ||
chainPoints = []; | ||
middlewareList = []; | ||
get debug() { | ||
return this.options.debug || false; | ||
} | ||
listen = (...args) => { | ||
const server = createHttpServer(this.callback()); | ||
return server.listen(...args); | ||
}; | ||
callback() { | ||
const fn = compose2(this.middlewareList); | ||
if (!this.listenerCount("error")) { | ||
this.on("error", this.log.bind(this)); | ||
} | ||
return (req, res) => { | ||
return fn(new WebContext(this, req, res)).then(res.flush.bind(res)).catch(res.onError); | ||
}; | ||
} | ||
log(err) { | ||
if (this.options.silent) | ||
return; | ||
if ((err.status || err.statusCode) === 404 || err.expose) | ||
return; | ||
const msgs = (err.stack || err.toString()).split(EOL, 2); | ||
console.error( | ||
["", chalk.bgRed(msgs.shift()), msgs.join(EOL), ""].join(EOL) | ||
); | ||
} | ||
on(eventName, listener) { | ||
return super.on(eventName, listener); | ||
} | ||
mount(middleware2) { | ||
if (middleware2 === null) | ||
return; | ||
if (middleware2 instanceof Chain2) { | ||
this.chainPoints.push(Chain2.createSplitPoint(middleware2)); | ||
} | ||
this.middlewareList.push(middleware2); | ||
} | ||
}; | ||
// src/app/response.ts | ||
@@ -310,31 +398,2 @@ import { ServerResponse } from "node:http"; | ||
import { createReadStream } from "node:fs"; | ||
// src/util/get-content-type.ts | ||
import mimeTypes2 from "mime-types"; | ||
import { MemoryCache } from "@aomex/core"; | ||
var cache = new MemoryCache({ | ||
maxItems: 100 | ||
}); | ||
var getMimeType = (filenameOrExt) => { | ||
let mimeType = cache.lru.get(filenameOrExt); | ||
if (!mimeType) { | ||
mimeType = mimeTypes2.contentType(filenameOrExt); | ||
cache.lru.set(filenameOrExt, mimeType); | ||
} | ||
return mimeType; | ||
}; | ||
// src/util/method.ts | ||
var METHOD = /* @__PURE__ */ ((METHOD2) => { | ||
METHOD2["GET"] = "GET"; | ||
METHOD2["POST"] = "POST"; | ||
METHOD2["PUT"] = "PUT"; | ||
METHOD2["PATCH"] = "PATCH"; | ||
METHOD2["DELETE"] = "DELETE"; | ||
METHOD2["OPTIONS"] = "OPTIONS"; | ||
METHOD2["HEAD"] = "HEAD"; | ||
return METHOD2; | ||
})(METHOD || {}); | ||
// src/app/response.ts | ||
import typeIs2 from "type-is"; | ||
@@ -595,55 +654,2 @@ import vary from "vary"; | ||
// src/app/app.ts | ||
var WebApp = class extends EventEmitter { | ||
constructor(options = {}) { | ||
super(); | ||
this.options = options; | ||
} | ||
chainPoints = []; | ||
middlewareList = []; | ||
get debug() { | ||
return this.options.debug || false; | ||
} | ||
listen = (...args) => { | ||
const server = createServer( | ||
{ | ||
IncomingMessage: WebRequest, | ||
ServerResponse: WebResponse | ||
}, | ||
this.callback() | ||
); | ||
return server.listen(...args); | ||
}; | ||
callback() { | ||
const fn = compose2(this.middlewareList); | ||
if (!this.listenerCount("error")) { | ||
this.on("error", this.log.bind(this)); | ||
} | ||
return (req, res) => { | ||
return fn(new WebContext(this, req, res)).then(res.flush.bind(res)).catch(res.onError); | ||
}; | ||
} | ||
log(err) { | ||
if (this.options.silent) | ||
return; | ||
if ((err.status || err.statusCode) === 404 || err.expose) | ||
return; | ||
const msgs = (err.stack || err.toString()).split(EOL, 2); | ||
console.error( | ||
["", chalk.bgRed(msgs.shift()), msgs.join(EOL), ""].join(EOL) | ||
); | ||
} | ||
on(eventName, listener) { | ||
return super.on(eventName, listener); | ||
} | ||
mount(middleware2) { | ||
if (middleware2 === null) | ||
return; | ||
if (middleware2 instanceof Chain2) { | ||
this.chainPoints.push(Chain2.createSplitPoint(middleware2)); | ||
} | ||
this.middlewareList.push(middleware2); | ||
} | ||
}; | ||
// src/middleware/body.ts | ||
@@ -813,2 +819,3 @@ import { validate, Validator as Validator2, rule, ValidatorError } from "@aomex/core"; | ||
body, | ||
createHttpServer, | ||
getMimeType, | ||
@@ -815,0 +822,0 @@ params, |
{ | ||
"name": "@aomex/web", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"description": "", | ||
@@ -29,3 +29,3 @@ "type": "module", | ||
"peerDependencies": { | ||
"@aomex/core": "^0.0.19" | ||
"@aomex/core": "^0.0.20" | ||
}, | ||
@@ -60,3 +60,3 @@ "dependencies": { | ||
"devDependencies": { | ||
"@aomex/core": "^0.0.19", | ||
"@aomex/core": "^0.0.20", | ||
"@types/co-body": "^6.1.0", | ||
@@ -63,0 +63,0 @@ "@types/content-type": "^1.1.5", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102613
1239
4