-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathindex.ts
More file actions
123 lines (109 loc) · 3.01 KB
/
index.ts
File metadata and controls
123 lines (109 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { mkdirSync, rmSync } from 'fs';
import * as tar from 'tar';
import { DiskCache } from '../disk-cache';
import { link } from '../link';
import { defaultCacheRoot } from './default-cache-root';
export type ExtractOptions = Omit<
tar.TarOptionsWithAliasesAsyncNoFile,
'file' | 'cwd'
>;
export interface ExtractResult {
/**
* When `'hit'`, the data was already present in cache and was returned from
* cache.
*
* When `'miss'`, the data was extracted into the cache and returned from
* cache.
*
* When `undefined`, the cache is not enabled.
*/
readonly cache?: 'hit' | 'miss';
}
let packageCacheEnabled =
(process.env.JSII_RUNTIME_PACKAGE_CACHE?.toLocaleLowerCase() ?? 'enabled') ===
'enabled';
/**
* Extracts the content of a tarball, possibly caching it on disk.
*
* @param file is the path to the tarball to be extracted.
* @param options are options to pass to `tar.extract`
* @param comments are included in the cache key, when caching is enabled.
*
* @returns the result of the extraction.
*/
export function extract(
file: string,
outDir: string,
options: ExtractOptions,
...comments: readonly string[]
): ExtractResult {
try {
return (packageCacheEnabled ? extractViaCache : extractToOutDir)(
file,
outDir,
options,
...comments,
);
} catch (err) {
rmSync(outDir, { force: true, recursive: true });
throw err;
}
}
/**
* Extract the tarball into a cached directory, symlink that directory into the target location
*/
function extractViaCache(
file: string,
outDir: string,
options: ExtractOptions = {},
...comments: readonly string[]
): { cache: 'hit' | 'miss' } {
const cacheRoot =
process.env.JSII_RUNTIME_PACKAGE_CACHE_ROOT ?? defaultCacheRoot();
const dirCache = DiskCache.inDirectory(cacheRoot);
const entry = dirCache.entryFor(file, ...comments);
const { path, cache } = entry.retrieve((path) => {
untarInto({
...options,
cwd: path,
file,
});
});
link(path, outDir);
return { cache };
}
/**
* Extract directory into the target location
*/
function extractToOutDir(
file: string,
cwd: string,
options: ExtractOptions = {},
): { cache?: undefined } {
// The output directory must already exist...
mkdirSync(cwd, { recursive: true });
// !!!IMPORTANT!!!
// Extract directly into the final target directory, as certain antivirus
// software configurations on Windows will make a `renameSync` operation
// fail with EPERM until the files have been fully analyzed.
untarInto({ ...options, cwd, file });
return {};
}
function untarInto(
options: tar.TarOptionsWithAliasesAsync & { cwd: string; file: string },
) {
try {
tar.extract({ ...options, sync: true });
} catch (error) {
rmSync(options.cwd, { force: true, recursive: true });
throw error;
}
}
/** @internal */
export function getPackageCacheEnabled(): boolean {
return packageCacheEnabled;
}
/** @internal */
export function setPackageCacheEnabled(value: boolean) {
packageCacheEnabled = value;
}