@shaxpir/duiduidui-models 1.4.7 → 1.4.9

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);
@@ -1,4 +1,5 @@
1
1
  import { Doc } from '@shaxpir/sharedb/lib/client';
2
+ import { MultiTime } from '@shaxpir/shaxpir-common';
2
3
  import { ShareSync } from '../repo';
3
4
  import { Content, ContentBody, ContentId, ContentMeta } from "./Content";
4
5
  export interface BillingPayload {
@@ -10,7 +11,7 @@ export interface BillingBody extends ContentBody {
10
11
  }
11
12
  export declare class Billing extends Content {
12
13
  static makeBillingId(userId: ContentId): ContentId;
13
- static create(userId: ContentId, payload: BillingPayload): Billing;
14
+ static create(userId: ContentId, payload: BillingPayload, createdAt?: MultiTime): Billing;
14
15
  constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
15
16
  get payload(): BillingPayload;
16
17
  }
@@ -9,8 +9,10 @@ class Billing extends Content_1.Content {
9
9
  static makeBillingId(userId) {
10
10
  return shaxpir_common_1.CachingHasher.makeMd5Base62Hash(userId + "-" + ContentKind_1.ContentKind.BILLING);
11
11
  }
12
- static create(userId, payload) {
12
+ static create(userId, payload, createdAt) {
13
13
  const now = shaxpir_common_1.MultiClock.now();
14
+ // If the createdAt time is not provided, use the current time
15
+ createdAt ??= now;
14
16
  const billingId = Billing.makeBillingId(userId);
15
17
  return repo_1.ShareSyncFactory.get().createContent({
16
18
  meta: {
@@ -18,8 +20,8 @@ class Billing extends Content_1.Content {
18
20
  kind: ContentKind_1.ContentKind.BILLING,
19
21
  id: billingId,
20
22
  owner: userId,
21
- created_at: now,
22
- updated_at: now
23
+ created_at: createdAt,
24
+ updated_at: createdAt
23
25
  },
24
26
  payload
25
27
  });
@@ -34,7 +34,7 @@ export interface ManifestPayload {
34
34
  export declare class Manifest extends Model {
35
35
  constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
36
36
  static makeManifestId(userId: ContentId): ContentId;
37
- static create(userId: ContentId): Manifest;
37
+ static create(userId: ContentId, createdAt?: SingleTime): Manifest;
38
38
  get meta(): ManifestMeta;
39
39
  get owner(): ContentId;
40
40
  canUserView(userId: ContentId): boolean;
@@ -13,16 +13,16 @@ class Manifest extends Model_1.Model {
13
13
  static makeManifestId(userId) {
14
14
  return shaxpir_common_1.CachingHasher.makeMd5Base62Hash(userId + "-" + ContentKind_1.ContentKind.MANIFEST);
15
15
  }
16
- static create(userId) {
16
+ static create(userId, createdAt) {
17
17
  const now = shaxpir_common_1.Time.utc();
18
+ // If the createdAt time is not provided, use the current time
19
+ const createdTime = createdAt ?? { utc_time: now };
18
20
  const manifestId = Manifest.makeManifestId(userId);
19
21
  return repo_1.ShareSyncFactory.get().createManifest({
20
22
  meta: {
21
23
  kind: ContentKind_1.ContentKind.MANIFEST,
22
24
  ref: manifestId,
23
- updated_at: {
24
- utc_time: now
25
- },
25
+ updated_at: createdTime,
26
26
  owner: userId
27
27
  },
28
28
  payload: {
@@ -28,7 +28,7 @@ class User extends Content_1.Content {
28
28
  id: userId,
29
29
  owner: userId,
30
30
  created_at: createdAt,
31
- updated_at: now
31
+ updated_at: createdAt
32
32
  },
33
33
  payload: payload
34
34
  });
@@ -28,7 +28,7 @@ export declare class Workspace extends Content {
28
28
  get payload(): WorkspacePayload;
29
29
  getGlobalTags(): TagFilterConfig;
30
30
  static makeWorkspaceId(userId: ContentId): ContentId;
31
- static create(userId: ContentId, deviceId: ContentId): Workspace;
31
+ static create(userId: ContentId, payload: WorkspacePayload): Workspace;
32
32
  get sessions(): ArrayView<MultiTime>;
33
33
  get devices(): ArrayView<ContentId>;
34
34
  acquireSessionsFromDate(sessionId: ContentId, localDate: CompactDate): Promise<Session[]>;
@@ -24,7 +24,7 @@ class Workspace extends Content_1.Content {
24
24
  static makeWorkspaceId(userId) {
25
25
  return shaxpir_common_1.CachingHasher.makeMd5Base62Hash(userId + "-" + ContentKind_1.ContentKind.WORKSPACE);
26
26
  }
27
- static create(userId, deviceId) {
27
+ static create(userId, payload) {
28
28
  const now = shaxpir_common_1.MultiClock.now();
29
29
  const workspaceId = Workspace.makeWorkspaceId(userId);
30
30
  return repo_1.ShareSyncFactory.get().createContent({
@@ -36,12 +36,7 @@ class Workspace extends Content_1.Content {
36
36
  created_at: now,
37
37
  updated_at: now
38
38
  },
39
- payload: {
40
- devices: [deviceId],
41
- journey: {},
42
- global_tags: {},
43
- sessions: []
44
- }
39
+ payload
45
40
  });
46
41
  }
47
42
  get sessions() {
@@ -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,11 @@ 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 initializeDurableStore;
44
50
  isDebug(): boolean;
45
51
  isOnline(): boolean;
46
52
  ping(): void;
@@ -38,6 +38,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.ShareSync = exports.ShareSyncFactory = exports.ShareSyncDisposalStrategy = void 0;
40
40
  const client_1 = __importStar(require("@shaxpir/sharedb/lib/client"));
41
+ const durable_store_1 = __importDefault(require("@shaxpir/sharedb/lib/client/durable-store"));
41
42
  const shaxpir_common_1 = require("@shaxpir/shaxpir-common");
42
43
  const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket"));
43
44
  const models_1 = require("../models");
@@ -78,9 +79,10 @@ class ShareSync {
78
79
  shareSync._debug = options.debug;
79
80
  shareSync._disposalStrategy = options.disposalStrategy;
80
81
  shareSync._opErrorCallback = options.opErrorCallback;
81
- shareSync._hasDurableStore = options.namespace && options.namespace.length > 0;
82
+ shareSync._hasDurableStore = options.enableDurableStore === true && options.namespace && options.namespace.length > 0;
82
83
  shareSync._useDurableStoreEncryption = options.encryptionKey && options.encryptionKey.length > 0;
83
84
  shareSync._durableStoreEncryptionKey = options.encryptionKey;
85
+ shareSync._storageFactory = options.storageFactory;
84
86
  // Create and configure the socket.
85
87
  if (options.webSocketConstructor) {
86
88
  shareSync._socket = new reconnecting_websocket_1.default(options.urlProvider, [], { WebSocket: options.webSocketConstructor });
@@ -109,39 +111,21 @@ class ShareSync {
109
111
  updateSocketIsOpen();
110
112
  shareSync._listener.onSocketEvent('error', event);
111
113
  });
112
- // Create the ShareDB connection from the socket
114
+ // Create the ShareDB connection from the socket (without DurableStore initially)
115
+ shareSync._connection = new client_1.Connection(shareSync._socket);
116
+ // Initialize DurableStore asynchronously if enabled
113
117
  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
- }
118
+ shareSync.initializeDurableStore(options).catch((error) => {
119
+ console.error('Failed to initialize DurableStore:', error);
120
+ shareSync._opErrorCallback && shareSync._opErrorCallback(error);
141
121
  });
142
122
  }
143
- else {
144
- shareSync._connection = new client_1.Connection(shareSync._socket);
123
+ else if (options.onReadyCallback) {
124
+ // If there is no DurableStore, then we can call the onReadyCallback now. However,
125
+ // we have to use setTimeout to invoke it, because the callback will probably want
126
+ // to retrieve the ShareSync instance from the factory, and because the constructor
127
+ // hasn't actually finished yet, the INSTANCE pointer hasn't quite been assigned yet.
128
+ setTimeout(options.onReadyCallback, 1);
145
129
  }
146
130
  shareSync._connection.on('error', (err) => {
147
131
  console.error(`ShareSync connection error: ${err}\n Error code: ${err.code}\n Error message: ${err.message}\n Error stack: ${err.stack}\n`);
@@ -151,12 +135,43 @@ class ShareSync {
151
135
  if (options.pingInterval && typeof (options.pingInterval) === 'number') {
152
136
  setInterval(() => { shareSync.ping(); }, options.pingInterval);
153
137
  }
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);
138
+ }
139
+ async initializeDurableStore(options) {
140
+ try {
141
+ // Create storage implementation if factory is provided
142
+ if (this._storageFactory) {
143
+ this._storage = await this._storageFactory();
144
+ }
145
+ // Create DurableStore with the storage
146
+ if (this._storage) {
147
+ this._durableStore = new durable_store_1.default(this._storage, {
148
+ debug: this._debug,
149
+ // Version decoder for determining document freshness
150
+ // Uses the 'docData.meta.updated_at.utc_time' field (universal to all Model subclasses)
151
+ extVersionDecoder: (docData) => {
152
+ if (docData &&
153
+ docData.meta &&
154
+ docData.meta.updated_at &&
155
+ docData.meta.updated_at.utc_time) {
156
+ return docData.meta.updated_at.utc_time;
157
+ }
158
+ return null;
159
+ }
160
+ });
161
+ // Attach DurableStore to connection
162
+ this._connection.useDurableStore(this._durableStore);
163
+ // Call ready callback once DurableStore is initialized
164
+ if (options.onReadyCallback) {
165
+ options.onReadyCallback();
166
+ }
167
+ }
168
+ else {
169
+ console.warn('DurableStore enabled but no storage factory provided');
170
+ }
171
+ }
172
+ catch (error) {
173
+ console.error('Failed to initialize DurableStore:', error);
174
+ throw error;
160
175
  }
161
176
  }
162
177
  isDebug() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.4.7",
3
+ "version": "1.4.9",
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",