Skip to content

Commit e683112

Browse files
save file
1 parent 853edcb commit e683112

File tree

1 file changed

+36
-11
lines changed
  • blog/25-07-31/fetch-download-stream/ex

1 file changed

+36
-11
lines changed

blog/25-07-31/fetch-download-stream/ex/ex.html

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,44 @@
6262
(async()=>{
6363
console.clear();
6464

65-
var $ = (root,sel)=>(!sel && (sel=root,root=document),root.querySelector(sel));
66-
65+
var $ = (root,sel)=>(!sel && (sel=root,root=document),root.querySelector(sel));
66+
var datatype = v=>Object.prototype.toString.call(v).slice(8,-1).toLowerCase();
67+
6768

68-
var url = 'https://raw.githubusercontent.com/javascript-2020/external/main/ffmpeg/ffmpeg-wasm/ffmpeg-wasm.zip'
69-
// var url = 'https://bgrins.github.io/videoconverter.js/build/ffmpeg-all-codecs.js';
69+
var url = 'https://raw.githubusercontent.com/javascript-2020/external/main/ffmpeg/ffmpeg-wasm/ffmpeg-wasm.zip'
70+
// var url = 'https://bgrins.github.io/videoconverter.js/build/ffmpeg-all-codecs.js';
7071

71-
var i = url.lastIndexOf('/')+1
72-
var fn = url.slice(i);
72+
var i = url.lastIndexOf('/')+1
73+
var fn = url.slice(i);
74+
7375

74-
var headers = {};
75-
var method = 'get';
76-
var body = undefined;
76+
var headers = {};
77+
var method = 'get';
78+
var body = undefined;
7779

78-
var res = await fetch(url,{headers,method,body});
80+
var err;
81+
try{
82+
83+
var res = await fetch(url,{headers,method,body});
84+
85+
}//try
86+
catch(err2){
87+
88+
err = err2;
89+
90+
}//catch
91+
if(err){
92+
var error = err.toString();
93+
console.error(error);
94+
return;
95+
}
96+
if(!res.ok){
97+
var txt = await res.text();
98+
var error = `failed to fetch : $(res.status} ${res.statusText}`;
99+
console.error(error);
100+
return;
101+
}
102+
79103

80104
var encoding = res.headers.get('content-encoding');
81105
console.log('content-encoding',encoding);
@@ -108,6 +132,7 @@
108132

109133
const {value,done} = await reader.read();
110134
if(done)break;
135+
111136
total += value.length;
112137
chunks.push(value);
113138

@@ -116,6 +141,7 @@
116141
$('#bar').style.width = w;
117142
console.log('chunk',datatype(value),value.byteLength,total,w);
118143
}
144+
119145
}//while
120146

121147
var total = chunks.reduce((acc,chunk)=>acc+chunk.length,0);
@@ -137,7 +163,6 @@
137163

138164
$('#bar').classList.remove('unknown');
139165

140-
function datatype(v){return Object.prototype.toString.call(v).slice(8,-1).toLowerCase()}
141166

142167
})();
143168

0 commit comments

Comments
 (0)