mosquito-transport 1.4.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.
- package/LICENSE +21 -0
- package/README.md +138 -0
- package/lib/helpers/EnginePath.js +8 -0
- package/lib/helpers/SocketHandler.d.ts +6 -0
- package/lib/helpers/SocketHandler.js +5 -0
- package/lib/helpers/listeners.js +5 -0
- package/lib/helpers/utils.js +105 -0
- package/lib/helpers/values.js +76 -0
- package/lib/helpers/variables.js +13 -0
- package/lib/index.d.ts +442 -0
- package/lib/index.js +1038 -0
- package/lib/products/auth/appleAuth.js +3 -0
- package/lib/products/auth/customAuth.js +267 -0
- package/lib/products/auth/facebookAuth.js +3 -0
- package/lib/products/auth/fallbackAuth.js +3 -0
- package/lib/products/auth/githubAuth.js +3 -0
- package/lib/products/auth/googleAuth.js +106 -0
- package/lib/products/auth/index.js +242 -0
- package/lib/products/auth/tokenizer.js +125 -0
- package/lib/products/auth/twitterAuth.js +3 -0
- package/lib/products/database/base.d.ts +3 -0
- package/lib/products/database/base.js +16 -0
- package/lib/products/database/index.js +644 -0
- package/lib/products/storage/index.js +172 -0
- package/package.json +67 -0
- package/rollup.config.js +34 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { Auth, Db, Document, MongoClient, SortDirection, UpdateDescription } from "mongodb";
|
|
2
|
+
import express from "express";
|
|
3
|
+
import { CorsOptions } from "cors";
|
|
4
|
+
import { Sort } from "mongodb";
|
|
5
|
+
import { Filter } from "mongodb";
|
|
6
|
+
import { UpdateFilter } from "mongodb";
|
|
7
|
+
import type { IncomingHttpHeaders } from "http";
|
|
8
|
+
import type { ParsedUrlQuery } from "querystring";
|
|
9
|
+
import { Socket } from "socket.io";
|
|
10
|
+
import { TokenPayload } from "google-auth-library";
|
|
11
|
+
|
|
12
|
+
interface SimpleError {
|
|
13
|
+
simpleError?: {
|
|
14
|
+
error: string;
|
|
15
|
+
message: string;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface PureHttpRequest extends express.Request {
|
|
20
|
+
res: undefined
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface StorageRulesSnapshot {
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface BatchUpdateValue {
|
|
28
|
+
scope: 'setOne' | 'setMany' | 'updateOne' | 'mergeOne' | 'deleteOne' | 'deleteMany' | 'replaceOne' | 'putOne';
|
|
29
|
+
find?: DatabaseRulesIOPrescription['find'];
|
|
30
|
+
value?: DatabaseRulesIOPrescription['value'];
|
|
31
|
+
path: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface DatabaseRulesIOPrescription {
|
|
35
|
+
path?: string;
|
|
36
|
+
direction?: SortDirection;
|
|
37
|
+
sort?: Sort;
|
|
38
|
+
limit?: number;
|
|
39
|
+
random?: boolean;
|
|
40
|
+
find?: Filter<undefined> | {} | undefined;
|
|
41
|
+
value?: UpdateFilter<undefined> | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface DatabaseRulesBatchWritePrescription {
|
|
45
|
+
value: BatchUpdateValue[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface DatabaseRulesSnapshot {
|
|
49
|
+
auth?: JWTAuthData | undefined;
|
|
50
|
+
endpoint: '_readDocument' | '_queryCollection' | '_writeDocument' | '_writeMapDocument' | '_documentCount' | '_listenCollection' | '_listenDocument' | '_startDisconnectWriteTask' | '_cancelDisconnectWriteTask';
|
|
51
|
+
prescription?: DatabaseRulesIOPrescription | DatabaseRulesBatchWritePrescription;
|
|
52
|
+
dbName?: string;
|
|
53
|
+
dbUrl?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type LogLevel = 'all' | 'disabled' | 'auth' | 'database' | 'storage' | 'external-requests' | 'served-content' | 'database-snapshot';
|
|
57
|
+
|
|
58
|
+
interface GoogleAuthConfig {
|
|
59
|
+
clientID?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface AppleAuthConfig {
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface FacebookAuthConfig {
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface GithubAuthConfig {
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface TwitterAuthConfig {
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface FallbackAuthConfig {
|
|
79
|
+
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface SneakSignupAuthConfig {
|
|
83
|
+
email?: string;
|
|
84
|
+
password?: string;
|
|
85
|
+
photo?: string;
|
|
86
|
+
name?: string;
|
|
87
|
+
metadata: Object
|
|
88
|
+
token?: string;
|
|
89
|
+
request: express.Request;
|
|
90
|
+
method: 'custom' | 'google' | 'apple' | 'github' | 'twitter' | 'facebook';
|
|
91
|
+
providerData?: TokenPayload;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
interface StaticContentProps {
|
|
95
|
+
/**
|
|
96
|
+
* Enable or disable accepting ranged requests, defaults to true.
|
|
97
|
+
* Disabling this will not send Accept-Ranges and ignore the contents of the Range request header.
|
|
98
|
+
*/
|
|
99
|
+
acceptRanges?: boolean | undefined;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Enable or disable setting Cache-Control response header, defaults to true.
|
|
103
|
+
* Disabling this will ignore the maxAge option.
|
|
104
|
+
*/
|
|
105
|
+
cacheControl?: boolean | undefined;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Set how "dotfiles" are treated when encountered.
|
|
109
|
+
* A dotfile is a file or directory that begins with a dot (".").
|
|
110
|
+
* Note this check is done on the path itself without checking if the path actually exists on the disk.
|
|
111
|
+
* If root is specified, only the dotfiles above the root are checked (i.e. the root itself can be within a dotfile when when set to "deny").
|
|
112
|
+
* 'allow' No special treatment for dotfiles.
|
|
113
|
+
* 'deny' Send a 403 for any request for a dotfile.
|
|
114
|
+
* 'ignore' Pretend like the dotfile does not exist and 404.
|
|
115
|
+
* The default value is similar to 'ignore', with the exception that this default will not ignore the files within a directory that begins with a dot, for backward-compatibility.
|
|
116
|
+
*/
|
|
117
|
+
dotfiles?: "allow" | "deny" | "ignore" | undefined;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Byte offset at which the stream ends, defaults to the length of the file minus 1.
|
|
121
|
+
* The end is inclusive in the stream, meaning end: 3 will include the 4th byte in the stream.
|
|
122
|
+
*/
|
|
123
|
+
end?: number | undefined;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Enable or disable etag generation, defaults to true.
|
|
127
|
+
*/
|
|
128
|
+
etag?: boolean | undefined;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* If a given file doesn't exist, try appending one of the given extensions, in the given order.
|
|
132
|
+
* By default, this is disabled (set to false).
|
|
133
|
+
* An example value that will serve extension-less HTML files: ['html', 'htm'].
|
|
134
|
+
* This is skipped if the requested file already has an extension.
|
|
135
|
+
*/
|
|
136
|
+
extensions?: string[] | string | boolean | undefined;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Enable or disable the immutable directive in the Cache-Control response header, defaults to false.
|
|
140
|
+
* If set to true, the maxAge option should also be specified to enable caching.
|
|
141
|
+
* The immutable directive will prevent supported clients from making conditional requests during the life of the maxAge option to check if the file has changed.
|
|
142
|
+
* @default false
|
|
143
|
+
*/
|
|
144
|
+
immutable?: boolean | undefined;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* By default send supports "index.html" files, to disable this set false or to supply a new index pass a string or an array in preferred order.
|
|
148
|
+
*/
|
|
149
|
+
index?: string[] | string | boolean | undefined;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Enable or disable Last-Modified header, defaults to true.
|
|
153
|
+
* Uses the file system's last modified value.
|
|
154
|
+
*/
|
|
155
|
+
lastModified?: boolean | undefined;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Provide a max-age in milliseconds for http caching, defaults to 0.
|
|
159
|
+
* This can also be a string accepted by the ms module.
|
|
160
|
+
*/
|
|
161
|
+
maxAge?: string | number | undefined;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Serve files relative to path.
|
|
165
|
+
*/
|
|
166
|
+
root?: string | undefined;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Byte offset at which the stream starts, defaults to 0.
|
|
170
|
+
* The start is inclusive, meaning start: 2 will include the 3rd byte in the stream.
|
|
171
|
+
*/
|
|
172
|
+
start?: number | undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface SneakSignupAuthResult {
|
|
176
|
+
metadata?: AuthData['metadata'];
|
|
177
|
+
profile?: AuthData['profile'];
|
|
178
|
+
uid?: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface MSocketHandshake {
|
|
182
|
+
/**
|
|
183
|
+
* The headers sent as part of the handshake
|
|
184
|
+
*/
|
|
185
|
+
headers: IncomingHttpHeaders;
|
|
186
|
+
/**
|
|
187
|
+
* The date of creation (as string)
|
|
188
|
+
*/
|
|
189
|
+
time: string;
|
|
190
|
+
/**
|
|
191
|
+
* The ip of the client
|
|
192
|
+
*/
|
|
193
|
+
address: string;
|
|
194
|
+
/**
|
|
195
|
+
* Whether the connection is cross-domain
|
|
196
|
+
*/
|
|
197
|
+
xdomain: boolean;
|
|
198
|
+
/**
|
|
199
|
+
* Whether the connection is secure
|
|
200
|
+
*/
|
|
201
|
+
secure: boolean;
|
|
202
|
+
/**
|
|
203
|
+
* The date of creation (as unix timestamp)
|
|
204
|
+
*/
|
|
205
|
+
issued: number;
|
|
206
|
+
/**
|
|
207
|
+
* The request URL string
|
|
208
|
+
*/
|
|
209
|
+
url: string;
|
|
210
|
+
/**
|
|
211
|
+
* The query object
|
|
212
|
+
*/
|
|
213
|
+
query: ParsedUrlQuery;
|
|
214
|
+
/**
|
|
215
|
+
* The user that made this request
|
|
216
|
+
*/
|
|
217
|
+
user?: Promise<AuthData>;
|
|
218
|
+
/**
|
|
219
|
+
* The auth object
|
|
220
|
+
*/
|
|
221
|
+
auth: {
|
|
222
|
+
[key: string]: any;
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* the access token of the user that initiated this handshake
|
|
226
|
+
*/
|
|
227
|
+
userToken: string | undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
interface MSocketSnapshot {
|
|
231
|
+
on: Socket['on'];
|
|
232
|
+
once: Socket['once'];
|
|
233
|
+
prependOnceListener: Socket['prependOnceListener'];
|
|
234
|
+
handshake: MSocketHandshake;
|
|
235
|
+
emit: Socket['emit'];
|
|
236
|
+
emitWithAck: Socket['emitWithAck'];
|
|
237
|
+
timeout: (timeout: number) => ({
|
|
238
|
+
emitWithAck: Socket['emitWithAck'];
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
interface MSocketError {
|
|
243
|
+
error: string;
|
|
244
|
+
message: string;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
interface TransformMediaOption {
|
|
248
|
+
localBuffer: Buffer;
|
|
249
|
+
request?: express.Request;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
interface TransformMediaRoute {
|
|
253
|
+
route: typeof RegExp | string;
|
|
254
|
+
type?: string;
|
|
255
|
+
transform: (options: TransformMediaOption) => Buffer | string | null | undefined;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
interface MongoInstances {
|
|
259
|
+
defaultName?: string;
|
|
260
|
+
instance: MongoClient;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
interface MongoInstancesMap {
|
|
264
|
+
[key: 'default' | 'admin' | string]: MongoInstances;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interface MosquitoServerConfig {
|
|
268
|
+
projectName: string;
|
|
269
|
+
signerKey: string;
|
|
270
|
+
storageRules: (snapshot?: StorageRulesSnapshot) => Promise<void> | undefined;
|
|
271
|
+
databaseRules: (snapshot?: DatabaseRulesSnapshot) => Promise<void> | undefined;
|
|
272
|
+
onSocketSnapshot?: (snapshot?: MSocketSnapshot, error?: MSocketError) => void;
|
|
273
|
+
port?: number;
|
|
274
|
+
enableSequentialUid?: boolean;
|
|
275
|
+
accessKey: string;
|
|
276
|
+
logger?: LogLevel | LogLevel[];
|
|
277
|
+
externalAddress?: string;
|
|
278
|
+
hostname?: string;
|
|
279
|
+
mongoInstances: MongoInstancesMap;
|
|
280
|
+
mergeAuthAccount?: boolean;
|
|
281
|
+
transformMediaRoute?: '*' | TransformMediaRoute[];
|
|
282
|
+
transformMediaCleanupTimeout?: string;
|
|
283
|
+
sneakSignupAuth?: (config: SneakSignupAuthConfig) => SneakSignupAuthResult;
|
|
284
|
+
googleAuthConfig?: GoogleAuthConfig;
|
|
285
|
+
appleAuthConfig?: AppleAuthConfig;
|
|
286
|
+
facebookAuthConfig?: FacebookAuthConfig;
|
|
287
|
+
githubAuthConfig?: GithubAuthConfig;
|
|
288
|
+
twitterAuthConfig?: TwitterAuthConfig;
|
|
289
|
+
fallbackAuthConfig?: FallbackAuthConfig;
|
|
290
|
+
staticContentProps?: StaticContentProps;
|
|
291
|
+
staticContentMaxAge?: number;
|
|
292
|
+
staticContentCacheControl?: number;
|
|
293
|
+
corsOrigin?: CorsOptions;
|
|
294
|
+
maxRequestBufferSize?: number;
|
|
295
|
+
maxUploadBufferSize?: number;
|
|
296
|
+
uidLength?: number;
|
|
297
|
+
accessTokenInterval?: number;
|
|
298
|
+
refreshTokenExpiry?: number;
|
|
299
|
+
dumpsterPath?: string;
|
|
300
|
+
e2eKeyPair?: string[] | undefined;
|
|
301
|
+
enforceE2E?: boolean;
|
|
302
|
+
preMiddlewares?: Function[] | Function;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
interface UserProfile {
|
|
306
|
+
email?: string,
|
|
307
|
+
name?: string,
|
|
308
|
+
photo?: string,
|
|
309
|
+
phoneNumber?: string,
|
|
310
|
+
bio?: string
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
interface AuthData {
|
|
314
|
+
email?: string;
|
|
315
|
+
metadata: Object;
|
|
316
|
+
signupMethod: 'google' | 'apple' | 'custom' | 'twitter' | 'facebook' | 'github' | string;
|
|
317
|
+
joinedOn: number;
|
|
318
|
+
uid: string;
|
|
319
|
+
claims: Object;
|
|
320
|
+
emailVerified: boolean;
|
|
321
|
+
profile: UserProfile;
|
|
322
|
+
disabled: boolean;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
interface UserData extends AuthData {
|
|
326
|
+
password?: string;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
interface JWTAuthData extends AuthData {
|
|
330
|
+
token: string;
|
|
331
|
+
exp?: number;
|
|
332
|
+
aud?: string;
|
|
333
|
+
iss?: string;
|
|
334
|
+
sub?: string;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
interface NewUserAuthData extends AuthData {
|
|
338
|
+
password?: string;
|
|
339
|
+
google_sub?: string;
|
|
340
|
+
apple_sub?: string;
|
|
341
|
+
twitter_sub?: string;
|
|
342
|
+
github_sub?: string;
|
|
343
|
+
facebook_sub?: string;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
interface MosquitoDbHttpOptions {
|
|
347
|
+
enforceUser?: boolean;
|
|
348
|
+
validateUser?: boolean;
|
|
349
|
+
enforceVerifiedUser?: boolean;
|
|
350
|
+
rawEntry?: boolean;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
interface DatabaseListenerOption {
|
|
354
|
+
includeBeforeData?: boolean;
|
|
355
|
+
includeAfterData?: boolean;
|
|
356
|
+
pipeline?: { pipeline?: Document[] }
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
interface DatabaseListenerCallbackData {
|
|
360
|
+
insertion?: { _id: string };
|
|
361
|
+
deletion?: string;
|
|
362
|
+
update?: UpdateDescription,
|
|
363
|
+
before?: Document,
|
|
364
|
+
after?: Document,
|
|
365
|
+
timestamp: number,
|
|
366
|
+
auth?: AuthData | undefined,
|
|
367
|
+
operation: 'insert' | 'delete' | 'update';
|
|
368
|
+
documentKey: string;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
interface WriteCommand {
|
|
372
|
+
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
interface DisconnectTaskInspector extends SimpleError {
|
|
376
|
+
status?: 'completed' | 'error' | 'cancelled';
|
|
377
|
+
committed?: boolean;
|
|
378
|
+
task?: ({
|
|
379
|
+
commands: WriteCommand;
|
|
380
|
+
dbName?: string;
|
|
381
|
+
dbUrl?: string;
|
|
382
|
+
})
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
interface StorageSnapshot {
|
|
386
|
+
systemDest: string;
|
|
387
|
+
dest: string;
|
|
388
|
+
buffer?: Buffer;
|
|
389
|
+
operation: 'uploadFile' | 'deleteFile' | 'deleteFolder';
|
|
390
|
+
auth?: JWTAuthData;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
interface RawBodyRequest extends express.Request {
|
|
394
|
+
rawBody: Buffer;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export default class MosquitoDbServer {
|
|
398
|
+
constructor(config: MosquitoServerConfig);
|
|
399
|
+
|
|
400
|
+
getDatabase(dbName?: string, dbUrl?: string): Db;
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* verify token to check if it was trully created using signerKey without checking against the expiry or local token reference
|
|
404
|
+
*
|
|
405
|
+
* @param token - the token to be verified
|
|
406
|
+
* @param isRefreshToken - set this to true if token is a refresh token
|
|
407
|
+
*/
|
|
408
|
+
verifyToken(token: string, isRefreshToken?: boolean): Promise<AuthData>;
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* verify token to check if it was trully created using signerKey and checking against the expiry and local token reference
|
|
412
|
+
*
|
|
413
|
+
* @param token - the token to be validated
|
|
414
|
+
* @param isRefreshToken - set this to true if token is a refresh token
|
|
415
|
+
*/
|
|
416
|
+
validateToken(token: string, isRefreshToken?: boolean): Promise<AuthData>;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* remove local reference of a token
|
|
420
|
+
*
|
|
421
|
+
* @param token - the token to be invalidated
|
|
422
|
+
* @param isRefreshToken - set this to true if token is a refresh token
|
|
423
|
+
*/
|
|
424
|
+
invalidateToken(token: string, isRefreshToken?: boolean): Promise<void | boolean>;
|
|
425
|
+
listenHttpsRequest(route: string, callback?: (request: RawBodyRequest, response: express.Response, auth?: JWTAuthData | null) => void, options?: MosquitoDbHttpOptions): void;
|
|
426
|
+
listenDatabase(collection: string, callback?: (data: DatabaseListenerCallbackData) => void, options?: DatabaseListenerOption): void;
|
|
427
|
+
listenStorage(callback?: (snapshot: StorageSnapshot) => void): () => void;
|
|
428
|
+
uploadBuffer(destination: string, buffer: Buffer): Promise<string>;
|
|
429
|
+
deleteFile(path: string): Promise<void>;
|
|
430
|
+
deleteFolder(path: string): Promise<void>;
|
|
431
|
+
listenNewUser(callback?: (user: NewUserAuthData) => void): void;
|
|
432
|
+
listenDeletedUser(callback?: (uid: string) => void): void;
|
|
433
|
+
inspectDocDisconnectionTask(callback?: (data: DisconnectTaskInspector) => void): void;
|
|
434
|
+
updateUserProfile(uid: string, profile: UserProfile): Promise<void>;
|
|
435
|
+
updateUserClaims(uid: string, claims: Object): Promise<void>;
|
|
436
|
+
updateUserEmailAddress(uid: string, email: string): Promise<void>;
|
|
437
|
+
updateUserPassword(uid: string, password: string): Promise<void>;
|
|
438
|
+
updateUserEmailVerify(uid: string, verified: boolean): Promise<void>;
|
|
439
|
+
disableUser(uid: string, disable: boolean): Promise<void>;
|
|
440
|
+
getUserData(uid: string): Promise<UserData>;
|
|
441
|
+
linkToFile(link: string): string;
|
|
442
|
+
}
|