@firebase-function-kits/firestore-bigquery-export 0.0.2-rc.11 → 0.0.2-rc.13
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/CHANGELOG.md +3 -0
- package/README.md +176 -49
- package/lib/config.d.ts +9 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +40 -2
- package/lib/config.js.map +1 -1
- package/lib/events.d.ts +8 -8
- package/lib/events.d.ts.map +1 -1
- package/lib/events.js +32 -19
- package/lib/events.js.map +1 -1
- package/lib/handlers.d.ts.map +1 -1
- package/lib/handlers.js +20 -35
- package/lib/handlers.js.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +13 -2
- package/lib/index.js.map +1 -1
- package/lib/region.d.ts +4 -2
- package/lib/region.d.ts.map +1 -1
- package/lib/region.js +7 -4
- package/lib/region.js.map +1 -1
- package/npm-shrinkwrap.json +379 -85
- package/package.json +3 -3
- package/src/config.ts +46 -2
- package/src/events.ts +36 -23
- package/src/handlers.ts +20 -33
- package/src/index.ts +18 -3
- package/src/region.ts +7 -4
- package/tests/eventarc-outage.test.ts +119 -0
- package/tests/events.test.ts +49 -7
- package/tests/handlers.test.ts +0 -14
- package/tests/region.test.ts +18 -0
- package/tests/required-apis.test.ts +6 -2
- package/tests/required-params.test.ts +138 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firebase-function-kits/firestore-bigquery-export",
|
|
3
|
-
"version": "0.0.2-rc.
|
|
3
|
+
"version": "0.0.2-rc.13",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/firebase/extensions.git",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@firebaseextensions/firestore-bigquery-change-tracker": "^2.2.1",
|
|
35
35
|
"@google-cloud/bigquery": "^7.6.0",
|
|
36
|
-
"firebase-admin": "^14.
|
|
37
|
-
"firebase-functions": "^7.
|
|
36
|
+
"firebase-admin": "^14.4.0",
|
|
37
|
+
"firebase-functions": "^7.4.0",
|
|
38
38
|
"generate-schema": "^2.6.0",
|
|
39
39
|
"lodash": "^4.17.14"
|
|
40
40
|
},
|
package/src/config.ts
CHANGED
|
@@ -129,8 +129,9 @@ const params = {
|
|
|
129
129
|
input: { text: { example: "(default)" } },
|
|
130
130
|
}),
|
|
131
131
|
// Declared so the CLI prompts for the value and persists it to `.env`; the
|
|
132
|
-
//
|
|
133
|
-
// reads the same variable from
|
|
132
|
+
// location to Cloud Run region lookup needs a nested ternary the CLI's CEL
|
|
133
|
+
// subset cannot express, so the entry point reads the same variable from
|
|
134
|
+
// `process.env` at module load instead.
|
|
134
135
|
databaseRegion: defineString("DATABASE_REGION", {
|
|
135
136
|
label: "Firestore Instance Location",
|
|
136
137
|
description:
|
|
@@ -705,3 +706,46 @@ export function configFromEnv(): ExportConfig {
|
|
|
705
706
|
maxEnqueueAttempts: optionalInt(params.maxEnqueueAttempts),
|
|
706
707
|
};
|
|
707
708
|
}
|
|
709
|
+
|
|
710
|
+
// Params the published extension marks `required: true`. A value the user never
|
|
711
|
+
// supplied is absent from process.env; one they deliberately blanked is present
|
|
712
|
+
// and empty. Only the second is a misconfiguration, so the guard below reads
|
|
713
|
+
// process.env rather than `.value()`, which reports both as "".
|
|
714
|
+
// DATABASE_REGION is `required: true` upstream but deliberately omitted here:
|
|
715
|
+
// an explicit empty value is a supported setting that leaves the function
|
|
716
|
+
// without a declared region, so the CLI resolves one at deploy time.
|
|
717
|
+
const REQUIRED_PARAMS = [
|
|
718
|
+
"DATASET_LOCATION",
|
|
719
|
+
"BIGQUERY_PROJECT_ID",
|
|
720
|
+
"DATABASE",
|
|
721
|
+
"COLLECTION_PATH",
|
|
722
|
+
"DATASET_ID",
|
|
723
|
+
"TABLE_ID",
|
|
724
|
+
"VIEW_TYPE",
|
|
725
|
+
"USE_NEW_SNAPSHOT_QUERY_SYNTAX",
|
|
726
|
+
"LOG_LEVEL",
|
|
727
|
+
] as const;
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* Rejects required params that were explicitly set to an empty value.
|
|
731
|
+
*
|
|
732
|
+
* `.env` values bypass the CLI's prompt-time validation and take precedence
|
|
733
|
+
* over a param's declared default, so an empty entry otherwise reaches the
|
|
734
|
+
* handlers silently. Called at module scope so deploy-time discovery fails
|
|
735
|
+
* before the function ships, rather than on the first event.
|
|
736
|
+
*/
|
|
737
|
+
export function assertRequiredParams(
|
|
738
|
+
names: ReadonlyArray<string> = REQUIRED_PARAMS
|
|
739
|
+
): void {
|
|
740
|
+
const blank = names.filter((name) => {
|
|
741
|
+
const raw = process.env[name];
|
|
742
|
+
return raw !== undefined && raw.trim() === "";
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
if (blank.length > 0) {
|
|
746
|
+
throw new Error(
|
|
747
|
+
`Required parameters are set to an empty value: ${blank.join(", ")}. ` +
|
|
748
|
+
"Set them in your .env file, or remove the entries to use their defaults."
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
}
|
package/src/events.ts
CHANGED
|
@@ -14,17 +14,22 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import * as eventArc from "firebase-admin/eventarc";
|
|
17
|
+
import { logger } from "firebase-functions";
|
|
17
18
|
|
|
18
19
|
const { getEventarc } = eventArc;
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
|
-
*
|
|
22
|
+
* Generates both the OLD and NEW event types to maintain backward compatibility.
|
|
23
|
+
*
|
|
24
|
+
* Old Event Type: firebase.extensions.firestore-counter.v1.{eventName}
|
|
25
|
+
* New Event Type: firebase.extensions.firestore-bigquery-export.v1.{eventName}
|
|
22
26
|
*
|
|
23
27
|
* @param eventName The name of the event (e.g., "onStart", "onError", etc.)
|
|
24
|
-
* @returns
|
|
28
|
+
* @returns An array containing both the old and new event types
|
|
25
29
|
*/
|
|
26
30
|
const getEventTypes = (eventName: string) => [
|
|
27
|
-
`firebase.extensions.firestore-
|
|
31
|
+
`firebase.extensions.firestore-counter.v1.${eventName}`, // OLD Event Type for backward compatibility
|
|
32
|
+
`firebase.extensions.firestore-bigquery-export.v1.${eventName}`, // NEW Event Type following the updated convention
|
|
28
33
|
];
|
|
29
34
|
|
|
30
35
|
let eventChannel: eventArc.Channel | undefined;
|
|
@@ -46,21 +51,35 @@ export const setupEventChannel = () => {
|
|
|
46
51
|
: undefined;
|
|
47
52
|
};
|
|
48
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Publishes an event, logging and swallowing any failure.
|
|
56
|
+
*
|
|
57
|
+
* A deleted or unreachable Eventarc channel must not fail the invocation: the
|
|
58
|
+
* BigQuery export is the contract, event delivery is best effort.
|
|
59
|
+
*/
|
|
60
|
+
const publish = async (event: eventArc.CloudEvent): Promise<void> => {
|
|
61
|
+
if (!eventChannel) return;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
await eventChannel.publish(event);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
logger.warn(`Failed to publish Eventarc event ${event.type}`, err);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
49
70
|
/**
|
|
50
71
|
* Publishes a "start" event using both OLD and NEW event types.
|
|
51
72
|
*
|
|
52
73
|
* @param data The payload to send with the event. Can be a string or an object.
|
|
53
|
-
* @returns A Promise resolving
|
|
74
|
+
* @returns A Promise resolving once both publishes have been attempted.
|
|
54
75
|
*/
|
|
55
76
|
export const recordStartEvent = async (data: string | object) => {
|
|
56
|
-
if (!eventChannel) return Promise.resolve();
|
|
57
|
-
|
|
58
77
|
const eventTypes = getEventTypes("onStart");
|
|
59
78
|
|
|
60
79
|
// Publish events for both OLD and NEW event types
|
|
61
|
-
|
|
80
|
+
await Promise.all(
|
|
62
81
|
eventTypes.map((type) =>
|
|
63
|
-
|
|
82
|
+
publish({
|
|
64
83
|
type,
|
|
65
84
|
data,
|
|
66
85
|
})
|
|
@@ -73,17 +92,15 @@ export const recordStartEvent = async (data: string | object) => {
|
|
|
73
92
|
*
|
|
74
93
|
* @param err The Error object containing the error message.
|
|
75
94
|
* @param subject (Optional) Subject identifier related to the error event.
|
|
76
|
-
* @returns A Promise resolving
|
|
95
|
+
* @returns A Promise resolving once both publishes have been attempted.
|
|
77
96
|
*/
|
|
78
97
|
export const recordErrorEvent = async (err: Error, subject?: string) => {
|
|
79
|
-
if (!eventChannel) return Promise.resolve();
|
|
80
|
-
|
|
81
98
|
const eventTypes = getEventTypes("onError");
|
|
82
99
|
|
|
83
100
|
// Publish events for both OLD and NEW event types
|
|
84
|
-
|
|
101
|
+
await Promise.all(
|
|
85
102
|
eventTypes.map((type) =>
|
|
86
|
-
|
|
103
|
+
publish({
|
|
87
104
|
type,
|
|
88
105
|
data: { message: err.message },
|
|
89
106
|
subject,
|
|
@@ -98,7 +115,7 @@ export const recordErrorEvent = async (err: Error, subject?: string) => {
|
|
|
98
115
|
* @param params An object containing the subject and the event data.
|
|
99
116
|
* @param params.subject A string representing the subject of the event.
|
|
100
117
|
* @param params.data The payload to send with the event.
|
|
101
|
-
* @returns A Promise resolving
|
|
118
|
+
* @returns A Promise resolving once both publishes have been attempted.
|
|
102
119
|
*/
|
|
103
120
|
export const recordSuccessEvent = async ({
|
|
104
121
|
subject,
|
|
@@ -107,14 +124,12 @@ export const recordSuccessEvent = async ({
|
|
|
107
124
|
subject: string;
|
|
108
125
|
data: string | object;
|
|
109
126
|
}) => {
|
|
110
|
-
if (!eventChannel) return Promise.resolve();
|
|
111
|
-
|
|
112
127
|
const eventTypes = getEventTypes("onSuccess");
|
|
113
128
|
|
|
114
129
|
// Publish events for both OLD and NEW event types
|
|
115
|
-
|
|
130
|
+
await Promise.all(
|
|
116
131
|
eventTypes.map((type) =>
|
|
117
|
-
|
|
132
|
+
publish({
|
|
118
133
|
type,
|
|
119
134
|
subject,
|
|
120
135
|
data,
|
|
@@ -127,17 +142,15 @@ export const recordSuccessEvent = async ({
|
|
|
127
142
|
* Publishes a "completion" event using both OLD and NEW event types.
|
|
128
143
|
*
|
|
129
144
|
* @param data The payload to send with the event. Can be a string or an object.
|
|
130
|
-
* @returns A Promise resolving
|
|
145
|
+
* @returns A Promise resolving once both publishes have been attempted.
|
|
131
146
|
*/
|
|
132
147
|
export const recordCompletionEvent = async (data: string | object) => {
|
|
133
|
-
if (!eventChannel) return Promise.resolve();
|
|
134
|
-
|
|
135
148
|
const eventTypes = getEventTypes("onCompletion");
|
|
136
149
|
|
|
137
150
|
// Publish events for both OLD and NEW event types
|
|
138
|
-
|
|
151
|
+
await Promise.all(
|
|
139
152
|
eventTypes.map((type) =>
|
|
140
|
-
|
|
153
|
+
publish({
|
|
141
154
|
type,
|
|
142
155
|
data,
|
|
143
156
|
})
|
package/src/handlers.ts
CHANGED
|
@@ -111,8 +111,6 @@ async function enqueueForSync(
|
|
|
111
111
|
try {
|
|
112
112
|
await ctx.enqueue(change);
|
|
113
113
|
} catch (enqueueErr) {
|
|
114
|
-
// Log before publishing: the log line is the only trace of the dropped
|
|
115
|
-
// row, and the event publish can itself reject.
|
|
116
114
|
logs.logFailedEventAction(
|
|
117
115
|
"Failed to enqueue event to Cloud Tasks from onWrite handler",
|
|
118
116
|
change.fullResourceName,
|
|
@@ -183,18 +181,13 @@ export async function handleDocumentWrite(
|
|
|
183
181
|
throw err;
|
|
184
182
|
}
|
|
185
183
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
});
|
|
194
|
-
} catch (err) {
|
|
195
|
-
logs.error(false, "Failed to record start event", err);
|
|
196
|
-
throw err;
|
|
197
|
-
}
|
|
184
|
+
await events.recordStartEvent({
|
|
185
|
+
documentId,
|
|
186
|
+
changeType,
|
|
187
|
+
before: { data: data.before.data() },
|
|
188
|
+
after: { data: data.after.data() },
|
|
189
|
+
context,
|
|
190
|
+
});
|
|
198
191
|
|
|
199
192
|
const change: SerializedDocumentChange = {
|
|
200
193
|
timestamp: context.time,
|
|
@@ -256,25 +249,19 @@ export async function handleSyncBigQueryTask(
|
|
|
256
249
|
throw err;
|
|
257
250
|
}
|
|
258
251
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
});
|
|
273
|
-
} catch (err) {
|
|
274
|
-
// The row is already in BigQuery. Rethrowing would have Cloud Tasks retry
|
|
275
|
-
// the insert past the dedupe window and duplicate it.
|
|
276
|
-
logs.error(false, "Failed to record success event", err as Error);
|
|
277
|
-
}
|
|
252
|
+
await events.recordSuccessEvent({
|
|
253
|
+
subject: change.documentId,
|
|
254
|
+
data: {
|
|
255
|
+
timestamp: change.timestamp,
|
|
256
|
+
operation: change.changeType,
|
|
257
|
+
documentName: change.fullResourceName,
|
|
258
|
+
documentId: change.documentId,
|
|
259
|
+
pathParams: change.params,
|
|
260
|
+
eventId: change.eventId,
|
|
261
|
+
data: change.data,
|
|
262
|
+
oldData: change.oldData,
|
|
263
|
+
},
|
|
264
|
+
});
|
|
278
265
|
|
|
279
266
|
logs.complete();
|
|
280
267
|
}
|
package/src/index.ts
CHANGED
|
@@ -38,7 +38,11 @@ import {
|
|
|
38
38
|
afterFirstDeploy,
|
|
39
39
|
afterRedeploy,
|
|
40
40
|
} from "firebase-functions/v2/lifecycle";
|
|
41
|
-
import {
|
|
41
|
+
import {
|
|
42
|
+
assertRequiredParams,
|
|
43
|
+
CONFIG_EXPRESSIONS,
|
|
44
|
+
configFromEnv,
|
|
45
|
+
} from "./config";
|
|
42
46
|
import * as events from "./events";
|
|
43
47
|
import {
|
|
44
48
|
DEFAULT_MAX_DISPATCHES_PER_SECOND,
|
|
@@ -56,6 +60,7 @@ import * as logs from "./logs";
|
|
|
56
60
|
import { firestoreLocationToFunctionRegion } from "./region";
|
|
57
61
|
import { enqueueSyncTask } from "./tasks";
|
|
58
62
|
|
|
63
|
+
assertRequiredParams();
|
|
59
64
|
// Re-export the side-effect-free library surface (handlers and config types).
|
|
60
65
|
export * from "./lib";
|
|
61
66
|
|
|
@@ -77,6 +82,11 @@ const REQUIRED_ROLES: ReadonlyArray<Role> = [
|
|
|
77
82
|
// Gen2 Firestore triggers need Eventarc receive and run.invoker on the function SA.
|
|
78
83
|
"roles/eventarc.eventReceiver",
|
|
79
84
|
"roles/run.invoker",
|
|
85
|
+
// The Extensions platform granted publish rights on the extension's Eventarc
|
|
86
|
+
// channel implicitly from `events:` in extension.yaml. Kits get no implicit
|
|
87
|
+
// grant, so without this the `channel.publish()` calls in ./events fail with
|
|
88
|
+
// PERMISSION_DENIED and no custom event is ever delivered.
|
|
89
|
+
"roles/eventarc.publisher",
|
|
80
90
|
// The trigger enqueues failed writes onto its own syncBigQuery task queue.
|
|
81
91
|
"roles/cloudtasks.enqueuer",
|
|
82
92
|
];
|
|
@@ -89,6 +99,10 @@ const REQUIRED_APIS = [
|
|
|
89
99
|
api: "bigquery.googleapis.com",
|
|
90
100
|
reason: "Mirrors data from your Cloud Firestore collection in BigQuery.",
|
|
91
101
|
},
|
|
102
|
+
{
|
|
103
|
+
api: "eventarcpublishing.googleapis.com",
|
|
104
|
+
reason: "Publishes the extension's custom events to its Eventarc channel.",
|
|
105
|
+
},
|
|
92
106
|
] as const;
|
|
93
107
|
|
|
94
108
|
for (const role of REQUIRED_ROLES) {
|
|
@@ -142,8 +156,9 @@ function getHandlerContext(): HandlerContext {
|
|
|
142
156
|
|
|
143
157
|
/*
|
|
144
158
|
* Read at module load: the CLI populates `.env` values into the discovery
|
|
145
|
-
* process env (firebase-tools >= 15.28.0), and the
|
|
146
|
-
*
|
|
159
|
+
* process env (firebase-tools >= 15.28.0), and the location to Cloud Run region
|
|
160
|
+
* lookup needs a nested ternary the CLI's CEL subset cannot express. When
|
|
161
|
+
* unset, no function declares a region and the CLI
|
|
147
162
|
* falls back to its default. The Eventarc trigger region needs no handling:
|
|
148
163
|
* the CLI pins it to the database's own region regardless of where the
|
|
149
164
|
* function runs.
|
package/src/region.ts
CHANGED
|
@@ -24,14 +24,17 @@ const MULTI_REGION_TO_FUNCTION_REGION: Record<string, string> = {
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Maps a Firestore database location to the Cloud Run region the functions
|
|
27
|
-
* should deploy to.
|
|
28
|
-
*
|
|
27
|
+
* should deploy to. The lookup is case-insensitive and ignores surrounding
|
|
28
|
+
* whitespace, as the CLI's own region handling is. Regional locations pass
|
|
29
|
+
* through lowercased; an unset or blank location returns `undefined`, meaning
|
|
30
|
+
* the functions declare no region.
|
|
29
31
|
*/
|
|
30
32
|
export function firestoreLocationToFunctionRegion(
|
|
31
33
|
location: string | undefined
|
|
32
34
|
): string | undefined {
|
|
33
|
-
|
|
35
|
+
const normalized = location?.trim().toLowerCase();
|
|
36
|
+
if (!normalized) {
|
|
34
37
|
return undefined;
|
|
35
38
|
}
|
|
36
|
-
return MULTI_REGION_TO_FUNCTION_REGION[
|
|
39
|
+
return MULTI_REGION_TO_FUNCTION_REGION[normalized] ?? normalized;
|
|
37
40
|
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The handler is driven with a real `../src/events` against an Eventarc
|
|
19
|
+
* channel that rejects every publish, as a deleted channel does with
|
|
20
|
+
* PERMISSION_DENIED. The export must still reach BigQuery.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { ChangeType } from "@firebaseextensions/firestore-bigquery-change-tracker";
|
|
24
|
+
import type { Request } from "firebase-functions/tasks";
|
|
25
|
+
import { logger } from "firebase-functions";
|
|
26
|
+
import { beforeEach, expect, test, vi } from "vitest";
|
|
27
|
+
|
|
28
|
+
const publish = vi
|
|
29
|
+
.fn()
|
|
30
|
+
.mockRejectedValue(
|
|
31
|
+
Object.assign(new Error("Permission denied"), { code: 403 })
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
vi.mock("firebase-admin/eventarc", () => ({
|
|
35
|
+
getEventarc: () => ({ channel: () => ({ publish }) }),
|
|
36
|
+
}));
|
|
37
|
+
vi.mock("../src/logs");
|
|
38
|
+
|
|
39
|
+
import * as events from "../src/events";
|
|
40
|
+
import { resolveExportConfig } from "../src/export-config";
|
|
41
|
+
import {
|
|
42
|
+
type DocumentWriteEvent,
|
|
43
|
+
type HandlerContext,
|
|
44
|
+
type SerializedDocumentChange,
|
|
45
|
+
handleDocumentWrite,
|
|
46
|
+
handleSyncBigQueryTask,
|
|
47
|
+
} from "../src/handlers";
|
|
48
|
+
|
|
49
|
+
function snap(exists: boolean, id: string, data: unknown = {}) {
|
|
50
|
+
return { exists, id, data: () => data };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeCtx(): HandlerContext {
|
|
54
|
+
return {
|
|
55
|
+
tracker: {
|
|
56
|
+
record: vi.fn().mockResolvedValue(undefined),
|
|
57
|
+
serializeData: vi.fn((d: unknown) => d),
|
|
58
|
+
},
|
|
59
|
+
config: resolveExportConfig({
|
|
60
|
+
collectionPath: "users",
|
|
61
|
+
datasetId: "ds",
|
|
62
|
+
tableId: "tbl",
|
|
63
|
+
projectId: "test-project",
|
|
64
|
+
}),
|
|
65
|
+
} as unknown as HandlerContext;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
vi.clearAllMocks();
|
|
70
|
+
vi.spyOn(logger, "warn").mockImplementation(() => {});
|
|
71
|
+
process.env.EVENTARC_CHANNEL = "projects/p/locations/l/channels/firebase";
|
|
72
|
+
events.setupEventChannel();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a document write still exports when every Eventarc publish is denied", async () => {
|
|
76
|
+
const ctx = makeCtx();
|
|
77
|
+
const event = {
|
|
78
|
+
data: { before: snap(false, "doc1"), after: snap(true, "doc1", { a: 1 }) },
|
|
79
|
+
id: "evt-1",
|
|
80
|
+
time: "2026-01-01T00:00:00Z",
|
|
81
|
+
document: "users/doc1",
|
|
82
|
+
params: { documentId: "doc1" },
|
|
83
|
+
} as unknown as DocumentWriteEvent;
|
|
84
|
+
|
|
85
|
+
await expect(handleDocumentWrite(event, ctx)).resolves.toBeUndefined();
|
|
86
|
+
|
|
87
|
+
expect(publish).toHaveBeenCalled();
|
|
88
|
+
expect(logger.warn).toHaveBeenCalled();
|
|
89
|
+
const record = vi.mocked(ctx.tracker.record);
|
|
90
|
+
expect(record).toHaveBeenCalledTimes(1);
|
|
91
|
+
expect(record.mock.calls[0][0][0].operation).toBe(ChangeType.CREATE);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("a dispatched task does not retry when the onSuccess publish is denied", async () => {
|
|
95
|
+
const ctx = makeCtx();
|
|
96
|
+
const change: SerializedDocumentChange = {
|
|
97
|
+
timestamp: "2026-01-01T00:00:00Z",
|
|
98
|
+
eventId: "evt-1",
|
|
99
|
+
fullResourceName:
|
|
100
|
+
"projects/test-project/databases/(default)/documents/users/doc1",
|
|
101
|
+
changeType: ChangeType.CREATE,
|
|
102
|
+
documentId: "doc1",
|
|
103
|
+
params: null,
|
|
104
|
+
data: { a: 1 },
|
|
105
|
+
oldData: undefined,
|
|
106
|
+
};
|
|
107
|
+
const req = {
|
|
108
|
+
data: change,
|
|
109
|
+
retryCount: 0,
|
|
110
|
+
} as Request<SerializedDocumentChange>;
|
|
111
|
+
|
|
112
|
+
// A rethrow here would have Cloud Tasks retry the insert past the insertId
|
|
113
|
+
// dedupe window and duplicate the row.
|
|
114
|
+
await expect(handleSyncBigQueryTask(req, ctx)).resolves.toBeUndefined();
|
|
115
|
+
|
|
116
|
+
expect(publish).toHaveBeenCalled();
|
|
117
|
+
expect(logger.warn).toHaveBeenCalled();
|
|
118
|
+
expect(vi.mocked(ctx.tracker.record)).toHaveBeenCalledTimes(1);
|
|
119
|
+
});
|
package/tests/events.test.ts
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
import { logger } from "firebase-functions";
|
|
17
18
|
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
18
19
|
|
|
19
20
|
const { publish, channel } = vi.hoisted(() => {
|
|
@@ -59,14 +60,13 @@ describe("channel configured", () => {
|
|
|
59
60
|
setupEventChannel();
|
|
60
61
|
});
|
|
61
62
|
|
|
62
|
-
test("publishes the
|
|
63
|
+
test("publishes both the legacy and the current event type", async () => {
|
|
63
64
|
await recordStartEvent({ a: 1 });
|
|
64
|
-
expect(publish).toHaveBeenCalledTimes(
|
|
65
|
-
expect(publish).
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
);
|
|
65
|
+
expect(publish).toHaveBeenCalledTimes(2);
|
|
66
|
+
expect(publish.mock.calls.map((c) => c[0].type)).toEqual([
|
|
67
|
+
"firebase.extensions.firestore-counter.v1.onStart",
|
|
68
|
+
"firebase.extensions.firestore-bigquery-export.v1.onStart",
|
|
69
|
+
]);
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
test("error / success / completion map to their event types", async () => {
|
|
@@ -76,9 +76,51 @@ describe("channel configured", () => {
|
|
|
76
76
|
|
|
77
77
|
const types = publish.mock.calls.map((c) => c[0].type);
|
|
78
78
|
expect(types).toEqual([
|
|
79
|
+
"firebase.extensions.firestore-counter.v1.onError",
|
|
79
80
|
"firebase.extensions.firestore-bigquery-export.v1.onError",
|
|
81
|
+
"firebase.extensions.firestore-counter.v1.onSuccess",
|
|
80
82
|
"firebase.extensions.firestore-bigquery-export.v1.onSuccess",
|
|
83
|
+
"firebase.extensions.firestore-counter.v1.onCompletion",
|
|
81
84
|
"firebase.extensions.firestore-bigquery-export.v1.onCompletion",
|
|
82
85
|
]);
|
|
83
86
|
});
|
|
87
|
+
|
|
88
|
+
test("the legacy copy carries the same payload as the current one", async () => {
|
|
89
|
+
await recordErrorEvent(new Error("boom"), "doc1");
|
|
90
|
+
|
|
91
|
+
const [legacy, current] = publish.mock.calls.map((c) => c[0]);
|
|
92
|
+
expect(legacy.data).toEqual({ message: "boom" });
|
|
93
|
+
expect(legacy.subject).toBe("doc1");
|
|
94
|
+
expect({ ...legacy, type: undefined }).toEqual({
|
|
95
|
+
...current,
|
|
96
|
+
type: undefined,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("publish failures", () => {
|
|
102
|
+
afterEach(() => {
|
|
103
|
+
publish.mockReset();
|
|
104
|
+
publish.mockResolvedValue(undefined);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("a rejected publish is logged and never reaches the caller", async () => {
|
|
108
|
+
const warn = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
|
109
|
+
publish.mockRejectedValue(
|
|
110
|
+
Object.assign(new Error("Permission denied"), { code: 403 })
|
|
111
|
+
);
|
|
112
|
+
process.env.EVENTARC_CHANNEL = "projects/p/locations/l/channels/c";
|
|
113
|
+
setupEventChannel();
|
|
114
|
+
|
|
115
|
+
await expect(recordStartEvent({ a: 1 })).resolves.toBeUndefined();
|
|
116
|
+
await expect(recordErrorEvent(new Error("boom"))).resolves.toBeUndefined();
|
|
117
|
+
await expect(
|
|
118
|
+
recordSuccessEvent({ subject: "s", data: {} })
|
|
119
|
+
).resolves.toBeUndefined();
|
|
120
|
+
await expect(recordCompletionEvent({ a: 1 })).resolves.toBeUndefined();
|
|
121
|
+
|
|
122
|
+
// Two event types per call: the old and the new.
|
|
123
|
+
expect(warn).toHaveBeenCalledTimes(8);
|
|
124
|
+
warn.mockRestore();
|
|
125
|
+
});
|
|
84
126
|
});
|
package/tests/handlers.test.ts
CHANGED
|
@@ -332,18 +332,4 @@ describe("handleSyncBigQueryTask", () => {
|
|
|
332
332
|
// belong to Cloud Tasks alone.
|
|
333
333
|
expect(ctx.enqueue).not.toHaveBeenCalled();
|
|
334
334
|
});
|
|
335
|
-
|
|
336
|
-
test("does not rethrow when the success event fails after the row lands", async () => {
|
|
337
|
-
// The row is already in BigQuery; a Cloud Tasks retry would land past the
|
|
338
|
-
// insertId dedupe window and duplicate it.
|
|
339
|
-
const ctx = makeCtx();
|
|
340
|
-
(
|
|
341
|
-
events.recordSuccessEvent as ReturnType<typeof vi.fn>
|
|
342
|
-
).mockRejectedValueOnce(new Error("channel down"));
|
|
343
|
-
|
|
344
|
-
await expect(
|
|
345
|
-
handleSyncBigQueryTask(taskRequest(serializedChange()), ctx)
|
|
346
|
-
).resolves.toBeUndefined();
|
|
347
|
-
expect(ctx.tracker.record).toHaveBeenCalledTimes(1);
|
|
348
|
-
});
|
|
349
335
|
});
|
package/tests/region.test.ts
CHANGED
|
@@ -41,4 +41,22 @@ describe("firestoreLocationToFunctionRegion", () => {
|
|
|
41
41
|
test("returns undefined for an empty location", () => {
|
|
42
42
|
expect(firestoreLocationToFunctionRegion("")).toBeUndefined();
|
|
43
43
|
});
|
|
44
|
+
|
|
45
|
+
test("returns undefined for a whitespace-only location", () => {
|
|
46
|
+
expect(firestoreLocationToFunctionRegion(" ")).toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test.each([
|
|
50
|
+
["NAM5", "us-central1"],
|
|
51
|
+
["Eur3", "europe-west1"],
|
|
52
|
+
[" nam7 ", "us-central1"],
|
|
53
|
+
])("normalizes %s before the multi-region lookup", (location, region) => {
|
|
54
|
+
expect(firestoreLocationToFunctionRegion(location)).toBe(region);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("lowercases and trims a regional location", () => {
|
|
58
|
+
expect(firestoreLocationToFunctionRegion(" Europe-West2 ")).toBe(
|
|
59
|
+
"europe-west2"
|
|
60
|
+
);
|
|
61
|
+
});
|
|
44
62
|
});
|
|
@@ -46,10 +46,14 @@ test.each([
|
|
|
46
46
|
"bigquery.googleapis.com",
|
|
47
47
|
"Mirrors data from your Cloud Firestore collection in BigQuery.",
|
|
48
48
|
],
|
|
49
|
+
[
|
|
50
|
+
"eventarcpublishing.googleapis.com",
|
|
51
|
+
"Publishes the extension's custom events to its Eventarc channel.",
|
|
52
|
+
],
|
|
49
53
|
])("declares %s", (api, reason) => {
|
|
50
54
|
expect(requiresAPI).toHaveBeenCalledWith(api, reason);
|
|
51
55
|
});
|
|
52
56
|
|
|
53
|
-
test("declares exactly the
|
|
54
|
-
expect(requiresAPI).toHaveBeenCalledTimes(
|
|
57
|
+
test("declares exactly the 3 APIs the kit needs", () => {
|
|
58
|
+
expect(requiresAPI).toHaveBeenCalledTimes(3);
|
|
55
59
|
});
|