-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathaction-cache.ts
171 lines (144 loc) · 5.61 KB
/
action-cache.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { createHash } from 'crypto';
import * as fs from 'fs';
import { copyFile } from './copy-file';
import { manglingDisabled } from './environment-options';
import { CacheKey, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
const cacache = require('cacache');
const packageVersion = require('../../package.json').version;
export interface CacheEntry {
path: string;
size: number;
integrity?: string;
}
export class BundleActionCache {
constructor(private readonly cachePath: string, private readonly integrityAlgorithm?: string) {}
static copyEntryContent(entry: CacheEntry | string, dest: fs.PathLike): void {
copyFile(typeof entry === 'string' ? entry : entry.path, dest);
if (process.platform !== 'win32') {
// The cache writes entries as readonly and when using copyFile the permissions will also be copied.
// See: https://github.com/npm/cacache/blob/073fbe1a9f789ba42d9a41de7b8429c93cf61579/lib/util/move-file.js#L36
fs.chmodSync(dest, 0o644);
}
}
generateBaseCacheKey(content: string): string {
// Create base cache key with elements:
// * package version - different build-angular versions cause different final outputs
// * code length/hash - ensure cached version matches the same input code
const algorithm = this.integrityAlgorithm || 'sha1';
const codeHash = createHash(algorithm)
.update(content)
.digest('base64');
let baseCacheKey = `${packageVersion}|${content.length}|${algorithm}-${codeHash}`;
if (manglingDisabled) {
baseCacheKey += '|MD';
}
return baseCacheKey;
}
generateCacheKeys(action: ProcessBundleOptions): string[] {
const baseCacheKey = this.generateBaseCacheKey(action.code);
// Postfix added to sourcemap cache keys when vendor sourcemaps are present
// Allows non-destructive caching of both variants
const SourceMapVendorPostfix = !!action.sourceMaps && action.vendorSourceMaps ? '|vendor' : '';
// Determine cache entries required based on build settings
const cacheKeys = [];
// If optimizing and the original is not ignored, add original as required
if ((action.optimize || action.optimizeOnly) && !action.ignoreOriginal) {
cacheKeys[CacheKey.OriginalCode] = baseCacheKey + '|orig';
// If sourcemaps are enabled, add original sourcemap as required
if (action.sourceMaps) {
cacheKeys[CacheKey.OriginalMap] = baseCacheKey + SourceMapVendorPostfix + '|orig-map';
}
}
// If not only optimizing, add downlevel as required
if (!action.optimizeOnly) {
cacheKeys[CacheKey.DownlevelCode] = baseCacheKey + '|dl';
// If sourcemaps are enabled, add downlevel sourcemap as required
if (action.sourceMaps) {
cacheKeys[CacheKey.DownlevelMap] = baseCacheKey + SourceMapVendorPostfix + '|dl-map';
}
}
return cacheKeys;
}
async getCacheEntries(cacheKeys: (string | null)[]): Promise<(CacheEntry | null)[] | false> {
// Attempt to get required cache entries
const cacheEntries = [];
for (const key of cacheKeys) {
if (key) {
const entry = await cacache.get.info(this.cachePath, key);
if (!entry) {
return false;
}
cacheEntries.push({
path: entry.path,
size: entry.size,
integrity: entry.metadata && entry.metadata.integrity,
});
} else {
cacheEntries.push(null);
}
}
return cacheEntries;
}
async getCachedBundleResult(action: ProcessBundleOptions): Promise<ProcessBundleResult | null> {
const entries = action.cacheKeys && await this.getCacheEntries(action.cacheKeys);
if (!entries) {
return null;
}
const result: ProcessBundleResult = { name: action.name };
let cacheEntry = entries[CacheKey.OriginalCode];
if (cacheEntry) {
result.original = {
filename: action.filename,
size: cacheEntry.size,
integrity: cacheEntry.integrity,
};
BundleActionCache.copyEntryContent(cacheEntry, result.original.filename);
cacheEntry = entries[CacheKey.OriginalMap];
if (cacheEntry) {
result.original.map = {
filename: action.filename + '.map',
size: cacheEntry.size,
};
BundleActionCache.copyEntryContent(cacheEntry, result.original.filename + '.map');
}
} else if (!action.ignoreOriginal) {
// If the original wasn't processed (and therefore not cached), add info
result.original = {
filename: action.filename,
size: Buffer.byteLength(action.code, 'utf8'),
map:
action.map === undefined
? undefined
: {
filename: action.filename + '.map',
size: Buffer.byteLength(action.map, 'utf8'),
},
};
}
cacheEntry = entries[CacheKey.DownlevelCode];
if (cacheEntry) {
result.downlevel = {
filename: action.filename.replace('es2015', 'es5'),
size: cacheEntry.size,
integrity: cacheEntry.integrity,
};
BundleActionCache.copyEntryContent(cacheEntry, result.downlevel.filename);
cacheEntry = entries[CacheKey.DownlevelMap];
if (cacheEntry) {
result.downlevel.map = {
filename: action.filename.replace('es2015', 'es5') + '.map',
size: cacheEntry.size,
};
BundleActionCache.copyEntryContent(cacheEntry, result.downlevel.filename + '.map');
}
}
return result;
}
}