@@ -16,9 +16,9 @@ Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) hashes.
16
16
* [ API] ( #api )
17
17
* Parsing & Serializing
18
18
* [ ` parse ` ] ( #parse )
19
+ * [ ` stringify ` ] ( #stringify )
19
20
* [ ` Integrity#concat ` ] ( #integrity-concat )
20
21
* [ ` Integrity#toString ` ] ( #integrity-to-string )
21
- * [ ` serialize ` ] ( #serialize )
22
22
* Integrity Generation
23
23
* [ ` fromData ` ] ( #from-data )
24
24
* [ ` fromStream ` ] ( #from-stream )
@@ -36,8 +36,8 @@ const integrity = 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xp
36
36
37
37
// Parsing and serializing
38
38
const parsed = ssri .parse (integrity)
39
+ ssri .stringify (parsed) // === integrity (works on non-Integrity objects)
39
40
parsed .toString () // === integrity
40
- ssri .serialize (parsed) // === integrity (works on non-Integrity objects)
41
41
42
42
// Async stream functions
43
43
ssri .checkStream (fs .createReadStream (' ./my-file' ), parsed).then (... )
@@ -101,6 +101,49 @@ browsers, or in other situations where strict adherence to the spec is needed.
101
101
ssri .parse (' sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==?foo' ) // -> Integrity
102
102
```
103
103
104
+ #### <a name =" stringify " ></a > ` > ssri.stringify(sri, [opts]) -> String `
105
+
106
+ This function is identical to [ ` Integrity#toString() ` ] ( #integrity-to-string ) ,
107
+ except it can be used on _ any_ object that [ ` parse ` ] ( #parse ) can handle -- that
108
+ is, a string, an ` IntegrityMetadata ` -like, or an ` Integrity ` -like.
109
+
110
+ The ` opts.sep ` option defines the string to use when joining multiple entries
111
+ together. To be spec-compliant, this _ must_ be whitespace. The default is a
112
+ single space (` ' ' ` ).
113
+
114
+ If ` opts.strict ` is true, the integrity string will be created using strict
115
+ parsing rules. See [ ` ssri.parse ` ] ( #parse ) .
116
+
117
+ ##### Example
118
+
119
+ ``` javascript
120
+ // Useful for cleaning up input SRI strings:
121
+ ssri .stringify (' \n\r sha512-foo\n\t\t sha384-bar' )
122
+ // -> 'sha512-foo sha384-bar'
123
+
124
+ // IntegrityMetadata-like: only a single entry.
125
+ ssri .stringify ({
126
+ algorithm: ' sha512' ,
127
+ digest: ' 9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==' ,
128
+ options: [' foo' ]
129
+ })
130
+ // ->
131
+ // 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==?foo'
132
+
133
+ // Integrity-like: full multi-entry syntax. Similar to output of `ssri.parse`
134
+ ssri .stringify ({
135
+ ' sha512' : [
136
+ {
137
+ algorithm: ' sha512' ,
138
+ digest: ' 9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==' ,
139
+ options: [' foo' ]
140
+ }
141
+ ]
142
+ })
143
+ // ->
144
+ // 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==?foo'
145
+ ```
146
+
104
147
#### <a name =" integrity-concat " ></a > ` > Integrity#concat(otherIntegrity, [opts]) -> Integrity `
105
148
106
149
Concatenates an ` Integrity ` object with another IntegrityLike, or a string
@@ -133,7 +176,7 @@ Returns the string representation of an `Integrity` object. All metadata entries
133
176
will be concatenated in the string by ` opts.sep ` , which defaults to ` ' ' ` .
134
177
135
178
If you want to serialize an object that didn't from from an ` ssri ` function,
136
- use [ ` ssri.serialize () ` ] ( #serialize ) .
179
+ use [ ` ssri.stringify () ` ] ( #stringify ) .
137
180
138
181
If ` opts.strict ` is true, the integrity string will be created using strict
139
182
parsing rules. See [ ` ssri.parse ` ] ( #parse ) .
@@ -146,45 +189,6 @@ const integrity = 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xp
146
189
ssri .parse (integrity).toString () === integrity
147
190
```
148
191
149
- #### <a name =" serialize " ></a > ` > ssri.serialize(sri, [opts]) -> String `
150
-
151
- This function is identical to [ ` Integrity#toString() ` ] ( #integrity-to-string ) ,
152
- except it can be used on _ any_ object that [ ` parse ` ] ( #parse ) can handle -- that
153
- is, a string, an ` IntegrityMetadata ` -like, or an ` Integrity ` -like.
154
-
155
- The ` opts.sep ` option defines the string to use when joining multiple entries
156
- together. To be spec-compliant, this _ must_ be whitespace. The default is a
157
- single space (` ' ' ` ).
158
-
159
- If ` opts.strict ` is true, the integrity string will be created using strict
160
- parsing rules. See [ ` ssri.parse ` ] ( #parse ) .
161
-
162
- ##### Example
163
-
164
- ``` javascript
165
- // IntegrityMetadata-like: only a single entry.
166
- ssri .serialize ({
167
- algorithm: ' sha512' ,
168
- digest: ' 9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==' ,
169
- options: [' foo' ]
170
- })
171
- // ->
172
- // 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==?foo'
173
-
174
- // Integrity-like: full multi-entry syntax. Similar to output of `ssri.parse`
175
- ssri .serialize ({
176
- ' sha512' : [
177
- {
178
- algorithm: ' sha512' ,
179
- digest: ' 9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==' ,
180
- options: [' foo' ]
181
- }
182
- ]
183
- })
184
- // ->
185
- // 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==?foo'
186
- ```
187
-
188
192
#### <a name =" from-data " ></a > ` > ssri.fromData(data, [opts]) -> Integrity `
189
193
190
194
Creates an ` Integrity ` object from either string or ` Buffer ` data, calculating
0 commit comments