mobiletto 1.2.0 → 2.0.0

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 (127) hide show
  1. package/lib/cjs/index.d.ts +2 -0
  2. package/lib/cjs/index.js +30 -0
  3. package/lib/cjs/package.json +16 -0
  4. package/lib/esm/index.d.ts +2 -0
  5. package/lib/esm/index.js +12 -0
  6. package/package.json +27 -13
  7. package/.babelrc +0 -5
  8. package/.eslintrc.js +0 -14
  9. package/docs/README.md +0 -113
  10. package/docs/af/README.md +0 -481
  11. package/docs/am/README.md +0 -481
  12. package/docs/ar/README.md +0 -481
  13. package/docs/az/README.md +0 -481
  14. package/docs/be/README.md +0 -481
  15. package/docs/bg/README.md +0 -481
  16. package/docs/bn/README.md +0 -481
  17. package/docs/bs/README.md +0 -481
  18. package/docs/ca/README.md +0 -481
  19. package/docs/ceb/README.md +0 -481
  20. package/docs/co/README.md +0 -481
  21. package/docs/cs/README.md +0 -481
  22. package/docs/cy/README.md +0 -481
  23. package/docs/da/README.md +0 -481
  24. package/docs/de/README.md +0 -481
  25. package/docs/el/README.md +0 -481
  26. package/docs/eo/README.md +0 -481
  27. package/docs/es/README.md +0 -481
  28. package/docs/et/README.md +0 -481
  29. package/docs/eu/README.md +0 -481
  30. package/docs/fa/README.md +0 -481
  31. package/docs/fi/README.md +0 -481
  32. package/docs/fr/README.md +0 -481
  33. package/docs/fy/README.md +0 -481
  34. package/docs/ga/README.md +0 -481
  35. package/docs/gd/README.md +0 -481
  36. package/docs/gl/README.md +0 -481
  37. package/docs/gu/README.md +0 -481
  38. package/docs/ha/README.md +0 -481
  39. package/docs/haw/README.md +0 -481
  40. package/docs/he/README.md +0 -481
  41. package/docs/hi/README.md +0 -481
  42. package/docs/hmn/README.md +0 -481
  43. package/docs/hr/README.md +0 -481
  44. package/docs/ht/README.md +0 -481
  45. package/docs/hu/README.md +0 -481
  46. package/docs/hy/README.md +0 -481
  47. package/docs/id/README.md +0 -481
  48. package/docs/ig/README.md +0 -481
  49. package/docs/is/README.md +0 -481
  50. package/docs/it/README.md +0 -481
  51. package/docs/iw/README.md +0 -481
  52. package/docs/ja/README.md +0 -481
  53. package/docs/jv/README.md +0 -481
  54. package/docs/ka/README.md +0 -481
  55. package/docs/kk/README.md +0 -481
  56. package/docs/km/README.md +0 -481
  57. package/docs/kn/README.md +0 -481
  58. package/docs/ko/README.md +0 -481
  59. package/docs/ku/README.md +0 -481
  60. package/docs/ky/README.md +0 -481
  61. package/docs/la/README.md +0 -481
  62. package/docs/lb/README.md +0 -481
  63. package/docs/lo/README.md +0 -481
  64. package/docs/lt/README.md +0 -481
  65. package/docs/lv/README.md +0 -481
  66. package/docs/mg/README.md +0 -481
  67. package/docs/mi/README.md +0 -481
  68. package/docs/mk/README.md +0 -481
  69. package/docs/ml/README.md +0 -481
  70. package/docs/mn/README.md +0 -481
  71. package/docs/mr/README.md +0 -481
  72. package/docs/ms/README.md +0 -481
  73. package/docs/mt/README.md +0 -481
  74. package/docs/my/README.md +0 -481
  75. package/docs/ne/README.md +0 -481
  76. package/docs/nl/README.md +0 -481
  77. package/docs/no/README.md +0 -481
  78. package/docs/ny/README.md +0 -481
  79. package/docs/or/README.md +0 -481
  80. package/docs/pa/README.md +0 -481
  81. package/docs/pl/README.md +0 -481
  82. package/docs/ps/README.md +0 -481
  83. package/docs/pt/README.md +0 -481
  84. package/docs/ro/README.md +0 -481
  85. package/docs/ru/README.md +0 -481
  86. package/docs/rw/README.md +0 -481
  87. package/docs/sd/README.md +0 -481
  88. package/docs/si/README.md +0 -481
  89. package/docs/sk/README.md +0 -481
  90. package/docs/sl/README.md +0 -481
  91. package/docs/sm/README.md +0 -481
  92. package/docs/sn/README.md +0 -481
  93. package/docs/so/README.md +0 -481
  94. package/docs/sq/README.md +0 -481
  95. package/docs/sr/README.md +0 -481
  96. package/docs/st/README.md +0 -481
  97. package/docs/su/README.md +0 -481
  98. package/docs/sv/README.md +0 -481
  99. package/docs/sw/README.md +0 -481
  100. package/docs/ta/README.md +0 -481
  101. package/docs/te/README.md +0 -481
  102. package/docs/tg/README.md +0 -481
  103. package/docs/th/README.md +0 -481
  104. package/docs/tk/README.md +0 -481
  105. package/docs/tl/README.md +0 -481
  106. package/docs/tr/README.md +0 -481
  107. package/docs/tt/README.md +0 -481
  108. package/docs/ug/README.md +0 -481
  109. package/docs/uk/README.md +0 -481
  110. package/docs/ur/README.md +0 -481
  111. package/docs/uz/README.md +0 -481
  112. package/docs/vi/README.md +0 -481
  113. package/docs/xh/README.md +0 -481
  114. package/docs/yi/README.md +0 -481
  115. package/docs/yo/README.md +0 -481
  116. package/docs/zh/README.md +0 -481
  117. package/docs/zu/README.md +0 -481
  118. package/drivers/b2/index.js +0 -451
  119. package/drivers/local/index.js +0 -315
  120. package/drivers/s3/index.js +0 -316
  121. package/hokey.json +0 -16
  122. package/index.js +0 -10
  123. package/test/api.spec.js +0 -512
  124. package/test/crypt.spec.js +0 -67
  125. package/util/crypt.js +0 -80
  126. package/util/logger.js +0 -27
  127. package/util/redis.js +0 -233
package/util/redis.js DELETED
@@ -1,233 +0,0 @@
1
- const Redis = require('ioredis')
2
- const redisScan = require('node-redis-scan')
3
- const { logger } = require('./logger')
4
- const LRU = require('lru-cache')
5
-
6
- const { DEFAULT_REDIS_OPTIONS } = require('ioredis/built/redis/RedisOptions')
7
-
8
- let REDIS_CLIENTS = {}
9
-
10
- const getRedis = (name, host, port, prefix) => {
11
- if (typeof REDIS_CLIENTS[name] === 'undefined') {
12
- REDIS_CLIENTS[name] = new MobilettoCache(name, host, port, prefix + name + '_')
13
- }
14
- return REDIS_CLIENTS[name]
15
- }
16
-
17
- const DEFAULT_EXPIRATION_MILLIS = 1000 * 60 * 60 * 24 // 1 day
18
-
19
- const ZERO_COUNTERS = {
20
- get: 0,
21
- set: 0,
22
- del: 0,
23
- flush: 0,
24
- hit: 0,
25
- miss: 0
26
- }
27
-
28
- class MobilettoCache {
29
- name
30
- redis
31
- scanner
32
- prefix
33
- scopedCaches = {}
34
- counters = Object.assign({}, ZERO_COUNTERS)
35
- printStatsInterval = 1000
36
- constructor(name,
37
- host = '127.0.0.1',
38
- port = 6379,
39
- prefix = '_mobiletto__') {
40
- this.name = name
41
- if (host && port) {
42
- try {
43
- this.redis = new Redis(Object.assign({}, DEFAULT_REDIS_OPTIONS, {host, port}))
44
- } catch (e) {
45
- logger.error(`redis(${name}) error connecting to redis, using fallback LRU for scoped caches: ${e}`)
46
- this.redis = null
47
- }
48
- } else {
49
- logger.warn(`redis(${name}) no host or port provided, using fallback LRU for scoped caches`)
50
- this.redis = null
51
- }
52
- this.prefix = prefix
53
- if (this.redis) {
54
- // test connection by flushing
55
- this.scanner = new redisScan(this.redis)
56
- this.flush()
57
- .then(() => { logger.debug(`redis(${name}) successfully flushed`) })
58
- .catch((e) => {
59
- logger.warn(`redis(${name}) error flushing: ${e}, disabling redis`)
60
- this.redis = null
61
- })
62
- }
63
- }
64
-
65
- stats = () => this.counters
66
- resetStats = () => { this.counters = Object.assign({}, ZERO_COUNTERS) }
67
- hitRate = () => this.counters.get === 0 ? 0 : (100 * this.counters.hit) / this.counters.get
68
- toString = () => `MobilettoCache(${this.name}) [${this.counters.hit}/${this.counters.get} = ${this.hitRate()}% hit] stats=${JSON.stringify(this.counters)}`
69
-
70
- pfx = key => key.startsWith(this.prefix) ? key : this.prefix + key
71
- unprefix = key => key && key.startsWith(this.prefix) ? key.substring(this.prefix.length) : key
72
-
73
- doRedisAsync = async (func, defaultValue = null) => {
74
- try {
75
- return this.redis ? await func(this.redis) : defaultValue
76
- } catch (e) {
77
- logger.warn(`redis(${this.name}) doRedisAsync(${func}) ${e} (returning default value: ${defaultValue})`)
78
- return defaultValue
79
- }
80
- }
81
-
82
- doRedis = (func, defaultValue = null) => {
83
- try {
84
- return this.redis ? func(this.redis) : defaultValue
85
- } catch (e) {
86
- logger.warn(`redis(${this.name}) doRedis(${func}) ${e} (returning default value: ${defaultValue})`)
87
- return defaultValue
88
- }
89
- }
90
-
91
- get = async key => {
92
- this.counters.get++
93
- logger.silly(`redis(${this.name}) get(${key}) starting`)
94
- const val = await this.doRedis(r => r.get(this.pfx(key)), null)
95
- logger.silly(`redis(${this.name}) get(${key}) found value: ${val}`)
96
- if (val) {
97
- this.counters.hit++
98
- } else {
99
- this.counters.miss++
100
- }
101
- if (this.printStatsInterval && this.printStatsInterval > 0 && this.counters.get % this.printStatsInterval === 0) {
102
- const message = `${new Date()}: ${this}`
103
- logger.info(message)
104
- }
105
- return val
106
- }
107
-
108
- set = async (key, val, expirationMillis = DEFAULT_EXPIRATION_MILLIS) => {
109
- this.counters.set++
110
- logger.silly(`redis(${this.name}) set(${key}, ${val}, ${expirationMillis}) starting`)
111
- await this.doRedisAsync(r => r.set(this.pfx(key), val, 'EX', expirationMillis / 1000))
112
- logger.silly(`redis(${this.name}) set(${key}, ${val}, ${expirationMillis}) finished`)
113
- }
114
-
115
- del = async key => {
116
- this.counters.del++
117
- await this.doRedisAsync(r => r.del(this.pfx(key)))
118
- }
119
-
120
- findMatchingKeys = async (pattern) => {
121
- return await new Promise((resolve, reject) => {
122
- this.scanner.scan(this.pfx(pattern), (err, matchingKeys) => {
123
- if (err) reject(err)
124
- resolve(matchingKeys)
125
- })
126
- })
127
- }
128
-
129
- applyToMatchingKeys = async (pattern, asyncFunc) => {
130
- const results = []
131
- let keyMatchCount = 0
132
- const logPrefix = `applyToMatchingKeys(${pattern}, ${asyncFunc && asyncFunc.name ? asyncFunc.name : 'nameless function'})`
133
- const start = Date.now()
134
- return await new Promise((resolve, reject) => {
135
- this.scanner.eachScan(this.pfx(pattern), {}, async (matchingKeys) => {
136
- if (!matchingKeys || typeof matchingKeys.length !== 'number') {
137
- const message = `${logPrefix}: eachScan received invalid matchingKeys = ${typeof matchingKeys !== 'undefined' ? matchingKeys : 'undefined'}`
138
- logger.error(message)
139
- reject(message)
140
- } else {
141
- keyMatchCount += matchingKeys.length
142
- }
143
- for (const key of matchingKeys) {
144
- asyncFunc(key)
145
- .then((val) => {
146
- if (typeof val !== 'undefined') {
147
- logger.silly(`${logPrefix} found key ${key} = ${val}`)
148
- results.push(val)
149
- } else {
150
- logger.silly(`${logPrefix} found key with undefined value: ${key}`)
151
- }
152
- })
153
- .catch((e) => {
154
- const message = `${logPrefix} error with key ${key}: ${e}`
155
- logger.error(message)
156
- reject(message)
157
- })
158
- }
159
- }, (err, matchCount) => {
160
- if (err) {
161
- logger.error(`${logPrefix} error in final callback: ${err}`)
162
- reject(err)
163
- }
164
- const duration = Date.now() - start
165
- logger.debug(`${logPrefix} resolving with matchCount=${matchCount}, keyMatchCount=${keyMatchCount}, results.length=${results.length}, duration ${duration/1000} seconds`)
166
- resolve(results)
167
- })
168
- })
169
- }
170
-
171
- removeMatchingKeys = async pattern => await this.applyToMatchingKeys(pattern, this.del)
172
-
173
- flush = async () => {
174
- this.counters.flush++
175
- await this.removeMatchingKeys('*')
176
- }
177
-
178
- scopedCache = (name, size = 100) => {
179
- if (this.scopedCaches[name]) {
180
- return this.scopedCaches[name]
181
- }
182
- if (!this.redis) {
183
- return size && size > 0 ? new LRU({ max: size }) : null
184
- }
185
- const realKey = k => k ? `${name}_${k}` : null
186
- const cache = {
187
- get: async key => {
188
- const val = await this.get(realKey(key))
189
- if (!val) return null
190
- try {
191
- return JSON.parse(val)
192
- } catch (e) {
193
- logger.warn(`redis(${this.name}) get(${key}) error: ${e}`)
194
- return null
195
- }
196
- },
197
- set: async (key, value) => {
198
- if (key && value) {
199
- try {
200
- await this.set(realKey(key), JSON.stringify(value))
201
- } catch (e) {
202
- logger.warn(`redis(${this.name}) set(${key}) error: ${e}`)
203
- }
204
- }
205
- }
206
- }
207
- this.scopedCaches[name] = cache
208
- return cache
209
- }
210
- }
211
-
212
- const forAllCaches = async (func) => {
213
- const promises = []
214
- for (const name of Object.keys(REDIS_CLIENTS)) {
215
- promises.push(func(REDIS_CLIENTS[name]))
216
- }
217
- return Promise.all(promises)
218
- }
219
-
220
- const teardown = async () => await forAllCaches((client) => {
221
- try {
222
- if (client.redis) {
223
- client.redis.disconnect()
224
- client.redis = null
225
- }
226
- } catch (e) {
227
- logger.warn(`teardown: error disconnecting from redis(${client.name}) ${e}`)
228
- }
229
- })
230
-
231
- const flushAll = async () => await forAllCaches(client => client.flush())
232
-
233
- module.exports = { getRedis, flushAll, teardown }