@universe/util
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "@universe/util", | ||
"version": "1.2.0", | ||
"main": "dist/src/index.js", | ||
"version": "1.2.1", | ||
"main": "dist/cjs/src/index.js", | ||
"description": "Universe development utilities.", | ||
@@ -10,6 +10,14 @@ "author": "Adam Miller <adam@universe.app>", | ||
"repository":"https://github.com/universe/big-blue", | ||
"type": "commonjs", | ||
"exports": { | ||
".": { | ||
"types": "./dist/cjs/src/index.d.ts", | ||
"require": "./dist/cjs/src/index.js", | ||
"import": "./dist/mjs/src/index.mjs" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "rm -rf dist && tsc --b tsconfig.json", | ||
"build": "rm -rf dist && tsc --b tsconfig.json && tsc --b tsconfig.mjs.json && mv dist/mjs/src/index.js dist/mjs/src/index.mjs", | ||
"pretest": "yarn run build", | ||
"test": "mocha ./dist/test/*.js -gc", | ||
"test": "mocha ./dist/cjs/test/*.js -gc", | ||
"watch": "watch 'yarn run test' './src' './test' --wait=3" | ||
@@ -16,0 +24,0 @@ }, |
@@ -0,5 +1,6 @@ | ||
/* eslint-disable max-len */ | ||
/* global MessageChannel */ | ||
import type { MessageChannel as IMessageChannel } from 'worker_threads'; | ||
const IS_BROWSER = typeof window === 'object'; | ||
const IS_BROWSER = typeof window === 'object' && window.document; | ||
@@ -63,6 +64,7 @@ // Generics type checking utils. | ||
/* eslint-disable-next-line @typescript-eslint/no-var-requires */ | ||
const crypto = IS_BROWSER ? window.crypto : require('crypto'); | ||
type NodeCrypto = typeof import("crypto"); | ||
let crypto = IS_BROWSER ? window.crypto : (globalThis.crypto as unknown as NodeCrypto); | ||
/* eslint-disable-next-line @typescript-eslint/no-var-requires */ | ||
const rand = IS_BROWSER ? window.crypto.getRandomValues.bind(window.crypto) : require('crypto').randomFillSync; | ||
const rand = IS_BROWSER ? (crypto as Crypto)?.getRandomValues.bind(window.crypto) : (crypto as NodeCrypto)?.randomFillSync; | ||
@@ -74,3 +76,3 @@ // https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript | ||
export const isUuid = (v: unknown): v is string => isString(v) && (v === NIL_UUID || uuidValidator.test(v)); | ||
export const uuid = crypto?.randomUUID?.bind(crypto) || ((): string => TMPL_UUID.replace(/[018]/g, c => (+c ^ rand(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16))); | ||
export const uuid: (() => string) = crypto?.randomUUID?.bind(crypto) || ((): string => TMPL_UUID.replace(/[018]/g, c => (+c ^ rand(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16))); | ||
@@ -221,5 +223,5 @@ // Encodes a uuid4 into Base85 for size savings. | ||
/* eslint-disable-next-line @typescript-eslint/no-var-requires */ | ||
if (!IS_BROWSER) { return require('crypto').createHash('sha256').update(message, 'utf8').digest('hex'); } | ||
if (!IS_BROWSER) { return (crypto as NodeCrypto).createHash('sha256').update(message, 'utf8').digest('hex'); } | ||
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array | ||
const hashBuffer = await window.crypto.subtle.digest('SHA-256', msgUint8); // hash the message | ||
const hashBuffer = await (crypto as Crypto).subtle.digest('SHA-256', msgUint8); // hash the message | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array | ||
@@ -255,4 +257,7 @@ const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string | ||
export async function lineCount(filePath: string): Promise<number> { | ||
if (IS_BROWSER) { | ||
throw new Error('[@universe/util] The lineCount function can only be used in a Node.js environment.') | ||
} | ||
/* eslint-disable-next-line */ | ||
const fs = require('fs'); | ||
const fs = await import('fs'); | ||
let lineCount = 0; | ||
@@ -361,3 +366,3 @@ return new Promise<number>((resolve) => { | ||
/* eslint-disable-next-line @typescript-eslint/no-var-requires */ | ||
if (!IS_BROWSER) { return require('crypto').createHash('md5').update(s, 'binary').digest('hex'); } | ||
if (!IS_BROWSER) { return (crypto as NodeCrypto).createHash('md5').update(s, 'binary').digest('hex'); } | ||
const n = s.length; | ||
@@ -416,3 +421,3 @@ const state = [ 1732584193, -271733879, -1732584194, 271733878 ]; | ||
/* eslint-disable-next-line @typescript-eslint/no-var-requires */ | ||
messageChannel = new (require('worker_threads').MessageChannel as typeof IMessageChannel)() as IMessageChannel & { mid: number; }; | ||
messageChannel = new ((await import('worker_threads')).MessageChannel as typeof IMessageChannel)() as IMessageChannel & { mid: number; }; | ||
messageChannel.mid = 0; | ||
@@ -419,0 +424,0 @@ messageChannel.port2.on('message', resolveClone); |
@@ -5,3 +5,4 @@ { | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
"module": "esnext", | ||
"outDir": "dist/cjs" | ||
}, | ||
@@ -8,0 +9,0 @@ "lib": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
621043
16
2289
1