-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
162 lines (132 loc) · 3.89 KB
/
mod.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*!
* Based on https://github.com/jshttp/content-type/blob/master/index.js
* Copyright(c) 2015 Douglas Christopher Wilson
* Copyright(c) 2020 Henry Zhuang
* MIT Licensed
*/
export type Parameters = { [key: string]: string };
export interface ContentType {
type: string;
parameters?: Parameters;
}
class ContentTypeImpl implements ContentType {
constructor(type: string) {
this.type = type;
this.parameters = Object.create(null);
}
type: string;
parameters: Parameters;
}
/**
* RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1
*
* parameter = token "=" ( token / quoted-string )
* token = 1*tchar
* tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
* / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
* / DIGIT / ALPHA
* ; any VCHAR, except delimiters
* quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
* qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
* obs-text = %x80-FF
* quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
*/
const PARAM_REGEXP =
/; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g;
const TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/;
const TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
/**
* RegExp to match quoted-pair in RFC 7230 sec 3.2.6
*
* quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
* obs-text = %x80-FF
*/
const QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g;
/**
* RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6
*/
const QUOTE_REGEXP = /([\\"])/g;
/**
* RegExp to match type in RFC 7231 sec 3.1.1.1
*
* media-type = type "/" subtype
* type = token
* subtype = token
*/
const TYPE_REGEXP =
/^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
/**
* Format ContentType object to media type.
*/
export function format(obj: ContentType): string {
const parameters = obj.parameters;
const type = obj.type;
if (!type || !TYPE_REGEXP.test(type)) {
throw new TypeError("invalid type");
}
let string = type;
// append parameters
if (parameters && typeof parameters === "object") {
let param;
const params = Object.keys(parameters).sort();
for (let i = 0; i < params.length; i++) {
param = params[i];
if (!TOKEN_REGEXP.test(param)) {
throw new TypeError("invalid parameter name");
}
string += "; " + param + "=" + qstring(parameters[param]);
}
}
return string;
}
/**
* Parse media type to object.
*/
export function parse(str: string): ContentType {
let index = str.indexOf(";");
const type = index !== -1 ? str.substr(0, index).trim() : str.trim();
if (!TYPE_REGEXP.test(type)) {
throw new TypeError("invalid media type");
}
const obj = new ContentTypeImpl(type.toLowerCase());
// parse parameters
if (index !== -1) {
let key;
let match;
let value;
PARAM_REGEXP.lastIndex = index;
while ((match = PARAM_REGEXP.exec(str))) {
if (match.index !== index) {
throw new TypeError("invalid parameter format");
}
index += match[0].length;
key = match[1].toLowerCase();
value = match[2];
if (value[0] === '"') {
// remove quotes and escapes
value = value
.substr(1, value.length - 2)
.replace(QESC_REGEXP, "$1");
}
obj.parameters[key] = value;
}
if (index !== str.length) {
throw new TypeError("invalid parameter format");
}
}
return obj;
}
/**
* Quote a string if necessary.
*/
function qstring(val: string): string {
const str = String(val);
// no need to quote tokens
if (TOKEN_REGEXP.test(str)) {
return str;
}
if (str.length > 0 && !TEXT_REGEXP.test(str)) {
throw new TypeError("invalid parameter value");
}
return '"' + str.replace(QUOTE_REGEXP, "\\$1") + '"';
}