From b883e0e558baed1db507afff8c26f12fb52e0b2f Mon Sep 17 00:00:00 2001 From: mathcovax Date: Mon, 22 Apr 2024 20:01:27 +0000 Subject: [PATCH] [fix] remove process var --- README.md | 4 +- duplo.d.ts | 4 ++ package-lock.json | 8 ++-- package.json | 16 ++++++-- rollup.config.mjs | 21 ++++++++++ scripts/duplo.ts | 78 ++++++++++++++++++++++++++++++++++++++ scripts/index.ts | 2 + scripts/lib/accelerator.ts | 71 ---------------------------------- scripts/lib/content.ts | 3 +- test/E2E/route/route.ts | 6 +-- 10 files changed, 128 insertions(+), 85 deletions(-) create mode 100644 duplo.d.ts create mode 100644 scripts/duplo.ts diff --git a/README.md b/README.md index b010d6c..2f23fbc 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,9 @@ const outputData = zodAccelerateSchema.parse(inputData); ## Implémentation dans duplojs ```ts import Duplo, {zod} from "@duplojs/duplojs"; -import {ZodAccelerator} from "@duplojs/zod-accelerator"; +import duploZodAccelerator from "@duplojs/zod-accelerator/duplo"; const duplo = Duplo({port: 1506, host: "localhost", environment: "DEV"}); -duplo.use(ZodAccelerator.duplojs, {DEV: true}); +duplo.use(duploZodAccelerator, {DEV: true}); ``` \ No newline at end of file diff --git a/duplo.d.ts b/duplo.d.ts new file mode 100644 index 0000000..b4fc375 --- /dev/null +++ b/duplo.d.ts @@ -0,0 +1,4 @@ +import duploZodAccelerator from "./types/duplo"; +export * from "./types/duplo"; + +export default duploZodAccelerator; diff --git a/package-lock.json b/package-lock.json index 4737a84..e882a84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.0", "license": "ISC", "dependencies": { - "@duplojs/duplojs": ">=1.1.11 <2.0.0", + "@duplojs/duplojs": ">=1.8.20 <2.0.0", "@duplojs/editor-tools": "^1.1.3 <2.0.0", "zod": "^3.22.4" }, @@ -547,9 +547,9 @@ } }, "node_modules/@duplojs/duplojs": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/@duplojs/duplojs/-/duplojs-1.1.11.tgz", - "integrity": "sha512-w2I5BujNE7FGrFrw4z7Bm11NQx/B59gDWRqRFsE6XczctSu3ZYQ7vDF24KbJP56ooS5w2t3B4p3Et7mdpky8GQ==", + "version": "1.8.20", + "resolved": "https://registry.npmjs.org/@duplojs/duplojs/-/duplojs-1.8.20.tgz", + "integrity": "sha512-CqR72IUTgzYErUhrX2nY9b6vwndcpxCEW6e69ARiG4lb8/jXF6dMorD1grc4j/CZ/lBJmnWvCR1pGwk3vZ35XA==", "dependencies": { "fast-querystring": "^1.1.2", "mime": "^3.0.0", diff --git a/package.json b/package.json index eab80e3..1e4573e 100644 --- a/package.json +++ b/package.json @@ -10,15 +10,23 @@ "json-schema", "parser", "validate", - "typecheck" + "typecheck", + "zod", + "faster", + "zod faster" ], "types": "./types/index.d.ts", "exports": { - "default": { + ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs", "types": "./types/index.d.ts" - } + }, + "./duplo": { + "types": "./duplo.d.ts", + "import": "./dist/duplo.mjs", + "require": "./dist/duplo.cjs" + } }, "files": [ "dist", @@ -35,7 +43,7 @@ "test:types": "tsc" }, "dependencies": { - "@duplojs/duplojs": ">=1.1.11 <2.0.0", + "@duplojs/duplojs": ">=1.8.20 <2.0.0", "@duplojs/editor-tools": "^1.1.3 <2.0.0", "zod": "^3.22.4" }, diff --git a/rollup.config.mjs b/rollup.config.mjs index 79200e0..8714c2e 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -24,4 +24,25 @@ export default defineConfig([ json(), ] }, + { + input: "scripts/duplo.ts", + output: [ + { + file: "dist/duplo.mjs", + format: "esm" + }, + { + file: "dist/duplo.cjs", + format: "cjs", + } + ], + plugins: [ + typescript({ + exclude: ["node_mudules"], + tsconfig: "tsconfig.json", + module: "ESNext", + }), + json(), + ] + }, ]); diff --git a/scripts/duplo.ts b/scripts/duplo.ts new file mode 100644 index 0000000..6b8e33e --- /dev/null +++ b/scripts/duplo.ts @@ -0,0 +1,78 @@ +import * as zod from "zod"; +import {AbstractRoute, DuploConfig, DuploInstance, Process, Route} from "@duplojs/duplojs"; +import {duploExtends, duploInject} from "@duplojs/editor-tools"; +import packageJson from "../package.json"; +import {ZodAccelerator} from "."; +import {ZodAcceleratorError} from "./lib/error"; + +export * from "./lib/accelerator"; +export * from "./lib/error"; +export * from "./lib/parser"; + +declare module "@duplojs/duplojs" { + interface Plugins { + "@duplojs/zod-accelerator": {version: string} + } +} + +export default function duploZodAccelerator( + instance: DuploInstance, + params: Partial< + Record< + DuploConfig["environment"], + boolean + > + > = {DEV: false, PROD: true} +){ + const environment = instance.config.environment; + + if(!params[environment]){ + return; + } + + instance.plugins["@duplojs/zod-accelerator"] = {version: packageJson.version}; + + const duploseHandler = (duplose: Route | Process | AbstractRoute) => { + if(Object.keys(duplose.extracted).length === 0){ + return; + } + + Object.entries(duplose.extracted).forEach(([key, value]) => { + if(!value){ + return; + } + + if(value instanceof zod.ZodType){ + ZodAccelerator.build(value); + } + else { + Object.entries(value).forEach(([key, deepValue]) => { + ZodAccelerator.build(deepValue); + }); + } + }); + + duploExtends(duplose, { + ZodAcceleratorError, + }); + + duploInject(duplose, ({}, d) => { + d.stringDuploseFunction = d.stringDuploseFunction.replace( + /\.parse\(request/g, + ".accelerator.parse(request" + ); + d.stringDuploseFunction = d.stringDuploseFunction.replace( + /error instanceof this\.ZodError/, + "error instanceof this.extensions.ZodAcceleratorError" + ); + }); + }; + + instance.addHook("onDeclareRoute", duploseHandler); + instance.addHook("onDeclareAbstractRoute", (abstractRoute) => { + if(abstractRoute instanceof AbstractRoute){ + duploseHandler(abstractRoute); + } + }); + instance.addHook("onCreateProcess", duploseHandler); +} diff --git a/scripts/index.ts b/scripts/index.ts index 53f46c8..f9b8a4d 100644 --- a/scripts/index.ts +++ b/scripts/index.ts @@ -33,3 +33,5 @@ import "./lib/accelerators/lazy"; export * from "./lib/accelerator"; export * from "./lib/error"; export * from "./lib/parser"; + +export {ZodAccelerator as default} from "./lib/accelerator"; diff --git a/scripts/lib/accelerator.ts b/scripts/lib/accelerator.ts index 0aa344a..785438c 100644 --- a/scripts/lib/accelerator.ts +++ b/scripts/lib/accelerator.ts @@ -1,10 +1,7 @@ import * as zod from "zod"; import {ZodAcceleratorContent} from "./content"; import {ZodAcceleratorParser} from "./parser"; -import {AbstractRoute, DuploConfig, DuploInstance, Process, Route} from "@duplojs/duplojs"; -import {duploExtends, duploInject} from "@duplojs/editor-tools"; import {ZodAcceleratorError} from "./error"; -import packageJson from "../../package.json"; declare module "zod" { interface ZodType{ @@ -12,12 +9,6 @@ declare module "zod" { } } -declare module "@duplojs/duplojs" { - interface Plugins { - "@duplojs/zod-accelerator": {version: string} - } -} - export abstract class ZodAccelerator{ protected static accelerators: ZodAccelerator[] = []; @@ -77,66 +68,4 @@ export abstract class ZodAccelerator{ public static autoInstance(zodAccelerator: { new(...args: any[]): ZodAccelerator }){ ZodAccelerator.accelerators.push(new zodAccelerator()); } - - public static duplojs( - instance: DuploInstance, - params: Partial< - Record< - DuploConfig["environment"], - boolean - > - > = {DEV: false, PROD: true} - ){ - const environment = instance.config.environment; - - if(!params[environment]){ - return; - } - - instance.plugins["@duplojs/zod-accelerator"] = {version: packageJson.version}; - - const duploseHandler = (duplose: Route | Process | AbstractRoute) => { - if(Object.keys(duplose.extracted).length === 0){ - return; - } - - Object.entries(duplose.extracted).forEach(([key, value]) => { - if(!value){ - return; - } - - if(value instanceof zod.ZodType){ - ZodAccelerator.build(value); - } - else { - Object.entries(value).forEach(([key, deepValue]) => { - ZodAccelerator.build(deepValue); - }); - } - }); - - duploExtends(duplose, { - ZodAcceleratorError, - }); - - duploInject(duplose, ({}, d) => { - d.stringDuploseFunction = d.stringDuploseFunction.replace( - /\.parse\(request/g, - ".accelerator.parse(request" - ); - d.stringDuploseFunction = d.stringDuploseFunction.replace( - /error instanceof this\.ZodError/, - "error instanceof this.extensions.ZodAcceleratorError" - ); - }); - }; - - instance.addHook("onDeclareRoute", duploseHandler); - instance.addHook("onDeclareAbstractRoute", (abstractRoute) => { - if(abstractRoute instanceof AbstractRoute){ - duploseHandler(abstractRoute); - } - }); - instance.addHook("onCreateProcess", duploseHandler); - } } diff --git a/scripts/lib/content.ts b/scripts/lib/content.ts index ca91cf4..8fd82ef 100644 --- a/scripts/lib/content.ts +++ b/scripts/lib/content.ts @@ -17,6 +17,7 @@ export class ZodAcceleratorContent{ "$this.": "this.{id}_", "$id": "{id}", }; + private static inc = 0; readonly id = ZodAcceleratorContent.generateId(); content: string[] = []; @@ -107,6 +108,6 @@ export class ZodAcceleratorContent{ } private static generateId(){ - return `duploj$${process.hrtime.bigint()}`; + return `duploj$${(this.inc++)}`; } } diff --git a/test/E2E/route/route.ts b/test/E2E/route/route.ts index 0a2a1f5..aa99938 100644 --- a/test/E2E/route/route.ts +++ b/test/E2E/route/route.ts @@ -1,10 +1,10 @@ import Duplo, {zod} from "@duplojs/duplojs"; import {parentPort} from "worker_threads"; -import {ZodAccelerator} from "../../../scripts"; +import duploZodAccelerator from "../../../scripts/duplo"; const duplo = Duplo({port: 1506, host: "localhost", environment: "DEV"}); -duplo.use(ZodAccelerator.duplojs, {DEV: true}); +duplo.use(duploZodAccelerator, {DEV: true}); duplo.declareRoute("GET", "/test/1") .extract({ @@ -20,7 +20,7 @@ duplo.declareRoute("POST", "/test/2") .extract({ body: zod.object({ test1: zod.string() - }) + }).passthrough() }) .handler(({pickup}, res) => { res.code(200).info("s").send(pickup("body"));