11import { ModInstaller } from './mod-installer'
22import { ModDB } from './moddb'
33import { ModEntry , ModImageConfig as ModIconConfig , NPDatabase } from './types'
4-
5- const fs : typeof import ( 'fs' ) = ( 0 , eval ) ( "require('fs')" )
64import type { IncomingMessage } from 'http'
7- const http : typeof import ( 'http' ) = ( 0 , eval ) ( "require('http')" )
8- const https : typeof import ( 'https' ) = ( 0 , eval ) ( "require('https')" )
5+
6+ const fs : typeof import ( 'fs' ) = window . require ?.( 'fs' )
7+ const http : typeof import ( 'http' ) = window . require ?.( 'http' )
8+ const https : typeof import ( 'https' ) = window . require ?.( 'https' )
99
1010async function * getFilesRecursive ( dir : string ) : AsyncIterable < string > {
1111 const dirents = await fs . promises . readdir ( dir , { withFileTypes : true } )
@@ -32,7 +32,8 @@ function getTag(head: IncomingMessage): string {
3232
3333async function getETag ( url : string ) : Promise < string > {
3434 const uri = new URL ( url )
35- const { get } = uri . protocol === 'https:' ? https : http
35+ const lib = uri . protocol === 'https:' ? https : http
36+ const get = lib ?. get
3637 if ( ! get ) return 'nointernet'
3738
3839 const head : IncomingMessage | undefined = await new Promise ( resolve =>
@@ -65,15 +66,17 @@ export class FileCache {
6566
6667 static async init ( ) {
6768 this . cacheDir = './assets/mod-data/CCModManager/cache'
68- await fs . promises . mkdir ( `${ this . cacheDir } ` , { recursive : true } )
6969
7070 this . inCache = new Set ( )
71- for await ( const path of getFilesRecursive ( this . cacheDir ) )
71+ if ( ! fs ) return
72+
73+ await fs . promises . mkdir ( `${ this . cacheDir } ` , { recursive : true } )
74+ for await ( const path of getFilesRecursive ( this . cacheDir ) )
7275 this . inCache . add ( path . substring ( './assets/mod-data/CCModManager/cache/' . length ) )
7376 }
7477
7578 static prepareDatabase ( name : string ) {
76- fs . promises . mkdir ( `${ this . cacheDir } /${ name } /icons` , { recursive : true } )
79+ fs ? .promises . mkdir ( `${ this . cacheDir } /${ name } /icons` , { recursive : true } )
7780 }
7881
7982 static async getIconConfig ( mod : ModEntry ) : Promise < ModIconConfig > {
0 commit comments