mobiletto 1.0.35 → 1.0.38

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/README.md CHANGED
@@ -5,7 +5,7 @@ Mobiletto is a JavaScript storage abstraction layer, with optional transparent c
5
5
 
6
6
  It supports apps that are agnostic to where files are stored.
7
7
 
8
- All drivers are tested for identical behavior with over 40 tests for each driver.
8
+ All drivers are tested for identical behavior with over 50 tests for each driver.
9
9
 
10
10
  Today the supported drivers are:
11
11
 
@@ -315,11 +315,13 @@ Logs **will** contain file paths and error messages, but will **never** contain
315
315
  or any other connection configuration information.
316
316
 
317
317
  ### Log level
318
- Use the `MOBILETTO_LOG_LEVEL` environment variable to set the log level.
318
+ Use the `MOBILETTO_LOG_LEVEL` environment variable to set the log level, using one
319
+ of the `npm` levels defined in [https://www.npmjs.com/package/winston#logging-levels](https://www.npmjs.com/package/winston#logging-levels)
320
+
319
321
  The default level is `warn`. The most verbose level is `silly`, although currently mobiletto
320
322
  does not log at levels below `debug`
321
323
 
322
- MOBILETTO_LOG_LEVEL=silly
324
+ MOBILETTO_LOG_LEVEL=silly # maximum logs!
323
325
 
324
326
  ### Log file
325
327
  By default, the logger writes to the console. To send logs to a file, set the `MOBILETTO_LOG_FILE`
@@ -330,4 +332,3 @@ environment variable. When logging to a file, logs will no longer be written to
330
332
  To turn off logging:
331
333
 
332
334
  MOBILETTO_LOG_FILE=/dev/null
333
- MOBILETTO_LOG_LEVEL=emerg # not strictly necessary; minimizes time spent in logging code
@@ -112,8 +112,8 @@ class StorageClient {
112
112
  }
113
113
  }
114
114
 
115
- async list (path = '', recursive = false, visitor = null) {
116
- const dir = this.normalizePath(path)
115
+ async list (pth = '', recursive = false, visitor = null) {
116
+ const dir = this.normalizePath(pth)
117
117
  try {
118
118
  if (visitor === null) {
119
119
  const results = []
@@ -123,7 +123,21 @@ class StorageClient {
123
123
  return await this.readDirFiles(dir, recursive, visitor)
124
124
  }
125
125
  } catch (err) {
126
- throw this.ioError(err, path, 'list')
126
+ if (err instanceof MobilettoNotFoundError && !recursive && pth.includes('/')) {
127
+ // are we trying to list a single file?
128
+ const parentDir = this.normalizePath(dirname(pth))
129
+ const results = []
130
+ await this.readDirFiles(parentDir, false, (obj) => {
131
+ if (obj.name === pth) {
132
+ results.push(obj)
133
+ }
134
+ })
135
+ if (results.length === 0) {
136
+ throw err
137
+ }
138
+ return results
139
+ }
140
+ throw this.ioError(err, pth, 'list')
127
141
  }
128
142
  }
129
143
  async metadata (path) {
@@ -3,6 +3,8 @@ const {
3
3
  MobilettoError, MobilettoNotFoundError, readStream
4
4
  } = require('../../index')
5
5
 
6
+ const { dirname } = require('path')
7
+
6
8
  const {
7
9
  S3Client,
8
10
  ListObjectsCommand,
@@ -57,7 +59,19 @@ class StorageClient {
57
59
  }
58
60
 
59
61
  async list (path = '', recursive = false, visitor = null) {
60
- return await this._list(path, recursive, visitor)
62
+ try {
63
+ return await this._list(path, recursive, visitor)
64
+ } catch (e) {
65
+ if (e instanceof MobilettoNotFoundError && !recursive && path.includes('/')) {
66
+ // are we trying to list a single file?
67
+ const objects = await this._list(dirname(path), false)
68
+ const found = objects.find(o => o.name === path)
69
+ if (found) {
70
+ return [found]
71
+ }
72
+ throw e
73
+ }
74
+ }
61
75
  }
62
76
 
63
77
  async _list (path, recursive = false, visitor = null, params = {}) {
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const fs = require('fs')
2
+ const path = require('path')
2
3
  const winston = require('winston')
3
4
  const LRU = require('lru-cache')
4
5
  const { basename, dirname } = require('path')
@@ -347,9 +348,25 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
347
348
  }
348
349
 
349
350
  const encClient = {
350
- list: async (path, recursive, visitor) => {
351
- const dirent = direntDir(path)
352
- const entries = await client.list(dirent, recursive)
351
+ list: async (pth = '', recursive, visitor) => {
352
+ const p = pth === '' ? '.' : pth
353
+ const dirent = direntDir(p)
354
+ let entries
355
+ try {
356
+ entries = await client.list(dirent, recursive)
357
+ } catch (e) {
358
+ if (e instanceof MobilettoNotFoundError && p.includes('/')) {
359
+ // it might be a single file, try listing the parent dir
360
+ const parentDirent = direntDir(path.dirname(p));
361
+ entries = await client.list(parentDirent, false)
362
+ const objects = await _loadMeta(parentDirent, entries)
363
+ const found = objects.find(o => o.name === p)
364
+ if (found) {
365
+ return [ found ]
366
+ }
367
+ throw e
368
+ }
369
+ }
353
370
  if (!entries || entries.length === 0) {
354
371
  return []
355
372
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto",
3
- "version": "1.0.35",
3
+ "version": "1.0.38",
4
4
  "description": "A storage abstraction layer",
5
5
  "main": "index.js",
6
6
  "author": "Jonathan Cobb",
@@ -149,7 +149,7 @@ for (const driverName of DRIVER_NAMES) {
149
149
  it("should read the file we just wrote", async () => {
150
150
  const data = await fixture.api.readFile(fixture.name);
151
151
  expect(data.length).is.equal(size, 'expected read API to return correct number of bytes read')
152
- expect(data.toString('utf8')).to.equal(fixture.randomData, 'expected to read back the same data we wrote')
152
+ expect(data.toString()).to.equal(fixture.randomData, 'expected to read back the same data we wrote')
153
153
  })
154
154
  it("should load metadata on the file we just wrote", async () => {
155
155
  await assertMeta (fixture.api, fixture.name, fixture.randomData.length)
@@ -167,7 +167,7 @@ for (const driverName of DRIVER_NAMES) {
167
167
  const encDesc = encryption ? '(with encryption)' : '(without encryption)'
168
168
  describe(`${driverName} - ${encDesc} fail to write and delete files in readOnly mode`, () => {
169
169
  // describe(`${driverName} - ENC fail to write and delete files in readOnly mode`, () => {
170
- // const encryption = {key: rand(32)}
170
+ // const encryption = {key: rand(32)}
171
171
  const size = 16
172
172
  const randomData = rand(size)
173
173
  const fileSuffix = '' + Date.now()
@@ -253,7 +253,7 @@ for (const driverName of DRIVER_NAMES) {
253
253
  const mirrorDest = `mirrorDest_${rand(5)}/`
254
254
  describe(`${driverName} - ${encDesc} write files in a new dir, read metadata, mirror to another place (${mirrorDriver}), verify mirror, recursively delete from both`, () => {
255
255
  // describe(`${driverName} - ENCRYPTION write files in a new dir, read metadata, recursively delete`, () => {
256
- // const encryption = {key: rand(32)}
256
+ // const encryption = {key: rand(32)}
257
257
  // const encryption = null
258
258
  // a random directory and file within it
259
259
  const randomParent = `testRPD_${rand(2)}/rand_${rand(4)}`
@@ -300,6 +300,16 @@ for (const driverName of DRIVER_NAMES) {
300
300
  const removed = await fixture.api.remove(streamFile)
301
301
  expect(!!removed).to.be.true
302
302
  })
303
+ it("should successfully list a file individually", async () => {
304
+ const singleFile = await fixture.api.list(fixture.name)
305
+ expect(singleFile).to.have.lengthOf(1)
306
+ expect(singleFile[0]).to.have.property('name', fixture.name)
307
+ })
308
+ it("should successfully list files in the base directory", async () => {
309
+ const baseListing = await fixture.api.list()
310
+ expect(baseListing).to.have.lengthOf.greaterThanOrEqual(1)
311
+ expect(baseListing.find(o => o.name === path.dirname(randomParent))).to.not.be.null
312
+ })
303
313
  it("should load metadata for one of the new files", async () => {
304
314
  await assertMeta(fixture.api, fixture.name, READ_SZ, encryption ? ENC_SIZE_CLOSE_ENOUGH_PERCENT : null)
305
315
  })