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

[Crates] Added crate size badge #10421

Merged
merged 7 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
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
56 changes: 56 additions & 0 deletions services/crates/crates-size.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import prettyBytes from 'pretty-bytes'
import { 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: size ? prettyBytes(size) : 'unknown',
color: size ? 'blue' : 'lightgray',
}
einstein8612 marked this conversation as resolved.
Show resolved Hide resolved
}

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

const size = this.constructor.getVersionObj(json).crate_size

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: 'size', message: 'unknown' })

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