@containerbase/base 14.49.0 → 14.51.0
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 +15 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/url.d.ts +31 -0
- package/dist/url.js +49 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -106,6 +106,21 @@ if (isContainerbase()) {
|
|
|
106
106
|
}
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
Containerbase replaces the urls it downloads from with the `CONTAINERBASE_CDN` and `URL_REPLACE_<n>_FROM` / `URL_REPLACE_<n>_TO` variables, see [custom registries](https://github.com/containerbase/base/blob/main/docs/custom-registries.md).
|
|
110
|
+
A reader can replace its urls the same way, so a mirror configured for containerbase works for it too:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { getUrlReplacements, replaceUrl } from '@containerbase/base';
|
|
114
|
+
|
|
115
|
+
const { replacements, invalid } = getUrlReplacements(); // reads `process.env`
|
|
116
|
+
// `invalid` lists the `URL_REPLACE_<n>_FROM` variables without a value or `_TO` counterpart
|
|
117
|
+
replaceUrl(
|
|
118
|
+
'https://containerbase.github.io/tool-versions/v1/node.json',
|
|
119
|
+
replacements,
|
|
120
|
+
process.env.CONTAINERBASE_CDN,
|
|
121
|
+
);
|
|
122
|
+
```
|
|
123
|
+
|
|
109
124
|
The schemas ignore unknown keys and accept any arch name, so additive changes of the format don't break a reader; readers ignore files of an arch they don't know.
|
|
110
125
|
|
|
111
126
|
## Contents
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type { Arch, DistroCode, InstallToolType, ToolMetadata, ToolType, } from
|
|
|
4
4
|
export type { FileIntegrity, FileReference, FormatIndex, ToolFile, ToolIndex, ToolVersion, ToolVersions, VersionFiles, VersionIntegrity, } from './tool-versions-types.ts';
|
|
5
5
|
export { containerbaseVersionFile, isCompatibleVersion, isContainerbase, packageVersion, toolVersionsPath, validateContainerbaseVersion, } from './containerbase.ts';
|
|
6
6
|
export { isToolVersionsFormat, toolVersionsFormat, toolVersionsFormatPath, } from './tool-versions-format.ts';
|
|
7
|
+
export type { UrlReplacement, UrlReplacements } from './url.ts';
|
|
8
|
+
export { getUrlReplacements, replaceUrl } from './url.ts';
|
|
7
9
|
export { tools };
|
|
8
10
|
/**
|
|
9
11
|
* The Ubuntu release the default containerbase image is built from, the
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { tools } from "./data.js";
|
|
2
2
|
export { containerbaseVersionFile, isCompatibleVersion, isContainerbase, packageVersion, toolVersionsPath, validateContainerbaseVersion, } from "./containerbase.js";
|
|
3
3
|
export { isToolVersionsFormat, toolVersionsFormat, toolVersionsFormatPath, } from "./tool-versions-format.js";
|
|
4
|
+
export { getUrlReplacements, replaceUrl } from "./url.js";
|
|
4
5
|
export { tools };
|
|
5
6
|
/**
|
|
6
7
|
* The Ubuntu release the default containerbase image is built from, the
|
package/dist/url.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A url replacement: the text to replace and its replacement. */
|
|
2
|
+
export type UrlReplacement = [from: string, to: string];
|
|
3
|
+
/** The url replacements read from the environment. */
|
|
4
|
+
export interface UrlReplacements {
|
|
5
|
+
/** The valid replacements, in numerical order. */
|
|
6
|
+
replacements: UrlReplacement[];
|
|
7
|
+
/**
|
|
8
|
+
* The `URL_REPLACE_<n>_FROM` variables which are ignored, because they or
|
|
9
|
+
* their `URL_REPLACE_<n>_TO` counterpart are empty or missing.
|
|
10
|
+
*/
|
|
11
|
+
invalid: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads the url replacements from the `URL_REPLACE_<n>_FROM` and
|
|
15
|
+
* `URL_REPLACE_<n>_TO` variables, in numerical order of `<n>`.
|
|
16
|
+
*
|
|
17
|
+
* @param env the environment to read, `process.env` by default
|
|
18
|
+
*/
|
|
19
|
+
export declare function getUrlReplacements(env?: Record<string, string | undefined>): UrlReplacements;
|
|
20
|
+
/**
|
|
21
|
+
* Replaces a url the way containerbase does for its downloads: with a `cdn`,
|
|
22
|
+
* `https://` is replaced by it first, eg. `https://example.com/file` becomes
|
|
23
|
+
* `<cdn>/example.com/file`. Then every replacement is applied in order, each
|
|
24
|
+
* replacing the first occurrence of its `from` text.
|
|
25
|
+
*
|
|
26
|
+
* @param url the url to replace
|
|
27
|
+
* @param replacements the replacements, see `getUrlReplacements`
|
|
28
|
+
* @param cdn the cdn url, eg. from `CONTAINERBASE_CDN`, skipped when empty
|
|
29
|
+
* @returns the replaced url
|
|
30
|
+
*/
|
|
31
|
+
export declare function replaceUrl(url: string, replacements: readonly UrlReplacement[], cdn?: string): string;
|
package/dist/url.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const fromRe = /^URL_REPLACE_\d+_FROM$/;
|
|
2
|
+
const collator = new Intl.Collator('en', {
|
|
3
|
+
sensitivity: 'base',
|
|
4
|
+
numeric: true,
|
|
5
|
+
});
|
|
6
|
+
/**
|
|
7
|
+
* Reads the url replacements from the `URL_REPLACE_<n>_FROM` and
|
|
8
|
+
* `URL_REPLACE_<n>_TO` variables, in numerical order of `<n>`.
|
|
9
|
+
*
|
|
10
|
+
* @param env the environment to read, `process.env` by default
|
|
11
|
+
*/
|
|
12
|
+
export function getUrlReplacements(env = process.env) {
|
|
13
|
+
const result = { replacements: [], invalid: [] };
|
|
14
|
+
for (const from of Object.keys(env)
|
|
15
|
+
.filter((key) => fromRe.test(key))
|
|
16
|
+
.sort((a, b) => collator.compare(a, b))) {
|
|
17
|
+
const to = from.replace(/_FROM$/, '_TO');
|
|
18
|
+
const fromValue = env[from];
|
|
19
|
+
const toValue = env[to];
|
|
20
|
+
if (fromValue && toValue) {
|
|
21
|
+
result.replacements.push([fromValue, toValue]);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
result.invalid.push(from);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Replaces a url the way containerbase does for its downloads: with a `cdn`,
|
|
31
|
+
* `https://` is replaced by it first, eg. `https://example.com/file` becomes
|
|
32
|
+
* `<cdn>/example.com/file`. Then every replacement is applied in order, each
|
|
33
|
+
* replacing the first occurrence of its `from` text.
|
|
34
|
+
*
|
|
35
|
+
* @param url the url to replace
|
|
36
|
+
* @param replacements the replacements, see `getUrlReplacements`
|
|
37
|
+
* @param cdn the cdn url, eg. from `CONTAINERBASE_CDN`, skipped when empty
|
|
38
|
+
* @returns the replaced url
|
|
39
|
+
*/
|
|
40
|
+
export function replaceUrl(url, replacements, cdn) {
|
|
41
|
+
let result = url;
|
|
42
|
+
if (cdn) {
|
|
43
|
+
result = result.replace(/^https:\//, cdn.replace(/\/$/, ''));
|
|
44
|
+
}
|
|
45
|
+
for (const [from, to] of replacements) {
|
|
46
|
+
result = result.replace(from, to);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|