prostgles-server 4.0.16 → 4.0.18

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 (135) hide show
  1. package/dist/AuthHandler.js +214 -210
  2. package/dist/AuthHandler.js.map +1 -1
  3. package/dist/DBEventsManager.js +34 -31
  4. package/dist/DBEventsManager.js.map +1 -1
  5. package/dist/DBSchemaBuilder.js +3 -2
  6. package/dist/DBSchemaBuilder.js.map +1 -1
  7. package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts +19 -14
  8. package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
  9. package/dist/DboBuilder/QueryBuilder/QueryBuilder.js +159 -151
  10. package/dist/DboBuilder/QueryBuilder/QueryBuilder.js.map +1 -1
  11. package/dist/DboBuilder/TableHandler.d.ts +9 -14
  12. package/dist/DboBuilder/TableHandler.d.ts.map +1 -1
  13. package/dist/DboBuilder/TableHandler.js +21 -41
  14. package/dist/DboBuilder/TableHandler.js.map +1 -1
  15. package/dist/DboBuilder/ViewHandler.d.ts +3 -3
  16. package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
  17. package/dist/DboBuilder/ViewHandler.js +24 -9
  18. package/dist/DboBuilder/ViewHandler.js.map +1 -1
  19. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts +1 -1
  20. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -1
  21. package/dist/DboBuilder/parseUpdateRules.d.ts.map +1 -1
  22. package/dist/DboBuilder/parseUpdateRules.js.map +1 -1
  23. package/dist/DboBuilder/runSQL.js +5 -5
  24. package/dist/DboBuilder/runSQL.js.map +1 -1
  25. package/dist/DboBuilder.d.ts.map +1 -1
  26. package/dist/DboBuilder.js +85 -78
  27. package/dist/DboBuilder.js.map +1 -1
  28. package/dist/FileManager/FileManager.d.ts +143 -0
  29. package/dist/FileManager/FileManager.d.ts.map +1 -0
  30. package/dist/FileManager/FileManager.js +278 -0
  31. package/dist/FileManager/FileManager.js.map +1 -0
  32. package/dist/FileManager/initFileManager.d.ts +4 -0
  33. package/dist/FileManager/initFileManager.d.ts.map +1 -0
  34. package/dist/FileManager/initFileManager.js +209 -0
  35. package/dist/FileManager/initFileManager.js.map +1 -0
  36. package/dist/FileManager/parseFile.d.ts +15 -0
  37. package/dist/FileManager/parseFile.d.ts.map +1 -0
  38. package/dist/FileManager/parseFile.js +59 -0
  39. package/dist/FileManager/parseFile.js.map +1 -0
  40. package/dist/FileManager/upload.d.ts +6 -0
  41. package/dist/FileManager/upload.d.ts.map +1 -0
  42. package/dist/FileManager/upload.js +76 -0
  43. package/dist/FileManager/upload.js.map +1 -0
  44. package/dist/FileManager/uploadStream.d.ts +5 -0
  45. package/dist/FileManager/uploadStream.d.ts.map +1 -0
  46. package/dist/FileManager/uploadStream.js +70 -0
  47. package/dist/FileManager/uploadStream.js.map +1 -0
  48. package/dist/Filtering.d.ts +1 -1
  49. package/dist/Filtering.d.ts.map +1 -1
  50. package/dist/PostgresNotifListenManager.js +26 -20
  51. package/dist/PostgresNotifListenManager.js.map +1 -1
  52. package/dist/Prostgles.d.ts +2 -2
  53. package/dist/Prostgles.d.ts.map +1 -1
  54. package/dist/Prostgles.js +192 -172
  55. package/dist/Prostgles.js.map +1 -1
  56. package/dist/PubSubManager/PubSubManager.d.ts +3 -3
  57. package/dist/PubSubManager/PubSubManager.js +164 -154
  58. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  59. package/dist/PublishParser.d.ts +13 -13
  60. package/dist/PublishParser.d.ts.map +1 -1
  61. package/dist/PublishParser.js +8 -2
  62. package/dist/PublishParser.js.map +1 -1
  63. package/dist/SchemaWatch.js +2 -1
  64. package/dist/SchemaWatch.js.map +1 -1
  65. package/dist/TableConfig/TableConfig.d.ts.map +1 -1
  66. package/dist/TableConfig/TableConfig.js +100 -92
  67. package/dist/TableConfig/TableConfig.js.map +1 -1
  68. package/lib/AuthHandler.js +1 -1
  69. package/lib/AuthHandler.ts +1 -1
  70. package/lib/DBSchemaBuilder.js +3 -2
  71. package/lib/DBSchemaBuilder.ts +3 -2
  72. package/lib/DboBuilder/TableHandler.d.ts +4 -9
  73. package/lib/DboBuilder/TableHandler.d.ts.map +1 -1
  74. package/lib/DboBuilder/TableHandler.js +1 -22
  75. package/lib/DboBuilder/TableHandler.ts +3 -31
  76. package/lib/DboBuilder/ViewHandler.ts +1 -1
  77. package/lib/DboBuilder/getSubscribeRelatedTables.d.ts +1 -1
  78. package/lib/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -1
  79. package/lib/DboBuilder/getSubscribeRelatedTables.ts +1 -1
  80. package/lib/DboBuilder/parseUpdateRules.d.ts.map +1 -1
  81. package/lib/DboBuilder/parseUpdateRules.ts +2 -2
  82. package/lib/DboBuilder.d.ts.map +1 -1
  83. package/lib/DboBuilder.js +0 -13
  84. package/lib/DboBuilder.ts +1 -16
  85. package/lib/{FileManager.d.ts → FileManager/FileManager.d.ts} +12 -20
  86. package/lib/FileManager/FileManager.d.ts.map +1 -0
  87. package/lib/FileManager/FileManager.js +303 -0
  88. package/lib/FileManager/FileManager.ts +378 -0
  89. package/lib/FileManager/initFileManager.d.ts +4 -0
  90. package/lib/FileManager/initFileManager.d.ts.map +1 -0
  91. package/lib/FileManager/initFileManager.js +231 -0
  92. package/lib/FileManager/initFileManager.ts +236 -0
  93. package/lib/FileManager/parseFile.d.ts +15 -0
  94. package/lib/FileManager/parseFile.d.ts.map +1 -0
  95. package/lib/FileManager/parseFile.js +58 -0
  96. package/lib/FileManager/parseFile.ts +69 -0
  97. package/lib/FileManager/upload.d.ts +6 -0
  98. package/lib/FileManager/upload.d.ts.map +1 -0
  99. package/lib/FileManager/upload.js +98 -0
  100. package/lib/FileManager/upload.ts +83 -0
  101. package/lib/FileManager/uploadStream.d.ts +5 -0
  102. package/lib/FileManager/uploadStream.d.ts.map +1 -0
  103. package/lib/FileManager/uploadStream.js +92 -0
  104. package/lib/FileManager/uploadStream.ts +80 -0
  105. package/lib/Filtering.d.ts +1 -1
  106. package/lib/Filtering.d.ts.map +1 -1
  107. package/lib/Filtering.ts +1 -1
  108. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +1 -1
  109. package/lib/Prostgles.d.ts +2 -2
  110. package/lib/Prostgles.d.ts.map +1 -1
  111. package/lib/Prostgles.js +8 -5
  112. package/lib/Prostgles.ts +10 -6
  113. package/lib/PublishParser.d.ts +13 -13
  114. package/lib/PublishParser.d.ts.map +1 -1
  115. package/lib/PublishParser.js +1 -0
  116. package/lib/PublishParser.ts +13 -12
  117. package/lib/TableConfig/TableConfig.d.ts.map +1 -1
  118. package/lib/TableConfig/TableConfig.js +12 -7
  119. package/lib/TableConfig/TableConfig.ts +13 -8
  120. package/package.json +3 -3
  121. package/tests/client/PID.txt +1 -1
  122. package/tests/client/index.d.ts.map +1 -1
  123. package/tests/client/index.ts +1 -3
  124. package/tests/client/package-lock.json +31 -4561
  125. package/tests/client/package.json +2 -2
  126. package/tests/client/tsconfig.json +1 -0
  127. package/tests/isomorphic_queries.d.ts.map +1 -1
  128. package/tests/isomorphic_queries.ts +3 -3
  129. package/tests/server/package-lock.json +13 -13
  130. package/tests/server/package.json +1 -1
  131. package/tests/server/tsconfig.json +1 -0
  132. package/tsconfig.json +3 -2
  133. package/lib/FileManager.d.ts.map +0 -1
  134. package/lib/FileManager.js +0 -676
  135. package/lib/FileManager.ts +0 -811
@@ -1,676 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.bytesToSize = exports.getFileType = exports.getFileTypeFromFilename = exports.removeExpressRoute = exports.asSQLIdentifier = void 0;
30
- const aws_sdk_1 = require("aws-sdk");
31
- // import { PutObjectCommand, S3 } from "@aws-sdk/client-s3";
32
- const fs = __importStar(require("fs"));
33
- const stream = __importStar(require("stream"));
34
- const sharp = __importStar(require("sharp"));
35
- const check_disk_space_1 = __importDefault(require("check-disk-space"));
36
- const prostgles_types_1 = require("prostgles-types");
37
- const HOUR = 3600 * 1000;
38
- const asSQLIdentifier = async (name, db) => {
39
- return (await db.one("select format('%I', $1) as name", [name]))?.name;
40
- };
41
- exports.asSQLIdentifier = asSQLIdentifier;
42
- const aws_sdk_2 = __importDefault(require("aws-sdk"));
43
- const runSQL_1 = require("./DboBuilder/runSQL");
44
- const path = __importStar(require("path"));
45
- const ViewHandler_1 = require("./DboBuilder/ViewHandler");
46
- class FileManager {
47
- static testCredentials = async (accessKeyId, secretAccessKey) => {
48
- const sts = new aws_sdk_2.default.STS();
49
- aws_sdk_2.default.config.credentials = {
50
- accessKeyId,
51
- secretAccessKey
52
- };
53
- const ident = await sts.getCallerIdentity({}).promise();
54
- return ident;
55
- };
56
- s3Client;
57
- config;
58
- imageOptions;
59
- prostgles;
60
- get dbo() {
61
- if (!this.prostgles?.dbo) {
62
- // this.prostgles?.refreshDBO();
63
- throw "this.prostgles.dbo missing";
64
- }
65
- return this.prostgles.dbo;
66
- }
67
- get db() {
68
- if (!this.prostgles?.db)
69
- throw "this.prostgles.db missing";
70
- return this.prostgles.db;
71
- }
72
- tableName;
73
- fileRoute;
74
- get fileRouteExpress() {
75
- return this.fileRoute + "/:name";
76
- }
77
- checkInterval;
78
- constructor(config, imageOptions) {
79
- this.config = config;
80
- this.imageOptions = imageOptions;
81
- if ("region" in config) {
82
- const { region, accessKeyId, secretAccessKey } = config;
83
- this.s3Client = new aws_sdk_1.S3({
84
- region,
85
- credentials: { accessKeyId, secretAccessKey },
86
- });
87
- }
88
- const fullConfig = this.prostgles?.opts.fileTable;
89
- if (fullConfig?.delayedDelete) {
90
- this.checkInterval = setInterval(async () => {
91
- const fileTable = fullConfig.tableName;
92
- const daysDelay = fullConfig.delayedDelete?.deleteAfterNDays ?? 0;
93
- if (fileTable && this.dbo[fileTable]?.delete && daysDelay) {
94
- const filesToDelete = await this.dbo[fileTable]?.find?.({ deleted_from_storage: null, deleted: { ">": Date.now() - (daysDelay * HOUR * 24) } }) ?? [];
95
- for await (const file of filesToDelete) {
96
- await this.deleteFile(file.name);
97
- }
98
- }
99
- else {
100
- console.error("FileManager checkInterval delayedDelete FAIL: Could not access file table tableHandler.delete()");
101
- }
102
- }, Math.max(10000, (fullConfig.delayedDelete.checkIntervalHours || 0) * HOUR));
103
- }
104
- }
105
- async getFileStream(name) {
106
- if ("bucket" in this.config && this.s3Client) {
107
- return this.s3Client.getObject({ Key: name, Bucket: this.config.bucket }).createReadStream();
108
- }
109
- else if ("localFolderPath" in this.config) {
110
- const filePath = path.resolve(`${this.config.localFolderPath}/${name}`);
111
- if (!fs.existsSync(filePath)) {
112
- throw `File ${filePath} could not be found`;
113
- }
114
- return fs.createReadStream(filePath, { encoding: undefined });
115
- }
116
- else
117
- throw new Error("Not expected");
118
- }
119
- async deleteFile(name) {
120
- if ("bucket" in this.config && this.s3Client) {
121
- const res = await this.s3Client?.deleteObject({ Bucket: this.config.bucket, Key: name }).promise();
122
- return res;
123
- }
124
- else if ("localFolderPath" in this.config) {
125
- const path = `${this.config.localFolderPath}/${name}`;
126
- if (!fs.existsSync(path)) {
127
- throw `File ${path} could not be found`;
128
- }
129
- fs.unlinkSync(path);
130
- if (fs.existsSync(path))
131
- throw new Error("Could not delete file");
132
- }
133
- return true;
134
- }
135
- async parseFile(args) {
136
- const { file, fileName, tableName, colName } = args;
137
- const config = this.prostgles?.opts.fileTable;
138
- if (!config)
139
- throw new Error("File table config missing");
140
- const buffer = typeof file === "string" ? Buffer.from(file, 'utf8') : file;
141
- const result = await (0, exports.getFileTypeFromFilename)(fileName);
142
- if (tableName && colName) {
143
- const tableConfig = config.referencedTables?.[tableName];
144
- if (tableConfig && (0, prostgles_types_1.isObject)(tableConfig) && tableConfig.referenceColumns[colName]) {
145
- const colConfig = tableConfig.referenceColumns[colName];
146
- if (colConfig.maxFileSizeMB) {
147
- const actualBufferSize = Buffer.byteLength(buffer);
148
- if ((actualBufferSize / 1e6) > colConfig.maxFileSizeMB) {
149
- throw new Error(`Provided file is larger than the ${colConfig.maxFileSizeMB}MB limit`);
150
- }
151
- }
152
- if ("acceptedContent" in colConfig && colConfig.acceptedContent && colConfig.acceptedContent !== "*") {
153
- const mime = await (0, exports.getFileType)(buffer, fileName);
154
- const CONTENTS = [
155
- "image",
156
- "audio",
157
- "video",
158
- "text",
159
- "application",
160
- ];
161
- const allowedContent = ViewHandler_1.ViewHandler._parseFieldFilter(colConfig.acceptedContent, false, CONTENTS);
162
- if (!allowedContent.some(c => mime.mime.startsWith(c))) {
163
- throw new Error(`Dissallowed content type provided: ${mime.mime.split("/")[0]}. Allowed content types: ${allowedContent} `);
164
- }
165
- }
166
- else if ("acceptedContentType" in colConfig && colConfig.acceptedContentType && colConfig.acceptedContentType !== "*") {
167
- const mime = await (0, exports.getFileType)(buffer, fileName);
168
- const allowedContentTypes = ViewHandler_1.ViewHandler._parseFieldFilter(colConfig.acceptedContentType, false, (0, prostgles_types_1.getKeys)(prostgles_types_1.CONTENT_TYPE_TO_EXT));
169
- if (!allowedContentTypes.some(c => c === mime.mime)) {
170
- throw new Error(`Dissallowed MIME provided: ${mime.mime}. Allowed MIME values: ${allowedContentTypes} `);
171
- }
172
- }
173
- else if ("acceptedFileTypes" in colConfig && colConfig.acceptedFileTypes && colConfig.acceptedFileTypes !== "*") {
174
- const mime = await (0, exports.getFileType)(buffer, fileName);
175
- const allowedExtensions = ViewHandler_1.ViewHandler._parseFieldFilter(colConfig.acceptedFileTypes, false, Object.values(prostgles_types_1.CONTENT_TYPE_TO_EXT).flat());
176
- if (!allowedExtensions.some(c => c === mime.ext)) {
177
- throw new Error(`Dissallowed extension provided: ${mime.ext}. Allowed extension values: ${allowedExtensions} `);
178
- }
179
- }
180
- }
181
- }
182
- if (!result?.mime)
183
- throw `File MIME type not found for the provided extension: ${result?.ext}`;
184
- return result;
185
- }
186
- getFileUrl = (name) => this.fileRoute ? `${this.fileRoute}/${name}` : "";
187
- checkFreeSpace = async (folderPath, fileSize = 0) => {
188
- if (!this.s3Client && "localFolderPath" in this.config) {
189
- const { minFreeBytes = 1.048e6 } = this.config;
190
- const required = Math.max(fileSize, minFreeBytes);
191
- if (required) {
192
- const diskSpace = await (0, check_disk_space_1.default)(folderPath);
193
- if (diskSpace.free < required) {
194
- const err = `There is not enough space on the server to save files.\nTotal: ${bytesToSize(diskSpace.size)} \nRemaning: ${bytesToSize(diskSpace.free)} \nRequired: ${bytesToSize(required)}`;
195
- throw new Error(err);
196
- }
197
- }
198
- }
199
- };
200
- uploadStream = (name, mime, onProgress, onError, onEnd, expectedSizeBytes) => {
201
- const passThrough = new stream.PassThrough();
202
- if (!this.s3Client && "localFolderPath" in this.config) {
203
- // throw new Error("S3 config missing. Can only upload streams to S3");
204
- try {
205
- this.checkFreeSpace(this.config.localFolderPath, expectedSizeBytes).catch(err => {
206
- onError?.(err);
207
- passThrough.end();
208
- });
209
- const url = this.getFileUrl(name);
210
- fs.mkdirSync(this.config.localFolderPath, { recursive: true });
211
- const filePath = path.resolve(`${this.config.localFolderPath}/${name}`);
212
- const writeStream = fs.createWriteStream(filePath);
213
- let errored = false;
214
- let loaded = 0;
215
- writeStream.on('error', err => {
216
- errored = true;
217
- onError?.(err);
218
- });
219
- let lastProgress = Date.now();
220
- const throttle = 1000;
221
- if (onProgress) {
222
- passThrough.on('data', function (chunk) {
223
- loaded += chunk.length;
224
- const now = Date.now();
225
- if (now - lastProgress > throttle) {
226
- lastProgress = now;
227
- onProgress?.({ loaded, total: 0 });
228
- }
229
- });
230
- }
231
- if (onEnd)
232
- writeStream.on('finish', () => {
233
- if (errored)
234
- return;
235
- let content_length = 0;
236
- try {
237
- content_length = fs.statSync(filePath).size;
238
- onEnd?.({
239
- url,
240
- filePath,
241
- etag: `none`,
242
- content_length
243
- });
244
- }
245
- catch (err) {
246
- onError?.(err);
247
- }
248
- });
249
- passThrough.pipe(writeStream);
250
- }
251
- catch (err) {
252
- onError?.(err);
253
- }
254
- }
255
- else {
256
- this.upload(passThrough, name, mime, onProgress).then(onEnd)
257
- .catch(onError);
258
- }
259
- return passThrough;
260
- };
261
- async upload(file, name, mime, onProgress) {
262
- return new Promise(async (resolve, reject) => {
263
- if (!file) {
264
- throw "No file. Expecting: Buffer | String | stream";
265
- }
266
- if (!name) {
267
- throw "No name. Expecting: String";
268
- }
269
- // let type = await this.getMIME(file, name, allowedExtensions);
270
- const url = this.getFileUrl(name);
271
- if (!this.s3Client) {
272
- if (file instanceof stream.PassThrough) {
273
- throw new Error("S3 config missing. Can only upload streams to S3");
274
- }
275
- const config = this.config;
276
- try {
277
- await fs.promises.mkdir(config.localFolderPath, { recursive: true });
278
- const filePath = `${config.localFolderPath}/${name}`;
279
- fs.writeFileSync(filePath, file);
280
- resolve({
281
- url,
282
- etag: `none`,
283
- content_length: fs.statSync(filePath).size
284
- });
285
- }
286
- catch (err) {
287
- console.error("Error saving file locally", err);
288
- reject("Internal error");
289
- }
290
- }
291
- else {
292
- /* S3 Upload */
293
- // ACL: "public-read",
294
- /* ACL needs this permission:
295
- "s3:PutObject",
296
- "s3:PutObjectAcl",
297
- "s3:GetObject",
298
- "s3:GetObjectAcl",
299
- */
300
- const params = {
301
- Bucket: this.config.bucket,
302
- Key: name,
303
- ContentType: mime,
304
- Body: file
305
- };
306
- let content_length = 0;
307
- const manager = this.s3Client.upload(params, (err, res) => {
308
- // const manager = this.s3Client.send(new PutObjectCommand(params), (err: Error, res) => {
309
- if (err) {
310
- reject(err.toString());
311
- console.error(err);
312
- }
313
- else {
314
- // console.log("Uploaded file:", res)
315
- resolve({
316
- url,
317
- etag: res.ETag,
318
- s3_url: res.Location,
319
- content_length // await fileMgr.s3Client?.headObject({ Bucket: ..., Key: ... }).promise() ).ContentLength;
320
- });
321
- }
322
- });
323
- manager.on('httpUploadProgress', prog => {
324
- content_length = prog.total;
325
- onProgress?.(prog);
326
- });
327
- }
328
- });
329
- }
330
- uploadAsMedia = async (params) => {
331
- const { item, imageOptions } = params;
332
- const { name, data, content_type, extension } = item;
333
- if (!data)
334
- throw "No file provided";
335
- if (!name || typeof name !== "string")
336
- throw "Expecting a string name";
337
- // const type = await this.getMIME(data, name, allowedExtensions, dissallowedExtensions);
338
- let _data = data;
339
- /** Resize/compress/remove exif from photos */
340
- if (content_type.startsWith("image") && extension.toLowerCase() !== "gif") {
341
- const compression = imageOptions?.compression;
342
- if (compression) {
343
- console.log("Resizing image");
344
- let opts;
345
- if ("contain" in compression) {
346
- opts = {
347
- fit: sharp.fit.contain,
348
- ...compression.contain
349
- };
350
- }
351
- else if ("inside" in compression) {
352
- opts = {
353
- fit: sharp.fit.inside,
354
- ...compression.inside
355
- };
356
- }
357
- _data = await sharp(data)
358
- .resize(opts)
359
- .withMetadata(Boolean(imageOptions?.keepMetadata))
360
- // .jpeg({ quality: 80 })
361
- .toBuffer();
362
- }
363
- else if (!imageOptions?.keepMetadata) {
364
- /**
365
- * Remove exif
366
- */
367
- // const metadata = await simg.metadata();
368
- // const simg = await sharp(data);
369
- _data = await sharp(data).clone().withMetadata({
370
- exif: {}
371
- })
372
- .toBuffer();
373
- }
374
- }
375
- const res = await this.upload(_data, name, content_type);
376
- return res;
377
- };
378
- async getFileS3URL(fileName, expiresInSeconds = 30 * 60) {
379
- const params = {
380
- Bucket: this.config.bucket,
381
- Key: fileName,
382
- Expires: Math.round(expiresInSeconds || 30 * 60) // Error if float
383
- };
384
- return await this.s3Client?.getSignedUrlPromise("getObject", params);
385
- }
386
- parseSQLIdentifier = async (name) => (0, exports.asSQLIdentifier)(name, this.prostgles.db); // this.prostgles.dbo.sql<"value">("select format('%I', $1)", [name], { returnType: "value" } )
387
- getColInfo = (args) => {
388
- const { colName, tableName } = args;
389
- const tableConfig = this.prostgles?.opts.fileTable?.referencedTables?.[tableName];
390
- const isReferencingFileTable = this.dbo[tableName]?.columns?.some(c => c.name === colName && c.references && c.references?.some(({ ftable }) => ftable === this.tableName));
391
- if (isReferencingFileTable) {
392
- if (tableConfig && typeof tableConfig !== "string") {
393
- return tableConfig.referenceColumns[colName];
394
- }
395
- return { acceptedContent: "*" };
396
- }
397
- return undefined;
398
- };
399
- init = async (prg) => {
400
- this.prostgles = prg;
401
- // const { dbo, db, opts } = prg;
402
- const { fileTable } = prg.opts;
403
- if (!fileTable)
404
- throw "fileTable missing";
405
- const { tableName = "media", referencedTables = {} } = fileTable;
406
- this.tableName = tableName;
407
- const maxBfSizeMB = (prg.opts.io?.engine?.opts?.maxHttpBufferSize || 1e6) / 1e6;
408
- console.log(`Prostgles: Initiated file manager. Max allowed file size: ${maxBfSizeMB}MB (maxHttpBufferSize = 1e6). To increase this set maxHttpBufferSize in socket.io server init options`);
409
- // throw `this.db.tx(d => do everything in a transaction pls!!!!`;
410
- const canCreate = await (0, runSQL_1.canCreateTables)(this.db);
411
- const runQuery = (q) => {
412
- if (!canCreate)
413
- throw "File table creation failed. Your postgres user does not have CREATE table privileges";
414
- return this.db.any(q);
415
- };
416
- /**
417
- * 1. Create media table
418
- */
419
- if (!this.dbo[tableName]) {
420
- console.log(`Creating fileTable ${(0, prostgles_types_1.asName)(tableName)} ...`);
421
- await runQuery(`CREATE EXTENSION IF NOT EXISTS pgcrypto `);
422
- await runQuery(`CREATE TABLE IF NOT EXISTS ${(0, prostgles_types_1.asName)(tableName)} (
423
- name TEXT NOT NULL,
424
- extension TEXT NOT NULL,
425
- content_type TEXT NOT NULL,
426
- content_length BIGINT NOT NULL DEFAULT 0,
427
- added TIMESTAMP NOT NULL DEFAULT NOW(),
428
- url TEXT NOT NULL,
429
- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
430
- original_name TEXT NOT NULL,
431
- description TEXT,
432
- s3_url TEXT,
433
- signed_url TEXT,
434
- signed_url_expires BIGINT,
435
- etag TEXT,
436
- deleted BIGINT,
437
- deleted_from_storage BIGINT,
438
- UNIQUE(id),
439
- UNIQUE(name)
440
- )`);
441
- console.log(`Created fileTable ${(0, prostgles_types_1.asName)(tableName)}`);
442
- await prg.refreshDBO();
443
- }
444
- /**
445
- * 2. Create media lookup tables
446
- */
447
- await Promise.all((0, prostgles_types_1.getKeys)(referencedTables).map(async (refTable) => {
448
- if (!this.dbo[refTable])
449
- throw `Referenced table (${refTable}) from fileTable.referencedTables prostgles init config does not exist`;
450
- const cols = await this.dbo[refTable].getColumns();
451
- const tableConfig = referencedTables[refTable];
452
- if (typeof tableConfig !== "string") {
453
- for await (const colName of (0, prostgles_types_1.getKeys)(tableConfig.referenceColumns)) {
454
- const existingCol = cols.find(c => c.name === colName);
455
- if (existingCol) {
456
- if (existingCol.references?.some(({ ftable }) => ftable === tableName)) {
457
- // All ok
458
- }
459
- else {
460
- if (existingCol.udt_name === "uuid") {
461
- try {
462
- const query = `ALTER TABLE ${(0, prostgles_types_1.asName)(refTable)} ADD FOREIGN KEY (${(0, prostgles_types_1.asName)(colName)}) REFERENCES ${(0, prostgles_types_1.asName)(tableName)} (id);`;
463
- console.log(`Referenced file column ${refTable} (${colName}) exists but is not referencing file table. Trying to add REFERENCE constraing...\n${query}`);
464
- await runQuery(query);
465
- console.log("SUCCESS: " + query);
466
- }
467
- catch (e) {
468
- console.error(`Could not add constraing. Err: ${e instanceof Error ? e.message : JSON.stringify(e)}`);
469
- }
470
- }
471
- else {
472
- console.error(`Referenced file column ${refTable} (${colName}) exists but is not of required type (UUID). Choose a different column name or ALTER the existing column to match the type and the data found in file table ${tableName}(id)`);
473
- }
474
- }
475
- }
476
- else {
477
- try {
478
- const query = `ALTER TABLE ${(0, prostgles_types_1.asName)(refTable)} ADD COLUMN ${(0, prostgles_types_1.asName)(colName)} UUID REFERENCES ${(0, prostgles_types_1.asName)(tableName)} (id);`;
479
- console.log(`Creating referenced file column ${refTable} (${colName})...\n${query}`);
480
- await runQuery(query);
481
- console.log("SUCCESS: " + query);
482
- }
483
- catch (e) {
484
- console.error(`FAILED. Err: ${e instanceof Error ? e.message : JSON.stringify(e)}`);
485
- }
486
- }
487
- }
488
- }
489
- else {
490
- const lookupTableName = await this.parseSQLIdentifier(`prostgles_lookup_${tableName}_${refTable}`);
491
- const pKeyFields = cols.filter(f => f.is_pkey);
492
- if (pKeyFields.length !== 1) {
493
- console.error(`Could not make link table for ${refTable}. ${pKeyFields} must have exactly one primary key column. Current pkeys: ${pKeyFields.map(f => f.name)}`);
494
- }
495
- const pkField = pKeyFields[0];
496
- const refType = referencedTables[refTable];
497
- if (!this.dbo[lookupTableName]) {
498
- // if(!(await dbo[lookupTableName].count())) await db.any(`DROP TABLE IF EXISTS ${lookupTableName};`);
499
- const action = ` (${tableName} <-> ${refTable}) join table ${lookupTableName}`; // PRIMARY KEY
500
- const query = `
501
- CREATE TABLE ${lookupTableName} (
502
- foreign_id ${pkField.udt_name} ${refType === "one" ? " PRIMARY KEY " : ""} REFERENCES ${(0, prostgles_types_1.asName)(refTable)}(${(0, prostgles_types_1.asName)(pkField.name)}),
503
- media_id UUID NOT NULL REFERENCES ${(0, prostgles_types_1.asName)(tableName)}(id)
504
- )
505
- `;
506
- console.log(`Creating ${action} ...`, lookupTableName);
507
- await runQuery(query);
508
- console.log(`Created ${action}`);
509
- }
510
- else {
511
- const cols = await this.dbo[lookupTableName].getColumns();
512
- const badCols = cols.filter(c => !c.references);
513
- await Promise.all(badCols.map(async (badCol) => {
514
- console.error(`Prostgles: media ${lookupTableName} joining table has lost a reference constraint for column ${badCol.name}.` +
515
- ` This may have been caused by a DROP TABLE ... CASCADE.`);
516
- let q = ` ALTER TABLE ${(0, prostgles_types_1.asName)(lookupTableName)} ADD FOREIGN KEY (${badCol.name}) `;
517
- console.log("Trying to add the missing constraint back");
518
- if (badCol.name === "foreign_id") {
519
- q += `REFERENCES ${(0, prostgles_types_1.asName)(refTable)}(${(0, prostgles_types_1.asName)(pkField.name)}) `;
520
- }
521
- else if (badCol.name === "media_id") {
522
- q += `REFERENCES ${(0, prostgles_types_1.asName)(tableName)}(id) `;
523
- }
524
- if (q) {
525
- try {
526
- await runQuery(q);
527
- console.log("Added missing constraint back");
528
- }
529
- catch (e) {
530
- console.error("Failed to add missing constraint", e);
531
- }
532
- }
533
- }));
534
- }
535
- }
536
- await prg.refreshDBO();
537
- return true;
538
- }));
539
- /**
540
- * 4. Serve media through express
541
- */
542
- const { fileServeRoute = `/${tableName}`, expressApp: app } = fileTable;
543
- if (fileServeRoute.endsWith("/")) {
544
- throw `fileServeRoute must not end with a '/'`;
545
- }
546
- this.fileRoute = fileServeRoute;
547
- if (app) {
548
- app.get(this.fileRouteExpress, async (req, res) => {
549
- if (!this.dbo[tableName]) {
550
- res.status(500).json({ err: `Internal error: media table (${tableName}) not valid` });
551
- return false;
552
- }
553
- const mediaTable = this.dbo[tableName];
554
- try {
555
- const { name } = req.params;
556
- if (typeof name !== "string" || !name)
557
- throw "Invalid media name";
558
- const media = await mediaTable.findOne({ name }, { select: { id: 1, name: 1, signed_url: 1, signed_url_expires: 1, content_type: 1 } }, { httpReq: req });
559
- if (!media) {
560
- /**
561
- * Redirect to login !??
562
- */
563
- // const mediaExists = await mediaTable.count({ name });
564
- // if(mediaExists && this.prostgles.authHandler){
565
- // } else {
566
- // throw "Invalid media";
567
- // }
568
- throw "Invalid media";
569
- }
570
- if (this.s3Client) {
571
- let url = media.signed_url;
572
- const expires = +(media.signed_url_expires || 0);
573
- const EXPIRES = Date.now() + HOUR;
574
- if (!url || expires < EXPIRES) {
575
- url = await this.getFileS3URL(media.name, 60 * 60);
576
- await mediaTable.update({ name }, { signed_url: url, signed_url_expires: EXPIRES });
577
- }
578
- res.redirect(url);
579
- }
580
- else {
581
- const pth = `${this.config.localFolderPath}/${media.name}`;
582
- if (!fs.existsSync(pth)) {
583
- throw new Error("File not found");
584
- }
585
- res.contentType(media.content_type);
586
- res.sendFile(pth);
587
- }
588
- }
589
- catch (e) {
590
- console.log(e);
591
- res.status(404).json({ err: "Invalid/missing media" });
592
- }
593
- });
594
- }
595
- };
596
- destroy = () => {
597
- (0, exports.removeExpressRoute)(this.prostgles?.opts.fileTable?.expressApp, [this.fileRouteExpress]);
598
- };
599
- }
600
- exports.default = FileManager;
601
- const removeExpressRoute = (app, routePaths) => {
602
- const routes = app?._router?.stack;
603
- if (routes) {
604
- routes.forEach((route, i) => {
605
- if (routePaths.filter(prostgles_types_1.isDefined).includes(route.route?.path)) {
606
- routes.splice(i, 1);
607
- }
608
- });
609
- }
610
- };
611
- exports.removeExpressRoute = removeExpressRoute;
612
- const getFileTypeFromFilename = (fileName) => {
613
- const nameParts = fileName.split(".");
614
- if (nameParts.length < 2)
615
- return undefined;
616
- const nameExt = nameParts.at(-1).toLowerCase(), mime = (0, prostgles_types_1.getKeys)(prostgles_types_1.CONTENT_TYPE_TO_EXT).find(k => prostgles_types_1.CONTENT_TYPE_TO_EXT[k].includes(nameExt));
617
- if (!mime)
618
- return undefined;
619
- return {
620
- mime,
621
- ext: nameExt,
622
- };
623
- };
624
- exports.getFileTypeFromFilename = getFileTypeFromFilename;
625
- // const fileType = require("file-type");
626
- // const res = await fileType.fromBuffer(typeof file === "string"? Buffer.from(file, 'utf8') : file);
627
- const getFileType = async (file, fileName) => {
628
- const { fileTypeFromBuffer } = await eval('import("file-type")');
629
- const fileNameMime = (0, exports.getFileTypeFromFilename)(fileName);
630
- if (!fileNameMime?.ext)
631
- throw new Error("File name must contain extenion");
632
- const res = await fileTypeFromBuffer(typeof file === "string" ? Buffer.from(file, 'utf8') : file);
633
- if (!res) {
634
- /* Set correct/missing extension */
635
- const nameExt = fileNameMime?.ext;
636
- if (["xml", "txt", "csv", "tsv", "svg", "sql"].includes(nameExt) && fileNameMime.mime) {
637
- return fileNameMime;
638
- }
639
- throw new Error("Could not get the file type from file buffer");
640
- }
641
- else {
642
- if (!res.ext || fileNameMime?.ext.toLowerCase() !== res.ext.toLowerCase()) {
643
- throw new Error(`There is a mismatch between file name extension and actual buffer extension: ${fileNameMime?.ext} vs ${res.ext}`);
644
- }
645
- }
646
- return res;
647
- };
648
- exports.getFileType = getFileType;
649
- function bytesToSize(bytes) {
650
- const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
651
- if (bytes == 0)
652
- return '0 Byte';
653
- const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)) + "");
654
- return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
655
- }
656
- exports.bytesToSize = bytesToSize;
657
- /**
658
- *
659
-
660
-
661
- // if(content_type && typeof content_type !== "string") throw "Invalid content_type provided";
662
- // if(title && typeof title !== "string") throw "Invalid title provided";
663
- // let fExt = name.split(".").pop()
664
- // if(content_type && name.split(".").length > 1 && fExt && fExt.length <= 4){
665
- // type = {
666
- // mime: content_type,
667
- // ext: fExt,
668
- // fileName: name,
669
- // }
670
- // } else {
671
- // type = await this.getMIME(data, name);//, ["png", "jpg", "ogg", "webm", "pdf", "doc"]);
672
- // }
673
-
674
-
675
-
676
- */