Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to format bytes as metric or IEC; affects [bundlejs bundlephobia CratesSize DockerSize GithubRepoSize GithubCodeSize GithubSize NpmUnpackedSize SpigetDownloadSize steam VisualStudioAppCenterReleasesSize whatpulse] #10547

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@shields_io/camp": "^18.1.2",
"@xmldom/xmldom": "0.9.2",
"badge-maker": "file:badge-maker",
"byte-size": "^9.0.0",
"bytes": "^3.1.2",
"camelcase": "^8.0.0",
"chalk": "^5.3.0",
Expand Down Expand Up @@ -57,7 +58,6 @@
"parse-link-header": "^2.0.0",
"path-to-regexp": "^6.3.0",
"pg": "^8.12.0",
"pretty-bytes": "^6.1.1",
"priorityqueuejs": "^2.0.0",
"prom-client": "^15.1.3",
"qs": "^6.13.0",
Expand Down
22 changes: 11 additions & 11 deletions services/bundlejs/bundlejs-package.service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import Joi from 'joi'
import { BaseJsonService, pathParam, queryParam } from '../index.js'
import { renderSizeBadge, unitsQueryParam, unitsOpenApiParam } from '../size.js'
import { nonNegativeInteger } from '../validators.js'

const defaultUnits = 'metric'

const schema = Joi.object({
size: Joi.object({
compressedSize: Joi.string().required(),
rawCompressedSize: nonNegativeInteger,
}).required(),
}).required()

const queryParamSchema = Joi.object({
exports: Joi.string(),
units: unitsQueryParam.default(defaultUnits),
}).required()

const esbuild =
Expand Down Expand Up @@ -47,6 +52,7 @@ export default class BundlejsPackage extends BaseJsonService {
name: 'exports',
example: 'isVal,val',
}),
unitsOpenApiParam(defaultUnits),
],
},
},
Expand All @@ -69,20 +75,14 @@ export default class BundlejsPackage extends BaseJsonService {
name: 'exports',
example: 'randEmail,randFullName',
}),
unitsOpenApiParam(defaultUnits),
],
},
},
}

static defaultBadgeData = { label: 'bundlejs', color: 'informational' }

static render({ size }) {
return {
label: 'minified size (gzip)',
message: size,
}
}

async fetch({ scope, packageName, exports }) {
const searchParams = {
q: `${scope ? `${scope}/` : ''}${packageName}`,
Expand All @@ -108,9 +108,9 @@ export default class BundlejsPackage extends BaseJsonService {
})
}

async handle({ scope, packageName }, { exports }) {
async handle({ scope, packageName }, { exports, units }) {
const json = await this.fetch({ scope, packageName, exports })
const size = json.size.compressedSize
return this.constructor.render({ size })
const size = json.size.rawCompressedSize
return renderSizeBadge(size, units, 'minified size (gzip)')
}
}
16 changes: 10 additions & 6 deletions services/bundlejs/bundlejs-package.tester.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { isFileSize } from '../test-validators.js'
import { isIecFileSize, isMetricFileSize } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('bundlejs/package (packageName)')
.get('/jquery.json')
.expectBadge({ label: 'minified size (gzip)', message: isFileSize })
.expectBadge({ label: 'minified size (gzip)', message: isMetricFileSize })

t.create('bundlejs/package (version)')
.get('/[email protected]')
.expectBadge({ label: 'minified size (gzip)', message: isFileSize })
.expectBadge({ label: 'minified size (gzip)', message: isMetricFileSize })

t.create('bundlejs/package (scoped)')
.get('/@cycle/rx-run.json')
.expectBadge({ label: 'minified size (gzip)', message: isFileSize })
.expectBadge({ label: 'minified size (gzip)', message: isMetricFileSize })

t.create('bundlejs/package (IEC bytes)')
.get('/jquery.json?units=IEC')
.expectBadge({ label: 'minified size (gzip)', message: isIecFileSize })

t.create('bundlejs/package (select exports)')
.get('/value-enhancer.json?exports=isVal,val')
.expectBadge({ label: 'minified size (gzip)', message: isFileSize })
.expectBadge({ label: 'minified size (gzip)', message: isMetricFileSize })

t.create('bundlejs/package (scoped version select exports)')
.get('/@ngneat/[email protected]?exports=randEmail,randFullName')
.expectBadge({ label: 'minified size (gzip)', message: isFileSize })
.expectBadge({ label: 'minified size (gzip)', message: isMetricFileSize })

t.create('bundlejs/package (not found)')
.get('/[email protected]')
Expand Down
90 changes: 49 additions & 41 deletions services/bundlephobia/bundlephobia.service.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import Joi from 'joi'
import prettyBytes from 'pretty-bytes'
import { renderSizeBadge, unitsQueryParam, unitsOpenApiParam } from '../size.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, pathParams } from '../index.js'
import { BaseJsonService, pathParam } from '../index.js'

const defaultUnits = 'IEC'

const schema = Joi.object({
size: nonNegativeInteger,
gzip: nonNegativeInteger,
}).required()

const queryParamSchema = Joi.object({
units: unitsQueryParam.default(defaultUnits),
}).required()

const description =
'[Bundlephobia](https://bundlephobia.com) lets you understand the size of a javascript package from NPM before it becomes a part of your bundle.'

Expand All @@ -17,91 +23,96 @@ export default class Bundlephobia extends BaseJsonService {
static route = {
base: 'bundlephobia',
pattern: ':format(min|minzip)/:scope(@[^/]+)?/:packageName/:version?',
queryParamSchema,
}

static openApi = {
'/bundlephobia/{format}/{packageName}': {
get: {
summary: 'npm bundle size',
description,
parameters: pathParams(
{
parameters: [
pathParam({
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
}),
pathParam({
name: 'packageName',
example: 'react',
},
),
}),
unitsOpenApiParam(defaultUnits),
],
},
},
'/bundlephobia/{format}/{scope}/{packageName}': {
get: {
summary: 'npm bundle size (scoped)',
description,
parameters: pathParams(
{
parameters: [
pathParam({
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
}),
pathParam({
name: 'scope',
example: '@cycle',
},
{
}),
pathParam({
name: 'packageName',
example: 'core',
},
),
}),
unitsOpenApiParam(defaultUnits),
],
},
},
'/bundlephobia/{format}/{packageName}/{version}': {
get: {
summary: 'npm bundle size (version)',
description,
parameters: pathParams(
{
parameters: [
pathParam({
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
}),
pathParam({
name: 'packageName',
example: 'react',
},
{
}),
pathParam({
name: 'version',
example: '15.0.0',
},
),
}),
unitsOpenApiParam(defaultUnits),
],
},
},
'/bundlephobia/{format}/{scope}/{packageName}/{version}': {
get: {
summary: 'npm bundle size (scoped version)',
description,
parameters: pathParams(
{
parameters: [
pathParam({
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
}),
pathParam({
name: 'scope',
example: '@cycle',
},
{
}),
pathParam({
name: 'packageName',
example: 'core',
},
{
}),
pathParam({
name: 'version',
example: '7.0.0',
},
),
}),
unitsOpenApiParam(defaultUnits),
],
},
},
}
Expand All @@ -110,12 +121,9 @@ export default class Bundlephobia extends BaseJsonService {

static defaultBadgeData = { label: 'bundlephobia', color: 'informational' }

static render({ format, size }) {
static render({ format, size, units }) {
const label = format === 'min' ? 'minified size' : 'minzipped size'
return {
label,
message: prettyBytes(size),
}
return renderSizeBadge(size, units, label)
}

async fetch({ scope, packageName, version }) {
Expand All @@ -133,9 +141,9 @@ export default class Bundlephobia extends BaseJsonService {
})
}

async handle({ format, scope, packageName, version }) {
async handle({ format, scope, packageName, version }, { units }) {
const json = await this.fetch({ scope, packageName, version })
const size = format === 'min' ? json.size : json.gzip
return this.constructor.render({ format, size })
return this.constructor.render({ format, size, units })
}
}
Loading