-
Notifications
You must be signed in to change notification settings - Fork 929
/
functionsEmulatorRuntime.ts
1083 lines (955 loc) · 35.3 KB
/
functionsEmulatorRuntime.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as fs from "fs";
import { CloudFunction, DeploymentOptions, https } from "firebase-functions";
import * as express from "express";
import * as path from "path";
import * as admin from "firebase-admin";
import * as bodyParser from "body-parser";
import { pathToFileURL, URL } from "url";
import * as _ from "lodash";
import { EmulatorLog } from "./types";
import { Constants } from "./constants";
import {
findModuleRoot,
FunctionsRuntimeBundle,
HttpConstants,
SignatureType,
} from "./functionsEmulatorShared";
import { compareVersionStrings, isLocalHost } from "./functionsEmulatorUtils";
import { EventUtils } from "./events/types";
interface RequestWithRawBody extends express.Request {
rawBody: Buffer;
}
let functionModule: any;
let FUNCTION_TARGET_NAME: string;
let FUNCTION_SIGNATURE: string;
let FUNCTION_DEBUG_MODE: string;
let developerPkgJSON: PackageJSON | undefined;
/**
* Dynamically load import function to prevent TypeScript from
* transpiling into a require.
*
* See https://github.com/microsoft/TypeScript/issues/43329.
*/
// eslint-disable-next-line @typescript-eslint/no-implied-eval
const dynamicImport = new Function("modulePath", "return import(modulePath)");
function noOp(): false {
return false;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function requireAsync(moduleName: string, opts?: { paths: string[] }): Promise<any> {
return new Promise((res, rej) => {
try {
res(require(require.resolve(moduleName, opts))); // eslint-disable-line @typescript-eslint/no-var-requires
} catch (e: any) {
rej(e);
}
});
}
function requireResolveAsync(moduleName: string, opts?: { paths: string[] }): Promise<string> {
return new Promise((res, rej) => {
try {
res(require.resolve(moduleName, opts));
} catch (e: any) {
rej(e);
}
});
}
interface PackageJSON {
engines?: { node?: string };
dependencies: { [name: string]: any }; // eslint-disable-line @typescript-eslint/no-explicit-any
devDependencies: { [name: string]: any }; // eslint-disable-line @typescript-eslint/no-explicit-any
}
interface ModuleResolution {
declared: boolean;
installed: boolean;
version?: string;
resolution?: string;
}
interface SuccessfulModuleResolution {
declared: true;
installed: true;
version: string;
resolution: string;
}
interface ProxyTarget extends Object {
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
/*
This helper is used to create mocks for Firebase SDKs. It simplifies creation of Proxy objects
by allowing us to easily overide some or all of an objects methods. When placed back into require's
cache the proxy will be automatically used when the module is imported so we can influence the runtime
behavior of Firebase SDKs in user code.
const px = new Proxied({"value": 1});
px.when("incremented", (original) => original["value"] + 1);
const obj = px.finalize();
obj.value === 1;
obj.incremented === 2;
*/
class Proxied<T extends ProxyTarget> {
/**
* Gets a property from the original object.
*/
static getOriginal(target: any, key: string): any {
const value = target[key];
if (!Proxied.isExists(value)) {
return undefined;
} else if (Proxied.isConstructor(value) || typeof value !== "function") {
return value;
} else {
return value.bind(target);
}
}
/**
* Run the original target.
*/
static applyOriginal(target: any, thisArg: any, argArray: any[]): any {
return target.apply(thisArg, argArray);
}
private static isConstructor(obj: any): boolean {
return !!obj.prototype && !!obj.prototype.constructor.name;
}
private static isExists(obj: any): boolean {
return obj !== undefined;
}
proxy: T;
private anyValue?: (target: T, key: string) => any;
private appliedValue?: (...args: any[]) => any;
private rewrites: {
[key: string]: (target: T, key: string) => any;
} = {};
/**
* When initialized an original object is passed. This object is supplied to both .when()
* and .any() functions so the original value of the object is accessible. When no
* .any() is provided, the original value of the object is returned when the field
* key does not match any known rewrite.
*/
constructor(private original: T) {
this.proxy = new Proxy(this.original, {
get: (target, key) => {
key = key.toString();
if (this.rewrites[key]) {
return this.rewrites[key](target, key);
}
if (this.anyValue) {
return this.anyValue(target, key);
}
return Proxied.getOriginal(target, key);
},
apply: (target, thisArg, argArray) => {
if (this.appliedValue) {
return this.appliedValue.apply(thisArg);
} else {
return Proxied.applyOriginal(target, thisArg, argArray);
}
},
});
}
/**
* Calling .when("a", () => "b") will rewrite obj["a"] to be equal to "b"
*/
when(key: string, value: (target: T, key: string) => any): Proxied<T> {
this.rewrites[key] = value;
return this as Proxied<T>;
}
/**
* Calling .any(() => "b") will rewrite all fields on obj to be equal to "b"
*/
any(value: (target: T, key: string) => any): Proxied<T> {
this.anyValue = value;
return this as Proxied<T>;
}
/**
* Calling .applied(() => "b") will make obj() equal to "b"
*/
applied(value: () => any): Proxied<T> {
this.appliedValue = value;
return this as Proxied<T>;
}
/**
* Return the final proxied object.
*/
finalize(): T {
return this.proxy;
}
}
async function resolveDeveloperNodeModule(name: string): Promise<ModuleResolution> {
const pkg = requirePackageJson();
if (!pkg) {
new EmulatorLog("SYSTEM", "missing-package-json", "").log();
throw new Error("Could not find package.json");
}
const dependencies = pkg.dependencies;
const devDependencies = pkg.devDependencies;
const isInPackageJSON = dependencies[name] || devDependencies[name];
// If there's no reference to the module in their package.json, prompt them to install it
if (!isInPackageJSON) {
return { declared: false, installed: false };
}
// Once we know it's in the package.json, make sure it's actually `npm install`ed
const resolveResult = await requireResolveAsync(name, { paths: [process.cwd()] }).catch(noOp);
if (!resolveResult) {
return { declared: true, installed: false };
}
const modPackageJSON = require(path.join(findModuleRoot(name, resolveResult), "package.json"));
const moduleResolution: ModuleResolution = {
declared: true,
installed: true,
version: modPackageJSON.version,
resolution: resolveResult,
};
logDebug(`Resolved module ${name}`, moduleResolution);
return moduleResolution;
}
async function assertResolveDeveloperNodeModule(name: string): Promise<SuccessfulModuleResolution> {
const resolution = await resolveDeveloperNodeModule(name);
if (
!(resolution.installed && resolution.declared && resolution.resolution && resolution.version)
) {
throw new Error(
`Assertion failure: could not fully resolve ${name}: ${JSON.stringify(resolution)}`,
);
}
return resolution as SuccessfulModuleResolution;
}
async function verifyDeveloperNodeModules(): Promise<boolean> {
const modBundles = [
{ name: "firebase-admin", isDev: false, minVersion: "8.9.0" },
{ name: "firebase-functions", isDev: false, minVersion: "3.13.1" },
];
for (const modBundle of modBundles) {
const resolution = await resolveDeveloperNodeModule(modBundle.name);
/*
If there's no reference to the module in their package.json, prompt them to install it
*/
if (!resolution.declared) {
new EmulatorLog("SYSTEM", "missing-module", "", modBundle).log();
return false;
}
if (!resolution.installed) {
new EmulatorLog("SYSTEM", "uninstalled-module", "", modBundle).log();
return false;
}
if (compareVersionStrings(resolution.version, modBundle.minVersion) < 0) {
new EmulatorLog("SYSTEM", "out-of-date-module", "", modBundle).log();
return false;
}
}
return true;
}
/**
* Get the developer's package.json file.
*/
function requirePackageJson(): PackageJSON | undefined {
if (developerPkgJSON) {
return developerPkgJSON;
}
try {
const pkg = require(`${process.cwd()}/package.json`);
developerPkgJSON = {
engines: pkg.engines || {},
dependencies: pkg.dependencies || {},
devDependencies: pkg.devDependencies || {},
};
return developerPkgJSON;
} catch (err: any) {
return;
}
}
/**
* We mock out a ton of different paths that we can take to network I/O. It doesn't matter if they
* overlap (like TLS and HTTPS) because the dev will either allowlist, block, or allow for one
* invocation on the first prompt, so we can be aggressive here.
*
* Sadly, these vary a lot between Node versions and it will always be possible to route around
* this, it's not security - just a helper. A good example of something difficult to catch is
* any I/O done via node-gyp (https://github.com/nodejs/node-gyp) since that I/O will be done in
* C, we have to catch it before then (which is how the google-gax blocker could work).
*
* So yeah, we'll try our best and hopefully we can catch 90% of requests.
*/
function initializeNetworkFiltering(): void {
const networkingModules = [
{ name: "http", module: require("http"), path: ["request"] },
{ name: "http", module: require("http"), path: ["get"] },
{ name: "https", module: require("https"), path: ["request"] },
{ name: "https", module: require("https"), path: ["get"] },
{ name: "net", module: require("net"), path: ["connect"] },
// HTTP2 is not currently mocked due to the inability to quiet Experiment warnings in Node.
];
const history: { [href: string]: boolean } = {};
const results = networkingModules.map((bundle) => {
let obj = bundle.module;
for (const field of bundle.path.slice(0, -1)) {
obj = obj[field];
}
const method = bundle.path.slice(-1)[0];
const original = obj[method].bind(bundle.module);
/* tslint:disable:only-arrow-functions */
// This can't be an arrow function because it needs to be new'able
obj[method] = function (...args: any[]): any {
const hrefs = args
.map((arg) => {
if (typeof arg === "string") {
try {
new URL(arg);
return arg;
} catch (err: any) {
return;
}
} else if (typeof arg === "object") {
return arg.href;
} else {
return;
}
})
.filter((v) => v);
const href = (hrefs.length && hrefs[0]) || "";
if (href && !history[href] && !isLocalHost(href)) {
history[href] = true;
if (href.indexOf("googleapis.com") !== -1) {
new EmulatorLog("SYSTEM", "googleapis-network-access", "", {
href,
module: bundle.name,
}).log();
} else {
new EmulatorLog("SYSTEM", "unidentified-network-access", "", {
href,
module: bundle.name,
}).log();
}
}
try {
return original(...args);
} catch (e: any) {
const newed = new original(...args); // eslint-disable-line new-cap
return newed;
}
};
return { name: bundle.name, status: "mocked" };
});
logDebug("Outgoing network have been stubbed.", results);
}
type CallableHandler = (data: any, context: https.CallableContext) => any | Promise<any>;
type HttpsHandler = (req: Request, resp: Response) => void;
/*
This stub handles a very specific use-case, when a developer (incorrectly) provides a HTTPS handler
which returns a promise. In this scenario, we can't catch errors which get raised in user code,
because they're happening async and then the errors get lost when firebase-functions drops the return value.
Currently, Node is willing to raise the error as an ugly un-handled promise, but this is hard to
read and long-term will be silenced by Node. Instead, we stub out onRequest and put a special reference
directly to the handler so we can invoke it directly and catch the errors in our normal reporting chain.
The relevant firebase-functions code is:
https://github.com/firebase/firebase-functions/blob/9e3bda13565454543b4c7b2fd10fb627a6a3ab97/src/providers/https.ts#L66
*/
async function initializeFirebaseFunctionsStubs(): Promise<void> {
const firebaseFunctionsResolution = await assertResolveDeveloperNodeModule("firebase-functions");
const firebaseFunctionsRoot = findModuleRoot(
"firebase-functions",
firebaseFunctionsResolution.resolution,
);
const httpsProviderResolution = path.join(firebaseFunctionsRoot, "lib/providers/https");
const httpsProviderV1Resolution = path.join(firebaseFunctionsRoot, "lib/v1/providers/https");
let httpsProvider: any;
try {
httpsProvider = require(httpsProviderV1Resolution);
} catch (e: any) {
httpsProvider = require(httpsProviderResolution);
}
// TODO: Remove this logic and stop relying on internal APIs. See #1480 for reasoning.
const onRequestInnerMethodName = "_onRequestWithOptions";
const onRequestMethodOriginal = httpsProvider[onRequestInnerMethodName];
httpsProvider[onRequestInnerMethodName] = (handler: HttpsHandler, opts: DeploymentOptions) => {
const cf = onRequestMethodOriginal(handler, opts);
cf.__emulator_func = handler;
return cf;
};
// If you take a look at the link above, you'll see that onRequest relies on _onRequestWithOptions
// so in theory, we should only need to mock _onRequestWithOptions, however that is not the case
// because onRequest is defined in the same scope as _onRequestWithOptions, so replacing
// the definition of _onRequestWithOptions does not replace the link to the original function
// which onRequest uses, so we need to manually force it to use our version.
httpsProvider.onRequest = (handler: HttpsHandler) => {
return httpsProvider[onRequestInnerMethodName](handler, {});
};
// Mocking https.onCall is very similar to onRequest
const onCallInnerMethodName = "_onCallWithOptions";
const onCallMethodOriginal = httpsProvider[onCallInnerMethodName];
// Newer versions of the firebase-functions package's _onCallWithOptions method expects 3 arguments.
if (onCallMethodOriginal.length === 3) {
httpsProvider[onCallInnerMethodName] = (
opts: any,
handler: any,
deployOpts: DeploymentOptions,
) => {
const wrapped = wrapCallableHandler(handler);
const cf = onCallMethodOriginal(opts, wrapped, deployOpts);
return cf;
};
} else {
httpsProvider[onCallInnerMethodName] = (handler: any, opts: DeploymentOptions) => {
const wrapped = wrapCallableHandler(handler);
const cf = onCallMethodOriginal(wrapped, opts);
return cf;
};
}
// Newer versions of the firebase-functions package's onCall method can accept upto 2 arguments.
httpsProvider.onCall = function (optsOrHandler: any, handler: CallableHandler) {
if (onCallMethodOriginal.length === 3) {
let opts;
if (arguments.length === 1) {
opts = {};
handler = optsOrHandler as CallableHandler;
} else {
opts = optsOrHandler;
}
return httpsProvider[onCallInnerMethodName](opts, handler, {});
} else {
return httpsProvider[onCallInnerMethodName](optsOrHandler, {});
}
};
}
/**
* Wrap a callable functions handler with an outer method that extracts a special authorization
* header used to mock auth in the emulator.
*/
function wrapCallableHandler(handler: CallableHandler): CallableHandler {
const newHandler = (data: any, context: https.CallableContext) => {
if (context.rawRequest) {
const authContext = context.rawRequest.header(HttpConstants.CALLABLE_AUTH_HEADER);
if (authContext) {
logDebug("Callable functions auth override", {
key: HttpConstants.CALLABLE_AUTH_HEADER,
value: authContext,
});
context.auth = JSON.parse(decodeURIComponent(authContext));
delete context.rawRequest.headers[HttpConstants.CALLABLE_AUTH_HEADER];
} else {
logDebug("No callable functions auth found");
}
// Restore the original auth header in case the code relies on parsing it (for
// example, the code could forward it to another function or server).
const originalAuth = context.rawRequest.header(HttpConstants.ORIGINAL_AUTH_HEADER);
if (originalAuth) {
context.rawRequest.headers["authorization"] = originalAuth;
delete context.rawRequest.headers[HttpConstants.ORIGINAL_AUTH_HEADER];
}
}
return handler(data, context);
};
return newHandler;
}
function getDefaultConfig(): any {
return JSON.parse(process.env.FIREBASE_CONFIG || "{}");
}
function initializeRuntimeConfig() {
// Most recent version of Firebase Functions SDK automatically picks up locally
// stored .runtimeconfig.json to populate the config entries.
// However, due to a bug in some older version of the Function SDK, this process may fail.
//
// See the following issues for more detail:
// https://github.com/firebase/firebase-tools/issues/3793
// https://github.com/firebase/firebase-functions/issues/877
//
// As a workaround, the emulator runtime will load the contents of the .runtimeconfig.json
// to the CLOUD_RUNTIME_CONFIG environment variable IF the env var is unused.
// In the future, we will bump up the minimum version of the Firebase Functions SDK
// required to run the functions emulator to v3.15.1 and get rid of this workaround.
if (!process.env.CLOUD_RUNTIME_CONFIG) {
const configPath = `${process.cwd()}/.runtimeconfig.json`;
try {
const configContent = fs.readFileSync(configPath, "utf8");
if (configContent) {
try {
JSON.parse(configContent.toString());
logDebug(`Found local functions config: ${configPath}`);
process.env.CLOUD_RUNTIME_CONFIG = configContent.toString();
} catch (e) {
new EmulatorLog("SYSTEM", "function-runtimeconfig-json-invalid", "").log();
}
}
} catch (e) {
// Ignore, config is optional
}
}
}
/**
* This stub is the most important and one of the only non-optional stubs.This feature redirects
* writes from the admin SDK back into emulated resources.
*
* To do this, we replace initializeApp so it drops the developers config options and returns a restricted,
* unauthenticated app.
*
* We also mock out firestore.settings() so we can merge the emulator settings with the developer's.
*/
async function initializeFirebaseAdminStubs(): Promise<void> {
const adminResolution = await assertResolveDeveloperNodeModule("firebase-admin");
const localAdminModule = require(adminResolution.resolution);
const functionsResolution = await assertResolveDeveloperNodeModule("firebase-functions");
const localFunctionsModule = require(functionsResolution.resolution);
// Configuration from the environment
const defaultConfig = getDefaultConfig();
const adminModuleProxy = new Proxied<typeof admin>(localAdminModule);
const proxiedAdminModule = adminModuleProxy
.when("initializeApp", (adminModuleTarget) => (opts?: admin.AppOptions, appName?: string) => {
if (appName) {
new EmulatorLog("SYSTEM", "non-default-admin-app-used", "", { appName, opts }).log();
return adminModuleTarget.initializeApp(opts, appName);
}
// If initializeApp() is called with options we use the provided options, otherwise
// we use the default options.
const defaultAppOptions = opts ? opts : defaultConfig;
new EmulatorLog("SYSTEM", "default-admin-app-used", `config=${defaultAppOptions}`, {
opts: defaultAppOptions,
}).log();
const defaultApp: admin.app.App = makeProxiedFirebaseApp(
adminModuleTarget.initializeApp(defaultAppOptions),
);
logDebug("initializeApp(DEFAULT)", defaultAppOptions);
// Tell the Firebase Functions SDK to use the proxied app so that things like "change.after.ref"
// point to the right place.
localFunctionsModule.app.setEmulatedAdminApp(defaultApp);
// When the auth emulator is running, try to disable JWT verification.
if (process.env[Constants.FIREBASE_AUTH_EMULATOR_HOST]) {
if (compareVersionStrings(adminResolution.version, "9.3.0") < 0) {
new EmulatorLog(
"WARN_ONCE",
"runtime-status",
"The Firebase Authentication emulator is running, but your 'firebase-admin' dependency is below version 9.3.0, so calls to Firebase Authentication will affect production.",
).log();
} else if (compareVersionStrings(adminResolution.version, "9.4.2") <= 0) {
// Between firebase-admin versions 9.3.0 and 9.4.2 (inclusive) we used the
// "auth.setJwtVerificationEnabled" hack to disable JWT verification while emulating.
// See: https://github.com/firebase/firebase-admin-node/pull/1148
const auth = defaultApp.auth();
if (typeof (auth as any).setJwtVerificationEnabled === "function") {
logDebug("auth.setJwtVerificationEnabled(false)", {});
(auth as any).setJwtVerificationEnabled(false);
} else {
logDebug("auth.setJwtVerificationEnabled not available", {});
}
}
}
return defaultApp;
})
.when("firestore", (target) => {
warnAboutFirestoreProd();
return Proxied.getOriginal(target, "firestore");
})
.when("database", (target) => {
warnAboutDatabaseProd();
return Proxied.getOriginal(target, "database");
})
.when("auth", (target) => {
warnAboutAuthProd();
return Proxied.getOriginal(target, "auth");
})
.when("storage", (target) => {
warnAboutStorageProd();
return Proxied.getOriginal(target, "storage");
})
.finalize();
// Stub the admin module in the require cache
const v = require.cache[adminResolution.resolution];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- this is not precedent.
require.cache[adminResolution.resolution] = Object.assign(v!, {
exports: proxiedAdminModule,
path: path.dirname(adminResolution.resolution),
});
logDebug("firebase-admin has been stubbed.", {
adminResolution,
});
}
function makeProxiedFirebaseApp(original: admin.app.App): admin.app.App {
const appProxy = new Proxied<admin.app.App>(original);
return appProxy
.when("firestore", (target: any) => {
warnAboutFirestoreProd();
return Proxied.getOriginal(target, "firestore");
})
.when("database", (target: any) => {
warnAboutDatabaseProd();
return Proxied.getOriginal(target, "database");
})
.when("auth", (target: any) => {
warnAboutAuthProd();
return Proxied.getOriginal(target, "auth");
})
.when("storage", (target: any) => {
warnAboutStorageProd();
return Proxied.getOriginal(target, "storage");
})
.finalize();
}
function warnAboutFirestoreProd(): void {
if (process.env[Constants.FIRESTORE_EMULATOR_HOST]) {
return;
}
new EmulatorLog(
"WARN_ONCE",
"runtime-status",
"The Cloud Firestore emulator is not running, so calls to Firestore will affect production.",
).log();
}
function warnAboutDatabaseProd(): void {
if (process.env[Constants.FIREBASE_DATABASE_EMULATOR_HOST]) {
return;
}
new EmulatorLog(
"WARN_ONCE",
"runtime-status",
"The Realtime Database emulator is not running, so calls to Realtime Database will affect production.",
).log();
}
function warnAboutAuthProd(): void {
if (process.env[Constants.FIREBASE_AUTH_EMULATOR_HOST]) {
return;
}
new EmulatorLog(
"WARN_ONCE",
"runtime-status",
"The Firebase Authentication emulator is not running, so calls to Firebase Authentication will affect production.",
).log();
}
function warnAboutStorageProd(): void {
if (process.env[Constants.FIREBASE_STORAGE_EMULATOR_HOST]) {
return;
}
new EmulatorLog(
"WARN_ONCE",
"runtime-status",
"The Firebase Storage emulator is not running, so calls to Firebase Storage will affect production.",
).log();
}
async function initializeFunctionsConfigHelper(): Promise<void> {
const functionsResolution = await assertResolveDeveloperNodeModule("firebase-functions");
const localFunctionsModule = require(functionsResolution.resolution);
logDebug("Checked functions.config()", {
config: localFunctionsModule.config(),
});
const originalConfig = localFunctionsModule.config();
const proxiedConfig = new Proxied(originalConfig)
.any((parentConfig, parentKey) => {
const isInternal = parentKey.startsWith("Symbol(") || parentKey.startsWith("inspect");
if (!parentConfig[parentKey] && !isInternal) {
new EmulatorLog("SYSTEM", "functions-config-missing-value", "", {
key: parentKey,
}).log();
}
return parentConfig[parentKey];
})
.finalize();
const functionsModuleProxy = new Proxied<typeof localFunctionsModule>(localFunctionsModule);
const proxiedFunctionsModule = functionsModuleProxy
.when("config", () => () => {
return proxiedConfig;
})
.finalize();
// Stub the functions module in the require cache
const v = require.cache[functionsResolution.resolution];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- this is not precedent.
require.cache[functionsResolution.resolution] = Object.assign(v!, {
exports: proxiedFunctionsModule,
path: path.dirname(functionsResolution.resolution),
});
logDebug("firebase-functions has been stubbed.", {
functionsResolution,
});
}
/*
Retains a reference to the raw body buffer to allow access to the raw body for things like request
signature validation. This is used as the "verify" function in body-parser options.
*/
function rawBodySaver(req: express.Request, res: express.Response, buf: Buffer): void {
(req as any).rawBody = buf;
}
async function processBackground(
trigger: CloudFunction<any>,
reqBody: any,
signature: SignatureType,
): Promise<void> {
if (signature === "cloudevent") {
return runCloudEvent(trigger, reqBody);
}
// All formats of the payload should carry a "data" property. The "context" property does
// not exist in all versions. Where it doesn't exist, context is everything besides data.
const data = reqBody.data;
delete reqBody.data;
const context = reqBody.context ? reqBody.context : reqBody;
// This is due to the fact that the Firestore emulator sends payloads in a newer
// format than production firestore.
if (!reqBody.eventType || !reqBody.eventType.startsWith("google.storage")) {
if (context.resource && context.resource.name) {
logDebug("ProcessBackground: lifting resource.name from resource", context.resource);
context.resource = context.resource.name;
}
}
await runBackground(trigger, { data, context });
}
/**
* Run the given function while redirecting logs and looking out for errors.
*/
async function runFunction(func: () => Promise<any>): Promise<any> {
let caughtErr;
try {
await func();
} catch (err: any) {
caughtErr = err;
}
if (caughtErr) {
throw caughtErr;
}
}
async function runBackground(trigger: CloudFunction<any>, reqBody: any): Promise<any> {
logDebug("RunBackground", reqBody);
await runFunction(() => {
return trigger(reqBody.data, reqBody.context);
});
}
async function runCloudEvent(trigger: CloudFunction<any>, event: unknown): Promise<any> {
logDebug("RunCloudEvent", event);
await runFunction(() => {
return trigger(event);
});
}
async function runHTTPS(trigger: CloudFunction<any>, args: any[]): Promise<any> {
if (args.length < 2) {
throw new Error("Function must be passed 2 args.");
}
await runFunction(() => {
return trigger(args[0], args[1]);
});
}
/*
This method attempts to help a developer whose code can't be loaded by suggesting
possible fixes based on the files in their functions directory.
*/
async function moduleResolutionDetective(error: Error): Promise<void> {
/*
These files could all potentially exist, if they don't then the value in the map will be
falsey, so we just catch to keep from throwing.
*/
const clues = {
tsconfigJSON: await requireAsync("./tsconfig.json", { paths: [process.cwd()] }).catch(noOp),
packageJSON: await requireAsync("./package.json", { paths: [process.cwd()] }).catch(noOp),
};
const isPotentially = {
typescript: false,
uncompiled: false,
wrong_directory: false,
};
isPotentially.typescript = !!clues.tsconfigJSON;
isPotentially.wrong_directory = !clues.packageJSON;
isPotentially.uncompiled = !!_.get(clues.packageJSON, "scripts.build", false);
new EmulatorLog("SYSTEM", "function-code-resolution-failed", "", {
isPotentially,
error: error.stack,
}).log();
}
function logDebug(msg: string, data?: any): void {
new EmulatorLog("DEBUG", "runtime-status", `[${process.pid}] ${msg}`, data).log();
}
async function initializeRuntime(): Promise<void> {
FUNCTION_DEBUG_MODE = process.env.FUNCTION_DEBUG_MODE || "";
if (!FUNCTION_DEBUG_MODE) {
FUNCTION_TARGET_NAME = process.env.FUNCTION_TARGET || "";
if (!FUNCTION_TARGET_NAME) {
new EmulatorLog(
"FATAL",
"runtime-status",
`Environment variable FUNCTION_TARGET cannot be empty. This shouldn't happen.`,
).log();
await flushAndExit(1);
}
FUNCTION_SIGNATURE = process.env.FUNCTION_SIGNATURE_TYPE || "";
if (!FUNCTION_SIGNATURE) {
new EmulatorLog(
"FATAL",
"runtime-status",
`Environment variable FUNCTION_SIGNATURE_TYPE cannot be empty. This shouldn't happen.`,
).log();
await flushAndExit(1);
}
}
const verified = await verifyDeveloperNodeModules();
if (!verified) {
// If we can't verify the node modules, then just leave, something bad will happen during runtime.
new EmulatorLog(
"INFO",
"runtime-status",
`Your functions could not be parsed due to an issue with your node_modules (see above)`,
).log();
return;
}
initializeRuntimeConfig();
initializeNetworkFiltering();
await initializeFunctionsConfigHelper();
await initializeFirebaseFunctionsStubs();
await initializeFirebaseAdminStubs();
}
async function loadTriggers(): Promise<any> {
let triggerModule;
try {
triggerModule = require(process.cwd());
} catch (err: any) {
if (err.code !== "ERR_REQUIRE_ESM") {
// Try to run diagnostics to see what could've gone wrong before rethrowing the error.
await moduleResolutionDetective(err);
throw err;
}
const modulePath = require.resolve(process.cwd());
// Resolve module path to file:// URL. Required for windows support.
const moduleURL = pathToFileURL(modulePath).href;
triggerModule = await dynamicImport(moduleURL);
}
return triggerModule;
}
async function flushAndExit(code: number) {
await EmulatorLog.waitForFlush();
process.exit(code);
}
async function handleMessage(message: string) {
let debug: FunctionsRuntimeBundle["debug"];
try {
debug = JSON.parse(message) as FunctionsRuntimeBundle["debug"];
} catch (e: any) {
new EmulatorLog("FATAL", "runtime-error", `Got unexpected message body: ${message}`).log();
await flushAndExit(1);
return;
}
if (FUNCTION_DEBUG_MODE) {
if (debug) {
FUNCTION_TARGET_NAME = debug.functionTarget;
FUNCTION_SIGNATURE = debug.functionSignature;
} else {
new EmulatorLog("WARN", "runtime-warning", "Expected debug payload while in debug mode.");
}
}
}
async function main(): Promise<void> {
// Since the functions run as attached processes they naturally inherit SIGINT
// sent to the functions emulator. We want them to ignore the first signal
// to allow for a clean shutdown.
let lastSignal = new Date().getTime();
let signalCount = 0;
process.on("SIGINT", () => {
const now = new Date().getTime();
if (now - lastSignal < 100) {
return;
}
signalCount = signalCount + 1;
lastSignal = now;
if (signalCount >= 2) {
process.exit(1);
}
});
await initializeRuntime();
try {
functionModule = await loadTriggers();
} catch (e: any) {
new EmulatorLog(
"FATAL",
"runtime-status",
`Failed to initialize and load triggers. This shouldn't happen: ${e.message}`,
).log();
await flushAndExit(1);
}
const app = express();
app.enable("trust proxy");
// TODO: This should be 10mb for v1 functions, 32mb for v2, but there is not an easy way to check platform from here.
const bodyParserLimit = "32mb";
app.use(
bodyParser.json({
limit: bodyParserLimit,
verify: rawBodySaver,
}),
);
app.use(
bodyParser.text({
limit: bodyParserLimit,
verify: rawBodySaver,
}),
);
app.use(
bodyParser.urlencoded({
extended: true,
limit: bodyParserLimit,