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

fix(react-native): fix for "<Intermediate Value>.stream is not a function" errors in React Native #1156

Merged
merged 1 commit into from
Sep 8, 2020

Conversation

crutchcorn
Copy link
Contributor

I'm fixing a bug or typo

  • if this is your first time contributing, run npm run add-contributor and follow the prompts to add yourself to the README
  • squash merge the PR with commit message "fix: [Description of fix]"

Explanation

While working on my React Native app, I kept finding myself running into <Intermediate Value>.stream is not a function errors. After some investigating, I realized that it was thanks to React Native's Blob supporting constructors but not the stream method. However, it does support CompressionStream. As such, it was defaulting to use browserDeflate, which was causing errors to be thrown.

I thought I could work around this by polyfilling Blob.stream:

    const streams = require('web-streams-polyfill/ponyfill');

    const stream = function stream() {
      let position = 0;
      // eslint-disable-next-line @typescript-eslint/no-this-alias
      const blob = this;

      return new streams.ReadableStream({
        type: 'bytes',
        autoAllocateChunkSize: 524288,

        pull: function(controller) {
          const v = controller.byobRequest.view;
          const chunk = blob.slice(position, position + v.byteLength);
          return chunk.arrayBuffer().then(function(buffer) {
            const uint8array = new Uint8Array(buffer);
            const bytesRead = uint8array.byteLength;

            position += bytesRead;
            v.set(uint8array);
            controller.byobRequest.respond(bytesRead);

            if (position >= blob.size) controller.close();
          });
        },
      });
    };

    // eslint-disable-next-line no-undef
    Blob.prototype.stream = stream;

    const b = new Blob([])
    console.log('b', b);
    b.stream()

But once this was done, I got an error that "anything passed to pipeThrough must be a WritableStream", so I figured a single LOC change was better than not.

By adding a simple check to see if stream is a function, we can tell React Native to instead use pako.deflate, which works exactly as expected.

@karma-pr-reporter
Copy link

Test Results for a04af4c:

Browser Pass Skip Fail Time Disconnect?
Chrome 79.0.3945 (Windows 10 0.0.0) 363 0 0 1m:28s false
Chrome Mobile 80.0.3987 (Android 0.0.0) 363 0 0 1m:39s false
Firefox 76.0.0 (Ubuntu 0.0.0) 363 0 0 1m:3s false
HeadlessChrome 0.0.0 (Linux 0.0.0) 363 0 0 0m:45s false
Mobile Safari 13.0.0 (iOS 13.0.0) 363 0 0 1m:45s false
Safari 13.0.3 (Mac OS X 10.15.1) 363 0 0 2m:28s false

Longest running tests

  • 10.735 secs - clone clone with noTags - Chrome
  • 9.547 secs - clone clone with noTags - Firefox
  • 8.813 secs - clone clone with noTags - Safari
  • 8.273 secs - clone clone with noTags - Mobile Safari
  • 6.232 secs - clone clone with noTags - HeadlessChrome
  • 6.206 secs - clone clone with noTags - Chrome Mobile
  • 4.826 secs - fetch shallow fetch (from Github) - Safari
  • 4.653 secs - walk can populate type, mode, oid, and content - Safari
  • 4.647 secs - fetch shallow fetch (from Github) - Firefox
  • 4.566 secs - fetch shallow fetch (from Github) - Mobile Safari
  • 4.43 secs - fetch shallow fetch (from Github) - Chrome Mobile

@crutchcorn
Copy link
Contributor Author

I tried running npm run add-contributor, as suggested, but got the following:

​C:\Users\crutchcorn\git\GitGui\isomorphic-git [master ≡ +1 ~0 -0 !]
λ  npm run add-contributor

> isomorphic-git@0.0.0-development add-contributor C:\Users\crutchcorn\git\GitGui\isomorphic-git
> nps contributors.add

C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\index.js:67
    throw err;
    ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\crutchcorn\git\GitGui\isomorphic-git\package-scripts.js
require() of ES modules is not supported.
require() of C:\Users\crutchcorn\git\GitGui\isomorphic-git\package-scripts.js from C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename package-scripts.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\crutchcorn\git\GitGui\isomorphic-git\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1216:13)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at requireDefaultFromModule (C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\index.js:200:13)
    at attemptModuleRequire (C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\index.js:186:14)
    at loadConfig (C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\index.js:90:14)
    at getPSConfig (C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\parser.js:265:35)
    at parse (C:\Users\crutchcorn\git\GitGui\isomorphic-git\node_modules\nps\dist\bin-utils\parser.js:115:18) {
  code: 'ERR_REQUIRE_ESM'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! isomorphic-git@0.0.0-development add-contributor: `nps contributors.add`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the isomorphic-git@0.0.0-development add-contributor script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\crutchcorn\AppData\Roaming\npm-cache\_logs\2020-06-10T01_53_56_402Z-debug.log

In both Node 12 and 14

@crutchcorn
Copy link
Contributor Author

@wmhilton do you know when we might be able to get this in? I'm able to work-around it, but am hoping we can remove the hack sometime in the app soon

@crutchcorn
Copy link
Contributor Author

Ping @wmhilton

@billiegoose billiegoose changed the title Added check for stream for React Native fix(react-native): fix for "<Intermediate Value>.stream is not a function" errors in React Native Sep 8, 2020
@billiegoose billiegoose merged commit 071e8ec into isomorphic-git:master Sep 8, 2020
billiegoose pushed a commit that referenced this pull request Dec 17, 2020
jcubic pushed a commit that referenced this pull request Jun 30, 2021
* chore: fix broken link in README.md (#1154)

* fix(react-native): fix for "<Intermediate Value>.stream is not a function" errors in React Native (#1156)

* Added an error handler for cleanup after failed clone

* chore: add comments

* fix: add recursive delete helper & tests

Co-authored-by: Xavier Francisco <xavier.n.francisco@gmail.com>
Co-authored-by: Corbin Crutchley <crutchcorn@gmail.com>
jcubic pushed a commit that referenced this pull request Jul 1, 2021
* chore: fix broken link in README.md (#1154)

* fix(react-native): fix for "<Intermediate Value>.stream is not a function" errors in React Native (#1156)

* chore: remove fallthrough switch statement

* docs: add @mmkal as a contributor

* docs: add @mmkal as a contributor

Co-authored-by: Xavier Francisco <xavier.n.francisco@gmail.com>
Co-authored-by: Corbin Crutchley <crutchcorn@gmail.com>
modesty pushed a commit to modesty/isomorphic-git that referenced this pull request Apr 9, 2024
modesty pushed a commit to modesty/isomorphic-git that referenced this pull request Apr 9, 2024
* chore: fix broken link in README.md (isomorphic-git#1154)

* fix(react-native): fix for "<Intermediate Value>.stream is not a function" errors in React Native (isomorphic-git#1156)

* Added an error handler for cleanup after failed clone

* chore: add comments

* fix: add recursive delete helper & tests

Co-authored-by: Xavier Francisco <xavier.n.francisco@gmail.com>
Co-authored-by: Corbin Crutchley <crutchcorn@gmail.com>
modesty pushed a commit to modesty/isomorphic-git that referenced this pull request Apr 9, 2024
* chore: fix broken link in README.md (isomorphic-git#1154)

* fix(react-native): fix for "<Intermediate Value>.stream is not a function" errors in React Native (isomorphic-git#1156)

* chore: remove fallthrough switch statement

* docs: add @mmkal as a contributor

* docs: add @mmkal as a contributor

Co-authored-by: Xavier Francisco <xavier.n.francisco@gmail.com>
Co-authored-by: Corbin Crutchley <crutchcorn@gmail.com>
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.

None yet

3 participants