@localnerve/csp-hashes 7.0.4 → 7.1.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/index.js +1 -1
- package/lib/index.js +39 -18
- package/package.json +1 -1
- package/readme.md +42 -0
package/index.js
CHANGED
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
* Copyright (c) 2022-2025 Alex Grant (@localnerve), LocalNerve LLC
|
|
9
9
|
* Licensed under the MIT license.
|
|
10
10
|
*/
|
|
11
|
-
export { hashstream as default, hashstream, removeCspMeta, createCspHash } from './lib/index.js';
|
|
11
|
+
export { hashstream as default, hashstream, removeCspMeta, createCspHash, cspHashes } from './lib/index.js';
|
package/lib/index.js
CHANGED
|
@@ -75,6 +75,44 @@ export function createCspHash(input, algo = 'sha256') {
|
|
|
75
75
|
return makeCspHash(algo, input);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/**
|
|
79
|
+
* cspHashes
|
|
80
|
+
* Accepts an algorithm and content buffer/string and returns a hashes object
|
|
81
|
+
* containing all CSP hashes for inline scripts and styles. This is useful for
|
|
82
|
+
* users who just have a few files to process and will handle I/O themselves,
|
|
83
|
+
* without needing a stream.
|
|
84
|
+
*
|
|
85
|
+
* @param {String} [algo] - hash algorithm, default sha256. Can be sha384, sha512.
|
|
86
|
+
* @param {Buffer|String} content - The HTML content to extract hashes from.
|
|
87
|
+
* @returns {Object} The hashes object with script and style elements and attributes.
|
|
88
|
+
*/
|
|
89
|
+
export function cspHashes (algo = 'sha256', content) {
|
|
90
|
+
if (!/^sha(256|384|512)$/.test(algo)) {
|
|
91
|
+
throw new Error('algo option must be one of "sha256", "sha384", or "sha512" only.');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const hashes = {
|
|
95
|
+
script: {
|
|
96
|
+
elements: [],
|
|
97
|
+
attributes: [],
|
|
98
|
+
get all () {
|
|
99
|
+
return this.elements.concat(this.attributes);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
style: {
|
|
103
|
+
elements: [],
|
|
104
|
+
attributes: [],
|
|
105
|
+
get all () {
|
|
106
|
+
return this.elements.concat(this.attributes);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
collectHashes(makeCspHash.bind(null, algo), content, hashes);
|
|
112
|
+
|
|
113
|
+
return hashes;
|
|
114
|
+
}
|
|
115
|
+
|
|
78
116
|
/**
|
|
79
117
|
* hashstream
|
|
80
118
|
* Accepts the processing options and returns a Transform object stream.
|
|
@@ -105,24 +143,7 @@ export function hashstream ({
|
|
|
105
143
|
const path = file.path;
|
|
106
144
|
const content = file.contents;
|
|
107
145
|
|
|
108
|
-
const hashes =
|
|
109
|
-
script: {
|
|
110
|
-
elements: [],
|
|
111
|
-
attributes: [],
|
|
112
|
-
get all () {
|
|
113
|
-
return this.elements.concat(this.attributes);
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
style: {
|
|
117
|
-
elements: [],
|
|
118
|
-
attributes: [],
|
|
119
|
-
get all () {
|
|
120
|
-
return this.elements.concat(this.attributes);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
collectHashes(makeCspHash.bind(null, algo), content, hashes);
|
|
146
|
+
const hashes = cspHashes(algo, content);
|
|
126
147
|
|
|
127
148
|
if (replace) {
|
|
128
149
|
const s = callback(path, hashes, content.toString());
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
- [Breaking Changes](#breaking-changes)
|
|
14
14
|
- [API](#api)
|
|
15
15
|
- [hashstream (also the default export)](#hashstream-also-the-default-export)
|
|
16
|
+
- [cspHashes](#csphashes)
|
|
16
17
|
- [createCspHash](#createcsphash)
|
|
17
18
|
- [removeCspMeta](#removecspmeta)
|
|
18
19
|
- [Hashstream Options](#hashstream-options)
|
|
@@ -49,6 +50,33 @@ Stream hashstream ({
|
|
|
49
50
|
|
|
50
51
|
See [`hashstream options`](#hashstream-options) for a detailed explanation of the input options.
|
|
51
52
|
|
|
53
|
+
### cspHashes
|
|
54
|
+
This library exports a convenience function that takes an algorithm and HTML content, and returns a hashes object containing all CSP hashes for inline scripts and styles. This is useful for users who just have a few files to process and will handle I/O themselves, without needing a stream.
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
Object cspHashes(algo = 'sha256', content)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
+ {String} **\[algo\]** - Optional - Defaults to `'sha256'`, can be one of 'sha256', 'sha384' or 'sha512'.
|
|
61
|
+
+ {Buffer|String} **content** - Required - The HTML content to extract hashes from.
|
|
62
|
+
|
|
63
|
+
Returns a [`hashes` object](#callback-hashes-object) with the same structure as the `hashstream` callback receives:
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
{
|
|
67
|
+
script: {
|
|
68
|
+
elements: [],
|
|
69
|
+
attributes: [],
|
|
70
|
+
get all () { return this.elements.concat(this.attributes); }
|
|
71
|
+
},
|
|
72
|
+
style: {
|
|
73
|
+
elements: [],
|
|
74
|
+
attributes: [],
|
|
75
|
+
get all () { return this.elements.concat(this.attributes); }
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
52
80
|
### createCspHash
|
|
53
81
|
This library exports the helper method it uses to make CSP formatted hashes. This is useful if you have a picece of code you need to hash and place into your hash list outside the scope of the page as rendered.
|
|
54
82
|
|
|
@@ -179,6 +207,20 @@ export function stripCspMetaContents (settings) {
|
|
|
179
207
|
}
|
|
180
208
|
```
|
|
181
209
|
|
|
210
|
+
### Simple File Processing (No Stream Required)
|
|
211
|
+
If you just have a few files to process and want to handle I/O yourself, use `cspHashes` instead of `hashstream`:
|
|
212
|
+
|
|
213
|
+
```javascript
|
|
214
|
+
import fs from 'node:fs';
|
|
215
|
+
import { cspHashes } from '@localnerve/csp-hashes';
|
|
216
|
+
|
|
217
|
+
const html = fs.readFileSync('./dist/index.html', { encoding: 'utf8' });
|
|
218
|
+
const hashes = cspHashes('sha256', html);
|
|
219
|
+
|
|
220
|
+
console.log('Script element hashes:', hashes.script.elements.join(' '));
|
|
221
|
+
console.log('Style attribute hashes:', hashes.style.attributes.join(' '));
|
|
222
|
+
```
|
|
223
|
+
|
|
182
224
|
### Native Node.js Streams (No Gulp/Vinyl Required)
|
|
183
225
|
You can use `hashstream` directly with native Node.js streams by wrapping raw file buffers into plain file-like objects. This example reads HTML files from a source directory, processes them through `hashstream`, and writes the output to a destination:
|
|
184
226
|
|