Skip to content

Commit a1e822c

Browse files
Braedon SaundersBraedon Saunders
authored andcommitted
Time out tree-sitter CDN fetches to fix offline hang
Closes #51 When the network is fully offline, the <script src=cdn.jsdelivr.net> tag for web-tree-sitter fails fast and TreeSitter is undefined, so initTreeSitter() returns null immediately and analysis falls back to the regex tokenizer. But on partially offline / flaky networks the runtime script can load from cache while subsequent wasm fetches hang indefinitely. TreeSitter.init() and Language.load() had no timeout, so analysis stalled forever on "Loading language parsers...". Wrap both calls in a Promise.race against an 8s timeout. A timeout rejects into the existing catch block, which returns null and lets the regex parser take over.
1 parent 3ad51ba commit a1e822c

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,16 +791,23 @@
791791
lua:{grammar:'lua',exts:['.lua'],coverage:'available'},
792792
bash:{grammar:'bash',exts:['.sh','.bash','.zsh','.fish'],coverage:'available'}
793793
},
794+
treeSitterFetchTimeoutMs:8000,
795+
_withTimeout:function(promise,ms){
796+
return Promise.race([
797+
promise,
798+
new Promise(function(_,reject){setTimeout(function(){reject(new Error('tree-sitter fetch timed out'));},ms);})
799+
]);
800+
},
794801
initTreeSitter:async function(){
795802
if(this._tsRuntimePromise)return this._tsRuntimePromise;
796803
this._tsRuntimePromise=(async()=>{
797804
if(typeof TreeSitter==='undefined')return null;
798805
try{
799-
await TreeSitter.init({
806+
await Parser._withTimeout(TreeSitter.init({
800807
locateFile:function(scriptName){
801808
return 'https://cdn.jsdelivr.net/npm/web-tree-sitter@0.20.8/'+scriptName;
802809
}
803-
});
810+
}),Parser.treeSitterFetchTimeoutMs);
804811
return TreeSitter;
805812
}catch(e){
806813
return null;
@@ -826,7 +833,7 @@
826833
var runtime=await Parser.initTreeSitter();
827834
if(!runtime)return null;
828835
try{
829-
var lang=await runtime.Language.load(Parser.treeSitterWasmBase+'tree-sitter-'+config.grammar+'.wasm');
836+
var lang=await Parser._withTimeout(runtime.Language.load(Parser.treeSitterWasmBase+'tree-sitter-'+config.grammar+'.wasm'),Parser.treeSitterFetchTimeoutMs);
830837
var parser=new runtime();
831838
parser.setLanguage(lang);
832839
Parser._tsLanguages[config.grammar]=lang;

0 commit comments

Comments
 (0)