mobiletto 1.0.34 → 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/drivers/local/index.js +1 -1
- package/index.js +5 -5
- package/package.json +1 -1
- package/test/index.spec.js +16 -2
- package/util/crypt.js +8 -2
package/drivers/local/index.js
CHANGED
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
|
|
393
|
+
let generatorBytes = 0
|
|
394
394
|
function* cryptGenerator(plaintextGenerator) {
|
|
395
395
|
let chunk = plaintextGenerator.next().value
|
|
396
396
|
while (chunk) {
|
|
397
|
-
|
|
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:
|
|
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
|
|
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
package/test/index.spec.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -13,7 +14,7 @@ function normalizeKey (k) {
|
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
function normalizeIV (iv, key) {
|
|
16
|
-
return (typeof iv === 'string' && iv.trim().length
|
|
17
|
+
return (typeof iv === 'string' && iv.trim().length >= 16)
|
|
17
18
|
? Buffer.from(shasum(iv.trim())).subarray(0, 16)
|
|
18
19
|
: key
|
|
19
20
|
? Buffer.from(shasum(key)).subarray(0, 16)
|
|
@@ -33,7 +34,12 @@ function setDefaultIV (iv) {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
function getCipher(enc) {
|
|
36
|
-
|
|
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') {
|