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

Drop eventsource polyfill to let users opt in to streaming support #901

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/npm_publish.yml
Expand Up @@ -33,6 +33,8 @@ jobs:

- name: Deprecate the old package
run: |
npm deprecate stellar-sdk@latest "⚠️ This package has moved to @stellar/stellar-sdk! 🚚"
V=$(cat package.json | jq '.version' | sed -e 's/\"//g')
echo "Deprecating stellar-sdk@$V"
npm deprecate stellar-sdk@"<= $V"⚠️ This package has moved to @stellar/stellar-sdk! 🚚"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Expand Up @@ -30,7 +30,9 @@ jobs:
run: yarn install --network-concurrency 1

- name: Build
run: yarn build:prod
run: |
yarn build:prod
yarn add --dev eventsource

- name: Unit Tests
run: yarn test:node
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ A breaking change will get clearly marked in this log.

## Unreleased

### Breaking Changes
* Downstream consumers must now polyfill an `EventSource` provider if they do not have native support or they will be opted out of Horizon streaming. This can be done via the [`eventsource`](https://www.npmjs.com/package/eventsource) package, i.e. `npm i eventsource` ([#878](https://github.com/stellar/js-stellar-sdk/pull/878)).


## [v11.1.0](https://github.com/stellar/js-stellar-sdk/compare/v11.0.1...v11.1.0)

Expand Down
10 changes: 5 additions & 5 deletions config/webpack.config.browser.js
Expand Up @@ -63,17 +63,17 @@ const config = {
new ESLintPlugin({
overrideConfigFile: path.resolve(__dirname, '../.eslintrc.js')
}),
// Ignore native modules (sodium-native)
new webpack.IgnorePlugin({ resourceRegExp: /sodium-native/ }),
// Ignore native modules (sodium-native) and opt-in polyfills
new webpack.IgnorePlugin({ resourceRegExp: /(sodium-native|eventsource)/ }),
new NodePolyfillPlugin({
includeAliases: ['http', 'https'] // others aren't needed
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer']
})
}),
],
watchOptions: {
ignored: /(node_modules|coverage|lib|dist)/
externals: {
'eventsource': ['umd', 'EventSource'],
}
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -88,7 +88,7 @@
"@stellar/tsconfig": "^1.0.2",
"@types/chai": "^4.3.6",
"@types/detect-node": "^2.0.0",
"@types/eventsource": "^1.1.12",
"@types/eventsource": "^1.1.15",
"@types/json-schema": "^7.0.15",
"@types/lodash": "^4.14.199",
"@types/mocha": "^10.0.2",
Expand All @@ -113,6 +113,7 @@
"eslint-plugin-prefer-import": "^0.0.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-webpack-plugin": "^4.0.1",
"eventsource": "^2.0.2",
"ghooks": "^2.0.4",
"husky": "^8.0.3",
"jsdoc": "^4.0.2",
Expand Down Expand Up @@ -147,7 +148,6 @@
"@stellar/stellar-base": "10.0.1",
"axios": "^1.6.0",
"bignumber.js": "^9.1.2",
"eventsource": "^2.0.2",
"randombytes": "^2.1.0",
"toml": "^3.0.0",
"urijs": "^1.19.1"
Expand Down
26 changes: 22 additions & 4 deletions src/horizon/call_builder.ts
Expand Up @@ -19,10 +19,21 @@ export interface EventSourceOptions<T> {

const anyGlobal = global as any;
type Constructable<T> = new (e: string) => T;
// require("eventsource") for Node and React Native environment
let EventSource: Constructable<EventSource> = anyGlobal.EventSource ??
anyGlobal.window?.EventSource ??
require("eventsource");
// require("eventsource") polyfill for Node and React Native environments
let EventSource: Constructable<EventSource>;
try {
EventSource = anyGlobal.EventSource ??
anyGlobal.window?.EventSource ??
require("eventsource");
} catch (e: any) {
console.warn(
'⚠️ No EventSource provider found: either polyfill it ' +
'(e.g. `npm i eventsource`) or you will not have streaming support.'
);
console.warn(
"⚠️ You can ignore this message if you don't care about streaming."
);
}

/**
* Creates a new {@link CallBuilder} pointed to server defined by serverUrl.
Expand Down Expand Up @@ -110,6 +121,13 @@ export class CallBuilder<

let createEventSource = (): EventSource => {
try {
if (!EventSource) {
console.warn(
'⚠️ No EventSource provider found: there is no streaming support ' +
'unless you polyfill it (e.g. `npm i eventsource`).'
);
}

es = new EventSource(this.url.toString());
} catch (err) {
if (options.onerror) {
Expand Down