mobiletto 1.1.32 → 1.1.34

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 (2) hide show
  1. package/index.js +30 -22
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -465,31 +465,22 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
465
465
  const _singleMeta = async (job) => {
466
466
  const dirent = job.data.dirent
467
467
  const entry = job.data.entry
468
- const files = job.data.files
469
468
  const logPrefix = `_singleMeta(${dirent}/${basename(entry.name)})`
470
- // every outcome always pushes something onto the files array
471
- // this is because we know we're done when files.length === entries.length, even if
472
- // some items in the files array are error messages instead of metadata objects
473
469
  return new Promise((resolve, reject) => {
474
470
  const cipherText = []
475
471
  client.read(dirent + '/' + basename(entry.name), reader(cipherText))
476
472
  .then((bytesRead) => {
477
473
  if (!bytesRead) {
478
474
  logger.warn(`${logPrefix} returned no data`)
479
- files.push({})
480
475
  resolve({})
481
476
  } else {
482
477
  const plain = decrypt(cipherText.toString(), enc)
483
478
  const realPath = plain.split(ENC_PAD_SEP)[0]
484
479
  _metadata(client)(realPath)
485
- .then((meta) => {
486
- files.push(meta)
487
- resolve(meta)
488
- })
480
+ .then(meta => resolve(meta))
489
481
  .catch((err) => {
490
482
  const message = `${logPrefix} error fetching _metadata: ${err}`
491
483
  logger.warn(message)
492
- files.push(message)
493
484
  reject(message)
494
485
  })
495
486
  }
@@ -497,7 +488,6 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
497
488
  .catch((err) => {
498
489
  const message = `${logPrefix} error reading file: ${err}`
499
490
  logger.warn(message)
500
- files.push(message)
501
491
  reject(message)
502
492
  })
503
493
  })
@@ -515,29 +505,47 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
515
505
  throw new MobilettoError(message)
516
506
  }
517
507
  META_LOAD_QUEUE = new Queue(META_LOAD_QUEUE_NAME, `redis://${client.redisConfig.host || '127.0.0.1'}:${client.redisConfig.port || 6379}`)
508
+ META_LOAD_QUEUE.mobilettoHandlers = {}
509
+ META_LOAD_QUEUE.mobilettoErrorHandlers = {}
518
510
  META_LOAD_QUEUE.process(META_LOAD_JOB_NAME, META_LOAD_CONCURRENCY, _singleMeta)
519
511
  META_LOAD_QUEUE.on('completed', function (job, result) {
520
512
  logger.info(`${META_LOAD_JOB_NAME} completed with result: ${JSON.stringify(result)}`)
513
+ if (job.data.jobId && META_LOAD_QUEUE.mobilettoHandlers[job.data.jobId]) {
514
+ META_LOAD_QUEUE.mobilettoHandlers[job.data.jobId](result)
515
+ }
516
+ })
517
+ META_LOAD_QUEUE.on('failed', function (job, result) {
518
+ logger.info(`${META_LOAD_JOB_NAME} failed with result: ${JSON.stringify(result)}`)
519
+ if (job.data.jobId && META_LOAD_QUEUE.mobilettoErrorHandlers[job.data.jobId]) {
520
+ META_LOAD_QUEUE.mobilettoErrorHandlers[job.data.jobId](result)
521
+ }
521
522
  })
522
523
  }
523
524
  return META_LOAD_QUEUE
524
525
  }
525
526
 
526
- const waitForFiles = (files, entries, resolve) => {
527
- if (files.length === entries.length) {
528
- resolve(files)
529
- } else {
530
- setTimeout(() => { waitForFiles(files, entries, resolve) }, 1000)
531
- }
532
- }
533
-
534
527
  const _loadMeta = async (dirent, entries) => {
535
528
  const files = []
529
+
530
+ const waitForFiles = (resolve) => {
531
+ if (files.length === entries.length) {
532
+ resolve(files)
533
+ } else {
534
+ setTimeout(() => waitForFiles(resolve), 1000)
535
+ }
536
+ }
537
+
538
+ const jobId = randomstring.generate(10)
539
+ const mq = metaLoadQueue()
540
+ mq.mobilettoHandlers[jobId] = meta => files.push(meta)
541
+ mq.mobilettoErrorHandlers[jobId] = err => files.push(err)
536
542
  for (const entry of entries) {
537
- const job = { dirent, entry, files }
538
- metaLoadQueue().add(META_LOAD_JOB_NAME, job)
543
+ const job = { jobId, dirent, entry, files }
544
+ mq.add(META_LOAD_JOB_NAME, job)
539
545
  }
540
- await new Promise(resolve => waitForFiles(files, entries, resolve))
546
+ await new Promise(resolve => waitForFiles(resolve))
547
+ delete mq.mobilettoHandlers[jobId]
548
+ delete mq.mobilettoErrorHandlers[jobId]
541
549
  return files
542
550
  }
543
551
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto",
3
- "version": "1.1.32",
3
+ "version": "1.1.34",
4
4
  "description": "A storage layer that presents a uniform interface to Amazon S3, Backblaze B2, and local storage",
5
5
  "main": "index.js",
6
6
  "keywords": [