forked from unjs/unenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_mime.ts
47 lines (39 loc) · 1.15 KB
/
_mime.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
import type {
MIMEType as MIMETypeT,
MIMEParams as MIMEParamsT,
} from "node:util";
// https://nodejs.org/api/util.html#class-utilmimetype
export class MIMEType implements MIMETypeT {
params = new MIMEParams();
type: string;
subtype: string;
constructor(input: string | { toString: () => string }) {
const [essense = "", ...params] = String(input).split(";");
const [type = "", subtype = ""] = essense.split("/");
this.type = type;
this.subtype = subtype;
this.params = new MIMEParams();
for (const param of params) {
const [name, value] = param.split("=");
this.params.set(name, value);
}
}
get essence() {
return this.type + "/" + this.subtype;
}
toString() {
const paramsStr = this.params.toString();
return this.essence + (paramsStr ? `;${paramsStr}` : "");
}
}
// https://nodejs.org/api/util.html#util_class_util_mimeparams
export class MIMEParams extends Map<string, string> implements MIMEParamsT {
get(name: string) {
return (super.get(name) || null) as any;
}
toString() {
return [...this.entries()]
.map(([name, value]) => `${name}=${value}`)
.join("&");
}
}