firebase-functions 3.24.1 → 4.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -9
- package/lib/bin/firebase-functions.js +13 -15
- package/lib/common/app.d.ts +14 -0
- package/lib/common/app.js +62 -0
- package/lib/common/change.d.ts +0 -5
- package/lib/common/change.js +5 -7
- package/lib/common/config.d.ts +6 -0
- package/lib/common/config.js +49 -0
- package/lib/common/debug.js +2 -2
- package/lib/common/encoding.d.ts +0 -8
- package/lib/common/encoding.js +6 -29
- package/lib/common/options.d.ts +14 -0
- package/lib/common/options.js +44 -0
- package/lib/common/params.d.ts +32 -0
- package/lib/common/params.js +23 -0
- package/lib/common/providers/database.d.ts +17 -19
- package/lib/common/providers/database.js +68 -39
- package/lib/common/providers/https.d.ts +11 -52
- package/lib/common/providers/https.js +115 -110
- package/lib/common/providers/identity.d.ts +8 -8
- package/lib/common/providers/identity.js +71 -79
- package/lib/common/providers/tasks.d.ts +11 -10
- package/lib/common/providers/tasks.js +10 -9
- package/lib/common/timezone.js +538 -538
- package/lib/common/trace.d.ts +14 -0
- package/lib/common/trace.js +69 -0
- package/lib/{encoder.d.ts → common/utilities/encoder.d.ts} +0 -0
- package/lib/{encoder.js → common/utilities/encoder.js} +1 -1
- package/lib/{utilities → common/utilities}/path-pattern.d.ts +0 -0
- package/lib/{utilities → common/utilities}/path-pattern.js +17 -16
- package/lib/{utilities → common/utilities}/path.d.ts +0 -0
- package/lib/{utilities → common/utilities}/path.js +5 -7
- package/lib/{utils.d.ts → common/utilities/utils.d.ts} +1 -1
- package/lib/{utils.js → common/utilities/utils.js} +18 -12
- package/lib/logger/common.d.ts +1 -3
- package/lib/logger/common.js +9 -14
- package/lib/logger/compat.js +9 -9
- package/lib/logger/index.d.ts +1 -1
- package/lib/logger/index.js +24 -30
- package/lib/{v2/params → params}/index.d.ts +22 -9
- package/lib/{v2/params → params}/index.js +22 -14
- package/lib/params/types.d.ts +180 -0
- package/lib/params/types.js +289 -0
- package/lib/runtime/loader.js +12 -14
- package/lib/runtime/manifest.d.ts +56 -24
- package/lib/runtime/manifest.js +114 -4
- package/lib/v1/cloud-functions.d.ts +232 -0
- package/lib/v1/cloud-functions.js +205 -0
- package/lib/v1/config.d.ts +8 -0
- package/lib/v1/config.js +75 -0
- package/lib/{function-builder.d.ts → v1/function-builder.d.ts} +16 -16
- package/lib/{function-builder.js → v1/function-builder.js} +56 -46
- package/lib/v1/function-configuration.d.ts +197 -0
- package/lib/v1/function-configuration.js +70 -0
- package/lib/v1/index.d.ts +20 -0
- package/lib/{index.js → v1/index.js} +5 -10
- package/lib/{providers → v1/providers}/analytics.d.ts +5 -11
- package/lib/{providers → v1/providers}/analytics.js +49 -43
- package/lib/{providers → v1/providers}/auth.d.ts +28 -18
- package/lib/{providers → v1/providers}/auth.js +45 -34
- package/lib/{providers → v1/providers}/database.d.ts +25 -42
- package/lib/{providers → v1/providers}/database.js +46 -46
- package/lib/{providers → v1/providers}/firestore.d.ts +15 -33
- package/lib/{providers → v1/providers}/firestore.js +40 -44
- package/lib/{providers → v1/providers}/https.d.ts +3 -8
- package/lib/{providers → v1/providers}/https.js +14 -22
- package/lib/{providers → v1/providers}/pubsub.d.ts +8 -16
- package/lib/{providers → v1/providers}/pubsub.js +22 -26
- package/lib/{providers → v1/providers}/remoteConfig.d.ts +2 -11
- package/lib/{providers → v1/providers}/remoteConfig.js +9 -9
- package/lib/{providers → v1/providers}/storage.d.ts +11 -26
- package/lib/{providers → v1/providers}/storage.js +24 -29
- package/lib/{providers → v1/providers}/tasks.d.ts +6 -8
- package/lib/{providers → v1/providers}/tasks.js +12 -17
- package/lib/{providers → v1/providers}/testLab.d.ts +86 -46
- package/lib/{providers → v1/providers}/testLab.js +14 -14
- package/lib/v2/core.d.ts +4 -5
- package/lib/v2/index.d.ts +16 -14
- package/lib/v2/index.js +5 -1
- package/lib/v2/options.d.ts +52 -32
- package/lib/v2/options.js +32 -47
- package/lib/v2/providers/alerts/alerts.d.ts +20 -18
- package/lib/v2/providers/alerts/alerts.js +9 -6
- package/lib/v2/providers/alerts/appDistribution.d.ts +28 -26
- package/lib/v2/providers/alerts/appDistribution.js +8 -7
- package/lib/v2/providers/alerts/billing.d.ts +5 -5
- package/lib/v2/providers/alerts/billing.js +5 -4
- package/lib/v2/providers/alerts/crashlytics.d.ts +32 -30
- package/lib/v2/providers/alerts/crashlytics.js +10 -9
- package/lib/v2/providers/alerts/index.d.ts +5 -5
- package/lib/v2/providers/alerts/performance.d.ts +3 -3
- package/lib/v2/providers/alerts/performance.js +5 -7
- package/lib/v2/providers/database.d.ts +41 -38
- package/lib/v2/providers/database.js +22 -17
- package/lib/v2/providers/eventarc.d.ts +25 -23
- package/lib/v2/providers/eventarc.js +10 -7
- package/lib/v2/providers/https.d.ts +43 -33
- package/lib/v2/providers/https.js +18 -56
- package/lib/v2/providers/identity.d.ts +25 -23
- package/lib/v2/providers/identity.js +10 -7
- package/lib/v2/providers/pubsub.d.ts +25 -23
- package/lib/v2/providers/pubsub.js +11 -27
- package/lib/v2/providers/remoteConfig.d.ts +63 -0
- package/lib/v2/providers/remoteConfig.js +65 -0
- package/lib/v2/providers/scheduler.d.ts +14 -12
- package/lib/v2/providers/scheduler.js +16 -14
- package/lib/v2/providers/storage.d.ts +32 -30
- package/lib/v2/providers/storage.js +16 -32
- package/lib/v2/providers/tasks.d.ts +30 -26
- package/lib/v2/providers/tasks.js +14 -31
- package/lib/v2/providers/testLab.d.ts +110 -0
- package/lib/v2/providers/testLab.js +65 -0
- package/lib/v2/trace.d.ts +4 -0
- package/lib/v2/trace.js +21 -0
- package/package.json +61 -66
- package/lib/apps.d.ts +0 -35
- package/lib/apps.js +0 -112
- package/lib/cloud-functions.d.ts +0 -231
- package/lib/cloud-functions.js +0 -251
- package/lib/config.d.ts +0 -24
- package/lib/config.js +0 -117
- package/lib/function-configuration.d.ts +0 -108
- package/lib/function-configuration.js +0 -71
- package/lib/handler-builder.d.ts +0 -256
- package/lib/handler-builder.js +0 -349
- package/lib/index.d.ts +0 -19
- package/lib/setup.d.ts +0 -1
- package/lib/setup.js +0 -60
- package/lib/v2/params/types.d.ts +0 -118
- package/lib/v2/params/types.js +0 -196
|
@@ -22,23 +22,24 @@
|
|
|
22
22
|
// SOFTWARE.
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.extractInstanceAndPath = exports.RefBuilder = exports._refWithOptions = exports.InstanceBuilder = exports._instanceWithOptions = exports.ref = exports.instance = exports.service = exports.provider = exports.DataSnapshot = void 0;
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const database_1 = require("
|
|
25
|
+
const app_1 = require("../../common/app");
|
|
26
|
+
const config_1 = require("../../common/config");
|
|
27
|
+
const database_1 = require("../../common/providers/database");
|
|
28
28
|
Object.defineProperty(exports, "DataSnapshot", { enumerable: true, get: function () { return database_1.DataSnapshot; } });
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
/** @
|
|
33
|
-
exports.provider =
|
|
34
|
-
/** @
|
|
35
|
-
exports.service =
|
|
36
|
-
const databaseURLRegex = new RegExp(
|
|
37
|
-
const emulatorDatabaseURLRegex = new RegExp(
|
|
29
|
+
const path_1 = require("../../common/utilities/path");
|
|
30
|
+
const utils_1 = require("../../common/utilities/utils");
|
|
31
|
+
const cloud_functions_1 = require("../cloud-functions");
|
|
32
|
+
/** @internal */
|
|
33
|
+
exports.provider = "google.firebase.database";
|
|
34
|
+
/** @internal */
|
|
35
|
+
exports.service = "firebaseio.com";
|
|
36
|
+
const databaseURLRegex = new RegExp("^https://([^.]+).");
|
|
37
|
+
const emulatorDatabaseURLRegex = new RegExp("^http://.*ns=([^&]+)");
|
|
38
38
|
/**
|
|
39
39
|
* Registers a function that triggers on events from a specific
|
|
40
40
|
* Firebase Realtime Database instance.
|
|
41
41
|
*
|
|
42
|
+
* @remarks
|
|
42
43
|
* Use this method together with `ref` to specify the instance on which to
|
|
43
44
|
* watch for database events. For example: `firebase.database.instance('my-app-db-2').ref('/foo/bar')`
|
|
44
45
|
*
|
|
@@ -47,7 +48,7 @@ const emulatorDatabaseURLRegex = new RegExp('^http://.*ns=([^&]+)');
|
|
|
47
48
|
*
|
|
48
49
|
* @param instance The instance name of the database instance
|
|
49
50
|
* to watch for write events.
|
|
50
|
-
* @
|
|
51
|
+
* @returns Firebase Realtime Database instance builder interface.
|
|
51
52
|
*/
|
|
52
53
|
function instance(instance) {
|
|
53
54
|
return _instanceWithOptions(instance, {});
|
|
@@ -57,6 +58,7 @@ exports.instance = instance;
|
|
|
57
58
|
* Registers a function that triggers on Firebase Realtime Database write
|
|
58
59
|
* events.
|
|
59
60
|
*
|
|
61
|
+
* @remarks
|
|
60
62
|
* This method behaves very similarly to the method of the same name in the
|
|
61
63
|
* client and Admin Firebase SDKs. Any change to the Database that affects the
|
|
62
64
|
* data at or below the provided `path` will fire an event in Cloud Functions.
|
|
@@ -64,6 +66,7 @@ exports.instance = instance;
|
|
|
64
66
|
* There are three important differences between listening to a Realtime
|
|
65
67
|
* Database event in Cloud Functions and using the Realtime Database in the
|
|
66
68
|
* client and Admin SDKs:
|
|
69
|
+
*
|
|
67
70
|
* 1. Cloud Functions allows wildcards in the `path` name. Any `path` component
|
|
68
71
|
* in curly brackets (`{}`) is a wildcard that matches all strings. The value
|
|
69
72
|
* that matched a certain invocation of a Cloud Function is returned as part
|
|
@@ -72,20 +75,22 @@ exports.instance = instance;
|
|
|
72
75
|
* `/messages/message1` or `/messages/message2`, resulting in
|
|
73
76
|
* `event.params.messageId` being set to `"message1"` or `"message2"`,
|
|
74
77
|
* respectively.
|
|
78
|
+
*
|
|
75
79
|
* 2. Cloud Functions do not fire an event for data that already existed before
|
|
76
80
|
* the Cloud Function was deployed.
|
|
81
|
+
*
|
|
77
82
|
* 3. Cloud Function events have access to more information, including a
|
|
78
83
|
* snapshot of the previous event data and information about the user who
|
|
79
84
|
* triggered the Cloud Function.
|
|
80
85
|
*
|
|
81
86
|
* @param path The path within the Database to watch for write events.
|
|
82
|
-
* @
|
|
87
|
+
* @returns Firebase Realtime Database builder interface.
|
|
83
88
|
*/
|
|
84
89
|
function ref(path) {
|
|
85
90
|
return _refWithOptions(path, {});
|
|
86
91
|
}
|
|
87
92
|
exports.ref = ref;
|
|
88
|
-
/** @
|
|
93
|
+
/** @internal */
|
|
89
94
|
function _instanceWithOptions(instance, options) {
|
|
90
95
|
return new InstanceBuilder(instance, options);
|
|
91
96
|
}
|
|
@@ -96,30 +101,29 @@ exports._instanceWithOptions = _instanceWithOptions;
|
|
|
96
101
|
* Access via [`database.instance()`](providers_database_.html#instance).
|
|
97
102
|
*/
|
|
98
103
|
class InstanceBuilder {
|
|
99
|
-
/** @hidden */
|
|
100
104
|
constructor(instance, options) {
|
|
101
105
|
this.instance = instance;
|
|
102
106
|
this.options = options;
|
|
103
107
|
}
|
|
104
108
|
/**
|
|
105
|
-
* @
|
|
109
|
+
* @returns Firebase Realtime Database reference builder interface.
|
|
106
110
|
*/
|
|
107
111
|
ref(path) {
|
|
108
112
|
const normalized = (0, path_1.normalizePath)(path);
|
|
109
|
-
return new RefBuilder((
|
|
113
|
+
return new RefBuilder(() => `projects/_/instances/${this.instance}/refs/${normalized}`, this.options);
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
116
|
exports.InstanceBuilder = InstanceBuilder;
|
|
113
|
-
/** @
|
|
117
|
+
/** @internal */
|
|
114
118
|
function _refWithOptions(path, options) {
|
|
115
119
|
const resourceGetter = () => {
|
|
116
120
|
const normalized = (0, path_1.normalizePath)(path);
|
|
117
121
|
const databaseURL = (0, config_1.firebaseConfig)().databaseURL;
|
|
118
122
|
if (!databaseURL) {
|
|
119
|
-
throw new Error(
|
|
120
|
-
|
|
123
|
+
throw new Error("Missing expected firebase config value databaseURL, " +
|
|
124
|
+
"config is actually" +
|
|
121
125
|
JSON.stringify((0, config_1.firebaseConfig)()) +
|
|
122
|
-
|
|
126
|
+
"\n If you are unit testing, please set process.env.FIREBASE_CONFIG");
|
|
123
127
|
}
|
|
124
128
|
let instance;
|
|
125
129
|
const prodMatch = databaseURL.match(databaseURLRegex);
|
|
@@ -133,11 +137,11 @@ function _refWithOptions(path, options) {
|
|
|
133
137
|
}
|
|
134
138
|
}
|
|
135
139
|
if (!instance) {
|
|
136
|
-
throw new Error(
|
|
140
|
+
throw new Error("Invalid value for config firebase.databaseURL: " + databaseURL);
|
|
137
141
|
}
|
|
138
142
|
return `projects/_/instances/${instance}/refs/${normalized}`;
|
|
139
143
|
};
|
|
140
|
-
return new RefBuilder(
|
|
144
|
+
return new RefBuilder(resourceGetter, options);
|
|
141
145
|
}
|
|
142
146
|
exports._refWithOptions = _refWithOptions;
|
|
143
147
|
/**
|
|
@@ -146,15 +150,13 @@ exports._refWithOptions = _refWithOptions;
|
|
|
146
150
|
* Access via [`functions.database.ref()`](functions.database#.ref).
|
|
147
151
|
*/
|
|
148
152
|
class RefBuilder {
|
|
149
|
-
|
|
150
|
-
constructor(apps, triggerResource, options) {
|
|
151
|
-
this.apps = apps;
|
|
153
|
+
constructor(triggerResource, options) {
|
|
152
154
|
this.triggerResource = triggerResource;
|
|
153
155
|
this.options = options;
|
|
154
156
|
this.changeConstructor = (raw) => {
|
|
155
157
|
const [dbInstance, path] = extractInstanceAndPath(raw.context.resource.name, raw.context.domain);
|
|
156
|
-
const before = new database_1.DataSnapshot(raw.data.data, path,
|
|
157
|
-
const after = new database_1.DataSnapshot((0, utils_1.applyChange)(raw.data.data, raw.data.delta), path,
|
|
158
|
+
const before = new database_1.DataSnapshot(raw.data.data, path, (0, app_1.getApp)(), dbInstance);
|
|
159
|
+
const after = new database_1.DataSnapshot((0, utils_1.applyChange)(raw.data.data, raw.data.delta), path, (0, app_1.getApp)(), dbInstance);
|
|
158
160
|
return {
|
|
159
161
|
before,
|
|
160
162
|
after,
|
|
@@ -167,10 +169,10 @@ class RefBuilder {
|
|
|
167
169
|
*
|
|
168
170
|
* @param handler Event handler that runs every time a Firebase Realtime Database
|
|
169
171
|
* write occurs.
|
|
170
|
-
* @
|
|
172
|
+
* @returns A function that you can export and deploy.
|
|
171
173
|
*/
|
|
172
174
|
onWrite(handler) {
|
|
173
|
-
return this.onOperation(handler,
|
|
175
|
+
return this.onOperation(handler, "ref.write", this.changeConstructor);
|
|
174
176
|
}
|
|
175
177
|
/**
|
|
176
178
|
* Event handler that fires every time data is updated in
|
|
@@ -178,11 +180,10 @@ class RefBuilder {
|
|
|
178
180
|
*
|
|
179
181
|
* @param handler Event handler which is run every time a Firebase Realtime Database
|
|
180
182
|
* write occurs.
|
|
181
|
-
* @
|
|
182
|
-
* Function which you can export and deploy.
|
|
183
|
+
* @returns A function which you can export and deploy.
|
|
183
184
|
*/
|
|
184
185
|
onUpdate(handler) {
|
|
185
|
-
return this.onOperation(handler,
|
|
186
|
+
return this.onOperation(handler, "ref.update", this.changeConstructor);
|
|
186
187
|
}
|
|
187
188
|
/**
|
|
188
189
|
* Event handler that fires every time new data is created in
|
|
@@ -190,14 +191,14 @@ class RefBuilder {
|
|
|
190
191
|
*
|
|
191
192
|
* @param handler Event handler that runs every time new data is created in
|
|
192
193
|
* Firebase Realtime Database.
|
|
193
|
-
* @
|
|
194
|
+
* @returns A function that you can export and deploy.
|
|
194
195
|
*/
|
|
195
196
|
onCreate(handler) {
|
|
196
197
|
const dataConstructor = (raw) => {
|
|
197
198
|
const [dbInstance, path] = extractInstanceAndPath(raw.context.resource.name, raw.context.domain);
|
|
198
|
-
return new database_1.DataSnapshot(raw.data.delta, path,
|
|
199
|
+
return new database_1.DataSnapshot(raw.data.delta, path, (0, app_1.getApp)(), dbInstance);
|
|
199
200
|
};
|
|
200
|
-
return this.onOperation(handler,
|
|
201
|
+
return this.onOperation(handler, "ref.create", dataConstructor);
|
|
201
202
|
}
|
|
202
203
|
/**
|
|
203
204
|
* Event handler that fires every time data is deleted from
|
|
@@ -205,14 +206,14 @@ class RefBuilder {
|
|
|
205
206
|
*
|
|
206
207
|
* @param handler Event handler that runs every time data is deleted from
|
|
207
208
|
* Firebase Realtime Database.
|
|
208
|
-
* @
|
|
209
|
+
* @returns A function that you can export and deploy.
|
|
209
210
|
*/
|
|
210
211
|
onDelete(handler) {
|
|
211
212
|
const dataConstructor = (raw) => {
|
|
212
213
|
const [dbInstance, path] = extractInstanceAndPath(raw.context.resource.name, raw.context.domain);
|
|
213
|
-
return new database_1.DataSnapshot(raw.data.data, path,
|
|
214
|
+
return new database_1.DataSnapshot(raw.data.data, path, (0, app_1.getApp)(), dbInstance);
|
|
214
215
|
};
|
|
215
|
-
return this.onOperation(handler,
|
|
216
|
+
return this.onOperation(handler, "ref.delete", dataConstructor);
|
|
216
217
|
}
|
|
217
218
|
onOperation(handler, eventType, dataConstructor) {
|
|
218
219
|
return (0, cloud_functions_1.makeCloudFunction)({
|
|
@@ -223,8 +224,6 @@ class RefBuilder {
|
|
|
223
224
|
legacyEventType: `providers/${exports.provider}/eventTypes/${eventType}`,
|
|
224
225
|
triggerResource: this.triggerResource,
|
|
225
226
|
dataConstructor,
|
|
226
|
-
before: (event) => this.apps.retain(),
|
|
227
|
-
after: (event) => this.apps.release(),
|
|
228
227
|
options: this.options,
|
|
229
228
|
});
|
|
230
229
|
}
|
|
@@ -238,16 +237,17 @@ const resourceRegex = /^projects\/([^/]+)\/instances\/([a-zA-Z0-9-]+)\/refs(\/.+
|
|
|
238
237
|
* It defaults to `firebaseio.com`.
|
|
239
238
|
* Multi-region RTDB will be served from different domains.
|
|
240
239
|
* Since region is not part of the resource name, it is provided through context.
|
|
240
|
+
*
|
|
241
|
+
* @internal
|
|
241
242
|
*/
|
|
242
|
-
|
|
243
|
-
function extractInstanceAndPath(resource, domain = 'firebaseio.com') {
|
|
243
|
+
function extractInstanceAndPath(resource, domain = "firebaseio.com") {
|
|
244
244
|
const match = resource.match(new RegExp(resourceRegex));
|
|
245
245
|
if (!match) {
|
|
246
246
|
throw new Error(`Unexpected resource string for Firebase Realtime Database event: ${resource}. ` +
|
|
247
247
|
'Expected string in the format of "projects/_/instances/{firebaseioSubdomain}/refs/{ref=**}"');
|
|
248
248
|
}
|
|
249
249
|
const [, project, dbInstanceName, path] = match;
|
|
250
|
-
if (project !==
|
|
250
|
+
if (project !== "_") {
|
|
251
251
|
throw new Error(`Expect project to be '_' in a Firebase Realtime Database event`);
|
|
252
252
|
}
|
|
253
253
|
const emuHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
|
|
@@ -256,7 +256,7 @@ function extractInstanceAndPath(resource, domain = 'firebaseio.com') {
|
|
|
256
256
|
return [dbInstance, path];
|
|
257
257
|
}
|
|
258
258
|
else {
|
|
259
|
-
const dbInstance =
|
|
259
|
+
const dbInstance = "https://" + dbInstanceName + "." + domain;
|
|
260
260
|
return [dbInstance, path];
|
|
261
261
|
}
|
|
262
262
|
}
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
export declare
|
|
7
|
-
|
|
8
|
-
export declare const service = "firestore.googleapis.com";
|
|
9
|
-
/** @hidden */
|
|
10
|
-
export declare const defaultDatabase = "(default)";
|
|
11
|
-
export declare type DocumentSnapshot = firebase.firestore.DocumentSnapshot;
|
|
12
|
-
export declare type QueryDocumentSnapshot = firebase.firestore.QueryDocumentSnapshot;
|
|
1
|
+
import * as firestore from "firebase-admin/firestore";
|
|
2
|
+
import { Change } from "../../common/change";
|
|
3
|
+
import { ParamsOf } from "../../common/params";
|
|
4
|
+
import { CloudFunction, Event, EventContext } from "../cloud-functions";
|
|
5
|
+
import { DeploymentOptions } from "../function-configuration";
|
|
6
|
+
export declare type DocumentSnapshot = firestore.DocumentSnapshot;
|
|
7
|
+
export declare type QueryDocumentSnapshot = firestore.QueryDocumentSnapshot;
|
|
13
8
|
/**
|
|
14
9
|
* Select the Firestore document to listen to for events.
|
|
15
10
|
* @param path Full database path to listen to. This includes the name of
|
|
@@ -17,49 +12,36 @@ export declare type QueryDocumentSnapshot = firebase.firestore.QueryDocumentSnap
|
|
|
17
12
|
* collection is named "users" and the document is named "Ada", then the
|
|
18
13
|
* path is "/users/Ada".
|
|
19
14
|
*/
|
|
20
|
-
export declare function document(path:
|
|
21
|
-
/** @hidden */
|
|
15
|
+
export declare function document<Path extends string>(path: Path): DocumentBuilder<Path>;
|
|
22
16
|
export declare function namespace(namespace: string): NamespaceBuilder;
|
|
23
|
-
/** @hidden */
|
|
24
17
|
export declare function database(database: string): DatabaseBuilder;
|
|
25
|
-
/** @hidden */
|
|
26
|
-
export declare function _databaseWithOptions(database: string, options: DeploymentOptions): DatabaseBuilder;
|
|
27
|
-
/** @hidden */
|
|
28
|
-
export declare function _namespaceWithOptions(namespace: string, options: DeploymentOptions): NamespaceBuilder;
|
|
29
|
-
/** @hidden */
|
|
30
|
-
export declare function _documentWithOptions(path: string, options: DeploymentOptions): DocumentBuilder;
|
|
31
18
|
export declare class DatabaseBuilder {
|
|
32
19
|
private database;
|
|
33
20
|
private options;
|
|
34
|
-
/** @hidden */
|
|
35
21
|
constructor(database: string, options: DeploymentOptions);
|
|
36
22
|
namespace(namespace: string): NamespaceBuilder;
|
|
37
|
-
document(path:
|
|
23
|
+
document<Path extends string>(path: Path): DocumentBuilder<Path>;
|
|
38
24
|
}
|
|
39
25
|
export declare class NamespaceBuilder {
|
|
40
26
|
private database;
|
|
41
27
|
private options;
|
|
42
28
|
private namespace?;
|
|
43
|
-
/** @hidden */
|
|
44
29
|
constructor(database: string, options: DeploymentOptions, namespace?: string);
|
|
45
|
-
document(path:
|
|
30
|
+
document<Path extends string>(path: Path): DocumentBuilder<Path>;
|
|
46
31
|
}
|
|
47
|
-
/** @hidden */
|
|
48
32
|
export declare function snapshotConstructor(event: Event): DocumentSnapshot;
|
|
49
|
-
/** @hidden */
|
|
50
33
|
export declare function beforeSnapshotConstructor(event: Event): DocumentSnapshot;
|
|
51
|
-
export declare class DocumentBuilder {
|
|
34
|
+
export declare class DocumentBuilder<Path extends string> {
|
|
52
35
|
private triggerResource;
|
|
53
36
|
private options;
|
|
54
|
-
/** @hidden */
|
|
55
37
|
constructor(triggerResource: () => string, options: DeploymentOptions);
|
|
56
38
|
/** Respond to all document writes (creates, updates, or deletes). */
|
|
57
|
-
onWrite(handler: (change: Change<DocumentSnapshot>, context: EventContext) => PromiseLike<any> | any): CloudFunction<Change<DocumentSnapshot>>;
|
|
39
|
+
onWrite(handler: (change: Change<DocumentSnapshot>, context: EventContext<ParamsOf<Path>>) => PromiseLike<any> | any): CloudFunction<Change<DocumentSnapshot>>;
|
|
58
40
|
/** Respond only to document updates. */
|
|
59
|
-
onUpdate(handler: (change: Change<QueryDocumentSnapshot>, context: EventContext) => PromiseLike<any> | any): CloudFunction<Change<QueryDocumentSnapshot>>;
|
|
41
|
+
onUpdate(handler: (change: Change<QueryDocumentSnapshot>, context: EventContext<ParamsOf<Path>>) => PromiseLike<any> | any): CloudFunction<Change<QueryDocumentSnapshot>>;
|
|
60
42
|
/** Respond only to document creations. */
|
|
61
|
-
onCreate(handler: (snapshot: QueryDocumentSnapshot, context: EventContext) => PromiseLike<any> | any): CloudFunction<QueryDocumentSnapshot>;
|
|
43
|
+
onCreate(handler: (snapshot: QueryDocumentSnapshot, context: EventContext<ParamsOf<Path>>) => PromiseLike<any> | any): CloudFunction<QueryDocumentSnapshot>;
|
|
62
44
|
/** Respond only to document deletions. */
|
|
63
|
-
onDelete(handler: (snapshot: QueryDocumentSnapshot, context: EventContext) => PromiseLike<any> | any): CloudFunction<QueryDocumentSnapshot>;
|
|
45
|
+
onDelete(handler: (snapshot: QueryDocumentSnapshot, context: EventContext<ParamsOf<Path>>) => PromiseLike<any> | any): CloudFunction<QueryDocumentSnapshot>;
|
|
64
46
|
private onOperation;
|
|
65
47
|
}
|
|
@@ -22,20 +22,19 @@
|
|
|
22
22
|
// SOFTWARE.
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.DocumentBuilder = exports.beforeSnapshotConstructor = exports.snapshotConstructor = exports.NamespaceBuilder = exports.DatabaseBuilder = exports._documentWithOptions = exports._namespaceWithOptions = exports._databaseWithOptions = exports.database = exports.namespace = exports.document = exports.defaultDatabase = exports.service = exports.provider = void 0;
|
|
25
|
-
const
|
|
26
|
-
const _ = require("lodash");
|
|
25
|
+
const firestore = require("firebase-admin/firestore");
|
|
27
26
|
const path_1 = require("path");
|
|
28
|
-
const
|
|
27
|
+
const app_1 = require("../../common/app");
|
|
28
|
+
const change_1 = require("../../common/change");
|
|
29
|
+
const encoder_1 = require("../../common/utilities/encoder");
|
|
30
|
+
const logger = require("../../logger");
|
|
29
31
|
const cloud_functions_1 = require("../cloud-functions");
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
exports.service = 'firestore.googleapis.com';
|
|
37
|
-
/** @hidden */
|
|
38
|
-
exports.defaultDatabase = '(default)';
|
|
32
|
+
/** @internal */
|
|
33
|
+
exports.provider = "google.firestore";
|
|
34
|
+
/** @internal */
|
|
35
|
+
exports.service = "firestore.googleapis.com";
|
|
36
|
+
/** @internal */
|
|
37
|
+
exports.defaultDatabase = "(default)";
|
|
39
38
|
let firestoreInstance;
|
|
40
39
|
/**
|
|
41
40
|
* Select the Firestore document to listen to for events.
|
|
@@ -48,35 +47,32 @@ function document(path) {
|
|
|
48
47
|
return _documentWithOptions(path, {});
|
|
49
48
|
}
|
|
50
49
|
exports.document = document;
|
|
51
|
-
/** @hidden */
|
|
52
50
|
// Multiple namespaces are not yet supported by Firestore.
|
|
53
51
|
function namespace(namespace) {
|
|
54
52
|
return _namespaceWithOptions(namespace, {});
|
|
55
53
|
}
|
|
56
54
|
exports.namespace = namespace;
|
|
57
|
-
/** @hidden */
|
|
58
55
|
// Multiple databases are not yet supported by Firestore.
|
|
59
56
|
function database(database) {
|
|
60
57
|
return _databaseWithOptions(database, {});
|
|
61
58
|
}
|
|
62
59
|
exports.database = database;
|
|
63
|
-
/** @
|
|
60
|
+
/** @internal */
|
|
64
61
|
function _databaseWithOptions(database = exports.defaultDatabase, options) {
|
|
65
62
|
return new DatabaseBuilder(database, options);
|
|
66
63
|
}
|
|
67
64
|
exports._databaseWithOptions = _databaseWithOptions;
|
|
68
|
-
/** @
|
|
65
|
+
/** @internal */
|
|
69
66
|
function _namespaceWithOptions(namespace, options) {
|
|
70
67
|
return _databaseWithOptions(exports.defaultDatabase, options).namespace(namespace);
|
|
71
68
|
}
|
|
72
69
|
exports._namespaceWithOptions = _namespaceWithOptions;
|
|
73
|
-
/** @
|
|
70
|
+
/** @internal */
|
|
74
71
|
function _documentWithOptions(path, options) {
|
|
75
72
|
return _databaseWithOptions(exports.defaultDatabase, options).document(path);
|
|
76
73
|
}
|
|
77
74
|
exports._documentWithOptions = _documentWithOptions;
|
|
78
75
|
class DatabaseBuilder {
|
|
79
|
-
/** @hidden */
|
|
80
76
|
constructor(database, options) {
|
|
81
77
|
this.database = database;
|
|
82
78
|
this.options = options;
|
|
@@ -90,7 +86,6 @@ class DatabaseBuilder {
|
|
|
90
86
|
}
|
|
91
87
|
exports.DatabaseBuilder = DatabaseBuilder;
|
|
92
88
|
class NamespaceBuilder {
|
|
93
|
-
/** @hidden */
|
|
94
89
|
constructor(database, options, namespace) {
|
|
95
90
|
this.database = database;
|
|
96
91
|
this.options = options;
|
|
@@ -99,59 +94,60 @@ class NamespaceBuilder {
|
|
|
99
94
|
document(path) {
|
|
100
95
|
return new DocumentBuilder(() => {
|
|
101
96
|
if (!process.env.GCLOUD_PROJECT) {
|
|
102
|
-
throw new Error(
|
|
97
|
+
throw new Error("process.env.GCLOUD_PROJECT is not set.");
|
|
103
98
|
}
|
|
104
|
-
const database = path_1.posix.join(
|
|
105
|
-
return path_1.posix.join(database, this.namespace ? `documents@${this.namespace}` :
|
|
99
|
+
const database = path_1.posix.join("projects", process.env.GCLOUD_PROJECT, "databases", this.database);
|
|
100
|
+
return path_1.posix.join(database, this.namespace ? `documents@${this.namespace}` : "documents", path);
|
|
106
101
|
}, this.options);
|
|
107
102
|
}
|
|
108
103
|
}
|
|
109
104
|
exports.NamespaceBuilder = NamespaceBuilder;
|
|
110
105
|
function _getValueProto(data, resource, valueFieldName) {
|
|
111
|
-
|
|
106
|
+
const value = data === null || data === void 0 ? void 0 : data[valueFieldName];
|
|
107
|
+
if (typeof value === "undefined" ||
|
|
108
|
+
value === null ||
|
|
109
|
+
(typeof value === "object" && !Object.keys(value).length)) {
|
|
112
110
|
// Firestore#snapshot_ takes resource string instead of proto for a non-existent snapshot
|
|
113
111
|
return resource;
|
|
114
112
|
}
|
|
115
113
|
const proto = {
|
|
116
|
-
fields:
|
|
117
|
-
createTime: (0, encoder_1.dateToTimestampProto)(
|
|
118
|
-
updateTime: (0, encoder_1.dateToTimestampProto)(
|
|
119
|
-
name:
|
|
114
|
+
fields: (value === null || value === void 0 ? void 0 : value.fields) || {},
|
|
115
|
+
createTime: (0, encoder_1.dateToTimestampProto)(value === null || value === void 0 ? void 0 : value.createTime),
|
|
116
|
+
updateTime: (0, encoder_1.dateToTimestampProto)(value === null || value === void 0 ? void 0 : value.updateTime),
|
|
117
|
+
name: (value === null || value === void 0 ? void 0 : value.name) || resource,
|
|
120
118
|
};
|
|
121
119
|
return proto;
|
|
122
120
|
}
|
|
123
|
-
/** @hidden */
|
|
124
121
|
function snapshotConstructor(event) {
|
|
125
|
-
var _a;
|
|
122
|
+
var _a, _b, _c, _d, _e;
|
|
126
123
|
if (!firestoreInstance) {
|
|
127
|
-
firestoreInstance =
|
|
124
|
+
firestoreInstance = firestore.getFirestore((0, app_1.getApp)());
|
|
128
125
|
}
|
|
129
|
-
const valueProto = _getValueProto(event.data, event.context.resource.name,
|
|
130
|
-
let timeString = (_a =
|
|
126
|
+
const valueProto = _getValueProto(event.data, event.context.resource.name, "value");
|
|
127
|
+
let timeString = (_c = (_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.readTime) !== null && _c !== void 0 ? _c : (_e = (_d = event === null || event === void 0 ? void 0 : event.data) === null || _d === void 0 ? void 0 : _d.value) === null || _e === void 0 ? void 0 : _e.updateTime;
|
|
131
128
|
if (!timeString) {
|
|
132
|
-
logger.warn(
|
|
129
|
+
logger.warn("Snapshot has no readTime. Using now()");
|
|
133
130
|
timeString = new Date().toISOString();
|
|
134
131
|
}
|
|
135
132
|
const readTime = (0, encoder_1.dateToTimestampProto)(timeString);
|
|
136
|
-
return firestoreInstance.snapshot_(valueProto, readTime,
|
|
133
|
+
return firestoreInstance.snapshot_(valueProto, readTime, "json");
|
|
137
134
|
}
|
|
138
135
|
exports.snapshotConstructor = snapshotConstructor;
|
|
139
|
-
/** @hidden */
|
|
140
136
|
// TODO remove this function when wire format changes to new format
|
|
141
137
|
function beforeSnapshotConstructor(event) {
|
|
138
|
+
var _a, _b;
|
|
142
139
|
if (!firestoreInstance) {
|
|
143
|
-
firestoreInstance =
|
|
140
|
+
firestoreInstance = firestore.getFirestore((0, app_1.getApp)());
|
|
144
141
|
}
|
|
145
|
-
const oldValueProto = _getValueProto(event.data, event.context.resource.name,
|
|
146
|
-
const oldReadTime = (0, encoder_1.dateToTimestampProto)(
|
|
147
|
-
return firestoreInstance.snapshot_(oldValueProto, oldReadTime,
|
|
142
|
+
const oldValueProto = _getValueProto(event.data, event.context.resource.name, "oldValue");
|
|
143
|
+
const oldReadTime = (0, encoder_1.dateToTimestampProto)((_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.oldValue) === null || _b === void 0 ? void 0 : _b.readTime);
|
|
144
|
+
return firestoreInstance.snapshot_(oldValueProto, oldReadTime, "json");
|
|
148
145
|
}
|
|
149
146
|
exports.beforeSnapshotConstructor = beforeSnapshotConstructor;
|
|
150
147
|
function changeConstructor(raw) {
|
|
151
148
|
return change_1.Change.fromObjects(beforeSnapshotConstructor(raw), snapshotConstructor(raw));
|
|
152
149
|
}
|
|
153
150
|
class DocumentBuilder {
|
|
154
|
-
/** @hidden */
|
|
155
151
|
constructor(triggerResource, options) {
|
|
156
152
|
this.triggerResource = triggerResource;
|
|
157
153
|
this.options = options;
|
|
@@ -159,19 +155,19 @@ class DocumentBuilder {
|
|
|
159
155
|
}
|
|
160
156
|
/** Respond to all document writes (creates, updates, or deletes). */
|
|
161
157
|
onWrite(handler) {
|
|
162
|
-
return this.onOperation(handler,
|
|
158
|
+
return this.onOperation(handler, "document.write", changeConstructor);
|
|
163
159
|
}
|
|
164
160
|
/** Respond only to document updates. */
|
|
165
161
|
onUpdate(handler) {
|
|
166
|
-
return this.onOperation(handler,
|
|
162
|
+
return this.onOperation(handler, "document.update", changeConstructor);
|
|
167
163
|
}
|
|
168
164
|
/** Respond only to document creations. */
|
|
169
165
|
onCreate(handler) {
|
|
170
|
-
return this.onOperation(handler,
|
|
166
|
+
return this.onOperation(handler, "document.create", snapshotConstructor);
|
|
171
167
|
}
|
|
172
168
|
/** Respond only to document deletions. */
|
|
173
169
|
onDelete(handler) {
|
|
174
|
-
return this.onOperation(handler,
|
|
170
|
+
return this.onOperation(handler, "document.delete", beforeSnapshotConstructor);
|
|
175
171
|
}
|
|
176
172
|
onOperation(handler, eventType, dataConstructor) {
|
|
177
173
|
return (0, cloud_functions_1.makeCloudFunction)({
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import * as express from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { DeploymentOptions } from '../function-configuration';
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { CallableContext, FunctionsErrorCode, HttpsError, Request } from "../../common/providers/https";
|
|
3
|
+
import { HttpsFunction, Runnable } from "../cloud-functions";
|
|
5
4
|
export { Request, CallableContext, FunctionsErrorCode, HttpsError };
|
|
6
5
|
/**
|
|
7
6
|
* Handle HTTP requests.
|
|
@@ -14,7 +13,3 @@ export declare function onRequest(handler: (req: Request, resp: express.Response
|
|
|
14
13
|
* @param handler A method that takes a data and context and returns a value.
|
|
15
14
|
*/
|
|
16
15
|
export declare function onCall(handler: (data: any, context: CallableContext) => any | Promise<any>): HttpsFunction & Runnable<any>;
|
|
17
|
-
/** @hidden */
|
|
18
|
-
export declare function _onRequestWithOptions(handler: (req: Request, resp: express.Response) => void | Promise<void>, options: DeploymentOptions): HttpsFunction;
|
|
19
|
-
/** @hidden */
|
|
20
|
-
export declare function _onCallWithOptions(handler: (data: any, context: CallableContext) => any | Promise<any>, options: DeploymentOptions): HttpsFunction & Runnable<any>;
|
|
@@ -22,10 +22,11 @@
|
|
|
22
22
|
// SOFTWARE.
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports._onCallWithOptions = exports._onRequestWithOptions = exports.onCall = exports.onRequest = exports.HttpsError = void 0;
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const https_1 = require("../common/providers/https");
|
|
25
|
+
const encoding_1 = require("../../common/encoding");
|
|
26
|
+
const https_1 = require("../../common/providers/https");
|
|
28
27
|
Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
|
|
28
|
+
const cloud_functions_1 = require("../cloud-functions");
|
|
29
|
+
const manifest_1 = require("../../runtime/manifest");
|
|
29
30
|
/**
|
|
30
31
|
* Handle HTTP requests.
|
|
31
32
|
* @param handler A function that takes a request and response object,
|
|
@@ -43,46 +44,37 @@ function onCall(handler) {
|
|
|
43
44
|
return _onCallWithOptions(handler, {});
|
|
44
45
|
}
|
|
45
46
|
exports.onCall = onCall;
|
|
46
|
-
/** @
|
|
47
|
+
/** @internal */
|
|
47
48
|
function _onRequestWithOptions(handler, options) {
|
|
48
|
-
// lets us add
|
|
49
|
+
// lets us add __endpoint without altering handler:
|
|
49
50
|
const cloudFunction = (req, res) => {
|
|
50
51
|
return handler(req, res);
|
|
51
52
|
};
|
|
52
|
-
cloudFunction.__trigger = {
|
|
53
|
-
...(0, cloud_functions_1.optionsToTrigger)(options),
|
|
54
|
-
httpsTrigger: {},
|
|
55
|
-
};
|
|
56
|
-
(0, encoding_1.convertIfPresent)(cloudFunction.__trigger.httpsTrigger, options, 'invoker', 'invoker', encoding_1.convertInvoker);
|
|
57
53
|
// TODO parse the options
|
|
58
54
|
cloudFunction.__endpoint = {
|
|
59
|
-
platform:
|
|
55
|
+
platform: "gcfv1",
|
|
56
|
+
...(0, manifest_1.initV1Endpoint)(options),
|
|
60
57
|
...(0, cloud_functions_1.optionsToEndpoint)(options),
|
|
61
58
|
httpsTrigger: {},
|
|
62
59
|
};
|
|
63
|
-
(0, encoding_1.convertIfPresent)(cloudFunction.__endpoint.httpsTrigger, options,
|
|
60
|
+
(0, encoding_1.convertIfPresent)(cloudFunction.__endpoint.httpsTrigger, options, "invoker", "invoker", encoding_1.convertInvoker);
|
|
64
61
|
return cloudFunction;
|
|
65
62
|
}
|
|
66
63
|
exports._onRequestWithOptions = _onRequestWithOptions;
|
|
67
|
-
/** @
|
|
64
|
+
/** @internal */
|
|
68
65
|
function _onCallWithOptions(handler, options) {
|
|
69
66
|
// onCallHandler sniffs the function length of the passed-in callback
|
|
70
67
|
// and the user could have only tried to listen to data. Wrap their handler
|
|
71
68
|
// in another handler to avoid accidentally triggering the v2 API
|
|
72
69
|
const fixedLen = (data, context) => handler(data, context);
|
|
73
70
|
const func = (0, https_1.onCallHandler)({
|
|
74
|
-
|
|
75
|
-
cors: { origin: true, methods:
|
|
71
|
+
enforceAppCheck: options.enforceAppCheck,
|
|
72
|
+
cors: { origin: true, methods: "POST" },
|
|
76
73
|
}, fixedLen);
|
|
77
|
-
func.__trigger = {
|
|
78
|
-
labels: {},
|
|
79
|
-
...(0, cloud_functions_1.optionsToTrigger)(options),
|
|
80
|
-
httpsTrigger: {},
|
|
81
|
-
};
|
|
82
|
-
func.__trigger.labels['deployment-callable'] = 'true';
|
|
83
74
|
func.__endpoint = {
|
|
84
|
-
platform:
|
|
75
|
+
platform: "gcfv1",
|
|
85
76
|
labels: {},
|
|
77
|
+
...(0, manifest_1.initV1Endpoint)(options),
|
|
86
78
|
...(0, cloud_functions_1.optionsToEndpoint)(options),
|
|
87
79
|
callableTrigger: {},
|
|
88
80
|
};
|