firebase-functions 3.21.2 → 3.22.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/lib/cloud-functions.d.ts +1 -48
- package/lib/cloud-functions.js +2 -59
- package/lib/common/change.d.ts +46 -0
- package/lib/common/change.js +82 -0
- package/lib/common/providers/database.d.ts +145 -0
- package/lib/common/providers/database.js +271 -0
- package/lib/common/providers/identity.js +7 -3
- package/lib/common/providers/tasks.js +13 -12
- package/lib/function-builder.d.ts +2 -2
- package/lib/function-builder.js +2 -2
- package/lib/providers/database.d.ts +4 -146
- package/lib/providers/database.js +7 -251
- package/lib/providers/firestore.d.ts +2 -1
- package/lib/providers/firestore.js +2 -1
- package/lib/utilities/path-pattern.d.ts +1 -0
- package/lib/utilities/path-pattern.js +142 -0
- package/lib/v2/core.d.ts +2 -0
- package/lib/v2/core.js +7 -0
- package/lib/v2/index.d.ts +3 -1
- package/lib/v2/index.js +5 -1
- package/lib/v2/providers/database.d.ts +182 -0
- package/lib/v2/providers/database.js +204 -0
- package/package.json +7 -3
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The MIT License (MIT)
|
|
3
|
+
//
|
|
4
|
+
// Copyright (c) 2022 Firebase
|
|
5
|
+
//
|
|
6
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
// in the Software without restriction, including without limitation the rights
|
|
9
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
// furnished to do so, subject to the following conditions:
|
|
12
|
+
//
|
|
13
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
// copies or substantial portions of the Software.
|
|
15
|
+
//
|
|
16
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
// SOFTWARE.
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.PathPattern = exports.trimParam = void 0;
|
|
25
|
+
const path_1 = require("./path");
|
|
26
|
+
/** https://cloud.google.com/eventarc/docs/path-patterns */
|
|
27
|
+
/** @hidden */
|
|
28
|
+
const WILDCARD_CAPTURE_REGEX = new RegExp('{[^/{}]+}', 'g');
|
|
29
|
+
/** @internal */
|
|
30
|
+
function trimParam(param) {
|
|
31
|
+
const paramNoBraces = param.slice(1, -1);
|
|
32
|
+
if (paramNoBraces.includes('=')) {
|
|
33
|
+
return paramNoBraces.slice(0, paramNoBraces.indexOf('='));
|
|
34
|
+
}
|
|
35
|
+
return paramNoBraces;
|
|
36
|
+
}
|
|
37
|
+
exports.trimParam = trimParam;
|
|
38
|
+
/** @hidden */
|
|
39
|
+
class Segment {
|
|
40
|
+
constructor(value) {
|
|
41
|
+
this.value = value;
|
|
42
|
+
this.name = 'segment';
|
|
43
|
+
this.trimmed = value;
|
|
44
|
+
}
|
|
45
|
+
isSingleSegmentWildcard() {
|
|
46
|
+
return this.value.includes('*') && !this.isMultiSegmentWildcard();
|
|
47
|
+
}
|
|
48
|
+
isMultiSegmentWildcard() {
|
|
49
|
+
return this.value.includes('**');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** @hidden */
|
|
53
|
+
class SingleCaptureSegment {
|
|
54
|
+
constructor(value) {
|
|
55
|
+
this.value = value;
|
|
56
|
+
this.name = 'single-capture';
|
|
57
|
+
this.trimmed = trimParam(value);
|
|
58
|
+
}
|
|
59
|
+
isSingleSegmentWildcard() {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
isMultiSegmentWildcard() {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** @hidden */
|
|
67
|
+
class MultiCaptureSegment {
|
|
68
|
+
constructor(value) {
|
|
69
|
+
this.value = value;
|
|
70
|
+
this.name = 'multi-capture';
|
|
71
|
+
this.trimmed = trimParam(value);
|
|
72
|
+
}
|
|
73
|
+
isSingleSegmentWildcard() {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
isMultiSegmentWildcard() {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Implements Eventarc's path pattern from the spec https://cloud.google.com/eventarc/docs/path-patterns
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
class PathPattern {
|
|
85
|
+
constructor(raw) {
|
|
86
|
+
this.raw = raw;
|
|
87
|
+
this.segments = [];
|
|
88
|
+
this.initPathSegments(raw);
|
|
89
|
+
}
|
|
90
|
+
/** @throws on validation error */
|
|
91
|
+
static compile(rawPath) { }
|
|
92
|
+
getValue() {
|
|
93
|
+
return this.raw;
|
|
94
|
+
}
|
|
95
|
+
// If false, we don't need to use pathPattern as our eventarc match type.
|
|
96
|
+
hasWildcards() {
|
|
97
|
+
return this.segments.some((segment) => segment.isSingleSegmentWildcard() || segment.isMultiSegmentWildcard());
|
|
98
|
+
}
|
|
99
|
+
hasCaptures() {
|
|
100
|
+
return this.segments.some((segment) => segment.name == 'single-capture' || segment.name === 'multi-capture');
|
|
101
|
+
}
|
|
102
|
+
extractMatches(path) {
|
|
103
|
+
const matches = {};
|
|
104
|
+
if (!this.hasCaptures()) {
|
|
105
|
+
return matches;
|
|
106
|
+
}
|
|
107
|
+
const pathSegments = (0, path_1.pathParts)(path);
|
|
108
|
+
let pathNdx = 0;
|
|
109
|
+
for (let segmentNdx = 0; segmentNdx < this.segments.length && pathNdx < pathSegments.length; segmentNdx++) {
|
|
110
|
+
const segment = this.segments[segmentNdx];
|
|
111
|
+
const remainingSegments = this.segments.length - 1 - segmentNdx;
|
|
112
|
+
const nextPathNdx = pathSegments.length - remainingSegments;
|
|
113
|
+
if (segment.name === 'single-capture') {
|
|
114
|
+
matches[segment.trimmed] = pathSegments[pathNdx];
|
|
115
|
+
}
|
|
116
|
+
else if (segment.name === 'multi-capture') {
|
|
117
|
+
matches[segment.trimmed] = pathSegments
|
|
118
|
+
.slice(pathNdx, nextPathNdx)
|
|
119
|
+
.join('/');
|
|
120
|
+
}
|
|
121
|
+
pathNdx = segment.isMultiSegmentWildcard() ? nextPathNdx : pathNdx + 1;
|
|
122
|
+
}
|
|
123
|
+
return matches;
|
|
124
|
+
}
|
|
125
|
+
initPathSegments(raw) {
|
|
126
|
+
const parts = (0, path_1.pathParts)(raw);
|
|
127
|
+
for (const part of parts) {
|
|
128
|
+
let segment;
|
|
129
|
+
const capture = part.match(WILDCARD_CAPTURE_REGEX);
|
|
130
|
+
if (capture && capture.length === 1) {
|
|
131
|
+
segment = part.includes('**')
|
|
132
|
+
? new MultiCaptureSegment(part)
|
|
133
|
+
: new SingleCaptureSegment(part);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
segment = new Segment(part);
|
|
137
|
+
}
|
|
138
|
+
this.segments.push(segment);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.PathPattern = PathPattern;
|
package/lib/v2/core.d.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
* Core functionality of the Firebase Functions v2 SDK.
|
|
3
3
|
* @packageDocumentation
|
|
4
4
|
*/
|
|
5
|
+
import { Change } from '../common/change';
|
|
5
6
|
import { ManifestEndpoint } from '../runtime/manifest';
|
|
7
|
+
export { Change };
|
|
6
8
|
/**
|
|
7
9
|
* A CloudEventBase is the base of a cross-platform format for encoding a serverless event.
|
|
8
10
|
* More information can be found in https://github.com/cloudevents/spec
|
package/lib/v2/core.js
CHANGED
|
@@ -21,3 +21,10 @@
|
|
|
21
21
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
// SOFTWARE.
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.Change = void 0;
|
|
25
|
+
/**
|
|
26
|
+
* Core functionality of the Firebase Functions v2 SDK.
|
|
27
|
+
* @packageDocumentation
|
|
28
|
+
*/
|
|
29
|
+
const change_1 = require("../common/change");
|
|
30
|
+
Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return change_1.Change; } });
|
package/lib/v2/index.d.ts
CHANGED
|
@@ -7,12 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import * as logger from '../logger';
|
|
9
9
|
import * as alerts from './providers/alerts';
|
|
10
|
+
import * as database from './providers/database';
|
|
10
11
|
import * as eventarc from './providers/eventarc';
|
|
11
12
|
import * as https from './providers/https';
|
|
12
13
|
import * as identity from './providers/identity';
|
|
13
14
|
import * as pubsub from './providers/pubsub';
|
|
14
15
|
import * as storage from './providers/storage';
|
|
15
16
|
import * as tasks from './providers/tasks';
|
|
16
|
-
export { alerts, storage, https, identity, pubsub, logger, tasks, eventarc };
|
|
17
|
+
export { alerts, database, storage, https, identity, pubsub, logger, tasks, eventarc, };
|
|
17
18
|
export { setGlobalOptions, GlobalOptions, SupportedRegion, MemoryOption, VpcEgressSetting, IngressSetting, EventHandlerOptions, } from './options';
|
|
19
|
+
export { Change } from '../common/change';
|
|
18
20
|
export { CloudFunction, CloudEvent } from './core';
|
package/lib/v2/index.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
// SOFTWARE.
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.setGlobalOptions = exports.eventarc = exports.tasks = exports.logger = exports.pubsub = exports.identity = exports.https = exports.storage = exports.alerts = void 0;
|
|
24
|
+
exports.Change = exports.setGlobalOptions = exports.eventarc = exports.tasks = exports.logger = exports.pubsub = exports.identity = exports.https = exports.storage = exports.database = exports.alerts = void 0;
|
|
25
25
|
/**
|
|
26
26
|
* The V2 API for Cloud Functions for Firebase.
|
|
27
27
|
* This SDK also supports deep imports. For example, the namespace
|
|
@@ -33,6 +33,8 @@ const logger = require("../logger");
|
|
|
33
33
|
exports.logger = logger;
|
|
34
34
|
const alerts = require("./providers/alerts");
|
|
35
35
|
exports.alerts = alerts;
|
|
36
|
+
const database = require("./providers/database");
|
|
37
|
+
exports.database = database;
|
|
36
38
|
const eventarc = require("./providers/eventarc");
|
|
37
39
|
exports.eventarc = eventarc;
|
|
38
40
|
const https = require("./providers/https");
|
|
@@ -47,3 +49,5 @@ const tasks = require("./providers/tasks");
|
|
|
47
49
|
exports.tasks = tasks;
|
|
48
50
|
var options_1 = require("./options");
|
|
49
51
|
Object.defineProperty(exports, "setGlobalOptions", { enumerable: true, get: function () { return options_1.setGlobalOptions; } });
|
|
52
|
+
var change_1 = require("../common/change");
|
|
53
|
+
Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return change_1.Change; } });
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Change } from '../../common/change';
|
|
2
|
+
import { DataSnapshot } from '../../common/providers/database';
|
|
3
|
+
import { CloudEvent, CloudFunction } from '../core';
|
|
4
|
+
import * as options from '../options';
|
|
5
|
+
export { DataSnapshot };
|
|
6
|
+
/** @hidden */
|
|
7
|
+
export interface RawRTDBCloudEventData {
|
|
8
|
+
['@type']: 'type.googleapis.com/google.events.firebase.database.v1.ReferenceEventData';
|
|
9
|
+
data: any;
|
|
10
|
+
delta: any;
|
|
11
|
+
}
|
|
12
|
+
/** @hidden */
|
|
13
|
+
export interface RawRTDBCloudEvent extends CloudEvent<RawRTDBCloudEventData> {
|
|
14
|
+
firebasedatabasehost: string;
|
|
15
|
+
instance: string;
|
|
16
|
+
ref: string;
|
|
17
|
+
location: string;
|
|
18
|
+
}
|
|
19
|
+
/** A CloudEvent that contains a DataSnapshot or a Change<DataSnapshot> */
|
|
20
|
+
export interface DatabaseEvent<T> extends CloudEvent<T> {
|
|
21
|
+
/** The domain of the database instance */
|
|
22
|
+
firebaseDatabaseHost: string;
|
|
23
|
+
/** The instance ID portion of the fully qualified resource name */
|
|
24
|
+
instance: string;
|
|
25
|
+
/** The database reference path */
|
|
26
|
+
ref: string;
|
|
27
|
+
/** The location of the database */
|
|
28
|
+
location: string;
|
|
29
|
+
/**
|
|
30
|
+
* An object containing the values of the path patterns.
|
|
31
|
+
* Only named capture groups will be populated - {key}, {key=*}, {key=**}
|
|
32
|
+
*/
|
|
33
|
+
params: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
/** ReferenceOptions extend EventHandlerOptions with provided ref and optional instance */
|
|
36
|
+
export interface ReferenceOptions extends options.EventHandlerOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Specify the handler to trigger on a database reference(s).
|
|
39
|
+
* This value can either be a single reference or a pattern.
|
|
40
|
+
* Examples: '/foo/bar', '/foo/{bar}'
|
|
41
|
+
*/
|
|
42
|
+
ref: string;
|
|
43
|
+
/**
|
|
44
|
+
* Specify the handler to trigger on a database instance(s).
|
|
45
|
+
* If present, this value can either be a single instance or a pattern.
|
|
46
|
+
* Examples: 'my-instance-1', 'my-instance-*'
|
|
47
|
+
* Note: The capture syntax cannot be used for 'instance'.
|
|
48
|
+
*/
|
|
49
|
+
instance?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Region where functions should be deployed.
|
|
52
|
+
*/
|
|
53
|
+
region?: options.SupportedRegion | string;
|
|
54
|
+
/**
|
|
55
|
+
* Amount of memory to allocate to a function.
|
|
56
|
+
* A value of null restores the defaults of 256MB.
|
|
57
|
+
*/
|
|
58
|
+
memory?: options.MemoryOption | null;
|
|
59
|
+
/**
|
|
60
|
+
* Timeout for the function in sections, possible values are 0 to 540.
|
|
61
|
+
* HTTPS functions can specify a higher timeout.
|
|
62
|
+
* A value of null restores the default of 60s
|
|
63
|
+
* The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
|
|
64
|
+
* function depends on the type of function: Event handling functions have a
|
|
65
|
+
* maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
|
|
66
|
+
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
|
|
67
|
+
* timeout of 1,800s (30 minutes)
|
|
68
|
+
*/
|
|
69
|
+
timeoutSeconds?: number | null;
|
|
70
|
+
/**
|
|
71
|
+
* Min number of actual instances to be running at a given time.
|
|
72
|
+
* Instances will be billed for memory allocation and 10% of CPU allocation
|
|
73
|
+
* while idle.
|
|
74
|
+
* A value of null restores the default min instances.
|
|
75
|
+
*/
|
|
76
|
+
minInstances?: number | null;
|
|
77
|
+
/**
|
|
78
|
+
* Max number of instances to be running in parallel.
|
|
79
|
+
* A value of null restores the default max instances.
|
|
80
|
+
*/
|
|
81
|
+
maxInstances?: number | null;
|
|
82
|
+
/**
|
|
83
|
+
* Number of requests a function can serve at once.
|
|
84
|
+
* Can only be applied to functions running on Cloud Functions v2.
|
|
85
|
+
* A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
|
|
86
|
+
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
|
|
87
|
+
* The maximum value for concurrency is 1,000.
|
|
88
|
+
*/
|
|
89
|
+
concurrency?: number | null;
|
|
90
|
+
/**
|
|
91
|
+
* Fractional number of CPUs to allocate to a function.
|
|
92
|
+
* Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
|
|
93
|
+
* This is different from the defaults when using the gcloud utility and is different from
|
|
94
|
+
* the fixed amount assigned in Google Cloud Functions generation 1.
|
|
95
|
+
* To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
|
|
96
|
+
* to the value "gcf_gen1"
|
|
97
|
+
*/
|
|
98
|
+
cpu?: number | 'gcf_gen1';
|
|
99
|
+
/**
|
|
100
|
+
* Connect cloud function to specified VPC connector.
|
|
101
|
+
* A value of null removes the VPC connector
|
|
102
|
+
*/
|
|
103
|
+
vpcConnector?: string | null;
|
|
104
|
+
/**
|
|
105
|
+
* Egress settings for VPC connector.
|
|
106
|
+
* A value of null turns off VPC connector egress settings
|
|
107
|
+
*/
|
|
108
|
+
vpcConnectorEgressSettings?: options.VpcEgressSetting | null;
|
|
109
|
+
/**
|
|
110
|
+
* Specific service account for the function to run as.
|
|
111
|
+
* A value of null restores the default service account.
|
|
112
|
+
*/
|
|
113
|
+
serviceAccount?: string | null;
|
|
114
|
+
/**
|
|
115
|
+
* Ingress settings which control where this function can be called from.
|
|
116
|
+
* A value of null turns off ingress settings.
|
|
117
|
+
*/
|
|
118
|
+
ingressSettings?: options.IngressSetting | null;
|
|
119
|
+
/**
|
|
120
|
+
* User labels to set on the function.
|
|
121
|
+
*/
|
|
122
|
+
labels?: Record<string, string>;
|
|
123
|
+
secrets?: string[];
|
|
124
|
+
/** Whether failed executions should be delivered again. */
|
|
125
|
+
retry?: boolean;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Event handler which triggers when data is created, updated, or deleted in Realtime Database.
|
|
129
|
+
*
|
|
130
|
+
* @param reference - The database reference path to trigger on.
|
|
131
|
+
* @param handler - Event handler which is run every time a Realtime Database create, update, or delete occurs.
|
|
132
|
+
*/
|
|
133
|
+
export declare function onValueWritten(reference: string, handler: (event: DatabaseEvent<Change<DataSnapshot>>) => any | Promise<any>): CloudFunction<DatabaseEvent<Change<DataSnapshot>>>;
|
|
134
|
+
/**
|
|
135
|
+
* Event handler which triggers when data is created, updated, or deleted in Realtime Database.
|
|
136
|
+
*
|
|
137
|
+
* @param opts - Options that can be set on an individual event-handling function.
|
|
138
|
+
* @param handler - Event handler which is run every time a Realtime Database create, update, or delete occurs.
|
|
139
|
+
*/
|
|
140
|
+
export declare function onValueWritten(opts: ReferenceOptions, handler: (event: DatabaseEvent<Change<DataSnapshot>>) => any | Promise<any>): CloudFunction<DatabaseEvent<Change<DataSnapshot>>>;
|
|
141
|
+
/**
|
|
142
|
+
* Event handler which triggers when data is created in Realtime Database.
|
|
143
|
+
*
|
|
144
|
+
* @param reference - The database reference path to trigger on.
|
|
145
|
+
* @param handler - Event handler which is run every time a Realtime Database create occurs.
|
|
146
|
+
*/
|
|
147
|
+
export declare function onValueCreated(reference: string, handler: (event: DatabaseEvent<DataSnapshot>) => any | Promise<any>): CloudFunction<DatabaseEvent<DataSnapshot>>;
|
|
148
|
+
/**
|
|
149
|
+
* Event handler which triggers when data is created in Realtime Database.
|
|
150
|
+
*
|
|
151
|
+
* @param opts - Options that can be set on an individual event-handling function.
|
|
152
|
+
* @param handler - Event handler which is run every time a Realtime Database create occurs.
|
|
153
|
+
*/
|
|
154
|
+
export declare function onValueCreated(opts: ReferenceOptions, handler: (event: DatabaseEvent<DataSnapshot>) => any | Promise<any>): CloudFunction<DatabaseEvent<DataSnapshot>>;
|
|
155
|
+
/**
|
|
156
|
+
* Event handler which triggers when data is updated in Realtime Database.
|
|
157
|
+
*
|
|
158
|
+
* @param reference - The database reference path to trigger on.
|
|
159
|
+
* @param handler - Event handler which is run every time a Realtime Database update occurs.
|
|
160
|
+
*/
|
|
161
|
+
export declare function onValueUpdated(reference: string, handler: (event: DatabaseEvent<Change<DataSnapshot>>) => any | Promise<any>): CloudFunction<DatabaseEvent<Change<DataSnapshot>>>;
|
|
162
|
+
/**
|
|
163
|
+
* Event handler which triggers when data is updated in Realtime Database.
|
|
164
|
+
*
|
|
165
|
+
* @param opts - Options that can be set on an individual event-handling function.
|
|
166
|
+
* @param handler - Event handler which is run every time a Realtime Database update occurs.
|
|
167
|
+
*/
|
|
168
|
+
export declare function onValueUpdated(opts: ReferenceOptions, handler: (event: DatabaseEvent<Change<DataSnapshot>>) => any | Promise<any>): CloudFunction<DatabaseEvent<Change<DataSnapshot>>>;
|
|
169
|
+
/**
|
|
170
|
+
* Event handler which triggers when data is deleted in Realtime Database.
|
|
171
|
+
*
|
|
172
|
+
* @param reference - The database reference path to trigger on.
|
|
173
|
+
* @param handler - Event handler which is run every time a Realtime Database deletion occurs.
|
|
174
|
+
*/
|
|
175
|
+
export declare function onValueDeleted(reference: string, handler: (event: DatabaseEvent<DataSnapshot>) => any | Promise<any>): CloudFunction<DatabaseEvent<DataSnapshot>>;
|
|
176
|
+
/**
|
|
177
|
+
* Event handler which triggers when data is deleted in Realtime Database.
|
|
178
|
+
*
|
|
179
|
+
* @param opts - Options that can be set on an individual event-handling function.
|
|
180
|
+
* @param handler - Event handler which is run every time a Realtime Database deletion occurs.
|
|
181
|
+
*/
|
|
182
|
+
export declare function onValueDeleted(opts: ReferenceOptions, handler: (event: DatabaseEvent<DataSnapshot>) => any | Promise<any>): CloudFunction<DatabaseEvent<DataSnapshot>>;
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The MIT License (MIT)
|
|
3
|
+
//
|
|
4
|
+
// Copyright (c) 2022 Firebase
|
|
5
|
+
//
|
|
6
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
// in the Software without restriction, including without limitation the rights
|
|
9
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
// furnished to do so, subject to the following conditions:
|
|
12
|
+
//
|
|
13
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
// copies or substantial portions of the Software.
|
|
15
|
+
//
|
|
16
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
// SOFTWARE.
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.onOperation = exports.onChangedOperation = exports.makeEndpoint = exports.makeParams = exports.getOpts = exports.onValueDeleted = exports.onValueUpdated = exports.onValueCreated = exports.onValueWritten = exports.deletedEventType = exports.updatedEventType = exports.createdEventType = exports.writtenEventType = exports.DataSnapshot = void 0;
|
|
25
|
+
const apps_1 = require("../../apps");
|
|
26
|
+
const database_1 = require("../../common/providers/database");
|
|
27
|
+
Object.defineProperty(exports, "DataSnapshot", { enumerable: true, get: function () { return database_1.DataSnapshot; } });
|
|
28
|
+
const path_1 = require("../../utilities/path");
|
|
29
|
+
const path_pattern_1 = require("../../utilities/path-pattern");
|
|
30
|
+
const utils_1 = require("../../utils");
|
|
31
|
+
const options = require("../options");
|
|
32
|
+
/** @internal */
|
|
33
|
+
exports.writtenEventType = 'google.firebase.database.ref.v1.written';
|
|
34
|
+
/** @internal */
|
|
35
|
+
exports.createdEventType = 'google.firebase.database.ref.v1.created';
|
|
36
|
+
/** @internal */
|
|
37
|
+
exports.updatedEventType = 'google.firebase.database.ref.v1.updated';
|
|
38
|
+
/** @internal */
|
|
39
|
+
exports.deletedEventType = 'google.firebase.database.ref.v1.deleted';
|
|
40
|
+
/**
|
|
41
|
+
* Event handler which triggers when data is created, updated, or deleted in Realtime Database.
|
|
42
|
+
*
|
|
43
|
+
* @param referenceOrOpts - Options or a string reference.
|
|
44
|
+
* @param handler - Event handler which is run every time a Realtime Database create, update, or delete occurs.
|
|
45
|
+
*/
|
|
46
|
+
function onValueWritten(referenceOrOpts, handler) {
|
|
47
|
+
return onChangedOperation(exports.writtenEventType, referenceOrOpts, handler);
|
|
48
|
+
}
|
|
49
|
+
exports.onValueWritten = onValueWritten;
|
|
50
|
+
/**
|
|
51
|
+
* Event handler which triggers when data is created in Realtime Database.
|
|
52
|
+
*
|
|
53
|
+
* @param referenceOrOpts - Options or a string reference.
|
|
54
|
+
* @param handler - Event handler which is run every time a Realtime Database create occurs.
|
|
55
|
+
*/
|
|
56
|
+
function onValueCreated(referenceOrOpts, handler) {
|
|
57
|
+
return onOperation(exports.createdEventType, referenceOrOpts, handler);
|
|
58
|
+
}
|
|
59
|
+
exports.onValueCreated = onValueCreated;
|
|
60
|
+
/**
|
|
61
|
+
* Event handler which triggers when data is updated in Realtime Database.
|
|
62
|
+
*
|
|
63
|
+
* @param referenceOrOpts - Options or a string reference.
|
|
64
|
+
* @param handler - Event handler which is run every time a Realtime Database update occurs.
|
|
65
|
+
*/
|
|
66
|
+
function onValueUpdated(referenceOrOpts, handler) {
|
|
67
|
+
return onChangedOperation(exports.updatedEventType, referenceOrOpts, handler);
|
|
68
|
+
}
|
|
69
|
+
exports.onValueUpdated = onValueUpdated;
|
|
70
|
+
/**
|
|
71
|
+
* Event handler which triggers when data is deleted in Realtime Database.
|
|
72
|
+
*
|
|
73
|
+
* @param referenceOrOpts - Options or a string reference.
|
|
74
|
+
* @param handler - Event handler which is run every time a Realtime Database deletion occurs.
|
|
75
|
+
*/
|
|
76
|
+
function onValueDeleted(referenceOrOpts, handler) {
|
|
77
|
+
// TODO - need to use event.data.delta
|
|
78
|
+
return onOperation(exports.deletedEventType, referenceOrOpts, handler);
|
|
79
|
+
}
|
|
80
|
+
exports.onValueDeleted = onValueDeleted;
|
|
81
|
+
/** @internal */
|
|
82
|
+
function getOpts(referenceOrOpts) {
|
|
83
|
+
let path, instance, opts;
|
|
84
|
+
if (typeof referenceOrOpts === 'string') {
|
|
85
|
+
path = (0, path_1.normalizePath)(referenceOrOpts);
|
|
86
|
+
instance = '*';
|
|
87
|
+
opts = {};
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
path = (0, path_1.normalizePath)(referenceOrOpts.ref);
|
|
91
|
+
instance = referenceOrOpts.instance || '*';
|
|
92
|
+
opts = { ...referenceOrOpts };
|
|
93
|
+
delete opts.ref;
|
|
94
|
+
delete opts.instance;
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
path,
|
|
98
|
+
instance,
|
|
99
|
+
opts,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
exports.getOpts = getOpts;
|
|
103
|
+
/** @internal */
|
|
104
|
+
function makeParams(event, path, instance) {
|
|
105
|
+
return {
|
|
106
|
+
...path.extractMatches(event.ref),
|
|
107
|
+
...instance.extractMatches(event.instance),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
exports.makeParams = makeParams;
|
|
111
|
+
/** @hidden */
|
|
112
|
+
function makeDatabaseEvent(event, data, instance, params) {
|
|
113
|
+
const snapshot = new database_1.DataSnapshot(data, event.ref, (0, apps_1.apps)().admin, instance);
|
|
114
|
+
const databaseEvent = {
|
|
115
|
+
...event,
|
|
116
|
+
firebaseDatabaseHost: event.firebasedatabasehost,
|
|
117
|
+
data: snapshot,
|
|
118
|
+
params,
|
|
119
|
+
};
|
|
120
|
+
delete databaseEvent.firebasedatabasehost;
|
|
121
|
+
return databaseEvent;
|
|
122
|
+
}
|
|
123
|
+
/** @hidden */
|
|
124
|
+
function makeChangedDatabaseEvent(event, instance, params) {
|
|
125
|
+
const before = new database_1.DataSnapshot(event.data.data, event.ref, (0, apps_1.apps)().admin, instance);
|
|
126
|
+
const after = new database_1.DataSnapshot((0, utils_1.applyChange)(event.data.data, event.data.delta), event.ref, (0, apps_1.apps)().admin, instance);
|
|
127
|
+
const databaseEvent = {
|
|
128
|
+
...event,
|
|
129
|
+
firebaseDatabaseHost: event.firebasedatabasehost,
|
|
130
|
+
data: {
|
|
131
|
+
before,
|
|
132
|
+
after,
|
|
133
|
+
},
|
|
134
|
+
params,
|
|
135
|
+
};
|
|
136
|
+
delete databaseEvent.firebasedatabasehost;
|
|
137
|
+
return databaseEvent;
|
|
138
|
+
}
|
|
139
|
+
/** @internal */
|
|
140
|
+
function makeEndpoint(eventType, opts, path, instance) {
|
|
141
|
+
const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
|
|
142
|
+
const specificOpts = options.optionsToEndpoint(opts);
|
|
143
|
+
const eventFilters = {};
|
|
144
|
+
const eventFilterPathPatterns = {
|
|
145
|
+
// Note: Eventarc always treats ref as a path pattern
|
|
146
|
+
ref: path.getValue(),
|
|
147
|
+
};
|
|
148
|
+
instance.hasWildcards()
|
|
149
|
+
? (eventFilterPathPatterns.instance = instance.getValue())
|
|
150
|
+
: (eventFilters.instance = instance.getValue());
|
|
151
|
+
return {
|
|
152
|
+
platform: 'gcfv2',
|
|
153
|
+
...baseOpts,
|
|
154
|
+
...specificOpts,
|
|
155
|
+
labels: {
|
|
156
|
+
...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
|
|
157
|
+
...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
|
|
158
|
+
},
|
|
159
|
+
eventTrigger: {
|
|
160
|
+
eventType,
|
|
161
|
+
eventFilters,
|
|
162
|
+
eventFilterPathPatterns,
|
|
163
|
+
retry: false,
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
exports.makeEndpoint = makeEndpoint;
|
|
168
|
+
/** @internal */
|
|
169
|
+
function onChangedOperation(eventType, referenceOrOpts, handler) {
|
|
170
|
+
const { path, instance, opts } = getOpts(referenceOrOpts);
|
|
171
|
+
const pathPattern = new path_pattern_1.PathPattern(path);
|
|
172
|
+
const instancePattern = new path_pattern_1.PathPattern(instance);
|
|
173
|
+
// wrap the handler
|
|
174
|
+
const func = (raw) => {
|
|
175
|
+
const event = raw;
|
|
176
|
+
const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
|
|
177
|
+
const params = makeParams(event, pathPattern, instancePattern);
|
|
178
|
+
const databaseEvent = makeChangedDatabaseEvent(event, instanceUrl, params);
|
|
179
|
+
return handler(databaseEvent);
|
|
180
|
+
};
|
|
181
|
+
func.run = handler;
|
|
182
|
+
func.__endpoint = makeEndpoint(eventType, opts, pathPattern, instancePattern);
|
|
183
|
+
return func;
|
|
184
|
+
}
|
|
185
|
+
exports.onChangedOperation = onChangedOperation;
|
|
186
|
+
/** @internal */
|
|
187
|
+
function onOperation(eventType, referenceOrOpts, handler) {
|
|
188
|
+
const { path, instance, opts } = getOpts(referenceOrOpts);
|
|
189
|
+
const pathPattern = new path_pattern_1.PathPattern(path);
|
|
190
|
+
const instancePattern = new path_pattern_1.PathPattern(instance);
|
|
191
|
+
// wrap the handler
|
|
192
|
+
const func = (raw) => {
|
|
193
|
+
const event = raw;
|
|
194
|
+
const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
|
|
195
|
+
const params = makeParams(event, pathPattern, instancePattern);
|
|
196
|
+
const data = eventType === exports.deletedEventType ? event.data.data : event.data.delta;
|
|
197
|
+
const databaseEvent = makeDatabaseEvent(event, data, instanceUrl, params);
|
|
198
|
+
return handler(databaseEvent);
|
|
199
|
+
};
|
|
200
|
+
func.run = handler;
|
|
201
|
+
func.__endpoint = makeEndpoint(eventType, opts, pathPattern, instancePattern);
|
|
202
|
+
return func;
|
|
203
|
+
}
|
|
204
|
+
exports.onOperation = onOperation;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "firebase-functions",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.22.0",
|
|
4
4
|
"description": "Firebase SDK for Cloud Functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"firebase",
|
|
@@ -65,7 +65,8 @@
|
|
|
65
65
|
"./v2/alerts/billing": "./lib/v2/providers/alerts/billing.js",
|
|
66
66
|
"./v2/alerts/crashlytics": "./lib/v2/providers/alerts/crashlytics.js",
|
|
67
67
|
"./v2/eventarc": "./lib/v2/providers/eventarc.js",
|
|
68
|
-
"./v2/identity": "./lib/v2/providers/identity.js"
|
|
68
|
+
"./v2/identity": "./lib/v2/providers/identity.js",
|
|
69
|
+
"./v2/database": "./lib/v2/providers/database.js"
|
|
69
70
|
},
|
|
70
71
|
"typesVersions": {
|
|
71
72
|
"*": {
|
|
@@ -126,6 +127,9 @@
|
|
|
126
127
|
"v2/base": [
|
|
127
128
|
"lib/v2/base"
|
|
128
129
|
],
|
|
130
|
+
"v2/database": [
|
|
131
|
+
"lib/v2/providers/database"
|
|
132
|
+
],
|
|
129
133
|
"v2/eventarc": [
|
|
130
134
|
"lib/v2/providers/eventarc"
|
|
131
135
|
],
|
|
@@ -225,7 +229,7 @@
|
|
|
225
229
|
"yargs": "^15.3.1"
|
|
226
230
|
},
|
|
227
231
|
"peerDependencies": {
|
|
228
|
-
"firebase-admin": "^8.0.0 || ^9.0.0 || ^10.0.0"
|
|
232
|
+
"firebase-admin": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0"
|
|
229
233
|
},
|
|
230
234
|
"engines": {
|
|
231
235
|
"node": "^8.13.0 || >=10.10.0"
|