@shaxpir/duiduidui-models 1.4.8 → 1.4.10

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.js CHANGED
@@ -15,7 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- /// <reference path="../decs.d.ts" />
19
18
  __exportStar(require("./models"), exports);
20
19
  __exportStar(require("./repo"), exports);
21
20
  __exportStar(require("./util"), exports);
@@ -21,6 +21,8 @@ export interface ShareSyncOptions {
21
21
  pingInterval?: number;
22
22
  encryptionKey?: string;
23
23
  webSocketConstructor?: any;
24
+ enableDurableStore?: boolean;
25
+ storageFactory?: () => Promise<any>;
24
26
  }
25
27
  export declare class ShareSyncFactory {
26
28
  private static INSTANCE;
@@ -40,7 +42,13 @@ export declare class ShareSync {
40
42
  private _socket;
41
43
  private _connection;
42
44
  private _listener;
45
+ private _storage;
46
+ private _durableStore;
47
+ private _storageFactory?;
43
48
  constructor(encryption: Encryption, options: ShareSyncOptions);
49
+ private setupConnectionEvents;
50
+ private createConnectionWithDurableStore;
51
+ private initializeDurableStore;
44
52
  isDebug(): boolean;
45
53
  isOnline(): boolean;
46
54
  ping(): void;
@@ -78,9 +78,10 @@ class ShareSync {
78
78
  shareSync._debug = options.debug;
79
79
  shareSync._disposalStrategy = options.disposalStrategy;
80
80
  shareSync._opErrorCallback = options.opErrorCallback;
81
- shareSync._hasDurableStore = options.namespace && options.namespace.length > 0;
81
+ shareSync._hasDurableStore = options.enableDurableStore === true && options.namespace && options.namespace.length > 0;
82
82
  shareSync._useDurableStoreEncryption = options.encryptionKey && options.encryptionKey.length > 0;
83
83
  shareSync._durableStoreEncryptionKey = options.encryptionKey;
84
+ shareSync._storageFactory = options.storageFactory;
84
85
  // Create and configure the socket.
85
86
  if (options.webSocketConstructor) {
86
87
  shareSync._socket = new reconnecting_websocket_1.default(options.urlProvider, [], { WebSocket: options.webSocketConstructor });
@@ -109,54 +110,71 @@ class ShareSync {
109
110
  updateSocketIsOpen();
110
111
  shareSync._listener.onSocketEvent('error', event);
111
112
  });
112
- // Create the ShareDB connection from the socket
113
+ // Initialize DurableStore first if enabled, then create connection with it
113
114
  if (shareSync._hasDurableStore) {
114
- shareSync._connection = new client_1.Connection(shareSync._socket, {
115
- durableStore: {
116
- namespace: options.namespace,
117
- debug: options.debug,
118
- useEncryption: shareSync._useDurableStoreEncryption,
119
- encryptionCallback: (plaintext) => {
120
- return encryption.encrypt(plaintext, shareSync._durableStoreEncryptionKey);
121
- },
122
- decryptionCallback: (cyphertext) => {
123
- return encryption.decrypt(cyphertext, shareSync._durableStoreEncryptionKey);
124
- },
125
- onReadyCallback: options.onReadyCallback,
126
- opErrorCallback: options.opErrorCallback,
127
- // Use the 'docData.meta.updated_at.utc_time' field (which is universal to all Model subclasses) as the 'version'
128
- // identifier that we use when determining if the doc in the DurableStore is up-to-date. This field won't yet
129
- // exist when the doc is newly created (since the 'doc.data' will be undefined), so in that case, we'll return
130
- // null as the version string, which will work correctly with the versioning semantics of the DurableStore.
131
- extVersionDecoder: (docData) => {
132
- if (docData &&
133
- docData.meta &&
134
- docData.meta.updated_at &&
135
- docData.meta.updated_at.utc_time) {
136
- return docData.meta.updated_at.utc_time;
137
- }
138
- return null;
139
- }
140
- }
115
+ shareSync.initializeDurableStore(options).then(() => {
116
+ // Create connection with DurableStore options
117
+ shareSync._connection = shareSync.createConnectionWithDurableStore(shareSync._socket, options);
118
+ shareSync.setupConnectionEvents(options);
119
+ }).catch((error) => {
120
+ console.error('Failed to initialize DurableStore:', error);
121
+ shareSync._opErrorCallback && shareSync._opErrorCallback(error);
141
122
  });
142
123
  }
143
124
  else {
125
+ // Create the ShareDB connection from the socket (without DurableStore)
144
126
  shareSync._connection = new client_1.Connection(shareSync._socket);
127
+ shareSync.setupConnectionEvents(options);
128
+ if (options.onReadyCallback) {
129
+ // If there is no DurableStore, then we can call the onReadyCallback now. However,
130
+ // we have to use setTimeout to invoke it, because the callback will probably want
131
+ // to retrieve the ShareSync instance from the factory, and because the constructor
132
+ // hasn't actually finished yet, the INSTANCE pointer hasn't quite been assigned yet.
133
+ setTimeout(options.onReadyCallback, 1);
134
+ }
145
135
  }
146
- shareSync._connection.on('error', (err) => {
136
+ }
137
+ setupConnectionEvents(options) {
138
+ this._connection.on('error', (err) => {
147
139
  console.error(`ShareSync connection error: ${err}\n Error code: ${err.code}\n Error message: ${err.message}\n Error stack: ${err.stack}\n`);
148
140
  });
149
141
  // If the client provided a pingInterval option, then set up a periodic ping the
150
142
  // connection alive. Clients who don't provide this option will send pings.
151
- if (options.pingInterval && typeof (options.pingInterval) === 'number') {
152
- setInterval(() => { shareSync.ping(); }, options.pingInterval);
143
+ if (options && options.pingInterval && typeof (options.pingInterval) === 'number') {
144
+ setInterval(() => { this.ping(); }, options.pingInterval);
153
145
  }
154
- if (!shareSync._hasDurableStore && !!options.onReadyCallback) {
155
- // If there is no DurableStore, then we can call the onReadyCallback now. However,
156
- // we have to use setTimeout to invoke it, because the callback will probably want
157
- // to retrieve the ShareSync instance from the factory, and because the constructor
158
- // hasn't actually finished yet, the INSTANCE pointer hasn't quite been assigned yet.
159
- setTimeout(options.onReadyCallback, 1);
146
+ }
147
+ createConnectionWithDurableStore(socket, options) {
148
+ return new client_1.Connection(socket, {
149
+ durableStore: {
150
+ storage: this._storage,
151
+ debug: this._debug,
152
+ extVersionDecoder: (docData) => {
153
+ if (docData &&
154
+ docData.meta &&
155
+ docData.meta.updated_at &&
156
+ docData.meta.updated_at.utc_time) {
157
+ return docData.meta.updated_at.utc_time;
158
+ }
159
+ return null;
160
+ },
161
+ onReadyCallback: options.onReadyCallback
162
+ }
163
+ });
164
+ }
165
+ async initializeDurableStore(options) {
166
+ try {
167
+ // Create storage implementation if factory is provided
168
+ if (this._storageFactory) {
169
+ this._storage = await this._storageFactory();
170
+ }
171
+ if (!this._storage) {
172
+ throw new Error('DurableStore enabled but no storage factory provided');
173
+ }
174
+ }
175
+ catch (error) {
176
+ console.error('Failed to initialize DurableStore:', error);
177
+ throw error;
160
178
  }
161
179
  }
162
180
  isDebug() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.4.8",
3
+ "version": "1.4.10",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"
@@ -16,7 +16,7 @@
16
16
  "dist/"
17
17
  ],
18
18
  "dependencies": {
19
- "@shaxpir/sharedb": "^5.3.0",
19
+ "@shaxpir/sharedb": "^6.0.3",
20
20
  "@shaxpir/shaxpir-common": "1.3.0",
21
21
  "ot-json1": "1.0.1",
22
22
  "ot-text-unicode": "4.0.0",