UNPKG

@sizium/core

Version:

Get the actual size of any local or remote package

152 lines (147 loc) 4.48 kB
import { P as PackageSuper } from './core.UrRfGGIn.mjs'; const isBrowser = typeof window !== "undefined" && typeof document !== "undefined"; const trimSlashes = (segment, index) => { if (index === 0) { let i = segment.length; while (i > 0 && segment[i - 1] === "/") i--; return segment.slice(0, i); } else { let start = 0, end = segment.length; while (start < end && segment[start] === "/") start++; while (end > start && segment[end - 1] === "/") end--; return segment.slice(start, end); } }; const joinPath = async (...paths) => { if (!isBrowser) { const { join } = await import('path'); return join(...paths); } return paths.filter(Boolean).map(trimSlashes).filter(Boolean).join("/"); }; const readFile = async (i) => { if (isBrowser) return fetch(i).then((r) => r.text()); return import('node:fs/promises').then(({ readFile: readFile2 }) => readFile2(i, "utf8")); }; const isDirectory = async (i) => { if (typeof window !== "undefined" && typeof document !== "undefined") { try { const res = await fetch(i, { method: "HEAD" }); const contentType = res.headers.get("Content-Type") || ""; const isLikelyDir = i.endsWith("/") || contentType.includes("text/html"); return res.ok && isLikelyDir; } catch { return false; } } else { const { stat } = await import('node:fs/promises'); try { const s = await stat(i); return s.isDirectory(); } catch { return false; } } }; const isAbsolute = async (i) => { try { if (isBrowser) return i.startsWith("/"); return (await import('node:path')).isAbsolute(i); } catch { return false; } }; const isPath = async (value) => { if (await isAbsolute(value) || /^(\.\/|\.\.\/|[A-Za-z]:\\|\/)/.test(value)) { if (/\s(?!\\)/.test(value) && !/\\\s/.test(value)) return false; try { const normalizedPath = await joinPath(value); return normalizedPath !== ""; } catch { return false; } } return false; }; const resolvePath = async (i) => { if (isBrowser) { return new URL(i, window.location.href).toString(); } else { const { resolve } = await import('node:path'); return resolve(i); } }; const isUrl = (value) => { try { new URL(value); return true; } catch { return false; } }; const isJsonString = (value) => { try { JSON.parse(value); return true; } catch { return false; } }; const isString = (value) => { return typeof value === "string" && value.trim().length > 0; }; const getInputType = async (value) => { if (!isString(value)) throw new Error("Input is not a valid string"); if (isUrl(value)) return "url"; if (await isPath(value)) return "path"; if (isJsonString(value)) return "json"; return "string"; }; class SiziumLocal extends PackageSuper { async #getPackage() { try { if (isUrl(this.input)) { const response = await fetch(this.input); if (!response.ok) throw new Error(`Failed to fetch package file from URL: ${response.statusText}`); const content = await response.text(); return JSON.parse(content); } else if (isJsonString(this.input)) { return JSON.parse(this.input); } else { let filePath = await resolvePath(this.input); if (await isDirectory(filePath)) filePath = await joinPath(filePath, "package.json"); const content = await readFile(filePath); return JSON.parse(content); } } catch (e) { throw new this.Error( this.ERROR_ID.GETTING_LOCAL_DATA, { msg: `Error processing package file: ${e instanceof Error ? e.message : ""}`, e } ); } } /** * Retrieves the package size information from a local environment. * It processes the package data from the input, resolving dependencies * and aggregating package data to return a comprehensive size response. * * @returns {Promise<SiziumResponse>} A promise that resolves with the package response data, * including size and dependency information. */ async get() { const packageData = await this.#getPackage(); const mainPackage = await this.getPkgData({ data: packageData, level: 0 }); const allPackages = await this.getPackagesData(mainPackage); return this.getMainPkgData(allPackages); } } export { SiziumLocal as S, getInputType as g };