Skip to content

Commit 9b8fe65

Browse files
authored
Fix emulated database URL parse issue (#838)
1 parent c69c9bc commit 9b8fe65

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"build:pack": "rm -rf lib && npm install && tsc -p tsconfig.release.json && npm pack",
3232
"build:release": "npm install --production && npm install --no-save typescript firebase-admin && tsc -p tsconfig.release.json",
3333
"build": "tsc -p tsconfig.release.json",
34+
"build:watch": "npm run build -- -w",
3435
"format": "prettier --check '**/*.{json,md,ts,yml,yaml}'",
3536
"format:fix": "prettier --write '**/*.{json,md,ts,yml,yaml}'",
3637
"lint": "tslint --config tslint.json --project tsconfig.json ",

spec/providers/database.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,17 @@ describe('Database Functions', () => {
472472
);
473473
}).to.throw(Error);
474474
});
475+
476+
it('should use the emulator host when present', () => {
477+
process.env.FIREBASE_DATABASE_EMULATOR_HOST = 'localhost:1234';
478+
const [instance, path] = database.extractInstanceAndPath(
479+
'projects/_/instances/foo/refs/bar',
480+
'firebaseio-staging.com'
481+
);
482+
expect(instance).to.equal('http://localhost:1234/?ns=foo');
483+
expect(path).to.equal('/bar');
484+
delete process.env.FIREBASE_DATABASE_EMULATOR_HOST;
485+
});
475486
});
476487

477488
describe('DataSnapshot', () => {

src/providers/database.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,15 @@ export function extractInstanceAndPath(
334334
`Expect project to be '_' in a Firebase Realtime Database event`
335335
);
336336
}
337-
const dbInstance = 'https://' + dbInstanceName + '.' + domain;
338-
return [dbInstance, path];
337+
338+
const emuHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
339+
if (emuHost) {
340+
const dbInstance = `http://${emuHost}/?ns=${dbInstanceName}`;
341+
return [dbInstance, path];
342+
} else {
343+
const dbInstance = 'https://' + dbInstanceName + '.' + domain;
344+
return [dbInstance, path];
345+
}
339346
}
340347

341348
/**

0 commit comments

Comments
 (0)