Skip to content

Commit a8fc3a2

Browse files
committed
fix(*): import and formatting updates
1 parent d180941 commit a8fc3a2

File tree

7 files changed

+81
-91
lines changed

7 files changed

+81
-91
lines changed

mocha/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as chai from "chai";
2-
import * as nock from "nock";
3-
import chaiAsPromised = require("chai-as-promised");
2+
import chaiAsPromised from "chai-as-promised";
3+
import nock from "nock";
44

55
chai.use(chaiAsPromised);
66

spec/common/config.spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import { expect } from "chai";
2424
import * as fs from "fs";
2525
import * as process from "process";
26-
import Sinon = require("sinon");
26+
import Sinon from "sinon";
2727

2828
import { firebaseConfig, resetCache } from "../../src/common/config";
2929

@@ -57,16 +57,21 @@ describe("firebaseConfig()", () => {
5757
});
5858

5959
it("loads Firebase configs from FIREBASE_CONFIG env variable pointing to a file", () => {
60-
const oldEnv = process.env;
61-
(process as any).env = {
62-
...oldEnv,
60+
const originalEnv = process.env;
61+
const mockEnv = {
62+
...originalEnv,
6363
FIREBASE_CONFIG: ".firebaseconfig.json",
6464
};
65+
66+
// Use Object.assign to modify the existing env object
67+
Object.assign(process.env, mockEnv);
68+
6569
try {
6670
readFileSync.returns(Buffer.from('{"databaseURL": "foo@firebaseio.com"}'));
6771
expect(firebaseConfig()).to.have.property("databaseURL", "foo@firebaseio.com");
6872
} finally {
69-
(process as any).env = oldEnv;
73+
// Restore original environment
74+
Object.assign(process.env, originalEnv);
7075
}
7176
});
7277
});

spec/fixtures/http.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,35 @@ interface AccessToken {
2828
}
2929

3030
export function mockCredentialFetch(tokenToReturn: AccessToken): nock.Scope {
31-
return nock('http://metadata.google.internal')
32-
.get('/computeMetadata/v1beta1/instance/service-accounts/default/token')
31+
return nock("http://metadata.google.internal")
32+
.get("/computeMetadata/v1beta1/instance/service-accounts/default/token")
3333
.reply(200, tokenToReturn);
3434
}
3535

3636
export function mockRCVariableFetch(
3737
projectId: string,
3838
varName: string,
3939
data: any,
40-
token: string = 'thetoken'
40+
token: string = "thetoken"
4141
): nock.Scope {
42-
return nock('https://runtimeconfig.googleapis.com')
42+
return nock("https://runtimeconfig.googleapis.com")
4343
.get(`/v1beta1/projects/${projectId}/configs/firebase/variables/${varName}`)
44-
.matchHeader('Authorization', `Bearer ${token}`)
44+
.matchHeader("Authorization", `Bearer ${token}`)
4545
.reply(200, { text: JSON.stringify(data) });
4646
}
4747

4848
export function mockMetaVariableWatch(
4949
projectId: string,
5050
data: any,
51-
token: string = 'thetoken',
51+
token: string = "thetoken",
5252
updateTime: string = new Date().toISOString()
5353
): nock.Scope {
54-
return nock('https://runtimeconfig.googleapis.com')
55-
.post(
56-
`/v1beta1/projects/${projectId}/configs/firebase/variables/meta:watch`
57-
)
58-
.matchHeader('Authorization', `Bearer ${token}`)
54+
return nock("https://runtimeconfig.googleapis.com")
55+
.post(`/v1beta1/projects/${projectId}/configs/firebase/variables/meta:watch`)
56+
.matchHeader("Authorization", `Bearer ${token}`)
5957
.reply(200, {
6058
updateTime,
61-
state: 'UPDATED',
59+
state: "UPDATED",
6260
text: JSON.stringify(data),
6361
});
6462
}
@@ -68,37 +66,33 @@ export function mockMetaVariableWatchTimeout(
6866
delay: number,
6967
token?: string
7068
): nock.Scope {
71-
let interceptor = nock('https://runtimeconfig.googleapis.com').post(
69+
let interceptor = nock("https://runtimeconfig.googleapis.com").post(
7270
`/v1beta1/projects/${projectId}/configs/firebase/variables/meta:watch`
7371
);
7472

7573
if (interceptor) {
76-
interceptor = interceptor.matchHeader('Authorization', `Bearer ${token}`);
74+
interceptor = interceptor.matchHeader("Authorization", `Bearer ${token}`);
7775
}
7876

7977
return interceptor.delay(delay).reply(502);
8078
}
8179

8280
export function mockCreateToken(
83-
token: AccessToken = { access_token: 'aToken', expires_in: 3600 }
81+
token: AccessToken = { access_token: "aToken", expires_in: 3600 }
8482
): nock.Scope {
85-
return nock('https://accounts.google.com')
86-
.post('/o/oauth2/token')
87-
.reply(200, token);
83+
return nock("https://accounts.google.com").post("/o/oauth2/token").reply(200, token);
8884
}
8985

9086
export function mockRefreshToken(
91-
token: AccessToken = { access_token: 'aToken', expires_in: 3600 }
87+
token: AccessToken = { access_token: "aToken", expires_in: 3600 }
9288
): nock.Scope {
93-
return nock('https://www.googleapis.com')
94-
.post('/oauth2/v4/token')
95-
.reply(200, token);
89+
return nock("https://www.googleapis.com").post("/oauth2/v4/token").reply(200, token);
9690
}
9791

9892
export function mockMetadataServiceToken(
99-
token: AccessToken = { access_token: 'aToken', expires_in: 3600 }
93+
token: AccessToken = { access_token: "aToken", expires_in: 3600 }
10094
): nock.Scope {
101-
return nock('http://metadata.google.internal')
102-
.get('/computeMetadata/v1beta1/instance/service-accounts/default/token')
95+
return nock("http://metadata.google.internal")
96+
.get("/computeMetadata/v1beta1/instance/service-accounts/default/token")
10397
.reply(200, token);
10498
}

spec/fixtures/mockrequest.ts

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import { EventEmitter } from 'node:stream';
1+
import { EventEmitter } from "node:stream";
22

3-
import * as jwt from 'jsonwebtoken';
4-
import * as jwkToPem from 'jwk-to-pem';
5-
import * as nock from 'nock';
6-
import * as mockJWK from '../fixtures/credential/jwk.json';
7-
import * as mockKey from '../fixtures/credential/key.json';
3+
import * as jwt from "jsonwebtoken";
4+
import jwkToPem from "jwk-to-pem";
5+
import nock from "nock";
6+
import * as mockJWK from "../fixtures/credential/jwk.json";
7+
import * as mockKey from "../fixtures/credential/key.json";
88

99
// MockRequest mocks an https.Request.
1010
export class MockRequest extends EventEmitter {
11-
public method: 'POST' | 'GET' | 'OPTIONS' = 'POST';
11+
public method: "POST" | "GET" | "OPTIONS" = "POST";
1212

13-
constructor(
14-
readonly body: any,
15-
readonly headers: { [name: string]: string }
16-
) {
17-
super()
13+
constructor(readonly body: any, readonly headers: { [name: string]: string }) {
14+
super();
1815
}
1916

2017
public header(name: string): string {
@@ -25,34 +22,34 @@ export class MockRequest extends EventEmitter {
2522
// Creates a mock request with the given data and content-type.
2623
export function mockRequest(
2724
data: any,
28-
contentType: string = 'application/json',
25+
contentType: string = "application/json",
2926
context: {
3027
authorization?: string;
3128
instanceIdToken?: string;
3229
appCheckToken?: string;
3330
} = {},
34-
reqHeaders?: Record<string, string>,
31+
reqHeaders?: Record<string, string>
3532
) {
3633
const body: any = {};
37-
if (typeof data !== 'undefined') {
34+
if (typeof data !== "undefined") {
3835
body.data = data;
3936
}
4037

4138
const headers = {
42-
'content-type': contentType,
39+
"content-type": contentType,
4340
authorization: context.authorization,
44-
'firebase-instance-id-token': context.instanceIdToken,
45-
'x-firebase-appcheck': context.appCheckToken,
46-
origin: 'example.com',
41+
"firebase-instance-id-token": context.instanceIdToken,
42+
"x-firebase-appcheck": context.appCheckToken,
43+
origin: "example.com",
4744
...reqHeaders,
4845
};
4946

5047
return new MockRequest(body, headers);
5148
}
5249

5350
export const expectedResponseHeaders = {
54-
'Access-Control-Allow-Origin': 'example.com',
55-
Vary: 'Origin',
51+
"Access-Control-Allow-Origin": "example.com",
52+
Vary: "Origin",
5653
};
5754

5855
/**
@@ -62,11 +59,11 @@ export const expectedResponseHeaders = {
6259
export function mockFetchPublicKeys(): nock.Scope {
6360
const mockedResponse = { [mockKey.key_id]: mockKey.public_key };
6461
const headers = {
65-
'cache-control': 'public, max-age=1, must-revalidate, no-transform',
62+
"cache-control": "public, max-age=1, must-revalidate, no-transform",
6663
};
6764

68-
return nock('https://www.googleapis.com:443')
69-
.get('/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com')
65+
return nock("https://www.googleapis.com:443")
66+
.get("/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com")
7067
.reply(200, mockedResponse, headers);
7168
}
7269

@@ -78,12 +75,12 @@ export function generateIdToken(projectId: string): string {
7875
const options: jwt.SignOptions = {
7976
audience: projectId,
8077
expiresIn: 60 * 60, // 1 hour in seconds
81-
issuer: 'https://securetoken.google.com/' + projectId,
78+
issuer: "https://securetoken.google.com/" + projectId,
8279
subject: mockKey.user_id,
83-
algorithm: 'RS256',
80+
algorithm: "RS256",
8481
header: {
8582
kid: mockKey.key_id,
86-
alg: 'RS256',
83+
alg: "RS256",
8784
},
8885
};
8986
return jwt.sign(claims, mockKey.private_key, options);
@@ -94,13 +91,13 @@ export function generateIdToken(projectId: string): string {
9491
*/
9592
export function generateUnsignedIdToken(projectId: string): string {
9693
return [
97-
{ alg: 'RS256', typ: 'JWT' },
94+
{ alg: "RS256", typ: "JWT" },
9895
{ aud: projectId, sub: mockKey.user_id },
99-
'Invalid signature',
96+
"Invalid signature",
10097
]
10198
.map((str) => JSON.stringify(str))
102-
.map((str) => Buffer.from(str).toString('base64'))
103-
.join('.');
99+
.map((str) => Buffer.from(str).toString("base64"))
100+
.join(".");
104101
}
105102

106103
/**
@@ -113,27 +110,24 @@ export function mockFetchAppCheckPublicJwks(): nock.Scope {
113110
keys: [{ kty, use, alg, kid, n, e }],
114111
};
115112

116-
return nock('https://firebaseappcheck.googleapis.com:443')
117-
.get('/v1/jwks')
113+
return nock("https://firebaseappcheck.googleapis.com:443")
114+
.get("/v1/jwks")
118115
.reply(200, mockedResponse);
119116
}
120117

121118
/**
122119
* Generates a mocked AppCheck token.
123120
*/
124-
export function generateAppCheckToken(
125-
projectId: string,
126-
appId: string
127-
): string {
121+
export function generateAppCheckToken(projectId: string, appId: string): string {
128122
const claims = {};
129123
const options: jwt.SignOptions = {
130124
audience: [`projects/${projectId}`],
131125
expiresIn: 60 * 60, // 1 hour in seconds
132126
issuer: `https://firebaseappcheck.googleapis.com/${projectId}`,
133127
subject: appId,
134128
header: {
135-
alg: 'RS256',
136-
typ: 'JWT',
129+
alg: "RS256",
130+
typ: "JWT",
137131
kid: mockJWK.kid,
138132
},
139133
};
@@ -143,16 +137,13 @@ export function generateAppCheckToken(
143137
/**
144138
* Generates a mocked, unsigned AppCheck token.
145139
*/
146-
export function generateUnsignedAppCheckToken(
147-
projectId: string,
148-
appId: string
149-
): string {
140+
export function generateUnsignedAppCheckToken(projectId: string, appId: string): string {
150141
return [
151-
{ alg: 'RS256', typ: 'JWT' },
142+
{ alg: "RS256", typ: "JWT" },
152143
{ aud: [`projects/${projectId}`], sub: appId },
153-
'Invalid signature',
144+
"Invalid signature",
154145
]
155146
.map((component) => JSON.stringify(component))
156-
.map((str) => Buffer.from(str).toString('base64'))
157-
.join('.');
147+
.map((str) => Buffer.from(str).toString("base64"))
148+
.join(".");
158149
}

spec/v1/config.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import { expect } from "chai";
2424
import * as fs from "fs";
2525
import * as process from "process";
26-
import Sinon = require("sinon");
26+
import Sinon from "sinon";
2727

2828
import { config, resetCache } from "../../src/v1/config";
2929

src/common/providers/https.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import * as cors from "cors";
23+
import cors, { CorsOptions } from "cors";
2424
import * as express from "express";
2525
import { DecodedAppCheckToken } from "firebase-admin/app-check";
2626

@@ -707,7 +707,7 @@ type v2CallableHandler<Req, Res, Stream> = (
707707

708708
/** @internal **/
709709
export interface CallableOptions<T = any> {
710-
cors: cors.CorsOptions;
710+
cors: CorsOptions;
711711
enforceAppCheck?: boolean;
712712
consumeAppCheckToken?: boolean;
713713
/* @deprecated */

src/v2/providers/https.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,30 @@
2525
* @packageDocumentation
2626
*/
2727

28-
import * as cors from "cors";
28+
import cors from "cors";
2929
import * as express from "express";
30-
import { convertIfPresent, convertInvoker, copyIfPresent } from "../../common/encoding";
31-
import { wrapTraceContext } from "../trace";
3230
import { isDebugFeatureEnabled } from "../../common/debug";
31+
import { convertIfPresent, convertInvoker, copyIfPresent } from "../../common/encoding";
32+
import { withInit } from "../../common/onInit";
3333
import { ResetValue } from "../../common/options";
3434
import {
35+
AuthData,
3536
CallableRequest,
3637
CallableResponse,
3738
FunctionsErrorCode,
3839
HttpsError,
3940
onCallHandler,
4041
Request,
41-
AuthData,
4242
} from "../../common/providers/https";
43-
import { initV2Endpoint, ManifestEndpoint } from "../../runtime/manifest";
44-
import { GlobalOptions, SupportedRegion } from "../options";
43+
import * as logger from "../../logger";
4544
import { Expression } from "../../params";
4645
import { SecretParam } from "../../params/types";
46+
import { initV2Endpoint, ManifestEndpoint } from "../../runtime/manifest";
4747
import * as options from "../options";
48-
import { withInit } from "../../common/onInit";
49-
import * as logger from "../../logger";
48+
import { GlobalOptions, SupportedRegion } from "../options";
49+
import { wrapTraceContext } from "../trace";
5050

51-
export { Request, CallableRequest, CallableResponse, FunctionsErrorCode, HttpsError };
51+
export { CallableRequest, CallableResponse, FunctionsErrorCode, HttpsError, Request };
5252

5353
/**
5454
* Options that can be set on an onRequest HTTPS function.

0 commit comments

Comments
 (0)