mobiletto 1.0.13 → 1.0.14

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.
Files changed (3) hide show
  1. package/README.md +9 -3
  2. package/index.js +36 -8
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -122,9 +122,15 @@ Enable transparent client-side encryption:
122
122
 
123
123
  // Pass encryption parameters
124
124
  const encryption = {
125
- key: randomstring.generate(128), // required, must be >= 32 chars
126
- iv: randomstring.generate(128), // optional, default is to derive IV from key
127
- algo: 'aes-256-cbc' // optional, aes-256-cbc is the default
125
+ // key is required, must be >= 16 chars
126
+ key: randomstring.generate(128),
127
+
128
+ // optional, the default is to derive IV from key
129
+ // when set, IV must be >= 16 chars
130
+ iv: randomstring.generate(128),
131
+
132
+ // optional, the default is aes-256-cbc
133
+ algo: 'aes-256-cbc'
128
134
  }
129
135
  const api = await mobiletto(driverName, key, secret, opts, encryption)
130
136
 
package/index.js CHANGED
@@ -1,10 +1,24 @@
1
1
  const fs = require('fs')
2
+ const winston = require('winston')
2
3
  const { basename, dirname } = require('path')
3
4
  const shasum = require('shasum')
4
5
  const randomstring = require('randomstring')
5
6
 
6
7
  const crypt = require('./util/crypt')
7
- const {DEFAULT_CRYPT_ALGO, normalizeKey, normalizeIV, encrypt, decrypt} = require("./util/crypt");
8
+ const {DEFAULT_CRYPT_ALGO, normalizeKey, normalizeIV, encrypt, decrypt} = require("./util/crypt")
9
+
10
+ const logger = winston.createLogger({
11
+ level: process.env.MOBILETTO_LOG_LEVEL || 'warn',
12
+ format: winston.format.simple(),
13
+ transports: [ new winston.transports.Console() ]
14
+ })
15
+
16
+ const setLogLevel = (level) => { logger.level = level }
17
+
18
+ const setLogTransports = (transports) => {
19
+ logger.transports.splice(0, logger.transports.length)
20
+ logger.transports.push(...transports)
21
+ }
8
22
 
9
23
  const DIR_ENT_DIR_SUFFIX = '__.dirent'
10
24
  const DIR_ENT_FILE_PREFIX = 'dirent__'
@@ -68,12 +82,14 @@ const UTILITY_FUNCTIONS = {
68
82
  return await client.write(path, readFunc())
69
83
  },
70
84
  mirror: (client) => async (source, clientPath = '', sourcePath = '') => {
85
+ logger.info(`mirror: starting, sourcePath=${sourcePath} -> clientPath=${clientPath}`)
71
86
  const results = {
72
87
  success: 0,
73
88
  errors: 0
74
89
  }
75
90
  const visitor = async (obj) => {
76
91
  if (obj.type && obj.type === M_FILE) {
92
+ logger.verbose(`mirror: mirroring file: ${obj.name}`)
77
93
  const tempPath = `${MOBILETTO_TMP}/mobiletto_${shasum(JSON.stringify(obj))}.${randomstring.generate(10)}`
78
94
  try {
79
95
  // write from source -> write to temp file
@@ -96,9 +112,10 @@ const UTILITY_FUNCTIONS = {
96
112
  })
97
113
  results.success++
98
114
  } catch (e) {
99
- console.warn(`mirror: error copying file: ${e}`)
115
+ logger.warn(`mirror: error copying file: ${e}`)
100
116
  results.errors++
101
117
  } finally {
118
+ logger.verbose(`mirror: file mirrored successfully: ${obj.name}`)
102
119
  fs.rmSync(tempPath, {force: true})
103
120
  }
104
121
  }
@@ -109,6 +126,7 @@ const UTILITY_FUNCTIONS = {
109
126
  }
110
127
 
111
128
  async function mirrorDir (source, sourcePath, visitor) {
129
+ logger.verbose(`mirrorDir: mirroring dir: ${sourcePath}`)
112
130
  const listing = await source.list(sourcePath, {recursive: false, visitor})
113
131
  for (const obj of listing) {
114
132
  if (obj.type === M_DIR) {
@@ -126,7 +144,7 @@ function addUtilityFunctions (client, readOnly = false) {
126
144
  if (readOnly) {
127
145
  for (const writeFunc of ['write', 'remove', 'writeFile']) {
128
146
  client[writeFunc] = async () => {
129
- // console.warn(`${writeFunc} not supported in readOnly mode`)
147
+ logger.warn(`${writeFunc} not supported in readOnly mode`)
130
148
  return false
131
149
  }
132
150
  }
@@ -139,22 +157,30 @@ async function connect (driverPath, key, secret, opts, encryption = null) {
139
157
  }
140
158
 
141
159
  async function mobiletto (driverPath, key, secret, opts, encryption = null) {
160
+ logger.info(`mobiletto: connecting with driver ${driverPath}`)
142
161
  const driver = require(driverPath.includes('/') ? driverPath : `./drivers/${driverPath}/index.js`)
143
162
  const client = driver.storageClient(key, secret, opts)
144
163
  if (!(await client.testConfig())) {
145
- throw new MobilettoError(`mobiletto(${driverPath}) error: test API call failed`)
164
+ const message = `mobiletto(${driverPath}) error: test API call failed`
165
+ logger.error(message)
166
+ throw new MobilettoError(message)
146
167
  }
147
168
  const readOnly = opts ? !!opts.readOnly : false
148
169
  if (encryption === null) {
170
+ logger.info(`mobiletto: successfully connected using driver ${driverPath}, returning client (encryption not enabled)`)
149
171
  return addUtilityFunctions(client, readOnly)
150
172
  }
151
173
  const encKey = normalizeKey(encryption.key)
152
174
  if (!encKey) {
153
- throw new MobilettoError(`mobiletto(${driverPath}): invalid encryption key`)
175
+ const message = `mobiletto(${driverPath}): invalid encryption key`
176
+ logger.error(message)
177
+ throw new MobilettoError(message)
154
178
  }
155
179
  const iv = normalizeIV(encryption.iv, encKey)
156
180
  if (!iv) {
157
- throw new MobilettoError(`mobiletto(${driverPath}): invalid encryption IV`)
181
+ const message = `mobiletto(${driverPath}): invalid encryption IV`
182
+ logger.error(message)
183
+ throw new MobilettoError(message)
158
184
  }
159
185
  const enc = {
160
186
  key: encKey,
@@ -360,6 +386,7 @@ async function mobiletto (driverPath, key, secret, opts, encryption = null) {
360
386
  return true
361
387
  }
362
388
  }
389
+ logger.info(`mobiletto: successfully connected using driver ${driverPath}, returning client (encryption enabled)`)
363
390
  return addUtilityFunctions(encClient, readOnly)
364
391
  }
365
392
 
@@ -388,7 +415,7 @@ function writeStream (stream) {
388
415
  if (chunk) {
389
416
  stream.write(chunk, (err) => {
390
417
  if (err) {
391
- console.error(`writeStream: error writing: ${err}`)
418
+ logger.error(`writeStream: error writing: ${err}`)
392
419
  throw err
393
420
  }
394
421
  })
@@ -399,7 +426,7 @@ function writeStream (stream) {
399
426
  function closeStream (stream) {
400
427
  return () => stream.close((err) => {
401
428
  if (err) {
402
- console.error(`closeStream: error closing: ${err}`)
429
+ logger.error(`closeStream: error closing: ${err}`)
403
430
  throw err
404
431
  }
405
432
  })
@@ -414,6 +441,7 @@ module.exports = {
414
441
  M_FILE, M_DIR, M_LINK, M_SPECIAL,
415
442
  isAsyncGenerator, isReadable,
416
443
  mobiletto, connect,
444
+ setLogLevel, setLogTransports,
417
445
  MobilettoError, MobilettoNotFoundError,
418
446
  readStream, writeStream, closeStream
419
447
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "A storage abstraction layer",
5
5
  "main": "index.js",
6
6
  "author": "Jonathan Cobb",
@@ -21,6 +21,7 @@
21
21
  "@aws-sdk/client-s3": "^3.150.0",
22
22
  "@aws-sdk/lib-storage": "^3.150.0",
23
23
  "node-fetch-commonjs": "^3.1.1",
24
- "shasum": "^1.0.2"
24
+ "shasum": "^1.0.2",
25
+ "winston": "^3.8.1"
25
26
  }
26
27
  }