hackpro-sdk
Version:
113 lines • 4.62 kB
JavaScript
"use strict";
/*
* Copyright 2018 balena.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const http_1 = require("./http");
const source_destination_1 = require("./source-destination");
const zip_1 = require("./zip");
class BalenaS3Source extends source_destination_1.SourceDestination {
constructor(bucket, deviceType, version, host = 's3.amazonaws.com') {
// example:
// bucket: resin-staging-img or resin-production-img-cloudformation
// deviceType: raspberry-pi
// version: 2.9.6+rev1.prod
super();
this.bucket = bucket;
this.deviceType = deviceType;
this.version = version;
this.host = host;
this.names = ['balena', 'resin'];
this.ready = this.prepare();
}
prepare() {
return __awaiter(this, void 0, void 0, function* () {
this.name = yield this.getName();
this.rawSource = new http_1.Http(this.getUrl(`image/${this.name}.img`));
this.zipSource = new zip_1.ZipSource(new http_1.Http(this.getUrl(`image/${this.name}.img.zip`)), true);
});
}
getName() {
return __awaiter(this, void 0, void 0, function* () {
for (const name of this.names) {
try {
yield axios_1.default({ method: 'head', url: this.getUrl(`image/${name}.img`) });
return name;
}
catch (error) {
if (error.response.status !== 404) {
throw error;
}
}
}
throw new Error('Could not find image');
});
}
canCreateReadStream() {
return __awaiter(this, void 0, void 0, function* () {
return true;
});
}
canRead() {
return __awaiter(this, void 0, void 0, function* () {
return true;
});
}
getUrl(path) {
return `https://${this.bucket}.${this.host}/images/${this.deviceType}/${encodeURIComponent(this.version)}/${path}`;
}
read(buffer, bufferOffset, length, sourceOffset) {
return __awaiter(this, void 0, void 0, function* () {
yield this.ready;
return yield this.rawSource.read(buffer, bufferOffset, length, sourceOffset);
});
}
createReadStream(...args) {
return __awaiter(this, void 0, void 0, function* () {
yield this.ready;
return yield this.zipSource.createReadStream(...args);
});
}
_getMetadata() {
return __awaiter(this, void 0, void 0, function* () {
yield this.ready;
return yield this.zipSource.getMetadata();
});
}
_open() {
return __awaiter(this, void 0, void 0, function* () {
yield this.ready;
yield Promise.all([this.rawSource.open(), yield this.zipSource.open()]);
});
}
_close() {
return __awaiter(this, void 0, void 0, function* () {
yield this.ready;
yield Promise.all([this.zipSource.close(), yield this.rawSource.close()]);
});
}
}
exports.BalenaS3Source = BalenaS3Source;
//# sourceMappingURL=balena-s3-source.js.map