@startupjs/backend
Advanced tools
Comparing version 0.42.0 to 0.42.2
72
index.js
@@ -14,3 +14,4 @@ const ShareDbAccess = require('@startupjs/sharedb-access') | ||
const shareDbHooks = require('sharedb-hooks') | ||
const getShareMongo = require('./getShareMongo') | ||
const getShareDbMongo = require('./getShareDbMongo') | ||
const getRedis = require('./getRedis') | ||
@@ -27,36 +28,20 @@ global.__clients = {} | ||
/* | ||
IMPORTANT: | ||
If you use Redis in your business logic (for example to implement locking with `redlock`) | ||
you should use `redisPrefix` which is returned by this function for all | ||
your redis keys and lock keys. This will prevent bugs when you have multiple staging | ||
apps using the same redis DB. | ||
*/ | ||
module.exports = async options => { | ||
// Use prefix for ShareDB's pubsub. This prevents issues with multiple | ||
// projects using the same redis db. | ||
// We use a combination of MONGO_URL and BASE_URL to generate a simple | ||
// hash because together they are going to be unique no matter whether | ||
// it's run on localhost or on the production server. | ||
// ref: https://github.com/share/sharedb/issues/420 | ||
const REDIS_PREFIX = '_' + simpleNumericHash(conf.get('MONGO_URL') + conf.get('BASE_URL')) | ||
const REDIS_URL = conf.get('REDIS_URL') | ||
let redisClient | ||
options = Object.assign({ secure: true }, options) | ||
// -------------------------------------------------------> storeUse <# | ||
if (options.ee != null) options.ee.emit('storeUse', racer) | ||
// ShareDB Setup | ||
const shareMongo = await getShareMongo() | ||
const shareDbMongo = await getShareDbMongo() | ||
if (options.pollDebounce) shareDbMongo.pollDebounce = options.pollDebounce | ||
if (options.pollDebounce) shareMongo.pollDebounce = options.pollDebounce | ||
let redisClient | ||
let redisPrefix | ||
let backend = (() => { | ||
// For horizontal scaling, in production, redis is required. | ||
if (REDIS_URL && !conf.get('NO_REDIS')) { | ||
redisClient = redis.createClient({ url: REDIS_URL }) | ||
let redisObserver = redis.createClient({ url: REDIS_URL }) | ||
if (!conf.get('NO_REDIS')) { | ||
const redis = getRedis() | ||
const redisObserver = redis.observer | ||
redisClient = redis.client | ||
redisPrefix = redis.prefix | ||
// Flush redis when starting the app. | ||
@@ -116,13 +101,11 @@ // When running in cluster this should only run on the first instance. | ||
client: redisClient, | ||
observer: redisObserver, | ||
prefix: REDIS_PREFIX | ||
observer: redisObserver | ||
}) | ||
return racer.createBackend({ | ||
db: shareMongo, | ||
db: shareDbMongo, | ||
pubsub: pubsub, | ||
extraDbs: options.extraDbs | ||
}) | ||
// redis alternative | ||
// redis alternative | ||
} else if (conf.get('WSBUS_URL') && !conf.get('NO_WSBUS')) { | ||
@@ -137,3 +120,3 @@ if (!wsbusPubSub) { | ||
return racer.createBackend({ | ||
db: shareMongo, | ||
db: shareDbMongo, | ||
pubsub: pubsub, | ||
@@ -145,3 +128,3 @@ extraDbs: options.extraDbs | ||
return racer.createBackend({ | ||
db: shareMongo, | ||
db: shareDbMongo, | ||
extraDbs: options.extraDbs | ||
@@ -278,3 +261,3 @@ }) | ||
options.ee.emit('backend', backend, { | ||
mongo: shareMongo.mongo | ||
mongo: shareDbMongo.mongo | ||
}) | ||
@@ -285,9 +268,10 @@ } | ||
backend, | ||
shareMongo, // you can get mongo client from shareMongo.mongo | ||
shareMongo: shareDbMongo, // mock old name of shareDbMongo | ||
shareDbMongo, // you can get mongo client from shareDbMongo.mongo | ||
redisClient, // you can directly pass this redis client to redlock | ||
redisPrefix: REDIS_PREFIX, // use this for you redis prefixes (and redlock prefixes) | ||
redisPrefix, // use this for you redis prefixes (and redlock prefixes) | ||
// mock old redis-url api. TODO: get rid of this after we refactor other libs to use redisClient directly | ||
redis: { | ||
connect () { | ||
return redis.createClient({ url: REDIS_URL }) | ||
return redis.createClient({ url: process.env.REDIS_URL }) | ||
} | ||
@@ -305,13 +289,1 @@ } | ||
} | ||
// ref: https://stackoverflow.com/a/8831937 | ||
function simpleNumericHash (source) { | ||
let hash = 0 | ||
if (source.length === 0) return hash | ||
for (let i = 0; i < source.length; i++) { | ||
const char = source.charCodeAt(i) | ||
hash = ((hash << 5) - hash) + char | ||
hash = hash & hash // Convert to 32bit integer | ||
} | ||
return hash | ||
} |
{ | ||
"name": "@startupjs/backend", | ||
"version": "0.42.0", | ||
"version": "0.42.2", | ||
"description": "Create new ShareDB backend instance", | ||
@@ -15,4 +15,4 @@ "main": "index.js", | ||
"@startupjs/sharedb-schema": "^0.42.0", | ||
"ioredis": "^4.28.2", | ||
"racer": "1.0.0", | ||
"redis": "^3.1.2", | ||
"redlock": "^4.2.0", | ||
@@ -31,3 +31,3 @@ "sharedb": "^1.0.0", | ||
}, | ||
"gitHead": "c1f6702f87fc833d73ee693bb353304b9279a917" | ||
"gitHead": "25170f4aa225553093a6e38ea066009a0a3edce0" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14312
5
364
10
+ Addedioredis@^4.28.2
+ Addedcluster-key-slot@1.1.2(transitive)
+ Addedioredis@4.28.5(transitive)
+ Addedlodash.defaults@4.2.0(transitive)
+ Addedlodash.flatten@4.4.0(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedp-map@2.1.0(transitive)
+ Addedstandard-as-callback@2.1.0(transitive)
- Removedredis@^3.1.2