@byoky/bridge 0.3.0 → 0.4.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/LICENSE +21 -0
- package/dist/host.js +66 -52
- package/package.json +6 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 byoky contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/host.js
CHANGED
|
@@ -3,18 +3,27 @@ import { createServer } from "http";
|
|
|
3
3
|
var MAX_PENDING_REQUESTS = 100;
|
|
4
4
|
var pendingRequests = /* @__PURE__ */ new Map();
|
|
5
5
|
function handleProxyResponse(msg) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
const pending = pendingRequests.get(msg.requestId);
|
|
7
|
+
if (!pending) return;
|
|
8
|
+
if (msg.type === "proxy_http_response_meta") {
|
|
9
|
+
const headers = { ...msg.headers };
|
|
10
|
+
delete headers["transfer-encoding"];
|
|
11
|
+
delete headers["content-encoding"];
|
|
12
|
+
delete headers["content-length"];
|
|
13
|
+
pending.res.writeHead(msg.status, headers);
|
|
14
|
+
} else if (msg.type === "proxy_http_response_chunk") {
|
|
15
|
+
pending.res.write(msg.chunk);
|
|
16
|
+
} else if (msg.type === "proxy_http_response_done") {
|
|
17
|
+
pending.res.end();
|
|
18
|
+
clearTimeout(pending.timeout);
|
|
19
|
+
pendingRequests.delete(msg.requestId);
|
|
12
20
|
} else if (msg.type === "proxy_http_error") {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
pending.reject(new Error(msg.error));
|
|
16
|
-
pendingRequests.delete(msg.requestId);
|
|
21
|
+
if (!pending.res.headersSent) {
|
|
22
|
+
pending.res.writeHead(502, { "Content-Type": "application/json" });
|
|
17
23
|
}
|
|
24
|
+
pending.res.end(JSON.stringify({ error: msg.error }));
|
|
25
|
+
clearTimeout(pending.timeout);
|
|
26
|
+
pendingRequests.delete(msg.requestId);
|
|
18
27
|
}
|
|
19
28
|
}
|
|
20
29
|
function startProxyServer(config) {
|
|
@@ -96,25 +105,17 @@ function startProxyServer(config) {
|
|
|
96
105
|
res.end(JSON.stringify({ error: "Too many concurrent requests" }));
|
|
97
106
|
return;
|
|
98
107
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
pendingRequests.
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const responseHeaders = { ...response.headers };
|
|
111
|
-
delete responseHeaders["transfer-encoding"];
|
|
112
|
-
res.writeHead(response.status, responseHeaders);
|
|
113
|
-
res.end(response.body);
|
|
114
|
-
} catch (err) {
|
|
115
|
-
res.writeHead(502, { "Content-Type": "application/json" });
|
|
116
|
-
res.end(JSON.stringify({ error: err.message }));
|
|
117
|
-
}
|
|
108
|
+
const timeout = setTimeout(() => {
|
|
109
|
+
if (pendingRequests.has(requestId)) {
|
|
110
|
+
pendingRequests.delete(requestId);
|
|
111
|
+
if (!res.headersSent) {
|
|
112
|
+
res.writeHead(504, { "Content-Type": "application/json" });
|
|
113
|
+
}
|
|
114
|
+
res.end(JSON.stringify({ error: "Request timed out" }));
|
|
115
|
+
}
|
|
116
|
+
}, 12e4);
|
|
117
|
+
pendingRequests.set(requestId, { res, timeout });
|
|
118
|
+
sendToExtension(proxyMsg);
|
|
118
119
|
});
|
|
119
120
|
server.listen(port, "127.0.0.1", () => {
|
|
120
121
|
process.stderr.write(`Byoky proxy listening on http://127.0.0.1:${port}
|
|
@@ -206,12 +207,17 @@ async function handleMessage(msg) {
|
|
|
206
207
|
if (!msg || typeof msg !== "object") return;
|
|
207
208
|
const message = msg;
|
|
208
209
|
if (message.type === "ping") {
|
|
209
|
-
writeMessage({ type: "pong", version: "0.
|
|
210
|
+
writeMessage({ type: "pong", version: "0.3.0" });
|
|
210
211
|
return;
|
|
211
212
|
}
|
|
212
213
|
if (message.type === "proxy") {
|
|
213
214
|
const req = msg;
|
|
214
|
-
await
|
|
215
|
+
await handleStreamingFetch(req.requestId, req.url, req.method, req.headers, req.body, "bridge");
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (message.type === "proxy_direct_fetch") {
|
|
219
|
+
const req = msg;
|
|
220
|
+
await handleStreamingFetch(req.requestId, req.url, req.method, req.headers, req.body, "proxy_http");
|
|
215
221
|
return;
|
|
216
222
|
}
|
|
217
223
|
if (message.type === "start-proxy") {
|
|
@@ -219,36 +225,44 @@ async function handleMessage(msg) {
|
|
|
219
225
|
handleStartProxy(req);
|
|
220
226
|
return;
|
|
221
227
|
}
|
|
222
|
-
if (
|
|
228
|
+
if (msg && typeof msg === "object" && "type" in msg && (msg.type === "proxy_http_response_meta" || msg.type === "proxy_http_response_chunk" || msg.type === "proxy_http_response_done" || msg.type === "proxy_http_error")) {
|
|
223
229
|
handleProxyResponse(msg);
|
|
224
230
|
return;
|
|
225
231
|
}
|
|
226
232
|
}
|
|
227
|
-
async function
|
|
233
|
+
async function handleStreamingFetch(requestId, url, method, headers, body, mode) {
|
|
234
|
+
const send = (msg) => {
|
|
235
|
+
if (mode === "bridge") {
|
|
236
|
+
writeMessage(msg);
|
|
237
|
+
} else {
|
|
238
|
+
handleProxyResponse(msg);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const prefix = mode === "bridge" ? "proxy_response" : "proxy_http_response";
|
|
242
|
+
const errorType = mode === "bridge" ? "proxy_error" : "proxy_http_error";
|
|
228
243
|
try {
|
|
229
|
-
const res = await fetch(
|
|
230
|
-
method
|
|
231
|
-
headers
|
|
232
|
-
body:
|
|
244
|
+
const res = await fetch(url, {
|
|
245
|
+
method,
|
|
246
|
+
headers,
|
|
247
|
+
body: body || void 0
|
|
233
248
|
});
|
|
234
|
-
const
|
|
235
|
-
const headers = {};
|
|
249
|
+
const resHeaders = {};
|
|
236
250
|
res.headers.forEach((v, k) => {
|
|
237
|
-
|
|
238
|
-
});
|
|
239
|
-
writeMessage({
|
|
240
|
-
type: "proxy_response",
|
|
241
|
-
requestId: req.requestId,
|
|
242
|
-
status: res.status,
|
|
243
|
-
headers,
|
|
244
|
-
body
|
|
251
|
+
resHeaders[k] = v;
|
|
245
252
|
});
|
|
253
|
+
send({ type: `${prefix}_meta`, requestId, status: res.status, headers: resHeaders });
|
|
254
|
+
if (res.body) {
|
|
255
|
+
const reader = res.body.getReader();
|
|
256
|
+
const decoder = new TextDecoder();
|
|
257
|
+
for (; ; ) {
|
|
258
|
+
const { done, value } = await reader.read();
|
|
259
|
+
if (done) break;
|
|
260
|
+
send({ type: `${prefix}_chunk`, requestId, chunk: decoder.decode(value, { stream: true }) });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
send({ type: `${prefix}_done`, requestId });
|
|
246
264
|
} catch (e) {
|
|
247
|
-
|
|
248
|
-
type: "proxy_error",
|
|
249
|
-
requestId: req.requestId,
|
|
250
|
-
error: e.message
|
|
251
|
-
});
|
|
265
|
+
send({ type: errorType, requestId, error: e.message });
|
|
252
266
|
}
|
|
253
267
|
}
|
|
254
268
|
function handleStartProxy(req) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byoky/bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Native messaging bridge for Byoky — routes setup token requests through Claude Code CLI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/host.js",
|
|
@@ -11,10 +11,6 @@
|
|
|
11
11
|
"dist",
|
|
12
12
|
"bin"
|
|
13
13
|
],
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "tsup",
|
|
16
|
-
"dev": "tsup --watch"
|
|
17
|
-
},
|
|
18
14
|
"keywords": [
|
|
19
15
|
"byoky",
|
|
20
16
|
"claude",
|
|
@@ -31,5 +27,9 @@
|
|
|
31
27
|
"@types/node": "25.5.0",
|
|
32
28
|
"tsup": "^8.0.0",
|
|
33
29
|
"typescript": "^5.5.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"dev": "tsup --watch"
|
|
34
34
|
}
|
|
35
|
-
}
|
|
35
|
+
}
|