Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

firebase-server

urish9.1kMITdeprecated1.1.0TypeScript support: included

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

Simple Firebase Server

firebase, server

readme

firebase-server

Firebase Web Socket Protocol Server. Useful for emulating the Firebase server in tests.

Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, Uri Shaked and contributors

Build Status Coverage Status npm version

Installation

You can install firebase-server through npm:

npm install --save-dev firebase-server

or yarn:

yarn add -D firebase-server

Usage examples

const FirebaseServer = require('firebase-server');

new FirebaseServer(5000, 'localhost', {
  states: {
    CA: 'California',
    AL: 'Alabama',
    KY: 'Kentucky'
  }
});

After running this server, you can create a Firebase client instance that connects to it:

import * as firebase from 'firebase/app';
import 'firebase/database';

const app = firebase.initializeApp({
  databaseURL: `ws://localhost:5000`,
});

app.database().ref().on('value', (snap) => {
  console.log('Got value: ', snap.val());
});

Setup with global test hooks

In the case of Mocha, you'd do something like the example below.

// => e.g. global-test-hooks.js
const FirebaseServer = require("firebase-server");

let firebaseServer;

before(() => {
   firebaseServer = new FirebaseServer(5000, "localhost");
});

after(async () => {
   await firebaseServer.close();
});
// => require the file with the --file flag
mocha --file /path/to/global-test-hooks.js

Command Line Interface

This package installs a CLI script called firebase-server. It can be installed locally or globally. If installed locally, use the following path to start the server: ./node_modules/.bin/firebase-server

The following command will start a firebase server on port 5555:

firebase-server -p 5555

... and with a specified bind IP address:

firebase-server -p 5555 -a 0.0.0.0

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.

firebase-server -d '{"foo": "bar"}'

firebase-server -f ./path/to/data.json

To load Firebase Security rules upon startup you can use the -r,--rules option.

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

You can also specify a shared client auth token secret with the -s argument:

firebase-server -s some-shared-secret

To enable REST API, run:

firebase-server -e

Note: currently REST API does not implement authentication or authorization.

To daemonize the server process, use:

firebase-server -b

To write the PID to a file, use:

firebase-server --pid /var/run/firebase-server.pid

_Note: PID file can be written with or without daemonization, and is NOT written by default when daemonizing.

For more information, run:

firebase-server -h

FirebaseServer methods

The constructor signature is FirebaseServer(portOrOptions, name, data) where portOrOptions is either a port number or a WebSocket.Server 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.

If you want the server to pick a free port for you, simply use the value 0 for the port. You can then get the assigned port number by calling the getPort() method on the returned server object.

FirebaseServer instances have the following API:

  • close(): Promise - Stops the server (closes the server socket)
  • getValue() - Returns a promise that will be resolved with the current data on the server
  • exportData() - Returns a promise that will be resolved with the current data on the server, including priority values. This is similar to DataSnapshot.exportVal().
  • address() - Returns the address the server is listening on
  • port(): number - Returns the port number the server is listening on
  • setRules(rules) - Sets the security rules for the server. Uses the targaryen library for rule validation.
  • setAuthSecret(secret) - Sets the shared secret used for validating Custom Authentication Tokens.
  • setTime(timestamp) - Sets the server time. The server time is returned by ServerValue.TIMESTAMP and is also used for checking the validity of Custom Authentication Tokens.

Debug logging

This project uses the excellent debug module for logging. It is configured by setting an environment variable:

$ DEBUG=* mocha                                # log everything
$ DEBUG=firebase-server* mocha                 # log everything from firebase-server
$ DEBUG=firebase-server:token-generator mocha  # log output from specific submodule

Advanced options are available from the debug docs

License

Released under the terms of MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

changelog

Changelog

1.1.0 - 2020-02-01

  • fix #136: upgrade targaryen to 3.1.0
  • fix #142: respond with null instead of {} for empty path
  • docs #140: Add global test hooks example to README.md
  • chore(deps): upgrade ws to 7.x
  • chore(deps): bump firebase from 6.0.1 to 7.2.3
  • chore(deps): bump mixin-deep from 1.3.1 to 1.3.2
  • chore(deps): bump handlebars from 4.0.11 to 4.5.1
  • chore(deps): bump lodash from 4.17.10 to 4.17.13
  • chore(deps): bump extend from 3.0.1 to 3.0.2
  • chore(deps): bump js-yaml from 3.11.0 to 3.13.1

1.0.2 - 2019-05-08

  • fix #138: security vulnerability affecting jwt-simple package

1.0.1 - 2018-11-29

  • fix #133: cli package vulnerability

1.0.0 - 2018-08-29

  • chore: upgrade dependencies (lodash, debug), addresses CVE-2017-16137, CVE-2018-3721
  • feat #130: add --version cli option

1.0.0-rc.2 - 2018-05-28

  • fix #123: Cannot run server v1.0.0-rc.1

1.0.0-rc.1 - 2018-05-20

  • docs: update usage example in README

1.0.0-rc.0 - 2018-05-20

  • Migrate project to TypeScript
  • Drop node 6.x support
  • Add address(), getPort() methods
  • close() method now returns a promise
  • Upgrade the Firebase JS Library to 5.x
  • Upgrade the ws library to 5.x
  • Split send payloads according to firebase's custom continuation format (#115, contributed by andrewparmet)
  • Align 'now' in Targaryen with firebase-server time (#112, contributed by dotdoom)

0.12.0 - 2017-11-16

0.11.0 - 2017-08-19

  • Migrate to Targaryen 3 (#100, contributed by dotdoom)
  • Support passing a WebSocket.Server options object to the FirebaseServer constructor (#101, contributed by tommie)
  • Fix security rules validation for updates (#99, contributed by dotdoom)
  • Add support for -s option (shared auth secret) (#91, contributed by dchaley)

0.10.1 - 2017-05-17

  • Properly fill in auth object for Android (#89, contributed by dotdoom)

0.10.0 - 2017-05-11

0.9.1 - 2017-02-16

  • Fix: CLI fails to start on Mac OS X (#80)

0.9.0 - 2017-02-06

  • Update targaryen dependency to 2.3.3 (#76, contributed by bmcbarron)
  • Add CLI data / file parameters (#78, contributed by mediavrog)

0.8.1 - 2017-01-29

  • Fix dropped listener callbacks for null nodes (#75, contributed by bmcbarron)

0.8.0 - 2017-01-22

  • Specify firebase client library as a dependency (#71, contributed by crowdcst)
  • Fix compatibility with Android client (path) (#74, contributed by dotdoom)

0.7.1 - 2016-11-28

  • Fix deprecation warning with Firebase 3.6.x (#68, contributed by mironal)
  • Fix broken unit tests with Firebase >= 3.3.0

0.7.0 - 2016-08-13

  • Upgrade ws dependency to 1.1.1 (latest version)

0.6.0 - 2016-06-22

  • Switch to Firebase 3.x. For firebase 2 support, use version 0.5.4 (#51, contributed by nfarina)

0.5.4 - 2016-03-08

  • Bugfix: large JSON messages spanning over several websocket frames cause errors (#39, contributed by abhishekgahlot)

0.5.3 - 2016-02-03

  • Add a firebase-server command line tool (CLI). See README for details.

0.5.2 - 2016-01-06

  • Support admin authentication with raw secret (#34), contributed by Alaneor)

0.5.1 - 2015-12-13

0.5.0 - 2015-10-28

  • Add Custom Authentication (#15, #25, #26 contributed by jamestalmage)
  • Support ServerValue.TIMESTAMP (see #18)
  • Use debug module instead of hand-rolled logging solution (#28, contributed by jamestalmage)
  • Bugfix: Transactions fail for nodes with priority value (see #23)

0.4.0 - 2015-10-24

  • Basic support for Firebase Security rules through targaryen (fixes #11)
  • Switch from MockFirebase to the official Firebase client library (#13, contributed by jamestalmage)
  • Deprecate the getData() method. Use instead the new getValue() method which returns a promise
  • Add an exportData() method for fetching the data along with priority values

0.3.1 - 2015-08-18

  • Bugfix: Wire protocol does not match Firebase server (fixes #9, contributed by azell)

0.3.0 - 2015-07-21

  • Implement update() (fixes #5)
  • Implement transaction()
  • Bugfix: remove() triggers two value events (fixes #6)

0.2.0 - 2015-06-12

  • Upgrade MockFirebase to 0.11.0, as well as other dependencies.
  • Bugfix: Value callbacks were always triggered with null first (#2)

0.1.1 - 2015-05-23

  • Fix a bug with supporting Firebase client library 2.2.4+ (fixes #1)

0.1.0 - 2014-11-21

  • Firebase 2.0 Support

0.0.2 - 2014-09-06

  • Add close() method to stop the server
  • Add getData() method that returns a copy of the server's data
  • Add functional tests
  • Make logging optional (through FirebaseServer.enableLogging())

0.0.1 - 2014-09-05

  • Initial release