11import fs from 'fs' ;
22import fetch from 'node-fetch' ;
3- import * as cheerio from 'cheerio' ;
43import decompress from 'decompress' ;
54
65async function getSqliteWasmDownloadLink ( ) {
76 const response = await fetch ( 'https://sqlite.org/download.html' ) ;
87 const html = await response . text ( ) ;
9- const $ = cheerio . load ( html ) ;
10- const fileName = $ ( 'a[name="wasm"]' ) . closest ( 'tr' ) . next ( ) . find ( 'a' ) . text ( ) ;
11- const sqliteWasmLink = `https://sqlite.org/${ new Date ( ) . getFullYear ( ) } /${ fileName } ` ;
8+ const sqliteWasmLink =
9+ 'https://sqlite.org/' +
10+ html
11+ . replace (
12+ / ^ .* ?< ! - - D o w n l o a d p r o d u c t d a t a f o r s c r i p t s t o r e a d ( .* ?) - - > .* ?$ / gms,
13+ '$1' ,
14+ )
15+ . split ( / \n / )
16+ . filter ( ( row ) => / s q l i t e - w a s m / . test ( row ) ) [ 0 ]
17+ . split ( / , / ) [ 2 ] ;
1218 console . log ( `Found SQLite Wasm download link: ${ sqliteWasmLink } ` ) ;
1319 return sqliteWasmLink ;
1420}
@@ -19,6 +25,11 @@ async function downloadAndUnzipSqliteWasm(sqliteWasmDownloadLink) {
1925 }
2026 console . log ( 'Downloading and unzipping SQLite Wasm...' ) ;
2127 const response = await fetch ( sqliteWasmDownloadLink ) ;
28+ if ( ! response . ok || response . status !== 200 ) {
29+ throw new Error (
30+ `Unable to download SQLite Wasm from ${ sqliteWasmDownloadLink } ` ,
31+ ) ;
32+ }
2233 const buffer = await response . arrayBuffer ( ) ;
2334 fs . writeFileSync ( 'sqlite-wasm.zip' , Buffer . from ( buffer ) ) ;
2435 const files = await decompress ( 'sqlite-wasm.zip' , 'sqlite-wasm' , {
@@ -35,9 +46,9 @@ async function downloadAndUnzipSqliteWasm(sqliteWasmDownloadLink) {
3546}
3647
3748async function main ( ) {
38- const sqliteWasmLink = await getSqliteWasmDownloadLink ( ) ;
39- await downloadAndUnzipSqliteWasm ( sqliteWasmLink ) ;
4049 try {
50+ const sqliteWasmLink = await getSqliteWasmDownloadLink ( ) ;
51+ await downloadAndUnzipSqliteWasm ( sqliteWasmLink ) ;
4152 fs . copyFileSync (
4253 './node_modules/module-workers-polyfill/module-workers-polyfill.min.js' ,
4354 './demo/module-workers-polyfill.min.js' ,
0 commit comments