@absolutejs/voice 0.0.22-beta.657 → 0.0.22-beta.658

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.
Files changed (2) hide show
  1. package/dist/index.js +52 -31
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -39835,6 +39835,7 @@ var createVoiceMonitorRunnerRoutes = (options) => {
39835
39835
  };
39836
39836
 
39837
39837
  // src/core/plugin.ts
39838
+ var socketIdentity = (ws) => ws.raw && typeof ws.raw === "object" ? ws.raw : ws;
39838
39839
  var resolveQueryScenario = (query) => {
39839
39840
  if (typeof query?.scenarioId === "string" && query.scenarioId.trim()) {
39840
39841
  return query.scenarioId.trim();
@@ -39946,14 +39947,15 @@ var parseClientMessage = (raw) => {
39946
39947
  };
39947
39948
  var resolveSessionId3 = (runtime, ws) => {
39948
39949
  const query = ws.data && typeof ws.data === "object" && "query" in ws.data ? ws.data.query : undefined;
39949
- const existing = runtime.socketSessions.get(ws);
39950
+ const identity = socketIdentity(ws);
39951
+ const existing = runtime.socketSessions.get(identity);
39950
39952
  const providedSessionId = typeof query?.sessionId === "string" && query.sessionId.trim() ? query.sessionId.trim() : existing?.sessionId ?? createId();
39951
39953
  const scenarioId = resolveQueryScenario(query) ?? existing?.scenarioId ?? null;
39952
39954
  const resolved = {
39953
39955
  scenarioId,
39954
39956
  sessionId: providedSessionId
39955
39957
  };
39956
- runtime.socketSessions.set(ws, resolved);
39958
+ runtime.socketSessions.set(identity, resolved);
39957
39959
  return resolved;
39958
39960
  };
39959
39961
  var resolveSocketQuery = (ws) => ws.data && typeof ws.data === "object" && ("query" in ws.data) ? ws.data.query ?? {} : {};
@@ -40158,30 +40160,48 @@ var voice = (config) => {
40158
40160
  socketSessions: new WeakMap
40159
40161
  };
40160
40162
  const authorizedSockets = new WeakSet;
40161
- const authorizeSocket = async (ws, sessionId, scenarioId) => {
40162
- if (!config.authorizeConnection) {
40163
- authorizedSockets.add(ws);
40163
+ const pendingSocketAuthorizations = new WeakMap;
40164
+ const authorizeSocket = (ws, sessionId, scenarioId) => {
40165
+ const identity = socketIdentity(ws);
40166
+ const existing = pendingSocketAuthorizations.get(identity);
40167
+ if (existing)
40168
+ return existing;
40169
+ const authorization = (async () => {
40170
+ if (!config.authorizeConnection) {
40171
+ authorizedSockets.add(identity);
40172
+ return true;
40173
+ }
40174
+ let authorized = false;
40175
+ try {
40176
+ authorized = await config.authorizeConnection({
40177
+ context: ws.data,
40178
+ headers: resolveSocketHeaders(ws),
40179
+ path: config.path,
40180
+ query: resolveSocketQuery(ws),
40181
+ scenarioId,
40182
+ sessionId
40183
+ });
40184
+ } catch (error) {
40185
+ runtime.logger.warn?.(`[voice] connection authorization failed for session "${sessionId}": ${error instanceof Error ? error.message : String(error)}`);
40186
+ }
40187
+ if (!authorized) {
40188
+ ws.close(4401, "unauthorized");
40189
+ return false;
40190
+ }
40191
+ authorizedSockets.add(identity);
40164
40192
  return true;
40165
- }
40166
- let authorized = false;
40167
- try {
40168
- authorized = await config.authorizeConnection({
40169
- context: ws.data,
40170
- headers: resolveSocketHeaders(ws),
40171
- path: config.path,
40172
- query: resolveSocketQuery(ws),
40173
- scenarioId,
40174
- sessionId
40175
- });
40176
- } catch (error) {
40177
- runtime.logger.warn?.(`[voice] connection authorization failed for session "${sessionId}": ${error instanceof Error ? error.message : String(error)}`);
40178
- }
40179
- if (!authorized) {
40180
- ws.close(4401, "unauthorized");
40193
+ })();
40194
+ pendingSocketAuthorizations.set(identity, authorization);
40195
+ return authorization;
40196
+ };
40197
+ const waitForSocketAuthorization = async (ws) => {
40198
+ const identity = socketIdentity(ws);
40199
+ if (authorizedSockets.has(identity))
40200
+ return true;
40201
+ const pending = pendingSocketAuthorizations.get(identity);
40202
+ if (!pending)
40181
40203
  return false;
40182
- }
40183
- authorizedSockets.add(ws);
40184
- return true;
40204
+ return pending;
40185
40205
  };
40186
40206
  const { monitor } = config;
40187
40207
  const registerMonitorSession = (sessionId, handle) => {
@@ -40491,8 +40511,9 @@ var voice = (config) => {
40491
40511
  };
40492
40512
  return new Elysia70({ name: "absolutejs-voice" }).ws(config.path, {
40493
40513
  close: async (ws, code, reason) => {
40494
- const socketState = runtime.socketSessions.get(ws);
40495
- if (!socketState || runtime.activeSockets.get(socketState.sessionId) !== ws) {
40514
+ const identity = socketIdentity(ws);
40515
+ const socketState = runtime.socketSessions.get(identity);
40516
+ if (!socketState || runtime.activeSockets.get(socketState.sessionId) !== identity) {
40496
40517
  return;
40497
40518
  }
40498
40519
  runtime.activeSockets.delete(socketState.sessionId);
@@ -40509,7 +40530,7 @@ var voice = (config) => {
40509
40530
  }
40510
40531
  },
40511
40532
  message: async (ws, raw) => {
40512
- if (!authorizedSockets.has(ws)) {
40533
+ if (!await waitForSocketAuthorization(ws)) {
40513
40534
  ws.close(4401, "unauthorized");
40514
40535
  return;
40515
40536
  }
@@ -40595,8 +40616,8 @@ var voice = (config) => {
40595
40616
  deregisterMonitorSession(sessionState.sessionId, "session-switch");
40596
40617
  }
40597
40618
  sessionState.sessionId = message.sessionId;
40598
- runtime.activeSockets.set(message.sessionId, ws);
40599
- runtime.socketSessions.set(ws, {
40619
+ runtime.activeSockets.set(message.sessionId, socketIdentity(ws));
40620
+ runtime.socketSessions.set(socketIdentity(ws), {
40600
40621
  ...sessionState,
40601
40622
  sessionId: message.sessionId,
40602
40623
  scenarioId: sessionState.scenarioId
@@ -40604,7 +40625,7 @@ var voice = (config) => {
40604
40625
  }
40605
40626
  if (message.type === "start" && message.scenarioId) {
40606
40627
  sessionState.scenarioId = message.scenarioId;
40607
- runtime.socketSessions.set(ws, {
40628
+ runtime.socketSessions.set(socketIdentity(ws), {
40608
40629
  ...sessionState,
40609
40630
  scenarioId: message.scenarioId
40610
40631
  });
@@ -40624,7 +40645,7 @@ var voice = (config) => {
40624
40645
  return;
40625
40646
  }
40626
40647
  const existing = runtime.activeSessions.get(sessionState.sessionId);
40627
- runtime.activeSockets.set(sessionState.sessionId, ws);
40648
+ runtime.activeSockets.set(sessionState.sessionId, socketIdentity(ws));
40628
40649
  if (existing) {
40629
40650
  await existing.disconnect({
40630
40651
  code: 1006,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@absolutejs/voice",
3
- "version": "0.0.22-beta.657",
3
+ "version": "0.0.22-beta.658",
4
4
  "description": "Voice primitives and Elysia plugin for AbsoluteJS",
5
5
  "repository": {
6
6
  "type": "git",