-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcontextFactory.js
75 lines (65 loc) · 1.35 KB
/
contextFactory.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
import npm from './adapters/npm';
import S3 from './adapters/s3';
import Logger from './adapters/logger';
const user = authorizer => ({
name: authorizer.username,
avatar: authorizer.avatar,
});
const command = (headers) => {
if (headers.Referer) {
const refererParts = headers.Referer.split(' ');
const name = refererParts[0];
return {
name,
args: refererParts.slice(1),
};
}
return {
name: 'Unknown',
args: [],
};
};
const storage = (region, bucket) =>
new S3({
region,
bucket,
});
const log = (cmd, namespace, region, topic) => {
if (process.env.clientId && process.env.secret) {
return new Logger(
cmd,
namespace,
{
clientId: process.env.clientId,
secret: process.env.secret,
},
);
}
return new Logger(
cmd,
namespace, {
region,
topic,
});
};
export default (namespace, { headers, requestContext }) => {
const {
registry,
cacheEnabled,
bucket,
bucketCache,
region,
logTopic,
} = process.env;
const cmd = command(headers);
return {
command: cmd,
cacheEnabled: (cacheEnabled === 'true'),
registry,
user: user(requestContext.authorizer),
storage: storage(region, bucket),
cache: storage(region, bucketCache),
log: log(cmd, namespace, region, logTopic),
npm,
};
};