From 94e3dac27276f256f95d72facb490ff720b4c89f Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Fri, 3 Nov 2023 14:53:53 -0700 Subject: [PATCH] Make eventsource an optional dependency for streamers --- CHANGELOG.md | 3 +++ package.json | 4 ++-- src/horizon/call_builder.ts | 7 ++++++- yarn.lock | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4acd676a7..7dd47a057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 must opt-out of Horizon streaming support. This can be done via the [`eventsource`](https://www.npmjs.com/package/eventsource) package ([TODO]()). + ## [v11.0.0-beta.6](https://github.com/stellar/js-stellar-sdk/compare/v11.0.0-beta.5...v11.0.0-beta.6) diff --git a/package.json b/package.json index 98df67a83..612a8f5d2 100644 --- a/package.json +++ b/package.json @@ -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.14", "@types/lodash": "^4.14.199", "@types/mocha": "^10.0.2", "@types/node": "^20.8.10", @@ -112,6 +112,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", @@ -144,7 +145,6 @@ "dependencies": { "axios": "^1.6.0", "bignumber.js": "^9.1.2", - "eventsource": "^2.0.2", "randombytes": "^2.1.0", "stellar-base": "10.0.0-beta.4", "toml": "^3.0.0", diff --git a/src/horizon/call_builder.ts b/src/horizon/call_builder.ts index 59ff97393..ca566aba6 100644 --- a/src/horizon/call_builder.ts +++ b/src/horizon/call_builder.ts @@ -20,9 +20,14 @@ export interface EventSourceOptions { const anyGlobal = global as any; type Constructable = new (e: string) => T; // require("eventsource") for Node and React Native environment -let EventSource: Constructable = anyGlobal.EventSource ?? +const EventSource: Constructable = anyGlobal.EventSource ?? anyGlobal.window?.EventSource ?? require("eventsource"); +if (!EventSource) { + console.warn( + '⚠️ No EventSource provider found: either polyfill it (e.g. `npm i eventsource`) or you will not have streaming support.' + ); +} /** * Creates a new {@link CallBuilder} pointed to server defined by serverUrl. diff --git a/yarn.lock b/yarn.lock index e6e539438..ffe1283b2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1423,7 +1423,7 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.4.tgz#d9748f5742171b26218516cf1828b8eafaf8a9fa" integrity sha512-2JwWnHK9H+wUZNorf2Zr6ves96WHoWDJIftkcxPKsS7Djta6Zu519LarhRNljPXkpsZR2ZMwNCPeW7omW07BJw== -"@types/eventsource@^1.1.12": +"@types/eventsource@^1.1.14": version "1.1.14" resolved "https://registry.yarnpkg.com/@types/eventsource/-/eventsource-1.1.14.tgz#b5b115b19f3a392a6c29331486bc88dcb4e8a4e2" integrity sha512-WiPIkZ5fuhTkeaVaPKbaP6vHuTX9FHnFNTrkSbm+Uf6g4TH3YNbdfw5/1oLzKIWsQRbrvSiByO2nPSxjr5/cgQ==