From 8bdf5c9eccb603e8a2ef8c4b082d476b1a5d8f70 Mon Sep 17 00:00:00 2001 From: hopperelec Date: Wed, 17 Apr 2024 04:10:27 +0100 Subject: [PATCH] Update to Ably v2 --- package-lock.json | 24 +++++++++++++++--------- package.json | 2 +- src/lib/ably-client.ts | 8 ++++---- src/lib/server/ably-server.ts | 20 +++++++++++++------- src/routes/ably-auth/+server.ts | 29 +++++++++++------------------ 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index cf7c0fe..8031fea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,8 +8,8 @@ "name": "definitiondash", "version": "0.0.1", "dependencies": { - "@prisma/client": "^5.11.0", - "ably": "^1.2.49", + "@prisma/client": "^5.12.1", + "ably": "^2.0.2", "google-auth-library": "^9.7.0", "html-minifier-terser": "^7.2.0", "panzoom": "^9.4.3", @@ -1833,9 +1833,9 @@ "dev": true }, "node_modules/@prisma/client": { - "version": "5.11.0", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.11.0.tgz", - "integrity": "sha512-SWshvS5FDXvgJKM/a0y9nDC1rqd7KG0Q6ZVzd+U7ZXK5soe73DJxJJgbNBt2GNXOa+ysWB4suTpdK5zfFPhwiw==", + "version": "5.12.1", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.12.1.tgz", + "integrity": "sha512-6/JnizEdlSBxDIdiLbrBdMW5NqDxOmhXAJaNXiPpgzAPr/nLZResT6MMpbOHLo5yAbQ1Vv5UU8PTPRzb0WIxdA==", "hasInstallScript": true, "engines": { "node": ">=16.13" @@ -2604,16 +2604,17 @@ "dev": true }, "node_modules/ably": { - "version": "1.2.49", - "resolved": "https://registry.npmjs.org/ably/-/ably-1.2.49.tgz", - "integrity": "sha512-p0plkM7o9N58LEIxu5SpqHkU/A0dyBrW9kalaCufRuZyF5ZKaXvfsb1Ay/+1KGqBwigYlud5AlL2jTQvTw8Arg==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/ably/-/ably-2.0.2.tgz", + "integrity": "sha512-ud6WxddwNQKn4BPbZLfhOrXHM7OXMr0KHd5zD6elnaQEn3poSuwAxCBePh7iXw13MQScrwBfxaZa8oUfPgURsg==", "dependencies": { "@ably/msgpack-js": "^0.4.0", + "fastestsmallesttextencoderdecoder": "^1.0.22", "got": "^11.8.5", "ws": "^8.14.2" }, "engines": { - "node": ">=5.10.x" + "node": ">=16" }, "peerDependencies": { "react": ">=16.8.0", @@ -4153,6 +4154,11 @@ "node": ">= 4.9.1" } }, + "node_modules/fastestsmallesttextencoderdecoder": { + "version": "1.0.22", + "resolved": "https://registry.npmjs.org/fastestsmallesttextencoderdecoder/-/fastestsmallesttextencoderdecoder-1.0.22.tgz", + "integrity": "sha512-Pb8d48e+oIuY4MaM64Cd7OW1gt4nxCHs7/ddPPZ/Ic3sg8yVGM7O9wDvZ7us6ScaUupzM+pfBolwtYhN1IxBIw==" + }, "node_modules/fastq": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", diff --git a/package.json b/package.json index feb61a7..5b0a8ee 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "dependencies": { "@prisma/client": "^5.11.0", - "ably": "^1.2.49", + "ably": "^2.0.2", "google-auth-library": "^9.7.0", "html-minifier-terser": "^7.2.0", "panzoom": "^9.4.3", diff --git a/src/lib/ably-client.ts b/src/lib/ably-client.ts index 7abd007..059f362 100644 --- a/src/lib/ably-client.ts +++ b/src/lib/ably-client.ts @@ -5,16 +5,16 @@ import { browser } from "$app/environment"; export const ablyClientConnection = browser && new ably.Realtime({ authUrl: "/ably-auth" }); -const messages: { [key: string]: Readable } = {}; +const messages: { [key: string]: Readable } = {}; export function getChannel(name: string) { if (!ablyClientConnection) return readable(undefined); if (name in messages) return messages[name]; const ablyChannel = ablyClientConnection.channels.get(name); - const message = readable(undefined, (set) => { - ablyChannel.subscribe(set); + const message = readable(undefined, (set) => { + ablyChannel.subscribe(set).then(); return () => { - ablyChannel.detach(); + ablyChannel.detach().then(); ablyChannel.unsubscribe(set); delete messages[name]; }; diff --git a/src/lib/server/ably-server.ts b/src/lib/server/ably-server.ts index 611f405..648be1f 100644 --- a/src/lib/server/ably-server.ts +++ b/src/lib/server/ably-server.ts @@ -12,10 +12,12 @@ export function updateRealtimePoints( ) { ablyServer.channels .get("player:" + gameId + ":" + userId) - .publish("points", { points }); + .publish("points", { points }) + .then(); ablyServer.channels .get("game:" + gameId + ":points") - .publish("points", { userId, points }); + .publish("points", { userId, points }) + .then(); } export async function moveRoom( @@ -60,7 +62,8 @@ export async function moveRoom( .publish("move", { userId: player.userId, svgRef: room.svgRef, - }); + }) + .then(); } export async function unclaimRooms( @@ -73,8 +76,11 @@ export async function unclaimRooms( roomId: { in: rooms.map((room) => Number(room.roomId)) }, }, }); - ablyServer.channels.get("game:" + gameId + ":positions").publish( - "unclaim", - rooms.map((room) => Number(room.svgRef)), - ); + ablyServer.channels + .get("game:" + gameId + ":positions") + .publish( + "unclaim", + rooms.map((room) => Number(room.svgRef)), + ) + .then(); } diff --git a/src/routes/ably-auth/+server.ts b/src/routes/ably-auth/+server.ts index 9352a19..2b08703 100644 --- a/src/routes/ably-auth/+server.ts +++ b/src/routes/ably-auth/+server.ts @@ -18,22 +18,15 @@ export const GET = async ({ locals }) => { for (const player of userData.players) { channels.push("game:" + player.gameId + ":*"); } - return new Promise((resolve, reject) => { - ablyServer.auth.createTokenRequest( - { - capability: channels.reduce( - (acc, channel) => { - acc[channel] = ["subscribe"]; - return acc; - }, - {} as { [key: string]: ["subscribe"] }, - ), - }, - undefined, - (err, tokenRequest) => { - if (err) reject(err.message); - else resolve(json(tokenRequest)); - }, - ); - }); + return json( + await ablyServer.auth.createTokenRequest({ + capability: channels.reduce( + (acc, channel) => { + acc[channel] = ["subscribe"]; + return acc; + }, + {} as { [key: string]: ["subscribe"] }, + ), + }), + ); };