Skip to content

Commit d525b69

Browse files
authored
Remove gRPC and protos from @firebase/testing (#3236)
1 parent 246ed9d commit d525b69

File tree

10 files changed

+53
-253
lines changed

10 files changed

+53
-253
lines changed

packages/testing/.firebaserc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

packages/testing/README.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
# @firebase/testing
22

3-
This is the testing component for the Firebase JS SDK.
3+
A set of utilities useful for testing Security Rules with the Realtime Database or Cloud Firestore
4+
emulators.
45

6+
See:
57

6-
## Protocol buffers
7-
8-
There are some protocol buffers (the `.proto` files in `src/protos/`) that
9-
are included in this package but should not be modified. They're copied from
10-
Google production and emulator APIs, and are immutable specifically to maintain
11-
compatibility with those services.
12-
13-
When those services update their APIs, the new versions should be mirrored into
14-
this package.
8+
* [Test your Cloud Firestore Security Rules](https://firebase.google.com/docs/firestore/security/test-rules-emulator)
9+
* [Testing Security Rules with the Realtime Database Emulator](https://firebase.google.com/docs/database/security/test-rules-emulator)

packages/testing/firebase.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"emulators": {
3+
"firestore": {
4+
"port": 9001
5+
},
6+
"database": {
7+
"port": 9000
8+
},
9+
"ui": {
10+
"enabled": false
11+
}
12+
}
13+
}

packages/testing/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"build": "rollup -c",
1515
"build:deps": "lerna run --scope @firebase/testing --include-dependencies build",
1616
"dev": "rollup -c -w",
17-
"test": "TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --config ../../config/mocharc.node.js",
17+
"test:nyc": "TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --config ../../config/mocharc.node.js",
18+
"test": "firebase emulators:exec 'yarn test:nyc'",
1819
"test:ci": "node ../../scripts/run_tests_in_ci.js",
1920
"prepare": "yarn build"
2021
},
@@ -23,13 +24,12 @@
2324
"firebase": "7.15.2",
2425
"@firebase/logger": "0.2.5",
2526
"@firebase/util": "0.2.49",
26-
"@types/request": "2.48.4",
2727
"request": "2.88.2"
2828
},
2929
"devDependencies": {
30+
"@types/request": "2.48.4",
31+
"firebase-tools": "^8.4.3",
3032
"rollup": "2.7.6",
31-
"rollup-plugin-copy-assets": "1.1.0",
32-
"rollup-plugin-replace": "2.2.0",
3333
"rollup-plugin-typescript2": "0.27.0"
3434
},
3535
"repository": {

packages/testing/rollup.config.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import typescriptPlugin from 'rollup-plugin-typescript2';
1919
import pkg from './package.json';
20-
import replace from 'rollup-plugin-replace';
21-
import copy from 'rollup-plugin-copy-assets';
2220
import typescript from 'typescript';
2321

2422
const plugins = [
@@ -34,13 +32,6 @@ const deps = Object.keys(
3432
export default {
3533
input: 'index.ts',
3634
output: [{ file: pkg.main, format: 'cjs', sourcemap: true }],
37-
plugins: [
38-
...plugins,
39-
// Needed as we also use the *.proto files
40-
copy({ assets: ['./src/protos'] }),
41-
replace({
42-
'process.env.FIRESTORE_EMULATOR_PROTO_ROOT': JSON.stringify('src/protos')
43-
})
44-
],
35+
plugins: [...plugins],
4536
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
4637
};

packages/testing/src/api/index.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,10 @@ import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
2121
import * as request from 'request';
2222
import { base64 } from '@firebase/util';
2323
import { setLogLevel, LogLevel } from '@firebase/logger';
24-
import * as grpc from '@grpc/grpc-js';
25-
import * as protoLoader from '@grpc/proto-loader';
26-
import { resolve } from 'path';
2724
import { Component, ComponentType } from '@firebase/component';
2825

2926
export { database, firestore } from 'firebase';
3027

31-
const PROTO_ROOT = resolve(
32-
__dirname,
33-
process.env.FIRESTORE_EMULATOR_PROTO_ROOT || '../protos'
34-
);
35-
const PROTO_FILE = resolve(
36-
PROTO_ROOT,
37-
'google/firestore/emulator/v1/firestore_emulator.proto'
38-
);
39-
const PKG_DEF = protoLoader.loadSync(PROTO_FILE, { includeDirs: [PROTO_ROOT] });
40-
const PROTOS = grpc.loadPackageDefinition(PKG_DEF);
41-
const EMULATOR = (PROTOS['google'] as any)['firestore']['emulator']['v1'];
42-
4328
/** If this environment variable is set, use it for the database emulator's address. */
4429
const DATABASE_ADDRESS_ENV: string = 'FIREBASE_DATABASE_EMULATOR_ADDRESS';
4530
/** The default address for the local database emulator. */
@@ -218,22 +203,24 @@ export function loadFirestoreRules(
218203
throw new Error('must provide rules to loadFirestoreRules');
219204
}
220205

221-
let client = new EMULATOR.FirestoreEmulator(
222-
FIRESTORE_ADDRESS,
223-
grpc.credentials.createInsecure()
224-
);
225206
return new Promise((resolve, reject) => {
226-
client.setSecurityRules(
207+
request.put(
227208
{
228-
project: `projects/${options.projectId}`,
229-
rules: { files: [{ content: options.rules }] }
209+
uri: `http://${FIRESTORE_ADDRESS}/emulator/v1/projects/${options.projectId}:securityRules`,
210+
body: JSON.stringify({
211+
rules: {
212+
files: [{ content: options.rules }]
213+
}
214+
})
230215
},
231-
// @ts-ignore Defined in protobuf.
232-
(err: Error, resp) => {
216+
(err, resp, body) => {
233217
if (err) {
234218
reject(err);
219+
} else if (resp.statusCode !== 200) {
220+
console.log('body', body);
221+
reject(JSON.parse(body).error);
235222
} else {
236-
resolve(resp);
223+
resolve();
237224
}
238225
}
239226
);
@@ -250,26 +237,22 @@ export function clearFirestoreData(
250237
throw new Error('projectId not specified');
251238
}
252239

253-
let client = new EMULATOR.FirestoreEmulator(
254-
FIRESTORE_ADDRESS,
255-
grpc.credentials.createInsecure(),
256-
{
257-
// As with 'loadFirestoreRules', cap how much backoff gRPC will perform.
258-
'grpc.initial_reconnect_backoff_ms': 100,
259-
'grpc.max_reconnect_backoff_ms': 100
260-
}
261-
);
262240
return new Promise((resolve, reject) => {
263-
client.clearData(
241+
request.delete(
264242
{
265-
database: `projects/${options.projectId}/databases/(default)`
243+
uri: `http://${FIRESTORE_ADDRESS}/emulator/v1/projects/${options.projectId}/databases/(default)/documents`,
244+
body: JSON.stringify({
245+
database: `projects/${options.projectId}/databases/(default)`
246+
})
266247
},
267-
// @ts-ignore Defined in protobuf.
268-
(err: Error, resp) => {
248+
(err, resp, body) => {
269249
if (err) {
270250
reject(err);
251+
} else if (resp.statusCode !== 200) {
252+
console.log('body', body);
253+
reject(JSON.parse(body).error);
271254
} else {
272-
resolve(resp);
255+
resolve();
273256
}
274257
}
275258
);

packages/testing/src/protos/google/firebase/rules/v1/source.proto

Lines changed: 0 additions & 74 deletions
This file was deleted.

packages/testing/src/protos/google/firestore/emulator/v1/firestore_emulator.proto

Lines changed: 0 additions & 56 deletions
This file was deleted.

packages/testing/src/protos/google/protobuf/empty.proto

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)