-
Notifications
You must be signed in to change notification settings - Fork 909
/
Copy pathindex.ts
736 lines (635 loc) · 19.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/**
* @license
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import firebase from 'firebase';
import 'firebase/database';
import 'firebase/firestore';
import 'firebase/storage';
import type { app } from 'firebase-admin';
import { _FirebaseApp } from '@firebase/app-types/private';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import * as request from 'request';
import { base64 } from '@firebase/util';
import { setLogLevel, LogLevel } from '@firebase/logger';
import { Component, ComponentType } from '@firebase/component';
const { firestore, database, storage } = firebase;
export { firestore, database, storage };
/** If this environment variable is set, use it for the database emulator's address. */
const DATABASE_ADDRESS_ENV: string = 'FIREBASE_DATABASE_EMULATOR_HOST';
/** The default address for the local database emulator. */
const DATABASE_ADDRESS_DEFAULT: string = 'localhost:9000';
/** If this environment variable is set, use it for the Firestore emulator. */
const FIRESTORE_ADDRESS_ENV: string = 'FIRESTORE_EMULATOR_HOST';
/** The default address for the local Firestore emulator. */
const FIRESTORE_ADDRESS_DEFAULT: string = 'localhost:8080';
/** If this environment variable is set, use it for the Storage emulator. */
const FIREBASE_STORAGE_ADDRESS_ENV: string = 'FIREBASE_STORAGE_EMULATOR_HOST';
const CLOUD_STORAGE_ADDRESS_ENV: string = 'STORAGE_EMULATOR_HOST';
/** The default address for the local Firestore emulator. */
const STORAGE_ADDRESS_DEFAULT: string = 'localhost:9199';
/** Environment variable to locate the Emulator Hub */
const HUB_HOST_ENV: string = 'FIREBASE_EMULATOR_HUB';
/** The default address for the Emulator Hub */
const HUB_HOST_DEFAULT: string = 'localhost:4400';
/** The actual address for the database emulator */
let _databaseHost: string | undefined = undefined;
/** The actual address for the Firestore emulator */
let _firestoreHost: string | undefined = undefined;
/** The actual address for the Storage emulator */
let _storageHost: string | undefined = undefined;
/** The actual address for the Emulator Hub */
let _hubHost: string | undefined = undefined;
export type Provider =
| 'custom'
| 'email'
| 'password'
| 'phone'
| 'anonymous'
| 'google.com'
| 'facebook.com'
| 'github.com'
| 'twitter.com'
| 'microsoft.com'
| 'apple.com';
export type FirebaseIdToken = {
// Always set to https://securetoken.google.com/PROJECT_ID
iss: string;
// Always set to PROJECT_ID
aud: string;
// The user's unique id
sub: string;
// The token issue time, in seconds since epoch
iat: number;
// The token expiry time, normally 'iat' + 3600
exp: number;
// The user's unique id, must be equal to 'sub'
user_id: string;
// The time the user authenticated, normally 'iat'
auth_time: number;
// The sign in provider, only set when the provider is 'anonymous'
provider_id?: 'anonymous';
// The user's primary email
email?: string;
// The user's email verification status
email_verified?: boolean;
// The user's primary phone number
phone_number?: string;
// The user's display name
name?: string;
// The user's profile photo URL
picture?: string;
// Information on all identities linked to this user
firebase: {
// The primary sign-in provider
sign_in_provider: Provider;
// A map of providers to the user's list of unique identifiers from
// each provider
identities?: { [provider in Provider]?: string[] };
};
// Custom claims set by the developer
[claim: string]: any;
};
// To avoid a breaking change, we accept the 'uid' option here, but
// new users should prefer 'sub' instead.
export type TokenOptions = Partial<FirebaseIdToken> & { uid?: string };
/**
* Host/port configuration for applicable Firebase Emulators.
*/
export type FirebaseEmulatorOptions = {
firestore?: {
host: string;
port: number;
};
database?: {
host: string;
port: number;
};
storage?: {
host: string;
port: number;
};
hub?: {
host: string;
port: number;
};
};
function createUnsecuredJwt(token: TokenOptions, projectId?: string): string {
// Unsecured JWTs use "none" as the algorithm.
const header = {
alg: 'none',
kid: 'fakekid',
type: 'JWT'
};
const project = projectId || 'fake-project';
const iat = token.iat || 0;
const uid = token.sub || token.uid || token.user_id;
if (!uid) {
throw new Error("Auth must contain 'sub', 'uid', or 'user_id' field!");
}
const payload: FirebaseIdToken = {
// Set all required fields to decent defaults
iss: `https://securetoken.google.com/${project}`,
aud: project,
iat: iat,
exp: iat + 3600,
auth_time: iat,
sub: uid,
user_id: uid,
firebase: {
sign_in_provider: 'custom',
identities: {}
},
// Override with user options
...token
};
// Remove the uid option since it's not actually part of the token spec.
if (payload.uid) {
delete payload.uid;
}
// Unsecured JWTs use the empty string as a signature.
const signature = '';
return [
base64.encodeString(JSON.stringify(header), /*webSafe=*/ false),
base64.encodeString(JSON.stringify(payload), /*webSafe=*/ false),
signature
].join('.');
}
export function apps(): firebase.app.App[] {
return firebase.apps;
}
export type AppOptions = {
databaseName?: string;
projectId?: string;
storageBucket?: string;
auth?: TokenOptions;
};
/** Construct an App authenticated with options.auth. */
export function initializeTestApp(options: AppOptions): firebase.app.App {
const jwt = options.auth
? createUnsecuredJwt(options.auth, options.projectId)
: undefined;
return initializeApp(
jwt,
options.databaseName,
options.projectId,
options.storageBucket
);
}
export type AdminAppOptions = {
databaseName?: string;
projectId?: string;
storageBucket?: string;
};
/** Construct an App authenticated as an admin user. */
export function initializeAdminApp(options: AdminAppOptions): app.App {
const admin = require('firebase-admin');
const app: app.App = admin.initializeApp(
getAppOptions(
options.databaseName,
options.projectId,
options.storageBucket
),
getRandomAppName()
);
if (options.projectId) {
app.firestore().settings({
host: getFirestoreHost(),
ssl: false
});
}
return app;
}
/**
* Set the host and port configuration for applicable emulators. This will override any values
* found in environment variables. Must be called before initializeAdminApp or initializeTestApp.
*
* @param options options object.
*/
export function useEmulators(options: FirebaseEmulatorOptions): void {
if (
!(options.database || options.firestore || options.storage || options.hub)
) {
throw new Error(
"Argument to useEmulators must contain at least one of 'database', 'firestore', 'storage', or 'hub'."
);
}
if (options.database) {
_databaseHost = getAddress(options.database.host, options.database.port);
}
if (options.firestore) {
_firestoreHost = getAddress(options.firestore.host, options.firestore.port);
}
if (options.storage) {
_storageHost = getAddress(options.storage.host, options.storage.port);
}
if (options.hub) {
_hubHost = getAddress(options.hub.host, options.hub.port);
}
}
/**
* Use the Firebase Emulator hub to discover other running emulators. Call useEmulators() with
* the result to configure the library to use the discovered emulators.
*
* @param hubHost the host where the Emulator Hub is running (ex: 'localhost')
* @param hubPort the port where the Emulator Hub is running (ex: 4400)
*/
export async function discoverEmulators(
hubHost?: string,
hubPort?: number
): Promise<FirebaseEmulatorOptions> {
if ((hubHost && !hubPort) || (!hubHost && hubPort)) {
throw new Error(
`Invalid configuration hubHost=${hubHost} and hubPort=${hubPort}. If either parameter is supplied, both must be defined.`
);
}
const hubAddress =
hubHost && hubPort ? getAddress(hubHost, hubPort) : getHubHost();
const res = await requestPromise(request.get, {
method: 'GET',
uri: `http://${hubAddress}/emulators`
});
if (res.statusCode !== 200) {
throw new Error(
`HTTP Error ${res.statusCode} when attempting to reach Emulator Hub at ${hubAddress}, are you sure it is running?`
);
}
const options: FirebaseEmulatorOptions = {};
const data = JSON.parse(res.body);
if (data.database) {
options.database = {
host: data.database.host,
port: data.database.port
};
}
if (data.firestore) {
options.firestore = {
host: data.firestore.host,
port: data.firestore.port
};
}
if (data.storage) {
options.storage = {
host: data.storage.host,
port: data.storage.port
};
}
if (data.hub) {
options.hub = {
host: data.hub.host,
port: data.hub.port
};
}
return options;
}
function getAddress(host: string, port: number) {
if (host.includes('::')) {
return `[${host}]:${port}`;
} else {
return `${host}:${port}`;
}
}
function getDatabaseHost() {
if (!_databaseHost) {
const fromEnv = process.env[DATABASE_ADDRESS_ENV];
if (fromEnv) {
_databaseHost = fromEnv;
} else {
console.warn(
`Warning: ${DATABASE_ADDRESS_ENV} not set, using default value ${DATABASE_ADDRESS_DEFAULT}`
);
_databaseHost = DATABASE_ADDRESS_DEFAULT;
}
}
return _databaseHost;
}
function getFirestoreHost() {
if (!_firestoreHost) {
const fromEnv = process.env[FIRESTORE_ADDRESS_ENV];
if (fromEnv) {
_firestoreHost = fromEnv;
} else {
console.warn(
`Warning: ${FIRESTORE_ADDRESS_ENV} not set, using default value ${FIRESTORE_ADDRESS_DEFAULT}`
);
_firestoreHost = FIRESTORE_ADDRESS_DEFAULT;
}
}
return _firestoreHost;
}
function getStorageHost() {
if (!_storageHost) {
const fromEnv =
process.env[FIREBASE_STORAGE_ADDRESS_ENV] ||
process.env[CLOUD_STORAGE_ADDRESS_ENV];
if (fromEnv) {
// The STORAGE_EMULATOR_HOST env var is an older Cloud Standard which includes http:// while
// the FIREBASE_STORAGE_EMULATOR_HOST is a newer variable supported beginning in the Admin
// SDK v9.7.0 which does not have the protocol.
_storageHost = fromEnv.replace('http://', '');
} else {
console.warn(
`Warning: ${FIREBASE_STORAGE_ADDRESS_ENV} not set, using default value ${STORAGE_ADDRESS_DEFAULT}`
);
_storageHost = STORAGE_ADDRESS_DEFAULT;
}
}
return _storageHost;
}
function getHubHost() {
if (!_hubHost) {
const fromEnv = process.env[HUB_HOST_ENV];
if (fromEnv) {
_hubHost = fromEnv;
} else {
console.warn(
`Warning: ${HUB_HOST_ENV} not set, using default value ${HUB_HOST_DEFAULT}`
);
_hubHost = HUB_HOST_DEFAULT;
}
}
return _hubHost;
}
function parseHost(host: string): { hostname: string; port: number } {
const withProtocol = host.startsWith('http') ? host : `http://${host}`;
const u = new URL(withProtocol);
return {
hostname: u.hostname,
port: Number.parseInt(u.port, 10)
};
}
function getRandomAppName(): string {
return 'app-' + new Date().getTime() + '-' + Math.random();
}
function getDatabaseUrl(databaseName: string) {
return `http://${getDatabaseHost()}?ns=${databaseName}`;
}
function getAppOptions(
databaseName?: string,
projectId?: string,
storageBucket?: string
): { [key: string]: string } {
let appOptions: { [key: string]: string } = {};
if (databaseName) {
appOptions['databaseURL'] = getDatabaseUrl(databaseName);
}
if (projectId) {
appOptions['projectId'] = projectId;
}
if (storageBucket) {
appOptions['storageBucket'] = storageBucket;
}
return appOptions;
}
function initializeApp(
accessToken?: string,
databaseName?: string,
projectId?: string,
storageBucket?: string
): firebase.app.App {
const appOptions = getAppOptions(databaseName, projectId, storageBucket);
const app = firebase.initializeApp(appOptions, getRandomAppName());
if (accessToken) {
const mockAuthComponent = new Component(
'auth-internal',
() =>
({
getToken: async () => ({ accessToken: accessToken }),
getUid: () => null,
addAuthTokenListener: listener => {
// Call listener once immediately with predefined accessToken.
listener(accessToken);
},
removeAuthTokenListener: () => {}
} as FirebaseAuthInternal),
ComponentType.PRIVATE
);
((app as unknown) as _FirebaseApp)._addOrOverwriteComponent(
mockAuthComponent
);
}
if (databaseName) {
const { hostname, port } = parseHost(getDatabaseHost());
app.database().useEmulator(hostname, port);
// Toggle network connectivity to force a reauthentication attempt.
// This mitigates a minor race condition where the client can send the
// first database request before authenticating.
app.database().goOffline();
app.database().goOnline();
}
if (projectId) {
const { hostname, port } = parseHost(getFirestoreHost());
app.firestore().useEmulator(hostname, port);
}
if (storageBucket) {
const { hostname, port } = parseHost(getStorageHost());
app.storage().useEmulator(hostname, port);
}
/**
Mute warnings for the previously-created database and whatever other
objects were just created.
*/
setLogLevel(LogLevel.ERROR);
return app;
}
export type LoadDatabaseRulesOptions = {
databaseName: string;
rules: string;
};
export async function loadDatabaseRules(
options: LoadDatabaseRulesOptions
): Promise<void> {
if (!options.databaseName) {
throw Error('databaseName not specified');
}
if (!options.rules) {
throw Error('must provide rules to loadDatabaseRules');
}
const resp = await requestPromise(request.put, {
method: 'PUT',
uri: `http://${getDatabaseHost()}/.settings/rules.json?ns=${
options.databaseName
}`,
headers: { Authorization: 'Bearer owner' },
body: options.rules
});
if (resp.statusCode !== 200) {
throw new Error(JSON.parse(resp.body.error));
}
}
export type LoadFirestoreRulesOptions = {
projectId: string;
rules: string;
};
export async function loadFirestoreRules(
options: LoadFirestoreRulesOptions
): Promise<void> {
if (!options.projectId) {
throw new Error('projectId not specified');
}
if (!options.rules) {
throw new Error('must provide rules to loadFirestoreRules');
}
const resp = await requestPromise(request.put, {
method: 'PUT',
uri: `http://${getFirestoreHost()}/emulator/v1/projects/${
options.projectId
}:securityRules`,
body: JSON.stringify({
rules: {
files: [{ content: options.rules }]
}
})
});
if (resp.statusCode !== 200) {
throw new Error(JSON.parse(resp.body.error));
}
}
export type LoadStorageRulesOptions = {
rules: string;
};
export async function loadStorageRules(
options: LoadStorageRulesOptions
): Promise<void> {
if (!options.rules) {
throw new Error('must provide rules to loadStorageRules');
}
const resp = await requestPromise(request.put, {
method: 'PUT',
uri: `http://${getStorageHost()}/internal/setRules`,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
rules: {
files: [{ name: 'storage.rules', content: options.rules }]
}
})
});
if (resp.statusCode !== 200) {
throw new Error(resp.body);
}
}
export type ClearFirestoreDataOptions = {
projectId: string;
};
export async function clearFirestoreData(
options: ClearFirestoreDataOptions
): Promise<void> {
if (!options.projectId) {
throw new Error('projectId not specified');
}
const resp = await requestPromise(request.delete, {
method: 'DELETE',
uri: `http://${getFirestoreHost()}/emulator/v1/projects/${
options.projectId
}/databases/(default)/documents`,
body: JSON.stringify({
database: `projects/${options.projectId}/databases/(default)`
})
});
if (resp.statusCode !== 200) {
throw new Error(JSON.parse(resp.body.error));
}
}
/**
* Run a setup function with background Cloud Functions triggers disabled. This can be used to
* import data into the Realtime Database or Cloud Firestore emulator without triggering locally
* emulated Cloud Functions.
*
* This method only works with Firebase CLI version 8.13.0 or higher.
*
* @param fn an function which returns a promise.
*/
export async function withFunctionTriggersDisabled<TResult>(
fn: () => TResult | Promise<TResult>
): Promise<TResult> {
const hubHost = getHubHost();
// Disable background triggers
const disableRes = await requestPromise(request.put, {
method: 'PUT',
uri: `http://${hubHost}/functions/disableBackgroundTriggers`
});
if (disableRes.statusCode !== 200) {
throw new Error(
`HTTP Error ${disableRes.statusCode} when disabling functions triggers, are you using firebase-tools 8.13.0 or higher?`
);
}
// Run the user's function
let result: TResult | undefined = undefined;
try {
result = await fn();
} finally {
// Re-enable background triggers
const enableRes = await requestPromise(request.put, {
method: 'PUT',
uri: `http://${hubHost}/functions/enableBackgroundTriggers`
});
if (enableRes.statusCode !== 200) {
throw new Error(
`HTTP Error ${enableRes.statusCode} when enabling functions triggers, are you using firebase-tools 8.13.0 or higher?`
);
}
}
// Return the user's function result
return result;
}
export function assertFails(pr: Promise<any>): any {
return pr.then(
(v: any) => {
return Promise.reject(
new Error('Expected request to fail, but it succeeded.')
);
},
(err: any) => {
const errCode = (err && err.code && err.code.toLowerCase()) || '';
const errMessage =
(err && err.message && err.message.toLowerCase()) || '';
const isPermissionDenied =
errCode === 'permission-denied' ||
errCode === 'permission_denied' ||
errMessage.indexOf('permission_denied') >= 0 ||
errMessage.indexOf('permission denied') >= 0;
if (!isPermissionDenied) {
return Promise.reject(
new Error(
`Expected PERMISSION_DENIED but got unexpected error: ${err}`
)
);
}
return err;
}
);
}
export function assertSucceeds(pr: Promise<any>): any {
return pr;
}
function requestPromise(
method: typeof request.get,
options: request.CoreOptions & request.UriOptions
): Promise<{ statusCode: number; body: any }> {
return new Promise((resolve, reject) => {
const callback: request.RequestCallback = (err, resp, body) => {
if (err) {
reject(err);
} else {
resolve({ statusCode: resp.statusCode, body });
}
};
// Unfortunately request's default method is not very test-friendly so having
// the caler pass in the method here makes this whole thing compatible with sinon
method(options, callback);
});
}