Skip to content

Commit

Permalink
[Crates] Added crate size badge (badges#10421)
Browse files Browse the repository at this point in the history
* add some basic tests for size badge

* add crate size to crates fetch

* add crate size service

* change test to reflect base-10 counting instead

* fix downloads tests to work with new crate_size scheme addition

* move handling of unknown size to handle
  • Loading branch information
einstein8612 authored Aug 4, 2024
1 parent 1a09e9a commit bdf84f9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
2 changes: 2 additions & 0 deletions services/crates/crates-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { BaseJsonService, InvalidResponse } from '../index.js'

const versionSchema = Joi.object({
downloads: nonNegativeInteger,
// Crate size is not available for all versions.
crate_size: nonNegativeInteger.allow(null),
num: Joi.string().required(),
license: Joi.string().required().allow(null),
rust_version: Joi.string().allow(null),
Expand Down
7 changes: 6 additions & 1 deletion services/crates/crates-downloads.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ t.create('recent downloads (null)')
max_version: '0.2.71',
},
versions: [
{ downloads: 42, license: 'MIT OR Apache-2.0', num: '0.2.71' },
{
downloads: 42,
license: 'MIT OR Apache-2.0',
num: '0.2.71',
crate_size: 42,
},
],
}),
)
Expand Down
59 changes: 59 additions & 0 deletions services/crates/crates-size.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import prettyBytes from 'pretty-bytes'
import { InvalidResponse, pathParams } from '../index.js'
import { BaseCratesService, description } from './crates-base.js'

export default class CratesSize extends BaseCratesService {
static category = 'size'
static route = {
base: 'crates/size',
pattern: ':crate/:version?',
}

static openApi = {
'/crates/size/{crate}': {
get: {
summary: 'Crates.io Size',
description,
parameters: pathParams({
name: 'crate',
example: 'rustc-serialize',
}),
},
},
'/crates/size/{crate}/{version}': {
get: {
summary: 'Crates.io Size (version)',
description,
parameters: pathParams(
{
name: 'crate',
example: 'rustc-serialize',
},
{
name: 'version',
example: '0.3.24',
},
),
},
},
}

render({ size }) {
return {
label: 'size',
message: prettyBytes(size),
color: 'blue',
}
}

async handle({ crate, version }) {
const json = await this.fetch({ crate, version })
const size = this.constructor.getVersionObj(json).crate_size

if (size == null) {
throw new InvalidResponse({ prettyMessage: 'unknown' })
}

return this.render({ size })
}
}
19 changes: 19 additions & 0 deletions services/crates/crates-size.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createServiceTester } from '../tester.js'
import { isFileSize } from '../test-validators.js'
export const t = await createServiceTester()

t.create('size')
.get('/tokio.json')
.expectBadge({ label: 'size', message: isFileSize })

t.create('size (with version)')
.get('/tokio/1.32.0.json')
.expectBadge({ label: 'size', message: '725 kB' })

t.create('size (with version where version doesnt have size)')
.get('/tokio/0.1.6.json')
.expectBadge({ label: 'crates.io', message: 'unknown' })

t.create('size (not found)')
.get('/not-a-crate.json')
.expectBadge({ label: 'crates.io', message: 'not found' })

0 comments on commit bdf84f9

Please sign in to comment.