@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
156 lines (146 loc) • 4.56 kB
JavaScript
/*!
* @techmely/utils
* Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
* MIT Licensed
*/
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/merge-streams.ts
var merge_streams_exports = {};
__export(merge_streams_exports, {
mergeStreams: () => mergeStreams
});
module.exports = __toCommonJS(merge_streams_exports);
var import_node_stream = require("stream");
// src/invariant.ts
var prefix = "Invariant failed";
function invariant(condition, message) {
if (condition) {
return;
}
if (typeof message === "string" || typeof message === "function") {
const provided = typeof message === "function" ? message() : message;
const value = provided ? `${prefix}: ${provided}` : prefix;
throw new Error(value);
}
if (message)
throw message;
throw new Error(prefix);
}
// src/convert.ts
function toBoolean(val) {
return val ? val !== "false" : false;
}
// src/env.ts
var import_meta = {};
var _envShim = /* @__PURE__ */ Object.create(null);
var _getEnv = (useShim) => globalThis.process?.env || // Node.js/Bun
import_meta.env || // Vite env
globalThis.Deno?.env.toObject() || // Deno env
globalThis.__env__ || // Custom env
(useShim ? _envShim : globalThis);
var envs = 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) {
if (!prop) {
return false;
}
const env = _getEnv(true);
delete env[prop];
return true;
},
ownKeys() {
const env = _getEnv();
return Object.keys(env);
}
});
var nodeENV = typeof process !== "undefined" && process.env && process.env.NODE_ENV || "";
// src/process.ts
var _process = globalThis.process || /* @__PURE__ */ Object.create(null);
var processShims = {
versions: {}
};
var process2 = new Proxy(_process, {
get(target, prop) {
if (prop === "env") {
return envs;
}
if (prop in target) {
return target[prop];
}
if (prop in processShims) {
return processShims[prop];
}
}
});
var platform = _process.platform || "";
// src/is.ts
function isArray(val) {
return val && Array.isArray(val);
}
var isBrowser = typeof window !== "undefined";
var isAndroid = isBrowser ? /(android)/i.test(navigator.userAgent) : false;
var match = isBrowser ? window.matchMedia || window.msMatchMedia : void 0;
var isMobile = isBrowser ? match?.("(pointer:coarse)")?.matches : false;
var isCrawler = isBrowser && (!("onscroll" in window) || /(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent));
var isCI = toBoolean(envs.CI);
var isNodeTest = nodeENV === "test" || toBoolean(envs.TEST);
var isWindows = /^win/i.test(platform);
var isLinux = /^linux/i.test(platform);
var isMacOS = /^darwin/i.test(platform);
// src/merge-streams.ts
function mergeStreams(streams) {
invariant(isArray(streams), "Expect input an array streams");
const passThroughStream = new import_node_stream.PassThrough({ objectMode: true });
if (streams.length === 0) {
passThroughStream.end();
return passThroughStream;
}
let streamsCount = streams.length;
for (const stream of streams) {
invariant(!(typeof stream?.pipe === "function"), "Expect a stream, got ");
stream.pipe(passThroughStream, { end: false });
stream.on("end", () => {
streamsCount--;
if (streamsCount === 0) {
passThroughStream.end();
}
});
stream.on("error", (error) => {
passThroughStream.emit("error", error);
});
}
return passThroughStream;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
mergeStreams
});