Skip to content

Commit 0c9e1dd

Browse files
committed
docs: add examples and field docs in io module
1 parent 442edf5 commit 0c9e1dd

1 file changed

Lines changed: 74 additions & 2 deletions

File tree

src/io.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ const FORCE_UPDATE_INCREMENT = "4";
88
const CACHE_DIRNAME = "cache-apt-pkgs";
99
const CACHE_PREFIX = "cache-apt-pkgs_";
1010

11+
/**
12+
* Returns true when apt lists contain at least one reachable file.
13+
*
14+
* This is used as a cheap signal to skip unnecessary apt-get update calls.
15+
*
16+
* @returns True when at least one apt list file is found within the search depth.
17+
*
18+
* @example
19+
* const fresh = isAptListsFresh();
20+
*/
1121
export function isAptListsFresh(): boolean {
1222
const aptListsPath = "/var/lib/apt/lists";
1323
const maxDepth = 5;
@@ -40,32 +50,71 @@ export function isAptListsFresh(): boolean {
4050
return search(aptListsPath, 0);
4151
}
4252

53+
/**
54+
* Simple package descriptor used by helper code.
55+
*
56+
* @example
57+
* const pkg = new Package("curl", "8.5.0");
58+
* const encoded = pkg.serialize(); // curl@8.5.0
59+
*/
4360
export class Package {
4461
constructor(
62+
/** Package name, for example "curl". */
4563
readonly name: string,
64+
/** Package version, for example "8.5.0". */
4665
readonly version: string,
4766
) {}
4867

68+
/**
69+
* Serializes the package descriptor.
70+
*
71+
* @returns Package serialized as name@version.
72+
*/
4973
serialize(): string {
5074
return `${this.name}@${this.version}`;
5175
}
5276
}
5377

78+
/**
79+
* Structured representation of cache key components.
80+
*
81+
* @example
82+
* const key = new CacheKey("v1", "4", "x86_64", ["curl=8.5.0"]);
83+
*/
5484
export class CacheKey {
5585
constructor(
86+
/** User-provided cache salt/version. */
5687
readonly version: string,
88+
/** Internal increment used to force broad invalidation. */
5789
readonly forceUpdateIncrement: string,
90+
/** Architecture string from `arch`. */
5891
readonly arch: string,
92+
/** Sorted normalized package specifiers. */
5993
readonly normalizedPackages: string[],
6094
) {}
6195

96+
/**
97+
* Serializes cache key fields to a stable, human-readable format.
98+
*
99+
* @returns Serialized cache key components.
100+
*/
62101
serialize(): string {
63102
return `${this.version} | ${this.forceUpdateIncrement} | ${this.arch} | ${this.normalizedPackages.join(",")}`;
64103
}
65104
}
66105

106+
/**
107+
* Parses a serialized cache key into its component fields.
108+
*
109+
* @param serialized Serialized cache key string.
110+
* @returns Parsed cache key object.
111+
* @throws Error when serialized value does not contain all expected fields.
112+
*
113+
* @example
114+
* const parsed = deserializeCacheKey("v1|4|x86_64|curl=8.5.0");
115+
*/
67116
export function deserializeCacheKey(serialized: string): CacheKey {
68-
const parts = serialized.split("|");
117+
const parts = serialized.split("|").map((part) => part.trim());
69118
if (parts.length !== 4) {
70119
throw new Error(`Invalid serialized cache key: ${serialized}`);
71120
}
@@ -75,23 +124,46 @@ export function deserializeCacheKey(serialized: string): CacheKey {
75124
version!,
76125
forceUpdateIncrement!,
77126
arch!,
78-
normalizedPackagesStr!.split(","),
127+
normalizedPackagesStr!
128+
.split(",")
129+
.map((packageSpecifier) => packageSpecifier.trim()),
79130
);
80131
}
81132

133+
/**
134+
* Computes action cache path and cache keys for package sets.
135+
*
136+
* @example
137+
* const cacheStore = new Cache("cache-apt-pkgs", commandRunner);
138+
* const key = await cacheStore.getKey(["curl=8.5.0"], "v1");
139+
*/
82140
export class Cache {
141+
/** Absolute cache directory path. */
83142
private readonly cachePath: string;
143+
/** Command runner used for architecture detection. */
84144
private readonly commandRunner: CommandRunner;
85145

86146
constructor(cacheDir: string = CACHE_DIRNAME, commandRunner: CommandRunner) {
87147
this.cachePath = path.join(os.homedir(), cacheDir);
88148
this.commandRunner = commandRunner;
89149
}
90150

151+
/**
152+
* Absolute path to the local cache directory.
153+
*
154+
* @returns Absolute cache directory path.
155+
*/
91156
get path(): string {
92157
return this.cachePath;
93158
}
94159

160+
/**
161+
* Generates the normalized cache key used by GitHub Actions cache.
162+
*
163+
* @param normalizedPackages Sorted package specifiers.
164+
* @param version User-provided cache version salt.
165+
* @returns Cache key with action-specific prefix.
166+
*/
95167
async getKey(normalizedPackages: string[], version: string): Promise<string> {
96168
const architecture = (await this.commandRunner.run("arch")).stdout.trim();
97169
let value = `${normalizedPackages.join(" ")} @ ${version} ${FORCE_UPDATE_INCREMENT}`;

0 commit comments

Comments
 (0)