vite-plugin-sri4 1.8.3 → 1.8.5

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/dist/index.cjs CHANGED
@@ -20,10 +20,15 @@ function log(message, options) {
20
20
 
21
21
  function computeSri(content, algorithm = 'sha384') {
22
22
  try {
23
- const hash = node_crypto.createHash(algorithm)
24
- .update(content)
25
- .digest('base64');
26
- return `${algorithm}-${hash}`;
23
+ const hash = node_crypto.createHash(algorithm);
24
+ if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
25
+ hash.update(content);
26
+ } else if (typeof content === 'string') {
27
+ hash.update(Buffer.from(content, 'utf-8'));
28
+ } else {
29
+ throw new Error('Invalid content type');
30
+ }
31
+ return `${algorithm}-${hash.digest('base64')}`;
27
32
  } catch (error) {
28
33
  console.error(`${LOG_PREFIX} Failed to compute SRI hash: ${error}`);
29
34
  return null;
@@ -66,23 +71,29 @@ function hasCrossOriginAttr(tag) {
66
71
  }
67
72
 
68
73
  function getBundleKey(url, base = '') {
69
- // 嘗試各種可能的路徑格式
70
- const possiblePaths = [
71
- url,
72
- url.replace(/^\//, ''),
73
- url.replace(/^\/static\//, ''),
74
- url.replace(/^static\//, ''),
75
- url.replace(base, ''),
76
- url.replace(base, '').replace(/^\//, '')
74
+ // Remove base prefix if exists
75
+ let cleanUrl = url.startsWith(base) ? url.slice(base.length) : url;
76
+ // Remove leading slash
77
+ cleanUrl = cleanUrl.startsWith('/') ? cleanUrl.slice(1) : cleanUrl;
78
+
79
+ // Try different path combinations
80
+ const paths = [
81
+ cleanUrl,
82
+ `static/${cleanUrl}`,
83
+ cleanUrl.replace(/^static\//, '')
77
84
  ];
78
85
 
79
- // 移除 hash 後的版本
80
- const withoutHash = url.replace(/-[a-zA-Z0-9]+\.([^.]+)$/, '.$1');
81
- if (withoutHash !== url) {
82
- possiblePaths.push(...getBundleKey(withoutHash, base));
86
+ // Remove hash part if exists and try again
87
+ const withoutHash = cleanUrl.replace(/-[a-zA-Z0-9]+\.([^.]+)$/, '.$1');
88
+ if (withoutHash !== cleanUrl) {
89
+ paths.push(...[
90
+ withoutHash,
91
+ `static/${withoutHash}`,
92
+ withoutHash.replace(/^static\//, '')
93
+ ]);
83
94
  }
84
95
 
85
- return [...new Set(possiblePaths)];
96
+ return [...new Set(paths)];
86
97
  }
87
98
 
88
99
  async function processTag(tag, url, options, bundle, base = '') {
@@ -128,6 +139,10 @@ async function processTag(tag, url, options, bundle, base = '') {
128
139
  // Handle local resources
129
140
  const possibleKeys = getBundleKey(url, base);
130
141
  let bundleItem = null;
142
+ let source;
143
+
144
+ log(`Looking for bundle keys:`, options);
145
+ possibleKeys.forEach(key => log(`- ${key}`, options));
131
146
 
132
147
  for (const key of possibleKeys) {
133
148
  if (bundle[key]) {
@@ -137,8 +152,23 @@ async function processTag(tag, url, options, bundle, base = '') {
137
152
  }
138
153
  }
139
154
 
140
- if (bundleItem) {
141
- const source = bundleItem.type === 'chunk' ? bundleItem.code : bundleItem.source;
155
+ if (!bundleItem) {
156
+ log(`Available bundle keys:`, options);
157
+ Object.keys(bundle).forEach(key => log(`- ${key}`, options));
158
+ return tag;
159
+ }
160
+
161
+ log(`Bundle item type: ${bundleItem.type}`, options);
162
+
163
+ try {
164
+ if (bundleItem.type === 'chunk') {
165
+ source = bundleItem.code;
166
+ log(`Processing chunk content of length: ${source.length}`, options);
167
+ } else {
168
+ source = bundleItem.source;
169
+ log(`Processing asset content of length: ${source.length}`, options);
170
+ }
171
+
142
172
  const integrity = computeSri(source, options.algorithm);
143
173
  if (integrity) {
144
174
  log(`Computing SRI for local resource ${url}: ${integrity}`, options);
@@ -148,9 +178,8 @@ async function processTag(tag, url, options, bundle, base = '') {
148
178
  log(`New tag: ${newTag}`, options);
149
179
  return newTag;
150
180
  }
151
- } else {
152
- log(`No bundle item found for ${url}`, options);
153
- log(`Available bundle keys: ${Object.keys(bundle).join(', ')}`, options);
181
+ } catch (error) {
182
+ log(`Error processing bundle item: ${error}`, options);
154
183
  }
155
184
 
156
185
  return tag;
package/dist/index.js CHANGED
@@ -18,10 +18,15 @@ function log(message, options) {
18
18
 
19
19
  function computeSri(content, algorithm = 'sha384') {
20
20
  try {
21
- const hash = createHash(algorithm)
22
- .update(content)
23
- .digest('base64');
24
- return `${algorithm}-${hash}`;
21
+ const hash = createHash(algorithm);
22
+ if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
23
+ hash.update(content);
24
+ } else if (typeof content === 'string') {
25
+ hash.update(Buffer.from(content, 'utf-8'));
26
+ } else {
27
+ throw new Error('Invalid content type');
28
+ }
29
+ return `${algorithm}-${hash.digest('base64')}`;
25
30
  } catch (error) {
26
31
  console.error(`${LOG_PREFIX} Failed to compute SRI hash: ${error}`);
27
32
  return null;
@@ -64,23 +69,29 @@ function hasCrossOriginAttr(tag) {
64
69
  }
65
70
 
66
71
  function getBundleKey(url, base = '') {
67
- // 嘗試各種可能的路徑格式
68
- const possiblePaths = [
69
- url,
70
- url.replace(/^\//, ''),
71
- url.replace(/^\/static\//, ''),
72
- url.replace(/^static\//, ''),
73
- url.replace(base, ''),
74
- url.replace(base, '').replace(/^\//, '')
72
+ // Remove base prefix if exists
73
+ let cleanUrl = url.startsWith(base) ? url.slice(base.length) : url;
74
+ // Remove leading slash
75
+ cleanUrl = cleanUrl.startsWith('/') ? cleanUrl.slice(1) : cleanUrl;
76
+
77
+ // Try different path combinations
78
+ const paths = [
79
+ cleanUrl,
80
+ `static/${cleanUrl}`,
81
+ cleanUrl.replace(/^static\//, '')
75
82
  ];
76
83
 
77
- // 移除 hash 後的版本
78
- const withoutHash = url.replace(/-[a-zA-Z0-9]+\.([^.]+)$/, '.$1');
79
- if (withoutHash !== url) {
80
- possiblePaths.push(...getBundleKey(withoutHash, base));
84
+ // Remove hash part if exists and try again
85
+ const withoutHash = cleanUrl.replace(/-[a-zA-Z0-9]+\.([^.]+)$/, '.$1');
86
+ if (withoutHash !== cleanUrl) {
87
+ paths.push(...[
88
+ withoutHash,
89
+ `static/${withoutHash}`,
90
+ withoutHash.replace(/^static\//, '')
91
+ ]);
81
92
  }
82
93
 
83
- return [...new Set(possiblePaths)];
94
+ return [...new Set(paths)];
84
95
  }
85
96
 
86
97
  async function processTag(tag, url, options, bundle, base = '') {
@@ -126,6 +137,10 @@ async function processTag(tag, url, options, bundle, base = '') {
126
137
  // Handle local resources
127
138
  const possibleKeys = getBundleKey(url, base);
128
139
  let bundleItem = null;
140
+ let source;
141
+
142
+ log(`Looking for bundle keys:`, options);
143
+ possibleKeys.forEach(key => log(`- ${key}`, options));
129
144
 
130
145
  for (const key of possibleKeys) {
131
146
  if (bundle[key]) {
@@ -135,8 +150,23 @@ async function processTag(tag, url, options, bundle, base = '') {
135
150
  }
136
151
  }
137
152
 
138
- if (bundleItem) {
139
- const source = bundleItem.type === 'chunk' ? bundleItem.code : bundleItem.source;
153
+ if (!bundleItem) {
154
+ log(`Available bundle keys:`, options);
155
+ Object.keys(bundle).forEach(key => log(`- ${key}`, options));
156
+ return tag;
157
+ }
158
+
159
+ log(`Bundle item type: ${bundleItem.type}`, options);
160
+
161
+ try {
162
+ if (bundleItem.type === 'chunk') {
163
+ source = bundleItem.code;
164
+ log(`Processing chunk content of length: ${source.length}`, options);
165
+ } else {
166
+ source = bundleItem.source;
167
+ log(`Processing asset content of length: ${source.length}`, options);
168
+ }
169
+
140
170
  const integrity = computeSri(source, options.algorithm);
141
171
  if (integrity) {
142
172
  log(`Computing SRI for local resource ${url}: ${integrity}`, options);
@@ -146,9 +176,8 @@ async function processTag(tag, url, options, bundle, base = '') {
146
176
  log(`New tag: ${newTag}`, options);
147
177
  return newTag;
148
178
  }
149
- } else {
150
- log(`No bundle item found for ${url}`, options);
151
- log(`Available bundle keys: ${Object.keys(bundle).join(', ')}`, options);
179
+ } catch (error) {
180
+ log(`Error processing bundle item: ${error}`, options);
152
181
  }
153
182
 
154
183
  return tag;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-sri4",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
4
4
  "description": "A Vite plugin to generate Subresource Integrity (SRI) hashes for output files.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",