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