1 | /*! firebase-admin v10.0.0 */
|
2 | ;
|
3 | /*!
|
4 | * @license
|
5 | * Copyright 2017 Google Inc.
|
6 | *
|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
8 | * you may not use this file except in compliance with the License.
|
9 | * You may obtain a copy of the License at
|
10 | *
|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
12 | *
|
13 | * Unless required by applicable law or agreed to in writing, software
|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 | * See the License for the specific language governing permissions and
|
17 | * limitations under the License.
|
18 | */
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.isTopic = exports.isURL = exports.isUTCDateString = exports.isISODateString = exports.isPhoneNumber = exports.isEmail = exports.isPassword = exports.isUid = exports.isNonNullObject = exports.isObject = exports.isNonEmptyString = exports.isBase64String = exports.isString = exports.isNumber = exports.isBoolean = exports.isNonEmptyArray = exports.isArray = exports.isBuffer = void 0;
|
21 | var url = require("url");
|
22 | /**
|
23 | * Validates that a value is a byte buffer.
|
24 | *
|
25 | * @param value - The value to validate.
|
26 | * @returns Whether the value is byte buffer or not.
|
27 | */
|
28 | function isBuffer(value) {
|
29 | return value instanceof Buffer;
|
30 | }
|
31 | exports.isBuffer = isBuffer;
|
32 | /**
|
33 | * Validates that a value is an array.
|
34 | *
|
35 | * @param value - The value to validate.
|
36 | * @returns Whether the value is an array or not.
|
37 | */
|
38 | function isArray(value) {
|
39 | return Array.isArray(value);
|
40 | }
|
41 | exports.isArray = isArray;
|
42 | /**
|
43 | * Validates that a value is a non-empty array.
|
44 | *
|
45 | * @param value - The value to validate.
|
46 | * @returns Whether the value is a non-empty array or not.
|
47 | */
|
48 | function isNonEmptyArray(value) {
|
49 | return isArray(value) && value.length !== 0;
|
50 | }
|
51 | exports.isNonEmptyArray = isNonEmptyArray;
|
52 | /**
|
53 | * Validates that a value is a boolean.
|
54 | *
|
55 | * @param value - The value to validate.
|
56 | * @returns Whether the value is a boolean or not.
|
57 | */
|
58 | function isBoolean(value) {
|
59 | return typeof value === 'boolean';
|
60 | }
|
61 | exports.isBoolean = isBoolean;
|
62 | /**
|
63 | * Validates that a value is a number.
|
64 | *
|
65 | * @param value - The value to validate.
|
66 | * @returns Whether the value is a number or not.
|
67 | */
|
68 | function isNumber(value) {
|
69 | return typeof value === 'number' && !isNaN(value);
|
70 | }
|
71 | exports.isNumber = isNumber;
|
72 | /**
|
73 | * Validates that a value is a string.
|
74 | *
|
75 | * @param value - The value to validate.
|
76 | * @returns Whether the value is a string or not.
|
77 | */
|
78 | function isString(value) {
|
79 | return typeof value === 'string';
|
80 | }
|
81 | exports.isString = isString;
|
82 | /**
|
83 | * Validates that a value is a base64 string.
|
84 | *
|
85 | * @param value - The value to validate.
|
86 | * @returns Whether the value is a base64 string or not.
|
87 | */
|
88 | function isBase64String(value) {
|
89 | if (!isString(value)) {
|
90 | return false;
|
91 | }
|
92 | return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value);
|
93 | }
|
94 | exports.isBase64String = isBase64String;
|
95 | /**
|
96 | * Validates that a value is a non-empty string.
|
97 | *
|
98 | * @param value - The value to validate.
|
99 | * @returns Whether the value is a non-empty string or not.
|
100 | */
|
101 | function isNonEmptyString(value) {
|
102 | return isString(value) && value !== '';
|
103 | }
|
104 | exports.isNonEmptyString = isNonEmptyString;
|
105 | /**
|
106 | * Validates that a value is a nullable object.
|
107 | *
|
108 | * @param value - The value to validate.
|
109 | * @returns Whether the value is an object or not.
|
110 | */
|
111 | function isObject(value) {
|
112 | return typeof value === 'object' && !isArray(value);
|
113 | }
|
114 | exports.isObject = isObject;
|
115 | /**
|
116 | * Validates that a value is a non-null object.
|
117 | *
|
118 | * @param value - The value to validate.
|
119 | * @returns Whether the value is a non-null object or not.
|
120 | */
|
121 | function isNonNullObject(value) {
|
122 | return isObject(value) && value !== null;
|
123 | }
|
124 | exports.isNonNullObject = isNonNullObject;
|
125 | /**
|
126 | * Validates that a string is a valid Firebase Auth uid.
|
127 | *
|
128 | * @param uid - The string to validate.
|
129 | * @returns Whether the string is a valid Firebase Auth uid.
|
130 | */
|
131 | function isUid(uid) {
|
132 | return typeof uid === 'string' && uid.length > 0 && uid.length <= 128;
|
133 | }
|
134 | exports.isUid = isUid;
|
135 | /**
|
136 | * Validates that a string is a valid Firebase Auth password.
|
137 | *
|
138 | * @param password - The password string to validate.
|
139 | * @returns Whether the string is a valid Firebase Auth password.
|
140 | */
|
141 | function isPassword(password) {
|
142 | // A password must be a string of at least 6 characters.
|
143 | return typeof password === 'string' && password.length >= 6;
|
144 | }
|
145 | exports.isPassword = isPassword;
|
146 | /**
|
147 | * Validates that a string is a valid email.
|
148 | *
|
149 | * @param email - The string to validate.
|
150 | * @returns Whether the string is valid email or not.
|
151 | */
|
152 | function isEmail(email) {
|
153 | if (typeof email !== 'string') {
|
154 | return false;
|
155 | }
|
156 | // There must at least one character before the @ symbol and another after.
|
157 | var re = /^[^@]+@[^@]+$/;
|
158 | return re.test(email);
|
159 | }
|
160 | exports.isEmail = isEmail;
|
161 | /**
|
162 | * Validates that a string is a valid phone number.
|
163 | *
|
164 | * @param phoneNumber - The string to validate.
|
165 | * @returns Whether the string is a valid phone number or not.
|
166 | */
|
167 | function isPhoneNumber(phoneNumber) {
|
168 | if (typeof phoneNumber !== 'string') {
|
169 | return false;
|
170 | }
|
171 | // Phone number validation is very lax here. Backend will enforce E.164
|
172 | // spec compliance and will normalize accordingly.
|
173 | // The phone number string must be non-empty and starts with a plus sign.
|
174 | var re1 = /^\+/;
|
175 | // The phone number string must contain at least one alphanumeric character.
|
176 | var re2 = /[\da-zA-Z]+/;
|
177 | return re1.test(phoneNumber) && re2.test(phoneNumber);
|
178 | }
|
179 | exports.isPhoneNumber = isPhoneNumber;
|
180 | /**
|
181 | * Validates that a string is a valid ISO date string.
|
182 | *
|
183 | * @param dateString - The string to validate.
|
184 | * @returns Whether the string is a valid ISO date string.
|
185 | */
|
186 | function isISODateString(dateString) {
|
187 | try {
|
188 | return isNonEmptyString(dateString) &&
|
189 | (new Date(dateString).toISOString() === dateString);
|
190 | }
|
191 | catch (e) {
|
192 | return false;
|
193 | }
|
194 | }
|
195 | exports.isISODateString = isISODateString;
|
196 | /**
|
197 | * Validates that a string is a valid UTC date string.
|
198 | *
|
199 | * @param dateString - The string to validate.
|
200 | * @returns Whether the string is a valid UTC date string.
|
201 | */
|
202 | function isUTCDateString(dateString) {
|
203 | try {
|
204 | return isNonEmptyString(dateString) &&
|
205 | (new Date(dateString).toUTCString() === dateString);
|
206 | }
|
207 | catch (e) {
|
208 | return false;
|
209 | }
|
210 | }
|
211 | exports.isUTCDateString = isUTCDateString;
|
212 | /**
|
213 | * Validates that a string is a valid web URL.
|
214 | *
|
215 | * @param urlStr - The string to validate.
|
216 | * @returns Whether the string is valid web URL or not.
|
217 | */
|
218 | function isURL(urlStr) {
|
219 | if (typeof urlStr !== 'string') {
|
220 | return false;
|
221 | }
|
222 | // Lookup illegal characters.
|
223 | var re = /[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i;
|
224 | if (re.test(urlStr)) {
|
225 | return false;
|
226 | }
|
227 | try {
|
228 | var uri = url.parse(urlStr);
|
229 | var scheme = uri.protocol;
|
230 | var slashes = uri.slashes;
|
231 | var hostname = uri.hostname;
|
232 | var pathname = uri.pathname;
|
233 | if ((scheme !== 'http:' && scheme !== 'https:') || !slashes) {
|
234 | return false;
|
235 | }
|
236 | // Validate hostname: Can contain letters, numbers, underscore and dashes separated by a dot.
|
237 | // Each zone must not start with a hyphen or underscore.
|
238 | if (!hostname || !/^[a-zA-Z0-9]+[\w-]*([.]?[a-zA-Z0-9]+[\w-]*)*$/.test(hostname)) {
|
239 | return false;
|
240 | }
|
241 | // Allow for pathnames: (/chars+)*/?
|
242 | // Where chars can be a combination of: a-z A-Z 0-9 - _ . ~ ! $ & ' ( ) * + , ; = : @ %
|
243 | var pathnameRe = /^(\/[\w\-.~!$'()*+,;=:@%]+)*\/?$/;
|
244 | // Validate pathname.
|
245 | if (pathname &&
|
246 | pathname !== '/' &&
|
247 | !pathnameRe.test(pathname)) {
|
248 | return false;
|
249 | }
|
250 | // Allow any query string and hash as long as no invalid character is used.
|
251 | }
|
252 | catch (e) {
|
253 | return false;
|
254 | }
|
255 | return true;
|
256 | }
|
257 | exports.isURL = isURL;
|
258 | /**
|
259 | * Validates that the provided topic is a valid FCM topic name.
|
260 | *
|
261 | * @param topic - The topic to validate.
|
262 | * @returns Whether the provided topic is a valid FCM topic name.
|
263 | */
|
264 | function isTopic(topic) {
|
265 | if (typeof topic !== 'string') {
|
266 | return false;
|
267 | }
|
268 | var VALID_TOPIC_REGEX = /^(\/topics\/)?(private\/)?[a-zA-Z0-9-_.~%]+$/;
|
269 | return VALID_TOPIC_REGEX.test(topic);
|
270 | }
|
271 | exports.isTopic = isTopic;
|