ghtml 2.2.0 → 2.2.1

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/bin/README.md CHANGED
@@ -1,4 +1,4 @@
1
- This executable lets you append unique hashes to assets referenced in your views to aggressively cache them while guaranteeing that clients receive the most recent versions.
1
+ Lets you append unique hashes to assets referenced in your views to aggressively cache them while guaranteeing that clients receive the most recent versions.
2
2
 
3
3
  ## Usage
4
4
 
@@ -1,3 +1,5 @@
1
+ /* eslint-disable n/no-missing-import */
2
+
1
3
  import { html } from "ghtml";
2
4
 
3
5
  export default async (fastify) => {
@@ -1,3 +1,5 @@
1
+ /* eslint-disable n/no-missing-import */
2
+
1
3
  import Fastify from "fastify";
2
4
 
3
5
  const fastify = Fastify();
package/bin/src/utils.js CHANGED
@@ -38,42 +38,34 @@ const updateFilePathsWithHashes = async (
38
38
 
39
39
  for await (const filePath of filesIterable) {
40
40
  let content = await readFile(filePath, "utf8");
41
- let hasChanges = false;
41
+ let found = false;
42
42
 
43
43
  for (const [originalPath, hash] of fileHashes) {
44
- const pathIndex = content.indexOf(originalPath);
45
- if (pathIndex !== -1) {
46
- hasChanges = true;
47
- const beforePath = content.slice(0, pathIndex);
48
- const afterPath = content.slice(pathIndex + originalPath.length);
49
-
50
- const queryStart = afterPath.indexOf("?");
51
- if (queryStart === -1) {
52
- content = `${beforePath}${originalPath}?hash=${hash}${afterPath}`;
53
- continue;
54
- }
55
-
56
- const queryEnd = afterPath.slice(queryStart).search(/[#"'`]/u);
57
- const queryString =
58
- queryEnd !== -1
59
- ? afterPath.slice(queryStart + 1, queryEnd)
60
- : afterPath.slice(queryStart + 1);
61
-
62
- const hashRegex = /(?<=(?:^|&))hash=[^&]*/u;
63
- if (hashRegex.test(queryString)) {
64
- const newQueryString = queryString.replace(
65
- hashRegex,
66
- `hash=${hash}`,
67
- );
68
- content = `${beforePath}${originalPath}?${newQueryString}${afterPath.slice(queryStart + queryString.length + 1)}`;
69
- continue;
70
- }
71
-
72
- content = `${beforePath}${originalPath}?hash=${hash}&${afterPath.slice(queryStart + 1)}`;
73
- }
44
+ const escapedPath = originalPath.replace(
45
+ /[$()*+.?[\\\]^{|}]/gu,
46
+ "\\$&",
47
+ );
48
+ const regex = new RegExp(
49
+ `(?<path>${escapedPath})(\\?(?<queryString>[^#"'\`]*))?`,
50
+ "gu",
51
+ );
52
+
53
+ content = content.replace(
54
+ regex,
55
+ (match, p1, p2, p3, offset, string, groups) => {
56
+ found = true;
57
+ const { path, queryString } = groups;
58
+
59
+ return !queryString
60
+ ? `${path}?hash=${hash}`
61
+ : queryString.includes("hash=")
62
+ ? `${path}?${queryString.replace(/(?<hash>hash=)[\dA-Fa-f]*/u, `$1${hash}`)}`
63
+ : `${path}?hash=${hash}&${queryString}`;
64
+ },
65
+ );
74
66
  }
75
67
 
76
- if (hasChanges) {
68
+ if (found) {
77
69
  await writeFile(filePath, content);
78
70
  }
79
71
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Replace your template engine with fast JavaScript by leveraging the power of tagged templates.",
4
4
  "author": "Gürgün Dayıoğlu",
5
5
  "license": "MIT",
6
- "version": "2.2.0",
6
+ "version": "2.2.1",
7
7
  "type": "module",
8
8
  "bin": "./bin/src/index.js",
9
9
  "main": "./src/index.js",