UNPKG

@colyseus/redis-driver

Version:

<div align="center"> <a href="https://github.com/colyseus/colyseus"> <img src="media/logo.svg?raw=true" width="60%" height="300" /> </a> <br> <br> <a href="https://npmjs.com/package/colyseus"> <img src="https://img.shields.io/npm/dm/coly

126 lines (125 loc) 4.93 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var RedisDriver_exports = {}; __export(RedisDriver_exports, { RedisDriver: () => RedisDriver }); module.exports = __toCommonJS(RedisDriver_exports); var import_ioredis = __toESM(require("ioredis")); var import_core = require("@colyseus/core"); var import_Query = require("./Query.js"); var import_RoomData = require("./RoomData.js"); const ROOMCACHES_KEY = "roomcaches"; class RedisDriver { constructor(options, clusterOptions) { this._roomCacheRequestByName = {}; this._client = Array.isArray(options) ? new import_ioredis.Cluster(options, clusterOptions) : new import_ioredis.default(options); } createInstance(initialValues = {}) { return new import_RoomData.RoomData(initialValues, this._client); } async has(roomId) { return await this._client.hexists(ROOMCACHES_KEY, roomId) === 1; } async query(conditions, sortOptions) { const rooms = await this.getRooms(); return rooms.filter((room) => { if (!room.roomId) { return false; } for (const field in conditions) { if (conditions.hasOwnProperty(field) && room[field] !== conditions[field]) { return false; } } return true; }); } async cleanup(processId) { const cachedRooms = await this.query({ processId }); (0, import_core.debugMatchMaking)("removing stale rooms by processId %s (%s rooms found)", processId, cachedRooms.length); const itemsPerCommand = 500; for (let i = 0; i < cachedRooms.length; i += itemsPerCommand) { await this._client.hdel(ROOMCACHES_KEY, ...cachedRooms.slice(i, i + itemsPerCommand).map((room) => room.roomId)); } } findOne(conditions, sortOptions) { if (typeof conditions.roomId !== "undefined") { return new Promise((resolve, reject) => { this._client.hget(ROOMCACHES_KEY, conditions.roomId).then((roomcache) => { if (roomcache) { resolve(new import_RoomData.RoomData(JSON.parse(roomcache), this._client)); } else { resolve(void 0); } }).catch(reject); }); } else { const query = new import_Query.Query(this.getRooms(conditions["name"]), conditions); if (sortOptions) { query.sort(sortOptions); } return query; } } getRooms(roomName) { if (this._roomCacheRequestByName[roomName] !== void 0) { return this._roomCacheRequestByName[roomName]; } const roomCacheRequest = this._concurrentRoomCacheRequest || this._client.hgetall(ROOMCACHES_KEY); this._concurrentRoomCacheRequest = roomCacheRequest; this._roomCacheRequestByName[roomName] = roomCacheRequest.then((result) => { this._concurrentRoomCacheRequest = void 0; delete this._roomCacheRequestByName[roomName]; let roomcaches = Object.entries(result ?? []); if (roomName !== void 0) { const roomNameField = `"name":"${roomName}"`; roomcaches = roomcaches.filter(([, roomcache]) => roomcache.includes(roomNameField)); } return roomcaches.map( // TODO: probably no need to instantiate RoomData here. ([, roomcache]) => new import_RoomData.RoomData(JSON.parse(roomcache), this._client) ); }); return this._roomCacheRequestByName[roomName]; } async shutdown() { await this._client.quit(); } // // only relevant for the test-suite. // not used during runtime. // clear() { this._client.del(ROOMCACHES_KEY); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RedisDriver });