AtoraSuunva / booru

Searches boorus for images using some js magic
https://www.npmjs.com/package/booru
MIT License
80 stars 19 forks source link

TypeError: resolvedFetch is not a function #96

Closed dnm13 closed 2 years ago

dnm13 commented 2 years ago
TypeError: resolvedFetch is not a function
    at Booru.doSearchRequest (<omitted>\node_modules\booru\dist\boorus\Booru.js:1:1639)
    at Booru.search (<omitted>\node_modules\booru\dist\boorus\Booru.js:1:821)
    at Object.search (<omitted>\node_modules\booru\dist\index.js:1:1555)
    at <omitted>\handlers\booru.js:23:21
    at new Promise (<anonymous>)
    at Object.<anonymous> (<omitted>\handlers\booru.js:22:16)
    at Generator.next (<anonymous>)
    at <omitted>\handlers\booru.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (<omitted>\handlers\booru.js:4:12)
    at <omitted>\handlers\booru.js:21:64
    at node:electron/js2c/browser_init:201:579
    at Object.<anonymous> (node:electron/js2c/browser_init:165:10272)
    at Object.emit (node:events:394:28) {
  name: 'BooruError'
}

Booru version: 2.6.2 Node version: 14.15.0

Tried with any booru. Before that, using v2.4.0, every NSFW boorus throws an error like this:

TypeError: Cannot read properties of undefined (reading 'split')
    at getTags (<omitted>\node_modules\booru\dist\structures\Post.js:1:810)
    at new Post (<omitted>\node_modules\booru\dist\structures\Post.js:1:1922)
    at <omitted>\node_modules\booru\dist\boorus\Booru.js:1:2386
    at Array.map (<anonymous>)
    at Booru.parseSearchResult (<omitted>\node_modules\booru\dist\boorus\Booru.js:1:2378)
    at Booru.search (<omitted>\node_modules\booru\dist\boorus\Booru.js:1:902)
    at processTicksAndRejections (node:internal/process/task_queues:96:5){
  name: 'BooruError'
}

While SFW boorus works normally.

dnm13 commented 2 years ago

Code example / usage

            search(site, `id:${post_id}`)
                .then(r => {
                    var data: BooruPost = r.posts[0] as any;
                    data.thumbnail = getThumbnail(r.posts[0] as any)
                    data.site_name = r.posts[0].booru.domain
                    data.extension = data.fileUrl.split('.').pop() || 'jpg'
                    data = AutoTag('booru', data as any)
                    res(data)
                })
                .catch(err => {
                    console.log(err);
                    res(false)
                })
AtoraSuunva commented 2 years ago

Looks like you're using electron, right?

It doesn't seem like electron supports fetch natively so window.fetch is undefined while window is defined

If undici works on electron I can add in an extra check, but if it doesn't you'll have to polyfill window.fetch with a working electron fetch

dnm13 commented 2 years ago

Looks like you're using electron, right?

It doesn't seem like electron supports fetch natively so window.fetch is undefined while window is defined

If undici works on electron I can add in an extra check, but if it doesn't you'll have to polyfill window.fetch with a working electron fetch

Yes. I'm using electron. But it's really weird since it has worked for long on my app (electron). I'm not sure what triggered this but it's only recently that this error is thrown.

AtoraSuunva commented 2 years ago

The actual line defining resolvedFetch hasn't changed since 2.3.0

const resolvedFetch = typeof window !== 'undefined' ? window.fetch.bind(window) : fetch

2.5.7 did change from using node-fetch to undici for the actual fetch, which might be why?

Are you trying to run booru from the main process or the renderer process? Which version of electron?

I tried Electron 19.0.6 and it works on both the main process and renderer process (with a brute-force nodeIntegration: true but still)

image

dnm13 commented 2 years ago

It's Electron v15.1.0 No, I run Booru from main process

MB for the late response

AtoraSuunva commented 2 years ago

I can reproduce it on Electron v15.1.0, but not on v16.0.0

The issue is the Node.js version

Node.js 16.5.0, Chromium 94.0.4606.61, and Electron 15.1.0.
Node.js 16.9.1, Chromium 96.0.4664.45, and Electron 16.0.0.

Undici only supports fetch on Node.js v16.8+, so undici.fetch doesn't exist on Electron v15

Since Electron v15 is no longer supported I'd recommend updating Electron first. Ideally to a currently-supported version, but even just to v16 would work

If you absolutely can't update Electron I could look into a way of passing your own fetch, but I suggest updating Electron before you run into more issues like these

dnm13 commented 2 years ago

That worked Thank you