From b6ce2826c0f592d789a6f1e2faa6aaac46f81445 Mon Sep 17 00:00:00 2001 From: Joey Li Date: Sun, 28 Jul 2024 15:55:41 +0200 Subject: [PATCH 1/6] add some basic tests for size badge --- services/crates/crates-size.tester.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 services/crates/crates-size.tester.js diff --git a/services/crates/crates-size.tester.js b/services/crates/crates-size.tester.js new file mode 100644 index 0000000000000..fa547be60cbe2 --- /dev/null +++ b/services/crates/crates-size.tester.js @@ -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: '708 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' }) From 3f16a49bb94fd113a7793f29a29e4b600da06529 Mon Sep 17 00:00:00 2001 From: Joey Li Date: Sun, 28 Jul 2024 15:56:57 +0200 Subject: [PATCH 2/6] add crate size to crates fetch --- services/crates/crates-base.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/crates/crates-base.js b/services/crates/crates-base.js index 079f519de5482..7b7571c55d029 100644 --- a/services/crates/crates-base.js +++ b/services/crates/crates-base.js @@ -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), From d024046542011617789182ac35fc4b2c369b7bf6 Mon Sep 17 00:00:00 2001 From: Joey Li Date: Sun, 28 Jul 2024 15:57:19 +0200 Subject: [PATCH 3/6] add crate size service --- services/crates/crates-size.service.js | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 services/crates/crates-size.service.js diff --git a/services/crates/crates-size.service.js b/services/crates/crates-size.service.js new file mode 100644 index 0000000000000..574c4df683e6d --- /dev/null +++ b/services/crates/crates-size.service.js @@ -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', + } + } + + async handle({ crate, version }) { + const json = await this.fetch({ crate, version }) + + const size = this.constructor.getVersionObj(json).crate_size + + return this.render({ size }) + } +} From 357866a7e501880a6214e16e972a9e6c1bff049a Mon Sep 17 00:00:00 2001 From: Joey Li Date: Sun, 28 Jul 2024 16:00:08 +0200 Subject: [PATCH 4/6] change test to reflect base-10 counting instead --- services/crates/crates-size.tester.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/crates/crates-size.tester.js b/services/crates/crates-size.tester.js index fa547be60cbe2..46a2db7df4705 100644 --- a/services/crates/crates-size.tester.js +++ b/services/crates/crates-size.tester.js @@ -8,7 +8,7 @@ t.create('size') t.create('size (with version)') .get('/tokio/1.32.0.json') - .expectBadge({ label: 'size', message: '708 kB' }) + .expectBadge({ label: 'size', message: '725 kB' }) t.create('size (with version where version doesnt have size)') .get('/tokio/0.1.6.json') From 64df855626ad5cef8a1c57d1731bc11641c39d2f Mon Sep 17 00:00:00 2001 From: Joey Li Date: Sun, 28 Jul 2024 16:03:28 +0200 Subject: [PATCH 5/6] fix downloads tests to work with new crate_size scheme addition --- services/crates/crates-downloads.tester.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/crates/crates-downloads.tester.js b/services/crates/crates-downloads.tester.js index 8bc169570e5fb..1f2986e722e13 100644 --- a/services/crates/crates-downloads.tester.js +++ b/services/crates/crates-downloads.tester.js @@ -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, + }, ], }), ) From 88823b4a05e40d25a165b4f8ddcd384eeba54649 Mon Sep 17 00:00:00 2001 From: Joey Li Date: Sun, 4 Aug 2024 14:08:31 +0200 Subject: [PATCH 6/6] move handling of unknown size to handle --- services/crates/crates-size.service.js | 11 +++++++---- services/crates/crates-size.tester.js | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/services/crates/crates-size.service.js b/services/crates/crates-size.service.js index 574c4df683e6d..ab819c46ac4ae 100644 --- a/services/crates/crates-size.service.js +++ b/services/crates/crates-size.service.js @@ -1,5 +1,5 @@ import prettyBytes from 'pretty-bytes' -import { pathParams } from '../index.js' +import { InvalidResponse, pathParams } from '../index.js' import { BaseCratesService, description } from './crates-base.js' export default class CratesSize extends BaseCratesService { @@ -41,16 +41,19 @@ export default class CratesSize extends BaseCratesService { render({ size }) { return { label: 'size', - message: size ? prettyBytes(size) : 'unknown', - color: size ? 'blue' : 'lightgray', + 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 }) } } diff --git a/services/crates/crates-size.tester.js b/services/crates/crates-size.tester.js index 46a2db7df4705..6aea268ced66d 100644 --- a/services/crates/crates-size.tester.js +++ b/services/crates/crates-size.tester.js @@ -12,7 +12,7 @@ t.create('size (with version)') t.create('size (with version where version doesnt have size)') .get('/tokio/0.1.6.json') - .expectBadge({ label: 'size', message: 'unknown' }) + .expectBadge({ label: 'crates.io', message: 'unknown' }) t.create('size (not found)') .get('/not-a-crate.json')