Skip to content

Commit 4acad30

Browse files
committed
feat(stringify): replaced unparse/serialize with stringify
BREAKING CHANGE: serialize and unparse have been removed. Use ssri.stringify instead.
1 parent bb8e9e7 commit 4acad30

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

README.md

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) hashes.
1616
* [API](#api)
1717
* Parsing & Serializing
1818
* [`parse`](#parse)
19+
* [`stringify`](#stringify)
1920
* [`Integrity#concat`](#integrity-concat)
2021
* [`Integrity#toString`](#integrity-to-string)
21-
* [`serialize`](#serialize)
2222
* Integrity Generation
2323
* [`fromData`](#from-data)
2424
* [`fromStream`](#from-stream)
@@ -36,8 +36,8 @@ const integrity = 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xp
3636

3737
// Parsing and serializing
3838
const parsed = ssri.parse(integrity)
39+
ssri.stringify(parsed) // === integrity (works on non-Integrity objects)
3940
parsed.toString() // === integrity
40-
ssri.serialize(parsed) // === integrity (works on non-Integrity objects)
4141

4242
// Async stream functions
4343
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.
101101
ssri.parse('sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xpp71j/r/pAe1DM+JI/A+line3jUBgzQ7A==?foo') // -> Integrity
102102
```
103103

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\rsha512-foo\n\t\tsha384-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+
104147
#### <a name="integrity-concat"></a> `> Integrity#concat(otherIntegrity, [opts]) -> Integrity`
105148

106149
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
133176
will be concatenated in the string by `opts.sep`, which defaults to `' '`.
134177

135178
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).
137180

138181
If `opts.strict` is true, the integrity string will be created using strict
139182
parsing rules. See [`ssri.parse`](#parse).
@@ -146,45 +189,6 @@ const integrity = 'sha512-9KhgCRIx/AmzC8xqYJTZRrnO8OW2Pxyl2DIMZSBOr0oDvtEFyht3xp
146189
ssri.parse(integrity).toString() === integrity
147190
```
148191

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-
188192
#### <a name="from-data"></a> `> ssri.fromData(data, [opts]) -> Integrity`
189193

190194
Creates an `Integrity` object from either string or `Buffer` data, calculating

index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Integrity {
7474
concat (integrity, opts) {
7575
const other = typeof integrity === 'string'
7676
? integrity
77-
: serialize(integrity)
77+
: stringify(integrity, opts)
7878
return parse(`${this.toString()} ${other}`, opts)
7979
}
8080
}
@@ -87,9 +87,9 @@ function parse (sri, opts) {
8787
} else if (sri.algorithm && sri.digest) {
8888
const fullSri = new Integrity()
8989
fullSri[sri.algorithm] = [sri]
90-
return _parse(serialize(fullSri, opts), opts)
90+
return _parse(stringify(fullSri, opts), opts)
9191
} else {
92-
return _parse(serialize(sri, opts), opts)
92+
return _parse(stringify(sri, opts), opts)
9393
}
9494
}
9595

@@ -107,11 +107,12 @@ function _parse (integrity, opts) {
107107
}, new Integrity())
108108
}
109109

110-
module.exports.serialize = serialize
111-
module.exports.unparse = serialize
112-
function serialize (obj, opts) {
110+
module.exports.stringify = stringify
111+
function stringify (obj, opts) {
113112
if (obj.algorithm && obj.digest) {
114113
return IntegrityMetadata.prototype.toString.call(obj, opts)
114+
} else if (typeof obj === 'string') {
115+
return stringify(parse(obj, opts), opts)
115116
} else {
116117
return Integrity.prototype.toString.call(obj, opts)
117118
}

0 commit comments

Comments
 (0)