@liveblocks/client 0.15.0-alpha.2 → 0.15.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 (62) hide show
  1. package/lib/esm/index.js +2991 -5
  2. package/lib/esm/index.mjs +2991 -0
  3. package/lib/esm/internal.js +149 -0
  4. package/lib/esm/internal.mjs +149 -0
  5. package/lib/{esm/types.d.ts → index.d.ts} +255 -50
  6. package/lib/index.js +4237 -0
  7. package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
  8. package/lib/internal.js +193 -0
  9. package/package.json +36 -9
  10. package/lib/cjs/AbstractCrdt.d.ts +0 -67
  11. package/lib/cjs/AbstractCrdt.js +0 -95
  12. package/lib/cjs/LiveList.d.ts +0 -144
  13. package/lib/cjs/LiveList.js +0 -527
  14. package/lib/cjs/LiveMap.d.ts +0 -91
  15. package/lib/cjs/LiveMap.js +0 -322
  16. package/lib/cjs/LiveObject.d.ts +0 -80
  17. package/lib/cjs/LiveObject.js +0 -453
  18. package/lib/cjs/LiveRegister.d.ts +0 -29
  19. package/lib/cjs/LiveRegister.js +0 -88
  20. package/lib/cjs/authentication.d.ts +0 -3
  21. package/lib/cjs/authentication.js +0 -71
  22. package/lib/cjs/client.d.ts +0 -27
  23. package/lib/cjs/client.js +0 -80
  24. package/lib/cjs/immutable.d.ts +0 -9
  25. package/lib/cjs/immutable.js +0 -291
  26. package/lib/cjs/index.d.ts +0 -6
  27. package/lib/cjs/index.js +0 -18
  28. package/lib/cjs/live.d.ts +0 -181
  29. package/lib/cjs/live.js +0 -49
  30. package/lib/cjs/position.d.ts +0 -6
  31. package/lib/cjs/position.js +0 -113
  32. package/lib/cjs/room.d.ts +0 -159
  33. package/lib/cjs/room.js +0 -1094
  34. package/lib/cjs/types.d.ts +0 -489
  35. package/lib/cjs/types.js +0 -2
  36. package/lib/cjs/utils.d.ts +0 -11
  37. package/lib/cjs/utils.js +0 -175
  38. package/lib/esm/AbstractCrdt.d.ts +0 -67
  39. package/lib/esm/AbstractCrdt.js +0 -91
  40. package/lib/esm/LiveList.d.ts +0 -144
  41. package/lib/esm/LiveList.js +0 -523
  42. package/lib/esm/LiveMap.d.ts +0 -91
  43. package/lib/esm/LiveMap.js +0 -318
  44. package/lib/esm/LiveObject.d.ts +0 -80
  45. package/lib/esm/LiveObject.js +0 -449
  46. package/lib/esm/LiveRegister.d.ts +0 -29
  47. package/lib/esm/LiveRegister.js +0 -84
  48. package/lib/esm/authentication.d.ts +0 -3
  49. package/lib/esm/authentication.js +0 -66
  50. package/lib/esm/client.d.ts +0 -27
  51. package/lib/esm/client.js +0 -76
  52. package/lib/esm/immutable.d.ts +0 -9
  53. package/lib/esm/immutable.js +0 -282
  54. package/lib/esm/index.d.ts +0 -6
  55. package/lib/esm/live.js +0 -46
  56. package/lib/esm/position.d.ts +0 -6
  57. package/lib/esm/position.js +0 -106
  58. package/lib/esm/room.d.ts +0 -159
  59. package/lib/esm/room.js +0 -1069
  60. package/lib/esm/types.js +0 -1
  61. package/lib/esm/utils.d.ts +0 -11
  62. package/lib/esm/utils.js +0 -164
@@ -1,453 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- 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");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _LiveObject_instances, _LiveObject_map, _LiveObject_propToLastUpdate, _LiveObject_applyUpdate, _LiveObject_applyDeleteObjectKey;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.LiveObject = void 0;
16
- const AbstractCrdt_1 = require("./AbstractCrdt");
17
- const utils_1 = require("./utils");
18
- const live_1 = require("./live");
19
- /**
20
- * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
21
- * Keys should be a string, and values should be serializable to JSON.
22
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
23
- */
24
- class LiveObject extends AbstractCrdt_1.AbstractCrdt {
25
- constructor(object = {}) {
26
- super();
27
- _LiveObject_instances.add(this);
28
- _LiveObject_map.set(this, void 0);
29
- _LiveObject_propToLastUpdate.set(this, new Map());
30
- for (const key in object) {
31
- const value = object[key];
32
- if (value instanceof AbstractCrdt_1.AbstractCrdt) {
33
- value._setParentLink(this, key);
34
- }
35
- }
36
- __classPrivateFieldSet(this, _LiveObject_map, new Map(Object.entries(object)), "f");
37
- }
38
- /**
39
- * INTERNAL
40
- */
41
- _serialize(parentId, parentKey, doc) {
42
- if (this._id == null) {
43
- throw new Error("Cannot serialize item is not attached");
44
- }
45
- const ops = [];
46
- const op = {
47
- id: this._id,
48
- opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
49
- type: live_1.OpType.CreateObject,
50
- parentId,
51
- parentKey,
52
- data: {},
53
- };
54
- ops.push(op);
55
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
56
- if (value instanceof AbstractCrdt_1.AbstractCrdt) {
57
- ops.push(...value._serialize(this._id, key, doc));
58
- }
59
- else {
60
- op.data[key] = value;
61
- }
62
- }
63
- return ops;
64
- }
65
- /**
66
- * INTERNAL
67
- */
68
- static _deserialize([id, item], parentToChildren, doc) {
69
- if (item.type !== live_1.CrdtType.Object) {
70
- throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
71
- }
72
- const object = new LiveObject(item.data);
73
- object._attach(id, doc);
74
- return this._deserializeChildren(object, parentToChildren, doc);
75
- }
76
- /**
77
- * INTERNAL
78
- */
79
- static _deserializeChildren(object, parentToChildren, doc) {
80
- const children = parentToChildren.get(object._id);
81
- if (children == null) {
82
- return object;
83
- }
84
- for (const entry of children) {
85
- const crdt = entry[1];
86
- if (crdt.parentKey == null) {
87
- throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
88
- }
89
- const child = (0, utils_1.deserialize)(entry, parentToChildren, doc);
90
- child._setParentLink(object, crdt.parentKey);
91
- __classPrivateFieldGet(object, _LiveObject_map, "f").set(crdt.parentKey, child);
92
- }
93
- return object;
94
- }
95
- /**
96
- * INTERNAL
97
- */
98
- _attach(id, doc) {
99
- super._attach(id, doc);
100
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
101
- if (value instanceof AbstractCrdt_1.AbstractCrdt) {
102
- value._attach(doc.generateId(), doc);
103
- }
104
- }
105
- }
106
- /**
107
- * INTERNAL
108
- */
109
- _attachChild(id, key, child, isLocal) {
110
- if (this._doc == null) {
111
- throw new Error("Can't attach child if doc is not present");
112
- }
113
- const previousValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
114
- let reverse;
115
- if ((0, utils_1.isCrdt)(previousValue)) {
116
- reverse = previousValue._serialize(this._id, key);
117
- previousValue._detach();
118
- }
119
- else if (previousValue === undefined) {
120
- reverse = [
121
- { type: live_1.OpType.DeleteObjectKey, id: this._id, key: key },
122
- ];
123
- }
124
- else {
125
- reverse = [
126
- {
127
- type: live_1.OpType.UpdateObject,
128
- id: this._id,
129
- data: { [key]: previousValue },
130
- },
131
- ];
132
- }
133
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, child);
134
- child._setParentLink(this, key);
135
- child._attach(id, this._doc);
136
- return {
137
- reverse,
138
- modified: {
139
- node: this,
140
- type: "LiveObject",
141
- updates: { [key]: { type: "update" } },
142
- },
143
- };
144
- }
145
- /**
146
- * INTERNAL
147
- */
148
- _detachChild(child) {
149
- if (child) {
150
- const reverse = child._serialize(this._id, child._parentKey, this._doc);
151
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
152
- if (value === child) {
153
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
154
- }
155
- }
156
- child._detach();
157
- const storageUpdate = {
158
- node: this,
159
- type: "LiveObject",
160
- updates: {
161
- [child._parentKey]: { type: "delete" },
162
- },
163
- };
164
- return { modified: storageUpdate, reverse };
165
- }
166
- return { modified: false };
167
- }
168
- /**
169
- * INTERNAL
170
- */
171
- _detachChildren() {
172
- for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
173
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
174
- value._detach();
175
- }
176
- }
177
- /**
178
- * INTERNAL
179
- */
180
- _detach() {
181
- super._detach();
182
- for (const value of __classPrivateFieldGet(this, _LiveObject_map, "f").values()) {
183
- if ((0, utils_1.isCrdt)(value)) {
184
- value._detach();
185
- }
186
- }
187
- }
188
- /**
189
- * INTERNAL
190
- */
191
- _apply(op, isLocal) {
192
- if (op.type === live_1.OpType.UpdateObject) {
193
- return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyUpdate).call(this, op, isLocal);
194
- }
195
- else if (op.type === live_1.OpType.DeleteObjectKey) {
196
- return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyDeleteObjectKey).call(this, op);
197
- }
198
- return super._apply(op, isLocal);
199
- }
200
- /**
201
- * INTERNAL
202
- */
203
- _toSerializedCrdt() {
204
- var _a;
205
- return {
206
- type: live_1.CrdtType.Object,
207
- parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
208
- parentKey: this._parentKey,
209
- data: this.toObject(),
210
- };
211
- }
212
- /**
213
- * INTERNAL
214
- */
215
- _getType() {
216
- return "LiveObject";
217
- }
218
- /**
219
- * Transform the LiveObject into a javascript object
220
- */
221
- toObject() {
222
- return Object.fromEntries(__classPrivateFieldGet(this, _LiveObject_map, "f"));
223
- }
224
- /**
225
- * Adds or updates a property with a specified key and a value.
226
- * @param key The key of the property to add
227
- * @param value The value of the property to add
228
- */
229
- set(key, value) {
230
- // TODO: Find out why typescript complains
231
- this.update({ [key]: value });
232
- }
233
- /**
234
- * Returns a specified property from the LiveObject.
235
- * @param key The key of the property to get
236
- */
237
- get(key) {
238
- return __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
239
- }
240
- /**
241
- * Deletes a key from the LiveObject
242
- * @param key The key of the property to delete
243
- */
244
- delete(key) {
245
- const keyAsString = key;
246
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(keyAsString);
247
- if (oldValue === undefined) {
248
- return;
249
- }
250
- if (this._doc == null || this._id == null) {
251
- if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
252
- oldValue._detach();
253
- }
254
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
255
- return;
256
- }
257
- let reverse;
258
- if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
259
- oldValue._detach();
260
- reverse = oldValue._serialize(this._id, keyAsString);
261
- }
262
- else {
263
- reverse = [
264
- {
265
- type: live_1.OpType.UpdateObject,
266
- data: { [keyAsString]: oldValue },
267
- id: this._id,
268
- },
269
- ];
270
- }
271
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
272
- const storageUpdates = new Map();
273
- storageUpdates.set(this._id, {
274
- node: this,
275
- type: "LiveObject",
276
- updates: { [key]: { type: "delete" } },
277
- });
278
- this._doc.dispatch([
279
- {
280
- type: live_1.OpType.DeleteObjectKey,
281
- key: keyAsString,
282
- id: this._id,
283
- opId: this._doc.generateOpId(),
284
- },
285
- ], reverse, storageUpdates);
286
- }
287
- /**
288
- * Adds or updates multiple properties at once with an object.
289
- * @param overrides The object used to overrides properties
290
- */
291
- update(overrides) {
292
- if (this._doc == null || this._id == null) {
293
- for (const key in overrides) {
294
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
295
- if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
296
- oldValue._detach();
297
- }
298
- const newValue = overrides[key];
299
- if (newValue instanceof AbstractCrdt_1.AbstractCrdt) {
300
- newValue._setParentLink(this, key);
301
- }
302
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
303
- }
304
- return;
305
- }
306
- const ops = [];
307
- const reverseOps = [];
308
- const opId = this._doc.generateOpId();
309
- const updatedProps = {};
310
- const reverseUpdateOp = {
311
- id: this._id,
312
- type: live_1.OpType.UpdateObject,
313
- data: {},
314
- };
315
- const updateDelta = {};
316
- for (const key in overrides) {
317
- __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, opId);
318
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
319
- if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
320
- reverseOps.push(...oldValue._serialize(this._id, key));
321
- oldValue._detach();
322
- }
323
- else if (oldValue === undefined) {
324
- reverseOps.push({ type: live_1.OpType.DeleteObjectKey, id: this._id, key });
325
- }
326
- else {
327
- reverseUpdateOp.data[key] = oldValue;
328
- }
329
- const newValue = overrides[key];
330
- if (newValue instanceof AbstractCrdt_1.AbstractCrdt) {
331
- newValue._setParentLink(this, key);
332
- newValue._attach(this._doc.generateId(), this._doc);
333
- ops.push(...newValue._serialize(this._id, key, this._doc));
334
- }
335
- else {
336
- updatedProps[key] = newValue;
337
- }
338
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
339
- updateDelta[key] = { type: "update" };
340
- }
341
- if (Object.keys(reverseUpdateOp.data).length !== 0) {
342
- reverseOps.unshift(reverseUpdateOp);
343
- }
344
- if (Object.keys(updatedProps).length !== 0) {
345
- ops.unshift({
346
- opId,
347
- id: this._id,
348
- type: live_1.OpType.UpdateObject,
349
- data: updatedProps,
350
- });
351
- }
352
- const storageUpdates = new Map();
353
- storageUpdates.set(this._id, {
354
- node: this,
355
- type: "LiveObject",
356
- updates: updateDelta,
357
- });
358
- this._doc.dispatch(ops, reverseOps, storageUpdates);
359
- }
360
- }
361
- exports.LiveObject = LiveObject;
362
- _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyUpdate = function _LiveObject_applyUpdate(op, isLocal) {
363
- let isModified = false;
364
- const reverse = [];
365
- const reverseUpdate = {
366
- type: live_1.OpType.UpdateObject,
367
- id: this._id,
368
- data: {},
369
- };
370
- reverse.push(reverseUpdate);
371
- for (const key in op.data) {
372
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
373
- if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
374
- reverse.push(...oldValue._serialize(this._id, key));
375
- oldValue._detach();
376
- }
377
- else if (oldValue !== undefined) {
378
- reverseUpdate.data[key] = oldValue;
379
- }
380
- else if (oldValue === undefined) {
381
- reverse.push({ type: live_1.OpType.DeleteObjectKey, id: this._id, key });
382
- }
383
- }
384
- let updateDelta = {};
385
- for (const key in op.data) {
386
- if (isLocal) {
387
- __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, op.opId);
388
- }
389
- else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) == null) {
390
- // Not modified localy so we apply update
391
- isModified = true;
392
- }
393
- else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === op.opId) {
394
- // Acknowlegment from local operation
395
- __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
396
- continue;
397
- }
398
- else {
399
- // Conflict, ignore remote operation
400
- continue;
401
- }
402
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
403
- if ((0, utils_1.isCrdt)(oldValue)) {
404
- oldValue._detach();
405
- }
406
- isModified = true;
407
- updateDelta[key] = { type: "update" };
408
- __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, op.data[key]);
409
- }
410
- if (Object.keys(reverseUpdate.data).length !== 0) {
411
- reverse.unshift(reverseUpdate);
412
- }
413
- return isModified
414
- ? {
415
- modified: {
416
- node: this,
417
- type: "LiveObject",
418
- updates: updateDelta,
419
- },
420
- reverse,
421
- }
422
- : { modified: false };
423
- }, _LiveObject_applyDeleteObjectKey = function _LiveObject_applyDeleteObjectKey(op) {
424
- const key = op.key;
425
- // If property does not exist, exit without notifying
426
- if (__classPrivateFieldGet(this, _LiveObject_map, "f").has(key) === false) {
427
- return { modified: false };
428
- }
429
- const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
430
- let reverse = [];
431
- if ((0, utils_1.isCrdt)(oldValue)) {
432
- reverse = oldValue._serialize(this._id, op.key);
433
- oldValue._detach();
434
- }
435
- else if (oldValue !== undefined) {
436
- reverse = [
437
- {
438
- type: live_1.OpType.UpdateObject,
439
- id: this._id,
440
- data: { [key]: oldValue },
441
- },
442
- ];
443
- }
444
- __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
445
- return {
446
- modified: {
447
- node: this,
448
- type: "LiveObject",
449
- updates: { [op.key]: { type: "delete" } },
450
- },
451
- reverse,
452
- };
453
- };
@@ -1,29 +0,0 @@
1
- import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { SerializedCrdtWithId, Op, SerializedCrdt } from "./live";
3
- /**
4
- * INTERNAL
5
- */
6
- export declare class LiveRegister<TValue = any> extends AbstractCrdt {
7
- #private;
8
- constructor(data: TValue);
9
- get data(): TValue;
10
- /**
11
- * INTERNAL
12
- */
13
- static _deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveRegister<any>;
14
- /**
15
- * INTERNAL
16
- */
17
- _serialize(parentId: string, parentKey: string, doc?: Doc): Op[];
18
- /**
19
- * INTERNAL
20
- */
21
- _toSerializedCrdt(): SerializedCrdt;
22
- _attachChild(id: string, key: string, crdt: AbstractCrdt, isLocal: boolean): ApplyResult;
23
- _detachChild(crdt: AbstractCrdt): ApplyResult;
24
- _apply(op: Op, isLocal: boolean): ApplyResult;
25
- /**
26
- * INTERNAL
27
- */
28
- _getType(): string;
29
- }
@@ -1,88 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- 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");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _LiveRegister_data;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.LiveRegister = void 0;
16
- const AbstractCrdt_1 = require("./AbstractCrdt");
17
- const live_1 = require("./live");
18
- /**
19
- * INTERNAL
20
- */
21
- class LiveRegister extends AbstractCrdt_1.AbstractCrdt {
22
- constructor(data) {
23
- super();
24
- _LiveRegister_data.set(this, void 0);
25
- __classPrivateFieldSet(this, _LiveRegister_data, data, "f");
26
- }
27
- get data() {
28
- return __classPrivateFieldGet(this, _LiveRegister_data, "f");
29
- }
30
- /**
31
- * INTERNAL
32
- */
33
- static _deserialize([id, item], parentToChildren, doc) {
34
- if (item.type !== live_1.CrdtType.Register) {
35
- throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
36
- }
37
- const register = new LiveRegister(item.data);
38
- register._attach(id, doc);
39
- return register;
40
- }
41
- /**
42
- * INTERNAL
43
- */
44
- _serialize(parentId, parentKey, doc) {
45
- if (this._id == null || parentId == null || parentKey == null) {
46
- throw new Error("Cannot serialize register if parentId or parentKey is undefined");
47
- }
48
- return [
49
- {
50
- type: live_1.OpType.CreateRegister,
51
- opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
52
- id: this._id,
53
- parentId,
54
- parentKey,
55
- data: this.data,
56
- },
57
- ];
58
- }
59
- /**
60
- * INTERNAL
61
- */
62
- _toSerializedCrdt() {
63
- var _a;
64
- return {
65
- type: live_1.CrdtType.Register,
66
- parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
67
- parentKey: this._parentKey,
68
- data: this.data,
69
- };
70
- }
71
- _attachChild(id, key, crdt, isLocal) {
72
- throw new Error("Method not implemented.");
73
- }
74
- _detachChild(crdt) {
75
- throw new Error("Method not implemented.");
76
- }
77
- _apply(op, isLocal) {
78
- return super._apply(op, isLocal);
79
- }
80
- /**
81
- * INTERNAL
82
- */
83
- _getType() {
84
- return "LiveRegister";
85
- }
86
- }
87
- exports.LiveRegister = LiveRegister;
88
- _LiveRegister_data = new WeakMap();
@@ -1,3 +0,0 @@
1
- import { AuthEndpoint, AuthenticationToken } from "./types";
2
- export default function auth(endpoint: AuthEndpoint, room: string, publicApiKey?: string): Promise<string>;
3
- export declare function parseToken(token: string): AuthenticationToken;
@@ -1,71 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.parseToken = void 0;
13
- function fetchAuthorize(endpoint, room, publicApiKey) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const res = yield fetch(endpoint, {
16
- method: "POST",
17
- headers: {
18
- "Content-Type": "application/json",
19
- },
20
- body: JSON.stringify({
21
- room,
22
- publicApiKey,
23
- }),
24
- });
25
- if (!res.ok) {
26
- throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`);
27
- }
28
- let authResponse = null;
29
- try {
30
- authResponse = yield res.json();
31
- }
32
- catch (er) {
33
- throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`);
34
- }
35
- if (typeof authResponse.token !== "string") {
36
- throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`);
37
- }
38
- return authResponse.token;
39
- });
40
- }
41
- function auth(endpoint, room, publicApiKey) {
42
- return __awaiter(this, void 0, void 0, function* () {
43
- if (typeof endpoint === "string") {
44
- return fetchAuthorize(endpoint, room, publicApiKey);
45
- }
46
- if (typeof endpoint === "function") {
47
- const { token } = yield endpoint(room);
48
- // TODO: Validation
49
- return token;
50
- }
51
- throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
52
- });
53
- }
54
- exports.default = auth;
55
- class AuthenticationError extends Error {
56
- constructor(message) {
57
- super(message);
58
- }
59
- }
60
- function parseToken(token) {
61
- const tokenParts = token.split(".");
62
- if (tokenParts.length !== 3) {
63
- throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`);
64
- }
65
- const data = JSON.parse(atob(tokenParts[1]));
66
- if (typeof data.actor !== "number") {
67
- throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`);
68
- }
69
- return data;
70
- }
71
- exports.parseToken = parseToken;
@@ -1,27 +0,0 @@
1
- import { ClientOptions, Client } from "./types";
2
- /**
3
- * Create a client that will be responsible to communicate with liveblocks servers.
4
- *
5
- * @example
6
- * const client = createClient({
7
- * authEndpoint: "/api/auth"
8
- * });
9
- *
10
- * // It's also possible to use a function to call your authentication endpoint.
11
- * // Useful to add additional headers or use an API wrapper (like Firebase functions)
12
- * const client = createClient({
13
- * authEndpoint: async (room) => {
14
- * const response = await fetch("/api/auth", {
15
- * method: "POST",
16
- * headers: {
17
- * Authentication: "token",
18
- * "Content-Type": "application/json"
19
- * },
20
- * body: JSON.stringify({ room })
21
- * });
22
- *
23
- * return await response.json();
24
- * }
25
- * });
26
- */
27
- export declare function createClient(options: ClientOptions): Client;