Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use readable-stream on service worker #487

Closed
userquin opened this issue Sep 8, 2022 · 3 comments
Closed

cannot use readable-stream on service worker #487

userquin opened this issue Sep 8, 2022 · 3 comments

Comments

@userquin
Copy link

userquin commented Sep 8, 2022

I patch these 2 modules with a postinstall script ("postinstall": "esno scripts/patch.ts"):

import { readFileSync, writeFileSync } from 'node:fs'

const patch = () => {
    let content: string = readFileSync(
        './node_modules/readable-stream/lib/_stream_readable.js',
        { encoding: 'utf-8' },
    )
    if (content.includes('global.Uint8Array')) {
        writeFileSync(
            './node_modules/readable-stream/lib/_stream_readable.js',
            content.replace(
                'var OurUint8Array = global.Uint8Array || function () {};',
                'var OurUint8Array = self.Uint8Array || function () {};'
            ),
            { encoding: 'utf-8' },
        )
    }
    content = readFileSync('./node_modules/readable-stream/lib/_stream_writable.js', { encoding: 'utf-8' })
    if (content.includes('global.Uint8Array')) {
        writeFileSync(
            './node_modules/readable-stream/lib/_stream_writable.js',
            content.replace(
                'var OurUint8Array = global.Uint8Array || function () {};',
                'var OurUint8Array = self.Uint8Array || function () {};'
            ),
            { encoding: 'utf-8' },
        )
    }
}

patch()
@benmccann
Copy link

Rather than just assuming global will be defined, probably need to do something like:

typeof global !== "undefined"
  ? global
  : typeof self !== "undefined"
  ? self
  : typeof window !== "undefined"
  ? window
  : {};

E.g. as done in https://github.com/Aslemammad/modern-node-polyfills/blob/main/global.ts

@mcollina
Copy link
Member

mcollina commented Sep 8, 2022

Would you like to send a PR? Checkout our build scripts inside build/.

@smeng9
Copy link

smeng9 commented Mar 1, 2023

Mark as closed by #502

@mcollina mcollina closed this as completed Mar 1, 2023
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 a pull request may close this issue.

4 participants