UNPKG

1.82 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ERROR_S3_NO_SUCH_KEY = new Error('NoSuchKey');
4class S3Client {
5 constructor(client) {
6 this.client = client;
7 }
8 download(bucket, key, ws) {
9 return new Promise((resolve, reject) => {
10 ws.on('error', err => {
11 reject(err);
12 });
13 const rs = this.client.getObject({ Bucket: bucket, Key: key }).createReadStream();
14 rs.on('error', (err) => {
15 if (err.code === 'NoSuchKey') {
16 return reject(exports.ERROR_S3_NO_SUCH_KEY);
17 }
18 reject(err);
19 });
20 rs.on('end', () => {
21 resolve();
22 });
23 rs.pipe(ws);
24 });
25 }
26 async upload(bucket, key, rs, params) {
27 await this.client.upload({ ...params, Body: rs, Bucket: bucket, Key: key }).promise();
28 }
29}
30exports.S3Client = S3Client;
31class ECRClient {
32 constructor(client) {
33 this.client = client;
34 }
35 async getLoginCredentials() {
36 const res = await this.client.getAuthorizationToken().promise();
37 if (!res.authorizationData) {
38 throw new Error('missing authorization data');
39 }
40 // Just use the first... ?
41 const [data] = res.authorizationData;
42 if (!data.authorizationToken) {
43 throw new Error('missing authorization token');
44 }
45 if (!data.proxyEndpoint) {
46 throw new Error('missing proxy endpoint');
47 }
48 const credentials = Buffer.from(data.authorizationToken, 'base64').toString();
49 const [username, password] = credentials.split(':');
50 return { username, password, endpoint: data.proxyEndpoint };
51 }
52}
53exports.ECRClient = ECRClient;