@inditextech/weave-store-azure-web-pubsub 0.68.1 → 0.69.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/dist/client.cjs +1 -0
- package/dist/client.d.cts +25 -2
- package/dist/client.d.ts +25 -2
- package/dist/client.js +1 -0
- package/dist/server.cjs +93 -68
- package/dist/server.d.cts +53 -33
- package/dist/server.d.ts +53 -33
- package/dist/server.js +93 -68
- package/package.json +2 -2
package/dist/client.cjs
CHANGED
|
@@ -30,6 +30,7 @@ const yjs = __toESM(require("yjs"));
|
|
|
30
30
|
const buffer = __toESM(require("buffer"));
|
|
31
31
|
const uuid = __toESM(require("uuid"));
|
|
32
32
|
const reconnecting_websocket = __toESM(require("reconnecting-websocket"));
|
|
33
|
+
require("ws");
|
|
33
34
|
|
|
34
35
|
//#region ../../node_modules/lodash/_listCacheClear.js
|
|
35
36
|
var require__listCacheClear = __commonJS({ "../../node_modules/lodash/_listCacheClear.js"(exports, module) {
|
package/dist/client.d.cts
CHANGED
|
@@ -6,6 +6,7 @@ import "konva/lib/types";
|
|
|
6
6
|
import Emittery from "emittery";
|
|
7
7
|
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
8
8
|
import * as awarenessProtocol from "y-protocols/awareness";
|
|
9
|
+
import { WebSocket } from "ws";
|
|
9
10
|
import { TokenCredential } from "@azure/identity";
|
|
10
11
|
|
|
11
12
|
//#region ../types/dist/types.d.ts
|
|
@@ -155,6 +156,28 @@ type WeaveStoreAzureWebPubsubOnDisconnectedEvent = {
|
|
|
155
156
|
context: ConnectionContext;
|
|
156
157
|
queries?: Record<string, string[]>;
|
|
157
158
|
};
|
|
159
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOpenEvent = {
|
|
160
|
+
group: string;
|
|
161
|
+
event: WebSocket.Event;
|
|
162
|
+
};
|
|
163
|
+
type WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent = {
|
|
164
|
+
group: string;
|
|
165
|
+
};
|
|
166
|
+
type WeaveStoreAzureWebPubsubOnWebsocketMessageEvent = {
|
|
167
|
+
group: string;
|
|
168
|
+
event: WebSocket.MessageEvent;
|
|
169
|
+
};
|
|
170
|
+
type WeaveStoreAzureWebPubsubOnWebsocketCloseEvent = {
|
|
171
|
+
group: string;
|
|
172
|
+
event: CloseEvent;
|
|
173
|
+
};
|
|
174
|
+
type WeaveStoreAzureWebPubsubOnWebsocketErrorEvent = {
|
|
175
|
+
group: string;
|
|
176
|
+
error: ErrorEvent;
|
|
177
|
+
};
|
|
178
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent = {
|
|
179
|
+
group: string;
|
|
180
|
+
};
|
|
158
181
|
|
|
159
182
|
//#endregion
|
|
160
183
|
//#region src/client.d.ts
|
|
@@ -212,7 +235,7 @@ declare class WeaveStoreAzureWebPubsub extends WeaveStore {
|
|
|
212
235
|
protected name: string;
|
|
213
236
|
protected supportsUndoManager: boolean;
|
|
214
237
|
protected awarenessCallback: (changes: any) => void;
|
|
215
|
-
constructor(storeOptions: WeaveStoreOptions, azureWebPubsubOptions: WeaveStoreAzureWebPubsubOptions);
|
|
238
|
+
constructor(storeOptions: WeaveStoreOptions, azureWebPubsubOptions: Pick<WeaveStoreAzureWebPubsubOptions, "roomId" | "url"> & Partial<Omit<WeaveStoreAzureWebPubsubOptions, "roomId" | "url">>);
|
|
216
239
|
private init;
|
|
217
240
|
emitEvent<T>(name: string, payload?: T): void;
|
|
218
241
|
getClientId(): string | null;
|
|
@@ -223,4 +246,4 @@ declare class WeaveStoreAzureWebPubsub extends WeaveStore {
|
|
|
223
246
|
}
|
|
224
247
|
|
|
225
248
|
//#endregion
|
|
226
|
-
export { FetchClient, FetchInitialState, FetchRoom, PersistRoom, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubsub, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOptions };
|
|
249
|
+
export { FetchClient, FetchInitialState, FetchRoom, PersistRoom, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubsub, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOnWebsocketCloseEvent, WeaveStoreAzureWebPubsubOnWebsocketErrorEvent, WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent, WeaveStoreAzureWebPubsubOnWebsocketMessageEvent, WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent, WeaveStoreAzureWebPubsubOnWebsocketOpenEvent, WeaveStoreAzureWebPubsubOptions };
|
package/dist/client.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import "konva";
|
|
|
3
3
|
import * as Y from "yjs";
|
|
4
4
|
import { Doc } from "yjs";
|
|
5
5
|
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
6
|
+
import { WebSocket } from "ws";
|
|
6
7
|
import "konva/lib/types";
|
|
7
8
|
import Emittery from "emittery";
|
|
8
9
|
import * as awarenessProtocol from "y-protocols/awareness";
|
|
@@ -155,6 +156,28 @@ type WeaveStoreAzureWebPubsubOnDisconnectedEvent = {
|
|
|
155
156
|
context: ConnectionContext;
|
|
156
157
|
queries?: Record<string, string[]>;
|
|
157
158
|
};
|
|
159
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOpenEvent = {
|
|
160
|
+
group: string;
|
|
161
|
+
event: WebSocket.Event;
|
|
162
|
+
};
|
|
163
|
+
type WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent = {
|
|
164
|
+
group: string;
|
|
165
|
+
};
|
|
166
|
+
type WeaveStoreAzureWebPubsubOnWebsocketMessageEvent = {
|
|
167
|
+
group: string;
|
|
168
|
+
event: WebSocket.MessageEvent;
|
|
169
|
+
};
|
|
170
|
+
type WeaveStoreAzureWebPubsubOnWebsocketCloseEvent = {
|
|
171
|
+
group: string;
|
|
172
|
+
event: CloseEvent;
|
|
173
|
+
};
|
|
174
|
+
type WeaveStoreAzureWebPubsubOnWebsocketErrorEvent = {
|
|
175
|
+
group: string;
|
|
176
|
+
error: ErrorEvent;
|
|
177
|
+
};
|
|
178
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent = {
|
|
179
|
+
group: string;
|
|
180
|
+
};
|
|
158
181
|
|
|
159
182
|
//#endregion
|
|
160
183
|
//#region src/client.d.ts
|
|
@@ -212,7 +235,7 @@ declare class WeaveStoreAzureWebPubsub extends WeaveStore {
|
|
|
212
235
|
protected name: string;
|
|
213
236
|
protected supportsUndoManager: boolean;
|
|
214
237
|
protected awarenessCallback: (changes: any) => void;
|
|
215
|
-
constructor(storeOptions: WeaveStoreOptions, azureWebPubsubOptions: WeaveStoreAzureWebPubsubOptions);
|
|
238
|
+
constructor(storeOptions: WeaveStoreOptions, azureWebPubsubOptions: Pick<WeaveStoreAzureWebPubsubOptions, "roomId" | "url"> & Partial<Omit<WeaveStoreAzureWebPubsubOptions, "roomId" | "url">>);
|
|
216
239
|
private init;
|
|
217
240
|
emitEvent<T>(name: string, payload?: T): void;
|
|
218
241
|
getClientId(): string | null;
|
|
@@ -223,4 +246,4 @@ declare class WeaveStoreAzureWebPubsub extends WeaveStore {
|
|
|
223
246
|
}
|
|
224
247
|
|
|
225
248
|
//#endregion
|
|
226
|
-
export { FetchClient, FetchInitialState, FetchRoom, PersistRoom, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubsub, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOptions };
|
|
249
|
+
export { FetchClient, FetchInitialState, FetchRoom, PersistRoom, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubsub, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOnWebsocketCloseEvent, WeaveStoreAzureWebPubsubOnWebsocketErrorEvent, WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent, WeaveStoreAzureWebPubsubOnWebsocketMessageEvent, WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent, WeaveStoreAzureWebPubsubOnWebsocketOpenEvent, WeaveStoreAzureWebPubsubOptions };
|
package/dist/client.js
CHANGED
|
@@ -5,6 +5,7 @@ import { Doc } from "yjs";
|
|
|
5
5
|
import { Buffer as Buffer$1 } from "buffer";
|
|
6
6
|
import { v4 } from "uuid";
|
|
7
7
|
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
8
|
+
import { WebSocket as WebSocket$1 } from "ws";
|
|
8
9
|
|
|
9
10
|
//#region rolldown:runtime
|
|
10
11
|
var __create = Object.create;
|
package/dist/server.cjs
CHANGED
|
@@ -27,12 +27,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
27
|
const __azure_web_pubsub = __toESM(require("@azure/web-pubsub"));
|
|
28
28
|
const __azure_identity = __toESM(require("@azure/identity"));
|
|
29
29
|
const node_util = __toESM(require("node:util"));
|
|
30
|
-
const yjs = __toESM(require("yjs"));
|
|
31
30
|
const ws = __toESM(require("ws"));
|
|
31
|
+
const yjs = __toESM(require("yjs"));
|
|
32
32
|
const node_url = __toESM(require("node:url"));
|
|
33
33
|
const node_os = __toESM(require("node:os"));
|
|
34
34
|
const node_process = __toESM(require("node:process"));
|
|
35
|
-
const reconnecting_websocket = __toESM(require("reconnecting-websocket"));
|
|
36
35
|
|
|
37
36
|
//#region ../../node_modules/ee-first/index.js
|
|
38
37
|
var require_ee_first = __commonJS({ "../../node_modules/ee-first/index.js"(exports, module) {
|
|
@@ -22763,15 +22762,14 @@ let MessageDataType = /* @__PURE__ */ function(MessageDataType$1) {
|
|
|
22763
22762
|
return MessageDataType$1;
|
|
22764
22763
|
}({});
|
|
22765
22764
|
const HostUserId = "host";
|
|
22766
|
-
var WeaveStoreAzureWebPubSubSyncHost = class
|
|
22767
|
-
constructor(client, topic, doc
|
|
22768
|
-
|
|
22765
|
+
var WeaveStoreAzureWebPubSubSyncHost = class {
|
|
22766
|
+
constructor(server, client, topic, doc) {
|
|
22767
|
+
this.server = server;
|
|
22769
22768
|
this.doc = doc;
|
|
22770
22769
|
this.topic = topic;
|
|
22771
22770
|
this.topicAwarenessChannel = `${topic}-awareness`;
|
|
22772
22771
|
this._client = client;
|
|
22773
22772
|
this._conn = null;
|
|
22774
|
-
this._polyfill = WebSocketPolyfill;
|
|
22775
22773
|
this._awareness = new Awareness(this.doc);
|
|
22776
22774
|
this._awareness.setLocalState(null);
|
|
22777
22775
|
const awarenessUpdateHandler = ({ added, updated, removed }, origin) => {
|
|
@@ -22802,59 +22800,74 @@ var WeaveStoreAzureWebPubSubSyncHost = class extends Emittery {
|
|
|
22802
22800
|
const u8 = toUint8Array(encoderAwarenessState);
|
|
22803
22801
|
this.broadcast(this.topic, origin, u8);
|
|
22804
22802
|
}
|
|
22805
|
-
async createWebSocket() {
|
|
22803
|
+
async createWebSocket(sleep = 0) {
|
|
22806
22804
|
const group = this.topic;
|
|
22807
22805
|
const { url: url$1 } = await this.negotiate(this.topic);
|
|
22808
|
-
|
|
22809
|
-
|
|
22810
|
-
|
|
22811
|
-
|
|
22812
|
-
|
|
22813
|
-
|
|
22814
|
-
|
|
22815
|
-
|
|
22816
|
-
|
|
22817
|
-
|
|
22818
|
-
|
|
22819
|
-
|
|
22820
|
-
|
|
22821
|
-
|
|
22822
|
-
|
|
22823
|
-
|
|
22824
|
-
|
|
22825
|
-
|
|
22826
|
-
|
|
22827
|
-
|
|
22828
|
-
|
|
22829
|
-
|
|
22830
|
-
|
|
22831
|
-
|
|
22832
|
-
|
|
22833
|
-
|
|
22834
|
-
|
|
22835
|
-
|
|
22836
|
-
|
|
22837
|
-
|
|
22838
|
-
|
|
22839
|
-
|
|
22840
|
-
|
|
22841
|
-
|
|
22842
|
-
|
|
22843
|
-
|
|
22844
|
-
|
|
22845
|
-
|
|
22806
|
+
if (sleep && sleep > 0) await new Promise((resolve) => setTimeout(resolve, sleep));
|
|
22807
|
+
return new Promise((resolve) => {
|
|
22808
|
+
const ws$1 = new ws.WebSocket(url$1, AzureWebPubSubJsonProtocol);
|
|
22809
|
+
ws$1.addEventListener("open", (event) => {
|
|
22810
|
+
this.server.emitEvent("onWsOpen", {
|
|
22811
|
+
group: `${group}.host`,
|
|
22812
|
+
event
|
|
22813
|
+
});
|
|
22814
|
+
ws$1.send(JSON.stringify({
|
|
22815
|
+
type: MessageType.JoinGroup,
|
|
22816
|
+
group: `${group}.host`
|
|
22817
|
+
}));
|
|
22818
|
+
this.server.emitEvent("onWsJoinGroup", { group: `${group}.host` });
|
|
22819
|
+
this._conn = ws$1;
|
|
22820
|
+
resolve();
|
|
22821
|
+
});
|
|
22822
|
+
ws$1.addEventListener("message", (e) => {
|
|
22823
|
+
this.server.emitEvent("onWsMessage", {
|
|
22824
|
+
group: `${group}.host`,
|
|
22825
|
+
event: e
|
|
22826
|
+
});
|
|
22827
|
+
const event = JSON.parse(e.data.toString());
|
|
22828
|
+
if (event.type === "message" && event.from === "group") switch (event.data.t) {
|
|
22829
|
+
case MessageDataType.Init:
|
|
22830
|
+
this.onClientInit(group, event.data);
|
|
22831
|
+
this.onClientSync(group, event.data);
|
|
22832
|
+
this.sendInitAwarenessInfo(event.data.f);
|
|
22833
|
+
return;
|
|
22834
|
+
case MessageDataType.Sync:
|
|
22835
|
+
this.onClientSync(group, event.data);
|
|
22836
|
+
return;
|
|
22837
|
+
case MessageDataType.Awareness:
|
|
22838
|
+
this.onAwareness(group, event.data);
|
|
22839
|
+
return;
|
|
22840
|
+
}
|
|
22841
|
+
});
|
|
22842
|
+
ws$1.addEventListener("close", (e) => {
|
|
22843
|
+
this.server.emitEvent("onWsClose", {
|
|
22844
|
+
group: `${group}.host`,
|
|
22845
|
+
event: e
|
|
22846
|
+
});
|
|
22847
|
+
if (e.code === 1008 && ws$1.readyState === ws.WebSocket.OPEN) {
|
|
22848
|
+
ws$1.close();
|
|
22849
|
+
this.createWebSocket();
|
|
22850
|
+
}
|
|
22851
|
+
});
|
|
22852
|
+
ws$1.addEventListener("error", (error) => {
|
|
22853
|
+
this.server.emitEvent("onWsError", {
|
|
22854
|
+
group: `${group}.host`,
|
|
22855
|
+
error
|
|
22856
|
+
});
|
|
22857
|
+
if (ws$1.readyState === ws.WebSocket.OPEN) ws$1.close();
|
|
22858
|
+
this.createWebSocket(2e4);
|
|
22859
|
+
});
|
|
22860
|
+
setTimeout(() => {
|
|
22861
|
+
if (ws$1.readyState === ws.WebSocket.OPEN) {
|
|
22862
|
+
ws$1.close();
|
|
22863
|
+
this.server.emitEvent("onWsTokenRefresh", { group: `${group}.host` });
|
|
22864
|
+
this.createWebSocket();
|
|
22865
|
+
}
|
|
22866
|
+
}, expirationTimeInMinutes * .75 * 60 * 1e3);
|
|
22846
22867
|
});
|
|
22847
|
-
setTimeout(() => {
|
|
22848
|
-
if (ws$1.readyState === ws.WebSocket.OPEN) {
|
|
22849
|
-
ws$1.close();
|
|
22850
|
-
this._conn = this.createWebSocket();
|
|
22851
|
-
}
|
|
22852
|
-
}, expirationTimeInMinutes * .75 * 60 * 1e3);
|
|
22853
|
-
this._conn = ws$1;
|
|
22854
|
-
return ws$1;
|
|
22855
22868
|
}
|
|
22856
22869
|
async start() {
|
|
22857
|
-
|
|
22870
|
+
await this.createWebSocket();
|
|
22858
22871
|
}
|
|
22859
22872
|
async stop() {
|
|
22860
22873
|
if (this._conn?.readyState === ws.WebSocket.OPEN) {
|
|
@@ -22862,8 +22875,11 @@ var WeaveStoreAzureWebPubSubSyncHost = class extends Emittery {
|
|
|
22862
22875
|
this._conn = null;
|
|
22863
22876
|
}
|
|
22864
22877
|
}
|
|
22878
|
+
simulateWebsocketError() {
|
|
22879
|
+
if (this._conn) this._conn.emit("error", new Error("Simulated connection failure"));
|
|
22880
|
+
}
|
|
22865
22881
|
broadcast(group, from$1, u8) {
|
|
22866
|
-
this._conn?.send(JSON.stringify({
|
|
22882
|
+
this._conn?.send?.(JSON.stringify({
|
|
22867
22883
|
type: MessageType.SendToGroup,
|
|
22868
22884
|
group,
|
|
22869
22885
|
noEcho: true,
|
|
@@ -22874,7 +22890,7 @@ var WeaveStoreAzureWebPubSubSyncHost = class extends Emittery {
|
|
|
22874
22890
|
}));
|
|
22875
22891
|
}
|
|
22876
22892
|
send(group, to, u8) {
|
|
22877
|
-
this._conn?.send(JSON.stringify({
|
|
22893
|
+
this._conn?.send?.(JSON.stringify({
|
|
22878
22894
|
type: MessageType.SendToGroup,
|
|
22879
22895
|
group,
|
|
22880
22896
|
noEcho: true,
|
|
@@ -22946,22 +22962,22 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
22946
22962
|
handleConnect: (req, res) => {
|
|
22947
22963
|
res.success();
|
|
22948
22964
|
this.syncOptions?.onConnect?.(req.context.connectionId, req.queries);
|
|
22949
|
-
this.
|
|
22965
|
+
this.server.emitEvent("onConnect", {
|
|
22950
22966
|
context: req.context,
|
|
22951
22967
|
queries: req.queries
|
|
22952
22968
|
});
|
|
22953
22969
|
},
|
|
22954
22970
|
onConnected: (req) => {
|
|
22955
22971
|
this.syncOptions?.onConnected?.(req.context.connectionId);
|
|
22956
|
-
this.
|
|
22972
|
+
this.server.emitEvent("onConnected", { context: req.context });
|
|
22957
22973
|
},
|
|
22958
22974
|
onDisconnected: (req) => {
|
|
22959
22975
|
this.handleConnectionDisconnection(req.context.connectionId);
|
|
22960
|
-
this.
|
|
22976
|
+
this.server.emitEvent("onDisconnected", { context: req.context });
|
|
22961
22977
|
}
|
|
22962
22978
|
});
|
|
22963
22979
|
this.syncOptions = syncHandlerOptions;
|
|
22964
|
-
this.
|
|
22980
|
+
this.server = server;
|
|
22965
22981
|
this.initialState = initialState;
|
|
22966
22982
|
this._client = client;
|
|
22967
22983
|
}
|
|
@@ -22969,15 +22985,15 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
22969
22985
|
return new yjs.Doc();
|
|
22970
22986
|
}
|
|
22971
22987
|
async setupRoomInstance(roomId) {
|
|
22972
|
-
|
|
22988
|
+
this._rooms.set(roomId, this.getNewYDoc());
|
|
22989
|
+
const doc = this._rooms.get(roomId);
|
|
22973
22990
|
let documentData = void 0;
|
|
22974
|
-
if (this.
|
|
22991
|
+
if (this.server?.fetchRoom) documentData = await this.server.fetchRoom(roomId);
|
|
22975
22992
|
if (documentData) yjs.applyUpdate(doc, documentData);
|
|
22976
22993
|
else this.initialState(doc);
|
|
22977
|
-
|
|
22978
|
-
connection.
|
|
22979
|
-
|
|
22980
|
-
this._roomsSyncHost.set(roomId, connection);
|
|
22994
|
+
this._roomsSyncHost.set(roomId, new WeaveStoreAzureWebPubSubSyncHost(this.server, this._client, roomId, doc));
|
|
22995
|
+
const connection = this._roomsSyncHost.get(roomId);
|
|
22996
|
+
await connection.start();
|
|
22981
22997
|
await this.setupRoomInstancePersistence(roomId);
|
|
22982
22998
|
}
|
|
22983
22999
|
async persistRoomTask(roomId) {
|
|
@@ -22985,7 +23001,7 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
22985
23001
|
const doc = this._rooms.get(roomId);
|
|
22986
23002
|
if (!doc) return;
|
|
22987
23003
|
const actualState = yjs.encodeStateAsUpdate(doc);
|
|
22988
|
-
if (this.
|
|
23004
|
+
if (this.server?.persistRoom) await this.server.persistRoom(roomId, actualState);
|
|
22989
23005
|
} catch (ex) {
|
|
22990
23006
|
console.error(ex);
|
|
22991
23007
|
}
|
|
@@ -23010,12 +23026,21 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
23010
23026
|
this._store_persistence.delete(roomId);
|
|
23011
23027
|
await this.persistRoomTask(roomId);
|
|
23012
23028
|
const syncHost = this._roomsSyncHost.get(roomId);
|
|
23013
|
-
if (syncHost)
|
|
23029
|
+
if (syncHost) {
|
|
23030
|
+
await syncHost.stop();
|
|
23031
|
+
this._roomsSyncHost.delete(roomId);
|
|
23032
|
+
}
|
|
23014
23033
|
this._rooms.delete(roomId);
|
|
23015
23034
|
}
|
|
23016
23035
|
async getHostConnection(roomId) {
|
|
23017
23036
|
if (!this._rooms.has(roomId)) await this.setupRoomInstance(roomId);
|
|
23018
23037
|
}
|
|
23038
|
+
getRoomsLoaded() {
|
|
23039
|
+
return Array.from(this._rooms.keys());
|
|
23040
|
+
}
|
|
23041
|
+
getRoomSyncHost(roomId) {
|
|
23042
|
+
return this._roomsSyncHost.get(roomId);
|
|
23043
|
+
}
|
|
23019
23044
|
async clientConnect(roomId) {
|
|
23020
23045
|
try {
|
|
23021
23046
|
await this.getHostConnection(roomId);
|
package/dist/server.d.cts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import koa from "koa";
|
|
2
2
|
import Emittery from "emittery";
|
|
3
|
+
import { WebSocket } from "ws";
|
|
3
4
|
import { TokenCredential } from "@azure/identity";
|
|
4
5
|
import * as Y$1 from "yjs";
|
|
5
6
|
import * as Y from "yjs";
|
|
6
7
|
import { WebPubSubServiceClient } from "@azure/web-pubsub";
|
|
7
8
|
import express, { RequestHandler } from "express-serve-static-core";
|
|
8
9
|
import * as awarenessProtocol from "y-protocols/awareness";
|
|
9
|
-
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
10
10
|
|
|
11
11
|
//#region src/types.d.ts
|
|
12
12
|
type WeaveStoreAzureWebPubsubConfig = {
|
|
@@ -58,6 +58,28 @@ type WeaveStoreAzureWebPubsubOnDisconnectedEvent = {
|
|
|
58
58
|
context: ConnectionContext;
|
|
59
59
|
queries?: Record<string, string[]>;
|
|
60
60
|
};
|
|
61
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOpenEvent = {
|
|
62
|
+
group: string;
|
|
63
|
+
event: WebSocket.Event;
|
|
64
|
+
};
|
|
65
|
+
type WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent = {
|
|
66
|
+
group: string;
|
|
67
|
+
};
|
|
68
|
+
type WeaveStoreAzureWebPubsubOnWebsocketMessageEvent = {
|
|
69
|
+
group: string;
|
|
70
|
+
event: WebSocket.MessageEvent;
|
|
71
|
+
};
|
|
72
|
+
type WeaveStoreAzureWebPubsubOnWebsocketCloseEvent = {
|
|
73
|
+
group: string;
|
|
74
|
+
event: CloseEvent;
|
|
75
|
+
};
|
|
76
|
+
type WeaveStoreAzureWebPubsubOnWebsocketErrorEvent = {
|
|
77
|
+
group: string;
|
|
78
|
+
error: ErrorEvent;
|
|
79
|
+
};
|
|
80
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent = {
|
|
81
|
+
group: string;
|
|
82
|
+
};
|
|
61
83
|
|
|
62
84
|
//#endregion
|
|
63
85
|
//#region src/server/event-handler/enum/mqtt-error-codes/mqtt-disconnect-reason-code.d.ts
|
|
@@ -842,6 +864,31 @@ declare class WebPubSubEventHandler {
|
|
|
842
864
|
getExpressJsMiddleware(): express.RequestHandler;
|
|
843
865
|
}
|
|
844
866
|
|
|
867
|
+
//#endregion
|
|
868
|
+
//#region src/server/azure-web-pubsub-host.d.ts
|
|
869
|
+
declare class WeaveStoreAzureWebPubSubSyncHost {
|
|
870
|
+
private readonly server;
|
|
871
|
+
doc: Y.Doc;
|
|
872
|
+
topic: string;
|
|
873
|
+
topicAwarenessChannel: string;
|
|
874
|
+
private _client;
|
|
875
|
+
private _conn;
|
|
876
|
+
private _awareness;
|
|
877
|
+
constructor(server: WeaveAzureWebPubsubServer, client: WebPubSubServiceClient, topic: string, doc: Y.Doc);
|
|
878
|
+
get awareness(): awarenessProtocol.Awareness;
|
|
879
|
+
sendInitAwarenessInfo(origin: string): void;
|
|
880
|
+
createWebSocket(sleep?: number): Promise<void>;
|
|
881
|
+
start(): Promise<void>;
|
|
882
|
+
stop(): Promise<void>;
|
|
883
|
+
simulateWebsocketError(): void;
|
|
884
|
+
private broadcast;
|
|
885
|
+
private send;
|
|
886
|
+
private onClientInit;
|
|
887
|
+
private onClientSync;
|
|
888
|
+
private onAwareness;
|
|
889
|
+
private negotiate;
|
|
890
|
+
}
|
|
891
|
+
|
|
845
892
|
//#endregion
|
|
846
893
|
//#region src/server/azure-web-pubsub-sync-handler.d.ts
|
|
847
894
|
declare class WeaveAzureWebPubsubSyncHandler extends WebPubSubEventHandler {
|
|
@@ -851,15 +898,17 @@ declare class WeaveAzureWebPubsubSyncHandler extends WebPubSubEventHandler {
|
|
|
851
898
|
private _store_persistence;
|
|
852
899
|
private readonly syncOptions?;
|
|
853
900
|
private initialState;
|
|
854
|
-
private
|
|
901
|
+
private readonly server;
|
|
855
902
|
constructor(hub: string, server: WeaveAzureWebPubsubServer, client: WebPubSubServiceClient, initialState: FetchInitialState, syncHandlerOptions?: WeaveAzureWebPubsubSyncHandlerOptions, eventHandlerOptions?: WebPubSubEventHandlerOptions);
|
|
856
903
|
private getNewYDoc;
|
|
857
904
|
private setupRoomInstance;
|
|
858
905
|
private persistRoomTask;
|
|
859
906
|
private setupRoomInstancePersistence;
|
|
860
907
|
private handleConnectionDisconnection;
|
|
861
|
-
|
|
908
|
+
destroyRoomInstance(roomId: string): Promise<void>;
|
|
862
909
|
private getHostConnection;
|
|
910
|
+
getRoomsLoaded(): string[];
|
|
911
|
+
getRoomSyncHost(roomId: string): WeaveStoreAzureWebPubSubSyncHost | undefined;
|
|
863
912
|
clientConnect(roomId: string): Promise<string | null>;
|
|
864
913
|
}
|
|
865
914
|
|
|
@@ -893,35 +942,6 @@ declare class WeaveAzureWebPubsubServer extends Emittery {
|
|
|
893
942
|
clientConnect(roomId: string): Promise<string | null>;
|
|
894
943
|
}
|
|
895
944
|
|
|
896
|
-
//#endregion
|
|
897
|
-
//#region src/server/azure-web-pubsub-host.d.ts
|
|
898
|
-
interface WebPubSubHostOptions {
|
|
899
|
-
WebSocketPolyfill: any;
|
|
900
|
-
}
|
|
901
|
-
declare class WeaveStoreAzureWebPubSubSyncHost extends Emittery {
|
|
902
|
-
doc: Y.Doc;
|
|
903
|
-
topic: string;
|
|
904
|
-
topicAwarenessChannel: string;
|
|
905
|
-
private _client;
|
|
906
|
-
private _polyfill;
|
|
907
|
-
private _conn;
|
|
908
|
-
private _awareness;
|
|
909
|
-
constructor(client: WebPubSubServiceClient, topic: string, doc: Y.Doc, {
|
|
910
|
-
WebSocketPolyfill
|
|
911
|
-
}: WebPubSubHostOptions);
|
|
912
|
-
get awareness(): awarenessProtocol.Awareness;
|
|
913
|
-
sendInitAwarenessInfo(origin: string): void;
|
|
914
|
-
createWebSocket(): Promise<ReconnectingWebSocket>;
|
|
915
|
-
start(): Promise<void>;
|
|
916
|
-
stop(): Promise<void>;
|
|
917
|
-
private broadcast;
|
|
918
|
-
private send;
|
|
919
|
-
private onClientInit;
|
|
920
|
-
private onClientSync;
|
|
921
|
-
private onAwareness;
|
|
922
|
-
private negotiate;
|
|
923
|
-
}
|
|
924
|
-
|
|
925
945
|
//#endregion
|
|
926
946
|
//#region src/constants.d.ts
|
|
927
947
|
declare const WEAVE_STORE_AZURE_WEB_PUBSUB = "store-azure-web-pubsub";
|
|
@@ -934,4 +954,4 @@ declare const WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG: {
|
|
|
934
954
|
};
|
|
935
955
|
|
|
936
956
|
//#endregion
|
|
937
|
-
export { Certificate, ConnectErrorResponse, ConnectRequest, ConnectResponse, ConnectResponseHandler, ConnectedRequest, ConnectionContext, DisconnectedRequest, FetchClient, FetchInitialState, FetchRoom, MqttConnectErrorResponse, MqttConnectErrorResponseProperties, MqttConnectProperties, MqttConnectRequest, MqttConnectResponse, MqttConnectResponseProperties, MqttConnectionContextProperties, MqttDisconnectPacket, MqttDisconnectReasonCode, MqttDisconnectedProperties, MqttDisconnectedRequest, MqttUserProperty, MqttV311ConnectReturnCode, MqttV500ConnectReasonCode, PersistRoom, UserEventRequest, UserEventResponseHandler, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubServer, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubSubSyncHost, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOptions, WebPubSubClientProtocol, WebPubSubEventHandler, WebPubSubEventHandlerOptions };
|
|
957
|
+
export { Certificate, ConnectErrorResponse, ConnectRequest, ConnectResponse, ConnectResponseHandler, ConnectedRequest, ConnectionContext, DisconnectedRequest, FetchClient, FetchInitialState, FetchRoom, MqttConnectErrorResponse, MqttConnectErrorResponseProperties, MqttConnectProperties, MqttConnectRequest, MqttConnectResponse, MqttConnectResponseProperties, MqttConnectionContextProperties, MqttDisconnectPacket, MqttDisconnectReasonCode, MqttDisconnectedProperties, MqttDisconnectedRequest, MqttUserProperty, MqttV311ConnectReturnCode, MqttV500ConnectReasonCode, PersistRoom, UserEventRequest, UserEventResponseHandler, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubServer, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubSubSyncHost, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOnWebsocketCloseEvent, WeaveStoreAzureWebPubsubOnWebsocketErrorEvent, WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent, WeaveStoreAzureWebPubsubOnWebsocketMessageEvent, WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent, WeaveStoreAzureWebPubsubOnWebsocketOpenEvent, WeaveStoreAzureWebPubsubOptions, WebPubSubClientProtocol, WebPubSubEventHandler, WebPubSubEventHandlerOptions };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { WebPubSubServiceClient } from "@azure/web-pubsub";
|
|
2
2
|
import { TokenCredential } from "@azure/identity";
|
|
3
|
+
import { WebSocket } from "ws";
|
|
3
4
|
import * as Y$1 from "yjs";
|
|
4
5
|
import * as Y from "yjs";
|
|
5
|
-
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
6
6
|
import koa from "koa";
|
|
7
7
|
import Emittery from "emittery";
|
|
8
8
|
import express, { RequestHandler } from "express-serve-static-core";
|
|
@@ -58,6 +58,28 @@ type WeaveStoreAzureWebPubsubOnDisconnectedEvent = {
|
|
|
58
58
|
context: ConnectionContext;
|
|
59
59
|
queries?: Record<string, string[]>;
|
|
60
60
|
};
|
|
61
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOpenEvent = {
|
|
62
|
+
group: string;
|
|
63
|
+
event: WebSocket.Event;
|
|
64
|
+
};
|
|
65
|
+
type WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent = {
|
|
66
|
+
group: string;
|
|
67
|
+
};
|
|
68
|
+
type WeaveStoreAzureWebPubsubOnWebsocketMessageEvent = {
|
|
69
|
+
group: string;
|
|
70
|
+
event: WebSocket.MessageEvent;
|
|
71
|
+
};
|
|
72
|
+
type WeaveStoreAzureWebPubsubOnWebsocketCloseEvent = {
|
|
73
|
+
group: string;
|
|
74
|
+
event: CloseEvent;
|
|
75
|
+
};
|
|
76
|
+
type WeaveStoreAzureWebPubsubOnWebsocketErrorEvent = {
|
|
77
|
+
group: string;
|
|
78
|
+
error: ErrorEvent;
|
|
79
|
+
};
|
|
80
|
+
type WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent = {
|
|
81
|
+
group: string;
|
|
82
|
+
};
|
|
61
83
|
|
|
62
84
|
//#endregion
|
|
63
85
|
//#region src/server/event-handler/enum/mqtt-error-codes/mqtt-disconnect-reason-code.d.ts
|
|
@@ -842,6 +864,31 @@ declare class WebPubSubEventHandler {
|
|
|
842
864
|
getExpressJsMiddleware(): express.RequestHandler;
|
|
843
865
|
}
|
|
844
866
|
|
|
867
|
+
//#endregion
|
|
868
|
+
//#region src/server/azure-web-pubsub-host.d.ts
|
|
869
|
+
declare class WeaveStoreAzureWebPubSubSyncHost {
|
|
870
|
+
private readonly server;
|
|
871
|
+
doc: Y.Doc;
|
|
872
|
+
topic: string;
|
|
873
|
+
topicAwarenessChannel: string;
|
|
874
|
+
private _client;
|
|
875
|
+
private _conn;
|
|
876
|
+
private _awareness;
|
|
877
|
+
constructor(server: WeaveAzureWebPubsubServer, client: WebPubSubServiceClient, topic: string, doc: Y.Doc);
|
|
878
|
+
get awareness(): awarenessProtocol.Awareness;
|
|
879
|
+
sendInitAwarenessInfo(origin: string): void;
|
|
880
|
+
createWebSocket(sleep?: number): Promise<void>;
|
|
881
|
+
start(): Promise<void>;
|
|
882
|
+
stop(): Promise<void>;
|
|
883
|
+
simulateWebsocketError(): void;
|
|
884
|
+
private broadcast;
|
|
885
|
+
private send;
|
|
886
|
+
private onClientInit;
|
|
887
|
+
private onClientSync;
|
|
888
|
+
private onAwareness;
|
|
889
|
+
private negotiate;
|
|
890
|
+
}
|
|
891
|
+
|
|
845
892
|
//#endregion
|
|
846
893
|
//#region src/server/azure-web-pubsub-sync-handler.d.ts
|
|
847
894
|
declare class WeaveAzureWebPubsubSyncHandler extends WebPubSubEventHandler {
|
|
@@ -851,15 +898,17 @@ declare class WeaveAzureWebPubsubSyncHandler extends WebPubSubEventHandler {
|
|
|
851
898
|
private _store_persistence;
|
|
852
899
|
private readonly syncOptions?;
|
|
853
900
|
private initialState;
|
|
854
|
-
private
|
|
901
|
+
private readonly server;
|
|
855
902
|
constructor(hub: string, server: WeaveAzureWebPubsubServer, client: WebPubSubServiceClient, initialState: FetchInitialState, syncHandlerOptions?: WeaveAzureWebPubsubSyncHandlerOptions, eventHandlerOptions?: WebPubSubEventHandlerOptions);
|
|
856
903
|
private getNewYDoc;
|
|
857
904
|
private setupRoomInstance;
|
|
858
905
|
private persistRoomTask;
|
|
859
906
|
private setupRoomInstancePersistence;
|
|
860
907
|
private handleConnectionDisconnection;
|
|
861
|
-
|
|
908
|
+
destroyRoomInstance(roomId: string): Promise<void>;
|
|
862
909
|
private getHostConnection;
|
|
910
|
+
getRoomsLoaded(): string[];
|
|
911
|
+
getRoomSyncHost(roomId: string): WeaveStoreAzureWebPubSubSyncHost | undefined;
|
|
863
912
|
clientConnect(roomId: string): Promise<string | null>;
|
|
864
913
|
}
|
|
865
914
|
|
|
@@ -893,35 +942,6 @@ declare class WeaveAzureWebPubsubServer extends Emittery {
|
|
|
893
942
|
clientConnect(roomId: string): Promise<string | null>;
|
|
894
943
|
}
|
|
895
944
|
|
|
896
|
-
//#endregion
|
|
897
|
-
//#region src/server/azure-web-pubsub-host.d.ts
|
|
898
|
-
interface WebPubSubHostOptions {
|
|
899
|
-
WebSocketPolyfill: any;
|
|
900
|
-
}
|
|
901
|
-
declare class WeaveStoreAzureWebPubSubSyncHost extends Emittery {
|
|
902
|
-
doc: Y.Doc;
|
|
903
|
-
topic: string;
|
|
904
|
-
topicAwarenessChannel: string;
|
|
905
|
-
private _client;
|
|
906
|
-
private _polyfill;
|
|
907
|
-
private _conn;
|
|
908
|
-
private _awareness;
|
|
909
|
-
constructor(client: WebPubSubServiceClient, topic: string, doc: Y.Doc, {
|
|
910
|
-
WebSocketPolyfill
|
|
911
|
-
}: WebPubSubHostOptions);
|
|
912
|
-
get awareness(): awarenessProtocol.Awareness;
|
|
913
|
-
sendInitAwarenessInfo(origin: string): void;
|
|
914
|
-
createWebSocket(): Promise<ReconnectingWebSocket>;
|
|
915
|
-
start(): Promise<void>;
|
|
916
|
-
stop(): Promise<void>;
|
|
917
|
-
private broadcast;
|
|
918
|
-
private send;
|
|
919
|
-
private onClientInit;
|
|
920
|
-
private onClientSync;
|
|
921
|
-
private onAwareness;
|
|
922
|
-
private negotiate;
|
|
923
|
-
}
|
|
924
|
-
|
|
925
945
|
//#endregion
|
|
926
946
|
//#region src/constants.d.ts
|
|
927
947
|
declare const WEAVE_STORE_AZURE_WEB_PUBSUB = "store-azure-web-pubsub";
|
|
@@ -934,4 +954,4 @@ declare const WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG: {
|
|
|
934
954
|
};
|
|
935
955
|
|
|
936
956
|
//#endregion
|
|
937
|
-
export { Certificate, ConnectErrorResponse, ConnectRequest, ConnectResponse, ConnectResponseHandler, ConnectedRequest, ConnectionContext, DisconnectedRequest, FetchClient, FetchInitialState, FetchRoom, MqttConnectErrorResponse, MqttConnectErrorResponseProperties, MqttConnectProperties, MqttConnectRequest, MqttConnectResponse, MqttConnectResponseProperties, MqttConnectionContextProperties, MqttDisconnectPacket, MqttDisconnectReasonCode, MqttDisconnectedProperties, MqttDisconnectedRequest, MqttUserProperty, MqttV311ConnectReturnCode, MqttV500ConnectReasonCode, PersistRoom, UserEventRequest, UserEventResponseHandler, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubServer, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubSubSyncHost, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOptions, WebPubSubClientProtocol, WebPubSubEventHandler, WebPubSubEventHandlerOptions };
|
|
957
|
+
export { Certificate, ConnectErrorResponse, ConnectRequest, ConnectResponse, ConnectResponseHandler, ConnectedRequest, ConnectionContext, DisconnectedRequest, FetchClient, FetchInitialState, FetchRoom, MqttConnectErrorResponse, MqttConnectErrorResponseProperties, MqttConnectProperties, MqttConnectRequest, MqttConnectResponse, MqttConnectResponseProperties, MqttConnectionContextProperties, MqttDisconnectPacket, MqttDisconnectReasonCode, MqttDisconnectedProperties, MqttDisconnectedRequest, MqttUserProperty, MqttV311ConnectReturnCode, MqttV500ConnectReasonCode, PersistRoom, UserEventRequest, UserEventResponseHandler, WEAVE_STORE_AZURE_WEB_PUBSUB, WEAVE_STORE_AZURE_WEB_PUBSUB_DEFAULT_CONFIG, WEAVE_STORE_HORIZONTAL_SYNC_HANDLER_CLIENT_TYPE, WeaveAzureWebPubsubServer, WeaveAzureWebPubsubSyncHandlerOptions, WeaveStoreAzureWebPubSubSyncHost, WeaveStoreAzureWebPubsubConfig, WeaveStoreAzureWebPubsubEvents, WeaveStoreAzureWebPubsubOnConnectEvent, WeaveStoreAzureWebPubsubOnConnectedEvent, WeaveStoreAzureWebPubsubOnDisconnectedEvent, WeaveStoreAzureWebPubsubOnStoreFetchConnectionUrlEvent, WeaveStoreAzureWebPubsubOnWebsocketCloseEvent, WeaveStoreAzureWebPubsubOnWebsocketErrorEvent, WeaveStoreAzureWebPubsubOnWebsocketJoinGroupEvent, WeaveStoreAzureWebPubsubOnWebsocketMessageEvent, WeaveStoreAzureWebPubsubOnWebsocketOnTokenRefreshEvent, WeaveStoreAzureWebPubsubOnWebsocketOpenEvent, WeaveStoreAzureWebPubsubOptions, WebPubSubClientProtocol, WebPubSubEventHandler, WebPubSubEventHandlerOptions };
|
package/dist/server.js
CHANGED
|
@@ -2,14 +2,13 @@ import { createRequire } from "module";
|
|
|
2
2
|
import { AzureKeyCredential, WebPubSubServiceClient } from "@azure/web-pubsub";
|
|
3
3
|
import { DefaultAzureCredential } from "@azure/identity";
|
|
4
4
|
import util from "node:util";
|
|
5
|
+
import { WebSocket } from "ws";
|
|
5
6
|
import * as Y$2 from "yjs";
|
|
6
7
|
import * as Y$1 from "yjs";
|
|
7
8
|
import * as Y from "yjs";
|
|
8
|
-
import WebSocket$1, { WebSocket } from "ws";
|
|
9
9
|
import { URL as URL$1 } from "node:url";
|
|
10
10
|
import { EOL } from "node:os";
|
|
11
11
|
import * as process$1 from "node:process";
|
|
12
|
-
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
13
12
|
|
|
14
13
|
//#region rolldown:runtime
|
|
15
14
|
var __create = Object.create;
|
|
@@ -22767,15 +22766,14 @@ let MessageDataType = /* @__PURE__ */ function(MessageDataType$1) {
|
|
|
22767
22766
|
return MessageDataType$1;
|
|
22768
22767
|
}({});
|
|
22769
22768
|
const HostUserId = "host";
|
|
22770
|
-
var WeaveStoreAzureWebPubSubSyncHost = class
|
|
22771
|
-
constructor(client, topic, doc
|
|
22772
|
-
|
|
22769
|
+
var WeaveStoreAzureWebPubSubSyncHost = class {
|
|
22770
|
+
constructor(server, client, topic, doc) {
|
|
22771
|
+
this.server = server;
|
|
22773
22772
|
this.doc = doc;
|
|
22774
22773
|
this.topic = topic;
|
|
22775
22774
|
this.topicAwarenessChannel = `${topic}-awareness`;
|
|
22776
22775
|
this._client = client;
|
|
22777
22776
|
this._conn = null;
|
|
22778
|
-
this._polyfill = WebSocketPolyfill;
|
|
22779
22777
|
this._awareness = new Awareness(this.doc);
|
|
22780
22778
|
this._awareness.setLocalState(null);
|
|
22781
22779
|
const awarenessUpdateHandler = ({ added, updated, removed }, origin) => {
|
|
@@ -22806,59 +22804,74 @@ var WeaveStoreAzureWebPubSubSyncHost = class extends Emittery {
|
|
|
22806
22804
|
const u8 = toUint8Array(encoderAwarenessState);
|
|
22807
22805
|
this.broadcast(this.topic, origin, u8);
|
|
22808
22806
|
}
|
|
22809
|
-
async createWebSocket() {
|
|
22807
|
+
async createWebSocket(sleep = 0) {
|
|
22810
22808
|
const group = this.topic;
|
|
22811
22809
|
const { url: url$1 } = await this.negotiate(this.topic);
|
|
22812
|
-
|
|
22813
|
-
|
|
22814
|
-
|
|
22815
|
-
|
|
22816
|
-
|
|
22817
|
-
|
|
22818
|
-
|
|
22819
|
-
|
|
22820
|
-
|
|
22821
|
-
|
|
22822
|
-
|
|
22823
|
-
|
|
22824
|
-
|
|
22825
|
-
|
|
22826
|
-
|
|
22827
|
-
|
|
22828
|
-
|
|
22829
|
-
|
|
22830
|
-
|
|
22831
|
-
|
|
22832
|
-
|
|
22833
|
-
|
|
22834
|
-
|
|
22835
|
-
|
|
22836
|
-
|
|
22837
|
-
|
|
22838
|
-
|
|
22839
|
-
|
|
22840
|
-
|
|
22841
|
-
|
|
22842
|
-
|
|
22843
|
-
|
|
22844
|
-
|
|
22845
|
-
|
|
22846
|
-
|
|
22847
|
-
|
|
22848
|
-
|
|
22849
|
-
|
|
22810
|
+
if (sleep && sleep > 0) await new Promise((resolve) => setTimeout(resolve, sleep));
|
|
22811
|
+
return new Promise((resolve) => {
|
|
22812
|
+
const ws = new WebSocket(url$1, AzureWebPubSubJsonProtocol);
|
|
22813
|
+
ws.addEventListener("open", (event) => {
|
|
22814
|
+
this.server.emitEvent("onWsOpen", {
|
|
22815
|
+
group: `${group}.host`,
|
|
22816
|
+
event
|
|
22817
|
+
});
|
|
22818
|
+
ws.send(JSON.stringify({
|
|
22819
|
+
type: MessageType.JoinGroup,
|
|
22820
|
+
group: `${group}.host`
|
|
22821
|
+
}));
|
|
22822
|
+
this.server.emitEvent("onWsJoinGroup", { group: `${group}.host` });
|
|
22823
|
+
this._conn = ws;
|
|
22824
|
+
resolve();
|
|
22825
|
+
});
|
|
22826
|
+
ws.addEventListener("message", (e) => {
|
|
22827
|
+
this.server.emitEvent("onWsMessage", {
|
|
22828
|
+
group: `${group}.host`,
|
|
22829
|
+
event: e
|
|
22830
|
+
});
|
|
22831
|
+
const event = JSON.parse(e.data.toString());
|
|
22832
|
+
if (event.type === "message" && event.from === "group") switch (event.data.t) {
|
|
22833
|
+
case MessageDataType.Init:
|
|
22834
|
+
this.onClientInit(group, event.data);
|
|
22835
|
+
this.onClientSync(group, event.data);
|
|
22836
|
+
this.sendInitAwarenessInfo(event.data.f);
|
|
22837
|
+
return;
|
|
22838
|
+
case MessageDataType.Sync:
|
|
22839
|
+
this.onClientSync(group, event.data);
|
|
22840
|
+
return;
|
|
22841
|
+
case MessageDataType.Awareness:
|
|
22842
|
+
this.onAwareness(group, event.data);
|
|
22843
|
+
return;
|
|
22844
|
+
}
|
|
22845
|
+
});
|
|
22846
|
+
ws.addEventListener("close", (e) => {
|
|
22847
|
+
this.server.emitEvent("onWsClose", {
|
|
22848
|
+
group: `${group}.host`,
|
|
22849
|
+
event: e
|
|
22850
|
+
});
|
|
22851
|
+
if (e.code === 1008 && ws.readyState === WebSocket.OPEN) {
|
|
22852
|
+
ws.close();
|
|
22853
|
+
this.createWebSocket();
|
|
22854
|
+
}
|
|
22855
|
+
});
|
|
22856
|
+
ws.addEventListener("error", (error) => {
|
|
22857
|
+
this.server.emitEvent("onWsError", {
|
|
22858
|
+
group: `${group}.host`,
|
|
22859
|
+
error
|
|
22860
|
+
});
|
|
22861
|
+
if (ws.readyState === WebSocket.OPEN) ws.close();
|
|
22862
|
+
this.createWebSocket(2e4);
|
|
22863
|
+
});
|
|
22864
|
+
setTimeout(() => {
|
|
22865
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
22866
|
+
ws.close();
|
|
22867
|
+
this.server.emitEvent("onWsTokenRefresh", { group: `${group}.host` });
|
|
22868
|
+
this.createWebSocket();
|
|
22869
|
+
}
|
|
22870
|
+
}, expirationTimeInMinutes * .75 * 60 * 1e3);
|
|
22850
22871
|
});
|
|
22851
|
-
setTimeout(() => {
|
|
22852
|
-
if (ws.readyState === WebSocket.OPEN) {
|
|
22853
|
-
ws.close();
|
|
22854
|
-
this._conn = this.createWebSocket();
|
|
22855
|
-
}
|
|
22856
|
-
}, expirationTimeInMinutes * .75 * 60 * 1e3);
|
|
22857
|
-
this._conn = ws;
|
|
22858
|
-
return ws;
|
|
22859
22872
|
}
|
|
22860
22873
|
async start() {
|
|
22861
|
-
|
|
22874
|
+
await this.createWebSocket();
|
|
22862
22875
|
}
|
|
22863
22876
|
async stop() {
|
|
22864
22877
|
if (this._conn?.readyState === WebSocket.OPEN) {
|
|
@@ -22866,8 +22879,11 @@ var WeaveStoreAzureWebPubSubSyncHost = class extends Emittery {
|
|
|
22866
22879
|
this._conn = null;
|
|
22867
22880
|
}
|
|
22868
22881
|
}
|
|
22882
|
+
simulateWebsocketError() {
|
|
22883
|
+
if (this._conn) this._conn.emit("error", new Error("Simulated connection failure"));
|
|
22884
|
+
}
|
|
22869
22885
|
broadcast(group, from$1, u8) {
|
|
22870
|
-
this._conn?.send(JSON.stringify({
|
|
22886
|
+
this._conn?.send?.(JSON.stringify({
|
|
22871
22887
|
type: MessageType.SendToGroup,
|
|
22872
22888
|
group,
|
|
22873
22889
|
noEcho: true,
|
|
@@ -22878,7 +22894,7 @@ var WeaveStoreAzureWebPubSubSyncHost = class extends Emittery {
|
|
|
22878
22894
|
}));
|
|
22879
22895
|
}
|
|
22880
22896
|
send(group, to, u8) {
|
|
22881
|
-
this._conn?.send(JSON.stringify({
|
|
22897
|
+
this._conn?.send?.(JSON.stringify({
|
|
22882
22898
|
type: MessageType.SendToGroup,
|
|
22883
22899
|
group,
|
|
22884
22900
|
noEcho: true,
|
|
@@ -22950,22 +22966,22 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
22950
22966
|
handleConnect: (req, res) => {
|
|
22951
22967
|
res.success();
|
|
22952
22968
|
this.syncOptions?.onConnect?.(req.context.connectionId, req.queries);
|
|
22953
|
-
this.
|
|
22969
|
+
this.server.emitEvent("onConnect", {
|
|
22954
22970
|
context: req.context,
|
|
22955
22971
|
queries: req.queries
|
|
22956
22972
|
});
|
|
22957
22973
|
},
|
|
22958
22974
|
onConnected: (req) => {
|
|
22959
22975
|
this.syncOptions?.onConnected?.(req.context.connectionId);
|
|
22960
|
-
this.
|
|
22976
|
+
this.server.emitEvent("onConnected", { context: req.context });
|
|
22961
22977
|
},
|
|
22962
22978
|
onDisconnected: (req) => {
|
|
22963
22979
|
this.handleConnectionDisconnection(req.context.connectionId);
|
|
22964
|
-
this.
|
|
22980
|
+
this.server.emitEvent("onDisconnected", { context: req.context });
|
|
22965
22981
|
}
|
|
22966
22982
|
});
|
|
22967
22983
|
this.syncOptions = syncHandlerOptions;
|
|
22968
|
-
this.
|
|
22984
|
+
this.server = server;
|
|
22969
22985
|
this.initialState = initialState;
|
|
22970
22986
|
this._client = client;
|
|
22971
22987
|
}
|
|
@@ -22973,15 +22989,15 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
22973
22989
|
return new Y$1.Doc();
|
|
22974
22990
|
}
|
|
22975
22991
|
async setupRoomInstance(roomId) {
|
|
22976
|
-
|
|
22992
|
+
this._rooms.set(roomId, this.getNewYDoc());
|
|
22993
|
+
const doc = this._rooms.get(roomId);
|
|
22977
22994
|
let documentData = void 0;
|
|
22978
|
-
if (this.
|
|
22995
|
+
if (this.server?.fetchRoom) documentData = await this.server.fetchRoom(roomId);
|
|
22979
22996
|
if (documentData) Y$1.applyUpdate(doc, documentData);
|
|
22980
22997
|
else this.initialState(doc);
|
|
22981
|
-
|
|
22982
|
-
connection.
|
|
22983
|
-
|
|
22984
|
-
this._roomsSyncHost.set(roomId, connection);
|
|
22998
|
+
this._roomsSyncHost.set(roomId, new WeaveStoreAzureWebPubSubSyncHost(this.server, this._client, roomId, doc));
|
|
22999
|
+
const connection = this._roomsSyncHost.get(roomId);
|
|
23000
|
+
await connection.start();
|
|
22985
23001
|
await this.setupRoomInstancePersistence(roomId);
|
|
22986
23002
|
}
|
|
22987
23003
|
async persistRoomTask(roomId) {
|
|
@@ -22989,7 +23005,7 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
22989
23005
|
const doc = this._rooms.get(roomId);
|
|
22990
23006
|
if (!doc) return;
|
|
22991
23007
|
const actualState = Y$1.encodeStateAsUpdate(doc);
|
|
22992
|
-
if (this.
|
|
23008
|
+
if (this.server?.persistRoom) await this.server.persistRoom(roomId, actualState);
|
|
22993
23009
|
} catch (ex) {
|
|
22994
23010
|
console.error(ex);
|
|
22995
23011
|
}
|
|
@@ -23014,12 +23030,21 @@ var WeaveAzureWebPubsubSyncHandler = class extends WebPubSubEventHandler {
|
|
|
23014
23030
|
this._store_persistence.delete(roomId);
|
|
23015
23031
|
await this.persistRoomTask(roomId);
|
|
23016
23032
|
const syncHost = this._roomsSyncHost.get(roomId);
|
|
23017
|
-
if (syncHost)
|
|
23033
|
+
if (syncHost) {
|
|
23034
|
+
await syncHost.stop();
|
|
23035
|
+
this._roomsSyncHost.delete(roomId);
|
|
23036
|
+
}
|
|
23018
23037
|
this._rooms.delete(roomId);
|
|
23019
23038
|
}
|
|
23020
23039
|
async getHostConnection(roomId) {
|
|
23021
23040
|
if (!this._rooms.has(roomId)) await this.setupRoomInstance(roomId);
|
|
23022
23041
|
}
|
|
23042
|
+
getRoomsLoaded() {
|
|
23043
|
+
return Array.from(this._rooms.keys());
|
|
23044
|
+
}
|
|
23045
|
+
getRoomSyncHost(roomId) {
|
|
23046
|
+
return this._roomsSyncHost.get(roomId);
|
|
23047
|
+
}
|
|
23023
23048
|
async clientConnect(roomId) {
|
|
23024
23049
|
try {
|
|
23025
23050
|
await this.getHostConnection(roomId);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inditextech/weave-store-azure-web-pubsub",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.69.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Jesus Manuel Piñeiro Cid <jesusmpc@inditex.com>",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"yjs": "13.6.27"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@inditextech/weave-sdk": "0.
|
|
64
|
+
"@inditextech/weave-sdk": "0.69.0",
|
|
65
65
|
"@koa/cors": "^5.0.0",
|
|
66
66
|
"@types/express": "^5.0.1",
|
|
67
67
|
"@types/ioredis": "^4.28.10",
|