Skip to content

Commit d9264ee

Browse files
committed
Improve browser compatibility
1 parent 39419c8 commit d9264ee

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# Change Log
44

55
## [Unreleased]
6+
7+
### Fixed
8+
9+
- Improve browser compatibility
10+
611
## [1.0.4] 2025-06-26
712

813
### Added

src/cache.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { ModInstaller } from './mod-installer'
22
import { ModDB } from './moddb'
33
import { ModEntry, ModImageConfig as ModIconConfig, NPDatabase } from './types'
4-
5-
const fs: typeof import('fs') = (0, eval)("require('fs')")
64
import 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

1010
async 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

3333
async 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> {

src/mod-installer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { ModDB } from './moddb'
33
import { ModEntry, ModEntryLocal, ModEntryLocalVirtual, ModEntryServer } from './types'
44
import { ModInstallDialogs, prepareModName } from './gui/install-dialogs'
55

6-
const fs: typeof import('fs') = (0, eval)("require('fs')")
7-
const path: typeof import('path') = (0, eval)("require('path')")
8-
const crypto: typeof import('crypto') = (0, eval)('require("crypto")')
6+
const fs: typeof import('fs') = window.require?.('fs')
7+
const path: typeof import('path') = window.require?.('path')
8+
const crypto: typeof import('crypto') = window.require?.('crypto')
99

1010
import { Opts } from './options'
1111
import { JSZip, semver } from './library-providers'
@@ -505,7 +505,7 @@ export class ModInstaller {
505505
}
506506

507507
static async isDirGit(dirPath: string): Promise<boolean> {
508-
if (!dirPath.trim()) return false
508+
if (!dirPath.trim() || !fs) return false
509509
const stat = await fs.promises.stat(dirPath)
510510
if (!stat.isDirectory()) return false
511511
return await this.fileExists(path.join(dirPath, '.git'))
@@ -534,8 +534,8 @@ export class ModInstaller {
534534
}
535535

536536
static restartGame() {
537-
if ('chrome' in window) {
538-
;(window.chrome as any).runtime.reload()
537+
if (window.chrome?.runtime?.reload) {
538+
window.chrome.runtime.reload()
539539
} else {
540540
window.location.reload()
541541
}

src/moddb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class ModDB {
9090
await moddb.getMods(() => {})
9191
modRecord = moddb.modRecord
9292
}
93-
if (!modRecord) throw new Error('wat?')
93+
if (!modRecord) continue
9494

9595
const dbMod = modRecord[id]
9696
if (dbMod) matches.push(dbMod)

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,8 @@ export type ModImageConfig = {
116116
declare global {
117117
/* hack to mute a random rimraf error :) */
118118
type StringDecoder = {}
119+
120+
interface Window {
121+
chrome: any
122+
}
119123
}

0 commit comments

Comments
 (0)