@dintero/sri-to-dist 1.0.7 → 1.0.9

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/lib.d.ts CHANGED
@@ -27,5 +27,5 @@ declare const readLocalContent: (src: string, baseDir: string, baseUrl: string)
27
27
  declare const calculateSha384: (content: Buffer) => string;
28
28
  declare const handleUpdatedHtml: (stdout: NodeJS.WriteStream, outputPath?: string, updatedHtml?: string) => void;
29
29
  declare const toSriScriptTag: (tag: string, integrity: string) => string;
30
- declare const ensureCrossoriginAnonymous: (tag: string) => string;
31
- export { extractLinkRel, isSriTag, toHtmlWithSri, getContent, readLocalContent, fetchRemoteContent, calculateSha384, ensureCrossoriginAnonymous, toSriScriptTag, handleUpdatedHtml, };
30
+ declare const alterTag: (tag: string, param: "crossorigin" | "integrity", value: string) => string;
31
+ export { extractLinkRel, isSriTag, toHtmlWithSri, getContent, readLocalContent, fetchRemoteContent, calculateSha384, toSriScriptTag, handleUpdatedHtml, alterTag };
package/dist/lib.js CHANGED
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.handleUpdatedHtml = exports.toSriScriptTag = exports.ensureCrossoriginAnonymous = exports.calculateSha384 = exports.fetchRemoteContent = exports.readLocalContent = exports.getContent = exports.toHtmlWithSri = exports.isSriTag = exports.extractLinkRel = exports.toSriImportMap = exports.extractImports = exports.parseImportMap = exports.isImportMapTag = void 0;
36
+ exports.alterTag = exports.handleUpdatedHtml = exports.toSriScriptTag = exports.calculateSha384 = exports.fetchRemoteContent = exports.readLocalContent = exports.getContent = exports.toHtmlWithSri = exports.isSriTag = exports.extractLinkRel = exports.toSriImportMap = exports.extractImports = exports.parseImportMap = exports.isImportMapTag = void 0;
37
37
  const fs = __importStar(require("node:fs"));
38
38
  const path = __importStar(require("node:path"));
39
39
  const node_crypto_1 = require("node:crypto");
@@ -298,35 +298,29 @@ const handleUpdatedHtml = (stdout, outputPath, updatedHtml) => {
298
298
  exports.handleUpdatedHtml = handleUpdatedHtml;
299
299
  const toSriScriptTag = (tag, integrity) => {
300
300
  // First handle the integrity attribute
301
- let newTag = tag;
302
- if (tag.includes("integrity=")) {
303
- // Replace existing integrity attribute
304
- const reIntegrity = /integrity="[^"]*"/g;
305
- newTag = newTag.replace(reIntegrity, `integrity="${integrity}"`);
306
- }
307
- else {
308
- // Add integrity attribute
309
- if (tag.endsWith("/>")) {
310
- newTag = newTag.replace(/\/>$/, ` integrity="${integrity}"/>`);
311
- }
312
- else {
313
- newTag = newTag.replace(/>$/, ` integrity="${integrity}">`);
314
- }
315
- }
301
+ const integrityTag = alterTag(tag, "integrity", integrity);
316
302
  // Ensure crossorigin attribute is set
317
- return ensureCrossoriginAnonymous(newTag);
303
+ return alterTag(integrityTag, "crossorigin", "anonymous");
318
304
  };
319
305
  exports.toSriScriptTag = toSriScriptTag;
320
- const ensureCrossoriginAnonymous = (tag) => {
321
- // Replace if exists
322
- if (tag.includes("crossorigin=")) {
323
- const reCrossorigin = /crossorigin="[^"]*"/g;
324
- return tag.replace(reCrossorigin, 'crossorigin="anonymous"');
306
+ const alterTag = (tag, param, value) => {
307
+ const hasParamRegex = new RegExp(`^<[^>]*\\s+${param}="[^"]*"`);
308
+ const keyValue = `${param}="${value}"`;
309
+ if (hasParamRegex.test(tag)) {
310
+ // Replace param with new value if existing param is found in tag
311
+ const replaceParamRegex = new RegExp(`${param}="[^"]*"`);
312
+ return tag.replace(replaceParamRegex, keyValue);
313
+ }
314
+ const hasClosingTag = /<\/script[^>]*>$|<\/link[^>]*>$/.test(tag);
315
+ if (hasClosingTag) {
316
+ // Add param and value to the opening tag
317
+ return tag.replace(/>(?!$)/, ` ${keyValue}>`);
325
318
  }
326
- // Add crossorigin="anonymous" attribute if missing from tag
327
319
  if (tag.endsWith("/>")) {
328
- return tag.replace(/\/>$/, ' crossorigin="anonymous"/>');
320
+ // Add param and value to self closing tag
321
+ return tag.replace(/\/>$/, ` ${keyValue}/>`);
329
322
  }
330
- return tag.replace(/>$/, ' crossorigin="anonymous">');
323
+ // No close tag and no self closing tag, add param and value to end of tag
324
+ return tag.replace(/>$/, ` ${keyValue}>`);
331
325
  };
332
- exports.ensureCrossoriginAnonymous = ensureCrossoriginAnonymous;
326
+ exports.alterTag = alterTag;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dintero/sri-to-dist",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "HTML tool for adding subresource integrity hashes",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,15 +33,15 @@
33
33
  "license": "MIT",
34
34
  "devDependencies": {
35
35
  "@biomejs/biome": "1.9.4",
36
- "@types/node": "22.15.2",
36
+ "@types/node": "22.15.19",
37
37
  "@types/temp": "0.9.4",
38
- "semantic-release": "24.2.3",
38
+ "semantic-release": "24.2.4",
39
39
  "temp": "0.9.4",
40
40
  "typescript": "5.8.3",
41
- "vitest": "3.1.2"
41
+ "vitest": "3.1.3"
42
42
  },
43
43
  "dependencies": {
44
- "commander": "13.1.0"
44
+ "commander": "14.0.0"
45
45
  },
46
46
  "bugs": {
47
47
  "url": "https://github.com/Dintero/sri-to-dist/issues"