@frontmcp/utils 0.12.2 → 1.0.0-beta.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/async-context/browser-async-context.d.ts +12 -0
- package/async-context/browser-async-context.js +49 -0
- package/async-context/index.d.ts +1 -0
- package/async-context/node-async-context.d.ts +1 -0
- package/async-context/node-async-context.js +30 -0
- package/crypto/browser.d.ts +16 -0
- package/crypto/browser.js +160 -0
- package/crypto/index.d.ts +15 -6
- package/crypto/jwt-alg.d.ts +5 -0
- package/crypto/key-persistence/factory.d.ts +8 -1
- package/crypto/key-persistence/types.d.ts +10 -2
- package/crypto/node.d.ts +2 -0
- package/crypto/node.js +185 -0
- package/env/browser-env.d.ts +17 -0
- package/env/browser-env.js +78 -0
- package/env/index.d.ts +1 -0
- package/env/node-env.d.ts +14 -0
- package/env/node-env.js +85 -0
- package/esm/async-context/browser-async-context.mjs +24 -0
- package/esm/async-context/node-async-context.mjs +5 -0
- package/esm/crypto/browser.mjs +131 -0
- package/esm/crypto/node.mjs +143 -0
- package/esm/env/browser-env.mjs +44 -0
- package/esm/env/node-env.mjs +51 -0
- package/esm/event-emitter/browser-event-emitter.mjs +49 -0
- package/esm/event-emitter/node-event-emitter.mjs +5 -0
- package/esm/index.mjs +3887 -3064
- package/event-emitter/browser-event-emitter.d.ts +19 -0
- package/event-emitter/browser-event-emitter.js +74 -0
- package/event-emitter/index.d.ts +1 -0
- package/event-emitter/node-event-emitter.d.ts +1 -0
- package/event-emitter/node-event-emitter.js +30 -0
- package/index.d.ts +14 -5
- package/index.js +4144 -3314
- package/llm/index.d.ts +2 -0
- package/llm/llm-tool-handler.types.d.ts +48 -0
- package/llm/llm-tool-handlers.d.ts +10 -0
- package/naming/index.d.ts +2 -1
- package/package.json +23 -1
- package/path/browser-path.d.ts +5 -0
- package/path/index.d.ts +1 -0
- package/path/node-path.d.ts +1 -0
- package/storage/adapters/index.d.ts +4 -0
- package/storage/adapters/indexeddb.d.ts +77 -0
- package/storage/adapters/localstorage.d.ts +84 -0
- package/storage/index.d.ts +2 -2
- package/uri/index.d.ts +2 -1
- package/esm/package.json +0 -69
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js environment access.
|
|
3
|
+
*/
|
|
4
|
+
export declare function getEnv(key: string): string | undefined;
|
|
5
|
+
export declare function getEnv(key: string, defaultValue: string): string;
|
|
6
|
+
export declare function getCwd(): string;
|
|
7
|
+
export declare function isProduction(): boolean;
|
|
8
|
+
export declare function isDevelopment(): boolean;
|
|
9
|
+
export declare function getEnvFlag(key: string): boolean;
|
|
10
|
+
export declare function isDebug(): boolean;
|
|
11
|
+
export declare function setEnv(key: string, value: string): void;
|
|
12
|
+
export declare function isEdgeRuntime(): boolean;
|
|
13
|
+
export declare function isServerless(): boolean;
|
|
14
|
+
export declare function supportsAnsi(): boolean;
|
package/env/node-env.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// libs/utils/src/env/node-env.ts
|
|
21
|
+
var node_env_exports = {};
|
|
22
|
+
__export(node_env_exports, {
|
|
23
|
+
getCwd: () => getCwd,
|
|
24
|
+
getEnv: () => getEnv,
|
|
25
|
+
getEnvFlag: () => getEnvFlag,
|
|
26
|
+
isDebug: () => isDebug,
|
|
27
|
+
isDevelopment: () => isDevelopment,
|
|
28
|
+
isEdgeRuntime: () => isEdgeRuntime,
|
|
29
|
+
isProduction: () => isProduction,
|
|
30
|
+
isServerless: () => isServerless,
|
|
31
|
+
setEnv: () => setEnv,
|
|
32
|
+
supportsAnsi: () => supportsAnsi
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(node_env_exports);
|
|
35
|
+
function getEnv(key, defaultValue) {
|
|
36
|
+
return process.env[key] ?? defaultValue;
|
|
37
|
+
}
|
|
38
|
+
function getCwd() {
|
|
39
|
+
return process.cwd();
|
|
40
|
+
}
|
|
41
|
+
function isProduction() {
|
|
42
|
+
return process.env["NODE_ENV"] === "production";
|
|
43
|
+
}
|
|
44
|
+
function isDevelopment() {
|
|
45
|
+
return process.env["NODE_ENV"] === "development";
|
|
46
|
+
}
|
|
47
|
+
function getEnvFlag(key) {
|
|
48
|
+
const v = process.env[key];
|
|
49
|
+
return v === "1" || v === "true";
|
|
50
|
+
}
|
|
51
|
+
function isDebug() {
|
|
52
|
+
return getEnvFlag("DEBUG");
|
|
53
|
+
}
|
|
54
|
+
function setEnv(key, value) {
|
|
55
|
+
process.env[key] = value;
|
|
56
|
+
}
|
|
57
|
+
function isEdgeRuntime() {
|
|
58
|
+
if (typeof globalThis !== "undefined" && "EdgeRuntime" in globalThis) return true;
|
|
59
|
+
if (typeof globalThis !== "undefined" && "caches" in globalThis && !("window" in globalThis)) return true;
|
|
60
|
+
return process.env["EDGE_RUNTIME"] !== void 0 && process.env["VERCEL_ENV"] !== void 0;
|
|
61
|
+
}
|
|
62
|
+
function isServerless() {
|
|
63
|
+
return !!(process.env["VERCEL"] || process.env["NETLIFY"] || process.env["CF_PAGES"] || process.env["AWS_LAMBDA_FUNCTION_NAME"] || process.env["AZURE_FUNCTIONS_ENVIRONMENT"] || process.env["K_SERVICE"] || process.env["RAILWAY_ENVIRONMENT"] || process.env["RENDER"] || process.env["FLY_APP_NAME"]);
|
|
64
|
+
}
|
|
65
|
+
function supportsAnsi() {
|
|
66
|
+
if (process.env["NO_COLOR"]) return false;
|
|
67
|
+
const forceColor = process.env["FORCE_COLOR"];
|
|
68
|
+
if (forceColor !== void 0) {
|
|
69
|
+
return forceColor !== "0" && forceColor.toLowerCase() !== "false";
|
|
70
|
+
}
|
|
71
|
+
return process.stdout?.isTTY === true;
|
|
72
|
+
}
|
|
73
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
+
0 && (module.exports = {
|
|
75
|
+
getCwd,
|
|
76
|
+
getEnv,
|
|
77
|
+
getEnvFlag,
|
|
78
|
+
isDebug,
|
|
79
|
+
isDevelopment,
|
|
80
|
+
isEdgeRuntime,
|
|
81
|
+
isProduction,
|
|
82
|
+
isServerless,
|
|
83
|
+
setEnv,
|
|
84
|
+
supportsAnsi
|
|
85
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// libs/utils/src/async-context/browser-async-context.ts
|
|
2
|
+
var AsyncLocalStorage = class {
|
|
3
|
+
stack = [];
|
|
4
|
+
run(store, callback, ...args) {
|
|
5
|
+
this.stack.push(store);
|
|
6
|
+
try {
|
|
7
|
+
const result = callback(...args);
|
|
8
|
+
if (result instanceof Promise) {
|
|
9
|
+
return result.finally(() => this.stack.pop());
|
|
10
|
+
}
|
|
11
|
+
this.stack.pop();
|
|
12
|
+
return result;
|
|
13
|
+
} catch (err) {
|
|
14
|
+
this.stack.pop();
|
|
15
|
+
throw err;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
getStore() {
|
|
19
|
+
return this.stack.length > 0 ? this.stack[this.stack.length - 1] : void 0;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export {
|
|
23
|
+
AsyncLocalStorage
|
|
24
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// libs/utils/src/crypto/browser.ts
|
|
2
|
+
import { sha256 as sha256Hash } from "@noble/hashes/sha2.js";
|
|
3
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
4
|
+
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
5
|
+
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils.js";
|
|
6
|
+
import { gcm } from "@noble/ciphers/aes.js";
|
|
7
|
+
|
|
8
|
+
// libs/utils/src/crypto/jwt-alg.ts
|
|
9
|
+
function isRsaPssAlg(jwtAlg) {
|
|
10
|
+
return jwtAlg.startsWith("PS");
|
|
11
|
+
}
|
|
12
|
+
var JWT_ALG_TO_WEB_CRYPTO = {
|
|
13
|
+
RS256: "SHA-256",
|
|
14
|
+
RS384: "SHA-384",
|
|
15
|
+
RS512: "SHA-512",
|
|
16
|
+
PS256: "SHA-256",
|
|
17
|
+
PS384: "SHA-384",
|
|
18
|
+
PS512: "SHA-512"
|
|
19
|
+
};
|
|
20
|
+
function jwtAlgToWebCryptoAlg(jwtAlg) {
|
|
21
|
+
const webAlg = JWT_ALG_TO_WEB_CRYPTO[jwtAlg];
|
|
22
|
+
if (!webAlg) {
|
|
23
|
+
throw new Error(`Unsupported JWT algorithm for WebCrypto: ${jwtAlg}`);
|
|
24
|
+
}
|
|
25
|
+
return webAlg;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// libs/utils/src/crypto/browser.ts
|
|
29
|
+
function toBytes(data) {
|
|
30
|
+
if (typeof data === "string") {
|
|
31
|
+
return new TextEncoder().encode(data);
|
|
32
|
+
}
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function toHex(bytes) {
|
|
36
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
37
|
+
}
|
|
38
|
+
function generateUUID() {
|
|
39
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
40
|
+
return crypto.randomUUID();
|
|
41
|
+
}
|
|
42
|
+
const bytes = nobleRandomBytes(16);
|
|
43
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
44
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
45
|
+
const hex = toHex(bytes);
|
|
46
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
47
|
+
}
|
|
48
|
+
function constantTimeEqual(a, b) {
|
|
49
|
+
if (a.length !== b.length) return false;
|
|
50
|
+
let result = 0;
|
|
51
|
+
for (let i = 0; i < a.length; i++) {
|
|
52
|
+
result |= a[i] ^ b[i];
|
|
53
|
+
}
|
|
54
|
+
return result === 0;
|
|
55
|
+
}
|
|
56
|
+
var browserCrypto = {
|
|
57
|
+
randomUUID() {
|
|
58
|
+
return generateUUID();
|
|
59
|
+
},
|
|
60
|
+
randomBytes(length) {
|
|
61
|
+
return nobleRandomBytes(length);
|
|
62
|
+
},
|
|
63
|
+
sha256(data) {
|
|
64
|
+
return sha256Hash(toBytes(data));
|
|
65
|
+
},
|
|
66
|
+
sha256Hex(data) {
|
|
67
|
+
return toHex(sha256Hash(toBytes(data)));
|
|
68
|
+
},
|
|
69
|
+
hmacSha256(key, data) {
|
|
70
|
+
return hmac(sha256Hash, key, data);
|
|
71
|
+
},
|
|
72
|
+
hkdfSha256(ikm, salt, info, length) {
|
|
73
|
+
const effectiveSalt = salt.length > 0 ? salt : new Uint8Array(32);
|
|
74
|
+
return hkdf(sha256Hash, ikm, effectiveSalt, info, length);
|
|
75
|
+
},
|
|
76
|
+
encryptAesGcm(key, plaintext, iv) {
|
|
77
|
+
const cipher = gcm(key, iv);
|
|
78
|
+
const sealed = cipher.encrypt(plaintext);
|
|
79
|
+
const ciphertext = sealed.slice(0, -16);
|
|
80
|
+
const tag = sealed.slice(-16);
|
|
81
|
+
return { ciphertext, tag };
|
|
82
|
+
},
|
|
83
|
+
decryptAesGcm(key, ciphertext, iv, tag) {
|
|
84
|
+
const cipher = gcm(key, iv);
|
|
85
|
+
const sealed = new Uint8Array(ciphertext.length + tag.length);
|
|
86
|
+
sealed.set(ciphertext);
|
|
87
|
+
sealed.set(tag, ciphertext.length);
|
|
88
|
+
return cipher.decrypt(sealed);
|
|
89
|
+
},
|
|
90
|
+
timingSafeEqual(a, b) {
|
|
91
|
+
return constantTimeEqual(a, b);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
async function rsaVerifyBrowser(jwtAlg, data, publicJwk, signature) {
|
|
95
|
+
if (typeof globalThis.crypto?.subtle === "undefined") {
|
|
96
|
+
throw new Error("WebCrypto API (crypto.subtle) is not available in this environment");
|
|
97
|
+
}
|
|
98
|
+
const webAlg = jwtAlgToWebCryptoAlg(jwtAlg);
|
|
99
|
+
const isPss = isRsaPssAlg(jwtAlg);
|
|
100
|
+
const algorithm = {
|
|
101
|
+
name: isPss ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
|
|
102
|
+
hash: { name: webAlg }
|
|
103
|
+
};
|
|
104
|
+
const key = await globalThis.crypto.subtle.importKey("jwk", publicJwk, algorithm, false, ["verify"]);
|
|
105
|
+
const verifyAlgorithm = isPss ? { name: "RSA-PSS", saltLength: getSaltLength(jwtAlg) } : { name: "RSASSA-PKCS1-v1_5" };
|
|
106
|
+
const sigBuf = new Uint8Array(signature).buffer;
|
|
107
|
+
const dataBuf = new Uint8Array(data).buffer;
|
|
108
|
+
return globalThis.crypto.subtle.verify(verifyAlgorithm, key, sigBuf, dataBuf);
|
|
109
|
+
}
|
|
110
|
+
function getSaltLength(jwtAlg) {
|
|
111
|
+
switch (jwtAlg) {
|
|
112
|
+
case "PS256":
|
|
113
|
+
return 32;
|
|
114
|
+
// SHA-256 digest length
|
|
115
|
+
case "PS384":
|
|
116
|
+
return 48;
|
|
117
|
+
// SHA-384 digest length
|
|
118
|
+
case "PS512":
|
|
119
|
+
return 64;
|
|
120
|
+
// SHA-512 digest length
|
|
121
|
+
default:
|
|
122
|
+
return 32;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export {
|
|
126
|
+
browserCrypto,
|
|
127
|
+
browserCrypto as cryptoProvider,
|
|
128
|
+
isRsaPssAlg,
|
|
129
|
+
jwtAlgToWebCryptoAlg,
|
|
130
|
+
rsaVerifyBrowser
|
|
131
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// libs/utils/src/crypto/node.ts
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
|
|
4
|
+
// libs/utils/src/crypto/jwt-alg.ts
|
|
5
|
+
var JWT_ALG_TO_NODE_DIGEST = {
|
|
6
|
+
RS256: "RSA-SHA256",
|
|
7
|
+
RS384: "RSA-SHA384",
|
|
8
|
+
RS512: "RSA-SHA512",
|
|
9
|
+
// For RSA-PSS, Node's crypto.sign/verify uses the digest algorithm + explicit PSS padding options.
|
|
10
|
+
PS256: "RSA-SHA256",
|
|
11
|
+
PS384: "RSA-SHA384",
|
|
12
|
+
PS512: "RSA-SHA512"
|
|
13
|
+
};
|
|
14
|
+
function jwtAlgToNodeAlg(jwtAlg) {
|
|
15
|
+
const nodeAlg = JWT_ALG_TO_NODE_DIGEST[jwtAlg];
|
|
16
|
+
if (!nodeAlg) {
|
|
17
|
+
throw new Error(`Unsupported JWT algorithm: ${jwtAlg}`);
|
|
18
|
+
}
|
|
19
|
+
return nodeAlg;
|
|
20
|
+
}
|
|
21
|
+
function isRsaPssAlg(jwtAlg) {
|
|
22
|
+
return jwtAlg.startsWith("PS");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// libs/utils/src/crypto/node.ts
|
|
26
|
+
function toUint8Array(buf) {
|
|
27
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
28
|
+
}
|
|
29
|
+
function toBuffer(data) {
|
|
30
|
+
if (typeof data === "string") {
|
|
31
|
+
return Buffer.from(data, "utf8");
|
|
32
|
+
}
|
|
33
|
+
return Buffer.from(data);
|
|
34
|
+
}
|
|
35
|
+
var nodeCrypto = {
|
|
36
|
+
randomUUID() {
|
|
37
|
+
return crypto.randomUUID();
|
|
38
|
+
},
|
|
39
|
+
randomBytes(length) {
|
|
40
|
+
return toUint8Array(crypto.randomBytes(length));
|
|
41
|
+
},
|
|
42
|
+
sha256(data) {
|
|
43
|
+
const hash = crypto.createHash("sha256").update(toBuffer(data)).digest();
|
|
44
|
+
return toUint8Array(hash);
|
|
45
|
+
},
|
|
46
|
+
sha256Hex(data) {
|
|
47
|
+
return crypto.createHash("sha256").update(toBuffer(data)).digest("hex");
|
|
48
|
+
},
|
|
49
|
+
hmacSha256(key, data) {
|
|
50
|
+
const hmac = crypto.createHmac("sha256", Buffer.from(key)).update(Buffer.from(data)).digest();
|
|
51
|
+
return toUint8Array(hmac);
|
|
52
|
+
},
|
|
53
|
+
hkdfSha256(ikm, salt, info, length) {
|
|
54
|
+
const ikmBuf = Buffer.from(ikm);
|
|
55
|
+
const saltBuf = salt.length > 0 ? Buffer.from(salt) : Buffer.alloc(32);
|
|
56
|
+
const prk = crypto.createHmac("sha256", saltBuf).update(ikmBuf).digest();
|
|
57
|
+
const hashLen = 32;
|
|
58
|
+
const n = Math.ceil(length / hashLen);
|
|
59
|
+
const chunks = [];
|
|
60
|
+
let prev = Buffer.alloc(0);
|
|
61
|
+
for (let i = 1; i <= n; i++) {
|
|
62
|
+
prev = crypto.createHmac("sha256", prk).update(Buffer.concat([prev, Buffer.from(info), Buffer.from([i])])).digest();
|
|
63
|
+
chunks.push(prev);
|
|
64
|
+
}
|
|
65
|
+
return toUint8Array(Buffer.concat(chunks).subarray(0, length));
|
|
66
|
+
},
|
|
67
|
+
encryptAesGcm(key, plaintext, iv) {
|
|
68
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", Buffer.from(key), Buffer.from(iv));
|
|
69
|
+
const encrypted = Buffer.concat([cipher.update(Buffer.from(plaintext)), cipher.final()]);
|
|
70
|
+
const tag = cipher.getAuthTag();
|
|
71
|
+
return {
|
|
72
|
+
ciphertext: toUint8Array(encrypted),
|
|
73
|
+
tag: toUint8Array(tag)
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
decryptAesGcm(key, ciphertext, iv, tag) {
|
|
77
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", Buffer.from(key), Buffer.from(iv));
|
|
78
|
+
decipher.setAuthTag(Buffer.from(tag));
|
|
79
|
+
const decrypted = Buffer.concat([decipher.update(Buffer.from(ciphertext)), decipher.final()]);
|
|
80
|
+
return toUint8Array(decrypted);
|
|
81
|
+
},
|
|
82
|
+
timingSafeEqual(a, b) {
|
|
83
|
+
if (a.length !== b.length) return false;
|
|
84
|
+
return crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
function generateRsaKeyPair(modulusLength = 2048, alg = "RS256") {
|
|
88
|
+
const kid = `rsa-key-${Date.now()}-${crypto.randomBytes(8).toString("hex")}`;
|
|
89
|
+
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", {
|
|
90
|
+
modulusLength
|
|
91
|
+
});
|
|
92
|
+
const exported = publicKey.export({ format: "jwk" });
|
|
93
|
+
const publicJwk = {
|
|
94
|
+
...exported,
|
|
95
|
+
kid,
|
|
96
|
+
alg,
|
|
97
|
+
use: "sig",
|
|
98
|
+
kty: "RSA"
|
|
99
|
+
};
|
|
100
|
+
return { privateKey, publicKey, publicJwk };
|
|
101
|
+
}
|
|
102
|
+
function rsaSign(algorithm, data, privateKey, options) {
|
|
103
|
+
const signingKey = options ? { key: privateKey, ...options } : privateKey;
|
|
104
|
+
return crypto.sign(algorithm, data, signingKey);
|
|
105
|
+
}
|
|
106
|
+
function rsaVerify(jwtAlg, data, publicJwk, signature) {
|
|
107
|
+
const publicKey = crypto.createPublicKey({ key: publicJwk, format: "jwk" });
|
|
108
|
+
const nodeAlgorithm = jwtAlgToNodeAlg(jwtAlg);
|
|
109
|
+
const verifyKey = isRsaPssAlg(jwtAlg) ? {
|
|
110
|
+
key: publicKey,
|
|
111
|
+
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
112
|
+
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
|
|
113
|
+
} : publicKey;
|
|
114
|
+
return crypto.verify(nodeAlgorithm, data, verifyKey, signature);
|
|
115
|
+
}
|
|
116
|
+
function createSignedJwt(payload, privateKey, kid, alg = "RS256") {
|
|
117
|
+
const header = { alg, typ: "JWT", kid };
|
|
118
|
+
const headerB64 = Buffer.from(JSON.stringify(header)).toString("base64url");
|
|
119
|
+
const payloadB64 = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
120
|
+
const signatureInput = `${headerB64}.${payloadB64}`;
|
|
121
|
+
const nodeAlgorithm = jwtAlgToNodeAlg(alg);
|
|
122
|
+
const signature = rsaSign(
|
|
123
|
+
nodeAlgorithm,
|
|
124
|
+
Buffer.from(signatureInput),
|
|
125
|
+
privateKey,
|
|
126
|
+
isRsaPssAlg(alg) ? {
|
|
127
|
+
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
128
|
+
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
|
|
129
|
+
} : void 0
|
|
130
|
+
);
|
|
131
|
+
const signatureB64 = signature.toString("base64url");
|
|
132
|
+
return `${headerB64}.${payloadB64}.${signatureB64}`;
|
|
133
|
+
}
|
|
134
|
+
export {
|
|
135
|
+
createSignedJwt,
|
|
136
|
+
nodeCrypto as cryptoProvider,
|
|
137
|
+
generateRsaKeyPair,
|
|
138
|
+
isRsaPssAlg,
|
|
139
|
+
jwtAlgToNodeAlg,
|
|
140
|
+
nodeCrypto,
|
|
141
|
+
rsaSign,
|
|
142
|
+
rsaVerify
|
|
143
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// libs/utils/src/env/browser-env.ts
|
|
2
|
+
function getEnv(_key, defaultValue) {
|
|
3
|
+
return defaultValue;
|
|
4
|
+
}
|
|
5
|
+
function getCwd() {
|
|
6
|
+
return "/";
|
|
7
|
+
}
|
|
8
|
+
function isProduction() {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
function isDevelopment() {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
function getEnvFlag(_key) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
function isDebug() {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
function setEnv(_key, _value) {
|
|
21
|
+
}
|
|
22
|
+
function isEdgeRuntime() {
|
|
23
|
+
if (typeof globalThis !== "undefined" && "EdgeRuntime" in globalThis) return true;
|
|
24
|
+
if (typeof globalThis !== "undefined" && "caches" in globalThis && !("window" in globalThis)) return true;
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function isServerless() {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
function supportsAnsi() {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
getCwd,
|
|
35
|
+
getEnv,
|
|
36
|
+
getEnvFlag,
|
|
37
|
+
isDebug,
|
|
38
|
+
isDevelopment,
|
|
39
|
+
isEdgeRuntime,
|
|
40
|
+
isProduction,
|
|
41
|
+
isServerless,
|
|
42
|
+
setEnv,
|
|
43
|
+
supportsAnsi
|
|
44
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// libs/utils/src/env/node-env.ts
|
|
2
|
+
function getEnv(key, defaultValue) {
|
|
3
|
+
return process.env[key] ?? defaultValue;
|
|
4
|
+
}
|
|
5
|
+
function getCwd() {
|
|
6
|
+
return process.cwd();
|
|
7
|
+
}
|
|
8
|
+
function isProduction() {
|
|
9
|
+
return process.env["NODE_ENV"] === "production";
|
|
10
|
+
}
|
|
11
|
+
function isDevelopment() {
|
|
12
|
+
return process.env["NODE_ENV"] === "development";
|
|
13
|
+
}
|
|
14
|
+
function getEnvFlag(key) {
|
|
15
|
+
const v = process.env[key];
|
|
16
|
+
return v === "1" || v === "true";
|
|
17
|
+
}
|
|
18
|
+
function isDebug() {
|
|
19
|
+
return getEnvFlag("DEBUG");
|
|
20
|
+
}
|
|
21
|
+
function setEnv(key, value) {
|
|
22
|
+
process.env[key] = value;
|
|
23
|
+
}
|
|
24
|
+
function isEdgeRuntime() {
|
|
25
|
+
if (typeof globalThis !== "undefined" && "EdgeRuntime" in globalThis) return true;
|
|
26
|
+
if (typeof globalThis !== "undefined" && "caches" in globalThis && !("window" in globalThis)) return true;
|
|
27
|
+
return process.env["EDGE_RUNTIME"] !== void 0 && process.env["VERCEL_ENV"] !== void 0;
|
|
28
|
+
}
|
|
29
|
+
function isServerless() {
|
|
30
|
+
return !!(process.env["VERCEL"] || process.env["NETLIFY"] || process.env["CF_PAGES"] || process.env["AWS_LAMBDA_FUNCTION_NAME"] || process.env["AZURE_FUNCTIONS_ENVIRONMENT"] || process.env["K_SERVICE"] || process.env["RAILWAY_ENVIRONMENT"] || process.env["RENDER"] || process.env["FLY_APP_NAME"]);
|
|
31
|
+
}
|
|
32
|
+
function supportsAnsi() {
|
|
33
|
+
if (process.env["NO_COLOR"]) return false;
|
|
34
|
+
const forceColor = process.env["FORCE_COLOR"];
|
|
35
|
+
if (forceColor !== void 0) {
|
|
36
|
+
return forceColor !== "0" && forceColor.toLowerCase() !== "false";
|
|
37
|
+
}
|
|
38
|
+
return process.stdout?.isTTY === true;
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
getCwd,
|
|
42
|
+
getEnv,
|
|
43
|
+
getEnvFlag,
|
|
44
|
+
isDebug,
|
|
45
|
+
isDevelopment,
|
|
46
|
+
isEdgeRuntime,
|
|
47
|
+
isProduction,
|
|
48
|
+
isServerless,
|
|
49
|
+
setEnv,
|
|
50
|
+
supportsAnsi
|
|
51
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// libs/utils/src/event-emitter/browser-event-emitter.ts
|
|
2
|
+
var EventEmitter = class {
|
|
3
|
+
events = /* @__PURE__ */ new Map();
|
|
4
|
+
emit(event, ...args) {
|
|
5
|
+
const handlers = this.events.get(event);
|
|
6
|
+
if (!handlers || handlers.length === 0) return false;
|
|
7
|
+
for (const handler of [...handlers]) {
|
|
8
|
+
handler(...args);
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
on(event, handler) {
|
|
13
|
+
const list = this.events.get(event) ?? [];
|
|
14
|
+
list.push(handler);
|
|
15
|
+
this.events.set(event, list);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
off(event, handler) {
|
|
19
|
+
const list = this.events.get(event);
|
|
20
|
+
if (list) {
|
|
21
|
+
const idx = list.indexOf(handler);
|
|
22
|
+
if (idx >= 0) list.splice(idx, 1);
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
addListener(event, handler) {
|
|
27
|
+
return this.on(event, handler);
|
|
28
|
+
}
|
|
29
|
+
removeListener(event, handler) {
|
|
30
|
+
return this.off(event, handler);
|
|
31
|
+
}
|
|
32
|
+
removeAllListeners(event) {
|
|
33
|
+
if (event !== void 0) {
|
|
34
|
+
this.events.delete(event);
|
|
35
|
+
} else {
|
|
36
|
+
this.events.clear();
|
|
37
|
+
}
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
setMaxListeners(_n) {
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
listenerCount(event) {
|
|
44
|
+
return this.events.get(event)?.length ?? 0;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
export {
|
|
48
|
+
EventEmitter
|
|
49
|
+
};
|