securequ 1.1.8 → 1.1.10
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/client/Base.d.ts +18 -18
- package/client/Base.js +146 -146
- package/client/Base.js.map +1 -1
- package/client/Base.mjs +146 -146
- package/client/Base.mjs.map +1 -1
- package/client/index.d.ts +8 -8
- package/client/index.js +138 -138
- package/client/index.js.map +1 -1
- package/client/index.mjs +138 -138
- package/client/index.mjs.map +1 -1
- package/client/types.d.ts +34 -34
- package/include/File.js +59 -59
- package/include/File.js.map +1 -1
- package/include/File.mjs +59 -59
- package/include/File.mjs.map +1 -1
- package/include/FileScaner.js +94 -94
- package/include/FileScaner.js.map +1 -1
- package/include/FileScaner.mjs +94 -94
- package/include/FileScaner.mjs.map +1 -1
- package/include/compress.d.ts +9 -9
- package/include/compress.js +18 -18
- package/include/compress.js.map +1 -1
- package/include/compress.mjs +18 -18
- package/include/compress.mjs.map +1 -1
- package/include/crypto.d.ts +15 -15
- package/include/crypto.js +54 -54
- package/include/crypto.js.map +1 -1
- package/include/crypto.mjs +54 -54
- package/include/crypto.mjs.map +1 -1
- package/package.json +1 -1
- package/readme.md +312 -312
- package/server/Base.d.ts +20 -20
- package/server/Base.js +105 -105
- package/server/Base.js.map +1 -1
- package/server/Base.mjs +105 -105
- package/server/Base.mjs.map +1 -1
- package/server/Router.d.ts +7 -7
- package/server/Router.js +29 -29
- package/server/Router.js.map +1 -1
- package/server/Router.mjs +29 -29
- package/server/Router.mjs.map +1 -1
- package/server/index.d.ts +5 -5
- package/server/index.js +187 -181
- package/server/index.js.map +1 -1
- package/server/index.mjs +187 -181
- package/server/index.mjs.map +1 -1
- package/server/types.d.ts +72 -72
package/server/Base.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import Router from './Router.js';
|
|
2
2
|
import { SecurequServerConfig, UploadFileMeta, HandlerInfo, Metadata } from './types.js';
|
|
3
3
|
|
|
4
|
-
declare class SecurequServerBase extends Router {
|
|
5
|
-
protected config: SecurequServerConfig;
|
|
6
|
-
protected secret: string | null;
|
|
7
|
-
protected clients: Map<string, string>;
|
|
8
|
-
protected uploadMeta: Map<string, UploadFileMeta & {
|
|
9
|
-
expire: number;
|
|
10
|
-
}>;
|
|
11
|
-
readonly CONTENT_TYPE = "application/octet-stream";
|
|
12
|
-
constructor(config: SecurequServerConfig);
|
|
13
|
-
protected getSecret(): Promise<string>;
|
|
14
|
-
protected clientInfo(path: string, origin: string): Promise<{
|
|
15
|
-
path: string;
|
|
16
|
-
secret: string;
|
|
17
|
-
hash: string;
|
|
18
|
-
searchParams: {
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
};
|
|
21
|
-
}>;
|
|
22
|
-
protected isValidSigneture(signeture: string | undefined, hash: string): Promise<void>;
|
|
23
|
-
handleRequest(info: HandlerInfo, metadata?: Metadata): Promise<void>;
|
|
4
|
+
declare class SecurequServerBase extends Router {
|
|
5
|
+
protected config: SecurequServerConfig;
|
|
6
|
+
protected secret: string | null;
|
|
7
|
+
protected clients: Map<string, string>;
|
|
8
|
+
protected uploadMeta: Map<string, UploadFileMeta & {
|
|
9
|
+
expire: number;
|
|
10
|
+
}>;
|
|
11
|
+
readonly CONTENT_TYPE = "application/octet-stream";
|
|
12
|
+
constructor(config: SecurequServerConfig);
|
|
13
|
+
protected getSecret(): Promise<string>;
|
|
14
|
+
protected clientInfo(path: string, origin: string): Promise<{
|
|
15
|
+
path: string;
|
|
16
|
+
secret: string;
|
|
17
|
+
hash: string;
|
|
18
|
+
searchParams: {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
protected isValidSigneture(signeture: string | undefined, hash: string): Promise<void>;
|
|
23
|
+
handleRequest(info: HandlerInfo, metadata?: Metadata): Promise<void>;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export { SecurequServerBase as default };
|
package/server/Base.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var crypto=require('../include/crypto.js'),Router=require('./Router.js');class SecurequServerBase extends Router.default {
|
|
2
|
-
constructor(config) {
|
|
3
|
-
super();
|
|
4
|
-
this.secret = null;
|
|
5
|
-
this.clients = new Map();
|
|
6
|
-
this.uploadMeta = new Map();
|
|
7
|
-
this.CONTENT_TYPE = "application/octet-stream";
|
|
8
|
-
if (!config.basepath)
|
|
9
|
-
throw new Error("Basepath is required");
|
|
10
|
-
if (!config.clients || Object.keys(config.clients).length === 0)
|
|
11
|
-
throw new Error("Atleast one client is required");
|
|
12
|
-
if (!config.basepath.startsWith("/"))
|
|
13
|
-
config.basepath = `/${config.basepath}`;
|
|
14
|
-
if (config.basepath.endsWith("/"))
|
|
15
|
-
config.basepath = config.basepath.substring(0, config.basepath.length - 1);
|
|
16
|
-
if (config.file) {
|
|
17
|
-
config.file.maxFilesize = config.file.maxFilesize || 50 * 1024; // 50MB default
|
|
18
|
-
config.file.checkFileType = config.file.checkFileType || true;
|
|
19
|
-
}
|
|
20
|
-
config.mode = ["production", 'development'].includes(config.mode || "") ? config.mode : "production";
|
|
21
|
-
this.config = config;
|
|
22
|
-
for (let client of this.config.clients) {
|
|
23
|
-
this.clients.set(client.origin, client.secret);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
async getSecret() {
|
|
27
|
-
if (!this.secret) {
|
|
28
|
-
this.secret = await crypto.default.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15));
|
|
29
|
-
}
|
|
30
|
-
return this.secret;
|
|
31
|
-
}
|
|
32
|
-
async clientInfo(path, origin) {
|
|
33
|
-
const isDev = path !== '/' && this.config.mode === 'development';
|
|
34
|
-
let splitUrl = path.split("?");
|
|
35
|
-
path = splitUrl[0];
|
|
36
|
-
path = path.replace(this.config.basepath + "/", "");
|
|
37
|
-
path = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
|
38
|
-
path = path.startsWith('/') ? path.substring(1) : path;
|
|
39
|
-
const split = path.split("/");
|
|
40
|
-
const hash = split.shift();
|
|
41
|
-
let client = this.clients.get(origin);
|
|
42
|
-
if (!client) {
|
|
43
|
-
client = this.clients.get("*");
|
|
44
|
-
}
|
|
45
|
-
if (!(hash === null || hash === void 0 ? void 0 : hash.length) || !client) {
|
|
46
|
-
throw new Response("Invalid client", { status: 403 });
|
|
47
|
-
}
|
|
48
|
-
let secret = client.substring(0, hash.length);
|
|
49
|
-
let searchParams = {};
|
|
50
|
-
if (splitUrl.length > 1) {
|
|
51
|
-
const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]));
|
|
52
|
-
if (isDev) {
|
|
53
|
-
searchParams = Object.fromEntries(uparams);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
const keys = Array.from(uparams.keys());
|
|
57
|
-
if (keys.length) {
|
|
58
|
-
const key = keys[0];
|
|
59
|
-
const value = uparams.get(key);
|
|
60
|
-
const decrypted = await crypto.default.decrypt(value, secret);
|
|
61
|
-
if (decrypted && decrypted.key === key) {
|
|
62
|
-
searchParams = decrypted.params || {};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
path: "/" + split.join('/'),
|
|
69
|
-
secret,
|
|
70
|
-
hash,
|
|
71
|
-
searchParams
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
async isValidSigneture(signeture, hash) {
|
|
75
|
-
const serverSecret = await this.getSecret();
|
|
76
|
-
if (this.secret && signeture) {
|
|
77
|
-
let info = await crypto.default.decrypt(signeture, serverSecret);
|
|
78
|
-
if (!info)
|
|
79
|
-
throw new Response("Invalid Signeture", { status: 403 });
|
|
80
|
-
let isHashValid = info.hash === hash;
|
|
81
|
-
let isNotExpired = info.expire > Date.now();
|
|
82
|
-
if (!isHashValid)
|
|
83
|
-
throw new Response("Invalid Signeture", { status: 403 });
|
|
84
|
-
if (!isNotExpired)
|
|
85
|
-
throw new Response("Signeture expired", { status: 403 });
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
throw new Response("Invalid Signeture", { status: 403 });
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async handleRequest(info, metadata) {
|
|
92
|
-
if (this.config.accept) {
|
|
93
|
-
const is = await this.config.accept(info, metadata);
|
|
94
|
-
if (!is)
|
|
95
|
-
throw new Response("Request not accepted", { status: 403 });
|
|
96
|
-
}
|
|
97
|
-
const { path, method } = info;
|
|
98
|
-
let values = Object.values(this.routes[method]);
|
|
99
|
-
for (let { test, handler } of values) {
|
|
100
|
-
const match = test(path);
|
|
101
|
-
if (match) {
|
|
102
|
-
await handler(Object.assign(Object.assign({}, info), { params: match.params }), metadata);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
1
|
+
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var crypto=require('../include/crypto.js'),Router=require('./Router.js');class SecurequServerBase extends Router.default {
|
|
2
|
+
constructor(config) {
|
|
3
|
+
super();
|
|
4
|
+
this.secret = null;
|
|
5
|
+
this.clients = new Map();
|
|
6
|
+
this.uploadMeta = new Map();
|
|
7
|
+
this.CONTENT_TYPE = "application/octet-stream";
|
|
8
|
+
if (!config.basepath)
|
|
9
|
+
throw new Error("Basepath is required");
|
|
10
|
+
if (!config.clients || Object.keys(config.clients).length === 0)
|
|
11
|
+
throw new Error("Atleast one client is required");
|
|
12
|
+
if (!config.basepath.startsWith("/"))
|
|
13
|
+
config.basepath = `/${config.basepath}`;
|
|
14
|
+
if (config.basepath.endsWith("/"))
|
|
15
|
+
config.basepath = config.basepath.substring(0, config.basepath.length - 1);
|
|
16
|
+
if (config.file) {
|
|
17
|
+
config.file.maxFilesize = config.file.maxFilesize || 50 * 1024; // 50MB default
|
|
18
|
+
config.file.checkFileType = config.file.checkFileType || true;
|
|
19
|
+
}
|
|
20
|
+
config.mode = ["production", 'development'].includes(config.mode || "") ? config.mode : "production";
|
|
21
|
+
this.config = config;
|
|
22
|
+
for (let client of this.config.clients) {
|
|
23
|
+
this.clients.set(client.origin, client.secret);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async getSecret() {
|
|
27
|
+
if (!this.secret) {
|
|
28
|
+
this.secret = await crypto.default.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15));
|
|
29
|
+
}
|
|
30
|
+
return this.secret;
|
|
31
|
+
}
|
|
32
|
+
async clientInfo(path, origin) {
|
|
33
|
+
const isDev = path !== '/' && this.config.mode === 'development';
|
|
34
|
+
let splitUrl = path.split("?");
|
|
35
|
+
path = splitUrl[0];
|
|
36
|
+
path = path.replace(this.config.basepath + "/", "");
|
|
37
|
+
path = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
|
38
|
+
path = path.startsWith('/') ? path.substring(1) : path;
|
|
39
|
+
const split = path.split("/");
|
|
40
|
+
const hash = split.shift();
|
|
41
|
+
let client = this.clients.get(origin);
|
|
42
|
+
if (!client) {
|
|
43
|
+
client = this.clients.get("*");
|
|
44
|
+
}
|
|
45
|
+
if (!(hash === null || hash === void 0 ? void 0 : hash.length) || !client) {
|
|
46
|
+
throw new Response("Invalid client", { status: 403 });
|
|
47
|
+
}
|
|
48
|
+
let secret = client.substring(0, hash.length);
|
|
49
|
+
let searchParams = {};
|
|
50
|
+
if (splitUrl.length > 1) {
|
|
51
|
+
const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]));
|
|
52
|
+
if (isDev) {
|
|
53
|
+
searchParams = Object.fromEntries(uparams);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const keys = Array.from(uparams.keys());
|
|
57
|
+
if (keys.length) {
|
|
58
|
+
const key = keys[0];
|
|
59
|
+
const value = uparams.get(key);
|
|
60
|
+
const decrypted = await crypto.default.decrypt(value, secret);
|
|
61
|
+
if (decrypted && decrypted.key === key) {
|
|
62
|
+
searchParams = decrypted.params || {};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
path: "/" + split.join('/'),
|
|
69
|
+
secret,
|
|
70
|
+
hash,
|
|
71
|
+
searchParams
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async isValidSigneture(signeture, hash) {
|
|
75
|
+
const serverSecret = await this.getSecret();
|
|
76
|
+
if (this.secret && signeture) {
|
|
77
|
+
let info = await crypto.default.decrypt(signeture, serverSecret);
|
|
78
|
+
if (!info)
|
|
79
|
+
throw new Response("Invalid Signeture", { status: 403 });
|
|
80
|
+
let isHashValid = info.hash === hash;
|
|
81
|
+
let isNotExpired = info.expire > Date.now();
|
|
82
|
+
if (!isHashValid)
|
|
83
|
+
throw new Response("Invalid Signeture", { status: 403 });
|
|
84
|
+
if (!isNotExpired)
|
|
85
|
+
throw new Response("Signeture expired", { status: 403 });
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new Response("Invalid Signeture", { status: 403 });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async handleRequest(info, metadata) {
|
|
92
|
+
if (this.config.accept) {
|
|
93
|
+
const is = await this.config.accept(info, metadata);
|
|
94
|
+
if (!is)
|
|
95
|
+
throw new Response("Request not accepted", { status: 403 });
|
|
96
|
+
}
|
|
97
|
+
const { path, method } = info;
|
|
98
|
+
let values = Object.values(this.routes[method]);
|
|
99
|
+
for (let { test, handler } of values) {
|
|
100
|
+
const match = test(path);
|
|
101
|
+
if (match) {
|
|
102
|
+
await handler(Object.assign(Object.assign({}, info), { params: match.params }), metadata);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
106
|
}exports.default=SecurequServerBase;//# sourceMappingURL=Base.js.map
|
package/server/Base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Base.js","sources":["../../src/server/Base.ts"],"sourcesContent":["import crypto from \"../include/crypto\";\nimport Router from \"./Router\";\nimport { HandlerInfo, SecurequServerConfig, ServerClientSecret, ServerClientOrigin, UploadFileMeta, Metadata } from \"./types\";\n\nclass SecurequServerBase extends Router {\n protected config: SecurequServerConfig;\n protected secret: string | null = null\n protected clients = new Map<ServerClientOrigin, ServerClientSecret>();\n protected uploadMeta = new Map<string, UploadFileMeta & { expire: number }>();\n readonly CONTENT_TYPE = \"application/octet-stream\";\n\n constructor(config: SecurequServerConfig) {\n super()\n if (!config.basepath) throw new Error(\"Basepath is required\");\n if (!config.clients || Object.keys(config.clients).length === 0) throw new Error(\"Atleast one client is required\");\n if (!config.basepath.startsWith(\"/\")) config.basepath = `/${config.basepath}`;\n if (config.basepath.endsWith(\"/\")) config.basepath = config.basepath.substring(0, config.basepath.length - 1);\n if (config.file) {\n config.file.maxFilesize = config.file.maxFilesize || 50 * 1024 // 50MB default\n config.file.checkFileType = config.file.checkFileType || true\n }\n config.mode = [\"production\", 'development'].includes(config.mode || \"\") ? config.mode : \"production\"\n this.config = config\n for (let client of this.config.clients) {\n this.clients.set(client.origin, client.secret)\n }\n }\n\n protected async getSecret() {\n if (!this.secret) {\n this.secret = await crypto.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15))\n }\n return this.secret\n }\n\n protected async clientInfo(path: string, origin: string) {\n const isDev = path !== '/' && this.config.mode === 'development'\n let splitUrl = path.split(\"?\")\n path = splitUrl[0]\n path = path.replace(this.config.basepath + \"/\", \"\")\n path = path.endsWith('/') ? path.substring(0, path.length - 1) : path\n path = path.startsWith('/') ? path.substring(1) : path\n\n const split = path.split(\"/\")\n const hash = split.shift()\n let client = this.clients.get(origin)\n if (!client) {\n client = this.clients.get(\"*\")\n }\n\n if (!hash?.length || !client) {\n throw new Response(\"Invalid client\", { status: 403 });\n }\n\n let secret = client.substring(0, hash.length);\n let searchParams: { [key: string]: any } = {}\n if (splitUrl.length > 1) {\n const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]))\n if (isDev) {\n searchParams = Object.fromEntries(uparams)\n } else {\n const keys = Array.from(uparams.keys());\n if (keys.length) {\n const key = keys[0]\n const value = uparams.get(key) as string;\n const decrypted: any = await crypto.decrypt(value, secret)\n if (decrypted && decrypted.key === key) {\n searchParams = decrypted.params || {}\n }\n }\n }\n }\n return {\n path: \"/\" + split.join('/'),\n secret,\n hash,\n searchParams\n }\n }\n\n protected async isValidSigneture(signeture: string | undefined, hash: string) {\n const serverSecret = await this.getSecret()\n if (this.secret && signeture) {\n let info: any = await crypto.decrypt(signeture, serverSecret)\n if (!info) throw new Response(\"Invalid Signeture\", { status: 403 });\n let isHashValid = info.hash === hash\n let isNotExpired = info.expire > Date.now()\n if (!isHashValid) throw new Response(\"Invalid Signeture\", { status: 403 });\n if (!isNotExpired) throw new Response(\"Signeture expired\", { status: 403 });\n } else {\n throw new Response(\"Invalid Signeture\", { status: 403 });\n }\n }\n\n async handleRequest(info: HandlerInfo, metadata?: Metadata) {\n if (this.config.accept) {\n const is = await this.config.accept(info, metadata)\n if (!is) throw new Response(\"Request not accepted\", { status: 403 })\n }\n\n const { path, method } = info;\n let values: any = Object.values(this.routes[method]);\n for (let { test, handler } of values) {\n const match = test(path)\n if (match) {\n await handler({ ...info, params: match.params }, metadata)\n }\n }\n }\n\n}\n\nexport default SecurequServerBase;"],"names":["Router","crypto"],"mappings":"+IAIA,MAAM,kBAAmB,SAAQA,cAAM,CAAA;AAOpC,IAAA,WAAA,CAAY,MAA4B,EAAA;AACrC,QAAA,KAAK,EAAE;QANA,IAAA,CAAA,MAAM,GAAkB,IAAI;AAC5B,QAAA,IAAA,CAAA,OAAO,GAAG,IAAI,GAAG,EAA0C;AAC3D,QAAA,IAAA,CAAA,UAAU,GAAG,IAAI,GAAG,EAA+C;QACpE,IAAA,CAAA,YAAY,GAAG,0BAA0B;QAI/C,IAAI,CAAC,MAAM,CAAC,QAAQ;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC;AAC7D,QAAA,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC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,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpD,KAAK,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,MAAM,EAAE;AACnC,YAAA,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;AACxB,YAAA,IAAI,KAAK,EAAE;AACR,gBAAA,MAAM,OAAO,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EAAM,IAAI,CAAA,EAAA,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAA,CAAA,EAAI,QAAQ,CAAC;AAC5D,YAAA;AACH,QAAA;IACJ;AAEF"}
|
|
1
|
+
{"version":3,"file":"Base.js","sources":["../../src/server/Base.ts"],"sourcesContent":["import crypto from \"../include/crypto\";\r\nimport Router from \"./Router\";\r\nimport { HandlerInfo, SecurequServerConfig, ServerClientSecret, ServerClientOrigin, UploadFileMeta, Metadata } from \"./types\";\r\n\r\nclass SecurequServerBase extends Router {\r\n protected config: SecurequServerConfig;\r\n protected secret: string | null = null\r\n protected clients = new Map<ServerClientOrigin, ServerClientSecret>();\r\n protected uploadMeta = new Map<string, UploadFileMeta & { expire: number }>();\r\n readonly CONTENT_TYPE = \"application/octet-stream\";\r\n\r\n constructor(config: SecurequServerConfig) {\r\n super()\r\n if (!config.basepath) throw new Error(\"Basepath is required\");\r\n if (!config.clients || Object.keys(config.clients).length === 0) throw new Error(\"Atleast one client is required\");\r\n if (!config.basepath.startsWith(\"/\")) config.basepath = `/${config.basepath}`;\r\n if (config.basepath.endsWith(\"/\")) config.basepath = config.basepath.substring(0, config.basepath.length - 1);\r\n if (config.file) {\r\n config.file.maxFilesize = config.file.maxFilesize || 50 * 1024 // 50MB default\r\n config.file.checkFileType = config.file.checkFileType || true\r\n }\r\n config.mode = [\"production\", 'development'].includes(config.mode || \"\") ? config.mode : \"production\"\r\n this.config = config\r\n for (let client of this.config.clients) {\r\n this.clients.set(client.origin, client.secret)\r\n }\r\n }\r\n\r\n protected async getSecret() {\r\n if (!this.secret) {\r\n this.secret = await crypto.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15))\r\n }\r\n return this.secret\r\n }\r\n\r\n protected async clientInfo(path: string, origin: string) {\r\n const isDev = path !== '/' && this.config.mode === 'development'\r\n let splitUrl = path.split(\"?\")\r\n path = splitUrl[0]\r\n path = path.replace(this.config.basepath + \"/\", \"\")\r\n path = path.endsWith('/') ? path.substring(0, path.length - 1) : path\r\n path = path.startsWith('/') ? path.substring(1) : path\r\n\r\n const split = path.split(\"/\")\r\n const hash = split.shift()\r\n let client = this.clients.get(origin)\r\n if (!client) {\r\n client = this.clients.get(\"*\")\r\n }\r\n\r\n if (!hash?.length || !client) {\r\n throw new Response(\"Invalid client\", { status: 403 });\r\n }\r\n\r\n let secret = client.substring(0, hash.length);\r\n let searchParams: { [key: string]: any } = {}\r\n if (splitUrl.length > 1) {\r\n const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]))\r\n if (isDev) {\r\n searchParams = Object.fromEntries(uparams)\r\n } else {\r\n const keys = Array.from(uparams.keys());\r\n if (keys.length) {\r\n const key = keys[0]\r\n const value = uparams.get(key) as string;\r\n const decrypted: any = await crypto.decrypt(value, secret)\r\n if (decrypted && decrypted.key === key) {\r\n searchParams = decrypted.params || {}\r\n }\r\n }\r\n }\r\n }\r\n return {\r\n path: \"/\" + split.join('/'),\r\n secret,\r\n hash,\r\n searchParams\r\n }\r\n }\r\n\r\n protected async isValidSigneture(signeture: string | undefined, hash: string) {\r\n const serverSecret = await this.getSecret()\r\n if (this.secret && signeture) {\r\n let info: any = await crypto.decrypt(signeture, serverSecret)\r\n if (!info) throw new Response(\"Invalid Signeture\", { status: 403 });\r\n let isHashValid = info.hash === hash\r\n let isNotExpired = info.expire > Date.now()\r\n if (!isHashValid) throw new Response(\"Invalid Signeture\", { status: 403 });\r\n if (!isNotExpired) throw new Response(\"Signeture expired\", { status: 403 });\r\n } else {\r\n throw new Response(\"Invalid Signeture\", { status: 403 });\r\n }\r\n }\r\n\r\n async handleRequest(info: HandlerInfo, metadata?: Metadata) {\r\n if (this.config.accept) {\r\n const is = await this.config.accept(info, metadata)\r\n if (!is) throw new Response(\"Request not accepted\", { status: 403 })\r\n }\r\n\r\n const { path, method } = info;\r\n let values: any = Object.values(this.routes[method]);\r\n for (let { test, handler } of values) {\r\n const match = test(path)\r\n if (match) {\r\n await handler({ ...info, params: match.params }, metadata)\r\n }\r\n }\r\n }\r\n\r\n}\r\n\r\nexport default SecurequServerBase;"],"names":["Router","crypto"],"mappings":"+IAIA,MAAM,kBAAmB,SAAQA,cAAM,CAAA;AAOpC,IAAA,WAAA,CAAY,MAA4B,EAAA;AACrC,QAAA,KAAK,EAAE,CAAA;QANA,IAAA,CAAA,MAAM,GAAkB,IAAI,CAAA;AAC5B,QAAA,IAAA,CAAA,OAAO,GAAG,IAAI,GAAG,EAA0C,CAAC;AAC5D,QAAA,IAAA,CAAA,UAAU,GAAG,IAAI,GAAG,EAA+C,CAAC;QACrE,IAAA,CAAA,YAAY,GAAG,0BAA0B,CAAC;QAIhD,IAAI,CAAC,MAAM,CAAC,QAAQ;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;AAC9D,QAAA,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACnH,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,MAAM,CAAC,QAAQ,GAAG,CAAA,CAAA,EAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC9E,QAAA,IAAI,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC;AAAE,YAAA,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,SAAS,CA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ACrD,KAAK,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,MAAM,EAAE;AACnC,YAAA,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAA;AACxB,YAAA,IAAI,KAAK,EAAE;AACR,gBAAA,MAAM,OAAO,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EAAM,IAAI,CAAA,EAAA,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAA,CAAA,EAAI,QAAQ,CAAC,CAAA;AAC5D,YAAA,CAAA;AACH,QAAA,CAAA;IACJ,CAAC;AAEH"}
|
package/server/Base.mjs
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import crypto from'../include/crypto.mjs';import Router from'./Router.mjs';class SecurequServerBase extends Router {
|
|
2
|
-
constructor(config) {
|
|
3
|
-
super();
|
|
4
|
-
this.secret = null;
|
|
5
|
-
this.clients = new Map();
|
|
6
|
-
this.uploadMeta = new Map();
|
|
7
|
-
this.CONTENT_TYPE = "application/octet-stream";
|
|
8
|
-
if (!config.basepath)
|
|
9
|
-
throw new Error("Basepath is required");
|
|
10
|
-
if (!config.clients || Object.keys(config.clients).length === 0)
|
|
11
|
-
throw new Error("Atleast one client is required");
|
|
12
|
-
if (!config.basepath.startsWith("/"))
|
|
13
|
-
config.basepath = `/${config.basepath}`;
|
|
14
|
-
if (config.basepath.endsWith("/"))
|
|
15
|
-
config.basepath = config.basepath.substring(0, config.basepath.length - 1);
|
|
16
|
-
if (config.file) {
|
|
17
|
-
config.file.maxFilesize = config.file.maxFilesize || 50 * 1024; // 50MB default
|
|
18
|
-
config.file.checkFileType = config.file.checkFileType || true;
|
|
19
|
-
}
|
|
20
|
-
config.mode = ["production", 'development'].includes(config.mode || "") ? config.mode : "production";
|
|
21
|
-
this.config = config;
|
|
22
|
-
for (let client of this.config.clients) {
|
|
23
|
-
this.clients.set(client.origin, client.secret);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
async getSecret() {
|
|
27
|
-
if (!this.secret) {
|
|
28
|
-
this.secret = await crypto.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15));
|
|
29
|
-
}
|
|
30
|
-
return this.secret;
|
|
31
|
-
}
|
|
32
|
-
async clientInfo(path, origin) {
|
|
33
|
-
const isDev = path !== '/' && this.config.mode === 'development';
|
|
34
|
-
let splitUrl = path.split("?");
|
|
35
|
-
path = splitUrl[0];
|
|
36
|
-
path = path.replace(this.config.basepath + "/", "");
|
|
37
|
-
path = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
|
38
|
-
path = path.startsWith('/') ? path.substring(1) : path;
|
|
39
|
-
const split = path.split("/");
|
|
40
|
-
const hash = split.shift();
|
|
41
|
-
let client = this.clients.get(origin);
|
|
42
|
-
if (!client) {
|
|
43
|
-
client = this.clients.get("*");
|
|
44
|
-
}
|
|
45
|
-
if (!(hash === null || hash === void 0 ? void 0 : hash.length) || !client) {
|
|
46
|
-
throw new Response("Invalid client", { status: 403 });
|
|
47
|
-
}
|
|
48
|
-
let secret = client.substring(0, hash.length);
|
|
49
|
-
let searchParams = {};
|
|
50
|
-
if (splitUrl.length > 1) {
|
|
51
|
-
const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]));
|
|
52
|
-
if (isDev) {
|
|
53
|
-
searchParams = Object.fromEntries(uparams);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
const keys = Array.from(uparams.keys());
|
|
57
|
-
if (keys.length) {
|
|
58
|
-
const key = keys[0];
|
|
59
|
-
const value = uparams.get(key);
|
|
60
|
-
const decrypted = await crypto.decrypt(value, secret);
|
|
61
|
-
if (decrypted && decrypted.key === key) {
|
|
62
|
-
searchParams = decrypted.params || {};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
path: "/" + split.join('/'),
|
|
69
|
-
secret,
|
|
70
|
-
hash,
|
|
71
|
-
searchParams
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
async isValidSigneture(signeture, hash) {
|
|
75
|
-
const serverSecret = await this.getSecret();
|
|
76
|
-
if (this.secret && signeture) {
|
|
77
|
-
let info = await crypto.decrypt(signeture, serverSecret);
|
|
78
|
-
if (!info)
|
|
79
|
-
throw new Response("Invalid Signeture", { status: 403 });
|
|
80
|
-
let isHashValid = info.hash === hash;
|
|
81
|
-
let isNotExpired = info.expire > Date.now();
|
|
82
|
-
if (!isHashValid)
|
|
83
|
-
throw new Response("Invalid Signeture", { status: 403 });
|
|
84
|
-
if (!isNotExpired)
|
|
85
|
-
throw new Response("Signeture expired", { status: 403 });
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
throw new Response("Invalid Signeture", { status: 403 });
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async handleRequest(info, metadata) {
|
|
92
|
-
if (this.config.accept) {
|
|
93
|
-
const is = await this.config.accept(info, metadata);
|
|
94
|
-
if (!is)
|
|
95
|
-
throw new Response("Request not accepted", { status: 403 });
|
|
96
|
-
}
|
|
97
|
-
const { path, method } = info;
|
|
98
|
-
let values = Object.values(this.routes[method]);
|
|
99
|
-
for (let { test, handler } of values) {
|
|
100
|
-
const match = test(path);
|
|
101
|
-
if (match) {
|
|
102
|
-
await handler(Object.assign(Object.assign({}, info), { params: match.params }), metadata);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
1
|
+
import crypto from'../include/crypto.mjs';import Router from'./Router.mjs';class SecurequServerBase extends Router {
|
|
2
|
+
constructor(config) {
|
|
3
|
+
super();
|
|
4
|
+
this.secret = null;
|
|
5
|
+
this.clients = new Map();
|
|
6
|
+
this.uploadMeta = new Map();
|
|
7
|
+
this.CONTENT_TYPE = "application/octet-stream";
|
|
8
|
+
if (!config.basepath)
|
|
9
|
+
throw new Error("Basepath is required");
|
|
10
|
+
if (!config.clients || Object.keys(config.clients).length === 0)
|
|
11
|
+
throw new Error("Atleast one client is required");
|
|
12
|
+
if (!config.basepath.startsWith("/"))
|
|
13
|
+
config.basepath = `/${config.basepath}`;
|
|
14
|
+
if (config.basepath.endsWith("/"))
|
|
15
|
+
config.basepath = config.basepath.substring(0, config.basepath.length - 1);
|
|
16
|
+
if (config.file) {
|
|
17
|
+
config.file.maxFilesize = config.file.maxFilesize || 50 * 1024; // 50MB default
|
|
18
|
+
config.file.checkFileType = config.file.checkFileType || true;
|
|
19
|
+
}
|
|
20
|
+
config.mode = ["production", 'development'].includes(config.mode || "") ? config.mode : "production";
|
|
21
|
+
this.config = config;
|
|
22
|
+
for (let client of this.config.clients) {
|
|
23
|
+
this.clients.set(client.origin, client.secret);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async getSecret() {
|
|
27
|
+
if (!this.secret) {
|
|
28
|
+
this.secret = await crypto.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15));
|
|
29
|
+
}
|
|
30
|
+
return this.secret;
|
|
31
|
+
}
|
|
32
|
+
async clientInfo(path, origin) {
|
|
33
|
+
const isDev = path !== '/' && this.config.mode === 'development';
|
|
34
|
+
let splitUrl = path.split("?");
|
|
35
|
+
path = splitUrl[0];
|
|
36
|
+
path = path.replace(this.config.basepath + "/", "");
|
|
37
|
+
path = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
|
38
|
+
path = path.startsWith('/') ? path.substring(1) : path;
|
|
39
|
+
const split = path.split("/");
|
|
40
|
+
const hash = split.shift();
|
|
41
|
+
let client = this.clients.get(origin);
|
|
42
|
+
if (!client) {
|
|
43
|
+
client = this.clients.get("*");
|
|
44
|
+
}
|
|
45
|
+
if (!(hash === null || hash === void 0 ? void 0 : hash.length) || !client) {
|
|
46
|
+
throw new Response("Invalid client", { status: 403 });
|
|
47
|
+
}
|
|
48
|
+
let secret = client.substring(0, hash.length);
|
|
49
|
+
let searchParams = {};
|
|
50
|
+
if (splitUrl.length > 1) {
|
|
51
|
+
const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]));
|
|
52
|
+
if (isDev) {
|
|
53
|
+
searchParams = Object.fromEntries(uparams);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const keys = Array.from(uparams.keys());
|
|
57
|
+
if (keys.length) {
|
|
58
|
+
const key = keys[0];
|
|
59
|
+
const value = uparams.get(key);
|
|
60
|
+
const decrypted = await crypto.decrypt(value, secret);
|
|
61
|
+
if (decrypted && decrypted.key === key) {
|
|
62
|
+
searchParams = decrypted.params || {};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
path: "/" + split.join('/'),
|
|
69
|
+
secret,
|
|
70
|
+
hash,
|
|
71
|
+
searchParams
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async isValidSigneture(signeture, hash) {
|
|
75
|
+
const serverSecret = await this.getSecret();
|
|
76
|
+
if (this.secret && signeture) {
|
|
77
|
+
let info = await crypto.decrypt(signeture, serverSecret);
|
|
78
|
+
if (!info)
|
|
79
|
+
throw new Response("Invalid Signeture", { status: 403 });
|
|
80
|
+
let isHashValid = info.hash === hash;
|
|
81
|
+
let isNotExpired = info.expire > Date.now();
|
|
82
|
+
if (!isHashValid)
|
|
83
|
+
throw new Response("Invalid Signeture", { status: 403 });
|
|
84
|
+
if (!isNotExpired)
|
|
85
|
+
throw new Response("Signeture expired", { status: 403 });
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new Response("Invalid Signeture", { status: 403 });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async handleRequest(info, metadata) {
|
|
92
|
+
if (this.config.accept) {
|
|
93
|
+
const is = await this.config.accept(info, metadata);
|
|
94
|
+
if (!is)
|
|
95
|
+
throw new Response("Request not accepted", { status: 403 });
|
|
96
|
+
}
|
|
97
|
+
const { path, method } = info;
|
|
98
|
+
let values = Object.values(this.routes[method]);
|
|
99
|
+
for (let { test, handler } of values) {
|
|
100
|
+
const match = test(path);
|
|
101
|
+
if (match) {
|
|
102
|
+
await handler(Object.assign(Object.assign({}, info), { params: match.params }), metadata);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
106
|
}export{SecurequServerBase as default};//# sourceMappingURL=Base.mjs.map
|
package/server/Base.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Base.mjs","sources":["../../src/server/Base.ts"],"sourcesContent":["import crypto from \"../include/crypto\";\nimport Router from \"./Router\";\nimport { HandlerInfo, SecurequServerConfig, ServerClientSecret, ServerClientOrigin, UploadFileMeta, Metadata } from \"./types\";\n\nclass SecurequServerBase extends Router {\n protected config: SecurequServerConfig;\n protected secret: string | null = null\n protected clients = new Map<ServerClientOrigin, ServerClientSecret>();\n protected uploadMeta = new Map<string, UploadFileMeta & { expire: number }>();\n readonly CONTENT_TYPE = \"application/octet-stream\";\n\n constructor(config: SecurequServerConfig) {\n super()\n if (!config.basepath) throw new Error(\"Basepath is required\");\n if (!config.clients || Object.keys(config.clients).length === 0) throw new Error(\"Atleast one client is required\");\n if (!config.basepath.startsWith(\"/\")) config.basepath = `/${config.basepath}`;\n if (config.basepath.endsWith(\"/\")) config.basepath = config.basepath.substring(0, config.basepath.length - 1);\n if (config.file) {\n config.file.maxFilesize = config.file.maxFilesize || 50 * 1024 // 50MB default\n config.file.checkFileType = config.file.checkFileType || true\n }\n config.mode = [\"production\", 'development'].includes(config.mode || \"\") ? config.mode : \"production\"\n this.config = config\n for (let client of this.config.clients) {\n this.clients.set(client.origin, client.secret)\n }\n }\n\n protected async getSecret() {\n if (!this.secret) {\n this.secret = await crypto.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15))\n }\n return this.secret\n }\n\n protected async clientInfo(path: string, origin: string) {\n const isDev = path !== '/' && this.config.mode === 'development'\n let splitUrl = path.split(\"?\")\n path = splitUrl[0]\n path = path.replace(this.config.basepath + \"/\", \"\")\n path = path.endsWith('/') ? path.substring(0, path.length - 1) : path\n path = path.startsWith('/') ? path.substring(1) : path\n\n const split = path.split(\"/\")\n const hash = split.shift()\n let client = this.clients.get(origin)\n if (!client) {\n client = this.clients.get(\"*\")\n }\n\n if (!hash?.length || !client) {\n throw new Response(\"Invalid client\", { status: 403 });\n }\n\n let secret = client.substring(0, hash.length);\n let searchParams: { [key: string]: any } = {}\n if (splitUrl.length > 1) {\n const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]))\n if (isDev) {\n searchParams = Object.fromEntries(uparams)\n } else {\n const keys = Array.from(uparams.keys());\n if (keys.length) {\n const key = keys[0]\n const value = uparams.get(key) as string;\n const decrypted: any = await crypto.decrypt(value, secret)\n if (decrypted && decrypted.key === key) {\n searchParams = decrypted.params || {}\n }\n }\n }\n }\n return {\n path: \"/\" + split.join('/'),\n secret,\n hash,\n searchParams\n }\n }\n\n protected async isValidSigneture(signeture: string | undefined, hash: string) {\n const serverSecret = await this.getSecret()\n if (this.secret && signeture) {\n let info: any = await crypto.decrypt(signeture, serverSecret)\n if (!info) throw new Response(\"Invalid Signeture\", { status: 403 });\n let isHashValid = info.hash === hash\n let isNotExpired = info.expire > Date.now()\n if (!isHashValid) throw new Response(\"Invalid Signeture\", { status: 403 });\n if (!isNotExpired) throw new Response(\"Signeture expired\", { status: 403 });\n } else {\n throw new Response(\"Invalid Signeture\", { status: 403 });\n }\n }\n\n async handleRequest(info: HandlerInfo, metadata?: Metadata) {\n if (this.config.accept) {\n const is = await this.config.accept(info, metadata)\n if (!is) throw new Response(\"Request not accepted\", { status: 403 })\n }\n\n const { path, method } = info;\n let values: any = Object.values(this.routes[method]);\n for (let { test, handler } of values) {\n const match = test(path)\n if (match) {\n await handler({ ...info, params: match.params }, metadata)\n }\n }\n }\n\n}\n\nexport default SecurequServerBase;"],"names":[],"mappings":"2EAIA,MAAM,kBAAmB,SAAQ,MAAM,CAAA;AAOpC,IAAA,WAAA,CAAY,MAA4B,EAAA;AACrC,QAAA,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C,MAAM,EAAE;AACrB,YAAA,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC;AACnD,YAAA,IAAI,CAAC,EAAE;gBAAE,MAAM,IAAI,QAAQ,CAAC,sBAAsB,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AACtE,QAAA;AAED,QAAA,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI;AAC7B,QAAA,IAAI,MAAM,GAAQ,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpD,KAAK,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,MAAM,EAAE;AACnC,YAAA,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;AACxB,YAAA,IAAI,KAAK,EAAE;AACR,gBAAA,MAAM,OAAO,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EAAM,IAAI,CAAA,EAAA,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAA,CAAA,EAAI,QAAQ,CAAC;AAC5D,YAAA;AACH,QAAA;IACJ;AAEF"}
|
|
1
|
+
{"version":3,"file":"Base.mjs","sources":["../../src/server/Base.ts"],"sourcesContent":["import crypto from \"../include/crypto\";\r\nimport Router from \"./Router\";\r\nimport { HandlerInfo, SecurequServerConfig, ServerClientSecret, ServerClientOrigin, UploadFileMeta, Metadata } from \"./types\";\r\n\r\nclass SecurequServerBase extends Router {\r\n protected config: SecurequServerConfig;\r\n protected secret: string | null = null\r\n protected clients = new Map<ServerClientOrigin, ServerClientSecret>();\r\n protected uploadMeta = new Map<string, UploadFileMeta & { expire: number }>();\r\n readonly CONTENT_TYPE = \"application/octet-stream\";\r\n\r\n constructor(config: SecurequServerConfig) {\r\n super()\r\n if (!config.basepath) throw new Error(\"Basepath is required\");\r\n if (!config.clients || Object.keys(config.clients).length === 0) throw new Error(\"Atleast one client is required\");\r\n if (!config.basepath.startsWith(\"/\")) config.basepath = `/${config.basepath}`;\r\n if (config.basepath.endsWith(\"/\")) config.basepath = config.basepath.substring(0, config.basepath.length - 1);\r\n if (config.file) {\r\n config.file.maxFilesize = config.file.maxFilesize || 50 * 1024 // 50MB default\r\n config.file.checkFileType = config.file.checkFileType || true\r\n }\r\n config.mode = [\"production\", 'development'].includes(config.mode || \"\") ? config.mode : \"production\"\r\n this.config = config\r\n for (let client of this.config.clients) {\r\n this.clients.set(client.origin, client.secret)\r\n }\r\n }\r\n\r\n protected async getSecret() {\r\n if (!this.secret) {\r\n this.secret = await crypto.hash(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15))\r\n }\r\n return this.secret\r\n }\r\n\r\n protected async clientInfo(path: string, origin: string) {\r\n const isDev = path !== '/' && this.config.mode === 'development'\r\n let splitUrl = path.split(\"?\")\r\n path = splitUrl[0]\r\n path = path.replace(this.config.basepath + \"/\", \"\")\r\n path = path.endsWith('/') ? path.substring(0, path.length - 1) : path\r\n path = path.startsWith('/') ? path.substring(1) : path\r\n\r\n const split = path.split(\"/\")\r\n const hash = split.shift()\r\n let client = this.clients.get(origin)\r\n if (!client) {\r\n client = this.clients.get(\"*\")\r\n }\r\n\r\n if (!hash?.length || !client) {\r\n throw new Response(\"Invalid client\", { status: 403 });\r\n }\r\n\r\n let secret = client.substring(0, hash.length);\r\n let searchParams: { [key: string]: any } = {}\r\n if (splitUrl.length > 1) {\r\n const uparams = new URLSearchParams(decodeURIComponent(splitUrl[1]))\r\n if (isDev) {\r\n searchParams = Object.fromEntries(uparams)\r\n } else {\r\n const keys = Array.from(uparams.keys());\r\n if (keys.length) {\r\n const key = keys[0]\r\n const value = uparams.get(key) as string;\r\n const decrypted: any = await crypto.decrypt(value, secret)\r\n if (decrypted && decrypted.key === key) {\r\n searchParams = decrypted.params || {}\r\n }\r\n }\r\n }\r\n }\r\n return {\r\n path: \"/\" + split.join('/'),\r\n secret,\r\n hash,\r\n searchParams\r\n }\r\n }\r\n\r\n protected async isValidSigneture(signeture: string | undefined, hash: string) {\r\n const serverSecret = await this.getSecret()\r\n if (this.secret && signeture) {\r\n let info: any = await crypto.decrypt(signeture, serverSecret)\r\n if (!info) throw new Response(\"Invalid Signeture\", { status: 403 });\r\n let isHashValid = info.hash === hash\r\n let isNotExpired = info.expire > Date.now()\r\n if (!isHashValid) throw new Response(\"Invalid Signeture\", { status: 403 });\r\n if (!isNotExpired) throw new Response(\"Signeture expired\", { status: 403 });\r\n } else {\r\n throw new Response(\"Invalid Signeture\", { status: 403 });\r\n }\r\n }\r\n\r\n async handleRequest(info: HandlerInfo, metadata?: Metadata) {\r\n if (this.config.accept) {\r\n const is = await this.config.accept(info, metadata)\r\n if (!is) throw new Response(\"Request not accepted\", { status: 403 })\r\n }\r\n\r\n const { path, method } = info;\r\n let values: any = Object.values(this.routes[method]);\r\n for (let { test, handler } of values) {\r\n const match = test(path)\r\n if (match) {\r\n await handler({ ...info, params: match.params }, metadata)\r\n }\r\n }\r\n }\r\n\r\n}\r\n\r\nexport default SecurequServerBase;"],"names":[],"mappings":"2EAIA,MAAM,kBAAmB,SAAQ,MAAM,CAAA;AAOpC,IAAA,WAAA,CAAY,MAA4B,EAAA;AACrC,QAAA,KAAK,EAAE,CAAA;QANA,IAAA,CAAA,MAAM,GAAkB,IAAI,CAAA;AAC5B,QAAA,IAAA,CAAA,OAAO,GAAG,IAAI,GAAG,EAA0C,CAAC;AAC5D,QAAA,IAAA,CAAA,UAAU,GAAG,IAAI,GAAG,EAA+C,CAAC;QACrE,IAAA,CAAA,YAAY,GAAG,0BAA0B,CAAC;QAIhD,IAAI,CAAC,MAAM,CAAC,QAAQ;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;AAC9D,QAAA,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,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M,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;AACnD,YAAA,IAAI,CAAC,EAAE;gBAAE,MAAM,IAAI,QAAQ,CAAC,sBAAsB,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;AACtE,QAAA,CAAA;AAED,QAAA,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;AAC9B,QAAA,IAAI,MAAM,GAAQ,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QACrD,KAAK,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,MAAM,EAAE;AACnC,YAAA,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAA;AACxB,YAAA,IAAI,KAAK,EAAE;AACR,gBAAA,MAAM,OAAO,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EAAM,IAAI,CAAA,EAAA,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAA,CAAA,EAAI,QAAQ,CAAC,CAAA;AAC5D,YAAA,CAAA;AACH,QAAA,CAAA;IACJ,CAAC;AAEH"}
|
package/server/Router.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { HTTPMethods } from '../client/types.js';
|
|
2
2
|
import { RouteFactory, HandlerFunction } from './types.js';
|
|
3
3
|
|
|
4
|
-
declare class Router {
|
|
5
|
-
protected routes: RouteFactory;
|
|
6
|
-
addRoute(path: string, method: HTTPMethods, handler: HandlerFunction): Promise<void>;
|
|
7
|
-
get(path: string, handler: HandlerFunction): Promise<void>;
|
|
8
|
-
post(path: string, handler: HandlerFunction): Promise<void>;
|
|
9
|
-
put(path: string, handler: HandlerFunction): Promise<void>;
|
|
10
|
-
delete(path: string, handler: HandlerFunction): Promise<void>;
|
|
4
|
+
declare class Router {
|
|
5
|
+
protected routes: RouteFactory;
|
|
6
|
+
addRoute(path: string, method: HTTPMethods, handler: HandlerFunction): Promise<void>;
|
|
7
|
+
get(path: string, handler: HandlerFunction): Promise<void>;
|
|
8
|
+
post(path: string, handler: HandlerFunction): Promise<void>;
|
|
9
|
+
put(path: string, handler: HandlerFunction): Promise<void>;
|
|
10
|
+
delete(path: string, handler: HandlerFunction): Promise<void>;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export { Router as default };
|