cbst 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +10 -2
- package/README.md +53 -46
- package/dist/CacheBuster.d.ts +4 -4
- package/dist/CacheBuster.js +36 -23
- package/package.json +9 -11
package/LICENSE.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
Copyright 2022-2024 Morris Brodersen <mb@morrisbrodersen.de>
|
|
2
2
|
|
|
3
|
-
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
4
|
+
with or without fee is hereby granted, provided that the above copyright notice
|
|
5
|
+
and this permission notice appear in all copies.
|
|
4
6
|
|
|
5
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
9
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
11
|
+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
12
|
+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
13
|
+
THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -31,37 +31,37 @@ This diff demonstrates how versioning is applied to a simple website:
|
|
|
31
31
|
<html>
|
|
32
32
|
<head>
|
|
33
33
|
<title>Hello, World!</title>
|
|
34
|
-
- <link rel="stylesheet" href="/
|
|
35
|
-
+ <link rel="stylesheet" href="/
|
|
34
|
+
- <link rel="stylesheet" href="/css/main.css">
|
|
35
|
+
+ <link rel="stylesheet" href="/css/main.v-ed1e118152.css">
|
|
36
36
|
</head>
|
|
37
37
|
<body>
|
|
38
38
|
<h1>Hello, World!</h1>
|
|
39
39
|
<p>
|
|
40
|
-
- <img id="welcome" src="/
|
|
41
|
-
+ <img id="welcome" src="/
|
|
40
|
+
- <img id="welcome" src="/img/loading.svg">
|
|
41
|
+
+ <img id="welcome" src="/img/loading.v-834ab3df39.svg">
|
|
42
42
|
</p>
|
|
43
43
|
<script type="module">
|
|
44
|
-
- import { main } from './
|
|
45
|
-
+ import { main } from './
|
|
44
|
+
- import { main } from './js/main.js';
|
|
45
|
+
+ import { main } from './js/main.v-f39e95e656.js';
|
|
46
46
|
|
|
47
47
|
setTimeout(main, 1000);
|
|
48
48
|
</script>
|
|
49
49
|
</body>
|
|
50
50
|
</html>
|
|
51
51
|
|
|
52
|
-
# /
|
|
52
|
+
# /js/main.js -> /js/main.v-f39e95e656.js
|
|
53
53
|
|
|
54
54
|
- import { hello } from './hello.js';
|
|
55
|
-
+ import { hello } from './hello.93724d33b5.js';
|
|
55
|
+
+ import { hello } from './hello.v-93724d33b5.js';
|
|
56
56
|
|
|
57
57
|
export function main() {
|
|
58
58
|
hello('world');
|
|
59
|
-
- document.getElementById('welcome').src = '/
|
|
60
|
-
+ document.getElementById('welcome').src = '/
|
|
59
|
+
- document.getElementById('welcome').src = '/img/welcome.svg';
|
|
60
|
+
+ document.getElementById('welcome').src = '/img/welcome.v-afe45bb832.svg';
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
# /
|
|
64
|
-
# /
|
|
63
|
+
# /css/main.css -> /css/main.v-ed1e118152.css
|
|
64
|
+
# /js/hello.js -> /js/hello.v-93724d33b5.js
|
|
65
65
|
# etc.
|
|
66
66
|
```
|
|
67
67
|
|
|
@@ -81,36 +81,38 @@ The configuration file (JSON) supports the following properties:
|
|
|
81
81
|
- `dynamic`
|
|
82
82
|
- List of file patterns to treat as dynamic.
|
|
83
83
|
- Matching files will not be versioned.
|
|
84
|
-
- References
|
|
84
|
+
- References found in matching source files will be still be rewritten.
|
|
85
85
|
- Defaults to `["*.html"]`.
|
|
86
86
|
- `metadata`
|
|
87
|
-
- Metadata filename.
|
|
88
|
-
-
|
|
87
|
+
- Metadata filename (JSON).
|
|
88
|
+
- If omitted, no metadata file is written.
|
|
89
89
|
- `hashLength`
|
|
90
90
|
- Length of hashes in versioned filenames.
|
|
91
91
|
- Defaults to `10`.
|
|
92
92
|
|
|
93
|
-
File patterns support `*` (slash-free wildcards) and `/**/` (directory
|
|
93
|
+
File patterns support `*` (slash-free wildcards) and `/**/` (directory
|
|
94
|
+
wildcards).
|
|
94
95
|
|
|
95
96
|
## Versioning and References
|
|
96
97
|
|
|
97
|
-
As `cbst` transforms files, it also rewrites any references to other versioned
|
|
98
|
-
References are detected by scanning source files for **quoted strings**
|
|
98
|
+
As `cbst` transforms files, it also rewrites any references to other versioned
|
|
99
|
+
files. References are detected by scanning source files for **quoted strings**
|
|
100
|
+
that
|
|
99
101
|
|
|
100
102
|
- are double- or single-quoted,
|
|
101
103
|
- contain no whitespace or quotes,
|
|
102
104
|
- and have a file extension.
|
|
103
105
|
|
|
104
|
-
If a reference can be resolved to a file, and that file is not `dynamic`,
|
|
105
|
-
|
|
106
|
+
If a reference can be resolved to a file, and that file is not `dynamic`, the
|
|
107
|
+
reference is rewritten with the versioned filename.
|
|
106
108
|
|
|
107
109
|
If a reference cannot be resolved to a file, the reference is not modified.
|
|
108
110
|
|
|
109
111
|
## Static vs. Dynamic Files
|
|
110
112
|
|
|
111
|
-
**Static files** (not listed in `dynamic`) will be versioned based on a hash
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
**Static files** (not listed in `dynamic`) will be versioned based on a hash of
|
|
114
|
+
their contents, as well as the contents of any referenced files. Circular
|
|
115
|
+
references are resolved deterministically.
|
|
114
116
|
|
|
115
117
|
Static files should be delivered with a `Cache-Control` header that allows
|
|
116
118
|
browsers and proxies to cache them indefinitely. For example:
|
|
@@ -119,15 +121,19 @@ browsers and proxies to cache them indefinitely. For example:
|
|
|
119
121
|
Cache-Control: public, max-age=31536000, immutable
|
|
120
122
|
```
|
|
121
123
|
|
|
124
|
+
Version tags generated by `cbst` are prefixed with `v-`. You may configure your
|
|
125
|
+
web server to deliver files containing `.v-` in their name with the above
|
|
126
|
+
immutable cache control header.
|
|
127
|
+
|
|
122
128
|
**Dynamic files** (those listed in `dynamic`) will not be versioned, but any
|
|
123
|
-
references detected will be rewritten if necessary. For
|
|
124
|
-
`*.html` files are usually be marked dynamic because they are directly
|
|
129
|
+
references detected inside these files will be rewritten if necessary. For
|
|
130
|
+
example, `*.html` files are usually be marked dynamic because they are directly
|
|
125
131
|
visited, not indirectly requested like other files.
|
|
126
132
|
|
|
127
|
-
Dynamic files should be delivered with a `Cache-Control` header that
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
Dynamic files should be delivered with a `Cache-Control` header that requires
|
|
134
|
+
browsers and proxies to revalidate the file in a timely fashion. Additional
|
|
135
|
+
`E-Tag` or `Last-Modified` headers should be provided to allow conditional
|
|
136
|
+
requests. For example:
|
|
131
137
|
|
|
132
138
|
```
|
|
133
139
|
Cache-Control: public, max-age=604800, stale-while-revalidate=86400
|
|
@@ -141,16 +147,16 @@ See also
|
|
|
141
147
|
|
|
142
148
|
## Discussion
|
|
143
149
|
|
|
144
|
-
> I can't use my favorite libraries from
|
|
150
|
+
> I can't use my favorite libraries from npm with this because they require
|
|
151
|
+
> Node.js resolution and/or bundling.
|
|
145
152
|
|
|
146
|
-
Chicken
|
|
147
|
-
|
|
148
|
-
assuming that everyone uses a bundler.
|
|
153
|
+
Chicken and egg problem here. The industry got used to bundlers and started
|
|
154
|
+
building libraries for the web assuming that everyone used a bundler.
|
|
149
155
|
|
|
150
|
-
Ask library authors to ship standard
|
|
151
|
-
|
|
156
|
+
Ask library authors to ship standard ES modules. That will make all our lives
|
|
157
|
+
easier.
|
|
152
158
|
|
|
153
|
-
> Isn't bundling still more performant larger websites?
|
|
159
|
+
> Isn't bundling still more performant for larger websites?
|
|
154
160
|
|
|
155
161
|
You should be fine if your website is delivered via HTTP/2 with compression and
|
|
156
162
|
solid cache control.
|
|
@@ -158,21 +164,22 @@ solid cache control.
|
|
|
158
164
|
For recurring users, bundled websites that are modified regularly require
|
|
159
165
|
downloading/parsing the entire bundle after every modification.
|
|
160
166
|
|
|
161
|
-
With fine-grained file versioning and long-term caching,
|
|
162
|
-
|
|
163
|
-
but recurring users will have a better experience.
|
|
167
|
+
With fine-grained file versioning and long-term caching, the first page visit
|
|
168
|
+
may suffer, but recurring users will have a better experience.
|
|
164
169
|
|
|
165
170
|
Finally, avoiding frameworks and large dependencies is a much more effective
|
|
166
171
|
optimization in the first place.
|
|
167
172
|
|
|
168
173
|
> What about minification?
|
|
169
174
|
|
|
170
|
-
It's out of scope
|
|
171
|
-
|
|
172
|
-
|
|
175
|
+
It's out of scope for `cbst`. There are too many options to consider, and any
|
|
176
|
+
integration with minifiers will probably result in config/plug-in hell.
|
|
177
|
+
|
|
178
|
+
Instead, websites should be minified in a separate pass during a production
|
|
179
|
+
build, usually before cache busting.
|
|
173
180
|
|
|
174
|
-
> Why is this implemented with regular expressions? Everyone knows you can't
|
|
181
|
+
> Why is this implemented with regular expressions? Everyone knows you can't
|
|
182
|
+
> parse HTML, CSS, and JS like that!!1
|
|
175
183
|
|
|
176
|
-
It's a trade-off.
|
|
177
|
-
|
|
178
|
-
and will only fail in obscure cases (hopefully).
|
|
184
|
+
It's a trade-off. The RegExp-based implementation is simple, fast, and will only
|
|
185
|
+
fail in obscure cases (hopefully).
|
package/dist/CacheBuster.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface CacheBusterConfig {
|
|
|
24
24
|
dynamic?: string[];
|
|
25
25
|
/**
|
|
26
26
|
* Metadata filename.
|
|
27
|
-
*
|
|
27
|
+
* If omitted, no metadata file is written.
|
|
28
28
|
*/
|
|
29
29
|
metadata?: string;
|
|
30
30
|
/**
|
|
@@ -65,9 +65,9 @@ export declare class CacheBuster extends EventEmitter {
|
|
|
65
65
|
protected extensionRx: RegExp;
|
|
66
66
|
constructor(options: CacheBusterOptions);
|
|
67
67
|
run(): Promise<void>;
|
|
68
|
-
writeMetadata(): Promise<void>;
|
|
69
|
-
handleDir(dir: string): Promise<
|
|
70
|
-
handleFile(file: string): Promise<
|
|
68
|
+
writeMetadata(file: string): Promise<void>;
|
|
69
|
+
handleDir(dir: string): Promise<void>;
|
|
70
|
+
handleFile(file: string): Promise<void>;
|
|
71
71
|
transformFile(file: string): Promise<string | Buffer>;
|
|
72
72
|
hashFile(file: string): Promise<string | undefined>;
|
|
73
73
|
hashFileReferences(file: string, exclude: string[]): Promise<string[]>;
|
package/dist/CacheBuster.js
CHANGED
|
@@ -11,7 +11,7 @@ export class CacheBuster extends EventEmitter {
|
|
|
11
11
|
source: ['*.html', '*.css', '*.js', '*.svg', '*.json'],
|
|
12
12
|
html: ['*.html'],
|
|
13
13
|
dynamic: ['*.html'],
|
|
14
|
-
metadata: '
|
|
14
|
+
metadata: '',
|
|
15
15
|
hashLength: 10,
|
|
16
16
|
};
|
|
17
17
|
inputDir;
|
|
@@ -29,7 +29,7 @@ export class CacheBuster extends EventEmitter {
|
|
|
29
29
|
map = new Map();
|
|
30
30
|
unresolved = new Map();
|
|
31
31
|
referenceRx = /((")(([^"\s]+)\.([a-zA-Z0-9]+))")|((')(([^'\s]+)\.([a-zA-Z0-9]+))')|# sourceMappingURL=(([^\s]+)\.([a-zA-Z0-9]+))/g;
|
|
32
|
-
externalReferenceRx = /^([a-zA-Z][a-zA-Z0-9
|
|
32
|
+
externalReferenceRx = /^([a-zA-Z][a-zA-Z0-9+.-]*:|\/\/)/;
|
|
33
33
|
baseRx = /<base[^<>]+href\s*=\s*"([^"]+)"[^<>]*>/i;
|
|
34
34
|
extensionRx = /(?<=\.)[^.]+$/;
|
|
35
35
|
constructor(options) {
|
|
@@ -46,14 +46,17 @@ export class CacheBuster extends EventEmitter {
|
|
|
46
46
|
}
|
|
47
47
|
async run() {
|
|
48
48
|
await this.handleDir('.');
|
|
49
|
-
|
|
49
|
+
if (this.metadata) {
|
|
50
|
+
await this.writeMetadata(this.metadata);
|
|
51
|
+
}
|
|
50
52
|
}
|
|
51
|
-
async writeMetadata() {
|
|
52
|
-
await this.writeFile(
|
|
53
|
+
async writeMetadata(file) {
|
|
54
|
+
await this.writeFile(file, JSON.stringify({ map: Object.fromEntries(this.map.entries()) }));
|
|
53
55
|
}
|
|
54
56
|
async handleDir(dir) {
|
|
55
|
-
if (this.excludeGlob.match(dir))
|
|
56
|
-
return
|
|
57
|
+
if (this.excludeGlob.match(dir)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
57
60
|
const entries = await this.readDir(dir);
|
|
58
61
|
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
59
62
|
for (const entry of entries) {
|
|
@@ -66,8 +69,9 @@ export class CacheBuster extends EventEmitter {
|
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
async handleFile(file) {
|
|
69
|
-
if (this.excludeGlob.match(file))
|
|
70
|
-
return
|
|
72
|
+
if (this.excludeGlob.match(file)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
71
75
|
const output = await this.transformFile(file);
|
|
72
76
|
const hash = await this.hashFileCache.get(file);
|
|
73
77
|
const outputFile = hash
|
|
@@ -96,28 +100,32 @@ export class CacheBuster extends EventEmitter {
|
|
|
96
100
|
const reference = groups[2] || groups[7] || groups[10];
|
|
97
101
|
const filename = groups[3] || groups[8] || groups[11];
|
|
98
102
|
const extension = groups[4] || groups[9] || groups[12];
|
|
99
|
-
if (!this.isResolvableReference(reference))
|
|
103
|
+
if (!this.isResolvableReference(reference)) {
|
|
100
104
|
return match;
|
|
105
|
+
}
|
|
101
106
|
const hash = hashes[i++];
|
|
102
107
|
if (!hash)
|
|
103
108
|
return match;
|
|
109
|
+
const version = this.formatVersion(hash);
|
|
104
110
|
if (groups[10]) {
|
|
105
|
-
return `# sourceMappingURL=${filename}.${
|
|
111
|
+
return `# sourceMappingURL=${filename}.${version}.${extension}`;
|
|
106
112
|
}
|
|
107
|
-
return `${quote}${filename}.${
|
|
113
|
+
return `${quote}${filename}.${version}.${extension}${quote}`;
|
|
108
114
|
});
|
|
109
115
|
}
|
|
110
116
|
async hashFile(file) {
|
|
111
|
-
if (this.dynamicGlob.match(file))
|
|
117
|
+
if (this.dynamicGlob.match(file)) {
|
|
112
118
|
return undefined;
|
|
119
|
+
}
|
|
113
120
|
return this.hash([
|
|
114
121
|
await this.hashRawFileCache.get(file),
|
|
115
122
|
...(await this.hashFileReferences(file, [])),
|
|
116
123
|
]);
|
|
117
124
|
}
|
|
118
125
|
async hashFileReferences(file, exclude) {
|
|
119
|
-
if (exclude.includes(file))
|
|
126
|
+
if (exclude.includes(file)) {
|
|
120
127
|
return [];
|
|
128
|
+
}
|
|
121
129
|
const references = await this.getReferencesCache.get(file);
|
|
122
130
|
const direct = await Promise.all(references.map((it) => this.hashRawFileCache.get(it).catch((err) => {
|
|
123
131
|
this.handleReferenceError(err, file, it);
|
|
@@ -139,15 +147,17 @@ export class CacheBuster extends EventEmitter {
|
|
|
139
147
|
unresolved = new Set();
|
|
140
148
|
this.unresolved.set(source, unresolved);
|
|
141
149
|
}
|
|
142
|
-
if (unresolved.has(reference))
|
|
150
|
+
if (unresolved.has(reference)) {
|
|
143
151
|
return;
|
|
152
|
+
}
|
|
144
153
|
unresolved.add(reference);
|
|
145
154
|
this.emit('error', new ReferenceError(error, source, reference));
|
|
146
155
|
}
|
|
147
|
-
//
|
|
156
|
+
// References
|
|
148
157
|
async getReferences(file) {
|
|
149
|
-
if (!this.sourceGlob.match(file))
|
|
158
|
+
if (!this.sourceGlob.match(file)) {
|
|
150
159
|
return [];
|
|
160
|
+
}
|
|
151
161
|
const buffer = await this.readFileCache.get(file);
|
|
152
162
|
const source = buffer.toString();
|
|
153
163
|
const base = await this.getBase(file);
|
|
@@ -167,8 +177,9 @@ export class CacheBuster extends EventEmitter {
|
|
|
167
177
|
return !!reference.match(this.externalReferenceRx);
|
|
168
178
|
}
|
|
169
179
|
resolveReference(base, reference) {
|
|
170
|
-
if (reference[0] === '/')
|
|
180
|
+
if (reference[0] === '/') {
|
|
171
181
|
return reference.slice(1);
|
|
182
|
+
}
|
|
172
183
|
return path.join(base[0] === '/' ? base.slice(1) : base, reference);
|
|
173
184
|
}
|
|
174
185
|
async getBase(file) {
|
|
@@ -176,22 +187,24 @@ export class CacheBuster extends EventEmitter {
|
|
|
176
187
|
const buffer = await this.readFileCache.get(file);
|
|
177
188
|
const source = buffer.toString();
|
|
178
189
|
const match = source.match(this.baseRx);
|
|
179
|
-
if (match)
|
|
190
|
+
if (match) {
|
|
180
191
|
return match[1];
|
|
192
|
+
}
|
|
181
193
|
}
|
|
182
194
|
return path.dirname(file);
|
|
183
195
|
}
|
|
184
|
-
//
|
|
196
|
+
// Hashing
|
|
185
197
|
formatVersion(hash) {
|
|
186
|
-
return hash.slice(0, this.hashLength)
|
|
198
|
+
return `v-${hash.slice(0, this.hashLength)}`;
|
|
187
199
|
}
|
|
188
200
|
hash(inputs) {
|
|
189
201
|
const h = createHash('sha1');
|
|
190
|
-
for (const input of inputs)
|
|
202
|
+
for (const input of inputs) {
|
|
191
203
|
h.update(input);
|
|
204
|
+
}
|
|
192
205
|
return h.digest('hex');
|
|
193
206
|
}
|
|
194
|
-
//
|
|
207
|
+
// I/O
|
|
195
208
|
async readFile(file) {
|
|
196
209
|
return fs.readFile(this.safePath(this.inputDir, file));
|
|
197
210
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cbst",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A cache buster that versions a website's files for immutable cache control",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"versioning",
|
|
@@ -30,22 +30,20 @@
|
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc --project tsconfig.build.json",
|
|
33
|
-
"clean": "rm -rf coverage dist test/fixtures/out",
|
|
33
|
+
"clean": "rm -rf coverage dist test/fixtures/all/out test/fixtures/example/out",
|
|
34
34
|
"lint": "eslint .",
|
|
35
35
|
"format": "prettier --write .",
|
|
36
36
|
"format-check": "prettier --check .",
|
|
37
|
-
"test": "
|
|
37
|
+
"test": "node --import tsx --test test/*.test.ts test/*/*.test.ts"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@
|
|
40
|
+
"@eslint/js": "^9.9.1",
|
|
41
|
+
"@types/eslint__js": "^8.42.3",
|
|
41
42
|
"@types/node": "^20.14.8",
|
|
42
|
-
"
|
|
43
|
-
"@typescript-eslint/parser": "^7.13.1",
|
|
44
|
-
"eslint": "^8.15.0",
|
|
45
|
-
"jest": "^29.1.2",
|
|
43
|
+
"eslint": "^9.9.1",
|
|
46
44
|
"prettier": "^3.3.2",
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"typescript": "^
|
|
45
|
+
"tsx": "^4.16.2",
|
|
46
|
+
"typescript": "^5.0.4",
|
|
47
|
+
"typescript-eslint": "^8.3.0"
|
|
50
48
|
}
|
|
51
49
|
}
|