@firebase-function-kits/firestore-bigquery-export 0.0.2-rc.0 → 0.0.2-rc.10

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/README.md +281 -60
  3. package/lib/config.d.ts +3 -2
  4. package/lib/config.d.ts.map +1 -1
  5. package/lib/config.js +295 -70
  6. package/lib/config.js.map +1 -1
  7. package/lib/events.d.ts.map +1 -1
  8. package/lib/events.js.map +1 -1
  9. package/lib/export-config.d.ts +19 -5
  10. package/lib/export-config.d.ts.map +1 -1
  11. package/lib/export-config.js +6 -1
  12. package/lib/export-config.js.map +1 -1
  13. package/lib/handlers.d.ts +38 -6
  14. package/lib/handlers.d.ts.map +1 -1
  15. package/lib/handlers.js +63 -18
  16. package/lib/handlers.js.map +1 -1
  17. package/lib/index.d.ts +13 -2
  18. package/lib/index.d.ts.map +1 -1
  19. package/lib/index.js +51 -12
  20. package/lib/index.js.map +1 -1
  21. package/lib/init.d.ts +1 -1
  22. package/lib/init.js +1 -1
  23. package/lib/init.js.map +1 -1
  24. package/lib/lib.d.ts +1 -1
  25. package/lib/lib.d.ts.map +1 -1
  26. package/lib/lib.js +2 -1
  27. package/lib/lib.js.map +1 -1
  28. package/lib/logs.d.ts +2 -2
  29. package/lib/logs.d.ts.map +1 -1
  30. package/lib/logs.js +4 -3
  31. package/lib/logs.js.map +1 -1
  32. package/lib/region.d.ts +7 -0
  33. package/lib/region.d.ts.map +1 -0
  34. package/lib/region.js +37 -0
  35. package/lib/region.js.map +1 -0
  36. package/lib/tasks.d.ts +40 -0
  37. package/lib/tasks.d.ts.map +1 -0
  38. package/lib/tasks.js +107 -0
  39. package/lib/tasks.js.map +1 -0
  40. package/lib/util.d.ts.map +1 -1
  41. package/lib/util.js.map +1 -1
  42. package/npm-shrinkwrap.json +6396 -0
  43. package/package.json +15 -5
  44. package/src/config.ts +359 -72
  45. package/src/export-config.ts +26 -6
  46. package/src/handlers.ts +96 -27
  47. package/src/index.ts +71 -13
  48. package/src/init.ts +1 -1
  49. package/src/lib.ts +2 -0
  50. package/src/logs.ts +5 -3
  51. package/src/region.ts +37 -0
  52. package/src/tasks.ts +126 -0
  53. package/tests/config.test.ts +101 -14
  54. package/tests/export-config.test.ts +10 -4
  55. package/tests/handlers.test.ts +139 -10
  56. package/tests/index.test.ts +162 -0
  57. package/tests/region.test.ts +44 -0
  58. package/tests/required-apis.test.ts +55 -0
  59. package/tests/tasks.emulator.test.ts +131 -0
  60. package/tests/tasks.test.ts +229 -0
  61. package/tsconfig.tsbuildinfo +1 -1
package/lib/tasks.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ import type { SerializedDocumentChange } from "./handlers";
2
+ /** Export name of the write-buffer task function. */
3
+ export declare const SYNC_BIGQUERY_FUNCTION = "syncBigQuery";
4
+ /**
5
+ * Resolves the queue resource path for a task function of this kit instance.
6
+ *
7
+ * The name is deliberately unprefixed: firebase-admin >= 14.2.0 resolves the
8
+ * deployed `kit-<instance id>-` prefix itself from the
9
+ * `FIREBASE_KIT_INSTANCE_ID` env var, which the CLI sets on every deployed kit
10
+ * function. All functions of a kit instance deploy to one region, so the
11
+ * enqueuing function's own region is the queue's region.
12
+ *
13
+ * The CLI-set `FUNCTION_REGION` wins because it is the region the function
14
+ * was actually deployed to; a `DATABASE_REGION`-derived region can disagree
15
+ * with it on a first deploy or when the variable is unset, and is only the
16
+ * fallback for local runs where the CLI has not populated the environment.
17
+ * With neither set, the bare function name is returned and the Admin SDK
18
+ * applies its default location, `us-central1`, which is also where the CLI
19
+ * places functions that declare no region.
20
+ *
21
+ * @param functionName - The export name of the task function.
22
+ * @returns The queue resource path, `locations/<region>/functions/<name>`,
23
+ * or the bare `<name>` when no region is known.
24
+ */
25
+ export declare function syncQueuePath(functionName?: string): string;
26
+ /**
27
+ * Enqueues a payload onto the `syncBigQuery` queue, retrying transient enqueue
28
+ * failures in-process with exponential backoff and jitter.
29
+ *
30
+ * The task id is derived from the event id, so a retried enqueue of an event
31
+ * that already reached Cloud Tasks is rejected rather than buffered twice.
32
+ *
33
+ * @param payload - The serialized change to enqueue.
34
+ * @param maxAttempts - How many enqueue attempts to make before giving up.
35
+ * Anything but a positive integer means a single attempt: resolving without
36
+ * an enqueue would report success for an event that was never buffered.
37
+ * @throws The last enqueue error, once every attempt has failed.
38
+ */
39
+ export declare function enqueueSyncTask(payload: SerializedDocumentChange, maxAttempts: number): Promise<void>;
40
+ //# sourceMappingURL=tasks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tasks.d.ts","sourceRoot":"","sources":["../src/tasks.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAG3D,qDAAqD;AACrD,eAAO,MAAM,sBAAsB,iBAAiB,CAAC;AAYrD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAC3B,YAAY,GAAE,MAA+B,GAC5C,MAAM,CAQR;AAQD;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,wBAAwB,EACjC,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC,CAoCf"}
package/lib/tasks.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2019 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * https://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.SYNC_BIGQUERY_FUNCTION = void 0;
19
+ exports.syncQueuePath = syncQueuePath;
20
+ exports.enqueueSyncTask = enqueueSyncTask;
21
+ const node_crypto_1 = require("node:crypto");
22
+ const functions_1 = require("firebase-admin/functions");
23
+ const region_1 = require("./region");
24
+ /** Export name of the write-buffer task function. */
25
+ exports.SYNC_BIGQUERY_FUNCTION = "syncBigQuery";
26
+ const MAX_BACKOFF_MS = 5000;
27
+ const BACKOFF_BASE_MS = 100;
28
+ const JITTER_MS = 100;
29
+ // Hashed rather than sanitized: a lossy mapping could collapse two event ids
30
+ // into one task id, and Cloud Tasks wants ids uniformly distributed.
31
+ function taskIdFor(change) {
32
+ return (0, node_crypto_1.createHash)("sha256").update(change.eventId).digest("hex");
33
+ }
34
+ /**
35
+ * Resolves the queue resource path for a task function of this kit instance.
36
+ *
37
+ * The name is deliberately unprefixed: firebase-admin >= 14.2.0 resolves the
38
+ * deployed `kit-<instance id>-` prefix itself from the
39
+ * `FIREBASE_KIT_INSTANCE_ID` env var, which the CLI sets on every deployed kit
40
+ * function. All functions of a kit instance deploy to one region, so the
41
+ * enqueuing function's own region is the queue's region.
42
+ *
43
+ * The CLI-set `FUNCTION_REGION` wins because it is the region the function
44
+ * was actually deployed to; a `DATABASE_REGION`-derived region can disagree
45
+ * with it on a first deploy or when the variable is unset, and is only the
46
+ * fallback for local runs where the CLI has not populated the environment.
47
+ * With neither set, the bare function name is returned and the Admin SDK
48
+ * applies its default location, `us-central1`, which is also where the CLI
49
+ * places functions that declare no region.
50
+ *
51
+ * @param functionName - The export name of the task function.
52
+ * @returns The queue resource path, `locations/<region>/functions/<name>`,
53
+ * or the bare `<name>` when no region is known.
54
+ */
55
+ function syncQueuePath(functionName = exports.SYNC_BIGQUERY_FUNCTION) {
56
+ const region = process.env.FUNCTION_REGION ||
57
+ (0, region_1.firestoreLocationToFunctionRegion)(process.env.DATABASE_REGION);
58
+ return region
59
+ ? `locations/${region}/functions/${functionName}`
60
+ : functionName;
61
+ }
62
+ function backoffMs(attempt, jitter) {
63
+ return (Math.min(Math.pow(2, attempt) * BACKOFF_BASE_MS, MAX_BACKOFF_MS) + jitter);
64
+ }
65
+ /**
66
+ * Enqueues a payload onto the `syncBigQuery` queue, retrying transient enqueue
67
+ * failures in-process with exponential backoff and jitter.
68
+ *
69
+ * The task id is derived from the event id, so a retried enqueue of an event
70
+ * that already reached Cloud Tasks is rejected rather than buffered twice.
71
+ *
72
+ * @param payload - The serialized change to enqueue.
73
+ * @param maxAttempts - How many enqueue attempts to make before giving up.
74
+ * Anything but a positive integer means a single attempt: resolving without
75
+ * an enqueue would report success for an event that was never buffered.
76
+ * @throws The last enqueue error, once every attempt has failed.
77
+ */
78
+ async function enqueueSyncTask(payload, maxAttempts) {
79
+ const queue = (0, functions_1.getFunctions)().taskQueue(syncQueuePath());
80
+ const id = taskIdFor(payload);
81
+ // Math.max(1, NaN) is NaN and would skip the loop entirely.
82
+ const attemptBudget = Number.isInteger(maxAttempts) && maxAttempts >= 1 ? maxAttempts : 1;
83
+ const jitter = Math.random() * JITTER_MS;
84
+ let attempts = 0;
85
+ while (attempts < attemptBudget) {
86
+ if (attempts > 0) {
87
+ await new Promise((resolve) => setTimeout(resolve, backoffMs(attempts, jitter)));
88
+ }
89
+ attempts++;
90
+ try {
91
+ await queue.enqueue(payload, { id });
92
+ return;
93
+ }
94
+ catch (enqueueErr) {
95
+ // The event is already buffered; a second task would double-write the row.
96
+ // firebase-admin prefixes its codes: `functions/task-already-exists`.
97
+ if (enqueueErr?.code ===
98
+ "functions/task-already-exists") {
99
+ return;
100
+ }
101
+ if (attempts >= attemptBudget) {
102
+ throw enqueueErr;
103
+ }
104
+ }
105
+ }
106
+ }
107
+ //# sourceMappingURL=tasks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tasks.js","sourceRoot":"","sources":["../src/tasks.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAyCH,sCAUC;AAqBD,0CAuCC;AA7GD,6CAAyC;AACzC,wDAAwD;AAExD,qCAA6D;AAE7D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AUhE,CAAC"}
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,UAAU,EAAE,MAAM,uDAAuD,CAAC;AACnF,OAAO,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAE7E;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,gBAAgB,CAAC,GAAG,UAAU,CAQ1E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,gBAAgB,CAAC,GAAG,MAAM,CAKtE;AAED;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,OAUhE,CAAC"}
package/lib/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;AAEH,6GAAmF;AAGnF;;;;GAIG;AACH,uBAA8B,MAAgC;IAC5D,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QACzB,OAAO,8CAAU,CAAC,MAAM,CAAC;IAC3B,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAC1B,OAAO,8CAAU,CAAC,MAAM,CAAC;IAC3B,CAAC;IACD,OAAO,8CAAU,CAAC,MAAM,CAAC;AAC3B,CAAC;AAED;;;;GAIG;AACH,uBAA8B,MAAgC;IAC5D,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QACxB,OAAO,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;IACzB,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;AAC1B,CAAC;AAED;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,CAAC,QAAgB,EAAE,IAAY,EAAE,EAA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