@testring/utils
Advanced tools
+51
| "use strict"; | ||
| var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
| function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ensureNewFile = exports.exists = exports.touchFile = exports.ensureDir = void 0; | ||
| const fs = require("fs"); | ||
| const { mkdir, promises } = fs; | ||
| const { open, access } = promises; | ||
| function ensureDir(savePath) { | ||
| return new Promise((resolve, reject) => { | ||
| mkdir(savePath, { recursive: true }, (err) => { | ||
| if (err && err.code !== 'EEXIST') { | ||
| reject(err); | ||
| } | ||
| else { | ||
| resolve(true); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| exports.ensureDir = ensureDir; | ||
| function touchFile(fName) { | ||
| return open(fName, 'a+').then((fHandle) => fHandle.close()); | ||
| } | ||
| exports.touchFile = touchFile; | ||
| function ensureNewFile(fName) { | ||
| return open(fName, 'ax') | ||
| .then((fHandle) => fHandle.close()) | ||
| .then(() => true) | ||
| .catch((e) => false); | ||
| } | ||
| exports.ensureNewFile = ensureNewFile; | ||
| function exists(path) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| try { | ||
| yield access(path); | ||
| return true; | ||
| } | ||
| catch (_a) { | ||
| return false; | ||
| } | ||
| }); | ||
| } | ||
| exports.exists = exists; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.restructureError = void 0; | ||
| function restructureError(error) { | ||
| if (error instanceof Error) { | ||
| return error; | ||
| } | ||
| const tmpError = new Error((error === null || error === void 0 ? void 0 : error.message) || error || 'unknown error'); | ||
| if (error === null || error === void 0 ? void 0 : error.stack) { | ||
| tmpError.stack = error.stack; | ||
| } | ||
| return tmpError; | ||
| } | ||
| exports.restructureError = restructureError; |
+38
| import * as fs from 'fs'; | ||
| const {mkdir, promises} = fs; | ||
| const {open, access} = promises; | ||
| function ensureDir(savePath: string) { | ||
| return new Promise((resolve, reject) => { | ||
| mkdir(savePath, {recursive: true}, (err) => { | ||
| if (err && err.code !== 'EEXIST') { | ||
| reject(err); | ||
| } else { | ||
| resolve(true); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| function touchFile(fName: string) { | ||
| return open(fName, 'a+').then((fHandle) => fHandle.close()); | ||
| } | ||
| function ensureNewFile(fName: string) { | ||
| return open(fName, 'ax') | ||
| .then((fHandle) => fHandle.close()) | ||
| .then(() => true) | ||
| .catch((e) => false); | ||
| } | ||
| async function exists(path: string) { | ||
| try { | ||
| await access(path); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
| export {ensureDir, touchFile, exists, ensureNewFile}; |
| export function restructureError(error) { | ||
| if (error instanceof Error) { | ||
| return error; | ||
| } | ||
| const tmpError = new Error(error?.message || error || 'unknown error'); | ||
| if (error?.stack) { | ||
| tmpError.stack = error.stack; | ||
| } | ||
| return tmpError; | ||
| } |
@@ -33,4 +33,7 @@ "use strict"; | ||
| server.listen(0, host, () => { | ||
| // @ts-ignore | ||
| const port = server.address().port; | ||
| const address = server.address(); | ||
| let port = 0; | ||
| if (address && typeof address === 'object') { | ||
| port = address === null || address === void 0 ? void 0 : address.port; | ||
| } | ||
| server.once('close', () => { | ||
@@ -50,3 +53,3 @@ resolve(port); | ||
| for (let i = 0, len = ports.length; i < len; i++) { | ||
| let port = ports[i]; | ||
| const port = ports[i]; | ||
| if (yield isAvailablePort(port, host)) { | ||
@@ -53,0 +56,0 @@ return port; |
+5
-0
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.restructureError = exports.throttle = exports.getHeapReport = exports.getMemoryReport = exports.generateUniqId = exports.MultiLock = exports.Stack = exports.Queue = exports.requirePlugin = exports.resolvePackage = exports.requirePackage = exports.getAvailableFollowingPort = exports.getRandomPort = exports.getAvailablePort = exports.isAvailablePort = exports.fs = void 0; | ||
| const fs = require("./fs"); | ||
| exports.fs = fs; | ||
| var find_available_ports_1 = require("./find-available-ports"); | ||
@@ -26,1 +29,3 @@ Object.defineProperty(exports, "isAvailablePort", { enumerable: true, get: function () { return find_available_ports_1.isAvailablePort; } }); | ||
| Object.defineProperty(exports, "throttle", { enumerable: true, get: function () { return throttle_1.throttle; } }); | ||
| var restructure_error_1 = require("./restructure-error"); | ||
| Object.defineProperty(exports, "restructureError", { enumerable: true, get: function () { return restructure_error_1.restructureError; } }); |
@@ -7,3 +7,3 @@ "use strict"; | ||
| * | ||
| * during init max total lock amount can be set defaulted to 1 | ||
| * during init, max total lock amount can be set defaulted to 0 (0 - unlimited) | ||
| * | ||
@@ -24,6 +24,6 @@ * on any given id lock can be acquired and released in the future | ||
| /** | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| acquire(id) { | ||
@@ -30,0 +30,0 @@ if (this.lockLimit !== 0 && this.lockLength >= this.lockLimit) { |
@@ -6,3 +6,4 @@ "use strict"; | ||
| const resolve = require("resolve"); | ||
| exports.isWebpack = () => typeof __webpack_require__ !== 'undefined'; | ||
| const isWebpack = () => typeof __webpack_require__ !== 'undefined'; | ||
| exports.isWebpack = isWebpack; | ||
| const requireById = (id) => { | ||
@@ -16,3 +17,3 @@ return module.require(`${id}`); | ||
| if (exports.isWebpack()) { | ||
| throw Error('Can\'t use dynamic imports with webpack.'); | ||
| throw Error("Can't use dynamic imports with webpack."); | ||
| } | ||
@@ -41,3 +42,3 @@ try { | ||
| if (exports.isWebpack()) { | ||
| throw Error('Can\'t use dynamic imports with webpack.'); | ||
| throw Error("Can't use dynamic imports with webpack."); | ||
| } | ||
@@ -44,0 +45,0 @@ const fileName = resolvePackage(modulePath, parentModule); |
@@ -5,6 +5,3 @@ "use strict"; | ||
| const package_require_1 = require("./package-require"); | ||
| const PREFIXES = [ | ||
| '@testring/plugin-', | ||
| 'testring-plugin-', | ||
| ]; | ||
| const PREFIXES = ['@testring/plugin-', 'testring-plugin-', '@testring/']; | ||
| function normalizeExport(module) { | ||
@@ -31,2 +28,3 @@ // filtering null and other falsy values | ||
| } | ||
| break; | ||
| } | ||
@@ -33,0 +31,0 @@ if (!resolvedPlugin) { |
+20
-0
@@ -32,2 +32,22 @@ "use strict"; | ||
| } | ||
| /** | ||
| * extract - filter items, return filtered, leave the rest | ||
| * | ||
| * @param {(T, number?)=>boolean} fn - function to filter elements for removal | ||
| * @returns - number of elements removed | ||
| */ | ||
| extract(fn) { | ||
| const result = []; | ||
| const rest = []; | ||
| this.array.forEach((item, index) => { | ||
| if (fn(item, index)) { | ||
| result.push(item); | ||
| } | ||
| else { | ||
| rest.push(item); | ||
| } | ||
| }); | ||
| this.array = rest; | ||
| return result; | ||
| } | ||
| getFirstElement(offset = 0) { | ||
@@ -34,0 +54,0 @@ const elementIndex = offset; |
+3
-2
@@ -5,3 +5,2 @@ "use strict"; | ||
| exports.throttle = void 0; | ||
| // eslint-disable-next-line space-before-function-paren | ||
| function throttle(func, limit) { | ||
@@ -11,3 +10,5 @@ let lastFunc; | ||
| return function () { | ||
| // eslint-disable-next-line @typescript-eslint/no-this-alias | ||
| const context = this; | ||
| // eslint-disable-next-line prefer-rest-params | ||
| const args = arguments; | ||
@@ -21,3 +22,3 @@ if (!lastRan) { | ||
| lastFunc = setTimeout(function () { | ||
| if ((Date.now() - lastRan) >= limit) { | ||
| if (Date.now() - lastRan >= limit) { | ||
| func.apply(context, args); | ||
@@ -24,0 +25,0 @@ lastRan = Date.now(); |
+7
-7
| { | ||
| "name": "@testring/utils", | ||
| "version": "0.5.36", | ||
| "version": "0.6.0", | ||
| "main": "./dist/index.js", | ||
@@ -13,11 +13,11 @@ "types": "./src/index.ts", | ||
| "dependencies": { | ||
| "@testring/types": "0.5.36", | ||
| "@types/bytes": "3.1.0", | ||
| "@testring/types": "0.6.0", | ||
| "@types/bytes": "3.1.1", | ||
| "@types/nanoid": "2.1.0", | ||
| "@types/node": "14.6.0", | ||
| "@types/resolve": "1.17.1", | ||
| "@types/node": "14.17.6", | ||
| "@types/resolve": "1.20.1", | ||
| "bytes": "3.1.0", | ||
| "nanoid": "3.1.12", | ||
| "resolve": "1.17.0" | ||
| "nanoid": "3.1.23", | ||
| "resolve": "1.20.0" | ||
| } | ||
| } |
| import * as net from 'net'; | ||
| export function isAvailablePort(currentPort: number, host: string): Promise<boolean> { | ||
| export function isAvailablePort( | ||
| currentPort: number, | ||
| host: string, | ||
| ): Promise<boolean> { | ||
| return new Promise((resolve) => { | ||
| const server = net.createServer(); | ||
| server.listen(currentPort, host,() => { | ||
| server.listen(currentPort, host, () => { | ||
| server.once('close', () => { | ||
@@ -21,6 +24,10 @@ resolve(true); | ||
| const server = net.createServer(); | ||
| server.listen(0, host,() => { | ||
| // @ts-ignore | ||
| const port = server.address().port; | ||
| server.listen(0, host, () => { | ||
| const address = server.address(); | ||
| let port = 0; | ||
| if (address && typeof address === 'object') { | ||
| port = address?.port; | ||
| } | ||
| server.once('close', () => { | ||
@@ -39,6 +46,6 @@ resolve(port); | ||
| ports: Array<number> = [], | ||
| host: string = 'localhost', | ||
| host = 'localhost', | ||
| ): Promise<number> { | ||
| for (let i = 0, len = ports.length; i < len; i++) { | ||
| let port = ports[i]; | ||
| const port = ports[i]; | ||
@@ -53,13 +60,12 @@ if (await isAvailablePort(port, host)) { | ||
| export async function getAvailableFollowingPort( | ||
| start: number, | ||
| host: string = 'localhost', | ||
| host = 'localhost', | ||
| skipPorts: Array<number> = [], | ||
| ): Promise<number> { | ||
| if (!skipPorts.includes(start) && await isAvailablePort(start, host)) { | ||
| if (!skipPorts.includes(start) && (await isAvailablePort(start, host))) { | ||
| return start; | ||
| } | ||
| return getAvailableFollowingPort(start+1, host, skipPorts); | ||
| return getAvailableFollowingPort(start + 1, host, skipPorts); | ||
| } |
@@ -1,2 +0,2 @@ | ||
| const { nanoid } = require('nanoid'); | ||
| const {nanoid} = require('nanoid'); | ||
@@ -3,0 +3,0 @@ export function generateUniqId(size?: number): string { |
+14
-16
@@ -0,1 +1,5 @@ | ||
| import * as fs from './fs'; | ||
| export {fs}; | ||
| export { | ||
@@ -8,22 +12,16 @@ isAvailablePort, | ||
| export { | ||
| requirePackage, | ||
| resolvePackage, | ||
| } from './package-require'; | ||
| export {requirePackage, resolvePackage} from './package-require'; | ||
| export { requirePlugin } from './plugin-require'; | ||
| export {requirePlugin} from './plugin-require'; | ||
| export { Queue } from './queue'; | ||
| export { Stack } from './stack'; | ||
| export { MultiLock } from './multi-lock'; | ||
| export {Queue} from './queue'; | ||
| export {Stack} from './stack'; | ||
| export {MultiLock} from './multi-lock'; | ||
| export { generateUniqId } from './generate-uniq-id'; | ||
| export {generateUniqId} from './generate-uniq-id'; | ||
| export { | ||
| getMemoryReport, | ||
| getHeapReport, | ||
| } from './memory-usage'; | ||
| export {getMemoryReport, getHeapReport} from './memory-usage'; | ||
| export { | ||
| throttle, | ||
| } from './throttle'; | ||
| export {throttle} from './throttle'; | ||
| export {restructureError} from './restructure-error'; |
+18
-19
@@ -1,28 +0,25 @@ | ||
| /** | ||
| * manages multi-count lock for multiple Ids - holds total count of locks & manages its change | ||
| * | ||
| * during init max total lock amount can be set defaulted to 1 | ||
| * | ||
| * | ||
| * during init, max total lock amount can be set defaulted to 0 (0 - unlimited) | ||
| * | ||
| * on any given id lock can be acquired and released in the future | ||
| * all locks can be cleared for specified id | ||
| * all locks can be cleared for specified id | ||
| */ | ||
| export class MultiLock { | ||
| private lockHash: Map<string, number> = new Map(); | ||
| private lockLength: number = 0; | ||
| private lockLength = 0; | ||
| /** | ||
| * | ||
| * | ||
| * @param lockLimit - max amount of locks for all ids | ||
| */ | ||
| constructor(public lockLimit: number = 0) { } | ||
| constructor(public lockLimit: number = 0) {} | ||
| /** | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| acquire(id: string): boolean { | ||
| if (this.lockLimit !== 0 && this.lockLength >= this.lockLimit) { | ||
@@ -36,5 +33,6 @@ return false; | ||
| } | ||
| /** | ||
| * releases one lock for given id | ||
| * | ||
| * releases one lock for given id | ||
| * | ||
| * @param {string} id - lockID | ||
@@ -58,3 +56,3 @@ */ | ||
| * unlocks all locks for given Id | ||
| * | ||
| * | ||
| * @param {string|void} id - lockID | ||
@@ -77,3 +75,3 @@ */ | ||
| * if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| * | ||
| * | ||
| * @param {string|void} id - lockID | ||
@@ -87,5 +85,6 @@ */ | ||
| } | ||
| /** | ||
| * return map if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| * | ||
| * | ||
| */ | ||
@@ -92,0 +91,0 @@ getIds() { |
@@ -13,9 +13,12 @@ import * as path from 'path'; | ||
| const requireResolveById = (id, options?: { paths?: string[]; }) => { | ||
| const requireResolveById = (id, options?: {paths?: string[]}) => { | ||
| return (module.require as NodeRequire).resolve(id, options); | ||
| }; | ||
| export function resolvePackage(modulePath: string, parentModule?: string): string { | ||
| export function resolvePackage( | ||
| modulePath: string, | ||
| parentModule?: string, | ||
| ): string { | ||
| if (isWebpack()) { | ||
| throw Error('Can\'t use dynamic imports with webpack.'); | ||
| throw Error("Can't use dynamic imports with webpack."); | ||
| } | ||
@@ -26,3 +29,6 @@ | ||
| const parentModuleDir = path.dirname(parentModule); | ||
| const relativeModulePath = path.resolve(parentModuleDir, modulePath); | ||
| const relativeModulePath = path.resolve( | ||
| parentModuleDir, | ||
| modulePath, | ||
| ); | ||
@@ -46,3 +52,3 @@ try { | ||
| if (isWebpack()) { | ||
| throw Error('Can\'t use dynamic imports with webpack.'); | ||
| throw Error("Can't use dynamic imports with webpack."); | ||
| } | ||
@@ -55,3 +61,5 @@ | ||
| } catch (exception) { | ||
| const error = new ReferenceError(`Error, while requiring '${modulePath}': ${exception.message}`); | ||
| const error = new ReferenceError( | ||
| `Error, while requiring '${modulePath}': ${exception.message}`, | ||
| ); | ||
@@ -58,0 +66,0 @@ error.stack = exception.stack; |
@@ -1,7 +0,4 @@ | ||
| import { requirePackage, resolvePackage } from './package-require'; | ||
| import {requirePackage, resolvePackage} from './package-require'; | ||
| const PREFIXES = [ | ||
| '@testring/plugin-', | ||
| 'testring-plugin-', | ||
| ]; | ||
| const PREFIXES = ['@testring/plugin-', 'testring-plugin-', '@testring/']; | ||
@@ -32,2 +29,3 @@ function normalizeExport(module) { | ||
| } | ||
| break; | ||
| } | ||
@@ -34,0 +32,0 @@ |
+23
-4
@@ -1,2 +0,2 @@ | ||
| import { IQueue } from '@testring/types'; | ||
| import {IQueue} from '@testring/types'; | ||
@@ -33,7 +33,27 @@ export class Queue<T> implements IQueue<T> { | ||
| const len = this.array.length; | ||
| this.array = this.array.filter((item, index)=>!fn(item, index)); | ||
| this.array = this.array.filter((item, index) => !fn(item, index)); | ||
| return len - this.array.length; | ||
| } | ||
| public getFirstElement(offset: number = 0): T | null { | ||
| /** | ||
| * extract - filter items, return filtered, leave the rest | ||
| * | ||
| * @param {(T, number?)=>boolean} fn - function to filter elements for removal | ||
| * @returns - number of elements removed | ||
| */ | ||
| public extract(fn: <T>(T, number?) => boolean): T[] { | ||
| const result: T[] = []; | ||
| const rest: T[] = []; | ||
| this.array.forEach((item, index) => { | ||
| if (fn(item, index)) { | ||
| result.push(item); | ||
| } else { | ||
| rest.push(item); | ||
| } | ||
| }); | ||
| this.array = rest; | ||
| return result; | ||
| } | ||
| public getFirstElement(offset = 0): T | null { | ||
| const elementIndex = offset; | ||
@@ -48,3 +68,2 @@ | ||
| public get length(): number { | ||
@@ -51,0 +70,0 @@ return this.array.length; |
+2
-2
@@ -1,2 +0,2 @@ | ||
| import { IStack } from '@testring/types'; | ||
| import {IStack} from '@testring/types'; | ||
@@ -26,3 +26,3 @@ export class Stack<T> implements IStack<T> { | ||
| public getLastElement(offset: number = 0): T | null { | ||
| public getLastElement(offset = 0): T | null { | ||
| const elementIndex = this.array.length - 1 - offset; | ||
@@ -29,0 +29,0 @@ |
+7
-4
@@ -1,6 +0,7 @@ | ||
| /* https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf */ | ||
| // eslint-disable-next-line space-before-function-paren | ||
| export function throttle<T extends (...args: any[]) => any>(func: T, limit: number): () => void { | ||
| export function throttle<T extends (...args: any[]) => any>( | ||
| func: T, | ||
| limit: number, | ||
| ): () => void { | ||
| let lastFunc; | ||
@@ -10,3 +11,5 @@ let lastRan; | ||
| return function () { | ||
| // eslint-disable-next-line @typescript-eslint/no-this-alias | ||
| const context = this; | ||
| // eslint-disable-next-line prefer-rest-params | ||
| const args = arguments; | ||
@@ -21,3 +24,3 @@ | ||
| lastFunc = setTimeout(function () { | ||
| if ((Date.now() - lastRan) >= limit) { | ||
| if (Date.now() - lastRan >= limit) { | ||
| func.apply(context, args); | ||
@@ -24,0 +27,0 @@ lastRan = Date.now(); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
32506
22.11%27
17.39%952
21.12%2
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated
Updated
Updated
Updated