openclaw-autoproxy 1.0.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.
@@ -0,0 +1,58 @@
1
+ import type { AddressInfo } from "node:net";
2
+ import { config } from "./config.js";
3
+ import { createGatewayHttpServer } from "./server-http.js";
4
+
5
+ export type GatewayServer = {
6
+ close: (opts?: { reason?: string }) => Promise<void>;
7
+ };
8
+
9
+ export type GatewayServerOptions = {
10
+ host?: string;
11
+ };
12
+
13
+ export async function startGatewayServer(
14
+ port = config.port,
15
+ opts: GatewayServerOptions = {},
16
+ ): Promise<GatewayServer> {
17
+ const host = opts.host ?? config.host;
18
+ const server = createGatewayHttpServer();
19
+
20
+ await new Promise<void>((resolve, reject) => {
21
+ const onError = (error: Error) => {
22
+ server.off("listening", onListening);
23
+ reject(error);
24
+ };
25
+
26
+ const onListening = () => {
27
+ server.off("error", onError);
28
+ resolve();
29
+ };
30
+
31
+ server.once("error", onError);
32
+ server.once("listening", onListening);
33
+ server.listen({
34
+ host,
35
+ port,
36
+ });
37
+ });
38
+
39
+ const address = server.address();
40
+ const resolvedPort = typeof address === "object" && address ? (address as AddressInfo).port : port;
41
+
42
+ console.log(`Gateway listening on http://${host}:${resolvedPort} -> ${config.upstreamBaseUrl}`);
43
+
44
+ return {
45
+ close: async () => {
46
+ await new Promise<void>((resolve, reject) => {
47
+ server.close((error) => {
48
+ if (error) {
49
+ reject(error);
50
+ return;
51
+ }
52
+
53
+ resolve();
54
+ });
55
+ });
56
+ },
57
+ };
58
+ }
@@ -0,0 +1,35 @@
1
+ import process from "node:process";
2
+ import path from "node:path";
3
+ import { pathToFileURL } from "node:url";
4
+ import { config } from "./config.js";
5
+ import { startGatewayServer as startGatewayServerImpl } from "./server.impl.js";
6
+
7
+ export type { GatewayServer, GatewayServerOptions } from "./server.impl.js";
8
+ export { startGatewayServer } from "./server.impl.js";
9
+
10
+ async function main(): Promise<void> {
11
+ try {
12
+ await startGatewayServerImpl(config.port, { host: config.host });
13
+ } catch (error) {
14
+ console.error(
15
+ error instanceof Error
16
+ ? `Failed to start gateway: ${error.message}`
17
+ : "Failed to start gateway due to an unknown error.",
18
+ );
19
+ process.exit(1);
20
+ }
21
+ }
22
+
23
+ const invokedAsScript = (() => {
24
+ const scriptArg = process.argv[1];
25
+
26
+ if (!scriptArg) {
27
+ return false;
28
+ }
29
+
30
+ return pathToFileURL(path.resolve(scriptArg)).href === import.meta.url;
31
+ })();
32
+
33
+ if (invokedAsScript) {
34
+ void main();
35
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "forceConsistentCasingInFileNames": true,
9
+ "skipLibCheck": true,
10
+ "resolveJsonModule": true,
11
+ "allowSyntheticDefaultImports": true,
12
+ "types": ["node"],
13
+ "noEmit": true
14
+ },
15
+ "include": ["src/**/*.ts", "bin/**/*.js"],
16
+ "exclude": ["node_modules"]
17
+ }