@zenweb/body
Advanced tools
+1
-2
@@ -1,4 +0,3 @@ | ||
| /// <reference types="node" /> | ||
| import { HelperBase } from '@zenweb/helper'; | ||
| import { BodyParser } from './parse'; | ||
| import { BodyParser } from './parse.js'; | ||
| /** | ||
@@ -5,0 +4,0 @@ * 原始请求内容-经过解压,未经过文字编码转换 |
+55
-43
@@ -1,2 +0,1 @@ | ||
| "use strict"; | ||
| var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -12,12 +11,10 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
| var _a, _b, _c, _d; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.BodyHelper = exports.ObjectBody = exports.Body = exports.TextBody = exports.RawBody = void 0; | ||
| const core_1 = require("@zenweb/core"); | ||
| const inject_1 = require("@zenweb/inject"); | ||
| const helper_1 = require("@zenweb/helper"); | ||
| const iconv = require("iconv-lite"); | ||
| const httpError = require("http-errors"); | ||
| const types_1 = require("./types"); | ||
| const read_1 = require("./read"); | ||
| const parse_1 = require("./parse"); | ||
| import { Context } from '@zenweb/core'; | ||
| import { init, component } from '@zenweb/inject'; | ||
| import { HelperBase } from '@zenweb/helper'; | ||
| import iconv from 'iconv-lite'; | ||
| import httpError from 'http-errors'; | ||
| import { BodyOption } from './types.js'; | ||
| import { streamReader } from './read.js'; | ||
| import { RawBodyParser, TextBodyParser, USE_BODY_PARSER } from './parse.js'; | ||
| /** | ||
@@ -27,2 +24,3 @@ * 原始请求内容-经过解压,未经过文字编码转换 | ||
| let RawBody = class RawBody { | ||
| data; | ||
| async [_a = Symbol()](option, ctx) { | ||
@@ -32,15 +30,15 @@ if (!ctx.request.length) { | ||
| } | ||
| this.data = await (0, read_1.streamReader)(ctx, option.limit, option.inflate); | ||
| this.data = await streamReader(ctx, option.limit, option.inflate); | ||
| } | ||
| }; | ||
| exports.RawBody = RawBody; | ||
| __decorate([ | ||
| inject_1.init, | ||
| init, | ||
| __metadata("design:type", Function), | ||
| __metadata("design:paramtypes", [types_1.BodyOption, core_1.Context]), | ||
| __metadata("design:paramtypes", [BodyOption, Context]), | ||
| __metadata("design:returntype", Promise) | ||
| ], RawBody.prototype, _a, null); | ||
| exports.RawBody = RawBody = __decorate([ | ||
| (0, inject_1.component)('request') | ||
| RawBody = __decorate([ | ||
| component('request') | ||
| ], RawBody); | ||
| export { RawBody }; | ||
| /** | ||
@@ -50,7 +48,7 @@ * 文本内容,经过文字编码转换 | ||
| let TextBody = class TextBody { | ||
| data; | ||
| // 再解析数据 | ||
| async [_b = Symbol()](option, ctx) { | ||
| var _e; | ||
| // 是否是支持的类型 | ||
| if (!((_e = option.textTypes) === null || _e === void 0 ? void 0 : _e.length) || !ctx.is(option.textTypes)) { | ||
| if (!option.textTypes?.length || !ctx.is(option.textTypes)) { | ||
| // throw httpError(415, 'unsupported type "' + ctx.request.type + '"', { | ||
@@ -86,12 +84,12 @@ // type: 'type.unsupported', | ||
| }; | ||
| exports.TextBody = TextBody; | ||
| __decorate([ | ||
| inject_1.init, | ||
| init, | ||
| __metadata("design:type", Function), | ||
| __metadata("design:paramtypes", [types_1.BodyOption, core_1.Context]), | ||
| __metadata("design:paramtypes", [BodyOption, Context]), | ||
| __metadata("design:returntype", Promise) | ||
| ], TextBody.prototype, _b, null); | ||
| exports.TextBody = TextBody = __decorate([ | ||
| (0, inject_1.component)('request') | ||
| TextBody = __decorate([ | ||
| component('request') | ||
| ], TextBody); | ||
| export { TextBody }; | ||
| /** | ||
@@ -101,6 +99,20 @@ * 请求 Body 数据解析 - 混合解析器 | ||
| let Body = class Body { | ||
| /** | ||
| * 解析出的数据 | ||
| */ | ||
| data; | ||
| /** | ||
| * 数据类型 | ||
| * - 如果匹配解析器中支持的类型则返回匹配的类型 | ||
| * - 如果没有匹配的解析器,但是匹配了 `textTypes` 则统一为 `text` 类型 | ||
| */ | ||
| type; | ||
| /** | ||
| * 匹配的解析器 | ||
| */ | ||
| parser; | ||
| async [_c = Symbol()](opt, ctx) { | ||
| // 使用指定解析器 | ||
| if (ctx[parse_1.USE_BODY_PARSER]) { | ||
| this.parser = await ctx.injector.getInstance(ctx[parse_1.USE_BODY_PARSER]); | ||
| if (ctx[USE_BODY_PARSER]) { | ||
| this.parser = await ctx.injector.getInstance(ctx[USE_BODY_PARSER]); | ||
| } | ||
@@ -120,3 +132,3 @@ // 匹配内容解析器 | ||
| if (this.parser) { | ||
| if (this.parser instanceof parse_1.TextBodyParser) { | ||
| if (this.parser instanceof TextBodyParser) { | ||
| const textBody = await ctx.injector.getInstance(TextBody); | ||
@@ -127,3 +139,3 @@ if (textBody.data) { | ||
| } | ||
| else if (this.parser instanceof parse_1.RawBodyParser) { | ||
| else if (this.parser instanceof RawBodyParser) { | ||
| const rawBody = await ctx.injector.getInstance(RawBody); | ||
@@ -151,12 +163,12 @@ if (rawBody.data) { | ||
| }; | ||
| exports.Body = Body; | ||
| __decorate([ | ||
| inject_1.init, | ||
| init, | ||
| __metadata("design:type", Function), | ||
| __metadata("design:paramtypes", [types_1.BodyOption, core_1.Context]), | ||
| __metadata("design:paramtypes", [BodyOption, Context]), | ||
| __metadata("design:returntype", Promise) | ||
| ], Body.prototype, _c, null); | ||
| exports.Body = Body = __decorate([ | ||
| (0, inject_1.component)('request') | ||
| Body = __decorate([ | ||
| component('request') | ||
| ], Body); | ||
| export { Body }; | ||
| /** | ||
@@ -168,6 +180,5 @@ * 请求 Body 数据解析为对象本身,请求内容必须为 json 或 form-urlencoded | ||
| async [_d = Symbol()](body) { | ||
| var _e; | ||
| if (!body.data) | ||
| return; | ||
| if (!((_e = body.parser) === null || _e === void 0 ? void 0 : _e.objected)) { | ||
| if (!body.parser?.objected) { | ||
| throw httpError(400, 'only supports objected format: JSON or urlencoded', { | ||
@@ -180,5 +191,4 @@ type: 'objected.only', | ||
| }; | ||
| exports.ObjectBody = ObjectBody; | ||
| __decorate([ | ||
| inject_1.init, | ||
| init, | ||
| __metadata("design:type", Function), | ||
@@ -188,9 +198,11 @@ __metadata("design:paramtypes", [Body]), | ||
| ], ObjectBody.prototype, _d, null); | ||
| exports.ObjectBody = ObjectBody = __decorate([ | ||
| (0, inject_1.component)('request') | ||
| ObjectBody = __decorate([ | ||
| component('request') | ||
| ], ObjectBody); | ||
| export { ObjectBody }; | ||
| /** | ||
| * ObjectBody 数据类型转换与校验 | ||
| */ | ||
| let BodyHelper = class BodyHelper extends helper_1.HelperBase { | ||
| let BodyHelper = class BodyHelper extends HelperBase { | ||
| input; | ||
| constructor(input) { | ||
@@ -201,6 +213,6 @@ super(); | ||
| }; | ||
| exports.BodyHelper = BodyHelper; | ||
| exports.BodyHelper = BodyHelper = __decorate([ | ||
| (0, inject_1.component)('request'), | ||
| BodyHelper = __decorate([ | ||
| component('request'), | ||
| __metadata("design:paramtypes", [ObjectBody]) | ||
| ], BodyHelper); | ||
| export { BodyHelper }; |
+2
-3
@@ -1,3 +0,2 @@ | ||
| /// <reference types="node" /> | ||
| import { ObjectBody } from "./body"; | ||
| import { ObjectBody } from "./body.js"; | ||
| /** | ||
@@ -22,3 +21,3 @@ * 取得当前请求原始请求内容 | ||
| get<O extends import("@zenweb/helper").PickOption>(fields: O): Promise<import("@zenweb/helper").PickReturnType<O>>; | ||
| page(opt?: import("@zenweb/helper").PageOption | undefined): Promise<import("@zenweb/helper").PageResultWithOption>; | ||
| page(opt?: import("@zenweb/helper").PageOption): Promise<import("@zenweb/helper").PageResultWithOption>; | ||
| }; |
+10
-16
@@ -1,7 +0,4 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.$body = exports.$getObjectBody = exports.$getTextBody = exports.$getRawBody = void 0; | ||
| const inject_1 = require("@zenweb/inject"); | ||
| const helper_1 = require("@zenweb/helper"); | ||
| const body_1 = require("./body"); | ||
| import { $getInstance } from "@zenweb/inject"; | ||
| import { $helperBase } from "@zenweb/helper"; | ||
| import { BodyHelper, ObjectBody, RawBody, TextBody } from "./body.js"; | ||
| /** | ||
@@ -11,25 +8,22 @@ * 取得当前请求原始请求内容 | ||
| */ | ||
| async function $getRawBody() { | ||
| const ins = await (0, inject_1.$getInstance)(body_1.RawBody); | ||
| export async function $getRawBody() { | ||
| const ins = await $getInstance(RawBody); | ||
| return ins.data; | ||
| } | ||
| exports.$getRawBody = $getRawBody; | ||
| /** | ||
| * 取得当前请求文本内容,经过文字编码转换 | ||
| */ | ||
| async function $getTextBody() { | ||
| const ins = await (0, inject_1.$getInstance)(body_1.TextBody); | ||
| export async function $getTextBody() { | ||
| const ins = await $getInstance(TextBody); | ||
| return ins.data; | ||
| } | ||
| exports.$getTextBody = $getTextBody; | ||
| /** | ||
| * 取得当前请求数据对象 | ||
| */ | ||
| function $getObjectBody() { | ||
| return (0, inject_1.$getInstance)(body_1.ObjectBody); | ||
| export function $getObjectBody() { | ||
| return $getInstance(ObjectBody); | ||
| } | ||
| exports.$getObjectBody = $getObjectBody; | ||
| /** | ||
| * BodyHelper 对象 | ||
| */ | ||
| exports.$body = (0, helper_1.$helperBase)(body_1.BodyHelper); | ||
| export const $body = $helperBase(BodyHelper); |
+5
-5
| import { SetupFunction } from '@zenweb/core'; | ||
| import { BodyOption } from './types'; | ||
| export * from './types'; | ||
| export * from './body'; | ||
| export * from './parse'; | ||
| export * from './global'; | ||
| import { BodyOption } from './types.js'; | ||
| export * from './types.js'; | ||
| export * from './body.js'; | ||
| export * from './parse.js'; | ||
| export * from './global.js'; | ||
| export default function setup(opt?: BodyOption): SetupFunction; |
+12
-29
@@ -1,23 +0,7 @@ | ||
| "use strict"; | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const parse_1 = require("./parse"); | ||
| const types_1 = require("./types"); | ||
| __exportStar(require("./types"), exports); | ||
| __exportStar(require("./body"), exports); | ||
| __exportStar(require("./parse"), exports); | ||
| __exportStar(require("./global"), exports); | ||
| import { JSONParser, RawBodyParser, TextBodyParser, URLEncodedParser } from './parse.js'; | ||
| import { BodyOption } from './types.js'; | ||
| export * from './types.js'; | ||
| export * from './body.js'; | ||
| export * from './parse.js'; | ||
| export * from './global.js'; | ||
| const defaultOption = { | ||
@@ -28,5 +12,5 @@ encoding: 'utf-8', | ||
| textTypes: ['text/*', 'xml', '+xml'], | ||
| parses: [parse_1.JSONParser, parse_1.URLEncodedParser], | ||
| parses: [JSONParser, URLEncodedParser], | ||
| }; | ||
| function setup(opt) { | ||
| export default function setup(opt) { | ||
| opt = Object.assign({}, defaultOption, opt); | ||
@@ -37,7 +21,7 @@ return function body(setup) { | ||
| setup.assertModuleExists('helper', 'need setup @zenweb/inject'); | ||
| setup.core.injector.define(types_1.BodyOption, opt); | ||
| setup.core.injector.define(BodyOption, opt); | ||
| // 在初始化后期执行解析器载入工作,方便其他模块添加解析器 | ||
| setup.after(async () => { | ||
| // 载入解析器 | ||
| if (opt === null || opt === void 0 ? void 0 : opt.parses) { | ||
| if (opt?.parses) { | ||
| if (!opt.textTypes) | ||
@@ -48,3 +32,3 @@ opt.textTypes = []; | ||
| const parser = await setup.core.injector.getInstance(parserClass); | ||
| if (parser instanceof parse_1.TextBodyParser) { | ||
| if (parser instanceof TextBodyParser) { | ||
| setup.debug('add textTypes: %o', parser.types); | ||
@@ -57,3 +41,3 @@ for (const type of parser.types) { | ||
| } | ||
| else if (parser instanceof parse_1.RawBodyParser) { | ||
| else if (parser instanceof RawBodyParser) { | ||
| } | ||
@@ -69,2 +53,1 @@ else { | ||
| } | ||
| exports.default = setup; |
+2
-4
@@ -1,5 +0,3 @@ | ||
| /// <reference types="node" /> | ||
| /// <reference types="node" /> | ||
| import * as querystring from 'querystring'; | ||
| import { BodyParserClass } from './types'; | ||
| import querystring from 'node:querystring'; | ||
| import { BodyParserClass } from './types.js'; | ||
| import { Middleware } from '@zenweb/core'; | ||
@@ -6,0 +4,0 @@ /** |
+17
-31
@@ -1,2 +0,1 @@ | ||
| "use strict"; | ||
| var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -8,7 +7,5 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.useBodyParser = exports.USE_BODY_PARSER = exports.URLEncodedParser = exports.JSONParser = exports.TextBodyParser = exports.RawBodyParser = exports.BodyParser = void 0; | ||
| const inject_1 = require("@zenweb/inject"); | ||
| const httpError = require("http-errors"); | ||
| const querystring = require("querystring"); | ||
| import { component } from '@zenweb/inject'; | ||
| import httpError from 'http-errors'; | ||
| import querystring from 'node:querystring'; | ||
| /** | ||
@@ -25,18 +22,16 @@ * 请求数据解析器基类 | ||
| }; | ||
| exports.BodyParser = BodyParser; | ||
| exports.BodyParser = BodyParser = __decorate([ | ||
| (0, inject_1.component)('singleton') | ||
| BodyParser = __decorate([ | ||
| component('singleton') | ||
| ], BodyParser); | ||
| export { BodyParser }; | ||
| /** | ||
| * 原始数据解析 | ||
| */ | ||
| class RawBodyParser extends BodyParser { | ||
| export class RawBodyParser extends BodyParser { | ||
| } | ||
| exports.RawBodyParser = RawBodyParser; | ||
| /** | ||
| * 文本数据解析 - 经过编码转换后的文本数据 | ||
| */ | ||
| class TextBodyParser extends BodyParser { | ||
| export class TextBodyParser extends BodyParser { | ||
| } | ||
| exports.TextBodyParser = TextBodyParser; | ||
| // Allowed whitespace is defined in RFC 7159 | ||
@@ -48,8 +43,5 @@ // http://www.rfc-editor.org/rfc/rfc7159.txt | ||
| */ | ||
| class JSONParser extends TextBodyParser { | ||
| constructor() { | ||
| super(...arguments); | ||
| this.objected = true; | ||
| this.types = ['json', '+json']; | ||
| } | ||
| export class JSONParser extends TextBodyParser { | ||
| objected = true; | ||
| types = ['json', '+json']; | ||
| parse(text) { | ||
@@ -71,12 +63,8 @@ if (!strictJSONReg.test(text)) { | ||
| } | ||
| exports.JSONParser = JSONParser; | ||
| /** | ||
| * form-urlencoded 表单解析 | ||
| */ | ||
| class URLEncodedParser extends TextBodyParser { | ||
| constructor() { | ||
| super(...arguments); | ||
| this.objected = true; | ||
| this.types = ['urlencoded']; | ||
| } | ||
| export class URLEncodedParser extends TextBodyParser { | ||
| objected = true; | ||
| types = ['urlencoded']; | ||
| parse(text) { | ||
@@ -86,4 +74,3 @@ return querystring.parse(text); | ||
| } | ||
| exports.URLEncodedParser = URLEncodedParser; | ||
| exports.USE_BODY_PARSER = Symbol(); | ||
| export const USE_BODY_PARSER = Symbol(); | ||
| /** | ||
@@ -93,8 +80,7 @@ * 使用指定的解析器 | ||
| */ | ||
| function useBodyParser(parserClass) { | ||
| export function useBodyParser(parserClass) { | ||
| return function useBodyParserMiddleware(ctx, next) { | ||
| ctx[exports.USE_BODY_PARSER] = parserClass; | ||
| ctx[USE_BODY_PARSER] = parserClass; | ||
| return next(); | ||
| }; | ||
| } | ||
| exports.useBodyParser = useBodyParser; |
+0
-1
@@ -1,2 +0,1 @@ | ||
| /// <reference types="node" /> | ||
| import { Context } from '@zenweb/core'; | ||
@@ -3,0 +2,0 @@ /** |
+3
-7
@@ -1,6 +0,3 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.streamReader = void 0; | ||
| const httpError = require("http-errors"); | ||
| const zlib = require("zlib"); | ||
| import httpError from 'http-errors'; | ||
| import zlib from 'node:zlib'; | ||
| /** | ||
@@ -11,3 +8,3 @@ * 内容读取流 | ||
| */ | ||
| function streamReader(ctx, limit, inflate = true) { | ||
| export function streamReader(ctx, limit, inflate = true) { | ||
| // 长度检查 | ||
@@ -133,2 +130,1 @@ const length = ctx.request.length; | ||
| } | ||
| exports.streamReader = streamReader; |
+1
-1
@@ -1,2 +0,2 @@ | ||
| import { BodyParser } from './parse'; | ||
| import { BodyParser } from './parse.js'; | ||
| export interface BodyParserClass { | ||
@@ -3,0 +3,0 @@ new (): BodyParser; |
+4
-7
@@ -1,2 +0,1 @@ | ||
| "use strict"; | ||
| var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -8,5 +7,3 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.BodyOption = void 0; | ||
| const inject_1 = require("@zenweb/inject"); | ||
| import { component } from '@zenweb/inject'; | ||
| /** | ||
@@ -17,5 +14,5 @@ * Body 解析配置 | ||
| }; | ||
| exports.BodyOption = BodyOption; | ||
| exports.BodyOption = BodyOption = __decorate([ | ||
| (0, inject_1.component)('singleton') | ||
| BodyOption = __decorate([ | ||
| component('singleton') | ||
| ], BodyOption); | ||
| export { BodyOption }; |
+10
-12
| { | ||
| "name": "@zenweb/body", | ||
| "packageManager": "yarn@4.0.2", | ||
| "version": "5.0.0", | ||
| "type": "module", | ||
| "version": "5.1.0", | ||
| "description": "Zenweb Body module", | ||
@@ -9,7 +9,5 @@ "exports": "./dist/index.js", | ||
| "scripts": { | ||
| "vscode": "yarn dlx @yarnpkg/sdks vscode", | ||
| "build": "rimraf dist && tsc", | ||
| "prepack": "yarn run build", | ||
| "bench": "cd example && ts-node app", | ||
| "dev": "cd example && cross-env DEBUG=\\* ts-node app" | ||
| "prepack": "npm run build", | ||
| "dev": "cd example && cross-env DEBUG=\\* node --loader ts-node/esm app.ts" | ||
| }, | ||
@@ -37,13 +35,13 @@ "files": [ | ||
| "@types/node": "^20.10.6", | ||
| "@zenweb/messagecode": "^4.0.1", | ||
| "@zenweb/result": "^4.1.3", | ||
| "@zenweb/messagecode": "^5.1.0", | ||
| "@zenweb/result": "^5.0.0", | ||
| "cross-env": "^7.0.3", | ||
| "rimraf": "^4.4.0", | ||
| "ts-node": "^10.9.1", | ||
| "typescript": "~5.3.3" | ||
| "typescript": "^5.6.3" | ||
| }, | ||
| "dependencies": { | ||
| "@zenweb/core": "^5.0.0", | ||
| "@zenweb/helper": "^5.0.0", | ||
| "@zenweb/inject": "^5.0.0", | ||
| "@zenweb/core": "^5.1.0", | ||
| "@zenweb/helper": "^5.1.0", | ||
| "@zenweb/inject": "^5.1.0", | ||
| "http-errors": "^2.0.0", | ||
@@ -50,0 +48,0 @@ "iconv-lite": "^0.6.3" |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Yes
NaN24787
-9.23%698
-5.03%Updated
Updated
Updated