-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (117 loc) · 3.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const path = require('path');
const fs = require('fs');
const uuid = require('uuid');
const cryptoTools = require('./lib/utils/crypto-tools');
const events = require('./lib/events');
const persistence = require('./lib/persistence');
const locks = require('./lib/locks');
const authMixin = require('./lib/auth');
const middlewareMixin = require('./lib/middleware');
const storageMixin = require('./lib/storage');
function resolveConfigPath(...parts) {
return path.join(process.mainModule.filename, ...parts);
}
// At least one of the plugin keys should point to this module
function checkPluginCfg(obj) {
return (
obj &&
typeof obj === 'object' &&
Object.keys(obj).reduce((found, k) => {
return (
found ||
(() => {
const mod = k[0] !== '.' ? k : resolveConfigPath(k);
const modPath = require.resolve(mod);
return modPath === __filename;
})()
);
}, false)
);
}
let singleton;
function ClusterStorage(config, params) {
console.log('Loading verdaccio-clustering');
if (singleton) {
console.log('Returning singleton');
return singleton;
}
if (!(this instanceof ClusterStorage)) {
console.log('Calling `new ClusterStorage(...)`');
// since verdaccio only uses new for ES6+ modules, help it out
return new ClusterStorage(config, params);
}
console.log('Plugin Config:', config);
console.log('Params:', params);
if (
!(
checkPluginCfg(params.config.auth) &&
checkPluginCfg(params.config.middlewares) &&
checkPluginCfg(params.config.store)
)
) {
console.error('Unsupported clustering configuration');
throw new Error(
'Clustering must be set as the auth, middleware, and storage plugin'
);
}
// always return the same object
singleton = this;
singleton.nodeId = uuid.v4();
const clusterConfig = params.config[config.key] || {};
const clusterPrivateKey =
process.env.VERDACCIO_CLUSTER_PRIV_KEY ||
fs.readFileSync(
resolveConfigPath(clusterConfig.privateKey || './cluster-private.key'),
{ encoding: 'utf8' }
);
const clusterPublicKey =
process.env.VERDACCIO_CLUSTER_PUBLIC_KEY ||
fs.readFileSync(
resolveConfigPath(clusterConfig.publicKey || './cluster-public.key'),
{ encoding: 'utf8' }
);
const { encrypt, decrypt, sign, verify } = cryptoTools({
privateKey: clusterPrivateKey,
publicKey: clusterPublicKey,
nodeId: singleton.nodeId
});
/*
Provide common events and persistence facades. These facades
can return synchronously, but load the wrapped module (plugin) in a promise
so long as all methods on the facade are asynchronous / non-blocking.
This helps avoid an issue with the streaming interface provided by (read|write)Tarball
that the promise-based interface would otherwise encounter.
For the events interface, events will be emitted synchronously locally as is normal for
an event emitter, but delivery of the event to remote listeners in the cluster is explicitly
asynchronous and non-blocking.
*/
const context = {
params,
events: events(clusterConfig.events),
persistence: persistence(clusterConfig.persistence),
encrypt,
decrypt,
sign,
verify
};
// initialize the (http request) lock event handling with our context
locks(context);
/*
Manually binding rather than using the prototype chain so that we can
pass the context promise - including the persistence interface - to the storage functions.
*/
Object.keys(authMixin).forEach(k => {
const fn = storageMixin[k];
singleton[k] = fn.bind(singleton, context);
});
Object.keys(middlewareMixin).forEach(k => {
const fn = storageMixin[k];
singleton[k] = fn.bind(singleton, context);
});
Object.keys(storageMixin).forEach(k => {
const fn = storageMixin[k];
singleton[k] = fn.bind(singleton, context);
});
return singleton;
}
module.exports = ClusterStorage;