mobiletto 1.0.8 → 1.0.11

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
@@ -71,6 +71,10 @@ Today the supported drivers are:
71
71
  local.write(path, generator)
72
72
  s3.write(path, generator)
73
73
 
74
+ // Write an entire file at once (convenience method)
75
+ await local.writeFile(path, bufferOrString) // returns count of bytes written
76
+ await s3.writeFile(path, bufferOrString) // returns count of bytes written
77
+
74
78
  // Delete a file
75
79
  // Quiet param is optional (default false), when set errors will not be thrown if the path does not exist
76
80
  const quiet = true
@@ -45,9 +45,7 @@ class StorageClient {
45
45
  }
46
46
  return { path, stat }
47
47
  }
48
- async testConfig () {
49
- return await this.list()
50
- }
48
+ testConfig = async () => await this.list()
51
49
  fileType (stat) {
52
50
  if (stat.isDirectory()) {
53
51
  return M_DIR
@@ -43,9 +43,8 @@ class StorageClient {
43
43
  this.client = new S3Client({region: this.region, credentials})
44
44
  }
45
45
 
46
- async testConfig () {
47
- return await this._list('', { MaxKeys: 1 })
48
- }
46
+ // noinspection JSUnusedGlobalSymbols -- called by driver init
47
+ testConfig = async () => await this._list('', { MaxKeys: 1 })
49
48
 
50
49
  async list (path = '') {
51
50
  return (await this._list(path)).map(obj => {
package/index.js CHANGED
@@ -32,17 +32,17 @@ function MobilettoNotFoundError (message) {
32
32
  }
33
33
  }
34
34
 
35
- const reader = (chunks) => (chunk) => {
36
- if (chunk) {
37
- chunks.push(chunk)
38
- }
39
- }
35
+ const reader = (chunks) => (chunk) => { if (chunk) { chunks.push(chunk) } }
40
36
 
41
37
  const UTILITY_FUNCTIONS = {
42
38
  readFile: (client) => async (path) => {
43
39
  const chunks = []
44
40
  await client.read(path, reader(chunks))
45
41
  return Buffer.concat(chunks)
42
+ },
43
+ writeFile: (client) => async (path, data) => {
44
+ const readFunc = function* () { yield data }
45
+ return await client.write(path, readFunc())
46
46
  }
47
47
  }
48
48
 
@@ -51,7 +51,7 @@ function addUtilityFunctions (client, readOnly = false) {
51
51
  client[func] = UTILITY_FUNCTIONS[func](client)
52
52
  }
53
53
  if (readOnly) {
54
- for (const writeFunc of ['write', 'remove']) {
54
+ for (const writeFunc of ['write', 'remove', 'writeFile']) {
55
55
  client[writeFunc] = async () => {
56
56
  // console.warn(`${writeFunc} not supported in readOnly mode`)
57
57
  return false
@@ -99,10 +99,19 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
99
99
  }
100
100
  return newPath + encrypted
101
101
  }
102
+ const metaPath = path => encryptPath(path + ' ~ META')
102
103
  const direntDir = dir => encryptPath(dir + DIR_ENT_DIR_SUFFIX)
103
104
  const direntFile = (dirent, path) => dirent + '/' + shasum(DIR_ENT_FILE_PREFIX + ' ' + path)
104
105
 
105
106
  const _metadata = (client) => async (path) => {
107
+ let metaObj
108
+ try {
109
+ let chunks = []
110
+ await client.read(metaPath(path), reader(chunks))
111
+ metaObj = JSON.parse(decrypt(Buffer.concat(chunks).toString(), enc))
112
+ } catch (e) {
113
+ metaObj = {}
114
+ }
106
115
  let meta
107
116
  try {
108
117
  meta = await client.metadata(encryptPath(path))
@@ -128,7 +137,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
128
137
  }
129
138
  }
130
139
  meta.name = path // rewrite name back to plaintext name
131
- return meta
140
+ return Object.assign({}, meta, metaObj)
132
141
  }
133
142
 
134
143
  const _loadMeta = async (dirent, entries) => {
@@ -148,6 +157,13 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
148
157
  const df = direntFile(direntDir(dirname(path)), path);
149
158
  await client.remove(df, {recursive: false, quiet: true})
150
159
  await client.remove(encryptPath(path), {recursive: false, quiet: true})
160
+ await client.remove(metaPath(path), {recursive: false, quiet: true})
161
+ }
162
+
163
+ function stringGenerator (value, enc) {
164
+ return function* () {
165
+ yield encrypt(value, enc)
166
+ }
151
167
  }
152
168
 
153
169
  const encClient = {
@@ -174,9 +190,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
174
190
  // if encrypting paths, write dirent file(s) for all parent directories
175
191
  let p = path
176
192
  while (true) {
177
- const direntGenerator = function* () {
178
- yield encrypt(p + enc.encPathPadding(), enc)
179
- }
193
+ const direntGenerator = stringGenerator(p + enc.encPathPadding(), enc)
180
194
  const dir = direntDir(dirname(p))
181
195
  const df = direntFile(dir, p);
182
196
  if (!(await client.write(df, direntGenerator()))) {
@@ -188,9 +202,11 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
188
202
  }
189
203
  }
190
204
 
205
+ let bytesRead = 0
191
206
  function* cryptGenerator(plaintextGenerator) {
192
207
  let chunk = plaintextGenerator.next().value
193
208
  while (chunk) {
209
+ bytesRead += chunk.length
194
210
  yield cipher.update(chunk)
195
211
  chunk = plaintextGenerator.next().value
196
212
  }
@@ -198,7 +214,9 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
198
214
  }
199
215
  const cipher = crypt.startEncryptStream(enc)
200
216
  const realPath = encryptPath(path)
201
- return client.write(realPath, cryptGenerator(readFunc))
217
+ await client.write(realPath, cryptGenerator(readFunc))
218
+ await client.write(metaPath(path), stringGenerator(JSON.stringify({ size: bytesRead }), enc)())
219
+ return bytesRead
202
220
  },
203
221
  remove: async (path, options) => {
204
222
  const recursive = (options && options.recursive) || false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto",
3
- "version": "1.0.8",
3
+ "version": "1.0.11",
4
4
  "description": "A storage abstraction layer",
5
5
  "main": "index.js",
6
6
  "author": "Jonathan Cobb",
@@ -34,20 +34,11 @@ DRIVER_CONFIG = {
34
34
  }
35
35
  }
36
36
 
37
- function closeEnough (expected, actual, percent = ENC_SIZE_CLOSE_ENOUGH_PERCENT) {
38
- expect(Math.abs(expected - actual)).to.be.lessThan(Math.floor(expected * percent),
39
- 'expected size within 5% of actual (due to encryption)')
40
- }
41
-
42
- async function assertMeta (api, name, expectedSize, closeEnoughPercent = null) {
37
+ async function assertMeta (api, name, expectedSize) {
43
38
  const meta = await api.metadata(name)
44
39
  should().exist(meta, 'expected return value from metadata call')
45
40
  expect(meta.name).equals(name, 'expected name of written file to be correct')
46
- if (closeEnoughPercent) {
47
- closeEnough(expectedSize, meta.size, closeEnoughPercent)
48
- } else {
49
- expect(meta.size).equals(expectedSize, 'expected size of written file to equal size of randomData')
50
- }
41
+ expect(meta.size).equals(expectedSize, 'expected size of written file to equal size of randomData')
51
42
  }
52
43
 
53
44
  async function assertMetaFail (api, name) {
@@ -91,7 +82,7 @@ describe('crypto test', () => {
91
82
 
92
83
  })
93
84
 
94
- const ENC_TESTS = [null, {key: rand(32)}]
85
+ const encryptionTests = () => [null, { key: rand(32) }]
95
86
 
96
87
  // To test a single driver:
97
88
  // - Uncomment one of the lines below to set driverName to the one you want to test
@@ -158,14 +149,13 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
158
149
  })
159
150
  })
160
151
 
161
- for (const encryption of ENC_TESTS) {
152
+ for (const encryption of encryptionTests()) {
162
153
  const encDesc = encryption ? '(with encryption)' : '(without encryption)'
163
154
  describe(`${driverName} - ${encDesc} fail to write and delete files in readOnly mode`, () => {
164
155
  // some random data, plus a bit extra
165
156
  const size = 16
166
157
  const randomData = rand(size)
167
158
  const fileSuffix = '' + Date.now()
168
- const encryption = {key: rand(32)}
169
159
  let fixture
170
160
  beforeEach((done) => {
171
161
  const name = `test_file_${fileSuffix}`
@@ -212,7 +202,6 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
212
202
  const randomData = rand(size)
213
203
  const fileSuffix = '' + Date.now()
214
204
  const encryptionKey = rand(32)
215
- let encryptedByteCount = null
216
205
  let fixture
217
206
  beforeEach((done) => {
218
207
  const name = `test_file_${fileSuffix}`
@@ -222,8 +211,8 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
222
211
  .finally(done)
223
212
  })
224
213
  it("should write an encrypted file", async () => {
225
- encryptedByteCount = await writeRandomFile(fixture, size);
226
- closeEnough(size, encryptedByteCount)
214
+ const bytesWritten = await writeRandomFile(fixture, size);
215
+ expect(bytesWritten).is.equal(size, 'expected write API to return correct number of bytes written')
227
216
  })
228
217
  it("should read the encrypted file we just wrote", async () => {
229
218
  const data = await fixture.api.readFile(fixture.name);
@@ -242,7 +231,7 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
242
231
  })
243
232
  })
244
233
 
245
- for (const encryption of ENC_TESTS) {
234
+ for (const encryption of encryptionTests()) {
246
235
  const encDesc = encryption ? '(with encryption)' : '(without encryption)'
247
236
  describe(`${driverName} - ${encDesc} write files in a new dir, read metadata, recursively delete`, () => {
248
237
  // describe(`${driverName} - ENCRYPTION write files in a new dir, read metadata, recursively delete`, () => {
@@ -269,11 +258,7 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
269
258
 
270
259
  for (let i = 0; i < fileCount; i++) {
271
260
  const bytesWritten = await fixture.api.write(tempFilename(fixture.name, i), dataGenerator())
272
- if (encryption) {
273
- closeEnough(READ_SZ, bytesWritten)
274
- } else {
275
- expect(bytesWritten).to.equal(READ_SZ, 'expected write API to return correct number of bytes written')
276
- }
261
+ expect(bytesWritten).to.equal(READ_SZ, 'expected write API to return correct number of bytes written')
277
262
  }
278
263
  })
279
264
  it("should load metadata for one of the new files", async () => {