@wxn0brp/falcon-frame-plugin 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 wxn0brP
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,34 @@
1
+ import { RouteHandler } from "@wxn0brp/falcon-frame";
2
+ import { Plugin, PSOpts } from "./types.js";
3
+ export declare class PluginSystem {
4
+ plugins: Plugin[];
5
+ _sorted: boolean;
6
+ /**
7
+ * Registers a plugin with the system.
8
+ * If the opts.after or opts.before properties are provided, they will be used to set the after and before properties of the plugin respectively.
9
+ * If the plugins have already been sorted, the plugins will be re-sorted after registration.
10
+ * @param plugin The plugin to register
11
+ * @param opts Options for registering the plugin
12
+ */
13
+ register(plugin: Plugin, opts?: PSOpts): void;
14
+ /**
15
+ * Sorts the plugins using the sortPlugins function and marks them as sorted
16
+ */
17
+ _sort(): void;
18
+ /**
19
+ * Returns a RouteHandler function that executes all registered plugins in the correct order
20
+ * When called, it will first sort the plugins if they haven't been sorted yet
21
+ * Then it will return a function that takes a request, response, and next function as arguments
22
+ * This returned function will recursively execute all plugins in the correct order
23
+ * @returns A RouteHandler function
24
+ */
25
+ getRouteHandler(): RouteHandler;
26
+ /**
27
+ * Recursively executes plugins in the correct order
28
+ * @param req - Request object
29
+ * @param res - Response object
30
+ * @param next - Next function
31
+ * @param index - Current index in the execution order
32
+ */
33
+ private executePlugins;
34
+ }
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ import { sortPlugins } from "./sort/index.js";
2
+ export class PluginSystem {
3
+ plugins = [];
4
+ _sorted = false;
5
+ /**
6
+ * Registers a plugin with the system.
7
+ * If the opts.after or opts.before properties are provided, they will be used to set the after and before properties of the plugin respectively.
8
+ * If the plugins have already been sorted, the plugins will be re-sorted after registration.
9
+ * @param plugin The plugin to register
10
+ * @param opts Options for registering the plugin
11
+ */
12
+ register(plugin, opts = {}) {
13
+ if (opts.after)
14
+ plugin.after = opts.after;
15
+ if (opts.before)
16
+ plugin.before = opts.before;
17
+ this.plugins.push(plugin);
18
+ if (this._sorted)
19
+ this._sort();
20
+ }
21
+ /**
22
+ * Sorts the plugins using the sortPlugins function and marks them as sorted
23
+ */
24
+ _sort() {
25
+ this.plugins = sortPlugins(this.plugins);
26
+ this._sorted = true;
27
+ }
28
+ /**
29
+ * Returns a RouteHandler function that executes all registered plugins in the correct order
30
+ * When called, it will first sort the plugins if they haven't been sorted yet
31
+ * Then it will return a function that takes a request, response, and next function as arguments
32
+ * This returned function will recursively execute all plugins in the correct order
33
+ * @returns A RouteHandler function
34
+ */
35
+ getRouteHandler() {
36
+ this._sort();
37
+ return (req, res, next) => {
38
+ return this.executePlugins(req, res, next, 0);
39
+ };
40
+ }
41
+ /**
42
+ * Recursively executes plugins in the correct order
43
+ * @param req - Request object
44
+ * @param res - Response object
45
+ * @param next - Next function
46
+ * @param index - Current index in the execution order
47
+ */
48
+ executePlugins(req, res, next, index) {
49
+ if (index >= this.plugins.length)
50
+ return next();
51
+ this.plugins[index].process(req, res, () => {
52
+ this.executePlugins(req, res, next, index + 1);
53
+ });
54
+ }
55
+ }
@@ -0,0 +1,6 @@
1
+ import { Plugin } from "../types.js";
2
+ export interface RateLimitRecord {
3
+ count: number;
4
+ lastRequest: number;
5
+ }
6
+ export declare function createRateLimiterPlugin(maxRequests: number, windowMs: number): Plugin;
@@ -0,0 +1,24 @@
1
+ ;
2
+ export function createRateLimiterPlugin(maxRequests, windowMs) {
3
+ const rateLimitMap = new Map();
4
+ return {
5
+ id: "rateLimiter",
6
+ process: (req, res, next) => {
7
+ const ip = req.socket.remoteAddress ?? "unknown";
8
+ const now = Date.now();
9
+ const record = rateLimitMap.get(ip);
10
+ if (!record || now - record.lastRequest > windowMs) {
11
+ rateLimitMap.set(ip, { count: 1, lastRequest: now });
12
+ return next();
13
+ }
14
+ if (record.count >= maxRequests) {
15
+ res.statusCode = 429;
16
+ return res.end("Too Many Requests");
17
+ }
18
+ record.count++;
19
+ record.lastRequest = now;
20
+ rateLimitMap.set(ip, record);
21
+ next();
22
+ },
23
+ };
24
+ }
@@ -0,0 +1,2 @@
1
+ import { Plugin } from "../types.js";
2
+ export declare function createSecurityPlugin(): Plugin;
@@ -0,0 +1,14 @@
1
+ export function createSecurityPlugin() {
2
+ return {
3
+ id: "security",
4
+ process: (req, res, next) => {
5
+ res.setHeader("X-Content-Type-Options", "nosniff");
6
+ res.setHeader("X-Frame-Options", "SAMEORIGIN");
7
+ res.setHeader("Referrer-Policy", "no-referrer");
8
+ res.setHeader("X-XSS-Protection", "1; mode=block");
9
+ res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
10
+ next();
11
+ },
12
+ after: "cors",
13
+ };
14
+ }
@@ -0,0 +1,2 @@
1
+ import { Plugin } from "../types.js";
2
+ export declare function sortPlugins(plugins: Plugin[]): Plugin[];
@@ -0,0 +1,32 @@
1
+ import { sort } from "./lib.js";
2
+ export function sortPlugins(plugins) {
3
+ const edges = [];
4
+ const ids = new Set();
5
+ for (const plugin of plugins) {
6
+ if (ids.has(plugin.id)) {
7
+ throw new Error(`Duplicate plugin id: "${plugin.id}"`);
8
+ }
9
+ ids.add(plugin.id);
10
+ }
11
+ for (const plugin of plugins) {
12
+ const { id, before, after } = plugin;
13
+ const addEdge = (from, to) => {
14
+ if (from !== to && ids.has(from) && ids.has(to)) {
15
+ edges.push([from, to]);
16
+ }
17
+ };
18
+ if (before) {
19
+ const beforeList = Array.isArray(before) ? before : [before];
20
+ for (const b of beforeList)
21
+ addEdge(id, b);
22
+ }
23
+ if (after) {
24
+ const afterList = Array.isArray(after) ? after : [after];
25
+ for (const a of afterList)
26
+ addEdge(a, id);
27
+ }
28
+ }
29
+ const sortedIds = sort([...ids], edges);
30
+ const byId = new Map(plugins.map(p => [p.id, p]));
31
+ return sortedIds.map(id => byId.get(id));
32
+ }
@@ -0,0 +1,3 @@
1
+ type Edge<T> = [T, T];
2
+ export declare function sort<T>(nodes: T[], edges: Edge<T>[]): T[];
3
+ export {};
@@ -0,0 +1,72 @@
1
+ /*
2
+ * Copyright (c) 2012 by Marcel Klehr <mklehr@gmx.net>
3
+ * Modified and rewritten by wxn0brP (2025)
4
+ * Licensed under the MIT License
5
+ */
6
+ function makeOutgoingEdges(edges) {
7
+ const outgoingEdges = new Map();
8
+ for (const [from, to] of edges) {
9
+ if (!outgoingEdges.has(from)) {
10
+ outgoingEdges.set(from, new Set());
11
+ }
12
+ if (!outgoingEdges.has(to)) {
13
+ outgoingEdges.set(to, new Set());
14
+ }
15
+ outgoingEdges.get(from).add(to);
16
+ }
17
+ return outgoingEdges;
18
+ }
19
+ function makeNodesHash(nodes) {
20
+ const hash = new Map();
21
+ nodes.forEach((node, index) => {
22
+ hash.set(node, index);
23
+ });
24
+ return hash;
25
+ }
26
+ export function sort(nodes, edges) {
27
+ const cursor = { value: nodes.length };
28
+ const sorted = new Array(cursor.value);
29
+ const visited = new Set();
30
+ const outgoingEdges = makeOutgoingEdges(edges);
31
+ const nodesHash = makeNodesHash(nodes);
32
+ for (const [from, to] of edges) {
33
+ if (!nodesHash.has(from) || !nodesHash.has(to)) {
34
+ throw new Error("Unknown node. There is an unknown node in the supplied edges.");
35
+ }
36
+ }
37
+ for (let i = nodes.length - 1; i >= 0; i--) {
38
+ if (!visited.has(i)) {
39
+ visit(nodes[i], i, new Set());
40
+ }
41
+ }
42
+ return sorted;
43
+ function visit(node, index, predecessors) {
44
+ if (predecessors.has(node)) {
45
+ let nodeRep = "";
46
+ try {
47
+ nodeRep = `, node was: ${JSON.stringify(node)}`;
48
+ }
49
+ catch (e) {
50
+ nodeRep = "";
51
+ }
52
+ throw new Error(`Cyclic dependency ${nodeRep}`);
53
+ }
54
+ if (!nodesHash.has(node)) {
55
+ throw new Error(`Found unknown node. Make sure to provide all involved nodes. Unknown node: ${JSON.stringify(node)}`);
56
+ }
57
+ if (visited.has(index))
58
+ return;
59
+ visited.add(index);
60
+ const outgoing = outgoingEdges.get(node) || new Set();
61
+ const outgoingArray = Array.from(outgoing);
62
+ if (outgoingArray.length > 0) {
63
+ predecessors.add(node);
64
+ for (const child of outgoingArray) {
65
+ const childIndex = nodesHash.get(child);
66
+ visit(child, childIndex, predecessors);
67
+ }
68
+ predecessors.delete(node);
69
+ }
70
+ sorted[--cursor.value] = node;
71
+ }
72
+ }
package/dist/test.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import { RouteHandler } from "@wxn0brp/falcon-frame";
2
+ export interface Plugin {
3
+ id: string;
4
+ process: RouteHandler;
5
+ before?: string | string[];
6
+ after?: string | string[];
7
+ }
8
+ export interface PSOpts {
9
+ before?: string | string[];
10
+ after?: string | string[];
11
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@wxn0brp/falcon-frame-plugin",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "description": "",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/wxn0brP/FalconFrame-plugin.git"
10
+ },
11
+ "homepage": "https://github.com/wxn0brP/FalconFrame-plugin",
12
+ "author": "wxn0brP",
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "scripts": {
16
+ "build": "tsc && tsc-alias"
17
+ },
18
+ "devDependencies": {
19
+ "@types/bun": "*",
20
+ "@wxn0brp/falcon-frame": "^0.5.0",
21
+ "tsc-alias": "*",
22
+ "typescript": "*"
23
+ },
24
+ "peerDependencies": {
25
+ "@wxn0brp/falcon-frame": ">=0.5.0"
26
+ },
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "./*": {
37
+ "types": "./dist/*.d.ts",
38
+ "import": "./dist/*.js",
39
+ "default": "./dist/*.js"
40
+ }
41
+ }
42
+ }