@frontmcp/utils 1.10.16 → 1.11.0-rc.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/README.md +1 -0
- package/browser/async-context/browser-async-context.mjs +177 -0
- package/browser/async-context/index.mjs +15 -0
- package/browser/async-context/overlap-error.mjs +10 -0
- package/browser/class-brand/class-brand.mjs +27 -0
- package/browser/class-brand/index.mjs +6 -0
- package/browser/content/content.mjs +151 -0
- package/browser/content/index.mjs +8 -0
- package/browser/crypto/browser.mjs +103 -0
- package/browser/crypto/encrypted-blob.mjs +119 -0
- package/browser/crypto/hmac-signing.mjs +70 -0
- package/browser/crypto/index.mjs +281 -0
- package/browser/crypto/jwt-alg.mjs +40 -0
- package/browser/crypto/key-persistence/factory.mjs +134 -0
- package/browser/crypto/key-persistence/index.mjs +26 -0
- package/browser/crypto/key-persistence/key-persistence.mjs +185 -0
- package/browser/crypto/key-persistence/schemas.mjs +82 -0
- package/browser/crypto/pkce/index.mjs +25 -0
- package/browser/crypto/pkce/pkce.mjs +59 -0
- package/browser/crypto/runtime.mjs +17 -0
- package/browser/crypto/secret-persistence/index.mjs +29 -0
- package/browser/crypto/secret-persistence/persistence.mjs +181 -0
- package/browser/crypto/secret-persistence/schema.mjs +40 -0
- package/browser/crypto/web-rsa.mjs +36 -0
- package/browser/env/browser-env.mjs +56 -0
- package/browser/env/browser-runtime-context.mjs +85 -0
- package/browser/env/build-target.mjs +25 -0
- package/browser/env/edge-scope.mjs +17 -0
- package/browser/env/index.mjs +47 -0
- package/browser/env/provider.mjs +46 -0
- package/browser/escape/escape.mjs +43 -0
- package/browser/escape/index.mjs +9 -0
- package/browser/event-emitter/browser-event-emitter.mjs +49 -0
- package/browser/event-emitter/index.mjs +5 -0
- package/browser/fs/fs.mjs +227 -0
- package/browser/fs/index.mjs +65 -0
- package/browser/http/cookie.mjs +85 -0
- package/browser/http/http.mjs +18 -0
- package/browser/http/index.mjs +11 -0
- package/browser/index.mjs +526 -0
- package/browser/llm/index.mjs +5 -0
- package/browser/llm/llm-tool-handlers.mjs +55 -0
- package/browser/machine-id/index.mjs +7 -0
- package/browser/machine-id/machine-id.mjs +108 -0
- package/browser/naming/index.mjs +10 -0
- package/browser/naming/naming.mjs +68 -0
- package/browser/net/index.mjs +10 -0
- package/browser/net/ip-address.mjs +128 -0
- package/browser/net/redirect.mjs +7 -0
- package/browser/path/browser-path.mjs +47 -0
- package/browser/path/index.mjs +24 -0
- package/browser/path/path.mjs +13 -0
- package/browser/process/child-process.mjs +23 -0
- package/browser/process/index.mjs +15 -0
- package/browser/process/owned-process.mjs +175 -0
- package/browser/process/process-tree.mjs +149 -0
- package/browser/process/process-watch.mjs +48 -0
- package/browser/redaction/index.mjs +6 -0
- package/browser/redaction/redaction.mjs +74 -0
- package/browser/regex/index.mjs +3 -0
- package/browser/regex/patterns.mjs +167 -0
- package/browser/regex/safe-regex.mjs +93 -0
- package/browser/serialization/index.mjs +5 -0
- package/browser/serialization/serialization.mjs +22 -0
- package/browser/stdio-bridge/errors.mjs +27 -0
- package/browser/stdio-bridge/index.mjs +24 -0
- package/browser/stdio-bridge/list-changed.mjs +21 -0
- package/browser/stdio-bridge/state-machine.mjs +211 -0
- package/browser/stdio-bridge/stdio-framer.mjs +91 -0
- package/browser/stdio-bridge/upstream-client.mjs +189 -0
- package/browser/storage/adapters/base.mjs +139 -0
- package/browser/storage/adapters/cloudflare-kv.mjs +149 -0
- package/browser/storage/adapters/filesystem.mjs +279 -0
- package/browser/storage/adapters/index.mjs +23 -0
- package/browser/storage/adapters/indexeddb.mjs +198 -0
- package/browser/storage/adapters/localstorage.mjs +222 -0
- package/browser/storage/adapters/memory.mjs +350 -0
- package/browser/storage/adapters/redis.mjs +497 -0
- package/browser/storage/adapters/upstash.mjs +299 -0
- package/browser/storage/adapters/vercel-kv.mjs +226 -0
- package/browser/storage/change-feed.mjs +189 -0
- package/browser/storage/encrypted-typed-storage.mjs +329 -0
- package/browser/storage/errors.mjs +92 -0
- package/browser/storage/factory.mjs +155 -0
- package/browser/storage/index.mjs +96 -0
- package/browser/storage/namespace.mjs +186 -0
- package/browser/storage/redis-error-listener.mjs +32 -0
- package/browser/storage/redis-url.mjs +66 -0
- package/browser/storage/typed-storage.mjs +171 -0
- package/browser/storage/utils/compare-and-delete.mjs +9 -0
- package/browser/storage/utils/index.mjs +24 -0
- package/browser/storage/utils/pattern.mjs +68 -0
- package/browser/storage/utils/ttl.mjs +48 -0
- package/browser/uri/index.mjs +19 -0
- package/browser/uri/uri-template.mjs +86 -0
- package/browser/uri/uri-validation.mjs +18 -0
- package/crypto/browser.d.ts +1 -13
- package/crypto/browser.js +1 -1
- package/crypto/node.js +1 -1
- package/crypto/web-rsa.d.ts +13 -0
- package/env/browser-runtime-context.js +1 -1
- package/env/runtime-context.js +1 -1
- package/esm/crypto/browser.mjs +1 -1
- package/esm/crypto/node.mjs +1 -1
- package/esm/env/browser-runtime-context.mjs +1 -1
- package/esm/env/runtime-context.mjs +1 -1
- package/esm/index.mjs +1 -1
- package/fs/fs.d.ts +20 -4
- package/fs/index.d.ts +1 -1
- package/index.d.ts +9 -3
- package/index.js +1 -1
- package/package.json +24 -8
- package/process/child-process.d.ts +2 -0
- package/process/index.d.ts +6 -0
- package/process/owned-process.d.ts +59 -0
- package/process/process-tree.d.ts +78 -0
- package/process/process-watch.d.ts +41 -0
- package/redaction/index.d.ts +2 -0
- package/redaction/redaction.d.ts +24 -0
- package/stdio-bridge/errors.d.ts +23 -0
- package/stdio-bridge/index.d.ts +9 -0
- package/stdio-bridge/list-changed.d.ts +3 -0
- package/stdio-bridge/state-machine.d.ts +61 -0
- package/stdio-bridge/stdio-framer.d.ts +49 -0
- package/stdio-bridge/types.d.ts +10 -0
- package/stdio-bridge/upstream-client.d.ts +66 -0
- package/storage/adapters/base.d.ts +2 -0
- package/storage/adapters/memory.d.ts +1 -0
- package/storage/adapters/redis.d.ts +3 -0
- package/storage/change-feed.d.ts +47 -0
- package/storage/factory.d.ts +6 -0
- package/storage/index.d.ts +3 -1
- package/storage/namespace.d.ts +2 -0
- package/storage/types.d.ts +46 -1
- package/storage/utils/compare-and-delete.d.ts +2 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// libs/utils/src/env/index.ts
|
|
2
|
+
import {
|
|
3
|
+
getEnv,
|
|
4
|
+
getCwd,
|
|
5
|
+
getNodeEnv,
|
|
6
|
+
isProduction,
|
|
7
|
+
isDevelopment,
|
|
8
|
+
getEnvFlag,
|
|
9
|
+
isDebug,
|
|
10
|
+
setEnv,
|
|
11
|
+
isEdgeRuntime,
|
|
12
|
+
isServerless,
|
|
13
|
+
supportsAnsi
|
|
14
|
+
} from "./browser-env.mjs";
|
|
15
|
+
import {
|
|
16
|
+
getRuntimeContext,
|
|
17
|
+
resetRuntimeContext,
|
|
18
|
+
detectRuntimeContext,
|
|
19
|
+
isEntryAvailable,
|
|
20
|
+
checkEntryAvailability,
|
|
21
|
+
entryAvailabilitySchema
|
|
22
|
+
} from "./browser-runtime-context.mjs";
|
|
23
|
+
import { detectProvider, resetProviderCacheForTesting } from "./provider.mjs";
|
|
24
|
+
import { getBuildTarget, resetBuildTargetCacheForTesting } from "./build-target.mjs";
|
|
25
|
+
export {
|
|
26
|
+
checkEntryAvailability,
|
|
27
|
+
detectProvider,
|
|
28
|
+
detectRuntimeContext,
|
|
29
|
+
entryAvailabilitySchema,
|
|
30
|
+
getBuildTarget,
|
|
31
|
+
getCwd,
|
|
32
|
+
getEnv,
|
|
33
|
+
getEnvFlag,
|
|
34
|
+
getNodeEnv,
|
|
35
|
+
getRuntimeContext,
|
|
36
|
+
isDebug,
|
|
37
|
+
isDevelopment,
|
|
38
|
+
isEdgeRuntime,
|
|
39
|
+
isEntryAvailable,
|
|
40
|
+
isProduction,
|
|
41
|
+
isServerless,
|
|
42
|
+
resetBuildTargetCacheForTesting,
|
|
43
|
+
resetProviderCacheForTesting,
|
|
44
|
+
resetRuntimeContext,
|
|
45
|
+
setEnv,
|
|
46
|
+
supportsAnsi
|
|
47
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : undefined;
|
|
2
|
+
const require = __frontmcpModule ? __frontmcpModule.createRequire(import.meta.url || 'file:///') : undefined;
|
|
3
|
+
// __frontmcpCreateRequire
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
+
}) : x)(function(x) {
|
|
7
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
8
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// libs/utils/src/env/provider.ts
|
|
12
|
+
var cached;
|
|
13
|
+
function detectProvider() {
|
|
14
|
+
if (cached) return cached;
|
|
15
|
+
cached = detectProviderUncached();
|
|
16
|
+
return cached;
|
|
17
|
+
}
|
|
18
|
+
function detectProviderUncached() {
|
|
19
|
+
const explicit = process.env["FRONTMCP_PROVIDER"];
|
|
20
|
+
if (explicit && isKnownProvider(explicit)) return explicit;
|
|
21
|
+
if (process.env["VERCEL"]) return "vercel";
|
|
22
|
+
if (process.env["AWS_LAMBDA_FUNCTION_NAME"]) return "lambda";
|
|
23
|
+
if (process.env["CF_PAGES"]) return "cloudflare";
|
|
24
|
+
if (process.env["NETLIFY"]) return "netlify";
|
|
25
|
+
if (process.env["AZURE_FUNCTIONS_ENVIRONMENT"]) return "azure";
|
|
26
|
+
if (process.env["K_SERVICE"]) return "gcp";
|
|
27
|
+
if (process.env["FLY_APP_NAME"]) return "fly";
|
|
28
|
+
if (process.env["RENDER"]) return "render";
|
|
29
|
+
if (process.env["RAILWAY_ENVIRONMENT"]) return "railway";
|
|
30
|
+
try {
|
|
31
|
+
const nodeFs = __require("node:fs");
|
|
32
|
+
if (nodeFs.existsSync("/.dockerenv")) return "docker";
|
|
33
|
+
} catch {
|
|
34
|
+
}
|
|
35
|
+
return "bare";
|
|
36
|
+
}
|
|
37
|
+
function isKnownProvider(value) {
|
|
38
|
+
return value === "bare" || value === "docker" || value === "vercel" || value === "lambda" || value === "cloudflare" || value === "netlify" || value === "azure" || value === "gcp" || value === "fly" || value === "render" || value === "railway";
|
|
39
|
+
}
|
|
40
|
+
function resetProviderCacheForTesting() {
|
|
41
|
+
cached = void 0;
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
detectProvider,
|
|
45
|
+
resetProviderCacheForTesting
|
|
46
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// libs/utils/src/escape/escape.ts
|
|
2
|
+
function escapeHtml(str) {
|
|
3
|
+
if (str === null || str === void 0) {
|
|
4
|
+
return "";
|
|
5
|
+
}
|
|
6
|
+
const s = String(str);
|
|
7
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
8
|
+
}
|
|
9
|
+
function escapeHtmlAttr(str) {
|
|
10
|
+
return str.replace(/&/g, "&").replace(/"/g, """);
|
|
11
|
+
}
|
|
12
|
+
function escapeJsString(str) {
|
|
13
|
+
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
14
|
+
}
|
|
15
|
+
function escapeScriptClose(jsonString) {
|
|
16
|
+
return jsonString.replace(/<\//g, "<\\/");
|
|
17
|
+
}
|
|
18
|
+
function safeJsonForScript(value) {
|
|
19
|
+
if (value === void 0) {
|
|
20
|
+
return "null";
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const jsonString = JSON.stringify(value, (_key, val) => {
|
|
24
|
+
if (typeof val === "bigint") {
|
|
25
|
+
return val.toString();
|
|
26
|
+
}
|
|
27
|
+
return val;
|
|
28
|
+
});
|
|
29
|
+
if (jsonString === void 0) {
|
|
30
|
+
return "null";
|
|
31
|
+
}
|
|
32
|
+
return escapeScriptClose(jsonString);
|
|
33
|
+
} catch {
|
|
34
|
+
return '{"error":"Value could not be serialized"}';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
escapeHtml,
|
|
39
|
+
escapeHtmlAttr,
|
|
40
|
+
escapeJsString,
|
|
41
|
+
escapeScriptClose,
|
|
42
|
+
safeJsonForScript
|
|
43
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : undefined;
|
|
2
|
+
const require = __frontmcpModule ? __frontmcpModule.createRequire(import.meta.url || 'file:///') : undefined;
|
|
3
|
+
// __frontmcpCreateRequire
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
+
}) : x)(function(x) {
|
|
7
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
8
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// libs/utils/src/fs/fs.ts
|
|
12
|
+
import { assertNode } from "../crypto/runtime.mjs";
|
|
13
|
+
var _fsp = null;
|
|
14
|
+
var _fs = null;
|
|
15
|
+
var _spawn = null;
|
|
16
|
+
function getFsp() {
|
|
17
|
+
if (!_fsp) {
|
|
18
|
+
assertNode("File system operations");
|
|
19
|
+
_fsp = __require("fs").promises;
|
|
20
|
+
}
|
|
21
|
+
return _fsp;
|
|
22
|
+
}
|
|
23
|
+
function getFs() {
|
|
24
|
+
if (!_fs) {
|
|
25
|
+
assertNode("File system operations");
|
|
26
|
+
_fs = __require("fs");
|
|
27
|
+
}
|
|
28
|
+
return _fs;
|
|
29
|
+
}
|
|
30
|
+
function getSpawn() {
|
|
31
|
+
if (!_spawn) {
|
|
32
|
+
assertNode("Child process operations");
|
|
33
|
+
_spawn = __require("child_process").spawn;
|
|
34
|
+
}
|
|
35
|
+
return _spawn;
|
|
36
|
+
}
|
|
37
|
+
function getSpawnFn() {
|
|
38
|
+
return getSpawn();
|
|
39
|
+
}
|
|
40
|
+
var F_OK = 0;
|
|
41
|
+
async function readFile(p, encoding = "utf8") {
|
|
42
|
+
const fsp = getFsp();
|
|
43
|
+
return fsp.readFile(p, encoding);
|
|
44
|
+
}
|
|
45
|
+
function readFileSync(p, encoding = "utf8") {
|
|
46
|
+
const fs = getFs();
|
|
47
|
+
return fs.readFileSync(p, encoding);
|
|
48
|
+
}
|
|
49
|
+
async function readFileBuffer(p) {
|
|
50
|
+
const fsp = getFsp();
|
|
51
|
+
return fsp.readFile(p);
|
|
52
|
+
}
|
|
53
|
+
async function writeFile(p, content, options) {
|
|
54
|
+
const fsp = getFsp();
|
|
55
|
+
await fsp.writeFile(p, content, { encoding: "utf8", mode: options?.mode, flag: options?.flag });
|
|
56
|
+
}
|
|
57
|
+
async function appendFile(p, content, options) {
|
|
58
|
+
const fsp = getFsp();
|
|
59
|
+
await fsp.appendFile(p, content, { encoding: "utf8", mode: options?.mode });
|
|
60
|
+
}
|
|
61
|
+
async function mkdir(p, options) {
|
|
62
|
+
const fsp = getFsp();
|
|
63
|
+
await fsp.mkdir(p, options);
|
|
64
|
+
}
|
|
65
|
+
function ensureDirSync(p) {
|
|
66
|
+
const fs = getFs();
|
|
67
|
+
fs.mkdirSync(p, { recursive: true });
|
|
68
|
+
}
|
|
69
|
+
async function rename(oldPath, newPath) {
|
|
70
|
+
const fsp = getFsp();
|
|
71
|
+
await fsp.rename(oldPath, newPath);
|
|
72
|
+
}
|
|
73
|
+
async function unlink(p) {
|
|
74
|
+
const fsp = getFsp();
|
|
75
|
+
await fsp.unlink(p);
|
|
76
|
+
}
|
|
77
|
+
async function fileExists(p) {
|
|
78
|
+
try {
|
|
79
|
+
const fsp = getFsp();
|
|
80
|
+
await fsp.access(p, F_OK);
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function fileExistsSync(p) {
|
|
87
|
+
return getFs().existsSync(p);
|
|
88
|
+
}
|
|
89
|
+
function isMissingSync(p) {
|
|
90
|
+
try {
|
|
91
|
+
getFs().statSync(p);
|
|
92
|
+
return false;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
const code = error.code;
|
|
95
|
+
return code === "ENOENT" || code === "ENOTDIR";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function readJSON(jsonPath) {
|
|
99
|
+
try {
|
|
100
|
+
const fsp = getFsp();
|
|
101
|
+
const buf = await fsp.readFile(jsonPath, "utf8");
|
|
102
|
+
return JSON.parse(buf);
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function writeJSON(p, obj) {
|
|
108
|
+
const fsp = getFsp();
|
|
109
|
+
await fsp.writeFile(p, JSON.stringify(obj, null, 2) + "\n", "utf8");
|
|
110
|
+
}
|
|
111
|
+
async function ensureDir(p) {
|
|
112
|
+
const fsp = getFsp();
|
|
113
|
+
await fsp.mkdir(p, { recursive: true });
|
|
114
|
+
}
|
|
115
|
+
async function stat(p) {
|
|
116
|
+
const fsp = getFsp();
|
|
117
|
+
return fsp.stat(p);
|
|
118
|
+
}
|
|
119
|
+
async function lstat(p) {
|
|
120
|
+
const fsp = getFsp();
|
|
121
|
+
return fsp.lstat(p);
|
|
122
|
+
}
|
|
123
|
+
async function chmod(p, mode) {
|
|
124
|
+
const fsp = getFsp();
|
|
125
|
+
await fsp.chmod(p, mode);
|
|
126
|
+
}
|
|
127
|
+
async function symlink(target, linkPath, type) {
|
|
128
|
+
const fsp = getFsp();
|
|
129
|
+
await fsp.symlink(target, linkPath, type);
|
|
130
|
+
}
|
|
131
|
+
async function realpath(p) {
|
|
132
|
+
const fsp = getFsp();
|
|
133
|
+
return fsp.realpath(p);
|
|
134
|
+
}
|
|
135
|
+
async function copyFile(src, dest) {
|
|
136
|
+
const fsp = getFsp();
|
|
137
|
+
await fsp.copyFile(src, dest);
|
|
138
|
+
}
|
|
139
|
+
async function cp(src, dest, options) {
|
|
140
|
+
const fsp = getFsp();
|
|
141
|
+
await fsp.cp(src, dest, options);
|
|
142
|
+
}
|
|
143
|
+
async function readdir(p) {
|
|
144
|
+
const fsp = getFsp();
|
|
145
|
+
return fsp.readdir(p);
|
|
146
|
+
}
|
|
147
|
+
function readdirSync(p) {
|
|
148
|
+
const fs = getFs();
|
|
149
|
+
return fs.readdirSync(p);
|
|
150
|
+
}
|
|
151
|
+
async function rm(p, options) {
|
|
152
|
+
const fsp = getFsp();
|
|
153
|
+
await fsp.rm(p, options);
|
|
154
|
+
}
|
|
155
|
+
async function isDirEmpty(dir) {
|
|
156
|
+
try {
|
|
157
|
+
const fsp = getFsp();
|
|
158
|
+
const items = await fsp.readdir(dir);
|
|
159
|
+
return items.length === 0;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
if (e?.code === "ENOENT") return true;
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
async function mkdtemp(prefix) {
|
|
166
|
+
const fsp = getFsp();
|
|
167
|
+
return fsp.mkdtemp(prefix);
|
|
168
|
+
}
|
|
169
|
+
async function access(p, mode) {
|
|
170
|
+
const fsp = getFsp();
|
|
171
|
+
await fsp.access(p, mode ?? F_OK);
|
|
172
|
+
}
|
|
173
|
+
function runCmd(cmd, args, opts = {}) {
|
|
174
|
+
const spawn = getSpawn();
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
const child = spawn(cmd, args, { stdio: "inherit", shell: false, ...opts });
|
|
177
|
+
child.on("close", (code) => code === 0 ? resolve() : reject(new Error(`${cmd} exited with code ${code}`)));
|
|
178
|
+
child.on("error", reject);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
function watchFile(p, onChange, options) {
|
|
182
|
+
const fs = getFs();
|
|
183
|
+
const watcher = fs.watch(p, { persistent: false, recursive: options?.recursive === true }, (eventType, filename) => {
|
|
184
|
+
onChange(eventType, filename);
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
close() {
|
|
188
|
+
watcher.close();
|
|
189
|
+
},
|
|
190
|
+
onError(handler) {
|
|
191
|
+
watcher.on("error", handler);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
export {
|
|
196
|
+
access,
|
|
197
|
+
appendFile,
|
|
198
|
+
chmod,
|
|
199
|
+
copyFile,
|
|
200
|
+
cp,
|
|
201
|
+
ensureDir,
|
|
202
|
+
ensureDirSync,
|
|
203
|
+
fileExists,
|
|
204
|
+
fileExistsSync,
|
|
205
|
+
getSpawnFn,
|
|
206
|
+
isDirEmpty,
|
|
207
|
+
isMissingSync,
|
|
208
|
+
lstat,
|
|
209
|
+
mkdir,
|
|
210
|
+
mkdtemp,
|
|
211
|
+
readFile,
|
|
212
|
+
readFileBuffer,
|
|
213
|
+
readFileSync,
|
|
214
|
+
readJSON,
|
|
215
|
+
readdir,
|
|
216
|
+
readdirSync,
|
|
217
|
+
realpath,
|
|
218
|
+
rename,
|
|
219
|
+
rm,
|
|
220
|
+
runCmd,
|
|
221
|
+
stat,
|
|
222
|
+
symlink,
|
|
223
|
+
unlink,
|
|
224
|
+
watchFile,
|
|
225
|
+
writeFile,
|
|
226
|
+
writeJSON
|
|
227
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// libs/utils/src/fs/index.ts
|
|
2
|
+
import {
|
|
3
|
+
readFile,
|
|
4
|
+
readFileSync,
|
|
5
|
+
readFileBuffer,
|
|
6
|
+
writeFile,
|
|
7
|
+
appendFile,
|
|
8
|
+
mkdir,
|
|
9
|
+
rename,
|
|
10
|
+
unlink,
|
|
11
|
+
stat,
|
|
12
|
+
lstat,
|
|
13
|
+
chmod,
|
|
14
|
+
copyFile,
|
|
15
|
+
cp,
|
|
16
|
+
readdir,
|
|
17
|
+
readdirSync,
|
|
18
|
+
realpath,
|
|
19
|
+
rm,
|
|
20
|
+
mkdtemp,
|
|
21
|
+
symlink,
|
|
22
|
+
access,
|
|
23
|
+
fileExists,
|
|
24
|
+
fileExistsSync,
|
|
25
|
+
readJSON,
|
|
26
|
+
writeJSON,
|
|
27
|
+
ensureDir,
|
|
28
|
+
ensureDirSync,
|
|
29
|
+
isDirEmpty,
|
|
30
|
+
runCmd,
|
|
31
|
+
watchFile,
|
|
32
|
+
getSpawnFn
|
|
33
|
+
} from "./fs.mjs";
|
|
34
|
+
export {
|
|
35
|
+
access,
|
|
36
|
+
appendFile,
|
|
37
|
+
chmod,
|
|
38
|
+
copyFile,
|
|
39
|
+
cp,
|
|
40
|
+
ensureDir,
|
|
41
|
+
ensureDirSync,
|
|
42
|
+
fileExists,
|
|
43
|
+
fileExistsSync,
|
|
44
|
+
getSpawnFn,
|
|
45
|
+
isDirEmpty,
|
|
46
|
+
lstat,
|
|
47
|
+
mkdir,
|
|
48
|
+
mkdtemp,
|
|
49
|
+
readFile,
|
|
50
|
+
readFileBuffer,
|
|
51
|
+
readFileSync,
|
|
52
|
+
readJSON,
|
|
53
|
+
readdir,
|
|
54
|
+
readdirSync,
|
|
55
|
+
realpath,
|
|
56
|
+
rename,
|
|
57
|
+
rm,
|
|
58
|
+
runCmd,
|
|
59
|
+
stat,
|
|
60
|
+
symlink,
|
|
61
|
+
unlink,
|
|
62
|
+
watchFile,
|
|
63
|
+
writeFile,
|
|
64
|
+
writeJSON
|
|
65
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// libs/utils/src/http/cookie.ts
|
|
2
|
+
import { isNode } from "../crypto/runtime.mjs";
|
|
3
|
+
var LOCALHOST_PATTERNS = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "0.0.0.0"]);
|
|
4
|
+
function isLocalhost(host) {
|
|
5
|
+
if (!host) return false;
|
|
6
|
+
if (host.startsWith("[")) {
|
|
7
|
+
const closeBracket = host.indexOf("]");
|
|
8
|
+
if (closeBracket > 0) {
|
|
9
|
+
return LOCALHOST_PATTERNS.has(host.slice(1, closeBracket));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if (LOCALHOST_PATTERNS.has(host)) return true;
|
|
13
|
+
const lastColon = host.lastIndexOf(":");
|
|
14
|
+
if (lastColon > 0) {
|
|
15
|
+
const hostname = host.slice(0, lastColon);
|
|
16
|
+
return LOCALHOST_PATTERNS.has(hostname);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
function isSecureRequest(req) {
|
|
21
|
+
if (!req) return false;
|
|
22
|
+
if (req.protocol === "https") return true;
|
|
23
|
+
const forwarded = req.headers?.["x-forwarded-proto"];
|
|
24
|
+
const forwardedStr = (Array.isArray(forwarded) ? forwarded[0] : forwarded)?.split(",")[0]?.trim()?.toLowerCase();
|
|
25
|
+
if (forwardedStr === "https") return true;
|
|
26
|
+
if (req.socket?.encrypted) return true;
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
function buildSetCookie(options, req) {
|
|
30
|
+
if (!isNode()) return null;
|
|
31
|
+
const { name, value, path = "/", maxAge = 86400, httpOnly = true, sameSite = "Strict", domain } = options;
|
|
32
|
+
let secure;
|
|
33
|
+
if (options.secure !== void 0) {
|
|
34
|
+
secure = options.secure;
|
|
35
|
+
} else {
|
|
36
|
+
const host = req?.headers?.["host"];
|
|
37
|
+
const hostStr = Array.isArray(host) ? host[0] : host;
|
|
38
|
+
if (isLocalhost(hostStr)) {
|
|
39
|
+
secure = false;
|
|
40
|
+
} else if (req) {
|
|
41
|
+
secure = isSecureRequest(req);
|
|
42
|
+
} else {
|
|
43
|
+
secure = process.env["NODE_ENV"] === "production";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (sameSite === "None" && !secure) {
|
|
47
|
+
secure = true;
|
|
48
|
+
}
|
|
49
|
+
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
|
|
50
|
+
parts.push(`Path=${path}`);
|
|
51
|
+
parts.push(`Max-Age=${maxAge}`);
|
|
52
|
+
if (httpOnly) parts.push("HttpOnly");
|
|
53
|
+
if (secure) parts.push("Secure");
|
|
54
|
+
parts.push(`SameSite=${sameSite}`);
|
|
55
|
+
if (domain) parts.push(`Domain=${domain}`);
|
|
56
|
+
return parts.join("; ");
|
|
57
|
+
}
|
|
58
|
+
function parseCookies(cookieHeader) {
|
|
59
|
+
const cookies = {};
|
|
60
|
+
if (!cookieHeader) return cookies;
|
|
61
|
+
const pairs = cookieHeader.split(";");
|
|
62
|
+
for (const pair of pairs) {
|
|
63
|
+
const eqIdx = pair.indexOf("=");
|
|
64
|
+
if (eqIdx < 0) continue;
|
|
65
|
+
try {
|
|
66
|
+
const name = decodeURIComponent(pair.slice(0, eqIdx).trim());
|
|
67
|
+
const value = decodeURIComponent(pair.slice(eqIdx + 1).trim());
|
|
68
|
+
if (name) cookies[name] = value;
|
|
69
|
+
} catch {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return cookies;
|
|
74
|
+
}
|
|
75
|
+
function getCookie(cookieHeader, name) {
|
|
76
|
+
if (!cookieHeader) return void 0;
|
|
77
|
+
return parseCookies(cookieHeader)[name];
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
buildSetCookie,
|
|
81
|
+
getCookie,
|
|
82
|
+
isLocalhost,
|
|
83
|
+
isSecureRequest,
|
|
84
|
+
parseCookies
|
|
85
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// libs/utils/src/http/http.ts
|
|
2
|
+
function validateBaseUrl(url) {
|
|
3
|
+
try {
|
|
4
|
+
const parsed = new URL(url);
|
|
5
|
+
if (!["http:", "https:"].includes(parsed.protocol)) {
|
|
6
|
+
throw new Error(`Unsupported protocol: ${parsed.protocol}. Only http: and https: are supported.`);
|
|
7
|
+
}
|
|
8
|
+
return parsed;
|
|
9
|
+
} catch (err) {
|
|
10
|
+
if (err instanceof Error && err.message.includes("Unsupported protocol")) {
|
|
11
|
+
throw err;
|
|
12
|
+
}
|
|
13
|
+
throw new Error(`Invalid base URL: ${url}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
validateBaseUrl
|
|
18
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// libs/utils/src/http/index.ts
|
|
2
|
+
import { validateBaseUrl } from "./http.mjs";
|
|
3
|
+
import { buildSetCookie, parseCookies, getCookie, isLocalhost, isSecureRequest } from "./cookie.mjs";
|
|
4
|
+
export {
|
|
5
|
+
buildSetCookie,
|
|
6
|
+
getCookie,
|
|
7
|
+
isLocalhost,
|
|
8
|
+
isSecureRequest,
|
|
9
|
+
parseCookies,
|
|
10
|
+
validateBaseUrl
|
|
11
|
+
};
|