mobiletto 1.1.2 → 1.1.5
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 +17 -14
- package/index.js +13 -2
- package/package.json +1 -1
- package/test/api.spec.js +2 -2
- package/util/redis.js +1 -0
package/README.md
CHANGED
|
@@ -58,38 +58,41 @@ To work with mobiletto at the command-line, use [mobiletto-cli](https://www.npmj
|
|
|
58
58
|
* [mobiletto on npm](https://www.npmjs.com/package/mobiletto)
|
|
59
59
|
|
|
60
60
|
## Quick Start
|
|
61
|
-
A
|
|
61
|
+
A short example using the mobiletto `s3` driver.
|
|
62
62
|
|
|
63
63
|
This code would run the same if the driver were `b2` or `local`.
|
|
64
64
|
|
|
65
65
|
const storage = require('mobiletto')
|
|
66
|
-
const
|
|
66
|
+
const bucket = await storage.connect('s3', aws_key, aws_secret, {bucket: 'bk'})
|
|
67
67
|
|
|
68
68
|
// list objects: returns array of metadata objects
|
|
69
|
-
const listing = await
|
|
70
|
-
const dirList = await
|
|
71
|
-
const everything = await
|
|
69
|
+
const listing = await bucket.list()
|
|
70
|
+
const dirList = await bucket.list('some/dir/')
|
|
71
|
+
const everything = await bucket.list('', {recursive: true})
|
|
72
72
|
|
|
73
73
|
// write an entire file in one-shot
|
|
74
|
-
let bytesWritten = await
|
|
74
|
+
let bytesWritten = await bucket.writeFile('some/path', someBufferOfData)
|
|
75
75
|
|
|
76
76
|
// write a file from a stream/generator
|
|
77
|
-
bytesWritten = await
|
|
77
|
+
bytesWritten = await bucket.write('some/path', streamOrGenerator)
|
|
78
78
|
|
|
79
79
|
// read an entire file in one-shot
|
|
80
|
-
|
|
80
|
+
// returns null if an exception would otherwise be thrown
|
|
81
|
+
const bufferOrNull = await bucket.safeReadFile('some/path')
|
|
81
82
|
|
|
82
|
-
// read
|
|
83
|
-
const bytesRead = await
|
|
83
|
+
// stream-read a file, passing data to callback
|
|
84
|
+
const bytesRead = await bucket.read('some/path', (chunk) => { ...do something with chunk... } )
|
|
84
85
|
|
|
85
86
|
// remove a file, returns the path removed
|
|
86
|
-
let removed = await
|
|
87
|
+
let removed = await bucket.remove('some/path') // removed is a string
|
|
87
88
|
|
|
88
89
|
// remove a directory, returns array of paths removed
|
|
89
|
-
removed = await
|
|
90
|
+
removed = await bucket.remove('some/directory', {recursive: true}) // removed is now an array!
|
|
90
91
|
|
|
91
92
|
----
|
|
92
93
|
## Basic usage
|
|
94
|
+
A much more extensive example, showing most of the features offered:
|
|
95
|
+
|
|
93
96
|
const { mobiletto } = require('mobiletto')
|
|
94
97
|
|
|
95
98
|
// General usage
|
|
@@ -105,8 +108,8 @@ This code would run the same if the driver were `b2` or `local`.
|
|
|
105
108
|
const local = await mobiletto('local', '/home/ubuntu/tmp', null, {fileMode: 0o0600, dirMode: '0700'})
|
|
106
109
|
|
|
107
110
|
// To use 's3' driver:
|
|
108
|
-
// * key: AWS
|
|
109
|
-
// * secret: AWS
|
|
111
|
+
// * key: AWS Access Key ID
|
|
112
|
+
// * secret: AWS Secret Key
|
|
110
113
|
// * opts object:
|
|
111
114
|
// * readOnly: optional, never change anything on the bucket; default is false
|
|
112
115
|
// * bucket: required, name of the S3 bucket
|
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@ const fs = require('fs')
|
|
|
2
2
|
const path = require('path')
|
|
3
3
|
const { Readable, Transform } = require('stream')
|
|
4
4
|
const { logger, setLogLevel: setLoggerLevel } = require('./util/logger')
|
|
5
|
-
const { getRedis } = require('./util/redis')
|
|
5
|
+
const { getRedis, teardown } = require('./util/redis')
|
|
6
6
|
const { basename, dirname } = require('path')
|
|
7
7
|
const shasum = require('shasum')
|
|
8
8
|
const randomstring = require('randomstring')
|
|
@@ -199,6 +199,14 @@ const UTILITY_FUNCTIONS = {
|
|
|
199
199
|
}
|
|
200
200
|
return data
|
|
201
201
|
},
|
|
202
|
+
safeReadFile: (client) => async (path) => {
|
|
203
|
+
try {
|
|
204
|
+
return await client.readFile(path)
|
|
205
|
+
} catch (e) {
|
|
206
|
+
logger.info(`safeReadFile(${path}): ${e}`)
|
|
207
|
+
return null
|
|
208
|
+
}
|
|
209
|
+
},
|
|
202
210
|
write: (client) => async (path, data) => {
|
|
203
211
|
logger.debug(`util.write(${path}) starting ...`)
|
|
204
212
|
// noinspection JSUnresolvedFunction
|
|
@@ -733,11 +741,14 @@ const M_SPECIAL = 'special'
|
|
|
733
741
|
|
|
734
742
|
const setLogLevel = level => setLoggerLevel(level)
|
|
735
743
|
|
|
744
|
+
const closeRedis = async () => { await teardown() }
|
|
745
|
+
|
|
736
746
|
module.exports = {
|
|
737
747
|
M_FILE, M_DIR, M_LINK, M_SPECIAL,
|
|
738
748
|
isAsyncGenerator, isReadable,
|
|
739
749
|
mobiletto, connect,
|
|
740
750
|
MobilettoError, MobilettoNotFoundError,
|
|
741
751
|
setLogLevel,
|
|
742
|
-
readStream, writeStream, closeStream
|
|
752
|
+
readStream, writeStream, closeStream,
|
|
753
|
+
closeRedis
|
|
743
754
|
}
|
package/package.json
CHANGED
package/test/api.spec.js
CHANGED
|
@@ -12,7 +12,7 @@ const { expect, should, assert } = require('chai')
|
|
|
12
12
|
|
|
13
13
|
const {
|
|
14
14
|
M_DIR, M_FILE,
|
|
15
|
-
mobiletto, connect, MobilettoNotFoundError,
|
|
15
|
+
mobiletto, connect, MobilettoNotFoundError, closeRedis
|
|
16
16
|
} = require("../index")
|
|
17
17
|
|
|
18
18
|
const { logger } = require("../util/logger")
|
|
@@ -481,5 +481,5 @@ for (const driverName of DRIVER_NAMES) {
|
|
|
481
481
|
|
|
482
482
|
after ( (done) => {
|
|
483
483
|
logger.info('all tests finished, tearing down redis...')
|
|
484
|
-
|
|
484
|
+
closeRedis().finally(done)
|
|
485
485
|
})
|
package/util/redis.js
CHANGED
|
@@ -187,6 +187,7 @@ const teardown = async () => await forAllCaches((client) => {
|
|
|
187
187
|
try {
|
|
188
188
|
if (client.redis) {
|
|
189
189
|
client.redis.disconnect()
|
|
190
|
+
client.redis = null
|
|
190
191
|
}
|
|
191
192
|
} catch (e) {
|
|
192
193
|
logger.warn(`teardown: error disconnecting from redis(${client.name}): ${e}`)
|