unenv
Version:
`unenv` is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.
182 lines (181 loc) • 4.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.process = void 0;
const process = exports.process = {};
let cachedSetTimeout;
let cachedClearTimeout;
function defaultSetTimeout() {
throw new Error("setTimeout has not been defined");
}
function defaultClearTimeout() {
throw new Error("clearTimeout has not been defined");
}
(function () {
try {
cachedSetTimeout = typeof setTimeout === "function" ? setTimeout : defaultSetTimeout;
} catch {
cachedSetTimeout = defaultSetTimeout;
}
try {
cachedClearTimeout = typeof clearTimeout === "function" ? clearTimeout : defaultClearTimeout;
} catch {
cachedClearTimeout = defaultClearTimeout;
}
})();
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
return setTimeout(fun, 0);
}
if ((cachedSetTimeout === defaultSetTimeout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
return cachedSetTimeout(fun, 0);
} catch {
try {
return cachedSetTimeout.call(null, fun, 0);
} catch {
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
return clearTimeout(marker);
}
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
return cachedClearTimeout(marker);
} catch {
try {
return cachedClearTimeout.call(null, marker);
} catch {
return cachedClearTimeout.call(this, marker);
}
}
}
let queue = [];
let draining = false;
let currentQueue;
let queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length > 0) {
queue = [...currentQueue, ...queue];
} else {
queueIndex = -1;
}
if (queue.length > 0) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
const timeout = runTimeout(cleanUpNextTick);
draining = true;
let len = queue.length;
while (len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
const args = Array.from({
length: arguments.length - 1
});
if (arguments.length > 1) {
for (let i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
};
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = "unenv";
const _envShim = /* @__PURE__ */Object.create(null);
const _processEnv = globalThis.process?.env;
const _getEnv = useShim => _processEnv || globalThis.__env__ || (useShim ? _envShim : globalThis);
process.env = new Proxy(_envShim, {
get(_, prop) {
const env = _getEnv();
return env[prop] ?? _envShim[prop];
},
has(_, prop) {
const env = _getEnv();
return prop in env || prop in _envShim;
},
set(_, prop, value) {
const env = _getEnv(true);
env[prop] = value;
return true;
},
deleteProperty(_, prop) {
const env = _getEnv(true);
delete env[prop];
},
ownKeys() {
const env = _getEnv();
return Object.keys(env);
}
});
process.argv = [];
process.version = "";
process.versions = {};
function noop() {
return process;
}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) {
return [];
};
process.binding = function (name) {
throw new Error("[unenv] process.binding is not supported");
};
let cwd = "/";
process.cwd = function () {
return cwd;
};
process.chdir = function (dir) {
cwd = dir;
};
process.umask = function () {
return 0;
};
;