cbst 2.0.0 → 3.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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2022-2024 Morris Brodersen <mb@morrisbrodersen.de>
1
+ Copyright 2022-2026 Morris Brodersen <mb@morrisbrodersen.de>
2
2
 
3
3
  Permission to use, copy, modify, and/or distribute this software for any purpose
4
4
  with or without fee is hereby granted, provided that the above copyright notice
package/README.md CHANGED
@@ -81,7 +81,7 @@ 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 found in matching source files will be still be rewritten.
84
+ - References found in matching source files will still be rewritten.
85
85
  - Defaults to `["*.html"]`.
86
86
  - `metadata`
87
87
  - Metadata filename (JSON).
@@ -95,19 +95,16 @@ wildcards).
95
95
 
96
96
  ## Versioning and References
97
97
 
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
98
+ As `cbst` transforms files, it rewrites any references to other versioned files.
99
+ References are detected by scanning source files for **quoted strings** that
101
100
 
102
101
  - are double- or single-quoted,
103
102
  - contain no whitespace or quotes,
104
- - and have a file extension.
103
+ - and have a file extension resolving to a known MIME type.
105
104
 
106
105
  If a reference can be resolved to a file, and that file is not `dynamic`, the
107
106
  reference is rewritten with the versioned filename.
108
107
 
109
- If a reference cannot be resolved to a file, the reference is not modified.
110
-
111
108
  ## Static vs. Dynamic Files
112
109
 
113
110
  **Static files** (not listed in `dynamic`) will be versioned based on a hash of
@@ -122,12 +119,12 @@ Cache-Control: public, max-age=31536000, immutable
122
119
  ```
123
120
 
124
121
  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
122
+ web server to deliver files containing `.v-` in their names with the above
126
123
  immutable cache control header.
127
124
 
128
125
  **Dynamic files** (those listed in `dynamic`) will not be versioned, but any
129
126
  references detected inside these files will be rewritten if necessary. For
130
- example, `*.html` files are usually be marked dynamic because they are directly
127
+ example, `*.html` files are usually marked dynamic because they are directly
131
128
  visited, not indirectly requested like other files.
132
129
 
133
130
  Dynamic files should be delivered with a `Cache-Control` header that requires
@@ -153,7 +150,7 @@ See also
153
150
  Chicken and egg problem here. The industry got used to bundlers and started
154
151
  building libraries for the web assuming that everyone used a bundler.
155
152
 
156
- Ask library authors to ship standard ES modules. That will make all our lives
153
+ Ask library authors to ship native ES modules. That will make all our lives
157
154
  easier.
158
155
 
159
156
  > Isn't bundling still more performant for larger websites?
@@ -1,4 +1,4 @@
1
- import EventEmitter from 'events';
1
+ import EventEmitter from 'node:events';
2
2
  import { Cache } from './util/Cache.js';
3
3
  import { Glob } from './util/Glob.js';
4
4
  export interface CacheBusterConfig {
@@ -56,7 +56,7 @@ export declare class CacheBuster extends EventEmitter {
56
56
  protected hashFileCache: Cache<string | undefined>;
57
57
  protected getReferencesCache: Cache<string[]>;
58
58
  protected hashRawFileCache: Cache<string>;
59
- protected readFileCache: Cache<Buffer>;
59
+ protected readFileCache: Cache<NonSharedBuffer>;
60
60
  protected map: Map<string, string>;
61
61
  protected unresolved: Map<string, Set<string>>;
62
62
  protected referenceRx: RegExp;
@@ -68,7 +68,7 @@ export declare class CacheBuster extends EventEmitter {
68
68
  writeMetadata(file: string): Promise<void>;
69
69
  handleDir(dir: string): Promise<void>;
70
70
  handleFile(file: string): Promise<void>;
71
- transformFile(file: string): Promise<string | Buffer>;
71
+ transformFile(file: string): Promise<string | NonSharedBuffer>;
72
72
  hashFile(file: string): Promise<string | undefined>;
73
73
  hashFileReferences(file: string, exclude: string[]): Promise<string[]>;
74
74
  hashRawFile(file: string): Promise<string>;
@@ -76,12 +76,13 @@ export declare class CacheBuster extends EventEmitter {
76
76
  getReferences(file: string): Promise<string[]>;
77
77
  isResolvableReference(reference: string): boolean;
78
78
  isExternalReference(reference: string): boolean;
79
+ hasKnownMIME(reference: string): boolean;
79
80
  resolveReference(base: string, reference: string): string;
80
81
  getBase(file: string): Promise<string>;
81
82
  formatVersion(hash: string): string;
82
83
  hash(inputs: (string | Buffer)[]): string;
83
- readFile(file: string): Promise<Buffer>;
84
- readDir(dir: string): Promise<import("fs").Dirent[]>;
84
+ readFile(file: string): Promise<NonSharedBuffer>;
85
+ readDir(dir: string): Promise<import("fs").Dirent<string>[]>;
85
86
  writeFile(file: string, data: string | Buffer): Promise<void>;
86
87
  safePath(rootDir: string, candidate: string): string;
87
88
  }
@@ -1,7 +1,8 @@
1
- import { createHash } from 'crypto';
2
- import EventEmitter from 'events';
3
- import { promises as fs } from 'fs';
4
- import { posix as path } from 'path';
1
+ import mime from 'mime';
2
+ import { createHash } from 'node:crypto';
3
+ import EventEmitter from 'node:events';
4
+ import { promises as fs } from 'node:fs';
5
+ import { posix as path } from 'node:path';
5
6
  import { ReferenceError } from './ReferenceError.js';
6
7
  import { Cache } from './util/Cache.js';
7
8
  import { Glob } from './util/Glob.js';
@@ -171,11 +172,16 @@ export class CacheBuster extends EventEmitter {
171
172
  return references;
172
173
  }
173
174
  isResolvableReference(reference) {
174
- return (!this.isExternalReference(reference) && !this.excludeGlob.match(reference));
175
+ return (!this.isExternalReference(reference) &&
176
+ !this.excludeGlob.match(reference) &&
177
+ this.hasKnownMIME(reference));
175
178
  }
176
179
  isExternalReference(reference) {
177
180
  return !!reference.match(this.externalReferenceRx);
178
181
  }
182
+ hasKnownMIME(reference) {
183
+ return !!mime.getType(reference);
184
+ }
179
185
  resolveReference(base, reference) {
180
186
  if (reference[0] === '/') {
181
187
  return reference.slice(1);
@@ -1,4 +1,4 @@
1
- import { promises as fs } from 'fs';
1
+ import { promises as fs } from 'node:fs';
2
2
  import { CacheBuster } from './CacheBuster.js';
3
3
  import { isRecord } from './util/isRecord.js';
4
4
  import { isStringArray } from './util/isStringArray.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cbst",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "A cache buster that versions a website's files for immutable cache control",
5
5
  "keywords": [
6
6
  "versioning",
@@ -17,10 +17,10 @@
17
17
  "bugs": {
18
18
  "url": "https://github.com/morris/cbst/issues"
19
19
  },
20
+ "type": "module",
20
21
  "engines": {
21
22
  "node": ">=20"
22
23
  },
23
- "type": "module",
24
24
  "files": [
25
25
  "dist"
26
26
  ],
@@ -37,13 +37,16 @@
37
37
  "test": "node --import tsx --test test/*.test.ts test/*/*.test.ts"
38
38
  },
39
39
  "devDependencies": {
40
- "@eslint/js": "^9.9.1",
40
+ "@eslint/js": "^10.0.1",
41
41
  "@types/eslint__js": "^8.42.3",
42
42
  "@types/node": "^20.14.8",
43
- "eslint": "^9.9.1",
43
+ "eslint": "^10.16.0",
44
44
  "prettier": "^3.3.2",
45
45
  "tsx": "^4.16.2",
46
- "typescript": "^5.0.4",
46
+ "typescript": "^6.0.3",
47
47
  "typescript-eslint": "^8.3.0"
48
+ },
49
+ "dependencies": {
50
+ "mime": "^4.1.2"
48
51
  }
49
52
  }