|
| 1 | +"use strict"; |
| 2 | +/* |
| 3 | + * The MIT License (MIT) |
| 4 | + * |
| 5 | + * Copyright (c) 2015 Jeremy Whitlock |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 26 | + if (k2 === undefined) k2 = k; |
| 27 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 28 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 29 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 30 | + } |
| 31 | + Object.defineProperty(o, k2, desc); |
| 32 | +}) : (function(o, m, k, k2) { |
| 33 | + if (k2 === undefined) k2 = k; |
| 34 | + o[k2] = m[k]; |
| 35 | +})); |
| 36 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 37 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 38 | +}) : function(o, v) { |
| 39 | + o["default"] = v; |
| 40 | +}); |
| 41 | +var __exportStar = (this && this.__exportStar) || function(m, exports) { |
| 42 | + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); |
| 43 | +}; |
| 44 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 45 | + if (mod && mod.__esModule) return mod; |
| 46 | + var result = {}; |
| 47 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 48 | + __setModuleDefault(result, mod); |
| 49 | + return result; |
| 50 | +}; |
| 51 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 52 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 53 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 54 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 55 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 56 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 57 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 58 | + }); |
| 59 | +}; |
| 60 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 61 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 62 | +}; |
| 63 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 64 | +exports.load = void 0; |
| 65 | +__exportStar(require("./typedefs"), exports); |
| 66 | +const HttpLoader = __importStar(require("./loaders/http")); |
| 67 | +const FilerLoader = __importStar(require("./loaders/file")); |
| 68 | +const bluebird_1 = __importDefault(require("bluebird")); |
| 69 | +const lodash_1 = require("lodash"); |
| 70 | +const supportedLoaders = { |
| 71 | + file: FilerLoader, |
| 72 | + http: HttpLoader, |
| 73 | + https: HttpLoader, |
| 74 | +}; |
| 75 | +const defaultLoader = typeof window === 'object' ? supportedLoaders.http : supportedLoaders.file; |
| 76 | +function getScheme(location) { |
| 77 | + if (!(0, lodash_1.isUndefined)(location)) { |
| 78 | + location = !location.includes('://') ? '' : location.split('://')[0]; |
| 79 | + } |
| 80 | + return location; |
| 81 | +} |
| 82 | +/** |
| 83 | + * Utility that provides a single API for loading the content of a path/URL. |
| 84 | + * |
| 85 | + * @module path-loader |
| 86 | + */ |
| 87 | +function getLoader(location) { |
| 88 | + const scheme = getScheme(location); |
| 89 | + let loader = supportedLoaders[scheme]; |
| 90 | + if ((0, lodash_1.isUndefined)(loader)) { |
| 91 | + if ((0, lodash_1.isEmpty)(scheme)) { |
| 92 | + loader = defaultLoader; |
| 93 | + } |
| 94 | + else { |
| 95 | + throw new Error('Unsupported scheme: ' + scheme); |
| 96 | + } |
| 97 | + } |
| 98 | + return loader; |
| 99 | +} |
| 100 | +function validateArgs(location, opts = {}) { |
| 101 | + if (typeof location === 'undefined') { |
| 102 | + throw new TypeError('location is required'); |
| 103 | + } |
| 104 | + else if (typeof location !== 'string') { |
| 105 | + throw new TypeError('location must be a string'); |
| 106 | + } |
| 107 | + if (typeof opts !== 'object') { |
| 108 | + throw new TypeError('options must be an object'); |
| 109 | + } |
| 110 | + else if (typeof opts.processContent !== 'undefined' && |
| 111 | + typeof opts.processContent !== 'function') { |
| 112 | + throw new TypeError('options.processContent must be a function'); |
| 113 | + } |
| 114 | +} |
| 115 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 116 | +function load(location, options = {}) { |
| 117 | + return __awaiter(this, void 0, void 0, function* () { |
| 118 | + // Validate arguments |
| 119 | + validateArgs(location, options); |
| 120 | + // Load the document from the provided location and process it |
| 121 | + const loader = getLoader(location); |
| 122 | + const promisifiedLoader = bluebird_1.default.promisify(loader.load); |
| 123 | + const data = yield promisifiedLoader(location, options); |
| 124 | + if (!options.processContent) { |
| 125 | + return data; |
| 126 | + } |
| 127 | + // For consistency between file and http, always send an object with a 'text' property containing the raw |
| 128 | + // string value being processed. |
| 129 | + const res = (0, lodash_1.isPlainObject)(data) |
| 130 | + ? (data) |
| 131 | + : { text: data }; |
| 132 | + // Pass the path being loaded |
| 133 | + res.location = location; |
| 134 | + const processContent = bluebird_1.default.promisify(options.processContent); |
| 135 | + return processContent(res); |
| 136 | + }); |
| 137 | +} |
| 138 | +exports.load = load; |
0 commit comments