@restatedev/restate-sdk 1.17.4 → 1.18.1
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/common_api.d.cts +1 -1
- package/dist/common_api.d.cts.map +1 -1
- package/dist/common_api.d.ts +1 -1
- package/dist/common_api.d.ts.map +1 -1
- package/dist/common_api.js.map +1 -1
- package/dist/context.d.cts +3 -0
- package/dist/context.d.cts.map +1 -1
- package/dist/context.d.ts +3 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js.map +1 -1
- package/dist/context_impl.cjs +8 -3
- package/dist/context_impl.js +8 -3
- package/dist/context_impl.js.map +1 -1
- package/dist/endpoint/components.cjs +28 -6
- package/dist/endpoint/components.js +28 -6
- package/dist/endpoint/components.js.map +1 -1
- package/dist/endpoint/connect/env.cjs +206 -0
- package/dist/endpoint/connect/env.js +199 -0
- package/dist/endpoint/connect/env.js.map +1 -0
- package/dist/endpoint/connect/index.cjs +113 -0
- package/dist/endpoint/connect/index.d.cts +41 -0
- package/dist/endpoint/connect/index.d.cts.map +1 -0
- package/dist/endpoint/connect/index.d.ts +41 -0
- package/dist/endpoint/connect/index.d.ts.map +1 -0
- package/dist/endpoint/connect/index.js +114 -0
- package/dist/endpoint/connect/index.js.map +1 -0
- package/dist/endpoint/connect/key.cjs +53 -0
- package/dist/endpoint/connect/key.js +53 -0
- package/dist/endpoint/connect/key.js.map +1 -0
- package/dist/endpoint/handlers/discovery.cjs +12 -2
- package/dist/endpoint/handlers/discovery.js +12 -2
- package/dist/endpoint/handlers/discovery.js.map +1 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.cjs +1 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js +1 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js.map +1 -1
- package/dist/endpoint/tunnel/backoff.cjs +37 -0
- package/dist/endpoint/tunnel/backoff.js +36 -0
- package/dist/endpoint/tunnel/backoff.js.map +1 -0
- package/dist/endpoint/tunnel/connection.cjs +604 -0
- package/dist/endpoint/tunnel/connection.js +600 -0
- package/dist/endpoint/tunnel/connection.js.map +1 -0
- package/dist/endpoint/tunnel/draining.cjs +40 -0
- package/dist/endpoint/tunnel/draining.js +40 -0
- package/dist/endpoint/tunnel/draining.js.map +1 -0
- package/dist/endpoint/tunnel/engine.cjs +264 -0
- package/dist/endpoint/tunnel/engine.d.cts +1 -0
- package/dist/endpoint/tunnel/engine.d.ts +1 -0
- package/dist/endpoint/tunnel/engine.js +265 -0
- package/dist/endpoint/tunnel/engine.js.map +1 -0
- package/dist/endpoint/tunnel/forwarded.cjs +34 -0
- package/dist/endpoint/tunnel/forwarded.js +34 -0
- package/dist/endpoint/tunnel/forwarded.js.map +1 -0
- package/dist/endpoint/tunnel/handshake.cjs +98 -0
- package/dist/endpoint/tunnel/handshake.js +97 -0
- package/dist/endpoint/tunnel/handshake.js.map +1 -0
- package/dist/endpoint/tunnel/index.d.cts +3 -0
- package/dist/endpoint/tunnel/index.d.ts +3 -0
- package/dist/endpoint/tunnel/options.cjs +168 -0
- package/dist/endpoint/tunnel/options.d.cts +2 -0
- package/dist/endpoint/tunnel/options.d.ts +2 -0
- package/dist/endpoint/tunnel/options.js +161 -0
- package/dist/endpoint/tunnel/options.js.map +1 -0
- package/dist/endpoint/tunnel/supervisor.cjs +180 -0
- package/dist/endpoint/tunnel/supervisor.js +181 -0
- package/dist/endpoint/tunnel/supervisor.js.map +1 -0
- package/dist/endpoint/tunnel/targets.cjs +110 -0
- package/dist/endpoint/tunnel/targets.js +107 -0
- package/dist/endpoint/tunnel/targets.js.map +1 -0
- package/dist/endpoint/tunnel/types.d.cts +270 -0
- package/dist/endpoint/tunnel/types.d.cts.map +1 -0
- package/dist/endpoint/tunnel/types.d.ts +270 -0
- package/dist/endpoint/tunnel/types.d.ts.map +1 -0
- package/dist/endpoint/tunnel/util.cjs +67 -0
- package/dist/endpoint/tunnel/util.js +65 -0
- package/dist/endpoint/tunnel/util.js.map +1 -0
- package/dist/fetch.d.cts +2 -2
- package/dist/fetch.d.ts +2 -2
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/dist/lambda.d.cts +2 -2
- package/dist/lambda.d.ts +2 -2
- package/dist/node.cjs +2 -0
- package/dist/node.d.cts +5 -2
- package/dist/node.d.cts.map +1 -1
- package/dist/node.d.ts +5 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2 -1
- package/dist/node.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/dist/types/errors.cjs +4 -2
- package/dist/types/errors.d.cts.map +1 -1
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +4 -2
- package/dist/types/errors.js.map +1 -1
- package/dist/types/rpc.d.cts +42 -17
- package/dist/types/rpc.d.cts.map +1 -1
- package/dist/types/rpc.d.ts +42 -17
- package/dist/types/rpc.d.ts.map +1 -1
- package/dist/types/rpc.js.map +1 -1
- package/package.json +2 -2
- package/dist/context_impl.d.ts.map +0 -1
- package/dist/endpoint/components.d.ts.map +0 -1
- package/dist/endpoint/discovery.d.ts +0 -184
- package/dist/endpoint/discovery.d.ts.map +0 -1
- package/dist/endpoint/discovery.js +0 -7
- package/dist/endpoint/discovery.js.map +0 -1
- package/dist/endpoint/endpoint.d.ts.map +0 -1
- package/dist/endpoint/handlers/core_logging.d.ts +0 -14
- package/dist/endpoint/handlers/core_logging.d.ts.map +0 -1
- package/dist/endpoint/handlers/discovery.d.ts +0 -5
- package/dist/endpoint/handlers/discovery.d.ts.map +0 -1
- package/dist/endpoint/handlers/fetch.d.ts +0 -5
- package/dist/endpoint/handlers/fetch.d.ts.map +0 -1
- package/dist/endpoint/handlers/generic.d.ts +0 -5
- package/dist/endpoint/handlers/generic.d.ts.map +0 -1
- package/dist/endpoint/handlers/lambda.d.ts +0 -10
- package/dist/endpoint/handlers/lambda.d.ts.map +0 -1
- package/dist/endpoint/handlers/preview.d.ts +0 -5
- package/dist/endpoint/handlers/preview.d.ts.map +0 -1
- package/dist/endpoint/handlers/types.d.ts.map +0 -1
- package/dist/endpoint/handlers/types.js +0 -2
- package/dist/endpoint/handlers/types.js.map +0 -1
- package/dist/endpoint/handlers/utils.d.ts +0 -23
- package/dist/endpoint/handlers/utils.d.ts.map +0 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts +0 -454
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts.map +0 -1
- package/dist/endpoint/node_endpoint.d.ts +0 -28
- package/dist/endpoint/node_endpoint.d.ts.map +0 -1
- package/dist/endpoint/types.js +0 -2
- package/dist/endpoint/types.js.map +0 -1
- package/dist/endpoint/withOptions.d.ts +0 -4
- package/dist/endpoint/withOptions.d.ts.map +0 -1
- package/dist/endpoint.js +0 -12
- package/dist/endpoint.js.map +0 -1
- package/dist/error_sanitization.d.ts +0 -13
- package/dist/error_sanitization.d.ts.map +0 -1
- package/dist/hooks.js +0 -2
- package/dist/hooks.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/io.d.ts.map +0 -1
- package/dist/logging/console_logger_transport.d.ts +0 -5
- package/dist/logging/console_logger_transport.d.ts.map +0 -1
- package/dist/logging/logger.d.ts.map +0 -1
- package/dist/user_agent.d.ts +0 -2
- package/dist/user_agent.d.ts.map +0 -1
- package/dist/utils/external_progress_channel.d.ts +0 -19
- package/dist/utils/external_progress_channel.d.ts.map +0 -1
- package/dist/utils/rand.d.ts +0 -13
- package/dist/utils/rand.d.ts.map +0 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/endpoint/tunnel/backoff.ts
|
|
3
|
+
/**
|
|
4
|
+
* Backoff resets only when a served connection stayed up at least this long
|
|
5
|
+
* (mirrors the Rust client's 5s "opened" guard). Without it, a server that
|
|
6
|
+
* authorizes the handshake but immediately drops the connection would be
|
|
7
|
+
* redialed at the backoff floor forever — a full TLS+h2+auth round trip
|
|
8
|
+
* every ~10ms.
|
|
9
|
+
*/
|
|
10
|
+
const MIN_UPTIME_FOR_BACKOFF_RESET_MS = 5e3;
|
|
11
|
+
/**
|
|
12
|
+
* Jittered exponential backoff: each `next()` returns the current delay
|
|
13
|
+
* with ±50% jitter and advances the schedule toward `maxMs`; `reset()`
|
|
14
|
+
* returns to the floor. Jitter keeps multi-homed slots from redialing in
|
|
15
|
+
* lockstep after a fleet-wide blip (thundering herd).
|
|
16
|
+
*/
|
|
17
|
+
var Backoff = class {
|
|
18
|
+
currentMs;
|
|
19
|
+
constructor(initialMs, factor, maxMs) {
|
|
20
|
+
this.initialMs = initialMs;
|
|
21
|
+
this.factor = factor;
|
|
22
|
+
this.maxMs = maxMs;
|
|
23
|
+
this.currentMs = initialMs;
|
|
24
|
+
}
|
|
25
|
+
next() {
|
|
26
|
+
const d = this.currentMs;
|
|
27
|
+
this.currentMs = Math.min(this.currentMs * this.factor, this.maxMs);
|
|
28
|
+
return d * (.5 + Math.random());
|
|
29
|
+
}
|
|
30
|
+
reset() {
|
|
31
|
+
this.currentMs = this.initialMs;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
exports.Backoff = Backoff;
|
|
37
|
+
exports.MIN_UPTIME_FOR_BACKOFF_RESET_MS = MIN_UPTIME_FOR_BACKOFF_RESET_MS;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/endpoint/tunnel/backoff.ts
|
|
2
|
+
/**
|
|
3
|
+
* Backoff resets only when a served connection stayed up at least this long
|
|
4
|
+
* (mirrors the Rust client's 5s "opened" guard). Without it, a server that
|
|
5
|
+
* authorizes the handshake but immediately drops the connection would be
|
|
6
|
+
* redialed at the backoff floor forever — a full TLS+h2+auth round trip
|
|
7
|
+
* every ~10ms.
|
|
8
|
+
*/
|
|
9
|
+
const MIN_UPTIME_FOR_BACKOFF_RESET_MS = 5e3;
|
|
10
|
+
/**
|
|
11
|
+
* Jittered exponential backoff: each `next()` returns the current delay
|
|
12
|
+
* with ±50% jitter and advances the schedule toward `maxMs`; `reset()`
|
|
13
|
+
* returns to the floor. Jitter keeps multi-homed slots from redialing in
|
|
14
|
+
* lockstep after a fleet-wide blip (thundering herd).
|
|
15
|
+
*/
|
|
16
|
+
var Backoff = class {
|
|
17
|
+
currentMs;
|
|
18
|
+
constructor(initialMs, factor, maxMs) {
|
|
19
|
+
this.initialMs = initialMs;
|
|
20
|
+
this.factor = factor;
|
|
21
|
+
this.maxMs = maxMs;
|
|
22
|
+
this.currentMs = initialMs;
|
|
23
|
+
}
|
|
24
|
+
next() {
|
|
25
|
+
const d = this.currentMs;
|
|
26
|
+
this.currentMs = Math.min(this.currentMs * this.factor, this.maxMs);
|
|
27
|
+
return d * (.5 + Math.random());
|
|
28
|
+
}
|
|
29
|
+
reset() {
|
|
30
|
+
this.currentMs = this.initialMs;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { Backoff, MIN_UPTIME_FOR_BACKOFF_RESET_MS };
|
|
36
|
+
//# sourceMappingURL=backoff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backoff.js","names":["initialMs: number","factor: number","maxMs: number"],"sources":["../../../src/endpoint/tunnel/backoff.ts"],"sourcesContent":["/*\n * Copyright (c) 2023-2026 - Restate Software, Inc., Restate GmbH\n *\n * This file is part of the Restate SDK for Node.js/TypeScript,\n * which is released under the MIT license.\n *\n * You can find a copy of the license in file LICENSE in the root\n * directory of this repository or package, or at\n * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE\n */\n\n// Reconnect backoff policy.\n\n/**\n * Backoff resets only when a served connection stayed up at least this long\n * (mirrors the Rust client's 5s \"opened\" guard). Without it, a server that\n * authorizes the handshake but immediately drops the connection would be\n * redialed at the backoff floor forever — a full TLS+h2+auth round trip\n * every ~10ms.\n */\nexport const MIN_UPTIME_FOR_BACKOFF_RESET_MS = 5_000;\n\n/**\n * Jittered exponential backoff: each `next()` returns the current delay\n * with ±50% jitter and advances the schedule toward `maxMs`; `reset()`\n * returns to the floor. Jitter keeps multi-homed slots from redialing in\n * lockstep after a fleet-wide blip (thundering herd).\n */\nexport class Backoff {\n private currentMs: number;\n\n constructor(\n private readonly initialMs: number,\n private readonly factor: number,\n private readonly maxMs: number\n ) {\n this.currentMs = initialMs;\n }\n\n next(): number {\n const d = this.currentMs;\n this.currentMs = Math.min(this.currentMs * this.factor, this.maxMs);\n return d * (0.5 + Math.random());\n }\n\n reset(): void {\n this.currentMs = this.initialMs;\n }\n}\n"],"mappings":";;;;;;;;AAoBA,MAAa,kCAAkC;;;;;;;AAQ/C,IAAa,UAAb,MAAqB;CACnB,AAAQ;CAER,YACE,AAAiBA,WACjB,AAAiBC,QACjB,AAAiBC,OACjB;EAHiB;EACA;EACA;AAEjB,OAAK,YAAY;;CAGnB,OAAe;EACb,MAAM,IAAI,KAAK;AACf,OAAK,YAAY,KAAK,IAAI,KAAK,YAAY,KAAK,QAAQ,KAAK,MAAM;AACnE,SAAO,KAAK,KAAM,KAAK,QAAQ;;CAGjC,QAAc;AACZ,OAAK,YAAY,KAAK"}
|
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_options = require('./options.cjs');
|
|
3
|
+
const require_handshake = require('./handshake.cjs');
|
|
4
|
+
const require_forwarded = require('./forwarded.cjs');
|
|
5
|
+
let node_http2 = require("node:http2");
|
|
6
|
+
node_http2 = require_rolldown_runtime.__toESM(node_http2);
|
|
7
|
+
let node_crypto = require("node:crypto");
|
|
8
|
+
node_crypto = require_rolldown_runtime.__toESM(node_crypto);
|
|
9
|
+
let node_net = require("node:net");
|
|
10
|
+
node_net = require_rolldown_runtime.__toESM(node_net);
|
|
11
|
+
let node_tls = require("node:tls");
|
|
12
|
+
node_tls = require_rolldown_runtime.__toESM(node_tls);
|
|
13
|
+
|
|
14
|
+
//#region src/endpoint/tunnel/connection.ts
|
|
15
|
+
const CLIENT_DRAIN_SESSION_CLOSE_TIMEOUT_MS = 1e3;
|
|
16
|
+
const TUNNEL_DRAINING_HEADER = "x-restate-tunnel-draining";
|
|
17
|
+
const CROCKFORD_BASE32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
18
|
+
function encodeBase32(value, length) {
|
|
19
|
+
let out = "";
|
|
20
|
+
for (let i = 0; i < length; i++) {
|
|
21
|
+
out = CROCKFORD_BASE32.charAt(Number(value & 31n)) + out;
|
|
22
|
+
value >>= 5n;
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function newTunnelConnectionId() {
|
|
27
|
+
let random = 0n;
|
|
28
|
+
for (const byte of (0, node_crypto.randomBytes)(10)) random = random << 8n | BigInt(byte);
|
|
29
|
+
return `${encodeBase32(BigInt(Date.now()), 10)}${encodeBase32(random, 16)}`;
|
|
30
|
+
}
|
|
31
|
+
function formatIdentity(workerId, connectionId) {
|
|
32
|
+
return `worker_id=${workerId} connection_id=${connectionId}`;
|
|
33
|
+
}
|
|
34
|
+
function targetLabel(target) {
|
|
35
|
+
return `${target.host}:${target.port}`;
|
|
36
|
+
}
|
|
37
|
+
function formatConnectionOutcome(outcome) {
|
|
38
|
+
switch (outcome.kind) {
|
|
39
|
+
case "served": return `served uptimeMs=${outcome.uptimeMs}`;
|
|
40
|
+
case "drained": return `drained uptimeMs=${outcome.uptimeMs}`;
|
|
41
|
+
case "retryable": return `retryable reason=${outcome.reason}`;
|
|
42
|
+
case "fatal": return `fatal reason=${outcome.reason}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function formatSettings(settings) {
|
|
46
|
+
const entries = Object.entries(settings).filter(([, value]) => value !== void 0);
|
|
47
|
+
if (entries.length === 0) return "{}";
|
|
48
|
+
return `{${entries.map(([key, value]) => `${key}=${String(value)}`).join(", ")}}`;
|
|
49
|
+
}
|
|
50
|
+
function pathWithoutQuery(url) {
|
|
51
|
+
if (url === void 0) return "?";
|
|
52
|
+
const queryStart = url.indexOf("?");
|
|
53
|
+
return queryStart === -1 ? url : url.slice(0, queryStart);
|
|
54
|
+
}
|
|
55
|
+
function endInternalError(res) {
|
|
56
|
+
try {
|
|
57
|
+
if (!res.headersSent) res.writeHead(500);
|
|
58
|
+
if (!res.writableEnded) res.end("tunnel: SDK handler error");
|
|
59
|
+
} catch {}
|
|
60
|
+
}
|
|
61
|
+
/** Classify an incoming h2 request. Control paths are cloud-originated and
|
|
62
|
+
* arrive UNPREFIXED (before any destination-prefix stripping). */
|
|
63
|
+
function classifyRequest(req) {
|
|
64
|
+
const rawPath = (req.url ?? "").split("?")[0];
|
|
65
|
+
if (req.method === "GET" && rawPath === require_handshake.START_TUNNEL_PATH) return { kind: "start-tunnel" };
|
|
66
|
+
if (rawPath === "/_/health") return { kind: "health" };
|
|
67
|
+
if (rawPath === "/_/drain-tunnel") return { kind: "drain" };
|
|
68
|
+
return { kind: "forwarded" };
|
|
69
|
+
}
|
|
70
|
+
/** Resolves a connection attempt's outcome exactly once. */
|
|
71
|
+
var Completion = class {
|
|
72
|
+
done = false;
|
|
73
|
+
resolveFn;
|
|
74
|
+
promise = new Promise((resolve) => {
|
|
75
|
+
this.resolveFn = resolve;
|
|
76
|
+
});
|
|
77
|
+
get settled() {
|
|
78
|
+
return this.done;
|
|
79
|
+
}
|
|
80
|
+
/** Resolve once; returns false if it was already resolved. */
|
|
81
|
+
resolve(outcome) {
|
|
82
|
+
if (this.done) return false;
|
|
83
|
+
this.done = true;
|
|
84
|
+
this.resolveFn(outcome);
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Liveness watchdog: periodic h2 PING; `pingMaxMissed` consecutive misses mean
|
|
90
|
+
* the connection is half-open (the OS may never surface it), so `onDead` fires.
|
|
91
|
+
* Owns its own timer/miss state so the connection doesn't have to.
|
|
92
|
+
*/
|
|
93
|
+
var Watchdog = class {
|
|
94
|
+
interval;
|
|
95
|
+
missed = 0;
|
|
96
|
+
constructor(session, opts, onDead) {
|
|
97
|
+
this.session = session;
|
|
98
|
+
this.opts = opts;
|
|
99
|
+
this.onDead = onDead;
|
|
100
|
+
}
|
|
101
|
+
start() {
|
|
102
|
+
this.interval = setInterval(() => this.beat(), this.opts.pingIntervalMs);
|
|
103
|
+
this.interval.unref();
|
|
104
|
+
}
|
|
105
|
+
stop() {
|
|
106
|
+
if (this.interval !== void 0) clearInterval(this.interval);
|
|
107
|
+
}
|
|
108
|
+
beat() {
|
|
109
|
+
if (this.session.destroyed) return;
|
|
110
|
+
let acked = false;
|
|
111
|
+
try {
|
|
112
|
+
this.session.ping((err) => {
|
|
113
|
+
if (err === null) {
|
|
114
|
+
acked = true;
|
|
115
|
+
this.missed = 0;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
} catch {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
setTimeout(() => {
|
|
122
|
+
if (acked || this.session.destroyed) return;
|
|
123
|
+
this.missed++;
|
|
124
|
+
if (this.missed >= this.opts.pingMaxMissed) this.onDead();
|
|
125
|
+
}, this.opts.pingTimeoutMs).unref();
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* The dial stage: connect TCP (+ TLS), bounded by `connectTimeoutMs` and the
|
|
130
|
+
* slot abort, and require ALPN to have negotiated h2. Owns the socket until it
|
|
131
|
+
* either hands it back connected or destroys it on failure — so all of the
|
|
132
|
+
* connect-phase timer/listener state stays local here.
|
|
133
|
+
*/
|
|
134
|
+
function dial(target, deps, plaintext, signal, connectionId) {
|
|
135
|
+
const log = deps.opts.logger;
|
|
136
|
+
const identity = formatIdentity(deps.opts.tunnelWorkerId, connectionId);
|
|
137
|
+
const tlsOptions = plaintext ? void 0 : require_options.buildTlsConnectOptions(deps.opts.tls, target.servername);
|
|
138
|
+
const socket = plaintext ? node_net.connect({
|
|
139
|
+
host: target.host,
|
|
140
|
+
port: target.port
|
|
141
|
+
}) : node_tls.connect({
|
|
142
|
+
host: target.host,
|
|
143
|
+
port: target.port,
|
|
144
|
+
...tlsOptions
|
|
145
|
+
});
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
let done = false;
|
|
148
|
+
const label = targetLabel(target);
|
|
149
|
+
const onError = (err) => fail(`socket error: ${err.message}`);
|
|
150
|
+
const onAbort = () => fail("tunnel closed");
|
|
151
|
+
const timer = setTimeout(() => fail(`connect timeout after ${deps.opts.connectTimeoutMs}ms`), deps.opts.connectTimeoutMs);
|
|
152
|
+
timer.unref();
|
|
153
|
+
const cleanup = () => {
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
signal.removeEventListener("abort", onAbort);
|
|
156
|
+
socket.removeListener("error", onError);
|
|
157
|
+
};
|
|
158
|
+
function fail(reason) {
|
|
159
|
+
if (done) return;
|
|
160
|
+
done = true;
|
|
161
|
+
cleanup();
|
|
162
|
+
log(`tunnel: failed to connect to ${label}: ${reason} (${identity})`, "warn");
|
|
163
|
+
socket.destroy();
|
|
164
|
+
resolve({
|
|
165
|
+
ok: false,
|
|
166
|
+
outcome: {
|
|
167
|
+
kind: "retryable",
|
|
168
|
+
reason
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
173
|
+
socket.on("error", onError);
|
|
174
|
+
socket.once(plaintext ? "connect" : "secureConnect", () => {
|
|
175
|
+
if (done) return;
|
|
176
|
+
socket.setNoDelay(true);
|
|
177
|
+
const alpn = plaintext ? "plaintext" : `tls alpn=${JSON.stringify(socket.alpnProtocol)}`;
|
|
178
|
+
if (!plaintext && socket.alpnProtocol !== "h2") {
|
|
179
|
+
fail("tunnel server did not negotiate h2 ALPN — it predates standard-h2 control traffic and cannot serve this client");
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
done = true;
|
|
183
|
+
cleanup();
|
|
184
|
+
log(`tunnel: connected socket to ${label} (${alpn}, ${identity})`);
|
|
185
|
+
resolve({
|
|
186
|
+
ok: true,
|
|
187
|
+
socket
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Run one connection attempt. Resolves (never rejects) with the outcome
|
|
194
|
+
* when the connection ends; `slotSignal` aborts the attempt at any phase.
|
|
195
|
+
*/
|
|
196
|
+
function runConnection(target, slotSignal, deps) {
|
|
197
|
+
let authToken;
|
|
198
|
+
try {
|
|
199
|
+
authToken = deps.opts.authToken();
|
|
200
|
+
} catch (err) {
|
|
201
|
+
return Promise.resolve({
|
|
202
|
+
kind: "retryable",
|
|
203
|
+
reason: `auth token unavailable: ${err instanceof Error ? err.message : String(err)}`
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return new ConnectionAttempt(target, slotSignal, deps, authToken).run();
|
|
207
|
+
}
|
|
208
|
+
var ConnectionAttempt = class {
|
|
209
|
+
state = { kind: "connecting" };
|
|
210
|
+
completion = new Completion();
|
|
211
|
+
/** Lifetime-scoped: destroyed on teardown in any phase, handed to the
|
|
212
|
+
* registry on a server drain. */
|
|
213
|
+
socket;
|
|
214
|
+
plaintext;
|
|
215
|
+
log;
|
|
216
|
+
connectionId = newTunnelConnectionId();
|
|
217
|
+
constructor(target, slotSignal, deps, authToken) {
|
|
218
|
+
this.target = target;
|
|
219
|
+
this.slotSignal = slotSignal;
|
|
220
|
+
this.deps = deps;
|
|
221
|
+
this.authToken = authToken;
|
|
222
|
+
this.log = deps.opts.logger;
|
|
223
|
+
this.plaintext = target.plaintext ?? deps.opts.tls === false;
|
|
224
|
+
}
|
|
225
|
+
get identity() {
|
|
226
|
+
return {
|
|
227
|
+
workerId: this.deps.opts.tunnelWorkerId,
|
|
228
|
+
connectionId: this.connectionId,
|
|
229
|
+
target: targetLabel(this.target)
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
identityLog() {
|
|
233
|
+
return formatIdentity(this.deps.opts.tunnelWorkerId, this.connectionId);
|
|
234
|
+
}
|
|
235
|
+
logWithIdentity(message) {
|
|
236
|
+
this.log(`${message} (${this.identityLog()})`);
|
|
237
|
+
}
|
|
238
|
+
run() {
|
|
239
|
+
this.deps.activeConnections.add(this);
|
|
240
|
+
this.drive();
|
|
241
|
+
return this.completion.promise;
|
|
242
|
+
}
|
|
243
|
+
/** Stage pipeline: dial → establish (role-flip). The remaining stages
|
|
244
|
+
* (handshake, serve) are event-driven from the h2 server's request handler. */
|
|
245
|
+
async drive() {
|
|
246
|
+
const dialed = await dial(this.target, this.deps, this.plaintext, this.slotSignal, this.connectionId);
|
|
247
|
+
if (this.completion.settled) {
|
|
248
|
+
if (dialed.ok) dialed.socket.destroy();
|
|
249
|
+
this.deps.activeConnections.delete(this);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (!dialed.ok) {
|
|
253
|
+
this.settle(dialed.outcome);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
this.socket = dialed.socket;
|
|
257
|
+
this.deps.activeSockets.add(dialed.socket);
|
|
258
|
+
this.slotSignal.addEventListener("abort", this.onAbort, { once: true });
|
|
259
|
+
dialed.socket.on("error", (err) => this.settle(this.endOutcome(`socket error: ${err.message}`), `socket error: ${err.message}`));
|
|
260
|
+
dialed.socket.on("close", () => this.settle(this.endOutcome("connection closed before handshake completed"), "socket closed"));
|
|
261
|
+
this.establish(dialed.socket);
|
|
262
|
+
}
|
|
263
|
+
get closed() {
|
|
264
|
+
return this.state.kind === "closed";
|
|
265
|
+
}
|
|
266
|
+
session() {
|
|
267
|
+
const s = this.state;
|
|
268
|
+
return s.kind === "handshaking" || s.kind === "serving" || s.kind === "draining" ? s.session : void 0;
|
|
269
|
+
}
|
|
270
|
+
get openedAt() {
|
|
271
|
+
const s = this.state;
|
|
272
|
+
return s.kind === "serving" || s.kind === "draining" ? s.openedAt : void 0;
|
|
273
|
+
}
|
|
274
|
+
uptimeMs() {
|
|
275
|
+
return this.openedAt === void 0 ? 0 : Date.now() - this.openedAt;
|
|
276
|
+
}
|
|
277
|
+
/** The end-of-connection outcome: "served" once established, else retryable. */
|
|
278
|
+
endOutcome(reason) {
|
|
279
|
+
return this.openedAt !== void 0 ? {
|
|
280
|
+
kind: "served",
|
|
281
|
+
uptimeMs: this.uptimeMs()
|
|
282
|
+
} : {
|
|
283
|
+
kind: "retryable",
|
|
284
|
+
reason
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
onAbort = () => this.settle({
|
|
288
|
+
kind: "retryable",
|
|
289
|
+
reason: "tunnel closed"
|
|
290
|
+
});
|
|
291
|
+
/** Stop the timers/monitors owned by the current phase. */
|
|
292
|
+
stopPhaseResources() {
|
|
293
|
+
const s = this.state;
|
|
294
|
+
if (s.kind === "handshaking") clearTimeout(s.firstRequestTimer);
|
|
295
|
+
else if (s.kind === "serving" || s.kind === "draining") s.watchdog.stop();
|
|
296
|
+
}
|
|
297
|
+
/** Detach from the engine registries and the slot-abort listener. */
|
|
298
|
+
deregister() {
|
|
299
|
+
this.slotSignal.removeEventListener("abort", this.onAbort);
|
|
300
|
+
this.deps.activeConnections.delete(this);
|
|
301
|
+
if (this.socket !== void 0) this.deps.activeSockets.delete(this.socket);
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The single terminal path — resolve the outcome (once), destroy the
|
|
305
|
+
* session/socket, and move to `closed`. Idempotent. A server drain does NOT
|
|
306
|
+
* funnel through here for teardown: it detaches via {@link beginServerDrain}
|
|
307
|
+
* and lets the DrainingRegistry destroy the session later.
|
|
308
|
+
*/
|
|
309
|
+
settle(outcome, detail) {
|
|
310
|
+
if (this.closed) return;
|
|
311
|
+
const phase = this.state.kind;
|
|
312
|
+
const session = this.session();
|
|
313
|
+
this.stopPhaseResources();
|
|
314
|
+
this.deregister();
|
|
315
|
+
this.state = { kind: "closed" };
|
|
316
|
+
session?.destroy();
|
|
317
|
+
this.socket?.destroy();
|
|
318
|
+
const firstOutcome = this.completion.resolve(outcome);
|
|
319
|
+
this.logWithIdentity(`tunnel: connection to ${targetLabel(this.target)} closed (phase=${phase}, outcome=${formatConnectionOutcome(outcome)}${detail === void 0 ? "" : `, detail=${detail}`}${firstOutcome ? "" : ", already reported"})`);
|
|
320
|
+
}
|
|
321
|
+
closeSessionGracefully(session) {
|
|
322
|
+
return new Promise((resolve) => {
|
|
323
|
+
if (session.closed || session.destroyed) {
|
|
324
|
+
resolve();
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
let done = false;
|
|
328
|
+
const finish = () => {
|
|
329
|
+
if (done) return;
|
|
330
|
+
done = true;
|
|
331
|
+
clearTimeout(timer);
|
|
332
|
+
resolve();
|
|
333
|
+
};
|
|
334
|
+
const timer = setTimeout(() => {
|
|
335
|
+
this.settle({
|
|
336
|
+
kind: "served",
|
|
337
|
+
uptimeMs: this.uptimeMs()
|
|
338
|
+
}, "session.close() timed out");
|
|
339
|
+
finish();
|
|
340
|
+
}, CLIENT_DRAIN_SESSION_CLOSE_TIMEOUT_MS);
|
|
341
|
+
timer.unref();
|
|
342
|
+
session.once("close", finish);
|
|
343
|
+
try {
|
|
344
|
+
session.close();
|
|
345
|
+
} catch {
|
|
346
|
+
this.settle(this.endOutcome("session close failed"), "session close failed");
|
|
347
|
+
finish();
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
sendClientDrainGoaway(session) {
|
|
352
|
+
if (session.closed || session.destroyed) return;
|
|
353
|
+
try {
|
|
354
|
+
session.goaway(node_http2.constants.NGHTTP2_NO_ERROR);
|
|
355
|
+
this.logWithIdentity(`tunnel: sent client-drain GOAWAY to ${targetLabel(this.target)}`);
|
|
356
|
+
} catch (err) {
|
|
357
|
+
this.logWithIdentity(`tunnel: failed to send client-drain GOAWAY to ${targetLabel(this.target)}: ${err instanceof Error ? err.message : String(err)}`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
establish(socket) {
|
|
361
|
+
this.logWithIdentity(`tunnel: connected to ${this.target.host}:${this.target.port}, starting handshake`);
|
|
362
|
+
const h2 = node_http2.createServer({
|
|
363
|
+
maxSessionMemory: this.deps.opts.maxSessionMemory,
|
|
364
|
+
settings: {
|
|
365
|
+
maxConcurrentStreams: this.deps.opts.maxConcurrentStreams,
|
|
366
|
+
initialWindowSize: 1024 * 1024,
|
|
367
|
+
maxFrameSize: 65536
|
|
368
|
+
}
|
|
369
|
+
}, (req, res) => this.handleRequest(req, res));
|
|
370
|
+
h2.on("session", (s) => {
|
|
371
|
+
this.logWithIdentity(`tunnel: h2 session established to ${targetLabel(this.target)} (localSettings=${formatSettings(s.localSettings)}, remoteSettings=${formatSettings(s.remoteSettings)})`);
|
|
372
|
+
s.on("localSettings", (settings) => this.logWithIdentity(`tunnel: h2 local settings acknowledged by ${targetLabel(this.target)}: ${formatSettings(settings)}`));
|
|
373
|
+
s.on("remoteSettings", (settings) => this.logWithIdentity(`tunnel: h2 remote settings from ${targetLabel(this.target)}: ${formatSettings(settings)}`));
|
|
374
|
+
if (this.state.kind === "connecting") {
|
|
375
|
+
const firstRequestTimer = setTimeout(() => {
|
|
376
|
+
if (this.state.kind === "handshaking" && this.state.handshake === void 0) this.settle({
|
|
377
|
+
kind: "retryable",
|
|
378
|
+
reason: "server never initiated /_/start-tunnel"
|
|
379
|
+
});
|
|
380
|
+
}, this.deps.opts.handshakeTimeoutMs);
|
|
381
|
+
firstRequestTimer.unref();
|
|
382
|
+
this.state = {
|
|
383
|
+
kind: "handshaking",
|
|
384
|
+
session: s,
|
|
385
|
+
firstRequestTimer,
|
|
386
|
+
handshake: void 0
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
try {
|
|
390
|
+
s.setLocalWindowSize?.(this.deps.opts.connectionWindowSize);
|
|
391
|
+
} catch {}
|
|
392
|
+
s.on("close", () => this.settle(this.endOutcome("session closed before handshake completed"), "session closed"));
|
|
393
|
+
s.on("error", (err) => this.settle(this.endOutcome(`session error: ${err.message}`), `session error: ${err.message}`));
|
|
394
|
+
});
|
|
395
|
+
h2.on("sessionError", (err) => this.settle(this.endOutcome(`session error: ${err.message}`), `session error: ${err.message}`));
|
|
396
|
+
h2.emit("connection", socket);
|
|
397
|
+
}
|
|
398
|
+
handleRequest(req, res) {
|
|
399
|
+
switch (classifyRequest(req).kind) {
|
|
400
|
+
case "health":
|
|
401
|
+
res.writeHead(200);
|
|
402
|
+
res.end();
|
|
403
|
+
return;
|
|
404
|
+
case "drain":
|
|
405
|
+
this.handleDrainRequest(res);
|
|
406
|
+
return;
|
|
407
|
+
case "start-tunnel":
|
|
408
|
+
if (this.state.kind === "handshaking" && this.state.handshake === void 0) this.startHandshake(req, res);
|
|
409
|
+
else this.notReady(res);
|
|
410
|
+
return;
|
|
411
|
+
case "forwarded":
|
|
412
|
+
this.handleForwarded(req, res);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
handleForwarded(req, res) {
|
|
417
|
+
if (this.state.kind === "serving" || this.state.kind === "draining") {
|
|
418
|
+
this.dispatchForwarded(req, res);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (this.state.kind === "handshaking" && this.state.handshake !== void 0) {
|
|
422
|
+
this.state.handshake.then(({ ok }) => {
|
|
423
|
+
if (this.closed || res.stream.destroyed) return;
|
|
424
|
+
try {
|
|
425
|
+
if (ok) this.dispatchForwarded(req, res);
|
|
426
|
+
else {
|
|
427
|
+
this.logWithIdentity(`tunnel: refused forwarded stream ${res.stream.id ?? "?"} before tunnel handshake completed`);
|
|
428
|
+
this.notReady(res);
|
|
429
|
+
}
|
|
430
|
+
} catch {}
|
|
431
|
+
});
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
this.logWithIdentity(`tunnel: refused forwarded stream ${res.stream.id ?? "?"} before tunnel handshake completed`);
|
|
435
|
+
this.notReady(res);
|
|
436
|
+
}
|
|
437
|
+
notReady(res) {
|
|
438
|
+
res.writeHead(503, { [TUNNEL_DRAINING_HEADER]: "true" });
|
|
439
|
+
res.end("tunnel: not ready");
|
|
440
|
+
}
|
|
441
|
+
/** First stream: run the handshake; its outcome opens the gate. */
|
|
442
|
+
startHandshake(req, res) {
|
|
443
|
+
if (this.state.kind !== "handshaking") return;
|
|
444
|
+
clearTimeout(this.state.firstRequestTimer);
|
|
445
|
+
this.state.handshake = require_handshake.performHandshake(req, res, {
|
|
446
|
+
authToken: this.authToken,
|
|
447
|
+
environmentId: this.deps.opts.environmentId,
|
|
448
|
+
tunnelName: this.deps.opts.name,
|
|
449
|
+
tunnelWorkerId: this.deps.opts.tunnelWorkerId,
|
|
450
|
+
tunnelConnectionId: this.connectionId,
|
|
451
|
+
supportsDrain: this.deps.opts.supportsDrain,
|
|
452
|
+
supportsClientDrain: this.deps.opts.supportsClientDrain
|
|
453
|
+
}, this.deps.opts.handshakeTimeoutMs).then((outcome) => {
|
|
454
|
+
if (this.state.kind !== "handshaking") return { ok: false };
|
|
455
|
+
if (outcome.kind === "ok") {
|
|
456
|
+
const { session } = this.state;
|
|
457
|
+
const openedAt = Date.now();
|
|
458
|
+
const watchdog = this.startWatchdog(session);
|
|
459
|
+
this.state = this.deps.isShuttingDown() ? {
|
|
460
|
+
kind: "draining",
|
|
461
|
+
session,
|
|
462
|
+
openedAt,
|
|
463
|
+
trigger: "client",
|
|
464
|
+
watchdog
|
|
465
|
+
} : {
|
|
466
|
+
kind: "serving",
|
|
467
|
+
session,
|
|
468
|
+
openedAt,
|
|
469
|
+
watchdog
|
|
470
|
+
};
|
|
471
|
+
this.logWithIdentity(`tunnel: established (name=${outcome.info.tunnelName}, proxy=${outcome.info.proxyUrl})`);
|
|
472
|
+
this.deps.onEstablished(outcome.info, this.identity);
|
|
473
|
+
return { ok: true };
|
|
474
|
+
}
|
|
475
|
+
this.settle(outcome);
|
|
476
|
+
return { ok: false };
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
/** Strip the destination prefix and hand the stream to the SDK. */
|
|
480
|
+
dispatchForwarded(req, res) {
|
|
481
|
+
if (this.state.kind === "draining" && this.state.trigger === "client" || this.deps.isShuttingDown()) {
|
|
482
|
+
this.logWithIdentity(`tunnel: refused forwarded stream ${res.stream.id ?? "?"} during client drain`);
|
|
483
|
+
res.writeHead(503, { [TUNNEL_DRAINING_HEADER]: "true" });
|
|
484
|
+
res.end();
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const tail = require_forwarded.forwardedTail(req.url ?? "");
|
|
488
|
+
if (tail === null) {
|
|
489
|
+
res.writeHead(400);
|
|
490
|
+
res.end("tunnel: malformed forwarded path");
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
req.url = tail;
|
|
494
|
+
this.deps.inflightStarted();
|
|
495
|
+
res.stream.once("close", () => this.deps.inflightEnded());
|
|
496
|
+
res.stream.once("error", (err) => {
|
|
497
|
+
const streamId = res.stream.id ?? "?";
|
|
498
|
+
const logPath = pathWithoutQuery(req.url);
|
|
499
|
+
this.logWithIdentity(`tunnel: forwarded stream ${streamId} ${req.method ?? "?"} ${logPath} failed: ${err.message}`);
|
|
500
|
+
});
|
|
501
|
+
try {
|
|
502
|
+
this.deps.sdkHandler(req, res);
|
|
503
|
+
} catch (err) {
|
|
504
|
+
const streamId = res.stream.id ?? "?";
|
|
505
|
+
const logPath = pathWithoutQuery(req.url);
|
|
506
|
+
this.logWithIdentity(`tunnel: SDK handler threw for forwarded stream ${streamId} ${req.method ?? "?"} ${logPath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
507
|
+
endInternalError(res);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
handleDrainRequest(res) {
|
|
511
|
+
res.writeHead(200);
|
|
512
|
+
res.end();
|
|
513
|
+
this.logWithIdentity(`tunnel: received server drain notification from ${targetLabel(this.target)}`);
|
|
514
|
+
if (!this.deps.opts.supportsDrain) {
|
|
515
|
+
this.logWithIdentity("tunnel: received /_/drain-tunnel (drain not advertised) — acknowledging");
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (this.state.kind === "serving") this.beginServerDrain();
|
|
519
|
+
else if (this.state.kind === "handshaking" && this.state.handshake !== void 0) this.state.handshake.then(({ ok }) => {
|
|
520
|
+
if (ok) this.beginServerDrain();
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Server-initiated drain: detach the still-serving session to the
|
|
525
|
+
* DrainingRegistry and resolve `run()` so the slot dials a replacement. The
|
|
526
|
+
* detached session keeps serving its in-flight invocations under the
|
|
527
|
+
* registry's grace window — `run()` resolves now, the session closes later
|
|
528
|
+
* (its `close` handler then runs `settle()` → `closed`, a no-op resolve).
|
|
529
|
+
*/
|
|
530
|
+
beginServerDrain() {
|
|
531
|
+
if (this.state.kind !== "serving") return;
|
|
532
|
+
const { session, openedAt, watchdog } = this.state;
|
|
533
|
+
this.logWithIdentity("tunnel: server drain notification accepted — opening a replacement connection");
|
|
534
|
+
watchdog.stop();
|
|
535
|
+
this.deregister();
|
|
536
|
+
this.deps.draining.add(session, this.socket, this.deps.opts.drainGraceMs);
|
|
537
|
+
this.state = {
|
|
538
|
+
kind: "draining",
|
|
539
|
+
session,
|
|
540
|
+
openedAt,
|
|
541
|
+
trigger: "server",
|
|
542
|
+
watchdog
|
|
543
|
+
};
|
|
544
|
+
this.completion.resolve({
|
|
545
|
+
kind: "drained",
|
|
546
|
+
uptimeMs: this.uptimeMs()
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Client-initiated drain: the engine is shutting this process down. Refuse
|
|
551
|
+
* new invocations and finish in-flight IN PLACE — no redial. The engine
|
|
552
|
+
* waits for in-flight to drain, then tears the connection down.
|
|
553
|
+
*/
|
|
554
|
+
beginClientDrain() {
|
|
555
|
+
const s = this.state;
|
|
556
|
+
switch (s.kind) {
|
|
557
|
+
case "serving":
|
|
558
|
+
this.state = {
|
|
559
|
+
kind: "draining",
|
|
560
|
+
session: s.session,
|
|
561
|
+
openedAt: s.openedAt,
|
|
562
|
+
trigger: "client",
|
|
563
|
+
watchdog: s.watchdog
|
|
564
|
+
};
|
|
565
|
+
this.sendClientDrainGoaway(s.session);
|
|
566
|
+
return;
|
|
567
|
+
case "connecting":
|
|
568
|
+
case "handshaking":
|
|
569
|
+
this.settle({
|
|
570
|
+
kind: "retryable",
|
|
571
|
+
reason: "shutting down"
|
|
572
|
+
});
|
|
573
|
+
return;
|
|
574
|
+
case "draining":
|
|
575
|
+
case "closed": return;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
async finishClientDrain(opts) {
|
|
579
|
+
const s = this.state;
|
|
580
|
+
if (s.kind !== "draining" || s.trigger !== "client") return;
|
|
581
|
+
if (opts.force) {
|
|
582
|
+
this.settle({
|
|
583
|
+
kind: "served",
|
|
584
|
+
uptimeMs: this.uptimeMs()
|
|
585
|
+
}, "client drain grace expired");
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
await this.closeSessionGracefully(s.session);
|
|
589
|
+
}
|
|
590
|
+
startWatchdog(session) {
|
|
591
|
+
const watchdog = new Watchdog(session, this.deps.opts, () => {
|
|
592
|
+
this.logWithIdentity("tunnel: pings missed — reconnecting");
|
|
593
|
+
this.settle({
|
|
594
|
+
kind: "served",
|
|
595
|
+
uptimeMs: this.uptimeMs()
|
|
596
|
+
}, "ping watchdog missed too many acknowledgements");
|
|
597
|
+
});
|
|
598
|
+
watchdog.start();
|
|
599
|
+
return watchdog;
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
//#endregion
|
|
604
|
+
exports.runConnection = runConnection;
|