forked from craftship/codebox-npm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.js
57 lines (49 loc) · 1.54 KB
/
get.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
import npm from './adapters/npm';
import S3 from './adapters/s3';
import Logger from './adapters/logger';
export default async (event, context, callback) => {
const { registry, bucket, region, logTopic } = process.env;
const user = {
name: event.requestContext.authorizer.username,
avatar: event.requestContext.authorizer.avatar,
};
const storage = new S3({ region, bucket });
const log = new Logger('package:get', { region, topic: logTopic });
const name = `${decodeURIComponent(event.pathParameters.name)}`;
try {
const pkgBuffer = await storage.get(`${name}/index.json`);
const json = JSON.parse(pkgBuffer.toString());
json._attachments = {}; // eslint-disable-line no-underscore-dangle
return callback(null, {
statusCode: 200,
body: JSON.stringify(json),
});
} catch (storageError) {
if (storageError.code === 'NoSuchKey') {
try {
const data = await npm.package(registry, event.pathParameters.name);
return callback(null, {
statusCode: 200,
body: JSON.stringify(data),
});
} catch (npmError) {
if (npmError.status === 500) {
await log.error(user, npmError);
}
return callback(null, {
statusCode: npmError.status,
body: JSON.stringify({
error: npmError.message,
}),
});
}
}
await log.error(user, storageError);
return callback(null, {
statusCode: 500,
body: JSON.stringify({
error: storageError.message,
}),
});
}
};