mobiletto 1.0.24 → 1.0.27

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
@@ -12,27 +12,33 @@ Today the supported drivers are:
12
12
  * `s3`: read/write an Amazon S3 bucket
13
13
  * `local`: read/write to local filesystem
14
14
 
15
+ ### Source
16
+ * [Mobiletto on GitHub](https://github.com/cobbzilla/mobiletto)
17
+ * [Mobiletto on npm](https://www.npmjs.com/package/mobiletto)
18
+
15
19
  ## TLDR
16
- A simple CRUD example using the mobiletto S3 driver.
20
+ A simple CRUD example using the mobiletto `s3` driver.
17
21
 
18
22
  This code would run the same if the driver were `local`.
19
23
 
20
24
  const storage = require('mobiletto')
21
25
  const s3 = await storage.connect('s3', aws_key, aws_secret, {bucket: 'bk', region: 'us-east-1', cacheSize: 100})
22
26
 
23
- // returns array of metadata objects
27
+ // list objects: returns array of metadata objects
24
28
  const listing = await s3.list()
29
+ const dirList = await s3.list('some/dir/')
30
+ const everything = await s3.list('', {recursive: true})
25
31
 
26
32
  // write an entire file in one-shot
27
- await s3.writeFile('some/path', someBufferOfData)
33
+ let bytesWritten = await s3.writeFile('some/path', someBufferOfData)
28
34
 
29
- // write a stream
30
- const bytesWritten = await s3.write('some/path', streamOrGenerator)
35
+ // write a file from a stream/generator
36
+ bytesWritten = await s3.write('some/path', streamOrGenerator)
31
37
 
32
38
  // read an entire file in one-shot
33
- const objectData = await s3.readFile('some/path')
39
+ const byteBuffer = await s3.readFile('some/path')
34
40
 
35
- // read a stream
41
+ // read file as a stream using data callback
36
42
  const bytesRead = await s3.read('some/path', (chunk) => { ...do something with chunk... } )
37
43
 
38
44
  // remove a file, returns true upon success
@@ -131,16 +137,18 @@ This code would run the same if the driver were `local`.
131
137
  await s3.writeFile(path, bufferOrString) // returns count of bytes written
132
138
 
133
139
  // Delete a file
134
- // Quiet param is optional (default false), when set errors will not be thrown if the path does not exist
140
+ // Quiet param is optional (default false), when set errors will not be thrown if the path does not exist
141
+ // Always returns a value or throws an error.
142
+ // Return value may be a single string of the file removed, or an array of all files removed (driver-dependent)
135
143
  const quiet = true
136
- local.remove(path, {quiet}) // returns true upon success
137
- s3.remove(path, {quiet}) // returns true upon success
144
+ local.remove(path, {quiet}) // returns single path removed
145
+ s3.remove(path, {quiet}) // returns single path removed
138
146
 
139
147
  // Recursively delete a directory and do it quietly (do not report errors)
140
148
  const recursive = true
141
149
  const quiet = true
142
- local.remove(path, {recursive, quiet})
143
- s3.remove(path, {recursive, quiet})
150
+ local.remove(path, {recursive, quiet}) // returns paths removed
151
+ s3.remove(path, {recursive, quiet}) // returns array containing all paths removed
144
152
 
145
153
  ## Metadata
146
154
  The `metadata` command returns metadata about a single filesystem entry.
@@ -282,4 +290,5 @@ The object that the storageClient function returns must define these functions:
282
290
  async write (path, generatorOrReadableStream)
283
291
 
284
292
  // Remove a file, or recursively delete a directory
293
+ // returns a string of a single path removed, or an array of multiple paths removed
285
294
  async remove (path, recursive = false, quiet = false)
@@ -63,7 +63,10 @@ class StorageClient {
63
63
  normalizePath = (path) => path.startsWith(this.baseDir)
64
64
  ? path
65
65
  : this.baseDir + (path.startsWith('/') ? path.substring(1) : path)
66
- denormalizePath = (path) => path.startsWith(this.baseDir) ? path.substring(this.baseDir.length) : path
66
+ denormalizePath = (path) => {
67
+ const norm = path.startsWith(this.baseDir) ? path.substring(this.baseDir.length) : path
68
+ return norm.startsWith('/') ? norm.substring(1) : norm
69
+ }
67
70
 
68
71
  fileToObject = (dir) => (f) => {
69
72
  const stat = fs.lstatSync(dir + '/' + f)
@@ -250,7 +253,7 @@ class StorageClient {
250
253
  throw new MobilettoError(`remove(${path}) error: ${err}`, err)
251
254
  }
252
255
  }
253
- return Promise.resolve(true)
256
+ return path
254
257
  }
255
258
  }
256
259
 
@@ -137,6 +137,11 @@ class StorageClient {
137
137
  : this.prefix + (path.startsWith(this.delimiter) ? path.substring(1) : path))
138
138
  return p.replaceAll(/\/{2,}/g, '/')
139
139
  }
140
+ denormalizeKey = (key) => {
141
+ return key.startsWith(this.prefix)
142
+ ? key.substring(this.prefix.length)
143
+ : key
144
+ }
140
145
 
141
146
  s3error (err, key, path, method) {
142
147
  return (err instanceof MobilettoError || err instanceof MobilettoNotFoundError)
@@ -217,6 +222,7 @@ class StorageClient {
217
222
 
218
223
  async remove (path, recursive, quiet) {
219
224
  if (recursive) {
225
+ const removed = []
220
226
  let objects = await this._list(path, true, null, { MaxKeys: DELETE_OBJECTS_MAX_KEYS })
221
227
  while (objects && objects.length > 0) {
222
228
  const Delete = {
@@ -240,6 +246,9 @@ class StorageClient {
240
246
  if (!quiet && response.Errors && response.Errors.length > 0) {
241
247
  throw new MobilettoError(`remove(${path}): DeleteObjectsCommand returned Errors: ${JSON.stringify(response.Errors)}`)
242
248
  }
249
+ if (response.Deleted) {
250
+ removed.push(...response.Deleted.map(del => this.denormalizeKey(del.Key)))
251
+ }
243
252
  try {
244
253
  objects = await this._list(path, true, null, {MaxKeys: DELETE_OBJECTS_MAX_KEYS})
245
254
  } catch (e) {
@@ -249,6 +258,7 @@ class StorageClient {
249
258
  objects = null
250
259
  }
251
260
  }
261
+ return removed
252
262
  } else {
253
263
  const Key = this.normalizeKey(path)
254
264
  const bucketParams = {
@@ -272,8 +282,8 @@ class StorageClient {
272
282
  } catch (err) {
273
283
  throw this.s3error(err, Key, path, 'remove')
274
284
  }
285
+ return path
275
286
  }
276
- return true
277
287
  }
278
288
  }
279
289
 
package/index.js CHANGED
@@ -405,6 +405,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
405
405
  await client.write(metaPath(path), stringGenerator(JSON.stringify(meta), enc)())
406
406
  return bytesRead
407
407
  },
408
+ // todo: remove should return an array of the paths that were actually removed
408
409
  remove: async (path, options) => {
409
410
  const recursive = options === true || (options && options.recursive) || false
410
411
  const quiet = (options && options.quiet) || false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto",
3
- "version": "1.0.24",
3
+ "version": "1.0.27",
4
4
  "description": "A storage abstraction layer",
5
5
  "main": "index.js",
6
6
  "author": "Jonathan Cobb",
@@ -155,7 +155,7 @@ for (const driverName of DRIVER_NAMES) {
155
155
  })
156
156
  it("should delete the file we just wrote", async () => {
157
157
  const removed = await fixture.api.remove(fixture.name)
158
- expect(removed).to.be.true
158
+ expect(!!removed).to.be.true
159
159
  })
160
160
  it("loading metadata on the file we wrote now fails", async () => {
161
161
  await assertMetaFail(fixture.api, fixture.name)
@@ -205,7 +205,7 @@ for (const driverName of DRIVER_NAMES) {
205
205
  })
206
206
  it("should fail to delete the file we did not write", async () => {
207
207
  const removed = await fixture.api.remove(fixture.name)
208
- expect(removed).to.be.false
208
+ expect(!!removed).to.be.false
209
209
  })
210
210
  })
211
211
  }
@@ -238,7 +238,7 @@ for (const driverName of DRIVER_NAMES) {
238
238
  })
239
239
  it("should delete the encrypted file we just wrote", async () => {
240
240
  const removed = await fixture.api.remove(fixture.name)
241
- expect(removed).to.be.true
241
+ expect(!!removed).to.be.true
242
242
  })
243
243
  it("loading metadata on the encrypted file we wrote now fails", async () => {
244
244
  await assertMetaFail(fixture.api, fixture.name)
@@ -327,12 +327,12 @@ for (const driverName of DRIVER_NAMES) {
327
327
  it("should recursively delete the mirrored data", async () => {
328
328
  const recursive = true
329
329
  const removed = await fixture.mirrorApi.remove(mirrorDest, {recursive})
330
- expect(removed).to.be.true
330
+ expect(!!removed).to.be.true
331
331
  })
332
332
  it("should recursively delete the directory and file we just created", async () => {
333
333
  const recursive = true
334
334
  const removed = await fixture.api.remove(randomParent, {recursive})
335
- expect(removed).to.be.true
335
+ expect(!!removed).to.be.true
336
336
  })
337
337
  it("loading metadata on the file we wrote now fails", async () => {
338
338
  await assertMetaFail(fixture.api, fixture.name)