@@ -6,14 +6,21 @@ import process from 'process'
6
6
import { existsSync } from 'fs'
7
7
import { mkdir , mkdtemp , readFile , stat , utimes , writeFile } from 'fs/promises'
8
8
9
+ /** The remote URL of a pre-indexed WinGet source repository. */
9
10
const remote = process . env . WINGET_REPO_URL ;
11
+
12
+ /** The local path to serve as the root of WinGet source repository. */
10
13
const local = process . env . TO ;
14
+
15
+ /** Maximum sync jobs to be executed in parallel. Defaults to 8. */
11
16
const parallelLimit = parseInt ( process . env . WINGET_REPO_JOBS ?? 8 ) ;
12
17
13
18
/**
14
- * @param { Response } response
19
+ * Get last modified date from HTTP response headers.
15
20
*
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.
17
24
*/
18
25
function getLastModifiedDate ( response ) {
19
26
const lastModified = response . headers . get ( 'Last-Modified' ) ;
@@ -25,9 +32,11 @@ function getLastModifiedDate(response) {
25
32
}
26
33
27
34
/**
28
- * @param {Response } response
35
+ * Get content length from HTTP response headers.
36
+ *
37
+ * @param {Response } response The HTTP `fetch` response to parse.
29
38
*
30
- * @returns {number }
39
+ * @returns {number } Content length derived from the response, in bytes.
31
40
*/
32
41
function getContentLength ( response ) {
33
42
const length = response . headers . get ( 'Content-Length' ) ;
@@ -39,10 +48,12 @@ function getContentLength(response) {
39
48
}
40
49
41
50
/**
42
- * @param {number } id
43
- * @param {Map<number, { parent: number, pathpart: string }> } pathparts
51
+ * Resolve path parts against the local storage.
44
52
*
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.
46
57
*/
47
58
function resolvePathpart ( id , pathparts ) {
48
59
const pathpart = pathparts . get ( id ) ;
@@ -51,10 +62,12 @@ function resolvePathpart(id, pathparts) {
51
62
}
52
63
53
64
/**
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.
56
69
*
57
- * @returns {Map<number, { parent: number, pathpart: string }> }
70
+ * @returns {Map<number, { parent: number, pathpart: string }> } In-memory path part storage to query against.
58
71
*/
59
72
export function buildPathpartMap ( error , rows ) {
60
73
if ( error ) {
@@ -67,11 +80,13 @@ export function buildPathpartMap(error, rows) {
67
80
}
68
81
69
82
/**
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.
73
84
*
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.
75
90
*/
76
91
export function buildURIList ( error , rows , pathparts ) {
77
92
if ( error ) {
@@ -81,19 +96,26 @@ export function buildURIList(error, rows, pathparts) {
81
96
return rows . map ( row => resolvePathpart ( row . pathpart , pathparts ) ) ;
82
97
}
83
98
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 ( ) {
85
105
if ( ! remote || ! local || ! parallelLimit ) {
86
- console . error ( "required envirenent variable(s) not set!" ) ;
106
+ console . error ( "required environment variable(s) not set!" ) ;
87
107
process . exit ( - 1 ) ;
88
108
}
89
109
return { remote, local, parallelLimit } ;
90
110
}
91
111
92
112
/**
93
- * @param {fs.PathLike } msixPath
94
- * @param {fs.PathLike } directory
113
+ * Extract database file from the source bundle.
95
114
*
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.
97
119
*/
98
120
export async function extractDatabaseFromBundle ( msixPath , directory ) {
99
121
const bundle = await readFile ( msixPath ) ;
@@ -105,18 +127,22 @@ export async function extractDatabaseFromBundle(msixPath, directory) {
105
127
}
106
128
107
129
/**
108
- * @param {string } uri
130
+ * Get the local sync path of a manifest.
131
+ *
132
+ * @param {string } uri Manifest URI.
109
133
*
110
- * @returns {string }
134
+ * @returns {string } Expected local path of the manifest file.
111
135
*/
112
136
export function getLocalPath ( uri ) {
113
137
return path . join ( local , uri ) ;
114
138
}
115
139
116
140
/**
117
- * @param { string } uri
141
+ * Get the remote URL of a manifest.
118
142
*
119
- * @returns {URL }
143
+ * @param {string } uri Manifest URI.
144
+ *
145
+ * @returns {URL } Remote URL to get the manifest from.
120
146
*/
121
147
export function getRemoteURL ( uri ) {
122
148
const remoteURL = new URL ( remote ) ;
@@ -125,18 +151,22 @@ export function getRemoteURL(uri) {
125
151
}
126
152
127
153
/**
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.
129
157
*
130
- * @returns {Promise<string> }
158
+ * @returns {Promise<string> } Path to the created temporary directory.
131
159
*/
132
160
export async function makeTempDirectory ( prefix ) {
133
161
return await mkdtemp ( path . join ( os . tmpdir ( ) , prefix ) ) ;
134
162
}
135
163
136
164
/**
137
- * @param {string } uri
165
+ * Sync a file with the remote server asynchronously.
166
+ *
167
+ * @param {string } uri URI to sync.
138
168
*
139
- * @returns {Promise<boolean> }
169
+ * @returns {Promise<boolean> } If the file is new or updated.
140
170
*/
141
171
export async function syncFile ( uri ) {
142
172
const localPath = getLocalPath ( uri ) ;
0 commit comments