[6a3a178] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var tslib_1 = require("tslib");
|
---|
| 4 | var isUrlRe = /^(?:https?:)?\/\//i;
|
---|
| 5 | var url_1 = tslib_1.__importDefault(require("url"));
|
---|
| 6 | var request;
|
---|
| 7 | var abstract_file_manager_js_1 = tslib_1.__importDefault(require("../less/environment/abstract-file-manager.js"));
|
---|
| 8 | var logger_1 = tslib_1.__importDefault(require("../less/logger"));
|
---|
| 9 | var UrlFileManager = function () { };
|
---|
| 10 | UrlFileManager.prototype = Object.assign(new abstract_file_manager_js_1.default(), {
|
---|
| 11 | supports: function (filename, currentDirectory, options, environment) {
|
---|
| 12 | return isUrlRe.test(filename) || isUrlRe.test(currentDirectory);
|
---|
| 13 | },
|
---|
| 14 | loadFile: function (filename, currentDirectory, options, environment) {
|
---|
| 15 | return new Promise(function (fulfill, reject) {
|
---|
| 16 | if (request === undefined) {
|
---|
| 17 | try {
|
---|
| 18 | request = require('needle');
|
---|
| 19 | }
|
---|
| 20 | catch (e) {
|
---|
| 21 | request = null;
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 | if (!request) {
|
---|
| 25 | reject({ type: 'File', message: 'optional dependency \'native-request\' required to import over http(s)\n' });
|
---|
| 26 | return;
|
---|
| 27 | }
|
---|
| 28 | var urlStr = isUrlRe.test(filename) ? filename : url_1.default.resolve(currentDirectory, filename);
|
---|
| 29 | /** native-request currently has a bug */
|
---|
| 30 | var hackUrlStr = urlStr.indexOf('?') === -1 ? urlStr + '?' : urlStr;
|
---|
| 31 | request.get(hackUrlStr, { follow_max: 5 }, function (err, resp, body) {
|
---|
| 32 | if (err || resp && resp.statusCode >= 400) {
|
---|
| 33 | var message = resp && resp.statusCode === 404
|
---|
| 34 | ? "resource '" + urlStr + "' was not found\n"
|
---|
| 35 | : "resource '" + urlStr + "' gave this Error:\n " + (err || resp.statusMessage || resp.statusCode) + "\n";
|
---|
| 36 | reject({ type: 'File', message: message });
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 | if (resp.statusCode >= 300) {
|
---|
| 40 | reject({ type: 'File', message: "resource '" + urlStr + "' caused too many redirects" });
|
---|
| 41 | return;
|
---|
| 42 | }
|
---|
| 43 | body = body.toString('utf8');
|
---|
| 44 | if (!body) {
|
---|
| 45 | logger_1.default.warn("Warning: Empty body (HTTP " + resp.statusCode + ") returned by \"" + urlStr + "\"");
|
---|
| 46 | }
|
---|
| 47 | fulfill({ contents: body || '', filename: urlStr });
|
---|
| 48 | });
|
---|
| 49 | });
|
---|
| 50 | }
|
---|
| 51 | });
|
---|
| 52 | exports.default = UrlFileManager;
|
---|
| 53 | //# sourceMappingURL=url-file-manager.js.map |
---|