Skip to content

Commit 3425d43

Browse files
committed
purl: Implement addRegistryUrl() fn
1 parent b33c0e7 commit 3425d43

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

app/utils/purl.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import window from 'ember-window-mock';
2+
3+
/**
4+
* Adds a repository_url query parameter to a PURL string based on the host.
5+
*
6+
* @param {string} purl - The base PURL string (e.g., "pkg:cargo/serde@1.0.0")
7+
* @param {string} [host] - The host to use for repository URL. Defaults to current window location host.
8+
* @returns {string} The PURL with repository_url parameter added, or unchanged if host is crates.io
9+
*/
10+
export function addRegistryUrl(purl) {
11+
let host = window.location.host;
12+
13+
// Don't add repository_url for the main crates.io registry
14+
if (host === 'crates.io') {
15+
return purl;
16+
}
17+
18+
// Add repository_url query parameter
19+
const repositoryUrl = `https://${host}/`;
20+
const separator = purl.includes('?') ? '&' : '?';
21+
return `${purl}${separator}repository_url=${encodeURIComponent(repositoryUrl)}`;
22+
}

tests/utils/purl-test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { module, test } from 'qunit';
2+
3+
import window from 'ember-window-mock';
4+
import { setupWindowMock } from 'ember-window-mock/test-support';
5+
6+
import { addRegistryUrl } from 'crates-io/utils/purl';
7+
8+
module('Utils | purl', function (hooks) {
9+
setupWindowMock(hooks);
10+
11+
module('addRegistryUrl()', function () {
12+
test('returns PURL unchanged for crates.io host', function (assert) {
13+
window.location = 'https://crates.io';
14+
15+
let purl = 'pkg:cargo/serde@1.0.0';
16+
let result = addRegistryUrl(purl);
17+
18+
assert.strictEqual(result, purl);
19+
});
20+
21+
test('adds repository_url parameter for non-crates.io hosts', function (assert) {
22+
window.location = 'https://staging.crates.io';
23+
24+
let purl = 'pkg:cargo/serde@1.0.0';
25+
let result = addRegistryUrl(purl);
26+
27+
assert.strictEqual(result, 'pkg:cargo/serde@1.0.0?repository_url=https%3A%2F%2Fstaging.crates.io%2F');
28+
});
29+
30+
test('adds repository_url parameter for custom registry hosts', function (assert) {
31+
window.location = 'https://my-registry.example.com';
32+
33+
let purl = 'pkg:cargo/my-crate@2.5.0';
34+
let result = addRegistryUrl(purl);
35+
36+
assert.strictEqual(result, 'pkg:cargo/my-crate@2.5.0?repository_url=https%3A%2F%2Fmy-registry.example.com%2F');
37+
});
38+
39+
test('appends repository_url parameter when PURL already has query parameters', function (assert) {
40+
window.location = 'https://staging.crates.io';
41+
42+
let purl = 'pkg:cargo/serde@1.0.0?arch=x86_64';
43+
let result = addRegistryUrl(purl);
44+
45+
assert.strictEqual(result, 'pkg:cargo/serde@1.0.0?arch=x86_64&repository_url=https%3A%2F%2Fstaging.crates.io%2F');
46+
});
47+
48+
test('properly URL encodes the repository URL', function (assert) {
49+
window.location = 'https://registry.example.com:8080';
50+
51+
let purl = 'pkg:cargo/test@1.0.0';
52+
let result = addRegistryUrl(purl);
53+
54+
assert.strictEqual(result, 'pkg:cargo/test@1.0.0?repository_url=https%3A%2F%2Fregistry.example.com%3A8080%2F');
55+
});
56+
57+
test('handles PURL with complex qualifiers', function (assert) {
58+
window.location = 'https://private.registry.co';
59+
60+
let purl = 'pkg:cargo/complex@1.0.0?os=linux&arch=amd64';
61+
let result = addRegistryUrl(purl);
62+
63+
assert.strictEqual(
64+
result,
65+
'pkg:cargo/complex@1.0.0?os=linux&arch=amd64&repository_url=https%3A%2F%2Fprivate.registry.co%2F',
66+
);
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)