mobiletto 1.0.9 → 1.0.10

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.
@@ -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,11 +32,7 @@ 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) => {
@@ -103,10 +99,19 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
103
99
  }
104
100
  return newPath + encrypted
105
101
  }
102
+ const metaPath = path => encryptPath(path + ' ~ META')
106
103
  const direntDir = dir => encryptPath(dir + DIR_ENT_DIR_SUFFIX)
107
104
  const direntFile = (dirent, path) => dirent + '/' + shasum(DIR_ENT_FILE_PREFIX + ' ' + path)
108
105
 
109
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
+ }
110
115
  let meta
111
116
  try {
112
117
  meta = await client.metadata(encryptPath(path))
@@ -132,7 +137,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
132
137
  }
133
138
  }
134
139
  meta.name = path // rewrite name back to plaintext name
135
- return meta
140
+ return Object.assign({}, meta, metaObj)
136
141
  }
137
142
 
138
143
  const _loadMeta = async (dirent, entries) => {
@@ -152,6 +157,13 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
152
157
  const df = direntFile(direntDir(dirname(path)), path);
153
158
  await client.remove(df, {recursive: false, quiet: true})
154
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
+ }
155
167
  }
156
168
 
157
169
  const encClient = {
@@ -178,9 +190,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
178
190
  // if encrypting paths, write dirent file(s) for all parent directories
179
191
  let p = path
180
192
  while (true) {
181
- const direntGenerator = function* () {
182
- yield encrypt(p + enc.encPathPadding(), enc)
183
- }
193
+ const direntGenerator = stringGenerator(p + enc.encPathPadding(), enc)
184
194
  const dir = direntDir(dirname(p))
185
195
  const df = direntFile(dir, p);
186
196
  if (!(await client.write(df, direntGenerator()))) {
@@ -192,9 +202,11 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
192
202
  }
193
203
  }
194
204
 
205
+ let bytesRead = 0
195
206
  function* cryptGenerator(plaintextGenerator) {
196
207
  let chunk = plaintextGenerator.next().value
197
208
  while (chunk) {
209
+ bytesRead += chunk.length
198
210
  yield cipher.update(chunk)
199
211
  chunk = plaintextGenerator.next().value
200
212
  }
@@ -202,7 +214,9 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
202
214
  }
203
215
  const cipher = crypt.startEncryptStream(enc)
204
216
  const realPath = encryptPath(path)
205
- 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
206
220
  },
207
221
  remove: async (path, options) => {
208
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.9",
3
+ "version": "1.0.10",
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 () => {