@swizzyweb/express-unuse 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # express-unuse
2
+
3
+ A library for removing methods from express app.
4
+
5
+ Currently supports removing routers from the express app.
6
+
7
+ Tested with express v5.2.1
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Using a submodule (recommended):
4
+ git submodule add --depth=1 https://github.com/expressjs/express.git vendor/express
5
+
6
+ # If you prefer a one-off copy instead:
7
+ # git clone --depth=1 https://github.com/expressjs/express.git /tmp/express
8
+ # mkdir -p vendor && rsync -a --delete /tmp/express/ vendor/express/ --exclude .git
@@ -0,0 +1,2 @@
1
+ export * from "./unuse.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./unuse.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
package/dist/m.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=m.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"m.d.ts","sourceRoot":"","sources":["../m.ts"],"names":[],"mappings":""}
package/dist/m.js ADDED
@@ -0,0 +1,22 @@
1
+ import express from "express";
2
+ const { Request, Router, Response } = express;
3
+ const app = express();
4
+ const router = Router();
5
+ router.get("getController", (req, res) => {
6
+ console.log("get");
7
+ });
8
+ router.post("postController", (req, res) => {
9
+ console.log("post");
10
+ });
11
+ router.put("putController", (req, res) => {
12
+ console.log("post");
13
+ });
14
+ router.patch("patchController", (req, res) => {
15
+ console.log("patch");
16
+ });
17
+ app.use("/router", router);
18
+ console.log(app);
19
+ console.log(app.router);
20
+ console.log(app.router.stack[0]);
21
+ console.log(router == app.router.stack[0]?.handle);
22
+ //# sourceMappingURL=m.js.map
package/dist/m.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"m.js","sourceRoot":"","sources":["../m.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;AAC9C,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AACtB,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;AACxB,MAAM,CAAC,GAAG,CAAC,eAAe,EAAE,CAAC,GAAY,EAAE,GAAa,EAAE,EAAE;IAC1D,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACrB,CAAC,CAAC,CAAC;AAEH,MAAM,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,GAAY,EAAE,GAAa,EAAE,EAAE;IAC5D,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC,CAAC,CAAC;AAEH,MAAM,CAAC,GAAG,CAAC,eAAe,EAAE,CAAC,GAAY,EAAE,GAAa,EAAE,EAAE;IAC1D,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC,CAAC,CAAC;AAEH,MAAM,CAAC,KAAK,CAAC,iBAAiB,EAAE,CAAC,GAAY,EAAE,GAAa,EAAE,EAAE;IAC9D,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AAE3B,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAEjB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACxB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACjC,OAAO,CAAC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./unuse.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./unuse.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { Application, Router, Request, Response } from "express";
2
+ export declare function unuseRouter(app: Application, router: Router): number | undefined;
3
+ export declare function unuseController(app: Application, controller: ((req: Request, res: Response) => void) | number): void;
4
+ //# sourceMappingURL=unuse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unuse.d.ts","sourceRoot":"","sources":["../../src/unuse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEtE,wBAAgB,WAAW,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,sBAY3D;AAED,wBAAgB,eAAe,CAC7B,GAAG,EAAE,WAAW,EAChB,UAAU,EAAE,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,KAAK,IAAI,CAAC,GAAG,MAAM,QAkB7D"}
@@ -0,0 +1,28 @@
1
+ export function unuseRouter(app, router) {
2
+ let index = 0;
3
+ for (const stack of app.router?.stack) {
4
+ const { handle } = stack;
5
+ console.error(handle);
6
+ if (handle == router) {
7
+ app.router.stack.splice(index, 1);
8
+ return index;
9
+ }
10
+ index++;
11
+ }
12
+ }
13
+ export function unuseController(app, controller) {
14
+ let index = 0;
15
+ if (typeof controller === "number") {
16
+ app.router.stack.splice(controller);
17
+ return;
18
+ }
19
+ for (const stack of app.router?.stack) {
20
+ const { handle } = stack;
21
+ if (handle == controller) {
22
+ app.router.stack.splice(index, 1);
23
+ return;
24
+ }
25
+ index++;
26
+ }
27
+ }
28
+ //# sourceMappingURL=unuse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unuse.js","sourceRoot":"","sources":["../../src/unuse.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,WAAW,CAAC,GAAgB,EAAE,MAAc;IAC1D,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,KAAK,MAAM,KAAK,IAAI,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;QACtC,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;QACzB,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACtB,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;YACrB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAClC,OAAO,KAAK,CAAC;QACf,CAAC;QACD,KAAK,EAAE,CAAC;IACV,CAAC;AACH,CAAC;AAED,MAAM,UAAU,eAAe,CAC7B,GAAgB,EAChB,UAA4D;IAE5D,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACnC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACpC,OAAO;IACT,CAAC;IAED,KAAK,MAAM,KAAK,IAAI,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;QACtC,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;QAEzB,IAAI,MAAM,IAAI,UAAU,EAAE,CAAC;YACzB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAClC,OAAO;QACT,CAAC;QAED,KAAK,EAAE,CAAC;IACV,CAAC;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Application, Router } from "express";
2
+ export declare function unuseRouter(app: Application, router: Router): void;
3
+ //# sourceMappingURL=unuse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unuse.d.ts","sourceRoot":"","sources":["../src/unuse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEnD,wBAAgB,WAAW,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,QAY3D"}
package/dist/unuse.js ADDED
@@ -0,0 +1,11 @@
1
+ export function unuseRouter(app, router) {
2
+ let index = 0;
3
+ for (const stack of app.router?.stack) {
4
+ const { handle } = stack;
5
+ if (handle === router) {
6
+ app.router.stack.splice(index, 1);
7
+ }
8
+ index++;
9
+ }
10
+ }
11
+ //# sourceMappingURL=unuse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unuse.js","sourceRoot":"","sources":["../src/unuse.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,WAAW,CAAC,GAAgB,EAAE,MAAc;IAC1D,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,KAAK,MAAM,KAAK,IAAI,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;QACtC,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;QAEzB,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;YACtB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QACpC,CAAC;QAED,KAAK,EAAE,CAAC;IACV,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@swizzyweb/express-unuse",
3
+ "version": "1.0.0",
4
+ "description": "Express wrapper package adding unuse functionality for SwizzyWeb.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "type": "module",
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "test": "bun --test test",
12
+ "test:ci": "npm tun test:cov",
13
+ "test:cov": "bun test test --coverage"
14
+ },
15
+ "keywords": [
16
+ "SwizzyWeb",
17
+ "Express",
18
+ "Unuse"
19
+ ],
20
+ "author": "Jason Gallagher",
21
+ "license": "MIT",
22
+ "dependencies": {
23
+ "express": "^5.2.1"
24
+ },
25
+ "devDependencies": {
26
+ "@types/bun": "^1.3.8",
27
+ "@types/express": "^5.0.6",
28
+ "bun": "^1.3.8",
29
+ "source-map-support": "^0.5.21",
30
+ "typescript": "^5.9.3"
31
+ }
32
+ }
package/patch_tests.sh ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+ find vendor/express/test -type f -name "*.js" \
3
+ -exec sed -i '1i\
4
+ require(process.cwd() + "../../../dist/unuse-patch.js").patchExpressModule(require("express"));\
5
+ ' {} +
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./unuse.js";
package/src/unuse.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { Application, Router } from "express";
2
+
3
+ export function unuseRouter(app: Application, router: Router) {
4
+ let index = 0;
5
+
6
+ for (const stack of app.router?.stack) {
7
+ const { handle } = stack;
8
+
9
+ if (handle === router) {
10
+ app.router.stack.splice(index, 1);
11
+ }
12
+
13
+ index++;
14
+ }
15
+ }
@@ -0,0 +1,171 @@
1
+ import { test, expect } from "bun:test";
2
+ import { unuseRouter } from "../src/unuse.ts";
3
+ import express, { Router, Request, Response } from "express";
4
+
5
+ test("Test unuse 1 router with 1 router", () => {
6
+ const app = express();
7
+ const router = Router();
8
+ router.get("getController", (req: Request, res: Response) => {
9
+ console.log("get");
10
+ });
11
+
12
+ router.post("postController", (req: Request, res: Response) => {
13
+ console.log("post");
14
+ });
15
+
16
+ router.put("putController", (req: Request, res: Response) => {
17
+ console.log("post");
18
+ });
19
+
20
+ router.patch("patchController", (req: Request, res: Response) => {
21
+ console.log("patch");
22
+ });
23
+
24
+ app.use(router);
25
+
26
+ expect(app.router.stack.length === 1);
27
+ unuseRouter(app, router);
28
+ expect(app.router.stack.length === 0);
29
+ });
30
+
31
+ test("Test unuse 1 router with 2 routers", () => {
32
+ const app = express();
33
+ const router = Router();
34
+ router.get("getController", (req: Request, res: Response) => {
35
+ console.log("get");
36
+ });
37
+
38
+ router.post("postController", (req: Request, res: Response) => {
39
+ console.log("post");
40
+ });
41
+
42
+ router.put("putController", (req: Request, res: Response) => {
43
+ console.log("post");
44
+ });
45
+
46
+ router.patch("patchController", (req: Request, res: Response) => {
47
+ console.log("patch");
48
+ });
49
+
50
+ app.use(router);
51
+ const router2 = Router();
52
+ router2.get("getController2", (req: Request, res: Response) => {
53
+ console.log("get");
54
+ });
55
+
56
+ router2.post("postController2", (req: Request, res: Response) => {
57
+ console.log("post");
58
+ });
59
+
60
+ router2.put("putController2", (req: Request, res: Response) => {
61
+ console.log("post");
62
+ });
63
+
64
+ router2.patch("patchController2", (req: Request, res: Response) => {
65
+ console.log("patch");
66
+ });
67
+
68
+ app.use(router);
69
+ app.use(router2);
70
+ expect(app.router.stack.length === 2);
71
+ unuseRouter(app, router);
72
+ expect(app.router.stack.length === 1);
73
+ });
74
+
75
+ test("Test unuse 2 routers with 2 routers", () => {
76
+ const app = express();
77
+ const router = Router();
78
+ router.get("getController", (req: Request, res: Response) => {
79
+ console.log("get");
80
+ });
81
+
82
+ router.post("postController", (req: Request, res: Response) => {
83
+ console.log("post");
84
+ });
85
+
86
+ router.put("putController", (req: Request, res: Response) => {
87
+ console.log("post");
88
+ });
89
+
90
+ router.patch("patchController", (req: Request, res: Response) => {
91
+ console.log("patch");
92
+ });
93
+
94
+ app.use(router);
95
+ const router2 = Router();
96
+ router2.get("getController2", (req: Request, res: Response) => {
97
+ console.log("get");
98
+ });
99
+
100
+ router2.post("postController2", (req: Request, res: Response) => {
101
+ console.log("post");
102
+ });
103
+
104
+ router2.put("putController2", (req: Request, res: Response) => {
105
+ console.log("post");
106
+ });
107
+
108
+ router2.patch("patchController2", (req: Request, res: Response) => {
109
+ console.log("patch");
110
+ });
111
+
112
+ app.use(router);
113
+ app.use(router2);
114
+ expect(app.router.stack.length === 2);
115
+ unuseRouter(app, router2);
116
+ expect(app.router.stack.length === 1);
117
+ unuseRouter(app, router);
118
+ expect(app.router.stack.length === 0);
119
+ });
120
+
121
+ test("Test unuse 2 routers with 2 routers and add 2 back", () => {
122
+ const app = express();
123
+ const router = Router();
124
+ router.get("getController", (req: Request, res: Response) => {
125
+ console.log("get");
126
+ });
127
+
128
+ router.post("postController", (req: Request, res: Response) => {
129
+ console.log("post");
130
+ });
131
+
132
+ router.put("putController", (req: Request, res: Response) => {
133
+ console.log("post");
134
+ });
135
+
136
+ router.patch("patchController", (req: Request, res: Response) => {
137
+ console.log("patch");
138
+ });
139
+
140
+ app.use(router);
141
+ const router2 = Router();
142
+ router2.get("getController2", (req: Request, res: Response) => {
143
+ console.log("get");
144
+ });
145
+
146
+ router2.post("postController2", (req: Request, res: Response) => {
147
+ console.log("post");
148
+ });
149
+
150
+ router2.put("putController2", (req: Request, res: Response) => {
151
+ console.log("post");
152
+ });
153
+
154
+ router2.patch("patchController2", (req: Request, res: Response) => {
155
+ console.log("patch");
156
+ });
157
+
158
+ app.use(router);
159
+ app.use(router2);
160
+ expect(app.router.stack.length === 2);
161
+
162
+ unuseRouter(app, router2);
163
+ expect(app.router.stack.length === 1);
164
+
165
+ unuseRouter(app, router);
166
+ expect(app.router.stack.length === 0);
167
+
168
+ app.use(router);
169
+ app.use(router2);
170
+ expect(app.router.stack.length === 2);
171
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ // "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "nodenext",
11
+ "target": "esnext",
12
+ // For nodejs:
13
+ "lib": ["esnext"],
14
+ "types": ["node"],
15
+ // and npm install -D @types/node
16
+
17
+ // Other Outputs
18
+ "sourceMap": true,
19
+ "declaration": true,
20
+ "declarationMap": true,
21
+
22
+ // Stricter Typechecking Options
23
+ "noUncheckedIndexedAccess": true,
24
+ "exactOptionalPropertyTypes": true,
25
+
26
+ // Style Options
27
+ // "noImplicitReturns": true,
28
+ // "noImplicitOverride": true,
29
+ // "noUnusedLocals": true,
30
+ // "noUnusedParameters": true,
31
+ // "noFallthroughCasesInSwitch": true,
32
+ // "noPropertyAccessFromIndexSignature": true,
33
+
34
+ // Recommended Options
35
+ "strict": true,
36
+ "jsx": "react-jsx",
37
+ "verbatimModuleSyntax": true,
38
+ "isolatedModules": true,
39
+ "noUncheckedSideEffectImports": true,
40
+ "moduleDetection": "force",
41
+ "skipLibCheck": true
42
+ },
43
+ "exclude": ["test", "dist"]
44
+ }
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+ find vendor/express/test -type f -name "*.js" \
3
+ -exec sed -i '/process\.cwd\(\).*unuse-patch\.js.*patchExpressModule(require("express"))/d' {} +