@seedprotocol/gateway-hyper 0.7.10-next.1353.g74aeeed → 0.7.10
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 +24 -2
- package/dist/bin.js +164 -48
- package/dist/bin.js.map +1 -1
- package/dist/cli.d.ts +5 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/keys.d.ts +16 -0
- package/dist/keys.d.ts.map +1 -1
- package/dist/src-DHEkxp5F.js +943 -0
- package/dist/src-DHEkxp5F.js.map +1 -0
- package/dist/tunnel/allowlist.d.ts +45 -0
- package/dist/tunnel/allowlist.d.ts.map +1 -0
- package/dist/tunnel/connect.d.ts.map +1 -1
- package/dist/tunnel/events.d.ts +131 -0
- package/dist/tunnel/events.d.ts.map +1 -0
- package/dist/tunnel/proxy.d.ts +11 -0
- package/dist/tunnel/proxy.d.ts.map +1 -1
- package/dist/tunnel/proxyApp.d.ts.map +1 -1
- package/dist/tunnel/rawConnect.d.ts +14 -0
- package/dist/tunnel/rawConnect.d.ts.map +1 -0
- package/dist/tunnel/rawPipe.d.ts +35 -0
- package/dist/tunnel/rawPipe.d.ts.map +1 -0
- package/dist/tunnel/rawServe.d.ts +18 -0
- package/dist/tunnel/rawServe.d.ts.map +1 -0
- package/dist/tunnel/serve.d.ts +11 -0
- package/dist/tunnel/serve.d.ts.map +1 -1
- package/dist/tunnel/session.d.ts +15 -1
- package/dist/tunnel/session.d.ts.map +1 -1
- package/dist/types.d.ts +125 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/src-qIlmrs1H.js +0 -573
- package/dist/src-qIlmrs1H.js.map +0 -1
package/dist/src-qIlmrs1H.js
DELETED
|
@@ -1,573 +0,0 @@
|
|
|
1
|
-
import e from "node:fs";
|
|
2
|
-
import t from "node:path";
|
|
3
|
-
import n from "hyperdht";
|
|
4
|
-
import r from "hypercore-id-encoding";
|
|
5
|
-
import i from "node:http";
|
|
6
|
-
import { URL as a } from "node:url";
|
|
7
|
-
//#region src/keys.ts
|
|
8
|
-
function o(e) {
|
|
9
|
-
return e.toString("hex");
|
|
10
|
-
}
|
|
11
|
-
function s(e) {
|
|
12
|
-
return Buffer.from(e, "hex");
|
|
13
|
-
}
|
|
14
|
-
function c(e) {
|
|
15
|
-
return r.encode(e);
|
|
16
|
-
}
|
|
17
|
-
function l(e) {
|
|
18
|
-
let t = e.trim();
|
|
19
|
-
if (!t) throw Error("Gateway Hyper key is empty");
|
|
20
|
-
try {
|
|
21
|
-
return Buffer.from(r.decode(t));
|
|
22
|
-
} catch {
|
|
23
|
-
if (/^[a-fA-F0-9]{64}$/.test(t)) return s(t);
|
|
24
|
-
throw Error(`Invalid Gateway Hyper key (expected z32 or 64-char hex): ${t.slice(0, 12)}…`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function u(r) {
|
|
28
|
-
let i = t.resolve(r);
|
|
29
|
-
if (e.existsSync(i)) {
|
|
30
|
-
let t = e.readFileSync(i, "utf8"), n = JSON.parse(t);
|
|
31
|
-
if (!n.publicKey || !n.secretKey) throw Error(`Invalid key file at ${i}: missing publicKey or secretKey`);
|
|
32
|
-
let r = s(n.publicKey);
|
|
33
|
-
return {
|
|
34
|
-
publicKey: r,
|
|
35
|
-
secretKey: s(n.secretKey),
|
|
36
|
-
z32: c(r)
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
let a = t.dirname(i);
|
|
40
|
-
e.mkdirSync(a, { recursive: !0 });
|
|
41
|
-
let l = n.keyPair(), u = {
|
|
42
|
-
publicKey: o(l.publicKey),
|
|
43
|
-
secretKey: o(l.secretKey),
|
|
44
|
-
encoding: "hex"
|
|
45
|
-
};
|
|
46
|
-
return e.writeFileSync(i, `${JSON.stringify(u, null, 2)}\n`, { mode: 384 }), {
|
|
47
|
-
publicKey: l.publicKey,
|
|
48
|
-
secretKey: l.secretKey,
|
|
49
|
-
z32: c(l.publicKey)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function d() {
|
|
53
|
-
let e = n.keyPair();
|
|
54
|
-
return {
|
|
55
|
-
publicKey: e.publicKey,
|
|
56
|
-
secretKey: e.secretKey,
|
|
57
|
-
z32: c(e.publicKey)
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/tunnel/protocol.ts
|
|
62
|
-
var f = 67108864, p = 1048576, m = class {
|
|
63
|
-
buffer = Buffer.alloc(0);
|
|
64
|
-
waiters = [];
|
|
65
|
-
failure = null;
|
|
66
|
-
constructor(e) {
|
|
67
|
-
e.on("data", (e) => {
|
|
68
|
-
this.buffer = Buffer.concat([this.buffer, e]), this.drain();
|
|
69
|
-
}), e.on("error", (e) => this.fail(e)), e.on("end", () => this.fail(/* @__PURE__ */ Error("Unexpected end of stream"))), e.on("close", () => this.fail(/* @__PURE__ */ Error("Stream closed")));
|
|
70
|
-
}
|
|
71
|
-
readExact(e) {
|
|
72
|
-
return new Promise((t, n) => {
|
|
73
|
-
e === 0 ? t(Buffer.alloc(0)) : (this.waiters.push({
|
|
74
|
-
length: e,
|
|
75
|
-
resolve: t,
|
|
76
|
-
reject: n
|
|
77
|
-
}), this.drain(), this.rejectWaiters());
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
fail(e) {
|
|
81
|
-
this.failure ??= e, this.drain(), this.rejectWaiters();
|
|
82
|
-
}
|
|
83
|
-
rejectWaiters() {
|
|
84
|
-
if (!this.failure || this.waiters.length === 0) return;
|
|
85
|
-
let e = this.waiters.splice(0);
|
|
86
|
-
for (let t of e) t.reject(/* @__PURE__ */ Error(`${this.failure.message} (expected ${t.length} bytes, got ${this.buffer.length})`));
|
|
87
|
-
}
|
|
88
|
-
drain() {
|
|
89
|
-
for (; this.waiters.length > 0;) {
|
|
90
|
-
let e = this.waiters[0];
|
|
91
|
-
if (!e || this.buffer.length < e.length) break;
|
|
92
|
-
this.waiters.shift();
|
|
93
|
-
let t = this.buffer.subarray(0, e.length);
|
|
94
|
-
this.buffer = this.buffer.subarray(e.length), e.resolve(t);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}, h = /* @__PURE__ */ new WeakMap();
|
|
98
|
-
function g(e) {
|
|
99
|
-
let t = h.get(e);
|
|
100
|
-
return t || (t = new m(e), h.set(e, t)), t;
|
|
101
|
-
}
|
|
102
|
-
function _(e, t) {
|
|
103
|
-
return g(e).readExact(t);
|
|
104
|
-
}
|
|
105
|
-
function v(e, t) {
|
|
106
|
-
return new Promise((n, r) => {
|
|
107
|
-
if (e.destroyed) r(/* @__PURE__ */ Error("Stream closed"));
|
|
108
|
-
else try {
|
|
109
|
-
if (e.write(t)) {
|
|
110
|
-
n();
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
let i = () => {
|
|
114
|
-
e.off("drain", a), e.off("error", o), e.off("close", s);
|
|
115
|
-
}, a = () => {
|
|
116
|
-
i(), n();
|
|
117
|
-
}, o = (e) => {
|
|
118
|
-
i(), r(e);
|
|
119
|
-
}, s = () => {
|
|
120
|
-
i(), r(/* @__PURE__ */ Error("Stream closed"));
|
|
121
|
-
};
|
|
122
|
-
e.once("drain", a), e.once("error", o), e.once("close", s);
|
|
123
|
-
} catch (e) {
|
|
124
|
-
r(e instanceof Error ? e : Error(String(e)));
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
function y(e) {
|
|
129
|
-
if (e > 67108864) throw Error(`Frame too large: ${e} bytes (max ${f})`);
|
|
130
|
-
}
|
|
131
|
-
async function b(e, t) {
|
|
132
|
-
y(t.length);
|
|
133
|
-
let n = Buffer.alloc(4);
|
|
134
|
-
n.writeUInt32BE(t.length, 0);
|
|
135
|
-
let r = t.subarray(0, p);
|
|
136
|
-
await v(e, Buffer.concat([n, r]));
|
|
137
|
-
for (let n = r.length; n < t.length; n += p) await v(e, t.subarray(n, n + p));
|
|
138
|
-
}
|
|
139
|
-
async function x(e) {
|
|
140
|
-
let t = (await _(e, 4)).readUInt32BE(0);
|
|
141
|
-
return t === 0 ? Buffer.alloc(0) : (y(t), _(e, t));
|
|
142
|
-
}
|
|
143
|
-
async function S(e, t, n) {
|
|
144
|
-
y(n.length), await b(e, Buffer.from(JSON.stringify(t), "utf8")), await b(e, n);
|
|
145
|
-
}
|
|
146
|
-
async function C(e) {
|
|
147
|
-
let t = await x(e), n = JSON.parse(t.toString("utf8"));
|
|
148
|
-
if (!n.method || !n.path) throw Error("Invalid tunnel request meta: missing method or path");
|
|
149
|
-
return n.headers = n.headers ?? {}, {
|
|
150
|
-
meta: n,
|
|
151
|
-
body: await x(e)
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
async function w(e, t, n) {
|
|
155
|
-
y(n.length), await b(e, Buffer.from(JSON.stringify(t), "utf8")), await b(e, n);
|
|
156
|
-
}
|
|
157
|
-
async function T(e) {
|
|
158
|
-
let t = await x(e), n = JSON.parse(t.toString("utf8"));
|
|
159
|
-
if (typeof n.status != "number") throw Error("Invalid tunnel response meta: missing status");
|
|
160
|
-
return n.headers = n.headers ?? {}, {
|
|
161
|
-
meta: n,
|
|
162
|
-
body: await x(e)
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
function E(e) {
|
|
166
|
-
let t = {};
|
|
167
|
-
for (let [n, r] of Object.entries(e)) r != null && (t[n] = Array.isArray(r) ? r.join(", ") : r);
|
|
168
|
-
return t;
|
|
169
|
-
}
|
|
170
|
-
function D(e) {
|
|
171
|
-
let t = /* @__PURE__ */ new Set([
|
|
172
|
-
"connection",
|
|
173
|
-
"keep-alive",
|
|
174
|
-
"transfer-encoding",
|
|
175
|
-
"upgrade"
|
|
176
|
-
]), n = {};
|
|
177
|
-
for (let [r, i] of Object.entries(e)) t.has(r.toLowerCase()) || i != null && (n[r] = Array.isArray(i) ? i.join(", ") : i);
|
|
178
|
-
return n;
|
|
179
|
-
}
|
|
180
|
-
//#endregion
|
|
181
|
-
//#region src/tunnel/errors.ts
|
|
182
|
-
var O = class extends Error {
|
|
183
|
-
timeoutMs;
|
|
184
|
-
code = "TUNNEL_TIMEOUT";
|
|
185
|
-
constructor(e) {
|
|
186
|
-
super(`No bytes sent or received on the tunnel for ${e} ms`), this.timeoutMs = e, this.name = "TunnelTimeoutError";
|
|
187
|
-
}
|
|
188
|
-
}, k = class extends Error {
|
|
189
|
-
timeoutMs;
|
|
190
|
-
code = "TUNNEL_CONNECT_TIMEOUT";
|
|
191
|
-
constructor(e) {
|
|
192
|
-
super(`Could not connect to the operator within ${e} ms`), this.timeoutMs = e, this.name = "TunnelConnectTimeoutError";
|
|
193
|
-
}
|
|
194
|
-
}, A = class extends Error {
|
|
195
|
-
timeoutMs;
|
|
196
|
-
code = "UPSTREAM_TIMEOUT";
|
|
197
|
-
constructor(e) {
|
|
198
|
-
super(`No response from upstream for ${e} ms`), this.timeoutMs = e, this.name = "UpstreamTimeoutError";
|
|
199
|
-
}
|
|
200
|
-
}, j = 3e4, M = /* @__PURE__ */ new Set([
|
|
201
|
-
"connection",
|
|
202
|
-
"keep-alive",
|
|
203
|
-
"proxy-connection",
|
|
204
|
-
"te",
|
|
205
|
-
"trailer",
|
|
206
|
-
"transfer-encoding",
|
|
207
|
-
"upgrade"
|
|
208
|
-
]);
|
|
209
|
-
function N(e) {
|
|
210
|
-
let t = new Set((Object.entries(e).find(([e]) => e.toLowerCase() === "connection")?.[1] ?? "").split(",").map((e) => e.trim().toLowerCase()).filter(Boolean)), n = {}, r;
|
|
211
|
-
for (let [i, a] of Object.entries(e)) {
|
|
212
|
-
let e = i.toLowerCase();
|
|
213
|
-
e === "host" ? r = a : e === "content-length" || M.has(e) || t.has(e) || (n[i] = a);
|
|
214
|
-
}
|
|
215
|
-
return r && !Object.keys(n).some((e) => e.toLowerCase() === "x-forwarded-host") && (n["x-forwarded-host"] = r), n;
|
|
216
|
-
}
|
|
217
|
-
async function P(e, t, n, r = {}) {
|
|
218
|
-
let o = e.replace(/\/$/, ""), s = new a(t.path, `${o}/`), c = r.timeoutMs ?? 3e4;
|
|
219
|
-
return new Promise((e, a) => {
|
|
220
|
-
let o = i.request(s, {
|
|
221
|
-
method: t.method,
|
|
222
|
-
headers: N(t.headers),
|
|
223
|
-
signal: r.signal
|
|
224
|
-
}, (t) => {
|
|
225
|
-
let n = [], r = 0;
|
|
226
|
-
t.on("data", (e) => {
|
|
227
|
-
r += e.length, r > 67108864 ? o.destroy(/* @__PURE__ */ Error(`Upstream response exceeds ${f} bytes, the tunnel's limit`)) : n.push(e);
|
|
228
|
-
}), t.on("end", () => {
|
|
229
|
-
e({
|
|
230
|
-
status: t.statusCode ?? 502,
|
|
231
|
-
headers: D(t.headers),
|
|
232
|
-
body: Buffer.concat(n)
|
|
233
|
-
});
|
|
234
|
-
}), t.on("error", a);
|
|
235
|
-
});
|
|
236
|
-
c > 0 && o.setTimeout(c, () => o.destroy(new A(c))), o.on("error", a), n.length > 0 ? o.end(n) : o.end();
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
//#endregion
|
|
240
|
-
//#region src/tunnel/serve.ts
|
|
241
|
-
function F(e, t) {
|
|
242
|
-
return {
|
|
243
|
-
status: e,
|
|
244
|
-
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
245
|
-
body: Buffer.from(t, "utf8")
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
async function I(e, t, n = {}) {
|
|
249
|
-
let r = new AbortController(), i = () => r.abort();
|
|
250
|
-
e.once("end", i), e.once("close", i), e.on("error", i);
|
|
251
|
-
let a = C(e);
|
|
252
|
-
try {
|
|
253
|
-
for (;;) {
|
|
254
|
-
let { meta: i, body: o } = await a, s;
|
|
255
|
-
try {
|
|
256
|
-
s = await P(t, i, o, {
|
|
257
|
-
timeoutMs: n.upstreamTimeoutMs,
|
|
258
|
-
signal: r.signal
|
|
259
|
-
});
|
|
260
|
-
} catch (e) {
|
|
261
|
-
if (r.signal.aborted) return;
|
|
262
|
-
let t = e instanceof Error ? e.message : String(e);
|
|
263
|
-
s = e instanceof A ? F(504, `Gateway Hyper upstream timeout: ${t}`) : F(502, `Gateway Hyper upstream error: ${t}`);
|
|
264
|
-
}
|
|
265
|
-
await w(e, {
|
|
266
|
-
status: s.status,
|
|
267
|
-
headers: s.headers
|
|
268
|
-
}, s.body), a = C(e);
|
|
269
|
-
}
|
|
270
|
-
} catch {
|
|
271
|
-
e.destroy();
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
async function L(e) {
|
|
275
|
-
let t = e.upstream.replace(/\/$/, ""), r = u(e.keyFile ?? ".seed/gateway-tunnel/operator.key.json"), i = new n(), a = i.createServer((n) => {
|
|
276
|
-
I(n, t, { upstreamTimeoutMs: e.upstreamTimeoutMs });
|
|
277
|
-
});
|
|
278
|
-
return await a.listen(r), {
|
|
279
|
-
key: r.z32,
|
|
280
|
-
upstream: t,
|
|
281
|
-
close: async () => {
|
|
282
|
-
await a.close(), await i.destroy();
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
//#endregion
|
|
287
|
-
//#region src/tunnel/mountPath.ts
|
|
288
|
-
function R(e, t) {
|
|
289
|
-
let n = e || "/", r = n.indexOf("?"), i = r === -1 ? n : n.slice(0, r), a = r === -1 ? "" : n.slice(r), o = (t || "").replace(/\/$/, "");
|
|
290
|
-
if (!o || o === "/") return `${i.startsWith("/") ? i : `/${i}`}${a}` || "/";
|
|
291
|
-
if (i === o || i.startsWith(`${o}/`)) {
|
|
292
|
-
let e = i.slice(o.length);
|
|
293
|
-
return `${e.startsWith("/") ? e : e ? `/${e}` : "/"}${a}`;
|
|
294
|
-
}
|
|
295
|
-
return `${i.startsWith("/") ? i : `/${i}`}${a}`;
|
|
296
|
-
}
|
|
297
|
-
//#endregion
|
|
298
|
-
//#region src/tunnel/forward.ts
|
|
299
|
-
async function z(e) {
|
|
300
|
-
let t = [];
|
|
301
|
-
for await (let n of e) t.push(Buffer.isBuffer(n) ? n : Buffer.from(n));
|
|
302
|
-
return Buffer.concat(t);
|
|
303
|
-
}
|
|
304
|
-
function B(e) {
|
|
305
|
-
return e instanceof O ? 504 : 502;
|
|
306
|
-
}
|
|
307
|
-
function V(e) {
|
|
308
|
-
return `Gateway Hyper tunnel error: ${e instanceof Error ? e.message : String(e)}`;
|
|
309
|
-
}
|
|
310
|
-
async function H(e, t, n, r) {
|
|
311
|
-
let i = new AbortController(), a = () => {
|
|
312
|
-
n.writableEnded || i.abort();
|
|
313
|
-
};
|
|
314
|
-
n.once("close", a);
|
|
315
|
-
try {
|
|
316
|
-
let a = await z(t), o = R(t.url ?? "/", r), s = {
|
|
317
|
-
method: t.method ?? "GET",
|
|
318
|
-
path: o,
|
|
319
|
-
headers: E(t.headers)
|
|
320
|
-
}, { meta: c, body: l } = await e.forwardHttp(s, a, { signal: i.signal });
|
|
321
|
-
U(n, c, l);
|
|
322
|
-
} catch (e) {
|
|
323
|
-
if (i.signal.aborted) return;
|
|
324
|
-
n.headersSent || n.writeHead(B(e), { "Content-Type": "text/plain; charset=utf-8" }), n.end(V(e));
|
|
325
|
-
} finally {
|
|
326
|
-
n.off("close", a);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
function U(e, t, n) {
|
|
330
|
-
e.writeHead(t.status, D(t.headers)), e.end(n);
|
|
331
|
-
}
|
|
332
|
-
async function W(e, t, n) {
|
|
333
|
-
try {
|
|
334
|
-
let r = new URL(t.url), i = R(`${r.pathname}${r.search}`, n), a = Buffer.from(await t.arrayBuffer()), o = {};
|
|
335
|
-
t.headers.forEach((e, t) => {
|
|
336
|
-
o[t] = e;
|
|
337
|
-
});
|
|
338
|
-
let s = {
|
|
339
|
-
method: t.method || "GET",
|
|
340
|
-
path: i,
|
|
341
|
-
headers: E(o)
|
|
342
|
-
}, { meta: c, body: l } = await e.forwardHttp(s, a, { signal: t.signal });
|
|
343
|
-
return new Response(l, {
|
|
344
|
-
status: c.status,
|
|
345
|
-
headers: D(c.headers)
|
|
346
|
-
});
|
|
347
|
-
} catch (e) {
|
|
348
|
-
return new Response(V(e), {
|
|
349
|
-
status: B(e),
|
|
350
|
-
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
//#endregion
|
|
355
|
-
//#region src/tunnel/session.ts
|
|
356
|
-
var G = 6e4, K = 3e4, q = 4;
|
|
357
|
-
function J(e) {
|
|
358
|
-
let t = e.reason;
|
|
359
|
-
if (t instanceof Error) return t;
|
|
360
|
-
let n = /* @__PURE__ */ Error("The request was aborted");
|
|
361
|
-
return n.name = "AbortError", n;
|
|
362
|
-
}
|
|
363
|
-
function Y(e, t) {
|
|
364
|
-
return t.aborted ? Promise.reject(J(t)) : new Promise((n, r) => {
|
|
365
|
-
let i = () => r(J(t));
|
|
366
|
-
t.addEventListener("abort", i, { once: !0 }), e.then((e) => {
|
|
367
|
-
t.removeEventListener("abort", i), n(e);
|
|
368
|
-
}, (e) => {
|
|
369
|
-
t.removeEventListener("abort", i), r(e);
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
function X(e, t = G) {
|
|
374
|
-
let n = async (n, r) => {
|
|
375
|
-
let { signal: i } = r, a = r.timeoutMs ?? t;
|
|
376
|
-
if (i?.aborted) throw J(i);
|
|
377
|
-
let o = await e();
|
|
378
|
-
if (i?.aborted) throw J(i);
|
|
379
|
-
return new Promise((e, t) => {
|
|
380
|
-
let r = !1, s = null, c = () => {
|
|
381
|
-
a <= 0 || (s && clearTimeout(s), s = setTimeout(() => u(new O(a)), a));
|
|
382
|
-
}, l = () => {
|
|
383
|
-
s && clearTimeout(s), o.off("data", c), o.off("drain", c), i?.removeEventListener("abort", d);
|
|
384
|
-
}, u = (e) => {
|
|
385
|
-
r || (r = !0, l(), o.destroy(), t(e));
|
|
386
|
-
}, d = () => u(J(i));
|
|
387
|
-
c(), o.on("data", c), o.on("drain", c), i?.addEventListener("abort", d, { once: !0 }), n(o).then((t) => {
|
|
388
|
-
r || (r = !0, l(), e(t));
|
|
389
|
-
}, u);
|
|
390
|
-
});
|
|
391
|
-
}, r = Promise.resolve();
|
|
392
|
-
return (e, t = {}) => {
|
|
393
|
-
let i = r.then(() => n(e, t));
|
|
394
|
-
return r = i.catch(() => {}), t.signal ? Y(i, t.signal) : i;
|
|
395
|
-
};
|
|
396
|
-
}
|
|
397
|
-
function Z() {
|
|
398
|
-
return /* @__PURE__ */ Error("Tunnel session is closed");
|
|
399
|
-
}
|
|
400
|
-
function Q(e, t = {}) {
|
|
401
|
-
let n = Math.max(1, Math.floor(t.maxStreams ?? 4)), r = t.requestTimeoutMs ?? 6e4, i = [], a = [], o = !1, s = () => {
|
|
402
|
-
let t = {
|
|
403
|
-
run: null,
|
|
404
|
-
socket: null,
|
|
405
|
-
busy: !1
|
|
406
|
-
};
|
|
407
|
-
return t.run = X(async () => {
|
|
408
|
-
if (o) throw Z();
|
|
409
|
-
if (t.socket && !t.socket.destroyed) return t.socket;
|
|
410
|
-
let n = await e();
|
|
411
|
-
if (o) throw n.destroy(), Z();
|
|
412
|
-
return t.socket = n, n;
|
|
413
|
-
}, r), i.push(t), t;
|
|
414
|
-
}, c = () => {
|
|
415
|
-
let e = i.filter((e) => !e.busy), t = e.find((e) => e.socket !== null && !e.socket.destroyed) ?? e[0] ?? (i.length < n ? s() : void 0);
|
|
416
|
-
return t ? (t.busy = !0, t) : null;
|
|
417
|
-
}, l = (e) => {
|
|
418
|
-
let t = a.shift();
|
|
419
|
-
t ? t.resolve(e) : e.busy = !1;
|
|
420
|
-
}, u = (e) => {
|
|
421
|
-
let t = c();
|
|
422
|
-
return t ? Promise.resolve(t) : new Promise((t, n) => {
|
|
423
|
-
let r = () => {
|
|
424
|
-
let t = a.indexOf(i);
|
|
425
|
-
t >= 0 && a.splice(t, 1), n(J(e));
|
|
426
|
-
}, i = {
|
|
427
|
-
resolve: (n) => {
|
|
428
|
-
e?.removeEventListener("abort", r), t(n);
|
|
429
|
-
},
|
|
430
|
-
reject: (t) => {
|
|
431
|
-
e?.removeEventListener("abort", r), n(t);
|
|
432
|
-
}
|
|
433
|
-
};
|
|
434
|
-
a.push(i), e?.addEventListener("abort", r, { once: !0 });
|
|
435
|
-
});
|
|
436
|
-
};
|
|
437
|
-
return {
|
|
438
|
-
runOnTunnel: async (e, t = {}) => {
|
|
439
|
-
if (t.signal?.aborted) throw J(t.signal);
|
|
440
|
-
if (o) throw Z();
|
|
441
|
-
let n = await u(t.signal);
|
|
442
|
-
try {
|
|
443
|
-
return await n.run(e, t);
|
|
444
|
-
} finally {
|
|
445
|
-
l(n);
|
|
446
|
-
}
|
|
447
|
-
},
|
|
448
|
-
close: () => {
|
|
449
|
-
o = !0;
|
|
450
|
-
for (let e of a.splice(0)) e.reject(Z());
|
|
451
|
-
for (let e of i) e.socket?.destroy();
|
|
452
|
-
}
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
async function $(e) {
|
|
456
|
-
let t = l(e.key), r = d(), i = e.connectTimeoutMs ?? 3e4, a = new n(), o = Q(() => new Promise((e, n) => {
|
|
457
|
-
let o = a.connect(t, { keyPair: {
|
|
458
|
-
publicKey: r.publicKey,
|
|
459
|
-
secretKey: r.secretKey
|
|
460
|
-
} }), s = !1, c = i > 0 ? setTimeout(() => l(new k(i)), i) : null, l = (e) => {
|
|
461
|
-
s || (s = !0, c && clearTimeout(c), o.destroy(), n(e));
|
|
462
|
-
}, u = () => {
|
|
463
|
-
s || (s = !0, c && clearTimeout(c), e(o));
|
|
464
|
-
};
|
|
465
|
-
o.on("error", (e) => l(e)), o.once("close", () => l(/* @__PURE__ */ Error("Tunnel connection closed before it opened"))), o.once("open", u), o.once("connect", u);
|
|
466
|
-
}), {
|
|
467
|
-
maxStreams: e.maxStreams,
|
|
468
|
-
requestTimeoutMs: e.requestTimeoutMs
|
|
469
|
-
}), { runOnTunnel: s } = o;
|
|
470
|
-
return {
|
|
471
|
-
key: e.key,
|
|
472
|
-
runOnTunnel: s,
|
|
473
|
-
forwardHttp: async (e, t, n) => s(async (n) => (await S(n, e, t), T(n)), n),
|
|
474
|
-
close: async () => {
|
|
475
|
-
o.close(), await a.destroy();
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
//#endregion
|
|
480
|
-
//#region src/tunnel/connect.ts
|
|
481
|
-
async function ee(e) {
|
|
482
|
-
let t = e.host ?? "127.0.0.1", n = e.port ?? 1984, r = await $({
|
|
483
|
-
key: e.key,
|
|
484
|
-
storePath: e.storePath,
|
|
485
|
-
requestTimeoutMs: e.requestTimeoutMs,
|
|
486
|
-
connectTimeoutMs: e.connectTimeoutMs,
|
|
487
|
-
maxStreams: e.maxStreams
|
|
488
|
-
}), a = i.createServer((e, t) => {
|
|
489
|
-
H(r, e, t, "");
|
|
490
|
-
});
|
|
491
|
-
await new Promise((e, r) => {
|
|
492
|
-
a.once("error", r), a.listen(n, t, () => e());
|
|
493
|
-
});
|
|
494
|
-
let o = a.address(), s = o && typeof o == "object" ? o.port : n, c = `http://${t}:${s}`;
|
|
495
|
-
return {
|
|
496
|
-
key: e.key,
|
|
497
|
-
host: t,
|
|
498
|
-
port: s,
|
|
499
|
-
baseUrl: c,
|
|
500
|
-
close: async () => {
|
|
501
|
-
await new Promise((e) => a.close(() => e())), await r.close();
|
|
502
|
-
}
|
|
503
|
-
};
|
|
504
|
-
}
|
|
505
|
-
function te(e, t = "") {
|
|
506
|
-
let n = e.replace(/\/$/, "");
|
|
507
|
-
return t ? t.startsWith("/") ? `${n}${t}` : `${n}/${t}` : n;
|
|
508
|
-
}
|
|
509
|
-
//#endregion
|
|
510
|
-
//#region src/tunnel/proxyApp.ts
|
|
511
|
-
function ne(e) {
|
|
512
|
-
let t = e.key?.trim() || (typeof process < "u" ? process.env.SEED_GATEWAY_HYPER_KEY?.trim() : "") || "";
|
|
513
|
-
if (!t) throw Error("createGatewayProxy requires options.key or SEED_GATEWAY_HYPER_KEY environment variable");
|
|
514
|
-
return t;
|
|
515
|
-
}
|
|
516
|
-
function re(e = {}) {
|
|
517
|
-
let t = e.mountPath?.trim() || "/api/seed-gateway", n = ne(e), r = null, i = null, a = async () => {
|
|
518
|
-
if (r) return r;
|
|
519
|
-
if (i) return i;
|
|
520
|
-
i = $({
|
|
521
|
-
key: n,
|
|
522
|
-
storePath: e.storePath,
|
|
523
|
-
requestTimeoutMs: e.requestTimeoutMs,
|
|
524
|
-
connectTimeoutMs: e.connectTimeoutMs,
|
|
525
|
-
maxStreams: e.maxStreams
|
|
526
|
-
}).then((e) => (r = e, i = null, e));
|
|
527
|
-
try {
|
|
528
|
-
return await i;
|
|
529
|
-
} catch (e) {
|
|
530
|
-
throw i = null, e;
|
|
531
|
-
}
|
|
532
|
-
}, o = {
|
|
533
|
-
mountPath: t,
|
|
534
|
-
start: async () => {
|
|
535
|
-
await a();
|
|
536
|
-
},
|
|
537
|
-
close: async () => {
|
|
538
|
-
if (i) try {
|
|
539
|
-
await i;
|
|
540
|
-
} catch {}
|
|
541
|
-
r &&= (await r.close(), null);
|
|
542
|
-
},
|
|
543
|
-
handleNode: (e, n) => {
|
|
544
|
-
(async () => {
|
|
545
|
-
try {
|
|
546
|
-
await H(await a(), e, n, t);
|
|
547
|
-
} catch (e) {
|
|
548
|
-
let t = e instanceof Error ? e.message : String(e);
|
|
549
|
-
n.headersSent || n.writeHead(502, { "Content-Type": "text/plain; charset=utf-8" }), n.end(`Gateway Hyper tunnel error: ${t}`);
|
|
550
|
-
}
|
|
551
|
-
})();
|
|
552
|
-
},
|
|
553
|
-
handleFetch: async (e) => {
|
|
554
|
-
try {
|
|
555
|
-
return await W(await a(), e, t);
|
|
556
|
-
} catch (e) {
|
|
557
|
-
let t = e instanceof Error ? e.message : String(e);
|
|
558
|
-
return new Response(`Gateway Hyper tunnel error: ${t}`, {
|
|
559
|
-
status: 502,
|
|
560
|
-
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
return e.eager && o.start().catch(() => {}), o;
|
|
566
|
-
}
|
|
567
|
-
//#endregion
|
|
568
|
-
//#region src/constants.ts
|
|
569
|
-
var ie = "";
|
|
570
|
-
//#endregion
|
|
571
|
-
export { w as C, u as E, S, c as T, E as _, K as a, T as b, $ as c, j as d, k as f, D as g, f as h, te as i, R as l, A as m, re as n, q as o, O as p, ee as r, G as s, ie as t, L as u, x as v, l as w, b as x, C as y };
|
|
572
|
-
|
|
573
|
-
//# sourceMappingURL=src-qIlmrs1H.js.map
|