@theokit/gateway 0.10.0 → 0.12.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/CHANGELOG.md +200 -0
- package/README.md +178 -1
- package/dist/index.cjs +369 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +434 -78
- package/dist/index.d.ts +434 -78
- package/dist/index.js +368 -77
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -26,6 +26,35 @@ var BasePlatformAdapter = class {
|
|
|
26
26
|
}
|
|
27
27
|
return "ok";
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Whether {@link editMessage} can replace the text of a message this adapter sent.
|
|
31
|
+
*
|
|
32
|
+
* Read it before starting a preview: on `false`, send the final text instead. An adapter sets
|
|
33
|
+
* it at construction and MUST NOT change it with `connect()`. Annotated as `boolean` so that an
|
|
34
|
+
* edit-capable adapter can override it with `true`.
|
|
35
|
+
*/
|
|
36
|
+
supportsEdit = false;
|
|
37
|
+
/**
|
|
38
|
+
* Replace the text of a message this adapter sent, keeping its id.
|
|
39
|
+
*
|
|
40
|
+
* Returns `{ ok: true, messageId }` on success and a `SendResult` error otherwise; it never
|
|
41
|
+
* throws on platform errors. An edit-capable adapter applies, with no request, the same local
|
|
42
|
+
* checks its `sendMessage` applies (such as `empty_text`, and `not_connected` where the send
|
|
43
|
+
* checks the connection), plus `invalid_message_id`, and `edit_too_long` where the adapter
|
|
44
|
+
* splits text: an edit that would need more than one platform message is refused, never split.
|
|
45
|
+
*
|
|
46
|
+
* Default: refuses with `edit_unsupported`, makes no platform call, and checks nothing else
|
|
47
|
+
* first, so capability is answered before input.
|
|
48
|
+
*/
|
|
49
|
+
async editMessage(_messageId, _out) {
|
|
50
|
+
return {
|
|
51
|
+
ok: false,
|
|
52
|
+
error: {
|
|
53
|
+
code: "edit_unsupported",
|
|
54
|
+
message: `${this.platform} adapter cannot edit a sent message`
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
29
58
|
/** Lifecycle: emit a typing indicator. Default: noop. */
|
|
30
59
|
async startTyping(_channelId) {
|
|
31
60
|
}
|
|
@@ -34,6 +63,90 @@ var BasePlatformAdapter = class {
|
|
|
34
63
|
}
|
|
35
64
|
};
|
|
36
65
|
|
|
66
|
+
// src/errors/config-error.ts
|
|
67
|
+
var GatewayConfigurationError = class extends Error {
|
|
68
|
+
name = "GatewayConfigurationError";
|
|
69
|
+
code;
|
|
70
|
+
detail;
|
|
71
|
+
/**
|
|
72
|
+
* @param prefix Package prefix used in the default message (e.g. `"gateway-line"`).
|
|
73
|
+
* @param opts `code` (required), optional explicit `message`, optional `detail`.
|
|
74
|
+
*/
|
|
75
|
+
constructor(prefix, opts) {
|
|
76
|
+
super(opts.message ?? `${prefix}: ${opts.code}`);
|
|
77
|
+
this.code = opts.code;
|
|
78
|
+
this.detail = opts.detail;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/channel/create-channels.ts
|
|
83
|
+
function isAbsent(value) {
|
|
84
|
+
return value === void 0 || value === null || value === "";
|
|
85
|
+
}
|
|
86
|
+
function missingRequiredKeys(entry, values) {
|
|
87
|
+
return entry.descriptor.config.filter((field) => field.required && isAbsent(values[field.key])).map((field) => field.key);
|
|
88
|
+
}
|
|
89
|
+
function refused(platform, code, reason) {
|
|
90
|
+
return {
|
|
91
|
+
refusal: {
|
|
92
|
+
platform,
|
|
93
|
+
error: new GatewayConfigurationError("gateway", {
|
|
94
|
+
code,
|
|
95
|
+
message: `channel ${platform}: ${reason}`
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function refuseBeforeCreate(entry, values, builtPlatforms) {
|
|
101
|
+
const platform = entry.descriptor.platform;
|
|
102
|
+
if (builtPlatforms.has(platform)) {
|
|
103
|
+
return refused(platform, "duplicate_channel", "a channel for this platform was already built");
|
|
104
|
+
}
|
|
105
|
+
const missing = missingRequiredKeys(entry, values);
|
|
106
|
+
if (missing.length > 0) {
|
|
107
|
+
return refused(
|
|
108
|
+
platform,
|
|
109
|
+
"missing_required_config",
|
|
110
|
+
`missing required config ${missing.join(", ")}`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return void 0;
|
|
114
|
+
}
|
|
115
|
+
function createAdapter(entry, values) {
|
|
116
|
+
const platform = entry.descriptor.platform;
|
|
117
|
+
let adapter;
|
|
118
|
+
try {
|
|
119
|
+
adapter = entry.descriptor.create(values);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
if (!(err instanceof GatewayConfigurationError)) throw err;
|
|
122
|
+
return { refusal: { platform, error: err } };
|
|
123
|
+
}
|
|
124
|
+
if (adapter.platform !== platform) {
|
|
125
|
+
return refused(
|
|
126
|
+
platform,
|
|
127
|
+
"platform_mismatch",
|
|
128
|
+
`create returned an adapter reporting platform ${adapter.platform}`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
return { adapter };
|
|
132
|
+
}
|
|
133
|
+
function createChannels(entries) {
|
|
134
|
+
const adapters = [];
|
|
135
|
+
const rejected = [];
|
|
136
|
+
const builtPlatforms = /* @__PURE__ */ new Set();
|
|
137
|
+
for (const entry of entries) {
|
|
138
|
+
const values = entry.config ?? {};
|
|
139
|
+
const outcome = refuseBeforeCreate(entry, values, builtPlatforms) ?? createAdapter(entry, values);
|
|
140
|
+
if (outcome.refusal !== void 0) {
|
|
141
|
+
rejected.push(outcome.refusal);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
adapters.push(outcome.adapter);
|
|
145
|
+
builtPlatforms.add(entry.descriptor.platform);
|
|
146
|
+
}
|
|
147
|
+
return { adapters, rejected };
|
|
148
|
+
}
|
|
149
|
+
|
|
37
150
|
// src/delivery/router.ts
|
|
38
151
|
var DeliveryRouter = class {
|
|
39
152
|
adapters = /* @__PURE__ */ new Map();
|
|
@@ -57,22 +170,6 @@ var DeliveryRouter = class {
|
|
|
57
170
|
}
|
|
58
171
|
};
|
|
59
172
|
|
|
60
|
-
// src/errors/config-error.ts
|
|
61
|
-
var GatewayConfigurationError = class extends Error {
|
|
62
|
-
name = "GatewayConfigurationError";
|
|
63
|
-
code;
|
|
64
|
-
detail;
|
|
65
|
-
/**
|
|
66
|
-
* @param prefix Package prefix used in the default message (e.g. `"gateway-line"`).
|
|
67
|
-
* @param opts `code` (required), optional explicit `message`, optional `detail`.
|
|
68
|
-
*/
|
|
69
|
-
constructor(prefix, opts) {
|
|
70
|
-
super(opts.message ?? `${prefix}: ${opts.code}`);
|
|
71
|
-
this.code = opts.code;
|
|
72
|
-
this.detail = opts.detail;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
173
|
// src/errors/lifecycle-error.ts
|
|
77
174
|
var GatewayLifecycleError = class extends Error {
|
|
78
175
|
name = "GatewayLifecycleError";
|
|
@@ -137,6 +234,18 @@ var CREDENTIAL_SHAPES = {
|
|
|
137
234
|
field: "gateway-whatsapp `appSecret`",
|
|
138
235
|
why: "32 lowercase hex, the same shape as the Twilio auth token above and as every md5 and dashless UUID. Masking it would eat the correlation ids in a log."
|
|
139
236
|
},
|
|
237
|
+
{
|
|
238
|
+
field: "gateway-nextcloud-talk `secret`",
|
|
239
|
+
why: "a Talk bot secret is a string of 40 to 128 characters the administrator chose for `occ talk:bot:install`, with no prefix or format to recognise. It never travels: both sides send only an HMAC made with it, and the adapter never logs it."
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
field: "gateway-irc `password` and `saslPassword`",
|
|
243
|
+
why: "a server password and a NickServ password are passphrases the operator chose, with no format to recognise. The SDK's `key=value` rule masks them as fields wherever they appear as `password=...`; the SASL payload on the wire is their base64, which the adapter never logs."
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
field: "gateway-google-chat `privateKey`",
|
|
247
|
+
why: "a PEM block spans lines, and in a log it usually arrives inside a JSON string with its line breaks escaped as \\n, which no single-line shape can claim whole. The SDK's `key=value` rule masks it as a field wherever it appears as `private_key=...` or `privateKey=...`."
|
|
248
|
+
},
|
|
140
249
|
{
|
|
141
250
|
field: "gateway-line `channelAccessToken`, gateway-sms `authToken` (Plivo)",
|
|
142
251
|
why: "long base64 runs with no prefix, indistinguishable from any encoded blob a platform library puts in an error message."
|
|
@@ -252,6 +361,26 @@ var HookExecutor = class {
|
|
|
252
361
|
// src/runner/gateway-runner.ts
|
|
253
362
|
var DEFAULT_DRAIN_MS = 1e4;
|
|
254
363
|
var DEFAULT_COMMAND_PREFIXES = ["/"];
|
|
364
|
+
function connectFailureReason(outcome) {
|
|
365
|
+
if (outcome.status === "fulfilled") return "connect returned false";
|
|
366
|
+
const message = outcome.reason instanceof Error ? outcome.reason.message : String(outcome.reason);
|
|
367
|
+
return redactSecrets(message);
|
|
368
|
+
}
|
|
369
|
+
async function releaseAdapters(adapters, during) {
|
|
370
|
+
await Promise.all(
|
|
371
|
+
adapters.map(async (a) => {
|
|
372
|
+
try {
|
|
373
|
+
await a.disconnect();
|
|
374
|
+
} catch (err) {
|
|
375
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
376
|
+
process.stderr.write(
|
|
377
|
+
`[gateway] adapter "${a.platform}" disconnect failed while ${during}: ${redactSecrets(message)}
|
|
378
|
+
`
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
})
|
|
382
|
+
);
|
|
383
|
+
}
|
|
255
384
|
var GatewayRunner = class {
|
|
256
385
|
constructor(opts) {
|
|
257
386
|
this.opts = opts;
|
|
@@ -265,7 +394,9 @@ var GatewayRunner = class {
|
|
|
265
394
|
unsubs = [];
|
|
266
395
|
/** Slash command registry: name → handler (word-boundary match, EC-A). */
|
|
267
396
|
commands = /* @__PURE__ */ new Map();
|
|
268
|
-
|
|
397
|
+
startResult;
|
|
398
|
+
/** The start that has not settled yet, shared by every overlapping `start()` call. */
|
|
399
|
+
startInFlight;
|
|
269
400
|
stopped = false;
|
|
270
401
|
/**
|
|
271
402
|
* Register a slash command. Match rule (EC-A): text === "/" + name
|
|
@@ -277,10 +408,29 @@ var GatewayRunner = class {
|
|
|
277
408
|
this.commands.set(name, handler);
|
|
278
409
|
}
|
|
279
410
|
/**
|
|
280
|
-
* Connect
|
|
411
|
+
* Connect the adapters and wire inbound dispatch for the ones that connected.
|
|
412
|
+
*
|
|
413
|
+
* Resolves with what connected. A second call after the first resolved returns the same result
|
|
414
|
+
* without connecting again.
|
|
415
|
+
*
|
|
416
|
+
* Overlapping calls share one start; a start overtaken by stop resolves with no adapter connected.
|
|
417
|
+
* A call made before `stop()` while another is still connecting connects nothing and settles
|
|
418
|
+
* with it; a call made after `stop()` throws `runner_stopped`. When `stop()` runs before the
|
|
419
|
+
* connect settles, the adapters that connected are disconnected, no inbound dispatch is wired,
|
|
420
|
+
* and the start resolves `{ connected: [], failed }`, keeping the `"isolate"` failures. Under
|
|
421
|
+
* `"rollback"`, a connect that fails still rejects the start.
|
|
422
|
+
*
|
|
423
|
+
* Under `connectFailure: "rollback"`, every connect settles before the rollback, so a failing
|
|
424
|
+
* start rejects only once its slowest connect settles, as a successful one resolves only then.
|
|
425
|
+
*
|
|
426
|
+
* An adapter whose `disconnect()` fails while a start rolls back, or while a start that `stop()`
|
|
427
|
+
* overtook gives its adapters back, is named on stderr with the error redacted; how the start
|
|
428
|
+
* settles does not change.
|
|
281
429
|
*
|
|
282
430
|
* @throws {GatewayLifecycleError} `runner_stopped` — the runner was already
|
|
283
431
|
* stopped. A stopped runner is spent; construct a new one.
|
|
432
|
+
* @throws {Error} under the default `connectFailure: "rollback"`, the first connect failure, after
|
|
433
|
+
* every connect settled and the adapters that did connect were disconnected.
|
|
284
434
|
*/
|
|
285
435
|
async start() {
|
|
286
436
|
if (this.stopped) {
|
|
@@ -289,21 +439,80 @@ var GatewayRunner = class {
|
|
|
289
439
|
message: "GatewayRunner.start(): this runner has already been stopped and cannot be restarted. Construct a new GatewayRunner instead."
|
|
290
440
|
});
|
|
291
441
|
}
|
|
292
|
-
if (this.
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
442
|
+
if (this.startResult !== void 0) return this.startResult;
|
|
443
|
+
if (this.startInFlight === void 0) {
|
|
444
|
+
const pending = this.runStart();
|
|
445
|
+
this.startInFlight = pending;
|
|
446
|
+
pending.catch(() => {
|
|
447
|
+
if (this.startInFlight === pending) this.startInFlight = void 0;
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
return this.startInFlight;
|
|
451
|
+
}
|
|
452
|
+
async runStart() {
|
|
453
|
+
const result = (this.opts.connectFailure ?? "rollback") === "isolate" ? await this.connectIsolated() : await this.connectAllOrRollback();
|
|
454
|
+
if (this.stopped) {
|
|
455
|
+
await releaseAdapters(result.connected, "releasing a start that stop() overtook");
|
|
456
|
+
this.startResult = { connected: [], failed: result.failed };
|
|
457
|
+
return this.startResult;
|
|
458
|
+
}
|
|
459
|
+
this.wireInbound(result.connected);
|
|
460
|
+
this.startResult = {
|
|
461
|
+
connected: result.connected.map((a) => a.platform),
|
|
462
|
+
failed: result.failed
|
|
463
|
+
};
|
|
464
|
+
return this.startResult;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* All or nothing: when any connect fails, disconnect the adapters that did connect, then rethrow
|
|
468
|
+
* the first failure. Every connect settles before the rollback: rolling back while one is still
|
|
469
|
+
* pending left that adapter live once it connected.
|
|
470
|
+
*/
|
|
471
|
+
async connectAllOrRollback() {
|
|
472
|
+
const adapters = this.opts.adapters;
|
|
473
|
+
const failures = [];
|
|
474
|
+
const settled = await Promise.allSettled(
|
|
475
|
+
adapters.map(async (a) => {
|
|
476
|
+
let ok;
|
|
477
|
+
try {
|
|
478
|
+
ok = await a.connect();
|
|
479
|
+
} catch (err) {
|
|
480
|
+
failures.push(err);
|
|
481
|
+
throw err;
|
|
482
|
+
}
|
|
483
|
+
if (!ok) {
|
|
484
|
+
const refusal = new Error(`adapter "${a.platform}" connect returned false`);
|
|
485
|
+
failures.push(refusal);
|
|
486
|
+
throw refusal;
|
|
487
|
+
}
|
|
488
|
+
})
|
|
489
|
+
);
|
|
490
|
+
if (failures.length > 0) {
|
|
491
|
+
const connected = adapters.filter((_, i) => settled[i]?.status === "fulfilled");
|
|
492
|
+
await releaseAdapters(connected, "rolling back a failed start");
|
|
493
|
+
throw failures[0];
|
|
305
494
|
}
|
|
306
|
-
|
|
495
|
+
return { connected: [...adapters], failed: [] };
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Connect every adapter and keep the ones that connected. The `async` wrapper turns a
|
|
499
|
+
* synchronous throw inside `connect()` into a rejection `allSettled` sees; a bare
|
|
500
|
+
* `a => a.connect()` would let it escape `map` and reject `start()` itself.
|
|
501
|
+
*/
|
|
502
|
+
async connectIsolated() {
|
|
503
|
+
const adapters = this.opts.adapters;
|
|
504
|
+
const settled = await Promise.allSettled(adapters.map(async (a) => a.connect()));
|
|
505
|
+
const connected = [];
|
|
506
|
+
const failed = [];
|
|
507
|
+
settled.forEach((outcome, i) => {
|
|
508
|
+
const adapter = adapters[i];
|
|
509
|
+
if (outcome.status === "fulfilled" && outcome.value) connected.push(adapter);
|
|
510
|
+
else failed.push({ platform: adapter.platform, reason: connectFailureReason(outcome) });
|
|
511
|
+
});
|
|
512
|
+
return { connected, failed };
|
|
513
|
+
}
|
|
514
|
+
wireInbound(adapters) {
|
|
515
|
+
for (const a of adapters) {
|
|
307
516
|
const unsub = a.onInbound(async (event) => {
|
|
308
517
|
const p = this.dispatch(event);
|
|
309
518
|
this.inflight.add(p);
|
|
@@ -311,11 +520,15 @@ var GatewayRunner = class {
|
|
|
311
520
|
});
|
|
312
521
|
this.unsubs.push(unsub);
|
|
313
522
|
}
|
|
314
|
-
this.connected = true;
|
|
315
523
|
}
|
|
316
524
|
/**
|
|
317
525
|
* Stop the runner. Drains in-flight handlers up to `drainTimeoutMs`,
|
|
318
|
-
* then disconnects adapters. Idempotent (EC-E).
|
|
526
|
+
* then disconnects adapters. Idempotent (EC-E). A disconnect that fails does not make `stop()`
|
|
527
|
+
* reject and does not stop the other disconnects; it is written to stderr, redacted, naming the
|
|
528
|
+
* platform.
|
|
529
|
+
*
|
|
530
|
+
* An adapter still connecting under an in-flight start is released when that start settles;
|
|
531
|
+
* `stop()` does not wait for it.
|
|
319
532
|
*/
|
|
320
533
|
async stop() {
|
|
321
534
|
if (this.stopped) return;
|
|
@@ -341,10 +554,7 @@ var GatewayRunner = class {
|
|
|
341
554
|
if (timer !== void 0) clearTimeout(timer);
|
|
342
555
|
}
|
|
343
556
|
}
|
|
344
|
-
await
|
|
345
|
-
[...this.adaptersByPlatform.values()].map((a) => a.disconnect().catch(() => void 0))
|
|
346
|
-
);
|
|
347
|
-
this.connected = false;
|
|
557
|
+
await releaseAdapters([...this.adaptersByPlatform.values()], "stopping the runner");
|
|
348
558
|
}
|
|
349
559
|
/**
|
|
350
560
|
* @internal Test seam, following `EmailAdapter._threadStoreSize`. The drain set must SHRINK: a
|
|
@@ -469,6 +679,74 @@ var SessionRouter = class {
|
|
|
469
679
|
}
|
|
470
680
|
};
|
|
471
681
|
|
|
682
|
+
// src/text/dialect.ts
|
|
683
|
+
var DIALECTS = {
|
|
684
|
+
// WhatsApp emphasis is inline and single-character: *bold*, _italic_, ~strike~, ```mono```.
|
|
685
|
+
whatsapp: { bold: "*", italic: "_", strike: "~", code: "```" },
|
|
686
|
+
// LINE text messages carry no rich text at all — decoration is emoji-only. Markers are dropped.
|
|
687
|
+
line: {},
|
|
688
|
+
// SMS is plain text by definition of the medium.
|
|
689
|
+
sms: {}
|
|
690
|
+
};
|
|
691
|
+
var PATTERN = /\*\*(?<bold>.+?)\*\*|~~(?<strike>.+?)~~|(?<![*\w])\*(?!\s)(?<italicStar>.+?)(?<!\s)\*(?!\*)|(?<![_\w])_(?!\s)(?<italicUnderscore>.+?)(?<!\s)_(?!\w)|`(?<code>.+?)`/gu;
|
|
692
|
+
var GROUP_MARKERS = [
|
|
693
|
+
["bold", "bold"],
|
|
694
|
+
["strike", "strike"],
|
|
695
|
+
["italicStar", "italic"],
|
|
696
|
+
["italicUnderscore", "italic"],
|
|
697
|
+
["code", "code"]
|
|
698
|
+
];
|
|
699
|
+
function toDialect(text, platform) {
|
|
700
|
+
const dialect = DIALECTS[platform];
|
|
701
|
+
if (dialect === void 0) return text;
|
|
702
|
+
return rewriteMarkers(text, dialect);
|
|
703
|
+
}
|
|
704
|
+
function rewriteMarkers(text, dialect) {
|
|
705
|
+
return text.replace(PATTERN, (matched, ...args) => {
|
|
706
|
+
const groups = args.at(-1);
|
|
707
|
+
if (groups === void 0) return matched;
|
|
708
|
+
for (const [group, marker] of GROUP_MARKERS) {
|
|
709
|
+
const content = groups[group];
|
|
710
|
+
if (content === void 0) continue;
|
|
711
|
+
const wrap = dialect[marker];
|
|
712
|
+
return wrap === void 0 ? content : `${wrap}${content}${wrap}`;
|
|
713
|
+
}
|
|
714
|
+
return matched;
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// src/text/channel-rendering.ts
|
|
719
|
+
var SLACK = { bold: "*", italic: "_", strike: "~", code: "`" };
|
|
720
|
+
var STRIP = {};
|
|
721
|
+
var identity = (text) => text;
|
|
722
|
+
var translateWith = (dialect) => (text) => rewriteMarkers(text, dialect);
|
|
723
|
+
var RENDERINGS = {
|
|
724
|
+
whatsapp: { format: "plain", translate: translateWith(DIALECTS.whatsapp) },
|
|
725
|
+
slack: { format: "markdown", translate: translateWith(SLACK) },
|
|
726
|
+
discord: { format: "markdown", translate: identity },
|
|
727
|
+
mattermost: { format: "markdown", translate: identity },
|
|
728
|
+
teams: { format: "markdown", translate: identity },
|
|
729
|
+
signal: { format: "markdown", translate: identity },
|
|
730
|
+
line: { format: "plain", translate: translateWith(DIALECTS.line) },
|
|
731
|
+
sms: { format: "plain", translate: translateWith(DIALECTS.sms) },
|
|
732
|
+
telegram: { format: "plain", translate: translateWith(STRIP) },
|
|
733
|
+
matrix: { format: "plain", translate: translateWith(STRIP) },
|
|
734
|
+
email: { format: "plain", translate: translateWith(STRIP) },
|
|
735
|
+
imessage: { format: "plain", translate: translateWith(STRIP) },
|
|
736
|
+
google_chat: { format: "plain", translate: translateWith(STRIP) },
|
|
737
|
+
irc: { format: "plain", translate: translateWith(STRIP) },
|
|
738
|
+
nextcloud_talk: { format: "markdown", translate: identity }
|
|
739
|
+
};
|
|
740
|
+
function channelRendering(platform) {
|
|
741
|
+
const rendering = Object.hasOwn(RENDERINGS, platform) ? RENDERINGS[platform] : void 0;
|
|
742
|
+
if (rendering === void 0) {
|
|
743
|
+
throw new RangeError(
|
|
744
|
+
`channelRendering: unknown platform "${String(platform)}"; expected one of ${Object.keys(RENDERINGS).join(", ")}`
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
return rendering;
|
|
748
|
+
}
|
|
749
|
+
|
|
472
750
|
// src/text/chunk.ts
|
|
473
751
|
function searchBoundary(remaining, window, boundaries) {
|
|
474
752
|
let cut = window;
|
|
@@ -545,39 +823,6 @@ function chunkByGrapheme(text, options) {
|
|
|
545
823
|
return parts;
|
|
546
824
|
}
|
|
547
825
|
|
|
548
|
-
// src/text/dialect.ts
|
|
549
|
-
var DIALECTS = {
|
|
550
|
-
// WhatsApp emphasis is inline and single-character: *bold*, _italic_, ~strike~, ```mono```.
|
|
551
|
-
whatsapp: { bold: "*", italic: "_", strike: "~", code: "```" },
|
|
552
|
-
// LINE text messages carry no rich text at all — decoration is emoji-only. Markers are dropped.
|
|
553
|
-
line: {},
|
|
554
|
-
// SMS is plain text by definition of the medium.
|
|
555
|
-
sms: {}
|
|
556
|
-
};
|
|
557
|
-
var PATTERN = /\*\*(?<bold>.+?)\*\*|~~(?<strike>.+?)~~|(?<![*\w])\*(?!\s)(?<italicStar>.+?)(?<!\s)\*(?!\*)|(?<![_\w])_(?!\s)(?<italicUnderscore>.+?)(?<!\s)_(?!\w)|`(?<code>.+?)`/gu;
|
|
558
|
-
var GROUP_MARKERS = [
|
|
559
|
-
["bold", "bold"],
|
|
560
|
-
["strike", "strike"],
|
|
561
|
-
["italicStar", "italic"],
|
|
562
|
-
["italicUnderscore", "italic"],
|
|
563
|
-
["code", "code"]
|
|
564
|
-
];
|
|
565
|
-
function toDialect(text, platform) {
|
|
566
|
-
const dialect = DIALECTS[platform];
|
|
567
|
-
if (dialect === void 0) return text;
|
|
568
|
-
return text.replace(PATTERN, (matched, ...args) => {
|
|
569
|
-
const groups = args.at(-1);
|
|
570
|
-
if (groups === void 0) return matched;
|
|
571
|
-
for (const [group, marker] of GROUP_MARKERS) {
|
|
572
|
-
const content = groups[group];
|
|
573
|
-
if (content === void 0) continue;
|
|
574
|
-
const wrap = dialect[marker];
|
|
575
|
-
return wrap === void 0 ? content : `${wrap}${content}${wrap}`;
|
|
576
|
-
}
|
|
577
|
-
return matched;
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
|
|
581
826
|
// src/webhook/express-server.ts
|
|
582
827
|
async function loadPeer(load, onMissing) {
|
|
583
828
|
try {
|
|
@@ -587,8 +832,14 @@ async function loadPeer(load, onMissing) {
|
|
|
587
832
|
onMissing();
|
|
588
833
|
}
|
|
589
834
|
}
|
|
590
|
-
function rawBodyCapture(tag) {
|
|
591
|
-
|
|
835
|
+
function rawBodyCapture(tag, options = {}) {
|
|
836
|
+
const { maxBytes } = options;
|
|
837
|
+
if (maxBytes !== void 0 && !(Number.isFinite(maxBytes) && maxBytes >= 0)) {
|
|
838
|
+
throw new RangeError(
|
|
839
|
+
`[${tag}] rawBodyCapture: maxBytes must be a finite number of zero or more, got ${maxBytes}`
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
return (rawReq, rawRes, next) => {
|
|
592
843
|
const req = rawReq;
|
|
593
844
|
if (req.readableEnded === true || req.complete === true) {
|
|
594
845
|
process.stderr.write(
|
|
@@ -600,17 +851,48 @@ function rawBodyCapture(tag) {
|
|
|
600
851
|
return;
|
|
601
852
|
}
|
|
602
853
|
let buf = "";
|
|
854
|
+
let bytes = 0;
|
|
855
|
+
let refused2 = maxBytes !== void 0 && declaredLength(req) > maxBytes;
|
|
856
|
+
let settled = false;
|
|
603
857
|
req.setEncoding("utf8");
|
|
604
858
|
req.on("data", (chunk) => {
|
|
859
|
+
if (refused2 || settled) return;
|
|
860
|
+
bytes += Buffer.byteLength(chunk);
|
|
861
|
+
if (maxBytes !== void 0 && bytes > maxBytes) {
|
|
862
|
+
refused2 = true;
|
|
863
|
+
buf = "";
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
605
866
|
buf += chunk;
|
|
606
867
|
});
|
|
607
868
|
req.on("end", () => {
|
|
869
|
+
if (settled) return;
|
|
870
|
+
settled = true;
|
|
871
|
+
if (refused2) {
|
|
872
|
+
answerTooLarge(rawRes, tag, maxBytes ?? 0);
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
608
875
|
req.rawBody = buf;
|
|
609
876
|
next();
|
|
610
877
|
});
|
|
611
|
-
req.on("error", (err) =>
|
|
878
|
+
req.on("error", (err) => {
|
|
879
|
+
if (settled) return;
|
|
880
|
+
settled = true;
|
|
881
|
+
if (refused2) return;
|
|
882
|
+
next(err);
|
|
883
|
+
});
|
|
612
884
|
};
|
|
613
885
|
}
|
|
886
|
+
function declaredLength(req) {
|
|
887
|
+
const value = req.headers?.["content-length"];
|
|
888
|
+
const declared = Number(Array.isArray(value) ? value[0] : value);
|
|
889
|
+
return Number.isFinite(declared) ? declared : 0;
|
|
890
|
+
}
|
|
891
|
+
function answerTooLarge(res, tag, maxBytes) {
|
|
892
|
+
process.stderr.write(`[${tag}] refused a request (413): body larger than ${maxBytes} bytes
|
|
893
|
+
`);
|
|
894
|
+
res.status(413).json({ error: "request body too large" });
|
|
895
|
+
}
|
|
614
896
|
function listenerLifecycle(opts) {
|
|
615
897
|
let started = false;
|
|
616
898
|
let stopped = false;
|
|
@@ -622,8 +904,17 @@ function listenerLifecycle(opts) {
|
|
|
622
904
|
return;
|
|
623
905
|
}
|
|
624
906
|
stopped = false;
|
|
625
|
-
await new Promise((resolve) => {
|
|
626
|
-
|
|
907
|
+
await new Promise((resolve, reject) => {
|
|
908
|
+
const settle = (err) => {
|
|
909
|
+
if (err === void 0) resolve();
|
|
910
|
+
else reject(err);
|
|
911
|
+
};
|
|
912
|
+
const current = opts.app.listen(opts.port, settle);
|
|
913
|
+
listener = current;
|
|
914
|
+
current.once?.("error", settle);
|
|
915
|
+
}).catch((err) => {
|
|
916
|
+
listener = void 0;
|
|
917
|
+
throw err;
|
|
627
918
|
});
|
|
628
919
|
started = true;
|
|
629
920
|
},
|
|
@@ -641,6 +932,6 @@ function listenerLifecycle(opts) {
|
|
|
641
932
|
};
|
|
642
933
|
}
|
|
643
934
|
|
|
644
|
-
export { BasePlatformAdapter, CREDENTIAL_SHAPES, DeliveryRouter, GatewayConfigurationError, GatewayLifecycleError, GatewayRunner, HookExecutor, SessionRouter, chunkByGrapheme, chunkText, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, rawBodyCapture, redactSecrets, toDialect };
|
|
935
|
+
export { BasePlatformAdapter, CREDENTIAL_SHAPES, DeliveryRouter, GatewayConfigurationError, GatewayLifecycleError, GatewayRunner, HookExecutor, SessionRouter, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, rawBodyCapture, redactSecrets, toDialect };
|
|
645
936
|
//# sourceMappingURL=index.js.map
|
|
646
937
|
//# sourceMappingURL=index.js.map
|