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

Add eventsource polyfill for Node.js and browser environments #8118

Merged
merged 53 commits into from May 2, 2024

Conversation

hannahblair
Copy link
Collaborator

@hannahblair hannahblair commented Apr 24, 2024

Description

Replaces the Web API eventsource with eventsource to support Node.js environments. Also addresses window is not defined errors in Node.js.

To test, run the following in a node environment:

import { Client } from "@gradio/client";

const test = async () => {
    try {
	const app = await Client.connect("gradio/hello_world");
	const result = await app.predict("/predict", ["John"]);
	console.log("Result:", result);
    } catch (error) {
	console.error("Error:", error);
    }
};

(async () => {
	await test();
})();

Closes: #6352
Closes: #7103

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 24, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/d3c2e57646ef2bf9576c34cb7ed232ced11c00cf/gradio-4.28.3-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@d3c2e57646ef2bf9576c34cb7ed232ced11c00cf#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 24, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/client patch
@gradio/upload patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Add eventsource polyfill for Node.js and browser environments

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@hannahblair hannahblair changed the title Add EventSource polyfill for Node.js and browser environments Add eventsource polyfill for Node.js and browser environments Apr 24, 2024
client/js/vite.config.js Outdated Show resolved Hide resolved
Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to run these test in node as well. We can doo this by creating a new script:

"pnpm:test:client:node": "vitest client/js -c path/to/config --mode=test_node"

Then in the vite.config.ts check the mode is test_node and set the test.environment to "node". We would also need to check anything that is currently checking for just mode == "test" and also catch the new node-test.

@hannahblair
Copy link
Collaborator Author

hannahblair commented May 1, 2024

@pngwn good thinking! i've added that now

i'm not sure if there's anything relevant that's checking for mode == "test" 🤔

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment but looks great. Thanks for sorting this one out @hannahblair! This will be a huge boost to quality!

client/js/src/client.ts Outdated Show resolved Hide resolved
client/js/src/client.ts Outdated Show resolved Hide resolved
client/js/src/client.ts Outdated Show resolved Hide resolved
@pngwn
Copy link
Member

pngwn commented May 2, 2024

Also closes #5840

I mean it closes this: ##5583

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for ironing out the kinks with this one @hannahblair! And sorry for the back and forth!

@hannahblair hannahblair merged commit 7aca673 into main May 2, 2024
8 checks passed
@hannahblair hannahblair deleted the client-eventsource branch May 2, 2024 20:49
dawoodkhan82 pushed a commit that referenced this pull request May 6, 2024
* add msw setup and initialisation tests

* add changeset

* add eventsource polyfill for node and browser envs

* add changeset

* add changeset

* config tweak

* types

* update eventsource usage

* add changeset

* add walk_and_store_blobs improvements and add tests

* add changeset

* api_info tests

* add direct space URL link tests

* fix tests

* add view_api tests

* add post_message test

* tweak

* add spaces tests

* jwt and protocol tests

* add post_data tests

* test tweaks

* dynamically import eventsource

* revet eventsource imports

* add node test

* lockfile

* add client test in root pkg file

* lcokfile

* remove eventsource from js/app

* add changeset

* remove ts ignore

* move eventsource polyfill to eventsource factory

* add changeset

* tweak

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.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
3 participants