s3db.js 2.0.0 → 2.1.1

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.
@@ -39,6 +39,7 @@ exports.S3Cache = void 0;
39
39
  const zlib_1 = __importDefault(require("zlib"));
40
40
  const path = __importStar(require("path"));
41
41
  const lodash_1 = require("lodash");
42
+ const sha256_1 = __importDefault(require("crypto-js/sha256"));
42
43
  const serializers_type_1 = __importDefault(require("./serializers.type"));
43
44
  const json_serializer_1 = require("./json.serializer");
44
45
  const avro_serializer_1 = require("./avro.serializer");
@@ -60,11 +61,8 @@ class S3Cache {
60
61
  if (filename.length === 0)
61
62
  filename = `empty`;
62
63
  if (hashed) {
63
- filename = Buffer.from(filename)
64
- .toString("base64")
65
- .split("")
66
- .reverse()
67
- .join("");
64
+ filename = (0, sha256_1.default)(filename);
65
+ // filename = Buffer.from(filename).toString("base64").split("").reverse().join("");
68
66
  }
69
67
  if (additionalPrefix.length > 0) {
70
68
  filename = additionalPrefix + filename;
@@ -25,10 +25,11 @@ class S3ResourceCache extends s3_cache_class_1.default {
25
25
  this.resource = resource;
26
26
  }
27
27
  getKey({ action = "list", params }) {
28
- return super.getKey({
28
+ const key = super.getKey({
29
29
  params,
30
30
  additionalPrefix: `resource=${this.resource.name}/action=${action}|`,
31
31
  });
32
+ return key;
32
33
  }
33
34
  put({ action = "list", params, data, }) {
34
35
  const _super = Object.create(null, {
@@ -300,6 +300,72 @@ class S3Client extends events_1.default {
300
300
  return keys;
301
301
  });
302
302
  }
303
+ getContinuationTokenAfterOffset({ prefix, offset = 1000, }) {
304
+ return __awaiter(this, void 0, void 0, function* () {
305
+ if (offset === 0)
306
+ return null;
307
+ let truncated = true;
308
+ let continuationToken;
309
+ let skipped = 0;
310
+ while (truncated) {
311
+ let maxKeys = offset < 1000
312
+ ? offset
313
+ : offset - skipped > 1000
314
+ ? 1000
315
+ : offset - skipped;
316
+ const options = {
317
+ prefix,
318
+ maxKeys,
319
+ continuationToken,
320
+ };
321
+ const res = yield this.listObjects(options);
322
+ if (res.Contents) {
323
+ skipped += res.Contents.length;
324
+ }
325
+ truncated = res.IsTruncated || false;
326
+ continuationToken = res.NextContinuationToken;
327
+ if (skipped >= offset) {
328
+ break;
329
+ }
330
+ }
331
+ return continuationToken;
332
+ });
333
+ }
334
+ getKeysPage({ prefix, offset = 0, amount = 100, } = {}) {
335
+ return __awaiter(this, void 0, void 0, function* () {
336
+ let keys = [];
337
+ let truncated = true;
338
+ let continuationToken;
339
+ if (offset > 0) {
340
+ continuationToken = yield this.getContinuationTokenAfterOffset({
341
+ prefix,
342
+ offset,
343
+ });
344
+ }
345
+ while (truncated) {
346
+ const options = {
347
+ prefix,
348
+ continuationToken,
349
+ };
350
+ const res = yield this.listObjects(options);
351
+ if (res.Contents) {
352
+ keys = keys.concat(res.Contents.map((x) => x.Key));
353
+ }
354
+ truncated = res.IsTruncated || false;
355
+ continuationToken = res.NextContinuationToken;
356
+ if (keys.length > amount) {
357
+ keys = keys.splice(0, amount);
358
+ break;
359
+ }
360
+ }
361
+ if (this.keyPrefix) {
362
+ keys = keys
363
+ .map((x) => x.replace(this.keyPrefix, ""))
364
+ .map((x) => (x.startsWith("/") ? x.replace(`/`, "") : x));
365
+ }
366
+ return keys;
367
+ });
368
+ }
303
369
  }
304
370
  exports.S3Client = S3Client;
305
371
  exports.default = S3Client;
@@ -416,7 +416,10 @@ class S3Resource extends events_1.default {
416
416
  getMany(ids) {
417
417
  return __awaiter(this, void 0, void 0, function* () {
418
418
  if (this.s3Cache) {
419
- const cached = yield this.s3Cache.get({ action: "getMany" });
419
+ const cached = yield this.s3Cache.get({
420
+ action: "getMany",
421
+ params: { ids: ids.sort() },
422
+ });
420
423
  if (cached)
421
424
  return cached;
422
425
  }
@@ -429,7 +432,11 @@ class S3Resource extends events_1.default {
429
432
  return data;
430
433
  }));
431
434
  if (this.s3Cache)
432
- yield this.s3Cache.put({ action: "getMany", data: results });
435
+ yield this.s3Cache.put({
436
+ action: "getMany",
437
+ params: { ids: ids.sort() },
438
+ data: results,
439
+ });
433
440
  this.emit("getMany", ids.length);
434
441
  return results;
435
442
  });
@@ -467,6 +474,32 @@ class S3Resource extends events_1.default {
467
474
  return results;
468
475
  });
469
476
  }
477
+ page({ offset = 0, size = 100 }) {
478
+ return __awaiter(this, void 0, void 0, function* () {
479
+ if (this.s3Cache) {
480
+ const cached = yield this.s3Cache.get({
481
+ action: "page",
482
+ params: { offset, size },
483
+ });
484
+ if (cached)
485
+ return cached;
486
+ }
487
+ const keys = yield this.s3Client.getKeysPage({
488
+ amount: size,
489
+ offset: offset,
490
+ prefix: `resource=${this.name}`,
491
+ });
492
+ const ids = keys.map((x) => x.replace(`resource=${this.name}/id=`, ""));
493
+ const data = yield this.getMany(ids);
494
+ if (this.s3Cache)
495
+ yield this.s3Cache.put({
496
+ action: "page",
497
+ params: { offset, size },
498
+ data,
499
+ });
500
+ return data;
501
+ });
502
+ }
470
503
  readable() {
471
504
  const stream = new resource_ids_read_stream_class_1.ResourceIdsReadStream({ resource: this });
472
505
  const transformer = new resource_ids_transformer_class_1.ResourceIdsToDataTransformer({ resource: this });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s3db.js",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Use AWS S3 as a cheap document database.",
5
5
  "main": "build/index.js",
6
6
  "author": "forattini-dev",