it('can be initialized', function () { const httpServer = {}; const parseLiveQueryServer = new ParseLiveQueryServer(httpServer); expect(parseLiveQueryServer.clientId).toBeUndefined(); expect(parseLiveQueryServer.clients.size).toBe(0); expect(parseLiveQueryServer.subscriptions.size).toBe(0); });
/** * @Lifecycle Activate * New one activated when old isnt being used. * * waitUntil(): activating ====> activated */ self.addEventListener('activate', (event) => { event.waitUntil(self.clients.claim()) })
// Helper functions to add mock client and subscription to a liveQueryServer function addMockClient(parseLiveQueryServer, clientId) { const Client = require('../lib/LiveQuery/Client').Client; const client = new Client(clientId, {}); parseLiveQueryServer.clients.set(clientId, client); return client; }
get(id) { return this.clients.get(id); }
it('basic beforeConnect rejection', async () => { Parse.Cloud.beforeConnect(function () { throw new Error('You shall not pass!'); }); const parseLiveQueryServer = new ParseLiveQueryServer({}); const parseWebSocket = { clientId: -1, }; await parseLiveQueryServer._handleConnect(parseWebSocket, { sessionToken: 'token', }); expect(parseLiveQueryServer.clients.size).toBe(0); const Client = require('../lib/LiveQuery/Client').Client; expect(Client.pushError).toHaveBeenCalled(); });
async listen(port, hostname, callback) { const app = await this.createExpressApp() const server = http.createServer(app) if (process.env.NODE_ENV === 'development') { const sockjs = require('sockjs') const echo = sockjs.createServer({ log: () => null }) echo.on('connection', connection => { this._app.clients[connection.id] = connection connection.on('close', () => { delete this._app.clients[connection.id] }) }) echo.installHandlers(server, { prefix: this._urls.sockjs.endpoint }) } server.listen(port, hostname, callback) }
initSync() { const options = this.options; assert(!(options.client && options.clients), `egg:singleton ${this.name} can not set options.client and options.clients both`); // alias app[name] as client, but still support createInstance method if (options.client) { const client = this.createInstance(options.client, options.name); this.app[this.name] = client; this._extendDynamicMethods(client); return; } // multi clent, use app[name].getInstance(id) if (options.clients) { Object.keys(options.clients).forEach(id => { const client = this.createInstance(options.clients[id], id); this.clients.set(id, client); }); this.app[this.name] = this; return; } // no config.clients and config.client this.app[this.name] = this; }
it('can handle connect command', async () => { const parseLiveQueryServer = new ParseLiveQueryServer({}); const parseWebSocket = { clientId: -1, }; parseLiveQueryServer._validateKeys = jasmine .createSpy('validateKeys') .and.returnValue(true); await parseLiveQueryServer._handleConnect(parseWebSocket, { sessionToken: 'token', }); const clientKeys = parseLiveQueryServer.clients.keys(); expect(parseLiveQueryServer.clients.size).toBe(1); const firstKey = clientKeys.next().value; expect(parseWebSocket.clientId).toBe(firstKey); const client = parseLiveQueryServer.clients.get(firstKey); expect(client).not.toBeNull(); // Make sure we send connect response to the client expect(client.pushConnect).toHaveBeenCalled(); });
async initAsync() { const options = this.options; assert(!(options.client && options.clients), `egg:singleton ${this.name} can not set options.client and options.clients both`); // alias app[name] as client, but still support createInstance method if (options.client) { const client = await this.createInstanceAsync(options.client, options.name); this.app[this.name] = client; this._extendDynamicMethods(client); return; } // multi clent, use app[name].getInstance(id) if (options.clients) { await Promise.all(Object.keys(options.clients).map(id => { return this.createInstanceAsync(options.clients[id], id) .then(client => this.clients.set(id, client)); })); this.app[this.name] = this; return; } // no config.clients and config.client this.app[this.name] = this; }
it('can be initialized from ParseServer without httpServer', function (done) { const parseLiveQueryServer = ParseServer.createLiveQueryServer(undefined, { port: 22345, }); expect(parseLiveQueryServer.clientId).toBeUndefined(); expect(parseLiveQueryServer.clients.size).toBe(0); expect(parseLiveQueryServer.subscriptions.size).toBe(0); parseLiveQueryServer.server.close(done); });
it('can be initialized from ParseServer', function () { const httpServer = {}; const parseLiveQueryServer = ParseServer.createLiveQueryServer( httpServer, {} ); expect(parseLiveQueryServer.clientId).toBeUndefined(); expect(parseLiveQueryServer.clients.size).toBe(0); expect(parseLiveQueryServer.subscriptions.size).toBe(0); });
async listen(port, hostname, callback) { const app = await this.createExpressApp() const server = http.createServer(app) if (process.env.NODE_ENV === 'development') { const sockjs = require('sockjs') const echo = sockjs.createServer({ log: () => null }) echo.on('connection', connection => { this._app.clients[connection.id] = connection connection.on('close', () => { delete this._app.clients[connection.id] }) }) echo.installHandlers(server, { prefix: this._urls.sockjs.endpoint }) } server.listen(port, hostname, callback) }