forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.ts
160 lines (118 loc) · 3.33 KB
/
fetch.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
namespace $ {
export class $mol_fetch_response extends $mol_object2 {
constructor( readonly native : Response ) {
super()
}
status() {
const types = [ 'unknown', 'inform', 'success', 'redirect', 'wrong', 'failed' ] as const
return types[ Math.floor( this.native.status / 100 ) ]
}
code() {
return this.native.status
}
message() {
return this.native.statusText || `HTTP Error ${ this.code() }`
}
headers() {
return this.native.headers
}
mime() {
return this.headers().get( 'content-type' )
}
@ $mol_action
stream() {
return this.native.body
}
@ $mol_action
text() {
const buffer = this.buffer()
const native = this.native
const mime = native.headers.get( 'content-type' ) || ''
const [,charset] = /charset=(.*)/.exec( mime ) || [, 'utf-8']
const decoder = new TextDecoder( charset )
return decoder.decode( buffer )
}
json() {
return $mol_wire_sync( this.native ).json() as unknown
}
blob() {
return $mol_wire_sync( this.native ).blob()
}
buffer() {
return $mol_wire_sync( this.native ).arrayBuffer()
}
@ $mol_action
xml() {
return $mol_dom_parse( this.text() , 'application/xml' )
}
@ $mol_action
xhtml() {
return $mol_dom_parse( this.text() , 'application/xhtml+xml' )
}
@ $mol_action
html() {
return $mol_dom_parse( this.text() , 'text/html' )
}
}
export class $mol_fetch extends $mol_object2 {
static request( input : RequestInfo , init : RequestInit = {} ) {
const native = globalThis.fetch ?? $node['undici'].fetch
const controller = new AbortController()
let done = false
const promise = native( input , {
... init,
signal: controller!.signal,
} ).finally( ()=> {
done = true
} )
return Object.assign( promise, {
destructor: ()=> {
// Abort of done request breaks response parsing
if( !done && !controller.signal.aborted ) controller.abort()
},
} )
}
@ $mol_action
static response( input: RequestInfo, init?: RequestInit ) {
return new $mol_fetch_response( $mol_wire_sync( this ).request( input , init ) )
}
@ $mol_action
static success( input: RequestInfo, init?: RequestInit ) {
const response = this.response( input , init )
if( response.status() === 'success' ) return response
throw new Error( response.message() )
}
@ $mol_action
static stream( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).stream()
}
@ $mol_action
static text( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).text()
}
@ $mol_action
static json( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).json()
}
@ $mol_action
static blob( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).blob()
}
@ $mol_action
static buffer( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).buffer()
}
@ $mol_action
static xml( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).xml()
}
@ $mol_action
static xhtml( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).xhtml()
}
@ $mol_action
static html( input: RequestInfo, init?: RequestInit ) {
return this.success( input , init ).html()
}
}
}