Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Support passing a WebSocket.Server options object to the FirebaseServ…
Browse files Browse the repository at this point in the history
…er constructor (#101)

* Remove some trailing whitespace in README.md.

* Support passing a WebSocket.Server options object to the FirebaseServer constructor.

This is useful to start a Firebase server on an existing http.Server.
  • Loading branch information
tommie authored and urish committed Aug 18, 2017
1 parent 0c86771 commit 1ae90a0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,31 @@ This package installs a CLI script called `firebase-server`. The following comma
start a firebase server on port 5555:

node_modules/.bin/firebase-server -p 5555
To bootstrap the server with some data you can use the `-d,--data` or the `-f,--file` option.

To bootstrap the server with some data you can use the `-d,--data` or the `-f,--file` option.
_Note: The file option will override the data option._

node_modules/.bin/firebase-server -d '{"foo": "bar"}'

node_modules/.bin/firebase-server -f ./path/to/data.json
To load [Firebase Security rules](https://firebase.google.com/docs/database/security/) upon startup you can use the `-r,--rules` option.

To load [Firebase Security rules](https://firebase.google.com/docs/database/security/) upon startup you can use the `-r,--rules` option.

node_modules/.bin/firebase-server -r ./path/to/rules.json

For more information, run:

node_modules/.bin/firebase-server -h

### FirebaseServer methods

The constructor signature is `FirebaseServer(portOrOptions, name, data)` where
`portOrOptions` is either a port number or a
[`WebSocket.Server`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback)
options object with either `port` or `server` set. `name` is optional and is
just used to report the server name to clients. `data` is the initial contents
of the database.

FirebaseServer instances have the following API:

* `close(callback)` - Stops the server (closes the server socket) and then calls the callback
Expand Down
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function normalizePath(fullPath) {
};
}

function FirebaseServer(port, name, data) {
function FirebaseServer(portOrOptions, name, data) {
this.name = name || 'mock.firebase.server';

// Firebase is more than just a "database" now; the "realtime database" is
Expand All @@ -82,9 +82,23 @@ function FirebaseServer(port, name, data) {

this.baseRef.set(data || null);

this._wss = new WebSocketServer({
port: port
});
var port = portOrOptions;
if (typeof portOrOptions === 'object') {
this._wss = new WebSocketServer(portOrOptions);
if (portOrOptions.server) {
var address = portOrOptions.server.address();
if (address) {
port = address.port;
}
} else {
port = portOrOptions.port;
}
} else {
this._wss = new WebSocketServer({
port: portOrOptions
});
port = portOrOptions;
}

this._clock = new TestableClock();
this._tokenValidator = new TokenValidator(null, this._clock);
Expand Down
17 changes: 17 additions & 0 deletions test/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var PORT = 45000;

var originalWebsocket = require('faye-websocket');
var assert = require('assert');
var http = require('http');
var proxyquire = require('proxyquire');
var _ = require('lodash');

Expand Down Expand Up @@ -89,6 +90,22 @@ describe('Firebase Server', function () {
return app.database().ref();
}

it('should successfully use an existing http.Server', function (done) {
var httpServer = http.createServer();
httpServer.listen(sequentialPort);
var fbServer = new FirebaseServer({server: httpServer}, 'localhost:' + sequentialPort);
sequentialPort++;
fbServer.close(function () {
httpServer.close(done);
});
});

it('should successfully use port within options', function (done) {
var fbServer = new FirebaseServer({port: sequentialPort}, 'localhost:' + sequentialPort);
sequentialPort++;
fbServer.close(done);
});

it('should successfully accept a client connection', function (done) {
var port = newFirebaseServer();
var client = newFirebaseClient(port);
Expand Down

0 comments on commit 1ae90a0

Please sign in to comment.