Skip to content

Commit 45ff9ac

Browse files
committed
winget-source: JSDoc-styled documentation
1 parent ea34971 commit 45ff9ac

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

winget-source/sync-repo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { rm } from 'fs/promises'
55
import {
66
buildPathpartMap,
77
buildURIList,
8-
checkEnvironmentVariables,
98
extractDatabaseFromBundle,
109
getLocalPath,
1110
makeTempDirectory,
11+
requireEnvironmentVariables,
1212
syncFile
1313
} from './utilities.js'
1414

15-
const { parallelLimit } = checkEnvironmentVariables();
15+
const { parallelLimit } = requireEnvironmentVariables();
1616

1717
syncFile('source.msix').then(async _ => {
1818
const temp = await makeTempDirectory('winget-repo-');

winget-source/utilities.js

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import process from 'process'
66
import { existsSync } from 'fs'
77
import { mkdir, mkdtemp, readFile, stat, utimes, writeFile } from 'fs/promises'
88

9+
/** The remote URL of a pre-indexed WinGet source repository. */
910
const remote = process.env.WINGET_REPO_URL;
11+
12+
/** The local path to serve as the root of WinGet source repository. */
1013
const local = process.env.TO;
14+
15+
/** Maximum sync jobs to be executed in parallel. Defaults to 8. */
1116
const parallelLimit = parseInt(process.env.WINGET_REPO_JOBS ?? 8);
1217

1318
/**
14-
* @param {Response} response
19+
* Get last modified date from HTTP response headers.
1520
*
16-
* @returns {Date | undefined}
21+
* @param {Response} response The HTTP `fetch` response to parse.
22+
*
23+
* @returns {Date | undefined} Last modified date derived from the response, if exists.
1724
*/
1825
function getLastModifiedDate(response) {
1926
const lastModified = response.headers.get('Last-Modified');
@@ -25,9 +32,11 @@ function getLastModifiedDate(response) {
2532
}
2633

2734
/**
28-
* @param {Response} response
35+
* Get content length from HTTP response headers.
36+
*
37+
* @param {Response} response The HTTP `fetch` response to parse.
2938
*
30-
* @returns {number}
39+
* @returns {number} Content length derived from the response, in bytes.
3140
*/
3241
function getContentLength(response) {
3342
const length = response.headers.get('Content-Length');
@@ -39,10 +48,12 @@ function getContentLength(response) {
3948
}
4049

4150
/**
42-
* @param {number} id
43-
* @param {Map<number, { parent: number, pathpart: string }>} pathparts
51+
* Resolve path parts against the local storage.
4452
*
45-
* @returns {string}
53+
* @param {number} id The ID of the target path part.
54+
* @param {Map<number, { parent: number, pathpart: string }>} pathparts Path part storage built from the database.
55+
*
56+
* @returns {string} Full URI resolved from the given ID.
4657
*/
4758
function resolvePathpart(id, pathparts) {
4859
const pathpart = pathparts.get(id);
@@ -51,10 +62,12 @@ function resolvePathpart(id, pathparts) {
5162
}
5263

5364
/**
54-
* @param {Error?} error
55-
* @param {{ rowid: number, parent: number, pathpart: string }[]} rows
65+
* Build a local storage for path parts from database query.
66+
*
67+
* @param {Error?} error Database error thrown from the query, if any.
68+
* @param {{ rowid: number, parent: number, pathpart: string }[]} rows Rows returned by the query.
5669
*
57-
* @returns {Map<number, { parent: number, pathpart: string }>}
70+
* @returns {Map<number, { parent: number, pathpart: string }>} In-memory path part storage to query against.
5871
*/
5972
export function buildPathpartMap(error, rows) {
6073
if (error) {
@@ -67,11 +80,13 @@ export function buildPathpartMap(error, rows) {
6780
}
6881

6982
/**
70-
* @param {Error?} error
71-
* @param {{ pathpart: string, [key: string]: string }[]} rows
72-
* @param {Map<number, { parent: number, pathpart: string }>} pathparts
83+
* Build a list of all manifest URIs from database query.
7384
*
74-
* @returns {string[]}
85+
* @param {Error?} error Database error thrown from the query, if any.
86+
* @param {{ pathpart: string, [key: string]: string }[]} rows Rows returned by the query.
87+
* @param {Map<number, { parent: number, pathpart: string }>} pathparts Path part storage built from the database.
88+
*
89+
* @returns {string[]} Manifest URIs to sync.
7590
*/
7691
export function buildURIList(error, rows, pathparts) {
7792
if (error) {
@@ -81,19 +96,26 @@ export function buildURIList(error, rows, pathparts) {
8196
return rows.map(row => resolvePathpart(row.pathpart, pathparts));
8297
}
8398

84-
export function checkEnvironmentVariables() {
99+
/**
100+
* Check and return the required environment variables.
101+
*
102+
* @returns Environment variables to be used in the program.
103+
*/
104+
export function requireEnvironmentVariables() {
85105
if (!remote || !local || !parallelLimit) {
86-
console.error("required envirenent variable(s) not set!");
106+
console.error("required environment variable(s) not set!");
87107
process.exit(-1);
88108
}
89109
return { remote, local, parallelLimit };
90110
}
91111

92112
/**
93-
* @param {fs.PathLike} msixPath
94-
* @param {fs.PathLike} directory
113+
* Extract database file from the source bundle.
95114
*
96-
* @returns {Promise<string>}
115+
* @param {fs.PathLike} msixPath Path of the MSIX bundle file.
116+
* @param {fs.PathLike} directory Path of directory to save the file.
117+
*
118+
* @returns {Promise<string>} Path of the extracted `index.db` file.
97119
*/
98120
export async function extractDatabaseFromBundle(msixPath, directory) {
99121
const bundle = await readFile(msixPath);
@@ -105,18 +127,22 @@ export async function extractDatabaseFromBundle(msixPath, directory) {
105127
}
106128

107129
/**
108-
* @param {string} uri
130+
* Get the local sync path of a manifest.
131+
*
132+
* @param {string} uri Manifest URI.
109133
*
110-
* @returns {string}
134+
* @returns {string} Expected local path of the manifest file.
111135
*/
112136
export function getLocalPath(uri) {
113137
return path.join(local, uri);
114138
}
115139

116140
/**
117-
* @param {string} uri
141+
* Get the remote URL of a manifest.
118142
*
119-
* @returns {URL}
143+
* @param {string} uri Manifest URI.
144+
*
145+
* @returns {URL} Remote URL to get the manifest from.
120146
*/
121147
export function getRemoteURL(uri) {
122148
const remoteURL = new URL(remote);
@@ -125,18 +151,22 @@ export function getRemoteURL(uri) {
125151
}
126152

127153
/**
128-
* @param {string} prefix
154+
* Create a unique temporary directory with given prefix.
155+
*
156+
* @param {string} prefix Temporary directory name prefix. Must not contain path separators.
129157
*
130-
* @returns {Promise<string>}
158+
* @returns {Promise<string>} Path to the created temporary directory.
131159
*/
132160
export async function makeTempDirectory(prefix) {
133161
return await mkdtemp(path.join(os.tmpdir(), prefix));
134162
}
135163

136164
/**
137-
* @param {string} uri
165+
* Sync a file with the remote server asynchronously.
166+
*
167+
* @param {string} uri URI to sync.
138168
*
139-
* @returns {Promise<boolean>}
169+
* @returns {Promise<boolean>} If the file is new or updated.
140170
*/
141171
export async function syncFile(uri) {
142172
const localPath = getLocalPath(uri);

0 commit comments

Comments
 (0)