@valuya/bot-channel-server-core 0.2.0-beta.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +28 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare function loadEnvFile(filePath?: string): void;
|
|
1
2
|
export declare function requiredEnv(name: string): string;
|
|
2
3
|
export declare function getRequestPath(rawUrl?: string | null): string;
|
|
3
4
|
export declare function readRequestBody(req: AsyncIterable<unknown>): Promise<string>;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function loadEnvFile(filePath = process.env.ENV_FILE?.trim() || path.resolve(process.cwd(), ".env")) {
|
|
4
|
+
if (!filePath)
|
|
5
|
+
return;
|
|
6
|
+
if (!fs.existsSync(filePath))
|
|
7
|
+
return;
|
|
8
|
+
const raw = fs.readFileSync(filePath, "utf8");
|
|
9
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
10
|
+
const trimmed = line.trim();
|
|
11
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
12
|
+
continue;
|
|
13
|
+
const eqIndex = trimmed.indexOf("=");
|
|
14
|
+
if (eqIndex <= 0)
|
|
15
|
+
continue;
|
|
16
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
17
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key))
|
|
18
|
+
continue;
|
|
19
|
+
if (process.env[key]?.trim())
|
|
20
|
+
continue;
|
|
21
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
22
|
+
if ((value.startsWith("\"") && value.endsWith("\"")) ||
|
|
23
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
24
|
+
value = value.slice(1, -1);
|
|
25
|
+
}
|
|
26
|
+
process.env[key] = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
1
29
|
export function requiredEnv(name) {
|
|
2
30
|
const value = process.env[name]?.trim();
|
|
3
31
|
if (!value)
|