Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'wyszynski/rtdb-emulator-functions' of https://github.co…
…m/firebase/firebase-tools into wyszynski/rtdb-emulator-functions
- Loading branch information
Showing
8 changed files
with
236 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import * as request from "request"; | ||
import { Response } from "request"; | ||
import * as responseToError from "../responseToError"; | ||
import * as utils from "../utils"; | ||
import * as FirebaseError from "../error"; | ||
import * as logger from "../logger"; | ||
import * as api from "../api"; | ||
|
||
export interface ListRemote { | ||
/** | ||
* Call the shallow get API with limitToFirst=numSubPath. | ||
* @param path the path to list | ||
* @param numSubPath the number of subPaths to fetch. | ||
* @param startAfter omit list entries comparing lower than `startAfter` | ||
* @param timeout milliseconds after which to timeout the request | ||
* @return the list of sub pathes found. | ||
*/ | ||
listPath( | ||
path: string, | ||
numSubPath: number, | ||
startAfter?: string, | ||
timeout?: number | ||
): Promise<string[]>; | ||
} | ||
|
||
export class RTDBListRemote implements ListRemote { | ||
constructor(private instance: string) {} | ||
|
||
async listPath( | ||
path: string, | ||
numSubPath: number, | ||
startAfter?: string, | ||
timeout?: number | ||
): Promise<string[]> { | ||
const url = `${utils.addSubdomain(api.realtimeOrigin, this.instance)}${path}.json`; | ||
|
||
const params: any = { | ||
shallow: true, | ||
limitToFirst: numSubPath, | ||
}; | ||
if (startAfter) { | ||
params.startAfter = startAfter; | ||
} | ||
if (timeout) { | ||
params.timeout = `${timeout}ms`; | ||
} | ||
|
||
const t0 = Date.now(); | ||
const reqOptionsWithToken = await api.addRequestHeaders({ url }); | ||
reqOptionsWithToken.qs = params; | ||
const paths = await new Promise<string[]>((resolve, reject) => { | ||
request.get(reqOptionsWithToken, (err: Error, res: Response, body: any) => { | ||
if (err) { | ||
return reject( | ||
new FirebaseError("Unexpected error while listing subtrees", { | ||
exit: 2, | ||
original: err, | ||
}) | ||
); | ||
} else if (res.statusCode >= 400) { | ||
return reject(responseToError(res, body)); | ||
} | ||
let data; | ||
try { | ||
data = JSON.parse(body); | ||
} catch (e) { | ||
return reject( | ||
new FirebaseError("Malformed JSON response in shallow get ", { | ||
exit: 2, | ||
original: e, | ||
}) | ||
); | ||
} | ||
if (data) { | ||
return resolve(Object.keys(data)); | ||
} | ||
return resolve([]); | ||
}); | ||
}); | ||
const dt = Date.now() - t0; | ||
logger.debug(`[database] sucessfully fetched ${paths.length} path at ${path} ${dt}`); | ||
return paths; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import * as pathLib from "path"; | ||
import * as chai from "chai"; | ||
|
||
import { ListRemote } from "../../database/listRemote"; | ||
|
||
const expect = chai.expect; | ||
|
||
/** | ||
* `FakeListRemote` is a test fixture for verifying logic lives in the | ||
* `DatabaseRemove` class. It is essentially a mock for the Realtime Database | ||
* that accepts a JSON tree to serve upon construction. | ||
*/ | ||
export class FakeListRemote implements ListRemote { | ||
data: any; | ||
delay: number; | ||
|
||
/** | ||
* @param data the fake database structure. Each leaf is an integer | ||
* representing the subtree's size. | ||
*/ | ||
constructor(data: any) { | ||
this.data = data; | ||
this.delay = 0; | ||
} | ||
|
||
async listPath( | ||
path: string, | ||
numChildren: number, | ||
startAfter?: string, | ||
timeout?: number | ||
): Promise<string[]> { | ||
if (timeout === 0) { | ||
throw new Error("timeout"); | ||
} | ||
const d = this.dataAtPath(path); | ||
if (d) { | ||
let keys = Object.keys(d); | ||
/* | ||
* We mirror a critical implementation detail of here. Namely, the | ||
* `startAfter` option (if it exists) is applied to the resulting key set | ||
* before the `limitToFirst` option. | ||
*/ | ||
if (startAfter) { | ||
keys = keys.filter((key) => key > startAfter); | ||
} | ||
keys = keys.slice(0, numChildren); | ||
return keys; | ||
} | ||
return []; | ||
} | ||
|
||
private size(data: any): number { | ||
if (typeof data === "number") { | ||
return data; | ||
} | ||
let size = 0; | ||
for (const key of Object.keys(data)) { | ||
size += this.size(data[key]); | ||
} | ||
return size; | ||
} | ||
|
||
private dataAtPath(path: string): any { | ||
const splitedPath = path.slice(1).split("/"); | ||
let d = this.data; | ||
for (const p of splitedPath) { | ||
if (d && p !== "") { | ||
if (typeof d === "number") { | ||
d = null; | ||
} else { | ||
d = d[p]; | ||
} | ||
} | ||
} | ||
return d; | ||
} | ||
} | ||
|
||
describe("FakeListRemote", () => { | ||
it("should return limit the number of subpaths returned", async () => { | ||
const fakeDb = new FakeListRemote({ 1: 1, 2: 2, 3: 3, 4: 4 }); | ||
await expect(fakeDb.listPath("/", 4)).to.eventually.eql(["1", "2", "3", "4"]); | ||
await expect(fakeDb.listPath("/", 3)).to.eventually.eql(["1", "2", "3"]); | ||
await expect(fakeDb.listPath("/", 2)).to.eventually.eql(["1", "2"]); | ||
await expect(fakeDb.listPath("/", 1)).to.eventually.eql(["1"]); | ||
await expect(fakeDb.listPath("/", 4, "1")).to.eventually.eql(["2", "3", "4"]); | ||
await expect(fakeDb.listPath("/", 4, "2")).to.eventually.eql(["3", "4"]); | ||
await expect(fakeDb.listPath("/", 4, "3")).to.eventually.eql(["4"]); | ||
await expect(fakeDb.listPath("/", 4, "4")).to.eventually.eql([]); | ||
await expect(fakeDb.listPath("/", 3, "1")).to.eventually.eql(["2", "3", "4"]); | ||
await expect(fakeDb.listPath("/", 3, "2")).to.eventually.eql(["3", "4"]); | ||
await expect(fakeDb.listPath("/", 3, "3")).to.eventually.eql(["4"]); | ||
await expect(fakeDb.listPath("/", 3, "3")).to.eventually.eql(["4"]); | ||
await expect(fakeDb.listPath("/", 3, "4")).to.eventually.eql([]); | ||
await expect(fakeDb.listPath("/", 1, "1")).to.eventually.eql(["2"]); | ||
await expect(fakeDb.listPath("/", 1, "2")).to.eventually.eql(["3"]); | ||
await expect(fakeDb.listPath("/", 1, "3")).to.eventually.eql(["4"]); | ||
await expect(fakeDb.listPath("/", 1, "4")).to.eventually.eql([]); | ||
await expect(fakeDb.listPath("/", 1, "1", 0)).to.be.rejected; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { expect } from "chai"; | ||
import * as nock from "nock"; | ||
|
||
import * as utils from "../../utils"; | ||
import * as api from "../../api"; | ||
import * as helpers from "../helpers"; | ||
import { RTDBListRemote } from "../../database/listRemote"; | ||
|
||
describe("ListRemote", () => { | ||
const instance = "fake-db"; | ||
const remote = new RTDBListRemote(instance); | ||
const serverUrl = utils.addSubdomain(api.realtimeOrigin, instance); | ||
|
||
afterEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
it("should return subpaths from shallow get request", async () => { | ||
nock(serverUrl) | ||
.get("/.json") | ||
.query({ shallow: true, limitToFirst: "1234" }) | ||
.reply(200, { | ||
a: true, | ||
x: true, | ||
f: true, | ||
}); | ||
await expect(remote.listPath("/", 1234)).to.eventually.eql(["a", "x", "f"]); | ||
}); | ||
}); |
Oops, something went wrong.