@colyseus/core 0.16.23 → 0.17.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.
- package/build/Debug.js +16 -4
- package/build/Debug.js.map +2 -2
- package/build/Debug.mjs +11 -1
- package/build/Debug.mjs.map +2 -2
- package/build/IPC.js +5 -3
- package/build/IPC.js.map +2 -2
- package/build/IPC.mjs +1 -0
- package/build/IPC.mjs.map +2 -2
- package/build/Logger.js +1 -0
- package/build/Logger.js.map +1 -1
- package/build/MatchMaker.js +148 -84
- package/build/MatchMaker.js.map +3 -3
- package/build/MatchMaker.mjs +130 -70
- package/build/MatchMaker.mjs.map +3 -3
- package/build/Protocol.js +54 -41
- package/build/Protocol.js.map +3 -3
- package/build/Protocol.mjs +52 -41
- package/build/Protocol.mjs.map +3 -3
- package/build/Room.js +430 -195
- package/build/Room.js.map +3 -3
- package/build/Room.mjs +417 -175
- package/build/Room.mjs.map +3 -3
- package/build/Server.js +48 -108
- package/build/Server.js.map +3 -3
- package/build/Server.mjs +39 -102
- package/build/Server.mjs.map +3 -3
- package/build/Stats.js +2 -1
- package/build/Stats.js.map +2 -2
- package/build/Stats.mjs.map +1 -1
- package/build/Transport.js +29 -11
- package/build/Transport.js.map +3 -3
- package/build/Transport.mjs +15 -9
- package/build/Transport.mjs.map +3 -3
- package/build/errors/RoomExceptions.js +9 -5
- package/build/errors/RoomExceptions.js.map +2 -2
- package/build/errors/RoomExceptions.mjs +8 -5
- package/build/errors/RoomExceptions.mjs.map +2 -2
- package/build/errors/SeatReservationError.js +1 -0
- package/build/errors/SeatReservationError.js.map +1 -1
- package/build/errors/ServerError.js +2 -1
- package/build/errors/ServerError.js.map +2 -2
- package/build/errors/ServerError.mjs.map +1 -1
- package/build/index.js +60 -26
- package/build/index.js.map +2 -2
- package/build/index.mjs +33 -7
- package/build/index.mjs.map +2 -2
- package/build/matchmaker/Lobby.js +13 -3
- package/build/matchmaker/Lobby.js.map +2 -2
- package/build/matchmaker/Lobby.mjs +11 -2
- package/build/matchmaker/Lobby.mjs.map +2 -2
- package/build/matchmaker/LocalDriver/LocalDriver.js +94 -0
- package/build/matchmaker/LocalDriver/LocalDriver.js.map +7 -0
- package/build/matchmaker/LocalDriver/LocalDriver.mjs +71 -0
- package/build/matchmaker/LocalDriver/LocalDriver.mjs.map +7 -0
- package/build/matchmaker/LocalDriver/Query.js +111 -0
- package/build/matchmaker/LocalDriver/Query.js.map +7 -0
- package/build/matchmaker/LocalDriver/Query.mjs +88 -0
- package/build/matchmaker/LocalDriver/Query.mjs.map +7 -0
- package/build/matchmaker/RegisteredHandler.js +57 -7
- package/build/matchmaker/RegisteredHandler.js.map +2 -2
- package/build/matchmaker/RegisteredHandler.mjs +54 -5
- package/build/matchmaker/RegisteredHandler.mjs.map +2 -2
- package/build/matchmaker/controller.js +8 -8
- package/build/matchmaker/controller.js.map +2 -2
- package/build/matchmaker/controller.mjs +4 -5
- package/build/matchmaker/controller.mjs.map +2 -2
- package/build/matchmaker/driver/api.js +21 -2
- package/build/matchmaker/driver/api.js.map +2 -2
- package/build/matchmaker/driver/api.mjs +18 -1
- package/build/matchmaker/driver/api.mjs.map +2 -2
- package/build/matchmaker/driver/local/LocalDriver.js +36 -7
- package/build/matchmaker/driver/local/LocalDriver.js.map +2 -2
- package/build/matchmaker/driver/local/LocalDriver.mjs +33 -5
- package/build/matchmaker/driver/local/LocalDriver.mjs.map +2 -2
- package/build/matchmaker/driver/local/Query.js +51 -18
- package/build/matchmaker/driver/local/Query.js.map +2 -2
- package/build/matchmaker/driver/local/Query.mjs +50 -18
- package/build/matchmaker/driver/local/Query.mjs.map +2 -2
- package/build/matchmaker/driver.js +44 -0
- package/build/matchmaker/driver.js.map +7 -0
- package/build/matchmaker/driver.mjs +20 -0
- package/build/matchmaker/driver.mjs.map +7 -0
- package/build/matchmaker/routes.js +79 -0
- package/build/matchmaker/routes.js.map +7 -0
- package/build/matchmaker/routes.mjs +45 -0
- package/build/matchmaker/routes.mjs.map +7 -0
- package/build/presence/LocalPresence.js +13 -27
- package/build/presence/LocalPresence.js.map +3 -3
- package/build/presence/LocalPresence.mjs +11 -16
- package/build/presence/LocalPresence.mjs.map +2 -2
- package/build/presence/Presence.js +37 -0
- package/build/presence/Presence.js.map +2 -2
- package/build/presence/Presence.mjs +29 -0
- package/build/presence/Presence.mjs.map +3 -3
- package/build/rooms/LobbyRoom.js +5 -5
- package/build/rooms/LobbyRoom.js.map +2 -2
- package/build/rooms/LobbyRoom.mjs +1 -2
- package/build/rooms/LobbyRoom.mjs.map +2 -2
- package/build/rooms/RankedQueueRoom.js +224 -0
- package/build/rooms/RankedQueueRoom.js.map +7 -0
- package/build/rooms/RankedQueueRoom.mjs +201 -0
- package/build/rooms/RankedQueueRoom.mjs.map +7 -0
- package/build/rooms/RelayRoom.js +6 -6
- package/build/rooms/RelayRoom.js.map +2 -2
- package/build/rooms/RelayRoom.mjs +4 -5
- package/build/rooms/RelayRoom.mjs.map +2 -2
- package/build/rooms/createRoom.js +51 -0
- package/build/rooms/createRoom.js.map +7 -0
- package/build/rooms/createRoom.mjs +28 -0
- package/build/rooms/createRoom.mjs.map +7 -0
- package/build/router/default_routes.js +79 -0
- package/build/router/default_routes.js.map +7 -0
- package/build/router/default_routes.mjs +45 -0
- package/build/router/default_routes.mjs.map +7 -0
- package/build/router/index.js +55 -0
- package/build/router/index.js.map +7 -0
- package/build/router/index.mjs +30 -0
- package/build/router/index.mjs.map +7 -0
- package/build/serializer/NoneSerializer.js +1 -0
- package/build/serializer/NoneSerializer.js.map +2 -2
- package/build/serializer/NoneSerializer.mjs.map +2 -2
- package/build/serializer/SchemaSerializer.js +6 -7
- package/build/serializer/SchemaSerializer.js.map +2 -2
- package/build/serializer/SchemaSerializer.mjs +3 -5
- package/build/serializer/SchemaSerializer.mjs.map +2 -2
- package/build/serializer/SchemaSerializerDebug.js +29 -0
- package/build/serializer/SchemaSerializerDebug.js.map +3 -3
- package/build/serializer/SchemaSerializerDebug.mjs +7 -0
- package/build/serializer/SchemaSerializerDebug.mjs.map +3 -3
- package/build/serializer/Serializer.js +1 -0
- package/build/serializer/Serializer.js.map +2 -2
- package/build/{Debug.d.ts → src/Debug.d.ts} +2 -1
- package/build/{IPC.d.ts → src/IPC.d.ts} +2 -2
- package/build/{MatchMaker.d.ts → src/MatchMaker.d.ts} +58 -33
- package/build/src/Protocol.d.ts +53 -0
- package/build/src/Room.d.ts +497 -0
- package/build/{Server.d.ts → src/Server.d.ts} +23 -25
- package/build/{Transport.d.ts → src/Transport.d.ts} +65 -22
- package/build/{errors → src/errors}/RoomExceptions.d.ts +8 -7
- package/build/src/index.d.ts +27 -0
- package/build/src/matchmaker/Lobby.d.ts +4 -0
- package/build/src/matchmaker/LocalDriver/LocalDriver.d.ts +17 -0
- package/build/src/matchmaker/LocalDriver/Query.d.ts +12 -0
- package/build/src/matchmaker/RegisteredHandler.d.ts +81 -0
- package/build/{matchmaker → src/matchmaker}/controller.d.ts +5 -6
- package/build/src/matchmaker/driver/api.d.ts +145 -0
- package/build/src/matchmaker/driver/local/LocalDriver.d.ts +17 -0
- package/build/src/matchmaker/driver/local/Query.d.ts +12 -0
- package/build/src/matchmaker/driver.d.ts +145 -0
- package/build/src/matchmaker/routes.d.ts +92 -0
- package/build/{presence → src/presence}/LocalPresence.d.ts +1 -1
- package/build/{presence → src/presence}/Presence.d.ts +2 -0
- package/build/{rooms → src/rooms}/LobbyRoom.d.ts +4 -4
- package/build/src/rooms/RankedQueueRoom.d.ts +125 -0
- package/build/{rooms → src/rooms}/RelayRoom.d.ts +5 -4
- package/build/src/rooms/createRoom.d.ts +65 -0
- package/build/src/router/default_routes.d.ts +103 -0
- package/build/src/router/index.d.ts +68 -0
- package/build/{serializer → src/serializer}/NoneSerializer.d.ts +2 -2
- package/build/{serializer → src/serializer}/SchemaSerializer.d.ts +9 -9
- package/build/{serializer → src/serializer}/Serializer.d.ts +3 -3
- package/build/{utils → src/utils}/DevMode.d.ts +5 -4
- package/build/{utils → src/utils}/StandardSchema.d.ts +1 -1
- package/build/{utils → src/utils}/Utils.d.ts +15 -4
- package/build/utils/DevMode.js +54 -26
- package/build/utils/DevMode.js.map +3 -3
- package/build/utils/DevMode.mjs +44 -19
- package/build/utils/DevMode.mjs.map +2 -2
- package/build/utils/StandardSchema.js.map +1 -1
- package/build/utils/StandardSchema.mjs.map +1 -1
- package/build/utils/Utils.js +8 -15
- package/build/utils/Utils.js.map +3 -3
- package/build/utils/Utils.mjs +6 -4
- package/build/utils/Utils.mjs.map +2 -2
- package/package.json +20 -14
- package/build/Protocol.d.ts +0 -37
- package/build/Room.d.ts +0 -265
- package/build/discovery/index.d.ts +0 -8
- package/build/discovery/index.js +0 -50
- package/build/discovery/index.js.map +0 -7
- package/build/discovery/index.mjs +0 -26
- package/build/discovery/index.mjs.map +0 -7
- package/build/index.d.ts +0 -24
- package/build/matchmaker/Lobby.d.ts +0 -4
- package/build/matchmaker/RegisteredHandler.d.ts +0 -19
- package/build/matchmaker/driver/Query.d.ts +0 -8
- package/build/matchmaker/driver/Query.js +0 -68
- package/build/matchmaker/driver/Query.js.map +0 -7
- package/build/matchmaker/driver/Query.mjs +0 -45
- package/build/matchmaker/driver/Query.mjs.map +0 -7
- package/build/matchmaker/driver/RoomData.d.ts +0 -19
- package/build/matchmaker/driver/RoomData.js +0 -79
- package/build/matchmaker/driver/RoomData.js.map +0 -7
- package/build/matchmaker/driver/RoomData.mjs +0 -56
- package/build/matchmaker/driver/RoomData.mjs.map +0 -7
- package/build/matchmaker/driver/api.d.ts +0 -104
- package/build/matchmaker/driver/index.d.ts +0 -13
- package/build/matchmaker/driver/index.js +0 -64
- package/build/matchmaker/driver/index.js.map +0 -7
- package/build/matchmaker/driver/index.mjs +0 -42
- package/build/matchmaker/driver/index.mjs.map +0 -7
- package/build/matchmaker/driver/interfaces.d.ts +0 -73
- package/build/matchmaker/driver/interfaces.js +0 -15
- package/build/matchmaker/driver/interfaces.js.map +0 -7
- package/build/matchmaker/driver/interfaces.mjs +0 -0
- package/build/matchmaker/driver/interfaces.mjs.map +0 -7
- package/build/matchmaker/driver/local/LocalDriver.d.ts +0 -13
- package/build/matchmaker/driver/local/Query.d.ts +0 -9
- package/build/matchmaker/driver/local/RoomData.d.ts +0 -19
- package/build/matchmaker/driver/local/RoomData.js +0 -79
- package/build/matchmaker/driver/local/RoomData.js.map +0 -7
- package/build/matchmaker/driver/local/RoomData.mjs +0 -57
- package/build/matchmaker/driver/local/RoomData.mjs.map +0 -7
- package/build/utils/types.d.ts +0 -1
- package/build/utils/types.js +0 -15
- package/build/utils/types.js.map +0 -7
- package/build/utils/types.mjs +0 -0
- package/build/utils/types.mjs.map +0 -7
- /package/build/{Logger.d.ts → src/Logger.d.ts} +0 -0
- /package/build/{Stats.d.ts → src/Stats.d.ts} +0 -0
- /package/build/{errors → src/errors}/SeatReservationError.d.ts +0 -0
- /package/build/{errors → src/errors}/ServerError.d.ts +0 -0
- /package/build/{serializer → src/serializer}/SchemaSerializerDebug.d.ts +0 -0
- /package/build/{utils → src/utils}/nanoevents.d.ts +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/presence/LocalPresence.ts"],
|
|
4
|
-
"sourcesContent": ["import fs from 'fs';\nimport path from 'path';\n\nimport { EventEmitter } from 'events';\nimport { spliceOne } from '../utils/Utils.js';\nimport { Presence } from './Presence.js';\n\nimport { isDevMode } from '../utils/DevMode.js';\n\ntype Callback = (...args: any[]) => void;\n\nconst DEVMODE_CACHE_FILE_PATH = path.resolve(\".devmode.json\");\n\nexport class LocalPresence implements Presence {\n public subscriptions = new EventEmitter();\n\n public data: {[roomName: string]: string[]} = {};\n public hash: {[roomName: string]: {[key: string]: string}} = {};\n\n public keys: {[name: string]: string | number} = {};\n\n private timeouts: {[name: string]: NodeJS.Timeout} = {};\n\n constructor() {\n //\n // reload from local cache on devMode\n //\n if (\n isDevMode &&\n fs.existsSync(DEVMODE_CACHE_FILE_PATH)\n ) {\n const cache = fs.readFileSync(DEVMODE_CACHE_FILE_PATH).toString('utf-8') || \"{}\";\n const parsed = JSON.parse(cache);\n if (parsed.data) { this.data = parsed.data; }\n if (parsed.hash) { this.hash = parsed.hash; }\n if (parsed.keys) { this.keys = parsed.keys; }\n }\n }\n\n public subscribe(topic: string, callback: (...args: any[]) => void) {\n this.subscriptions.on(topic, callback);\n return Promise.resolve(this);\n }\n\n public unsubscribe(topic: string, callback?: Callback) {\n if (callback) {\n this.subscriptions.removeListener(topic, callback);\n\n } else {\n this.subscriptions.removeAllListeners(topic);\n }\n\n return this;\n }\n\n public publish(topic: string, data: any) {\n this.subscriptions.emit(topic, data);\n return this;\n }\n\n public async channels (pattern?: string) {\n let eventNames = this.subscriptions.eventNames() as string[];\n if (pattern) {\n //\n // This is a limited glob pattern to regexp implementation.\n // If needed, we can use a full implementation like picomatch: https://github.com/micromatch/picomatch/\n //\n const regexp = new RegExp(\n pattern.\n replaceAll(\".\", \"\\\\.\").\n replaceAll(\"$\", \"\\\\$\").\n replaceAll(\"*\", \".*\").\n replaceAll(\"?\", \".\"),\n \"gi\"\n );\n eventNames = eventNames.filter((eventName) => regexp.test(eventName));\n }\n return eventNames;\n }\n\n public async exists(key: string): Promise<boolean> {\n return (\n this.keys[key] !== undefined ||\n this.data[key] !== undefined ||\n this.hash[key] !== undefined\n );\n }\n\n public set(key: string, value: string) {\n this.keys[key] = value;\n }\n\n public setex(key: string, value: string, seconds: number) {\n this.keys[key] = value;\n this.expire(key, seconds);\n }\n\n public expire(key: string, seconds: number) {\n // ensure previous timeout is clear before setting another one.\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.keys[key];\n delete this.timeouts[key];\n }, seconds * 1000);\n }\n\n public get(key: string) {\n return this.keys[key];\n }\n\n public del(key: string) {\n delete this.keys[key];\n delete this.data[key];\n delete this.hash[key];\n }\n\n public sadd(key: string, value: any) {\n if (!this.data[key]) {\n this.data[key] = [];\n }\n\n if (this.data[key].indexOf(value) === -1) {\n this.data[key].push(value);\n }\n }\n\n public async smembers(key: string): Promise<string[]> {\n return this.data[key] || [];\n }\n\n public async sismember(key: string, field: string) {\n return this.data[key] && this.data[key].includes(field) ? 1 : 0;\n }\n\n public srem(key: string, value: any) {\n if (this.data[key]) {\n spliceOne(this.data[key], this.data[key].indexOf(value));\n }\n }\n\n public scard(key: string) {\n return (this.data[key] || []).length;\n }\n\n public async sinter(...keys: string[]) {\n const intersection: {[value: string]: number} = {};\n\n for (let i = 0, l = keys.length; i < l; i++) {\n (await this.smembers(keys[i])).forEach((member) => {\n if (!intersection[member]) {\n intersection[member] = 0;\n }\n\n intersection[member]++;\n });\n }\n\n return Object.keys(intersection).reduce((prev, curr) => {\n if (intersection[curr] > 1) {\n prev.push(curr);\n }\n return prev;\n }, []);\n }\n\n public hset(key: string, field: string, value: string) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n this.hash[key][field] = value;\n return Promise.resolve(true);\n }\n\n public hincrby(key: string, field: string, incrBy: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n return Promise.resolve(value);\n }\n\n public hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n\n //\n // FIXME: delete only hash[key][field]\n // (we can't use \"HEXPIRE\" in Redis because it's only available since Redis version 7.4.0+)\n //\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.hash[key];\n delete this.timeouts[key];\n }, expireInSeconds * 1000);\n\n return Promise.resolve(value);\n }\n\n public async hget(key: string, field: string) {\n return (typeof(this.hash[key]) === 'object')\n ? this.hash[key][field] ?? null\n : null;\n }\n\n public async hgetall(key: string) {\n return this.hash[key] || {};\n }\n\n public hdel(key: string, field: any) {\n const success = this.hash?.[key]?.[field] !== undefined;\n if (success) {\n delete this.hash[key][field];\n }\n return Promise.resolve(success);\n }\n\n public async hlen(key: string) {\n return this.hash[key] && Object.keys(this.hash[key]).length || 0;\n }\n\n public async incr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)++;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public async decr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)--;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public llen(key: string) {\n return Promise.resolve((this.data[key] && this.data[key].length) || 0);\n }\n\n public rpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].push(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].unshift(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpop(key: string): Promise<string> {\n return Promise.resolve(Array.isArray(this.data[key])\n ? this.data[key].shift()\n : null);\n }\n\n public rpop(key: string): Promise<string | null> {\n return Promise.resolve(this.data[key].pop());\n }\n\n public brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null> {\n const keys = args.slice(0, -1) as string[];\n const timeoutInSeconds = args[args.length - 1] as number;\n\n const getFirstPopulated = (): [string, string] | null => {\n const keyWithValue = keys.find(key => this.data[key] && this.data[key].length > 0);\n if (keyWithValue) {\n return [keyWithValue, this.data[keyWithValue].pop()];\n } else {\n return null;\n }\n }\n\n const firstPopulated = getFirstPopulated();\n\n if (firstPopulated) {\n // return first populated key + item\n return Promise.resolve(firstPopulated);\n\n } else {\n // 8 retries per second\n const maxRetries = timeoutInSeconds * 8;\n\n let tries = 0;\n return new Promise((resolve) => {\n const interval = setInterval(() => {\n tries++;\n\n const firstPopulated = getFirstPopulated();\n if (firstPopulated) {\n clearInterval(interval);\n return resolve(firstPopulated);\n\n } else if (tries >= maxRetries) {\n clearInterval(interval);\n return resolve(null);\n }\n\n }, (timeoutInSeconds * 1000) / maxRetries);\n });\n }\n }\n\n public setMaxListeners(number: number) {\n this.subscriptions.setMaxListeners(number);\n }\n\n public shutdown() {\n if (isDevMode) {\n const cache = JSON.stringify({\n data: this.data,\n hash: this.hash,\n keys: this.keys\n });\n fs.writeFileSync(DEVMODE_CACHE_FILE_PATH, cache, { encoding: \"utf-8\" });\n }\n }\n\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
4
|
+
"sourcesContent": ["\nimport { EventEmitter } from 'events';\nimport { spliceOne } from '../utils/Utils.ts';\nimport type { Presence } from './Presence.ts';\n\nimport { hasDevModeCache, isDevMode, getDevModeCache, writeDevModeCache } from '../utils/DevMode.ts';\n\ntype Callback = (...args: any[]) => void;\n\nexport class LocalPresence implements Presence {\n public subscriptions = new EventEmitter();\n\n public data: {[roomName: string]: string[]} = {};\n public hash: {[roomName: string]: {[key: string]: string}} = {};\n\n public keys: {[name: string]: string | number} = {};\n\n private timeouts: {[name: string]: NodeJS.Timeout} = {};\n\n constructor() {\n //\n // reload from local cache on devMode\n //\n if (\n isDevMode &&\n hasDevModeCache()\n ) {\n const cache = getDevModeCache();\n if (cache.data) { this.data = cache.data; }\n if (cache.hash) { this.hash = cache.hash; }\n if (cache.keys) { this.keys = cache.keys; }\n }\n }\n\n public subscribe(topic: string, callback: (...args: any[]) => void) {\n this.subscriptions.on(topic, callback);\n return Promise.resolve(this);\n }\n\n public unsubscribe(topic: string, callback?: Callback) {\n if (callback) {\n this.subscriptions.removeListener(topic, callback);\n\n } else {\n this.subscriptions.removeAllListeners(topic);\n }\n\n return this;\n }\n\n public publish(topic: string, data: any) {\n this.subscriptions.emit(topic, data);\n return this;\n }\n\n public async channels (pattern?: string) {\n let eventNames = this.subscriptions.eventNames() as string[];\n if (pattern) {\n //\n // This is a limited glob pattern to regexp implementation.\n // If needed, we can use a full implementation like picomatch: https://github.com/micromatch/picomatch/\n //\n const regexp = new RegExp(\n pattern.\n replaceAll(\".\", \"\\\\.\").\n replaceAll(\"$\", \"\\\\$\").\n replaceAll(\"*\", \".*\").\n replaceAll(\"?\", \".\"),\n \"i\"\n );\n eventNames = eventNames.filter((eventName) => regexp.test(eventName));\n }\n return eventNames;\n }\n\n public async exists(key: string): Promise<boolean> {\n return (\n this.keys[key] !== undefined ||\n this.data[key] !== undefined ||\n this.hash[key] !== undefined\n );\n }\n\n public set(key: string, value: string) {\n this.keys[key] = value;\n }\n\n public setex(key: string, value: string, seconds: number) {\n this.keys[key] = value;\n this.expire(key, seconds);\n }\n\n public expire(key: string, seconds: number) {\n // ensure previous timeout is clear before setting another one.\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.keys[key];\n delete this.timeouts[key];\n }, seconds * 1000);\n }\n\n public get(key: string) {\n return this.keys[key];\n }\n\n public del(key: string) {\n delete this.keys[key];\n delete this.data[key];\n delete this.hash[key];\n }\n\n public sadd(key: string, value: any) {\n if (!this.data[key]) {\n this.data[key] = [];\n }\n\n if (this.data[key].indexOf(value) === -1) {\n this.data[key].push(value);\n }\n }\n\n public async smembers(key: string): Promise<string[]> {\n return this.data[key] || [];\n }\n\n public async sismember(key: string, field: string) {\n return this.data[key] && this.data[key].includes(field) ? 1 : 0;\n }\n\n public srem(key: string, value: any) {\n if (this.data[key]) {\n spliceOne(this.data[key], this.data[key].indexOf(value));\n }\n }\n\n public scard(key: string) {\n return (this.data[key] || []).length;\n }\n\n public async sinter(...keys: string[]) {\n const intersection: {[value: string]: number} = {};\n\n for (let i = 0, l = keys.length; i < l; i++) {\n (await this.smembers(keys[i])).forEach((member) => {\n if (!intersection[member]) {\n intersection[member] = 0;\n }\n\n intersection[member]++;\n });\n }\n\n return Object.keys(intersection).reduce((prev, curr) => {\n if (intersection[curr] > 1) {\n prev.push(curr);\n }\n return prev;\n }, []);\n }\n\n public hset(key: string, field: string, value: string) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n this.hash[key][field] = value;\n return Promise.resolve(true);\n }\n\n public hincrby(key: string, field: string, incrBy: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n return Promise.resolve(value);\n }\n\n public hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n\n //\n // FIXME: delete only hash[key][field]\n // (we can't use \"HEXPIRE\" in Redis because it's only available since Redis version 7.4.0+)\n //\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.hash[key];\n delete this.timeouts[key];\n }, expireInSeconds * 1000);\n\n return Promise.resolve(value);\n }\n\n public async hget(key: string, field: string) {\n return (typeof(this.hash[key]) === 'object')\n ? this.hash[key][field] ?? null\n : null;\n }\n\n public async hgetall(key: string) {\n return this.hash[key] || {};\n }\n\n public hdel(key: string, field: any) {\n const success = this.hash?.[key]?.[field] !== undefined;\n if (success) {\n delete this.hash[key][field];\n }\n return Promise.resolve(success);\n }\n\n public async hlen(key: string) {\n return this.hash[key] && Object.keys(this.hash[key]).length || 0;\n }\n\n public async incr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)++;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public async decr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)--;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public llen(key: string) {\n return Promise.resolve((this.data[key] && this.data[key].length) || 0);\n }\n\n public rpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].push(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].unshift(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpop(key: string): Promise<string> {\n return Promise.resolve(Array.isArray(this.data[key])\n ? this.data[key].shift()\n : null);\n }\n\n public rpop(key: string): Promise<string | null> {\n return Promise.resolve(this.data[key].pop());\n }\n\n public brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null> {\n const keys = args.slice(0, -1) as string[];\n const timeoutInSeconds = args[args.length - 1] as number;\n\n const getFirstPopulated = (): [string, string] | null => {\n const keyWithValue = keys.find(key => this.data[key] && this.data[key].length > 0);\n if (keyWithValue) {\n return [keyWithValue, this.data[keyWithValue].pop()];\n } else {\n return null;\n }\n }\n\n const firstPopulated = getFirstPopulated();\n\n if (firstPopulated) {\n // return first populated key + item\n return Promise.resolve(firstPopulated);\n\n } else {\n // 8 retries per second\n const maxRetries = timeoutInSeconds * 8;\n\n let tries = 0;\n return new Promise((resolve) => {\n const interval = setInterval(() => {\n tries++;\n\n const firstPopulated = getFirstPopulated();\n if (firstPopulated) {\n clearInterval(interval);\n return resolve(firstPopulated);\n\n } else if (tries >= maxRetries) {\n clearInterval(interval);\n return resolve(null);\n }\n\n }, (timeoutInSeconds * 1000) / maxRetries);\n });\n }\n }\n\n public setMaxListeners(number: number) {\n this.subscriptions.setMaxListeners(number);\n }\n\n public shutdown() {\n if (isDevMode) {\n writeDevModeCache({\n data: this.data,\n hash: this.hash,\n keys: this.keys\n });\n }\n }\n\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA6B;AAC7B,mBAA0B;AAG1B,qBA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2
|
import { EventEmitter } from "events";
|
|
5
3
|
import { spliceOne } from "../utils/Utils.mjs";
|
|
6
|
-
import { isDevMode } from "../utils/DevMode.mjs";
|
|
7
|
-
var DEVMODE_CACHE_FILE_PATH = path.resolve(".devmode.json");
|
|
4
|
+
import { hasDevModeCache, isDevMode, getDevModeCache, writeDevModeCache } from "../utils/DevMode.mjs";
|
|
8
5
|
var LocalPresence = class {
|
|
9
6
|
constructor() {
|
|
10
7
|
this.subscriptions = new EventEmitter();
|
|
@@ -12,17 +9,16 @@ var LocalPresence = class {
|
|
|
12
9
|
this.hash = {};
|
|
13
10
|
this.keys = {};
|
|
14
11
|
this.timeouts = {};
|
|
15
|
-
if (isDevMode &&
|
|
16
|
-
const cache =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.data = parsed.data;
|
|
12
|
+
if (isDevMode && hasDevModeCache()) {
|
|
13
|
+
const cache = getDevModeCache();
|
|
14
|
+
if (cache.data) {
|
|
15
|
+
this.data = cache.data;
|
|
20
16
|
}
|
|
21
|
-
if (
|
|
22
|
-
this.hash =
|
|
17
|
+
if (cache.hash) {
|
|
18
|
+
this.hash = cache.hash;
|
|
23
19
|
}
|
|
24
|
-
if (
|
|
25
|
-
this.keys =
|
|
20
|
+
if (cache.keys) {
|
|
21
|
+
this.keys = cache.keys;
|
|
26
22
|
}
|
|
27
23
|
}
|
|
28
24
|
}
|
|
@@ -47,7 +43,7 @@ var LocalPresence = class {
|
|
|
47
43
|
if (pattern) {
|
|
48
44
|
const regexp = new RegExp(
|
|
49
45
|
pattern.replaceAll(".", "\\.").replaceAll("$", "\\$").replaceAll("*", ".*").replaceAll("?", "."),
|
|
50
|
-
"
|
|
46
|
+
"i"
|
|
51
47
|
);
|
|
52
48
|
eventNames = eventNames.filter((eventName) => regexp.test(eventName));
|
|
53
49
|
}
|
|
@@ -247,12 +243,11 @@ var LocalPresence = class {
|
|
|
247
243
|
}
|
|
248
244
|
shutdown() {
|
|
249
245
|
if (isDevMode) {
|
|
250
|
-
|
|
246
|
+
writeDevModeCache({
|
|
251
247
|
data: this.data,
|
|
252
248
|
hash: this.hash,
|
|
253
249
|
keys: this.keys
|
|
254
250
|
});
|
|
255
|
-
fs.writeFileSync(DEVMODE_CACHE_FILE_PATH, cache, { encoding: "utf-8" });
|
|
256
251
|
}
|
|
257
252
|
}
|
|
258
253
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/presence/LocalPresence.ts"],
|
|
4
|
-
"sourcesContent": ["import fs from 'fs';\nimport path from 'path';\n\nimport { EventEmitter } from 'events';\nimport { spliceOne } from '../utils/Utils.js';\nimport { Presence } from './Presence.js';\n\nimport { isDevMode } from '../utils/DevMode.js';\n\ntype Callback = (...args: any[]) => void;\n\nconst DEVMODE_CACHE_FILE_PATH = path.resolve(\".devmode.json\");\n\nexport class LocalPresence implements Presence {\n public subscriptions = new EventEmitter();\n\n public data: {[roomName: string]: string[]} = {};\n public hash: {[roomName: string]: {[key: string]: string}} = {};\n\n public keys: {[name: string]: string | number} = {};\n\n private timeouts: {[name: string]: NodeJS.Timeout} = {};\n\n constructor() {\n //\n // reload from local cache on devMode\n //\n if (\n isDevMode &&\n fs.existsSync(DEVMODE_CACHE_FILE_PATH)\n ) {\n const cache = fs.readFileSync(DEVMODE_CACHE_FILE_PATH).toString('utf-8') || \"{}\";\n const parsed = JSON.parse(cache);\n if (parsed.data) { this.data = parsed.data; }\n if (parsed.hash) { this.hash = parsed.hash; }\n if (parsed.keys) { this.keys = parsed.keys; }\n }\n }\n\n public subscribe(topic: string, callback: (...args: any[]) => void) {\n this.subscriptions.on(topic, callback);\n return Promise.resolve(this);\n }\n\n public unsubscribe(topic: string, callback?: Callback) {\n if (callback) {\n this.subscriptions.removeListener(topic, callback);\n\n } else {\n this.subscriptions.removeAllListeners(topic);\n }\n\n return this;\n }\n\n public publish(topic: string, data: any) {\n this.subscriptions.emit(topic, data);\n return this;\n }\n\n public async channels (pattern?: string) {\n let eventNames = this.subscriptions.eventNames() as string[];\n if (pattern) {\n //\n // This is a limited glob pattern to regexp implementation.\n // If needed, we can use a full implementation like picomatch: https://github.com/micromatch/picomatch/\n //\n const regexp = new RegExp(\n pattern.\n replaceAll(\".\", \"\\\\.\").\n replaceAll(\"$\", \"\\\\$\").\n replaceAll(\"*\", \".*\").\n replaceAll(\"?\", \".\"),\n \"gi\"\n );\n eventNames = eventNames.filter((eventName) => regexp.test(eventName));\n }\n return eventNames;\n }\n\n public async exists(key: string): Promise<boolean> {\n return (\n this.keys[key] !== undefined ||\n this.data[key] !== undefined ||\n this.hash[key] !== undefined\n );\n }\n\n public set(key: string, value: string) {\n this.keys[key] = value;\n }\n\n public setex(key: string, value: string, seconds: number) {\n this.keys[key] = value;\n this.expire(key, seconds);\n }\n\n public expire(key: string, seconds: number) {\n // ensure previous timeout is clear before setting another one.\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.keys[key];\n delete this.timeouts[key];\n }, seconds * 1000);\n }\n\n public get(key: string) {\n return this.keys[key];\n }\n\n public del(key: string) {\n delete this.keys[key];\n delete this.data[key];\n delete this.hash[key];\n }\n\n public sadd(key: string, value: any) {\n if (!this.data[key]) {\n this.data[key] = [];\n }\n\n if (this.data[key].indexOf(value) === -1) {\n this.data[key].push(value);\n }\n }\n\n public async smembers(key: string): Promise<string[]> {\n return this.data[key] || [];\n }\n\n public async sismember(key: string, field: string) {\n return this.data[key] && this.data[key].includes(field) ? 1 : 0;\n }\n\n public srem(key: string, value: any) {\n if (this.data[key]) {\n spliceOne(this.data[key], this.data[key].indexOf(value));\n }\n }\n\n public scard(key: string) {\n return (this.data[key] || []).length;\n }\n\n public async sinter(...keys: string[]) {\n const intersection: {[value: string]: number} = {};\n\n for (let i = 0, l = keys.length; i < l; i++) {\n (await this.smembers(keys[i])).forEach((member) => {\n if (!intersection[member]) {\n intersection[member] = 0;\n }\n\n intersection[member]++;\n });\n }\n\n return Object.keys(intersection).reduce((prev, curr) => {\n if (intersection[curr] > 1) {\n prev.push(curr);\n }\n return prev;\n }, []);\n }\n\n public hset(key: string, field: string, value: string) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n this.hash[key][field] = value;\n return Promise.resolve(true);\n }\n\n public hincrby(key: string, field: string, incrBy: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n return Promise.resolve(value);\n }\n\n public hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n\n //\n // FIXME: delete only hash[key][field]\n // (we can't use \"HEXPIRE\" in Redis because it's only available since Redis version 7.4.0+)\n //\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.hash[key];\n delete this.timeouts[key];\n }, expireInSeconds * 1000);\n\n return Promise.resolve(value);\n }\n\n public async hget(key: string, field: string) {\n return (typeof(this.hash[key]) === 'object')\n ? this.hash[key][field] ?? null\n : null;\n }\n\n public async hgetall(key: string) {\n return this.hash[key] || {};\n }\n\n public hdel(key: string, field: any) {\n const success = this.hash?.[key]?.[field] !== undefined;\n if (success) {\n delete this.hash[key][field];\n }\n return Promise.resolve(success);\n }\n\n public async hlen(key: string) {\n return this.hash[key] && Object.keys(this.hash[key]).length || 0;\n }\n\n public async incr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)++;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public async decr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)--;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public llen(key: string) {\n return Promise.resolve((this.data[key] && this.data[key].length) || 0);\n }\n\n public rpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].push(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].unshift(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpop(key: string): Promise<string> {\n return Promise.resolve(Array.isArray(this.data[key])\n ? this.data[key].shift()\n : null);\n }\n\n public rpop(key: string): Promise<string | null> {\n return Promise.resolve(this.data[key].pop());\n }\n\n public brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null> {\n const keys = args.slice(0, -1) as string[];\n const timeoutInSeconds = args[args.length - 1] as number;\n\n const getFirstPopulated = (): [string, string] | null => {\n const keyWithValue = keys.find(key => this.data[key] && this.data[key].length > 0);\n if (keyWithValue) {\n return [keyWithValue, this.data[keyWithValue].pop()];\n } else {\n return null;\n }\n }\n\n const firstPopulated = getFirstPopulated();\n\n if (firstPopulated) {\n // return first populated key + item\n return Promise.resolve(firstPopulated);\n\n } else {\n // 8 retries per second\n const maxRetries = timeoutInSeconds * 8;\n\n let tries = 0;\n return new Promise((resolve) => {\n const interval = setInterval(() => {\n tries++;\n\n const firstPopulated = getFirstPopulated();\n if (firstPopulated) {\n clearInterval(interval);\n return resolve(firstPopulated);\n\n } else if (tries >= maxRetries) {\n clearInterval(interval);\n return resolve(null);\n }\n\n }, (timeoutInSeconds * 1000) / maxRetries);\n });\n }\n }\n\n public setMaxListeners(number: number) {\n this.subscriptions.setMaxListeners(number);\n }\n\n public shutdown() {\n if (isDevMode) {\n const cache = JSON.stringify({\n data: this.data,\n hash: this.hash,\n keys: this.keys\n });\n fs.writeFileSync(DEVMODE_CACHE_FILE_PATH, cache, { encoding: \"utf-8\" });\n }\n }\n\n}\n"],
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["\nimport { EventEmitter } from 'events';\nimport { spliceOne } from '../utils/Utils.ts';\nimport type { Presence } from './Presence.ts';\n\nimport { hasDevModeCache, isDevMode, getDevModeCache, writeDevModeCache } from '../utils/DevMode.ts';\n\ntype Callback = (...args: any[]) => void;\n\nexport class LocalPresence implements Presence {\n public subscriptions = new EventEmitter();\n\n public data: {[roomName: string]: string[]} = {};\n public hash: {[roomName: string]: {[key: string]: string}} = {};\n\n public keys: {[name: string]: string | number} = {};\n\n private timeouts: {[name: string]: NodeJS.Timeout} = {};\n\n constructor() {\n //\n // reload from local cache on devMode\n //\n if (\n isDevMode &&\n hasDevModeCache()\n ) {\n const cache = getDevModeCache();\n if (cache.data) { this.data = cache.data; }\n if (cache.hash) { this.hash = cache.hash; }\n if (cache.keys) { this.keys = cache.keys; }\n }\n }\n\n public subscribe(topic: string, callback: (...args: any[]) => void) {\n this.subscriptions.on(topic, callback);\n return Promise.resolve(this);\n }\n\n public unsubscribe(topic: string, callback?: Callback) {\n if (callback) {\n this.subscriptions.removeListener(topic, callback);\n\n } else {\n this.subscriptions.removeAllListeners(topic);\n }\n\n return this;\n }\n\n public publish(topic: string, data: any) {\n this.subscriptions.emit(topic, data);\n return this;\n }\n\n public async channels (pattern?: string) {\n let eventNames = this.subscriptions.eventNames() as string[];\n if (pattern) {\n //\n // This is a limited glob pattern to regexp implementation.\n // If needed, we can use a full implementation like picomatch: https://github.com/micromatch/picomatch/\n //\n const regexp = new RegExp(\n pattern.\n replaceAll(\".\", \"\\\\.\").\n replaceAll(\"$\", \"\\\\$\").\n replaceAll(\"*\", \".*\").\n replaceAll(\"?\", \".\"),\n \"i\"\n );\n eventNames = eventNames.filter((eventName) => regexp.test(eventName));\n }\n return eventNames;\n }\n\n public async exists(key: string): Promise<boolean> {\n return (\n this.keys[key] !== undefined ||\n this.data[key] !== undefined ||\n this.hash[key] !== undefined\n );\n }\n\n public set(key: string, value: string) {\n this.keys[key] = value;\n }\n\n public setex(key: string, value: string, seconds: number) {\n this.keys[key] = value;\n this.expire(key, seconds);\n }\n\n public expire(key: string, seconds: number) {\n // ensure previous timeout is clear before setting another one.\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.keys[key];\n delete this.timeouts[key];\n }, seconds * 1000);\n }\n\n public get(key: string) {\n return this.keys[key];\n }\n\n public del(key: string) {\n delete this.keys[key];\n delete this.data[key];\n delete this.hash[key];\n }\n\n public sadd(key: string, value: any) {\n if (!this.data[key]) {\n this.data[key] = [];\n }\n\n if (this.data[key].indexOf(value) === -1) {\n this.data[key].push(value);\n }\n }\n\n public async smembers(key: string): Promise<string[]> {\n return this.data[key] || [];\n }\n\n public async sismember(key: string, field: string) {\n return this.data[key] && this.data[key].includes(field) ? 1 : 0;\n }\n\n public srem(key: string, value: any) {\n if (this.data[key]) {\n spliceOne(this.data[key], this.data[key].indexOf(value));\n }\n }\n\n public scard(key: string) {\n return (this.data[key] || []).length;\n }\n\n public async sinter(...keys: string[]) {\n const intersection: {[value: string]: number} = {};\n\n for (let i = 0, l = keys.length; i < l; i++) {\n (await this.smembers(keys[i])).forEach((member) => {\n if (!intersection[member]) {\n intersection[member] = 0;\n }\n\n intersection[member]++;\n });\n }\n\n return Object.keys(intersection).reduce((prev, curr) => {\n if (intersection[curr] > 1) {\n prev.push(curr);\n }\n return prev;\n }, []);\n }\n\n public hset(key: string, field: string, value: string) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n this.hash[key][field] = value;\n return Promise.resolve(true);\n }\n\n public hincrby(key: string, field: string, incrBy: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n return Promise.resolve(value);\n }\n\n public hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n\n //\n // FIXME: delete only hash[key][field]\n // (we can't use \"HEXPIRE\" in Redis because it's only available since Redis version 7.4.0+)\n //\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.hash[key];\n delete this.timeouts[key];\n }, expireInSeconds * 1000);\n\n return Promise.resolve(value);\n }\n\n public async hget(key: string, field: string) {\n return (typeof(this.hash[key]) === 'object')\n ? this.hash[key][field] ?? null\n : null;\n }\n\n public async hgetall(key: string) {\n return this.hash[key] || {};\n }\n\n public hdel(key: string, field: any) {\n const success = this.hash?.[key]?.[field] !== undefined;\n if (success) {\n delete this.hash[key][field];\n }\n return Promise.resolve(success);\n }\n\n public async hlen(key: string) {\n return this.hash[key] && Object.keys(this.hash[key]).length || 0;\n }\n\n public async incr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)++;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public async decr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)--;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public llen(key: string) {\n return Promise.resolve((this.data[key] && this.data[key].length) || 0);\n }\n\n public rpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].push(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpush(key: string, ...values: string[]): Promise<number> {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].unshift(value);\n });\n\n return Promise.resolve(lastLength);\n }\n\n public lpop(key: string): Promise<string> {\n return Promise.resolve(Array.isArray(this.data[key])\n ? this.data[key].shift()\n : null);\n }\n\n public rpop(key: string): Promise<string | null> {\n return Promise.resolve(this.data[key].pop());\n }\n\n public brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null> {\n const keys = args.slice(0, -1) as string[];\n const timeoutInSeconds = args[args.length - 1] as number;\n\n const getFirstPopulated = (): [string, string] | null => {\n const keyWithValue = keys.find(key => this.data[key] && this.data[key].length > 0);\n if (keyWithValue) {\n return [keyWithValue, this.data[keyWithValue].pop()];\n } else {\n return null;\n }\n }\n\n const firstPopulated = getFirstPopulated();\n\n if (firstPopulated) {\n // return first populated key + item\n return Promise.resolve(firstPopulated);\n\n } else {\n // 8 retries per second\n const maxRetries = timeoutInSeconds * 8;\n\n let tries = 0;\n return new Promise((resolve) => {\n const interval = setInterval(() => {\n tries++;\n\n const firstPopulated = getFirstPopulated();\n if (firstPopulated) {\n clearInterval(interval);\n return resolve(firstPopulated);\n\n } else if (tries >= maxRetries) {\n clearInterval(interval);\n return resolve(null);\n }\n\n }, (timeoutInSeconds * 1000) / maxRetries);\n });\n }\n }\n\n public setMaxListeners(number: number) {\n this.subscriptions.setMaxListeners(number);\n }\n\n public shutdown() {\n if (isDevMode) {\n writeDevModeCache({\n data: this.data,\n hash: this.hash,\n keys: this.keys\n });\n }\n }\n\n}\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,oBAAoB;AAC7B,SAAS,iBAAiB;AAG1B,SAAS,iBAAiB,WAAW,iBAAiB,yBAAyB;AAIxE,IAAM,gBAAN,MAAwC;AAAA,EAU3C,cAAc;AATd,SAAO,gBAAgB,IAAI,aAAa;AAExC,SAAO,OAAuC,CAAC;AAC/C,SAAO,OAAsD,CAAC;AAE9D,SAAO,OAA0C,CAAC;AAElD,SAAQ,WAA6C,CAAC;AAMpD,QACE,aACA,gBAAgB,GAChB;AACA,YAAM,QAAQ,gBAAgB;AAC9B,UAAI,MAAM,MAAM;AAAE,aAAK,OAAO,MAAM;AAAA,MAAM;AAC1C,UAAI,MAAM,MAAM;AAAE,aAAK,OAAO,MAAM;AAAA,MAAM;AAC1C,UAAI,MAAM,MAAM;AAAE,aAAK,OAAO,MAAM;AAAA,MAAM;AAAA,IAC5C;AAAA,EACF;AAAA,EA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mBAAmB;AAEtC,UAAI,QAAQ;AACZ,aAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,cAAM,WAAW,YAAY,MAAM;AACjC;AAEA,gBAAMA,kBAAiB,kBAAkB;AACzC,cAAIA,iBAAgB;AAClB,0BAAc,QAAQ;AACtB,mBAAO,QAAQA,eAAc;AAAA,UAE/B,WAAW,SAAS,YAAY;AAC9B,0BAAc,QAAQ;AACtB,mBAAO,QAAQ,IAAI;AAAA,UACrB;AAAA,QAEF,GAAI,mBAAmB,MAAQ,UAAU;AAAA,MAC3C,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEO,gBAAgB,QAAgB;AACrC,SAAK,cAAc,gBAAgB,MAAM;AAAA,EAC3C;AAAA,EAEO,WAAW;AAChB,QAAI,WAAW;AACb,wBAAkB;AAAA,QAChB,MAAM,KAAK;AAAA,QACX,MAAM,KAAK;AAAA,QACX,MAAM,KAAK;AAAA,MACb,CAAC;AAAA,IACH;AAAA,EACF;AAEJ;",
|
|
6
6
|
"names": ["firstPopulated"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
5
10
|
var __copyProps = (to, from, except, desc) => {
|
|
6
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -12,4 +17,36 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
12
17
|
};
|
|
13
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
19
|
var Presence_exports = {};
|
|
20
|
+
__export(Presence_exports, {
|
|
21
|
+
createScopedPresence: () => createScopedPresence
|
|
22
|
+
});
|
|
15
23
|
module.exports = __toCommonJS(Presence_exports);
|
|
24
|
+
function createScopedPresence(room, presence) {
|
|
25
|
+
const subscriptions = [];
|
|
26
|
+
const scopedPresence = Object.create(presence);
|
|
27
|
+
scopedPresence.subscribe = async function(topic, callback) {
|
|
28
|
+
subscriptions.push({ topic, callback });
|
|
29
|
+
await presence.subscribe(topic, callback);
|
|
30
|
+
return scopedPresence;
|
|
31
|
+
};
|
|
32
|
+
scopedPresence.unsubscribe = function(topic, callback) {
|
|
33
|
+
const index = subscriptions.findIndex(
|
|
34
|
+
(sub) => sub.topic === topic && (!callback || sub.callback === callback)
|
|
35
|
+
);
|
|
36
|
+
if (index !== -1) {
|
|
37
|
+
subscriptions.splice(index, 1);
|
|
38
|
+
}
|
|
39
|
+
presence.unsubscribe(topic, callback);
|
|
40
|
+
};
|
|
41
|
+
room["_events"].on("dispose", () => {
|
|
42
|
+
for (const { topic, callback } of subscriptions) {
|
|
43
|
+
presence.unsubscribe(topic, callback);
|
|
44
|
+
}
|
|
45
|
+
subscriptions.length = 0;
|
|
46
|
+
});
|
|
47
|
+
return scopedPresence;
|
|
48
|
+
}
|
|
49
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
50
|
+
0 && (module.exports = {
|
|
51
|
+
createScopedPresence
|
|
52
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/presence/Presence.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * When you need to scale your server on multiple processes and/or machines, you'd need to provide\n * the Presence option to the Server. The purpose of Presence is to allow communicating and\n * sharing data between different processes, specially during match-making.\n *\n * - Local presence - This is the default option. It's meant to be used when you're running Colyseus in\n * a single process.\n * - Redis presence - Use this option when you're running Colyseus on multiple processes and/or machines.\n *\n * @default Local presence\n */\nexport interface Presence {\n /**\n * Subscribes to the given topic. The callback will be triggered whenever a message is published on topic.\n *\n * @param topic - Topic name.\n * @param callback - Callback to trigger on subscribing.\n */\n subscribe(topic: string, callback: Function): Promise<this>;\n\n /**\n * Unsubscribe from given topic.\n *\n * @param topic - Topic name.\n * @param callback - Callback to trigger on topic unsubscribing.\n */\n unsubscribe(topic: string, callback?: Function);\n\n /**\n * Lists the currently active channels / subscriptions\n * @param pattern\n */\n channels(pattern?: string): Promise<string[]>;\n\n /**\n * Posts a message to given topic.\n *\n * @param topic - Topic name.\n * @param data - Message body/object.\n */\n publish(topic: string, data: any);\n\n /**\n * Returns if key exists.\n *\n * @param key\n */\n exists(key: string): Promise<boolean>;\n\n /**\n * Set key to hold the string value.\n *\n * @param key - Identifier.\n * @param value - Message body/object.\n */\n set(key: string, value: string);\n\n /**\n * Set key to hold the string value and set key to timeout after a given number of seconds.\n *\n * @param key - Identifier.\n * @param value - Message body/object.\n * @param seconds - Timeout value.\n */\n setex(key: string, value: string, seconds: number);\n\n /**\n * Expire the key in seconds.\n *\n * @param key - Identifier.\n * @param seconds - Seconds to expire the key.\n */\n expire(key: string, seconds: number);\n\n /**\n * Get the value of key.\n *\n * @param key - Identifier.\n */\n get(key: string);\n\n /**\n * Removes the specified key.\n *\n * @param key - Identifier of the object to removed.\n */\n del(key: string): void;\n\n /**\n * Add the specified members to the set stored at key. Specified members that are already\n * a member of this set are ignored. If key does not exist, a new set is created before\n * adding the specified members.\n *\n * @param key - Name/Identifier of the set.\n * @param value - Message body/object.\n */\n sadd(key: string, value: any);\n\n /**\n * Returns all the members of the set value stored at key.\n *\n * @param key - Name/Identifier of the set.\n */\n smembers(key: string): Promise<string[]>;\n\n /**\n * Returns if member is a member of the set stored at key.\n *\n * @param key - Name/Identifier of the set.\n * @param field - Key value within the set.\n * @returns `1` if the element is a member of the set else `0`.\n */\n sismember(key: string, field: string);\n\n /**\n * Remove the specified members from the set stored at key. Specified members that are not a\n * member of this set are ignored. If key does not exist, it is treated as an empty set\n * and this command returns 0.\n *\n * @param key - Name/Identifier of the set.\n * @param value - Key value within the set.\n */\n srem(key: string, value: any);\n\n /**\n * Returns the set cardinality (number of elements) of the set stored at key.\n *\n * @param key - Name/Identifier of the set.\n */\n scard(key: string);\n\n /**\n * Returns the members of the set resulting from the intersection of all the given sets.\n *\n * @param keys - Key values within the set.\n */\n sinter(...keys: string[]): Promise<string[]>;\n\n /**\n * Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created.\n * If field already exists in the hash, it is overwritten.\n */\n hset(key: string, field: string, value: string): Promise<boolean>;\n\n /**\n * Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key\n * holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.\n */\n hincrby(key: string, field: string, value: number): Promise<number>;\n\n /**\n * WARNING: DO NOT USE THIS METHOD. It is meant for internal use only.\n * @private\n */\n hincrbyex(key: string, field: string, value: number, expireInSeconds: number): Promise<number>;\n\n /**\n * Returns the value associated with field in the hash stored at key.\n */\n hget(key: string, field: string): Promise<string | null>;\n\n /**\n * Returns all fields and values of the hash stored at key.\n */\n hgetall(key: string): Promise<{ [key: string]: string }>;\n\n /**\n * Removes the specified fields from the hash stored at key. Specified fields that do not exist within\n * this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.\n */\n hdel(key: string, field: string): Promise<boolean>;\n\n /**\n * Returns the number of fields contained in the hash stored at key\n */\n hlen(key: string): Promise<number>;\n\n /**\n * Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing\n * the operation. An error is returned if the key contains a value of the wrong type or\n * contains a string that can not be represented as integer. This operation is limited to 64-bit signed integers.\n */\n incr(key: string): Promise<number>;\n\n /**\n * Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing\n * the operation. An error is returned if the key contains a value of the wrong type or contains a string\n * that can not be represented as integer. This operation is limited to 64-bit signed integers.\n */\n decr(key: string): Promise<number>;\n\n /**\n * Returns the length of the list stored at key.\n */\n llen(key: string): Promise<number>;\n\n /**\n * Adds the string value to the end of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.\n */\n rpush(key: string, ...values: string[]): Promise<number>;\n\n /**\n * Adds the string value to the begginning of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.\n */\n lpush(key: string, ...values: string[]): Promise<number>;\n\n /**\n * Removes and returns the last element of the list stored at key.\n */\n rpop(key: string): Promise<string | null>;\n\n /**\n * Removes and returns the first element of the list stored at key.\n */\n lpop(key: string): Promise<string | null>;\n\n /**\n * Removes and returns the last element of the list stored at key. If the list is empty, the execution is halted until an element is available or the timeout is reached.\n */\n brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null>;\n\n setMaxListeners(number: number): void;\n\n shutdown(): void;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { Room } from \"../Room.ts\";\n\n/**\n * When you need to scale your server on multiple processes and/or machines, you'd need to provide\n * the Presence option to the Server. The purpose of Presence is to allow communicating and\n * sharing data between different processes, specially during match-making.\n *\n * - Local presence - This is the default option. It's meant to be used when you're running Colyseus in\n * a single process.\n * - Redis presence - Use this option when you're running Colyseus on multiple processes and/or machines.\n *\n * @default Local presence\n */\nexport interface Presence {\n /**\n * Subscribes to the given topic. The callback will be triggered whenever a message is published on topic.\n *\n * @param topic - Topic name.\n * @param callback - Callback to trigger on subscribing.\n */\n subscribe(topic: string, callback: Function): Promise<this>;\n\n /**\n * Unsubscribe from given topic.\n *\n * @param topic - Topic name.\n * @param callback - Callback to trigger on topic unsubscribing.\n */\n unsubscribe(topic: string, callback?: Function);\n\n /**\n * Lists the currently active channels / subscriptions\n * @param pattern\n */\n channels(pattern?: string): Promise<string[]>;\n\n /**\n * Posts a message to given topic.\n *\n * @param topic - Topic name.\n * @param data - Message body/object.\n */\n publish(topic: string, data: any);\n\n /**\n * Returns if key exists.\n *\n * @param key\n */\n exists(key: string): Promise<boolean>;\n\n /**\n * Set key to hold the string value.\n *\n * @param key - Identifier.\n * @param value - Message body/object.\n */\n set(key: string, value: string);\n\n /**\n * Set key to hold the string value and set key to timeout after a given number of seconds.\n *\n * @param key - Identifier.\n * @param value - Message body/object.\n * @param seconds - Timeout value.\n */\n setex(key: string, value: string, seconds: number);\n\n /**\n * Expire the key in seconds.\n *\n * @param key - Identifier.\n * @param seconds - Seconds to expire the key.\n */\n expire(key: string, seconds: number);\n\n /**\n * Get the value of key.\n *\n * @param key - Identifier.\n */\n get(key: string);\n\n /**\n * Removes the specified key.\n *\n * @param key - Identifier of the object to removed.\n */\n del(key: string): void;\n\n /**\n * Add the specified members to the set stored at key. Specified members that are already\n * a member of this set are ignored. If key does not exist, a new set is created before\n * adding the specified members.\n *\n * @param key - Name/Identifier of the set.\n * @param value - Message body/object.\n */\n sadd(key: string, value: any);\n\n /**\n * Returns all the members of the set value stored at key.\n *\n * @param key - Name/Identifier of the set.\n */\n smembers(key: string): Promise<string[]>;\n\n /**\n * Returns if member is a member of the set stored at key.\n *\n * @param key - Name/Identifier of the set.\n * @param field - Key value within the set.\n * @returns `1` if the element is a member of the set else `0`.\n */\n sismember(key: string, field: string);\n\n /**\n * Remove the specified members from the set stored at key. Specified members that are not a\n * member of this set are ignored. If key does not exist, it is treated as an empty set\n * and this command returns 0.\n *\n * @param key - Name/Identifier of the set.\n * @param value - Key value within the set.\n */\n srem(key: string, value: any);\n\n /**\n * Returns the set cardinality (number of elements) of the set stored at key.\n *\n * @param key - Name/Identifier of the set.\n */\n scard(key: string);\n\n /**\n * Returns the members of the set resulting from the intersection of all the given sets.\n *\n * @param keys - Key values within the set.\n */\n sinter(...keys: string[]): Promise<string[]>;\n\n /**\n * Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created.\n * If field already exists in the hash, it is overwritten.\n */\n hset(key: string, field: string, value: string): Promise<boolean>;\n\n /**\n * Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key\n * holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.\n */\n hincrby(key: string, field: string, value: number): Promise<number>;\n\n /**\n * WARNING: DO NOT USE THIS METHOD. It is meant for internal use only.\n * @private\n */\n hincrbyex(key: string, field: string, value: number, expireInSeconds: number): Promise<number>;\n\n /**\n * Returns the value associated with field in the hash stored at key.\n */\n hget(key: string, field: string): Promise<string | null>;\n\n /**\n * Returns all fields and values of the hash stored at key.\n */\n hgetall(key: string): Promise<{ [key: string]: string }>;\n\n /**\n * Removes the specified fields from the hash stored at key. Specified fields that do not exist within\n * this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.\n */\n hdel(key: string, field: string): Promise<boolean>;\n\n /**\n * Returns the number of fields contained in the hash stored at key\n */\n hlen(key: string): Promise<number>;\n\n /**\n * Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing\n * the operation. An error is returned if the key contains a value of the wrong type or\n * contains a string that can not be represented as integer. This operation is limited to 64-bit signed integers.\n */\n incr(key: string): Promise<number>;\n\n /**\n * Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing\n * the operation. An error is returned if the key contains a value of the wrong type or contains a string\n * that can not be represented as integer. This operation is limited to 64-bit signed integers.\n */\n decr(key: string): Promise<number>;\n\n /**\n * Returns the length of the list stored at key.\n */\n llen(key: string): Promise<number>;\n\n /**\n * Adds the string value to the end of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.\n */\n rpush(key: string, ...values: string[]): Promise<number>;\n\n /**\n * Adds the string value to the begginning of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.\n */\n lpush(key: string, ...values: string[]): Promise<number>;\n\n /**\n * Removes and returns the last element of the list stored at key.\n */\n rpop(key: string): Promise<string | null>;\n\n /**\n * Removes and returns the first element of the list stored at key.\n */\n lpop(key: string): Promise<string | null>;\n\n /**\n * Removes and returns the last element of the list stored at key. If the list is empty, the execution is halted until an element is available or the timeout is reached.\n */\n brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null>;\n\n setMaxListeners(number: number): void;\n\n shutdown(): void;\n}\n\nexport function createScopedPresence(room: Room, presence: Presence): Presence {\n // Keep a local copy of all subscriptions made through this scoped presence\n const subscriptions: Array<{ topic: string; callback: Function }> = [];\n\n // Create a copy of the presence object\n const scopedPresence = Object.create(presence) as Presence;\n\n // Override subscribe method to track subscriptions\n scopedPresence.subscribe = async function (topic: string, callback: Function): Promise<Presence> {\n subscriptions.push({ topic, callback });\n await presence.subscribe(topic, callback);\n return scopedPresence;\n };\n\n // Override unsubscribe method to remove from tracking\n scopedPresence.unsubscribe = function (topic: string, callback?: Function) {\n const index = subscriptions.findIndex(\n (sub) => sub.topic === topic && (!callback || sub.callback === callback)\n );\n if (index !== -1) {\n subscriptions.splice(index, 1);\n }\n presence.unsubscribe(topic, callback);\n };\n\n // Clean up all subscriptions when the room is disposed\n room['_events'].on('dispose', () => {\n for (const { topic, callback } of subscriptions) {\n presence.unsubscribe(topic, callback);\n }\n subscriptions.length = 0;\n });\n\n return scopedPresence;\n}"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAoOO,SAAS,qBAAqB,MAAY,UAA8B;AAE7E,QAAM,gBAA8D,CAAC;AAGrE,QAAM,iBAAiB,OAAO,OAAO,QAAQ;AAG7C,iBAAe,YAAY,eAAgB,OAAe,UAAuC;AAC/F,kBAAc,KAAK,EAAE,OAAO,SAAS,CAAC;AACtC,UAAM,SAAS,UAAU,OAAO,QAAQ;AACxC,WAAO;AAAA,EACT;AAGA,iBAAe,cAAc,SAAU,OAAe,UAAqB;AACzE,UAAM,QAAQ,cAAc;AAAA,MAC1B,CAAC,QAAQ,IAAI,UAAU,UAAU,CAAC,YAAY,IAAI,aAAa;AAAA,IACjE;AACA,QAAI,UAAU,IAAI;AAChB,oBAAc,OAAO,OAAO,CAAC;AAAA,IAC/B;AACA,aAAS,YAAY,OAAO,QAAQ;AAAA,EACtC;AAGA,OAAK,SAAS,EAAE,GAAG,WAAW,MAAM;AAClC,eAAW,EAAE,OAAO,SAAS,KAAK,eAAe;AAC/C,eAAS,YAAY,OAAO,QAAQ;AAAA,IACtC;AACA,kBAAc,SAAS;AAAA,EACzB,CAAC;AAED,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// packages/core/src/presence/Presence.ts
|
|
2
|
+
function createScopedPresence(room, presence) {
|
|
3
|
+
const subscriptions = [];
|
|
4
|
+
const scopedPresence = Object.create(presence);
|
|
5
|
+
scopedPresence.subscribe = async function(topic, callback) {
|
|
6
|
+
subscriptions.push({ topic, callback });
|
|
7
|
+
await presence.subscribe(topic, callback);
|
|
8
|
+
return scopedPresence;
|
|
9
|
+
};
|
|
10
|
+
scopedPresence.unsubscribe = function(topic, callback) {
|
|
11
|
+
const index = subscriptions.findIndex(
|
|
12
|
+
(sub) => sub.topic === topic && (!callback || sub.callback === callback)
|
|
13
|
+
);
|
|
14
|
+
if (index !== -1) {
|
|
15
|
+
subscriptions.splice(index, 1);
|
|
16
|
+
}
|
|
17
|
+
presence.unsubscribe(topic, callback);
|
|
18
|
+
};
|
|
19
|
+
room["_events"].on("dispose", () => {
|
|
20
|
+
for (const { topic, callback } of subscriptions) {
|
|
21
|
+
presence.unsubscribe(topic, callback);
|
|
22
|
+
}
|
|
23
|
+
subscriptions.length = 0;
|
|
24
|
+
});
|
|
25
|
+
return scopedPresence;
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
createScopedPresence
|
|
29
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": [],
|
|
4
|
-
"sourcesContent": [],
|
|
5
|
-
"mappings": "",
|
|
3
|
+
"sources": ["../../src/presence/Presence.ts"],
|
|
4
|
+
"sourcesContent": ["import type { Room } from \"../Room.ts\";\n\n/**\n * When you need to scale your server on multiple processes and/or machines, you'd need to provide\n * the Presence option to the Server. The purpose of Presence is to allow communicating and\n * sharing data between different processes, specially during match-making.\n *\n * - Local presence - This is the default option. It's meant to be used when you're running Colyseus in\n * a single process.\n * - Redis presence - Use this option when you're running Colyseus on multiple processes and/or machines.\n *\n * @default Local presence\n */\nexport interface Presence {\n /**\n * Subscribes to the given topic. The callback will be triggered whenever a message is published on topic.\n *\n * @param topic - Topic name.\n * @param callback - Callback to trigger on subscribing.\n */\n subscribe(topic: string, callback: Function): Promise<this>;\n\n /**\n * Unsubscribe from given topic.\n *\n * @param topic - Topic name.\n * @param callback - Callback to trigger on topic unsubscribing.\n */\n unsubscribe(topic: string, callback?: Function);\n\n /**\n * Lists the currently active channels / subscriptions\n * @param pattern\n */\n channels(pattern?: string): Promise<string[]>;\n\n /**\n * Posts a message to given topic.\n *\n * @param topic - Topic name.\n * @param data - Message body/object.\n */\n publish(topic: string, data: any);\n\n /**\n * Returns if key exists.\n *\n * @param key\n */\n exists(key: string): Promise<boolean>;\n\n /**\n * Set key to hold the string value.\n *\n * @param key - Identifier.\n * @param value - Message body/object.\n */\n set(key: string, value: string);\n\n /**\n * Set key to hold the string value and set key to timeout after a given number of seconds.\n *\n * @param key - Identifier.\n * @param value - Message body/object.\n * @param seconds - Timeout value.\n */\n setex(key: string, value: string, seconds: number);\n\n /**\n * Expire the key in seconds.\n *\n * @param key - Identifier.\n * @param seconds - Seconds to expire the key.\n */\n expire(key: string, seconds: number);\n\n /**\n * Get the value of key.\n *\n * @param key - Identifier.\n */\n get(key: string);\n\n /**\n * Removes the specified key.\n *\n * @param key - Identifier of the object to removed.\n */\n del(key: string): void;\n\n /**\n * Add the specified members to the set stored at key. Specified members that are already\n * a member of this set are ignored. If key does not exist, a new set is created before\n * adding the specified members.\n *\n * @param key - Name/Identifier of the set.\n * @param value - Message body/object.\n */\n sadd(key: string, value: any);\n\n /**\n * Returns all the members of the set value stored at key.\n *\n * @param key - Name/Identifier of the set.\n */\n smembers(key: string): Promise<string[]>;\n\n /**\n * Returns if member is a member of the set stored at key.\n *\n * @param key - Name/Identifier of the set.\n * @param field - Key value within the set.\n * @returns `1` if the element is a member of the set else `0`.\n */\n sismember(key: string, field: string);\n\n /**\n * Remove the specified members from the set stored at key. Specified members that are not a\n * member of this set are ignored. If key does not exist, it is treated as an empty set\n * and this command returns 0.\n *\n * @param key - Name/Identifier of the set.\n * @param value - Key value within the set.\n */\n srem(key: string, value: any);\n\n /**\n * Returns the set cardinality (number of elements) of the set stored at key.\n *\n * @param key - Name/Identifier of the set.\n */\n scard(key: string);\n\n /**\n * Returns the members of the set resulting from the intersection of all the given sets.\n *\n * @param keys - Key values within the set.\n */\n sinter(...keys: string[]): Promise<string[]>;\n\n /**\n * Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created.\n * If field already exists in the hash, it is overwritten.\n */\n hset(key: string, field: string, value: string): Promise<boolean>;\n\n /**\n * Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key\n * holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.\n */\n hincrby(key: string, field: string, value: number): Promise<number>;\n\n /**\n * WARNING: DO NOT USE THIS METHOD. It is meant for internal use only.\n * @private\n */\n hincrbyex(key: string, field: string, value: number, expireInSeconds: number): Promise<number>;\n\n /**\n * Returns the value associated with field in the hash stored at key.\n */\n hget(key: string, field: string): Promise<string | null>;\n\n /**\n * Returns all fields and values of the hash stored at key.\n */\n hgetall(key: string): Promise<{ [key: string]: string }>;\n\n /**\n * Removes the specified fields from the hash stored at key. Specified fields that do not exist within\n * this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.\n */\n hdel(key: string, field: string): Promise<boolean>;\n\n /**\n * Returns the number of fields contained in the hash stored at key\n */\n hlen(key: string): Promise<number>;\n\n /**\n * Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing\n * the operation. An error is returned if the key contains a value of the wrong type or\n * contains a string that can not be represented as integer. This operation is limited to 64-bit signed integers.\n */\n incr(key: string): Promise<number>;\n\n /**\n * Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing\n * the operation. An error is returned if the key contains a value of the wrong type or contains a string\n * that can not be represented as integer. This operation is limited to 64-bit signed integers.\n */\n decr(key: string): Promise<number>;\n\n /**\n * Returns the length of the list stored at key.\n */\n llen(key: string): Promise<number>;\n\n /**\n * Adds the string value to the end of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.\n */\n rpush(key: string, ...values: string[]): Promise<number>;\n\n /**\n * Adds the string value to the begginning of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.\n */\n lpush(key: string, ...values: string[]): Promise<number>;\n\n /**\n * Removes and returns the last element of the list stored at key.\n */\n rpop(key: string): Promise<string | null>;\n\n /**\n * Removes and returns the first element of the list stored at key.\n */\n lpop(key: string): Promise<string | null>;\n\n /**\n * Removes and returns the last element of the list stored at key. If the list is empty, the execution is halted until an element is available or the timeout is reached.\n */\n brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null>;\n\n setMaxListeners(number: number): void;\n\n shutdown(): void;\n}\n\nexport function createScopedPresence(room: Room, presence: Presence): Presence {\n // Keep a local copy of all subscriptions made through this scoped presence\n const subscriptions: Array<{ topic: string; callback: Function }> = [];\n\n // Create a copy of the presence object\n const scopedPresence = Object.create(presence) as Presence;\n\n // Override subscribe method to track subscriptions\n scopedPresence.subscribe = async function (topic: string, callback: Function): Promise<Presence> {\n subscriptions.push({ topic, callback });\n await presence.subscribe(topic, callback);\n return scopedPresence;\n };\n\n // Override unsubscribe method to remove from tracking\n scopedPresence.unsubscribe = function (topic: string, callback?: Function) {\n const index = subscriptions.findIndex(\n (sub) => sub.topic === topic && (!callback || sub.callback === callback)\n );\n if (index !== -1) {\n subscriptions.splice(index, 1);\n }\n presence.unsubscribe(topic, callback);\n };\n\n // Clean up all subscriptions when the room is disposed\n room['_events'].on('dispose', () => {\n for (const { topic, callback } of subscriptions) {\n presence.unsubscribe(topic, callback);\n }\n subscriptions.length = 0;\n });\n\n return scopedPresence;\n}"],
|
|
5
|
+
"mappings": ";AAoOO,SAAS,qBAAqB,MAAY,UAA8B;AAE7E,QAAM,gBAA8D,CAAC;AAGrE,QAAM,iBAAiB,OAAO,OAAO,QAAQ;AAG7C,iBAAe,YAAY,eAAgB,OAAe,UAAuC;AAC/F,kBAAc,KAAK,EAAE,OAAO,SAAS,CAAC;AACtC,UAAM,SAAS,UAAU,OAAO,QAAQ;AACxC,WAAO;AAAA,EACT;AAGA,iBAAe,cAAc,SAAU,OAAe,UAAqB;AACzE,UAAM,QAAQ,cAAc;AAAA,MAC1B,CAAC,QAAQ,IAAI,UAAU,UAAU,CAAC,YAAY,IAAI,aAAa;AAAA,IACjE;AACA,QAAI,UAAU,IAAI;AAChB,oBAAc,OAAO,OAAO,CAAC;AAAA,IAC/B;AACA,aAAS,YAAY,OAAO,QAAQ;AAAA,EACtC;AAGA,OAAK,SAAS,EAAE,GAAG,WAAW,MAAM;AAClC,eAAW,EAAE,OAAO,SAAS,KAAK,eAAe;AAC/C,eAAS,YAAY,OAAO,QAAQ;AAAA,IACtC;AACA,kBAAc,SAAS;AAAA,EACzB,CAAC;AAED,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build/rooms/LobbyRoom.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -30,18 +31,17 @@ __export(LobbyRoom_exports, {
|
|
|
30
31
|
LobbyRoom: () => LobbyRoom
|
|
31
32
|
});
|
|
32
33
|
module.exports = __toCommonJS(LobbyRoom_exports);
|
|
33
|
-
var matchMaker = __toESM(require("../MatchMaker.
|
|
34
|
-
var import_Lobby = require("../matchmaker/Lobby.
|
|
35
|
-
var import_Room = require("../Room.
|
|
34
|
+
var matchMaker = __toESM(require("../MatchMaker.ts"), 1);
|
|
35
|
+
var import_Lobby = require("../matchmaker/Lobby.ts");
|
|
36
|
+
var import_Room = require("../Room.ts");
|
|
36
37
|
class LobbyRoom extends import_Room.Room {
|
|
37
38
|
constructor() {
|
|
38
39
|
super(...arguments);
|
|
39
|
-
// tslint:disable-line
|
|
40
40
|
this.rooms = [];
|
|
41
41
|
this.clientOptions = {};
|
|
42
42
|
}
|
|
43
43
|
async onCreate(options) {
|
|
44
|
-
this.
|
|
44
|
+
this["_listing"].unlisted = true;
|
|
45
45
|
this.unsubscribeLobby = await (0, import_Lobby.subscribeLobby)((roomId, data) => {
|
|
46
46
|
const roomIndex = this.rooms.findIndex((room) => room.roomId === roomId);
|
|
47
47
|
const clients = this.clients.filter((client) => this.clientOptions[client.sessionId]);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/rooms/LobbyRoom.ts"],
|
|
4
|
-
"sourcesContent": ["\nimport * as matchMaker from '../MatchMaker.
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["\nimport * as matchMaker from '../MatchMaker.ts';\nimport type { IRoomCache } from '../matchmaker/LocalDriver/LocalDriver.ts';\nimport type { Client } from '../Transport.ts';\nimport { subscribeLobby } from '../matchmaker/Lobby.ts';\nimport { Room } from '../Room.ts';\n\n// TODO: use Schema state & filters on version 1.0.0\n\n// class DummyLobbyState extends Schema { // tslint:disable-line\n// @type(\"number\") public _: number;\n// }\n\nexport interface FilterInput {\n name?: string;\n metadata?: any;\n}\n\nexport interface LobbyOptions {\n filter?: FilterInput;\n}\n\nexport class LobbyRoom extends Room {\n public rooms: IRoomCache[] = [];\n public unsubscribeLobby: () => void;\n\n public clientOptions: { [sessionId: string]: LobbyOptions } = {};\n\n public async onCreate(options: any) {\n // prevent LobbyRoom to notify itself\n this['_listing'].unlisted = true;\n\n this.unsubscribeLobby = await subscribeLobby((roomId, data) => {\n const roomIndex = this.rooms.findIndex((room) => room.roomId === roomId);\n const clients = this.clients.filter((client) => this.clientOptions[client.sessionId]);\n\n if (!data) {\n // remove room listing data\n if (roomIndex !== -1) {\n const previousData = this.rooms[roomIndex];\n\n this.rooms.splice(roomIndex, 1);\n\n clients.forEach((client) => {\n if (this.filterItemForClient(previousData, this.clientOptions[client.sessionId].filter)) {\n client.send('-', roomId);\n }\n });\n }\n\n } else if (roomIndex === -1) {\n // append room listing data\n this.rooms.push(data);\n\n clients.forEach((client) => {\n if (this.filterItemForClient(data, this.clientOptions[client.sessionId].filter)) {\n client.send('+', [roomId, data]);\n }\n });\n\n } else {\n const previousData = this.rooms[roomIndex];\n\n // replace room listing data\n this.rooms[roomIndex] = data;\n\n clients.forEach((client) => {\n const hadData = this.filterItemForClient(previousData, this.clientOptions[client.sessionId].filter);\n const hasData = this.filterItemForClient(data, this.clientOptions[client.sessionId].filter);\n\n if (hadData && !hasData) {\n client.send('-', roomId);\n\n } else if (hasData) {\n client.send('+', [roomId, data]);\n }\n });\n }\n });\n\n this.rooms = await matchMaker.query({ private: false, unlisted: false });\n\n this.onMessage('filter', (client: Client, filter: FilterInput) => {\n this.clientOptions[client.sessionId].filter = filter;\n client.send('rooms', this.filterItemsForClient(this.clientOptions[client.sessionId]));\n });\n }\n\n public onJoin(client: Client, options: LobbyOptions) {\n this.clientOptions[client.sessionId] = options || {};\n client.send('rooms', this.filterItemsForClient(this.clientOptions[client.sessionId]));\n }\n\n public onLeave(client: Client) {\n delete this.clientOptions[client.sessionId];\n }\n\n public onDispose() {\n if (this.unsubscribeLobby) {\n this.unsubscribeLobby();\n }\n }\n\n protected filterItemsForClient(options: LobbyOptions) {\n const filter = options.filter;\n\n return (filter)\n ? this.rooms.filter((room) => this.filterItemForClient(room, filter))\n : this.rooms;\n }\n\n protected filterItemForClient(room: IRoomCache, filter?: LobbyOptions['filter']) {\n if (!filter) {\n return true;\n }\n\n let isAllowed = true;\n\n if (filter.name !== room.name) {\n isAllowed = false;\n }\n\n if (filter.metadata) {\n for (const field in filter.metadata) {\n if (room.metadata[field] !== filter.metadata[field]) {\n isAllowed = false;\n break;\n }\n }\n }\n\n return isAllowed;\n }\n\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;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AI,KAAK,SAAS,KAAK,MAAM,OAAO,SAAS,KAAK,GAAG;AACnD,sBAAY;AACZ;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,12 +5,11 @@ import { Room } from "../Room.mjs";
|
|
|
5
5
|
var LobbyRoom = class extends Room {
|
|
6
6
|
constructor() {
|
|
7
7
|
super(...arguments);
|
|
8
|
-
// tslint:disable-line
|
|
9
8
|
this.rooms = [];
|
|
10
9
|
this.clientOptions = {};
|
|
11
10
|
}
|
|
12
11
|
async onCreate(options) {
|
|
13
|
-
this.
|
|
12
|
+
this["_listing"].unlisted = true;
|
|
14
13
|
this.unsubscribeLobby = await subscribeLobby((roomId, data) => {
|
|
15
14
|
const roomIndex = this.rooms.findIndex((room) => room.roomId === roomId);
|
|
16
15
|
const clients = this.clients.filter((client) => this.clientOptions[client.sessionId]);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/rooms/LobbyRoom.ts"],
|
|
4
|
-
"sourcesContent": ["\nimport * as matchMaker from '../MatchMaker.
|
|
5
|
-
"mappings": ";AACA,YAAY,gBAAgB;
|
|
4
|
+
"sourcesContent": ["\nimport * as matchMaker from '../MatchMaker.ts';\nimport type { IRoomCache } from '../matchmaker/LocalDriver/LocalDriver.ts';\nimport type { Client } from '../Transport.ts';\nimport { subscribeLobby } from '../matchmaker/Lobby.ts';\nimport { Room } from '../Room.ts';\n\n// TODO: use Schema state & filters on version 1.0.0\n\n// class DummyLobbyState extends Schema { // tslint:disable-line\n// @type(\"number\") public _: number;\n// }\n\nexport interface FilterInput {\n name?: string;\n metadata?: any;\n}\n\nexport interface LobbyOptions {\n filter?: FilterInput;\n}\n\nexport class LobbyRoom extends Room {\n public rooms: IRoomCache[] = [];\n public unsubscribeLobby: () => void;\n\n public clientOptions: { [sessionId: string]: LobbyOptions } = {};\n\n public async onCreate(options: any) {\n // prevent LobbyRoom to notify itself\n this['_listing'].unlisted = true;\n\n this.unsubscribeLobby = await subscribeLobby((roomId, data) => {\n const roomIndex = this.rooms.findIndex((room) => room.roomId === roomId);\n const clients = this.clients.filter((client) => this.clientOptions[client.sessionId]);\n\n if (!data) {\n // remove room listing data\n if (roomIndex !== -1) {\n const previousData = this.rooms[roomIndex];\n\n this.rooms.splice(roomIndex, 1);\n\n clients.forEach((client) => {\n if (this.filterItemForClient(previousData, this.clientOptions[client.sessionId].filter)) {\n client.send('-', roomId);\n }\n });\n }\n\n } else if (roomIndex === -1) {\n // append room listing data\n this.rooms.push(data);\n\n clients.forEach((client) => {\n if (this.filterItemForClient(data, this.clientOptions[client.sessionId].filter)) {\n client.send('+', [roomId, data]);\n }\n });\n\n } else {\n const previousData = this.rooms[roomIndex];\n\n // replace room listing data\n this.rooms[roomIndex] = data;\n\n clients.forEach((client) => {\n const hadData = this.filterItemForClient(previousData, this.clientOptions[client.sessionId].filter);\n const hasData = this.filterItemForClient(data, this.clientOptions[client.sessionId].filter);\n\n if (hadData && !hasData) {\n client.send('-', roomId);\n\n } else if (hasData) {\n client.send('+', [roomId, data]);\n }\n });\n }\n });\n\n this.rooms = await matchMaker.query({ private: false, unlisted: false });\n\n this.onMessage('filter', (client: Client, filter: FilterInput) => {\n this.clientOptions[client.sessionId].filter = filter;\n client.send('rooms', this.filterItemsForClient(this.clientOptions[client.sessionId]));\n });\n }\n\n public onJoin(client: Client, options: LobbyOptions) {\n this.clientOptions[client.sessionId] = options || {};\n client.send('rooms', this.filterItemsForClient(this.clientOptions[client.sessionId]));\n }\n\n public onLeave(client: Client) {\n delete this.clientOptions[client.sessionId];\n }\n\n public onDispose() {\n if (this.unsubscribeLobby) {\n this.unsubscribeLobby();\n }\n }\n\n protected filterItemsForClient(options: LobbyOptions) {\n const filter = options.filter;\n\n return (filter)\n ? this.rooms.filter((room) => this.filterItemForClient(room, filter))\n : this.rooms;\n }\n\n protected filterItemForClient(room: IRoomCache, filter?: LobbyOptions['filter']) {\n if (!filter) {\n return true;\n }\n\n let isAllowed = true;\n\n if (filter.name !== room.name) {\n isAllowed = false;\n }\n\n if (filter.metadata) {\n for (const field in filter.metadata) {\n if (room.metadata[field] !== filter.metadata[field]) {\n isAllowed = false;\n break;\n }\n }\n }\n\n return isAllowed;\n }\n\n}\n"],
|
|
5
|
+
"mappings": ";AACA,YAAY,gBAAgB;AAG5B,SAAS,sBAAsB;AAC/B,SAAS,YAAY;AAiBd,IAAM,YAAN,cAAwB,KAAK;AAAA,EAA7B;AAAA;AACL,SAAO,QAAsB,CAAC;AAG9B,SAAO,gBAAuD,CAAC;AAAA;AAAA,EAE/D,MAAa,SAAS,SAAc;AAElC,SAAK,UAAU,EAAE,WAAW;AAE5B,SAAK,mBAAmB,MAAM,eAAe,CAAC,QAAQ,SAAS;AAC7D,YAAM,YAAY,KAAK,MAAM,UAAU,CAAC,SAAS,KAAK,WAAW,MAAM;AACvE,YAAM,UAAU,KAAK,QAAQ,OAAO,CAAC,WAAW,KAAK,cAAc,OAAO,SAAS,CAAC;AAEpF,UAAI,CAAC,MAAM;AAET,YAAI,cAAc,IAAI;AACpB,gBAAM,eAAe,KAAK,MAAM,SAAS;AAEzC,eAAK,MAAM,OAAO,WAAW,CAAC;AAE9B,kBAAQ,QAAQ,CAAC,WAAW;AAC1B,gBAAI,KAAK,oBAAoB,cAAc,KAAK,cAAc,OAAO,SAAS,EAAE,MAAM,GAAG;AACvF,qBAAO,KAAK,KAAK,MAAM;AAAA,YACzB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MAEF,WAAW,cAAc,IAAI;AAE3B,aAAK,MAAM,KAAK,IAAI;AAEpB,gBAAQ,QAAQ,CAAC,WAAW;AAC1B,cAAI,KAAK,oBAAoB,MAAM,KAAK,cAAc,OAAO,SAAS,EAAE,MAAM,GAAG;AAC/E,mBAAO,KAAK,KAAK,CAAC,QAAQ,IAAI,CAAC;AAAA,UACjC;AAAA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