UNPKG

@stencila/jesta

Version:

Stencila plugin for executable documents using JavaScript

65 lines (64 loc) 2.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pull = exports.schema = void 0; const fs_1 = __importDefault(require("fs")); const errors_1 = require("./util/errors"); const http_1 = require("./util/http"); exports.schema = { title: 'pull', description: 'Pull file/s from a URL to the file system', required: ['input', 'output'], properties: { input: { description: 'The URL to fetch.', type: 'string', pattern: '^(https?|file|stdio|stdin|string)://.*', }, output: { description: 'The file path to write to', type: 'string', }, }, }; /** * Pull file/s from a URL to the file system * * For `http/s` URLs this is optimised to stream content directly * to the file system. For `file` URLs it is optimised to simply * copy the file. For all other URL types, falls back to combining * `read` and `write`. * * @param input The URL to fetch * @param output The file path to write to * @returns The file that was created (or written over) */ async function pull(input, output) { if (output.startsWith('file://')) { output = output.slice(7); } const match = /^([a-z]{2,6}):\/\//.exec(input); if (match) { const protocol = match[1]; switch (protocol) { case 'http': case 'https': { await http_1.download(input, output); return output; } case 'file': { await fs_1.default.promises.copyFile(input.slice(7), output); return output; } default: { const [content] = await this.read(input); return this.write(content, output); } } } throw new errors_1.CapabilityError(`pull from URL "${input.slice(0, 500)}"`); } exports.pull = pull; pull.schema = exports.schema;