seald/nedb

Electron: TypeError: chunk.split is not a function

Closed this issue · 8 comments

When using NeDB in Electron and the database has data and gets loaded; Electron crashes with the following message:
TypeError: chunk.split is not a function at LineStream._transform

This is caused by the lib/byline.js file. Electron doesn't load the Buffer module by default. When adding const Buffer = require('buffer').Buffer; to lib/byline.js the issue is solved.

tex0l commented

Does it only happen when Nedb is executed in the renderer process, or does it also happen in the main process?

Also, what kind of persistence do you use (in-memory only, on disk or localforage)?

This happens in the main process, I'm using disk storage for that.
I've not yet tested in the renderer process or with any other persistence.

I'm guessing this issue only occurs when reading from disk cause the error is caused by the byline.js library.

The pullrequest I've created and mentioned here solved the issue for me.

tex0l commented

It seems surprising as Buffer should be in the global scope as it is a regular Node.js process, does it work if you import node:buffer instead of buffer (I would like to check if it is the buffer polyfill that is imported by your fix or the actual standard library)?

tex0l commented

Could you please provide me with a minimal repro case where it fails? I'll investigate.

tex0l commented

I tried to repro:

const { app } = require('electron')
const NeDB = require('@seald-io/nedb')

console.log('starting')

app.whenReady().then(async () => {
  try {
    let db = new NeDB({ filename: './test' })
    await db.loadDatabaseAsync()
    await db.insertAsync({ toto: 'tata' })
    await db.compactDatafileAsync()
    db = new NeDB({ filename: './test' })
    await db.loadDatabaseAsync()
    const res = await db.findAsync({})
    console.log(res)
  } catch (error) {
    console.error('failed', error)
  }
})

with

    "@seald-io/nedb": "^4.0.1",
    "electron": "^23.1.3"

It doesn't fail.

Can you provide a gist showcasing a minimal repro case please?

tex0l commented

@maxdaniel98 were you able to reproduce in a minimal env?

tex0l commented

Hi, I hadn't seen that you provided a PR. I merged the PR, it will be released in a future version

tex0l commented

It landed in 4.0.4