From fe34c934ba1914f03151d75590699e319e87c9f3 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 22 Jul 2021 07:49:40 -0700 Subject: [PATCH] deps: move from byte-size to pretty-bytes In its latest release, byte-size dropped support for node versions lower than 14. The cli currently supports node 10 and up. The pretty-bytes module supports (via its github actions testing workflow) node 6 and up. --- lib/utils/tar.js | 10 +- lib/view.js | 7 +- node_modules/byte-size/LICENSE | 21 ---- node_modules/byte-size/dist/index.js | 123 ------------------------ node_modules/byte-size/index.mjs | 115 ---------------------- node_modules/byte-size/package.json | 55 ----------- node_modules/pretty-bytes/index.d.ts | 127 +++++++++++++++++++++++++ node_modules/pretty-bytes/index.js | 118 +++++++++++++++++++++++ node_modules/pretty-bytes/license | 9 ++ node_modules/pretty-bytes/package.json | 44 +++++++++ package-lock.json | 32 ++++--- package.json | 2 +- test/lib/utils/tar.js | 4 + 13 files changed, 329 insertions(+), 338 deletions(-) delete mode 100644 node_modules/byte-size/LICENSE delete mode 100644 node_modules/byte-size/dist/index.js delete mode 100644 node_modules/byte-size/index.mjs delete mode 100644 node_modules/byte-size/package.json create mode 100644 node_modules/pretty-bytes/index.d.ts create mode 100644 node_modules/pretty-bytes/index.js create mode 100644 node_modules/pretty-bytes/license create mode 100644 node_modules/pretty-bytes/package.json diff --git a/lib/utils/tar.js b/lib/utils/tar.js index 9e7c3329530ee..3037f42d08feb 100644 --- a/lib/utils/tar.js +++ b/lib/utils/tar.js @@ -1,7 +1,7 @@ const tar = require('tar') const ssri = require('ssri') const npmlog = require('npmlog') -const byteSize = require('byte-size') +const prettyBytes = require('pretty-bytes') const columnify = require('columnify') const logTar = (tarball, opts = {}) => { @@ -11,9 +11,9 @@ const logTar = (tarball, opts = {}) => { log.notice('=== Tarball Contents ===') if (tarball.files.length) { log.notice('', columnify(tarball.files.map((f) => { - const bytes = byteSize(f.size) + const bytes = prettyBytes(f.size) return (/^node_modules\//.test(f.path)) ? null - : { path: f.path, size: `${bytes.value}${bytes.unit}` } + : { path: f.path, size: `${bytes.replace(' ', '')}` } }).filter(f => f), { include: ['size', 'path'], showHeaders: false, @@ -28,8 +28,8 @@ const logTar = (tarball, opts = {}) => { { name: 'name:', value: tarball.name }, { name: 'version:', value: tarball.version }, tarball.filename && { name: 'filename:', value: tarball.filename }, - { name: 'package size:', value: byteSize(tarball.size) }, - { name: 'unpacked size:', value: byteSize(tarball.unpackedSize) }, + { name: 'package size:', value: prettyBytes(tarball.size) }, + { name: 'unpacked size:', value: prettyBytes(tarball.unpackedSize) }, { name: 'shasum:', value: tarball.shasum }, { name: 'integrity:', diff --git a/lib/view.js b/lib/view.js index 47e631f5565c0..f7688c71399f7 100644 --- a/lib/view.js +++ b/lib/view.js @@ -1,6 +1,5 @@ // npm view [pkg [pkg ...]] -const byteSize = require('byte-size') const color = require('ansicolors') const columns = require('cli-columns') const fs = require('fs') @@ -8,6 +7,7 @@ const jsonParse = require('json-parse-even-better-errors') const log = require('npmlog') const npa = require('npm-package-arg') const { resolve } = require('path') +const prettyBytes = require('pretty-bytes') const relativeDate = require('tiny-relative-date') const semver = require('semver') const style = require('ansistyles') @@ -315,7 +315,8 @@ class View extends BaseCommand { tags.push(`${style.bright(color.green(t))}: ${version}`) }) const unpackedSize = manifest.dist.unpackedSize && - byteSize(manifest.dist.unpackedSize) + prettyBytes(manifest.dist.unpackedSize) + const [unpackedValue, unpackedUnit] = unpackedSize && unpackedSize.split(' ') const licenseField = manifest.license || 'Proprietary' const info = { name: color.green(manifest.name), @@ -356,7 +357,7 @@ class View extends BaseCommand { manifest.dist.integrity && color.yellow(manifest.dist.integrity), fileCount: manifest.dist.fileCount && color.yellow(manifest.dist.fileCount), - unpackedSize: unpackedSize && color.yellow(unpackedSize.value) + ' ' + unpackedSize.unit, + unpackedSize: unpackedSize && color.yellow(unpackedValue) + ' ' + unpackedUnit, } if (info.license.toLowerCase().trim() === 'proprietary') info.license = style.bright(color.red(info.license)) diff --git a/node_modules/byte-size/LICENSE b/node_modules/byte-size/LICENSE deleted file mode 100644 index 5699dfbe51830..0000000000000 --- a/node_modules/byte-size/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2014-21 Lloyd Brookes <75pound@gmail.com> - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/node_modules/byte-size/dist/index.js b/node_modules/byte-size/dist/index.js deleted file mode 100644 index dd1debda59abd..0000000000000 --- a/node_modules/byte-size/dist/index.js +++ /dev/null @@ -1,123 +0,0 @@ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.byteSize = factory()); -}(this, (function () { 'use strict'; - - /** - * @module byte-size - */ - - let defaultOptions = {}; - const _options = new WeakMap(); - - class ByteSize { - constructor (bytes, options) { - options = Object.assign({ - units: 'metric', - precision: 1 - }, defaultOptions, options); - _options.set(this, options); - - const tables = { - metric: [ - { from: 0 , to: 1e3 , unit: 'B' , long: 'bytes' }, - { from: 1e3 , to: 1e6 , unit: 'kB', long: 'kilobytes' }, - { from: 1e6 , to: 1e9 , unit: 'MB', long: 'megabytes' }, - { from: 1e9 , to: 1e12, unit: 'GB', long: 'gigabytes' }, - { from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' }, - { from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' }, - { from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' }, - { from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' }, - { from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' }, - ], - metric_octet: [ - { from: 0 , to: 1e3 , unit: 'o' , long: 'octets' }, - { from: 1e3 , to: 1e6 , unit: 'ko', long: 'kilooctets' }, - { from: 1e6 , to: 1e9 , unit: 'Mo', long: 'megaoctets' }, - { from: 1e9 , to: 1e12, unit: 'Go', long: 'gigaoctets' }, - { from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' }, - { from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' }, - { from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' }, - { from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' }, - { from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' }, - ], - iec: [ - { from: 0 , to: Math.pow(1024, 1), unit: 'B' , long: 'bytes' }, - { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' }, - { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' }, - { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' }, - { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' }, - { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' }, - { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' }, - { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' }, - { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' }, - ], - iec_octet: [ - { from: 0 , to: Math.pow(1024, 1), unit: 'o' , long: 'octets' }, - { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' }, - { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' }, - { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' }, - { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' }, - { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' }, - { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' }, - { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' }, - { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' }, - ], - }; - Object.assign(tables, options.customUnits); - - const prefix = bytes < 0 ? '-' : ''; - bytes = Math.abs(bytes); - const table = tables[options.units]; - if (table) { - const units = table.find(u => bytes >= u.from && bytes < u.to); - if (units) { - const value = units.from === 0 - ? prefix + bytes - : prefix + (bytes / units.from).toFixed(options.precision); - this.value = value; - this.unit = units.unit; - this.long = units.long; - } else { - this.value = prefix + bytes; - this.unit = ''; - this.long = ''; - } - } else { - throw new Error(`Invalid units specified: ${options.units}`) - } - } - - toString () { - const options = _options.get(this); - return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}` - } - } - - /** - * Returns an object with the spec `{ value: string, unit: string, long: string }`. The returned object defines a `toString` method meaning it can be used in any string context. - * @param {number} - The bytes value to convert. - * @param [options] {object} - Optional config. - * @param [options.precision] {number} - Number of decimal places. Defaults to `1`. - * @param [options.units] {string} - Specify `'metric'`, `'iec'`, `'metric_octet'`, `'iec_octet'` or the name of a property from the custom units table in `options.customUnits`. Defaults to `metric`. - * @param [options.customUnits] {object} - An object containing one or more custom unit lookup tables. - * @param [options.toStringFn] {function} - A `toString` function to override the default. - * @returns {object} - * @alias module:byte-size - */ - function byteSize (bytes, options) { - return new ByteSize(bytes, options) - } - - /** - * Set the default `byteSize` options for the duration of the process. - * @param options {object} - A `byteSize` options object. - */ - byteSize.defaultOptions = function (options) { - defaultOptions = options; - }; - - return byteSize; - -}))); diff --git a/node_modules/byte-size/index.mjs b/node_modules/byte-size/index.mjs deleted file mode 100644 index bd6548c686aa5..0000000000000 --- a/node_modules/byte-size/index.mjs +++ /dev/null @@ -1,115 +0,0 @@ -/** - * @module byte-size - */ - -let defaultOptions = {} -const _options = new WeakMap() - -class ByteSize { - constructor (bytes, options) { - options = Object.assign({ - units: 'metric', - precision: 1 - }, defaultOptions, options) - _options.set(this, options) - - const tables = { - metric: [ - { from: 0 , to: 1e3 , unit: 'B' , long: 'bytes' }, - { from: 1e3 , to: 1e6 , unit: 'kB', long: 'kilobytes' }, - { from: 1e6 , to: 1e9 , unit: 'MB', long: 'megabytes' }, - { from: 1e9 , to: 1e12, unit: 'GB', long: 'gigabytes' }, - { from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' }, - { from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' }, - { from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' }, - { from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' }, - { from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' }, - ], - metric_octet: [ - { from: 0 , to: 1e3 , unit: 'o' , long: 'octets' }, - { from: 1e3 , to: 1e6 , unit: 'ko', long: 'kilooctets' }, - { from: 1e6 , to: 1e9 , unit: 'Mo', long: 'megaoctets' }, - { from: 1e9 , to: 1e12, unit: 'Go', long: 'gigaoctets' }, - { from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' }, - { from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' }, - { from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' }, - { from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' }, - { from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' }, - ], - iec: [ - { from: 0 , to: Math.pow(1024, 1), unit: 'B' , long: 'bytes' }, - { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' }, - { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' }, - { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' }, - { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' }, - { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' }, - { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' }, - { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' }, - { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' }, - ], - iec_octet: [ - { from: 0 , to: Math.pow(1024, 1), unit: 'o' , long: 'octets' }, - { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' }, - { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' }, - { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' }, - { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' }, - { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' }, - { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' }, - { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' }, - { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' }, - ], - } - Object.assign(tables, options.customUnits) - - const prefix = bytes < 0 ? '-' : '' - bytes = Math.abs(bytes) - const table = tables[options.units] - if (table) { - const units = table.find(u => bytes >= u.from && bytes < u.to) - if (units) { - const value = units.from === 0 - ? prefix + bytes - : prefix + (bytes / units.from).toFixed(options.precision) - this.value = value - this.unit = units.unit - this.long = units.long - } else { - this.value = prefix + bytes - this.unit = '' - this.long = '' - } - } else { - throw new Error(`Invalid units specified: ${options.units}`) - } - } - - toString () { - const options = _options.get(this) - return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}` - } -} - -/** - * Returns an object with the spec `{ value: string, unit: string, long: string }`. The returned object defines a `toString` method meaning it can be used in any string context. - * @param {number} - The bytes value to convert. - * @param [options] {object} - Optional config. - * @param [options.precision] {number} - Number of decimal places. Defaults to `1`. - * @param [options.units] {string} - Specify `'metric'`, `'iec'`, `'metric_octet'`, `'iec_octet'` or the name of a property from the custom units table in `options.customUnits`. Defaults to `metric`. - * @param [options.customUnits] {object} - An object containing one or more custom unit lookup tables. - * @param [options.toStringFn] {function} - A `toString` function to override the default. - * @returns {object} - * @alias module:byte-size - */ -function byteSize (bytes, options) { - return new ByteSize(bytes, options) -} - -/** - * Set the default `byteSize` options for the duration of the process. - * @param options {object} - A `byteSize` options object. - */ -byteSize.defaultOptions = function (options) { - defaultOptions = options -} - -export default byteSize diff --git a/node_modules/byte-size/package.json b/node_modules/byte-size/package.json deleted file mode 100644 index b5f454592da10..0000000000000 --- a/node_modules/byte-size/package.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "name": "byte-size", - "author": "Lloyd Brookes <75pound@gmail.com>", - "contributors": [ - { - "name": "Raul Perez", - "email": "repejota@gmail.com", - "url": "http://repejota.com" - } - ], - "version": "7.0.1", - "main": "dist/index.js", - "license": "MIT", - "engines": { - "node": ">=10" - }, - "description": "Convert a bytes or octets value (e.g. 34565346) to a human-readable string ('34.6 MB'). Choose between metric or IEC units.", - "repository": "https://github.com/75lb/byte-size", - "files": [ - "index.mjs", - "dist/index.js" - ], - "keywords": [ - "convert", - "bytes", - "octet", - "size", - "human", - "readable", - "metric", - "IEC" - ], - "scripts": { - "test": "npm run dist && npm run test:esm && npm run test:web", - "test:esm": "esm-runner test.mjs", - "test:web": "web-runner test.mjs", - "docs": "jsdoc2md -t README.hbs dist/index.js > README.md", - "cover": "c8 npm test && c8 report --reporter=text-lcov | coveralls", - "dist": "rollup -f umd -n byteSize -o dist/index.js index.mjs" - }, - "devDependencies": { - "@test-runner/web": "^0.3.5", - "coveralls": "^3.1.0", - "esm-runner": "^0.3.4", - "isomorphic-assert": "^0.1.1", - "jsdoc-to-markdown": "^7.0.0", - "rollup": "^2.40.0", - "test-object-model": "^0.6.1" - }, - "standard": { - "ignore": [ - "dist" - ] - } -} diff --git a/node_modules/pretty-bytes/index.d.ts b/node_modules/pretty-bytes/index.d.ts new file mode 100644 index 0000000000000..c617e9fd45e28 --- /dev/null +++ b/node_modules/pretty-bytes/index.d.ts @@ -0,0 +1,127 @@ +declare namespace prettyBytes { + interface Options { + /** + Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment. + + @default false + */ + readonly signed?: boolean; + + /** + - If `false`: Output won't be localized. + - If `true`: Localize the output using the system/browser locale. + - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …) + - If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …) + + __Note:__ Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime. + + @default false + */ + readonly locale?: boolean | string | readonly string[]; + + /** + Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate). + + @default false + + @example + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1337, {bits: true}); + //=> '1.34 kbit' + ``` + */ + readonly bits?: boolean; + + /** + Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes. + + @default false + + @example + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1000, {binary: true}); + //=> '1000 bit' + + prettyBytes(1024, {binary: true}); + //=> '1 kiB' + ``` + */ + readonly binary?: boolean; + + /** + The minimum number of fraction digits to display. + + If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. + + @default undefined + + ``` + import prettyBytes = require('pretty-bytes'); + + // Show the number with at least 3 fractional digits + prettyBytes(1900, {minimumFractionDigits: 3}); + //=> '1.900 kB' + + prettyBytes(1900); + //=> '1.9 kB' + ``` + */ + readonly minimumFractionDigits?: number; + + + /** + The maximum number of fraction digits to display. + + If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. + + @default undefined + + ``` + import prettyBytes = require('pretty-bytes'); + + // Show the number with at most 1 fractional digit + prettyBytes(1920, {maximumFractionDigits: 1}); + //=> '1.9 kB' + + prettyBytes(1920); + //=> '1.92 kB' + ``` + */ + readonly maximumFractionDigits?: number; + } +} + +/** +Convert bytes to a human readable string: `1337` → `1.34 kB`. + +@param number - The number to format. + +@example +``` +import prettyBytes = require('pretty-bytes'); + +prettyBytes(1337); +//=> '1.34 kB' + +prettyBytes(100); +//=> '100 B' + +// Display file size differences +prettyBytes(42, {signed: true}); +//=> '+42 B' + +// Localized output using German locale +prettyBytes(1337, {locale: 'de'}); +//=> '1,34 kB' +``` +*/ +declare function prettyBytes( + number: number, + options?: prettyBytes.Options +): string; + +export = prettyBytes; diff --git a/node_modules/pretty-bytes/index.js b/node_modules/pretty-bytes/index.js new file mode 100644 index 0000000000000..661c4650a661a --- /dev/null +++ b/node_modules/pretty-bytes/index.js @@ -0,0 +1,118 @@ +'use strict'; + +const BYTE_UNITS = [ + 'B', + 'kB', + 'MB', + 'GB', + 'TB', + 'PB', + 'EB', + 'ZB', + 'YB' +]; + +const BIBYTE_UNITS = [ + 'B', + 'kiB', + 'MiB', + 'GiB', + 'TiB', + 'PiB', + 'EiB', + 'ZiB', + 'YiB' +]; + +const BIT_UNITS = [ + 'b', + 'kbit', + 'Mbit', + 'Gbit', + 'Tbit', + 'Pbit', + 'Ebit', + 'Zbit', + 'Ybit' +]; + +const BIBIT_UNITS = [ + 'b', + 'kibit', + 'Mibit', + 'Gibit', + 'Tibit', + 'Pibit', + 'Eibit', + 'Zibit', + 'Yibit' +]; + +/* +Formats the given number using `Number#toLocaleString`. +- If locale is a string, the value is expected to be a locale-key (for example: `de`). +- If locale is true, the system default locale is used for translation. +- If no value for locale is specified, the number is returned unmodified. +*/ +const toLocaleString = (number, locale, options) => { + let result = number; + if (typeof locale === 'string' || Array.isArray(locale)) { + result = number.toLocaleString(locale, options); + } else if (locale === true || options !== undefined) { + result = number.toLocaleString(undefined, options); + } + + return result; +}; + +module.exports = (number, options) => { + if (!Number.isFinite(number)) { + throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); + } + + options = Object.assign({bits: false, binary: false}, options); + + const UNITS = options.bits ? + (options.binary ? BIBIT_UNITS : BIT_UNITS) : + (options.binary ? BIBYTE_UNITS : BYTE_UNITS); + + if (options.signed && number === 0) { + return ` 0 ${UNITS[0]}`; + } + + const isNegative = number < 0; + const prefix = isNegative ? '-' : (options.signed ? '+' : ''); + + if (isNegative) { + number = -number; + } + + let localeOptions; + + if (options.minimumFractionDigits !== undefined) { + localeOptions = {minimumFractionDigits: options.minimumFractionDigits}; + } + + if (options.maximumFractionDigits !== undefined) { + localeOptions = Object.assign({maximumFractionDigits: options.maximumFractionDigits}, localeOptions); + } + + if (number < 1) { + const numberString = toLocaleString(number, options.locale, localeOptions); + return prefix + numberString + ' ' + UNITS[0]; + } + + const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1); + // eslint-disable-next-line unicorn/prefer-exponentiation-operator + number /= Math.pow(options.binary ? 1024 : 1000, exponent); + + if (!localeOptions) { + number = number.toPrecision(3); + } + + const numberString = toLocaleString(Number(number), options.locale, localeOptions); + + const unit = UNITS[exponent]; + + return prefix + numberString + ' ' + unit; +}; diff --git a/node_modules/pretty-bytes/license b/node_modules/pretty-bytes/license new file mode 100644 index 0000000000000..fa7ceba3eb4a9 --- /dev/null +++ b/node_modules/pretty-bytes/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus (https://sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/pretty-bytes/package.json b/node_modules/pretty-bytes/package.json new file mode 100644 index 0000000000000..fb090d430f01d --- /dev/null +++ b/node_modules/pretty-bytes/package.json @@ -0,0 +1,44 @@ +{ + "name": "pretty-bytes", + "version": "5.6.0", + "description": "Convert bytes to a human readable string: 1337 → 1.34 kB", + "license": "MIT", + "repository": "sindresorhus/pretty-bytes", + "funding": "https://github.com/sponsors/sindresorhus", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "https://sindresorhus.com" + }, + "engines": { + "node": ">=6" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "pretty", + "bytes", + "byte", + "filesize", + "size", + "file", + "human", + "humanized", + "readable", + "si", + "data", + "locale", + "localization", + "localized" + ], + "devDependencies": { + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" + } +} diff --git a/package-lock.json b/package-lock.json index 7fe60ea862cb9..2f637f2084022 100644 --- a/package-lock.json +++ b/package-lock.json @@ -92,7 +92,6 @@ "ansicolors": "~0.3.2", "ansistyles": "~0.1.3", "archy": "~1.0.0", - "byte-size": "^7.0.1", "cacache": "^15.2.0", "chalk": "^4.1.0", "chownr": "^2.0.0", @@ -136,6 +135,7 @@ "opener": "^1.5.2", "pacote": "^11.3.5", "parse-conflict-json": "^1.1.1", + "pretty-bytes": "^5.6.0", "qrcode-terminal": "^0.12.0", "read": "~1.0.7", "read-package-json": "^3.0.1", @@ -1527,15 +1527,6 @@ "integrity": "sha1-y5T662HIaWRR2zZTThQi+U8K7og=", "inBundle": true }, - "node_modules/byte-size": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-7.0.1.tgz", - "integrity": "sha512-crQdqyCwhokxwV1UyDzLZanhkugAgft7vt0qbbdt60C6Zf3CAiGmtUCylbtYwrU6loOUw3euGrNtW1J651ot1A==", - "inBundle": true, - "engines": { - "node": ">=10" - } - }, "node_modules/cacache": { "version": "15.2.0", "resolved": "https://registry.npmjs.org/cacache/-/cacache-15.2.0.tgz", @@ -6142,6 +6133,17 @@ "node": ">= 0.8.0" } }, + "node_modules/pretty-bytes": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz", + "integrity": "sha512-FFw039TmrBqFK8ma/7OL3sDz/VytdtJr044/QUJtH0wK9lb9jLq9tJyIxUwtQJHwar2BqtiA4iCWSwo9JLkzFg==", + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/proc-log": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/proc-log/-/proc-log-1.0.0.tgz", @@ -11497,11 +11499,6 @@ "resolved": "https://registry.npmjs.org/builtins/-/builtins-1.0.3.tgz", "integrity": "sha1-y5T662HIaWRR2zZTThQi+U8K7og=" }, - "byte-size": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-7.0.1.tgz", - "integrity": "sha512-crQdqyCwhokxwV1UyDzLZanhkugAgft7vt0qbbdt60C6Zf3CAiGmtUCylbtYwrU6loOUw3euGrNtW1J651ot1A==" - }, "cacache": { "version": "15.2.0", "resolved": "https://registry.npmjs.org/cacache/-/cacache-15.2.0.tgz", @@ -14900,6 +14897,11 @@ "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==", "dev": true }, + "pretty-bytes": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz", + "integrity": "sha512-FFw039TmrBqFK8ma/7OL3sDz/VytdtJr044/QUJtH0wK9lb9jLq9tJyIxUwtQJHwar2BqtiA4iCWSwo9JLkzFg==" + }, "proc-log": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/proc-log/-/proc-log-1.0.0.tgz", diff --git a/package.json b/package.json index e91ff4bef974b..3eae00da84fb6 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "ansicolors": "~0.3.2", "ansistyles": "~0.1.3", "archy": "~1.0.0", - "byte-size": "^7.0.1", "cacache": "^15.2.0", "chalk": "^4.1.0", "chownr": "^2.0.0", @@ -106,6 +105,7 @@ "opener": "^1.5.2", "pacote": "^11.3.5", "parse-conflict-json": "^1.1.1", + "pretty-bytes": "^5.6.0", "qrcode-terminal": "^0.12.0", "read": "~1.0.7", "read-package-json": "^3.0.1", diff --git a/test/lib/utils/tar.js b/test/lib/utils/tar.js index 2662d47ace486..19d94916945db 100644 --- a/test/lib/utils/tar.js +++ b/test/lib/utils/tar.js @@ -57,6 +57,8 @@ t.test('should log tarball contents with unicode', async (t) => { logTar({ files: [], bundled: [], + size: 0, + unpackedSize: 0, integrity: '', }, { unicode: true }) t.end() @@ -75,6 +77,8 @@ t.test('should default to npmlog', async (t) => { logTar({ files: [], bundled: [], + size: 0, + unpackedSize: 0, integrity: '', }) t.end()