UNPKG

@shockpkg/dir-projector

Version:

Package for creating Shockwave Director projectors

143 lines (118 loc) 3.05 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.once = once; exports.trimDotSlash = trimDotSlash; exports.pathRelativeBase = pathRelativeBase; exports.pathRelativeBaseMatch = pathRelativeBaseMatch; exports.trimExtension = trimExtension; exports.bufferToArrayBuffer = bufferToArrayBuffer; exports.launcher = launcher; var _zlib = _interopRequireDefault(require("zlib")); var _launchers = require("./launchers"); /** * Create return value once. * * @param create Create function. * @returns Returned value. */ function once(create) { let called = false; let value; return () => { if (!called) { value = create(); called = true; } return value; }; } /** * Trim dot flash from head of path. * * @param path Path string. * @returns Trimmed path. */ function trimDotSlash(path) { return path.replace(/^(\.\/)+/, ''); } /** * Find path relative from base, if base matches. * * @param path Path to match against. * @param start Search start. * @param nocase Match case-insensitive. * @returns Returns path, or null. */ function pathRelativeBase(path, start, nocase = false) { const p = trimDotSlash(nocase ? path.toLowerCase() : path); const s = trimDotSlash(nocase ? start.toLowerCase() : start); if (p === s) { return ''; } if (p.startsWith(`${s}/`)) { return path.substr(s.length + 1); } return null; } /** * Same as pathRelativeBase, but retuns true on a match, else false. * * @param path Path to match against. * @param start Search start. * @param nocase Match case-insensitive. * @returns Returns true on match, else false. */ function pathRelativeBaseMatch(path, start, nocase = false) { return pathRelativeBase(path, start, nocase) !== null; } /** * Trim a file extenion. * * @param path File path. * @param ext File extension. * @param nocase Match case-insensitive. * @returns Path without file extension. */ function trimExtension(path, ext, nocase = false) { const p = nocase ? path.toLowerCase() : path; const e = nocase ? ext.toLowerCase() : ext; return p.endsWith(e) ? path.substr(0, p.length - e.length) : path; } /** * Get ArrayBuffer from Buffer. * * @param buffer Buffer instance. * @returns ArrayBuffer copy. */ function bufferToArrayBuffer(buffer) { const { byteOffset, byteLength } = buffer; return buffer.buffer.slice(byteOffset, byteOffset + byteLength); } /** * Get launcher data for an ID. * * @param id Laucher ID. * @returns Launcher data. */ async function launcher(id) { const b64 = (0, _launchers.launchers)()[id]; if (typeof b64 !== 'string') { throw new Error(`Invalid launcher id: ${id}`); } return new Promise((resolve, reject) => { _zlib.default.inflateRaw(Buffer.from(b64, 'base64'), (err, data) => { if (err) { reject(err); return; } resolve(data); }); }); } //# sourceMappingURL=util.js.map