-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
247 additions
and
80 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,103 @@ | ||
import * as pathLib from "path"; | ||
import { expect } from "chai"; | ||
|
||
import { ListRemote } from "../../database/listRemote"; | ||
|
||
export class FakeListRemote implements ListRemote { | ||
data: any; | ||
delay: number; | ||
|
||
/** | ||
* @constructor | ||
* @param data the fake database structure. Each leaf is an integer representing the subtree's size. | ||
* @param largeThreshold the threshold to determine if a delete exceeds the writeSizeLimit. | ||
* If the sum of all leaves to delete is larger than largeThreshold, | ||
* the delete will return false. | ||
*/ | ||
constructor(data: any) { | ||
this.data = data; | ||
this.delay = 0; | ||
} | ||
|
||
async listPath( | ||
path: string, | ||
numChildren: number, | ||
startAfter?: string, | ||
timeout?: number | ||
): Promise<string[]> { | ||
const startTime = Date.now(); | ||
await new Promise((resolve, _) => setTimeout(resolve, this.delay)); | ||
const latency = Date.now() - startTime; | ||
if (timeout && latency >= timeout) { | ||
return Promise.reject(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 Promise.resolve(keys); | ||
} | ||
return Promise.resolve([]); | ||
} | ||
|
||
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 setArtificialDelay(delay: number): any { | ||
this.delay = delay; | ||
} | ||
|
||
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([]); | ||
}); | ||
}); |
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,37 @@ | ||
import { expect } from "chai"; | ||
import * as sinon from "sinon"; | ||
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); | ||
let sandbox: sinon.SinonSandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
helpers.mockAuth(sandbox); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
nock.cleanAll(); | ||
}); | ||
|
||
it("should return subpaths from shallow get request", () => { | ||
nock(serverUrl) | ||
.get("/.json") | ||
.query({ shallow: true, limitToFirst: "1234" }) | ||
.reply(200, { | ||
a: true, | ||
x: true, | ||
f: true, | ||
}); | ||
return expect(remote.listPath("/", 1234)).to.eventually.eql(["a", "x", "f"]); | ||
}); | ||
}); |
Oops, something went wrong.