mobiletto 1.0.32 → 1.0.35

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
@@ -18,8 +18,8 @@ Mobiletto is intended to be used as a library by other JavaScript code.
18
18
  To work with mobiletto at the command-line, use [mobiletto-cli](https://www.npmjs.com/package/mobiletto-cli)
19
19
 
20
20
  ### Source
21
- * [Mobiletto on GitHub](https://github.com/cobbzilla/mobiletto)
22
- * [Mobiletto on npm](https://www.npmjs.com/package/mobiletto)
21
+ * [mobiletto on GitHub](https://github.com/cobbzilla/mobiletto)
22
+ * [mobiletto on npm](https://www.npmjs.com/package/mobiletto)
23
23
 
24
24
  ## TLDR
25
25
  A simple example using the mobiletto `s3` driver.
@@ -228,7 +228,7 @@ class StorageClient {
228
228
  : generator.next().value
229
229
  }
230
230
  closer()
231
- return Promise.resolve(count)
231
+ return count
232
232
  }
233
233
 
234
234
  async read (path, callback, endCallback = null) {
package/index.js CHANGED
@@ -390,11 +390,11 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
390
390
  }
391
391
  }
392
392
 
393
- let bytesRead = 0
393
+ let generatorBytes = 0
394
394
  function* cryptGenerator(plaintextGenerator) {
395
395
  let chunk = plaintextGenerator.next().value
396
396
  while (chunk) {
397
- bytesRead += chunk.length
397
+ generatorBytes += chunk.length
398
398
  yield cipher.update(chunk)
399
399
  chunk = plaintextGenerator.next().value
400
400
  }
@@ -402,10 +402,10 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
402
402
  }
403
403
  const cipher = crypt.startEncryptStream(enc)
404
404
  const realPath = encryptPath(path)
405
- await client.write(realPath, cryptGenerator(readFunc))
406
- const meta = { name: path, size: bytesRead, type: M_FILE }
405
+ const streamBytes = await client.write(realPath, isReadable(readFunc) ? readFunc : cryptGenerator(readFunc))
406
+ const meta = { name: path, size: generatorBytes, type: M_FILE }
407
407
  await client.write(metaPath(path), stringGenerator(JSON.stringify(meta), enc)())
408
- return bytesRead
408
+ return isReadable(readFunc) ? streamBytes : generatorBytes
409
409
  },
410
410
  // todo: remove should return an array of the paths that were actually removed
411
411
  remove: async (path, options) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto",
3
- "version": "1.0.32",
3
+ "version": "1.0.35",
4
4
  "description": "A storage abstraction layer",
5
5
  "main": "index.js",
6
6
  "author": "Jonathan Cobb",
@@ -1,5 +1,6 @@
1
1
  // To run the tests, you need a .env file one level above this directory
2
2
  // that contains env vars for all the process.env stuff in DRIVER_CONFIG below
3
+ const fs = require('fs')
3
4
  require('dotenv').config()
4
5
 
5
6
  const randomstring = require('randomstring')
@@ -166,7 +167,7 @@ for (const driverName of DRIVER_NAMES) {
166
167
  const encDesc = encryption ? '(with encryption)' : '(without encryption)'
167
168
  describe(`${driverName} - ${encDesc} fail to write and delete files in readOnly mode`, () => {
168
169
  // describe(`${driverName} - ENC fail to write and delete files in readOnly mode`, () => {
169
- // const encryption = {key: rand(32)}
170
+ // const encryption = {key: rand(32)}
170
171
  const size = 16
171
172
  const randomData = rand(size)
172
173
  const fileSuffix = '' + Date.now()
@@ -252,7 +253,7 @@ for (const driverName of DRIVER_NAMES) {
252
253
  const mirrorDest = `mirrorDest_${rand(5)}/`
253
254
  describe(`${driverName} - ${encDesc} write files in a new dir, read metadata, mirror to another place (${mirrorDriver}), verify mirror, recursively delete from both`, () => {
254
255
  // describe(`${driverName} - ENCRYPTION write files in a new dir, read metadata, recursively delete`, () => {
255
- // const encryption = {key: rand(32)}
256
+ // const encryption = {key: rand(32)}
256
257
  // const encryption = null
257
258
  // a random directory and file within it
258
259
  const randomParent = `testRPD_${rand(2)}/rand_${rand(4)}`
@@ -286,6 +287,19 @@ for (const driverName of DRIVER_NAMES) {
286
287
  expect(bytesWritten).to.equal(READ_SZ, 'expected write API to return correct number of bytes written')
287
288
  }
288
289
  })
290
+ it('should write a file in a new directory using a stream', async () => {
291
+ // create a random temp file, write it
292
+ const data = randomstring.generate(READ_SZ)
293
+ const tempFile = `/tmp/${randomstring.generate(10)}_cool_${Date.now()}`;
294
+ fs.writeFileSync(tempFile, data)
295
+ const reader = fs.createReadStream(tempFile)
296
+ const streamFile = fixture.name + '_stream';
297
+ const bytesWritten = await fixture.api.write(streamFile, reader)
298
+ expect(bytesWritten).to.equal(READ_SZ, 'expected write API to return correct number of bytes written for STREAM')
299
+ // remove stream file
300
+ const removed = await fixture.api.remove(streamFile)
301
+ expect(!!removed).to.be.true
302
+ })
289
303
  it("should load metadata for one of the new files", async () => {
290
304
  await assertMeta(fixture.api, fixture.name, READ_SZ, encryption ? ENC_SIZE_CLOSE_ENOUGH_PERCENT : null)
291
305
  })
package/util/crypt.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // adapted from https://stackoverflow.com/a/64136185
2
2
  const crypto = require('crypto')
3
3
  const shasum = require('shasum')
4
+ const winston = require("winston");
4
5
 
5
6
  const WARN_PLAINTEXT = !process.env.IGNORE_DISABLED_ENCRYPTION
6
7
  const MIN_KEY_LEN = 16
@@ -8,13 +9,13 @@ const DEFAULT_CRYPT_ALGO = 'aes-256-cbc';
8
9
 
9
10
  function normalizeKey (k) {
10
11
  return (typeof k === 'string' && k.trim().length > MIN_KEY_LEN)
11
- ? Buffer.from(shasum(k)).subarray(0, 32)
12
+ ? Buffer.from(shasum(k.trim())).subarray(0, 32)
12
13
  : null
13
14
  }
14
15
 
15
16
  function normalizeIV (iv, key) {
16
- return (typeof iv === 'string')
17
- ? Buffer.from(shasum(iv)).subarray(0, 16)
17
+ return (typeof iv === 'string' && iv.trim().length >= 16)
18
+ ? Buffer.from(shasum(iv.trim())).subarray(0, 16)
18
19
  : key
19
20
  ? Buffer.from(shasum(key)).subarray(0, 16)
20
21
  : null
@@ -33,7 +34,12 @@ function setDefaultIV (iv) {
33
34
  }
34
35
 
35
36
  function getCipher(enc) {
36
- return crypto.createCipheriv(enc.algo, enc.key, enc.iv);
37
+ try {
38
+ return crypto.createCipheriv(enc.algo, enc.key, enc.iv);
39
+ } catch (e) {
40
+ winston.error(`getCipher(${enc}): ${e}`)
41
+ throw e
42
+ }
37
43
  }
38
44
 
39
45
  function encrypt (plainText, encryption, outputEncoding = 'base64') {