UNPKG

hackpro-sdk

Version:
141 lines 5.43 kB
"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"); // Always use the node adapter (even in a browser) // @ts-ignore const axiosNodeAdapter = require("axios/lib/adapters/http"); const path_1 = require("path"); const querystring_1 = require("querystring"); const url_1 = require("url"); const source_destination_1 = require("./source-destination"); axios_1.default.defaults.adapter = axiosNodeAdapter; class Http extends source_destination_1.SourceDestination { constructor(url) { super(); this.url = url; this.ready = this.getInfo(); } getInfo() { return __awaiter(this, void 0, void 0, function* () { try { const response = yield axios_1.default({ method: 'head', url: this.url }); this.size = parseInt(response.headers['content-length'], 10); this.acceptsRange = response.headers['accept-ranges'] === 'bytes'; } catch (error) { this.error = error; } }); } canRead() { return __awaiter(this, void 0, void 0, function* () { yield this.ready; if (this.error) { throw this.error; } return this.acceptsRange; }); } canCreateReadStream() { return __awaiter(this, void 0, void 0, function* () { return true; }); } _getMetadata() { return __awaiter(this, void 0, void 0, function* () { yield this.ready; if (this.error) { throw this.error; } let name; const pathname = url_1.parse(this.url).pathname; if (pathname !== undefined) { name = path_1.basename(querystring_1.unescape(pathname)); } return { size: this.size, name, }; }); } getRange(start = 0, end) { // start and end are inclusive let range = `bytes=${start}-`; if (end !== undefined) { range += end; } return range; } read(buffer, bufferOffset, length, sourceOffset) { return __awaiter(this, void 0, void 0, function* () { const response = yield axios_1.default({ method: 'get', url: this.url, responseType: 'arraybuffer', headers: { Range: this.getRange(sourceOffset, sourceOffset + length - 1), }, }); const bytesRead = response.data.length; // TODO: it would be nice to avoid copying here but it would require modifying axios response.data.copy(buffer, bufferOffset); return { bytesRead, buffer }; }); } createReadStream(emitProgress = false, start = 0, end) { return __awaiter(this, void 0, void 0, function* () { const response = yield axios_1.default({ method: 'get', url: this.url, headers: { Range: this.getRange(start, end), }, responseType: 'stream', }); if (emitProgress) { let bytes = 0; let lastTime = Date.now(); response.data.on('data', (chunk) => { const now = Date.now(); const length = chunk.length; bytes += length; const speed = length / ((now - lastTime) / 1000); lastTime = now; response.data.emit('progress', { position: bytes, bytes, speed, }); }); response.data.pause(); } return response.data; }); } } exports.Http = Http; //# sourceMappingURL=http.js.map