@containerbase/base 14.45.0 → 14.46.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 CHANGED
@@ -33,7 +33,7 @@ The raw data and its json schema are also exported, for consumers which don't us
33
33
  import tools from '@containerbase/base/tools.json' with { type: 'json' };
34
34
  ```
35
35
 
36
- The zod schemas live behind a separate entry point, so the default export stays dependency free:
36
+ The zod schemas live behind a separate entry point, so the default export only depends on `semver`:
37
37
 
38
38
  ```ts
39
39
  import { SupportedTools, ToolName } from '@containerbase/base/zod';
@@ -74,6 +74,38 @@ isToolVersionsFormat(
74
74
  ); // false
75
75
  ```
76
76
 
77
+ The containerbase image embeds the tool-versions data, a reader running in the image can use it when the site can't be reached:
78
+
79
+ ```ts
80
+ import {
81
+ isContainerbase,
82
+ toolVersionsFormatPath,
83
+ toolVersionsPath,
84
+ } from '@containerbase/base';
85
+
86
+ if (isContainerbase()) {
87
+ // eg. '/usr/local/containerbase/tool-versions/v1/node.json'
88
+ const file = `${toolVersionsPath}/${toolVersionsFormatPath}/node.json`;
89
+ }
90
+ ```
91
+
92
+ `isContainerbase()` checks the `CONTAINERBASE` variable the image sets, pass another environment to check that one instead of `process.env`.
93
+
94
+ `validateContainerbaseVersion()` reads the version of the image from `/usr/local/containerbase/version` (`containerbaseVersionFile`) and throws when the image doesn't support everything this package describes: it needs the same major version as this package and the same or a newer minor or patch version.
95
+ Development images, whose version is a git sha or `0.0.0-…`, aren't checked; a missing or empty version file throws.
96
+ Call it on startup when `isContainerbase()` is true; `isCompatibleVersion(imageVersion)` does the comparison without reading the file, and `packageVersion` is the version it compares with:
97
+
98
+ ```ts
99
+ import {
100
+ isContainerbase,
101
+ validateContainerbaseVersion,
102
+ } from '@containerbase/base';
103
+
104
+ if (isContainerbase()) {
105
+ await validateContainerbaseVersion(); // eg. '14.2.0', or throws
106
+ }
107
+ ```
108
+
77
109
  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.
78
110
 
79
111
  ## Contents
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Where the containerbase image embeds the tool-versions data: the extracted
3
+ * release archive, with the root `index.json` and one folder per format, eg.
4
+ * `v1/<tool>.json`.
5
+ */
6
+ export declare const toolVersionsPath = "/usr/local/containerbase/tool-versions";
7
+ /**
8
+ * The file in which the containerbase image records its version.
9
+ */
10
+ export declare const containerbaseVersionFile = "/usr/local/containerbase/version";
11
+ /**
12
+ * The version of this package, which is released with the same version as
13
+ * containerbase itself.
14
+ */
15
+ export declare const packageVersion: string;
16
+ /**
17
+ * Checks if the process runs in a containerbase image, whose environment sets
18
+ * `CONTAINERBASE`.
19
+ *
20
+ * @param env the environment to check, `process.env` by default
21
+ */
22
+ export declare function isContainerbase(env?: Record<string, string | undefined>): boolean;
23
+ /**
24
+ * Checks if a containerbase image supports everything this package describes:
25
+ * the image has the same major version and the same or a newer minor or patch
26
+ * version.
27
+ *
28
+ * @param imageVersion the version of the image, eg. from
29
+ * `containerbaseVersionFile`
30
+ * @param version the version to compare with, this package's by default
31
+ * @returns `false` for another major, an older image or a version which can't
32
+ * be parsed
33
+ */
34
+ export declare function isCompatibleVersion(imageVersion: string, version?: string): boolean;
35
+ /**
36
+ * Reads the version of the containerbase image and throws when it isn't
37
+ * compatible with this package, see `isCompatibleVersion`. Call it only when
38
+ * `isContainerbase()` is true.
39
+ *
40
+ * Development images, whose version is a git sha or `0.0.0-…`, aren't checked.
41
+ * A missing or empty version file still throws.
42
+ *
43
+ * @param file the version file to read, `containerbaseVersionFile` by default
44
+ * @returns the version of the image
45
+ */
46
+ export declare function validateContainerbaseVersion(file?: string): Promise<string>;
@@ -0,0 +1,74 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { createRequire } from 'node:module';
3
+ // semver has no exports map, native ESM needs the file extension
4
+ import semverGte from 'semver/functions/gte.js';
5
+ import semverParse from 'semver/functions/parse.js';
6
+ /**
7
+ * Where the containerbase image embeds the tool-versions data: the extracted
8
+ * release archive, with the root `index.json` and one folder per format, eg.
9
+ * `v1/<tool>.json`.
10
+ */
11
+ export const toolVersionsPath = '/usr/local/containerbase/tool-versions';
12
+ /**
13
+ * The file in which the containerbase image records its version.
14
+ */
15
+ export const containerbaseVersionFile = '/usr/local/containerbase/version';
16
+ /**
17
+ * The version of this package, which is released with the same version as
18
+ * containerbase itself.
19
+ */
20
+ export const packageVersion = createRequire(import.meta.url)('../package.json').version;
21
+ /**
22
+ * Checks if the process runs in a containerbase image, whose environment sets
23
+ * `CONTAINERBASE`.
24
+ *
25
+ * @param env the environment to check, `process.env` by default
26
+ */
27
+ export function isContainerbase(env = process.env) {
28
+ return !!env.CONTAINERBASE;
29
+ }
30
+ /**
31
+ * Checks if a containerbase image supports everything this package describes:
32
+ * the image has the same major version and the same or a newer minor or patch
33
+ * version.
34
+ *
35
+ * @param imageVersion the version of the image, eg. from
36
+ * `containerbaseVersionFile`
37
+ * @param version the version to compare with, this package's by default
38
+ * @returns `false` for another major, an older image or a version which can't
39
+ * be parsed
40
+ */
41
+ export function isCompatibleVersion(imageVersion, version = packageVersion) {
42
+ const image = semverParse(imageVersion.trim());
43
+ const pkg = semverParse(version);
44
+ return !!image && !!pkg && image.major === pkg.major && semverGte(image, pkg);
45
+ }
46
+ /**
47
+ * Checks if an image version comes from a release. Development builds write
48
+ * the git sha or `0.0.0-PLACEHOLDER` instead.
49
+ */
50
+ function isReleaseVersion(imageVersion) {
51
+ const image = semverParse(imageVersion);
52
+ return (!!image && !(image.major === 0 && image.minor === 0 && image.patch === 0));
53
+ }
54
+ /**
55
+ * Reads the version of the containerbase image and throws when it isn't
56
+ * compatible with this package, see `isCompatibleVersion`. Call it only when
57
+ * `isContainerbase()` is true.
58
+ *
59
+ * Development images, whose version is a git sha or `0.0.0-…`, aren't checked.
60
+ * A missing or empty version file still throws.
61
+ *
62
+ * @param file the version file to read, `containerbaseVersionFile` by default
63
+ * @returns the version of the image
64
+ */
65
+ export async function validateContainerbaseVersion(file = containerbaseVersionFile) {
66
+ const imageVersion = (await readFile(file, 'utf8')).trim();
67
+ if (!imageVersion) {
68
+ throw new Error(`The containerbase image version file ${file} is empty`);
69
+ }
70
+ if (isReleaseVersion(imageVersion) && !isCompatibleVersion(imageVersion)) {
71
+ throw new Error(`The containerbase image version ${imageVersion} is not compatible with @containerbase/base ${packageVersion}, it needs the same major version and the same or a newer minor or patch version`);
72
+ }
73
+ return imageVersion;
74
+ }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { tools } from './data.ts';
2
2
  import type { DistroCode } from './types.ts';
3
3
  export type { DistroCode, InstallToolType, ToolMetadata, ToolType, } from './types.ts';
4
4
  export type { FileIntegrity, FileReference, FormatIndex, ToolFile, ToolIndex, ToolVersion, ToolVersions, VersionFiles, VersionIntegrity, } from './tool-versions-types.ts';
5
+ export { containerbaseVersionFile, isCompatibleVersion, isContainerbase, packageVersion, toolVersionsPath, validateContainerbaseVersion, } from './containerbase.ts';
5
6
  export { isToolVersionsFormat, toolVersionsFormat, toolVersionsFormatPath, } from './tool-versions-format.ts';
6
7
  export { tools };
7
8
  /**
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { tools } from "./data.js";
2
+ export { containerbaseVersionFile, isCompatibleVersion, isContainerbase, packageVersion, toolVersionsPath, validateContainerbaseVersion, } from "./containerbase.js";
2
3
  export { isToolVersionsFormat, toolVersionsFormat, toolVersionsFormatPath, } from "./tool-versions-format.js";
3
4
  export { tools };
4
5
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@containerbase/base",
3
- "version": "14.45.0",
3
+ "version": "14.46.0",
4
4
  "description": "Metadata about the tools supported by containerbase",
5
5
  "keywords": [
6
6
  "containerbase",
@@ -31,7 +31,11 @@
31
31
  "data",
32
32
  "dist"
33
33
  ],
34
+ "dependencies": {
35
+ "semver": "^7.8.5"
36
+ },
34
37
  "devDependencies": {
38
+ "@types/semver": "7.8.0",
35
39
  "zod": "4.6.7"
36
40
  },
37
41
  "peerDependencies": {