firebase-functions 3.21.1 → 3.23.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/bin/firebase-functions.js +2 -1
- 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 +12 -7
- package/lib/common/providers/tasks.d.ts +8 -7
- package/lib/common/providers/tasks.js +13 -12
- package/lib/common/timezone.d.ts +2 -0
- package/lib/common/timezone.js +543 -0
- package/lib/function-builder.d.ts +2 -2
- package/lib/function-builder.js +2 -2
- package/lib/function-configuration.d.ts +6 -5
- 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/runtime/loader.js +6 -1
- package/lib/runtime/manifest.d.ts +19 -16
- package/lib/runtime/manifest.js +24 -0
- 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 +4 -1
- package/lib/v2/index.js +7 -1
- package/lib/v2/options.d.ts +16 -6
- package/lib/v2/options.js +2 -2
- package/lib/v2/params/index.d.ts +14 -12
- package/lib/v2/params/index.js +25 -15
- package/lib/v2/params/types.d.ts +97 -24
- package/lib/v2/params/types.js +127 -67
- package/lib/v2/providers/alerts/alerts.d.ts +7 -6
- package/lib/v2/providers/alerts/appDistribution.d.ts +50 -5
- package/lib/v2/providers/alerts/appDistribution.js +23 -1
- package/lib/v2/providers/alerts/crashlytics.d.ts +6 -5
- package/lib/v2/providers/database.d.ts +183 -0
- package/lib/v2/providers/database.js +204 -0
- package/lib/v2/providers/eventarc.d.ts +6 -5
- package/lib/v2/providers/https.d.ts +6 -5
- package/lib/v2/providers/identity.d.ts +8 -7
- package/lib/v2/providers/pubsub.d.ts +6 -5
- package/lib/v2/providers/scheduler.d.ts +63 -0
- package/lib/v2/providers/scheduler.js +98 -0
- package/lib/v2/providers/storage.d.ts +6 -5
- package/lib/v2/providers/tasks.d.ts +7 -6
- package/package.json +18 -8
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
const express = require("express");
|
|
26
26
|
const loader_1 = require("../runtime/loader");
|
|
27
|
+
const manifest_1 = require("../runtime/manifest");
|
|
27
28
|
function printUsageAndExit() {
|
|
28
29
|
console.error(`
|
|
29
30
|
Usage: firebase-functions [functionsDir]
|
|
@@ -54,7 +55,7 @@ if (process.env.FUNCTIONS_CONTROL_API === 'true') {
|
|
|
54
55
|
try {
|
|
55
56
|
const stack = await (0, loader_1.loadStack)(functionsDir);
|
|
56
57
|
res.setHeader('content-type', 'text/yaml');
|
|
57
|
-
res.send(JSON.stringify(stack));
|
|
58
|
+
res.send(JSON.stringify((0, manifest_1.stackToWire)(stack)));
|
|
58
59
|
}
|
|
59
60
|
catch (e) {
|
|
60
61
|
res
|
package/lib/cloud-functions.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { DeploymentOptions, FailurePolicy, Schedule } from './function-configura
|
|
|
3
3
|
export { Request, Response };
|
|
4
4
|
import { Duration } from './common/encoding';
|
|
5
5
|
import { ManifestEndpoint, ManifestRequiredAPI } from './runtime/manifest';
|
|
6
|
+
export { Change } from './common/change';
|
|
6
7
|
/**
|
|
7
8
|
* @hidden
|
|
8
9
|
*
|
|
@@ -111,54 +112,6 @@ export interface EventContext {
|
|
|
111
112
|
*/
|
|
112
113
|
timestamp: string;
|
|
113
114
|
}
|
|
114
|
-
/**
|
|
115
|
-
* The Functions interface for events that change state, such as
|
|
116
|
-
* Realtime Database or Cloud Firestore `onWrite` and `onUpdate`.
|
|
117
|
-
*
|
|
118
|
-
* For more information about the format used to construct `Change` objects, see
|
|
119
|
-
* [`cloud-functions.ChangeJson`](/docs/reference/functions/cloud_functions_.changejson).
|
|
120
|
-
*
|
|
121
|
-
*/
|
|
122
|
-
export declare class Change<T> {
|
|
123
|
-
before: T;
|
|
124
|
-
after: T;
|
|
125
|
-
constructor(before: T, after: T);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* `ChangeJson` is the JSON format used to construct a Change object.
|
|
129
|
-
*/
|
|
130
|
-
export interface ChangeJson {
|
|
131
|
-
/**
|
|
132
|
-
* Key-value pairs representing state of data after the change.
|
|
133
|
-
*/
|
|
134
|
-
after?: any;
|
|
135
|
-
/**
|
|
136
|
-
* Key-value pairs representing state of data before the change. If
|
|
137
|
-
* `fieldMask` is set, then only fields that changed are present in `before`.
|
|
138
|
-
*/
|
|
139
|
-
before?: any;
|
|
140
|
-
/**
|
|
141
|
-
* @hidden
|
|
142
|
-
* Comma-separated string that represents names of fields that changed.
|
|
143
|
-
*/
|
|
144
|
-
fieldMask?: string;
|
|
145
|
-
}
|
|
146
|
-
export declare namespace Change {
|
|
147
|
-
/**
|
|
148
|
-
* @hidden
|
|
149
|
-
* Factory method for creating a Change from a `before` object and an `after`
|
|
150
|
-
* object.
|
|
151
|
-
*/
|
|
152
|
-
function fromObjects<T>(before: T, after: T): Change<T>;
|
|
153
|
-
/**
|
|
154
|
-
* @hidden
|
|
155
|
-
* Factory method for creating a Change from a JSON and an optional customizer
|
|
156
|
-
* function to be applied to both the `before` and the `after` fields.
|
|
157
|
-
*/
|
|
158
|
-
function fromJSON<T>(json: ChangeJson, customizer?: (x: any) => T): Change<T>;
|
|
159
|
-
/** @hidden */
|
|
160
|
-
function applyFieldMask(sparseBefore: any, after: any, fieldMask: string): any;
|
|
161
|
-
}
|
|
162
115
|
/**
|
|
163
116
|
* Resource is a standard format for defining a resource
|
|
164
117
|
* (google.rpc.context.AttributeContext.Resource). In Cloud Functions, it is the
|
package/lib/cloud-functions.js
CHANGED
|
@@ -26,67 +26,10 @@ const _ = require("lodash");
|
|
|
26
26
|
const function_configuration_1 = require("./function-configuration");
|
|
27
27
|
const logger_1 = require("./logger");
|
|
28
28
|
const encoding_1 = require("./common/encoding");
|
|
29
|
+
var change_1 = require("./common/change");
|
|
30
|
+
Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return change_1.Change; } });
|
|
29
31
|
/** @hidden */
|
|
30
32
|
const WILDCARD_REGEX = new RegExp('{[^/{}]*}', 'g');
|
|
31
|
-
/**
|
|
32
|
-
* The Functions interface for events that change state, such as
|
|
33
|
-
* Realtime Database or Cloud Firestore `onWrite` and `onUpdate`.
|
|
34
|
-
*
|
|
35
|
-
* For more information about the format used to construct `Change` objects, see
|
|
36
|
-
* [`cloud-functions.ChangeJson`](/docs/reference/functions/cloud_functions_.changejson).
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
class Change {
|
|
40
|
-
constructor(before, after) {
|
|
41
|
-
this.before = before;
|
|
42
|
-
this.after = after;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.Change = Change;
|
|
46
|
-
(function (Change) {
|
|
47
|
-
/** @hidden */
|
|
48
|
-
function reinterpretCast(x) {
|
|
49
|
-
return x;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @hidden
|
|
53
|
-
* Factory method for creating a Change from a `before` object and an `after`
|
|
54
|
-
* object.
|
|
55
|
-
*/
|
|
56
|
-
function fromObjects(before, after) {
|
|
57
|
-
return new Change(before, after);
|
|
58
|
-
}
|
|
59
|
-
Change.fromObjects = fromObjects;
|
|
60
|
-
/**
|
|
61
|
-
* @hidden
|
|
62
|
-
* Factory method for creating a Change from a JSON and an optional customizer
|
|
63
|
-
* function to be applied to both the `before` and the `after` fields.
|
|
64
|
-
*/
|
|
65
|
-
function fromJSON(json, customizer = reinterpretCast) {
|
|
66
|
-
let before = { ...json.before };
|
|
67
|
-
if (json.fieldMask) {
|
|
68
|
-
before = applyFieldMask(before, json.after, json.fieldMask);
|
|
69
|
-
}
|
|
70
|
-
return Change.fromObjects(customizer(before || {}), customizer(json.after || {}));
|
|
71
|
-
}
|
|
72
|
-
Change.fromJSON = fromJSON;
|
|
73
|
-
/** @hidden */
|
|
74
|
-
function applyFieldMask(sparseBefore, after, fieldMask) {
|
|
75
|
-
const before = { ...after };
|
|
76
|
-
const masks = fieldMask.split(',');
|
|
77
|
-
masks.forEach((mask) => {
|
|
78
|
-
const val = _.get(sparseBefore, mask);
|
|
79
|
-
if (typeof val === 'undefined') {
|
|
80
|
-
_.unset(before, mask);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
_.set(before, mask, val);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
return before;
|
|
87
|
-
}
|
|
88
|
-
Change.applyFieldMask = applyFieldMask;
|
|
89
|
-
})(Change = exports.Change || (exports.Change = {}));
|
|
90
33
|
/** @hidden */
|
|
91
34
|
function makeCloudFunction({ after = () => { }, before = () => { }, contextOnlyHandler, dataConstructor = (raw) => raw.data, eventType, handler, labels = {}, legacyEventType, options = {}, provider, service, triggerResource, }) {
|
|
92
35
|
const cloudFunction = (data, context) => {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ChangeJson` is the JSON format used to construct a Change object.
|
|
3
|
+
*/
|
|
4
|
+
export interface ChangeJson {
|
|
5
|
+
/**
|
|
6
|
+
* Key-value pairs representing state of data after the change.
|
|
7
|
+
*/
|
|
8
|
+
after?: any;
|
|
9
|
+
/**
|
|
10
|
+
* Key-value pairs representing state of data before the change. If
|
|
11
|
+
* `fieldMask` is set, then only fields that changed are present in `before`.
|
|
12
|
+
*/
|
|
13
|
+
before?: any;
|
|
14
|
+
/**
|
|
15
|
+
* @hidden
|
|
16
|
+
* Comma-separated string that represents names of fields that changed.
|
|
17
|
+
*/
|
|
18
|
+
fieldMask?: string;
|
|
19
|
+
}
|
|
20
|
+
/** @hidden */
|
|
21
|
+
export declare function applyFieldMask(sparseBefore: any, after: any, fieldMask: string): any;
|
|
22
|
+
/**
|
|
23
|
+
* The Functions interface for events that change state, such as
|
|
24
|
+
* Realtime Database or Cloud Firestore `onWrite` and `onUpdate`.
|
|
25
|
+
*
|
|
26
|
+
* For more information about the format used to construct `Change` objects, see
|
|
27
|
+
* {@link ChangeJson} below.
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
export declare class Change<T> {
|
|
31
|
+
before: T;
|
|
32
|
+
after: T;
|
|
33
|
+
/**
|
|
34
|
+
* @hidden
|
|
35
|
+
* Factory method for creating a Change from a `before` object and an `after`
|
|
36
|
+
* object.
|
|
37
|
+
*/
|
|
38
|
+
static fromObjects<T>(before: T, after: T): Change<T>;
|
|
39
|
+
/**
|
|
40
|
+
* @hidden
|
|
41
|
+
* Factory method for creating a Change from a JSON and an optional customizer
|
|
42
|
+
* function to be applied to both the `before` and the `after` fields.
|
|
43
|
+
*/
|
|
44
|
+
static fromJSON<T>(json: ChangeJson, customizer?: (x: any) => T): Change<T>;
|
|
45
|
+
constructor(before: T, after: T);
|
|
46
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
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.Change = exports.applyFieldMask = void 0;
|
|
25
|
+
/** @hidden */
|
|
26
|
+
function applyFieldMask(sparseBefore, after, fieldMask) {
|
|
27
|
+
const before = { ...after };
|
|
28
|
+
const masks = fieldMask.split(',');
|
|
29
|
+
for (const mask of masks) {
|
|
30
|
+
const parts = mask.split('.');
|
|
31
|
+
const head = parts[0];
|
|
32
|
+
const tail = parts.slice(1).join('.');
|
|
33
|
+
if (parts.length > 1) {
|
|
34
|
+
before[head] = applyFieldMask(sparseBefore === null || sparseBefore === void 0 ? void 0 : sparseBefore[head], after[head], tail);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const val = sparseBefore === null || sparseBefore === void 0 ? void 0 : sparseBefore[head];
|
|
38
|
+
if (typeof val === 'undefined') {
|
|
39
|
+
delete before[mask];
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
before[mask] = val;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return before;
|
|
46
|
+
}
|
|
47
|
+
exports.applyFieldMask = applyFieldMask;
|
|
48
|
+
/**
|
|
49
|
+
* The Functions interface for events that change state, such as
|
|
50
|
+
* Realtime Database or Cloud Firestore `onWrite` and `onUpdate`.
|
|
51
|
+
*
|
|
52
|
+
* For more information about the format used to construct `Change` objects, see
|
|
53
|
+
* {@link ChangeJson} below.
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
class Change {
|
|
57
|
+
constructor(before, after) {
|
|
58
|
+
this.before = before;
|
|
59
|
+
this.after = after;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @hidden
|
|
63
|
+
* Factory method for creating a Change from a `before` object and an `after`
|
|
64
|
+
* object.
|
|
65
|
+
*/
|
|
66
|
+
static fromObjects(before, after) {
|
|
67
|
+
return new Change(before, after);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* @hidden
|
|
71
|
+
* Factory method for creating a Change from a JSON and an optional customizer
|
|
72
|
+
* function to be applied to both the `before` and the `after` fields.
|
|
73
|
+
*/
|
|
74
|
+
static fromJSON(json, customizer = (x) => x) {
|
|
75
|
+
let before = { ...json.before };
|
|
76
|
+
if (json.fieldMask) {
|
|
77
|
+
before = applyFieldMask(before, json.after, json.fieldMask);
|
|
78
|
+
}
|
|
79
|
+
return Change.fromObjects(customizer(before || {}), customizer(json.after || {}));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.Change = Change;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as firebase from 'firebase-admin';
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing a Firebase Realtime Database data snapshot.
|
|
4
|
+
*/
|
|
5
|
+
export declare class DataSnapshot {
|
|
6
|
+
private app?;
|
|
7
|
+
instance: string;
|
|
8
|
+
/** @hidden */
|
|
9
|
+
private _ref;
|
|
10
|
+
/** @hidden */
|
|
11
|
+
private _path;
|
|
12
|
+
/** @hidden */
|
|
13
|
+
private _data;
|
|
14
|
+
/** @hidden */
|
|
15
|
+
private _childPath;
|
|
16
|
+
constructor(data: any, path?: string, // path will be undefined for the database root
|
|
17
|
+
app?: firebase.app.App, instance?: string);
|
|
18
|
+
/**
|
|
19
|
+
* Returns a [`Reference`](/docs/reference/admin/node/admin.database.Reference)
|
|
20
|
+
* to the Database location where the triggering write occurred. Has
|
|
21
|
+
* full read and write access.
|
|
22
|
+
*/
|
|
23
|
+
get ref(): firebase.database.Reference;
|
|
24
|
+
/**
|
|
25
|
+
* The key (last part of the path) of the location of this `DataSnapshot`.
|
|
26
|
+
*
|
|
27
|
+
* The last token in a Database location is considered its key. For example,
|
|
28
|
+
* "ada" is the key for the `/users/ada/` node. Accessing the key on any
|
|
29
|
+
* `DataSnapshot` will return the key for the location that generated it.
|
|
30
|
+
* However, accessing the key on the root URL of a Database will return `null`.
|
|
31
|
+
*/
|
|
32
|
+
get key(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Extracts a JavaScript value from a `DataSnapshot`.
|
|
35
|
+
*
|
|
36
|
+
* Depending on the data in a `DataSnapshot`, the `val()` method may return a
|
|
37
|
+
* scalar type (string, number, or boolean), an array, or an object. It may also
|
|
38
|
+
* return `null`, indicating that the `DataSnapshot` is empty (contains no
|
|
39
|
+
* data).
|
|
40
|
+
*
|
|
41
|
+
* @return The DataSnapshot's contents as a JavaScript value (Object,
|
|
42
|
+
* Array, string, number, boolean, or `null`).
|
|
43
|
+
*/
|
|
44
|
+
val(): any;
|
|
45
|
+
/**
|
|
46
|
+
* Exports the entire contents of the `DataSnapshot` as a JavaScript object.
|
|
47
|
+
*
|
|
48
|
+
* The `exportVal()` method is similar to `val()`, except priority information
|
|
49
|
+
* is included (if available), making it suitable for backing up your data.
|
|
50
|
+
*
|
|
51
|
+
* @return The contents of the `DataSnapshot` as a JavaScript value
|
|
52
|
+
* (Object, Array, string, number, boolean, or `null`).
|
|
53
|
+
*/
|
|
54
|
+
exportVal(): any;
|
|
55
|
+
/**
|
|
56
|
+
* Gets the priority value of the data in this `DataSnapshot`.
|
|
57
|
+
*
|
|
58
|
+
* As an alternative to using priority, applications can order collections by
|
|
59
|
+
* ordinary properties. See [Sorting and filtering
|
|
60
|
+
* data](/docs/database/web/lists-of-data#sorting_and_filtering_data).
|
|
61
|
+
*
|
|
62
|
+
* @return The priority value of the data.
|
|
63
|
+
*/
|
|
64
|
+
getPriority(): string | number | null;
|
|
65
|
+
/**
|
|
66
|
+
* Returns `true` if this `DataSnapshot` contains any data. It is slightly more
|
|
67
|
+
* efficient than using `snapshot.val() !== null`.
|
|
68
|
+
*
|
|
69
|
+
* @return `true` if this `DataSnapshot` contains any data; otherwise, `false`.
|
|
70
|
+
*/
|
|
71
|
+
exists(): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Gets a `DataSnapshot` for the location at the specified relative path.
|
|
74
|
+
*
|
|
75
|
+
* The relative path can either be a simple child name (for example, "ada") or
|
|
76
|
+
* a deeper slash-separated path (for example, "ada/name/first").
|
|
77
|
+
*
|
|
78
|
+
* @param path A relative path from this location to the desired child
|
|
79
|
+
* location.
|
|
80
|
+
* @return The specified child location.
|
|
81
|
+
*/
|
|
82
|
+
child(childPath: string): DataSnapshot;
|
|
83
|
+
/**
|
|
84
|
+
* Enumerates the `DataSnapshot`s of the children items.
|
|
85
|
+
*
|
|
86
|
+
* Because of the way JavaScript objects work, the ordering of data in the
|
|
87
|
+
* JavaScript object returned by `val()` is not guaranteed to match the ordering
|
|
88
|
+
* on the server nor the ordering of `child_added` events. That is where
|
|
89
|
+
* `forEach()` comes in handy. It guarantees the children of a `DataSnapshot`
|
|
90
|
+
* will be iterated in their query order.
|
|
91
|
+
*
|
|
92
|
+
* If no explicit `orderBy*()` method is used, results are returned
|
|
93
|
+
* ordered by key (unless priorities are used, in which case, results are
|
|
94
|
+
* returned by priority).
|
|
95
|
+
*
|
|
96
|
+
* @param action A function that will be called for each child `DataSnapshot`.
|
|
97
|
+
* The callback can return `true` to cancel further enumeration.
|
|
98
|
+
*
|
|
99
|
+
* @return `true` if enumeration was canceled due to your callback
|
|
100
|
+
* returning `true`.
|
|
101
|
+
*/
|
|
102
|
+
forEach(action: (a: DataSnapshot) => boolean | void): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Returns `true` if the specified child path has (non-`null`) data.
|
|
105
|
+
*
|
|
106
|
+
* @param path A relative path to the location of a potential child.
|
|
107
|
+
* @return `true` if data exists at the specified child path; otherwise,
|
|
108
|
+
* `false`.
|
|
109
|
+
*/
|
|
110
|
+
hasChild(childPath: string): boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Returns whether or not the `DataSnapshot` has any non-`null` child
|
|
113
|
+
* properties.
|
|
114
|
+
*
|
|
115
|
+
* You can use `hasChildren()` to determine if a `DataSnapshot` has any
|
|
116
|
+
* children. If it does, you can enumerate them using `forEach()`. If it
|
|
117
|
+
* doesn't, then either this snapshot contains a primitive value (which can be
|
|
118
|
+
* retrieved with `val()`) or it is empty (in which case, `val()` will return
|
|
119
|
+
* `null`).
|
|
120
|
+
*
|
|
121
|
+
* @return `true` if this snapshot has any children; else `false`.
|
|
122
|
+
*/
|
|
123
|
+
hasChildren(): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Returns the number of child properties of this `DataSnapshot`.
|
|
126
|
+
*
|
|
127
|
+
* @return Number of child properties of this `DataSnapshot`.
|
|
128
|
+
*/
|
|
129
|
+
numChildren(): number;
|
|
130
|
+
/**
|
|
131
|
+
* Returns a JSON-serializable representation of this object.
|
|
132
|
+
*
|
|
133
|
+
* @return A JSON-serializable representation of this object.
|
|
134
|
+
*/
|
|
135
|
+
toJSON(): Object;
|
|
136
|
+
/** Recursive function to check if keys are numeric & convert node object to array if they are
|
|
137
|
+
*
|
|
138
|
+
* @hidden
|
|
139
|
+
*/
|
|
140
|
+
private _checkAndConvertToArray;
|
|
141
|
+
/** @hidden */
|
|
142
|
+
private _dup;
|
|
143
|
+
/** @hidden */
|
|
144
|
+
private _fullPath;
|
|
145
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
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.DataSnapshot = void 0;
|
|
25
|
+
const _ = require("lodash");
|
|
26
|
+
const path_1 = require("../../utilities/path");
|
|
27
|
+
/**
|
|
28
|
+
* Interface representing a Firebase Realtime Database data snapshot.
|
|
29
|
+
*/
|
|
30
|
+
class DataSnapshot {
|
|
31
|
+
constructor(data, path, // path will be undefined for the database root
|
|
32
|
+
app, instance) {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
this.app = app;
|
|
35
|
+
if ((_b = (_a = app === null || app === void 0 ? void 0 : app.options) === null || _a === void 0 ? void 0 : _a.databaseURL) === null || _b === void 0 ? void 0 : _b.startsWith('http:')) {
|
|
36
|
+
// In this case we're dealing with an emulator
|
|
37
|
+
this.instance = app.options.databaseURL;
|
|
38
|
+
}
|
|
39
|
+
else if (instance) {
|
|
40
|
+
// SDK always supplies instance, but user's unit tests may not
|
|
41
|
+
this.instance = instance;
|
|
42
|
+
}
|
|
43
|
+
else if (app) {
|
|
44
|
+
this.instance = app.options.databaseURL;
|
|
45
|
+
}
|
|
46
|
+
else if (process.env.GCLOUD_PROJECT) {
|
|
47
|
+
this.instance =
|
|
48
|
+
'https://' + process.env.GCLOUD_PROJECT + '.firebaseio.com';
|
|
49
|
+
}
|
|
50
|
+
this._path = path;
|
|
51
|
+
this._data = data;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns a [`Reference`](/docs/reference/admin/node/admin.database.Reference)
|
|
55
|
+
* to the Database location where the triggering write occurred. Has
|
|
56
|
+
* full read and write access.
|
|
57
|
+
*/
|
|
58
|
+
get ref() {
|
|
59
|
+
if (!this.app) {
|
|
60
|
+
// may be unpopulated in user's unit tests
|
|
61
|
+
throw new Error('Please supply a Firebase app in the constructor for DataSnapshot' +
|
|
62
|
+
' in order to use the .ref method.');
|
|
63
|
+
}
|
|
64
|
+
if (!this._ref) {
|
|
65
|
+
this._ref = this.app.database(this.instance).ref(this._fullPath());
|
|
66
|
+
}
|
|
67
|
+
return this._ref;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The key (last part of the path) of the location of this `DataSnapshot`.
|
|
71
|
+
*
|
|
72
|
+
* The last token in a Database location is considered its key. For example,
|
|
73
|
+
* "ada" is the key for the `/users/ada/` node. Accessing the key on any
|
|
74
|
+
* `DataSnapshot` will return the key for the location that generated it.
|
|
75
|
+
* However, accessing the key on the root URL of a Database will return `null`.
|
|
76
|
+
*/
|
|
77
|
+
get key() {
|
|
78
|
+
const last = _.last((0, path_1.pathParts)(this._fullPath()));
|
|
79
|
+
return !last || last === '' ? null : last;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Extracts a JavaScript value from a `DataSnapshot`.
|
|
83
|
+
*
|
|
84
|
+
* Depending on the data in a `DataSnapshot`, the `val()` method may return a
|
|
85
|
+
* scalar type (string, number, or boolean), an array, or an object. It may also
|
|
86
|
+
* return `null`, indicating that the `DataSnapshot` is empty (contains no
|
|
87
|
+
* data).
|
|
88
|
+
*
|
|
89
|
+
* @return The DataSnapshot's contents as a JavaScript value (Object,
|
|
90
|
+
* Array, string, number, boolean, or `null`).
|
|
91
|
+
*/
|
|
92
|
+
val() {
|
|
93
|
+
const parts = (0, path_1.pathParts)(this._childPath);
|
|
94
|
+
const source = this._data;
|
|
95
|
+
const node = _.cloneDeep(parts.length ? _.get(source, parts, null) : source);
|
|
96
|
+
return this._checkAndConvertToArray(node);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Exports the entire contents of the `DataSnapshot` as a JavaScript object.
|
|
100
|
+
*
|
|
101
|
+
* The `exportVal()` method is similar to `val()`, except priority information
|
|
102
|
+
* is included (if available), making it suitable for backing up your data.
|
|
103
|
+
*
|
|
104
|
+
* @return The contents of the `DataSnapshot` as a JavaScript value
|
|
105
|
+
* (Object, Array, string, number, boolean, or `null`).
|
|
106
|
+
*/
|
|
107
|
+
exportVal() {
|
|
108
|
+
return this.val();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Gets the priority value of the data in this `DataSnapshot`.
|
|
112
|
+
*
|
|
113
|
+
* As an alternative to using priority, applications can order collections by
|
|
114
|
+
* ordinary properties. See [Sorting and filtering
|
|
115
|
+
* data](/docs/database/web/lists-of-data#sorting_and_filtering_data).
|
|
116
|
+
*
|
|
117
|
+
* @return The priority value of the data.
|
|
118
|
+
*/
|
|
119
|
+
getPriority() {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns `true` if this `DataSnapshot` contains any data. It is slightly more
|
|
124
|
+
* efficient than using `snapshot.val() !== null`.
|
|
125
|
+
*
|
|
126
|
+
* @return `true` if this `DataSnapshot` contains any data; otherwise, `false`.
|
|
127
|
+
*/
|
|
128
|
+
exists() {
|
|
129
|
+
return !_.isNull(this.val());
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Gets a `DataSnapshot` for the location at the specified relative path.
|
|
133
|
+
*
|
|
134
|
+
* The relative path can either be a simple child name (for example, "ada") or
|
|
135
|
+
* a deeper slash-separated path (for example, "ada/name/first").
|
|
136
|
+
*
|
|
137
|
+
* @param path A relative path from this location to the desired child
|
|
138
|
+
* location.
|
|
139
|
+
* @return The specified child location.
|
|
140
|
+
*/
|
|
141
|
+
child(childPath) {
|
|
142
|
+
if (!childPath) {
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
return this._dup(childPath);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Enumerates the `DataSnapshot`s of the children items.
|
|
149
|
+
*
|
|
150
|
+
* Because of the way JavaScript objects work, the ordering of data in the
|
|
151
|
+
* JavaScript object returned by `val()` is not guaranteed to match the ordering
|
|
152
|
+
* on the server nor the ordering of `child_added` events. That is where
|
|
153
|
+
* `forEach()` comes in handy. It guarantees the children of a `DataSnapshot`
|
|
154
|
+
* will be iterated in their query order.
|
|
155
|
+
*
|
|
156
|
+
* If no explicit `orderBy*()` method is used, results are returned
|
|
157
|
+
* ordered by key (unless priorities are used, in which case, results are
|
|
158
|
+
* returned by priority).
|
|
159
|
+
*
|
|
160
|
+
* @param action A function that will be called for each child `DataSnapshot`.
|
|
161
|
+
* The callback can return `true` to cancel further enumeration.
|
|
162
|
+
*
|
|
163
|
+
* @return `true` if enumeration was canceled due to your callback
|
|
164
|
+
* returning `true`.
|
|
165
|
+
*/
|
|
166
|
+
forEach(action) {
|
|
167
|
+
const val = this.val();
|
|
168
|
+
if (_.isPlainObject(val)) {
|
|
169
|
+
return _.some(val, (value, key) => action(this.child(key)) === true);
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Returns `true` if the specified child path has (non-`null`) data.
|
|
175
|
+
*
|
|
176
|
+
* @param path A relative path to the location of a potential child.
|
|
177
|
+
* @return `true` if data exists at the specified child path; otherwise,
|
|
178
|
+
* `false`.
|
|
179
|
+
*/
|
|
180
|
+
hasChild(childPath) {
|
|
181
|
+
return this.child(childPath).exists();
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns whether or not the `DataSnapshot` has any non-`null` child
|
|
185
|
+
* properties.
|
|
186
|
+
*
|
|
187
|
+
* You can use `hasChildren()` to determine if a `DataSnapshot` has any
|
|
188
|
+
* children. If it does, you can enumerate them using `forEach()`. If it
|
|
189
|
+
* doesn't, then either this snapshot contains a primitive value (which can be
|
|
190
|
+
* retrieved with `val()`) or it is empty (in which case, `val()` will return
|
|
191
|
+
* `null`).
|
|
192
|
+
*
|
|
193
|
+
* @return `true` if this snapshot has any children; else `false`.
|
|
194
|
+
*/
|
|
195
|
+
hasChildren() {
|
|
196
|
+
const val = this.val();
|
|
197
|
+
return _.isPlainObject(val) && _.keys(val).length > 0;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Returns the number of child properties of this `DataSnapshot`.
|
|
201
|
+
*
|
|
202
|
+
* @return Number of child properties of this `DataSnapshot`.
|
|
203
|
+
*/
|
|
204
|
+
numChildren() {
|
|
205
|
+
const val = this.val();
|
|
206
|
+
return _.isPlainObject(val) ? Object.keys(val).length : 0;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Returns a JSON-serializable representation of this object.
|
|
210
|
+
*
|
|
211
|
+
* @return A JSON-serializable representation of this object.
|
|
212
|
+
*/
|
|
213
|
+
toJSON() {
|
|
214
|
+
return this.val();
|
|
215
|
+
}
|
|
216
|
+
/** Recursive function to check if keys are numeric & convert node object to array if they are
|
|
217
|
+
*
|
|
218
|
+
* @hidden
|
|
219
|
+
*/
|
|
220
|
+
_checkAndConvertToArray(node) {
|
|
221
|
+
if (node === null || typeof node === 'undefined') {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
if (typeof node !== 'object') {
|
|
225
|
+
return node;
|
|
226
|
+
}
|
|
227
|
+
const obj = {};
|
|
228
|
+
let numKeys = 0;
|
|
229
|
+
let maxKey = 0;
|
|
230
|
+
let allIntegerKeys = true;
|
|
231
|
+
for (const key in node) {
|
|
232
|
+
if (!node.hasOwnProperty(key)) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const childNode = node[key];
|
|
236
|
+
obj[key] = this._checkAndConvertToArray(childNode);
|
|
237
|
+
numKeys++;
|
|
238
|
+
const integerRegExp = /^(0|[1-9]\d*)$/;
|
|
239
|
+
if (allIntegerKeys && integerRegExp.test(key)) {
|
|
240
|
+
maxKey = Math.max(maxKey, Number(key));
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
allIntegerKeys = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (allIntegerKeys && maxKey < 2 * numKeys) {
|
|
247
|
+
// convert to array.
|
|
248
|
+
const array = [];
|
|
249
|
+
_.forOwn(obj, (val, key) => {
|
|
250
|
+
array[key] = val;
|
|
251
|
+
});
|
|
252
|
+
return array;
|
|
253
|
+
}
|
|
254
|
+
return obj;
|
|
255
|
+
}
|
|
256
|
+
/** @hidden */
|
|
257
|
+
_dup(childPath) {
|
|
258
|
+
const dup = new DataSnapshot(this._data, undefined, this.app, this.instance);
|
|
259
|
+
[dup._path, dup._childPath] = [this._path, this._childPath];
|
|
260
|
+
if (childPath) {
|
|
261
|
+
dup._childPath = (0, path_1.joinPath)(dup._childPath, childPath);
|
|
262
|
+
}
|
|
263
|
+
return dup;
|
|
264
|
+
}
|
|
265
|
+
/** @hidden */
|
|
266
|
+
_fullPath() {
|
|
267
|
+
const out = (this._path || '') + '/' + (this._childPath || '');
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
exports.DataSnapshot = DataSnapshot;
|