@liveblocks/node 2.13.3-emails1 → 2.14.0-v2encoding
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.d.mts +3 -21
- package/dist/index.d.ts +3 -21
- package/dist/index.js +113 -111
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +109 -107
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { detectDupes } from "@liveblocks/core";
|
|
|
3
3
|
|
|
4
4
|
// src/version.ts
|
|
5
5
|
var PKG_NAME = "@liveblocks/node";
|
|
6
|
-
var PKG_VERSION = "2.
|
|
6
|
+
var PKG_VERSION = "2.14.0-v2encoding";
|
|
7
7
|
var PKG_FORMAT = "esm";
|
|
8
8
|
|
|
9
9
|
// src/client.ts
|
|
@@ -91,35 +91,35 @@ var READ_ACCESS = Object.freeze([
|
|
|
91
91
|
var FULL_ACCESS = Object.freeze(["room:write", "comments:write"]);
|
|
92
92
|
var roomPatternRegex = /^([*]|[^*]{1,128}[*]?)$/;
|
|
93
93
|
var Session = class {
|
|
94
|
+
FULL_ACCESS = FULL_ACCESS;
|
|
95
|
+
READ_ACCESS = READ_ACCESS;
|
|
96
|
+
#postFn;
|
|
97
|
+
#userId;
|
|
98
|
+
#userInfo;
|
|
99
|
+
#sealed = false;
|
|
100
|
+
#permissions = /* @__PURE__ */ new Map();
|
|
94
101
|
/** @internal */
|
|
95
102
|
constructor(postFn, userId, userInfo) {
|
|
96
|
-
this.FULL_ACCESS = FULL_ACCESS;
|
|
97
|
-
this.READ_ACCESS = READ_ACCESS;
|
|
98
|
-
/** @internal */
|
|
99
|
-
this._sealed = false;
|
|
100
|
-
/** @internal */
|
|
101
|
-
this._permissions = /* @__PURE__ */ new Map();
|
|
102
103
|
assertNonEmpty(userId, "userId");
|
|
103
|
-
this
|
|
104
|
-
this
|
|
105
|
-
this
|
|
104
|
+
this.#postFn = postFn;
|
|
105
|
+
this.#userId = userId;
|
|
106
|
+
this.#userInfo = userInfo;
|
|
106
107
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (this._sealed) {
|
|
108
|
+
#getOrCreate(roomId) {
|
|
109
|
+
if (this.#sealed) {
|
|
110
110
|
throw new Error("You can no longer change these permissions.");
|
|
111
111
|
}
|
|
112
|
-
let perms = this.
|
|
112
|
+
let perms = this.#permissions.get(roomId);
|
|
113
113
|
if (perms) {
|
|
114
114
|
return perms;
|
|
115
115
|
} else {
|
|
116
|
-
if (this.
|
|
116
|
+
if (this.#permissions.size >= MAX_PERMS_PER_SET) {
|
|
117
117
|
throw new Error(
|
|
118
118
|
"You cannot add permissions for more than 10 rooms in a single token"
|
|
119
119
|
);
|
|
120
120
|
}
|
|
121
121
|
perms = /* @__PURE__ */ new Set();
|
|
122
|
-
this.
|
|
122
|
+
this.#permissions.set(roomId, perms);
|
|
123
123
|
return perms;
|
|
124
124
|
}
|
|
125
125
|
}
|
|
@@ -133,7 +133,7 @@ var Session = class {
|
|
|
133
133
|
if (newPerms.length === 0) {
|
|
134
134
|
throw new Error("Permission list cannot be empty");
|
|
135
135
|
}
|
|
136
|
-
const existingPerms = this
|
|
136
|
+
const existingPerms = this.#getOrCreate(roomIdOrPattern);
|
|
137
137
|
for (const perm of newPerms) {
|
|
138
138
|
if (!isPermission(perm)) {
|
|
139
139
|
throw new Error(`Not a valid permission: ${perm}`);
|
|
@@ -144,21 +144,21 @@ var Session = class {
|
|
|
144
144
|
}
|
|
145
145
|
/** @internal - For unit tests only */
|
|
146
146
|
hasPermissions() {
|
|
147
|
-
return this.
|
|
147
|
+
return this.#permissions.size > 0;
|
|
148
148
|
}
|
|
149
149
|
/** @internal - For unit tests only */
|
|
150
150
|
seal() {
|
|
151
|
-
if (this
|
|
151
|
+
if (this.#sealed) {
|
|
152
152
|
throw new Error(
|
|
153
153
|
"You cannot reuse Session instances. Please create a new session every time."
|
|
154
154
|
);
|
|
155
155
|
}
|
|
156
|
-
this
|
|
156
|
+
this.#sealed = true;
|
|
157
157
|
}
|
|
158
158
|
/** @internal - For unit tests only */
|
|
159
159
|
serializePermissions() {
|
|
160
160
|
return Object.fromEntries(
|
|
161
|
-
Array.from(this.
|
|
161
|
+
Array.from(this.#permissions.entries()).map(([pat, perms]) => [
|
|
162
162
|
pat,
|
|
163
163
|
Array.from(perms)
|
|
164
164
|
])
|
|
@@ -177,12 +177,12 @@ var Session = class {
|
|
|
177
177
|
);
|
|
178
178
|
}
|
|
179
179
|
try {
|
|
180
|
-
const resp = await this
|
|
180
|
+
const resp = await this.#postFn(url`/v2/authorize-user`, {
|
|
181
181
|
// Required
|
|
182
|
-
userId: this
|
|
182
|
+
userId: this.#userId,
|
|
183
183
|
permissions: this.serializePermissions(),
|
|
184
184
|
// Optional metadata
|
|
185
|
-
userInfo: this
|
|
185
|
+
userInfo: this.#userInfo
|
|
186
186
|
});
|
|
187
187
|
return {
|
|
188
188
|
status: normalizeStatusCode(resp.status),
|
|
@@ -200,6 +200,8 @@ var Session = class {
|
|
|
200
200
|
|
|
201
201
|
// src/client.ts
|
|
202
202
|
var Liveblocks = class {
|
|
203
|
+
#secret;
|
|
204
|
+
#baseUrl;
|
|
203
205
|
/**
|
|
204
206
|
* Interact with the Liveblocks API from your Node.js backend.
|
|
205
207
|
*/
|
|
@@ -207,14 +209,13 @@ var Liveblocks = class {
|
|
|
207
209
|
const options_ = options;
|
|
208
210
|
const secret = options_.secret;
|
|
209
211
|
assertSecretKey(secret, "secret");
|
|
210
|
-
this
|
|
211
|
-
this
|
|
212
|
+
this.#secret = secret;
|
|
213
|
+
this.#baseUrl = new URL(getBaseUrl(options.baseUrl));
|
|
212
214
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const url3 = urljoin(this._baseUrl, path);
|
|
215
|
+
async #post(path, json) {
|
|
216
|
+
const url3 = urljoin(this.#baseUrl, path);
|
|
216
217
|
const headers = {
|
|
217
|
-
Authorization: `Bearer ${this
|
|
218
|
+
Authorization: `Bearer ${this.#secret}`,
|
|
218
219
|
"Content-Type": "application/json"
|
|
219
220
|
};
|
|
220
221
|
const fetch = await fetchPolyfill();
|
|
@@ -225,11 +226,10 @@ var Liveblocks = class {
|
|
|
225
226
|
});
|
|
226
227
|
return res;
|
|
227
228
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const url3 = urljoin(this._baseUrl, path);
|
|
229
|
+
async #put(path, json) {
|
|
230
|
+
const url3 = urljoin(this.#baseUrl, path);
|
|
231
231
|
const headers = {
|
|
232
|
-
Authorization: `Bearer ${this
|
|
232
|
+
Authorization: `Bearer ${this.#secret}`,
|
|
233
233
|
"Content-Type": "application/json"
|
|
234
234
|
};
|
|
235
235
|
const fetch = await fetchPolyfill();
|
|
@@ -239,31 +239,28 @@ var Liveblocks = class {
|
|
|
239
239
|
body: JSON.stringify(json)
|
|
240
240
|
});
|
|
241
241
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const url3 = urljoin(this._baseUrl, path, params);
|
|
242
|
+
async #putBinary(path, body, params) {
|
|
243
|
+
const url3 = urljoin(this.#baseUrl, path, params);
|
|
245
244
|
const headers = {
|
|
246
|
-
Authorization: `Bearer ${this
|
|
245
|
+
Authorization: `Bearer ${this.#secret}`,
|
|
247
246
|
"Content-Type": "application/octet-stream"
|
|
248
247
|
};
|
|
249
248
|
const fetch = await fetchPolyfill();
|
|
250
249
|
return await fetch(url3, { method: "PUT", headers, body });
|
|
251
250
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const url3 = urljoin(this._baseUrl, path);
|
|
251
|
+
async #delete(path) {
|
|
252
|
+
const url3 = urljoin(this.#baseUrl, path);
|
|
255
253
|
const headers = {
|
|
256
|
-
Authorization: `Bearer ${this
|
|
254
|
+
Authorization: `Bearer ${this.#secret}`
|
|
257
255
|
};
|
|
258
256
|
const fetch = await fetchPolyfill();
|
|
259
257
|
const res = await fetch(url3, { method: "DELETE", headers });
|
|
260
258
|
return res;
|
|
261
259
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const url3 = urljoin(this._baseUrl, path, params);
|
|
260
|
+
async #get(path, params) {
|
|
261
|
+
const url3 = urljoin(this.#baseUrl, path, params);
|
|
265
262
|
const headers = {
|
|
266
|
-
Authorization: `Bearer ${this
|
|
263
|
+
Authorization: `Bearer ${this.#secret}`
|
|
267
264
|
};
|
|
268
265
|
const fetch = await fetchPolyfill();
|
|
269
266
|
const res = await fetch(url3, { method: "GET", headers });
|
|
@@ -290,7 +287,7 @@ var Liveblocks = class {
|
|
|
290
287
|
*/
|
|
291
288
|
prepareSession(userId, ...rest) {
|
|
292
289
|
const options = rest[0];
|
|
293
|
-
return new Session(this
|
|
290
|
+
return new Session(this.#post.bind(this), userId, options?.userInfo);
|
|
294
291
|
}
|
|
295
292
|
/**
|
|
296
293
|
* Call this to authenticate the user as an actor you want to allow to use
|
|
@@ -332,7 +329,7 @@ var Liveblocks = class {
|
|
|
332
329
|
const groupIds = typeof identity === "string" ? void 0 : identity.groupIds;
|
|
333
330
|
assertNonEmpty(userId, "userId");
|
|
334
331
|
try {
|
|
335
|
-
const resp = await this
|
|
332
|
+
const resp = await this.#post(path, {
|
|
336
333
|
userId,
|
|
337
334
|
groupIds,
|
|
338
335
|
// Optional metadata
|
|
@@ -346,7 +343,7 @@ var Liveblocks = class {
|
|
|
346
343
|
return {
|
|
347
344
|
status: 503,
|
|
348
345
|
body: `Call to ${urljoin(
|
|
349
|
-
this
|
|
346
|
+
this.#baseUrl,
|
|
350
347
|
path
|
|
351
348
|
)} failed. See "error" for more information.`,
|
|
352
349
|
error: er
|
|
@@ -388,7 +385,7 @@ var Liveblocks = class {
|
|
|
388
385
|
),
|
|
389
386
|
query
|
|
390
387
|
};
|
|
391
|
-
const res = await this
|
|
388
|
+
const res = await this.#get(path, queryParams);
|
|
392
389
|
if (!res.ok) {
|
|
393
390
|
const text = await res.text();
|
|
394
391
|
throw new LiveblocksError(res.status, text);
|
|
@@ -419,7 +416,7 @@ var Liveblocks = class {
|
|
|
419
416
|
*/
|
|
420
417
|
async createRoom(roomId, params) {
|
|
421
418
|
const { defaultAccesses, groupsAccesses, usersAccesses, metadata } = params;
|
|
422
|
-
const res = await this
|
|
419
|
+
const res = await this.#post(url2`/v2/rooms`, {
|
|
423
420
|
id: roomId,
|
|
424
421
|
defaultAccesses,
|
|
425
422
|
groupsAccesses,
|
|
@@ -445,7 +442,7 @@ var Liveblocks = class {
|
|
|
445
442
|
* @returns The room with the given id.
|
|
446
443
|
*/
|
|
447
444
|
async getRoom(roomId) {
|
|
448
|
-
const res = await this
|
|
445
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}`);
|
|
449
446
|
if (!res.ok) {
|
|
450
447
|
const text = await res.text();
|
|
451
448
|
throw new LiveblocksError(res.status, text);
|
|
@@ -471,7 +468,7 @@ var Liveblocks = class {
|
|
|
471
468
|
*/
|
|
472
469
|
async updateRoom(roomId, params) {
|
|
473
470
|
const { defaultAccesses, groupsAccesses, usersAccesses, metadata } = params;
|
|
474
|
-
const res = await this
|
|
471
|
+
const res = await this.#post(url2`/v2/rooms/${roomId}`, {
|
|
475
472
|
defaultAccesses,
|
|
476
473
|
groupsAccesses,
|
|
477
474
|
usersAccesses,
|
|
@@ -495,7 +492,7 @@ var Liveblocks = class {
|
|
|
495
492
|
* @param roomId The id of the room to delete.
|
|
496
493
|
*/
|
|
497
494
|
async deleteRoom(roomId) {
|
|
498
|
-
const res = await this
|
|
495
|
+
const res = await this.#delete(url2`/v2/rooms/${roomId}`);
|
|
499
496
|
if (!res.ok) {
|
|
500
497
|
const text = await res.text();
|
|
501
498
|
throw new LiveblocksError(res.status, text);
|
|
@@ -507,7 +504,7 @@ var Liveblocks = class {
|
|
|
507
504
|
* @returns A list of users currently present in the requested room.
|
|
508
505
|
*/
|
|
509
506
|
async getActiveUsers(roomId) {
|
|
510
|
-
const res = await this
|
|
507
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}/active_users`);
|
|
511
508
|
if (!res.ok) {
|
|
512
509
|
const text = await res.text();
|
|
513
510
|
throw new LiveblocksError(res.status, text);
|
|
@@ -520,7 +517,7 @@ var Liveblocks = class {
|
|
|
520
517
|
* @param message The message to broadcast. It can be any JSON serializable value.
|
|
521
518
|
*/
|
|
522
519
|
async broadcastEvent(roomId, message) {
|
|
523
|
-
const res = await this
|
|
520
|
+
const res = await this.#post(
|
|
524
521
|
url2`/v2/rooms/${roomId}/broadcast_event`,
|
|
525
522
|
message
|
|
526
523
|
);
|
|
@@ -530,7 +527,7 @@ var Liveblocks = class {
|
|
|
530
527
|
}
|
|
531
528
|
}
|
|
532
529
|
async getStorageDocument(roomId, format = "plain-lson") {
|
|
533
|
-
const res = await this
|
|
530
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}/storage`, { format });
|
|
534
531
|
if (!res.ok) {
|
|
535
532
|
const text = await res.text();
|
|
536
533
|
throw new LiveblocksError(res.status, text);
|
|
@@ -546,7 +543,7 @@ var Liveblocks = class {
|
|
|
546
543
|
* @returns The initialized storage document. It is of the same format as the one passed in.
|
|
547
544
|
*/
|
|
548
545
|
async initializeStorageDocument(roomId, document) {
|
|
549
|
-
const res = await this
|
|
546
|
+
const res = await this.#post(url2`/v2/rooms/${roomId}/storage`, document);
|
|
550
547
|
if (!res.ok) {
|
|
551
548
|
const text = await res.text();
|
|
552
549
|
throw new LiveblocksError(res.status, text);
|
|
@@ -558,7 +555,7 @@ var Liveblocks = class {
|
|
|
558
555
|
* @param roomId The id of the room to delete the storage from.
|
|
559
556
|
*/
|
|
560
557
|
async deleteStorageDocument(roomId) {
|
|
561
|
-
const res = await this
|
|
558
|
+
const res = await this.#delete(url2`/v2/rooms/${roomId}/storage`);
|
|
562
559
|
if (!res.ok) {
|
|
563
560
|
const text = await res.text();
|
|
564
561
|
throw new LiveblocksError(res.status, text);
|
|
@@ -578,7 +575,7 @@ var Liveblocks = class {
|
|
|
578
575
|
async getYjsDocument(roomId, params = {}) {
|
|
579
576
|
const { format, key, type } = params;
|
|
580
577
|
const path = url2`v2/rooms/${roomId}/ydoc`;
|
|
581
|
-
const res = await this
|
|
578
|
+
const res = await this.#get(path, {
|
|
582
579
|
formatting: format ? "true" : void 0,
|
|
583
580
|
key,
|
|
584
581
|
type
|
|
@@ -596,7 +593,7 @@ var Liveblocks = class {
|
|
|
596
593
|
* @param params.guid (optional) If provided, the binary update will be applied to the Yjs subdocument with the given guid. If not provided, the binary update will be applied to the root Yjs document.
|
|
597
594
|
*/
|
|
598
595
|
async sendYjsBinaryUpdate(roomId, update, params = {}) {
|
|
599
|
-
const res = await this
|
|
596
|
+
const res = await this.#putBinary(url2`/v2/rooms/${roomId}/ydoc`, update, {
|
|
600
597
|
guid: params.guid
|
|
601
598
|
});
|
|
602
599
|
if (!res.ok) {
|
|
@@ -612,7 +609,7 @@ var Liveblocks = class {
|
|
|
612
609
|
* @returns The room’s Yjs document encoded as a single binary update.
|
|
613
610
|
*/
|
|
614
611
|
async getYjsDocumentAsBinaryUpdate(roomId, params = {}) {
|
|
615
|
-
const res = await this
|
|
612
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}/ydoc-binary`, {
|
|
616
613
|
guid: params.guid
|
|
617
614
|
});
|
|
618
615
|
if (!res.ok) {
|
|
@@ -631,7 +628,7 @@ var Liveblocks = class {
|
|
|
631
628
|
* @returns The created schema.
|
|
632
629
|
*/
|
|
633
630
|
async createSchema(name, body) {
|
|
634
|
-
const res = await this
|
|
631
|
+
const res = await this.#post(url2`/v2/schemas`, {
|
|
635
632
|
name,
|
|
636
633
|
body
|
|
637
634
|
});
|
|
@@ -654,7 +651,7 @@ var Liveblocks = class {
|
|
|
654
651
|
* @returns The schema with the given id.
|
|
655
652
|
*/
|
|
656
653
|
async getSchema(schemaId) {
|
|
657
|
-
const res = await this
|
|
654
|
+
const res = await this.#get(url2`/v2/schemas/${schemaId}`);
|
|
658
655
|
if (!res.ok) {
|
|
659
656
|
const text = await res.text();
|
|
660
657
|
throw new LiveblocksError(res.status, text);
|
|
@@ -675,7 +672,7 @@ var Liveblocks = class {
|
|
|
675
672
|
* @returns The updated schema. The version of the schema will be incremented.
|
|
676
673
|
*/
|
|
677
674
|
async updateSchema(schemaId, body) {
|
|
678
|
-
const res = await this
|
|
675
|
+
const res = await this.#put(url2`/v2/schemas/${schemaId}`, {
|
|
679
676
|
body
|
|
680
677
|
});
|
|
681
678
|
if (!res.ok) {
|
|
@@ -696,7 +693,7 @@ var Liveblocks = class {
|
|
|
696
693
|
* @param schemaId Id of the schema - this is the combination of the schema name and version of the schema to update. For example, `my-schema@1`.
|
|
697
694
|
*/
|
|
698
695
|
async deleteSchema(schemaId) {
|
|
699
|
-
const res = await this
|
|
696
|
+
const res = await this.#delete(url2`/v2/schemas/${schemaId}`);
|
|
700
697
|
if (!res.ok) {
|
|
701
698
|
const text = await res.text();
|
|
702
699
|
throw new LiveblocksError(res.status, text);
|
|
@@ -708,7 +705,7 @@ var Liveblocks = class {
|
|
|
708
705
|
* @returns
|
|
709
706
|
*/
|
|
710
707
|
async getSchemaByRoomId(roomId) {
|
|
711
|
-
const res = await this
|
|
708
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}/schema`);
|
|
712
709
|
if (!res.ok) {
|
|
713
710
|
const text = await res.text();
|
|
714
711
|
throw new LiveblocksError(res.status, text);
|
|
@@ -730,7 +727,7 @@ var Liveblocks = class {
|
|
|
730
727
|
* @returns The schema id as JSON.
|
|
731
728
|
*/
|
|
732
729
|
async attachSchemaToRoom(roomId, schemaId) {
|
|
733
|
-
const res = await this
|
|
730
|
+
const res = await this.#post(url2`/v2/rooms/${roomId}/schema`, {
|
|
734
731
|
schema: schemaId
|
|
735
732
|
});
|
|
736
733
|
if (!res.ok) {
|
|
@@ -744,7 +741,7 @@ var Liveblocks = class {
|
|
|
744
741
|
* @param roomId The id of the room to detach the schema from.
|
|
745
742
|
*/
|
|
746
743
|
async detachSchemaFromRoom(roomId) {
|
|
747
|
-
const res = await this
|
|
744
|
+
const res = await this.#delete(url2`/v2/rooms/${roomId}/schema`);
|
|
748
745
|
if (!res.ok) {
|
|
749
746
|
const text = await res.text();
|
|
750
747
|
throw new LiveblocksError(res.status, text);
|
|
@@ -768,7 +765,7 @@ var Liveblocks = class {
|
|
|
768
765
|
} else if (typeof params.query === "object") {
|
|
769
766
|
query = objectToQuery(params.query);
|
|
770
767
|
}
|
|
771
|
-
const res = await this
|
|
768
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}/threads`, {
|
|
772
769
|
query
|
|
773
770
|
});
|
|
774
771
|
if (!res.ok) {
|
|
@@ -789,7 +786,7 @@ var Liveblocks = class {
|
|
|
789
786
|
*/
|
|
790
787
|
async getThread(params) {
|
|
791
788
|
const { roomId, threadId } = params;
|
|
792
|
-
const res = await this
|
|
789
|
+
const res = await this.#get(url2`/v2/rooms/${roomId}/threads/${threadId}`);
|
|
793
790
|
if (!res.ok) {
|
|
794
791
|
const text = await res.text();
|
|
795
792
|
throw new LiveblocksError(res.status, text);
|
|
@@ -808,7 +805,7 @@ var Liveblocks = class {
|
|
|
808
805
|
*/
|
|
809
806
|
async getThreadParticipants(params) {
|
|
810
807
|
const { roomId, threadId } = params;
|
|
811
|
-
const res = await this
|
|
808
|
+
const res = await this.#get(
|
|
812
809
|
url2`/v2/rooms/${roomId}/threads/${threadId}/participants`
|
|
813
810
|
);
|
|
814
811
|
if (!res.ok) {
|
|
@@ -827,7 +824,7 @@ var Liveblocks = class {
|
|
|
827
824
|
*/
|
|
828
825
|
async getComment(params) {
|
|
829
826
|
const { roomId, threadId, commentId } = params;
|
|
830
|
-
const res = await this
|
|
827
|
+
const res = await this.#get(
|
|
831
828
|
url2`/v2/rooms/${roomId}/threads/${threadId}/comments/${commentId}`
|
|
832
829
|
);
|
|
833
830
|
if (!res.ok) {
|
|
@@ -848,7 +845,7 @@ var Liveblocks = class {
|
|
|
848
845
|
*/
|
|
849
846
|
async createComment(params) {
|
|
850
847
|
const { roomId, threadId, data } = params;
|
|
851
|
-
const res = await this
|
|
848
|
+
const res = await this.#post(
|
|
852
849
|
url2`/v2/rooms/${roomId}/threads/${threadId}/comments`,
|
|
853
850
|
{
|
|
854
851
|
...data,
|
|
@@ -872,7 +869,7 @@ var Liveblocks = class {
|
|
|
872
869
|
*/
|
|
873
870
|
async editComment(params) {
|
|
874
871
|
const { roomId, threadId, commentId, data } = params;
|
|
875
|
-
const res = await this
|
|
872
|
+
const res = await this.#post(
|
|
876
873
|
url2`/v2/rooms/${roomId}/threads/${threadId}/comments/${commentId}`,
|
|
877
874
|
{
|
|
878
875
|
...data,
|
|
@@ -893,7 +890,7 @@ var Liveblocks = class {
|
|
|
893
890
|
*/
|
|
894
891
|
async deleteComment(params) {
|
|
895
892
|
const { roomId, threadId, commentId } = params;
|
|
896
|
-
const res = await this
|
|
893
|
+
const res = await this.#delete(
|
|
897
894
|
url2`/v2/rooms/${roomId}/threads/${threadId}/comments/${commentId}`
|
|
898
895
|
);
|
|
899
896
|
if (!res.ok) {
|
|
@@ -913,7 +910,7 @@ var Liveblocks = class {
|
|
|
913
910
|
*/
|
|
914
911
|
async createThread(params) {
|
|
915
912
|
const { roomId, data } = params;
|
|
916
|
-
const res = await this
|
|
913
|
+
const res = await this.#post(url2`/v2/rooms/${roomId}/threads`, {
|
|
917
914
|
...data,
|
|
918
915
|
comment: {
|
|
919
916
|
...data.comment,
|
|
@@ -933,7 +930,9 @@ var Liveblocks = class {
|
|
|
933
930
|
*/
|
|
934
931
|
async deleteThread(params) {
|
|
935
932
|
const { roomId, threadId } = params;
|
|
936
|
-
const res = await this
|
|
933
|
+
const res = await this.#delete(
|
|
934
|
+
url2`/v2/rooms/${roomId}/threads/${threadId}`
|
|
935
|
+
);
|
|
937
936
|
if (!res.ok) {
|
|
938
937
|
const text = await res.text();
|
|
939
938
|
throw new LiveblocksError(res.status, text);
|
|
@@ -948,7 +947,7 @@ var Liveblocks = class {
|
|
|
948
947
|
*/
|
|
949
948
|
async markThreadAsResolved(params) {
|
|
950
949
|
const { roomId, threadId } = params;
|
|
951
|
-
const res = await this
|
|
950
|
+
const res = await this.#post(
|
|
952
951
|
url2`/v2/rooms/${roomId}/threads/${threadId}/mark-as-resolved`,
|
|
953
952
|
{}
|
|
954
953
|
);
|
|
@@ -967,7 +966,7 @@ var Liveblocks = class {
|
|
|
967
966
|
*/
|
|
968
967
|
async markThreadAsUnresolved(params) {
|
|
969
968
|
const { roomId, threadId } = params;
|
|
970
|
-
const res = await this
|
|
969
|
+
const res = await this.#post(
|
|
971
970
|
url2`/v2/rooms/${roomId}/threads/${threadId}/mark-as-unresolved`,
|
|
972
971
|
{}
|
|
973
972
|
);
|
|
@@ -988,7 +987,7 @@ var Liveblocks = class {
|
|
|
988
987
|
*/
|
|
989
988
|
async editThreadMetadata(params) {
|
|
990
989
|
const { roomId, threadId, data } = params;
|
|
991
|
-
const res = await this
|
|
990
|
+
const res = await this.#post(
|
|
992
991
|
url2`/v2/rooms/${roomId}/threads/${threadId}/metadata`,
|
|
993
992
|
{
|
|
994
993
|
...data,
|
|
@@ -1013,7 +1012,7 @@ var Liveblocks = class {
|
|
|
1013
1012
|
*/
|
|
1014
1013
|
async addCommentReaction(params) {
|
|
1015
1014
|
const { roomId, threadId, commentId, data } = params;
|
|
1016
|
-
const res = await this
|
|
1015
|
+
const res = await this.#post(
|
|
1017
1016
|
url2`/v2/rooms/${roomId}/threads/${threadId}/comments/${commentId}/add-reaction`,
|
|
1018
1017
|
{
|
|
1019
1018
|
...data,
|
|
@@ -1038,7 +1037,7 @@ var Liveblocks = class {
|
|
|
1038
1037
|
*/
|
|
1039
1038
|
async removeCommentReaction(params) {
|
|
1040
1039
|
const { roomId, threadId, data } = params;
|
|
1041
|
-
const res = await this
|
|
1040
|
+
const res = await this.#post(
|
|
1042
1041
|
url2`/v2/rooms/${roomId}/threads/${threadId}/comments/${params.commentId}/remove-reaction`,
|
|
1043
1042
|
{
|
|
1044
1043
|
...data,
|
|
@@ -1057,7 +1056,7 @@ var Liveblocks = class {
|
|
|
1057
1056
|
*/
|
|
1058
1057
|
async getInboxNotification(params) {
|
|
1059
1058
|
const { userId, inboxNotificationId } = params;
|
|
1060
|
-
const res = await this
|
|
1059
|
+
const res = await this.#get(
|
|
1061
1060
|
url2`/v2/users/${userId}/inbox-notifications/${inboxNotificationId}`
|
|
1062
1061
|
);
|
|
1063
1062
|
if (!res.ok) {
|
|
@@ -1081,7 +1080,7 @@ var Liveblocks = class {
|
|
|
1081
1080
|
} else if (typeof params.query === "object") {
|
|
1082
1081
|
query = objectToQuery(params.query);
|
|
1083
1082
|
}
|
|
1084
|
-
const res = await this
|
|
1083
|
+
const res = await this.#get(url2`/v2/users/${userId}/inbox-notifications`, {
|
|
1085
1084
|
query
|
|
1086
1085
|
});
|
|
1087
1086
|
if (!res.ok) {
|
|
@@ -1100,7 +1099,7 @@ var Liveblocks = class {
|
|
|
1100
1099
|
*/
|
|
1101
1100
|
async getRoomNotificationSettings(params) {
|
|
1102
1101
|
const { userId, roomId } = params;
|
|
1103
|
-
const res = await this
|
|
1102
|
+
const res = await this.#get(
|
|
1104
1103
|
url2`/v2/rooms/${roomId}/users/${userId}/notification-settings`
|
|
1105
1104
|
);
|
|
1106
1105
|
if (!res.ok) {
|
|
@@ -1117,7 +1116,7 @@ var Liveblocks = class {
|
|
|
1117
1116
|
*/
|
|
1118
1117
|
async updateRoomNotificationSettings(params) {
|
|
1119
1118
|
const { userId, roomId, data } = params;
|
|
1120
|
-
const res = await this
|
|
1119
|
+
const res = await this.#post(
|
|
1121
1120
|
url2`/v2/rooms/${roomId}/users/${userId}/notification-settings`,
|
|
1122
1121
|
data
|
|
1123
1122
|
);
|
|
@@ -1134,7 +1133,7 @@ var Liveblocks = class {
|
|
|
1134
1133
|
*/
|
|
1135
1134
|
async deleteRoomNotificationSettings(params) {
|
|
1136
1135
|
const { userId, roomId } = params;
|
|
1137
|
-
const res = await this
|
|
1136
|
+
const res = await this.#delete(
|
|
1138
1137
|
url2`/v2/rooms/${roomId}/users/${userId}/notification-settings`
|
|
1139
1138
|
);
|
|
1140
1139
|
if (!res.ok) {
|
|
@@ -1149,7 +1148,7 @@ var Liveblocks = class {
|
|
|
1149
1148
|
*/
|
|
1150
1149
|
async updateRoomId(params) {
|
|
1151
1150
|
const { currentRoomId, newRoomId } = params;
|
|
1152
|
-
const res = await this
|
|
1151
|
+
const res = await this.#post(
|
|
1153
1152
|
url2`/v2/rooms/${currentRoomId}/update-room-id`,
|
|
1154
1153
|
{
|
|
1155
1154
|
newRoomId
|
|
@@ -1167,7 +1166,7 @@ var Liveblocks = class {
|
|
|
1167
1166
|
};
|
|
1168
1167
|
}
|
|
1169
1168
|
async triggerInboxNotification(params) {
|
|
1170
|
-
const res = await this
|
|
1169
|
+
const res = await this.#post(url2`/v2/inbox-notifications/trigger`, params);
|
|
1171
1170
|
if (!res.ok) {
|
|
1172
1171
|
const text = await res.text();
|
|
1173
1172
|
throw new LiveblocksError(res.status, text);
|
|
@@ -1180,7 +1179,7 @@ var Liveblocks = class {
|
|
|
1180
1179
|
*/
|
|
1181
1180
|
async deleteInboxNotification(params) {
|
|
1182
1181
|
const { userId, inboxNotificationId } = params;
|
|
1183
|
-
const res = await this
|
|
1182
|
+
const res = await this.#delete(
|
|
1184
1183
|
url2`/v2/users/${userId}/inbox-notifications/${inboxNotificationId}`
|
|
1185
1184
|
);
|
|
1186
1185
|
if (!res.ok) {
|
|
@@ -1194,7 +1193,9 @@ var Liveblocks = class {
|
|
|
1194
1193
|
*/
|
|
1195
1194
|
async deleteAllInboxNotifications(params) {
|
|
1196
1195
|
const { userId } = params;
|
|
1197
|
-
const res = await this
|
|
1196
|
+
const res = await this.#delete(
|
|
1197
|
+
url2`/v2/users/${userId}/inbox-notifications`
|
|
1198
|
+
);
|
|
1198
1199
|
if (!res.ok) {
|
|
1199
1200
|
const text = await res.text();
|
|
1200
1201
|
throw new LiveblocksError(res.status, text);
|
|
@@ -1202,6 +1203,7 @@ var Liveblocks = class {
|
|
|
1202
1203
|
}
|
|
1203
1204
|
};
|
|
1204
1205
|
var LiveblocksError = class extends Error {
|
|
1206
|
+
status;
|
|
1205
1207
|
constructor(status, message = "") {
|
|
1206
1208
|
super(message);
|
|
1207
1209
|
this.name = "LiveblocksError";
|
|
@@ -1212,28 +1214,30 @@ var LiveblocksError = class extends Error {
|
|
|
1212
1214
|
// src/webhooks.ts
|
|
1213
1215
|
import * as base64 from "@stablelib/base64";
|
|
1214
1216
|
import * as sha256 from "fast-sha256";
|
|
1215
|
-
var
|
|
1217
|
+
var WebhookHandler = class _WebhookHandler {
|
|
1218
|
+
#secretBuffer;
|
|
1219
|
+
static #secretPrefix = "whsec_";
|
|
1216
1220
|
constructor(secret) {
|
|
1217
1221
|
if (!secret) throw new Error("Secret is required");
|
|
1218
1222
|
if (typeof secret !== "string") throw new Error("Secret must be a string");
|
|
1219
|
-
if (secret.startsWith(_WebhookHandler
|
|
1223
|
+
if (secret.startsWith(_WebhookHandler.#secretPrefix) === false)
|
|
1220
1224
|
throw new Error("Invalid secret, must start with whsec_");
|
|
1221
|
-
const secretKey = secret.slice(_WebhookHandler
|
|
1222
|
-
this
|
|
1225
|
+
const secretKey = secret.slice(_WebhookHandler.#secretPrefix.length);
|
|
1226
|
+
this.#secretBuffer = Buffer.from(secretKey, "base64");
|
|
1223
1227
|
}
|
|
1224
1228
|
/**
|
|
1225
1229
|
* Verifies a webhook request and returns the event
|
|
1226
1230
|
*/
|
|
1227
1231
|
verifyRequest(request) {
|
|
1228
1232
|
const { headers, rawBody } = request;
|
|
1229
|
-
const { webhookId, timestamp, rawSignatures } = this
|
|
1233
|
+
const { webhookId, timestamp, rawSignatures } = this.#verifyHeaders(headers);
|
|
1230
1234
|
if (typeof rawBody !== "string") {
|
|
1231
1235
|
throw new Error(
|
|
1232
1236
|
`Invalid rawBody field, must be a string, got "${typeof rawBody}" instead. It is likely that you need to JSON.stringify the body before passing it.`
|
|
1233
1237
|
);
|
|
1234
1238
|
}
|
|
1235
|
-
this
|
|
1236
|
-
const signature = this
|
|
1239
|
+
this.#verifyTimestamp(timestamp);
|
|
1240
|
+
const signature = this.#sign(`${webhookId}.${timestamp}.${rawBody}`);
|
|
1237
1241
|
const expectedSignatures = rawSignatures.split(" ").map((rawSignature) => {
|
|
1238
1242
|
const [, parsedSignature] = rawSignature.split(",");
|
|
1239
1243
|
return parsedSignature;
|
|
@@ -1245,13 +1249,13 @@ var _WebhookHandler = class _WebhookHandler {
|
|
|
1245
1249
|
)}, got ${signature}`
|
|
1246
1250
|
);
|
|
1247
1251
|
const event = JSON.parse(rawBody);
|
|
1248
|
-
this
|
|
1252
|
+
this.#verifyWebhookEventType(event);
|
|
1249
1253
|
return event;
|
|
1250
1254
|
}
|
|
1251
1255
|
/**
|
|
1252
1256
|
* Verifies the headers and returns the webhookId, timestamp and rawSignatures
|
|
1253
1257
|
*/
|
|
1254
|
-
verifyHeaders(headers) {
|
|
1258
|
+
#verifyHeaders(headers) {
|
|
1255
1259
|
const usingNativeHeaders = typeof Headers !== "undefined" && headers instanceof Headers;
|
|
1256
1260
|
const normalizedHeaders = usingNativeHeaders ? Object.fromEntries(headers) : headers;
|
|
1257
1261
|
const sanitizedHeaders = {};
|
|
@@ -1274,15 +1278,15 @@ var _WebhookHandler = class _WebhookHandler {
|
|
|
1274
1278
|
* @param content
|
|
1275
1279
|
* @returns `string`
|
|
1276
1280
|
*/
|
|
1277
|
-
sign(content) {
|
|
1281
|
+
#sign(content) {
|
|
1278
1282
|
const encoder = new TextEncoder();
|
|
1279
1283
|
const toSign = encoder.encode(content);
|
|
1280
|
-
return base64.encode(sha256.hmac(this
|
|
1284
|
+
return base64.encode(sha256.hmac(this.#secretBuffer, toSign));
|
|
1281
1285
|
}
|
|
1282
1286
|
/**
|
|
1283
1287
|
* Verifies that the timestamp is not too old or in the future
|
|
1284
1288
|
*/
|
|
1285
|
-
verifyTimestamp(timestampHeader) {
|
|
1289
|
+
#verifyTimestamp(timestampHeader) {
|
|
1286
1290
|
const now = Math.floor(Date.now() / 1e3);
|
|
1287
1291
|
const timestamp = parseInt(timestampHeader, 10);
|
|
1288
1292
|
if (isNaN(timestamp)) {
|
|
@@ -1299,7 +1303,7 @@ var _WebhookHandler = class _WebhookHandler {
|
|
|
1299
1303
|
* Ensures that the event is a known event type
|
|
1300
1304
|
* or throws and prompts the user to upgrade to a higher version of @liveblocks/node
|
|
1301
1305
|
*/
|
|
1302
|
-
verifyWebhookEventType(event) {
|
|
1306
|
+
#verifyWebhookEventType(event) {
|
|
1303
1307
|
if (event && event.type && [
|
|
1304
1308
|
"storageUpdated",
|
|
1305
1309
|
"userEntered",
|
|
@@ -1336,8 +1340,6 @@ var _WebhookHandler = class _WebhookHandler {
|
|
|
1336
1340
|
);
|
|
1337
1341
|
}
|
|
1338
1342
|
};
|
|
1339
|
-
_WebhookHandler.secretPrefix = "whsec_";
|
|
1340
|
-
var WebhookHandler = _WebhookHandler;
|
|
1341
1343
|
var WEBHOOK_TOLERANCE_IN_SECONDS = 5 * 60;
|
|
1342
1344
|
var isNotUndefined = (value) => value !== void 0;
|
|
1343
1345
|
function isThreadNotificationEvent(event) {
|