Skip to content

Commit

Permalink
Merge b3ae92e into 2014e83
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwski committed Jun 4, 2019
2 parents 2014e83 + b3ae92e commit 7fb24a6
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "6"
- "8"
- "10"
after_script:
Expand All @@ -9,7 +8,7 @@ jobs:
include:
- stage: hosting functional test
if: repo == head_repo OR type == push
node_js: "6"
node_js: "8"
before_script: ./scripts/decrypt-app-credentials.sh
script: ./scripts/test-hosting.sh
after_script: skip
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
@@ -0,0 +1 @@
* **BREAKING** The CLI no longer supports being run in Node 6 environments.
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -15,7 +15,6 @@
"lint:js": "eslint 'src/**/*.js'",
"lint:ts": "tslint --project tsconfig.json --config tslint.json",
"mocha": "nyc mocha --opts mocha.opts",
"prepublish": "npm run clean && npm run build",
"prepare": "npm run clean && npm run build",
"test": "npm run lint && npm run mocha"
},
Expand All @@ -40,9 +39,8 @@
],
"preferGlobal": true,
"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},
"engineStrict": true,
"author": "Firebase (https://firebase.google.com/)",
"license": "MIT",
"bugs": {
Expand Down
15 changes: 15 additions & 0 deletions src/bin/firebase.js
@@ -1,7 +1,22 @@
#!/usr/bin/env node
"use strict";

// Make check for Node 6, which is no longer supported by the CLI.
var semver = require("semver");
var pkg = require("../../package.json");
var nodeVersion = process.version;
if (!semver.satisfies(nodeVersion, pkg.engines.node)) {
console.error(
"Firebase CLI v" +
pkg.version +
" is incompatible with Node.js " +
nodeVersion +
" Please upgrade Node.js to version " +
pkg.engines.node
);
process.exit(1);
}

var updateNotifier = require("update-notifier")({ pkg: pkg });
updateNotifier.notify({ defer: true, isGlobal: true });

Expand Down
1 change: 1 addition & 0 deletions src/emulator/constants.ts
Expand Up @@ -13,6 +13,7 @@ const DEFAULT_HOST = "localhost";

export class Constants {
static SERVICE_FIRESTORE = "firestore.googleapis.com";
static SERVICE_REALTIME_DATABASE = "firebaseio.com";

static getDefaultHost(emulator: Emulators): string {
return DEFAULT_HOST;
Expand Down
25 changes: 15 additions & 10 deletions src/emulator/controller.ts
Expand Up @@ -154,17 +154,22 @@ export async function startAll(options: any): Promise<void> {

if (targets.indexOf(Emulators.DATABASE) > -1) {
const databaseAddr = Constants.getAddress(Emulators.DATABASE, options);
const databaseEmulator = new DatabaseEmulator({
host: databaseAddr.host,
port: databaseAddr.port,
});
let databaseEmulator;
if (targets.indexOf(Emulators.FUNCTIONS) > -1) {
const functionsAddr = Constants.getAddress(Emulators.FUNCTIONS, options);
databaseEmulator = new DatabaseEmulator({
host: databaseAddr.host,
port: databaseAddr.port,
functions_emulator_host: functionsAddr.host,
functions_emulator_port: functionsAddr.port,
});
} else {
databaseEmulator = new DatabaseEmulator({
host: databaseAddr.host,
port: databaseAddr.port,
});
}
await startEmulator(databaseEmulator);

// TODO: When the database emulator is integrated with the Functions
// emulator, we will need to pass the port in and remove this warning
utils.logWarning(
`Note: the database emulator is not currently integrated with the functions emulator.`
);
}

if (targets.indexOf(Emulators.HOSTING) > -1) {
Expand Down
2 changes: 2 additions & 0 deletions src/emulator/databaseEmulator.ts
Expand Up @@ -5,6 +5,8 @@ import { Constants } from "./constants";
interface DatabaseEmulatorArgs {
port?: number;
host?: string;
functions_emulator_port?: number;
functions_emulator_host?: string;
}

export class DatabaseEmulator implements EmulatorInstance {
Expand Down
79 changes: 79 additions & 0 deletions src/emulator/functionsEmulator.ts
Expand Up @@ -32,6 +32,14 @@ import { EmulatorLogger, Verbosity } from "./emulatorLogger";

const EVENT_INVOKE = "functions:invoke";

/*
* The Realtime Database emulator expects the `path` field in its trigger
* definition to be relative to the database root. This regex is used to extract
* that path from the `resource` member in the trigger definition used by the
* functions emulator.
*/
const DATABASE_PATH_PATTERN = new RegExp("^projects/[^/]+/instances/[^/]+/refs(/.*)$");

export interface FunctionsEmulatorArgs {
port?: number;
host?: string;
Expand Down Expand Up @@ -490,6 +498,9 @@ You can probably fix this by running "npm install ${
case Constants.SERVICE_FIRESTORE:
await this.addFirestoreTrigger(this.projectId, definition);
break;
case Constants.SERVICE_REALTIME_DATABASE:
await this.addRealtimeDatabaseTrigger(this.projectId, definition);
break;
default:
EmulatorLogger.log("DEBUG", `Unsupported trigger: ${JSON.stringify(definition)}`);
EmulatorLogger.log(
Expand All @@ -514,6 +525,74 @@ You can probably fix this by running "npm install ${
return loadTriggers();
}

addRealtimeDatabaseTrigger(
projectId: string,
definition: EmulatedTriggerDefinition
): Promise<any> {
const databasePort = EmulatorRegistry.getPort(Emulators.DATABASE);
if (!databasePort) {
EmulatorLogger.log(
"INFO",
`Ignoring trigger "${
definition.name
}" because the Realtime Database emulator is not running.`
);
return Promise.resolve();
}
if (definition.eventTrigger === undefined) {
EmulatorLogger.log(
"WARN",
`Event trigger "${definition.name}" has undefined "eventTrigger" member`
);
return Promise.reject();
}

const result: string[] | null = DATABASE_PATH_PATTERN.exec(definition.eventTrigger.resource);
if (result === null || result.length !== 2) {
EmulatorLogger.log(
"WARN",
`Event trigger "${definition.name}" has malformed "resource" member. ` +
`${definition.eventTrigger.resource}`
);
return Promise.reject();
}

const bundle = JSON.stringify([
{
name: `projects/${projectId}/locations/_/functions/${definition.name}`,
path: result[1], // path stored in the first capture group
event: definition.eventTrigger.eventType,
topic: `projects/${projectId}/topics/${definition.name}`,
},
]);

EmulatorLogger.logLabeled(
"BULLET",
"functions",
`Setting up Realtime Database trigger "${definition.name}"`
);
logger.debug(`addDatabaseTrigger`, JSON.stringify(bundle));
return new Promise((resolve, reject) => {
request.put(
`http://localhost:${databasePort}/.settings/functionTriggers.json`,
{
auth: {
bearer: "owner",
},
body: bundle,
},
(err, res, body) => {
if (err) {
EmulatorLogger.log("WARN", "Error adding trigger: " + err);
reject();
return;
}
resolve();
}
);
});
}

addFirestoreTrigger(projectId: string, definition: EmulatedTriggerDefinition): Promise<any> {
const firestorePort = EmulatorRegistry.getPort(Emulators.FIRESTORE);
if (!firestorePort) {
Expand Down

0 comments on commit 7fb24a6

Please sign in to comment.