import() cannot be used anymore, converted to require()
Massimo Melina committed
Nov 3, 2024 at 22:52 UTC
512319e9c65af3319489d7ce6776f611afcc881d
2 files changed
+13
-10
src/QuickZipStream.ts
+5
-4
@@ -4,14 +4,15 @@ import { Readable } from 'stream'
4
// @ts-ignore
5
import { unsigned } from 'buffer-crc32'
6
import assert from 'assert'
7
+import { try_ } from './misc'
8
9
const ZIP64_SIZE_LIMIT = 0xffffffff
10
const ZIP64_NUMBER_LIMIT = 0xffff
11
11
-let crc32function: (input: string | Buffer, initialState?: number | undefined) => number
12
-import('@node-rs/crc32').then(lib => crc32function = lib.crc32, () => {
13
- console.log('using generic lib for crc32')
14
- crc32function = unsigned
12
+type Crc32Function = (input: string | Buffer, initialState?: number | undefined) => number
13
+const crc32function: Crc32Function = try_(() => require('@node-rs/crc32').crc32 satisfies Crc32Function, () => {
14
+ console.log('! using generic lib for crc32')
15
+ return unsigned satisfies Crc32Function
16
})
17
18
const FLAGS = 0x0808 // bit3 = no crc in local header + bit11 = utf8
src/fileAttr.ts
+8
-6
@@ -2,13 +2,15 @@ import { KvStorage } from '@rejetto/kvstorage'
2
import { existsSync } from 'fs'
3
import { promisify } from 'util'
4
import { access } from 'fs/promises'
5
-import { tryJson } from './cross'
5
+import { try_, tryJson } from './cross'
6
import { onProcessExit } from './first'
7
import { utimes, stat } from 'node:fs/promises'
8
import { IS_WINDOWS } from './const'
9
-// @ts-ignore
10
-const fsx = import('fs-x-attributes').then(x => ({ set: promisify(x.set), get: promisify(x.get) }),
11
- () => console.log('fs-x-attributes not available') )
9
+
10
+const fsx = try_(() => {
11
+ const lib = require('fs-x-attributes')
12
+ return { set: promisify(lib.set), get: promisify(lib.get) }
13
+}, () => console.log('! fs-x-attributes not available'))
14
15
const fileAttrDb = new KvStorage({ defaultPutDelay: 1000, maxPutDelay: 5000 })
16
onProcessExit(() => fileAttrDb.flush())
@@ -18,7 +20,7 @@ if (existsSync(FN))
20
const FILE_ATTR_PREFIX = 'user.hfs.' // user. prefix to be linux compatible
21
export async function storeFileAttr(path: string, k: string, v: any) {
22
const s = await stat(path)
21
- if (await fsx.then(x => x?.set(path, FILE_ATTR_PREFIX + k, JSON.stringify(v)).then(() => 1, () => 0))) {
23
+ if (await fsx?.set(path, FILE_ATTR_PREFIX + k, JSON.stringify(v)).then(() => 1, () => 0)) {
24
if (IS_WINDOWS) utimes(path, s.atime, s.mtime) // restore timestamps, necessary only on Windows
25
return true
26
}
@@ -33,7 +35,7 @@ export async function storeFileAttr(path: string, k: string, v: any) {
35
}
36
37
export async function loadFileAttr(path: string, k: string) {
36
- return await fsx.then(x => x?.get(path, FILE_ATTR_PREFIX + k))
38
+ return await fsx?.get(path, FILE_ATTR_PREFIX + k)
39
.then((x: any) => x && tryJson(String(x)), () => {})
40
.then((x: any) => x ?? (fileAttrDb.isOpen() ? fileAttrDb.get(`${path}|${k}`) : null))
41
?? undefined // normalize, as we get null instead of undefined on windows