linkzero 0.1.1 → 0.1.3
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/cli.js +59 -15
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -11,7 +11,10 @@ import { generateKeypair, LinkZeroClient, deriveWalletAddress } from "@linkzeroa
|
|
|
11
11
|
|
|
12
12
|
// src/provider.ts
|
|
13
13
|
import http from "http";
|
|
14
|
+
import fs from "fs";
|
|
15
|
+
import path from "path";
|
|
14
16
|
import { spawn } from "child_process";
|
|
17
|
+
var OUTPUT_DIR = path.join(process.cwd(), "lz-outputs");
|
|
15
18
|
async function dispatchEcho(payload) {
|
|
16
19
|
return {
|
|
17
20
|
output: {
|
|
@@ -23,40 +26,57 @@ async function dispatchEcho(payload) {
|
|
|
23
26
|
}
|
|
24
27
|
};
|
|
25
28
|
}
|
|
26
|
-
async function dispatchHandler(command, payload, timeout) {
|
|
29
|
+
async function dispatchHandler(command, payload, timeout, endpoint) {
|
|
27
30
|
return new Promise((resolve, reject) => {
|
|
28
31
|
const [cmd, ...args] = command.split(/\s+/);
|
|
29
32
|
const child = spawn(cmd, args, {
|
|
30
33
|
stdio: ["pipe", "pipe", "pipe"],
|
|
31
|
-
shell: true
|
|
34
|
+
shell: true,
|
|
35
|
+
env: {
|
|
36
|
+
...process.env,
|
|
37
|
+
LZ_ENDPOINT: endpoint || "",
|
|
38
|
+
LZ_OUTPUT_DIR: OUTPUT_DIR
|
|
39
|
+
}
|
|
32
40
|
});
|
|
33
|
-
|
|
41
|
+
const chunks = [];
|
|
34
42
|
let stderr = "";
|
|
43
|
+
let stdoutDone = false;
|
|
44
|
+
let exitCode = null;
|
|
35
45
|
const timer = setTimeout(() => {
|
|
36
46
|
child.kill("SIGKILL");
|
|
37
47
|
reject(new Error(`Handler timed out after ${timeout}ms`));
|
|
38
48
|
}, timeout);
|
|
49
|
+
function tryFinish() {
|
|
50
|
+
if (!stdoutDone || exitCode === null) return;
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
const stdout = Buffer.concat(chunks).toString();
|
|
53
|
+
if (exitCode !== 0) {
|
|
54
|
+
reject(new Error(`Handler exited with code ${exitCode}: ${stderr.trim()}`));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
resolve(JSON.parse(stdout));
|
|
59
|
+
} catch {
|
|
60
|
+
reject(new Error(`Handler returned invalid JSON: ${stdout.slice(0, 200)}`));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
39
63
|
child.stdout.on("data", (data) => {
|
|
40
|
-
|
|
64
|
+
chunks.push(data);
|
|
41
65
|
});
|
|
42
66
|
child.stderr.on("data", (data) => {
|
|
43
67
|
stderr += data.toString();
|
|
44
68
|
});
|
|
69
|
+
child.stdout.on("end", () => {
|
|
70
|
+
stdoutDone = true;
|
|
71
|
+
tryFinish();
|
|
72
|
+
});
|
|
45
73
|
child.on("error", (err) => {
|
|
46
74
|
clearTimeout(timer);
|
|
47
75
|
reject(new Error(`Handler failed to start: ${err.message}`));
|
|
48
76
|
});
|
|
49
77
|
child.on("close", (code) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
reject(new Error(`Handler exited with code ${code}: ${stderr.trim()}`));
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
resolve(JSON.parse(stdout));
|
|
57
|
-
} catch {
|
|
58
|
-
reject(new Error(`Handler returned invalid JSON: ${stdout.slice(0, 200)}`));
|
|
59
|
-
}
|
|
78
|
+
exitCode = code;
|
|
79
|
+
tryFinish();
|
|
60
80
|
});
|
|
61
81
|
child.stdin.write(JSON.stringify(payload));
|
|
62
82
|
child.stdin.end();
|
|
@@ -87,10 +107,33 @@ function createProviderDaemon(options) {
|
|
|
87
107
|
let originalEndpoint = null;
|
|
88
108
|
let requestCount = 0;
|
|
89
109
|
const startTime = Date.now();
|
|
90
|
-
const dispatch = handler ? (payload) => dispatchHandler(handler, payload, handlerTimeout) : forward ? (payload) => dispatchForward(forward, payload, handlerTimeout) : dispatchEcho;
|
|
110
|
+
const dispatch = handler ? (payload) => dispatchHandler(handler, payload, handlerTimeout, options.endpoint) : forward ? (payload) => dispatchForward(forward, payload, handlerTimeout) : dispatchEcho;
|
|
91
111
|
const dispatchMode = handler ? "handler" : forward ? "forward" : "echo";
|
|
92
112
|
function createServer() {
|
|
93
113
|
return http.createServer(async (req, res) => {
|
|
114
|
+
if (req.method === "GET" && req.url?.startsWith("/files/")) {
|
|
115
|
+
const filename = path.basename(req.url.slice(7));
|
|
116
|
+
const filePath = path.join(OUTPUT_DIR, filename);
|
|
117
|
+
if (!fs.existsSync(filePath)) {
|
|
118
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
119
|
+
res.end(JSON.stringify({ error: "File not found" }));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const ext = path.extname(filename).toLowerCase();
|
|
123
|
+
const mimeTypes = {
|
|
124
|
+
".png": "image/png",
|
|
125
|
+
".jpg": "image/jpeg",
|
|
126
|
+
".jpeg": "image/jpeg",
|
|
127
|
+
".gif": "image/gif",
|
|
128
|
+
".webp": "image/webp",
|
|
129
|
+
".svg": "image/svg+xml",
|
|
130
|
+
".json": "application/json",
|
|
131
|
+
".txt": "text/plain"
|
|
132
|
+
};
|
|
133
|
+
res.writeHead(200, { "Content-Type": mimeTypes[ext] || "application/octet-stream" });
|
|
134
|
+
fs.createReadStream(filePath).pipe(res);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
94
137
|
if (req.method === "GET" && req.url === "/health") {
|
|
95
138
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
96
139
|
res.end(JSON.stringify({
|
|
@@ -138,6 +181,7 @@ function createProviderDaemon(options) {
|
|
|
138
181
|
});
|
|
139
182
|
}
|
|
140
183
|
async function start() {
|
|
184
|
+
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
141
185
|
const { registrations } = await client.listBidderRegistrations();
|
|
142
186
|
if (registrations.length === 0) {
|
|
143
187
|
throw new Error(
|