mobiletto 1.0.7 → 1.0.8
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 +3 -1
- package/index.js +12 -3
- package/package.json +1 -1
- package/test/index.spec.js +52 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Mobiletto is a JavaScript storage abstraction layer, with optional transparent c
|
|
|
5
5
|
|
|
6
6
|
It supports apps that are agnostic to where files are stored.
|
|
7
7
|
|
|
8
|
-
All drivers are tested for identical behavior with
|
|
8
|
+
All drivers are tested for identical behavior with over 35 tests for each driver.
|
|
9
9
|
|
|
10
10
|
Today the supported drivers are:
|
|
11
11
|
|
|
@@ -22,6 +22,7 @@ Today the supported drivers are:
|
|
|
22
22
|
// * key: base directory
|
|
23
23
|
// * secret: ignored, can be null
|
|
24
24
|
// * opts object:
|
|
25
|
+
// * readOnly: optional, never change anything on the filesystem; default is false
|
|
25
26
|
// * mode: optional, filesystem permissions to set when creating new files and directories, default is 0700
|
|
26
27
|
const local = await mobiletto('local', '/home/ubuntu/tmp', null, {mode: '0700'})
|
|
27
28
|
|
|
@@ -29,6 +30,7 @@ Today the supported drivers are:
|
|
|
29
30
|
// * key: AWS access key
|
|
30
31
|
// * secret: AWS secret key
|
|
31
32
|
// * opts object:
|
|
33
|
+
// * readOnly: optional, never change anything on the bucket; default is false
|
|
32
34
|
// * bucket: required, name of the S3 bucket
|
|
33
35
|
// * region: optional, the AWS region to communicate with, default is us-east-1
|
|
34
36
|
// * prefix: optional, all read/writes within the S3 bucket will be under this prefix
|
package/index.js
CHANGED
|
@@ -46,10 +46,18 @@ const UTILITY_FUNCTIONS = {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
function addUtilityFunctions (client) {
|
|
49
|
+
function addUtilityFunctions (client, readOnly = false) {
|
|
50
50
|
for (const func of Object.keys(UTILITY_FUNCTIONS)) {
|
|
51
51
|
client[func] = UTILITY_FUNCTIONS[func](client)
|
|
52
52
|
}
|
|
53
|
+
if (readOnly) {
|
|
54
|
+
for (const writeFunc of ['write', 'remove']) {
|
|
55
|
+
client[writeFunc] = async () => {
|
|
56
|
+
// console.warn(`${writeFunc} not supported in readOnly mode`)
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
53
61
|
return client
|
|
54
62
|
}
|
|
55
63
|
|
|
@@ -63,8 +71,9 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
|
|
|
63
71
|
if (!(await client.testConfig())) {
|
|
64
72
|
throw new MobilettoError(`mobiletto(${driverPath}) error: test API call failed`)
|
|
65
73
|
}
|
|
74
|
+
const readOnly = opts ? !!opts.readOnly : false
|
|
66
75
|
if (encryption === null) {
|
|
67
|
-
return addUtilityFunctions(client)
|
|
76
|
+
return addUtilityFunctions(client, readOnly)
|
|
68
77
|
}
|
|
69
78
|
const encKey = normalizeKey(encryption.key)
|
|
70
79
|
if (!encKey) {
|
|
@@ -247,7 +256,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
|
|
|
247
256
|
return true
|
|
248
257
|
}
|
|
249
258
|
}
|
|
250
|
-
return addUtilityFunctions(encClient)
|
|
259
|
+
return addUtilityFunctions(encClient, readOnly)
|
|
251
260
|
}
|
|
252
261
|
|
|
253
262
|
async function readStream(stream, callback, endCallback) {
|
package/package.json
CHANGED
package/test/index.spec.js
CHANGED
|
@@ -91,6 +91,8 @@ describe('crypto test', () => {
|
|
|
91
91
|
|
|
92
92
|
})
|
|
93
93
|
|
|
94
|
+
const ENC_TESTS = [null, {key: rand(32)}]
|
|
95
|
+
|
|
94
96
|
// To test a single driver:
|
|
95
97
|
// - Uncomment one of the lines below to set driverName to the one you want to test
|
|
96
98
|
// - Comment out the next `for` line, and its closing curly brace (just before EOF)
|
|
@@ -156,6 +158,54 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
|
|
|
156
158
|
})
|
|
157
159
|
})
|
|
158
160
|
|
|
161
|
+
for (const encryption of ENC_TESTS) {
|
|
162
|
+
const encDesc = encryption ? '(with encryption)' : '(without encryption)'
|
|
163
|
+
describe(`${driverName} - ${encDesc} fail to write and delete files in readOnly mode`, () => {
|
|
164
|
+
// some random data, plus a bit extra
|
|
165
|
+
const size = 16
|
|
166
|
+
const randomData = rand(size)
|
|
167
|
+
const fileSuffix = '' + Date.now()
|
|
168
|
+
const encryption = {key: rand(32)}
|
|
169
|
+
let fixture
|
|
170
|
+
beforeEach((done) => {
|
|
171
|
+
const name = `test_file_${fileSuffix}`
|
|
172
|
+
const opts = Object.assign({}, config.opts, {readOnly: true})
|
|
173
|
+
connect(driverName, config.key, config.secret, opts, encryption)
|
|
174
|
+
.then(api => { fixture = {api, name, randomData} })
|
|
175
|
+
.catch(err => { throw err })
|
|
176
|
+
.finally(done)
|
|
177
|
+
})
|
|
178
|
+
it("should try to write a file and fail", async () => {
|
|
179
|
+
const response = await writeRandomFile(fixture, size)
|
|
180
|
+
expect(response).to.be.false
|
|
181
|
+
})
|
|
182
|
+
it("should fail to read the file we did not write", async () => {
|
|
183
|
+
try {
|
|
184
|
+
await fixture.api.readFile(fixture.name)
|
|
185
|
+
assert.fail('should not have been able to read non-existent file that we did not write')
|
|
186
|
+
} catch (e) {
|
|
187
|
+
if (!(e instanceof MobilettoNotFoundError)) {
|
|
188
|
+
assert.fail(`unexpected error: ${e} trying to read non-existent file`)
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
it("should fail to load metadata on the file we did not write", async () => {
|
|
193
|
+
try {
|
|
194
|
+
await assertMeta(fixture.api, fixture.name, fixture.randomData.length)
|
|
195
|
+
assert.fail('should not have been able to read metadata for non-existent file that we did not write')
|
|
196
|
+
} catch (e) {
|
|
197
|
+
if (!(e instanceof MobilettoNotFoundError)) {
|
|
198
|
+
assert.fail(`unexpected error: ${e} trying to read metadata on non-existent file`)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
it("should fail to delete the file we did not write", async () => {
|
|
203
|
+
const removed = await fixture.api.remove(fixture.name)
|
|
204
|
+
expect(removed).to.be.false
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
|
|
159
209
|
describe(`${driverName} - write an encrypted file, read file, read metadata, delete file`, () => {
|
|
160
210
|
// some random data, plus a bit extra
|
|
161
211
|
const size = (READ_SZ * TEMP_SZ_MULTIPLE) + Math.floor(Math.random() * (READ_SZ/2))
|
|
@@ -168,6 +218,7 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
|
|
|
168
218
|
const name = `test_file_${fileSuffix}`
|
|
169
219
|
mobiletto(driverName, config.key, config.secret, config.opts, {key: encryptionKey})
|
|
170
220
|
.then(api => { fixture = {api, name, randomData} })
|
|
221
|
+
.catch((err) => { throw err })
|
|
171
222
|
.finally(done)
|
|
172
223
|
})
|
|
173
224
|
it("should write an encrypted file", async () => {
|
|
@@ -191,7 +242,7 @@ for (const driverName of Object.keys(DRIVER_CONFIG)) {
|
|
|
191
242
|
})
|
|
192
243
|
})
|
|
193
244
|
|
|
194
|
-
for (const encryption of
|
|
245
|
+
for (const encryption of ENC_TESTS) {
|
|
195
246
|
const encDesc = encryption ? '(with encryption)' : '(without encryption)'
|
|
196
247
|
describe(`${driverName} - ${encDesc} write files in a new dir, read metadata, recursively delete`, () => {
|
|
197
248
|
// describe(`${driverName} - ENCRYPTION write files in a new dir, read metadata, recursively delete`, () => {
|