@shockpkg/dir-projector
Version:
Package for creating Shockwave Director projectors
121 lines (106 loc) • 2.6 kB
JavaScript
import zlib from 'zlib';
import { launchers } from "./launchers.mjs";
/**
* Create return value once.
*
* @param create Create function.
* @returns Returned value.
*/
export 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.
*/
export 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.
*/
export 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.
*/
export 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.
*/
export 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.
*/
export 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.
*/
export async function launcher(id) {
const b64 = launchers()[id];
if (typeof b64 !== 'string') {
throw new Error(`Invalid launcher id: ${id}`);
}
return new Promise((resolve, reject) => {
zlib.inflateRaw(Buffer.from(b64, 'base64'), (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
//# sourceMappingURL=util.mjs.map