fiber-firebase-functions 1.0.1 → 1.0.3

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.
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (C) 2025 Fiber
4
+ *
5
+ * All rights reserved. This script, including its code and logic, is the
6
+ * exclusive property of Fiber. Redistribution, reproduction,
7
+ * or modification of any part of this script is strictly prohibited
8
+ * without prior written permission from Fiber.
9
+ *
10
+ * Conditions of use:
11
+ * - The code may not be copied, duplicated, or used, in whole or in part,
12
+ * for any purpose without explicit authorization.
13
+ * - Redistribution of this code, with or without modification, is not
14
+ * permitted unless expressly agreed upon by Fiber.
15
+ * - The name "Fiber" and any associated branding, logos, or
16
+ * trademarks may not be used to endorse or promote derived products
17
+ * or services without prior written approval.
18
+ *
19
+ * Disclaimer:
20
+ * THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
23
+ * FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
25
+ * DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
26
+ * OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ *
28
+ * Unauthorized copying or reproduction of this script, in whole or in part,
29
+ * is a violation of applicable intellectual property laws and will result
30
+ * in legal action.
31
+ */
32
+ var __importDefault = (this && this.__importDefault) || function (mod) {
33
+ return (mod && mod.__esModule) ? mod : { "default": mod };
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.appInitialize = appInitialize;
37
+ const fs_1 = __importDefault(require("fs"));
38
+ const path_1 = __importDefault(require("path"));
39
+ let cachedConfig = null;
40
+ function appInitialize() {
41
+ if (cachedConfig)
42
+ return cachedConfig;
43
+ const configPath = path_1.default.resolve(process.cwd(), "config", "app.json");
44
+ if (!fs_1.default.existsSync(configPath)) {
45
+ throw new Error(`Missing configuration file at: ${configPath}`);
46
+ }
47
+ const raw = fs_1.default.readFileSync(configPath, "utf-8");
48
+ const parsed = JSON.parse(raw);
49
+ cachedConfig = {
50
+ middleware: {
51
+ appName: parsed.middleware.app_name,
52
+ url: parsed.middleware.url
53
+ },
54
+ security: {
55
+ url: parsed.security.url,
56
+ }
57
+ };
58
+ return cachedConfig;
59
+ }
60
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/common/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;AAqBH,sCAuBC;AA1CD,4CAAoB;AACpB,gDAAwB;AAgBxB,IAAI,YAAY,GAAqB,IAAI,CAAC;AAE1C,SAAgB,aAAa;IACzB,IAAI,YAAY;QAAE,OAAO,YAAY,CAAC;IAEtC,MAAM,UAAU,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC;IAErE,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,kCAAkC,UAAU,EAAE,CAAC,CAAC;IACpE,CAAC;IAED,MAAM,GAAG,GAAG,YAAE,CAAC,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE/B,YAAY,GAAG;QACX,UAAU,EAAE;YACR,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,QAAQ;YACnC,GAAG,EAAE,MAAM,CAAC,UAAU,CAAC,GAAG;SAC7B;QACD,QAAQ,EAAE;YACN,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,GAAG;SAC3B;KACJ,CAAC;IAEF,OAAO,YAAY,CAAC;AACxB,CAAC"}
@@ -34,27 +34,22 @@ exports.realtimeDatabase = realtimeDatabase;
34
34
  const app_1 = require("firebase-admin/app");
35
35
  const database_1 = require("firebase-admin/database");
36
36
  function realtimeDatabase(config) {
37
- return function getRealtimeDatabase(name) {
38
- const entry = config[name];
39
- if (!entry) {
40
- throw new Error(`Realtime DB config "${name}" not found`);
41
- }
42
- const { appName, url } = entry;
43
- let app;
44
- try {
45
- app = (0, app_1.getApps)().find((a) => a.name === appName) || (0, app_1.initializeApp)({ databaseURL: url }, appName);
46
- }
47
- catch (e) {
48
- console.error("[Error:getRealtimeDatabase]", {
49
- appName,
50
- url,
51
- loadedApps: (0, app_1.getApps)().map((a) => a.name),
52
- error: e.message,
53
- stack: e.stack,
54
- });
55
- throw e;
56
- }
57
- return (0, database_1.getDatabase)(app);
58
- };
37
+ const { appName, url } = config;
38
+ let app;
39
+ try {
40
+ app = (0, app_1.getApps)().find((a) => a.name === appName) || (0, app_1.initializeApp)({ databaseURL: url }, appName);
41
+ }
42
+ catch (e) {
43
+ console.error("[Error:getRealtimeDatabase]", {
44
+ appName,
45
+ url,
46
+ loadedApps: (0, app_1.getApps)().map((a) => a.name),
47
+ error: e.message,
48
+ stack: e.stack,
49
+ });
50
+ throw e;
51
+ }
52
+ return (0, database_1.getDatabase)(app);
59
53
  }
54
+ ;
60
55
  //# sourceMappingURL=realtime_database.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"realtime_database.js","sourceRoot":"","sources":["../../src/common/realtime_database.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;AAUH,4CA2BC;AAnCD,4CAAiE;AACjE,sDAAgE;AAOh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;IAEhC,IAAI,GAAQ,CAAC;IAEb,IAAI,CAAC;QACD,GAAG,GAAG,IAAA,aAAO,GAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,IAAA,mBAAa,EAAC,EAAE,WAAW,EAAE,GAAG,EAAE,EAAE,OAAO,CAAC,CAAC;IACpG,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE;YACzC,OAAO;YACP,GAAG;YACH,UAAU,EAAE,IAAA,aAAO,GAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;YACxC,KAAK,EAAG,CAAW,CAAC,OAAO;YAC3B,KAAK,EAAG,CAAW,CAAC,KAAK;SAC5B,CAAC,CAAC;QAEH,MAAM,CAAC,CAAC;IACZ,CAAC;IAED,OAAO,IAAA,sBAAW,EAAC,GAAG,CAAC,CAAC;AAC5B,CAAC;AAAA,CAAC"}
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (C) 2025 Fiber
4
+ *
5
+ * All rights reserved. This script, including its code and logic, is the
6
+ * exclusive property of Fiber. Redistribution, reproduction,
7
+ * or modification of any part of this script is strictly prohibited
8
+ * without prior written permission from Fiber.
9
+ *
10
+ * Conditions of use:
11
+ * - The code may not be copied, duplicated, or used, in whole or in part,
12
+ * for any purpose without explicit authorization.
13
+ * - Redistribution of this code, with or without modification, is not
14
+ * permitted unless expressly agreed upon by Fiber.
15
+ * - The name "Fiber" and any associated branding, logos, or
16
+ * trademarks may not be used to endorse or promote derived products
17
+ * or services without prior written approval.
18
+ *
19
+ * Disclaimer:
20
+ * THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
23
+ * FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
25
+ * DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
26
+ * OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ *
28
+ * Unauthorized copying or reproduction of this script, in whole or in part,
29
+ * is a violation of applicable intellectual property laws and will result
30
+ * in legal action.
31
+ */
32
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
33
+ if (k2 === undefined) k2 = k;
34
+ var desc = Object.getOwnPropertyDescriptor(m, k);
35
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
36
+ desc = { enumerable: true, get: function() { return m[k]; } };
37
+ }
38
+ Object.defineProperty(o, k2, desc);
39
+ }) : (function(o, m, k, k2) {
40
+ if (k2 === undefined) k2 = k;
41
+ o[k2] = m[k];
42
+ }));
43
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
44
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
45
+ }) : function(o, v) {
46
+ o["default"] = v;
47
+ });
48
+ var __importStar = (this && this.__importStar) || (function () {
49
+ var ownKeys = function(o) {
50
+ ownKeys = Object.getOwnPropertyNames || function (o) {
51
+ var ar = [];
52
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
53
+ return ar;
54
+ };
55
+ return ownKeys(o);
56
+ };
57
+ return function (mod) {
58
+ if (mod && mod.__esModule) return mod;
59
+ var result = {};
60
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
61
+ __setModuleDefault(result, mod);
62
+ return result;
63
+ };
64
+ })();
65
+ Object.defineProperty(exports, "__esModule", { value: true });
66
+ exports.sendEmail = sendEmail;
67
+ const admin = __importStar(require("firebase-admin"));
68
+ if (admin.apps.length === 0) {
69
+ admin.initializeApp();
70
+ }
71
+ async function sendEmail(collection, to, subject, message) {
72
+ const firestore = admin.firestore();
73
+ await firestore.collection(collection).add({
74
+ to: [to],
75
+ message: {
76
+ subject: subject,
77
+ text: message,
78
+ },
79
+ });
80
+ }
81
+ //# sourceMappingURL=send_email.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send_email.js","sourceRoot":"","sources":["../../src/email/send_email.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQH,8BAUC;AAhBD,sDAAwC;AAExC,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;IAC1B,KAAK,CAAC,aAAa,EAAE,CAAC;AAC1B,CAAC;AAEM,KAAK,UAAU,SAAS,CAAC,UAAkB,EAAE,EAAU,EAAE,OAAe,EAAE,OAAe;IAC5F,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;IAEpC,MAAM,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC;QACvC,EAAE,EAAE,CAAC,EAAE,CAAC;QACR,OAAO,EAAE;YACL,OAAO,EAAE,OAAO;YAChB,IAAI,EAAE,OAAO;SAChB;KACJ,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (C) 2025 Fiber
4
+ *
5
+ * All rights reserved. This script, including its code and logic, is the
6
+ * exclusive property of Fiber. Redistribution, reproduction,
7
+ * or modification of any part of this script is strictly prohibited
8
+ * without prior written permission from Fiber.
9
+ *
10
+ * Conditions of use:
11
+ * - The code may not be copied, duplicated, or used, in whole or in part,
12
+ * for any purpose without explicit authorization.
13
+ * - Redistribution of this code, with or without modification, is not
14
+ * permitted unless expressly agreed upon by Fiber.
15
+ * - The name "Fiber" and any associated branding, logos, or
16
+ * trademarks may not be used to endorse or promote derived products
17
+ * or services without prior written approval.
18
+ *
19
+ * Disclaimer:
20
+ * THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
23
+ * FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
25
+ * DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
26
+ * OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ *
28
+ * Unauthorized copying or reproduction of this script, in whole or in part,
29
+ * is a violation of applicable intellectual property laws and will result
30
+ * in legal action.
31
+ */
32
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
33
+ if (k2 === undefined) k2 = k;
34
+ var desc = Object.getOwnPropertyDescriptor(m, k);
35
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
36
+ desc = { enumerable: true, get: function() { return m[k]; } };
37
+ }
38
+ Object.defineProperty(o, k2, desc);
39
+ }) : (function(o, m, k, k2) {
40
+ if (k2 === undefined) k2 = k;
41
+ o[k2] = m[k];
42
+ }));
43
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
44
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
45
+ }) : function(o, v) {
46
+ o["default"] = v;
47
+ });
48
+ var __importStar = (this && this.__importStar) || (function () {
49
+ var ownKeys = function(o) {
50
+ ownKeys = Object.getOwnPropertyNames || function (o) {
51
+ var ar = [];
52
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
53
+ return ar;
54
+ };
55
+ return ownKeys(o);
56
+ };
57
+ return function (mod) {
58
+ if (mod && mod.__esModule) return mod;
59
+ var result = {};
60
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
61
+ __setModuleDefault(result, mod);
62
+ return result;
63
+ };
64
+ })();
65
+ Object.defineProperty(exports, "__esModule", { value: true });
66
+ exports.SetRateLimitStatus = exports.RateLimitCheckStatus = void 0;
67
+ exports.isRateLimited = isRateLimited;
68
+ exports.recordRateLimitHit = recordRateLimitHit;
69
+ const admin = __importStar(require("firebase-admin"));
70
+ const config_1 = require("../common/config");
71
+ const realtime_database_1 = require("../common/realtime_database");
72
+ if (!admin.apps.length) {
73
+ admin.initializeApp();
74
+ }
75
+ var RateLimitCheckStatus;
76
+ (function (RateLimitCheckStatus) {
77
+ RateLimitCheckStatus["MISSING_IDENTIFIER"] = "MISSING_IDENTIFIER";
78
+ RateLimitCheckStatus["MISSING_IDENTIFIER_ID"] = "MISSING_IDENTIFIER_ID";
79
+ RateLimitCheckStatus["MISSING_IDENTIFIER_TARGET"] = "MISSING_IDENTIFIER_TARGET";
80
+ RateLimitCheckStatus["MISSING_RULE"] = "MISSING_RULE";
81
+ RateLimitCheckStatus["MISSING_DATABASE_CONFIG"] = "MISSING_DATABASE_CONFIG";
82
+ RateLimitCheckStatus["LIMIT_EXISTS"] = "LIMIT_EXISTS";
83
+ RateLimitCheckStatus["LIMIT_NOT_FOUND"] = "LIMIT_NOT_FOUND";
84
+ RateLimitCheckStatus["INTERNAL_ERROR"] = "INTERNAL_ERROR";
85
+ })(RateLimitCheckStatus || (exports.RateLimitCheckStatus = RateLimitCheckStatus = {}));
86
+ var SetRateLimitStatus;
87
+ (function (SetRateLimitStatus) {
88
+ SetRateLimitStatus["MISSING_IDENTIFIER"] = "MISSING_IDENTIFIER";
89
+ SetRateLimitStatus["MISSING_IDENTIFIER_ID"] = "MISSING_IDENTIFIER_ID";
90
+ SetRateLimitStatus["MISSING_IDENTIFIER_TARGET"] = "MISSING_IDENTIFIER_TARGET";
91
+ SetRateLimitStatus["MISSING_RULE"] = "MISSING_RULE";
92
+ SetRateLimitStatus["MISSING_DATABASE_CONFIG"] = "MISSING_DATABASE_CONFIG";
93
+ SetRateLimitStatus["RATE_LIMITED"] = "RATE_LIMITED";
94
+ SetRateLimitStatus["ALLOWED"] = "ALLOWED";
95
+ SetRateLimitStatus["INTERNAL_ERROR"] = "INTERNAL_ERROR";
96
+ })(SetRateLimitStatus || (exports.SetRateLimitStatus = SetRateLimitStatus = {}));
97
+ async function isRateLimited(identifier, rule) {
98
+ if (!identifier)
99
+ return RateLimitCheckStatus.MISSING_IDENTIFIER;
100
+ if (!identifier.id)
101
+ return RateLimitCheckStatus.MISSING_IDENTIFIER_ID;
102
+ if (!identifier.target)
103
+ return RateLimitCheckStatus.MISSING_IDENTIFIER_TARGET;
104
+ if (!rule)
105
+ return RateLimitCheckStatus.MISSING_RULE;
106
+ const config = (0, config_1.appInitialize)();
107
+ const middleware = config.middleware;
108
+ if (!middleware)
109
+ return RateLimitCheckStatus.MISSING_DATABASE_CONFIG;
110
+ try {
111
+ const databaseConfig = {
112
+ appName: middleware.appName,
113
+ url: middleware.url,
114
+ };
115
+ const database = (0, realtime_database_1.realtimeDatabase)(databaseConfig);
116
+ const reference = database.ref("__fbs__limiter").child(identifier.id).child(identifier.target).child("__fbs__timestamps");
117
+ const query = await reference.get();
118
+ if (!query.exists())
119
+ return RateLimitCheckStatus.LIMIT_NOT_FOUND;
120
+ const now = Date.now();
121
+ let timestamps = query.val();
122
+ timestamps = timestamps.filter((t) => now - t < rule.windowMs);
123
+ if (timestamps.length === 0) {
124
+ await reference.remove();
125
+ return RateLimitCheckStatus.LIMIT_NOT_FOUND;
126
+ }
127
+ if (timestamps.length >= rule.maxHits)
128
+ return RateLimitCheckStatus.LIMIT_EXISTS;
129
+ return RateLimitCheckStatus.LIMIT_NOT_FOUND;
130
+ }
131
+ catch (e) {
132
+ return RateLimitCheckStatus.INTERNAL_ERROR;
133
+ }
134
+ }
135
+ async function recordRateLimitHit(identifier, rule) {
136
+ if (!identifier)
137
+ return SetRateLimitStatus.MISSING_IDENTIFIER;
138
+ if (!identifier.id)
139
+ return SetRateLimitStatus.MISSING_IDENTIFIER_ID;
140
+ if (!identifier.target)
141
+ return SetRateLimitStatus.MISSING_IDENTIFIER_TARGET;
142
+ if (!rule)
143
+ return SetRateLimitStatus.MISSING_RULE;
144
+ const config = (0, config_1.appInitialize)();
145
+ const middleware = config.middleware;
146
+ if (!middleware)
147
+ return SetRateLimitStatus.MISSING_DATABASE_CONFIG;
148
+ try {
149
+ const databaseConfig = {
150
+ appName: middleware.appName,
151
+ url: middleware.url,
152
+ };
153
+ const database = (0, realtime_database_1.realtimeDatabase)(databaseConfig);
154
+ const reference = database.ref("__fbs__limiter").child(identifier.id).child(identifier.target).child("__fbs__timestamps");
155
+ const query = await reference.get();
156
+ let timestamps = query.exists() ? query.val() : [];
157
+ const now = Date.now();
158
+ timestamps = timestamps.filter((t) => now - t < rule.windowMs);
159
+ if (timestamps.length >= rule.maxHits)
160
+ return SetRateLimitStatus.RATE_LIMITED;
161
+ timestamps.push(now);
162
+ await reference.set(timestamps);
163
+ return SetRateLimitStatus.ALLOWED;
164
+ }
165
+ catch (e) {
166
+ return SetRateLimitStatus.INTERNAL_ERROR;
167
+ }
168
+ }
169
+ //# sourceMappingURL=rate_limiter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate_limiter.js","sourceRoot":"","sources":["../../src/middleware/rate_limiter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2C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AC,GAAG,CAAC,CAAC;QACrB,MAAM,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAEhC,OAAO,kBAAkB,CAAC,OAAO,CAAC;IACtC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,kBAAkB,CAAC,cAAc,CAAC;IAC7C,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiber-firebase-functions",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A collection of ready-to-use Firebase Cloud Functions utilities and wrappers designed for any application built by Fiber. Provides reusable helpers, common patterns, and production-grade modules to streamline backend development across all Fiber projects.",
5
5
  "author": "Fiber",
6
6
  "license": "FIBER-PROPRIETARY",
@@ -30,36 +30,63 @@
30
30
  */
31
31
 
32
32
  import * as admin from "firebase-admin";
33
- import { onCall } from "firebase-functions/v2/https";
34
33
 
35
34
  if (admin.apps.length === 0) {
36
35
  admin.initializeApp();
37
36
  }
38
37
 
39
- export enum UserDisabledStatus {
40
- MISSING_UUID = "MISSING_UUID",
38
+ export enum UserDisabledByIdStatus {
39
+ MISSING_USER_ID = "MISSING_USER_ID",
41
40
  ENABLED = "ENABLED",
42
41
  DISABLED = "DISABLED",
43
42
  NOT_FOUND = "NOT_FOUND",
44
43
  INTERNAL_ERROR = "INTERNAL_ERROR",
45
44
  }
46
45
 
47
- export const isUserDisabled = onCall(async (request) => {
48
- const uid: string | undefined = request.data?.uid;
46
+ export enum UserDisabledByEmailStatus {
47
+ MISSING_USER_ID = "MISSING_USER_ID",
48
+ ENABLED = "ENABLED",
49
+ DISABLED = "DISABLED",
50
+ NOT_FOUND = "NOT_FOUND",
51
+ INTERNAL_ERROR = "INTERNAL_ERROR",
52
+ }
53
+
54
+ export async function isUserDisabledById(userId: string): Promise<UserDisabledByIdStatus> {
55
+ userId = userId.trim();
56
+
57
+ if (!userId) return UserDisabledByIdStatus.MISSING_USER_ID;
58
+
59
+ try {
60
+ const user = await admin.auth().getUser(userId);
61
+ const isUserDisabled = user.disabled;
62
+
63
+ return isUserDisabled
64
+ ? UserDisabledByIdStatus.DISABLED
65
+ : UserDisabledByIdStatus.ENABLED;
66
+ } catch (error: any) {
67
+ if (error.code === "auth/user-not-found") {
68
+ return UserDisabledByIdStatus.NOT_FOUND;
69
+ }
70
+ return UserDisabledByIdStatus.INTERNAL_ERROR;
71
+ }
72
+ }
73
+
74
+ export async function isUserDisabledByEmail(email: string): Promise<UserDisabledByEmailStatus> {
75
+ email = email.trim();
49
76
 
50
- if (!uid) return UserDisabledStatus.MISSING_UUID;
77
+ if (!email) return UserDisabledByEmailStatus.MISSING_USER_ID;
51
78
 
52
79
  try {
53
- const user = await admin.auth().getUser(uid);
80
+ const user = await admin.auth().getUserByEmail(email);
54
81
  const isUserDisabled = user.disabled;
55
82
 
56
83
  return isUserDisabled
57
- ? UserDisabledStatus.DISABLED
58
- : UserDisabledStatus.ENABLED;
84
+ ? UserDisabledByEmailStatus.DISABLED
85
+ : UserDisabledByEmailStatus.ENABLED;
59
86
  } catch (error: any) {
60
87
  if (error.code === "auth/user-not-found") {
61
- return UserDisabledStatus.NOT_FOUND;
88
+ return UserDisabledByEmailStatus.NOT_FOUND;
62
89
  }
63
- return UserDisabledStatus.INTERNAL_ERROR;
90
+ return UserDisabledByEmailStatus.INTERNAL_ERROR;
64
91
  }
65
- });
92
+ }
@@ -30,32 +30,55 @@
30
30
  */
31
31
 
32
32
  import * as admin from "firebase-admin";
33
- import { onCall } from "firebase-functions/v2/https";
34
33
 
35
34
  if (admin.apps.length === 0) {
36
35
  admin.initializeApp();
37
36
  }
38
37
 
39
- export enum UserExistsStatus {
40
- MISSING_UUID = "MISSING_UUID",
38
+ export enum UserExistsByIdStatus {
39
+ MISSING_USER_ID = "MISSING_USER_ID",
41
40
  EXISTS = "EXISTS",
42
41
  NOT_FOUND = "NOT_FOUND",
43
42
  INTERNAL_ERROR = "INTERNAL_ERROR",
44
43
  }
45
44
 
46
- export const isUserExists = onCall(async (request) => {
47
- const uid: string | undefined = request.data?.uid;
45
+ export enum UserExistsByEmailStatus {
46
+ MISSING_USER_EMAIL = "MISSING_USER_EMAIL",
47
+ EXISTS = "EXISTS",
48
+ NOT_FOUND = "NOT_FOUND",
49
+ INTERNAL_ERROR = "INTERNAL_ERROR",
50
+ }
51
+
52
+ export async function isUserExistsById(userId: string): Promise<UserExistsByIdStatus> {
53
+ userId = userId.trim();
54
+
55
+ if (!userId || userId === "") return UserExistsByIdStatus.MISSING_USER_ID;
56
+
57
+ try {
58
+ await admin.auth().getUser(userId);
59
+
60
+ return UserExistsByIdStatus.EXISTS;
61
+ } catch (error: any) {
62
+ if (error.code === "auth/user-not-found") {
63
+ return UserExistsByIdStatus.NOT_FOUND;
64
+ }
65
+ return UserExistsByIdStatus.INTERNAL_ERROR;
66
+ }
67
+ }
68
+
69
+ export async function isUserExistsByEmail(email: string): Promise<UserExistsByEmailStatus> {
70
+ email = email.trim();
48
71
 
49
- if (!uid) return UserExistsStatus.MISSING_UUID;
72
+ if (!email || email === "") return UserExistsByEmailStatus.MISSING_USER_EMAIL;
50
73
 
51
74
  try {
52
- await admin.auth().getUser(uid);
75
+ await admin.auth().getUserByEmail(email);
53
76
 
54
- return UserExistsStatus.EXISTS;
77
+ return UserExistsByEmailStatus.EXISTS;
55
78
  } catch (error: any) {
56
79
  if (error.code === "auth/user-not-found") {
57
- return UserExistsStatus.NOT_FOUND;
80
+ return UserExistsByEmailStatus.NOT_FOUND;
58
81
  }
59
- return UserExistsStatus.INTERNAL_ERROR;
82
+ return UserExistsByEmailStatus.INTERNAL_ERROR;
60
83
  }
61
- });
84
+ }
@@ -0,0 +1,70 @@
1
+ /*
2
+ * Copyright (C) 2025 Fiber
3
+ *
4
+ * All rights reserved. This script, including its code and logic, is the
5
+ * exclusive property of Fiber. Redistribution, reproduction,
6
+ * or modification of any part of this script is strictly prohibited
7
+ * without prior written permission from Fiber.
8
+ *
9
+ * Conditions of use:
10
+ * - The code may not be copied, duplicated, or used, in whole or in part,
11
+ * for any purpose without explicit authorization.
12
+ * - Redistribution of this code, with or without modification, is not
13
+ * permitted unless expressly agreed upon by Fiber.
14
+ * - The name "Fiber" and any associated branding, logos, or
15
+ * trademarks may not be used to endorse or promote derived products
16
+ * or services without prior written approval.
17
+ *
18
+ * Disclaimer:
19
+ * THIS SCRIPT AND ITS CODE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
22
+ * FIBER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF USE,
24
+ * DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT OF OR RELATED TO THE USE
25
+ * OR INABILITY TO USE THIS SCRIPT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ *
27
+ * Unauthorized copying or reproduction of this script, in whole or in part,
28
+ * is a violation of applicable intellectual property laws and will result
29
+ * in legal action.
30
+ */
31
+
32
+ import * as admin from "firebase-admin";
33
+
34
+ if (admin.apps.length === 0) {
35
+ admin.initializeApp();
36
+ }
37
+
38
+ export enum GenerateOTPStatus {
39
+ MISSING_DATABASE_CONFIG = "MISSING_DATABASE_CONFIG",
40
+ TOO_MANY_REQUEST = "TOO_MANY_REQUEST",
41
+ SUCCESS = "SUCCESS",
42
+ INTERNAL_ERROR = "INTERNAL_ERROR",
43
+ }
44
+
45
+ export interface GenerateOTPIdentifier {
46
+ id: string;
47
+ target: string;
48
+ }
49
+
50
+ // export async function generateOTP(identifier: GenerateOTPIdentifier): Promise<GenerateOTPStatus> {
51
+ // const config = appInitialize();
52
+ // const security = config.security;
53
+
54
+ // if (!security) return GenerateOTPStatus.MISSING_DATABASE_CONFIG;
55
+
56
+ // if (await isRateLimited(identifier, rule, databaseConfig) !== RateLimitCheckStatus.LIMIT_NOT_FOUND) {
57
+ // return GenerateOTPStatus.TOO_MANY_REQUEST;
58
+ // }
59
+ // await recordRateLimitHit(identifier, rule, databaseConfig);
60
+
61
+ // try {
62
+ // const database = realtimeDatabase(databaseConfig);
63
+ // const reference = database.ref("__fbs__otp").child("feature");
64
+ // const query = await reference.get();
65
+
66
+ // return GenerateOTPStatus.SUCCESS;
67
+ // } catch (e) {
68
+ // return GenerateOTPStatus.INTERNAL_ERROR;
69
+ // }
70
+ // }