Skip to content

Conversation

@msikma
Copy link

@msikma msikma commented Aug 8, 2022

This attempts to add support for Blob URLs for Node.

Should fix #30. But I'm not sure if any other details need to be looked into.

Fixes the "TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file" bug that you see when you try to use a Blob.

Tiny little test case:

const Worker = require('web-worker')
const {Blob} = require('node:buffer')

const workerSrc = `
self.onmessage = function(e) {
  self.postMessage('Doing expensive calculation.')
  const t1 = Number(new Date())
  while (true) {
    const t2 = Number(new Date())
    if (t2 > t1 + 1000) {
      break
    }
  }
  postMessage('Done.')
}
`

async function main() {
  const blob = new Blob([workerSrc], {type: 'text/javascript'})
  const blobUrl = URL.createObjectURL(blob)
  const worker = new Worker(blobUrl)
  worker.onmessage = function(e) {
    console.log('msg:', e.data)
    if (e.data === 'Done.') {
      process.exit(0)
    }
  }
  worker.postMessage('start')
}

main()

@theomessin
Copy link

Bump

@sindresorhus
Copy link

@developit

@exoRift
Copy link

exoRift commented Jul 17, 2025

Bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blob URL does not work on Node.js

4 participants