@absolutejs/voice 0.0.22-beta.270 → 0.0.22-beta.271

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/index.d.ts CHANGED
@@ -161,12 +161,12 @@ export type { StoredVoiceIntegrationEvent, StoredVoiceExternalObjectMap, StoredV
161
161
  export { createTwilioMediaStreamBridge, createTwilioVoiceRoutes, createTwilioVoiceResponse, decodeTwilioMulawBase64, encodeTwilioMulawBase64, transcodePCMToTwilioOutboundPayload, transcodeTwilioInboundPayloadToPCM16 } from './telephony/twilio';
162
162
  export { evaluateVoiceTelephonyContract } from './telephony/contract';
163
163
  export { createTelnyxMediaStreamBridge, createTelnyxVoiceResponse, createTelnyxVoiceRoutes, verifyVoiceTelnyxWebhookSignature } from './telephony/telnyx';
164
- export { createMemoryVoicePlivoWebhookNonceStore, createPlivoMediaStreamBridge, createPlivoVoiceResponse, createPlivoVoiceRoutes, createVoicePlivoWebhookVerifier, signVoicePlivoWebhook, verifyVoicePlivoWebhookSignature } from './telephony/plivo';
164
+ export { createMemoryVoicePlivoWebhookNonceStore, createPlivoMediaStreamBridge, createPlivoVoiceResponse, createPlivoVoiceRoutes, createVoicePostgresPlivoWebhookNonceStore, createVoicePlivoWebhookVerifier, createVoiceRedisPlivoWebhookNonceStore, createVoiceSQLitePlivoWebhookNonceStore, signVoicePlivoWebhook, verifyVoicePlivoWebhookSignature } from './telephony/plivo';
165
165
  export { createVoiceTelephonyCarrierMatrix, createVoiceTelephonyCarrierMatrixRoutes, renderVoiceTelephonyCarrierMatrixHTML } from './telephony/matrix';
166
166
  export type { TwilioInboundMessage, TwilioMediaStreamBridge, TwilioMediaStreamBridgeOptions, TwilioMediaStreamSocket, TwilioOutboundClearMessage, TwilioOutboundMarkMessage, TwilioOutboundMediaMessage, TwilioOutboundMessage, TwilioVoiceRouteParameters, TwilioVoiceResponseOptions, TwilioVoiceSmokeCheck, TwilioVoiceSmokeOptions, TwilioVoiceSmokeReport, TwilioVoiceSetupOptions, TwilioVoiceSetupStatus, TwilioVoiceRoutesOptions } from './telephony/twilio';
167
167
  export type { VoiceTelephonyContractIssue, VoiceTelephonyContractOptions, VoiceTelephonyContractReport, VoiceTelephonyContractRequirement, VoiceTelephonyProvider, VoiceTelephonySetupStatus, VoiceTelephonySmokeCheck, VoiceTelephonySmokeReport } from './telephony/contract';
168
168
  export type { TelnyxInboundMessage, TelnyxMediaPayload, TelnyxMediaStreamBridge, TelnyxMediaStreamBridgeOptions, TelnyxMediaStreamSocket, TelnyxOutboundClearMessage, TelnyxOutboundMarkMessage, TelnyxOutboundMediaMessage, TelnyxOutboundMessage, TelnyxVoiceResponseOptions, TelnyxVoiceRoutesOptions, TelnyxVoiceSetupOptions, TelnyxVoiceSetupStatus, TelnyxVoiceSmokeCheck, TelnyxVoiceSmokeOptions, TelnyxVoiceSmokeReport } from './telephony/telnyx';
169
- export type { PlivoInboundMessage, PlivoMediaStreamBridge, PlivoMediaStreamBridgeOptions, PlivoMediaStreamSocket, PlivoOutboundCheckpointMessage, PlivoOutboundClearAudioMessage, PlivoOutboundMessage, PlivoOutboundPlayAudioMessage, PlivoVoiceResponseOptions, PlivoVoiceRoutesOptions, PlivoVoiceSetupOptions, PlivoVoiceSetupStatus, PlivoVoiceSmokeCheck, PlivoVoiceSmokeOptions, PlivoVoiceSmokeReport, VoicePlivoWebhookNonceStore, VoicePlivoWebhookVerifierOptions } from './telephony/plivo';
169
+ export type { PlivoInboundMessage, PlivoMediaStreamBridge, PlivoMediaStreamBridgeOptions, PlivoMediaStreamSocket, PlivoOutboundCheckpointMessage, PlivoOutboundClearAudioMessage, PlivoOutboundMessage, PlivoOutboundPlayAudioMessage, PlivoVoiceResponseOptions, PlivoVoiceRoutesOptions, PlivoVoiceSetupOptions, PlivoVoiceSetupStatus, PlivoVoiceSmokeCheck, PlivoVoiceSmokeOptions, PlivoVoiceSmokeReport, VoicePostgresPlivoWebhookNonceStoreOptions, VoicePlivoWebhookNonceStore, VoicePlivoWebhookNonceStoreOptions, VoicePlivoWebhookVerifierOptions, VoiceRedisPlivoWebhookNonceClient, VoiceRedisPlivoWebhookNonceStoreOptions, VoiceSQLitePlivoWebhookNonceStoreOptions } from './telephony/plivo';
170
170
  export type { VoiceTelephonyCarrierMatrix, VoiceTelephonyCarrierMatrixEntry, VoiceTelephonyCarrierMatrixInput, VoiceTelephonyCarrierMatrixOptions, VoiceTelephonyCarrierMatrixRoutesOptions, VoiceTelephonyCarrierMatrixStatus } from './telephony/matrix';
171
171
  export { shapeTelephonyAssistantText } from './telephony/response';
172
172
  export type { TelephonyResponseShapeMode, TelephonyResponseShapeOptions } from './telephony/response';
package/dist/index.js CHANGED
@@ -18109,6 +18109,7 @@ import { Elysia as Elysia34 } from "elysia";
18109
18109
 
18110
18110
  // src/telephony/plivo.ts
18111
18111
  import { Buffer as Buffer5 } from "buffer";
18112
+ import { Database } from "bun:sqlite";
18112
18113
  import { Elysia as Elysia30 } from "elysia";
18113
18114
 
18114
18115
  // src/telephony/contract.ts
@@ -19125,12 +19126,148 @@ var verifyVoicePlivoWebhookSignature = async (input) => {
19125
19126
  var createMemoryVoicePlivoWebhookNonceStore = () => {
19126
19127
  const nonces = new Set;
19127
19128
  return {
19129
+ claim: (nonce) => {
19130
+ if (nonces.has(nonce)) {
19131
+ return false;
19132
+ }
19133
+ nonces.add(nonce);
19134
+ return true;
19135
+ },
19128
19136
  has: (nonce) => nonces.has(nonce),
19129
19137
  set: (nonce) => {
19130
19138
  nonces.add(nonce);
19131
19139
  }
19132
19140
  };
19133
19141
  };
19142
+ var normalizePlivoStoreIdentifierSegment = (value) => value.trim().replace(/[^a-zA-Z0-9_]+/g, "_").replace(/^_+|_+$/g, "") || "voice";
19143
+ var quotePlivoStoreIdentifier = (value) => `"${value.replace(/"/g, '""')}"`;
19144
+ var resolvePlivoNonceTableName = (input) => {
19145
+ if (input.tableName) {
19146
+ return normalizePlivoStoreIdentifierSegment(input.tableName);
19147
+ }
19148
+ return `${normalizePlivoStoreIdentifierSegment(input.tablePrefix ?? "voice")}_${normalizePlivoStoreIdentifierSegment(input.fallback)}`;
19149
+ };
19150
+ var getPlivoNonceExpiresAt = (ttlSeconds) => typeof ttlSeconds === "number" && ttlSeconds > 0 ? Date.now() + Math.ceil(ttlSeconds * 1000) : null;
19151
+ var createVoiceSQLitePlivoWebhookNonceStore = (options) => {
19152
+ const database = options.database ?? new Database(options.path ?? ":memory:", {
19153
+ create: true
19154
+ });
19155
+ const tableName = resolvePlivoNonceTableName({
19156
+ fallback: "plivo_webhook_nonces",
19157
+ tableName: options.tableName,
19158
+ tablePrefix: options.tablePrefix
19159
+ });
19160
+ database.exec(`CREATE TABLE IF NOT EXISTS "${tableName}" (
19161
+ nonce TEXT PRIMARY KEY,
19162
+ created_at INTEGER NOT NULL,
19163
+ expires_at INTEGER
19164
+ )`);
19165
+ const pruneExpired = database.query(`DELETE FROM "${tableName}" WHERE expires_at IS NOT NULL AND expires_at <= ?1`);
19166
+ const select = database.query(`SELECT nonce FROM "${tableName}" WHERE nonce = ?1 AND (expires_at IS NULL OR expires_at > ?2) LIMIT 1`);
19167
+ const insert = database.query(`INSERT OR IGNORE INTO "${tableName}" (nonce, created_at, expires_at) VALUES (?1, ?2, ?3)`);
19168
+ const upsert = database.query(`INSERT INTO "${tableName}" (nonce, created_at, expires_at) VALUES (?1, ?2, ?3)
19169
+ ON CONFLICT(nonce) DO UPDATE SET expires_at = excluded.expires_at`);
19170
+ return {
19171
+ claim: (nonce) => {
19172
+ const now = Date.now();
19173
+ pruneExpired.run(now);
19174
+ const result = insert.run(nonce, now, getPlivoNonceExpiresAt(options.ttlSeconds));
19175
+ return result.changes > 0;
19176
+ },
19177
+ has: (nonce) => Boolean(select.get(nonce, Date.now())),
19178
+ set: (nonce) => {
19179
+ upsert.run(nonce, Date.now(), getPlivoNonceExpiresAt(options.ttlSeconds));
19180
+ }
19181
+ };
19182
+ };
19183
+ var createVoicePlivoPostgresClient = async (options) => {
19184
+ if (options.sql) {
19185
+ return options.sql;
19186
+ }
19187
+ if (!options.connectionString) {
19188
+ throw new Error("createVoicePostgresPlivoWebhookNonceStore requires either options.sql or options.connectionString.");
19189
+ }
19190
+ const sql = new Bun.SQL(options.connectionString);
19191
+ return {
19192
+ unsafe: sql.unsafe.bind(sql)
19193
+ };
19194
+ };
19195
+ var resolvePlivoNonceQualifiedTableName = (options) => {
19196
+ const schema = normalizePlivoStoreIdentifierSegment(options.schemaName ?? "public");
19197
+ const table = resolvePlivoNonceTableName({
19198
+ fallback: "plivo_webhook_nonces",
19199
+ tableName: options.tableName,
19200
+ tablePrefix: options.tablePrefix
19201
+ });
19202
+ return `${quotePlivoStoreIdentifier(schema)}.${quotePlivoStoreIdentifier(table)}`;
19203
+ };
19204
+ var createVoicePostgresPlivoWebhookNonceStore = (options = {}) => {
19205
+ const qualifiedTableName = resolvePlivoNonceQualifiedTableName(options);
19206
+ const schemaMatch = qualifiedTableName.match(/^"([^"]+)"\./);
19207
+ const client = createVoicePlivoPostgresClient(options);
19208
+ const initialized = (async () => {
19209
+ const sql = await client;
19210
+ if (schemaMatch?.[1]) {
19211
+ await sql.unsafe(`CREATE SCHEMA IF NOT EXISTS ${quotePlivoStoreIdentifier(schemaMatch[1])}`);
19212
+ }
19213
+ await sql.unsafe(`CREATE TABLE IF NOT EXISTS ${qualifiedTableName} (
19214
+ nonce TEXT PRIMARY KEY,
19215
+ created_at BIGINT NOT NULL,
19216
+ expires_at BIGINT
19217
+ )`);
19218
+ })();
19219
+ const pruneExpired = async () => {
19220
+ await initialized;
19221
+ const sql = await client;
19222
+ await sql.unsafe(`DELETE FROM ${qualifiedTableName} WHERE expires_at IS NOT NULL AND expires_at <= $1`, [Date.now()]);
19223
+ };
19224
+ return {
19225
+ claim: async (nonce) => {
19226
+ await pruneExpired();
19227
+ const sql = await client;
19228
+ const rows = await sql.unsafe(`INSERT INTO ${qualifiedTableName} (nonce, created_at, expires_at)
19229
+ VALUES ($1, $2, $3)
19230
+ ON CONFLICT (nonce) DO NOTHING
19231
+ RETURNING nonce`, [nonce, Date.now(), getPlivoNonceExpiresAt(options.ttlSeconds)]);
19232
+ return rows.length > 0;
19233
+ },
19234
+ has: async (nonce) => {
19235
+ await initialized;
19236
+ const sql = await client;
19237
+ const rows = await sql.unsafe(`SELECT nonce FROM ${qualifiedTableName}
19238
+ WHERE nonce = $1 AND (expires_at IS NULL OR expires_at > $2)
19239
+ LIMIT 1`, [nonce, Date.now()]);
19240
+ return rows.length > 0;
19241
+ },
19242
+ set: async (nonce) => {
19243
+ await initialized;
19244
+ const sql = await client;
19245
+ await sql.unsafe(`INSERT INTO ${qualifiedTableName} (nonce, created_at, expires_at)
19246
+ VALUES ($1, $2, $3)
19247
+ ON CONFLICT (nonce) DO UPDATE SET expires_at = EXCLUDED.expires_at`, [nonce, Date.now(), getPlivoNonceExpiresAt(options.ttlSeconds)]);
19248
+ }
19249
+ };
19250
+ };
19251
+ var getPlivoRedisNonceKey = (keyPrefix, nonce) => `${keyPrefix}:${nonce}`;
19252
+ var createVoiceRedisPlivoWebhookNonceStore = (options = {}) => {
19253
+ const client = options.client ?? new Bun.RedisClient(options.url);
19254
+ const keyPrefix = options.keyPrefix?.trim() || "voice:plivo-webhook-nonce";
19255
+ const ttlSeconds = options.ttlSeconds;
19256
+ const setNonce = async (nonce, nx) => {
19257
+ const key = getPlivoRedisNonceKey(keyPrefix, nonce);
19258
+ if (typeof ttlSeconds === "number" && ttlSeconds > 0) {
19259
+ return client.set(key, "1", "EX", String(Math.ceil(ttlSeconds)), ...nx ? ["NX"] : []);
19260
+ }
19261
+ return client.set(key, "1", ...nx ? ["NX"] : []);
19262
+ };
19263
+ return {
19264
+ claim: async (nonce) => await setNonce(nonce, true) === "OK",
19265
+ has: async (nonce) => Boolean(await client.exists(getPlivoRedisNonceKey(keyPrefix, nonce))),
19266
+ set: async (nonce) => {
19267
+ await setNonce(nonce, false);
19268
+ }
19269
+ };
19270
+ };
19134
19271
  var createVoicePlivoWebhookVerifier = (options) => async (input) => {
19135
19272
  const verificationUrl = options.verificationUrl;
19136
19273
  const verification = await verifyVoicePlivoWebhookSignature({
@@ -19150,7 +19287,16 @@ var createVoicePlivoWebhookVerifier = (options) => async (input) => {
19150
19287
  return verification;
19151
19288
  }
19152
19289
  const nonce = input.headers.get("x-plivo-signature-v3-nonce");
19153
- if (!nonce || await nonceStore.has(nonce)) {
19290
+ if (!nonce) {
19291
+ return { ok: false, reason: "invalid-signature" };
19292
+ }
19293
+ if (nonceStore.claim) {
19294
+ if (!await nonceStore.claim(nonce)) {
19295
+ return { ok: false, reason: "invalid-signature" };
19296
+ }
19297
+ return verification;
19298
+ }
19299
+ if (await nonceStore.has(nonce)) {
19154
19300
  return { ok: false, reason: "invalid-signature" };
19155
19301
  }
19156
19302
  await nonceStore.set(nonce);
@@ -23844,7 +23990,7 @@ var createVoiceOpsRecoveryRoutes = (options = {}) => {
23844
23990
 
23845
23991
  // src/observabilityExport.ts
23846
23992
  import { Elysia as Elysia41 } from "elysia";
23847
- import { Database } from "bun:sqlite";
23993
+ import { Database as Database2 } from "bun:sqlite";
23848
23994
  import { createHash } from "crypto";
23849
23995
  import { mkdir as mkdir4, readFile as readFile2, stat, unlink } from "fs/promises";
23850
23996
  import { join as join3 } from "path";
@@ -25074,7 +25220,7 @@ var loadVoiceObservabilityExportReplaySource = async (source) => {
25074
25220
  if (!source.database && !source.path) {
25075
25221
  throw new Error("SQLite observability export replay requires source.database or source.path.");
25076
25222
  }
25077
- const database = source.database ?? new Database(source.path, { create: false });
25223
+ const database = source.database ?? new Database2(source.path, { create: false });
25078
25224
  const table2 = quoteObservabilityIdentifier(normalizeObservabilityIdentifier(source.tableName));
25079
25225
  const row2 = database.query(`SELECT manifest_json, artifact_index_json, payload_json FROM ${table2} WHERE run_id = $runId`).get({ $runId: source.runId });
25080
25226
  if (!row2) {
@@ -25151,7 +25297,7 @@ var deliverObservabilityExportToSQLite = async (input) => {
25151
25297
  if (!input.destination.database && !input.destination.path) {
25152
25298
  throw new Error("SQLite observability export delivery requires destination.database or destination.path.");
25153
25299
  }
25154
- const database = input.destination.database ?? new Database(input.destination.path, { create: true });
25300
+ const database = input.destination.database ?? new Database2(input.destination.path, { create: true });
25155
25301
  const table = quoteObservabilityIdentifier(normalizeObservabilityIdentifier(input.destination.tableName));
25156
25302
  const record = buildObservabilityExportDatabaseRecord(input);
25157
25303
  database.exec(`CREATE TABLE IF NOT EXISTS ${table} (
@@ -29261,7 +29407,7 @@ var createVoiceTraceDeliveryRoutes = (options) => {
29261
29407
  return routes;
29262
29408
  };
29263
29409
  // src/sqliteStore.ts
29264
- import { Database as Database2 } from "bun:sqlite";
29410
+ import { Database as Database3 } from "bun:sqlite";
29265
29411
  var normalizeTableNameSegment = (value) => value.trim().replace(/[^a-zA-Z0-9_]+/g, "_").replace(/^_+|_+$/g, "") || "voice";
29266
29412
  var resolveTableName = (input) => {
29267
29413
  if (input.options.tableName) {
@@ -29272,7 +29418,7 @@ var resolveTableName = (input) => {
29272
29418
  return `${prefix}_${fallback}`;
29273
29419
  };
29274
29420
  var openVoiceSQLiteDatabase = (path) => {
29275
- const database = new Database2(path, {
29421
+ const database = new Database3(path, {
29276
29422
  create: true
29277
29423
  });
29278
29424
  database.exec("PRAGMA journal_mode = WAL;");
@@ -31662,6 +31808,7 @@ export {
31662
31808
  createVoiceSQLiteSessionStore,
31663
31809
  createVoiceSQLiteRuntimeStorage,
31664
31810
  createVoiceSQLiteReviewStore,
31811
+ createVoiceSQLitePlivoWebhookNonceStore,
31665
31812
  createVoiceSQLiteIntegrationEventStore,
31666
31813
  createVoiceSQLiteExternalObjectMapStore,
31667
31814
  createVoiceSQLiteDeliverySink,
@@ -31675,6 +31822,7 @@ export {
31675
31822
  createVoiceResilienceRoutes,
31676
31823
  createVoiceRedisTelephonyWebhookIdempotencyStore,
31677
31824
  createVoiceRedisTaskLeaseCoordinator,
31825
+ createVoiceRedisPlivoWebhookNonceStore,
31678
31826
  createVoiceRedisIdempotencyStore,
31679
31827
  createVoiceReconnectContractRoutes,
31680
31828
  createVoiceReadinessProfile,
@@ -31700,6 +31848,7 @@ export {
31700
31848
  createVoicePostgresSessionStore,
31701
31849
  createVoicePostgresRuntimeStorage,
31702
31850
  createVoicePostgresReviewStore,
31851
+ createVoicePostgresPlivoWebhookNonceStore,
31703
31852
  createVoicePostgresIntegrationEventStore,
31704
31853
  createVoicePostgresExternalObjectMapStore,
31705
31854
  createVoicePostgresDeliverySink,
@@ -1,7 +1,10 @@
1
+ import { Database } from 'bun:sqlite';
2
+ import type { RedisClient } from 'bun';
1
3
  import { Elysia } from 'elysia';
2
4
  import { type VoiceTelephonyContractReport, type VoiceTelephonySetupStatus, type VoiceTelephonySmokeCheck, type VoiceTelephonySmokeReport } from './contract';
3
5
  import { type VoiceTelephonyOutcomePolicy, type VoiceTelephonyWebhookRoutesOptions, type VoiceTelephonyWebhookVerificationResult } from '../telephonyOutcome';
4
6
  import type { VoiceServerMessage, VoiceSessionRecord } from '../types';
7
+ import type { VoicePostgresClient } from '../postgresStore';
5
8
  import { type TwilioMediaStreamBridgeOptions } from './twilio';
6
9
  export type PlivoInboundMessage = {
7
10
  event: 'start';
@@ -132,9 +135,32 @@ export type PlivoVoiceSmokeOptions = {
132
135
  title?: string;
133
136
  };
134
137
  export type VoicePlivoWebhookNonceStore = {
138
+ claim?: (nonce: string) => Promise<boolean> | boolean;
135
139
  has: (nonce: string) => Promise<boolean> | boolean;
136
140
  set: (nonce: string) => Promise<void> | void;
137
141
  };
142
+ export type VoicePlivoWebhookNonceStoreOptions = {
143
+ ttlSeconds?: number;
144
+ };
145
+ export type VoiceSQLitePlivoWebhookNonceStoreOptions = VoicePlivoWebhookNonceStoreOptions & {
146
+ database?: Database;
147
+ path?: string;
148
+ tableName?: string;
149
+ tablePrefix?: string;
150
+ };
151
+ export type VoicePostgresPlivoWebhookNonceStoreOptions = VoicePlivoWebhookNonceStoreOptions & {
152
+ connectionString?: string;
153
+ schemaName?: string;
154
+ sql?: VoicePostgresClient;
155
+ tableName?: string;
156
+ tablePrefix?: string;
157
+ };
158
+ export type VoiceRedisPlivoWebhookNonceClient = Pick<RedisClient, 'exists' | 'set'>;
159
+ export type VoiceRedisPlivoWebhookNonceStoreOptions = VoicePlivoWebhookNonceStoreOptions & {
160
+ client?: VoiceRedisPlivoWebhookNonceClient;
161
+ keyPrefix?: string;
162
+ url?: string;
163
+ };
138
164
  export type VoicePlivoWebhookVerifierOptions = {
139
165
  authToken?: string;
140
166
  nonceStore?: VoicePlivoWebhookNonceStore;
@@ -186,6 +212,9 @@ export declare const verifyVoicePlivoWebhookSignature: (input: {
186
212
  url: string;
187
213
  }) => Promise<VoiceTelephonyWebhookVerificationResult>;
188
214
  export declare const createMemoryVoicePlivoWebhookNonceStore: () => VoicePlivoWebhookNonceStore;
215
+ export declare const createVoiceSQLitePlivoWebhookNonceStore: (options: VoiceSQLitePlivoWebhookNonceStoreOptions) => VoicePlivoWebhookNonceStore;
216
+ export declare const createVoicePostgresPlivoWebhookNonceStore: (options?: VoicePostgresPlivoWebhookNonceStoreOptions) => VoicePlivoWebhookNonceStore;
217
+ export declare const createVoiceRedisPlivoWebhookNonceStore: (options?: VoiceRedisPlivoWebhookNonceStoreOptions) => VoicePlivoWebhookNonceStore;
189
218
  export declare const createVoicePlivoWebhookVerifier: (options: VoicePlivoWebhookVerifierOptions) => (input: {
190
219
  body: unknown;
191
220
  headers: Headers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@absolutejs/voice",
3
- "version": "0.0.22-beta.270",
3
+ "version": "0.0.22-beta.271",
4
4
  "description": "Voice primitives and Elysia plugin for AbsoluteJS",
5
5
  "repository": {
6
6
  "type": "git",