@containerbase/base 14.63.0 → 14.63.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/url.d.ts +11 -4
- package/dist/url.js +15 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,8 +112,9 @@ A reader can replace its urls the same way, so a mirror configured for container
|
|
|
112
112
|
```ts
|
|
113
113
|
import { getUrlReplacements, replaceUrl } from '@containerbase/base';
|
|
114
114
|
|
|
115
|
-
const { replacements, invalid } = getUrlReplacements(); // reads `process.env`
|
|
116
|
-
// `invalid` lists the `URL_REPLACE_<n>_FROM` variables without a value or `_TO` counterpart
|
|
115
|
+
const { replacements, invalid, withoutScheme } = getUrlReplacements(); // reads `process.env`
|
|
116
|
+
// `invalid` lists the `URL_REPLACE_<n>_FROM` variables without a value or `_TO` counterpart,
|
|
117
|
+
// `withoutScheme` the ones whose value doesn't start with `http://` or `https://`
|
|
117
118
|
replaceUrl(
|
|
118
119
|
'https://containerbase.github.io/tool-versions/v1/node.json',
|
|
119
120
|
replacements,
|
|
@@ -121,9 +122,8 @@ replaceUrl(
|
|
|
121
122
|
);
|
|
122
123
|
```
|
|
123
124
|
|
|
124
|
-
`replaceUrl`
|
|
125
|
-
`replaceUrlPrefix
|
|
126
|
-
It has no cdn step and returns the replaced url with the rules that matched, other fields of the rules are kept.
|
|
125
|
+
`replaceUrl` applies the cdn first, then the replacements like `replaceUrlPrefix(url, rules)`: in order, each to the result of the previous ones, and only when the url starts with its `from`.
|
|
126
|
+
`replaceUrlPrefix` has no cdn step and returns the replaced url with the rules that matched, other fields of the rules are kept.
|
|
127
127
|
End `from` and `to` with a `/`, like `https://example.com/`, so a rule only matches that host: `https://example.com` also matches `https://example.com.test/`.
|
|
128
128
|
|
|
129
129
|
```ts
|
package/dist/url.d.ts
CHANGED
|
@@ -9,6 +9,12 @@ export interface UrlReplacements {
|
|
|
9
9
|
* their `URL_REPLACE_<n>_TO` counterpart are empty or missing.
|
|
10
10
|
*/
|
|
11
11
|
invalid: string[];
|
|
12
|
+
/**
|
|
13
|
+
* The `URL_REPLACE_<n>_FROM` variables whose value doesn't start with
|
|
14
|
+
* `http://` or `https://`, like a bare host. They are used, but only match
|
|
15
|
+
* urls which start with them.
|
|
16
|
+
*/
|
|
17
|
+
withoutScheme: string[];
|
|
12
18
|
}
|
|
13
19
|
/**
|
|
14
20
|
* Reads the url replacements from the `URL_REPLACE_<n>_FROM` and
|
|
@@ -20,8 +26,9 @@ export declare function getUrlReplacements(env?: Record<string, string | undefin
|
|
|
20
26
|
/**
|
|
21
27
|
* Replaces a url the way containerbase does for its downloads: with a `cdn`,
|
|
22
28
|
* `https://` is replaced by it first, eg. `https://example.com/file` becomes
|
|
23
|
-
* `<cdn>/example.com/file`. Then
|
|
24
|
-
*
|
|
29
|
+
* `<cdn>/example.com/file`. Then the replacements are applied in order like
|
|
30
|
+
* `replaceUrlPrefix`, each to the result of the previous ones and only when
|
|
31
|
+
* the url starts with its `from`.
|
|
25
32
|
*
|
|
26
33
|
* @param url the url to replace
|
|
27
34
|
* @param replacements the replacements, see `getUrlReplacements`
|
|
@@ -46,8 +53,8 @@ export interface UrlPrefixResult<T extends UrlPrefixRule> {
|
|
|
46
53
|
/**
|
|
47
54
|
* Replaces url prefixes: the rules are applied in order, each one to the
|
|
48
55
|
* result of the previous ones, and a rule only applies when the url starts
|
|
49
|
-
* with its `from`, which is then replaced by its `to`.
|
|
50
|
-
*
|
|
56
|
+
* with its `from`, which is then replaced by its `to`. A `from` elsewhere in
|
|
57
|
+
* the url never matches, and unlike `replaceUrl` there is no cdn. End `from`
|
|
51
58
|
* and `to` with a `/`, like `https://example.com/`, so a rule only matches that
|
|
52
59
|
* host: `https://example.com` also matches `https://example.com.test/`.
|
|
53
60
|
*
|
package/dist/url.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fromRe = /^URL_REPLACE_\d+_FROM$/;
|
|
2
|
+
const schemeRe = /^https?:\/\//;
|
|
2
3
|
const collator = new Intl.Collator('en', {
|
|
3
4
|
sensitivity: 'base',
|
|
4
5
|
numeric: true,
|
|
@@ -10,7 +11,11 @@ const collator = new Intl.Collator('en', {
|
|
|
10
11
|
* @param env the environment to read, `process.env` by default
|
|
11
12
|
*/
|
|
12
13
|
export function getUrlReplacements(env = process.env) {
|
|
13
|
-
const result = {
|
|
14
|
+
const result = {
|
|
15
|
+
replacements: [],
|
|
16
|
+
invalid: [],
|
|
17
|
+
withoutScheme: [],
|
|
18
|
+
};
|
|
14
19
|
for (const from of Object.keys(env)
|
|
15
20
|
.filter((key) => fromRe.test(key))
|
|
16
21
|
.sort((a, b) => collator.compare(a, b))) {
|
|
@@ -19,6 +24,9 @@ export function getUrlReplacements(env = process.env) {
|
|
|
19
24
|
const toValue = env[to];
|
|
20
25
|
if (fromValue && toValue) {
|
|
21
26
|
result.replacements.push([fromValue, toValue]);
|
|
27
|
+
if (!schemeRe.test(fromValue)) {
|
|
28
|
+
result.withoutScheme.push(from);
|
|
29
|
+
}
|
|
22
30
|
}
|
|
23
31
|
else {
|
|
24
32
|
result.invalid.push(from);
|
|
@@ -29,30 +37,27 @@ export function getUrlReplacements(env = process.env) {
|
|
|
29
37
|
/**
|
|
30
38
|
* Replaces a url the way containerbase does for its downloads: with a `cdn`,
|
|
31
39
|
* `https://` is replaced by it first, eg. `https://example.com/file` becomes
|
|
32
|
-
* `<cdn>/example.com/file`. Then
|
|
33
|
-
*
|
|
40
|
+
* `<cdn>/example.com/file`. Then the replacements are applied in order like
|
|
41
|
+
* `replaceUrlPrefix`, each to the result of the previous ones and only when
|
|
42
|
+
* the url starts with its `from`.
|
|
34
43
|
*
|
|
35
44
|
* @param url the url to replace
|
|
36
45
|
* @param replacements the replacements, see `getUrlReplacements`
|
|
37
46
|
* @param cdn the cdn url, eg. from `CONTAINERBASE_CDN`, skipped when empty
|
|
38
47
|
* @returns the replaced url
|
|
39
48
|
*/
|
|
40
|
-
// TODO: match only at the start of the url in the next major, like `replaceUrlPrefix` (#8160)
|
|
41
49
|
export function replaceUrl(url, replacements, cdn) {
|
|
42
50
|
let result = url;
|
|
43
51
|
if (cdn) {
|
|
44
52
|
result = result.replace(/^https:\//, cdn.replace(/\/$/, ''));
|
|
45
53
|
}
|
|
46
|
-
|
|
47
|
-
result = result.replace(from, to);
|
|
48
|
-
}
|
|
49
|
-
return result;
|
|
54
|
+
return replaceUrlPrefix(result, replacements.map(([from, to]) => ({ from, to }))).url;
|
|
50
55
|
}
|
|
51
56
|
/**
|
|
52
57
|
* Replaces url prefixes: the rules are applied in order, each one to the
|
|
53
58
|
* result of the previous ones, and a rule only applies when the url starts
|
|
54
|
-
* with its `from`, which is then replaced by its `to`.
|
|
55
|
-
*
|
|
59
|
+
* with its `from`, which is then replaced by its `to`. A `from` elsewhere in
|
|
60
|
+
* the url never matches, and unlike `replaceUrl` there is no cdn. End `from`
|
|
56
61
|
* and `to` with a `/`, like `https://example.com/`, so a rule only matches that
|
|
57
62
|
* host: `https://example.com` also matches `https://example.com.test/`.
|
|
58
63
|
*
|