sqlite-wasm-http icon indicating copy to clipboard operation
sqlite-wasm-http copied to clipboard

nodejs: "Timeout while waiting on backend" when createSQLiteThread()

Open jbeuckm opened this issue 3 months ago • 1 comments

I can't get the worker going in node v22.14.0

I had to hack the package.json for this package to include an export for "require" under line 11.

Here is what I'm trying to add to my service:

import { Worker } from 'worker_threads'
globalThis.Worker = Worker as any

const { createHttpBackend, createSQLiteThread } = require('sqlite-wasm-http')

const httpBackendGlobale = createHttpBackend({
  maxPageSize: 4096, // this is the current default SQLite page size
  timeout: 5000, // 5s
  cacheSize: 4096, // 4 MB
})
console.log('httpBackendGlobale', httpBackendGlobale)

createSQLiteThread({ http: httpBackendGlobale })
  .then(db => console.log('ready', db))
  .catch(e => console.log(e))

After my 5sec (or 60+ sec) timeout expires, I crash like this:

node:internal/process/promises:392
      new UnhandledPromiseRejection(reason);
      ^

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Timeout while waiting on backend".
    at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
    at processPromiseRejections (node:internal/process/promises:475:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
  code: 'ERR_UNHANDLED_REJECTION'
}

MORE:

I now realize that the readme says to use web-worker so I'm trying to import that but have a similar module resolution problem:

SyntaxError: Cannot use import statement outside a module
    at new Script (node:vm:117:7)
    at createScript (node:vm:269:10)
    at Object.runInThisContext (node:vm:317:10)
    at importScripts (/Users/jbeuckman/Documents/marketplace-app/node_modules/web-worker/dist/node/index.cjs:121:32)

jbeuckm avatar Aug 22 '25 18:08 jbeuckm

This is mostly a browser package where it is the most useful. It is especially in the browser that you need to be able to access remote SQLite databases using only HTTP(S).

It can work in Node.js - the unit tests do this - but it requires replacing the Node.js worker_thread with a browser Worker polyfill - I use @mmomtchev/web-worker. It is essentially the same as web-worker but with a few improvements for recent Node.js versions that I made. My PRs are not getting merged because the brilliant genius behind the planetary-wide extortion that I am at center at decided that blocking my PRs was a smart and well thought out thing to do.

You can check the unit tests and the setup of the mocha hooks for example code that does this.

mmomtchev avatar Aug 22 '25 20:08 mmomtchev