mobiletto 1.0.25 → 1.0.28

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
@@ -17,7 +17,7 @@ Today the supported drivers are:
17
17
  * [Mobiletto on npm](https://www.npmjs.com/package/mobiletto)
18
18
 
19
19
  ## TLDR
20
- A simple CRUD example using the mobiletto S3 driver.
20
+ A simple CRUD example using the mobiletto `s3` driver.
21
21
 
22
22
  This code would run the same if the driver were `local`.
23
23
 
@@ -137,16 +137,18 @@ This code would run the same if the driver were `local`.
137
137
  await s3.writeFile(path, bufferOrString) // returns count of bytes written
138
138
 
139
139
  // Delete a file
140
- // 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)
141
143
  const quiet = true
142
- local.remove(path, {quiet}) // returns true upon success
143
- 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
144
146
 
145
147
  // Recursively delete a directory and do it quietly (do not report errors)
146
148
  const recursive = true
147
149
  const quiet = true
148
- local.remove(path, {recursive, quiet})
149
- 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
150
152
 
151
153
  ## Metadata
152
154
  The `metadata` command returns metadata about a single filesystem entry.
@@ -288,4 +290,5 @@ The object that the storageClient function returns must define these functions:
288
290
  async write (path, generatorOrReadableStream)
289
291
 
290
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
291
294
  async remove (path, recursive = false, quiet = false)
@@ -11,14 +11,6 @@ const DEFAULT_MODE = '0700'
11
11
 
12
12
  const isNotExistError = (err) => err.code && (err.code === 'ENOENT' || err.code === 'ENOTDIR')
13
13
 
14
- const ioError = (err, path, method) => isNotExistError(err)
15
- ? err instanceof MobilettoNotFoundError
16
- ? err
17
- : new MobilettoNotFoundError(path)
18
- : err instanceof MobilettoError || err instanceof MobilettoNotFoundError
19
- ? err
20
- : new MobilettoError(`${method}(${path}) error: ${err}`, err)
21
-
22
14
  const fileType = (stat) => {
23
15
  if (stat.isDirectory()) {
24
16
  return M_DIR
@@ -63,7 +55,18 @@ class StorageClient {
63
55
  normalizePath = (path) => path.startsWith(this.baseDir)
64
56
  ? path
65
57
  : this.baseDir + (path.startsWith('/') ? path.substring(1) : path)
66
- denormalizePath = (path) => path.startsWith(this.baseDir) ? path.substring(this.baseDir.length) : path
58
+ denormalizePath = (path) => {
59
+ const norm = path.startsWith(this.baseDir) ? path.substring(this.baseDir.length) : path
60
+ return norm.startsWith('/') ? norm.substring(1) : norm
61
+ }
62
+ ioError = (err, path, method) => isNotExistError(err)
63
+ ? err instanceof MobilettoNotFoundError
64
+ ? err
65
+ : new MobilettoNotFoundError(this.denormalizePath(path))
66
+ : err instanceof MobilettoError || err instanceof MobilettoNotFoundError
67
+ ? err
68
+ : new MobilettoError(`${method}(${path}) error: ${err}`, err)
69
+
67
70
 
68
71
  fileToObject = (dir) => (f) => {
69
72
  const stat = fs.lstatSync(dir + '/' + f)
@@ -102,7 +105,7 @@ class StorageClient {
102
105
  return files
103
106
  } catch (e) {
104
107
  console.error(`dirFiles: ${e}`)
105
- throw ioError(e, norm, 'dirFiles')
108
+ throw this.ioError(e, norm, 'dirFiles')
106
109
  }
107
110
  }
108
111
 
@@ -117,7 +120,7 @@ class StorageClient {
117
120
  return await this.readDirFiles(dir, recursive, visitor)
118
121
  }
119
122
  } catch (err) {
120
- throw ioError(err, path, 'list')
123
+ throw this.ioError(err, path, 'list')
121
124
  }
122
125
  }
123
126
  async metadata (path) {
@@ -141,7 +144,7 @@ class StorageClient {
141
144
  mtime: lstat.mtimeMs
142
145
  }
143
146
  } catch (err) {
144
- throw ioError(err, path, 'metadata')
147
+ throw this.ioError(err, path, 'metadata')
145
148
  }
146
149
  }
147
150
 
@@ -232,7 +235,7 @@ class StorageClient {
232
235
  const stream = fs.createReadStream(file)
233
236
  return await readStream(stream, callback, endCallback)
234
237
  } catch (err) {
235
- throw ioError(err, path, 'read')
238
+ throw this.ioError(err, path, 'read')
236
239
  }
237
240
  }
238
241
 
@@ -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,12 +137,17 @@ 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)
143
148
  ? err
144
149
  : (err instanceof NoSuchKey) || (err.name && err.name === 'NotFound')
145
- ? new MobilettoNotFoundError(key)
150
+ ? new MobilettoNotFoundError(this.denormalizeKey(key))
146
151
  : new MobilettoError(`${method}(${path}) error: ${err}`, err)
147
152
  }
148
153
 
@@ -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.25",
3
+ "version": "1.0.28",
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)