@sinch/functions-runtime 0.5.10 → 0.5.13
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/bin/sinch-runtime.js +6 -4
- package/dist/bin/sinch-runtime.js.map +1 -1
- package/dist/{chunk-LU5JRQ64.js → chunk-IRSTA74R.js} +293 -62
- package/dist/chunk-IRSTA74R.js.map +1 -0
- package/dist/chunk-X44DQ6CI.js +52 -0
- package/dist/chunk-X44DQ6CI.js.map +1 -0
- package/dist/index.d.ts +67 -19
- package/dist/index.js +2 -1
- package/dist/voice/v2.d.ts +1597 -0
- package/dist/voice/v2.js +1259 -0
- package/dist/voice/v2.js.map +1 -0
- package/package.json +7 -2
- package/dist/chunk-LU5JRQ64.js.map +0 -1
|
@@ -1,45 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createRequire as __sinchCreateRequire } from 'node:module';
|
|
3
3
|
const require = __sinchCreateRequire(import.meta.url);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}) : x)(function(x) {
|
|
13
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
14
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
15
|
-
});
|
|
16
|
-
var __esm = (fn, res) => function __init() {
|
|
17
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
18
|
-
};
|
|
19
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
20
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
21
|
-
};
|
|
22
|
-
var __export = (target, all3) => {
|
|
23
|
-
for (var name in all3)
|
|
24
|
-
__defProp(target, name, { get: all3[name], enumerable: true });
|
|
25
|
-
};
|
|
26
|
-
var __copyProps = (to, from, except, desc) => {
|
|
27
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
-
for (let key of __getOwnPropNames(from))
|
|
29
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
-
}
|
|
32
|
-
return to;
|
|
33
|
-
};
|
|
34
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
35
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
36
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
37
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
38
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
39
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
40
|
-
mod
|
|
41
|
-
));
|
|
42
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
4
|
+
import {
|
|
5
|
+
__commonJS,
|
|
6
|
+
__esm,
|
|
7
|
+
__export,
|
|
8
|
+
__require,
|
|
9
|
+
__toCommonJS,
|
|
10
|
+
__toESM
|
|
11
|
+
} from "./chunk-X44DQ6CI.js";
|
|
43
12
|
|
|
44
13
|
// ../../node_modules/ms/index.js
|
|
45
14
|
var require_ms = __commonJS({
|
|
@@ -55001,8 +54970,8 @@ var require_buffer_util = __commonJS({
|
|
|
55001
54970
|
}
|
|
55002
54971
|
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
|
|
55003
54972
|
}
|
|
55004
|
-
function
|
|
55005
|
-
|
|
54973
|
+
function toBuffer2(data) {
|
|
54974
|
+
toBuffer2.readOnly = true;
|
|
55006
54975
|
if (Buffer.isBuffer(data)) return data;
|
|
55007
54976
|
let buf;
|
|
55008
54977
|
if (data instanceof ArrayBuffer) {
|
|
@@ -55011,7 +54980,7 @@ var require_buffer_util = __commonJS({
|
|
|
55011
54980
|
buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
|
|
55012
54981
|
} else {
|
|
55013
54982
|
buf = Buffer.from(data);
|
|
55014
|
-
|
|
54983
|
+
toBuffer2.readOnly = false;
|
|
55015
54984
|
}
|
|
55016
54985
|
return buf;
|
|
55017
54986
|
}
|
|
@@ -55019,7 +54988,7 @@ var require_buffer_util = __commonJS({
|
|
|
55019
54988
|
concat,
|
|
55020
54989
|
mask: _mask,
|
|
55021
54990
|
toArrayBuffer,
|
|
55022
|
-
toBuffer,
|
|
54991
|
+
toBuffer: toBuffer2,
|
|
55023
54992
|
unmask: _unmask
|
|
55024
54993
|
};
|
|
55025
54994
|
if (!process.env.WS_NO_BUFFER_UTIL) {
|
|
@@ -56274,7 +56243,7 @@ var require_sender = __commonJS({
|
|
|
56274
56243
|
var PerMessageDeflate = require_permessage_deflate();
|
|
56275
56244
|
var { EMPTY_BUFFER, kWebSocket, NOOP } = require_constants();
|
|
56276
56245
|
var { isBlob: isBlob3, isValidStatusCode } = require_validation();
|
|
56277
|
-
var { mask: applyMask, toBuffer } = require_buffer_util();
|
|
56246
|
+
var { mask: applyMask, toBuffer: toBuffer2 } = require_buffer_util();
|
|
56278
56247
|
var kByteLength = /* @__PURE__ */ Symbol("kByteLength");
|
|
56279
56248
|
var maskBuffer = Buffer.alloc(4);
|
|
56280
56249
|
var RANDOM_POOL_SIZE = 8 * 1024;
|
|
@@ -56462,9 +56431,9 @@ var require_sender = __commonJS({
|
|
|
56462
56431
|
byteLength = data.size;
|
|
56463
56432
|
readOnly = false;
|
|
56464
56433
|
} else {
|
|
56465
|
-
data =
|
|
56434
|
+
data = toBuffer2(data);
|
|
56466
56435
|
byteLength = data.length;
|
|
56467
|
-
readOnly =
|
|
56436
|
+
readOnly = toBuffer2.readOnly;
|
|
56468
56437
|
}
|
|
56469
56438
|
if (byteLength > 125) {
|
|
56470
56439
|
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
@@ -56509,9 +56478,9 @@ var require_sender = __commonJS({
|
|
|
56509
56478
|
byteLength = data.size;
|
|
56510
56479
|
readOnly = false;
|
|
56511
56480
|
} else {
|
|
56512
|
-
data =
|
|
56481
|
+
data = toBuffer2(data);
|
|
56513
56482
|
byteLength = data.length;
|
|
56514
|
-
readOnly =
|
|
56483
|
+
readOnly = toBuffer2.readOnly;
|
|
56515
56484
|
}
|
|
56516
56485
|
if (byteLength > 125) {
|
|
56517
56486
|
throw new RangeError("The data size must not be greater than 125 bytes");
|
|
@@ -56567,9 +56536,9 @@ var require_sender = __commonJS({
|
|
|
56567
56536
|
byteLength = data.size;
|
|
56568
56537
|
readOnly = false;
|
|
56569
56538
|
} else {
|
|
56570
|
-
data =
|
|
56539
|
+
data = toBuffer2(data);
|
|
56571
56540
|
byteLength = data.length;
|
|
56572
|
-
readOnly =
|
|
56541
|
+
readOnly = toBuffer2.readOnly;
|
|
56573
56542
|
}
|
|
56574
56543
|
if (this._firstFragment) {
|
|
56575
56544
|
this._firstFragment = false;
|
|
@@ -56639,7 +56608,7 @@ var require_sender = __commonJS({
|
|
|
56639
56608
|
return;
|
|
56640
56609
|
}
|
|
56641
56610
|
this._bufferedBytes -= options[kByteLength];
|
|
56642
|
-
const data =
|
|
56611
|
+
const data = toBuffer2(arrayBuffer);
|
|
56643
56612
|
if (!compress) {
|
|
56644
56613
|
this._state = DEFAULT;
|
|
56645
56614
|
this.sendFrame(_Sender.frame(data, options), cb);
|
|
@@ -57165,7 +57134,7 @@ var require_websocket = __commonJS({
|
|
|
57165
57134
|
EventTarget: { addEventListener, removeEventListener }
|
|
57166
57135
|
} = require_event_target();
|
|
57167
57136
|
var { format, parse: parse2 } = require_extension();
|
|
57168
|
-
var { toBuffer } = require_buffer_util();
|
|
57137
|
+
var { toBuffer: toBuffer2 } = require_buffer_util();
|
|
57169
57138
|
var closeTimeout = 30 * 1e3;
|
|
57170
57139
|
var kAborted = /* @__PURE__ */ Symbol("kAborted");
|
|
57171
57140
|
var protocolVersions = [8, 13];
|
|
@@ -57901,7 +57870,7 @@ var require_websocket = __commonJS({
|
|
|
57901
57870
|
}
|
|
57902
57871
|
function sendAfterClose(websocket, data, cb) {
|
|
57903
57872
|
if (data) {
|
|
57904
|
-
const length = isBlob3(data) ? data.size :
|
|
57873
|
+
const length = isBlob3(data) ? data.size : toBuffer2(data).length;
|
|
57905
57874
|
if (websocket._socket) websocket._sender._bufferedBytes += length;
|
|
57906
57875
|
else websocket._bufferedAmount += length;
|
|
57907
57876
|
}
|
|
@@ -59088,19 +59057,46 @@ var AgentProvider;
|
|
|
59088
59057
|
(function(AgentProvider2) {
|
|
59089
59058
|
AgentProvider2["Sinch"] = "sinch";
|
|
59090
59059
|
AgentProvider2["ElevenLabs"] = "elevenlabs";
|
|
59060
|
+
AgentProvider2["Grok"] = "grok";
|
|
59091
59061
|
})(AgentProvider || (AgentProvider = {}));
|
|
59092
59062
|
var AgentSipHelper = class {
|
|
59093
59063
|
/**
|
|
59094
59064
|
* Build a SIP URI for connecting to an ElevenLabs agent
|
|
59095
59065
|
*/
|
|
59096
59066
|
static buildElevenLabsSipUri(agentId, options) {
|
|
59097
|
-
const baseUri =
|
|
59067
|
+
const baseUri = `${agentId}@sip.elevenlabs.io`;
|
|
59098
59068
|
if (options?.customHeaders && Object.keys(options.customHeaders).length > 0) {
|
|
59099
59069
|
const headers = Object.entries(options.customHeaders).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
|
|
59100
59070
|
return `${baseUri};${headers}`;
|
|
59101
59071
|
}
|
|
59102
59072
|
return baseUri;
|
|
59103
59073
|
}
|
|
59074
|
+
/**
|
|
59075
|
+
* Build a SIP URI for connecting to an xAI Grok Voice agent.
|
|
59076
|
+
*
|
|
59077
|
+
* Note the user part is the **Direct SIP phone number** registered in the xAI console, not an
|
|
59078
|
+
* agent id — xAI routes the INVITE to whichever agent is attached to that number. This is the
|
|
59079
|
+
* one place Grok differs sharply from ElevenLabs, which puts the agent id there.
|
|
59080
|
+
*
|
|
59081
|
+
* @see https://docs.x.ai/developers/model-capabilities/audio/voice-agent/sip
|
|
59082
|
+
*/
|
|
59083
|
+
static buildGrokSipUri(phoneNumber, options) {
|
|
59084
|
+
const baseUri = `${phoneNumber}@sip.voice.x.ai:5061;transport=tls`;
|
|
59085
|
+
if (options?.customHeaders && Object.keys(options.customHeaders).length > 0) {
|
|
59086
|
+
const headers = Object.entries(options.customHeaders).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
|
|
59087
|
+
return `${baseUri};${headers}`;
|
|
59088
|
+
}
|
|
59089
|
+
return baseUri;
|
|
59090
|
+
}
|
|
59091
|
+
/**
|
|
59092
|
+
* The SIP transport a provider's endpoint requires.
|
|
59093
|
+
*
|
|
59094
|
+
* Mirrors the C# runtime's `GetTransport`. xAI's endpoint is TLS-only — sending TCP to it is
|
|
59095
|
+
* refused outright.
|
|
59096
|
+
*/
|
|
59097
|
+
static getTransport(provider) {
|
|
59098
|
+
return provider === AgentProvider.Grok ? "TLS" : "TCP";
|
|
59099
|
+
}
|
|
59104
59100
|
/**
|
|
59105
59101
|
* Build a SIP URI for the given provider and agent
|
|
59106
59102
|
*/
|
|
@@ -59108,6 +59104,8 @@ var AgentSipHelper = class {
|
|
|
59108
59104
|
switch (provider) {
|
|
59109
59105
|
case AgentProvider.ElevenLabs:
|
|
59110
59106
|
return this.buildElevenLabsSipUri(agentId, options);
|
|
59107
|
+
case AgentProvider.Grok:
|
|
59108
|
+
return this.buildGrokSipUri(agentId, options);
|
|
59111
59109
|
default:
|
|
59112
59110
|
throw new Error(`Unsupported agent provider: ${provider}`);
|
|
59113
59111
|
}
|
|
@@ -59118,14 +59116,26 @@ function createConnectAgentAction(provider, agentId, options) {
|
|
|
59118
59116
|
throw new Error("AgentProvider.Sinch uses connectStream, not connectSip. Use connectAgent() from @sinch/agents instead.");
|
|
59119
59117
|
}
|
|
59120
59118
|
const sipUri = AgentSipHelper.buildSipUri(provider, agentId, options);
|
|
59119
|
+
const transport = options?.transport ?? AgentSipHelper.getTransport(provider);
|
|
59120
|
+
const callHeaders = toCallHeaderArray(options?.callHeaders);
|
|
59121
59121
|
return {
|
|
59122
59122
|
name: "connectSip",
|
|
59123
|
-
|
|
59123
|
+
// SVAML's destinationSip requires `type` as well as `endpoint`; omitting it produced an
|
|
59124
|
+
// action the platform rejects. The C# runtime has always sent it.
|
|
59125
|
+
destination: { type: "sip", endpoint: sipUri },
|
|
59126
|
+
transport,
|
|
59124
59127
|
...options?.cli && { cli: options.cli },
|
|
59125
59128
|
...options?.maxDuration !== void 0 && { maxDuration: options.maxDuration },
|
|
59126
|
-
...options?.suppressCallbacks !== void 0 && { suppressCallbacks: options.suppressCallbacks }
|
|
59129
|
+
...options?.suppressCallbacks !== void 0 && { suppressCallbacks: options.suppressCallbacks },
|
|
59130
|
+
...callHeaders && { callHeaders }
|
|
59127
59131
|
};
|
|
59128
59132
|
}
|
|
59133
|
+
function toCallHeaderArray(headers) {
|
|
59134
|
+
if (!headers)
|
|
59135
|
+
return void 0;
|
|
59136
|
+
const entries = Object.entries(headers);
|
|
59137
|
+
return entries.length ? entries.map(([key, value]) => ({ key, value })) : void 0;
|
|
59138
|
+
}
|
|
59129
59139
|
|
|
59130
59140
|
// ../runtime-shared/dist/builders/svaml.js
|
|
59131
59141
|
var BaseSvamlBuilder = class {
|
|
@@ -59281,7 +59291,11 @@ var IceSvamlBuilder = class extends BaseSvamlBuilder {
|
|
|
59281
59291
|
connectSip(destination, options) {
|
|
59282
59292
|
this.action = {
|
|
59283
59293
|
name: "connectSip",
|
|
59284
|
-
destination,
|
|
59294
|
+
// SVAML's `destination` is a discriminated object, not a string — see
|
|
59295
|
+
// `svaml.action.connectSip` -> `destinationSip` in the Voice API reference, which requires
|
|
59296
|
+
// both `type` and `endpoint`. Emitting a bare string produced SVAML the platform rejects,
|
|
59297
|
+
// which is why callers were hand-building this action instead of using the builder.
|
|
59298
|
+
destination: { type: "sip", endpoint: destination },
|
|
59285
59299
|
...options
|
|
59286
59300
|
};
|
|
59287
59301
|
return this;
|
|
@@ -59295,7 +59309,9 @@ var IceSvamlBuilder = class extends BaseSvamlBuilder {
|
|
|
59295
59309
|
connectMxp(destination, options) {
|
|
59296
59310
|
this.action = {
|
|
59297
59311
|
name: "connectMxp",
|
|
59298
|
-
|
|
59312
|
+
// Same discriminated-object shape as connectSip — `destinationMxp` requires `type` and
|
|
59313
|
+
// `endpoint`, so a bare string is rejected.
|
|
59314
|
+
destination: { type: "username", endpoint: destination },
|
|
59299
59315
|
...options
|
|
59300
59316
|
};
|
|
59301
59317
|
return this;
|
|
@@ -68746,6 +68762,197 @@ async function configureElevenLabs() {
|
|
|
68746
68762
|
}
|
|
68747
68763
|
}
|
|
68748
68764
|
|
|
68765
|
+
// src/tunnel/ws-stream.ts
|
|
68766
|
+
var UPGRADE_TIMEOUT_MS = 1e4;
|
|
68767
|
+
var DEFAULT_CLOSE_CODE = 1e3;
|
|
68768
|
+
var MAX_CLOSE_REASON_BYTES = 120;
|
|
68769
|
+
var TunnelStreamManager = class {
|
|
68770
|
+
streams = /* @__PURE__ */ new Map();
|
|
68771
|
+
localPort;
|
|
68772
|
+
send;
|
|
68773
|
+
createSocket;
|
|
68774
|
+
log;
|
|
68775
|
+
constructor(options) {
|
|
68776
|
+
this.localPort = options.localPort;
|
|
68777
|
+
this.send = options.send;
|
|
68778
|
+
this.createSocket = options.createSocket ?? ((url2, protocols, opts) => new wrapper_default(url2, protocols, opts));
|
|
68779
|
+
this.log = options.log ?? ((message) => console.log(message));
|
|
68780
|
+
}
|
|
68781
|
+
/** Number of live proxied sockets. */
|
|
68782
|
+
get activeCount() {
|
|
68783
|
+
return this.streams.size;
|
|
68784
|
+
}
|
|
68785
|
+
/**
|
|
68786
|
+
* Handles ws_open: dials the local dev server and answers the gateway once the outcome is
|
|
68787
|
+
* known. Every exit path out of 'connecting' sends exactly one ws_opened or ws_failed.
|
|
68788
|
+
*/
|
|
68789
|
+
open(message) {
|
|
68790
|
+
const id = message.id;
|
|
68791
|
+
if (!id) {
|
|
68792
|
+
this.log("[WS] Ignoring ws_open with no stream id");
|
|
68793
|
+
return;
|
|
68794
|
+
}
|
|
68795
|
+
if (this.streams.has(id)) {
|
|
68796
|
+
this.log(`[WS] Ignoring duplicate ws_open for stream ${id}`);
|
|
68797
|
+
return;
|
|
68798
|
+
}
|
|
68799
|
+
const path6 = message.path ?? "/";
|
|
68800
|
+
const url2 = `ws://localhost:${this.localPort}${path6}${message.query ?? ""}`;
|
|
68801
|
+
const protocols = message.protocol ? message.protocol.split(",").map((p) => p.trim()).filter(Boolean) : [];
|
|
68802
|
+
this.log(`[WS] Opening tunnelled WebSocket ${id} to ${url2}`);
|
|
68803
|
+
let socket;
|
|
68804
|
+
try {
|
|
68805
|
+
socket = this.createSocket(url2, protocols, { headers: { ...message.headers ?? {} } });
|
|
68806
|
+
} catch (error) {
|
|
68807
|
+
this.send({ type: "ws_failed", id, statusCode: 502, reason: describe(error) });
|
|
68808
|
+
return;
|
|
68809
|
+
}
|
|
68810
|
+
const stream4 = { socket, state: "connecting", timer: null };
|
|
68811
|
+
stream4.timer = setTimeout(() => {
|
|
68812
|
+
this.fail(id, 504, "Timed out opening the local WebSocket");
|
|
68813
|
+
}, UPGRADE_TIMEOUT_MS);
|
|
68814
|
+
stream4.timer.unref?.();
|
|
68815
|
+
this.streams.set(id, stream4);
|
|
68816
|
+
socket.on("open", () => {
|
|
68817
|
+
const live = this.streams.get(id);
|
|
68818
|
+
if (!live || live.state !== "connecting") return;
|
|
68819
|
+
this.settle(live, "open");
|
|
68820
|
+
this.send({ type: "ws_opened", id, protocol: socket.protocol || void 0 });
|
|
68821
|
+
});
|
|
68822
|
+
socket.on("unexpected-response", (_request, response) => {
|
|
68823
|
+
const statusCode = response.statusCode ?? 502;
|
|
68824
|
+
this.fail(id, statusCode, response.statusMessage || `Local server returned ${statusCode}`);
|
|
68825
|
+
});
|
|
68826
|
+
socket.on("error", (error) => {
|
|
68827
|
+
const live = this.streams.get(id);
|
|
68828
|
+
if (!live) return;
|
|
68829
|
+
if (live.state === "connecting") {
|
|
68830
|
+
this.fail(id, 502, describe(error));
|
|
68831
|
+
return;
|
|
68832
|
+
}
|
|
68833
|
+
this.log(`[WS] Tunnelled WebSocket ${id} errored: ${error.message}`);
|
|
68834
|
+
this.finish(id, 1011, error.message);
|
|
68835
|
+
});
|
|
68836
|
+
socket.on("message", (data, isBinary) => {
|
|
68837
|
+
this.send({
|
|
68838
|
+
type: "ws_data",
|
|
68839
|
+
id,
|
|
68840
|
+
binary: isBinary,
|
|
68841
|
+
data: toBuffer(data).toString("base64")
|
|
68842
|
+
});
|
|
68843
|
+
});
|
|
68844
|
+
socket.on("close", (code, reason) => {
|
|
68845
|
+
const live = this.streams.get(id);
|
|
68846
|
+
if (!live) return;
|
|
68847
|
+
if (live.state === "connecting") {
|
|
68848
|
+
this.fail(id, 502, "The local server closed before completing the upgrade");
|
|
68849
|
+
return;
|
|
68850
|
+
}
|
|
68851
|
+
this.finish(id, code, reason.toString());
|
|
68852
|
+
});
|
|
68853
|
+
}
|
|
68854
|
+
/** Handles ws_data: one frame from the caller, replayed onto the local socket. */
|
|
68855
|
+
data(message) {
|
|
68856
|
+
const id = message.id;
|
|
68857
|
+
const stream4 = id ? this.streams.get(id) : void 0;
|
|
68858
|
+
if (!stream4) {
|
|
68859
|
+
this.log(`[WS] Dropping ws_data for unknown stream ${id ?? "(no id)"}`);
|
|
68860
|
+
return;
|
|
68861
|
+
}
|
|
68862
|
+
if (stream4.state !== "open" || stream4.socket.readyState !== wrapper_default.OPEN) {
|
|
68863
|
+
this.log(`[WS] Dropping ws_data for stream ${id} \u2014 the local socket is not open`);
|
|
68864
|
+
return;
|
|
68865
|
+
}
|
|
68866
|
+
stream4.socket.send(Buffer.from(message.data ?? "", "base64"), {
|
|
68867
|
+
binary: message.binary === true
|
|
68868
|
+
});
|
|
68869
|
+
}
|
|
68870
|
+
/** Handles ws_close: the caller's socket went away, so drop the local one. */
|
|
68871
|
+
close(message) {
|
|
68872
|
+
const id = message.id;
|
|
68873
|
+
const stream4 = id ? this.streams.get(id) : void 0;
|
|
68874
|
+
if (!id || !stream4) {
|
|
68875
|
+
this.log(`[WS] Dropping ws_close for unknown stream ${id ?? "(no id)"}`);
|
|
68876
|
+
return;
|
|
68877
|
+
}
|
|
68878
|
+
this.log(`[WS] Caller closed tunnelled WebSocket ${id}`);
|
|
68879
|
+
this.streams.delete(id);
|
|
68880
|
+
this.shutdown(stream4, message.code, message.reason);
|
|
68881
|
+
}
|
|
68882
|
+
/**
|
|
68883
|
+
* Drops every proxied socket. Called when the tunnel itself disconnects — the gateway is gone,
|
|
68884
|
+
* so there is nobody left to send ws_close to, and these sockets would otherwise leak.
|
|
68885
|
+
*/
|
|
68886
|
+
closeAll(reason = "Tunnel disconnected") {
|
|
68887
|
+
for (const [id, stream4] of this.streams) {
|
|
68888
|
+
this.log(`[WS] Closing tunnelled WebSocket ${id}: ${reason}`);
|
|
68889
|
+
this.shutdown(stream4, 1001, reason);
|
|
68890
|
+
}
|
|
68891
|
+
this.streams.clear();
|
|
68892
|
+
}
|
|
68893
|
+
/** Answers a pending upgrade with a failure and abandons the half-open socket. */
|
|
68894
|
+
fail(id, statusCode, reason) {
|
|
68895
|
+
const stream4 = this.streams.get(id);
|
|
68896
|
+
if (!stream4 || stream4.state !== "connecting") return;
|
|
68897
|
+
this.log(`[WS] Tunnelled WebSocket ${id} failed to open: ${statusCode} ${reason}`);
|
|
68898
|
+
this.streams.delete(id);
|
|
68899
|
+
this.shutdown(stream4, DEFAULT_CLOSE_CODE, reason);
|
|
68900
|
+
this.send({ type: "ws_failed", id, statusCode, reason });
|
|
68901
|
+
}
|
|
68902
|
+
/** Reports an established stream ending on the local side. */
|
|
68903
|
+
finish(id, code, reason) {
|
|
68904
|
+
const stream4 = this.streams.get(id);
|
|
68905
|
+
if (!stream4) return;
|
|
68906
|
+
this.streams.delete(id);
|
|
68907
|
+
this.shutdown(stream4, code, reason);
|
|
68908
|
+
this.send({ type: "ws_close", id, code, reason });
|
|
68909
|
+
}
|
|
68910
|
+
settle(stream4, state) {
|
|
68911
|
+
if (stream4.timer) {
|
|
68912
|
+
clearTimeout(stream4.timer);
|
|
68913
|
+
stream4.timer = null;
|
|
68914
|
+
}
|
|
68915
|
+
stream4.state = state;
|
|
68916
|
+
}
|
|
68917
|
+
/** Detaches from a socket and tears it down without emitting anything further. */
|
|
68918
|
+
shutdown(stream4, code, reason) {
|
|
68919
|
+
this.settle(stream4, "done");
|
|
68920
|
+
const socket = stream4.socket;
|
|
68921
|
+
socket.removeAllListeners();
|
|
68922
|
+
socket.on("error", () => {
|
|
68923
|
+
});
|
|
68924
|
+
if (socket.readyState === wrapper_default.OPEN) {
|
|
68925
|
+
socket.close(normalizeCloseCode(code), truncateReason(reason));
|
|
68926
|
+
} else if (socket.readyState !== wrapper_default.CLOSED) {
|
|
68927
|
+
socket.terminate();
|
|
68928
|
+
}
|
|
68929
|
+
}
|
|
68930
|
+
};
|
|
68931
|
+
function toBuffer(data) {
|
|
68932
|
+
if (Buffer.isBuffer(data)) return data;
|
|
68933
|
+
if (Array.isArray(data)) return Buffer.concat(data);
|
|
68934
|
+
return Buffer.from(data);
|
|
68935
|
+
}
|
|
68936
|
+
function normalizeCloseCode(code) {
|
|
68937
|
+
if (typeof code !== "number") return DEFAULT_CLOSE_CODE;
|
|
68938
|
+
const valid = code >= 1e3 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006 || code >= 3e3 && code <= 4999;
|
|
68939
|
+
return valid ? code : DEFAULT_CLOSE_CODE;
|
|
68940
|
+
}
|
|
68941
|
+
function truncateReason(reason) {
|
|
68942
|
+
if (!reason) return void 0;
|
|
68943
|
+
const bytes = Buffer.from(reason, "utf8");
|
|
68944
|
+
return bytes.length <= MAX_CLOSE_REASON_BYTES ? reason : bytes.subarray(0, MAX_CLOSE_REASON_BYTES).toString("utf8");
|
|
68945
|
+
}
|
|
68946
|
+
function describe(error) {
|
|
68947
|
+
if (error instanceof AggregateError && error.errors.length > 0) {
|
|
68948
|
+
return error.errors.map(describe).join("; ");
|
|
68949
|
+
}
|
|
68950
|
+
if (error instanceof Error) {
|
|
68951
|
+
return error.message || error.code || error.name;
|
|
68952
|
+
}
|
|
68953
|
+
return String(error);
|
|
68954
|
+
}
|
|
68955
|
+
|
|
68749
68956
|
// src/tunnel/index.ts
|
|
68750
68957
|
var TUNNEL_GATEWAY_DEFAULT = "https://tunnel.fn.sinch.com";
|
|
68751
68958
|
var TunnelClient = class {
|
|
@@ -68760,8 +68967,19 @@ var TunnelClient = class {
|
|
|
68760
68967
|
localPort;
|
|
68761
68968
|
webhookConfig = {};
|
|
68762
68969
|
welcomeResolver = null;
|
|
68970
|
+
streams;
|
|
68763
68971
|
constructor(localPort = 3e3) {
|
|
68764
68972
|
this.localPort = localPort;
|
|
68973
|
+
this.streams = new TunnelStreamManager({
|
|
68974
|
+
localPort,
|
|
68975
|
+
send: (message) => this.sendMessage(message)
|
|
68976
|
+
});
|
|
68977
|
+
}
|
|
68978
|
+
/** Sends one JSON message up the tunnel channel, if it is still connected. */
|
|
68979
|
+
sendMessage(message) {
|
|
68980
|
+
if (this.ws && this.ws.readyState === wrapper_default.OPEN) {
|
|
68981
|
+
this.ws.send(JSON.stringify(message));
|
|
68982
|
+
}
|
|
68765
68983
|
}
|
|
68766
68984
|
getTunnelGatewayUrl() {
|
|
68767
68985
|
const explicitUrl = process.env.TUNNEL_GATEWAY_URL;
|
|
@@ -68829,6 +69047,7 @@ var TunnelClient = class {
|
|
|
68829
69047
|
this.isConnected = false;
|
|
68830
69048
|
console.log("Tunnel connection closed");
|
|
68831
69049
|
this.stopHeartbeat();
|
|
69050
|
+
this.streams.closeAll();
|
|
68832
69051
|
this.scheduleReconnect();
|
|
68833
69052
|
});
|
|
68834
69053
|
this.ws.on("error", (error) => {
|
|
@@ -68857,6 +69076,20 @@ var TunnelClient = class {
|
|
|
68857
69076
|
case "ping":
|
|
68858
69077
|
this.sendPong();
|
|
68859
69078
|
break;
|
|
69079
|
+
case "ws_open":
|
|
69080
|
+
this.streams.open(message);
|
|
69081
|
+
break;
|
|
69082
|
+
case "ws_data":
|
|
69083
|
+
this.streams.data(message);
|
|
69084
|
+
break;
|
|
69085
|
+
case "ws_close":
|
|
69086
|
+
this.streams.close(message);
|
|
69087
|
+
break;
|
|
69088
|
+
default:
|
|
69089
|
+
console.warn(
|
|
69090
|
+
`Ignoring unknown tunnel message type '${message.type}'`
|
|
69091
|
+
);
|
|
69092
|
+
break;
|
|
68860
69093
|
}
|
|
68861
69094
|
}
|
|
68862
69095
|
/**
|
|
@@ -69048,6 +69281,7 @@ var TunnelClient = class {
|
|
|
69048
69281
|
}
|
|
69049
69282
|
async disconnect() {
|
|
69050
69283
|
this.stopHeartbeat();
|
|
69284
|
+
this.streams.closeAll("Tunnel client shutting down");
|
|
69051
69285
|
await this.cleanupWebhooks();
|
|
69052
69286
|
if (this.ws) {
|
|
69053
69287
|
this.ws.close();
|
|
@@ -69592,9 +69826,6 @@ var SecretsLoader = class {
|
|
|
69592
69826
|
var secretsLoader = new SecretsLoader();
|
|
69593
69827
|
|
|
69594
69828
|
export {
|
|
69595
|
-
__require,
|
|
69596
|
-
__commonJS,
|
|
69597
|
-
__toESM,
|
|
69598
69829
|
AgentProvider,
|
|
69599
69830
|
AgentSipHelper,
|
|
69600
69831
|
createConnectAgentAction,
|
|
@@ -69980,4 +70211,4 @@ serve-static/index.js:
|
|
|
69980
70211
|
* MIT Licensed
|
|
69981
70212
|
*)
|
|
69982
70213
|
*/
|
|
69983
|
-
//# sourceMappingURL=chunk-
|
|
70214
|
+
//# sourceMappingURL=chunk-IRSTA74R.js.map
|