77 isString ,
88 isToken ,
99 parseListFields ,
10+ type QuotedString ,
11+ type Token ,
1012 toLowerCase ,
1113} from "./deps.ts" ;
1214import { Msg } from "./constants.ts" ;
@@ -64,37 +66,20 @@ export function parseAuthorization(input: string): Authorization {
6466 return { authScheme, params } ;
6567}
6668
67- /** Generate from _abnf.ts. */
68- const reAuthParam =
69- / ^ (?< key > (? = ( [ \w ! # $ % & ' * + . ^ ` | ~ - ] + ) ) \2) [ \t ] * = [ \t ] * (?: (?< token > (? = ( [ \w ! # $ % & ' * + . ^ ` | ~ - ] + ) ) \4) | (?< quotedString > " (? = ( (?: \t | | ! | [ \x23 - \x5B \x5D - \x7E ] | [ \x80 - \xFF ] | \\ (?: \t | | [ \x21 - \x7E ] | [ \x80 - \xFF ] ) ) * ) ) \6" ) ) $ / ;
70-
7169type AuthParamGroups =
7270 & { key : string }
7371 & ( { token : string ; quotedString : never } | {
7472 token : never ;
7573 quotedString : string ;
7674 } ) ;
7775
78- /** Parse string into {@link AuthParam }.
76+ /** Parse string into {@link AuthParams }.
7977 * @throws {SyntaxError } It the input is invalid [auth-param](https://www.rfc-editor.org/rfc/rfc9110.html#section-11.2-5).
8078 * @throws {Error } If the auth param key is duplicated.
8179 */
8280export function parseAuthParams ( input : string ) : AuthParams {
8381 const list = parseListFields ( input ) ;
84-
85- const entries = list . map ( ( el ) => {
86- const result = reAuthParam . exec ( el ) ;
87-
88- if ( ! result || ! result . groups ) throw new SyntaxError ( Msg . InvalidSyntax ) ;
89-
90- const groups = result . groups as AuthParamGroups ;
91- const value = isString ( groups . token )
92- ? groups . token
93- : groups . quotedString . replace ( / \\ ( .) / g, "$1" ) ;
94-
95- return [ groups . key , value ] as const ;
96- } ) ;
97-
82+ const entries = list . map ( parseAuthParam ) ;
9883 const duplicates = duplicate (
9984 entries
10085 . map < string > ( head )
@@ -105,3 +90,25 @@ export function parseAuthParams(input: string): AuthParams {
10590
10691 return Object . fromEntries ( entries ) ;
10792}
93+
94+ type AuthParam = [ key : string , value : Token | QuotedString ] ;
95+
96+ /** Generate from _abnf.ts. */
97+ const reAuthParam =
98+ / ^ (?< key > [ \w ! # $ % & ' * + . ^ ` | ~ - ] + ?) [ \t ] * ?= [ \t ] * ?(?: (?< token > [ \w ! # $ % & ' * + . ^ ` | ~ - ] + ?) | (?< quotedString > " (?: \t | | ! | [ \x23 - \x5B \x5D - \x7E ] | [ \x80 - \xFF ] | \\ (?: \t | | [ \x21 - \x7E ] | [ \x80 - \xFF ] ) ) * ?" ) ) $ / ;
99+
100+ /** Parse string into {@link AuthParam}.
101+ * @throws {SyntaxError } It the input is invalid [auth-param](https://www.rfc-editor.org/rfc/rfc9110.html#section-11.2-5).
102+ */
103+ function parseAuthParam ( input : string ) : AuthParam {
104+ const result = reAuthParam . exec ( input ) ;
105+
106+ if ( ! result || ! result . groups ) throw new SyntaxError ( Msg . InvalidSyntax ) ;
107+
108+ const groups = result . groups as AuthParamGroups ;
109+ const value = isString ( groups . token )
110+ ? groups . token as Token
111+ : groups . quotedString . replace ( / \\ ( .) / g, "$1" ) as QuotedString ;
112+
113+ return [ groups . key as Token , value ] ;
114+ }
0 commit comments