Files
bare-operating-system/kernel/lib/bare/bundles/bareUnpack.js
T
2026-04-04 01:20:57 -04:00

618 lines
23 KiB
JavaScript

var __bare_os_bundle_exports__ = (() => {
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
var __commonJS = (cb, mod) => function __require2() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// ../../node_modules/promaphore/index.js
var require_promaphore = __commonJS({
"../../node_modules/promaphore/index.js"(exports, module) {
var DONE = Promise.resolve(true);
var DESTROYED = Promise.resolve(false);
module.exports = class Semaphore {
constructor(limit = 1) {
this.limit = limit;
this.active = 0;
this.waiting = [];
this.destroyed = false;
this._onwait = (resolve) => {
this.waiting.push(resolve);
};
}
wait() {
if (this.destroyed === true) return DESTROYED;
if (this.active < this.limit && this.waiting.length === 0) {
this.active++;
return DONE;
}
return new Promise(this._onwait);
}
signal() {
if (this.destroyed === true) return;
this.active--;
while (this.active < this.limit && this.waiting.length > 0 && this.destroyed === false) {
this.active++;
this.waiting.shift()(true);
}
}
async flush() {
if (this.destroyed === true) return;
this.limit = 1;
await this.wait();
this.signal();
}
destroy() {
this.destroyed = true;
this.active = 0;
while (this.waiting.length) this.waiting.pop()(false);
}
};
}
});
// ../../node_modules/bare-bundle/lib/errors.js
var require_errors = __commonJS({
"../../node_modules/bare-bundle/lib/errors.js"(exports, module) {
module.exports = class BundleError extends Error {
constructor(msg, fn = BundleError, opts = {}) {
const { cause, code = fn.name } = opts;
super(`${code}: ${msg}`, { cause });
this.code = code;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, fn);
}
}
get name() {
return "BundleError";
}
static INVALID_BUNDLE_HEADER(msg, cause) {
return new BundleError(msg, BundleError.INVALID_BUNDLE_HEADER, { cause });
}
};
}
});
// ../../node_modules/bare-bundle/index.js
var require_bare_bundle = __commonJS({
"../../node_modules/bare-bundle/index.js"(exports, module) {
var errors = require_errors();
var kind = Symbol.for("bare.bundle.kind");
var MemoryFile = class _MemoryFile {
constructor(data, opts = {}) {
const { executable = false, mode = executable ? 493 : 420 } = opts;
this._data = typeof data === "string" ? Buffer.from(data) : data;
this._mode = mode;
}
size() {
return this._data.byteLength;
}
mode() {
return this._mode;
}
read() {
return this._data;
}
inspect() {
return {
__proto__: { constructor: _MemoryFile },
data: this._data,
mode: this._mode.toString(8)
};
}
[Symbol.for("bare.inspect")]() {
return this.inspect();
}
[Symbol.for("nodejs.util.inspect.custom")]() {
return this.inspect();
}
};
module.exports = exports = class Bundle2 {
static get [kind]() {
return 0;
}
static get version() {
return 0;
}
constructor(opts = {}) {
const { File = MemoryFile } = opts;
this._File = File;
this._id = null;
this._main = null;
this._imports = {};
this._resolutions = {};
this._addons = [];
this._assets = [];
this._files = /* @__PURE__ */ new Map();
}
get [kind]() {
return Bundle2[kind];
}
get version() {
return Bundle2.version;
}
get id() {
return this._id;
}
set id(value) {
if (typeof value !== "string" && value !== null) {
throw new TypeError(`ID must be a string or null. Received type ${typeof value} (${value})`);
}
this._id = value;
}
get main() {
return this._main;
}
set main(value) {
if (typeof value !== "string" && value !== null) {
throw new TypeError(`Main must be a string or null. Received type ${typeof value} (${value})`);
}
this._main = value;
}
get imports() {
return this._imports;
}
set imports(value) {
this._imports = cloneImportsMap(value);
}
get resolutions() {
return this._resolutions;
}
set resolutions(value) {
this._resolutions = cloneResolutionsMap(value);
}
get addons() {
return this._addons;
}
set addons(value) {
this._addons = cloneFilesList(value, "Addons");
}
get assets() {
return this._assets;
}
set assets(value) {
this._assets = cloneFilesList(value, "Assets");
}
get files() {
return Object.fromEntries(this._files.entries());
}
*[Symbol.iterator]() {
for (const [key, file] of this._files) {
yield [key, file.read(), file.mode()];
}
}
empty() {
return this._files.size === 0;
}
keys() {
return this._files.keys();
}
exists(key) {
return this._files.has(key);
}
size(key) {
const file = this._files.get(key) || null;
if (file === null) return 0;
return file.size();
}
mode(key) {
const file = this._files.get(key) || null;
if (file === null) return 0;
return file.mode();
}
read(key) {
const file = this._files.get(key) || null;
if (file === null) return null;
return file.read();
}
write(key, data, opts = {}) {
if (typeof key !== "string") {
throw new TypeError(`File path must be a string. Received type ${typeof key} (${key})`);
}
const { main = false, alias = null, imports = null, addon = false, asset = false } = opts;
this._files.set(key, new MemoryFile(data, opts));
if (main) this._main = key;
if (alias) this._imports[alias] = key;
if (imports) this._resolutions[key] = cloneImportsMap(imports);
if (addon) this._addons.push(key);
if (asset) this._assets.push(key);
return this;
}
mount(root, opts = {}) {
const bundle = new Bundle2();
bundle._File = this._File;
bundle._id = this._id;
if (this._main) bundle._main = mountSpecifier(this._main, root);
bundle._imports = transformImportsMap(this._imports, root, null, opts, mountSpecifier);
bundle._resolutions = transformResolutionsMap(this._resolutions, root, opts, mountSpecifier);
for (const [key, file] of this._files) {
bundle._files.set(mountSpecifier(key, root), file);
}
bundle._addons = transformFilesList(this._addons, root, mountSpecifier);
bundle._assets = transformFilesList(this._assets, root, mountSpecifier);
return bundle;
}
unmount(root, opts = {}) {
const bundle = new Bundle2();
bundle._File = this._File;
bundle._id = this._id;
if (this._main) bundle._main = unmountSpecifier(this._main, root);
bundle._imports = transformImportsMap(this._imports, root, null, opts, unmountSpecifier);
bundle._resolutions = transformResolutionsMap(this._resolutions, root, opts, unmountSpecifier);
for (const [key, file] of this._files) {
bundle._files.set(unmountSpecifier(key, root), file);
}
bundle._addons = transformFilesList(this._addons, root, unmountSpecifier);
bundle._assets = transformFilesList(this._assets, root, unmountSpecifier);
return bundle;
}
toBuffer(opts = {}) {
const { indent = 0, shared = false } = opts;
const header = {
version: Bundle2.version,
id: this._id,
main: this._main,
imports: cloneImportsMap(this._imports),
resolutions: cloneResolutionsMap(this._resolutions),
addons: cloneFilesList(this._addons, "Addons"),
assets: cloneFilesList(this._assets, "Assets"),
files: {}
};
const keys = [...this._files.keys()].sort();
let offset = 0;
for (const key of keys) {
const length2 = this.size(key);
header.files[key] = { offset, length: length2, mode: this.mode(key) };
offset += length2;
}
const json = Buffer.from(`
${JSON.stringify(header, null, indent)}
`);
const length = Buffer.from(json.byteLength.toString(10));
const total = length.byteLength + json.byteLength + offset;
const storage = shared ? new SharedArrayBuffer(total) : new ArrayBuffer(total);
const buffer = Buffer.from(storage);
offset = 0;
buffer.set(length, offset);
offset += length.byteLength;
buffer.set(json, offset);
offset += json.byteLength;
for (const key of keys) {
buffer.set(this.read(key), offset);
offset += this.size(key);
}
return buffer;
}
inspect() {
return {
__proto__: { constructor: Bundle2 },
version: this.version,
id: this.id,
main: this.main,
imports: this.imports,
resolutions: this.resolutions,
addons: this.addons,
assets: this.assets,
files: this.files
};
}
[Symbol.for("bare.inspect")]() {
return this.inspect();
}
[Symbol.for("nodejs.util.inspect.custom")]() {
return this.inspect();
}
};
var Bundle = exports;
exports.errors = errors;
exports.isBundle = function isBundle(value) {
if (value instanceof Bundle) return true;
return typeof value === "object" && value !== null && value[kind] === Bundle[kind];
};
exports.from = function from(value) {
if (typeof value === "string") return fromString(value);
if (Buffer.isBuffer(value)) return fromBuffer(value);
return value;
};
function fromString(string) {
return fromBuffer(Buffer.from(string));
}
function fromBuffer(buffer) {
if (buffer[0] === 35 && buffer[1] === 33) {
let end2 = 2;
while (buffer[end2] !== 10) end2++;
buffer = buffer.subarray(end2 + 1);
}
let end = 0;
while (isDecimal(buffer[end])) end++;
const len = parseInt(buffer.toString("utf8", 0, end), 10);
let header;
try {
header = JSON.parse(buffer.toString("utf8", end, end + len));
} catch (err) {
throw errors.INVALID_BUNDLE_HEADER("Invalid bundle header", err);
}
const bundle = new Bundle();
if (header.id) bundle.id = header.id;
if (header.main) bundle.main = header.main;
if (header.imports) bundle.imports = header.imports;
if (header.resolutions) bundle.resolutions = header.resolutions;
if (header.addons) bundle.addons = header.addons;
if (header.assets) bundle.assets = header.assets;
let offset = end + len;
for (const [file, info] of Object.entries(header.files)) {
bundle.write(file, buffer.subarray(offset, offset + info.length), {
mode: info.mode || 420
});
offset += info.length;
}
return bundle;
}
function isDecimal(c) {
return c >= 48 && c <= 57;
}
function compareKeys([a], [b]) {
return a > b ? 1 : a < b ? -1 : 0;
}
function cloneImportsMap(value) {
if (typeof value === "object" && value !== null) {
const imports = {};
for (const entry of Object.entries(value).sort(compareKeys)) {
imports[entry[0]] = cloneImportsMapEntry(entry[1]);
}
return imports;
}
throw new TypeError(`Imports map must be an object. Received type ${typeof value} (${value})`);
}
function cloneImportsMapEntry(value) {
if (typeof value === "string") return value;
if (typeof value === "object" && value !== null) {
const imports = {};
for (const entry of Object.entries(value)) {
imports[entry[0]] = cloneImportsMapEntry(entry[1]);
}
return imports;
}
throw new TypeError(
`Imports map entry must be a string or object. Received type ${typeof value} (${value})`
);
}
function cloneResolutionsMap(value) {
if (typeof value === "object" && value !== null) {
const resolutions = {};
for (const entry of Object.entries(value).sort(compareKeys)) {
resolutions[entry[0]] = cloneImportsMap(entry[1]);
}
return resolutions;
}
throw new TypeError(`Resolutions map must be an object. Received type ${typeof value} (${value})`);
}
function cloneFilesList(value, name) {
if (Array.isArray(value)) {
const files = [];
for (const entry of value) {
if (typeof entry !== "string") {
throw new TypeError(
`${name} entry must be a string. Received type ${typeof entry} (${entry})`
);
}
files.push(entry);
}
return files.sort();
}
throw new TypeError(`${name} list must be an array. Received type ${typeof value} (${value})`);
}
function transformImportsMap(value, root, conditionalRoot, opts, fn) {
const { conditions = {} } = opts;
const imports = {};
for (const entry of Object.entries(value)) {
const condition = entry[0];
imports[condition] = transformImportsMapEntry(
entry[1],
root,
conditionalRoot || conditions[condition],
opts,
fn
);
}
return imports;
}
function transformImportsMapEntry(value, root, conditionalRoot, opts, fn) {
const { conditions = {} } = opts;
if (typeof value === "string") {
return fn(value, conditionalRoot || conditions.default || root);
}
return transformImportsMap(value, root, conditionalRoot, opts, fn);
}
function transformResolutionsMap(value, root, opts, fn) {
const resolutions = {};
for (const entry of Object.entries(value)) {
resolutions[fn(entry[0], root)] = transformImportsMap(entry[1], root, null, opts, fn);
}
return resolutions;
}
function transformFilesList(value, root, fn) {
const files = [];
for (const entry of value) {
files.push(fn(entry, root));
}
return files;
}
function mountSpecifier(specifier, root) {
if (startsWithWindowsDriveLetter(specifier)) {
specifier = "/" + specifier;
}
if (specifier[0] === "/" || specifier[0] === "\\") {
specifier = "." + specifier;
}
if (specifier.startsWith("./") || specifier.startsWith(".\\")) {
return new URL(specifier, root).href;
}
return specifier;
}
function unmountSpecifier(specifier, root) {
specifier = new URL(specifier);
if (typeof root === "string") root = new URL(root);
if (specifier.protocol !== root.protocol || specifier.host !== root.host || specifier.port !== root.port) {
return specifier.href;
}
const specifierPath = splitPath(specifier.pathname);
const rootPath = splitPath(root.pathname);
while (specifierPath.length > 0 && rootPath[0] === specifierPath[0]) {
specifierPath.shift();
rootPath.shift();
}
rootPath.fill("..");
return "/" + rootPath.concat(specifierPath).join("/");
}
function splitPath(path) {
const parts = path.split("/");
if (!parts[0]) parts.shift();
if (!parts[parts.length - 1]) parts.pop();
return parts;
}
function isASCIIUpperAlpha(c) {
return c >= 65 && c <= 90;
}
function isASCIILowerAlpha(c) {
return c >= 97 && c <= 122;
}
function isASCIIAlpha(c) {
return isASCIIUpperAlpha(c) || isASCIILowerAlpha(c);
}
function isWindowsDriveLetter(input) {
return input.length >= 2 && isASCIIAlpha(input.charCodeAt(0)) && (input.charCodeAt(1) === 58 || input.charCodeAt(1) === 124);
}
function startsWithWindowsDriveLetter(input) {
return input.length >= 2 && isWindowsDriveLetter(input) && (input.length === 2 || input.charCodeAt(2) === 47 || input.charCodeAt(2) === 92 || input.charCodeAt(2) === 63 || input.charCodeAt(2) === 35);
}
}
});
// ../../node_modules/bare-unpack/index.js
var require_bare_unpack = __commonJS({
"../../node_modules/bare-unpack/index.js"(exports, module) {
var path = __require("path");
var Semaphore = require_promaphore();
var Bundle = require_bare_bundle();
module.exports = async function unpack(bundle, opts, writeFile) {
if (typeof opts === "function") {
writeFile = opts;
opts = {};
}
const { files = true, addons = files, assets = files, concurrency = 0 } = opts;
const semaphore = concurrency > 0 ? new Semaphore(concurrency) : null;
const unpack2 = /* @__PURE__ */ new Set();
if (files === true) {
for (const key of bundle.keys()) unpack2.add(key);
if (addons !== true) {
for (const key of bundle.addons) unpack2.delete(key);
}
if (assets !== true) {
for (const key of bundle.assets) unpack2.delete(key);
}
} else {
if (addons === true) {
for (const key of bundle.addons) unpack2.add(key);
}
if (assets === true) {
for (const key of bundle.assets) unpack2.add(key);
}
}
const repack = /* @__PURE__ */ new Set();
const rewrites = /* @__PURE__ */ new Map();
const promises = [];
for (const key of bundle.keys()) promises.push(process(key));
await Promise.all(promises);
const result = new Bundle();
if (files !== true) result.main = bundle.main;
result.imports = rewriteImportsMap(bundle.imports, rewrites);
if (addons !== true) result.addons = bundle.addons;
if (assets !== true) result.assets = bundle.assets;
for (const key of repack) {
result.write(key, bundle.read(key), {
mode: bundle.mode(key),
imports: rewriteImportsMap(bundle.resolutions[key], rewrites)
});
}
return result;
async function process(key) {
if (semaphore !== null) await semaphore.wait();
if (unpack2.has(key)) {
let target = String(await writeFile(key));
rewrites.set(key, target);
for (; ; ) {
key = key.substring(0, key.lastIndexOf("/"));
if (key === "" || key === "/" || key[key.length - 1] === "/" || key[key.length - 1] === ":") {
break;
}
target = path.dirname(target);
rewrites.set(key, target);
}
} else {
repack.add(key);
}
if (semaphore !== null) semaphore.signal();
}
};
function rewriteImportsMap(imports, rewrites) {
if (typeof imports !== "object" || imports === null) return null;
return transformImportsMap(imports, (value) => rewrites.get(value) || value);
}
function transformImportsMap(value, fn) {
const imports = {};
for (const entry of Object.entries(value)) {
const condition = entry[0];
imports[condition] = transformImportsMapEntry(entry[1], fn);
}
return imports;
}
function transformImportsMapEntry(value, fn) {
if (typeof value === "string") return fn(value);
return transformImportsMap(value, fn);
}
}
});
// ../../bare-lib-entry-bareUnpack.js
var bare_lib_entry_bareUnpack_exports = {};
__export(bare_lib_entry_bareUnpack_exports, {
default: () => bare_lib_entry_bareUnpack_default
});
var import_bare_unpack = __toESM(require_bare_unpack());
var bare_lib_entry_bareUnpack_default = import_bare_unpack.default;
return __toCommonJS(bare_lib_entry_bareUnpack_exports);
})();
;(function(){var g=globalThis;var s="__bare_os_stdlib__";g[s]=g[s]||{};var e=typeof __bare_os_bundle_exports__!=="undefined"?__bare_os_bundle_exports__:void 0;var v=e!=null&&typeof e==="object"&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e;g[s]["bareUnpack"]=v;})();