@gbraver-burst-network/anonymous-browser-sdk 2.4.0 → 2.5.0-beta.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.
Files changed (61) hide show
  1. package/lib/abort-controller/abort-error.d.ts +7 -0
  2. package/lib/abort-controller/abort-error.js +11 -0
  3. package/lib/abort-controller/abort-manager.d.ts +17 -0
  4. package/lib/abort-controller/abort-manager.js +58 -0
  5. package/lib/abort-controller/signal-container.d.ts +5 -0
  6. package/lib/abort-controller/signal-container.js +2 -0
  7. package/lib/index.d.ts +3 -3
  8. package/lib/index.js +2 -2
  9. package/lib/sdk/{guest-anonymous-sdk.d.ts → guest/guest-anonymous-sdk.d.ts} +1 -1
  10. package/lib/sdk/{guest-anonymous-sdk.js → guest/guest-anonymous-sdk.js} +39 -23
  11. package/lib/sdk/{guest-battle-sdk.d.ts → guest/guest-battle-sdk.d.ts} +4 -1
  12. package/lib/sdk/{guest-battle-sdk.js → guest/guest-battle-sdk.js} +17 -6
  13. package/lib/sdk/{guest-webrtc-connection-manager.d.ts → guest/guest-webrtc-connection-manager.d.ts} +1 -1
  14. package/lib/sdk/{guest-webrtc-connection-manager.js → guest/guest-webrtc-connection-manager.js} +2 -2
  15. package/lib/sdk/{host-anonymous-sdk.js → host/host-anonymous-sdk.js} +17 -6
  16. package/lib/sdk/{host-battle-sdk.d.ts → host/host-battle-sdk.d.ts} +4 -1
  17. package/lib/sdk/{host-battle-sdk.js → host/host-battle-sdk.js} +20 -7
  18. package/lib/sdk/{host-webrtc-connection-manager.d.ts → host/host-webrtc-connection-manager.d.ts} +1 -1
  19. package/lib/sdk/{host-webrtc-connection-manager.js → host/host-webrtc-connection-manager.js} +1 -1
  20. package/lib/sdk/{local-webrtc-room.d.ts → host/local-webrtc-room.d.ts} +6 -3
  21. package/lib/sdk/{local-webrtc-room.js → host/local-webrtc-room.js} +36 -21
  22. package/lib/webrtc/gather-all-ice-candidate.d.ts +4 -1
  23. package/lib/webrtc/gather-all-ice-candidate.js +17 -2
  24. package/lib/webrtc/guest/guest-message.d.ts +9 -1
  25. package/lib/webrtc/guest/guest-message.js +5 -1
  26. package/lib/webrtc/guest/guest-ping-pong-check.d.ts +12 -0
  27. package/lib/webrtc/guest/guest-ping-pong-check.js +47 -0
  28. package/lib/webrtc/guest/receive-battle-progressed.d.ts +7 -2
  29. package/lib/webrtc/guest/receive-battle-progressed.js +18 -3
  30. package/lib/webrtc/guest/receive-battle-start.d.ts +7 -2
  31. package/lib/webrtc/guest/receive-battle-start.js +18 -3
  32. package/lib/webrtc/guest/receive-request-selected-player.d.ts +7 -2
  33. package/lib/webrtc/guest/receive-request-selected-player.js +18 -3
  34. package/lib/webrtc/guest/wait-until-data-channel.d.ts +4 -1
  35. package/lib/webrtc/guest/wait-until-data-channel.js +23 -5
  36. package/lib/webrtc/host/host-message.d.ts +9 -1
  37. package/lib/webrtc/host/host-message.js +5 -1
  38. package/lib/webrtc/host/host-ping-pong-check.d.ts +12 -0
  39. package/lib/webrtc/host/host-ping-pong-check.js +47 -0
  40. package/lib/webrtc/host/receive-send-command.d.ts +10 -4
  41. package/lib/webrtc/host/receive-send-command.js +20 -5
  42. package/lib/webrtc/host/request-selected-player.d.ts +9 -3
  43. package/lib/webrtc/host/request-selected-player.js +19 -4
  44. package/lib/webrtc/wait-until-connected.d.ts +4 -1
  45. package/lib/webrtc/wait-until-connected.js +17 -2
  46. package/lib/webrtc/wait-until-data-channel-ready.d.ts +4 -1
  47. package/lib/webrtc/wait-until-data-channel-ready.js +17 -2
  48. package/lib/websocket-api/connect-ws-signal.d.ts +3 -1
  49. package/lib/websocket-api/connect-ws-signal.js +15 -1
  50. package/lib/websocket-api/create-room.d.ts +7 -1
  51. package/lib/websocket-api/create-room.js +18 -2
  52. package/lib/websocket-api/delete-signaling-channel.d.ts +3 -1
  53. package/lib/websocket-api/delete-signaling-channel.js +15 -2
  54. package/lib/websocket-api/join-room.d.ts +3 -1
  55. package/lib/websocket-api/join-room.js +15 -2
  56. package/lib/websocket-api/wait-until-matching.d.ts +7 -2
  57. package/lib/websocket-api/wait-until-matching.js +18 -3
  58. package/lib/websocket-api/wait-until-sdp-recieve.d.ts +7 -2
  59. package/lib/websocket-api/wait-until-sdp-recieve.js +18 -2
  60. package/package.json +1 -1
  61. /package/lib/sdk/{host-anonymous-sdk.d.ts → host/host-anonymous-sdk.d.ts} +0 -0
@@ -19,22 +19,23 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
19
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
21
  };
22
- var _LocalWebRTCRoomImpl_instances, _LocalWebRTCRoomImpl_spanId, _LocalWebRTCRoomImpl_webRTCConnection, _LocalWebRTCRoomImpl_websocketConnection, _LocalWebRTCRoomImpl_frontendLog, _LocalWebRTCRoomImpl_hostArmdozerId, _LocalWebRTCRoomImpl_hostPilotId, _LocalWebRTCRoomImpl_signaling;
22
+ var _LocalWebRTCRoomImpl_instances, _LocalWebRTCRoomImpl_spanId, _LocalWebRTCRoomImpl_webRTCConnection, _LocalWebRTCRoomImpl_websocketConnection, _LocalWebRTCRoomImpl_frontendLog, _LocalWebRTCRoomImpl_abortManager, _LocalWebRTCRoomImpl_hostArmdozerId, _LocalWebRTCRoomImpl_hostPilotId, _LocalWebRTCRoomImpl_signaling;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.LocalWebRTCRoomImpl = void 0;
25
25
  const nanoid_1 = require("nanoid");
26
26
  const rxjs_1 = require("rxjs");
27
- const gather_all_ice_candidate_1 = require("../webrtc/gather-all-ice-candidate");
28
- const get_selected_ice_candidate_summary_1 = require("../webrtc/get-selected-ice-candidate-summary");
29
- const host_message_1 = require("../webrtc/host/host-message");
30
- const request_selected_player_1 = require("../webrtc/host/request-selected-player");
31
- const wait_until_connected_1 = require("../webrtc/wait-until-connected");
32
- const wait_until_data_channel_ready_1 = require("../webrtc/wait-until-data-channel-ready");
33
- const delete_signaling_channel_1 = require("../websocket-api/delete-signaling-channel");
34
- const notify_ice_candidate_recieved_1 = require("../websocket-api/notify-ice-candidate-recieved");
35
- const send_to_ws_signal_1 = require("../websocket-api/send-to-ws-signal");
36
- const wait_until_matching_1 = require("../websocket-api/wait-until-matching");
37
- const wait_until_sdp_recieve_1 = require("../websocket-api/wait-until-sdp-recieve");
27
+ const gather_all_ice_candidate_1 = require("../../webrtc/gather-all-ice-candidate");
28
+ const get_selected_ice_candidate_summary_1 = require("../../webrtc/get-selected-ice-candidate-summary");
29
+ const host_message_1 = require("../../webrtc/host/host-message");
30
+ const host_ping_pong_check_1 = require("../../webrtc/host/host-ping-pong-check");
31
+ const request_selected_player_1 = require("../../webrtc/host/request-selected-player");
32
+ const wait_until_connected_1 = require("../../webrtc/wait-until-connected");
33
+ const wait_until_data_channel_ready_1 = require("../../webrtc/wait-until-data-channel-ready");
34
+ const delete_signaling_channel_1 = require("../../websocket-api/delete-signaling-channel");
35
+ const notify_ice_candidate_recieved_1 = require("../../websocket-api/notify-ice-candidate-recieved");
36
+ const send_to_ws_signal_1 = require("../../websocket-api/send-to-ws-signal");
37
+ const wait_until_matching_1 = require("../../websocket-api/wait-until-matching");
38
+ const wait_until_sdp_recieve_1 = require("../../websocket-api/wait-until-sdp-recieve");
38
39
  const host_battle_sdk_1 = require("./host-battle-sdk");
39
40
  /** ローカルWebRTCルームの実装 */
40
41
  class LocalWebRTCRoomImpl {
@@ -46,6 +47,7 @@ class LocalWebRTCRoomImpl {
46
47
  * @param options.webRTCConnection WebRTCコネクションマネジャー
47
48
  * @param options.websocketConnection WebSocketコネクションマネジャー
48
49
  * @param options.frontendLog フロントエンドログマネジャー
50
+ * @param options.abortManager アボート管理オブジェクト
49
51
  */
50
52
  constructor(options) {
51
53
  _LocalWebRTCRoomImpl_instances.add(this);
@@ -57,11 +59,13 @@ class LocalWebRTCRoomImpl {
57
59
  _LocalWebRTCRoomImpl_websocketConnection.set(this, void 0);
58
60
  /** フロントエンドログマネージャー */
59
61
  _LocalWebRTCRoomImpl_frontendLog.set(this, void 0);
62
+ /** アボート管理オブジェクト */
63
+ _LocalWebRTCRoomImpl_abortManager.set(this, void 0);
60
64
  /** ホストが選択したアームドーザID */
61
65
  _LocalWebRTCRoomImpl_hostArmdozerId.set(this, void 0);
62
66
  /** ホストが選択したパイロットID */
63
67
  _LocalWebRTCRoomImpl_hostPilotId.set(this, void 0);
64
- const { roomID, spanId, webRTCConnection, websocketConnection, hostArmdozerId, hostPilotId, frontendLog, } = options;
68
+ const { roomID, spanId, webRTCConnection, websocketConnection, hostArmdozerId, hostPilotId, frontendLog, abortManager, } = options;
65
69
  this.roomID = roomID;
66
70
  __classPrivateFieldSet(this, _LocalWebRTCRoomImpl_spanId, spanId, "f");
67
71
  __classPrivateFieldSet(this, _LocalWebRTCRoomImpl_webRTCConnection, webRTCConnection, "f");
@@ -69,6 +73,7 @@ class LocalWebRTCRoomImpl {
69
73
  __classPrivateFieldSet(this, _LocalWebRTCRoomImpl_hostArmdozerId, hostArmdozerId, "f");
70
74
  __classPrivateFieldSet(this, _LocalWebRTCRoomImpl_hostPilotId, hostPilotId, "f");
71
75
  __classPrivateFieldSet(this, _LocalWebRTCRoomImpl_frontendLog, frontendLog, "f");
76
+ __classPrivateFieldSet(this, _LocalWebRTCRoomImpl_abortManager, abortManager, "f");
72
77
  }
73
78
  /**
74
79
  * マッチングするまで待機する
@@ -76,17 +81,25 @@ class LocalWebRTCRoomImpl {
76
81
  */
77
82
  waitUntilMatching() {
78
83
  return __awaiter(this, void 0, void 0, function* () {
84
+ const { signal } = __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_abortManager, "f").getAbortController();
85
+ if (signal.aborted) {
86
+ return Promise.reject(signal.reason);
87
+ }
79
88
  const websocket = yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_websocketConnection, "f").getOrCreate();
80
- const signalingID = yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_instances, "m", _LocalWebRTCRoomImpl_signaling).call(this, websocket);
89
+ const signalingID = yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_instances, "m", _LocalWebRTCRoomImpl_signaling).call(this, websocket, signal);
81
90
  const { dataChannel } = yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_webRTCConnection, "f").getOrCreateConnection();
91
+ yield (0, host_ping_pong_check_1.hostPingPongCheck)({ dataChannel, signal });
92
+ yield (0, delete_signaling_channel_1.deleteSignalingChannel)({ signalingID, websocket, signal });
93
+ __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_websocketConnection, "f").gracefulDisconnect();
82
94
  const flowID = (0, nanoid_1.nanoid)();
83
- const { armdozerId: guestArmdozerId, pilotId: guestPilotId } = yield (0, request_selected_player_1.requestSelectedPlayer)(dataChannel, flowID);
95
+ const { armdozerId: guestArmdozerId, pilotId: guestPilotId } = yield (0, request_selected_player_1.requestSelectedPlayer)({ dataChannel, flowID, signal });
84
96
  const battleSDK = new host_battle_sdk_1.HostBattleSDK({
85
97
  hostArmdozerId: __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_hostArmdozerId, "f"),
86
98
  hostPilotId: __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_hostPilotId, "f"),
87
99
  guestArmdozerId,
88
100
  guestPilotId,
89
101
  webRTCConnection: __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_webRTCConnection, "f"),
102
+ abortManager: __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_abortManager, "f"),
90
103
  });
91
104
  (0, host_message_1.sendHostMessage)(dataChannel, {
92
105
  type: "battle-start",
@@ -95,21 +108,23 @@ class LocalWebRTCRoomImpl {
95
108
  guestPlayer: battleSDK.enemy,
96
109
  update: battleSDK.initialState,
97
110
  });
98
- yield (0, delete_signaling_channel_1.deleteSignalingChannel)({ signalingID, websocket });
99
111
  return battleSDK;
100
112
  });
101
113
  }
102
114
  }
103
115
  exports.LocalWebRTCRoomImpl = LocalWebRTCRoomImpl;
104
- _LocalWebRTCRoomImpl_spanId = new WeakMap(), _LocalWebRTCRoomImpl_webRTCConnection = new WeakMap(), _LocalWebRTCRoomImpl_websocketConnection = new WeakMap(), _LocalWebRTCRoomImpl_frontendLog = new WeakMap(), _LocalWebRTCRoomImpl_hostArmdozerId = new WeakMap(), _LocalWebRTCRoomImpl_hostPilotId = new WeakMap(), _LocalWebRTCRoomImpl_instances = new WeakSet(), _LocalWebRTCRoomImpl_signaling = function _LocalWebRTCRoomImpl_signaling(websocket) {
116
+ _LocalWebRTCRoomImpl_spanId = new WeakMap(), _LocalWebRTCRoomImpl_webRTCConnection = new WeakMap(), _LocalWebRTCRoomImpl_websocketConnection = new WeakMap(), _LocalWebRTCRoomImpl_frontendLog = new WeakMap(), _LocalWebRTCRoomImpl_abortManager = new WeakMap(), _LocalWebRTCRoomImpl_hostArmdozerId = new WeakMap(), _LocalWebRTCRoomImpl_hostPilotId = new WeakMap(), _LocalWebRTCRoomImpl_instances = new WeakSet(), _LocalWebRTCRoomImpl_signaling = function _LocalWebRTCRoomImpl_signaling(websocket, signal) {
105
117
  return __awaiter(this, void 0, void 0, function* () {
118
+ if (signal.aborted) {
119
+ return Promise.reject(signal.reason);
120
+ }
106
121
  let unSubscribers = [];
107
122
  try {
108
123
  yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_frontendLog, "f").log({
109
124
  type: "SIGNALING_START",
110
125
  spanId: __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_spanId, "f"),
111
126
  });
112
- const signalingID = yield (0, wait_until_matching_1.waitUntilMatching)(websocket);
127
+ const signalingID = yield (0, wait_until_matching_1.waitUntilMatching)({ websocket, signal });
113
128
  const { connection, dataChannel } = yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_webRTCConnection, "f").getOrCreateConnection();
114
129
  let isRemoteDescriptionSet = false;
115
130
  const pendingIceCandidates = [];
@@ -147,15 +162,15 @@ _LocalWebRTCRoomImpl_spanId = new WeakMap(), _LocalWebRTCRoomImpl_webRTCConnecti
147
162
  signalingID,
148
163
  sdp,
149
164
  });
150
- const remoteSDP = yield (0, wait_until_sdp_recieve_1.waitUntilSDPReceive)(websocket);
165
+ const remoteSDP = yield (0, wait_until_sdp_recieve_1.waitUntilSDPReceive)({ websocket, signal });
151
166
  yield connection.setRemoteDescription(remoteSDP);
152
167
  isRemoteDescriptionSet = true;
153
168
  pendingIceCandidates.forEach((iceCandidate) => {
154
169
  connection.addIceCandidate(iceCandidate);
155
170
  });
156
171
  yield Promise.all([
157
- (0, wait_until_connected_1.waitUntilConnected)(connection),
158
- (0, wait_until_data_channel_ready_1.waitUntilDataChannelOpen)(dataChannel),
172
+ (0, wait_until_connected_1.waitUntilConnected)(connection, { signal }),
173
+ (0, wait_until_data_channel_ready_1.waitUntilDataChannelOpen)(dataChannel, { signal }),
159
174
  ]);
160
175
  yield __classPrivateFieldGet(this, _LocalWebRTCRoomImpl_frontendLog, "f").log({
161
176
  type: "SIGNALING_END",
@@ -1,3 +1,4 @@
1
+ import { SignalContainer } from "../abort-controller/signal-container";
1
2
  /**
2
3
  * ICE Candidateの収集中に発生したエラーのメッセージを作成する
3
4
  * @param error ICE Candidateの収集中に発生したエラー
@@ -14,7 +15,9 @@ type Result = {
14
15
  /**
15
16
  * すべてのICE Candidateが収集する
16
17
  * @param connection 待機するコネクション
18
+ * @param options オプション
19
+ * @param options.signal Promise強制終了用シグナル
17
20
  * @returns 収集結果
18
21
  */
19
- export declare const gatherAllIceCandidates: (connection: RTCPeerConnection) => Promise<Result>;
22
+ export declare const gatherAllIceCandidates: (connection: RTCPeerConnection, options?: Partial<SignalContainer>) => Promise<Result>;
20
23
  export {};
@@ -11,14 +11,26 @@ exports.createICECandidateErrorMessage = createICECandidateErrorMessage;
11
11
  /**
12
12
  * すべてのICE Candidateが収集する
13
13
  * @param connection 待機するコネクション
14
+ * @param options オプション
15
+ * @param options.signal Promise強制終了用シグナル
14
16
  * @returns 収集結果
15
17
  */
16
- const gatherAllIceCandidates = (connection) => {
18
+ const gatherAllIceCandidates = (connection, options) => {
19
+ const signal = options === null || options === void 0 ? void 0 : options.signal;
17
20
  let icecandidateHandler = null;
18
21
  let icecandidateErrorHandler = null;
22
+ let onAbort = null;
19
23
  let candidates = [];
20
24
  let iceCandidateErrors = [];
21
- return new Promise((resolve) => {
25
+ return new Promise((resolve, reject) => {
26
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
27
+ reject(signal.reason);
28
+ return;
29
+ }
30
+ onAbort = () => {
31
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
32
+ };
33
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
22
34
  icecandidateHandler = (event) => {
23
35
  if (event.candidate === null) {
24
36
  resolve({ iceCandidates: candidates, iceCandidateErrors });
@@ -34,6 +46,9 @@ const gatherAllIceCandidates = (connection) => {
34
46
  connection.addEventListener("icecandidate", icecandidateHandler);
35
47
  connection.addEventListener("icecandidateerror", icecandidateErrorHandler);
36
48
  }).finally(() => {
49
+ if (onAbort) {
50
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
51
+ }
37
52
  if (icecandidateHandler) {
38
53
  connection.removeEventListener("icecandidate", icecandidateHandler);
39
54
  }
@@ -1,5 +1,13 @@
1
1
  import { ArmdozerId, Command, PilotId } from "gbraver-burst-core";
2
2
  import { z } from "zod";
3
+ /** Pong */
4
+ export type Pong = {
5
+ type: "pong";
6
+ };
7
+ /** Pong zod のスキーマ */
8
+ export declare const PongSchema: z.ZodObject<{
9
+ type: z.ZodLiteral<"pong">;
10
+ }, z.core.$strip>;
3
11
  /** 選択したプレイヤーをホストに送信する */
4
12
  export type SendPlayer = {
5
13
  type: "send-player";
@@ -39,7 +47,7 @@ export declare const SendCommandSchema: z.ZodObject<{
39
47
  }, z.core.$strip>]>;
40
48
  }, z.core.$strip>;
41
49
  /** ゲストから送信されるメッセージ */
42
- export type GuestMessage = SendPlayer | SendCommand;
50
+ export type GuestMessage = Pong | SendPlayer | SendCommand;
43
51
  /**
44
52
  * ゲストメッセージをホストに送信する
45
53
  * @param dataChannel データチャンネル
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sendGuestMessage = exports.SendCommandSchema = exports.SendPlayerSchema = void 0;
3
+ exports.sendGuestMessage = exports.SendCommandSchema = exports.SendPlayerSchema = exports.PongSchema = void 0;
4
4
  const gbraver_burst_core_1 = require("gbraver-burst-core");
5
5
  const zod_1 = require("zod");
6
+ /** Pong zod のスキーマ */
7
+ exports.PongSchema = zod_1.z.object({
8
+ type: zod_1.z.literal("pong"),
9
+ });
6
10
  /** SendPlayer zod のスキーマ */
7
11
  exports.SendPlayerSchema = zod_1.z.object({
8
12
  type: zod_1.z.literal("send-player"),
@@ -0,0 +1,12 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
2
+ /**
3
+ * ping -> pong による疎通確認を行う
4
+ * ゲストなので ping を待って pong を送信する
5
+ * @param options オプション
6
+ * @param options.dataChannel データチャネル
7
+ * @param options.signal アボートシグナル
8
+ * @returns 疎通確認が完了したら発火するPromise
9
+ */
10
+ export declare const guestPingPongCheck: (options: {
11
+ dataChannel: RTCDataChannel;
12
+ } & Partial<SignalContainer>) => Promise<unknown>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.guestPingPongCheck = void 0;
4
+ const parse_1 = require("../../json/parse");
5
+ const host_message_1 = require("../host/host-message");
6
+ const guest_message_1 = require("./guest-message");
7
+ /**
8
+ * ping -> pong による疎通確認を行う
9
+ * ゲストなので ping を待って pong を送信する
10
+ * @param options オプション
11
+ * @param options.dataChannel データチャネル
12
+ * @param options.signal アボートシグナル
13
+ * @returns 疎通確認が完了したら発火するPromise
14
+ */
15
+ const guestPingPongCheck = (options) => {
16
+ const { dataChannel, signal } = options;
17
+ let handler = null;
18
+ let onAbort = null;
19
+ return new Promise((resolve, reject) => {
20
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
21
+ reject(signal.reason);
22
+ return;
23
+ }
24
+ onAbort = () => {
25
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
26
+ };
27
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
28
+ handler = (event) => {
29
+ const data = (0, parse_1.parseJSON)(event.data);
30
+ const parsedPing = host_message_1.PingSchema.safeParse(data);
31
+ if (parsedPing.success) {
32
+ (0, guest_message_1.sendGuestMessage)(dataChannel, { type: "pong" });
33
+ resolve(parsedPing.data);
34
+ return;
35
+ }
36
+ };
37
+ dataChannel.addEventListener("message", handler);
38
+ }).finally(() => {
39
+ if (handler) {
40
+ dataChannel.removeEventListener("message", handler);
41
+ }
42
+ if (onAbort) {
43
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
44
+ }
45
+ });
46
+ };
47
+ exports.guestPingPongCheck = guestPingPongCheck;
@@ -1,7 +1,12 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
1
2
  import { BattleProgressed } from "../host/host-message";
2
3
  /**
3
4
  * ホストから「BattleProgressed」メッセージを待ち受ける
4
- * @param dataChannel データチャネル
5
+ * @param options オプション
6
+ * @param options.dataChannel データチャネル
7
+ * @param options.signal Promise強制終了用シグナル
5
8
  * @returns BattleProgressedメッセージの内容
6
9
  */
7
- export declare const receiveBattleProgressed: (dataChannel: RTCDataChannel) => Promise<BattleProgressed>;
10
+ export declare const receiveBattleProgressed: (options: {
11
+ dataChannel: RTCDataChannel;
12
+ } & Partial<SignalContainer>) => Promise<BattleProgressed>;
@@ -5,12 +5,24 @@ const parse_1 = require("../../json/parse");
5
5
  const host_message_1 = require("../host/host-message");
6
6
  /**
7
7
  * ホストから「BattleProgressed」メッセージを待ち受ける
8
- * @param dataChannel データチャネル
8
+ * @param options オプション
9
+ * @param options.dataChannel データチャネル
10
+ * @param options.signal Promise強制終了用シグナル
9
11
  * @returns BattleProgressedメッセージの内容
10
12
  */
11
- const receiveBattleProgressed = (dataChannel) => {
13
+ const receiveBattleProgressed = (options) => {
14
+ const { dataChannel, signal } = options;
12
15
  let handler = null;
13
- return new Promise((resolve) => {
16
+ let onAbort = null;
17
+ return new Promise((resolve, reject) => {
18
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
19
+ reject(signal.reason);
20
+ return;
21
+ }
22
+ onAbort = () => {
23
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
24
+ };
25
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
14
26
  handler = (event) => {
15
27
  const data = (0, parse_1.parseJSON)(event.data);
16
28
  const parsedBattleProgressed = host_message_1.BattleProgressedSchema.safeParse(data);
@@ -24,6 +36,9 @@ const receiveBattleProgressed = (dataChannel) => {
24
36
  if (handler) {
25
37
  dataChannel.removeEventListener("message", handler);
26
38
  }
39
+ if (onAbort) {
40
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
41
+ }
27
42
  });
28
43
  };
29
44
  exports.receiveBattleProgressed = receiveBattleProgressed;
@@ -1,7 +1,12 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
1
2
  import { BattleStart } from "../host/host-message";
2
3
  /**
3
4
  * ホストから「BattleStart」メッセージを待ち受ける
4
- * @param dataChannel データチャネル
5
+ * @param options オプション
6
+ * @param options.dataChannel データチャネル
7
+ * @param options.signal Promise強制終了用シグナル
5
8
  * @returns BattleStartメッセージの内容
6
9
  */
7
- export declare const receiveBattleStart: (dataChannel: RTCDataChannel) => Promise<BattleStart>;
10
+ export declare const receiveBattleStart: (options: {
11
+ dataChannel: RTCDataChannel;
12
+ } & Partial<SignalContainer>) => Promise<BattleStart>;
@@ -5,12 +5,24 @@ const parse_1 = require("../../json/parse");
5
5
  const host_message_1 = require("../host/host-message");
6
6
  /**
7
7
  * ホストから「BattleStart」メッセージを待ち受ける
8
- * @param dataChannel データチャネル
8
+ * @param options オプション
9
+ * @param options.dataChannel データチャネル
10
+ * @param options.signal Promise強制終了用シグナル
9
11
  * @returns BattleStartメッセージの内容
10
12
  */
11
- const receiveBattleStart = (dataChannel) => {
13
+ const receiveBattleStart = (options) => {
14
+ const { dataChannel, signal } = options;
12
15
  let handler = null;
13
- return new Promise((resolve) => {
16
+ let onAbort = null;
17
+ return new Promise((resolve, reject) => {
18
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
19
+ reject(signal.reason);
20
+ return;
21
+ }
22
+ onAbort = () => {
23
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
24
+ };
25
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
14
26
  handler = (event) => {
15
27
  const data = (0, parse_1.parseJSON)(event.data);
16
28
  const parsedBattleStart = host_message_1.BattleStartSchema.safeParse(data);
@@ -24,6 +36,9 @@ const receiveBattleStart = (dataChannel) => {
24
36
  if (handler) {
25
37
  dataChannel.removeEventListener("message", handler);
26
38
  }
39
+ if (onAbort) {
40
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
41
+ }
27
42
  });
28
43
  };
29
44
  exports.receiveBattleStart = receiveBattleStart;
@@ -1,7 +1,12 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
1
2
  import { RequestSelectedPlayer } from "../host/host-message";
2
3
  /**
3
4
  * ホストから「RequestSelectedPlayer」メッセージを待ち受ける
4
- * @param dataChannel データチャネル
5
+ * @param options オプション
6
+ * @param options.dataChannel データチャネル
7
+ * @param options.signal Promise強制終了用シグナル
5
8
  * @returns フローID
6
9
  */
7
- export declare const receiveRequestSelectedPlayer: (dataChannel: RTCDataChannel) => Promise<RequestSelectedPlayer>;
10
+ export declare const receiveRequestSelectedPlayer: (options: {
11
+ dataChannel: RTCDataChannel;
12
+ } & Partial<SignalContainer>) => Promise<RequestSelectedPlayer>;
@@ -5,12 +5,24 @@ const parse_1 = require("../../json/parse");
5
5
  const host_message_1 = require("../host/host-message");
6
6
  /**
7
7
  * ホストから「RequestSelectedPlayer」メッセージを待ち受ける
8
- * @param dataChannel データチャネル
8
+ * @param options オプション
9
+ * @param options.dataChannel データチャネル
10
+ * @param options.signal Promise強制終了用シグナル
9
11
  * @returns フローID
10
12
  */
11
- const receiveRequestSelectedPlayer = (dataChannel) => {
13
+ const receiveRequestSelectedPlayer = (options) => {
14
+ const { dataChannel, signal } = options;
12
15
  let handler = null;
13
- return new Promise((resolve) => {
16
+ let onAbort = null;
17
+ return new Promise((resolve, reject) => {
18
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
19
+ reject(signal.reason);
20
+ return;
21
+ }
22
+ onAbort = () => {
23
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
24
+ };
25
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
14
26
  handler = (event) => {
15
27
  const data = (0, parse_1.parseJSON)(event.data);
16
28
  const parsedRequestSelectedPlayer = host_message_1.RequestSelectedPlayerSchema.safeParse(data);
@@ -24,6 +36,9 @@ const receiveRequestSelectedPlayer = (dataChannel) => {
24
36
  if (handler) {
25
37
  dataChannel.removeEventListener("message", handler);
26
38
  }
39
+ if (onAbort) {
40
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
41
+ }
27
42
  });
28
43
  };
29
44
  exports.receiveRequestSelectedPlayer = receiveRequestSelectedPlayer;
@@ -1,6 +1,9 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
1
2
  /**
2
3
  * ゲスト側でデータチャネルが開通するのを待つ
3
4
  * @param connection ホスト側のWebRTCコネクション
5
+ * @param options オプション
6
+ * @param options.signal Promise強制終了用シグナル
4
7
  * @returns 開通したデータチャネル
5
8
  */
6
- export declare const waitUntilDataChannel: (connection: RTCPeerConnection) => Promise<RTCDataChannel>;
9
+ export declare const waitUntilDataChannel: (connection: RTCPeerConnection, options?: Partial<SignalContainer>) => Promise<RTCDataChannel>;
@@ -4,11 +4,29 @@ exports.waitUntilDataChannel = void 0;
4
4
  /**
5
5
  * ゲスト側でデータチャネルが開通するのを待つ
6
6
  * @param connection ホスト側のWebRTCコネクション
7
+ * @param options オプション
8
+ * @param options.signal Promise強制終了用シグナル
7
9
  * @returns 開通したデータチャネル
8
10
  */
9
- const waitUntilDataChannel = (connection) => new Promise((resolve) => {
10
- connection.addEventListener("datachannel", (event) => {
11
- resolve(event.channel);
12
- }, { once: true });
13
- });
11
+ const waitUntilDataChannel = (connection, options) => {
12
+ const signal = options === null || options === void 0 ? void 0 : options.signal;
13
+ let onAbort = null;
14
+ return new Promise((resolve, reject) => {
15
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
16
+ reject(signal.reason);
17
+ return;
18
+ }
19
+ onAbort = () => {
20
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
21
+ };
22
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
23
+ connection.addEventListener("datachannel", (event) => {
24
+ resolve(event.channel);
25
+ }, { once: true });
26
+ }).finally(() => {
27
+ if (onAbort) {
28
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
29
+ }
30
+ });
31
+ };
14
32
  exports.waitUntilDataChannel = waitUntilDataChannel;
@@ -1,5 +1,13 @@
1
1
  import { GameState, Player } from "gbraver-burst-core";
2
2
  import { z } from "zod";
3
+ /** Ping */
4
+ export type Ping = {
5
+ type: "ping";
6
+ };
7
+ /** Ping zod のスキーマ */
8
+ export declare const PingSchema: z.ZodObject<{
9
+ type: z.ZodLiteral<"ping">;
10
+ }, z.core.$strip>;
3
11
  /** 選択したキャラクターをホストに送信するように要求する */
4
12
  export type RequestSelectedPlayer = {
5
13
  type: "request-selected-player";
@@ -992,7 +1000,7 @@ export declare const BattleProgressedSchema: z.ZodObject<{
992
1000
  }, z.core.$strip>>;
993
1001
  }, z.core.$strip>;
994
1002
  /** ホストから送信されるメッセージ */
995
- export type HostMessage = RequestSelectedPlayer | BattleStart | BattleProgressed;
1003
+ export type HostMessage = Ping | RequestSelectedPlayer | BattleStart | BattleProgressed;
996
1004
  /**
997
1005
  * ホストメッセージをゲストに送信する
998
1006
  * @param dataChannel データチャンネル
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sendHostMessage = exports.BattleProgressedSchema = exports.BattleStartSchema = exports.RequestSelectedPlayerSchema = void 0;
3
+ exports.sendHostMessage = exports.BattleProgressedSchema = exports.BattleStartSchema = exports.RequestSelectedPlayerSchema = exports.PingSchema = void 0;
4
4
  const gbraver_burst_core_1 = require("gbraver-burst-core");
5
5
  const zod_1 = require("zod");
6
+ /** Ping zod のスキーマ */
7
+ exports.PingSchema = zod_1.z.object({
8
+ type: zod_1.z.literal("ping"),
9
+ });
6
10
  /** RequestSelectedPlayer zod のスキーマ */
7
11
  exports.RequestSelectedPlayerSchema = zod_1.z.object({
8
12
  type: zod_1.z.literal("request-selected-player"),
@@ -0,0 +1,12 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
2
+ /**
3
+ * ping -> pong による疎通確認を行う
4
+ * ホストなので ping を送信して pong を待つ
5
+ * @param options オプション
6
+ * @param options.dataChannel データチャネル
7
+ * @param options.signal アボートシグナル
8
+ * @returns
9
+ */
10
+ export declare const hostPingPongCheck: (options: {
11
+ dataChannel: RTCDataChannel;
12
+ } & Partial<SignalContainer>) => Promise<unknown>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hostPingPongCheck = void 0;
4
+ const parse_1 = require("../../json/parse");
5
+ const guest_message_1 = require("../guest/guest-message");
6
+ const host_message_1 = require("./host-message");
7
+ /**
8
+ * ping -> pong による疎通確認を行う
9
+ * ホストなので ping を送信して pong を待つ
10
+ * @param options オプション
11
+ * @param options.dataChannel データチャネル
12
+ * @param options.signal アボートシグナル
13
+ * @returns
14
+ */
15
+ const hostPingPongCheck = (options) => {
16
+ const { dataChannel, signal } = options;
17
+ let handler = null;
18
+ let onAbort = null;
19
+ return new Promise((resolve, reject) => {
20
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
21
+ reject(signal.reason);
22
+ return;
23
+ }
24
+ onAbort = () => {
25
+ reject(signal === null || signal === void 0 ? void 0 : signal.reason);
26
+ };
27
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", onAbort);
28
+ handler = (event) => {
29
+ const data = (0, parse_1.parseJSON)(event.data);
30
+ const parsedPong = guest_message_1.PongSchema.safeParse(data);
31
+ if (parsedPong.success) {
32
+ resolve(parsedPong.data);
33
+ return;
34
+ }
35
+ };
36
+ dataChannel.addEventListener("message", handler);
37
+ (0, host_message_1.sendHostMessage)(dataChannel, { type: "ping" });
38
+ }).finally(() => {
39
+ if (handler) {
40
+ dataChannel.removeEventListener("message", handler);
41
+ }
42
+ if (onAbort) {
43
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener("abort", onAbort);
44
+ }
45
+ });
46
+ };
47
+ exports.hostPingPongCheck = hostPingPongCheck;
@@ -1,8 +1,14 @@
1
+ import { SignalContainer } from "../../abort-controller/signal-container";
1
2
  import { SendCommand } from "../guest/guest-message";
2
3
  /**
3
4
  * ゲストからflowIDが一致した「SendCommand」メッセージを待ち受ける
4
- * @param dataChannel データチャネル
5
- * @param flowID フローID
6
- * @returns フローID
5
+ * @param options オプション
6
+ * @param options.dataChannel データチャネル
7
+ * @param options.flowID フローID
8
+ * @param options.signal Promise強制終了用シグナル
9
+ * @returns ゲストから送信されたSendCommandメッセージ
7
10
  */
8
- export declare const receiveSendCommand: (dataChannel: RTCDataChannel, flowID: string) => Promise<SendCommand>;
11
+ export declare const receiveSendCommand: (options: {
12
+ dataChannel: RTCDataChannel;
13
+ flowID: string;
14
+ } & Partial<SignalContainer>) => Promise<SendCommand>;