-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
35 lines (33 loc) · 1.03 KB
/
parse.ts
File metadata and controls
35 lines (33 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @fileoverview SSRI parser — splits a `<algorithm>-<base64hash>`
* string into its component fields.
*/
import { ErrorCtor } from '../primordials/error'
/**
* Parse SSRI format into components.
*
* Extracts the algorithm and base64 hash from an SSRI string.
*
* @param ssri - Hash in SSRI format
* @returns Object with algorithm and base64Hash properties
* @throws Error if SSRI format is invalid
*
* @example
* ```typescript
* const { algorithm, base64Hash } = parseSsri('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=')
* // Returns: { algorithm: 'sha256', base64Hash: 'dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=' }
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function parseSsri(ssri: string): {
algorithm: string
base64Hash: string
} {
const match = /^([a-z0-9]+)-([A-Za-z0-9+/]+=*)$/i.exec(ssri)
if (!match || !match[1] || !match[2] || match[2].length < 2) {
throw new ErrorCtor(`Invalid SSRI format: ${ssri}`)
}
const algorithm = match[1]
const base64Hash = match[2]
return { algorithm, base64Hash }
}