From 87b0fa15fd8b76cb56a654af0ab0a0d8cf227d02 Mon Sep 17 00:00:00 2001 From: Blaine Heffron Date: Fri, 27 Sep 2024 14:33:29 -0400 Subject: [PATCH] Activate the ability to build without eventsource (#1066) --- CHANGELOG.md | 4 +++ README.md | 19 ++++++++++++-- src/horizon/call_builder.ts | 50 +++++++++++++++++++++++-------------- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51b3e2c63..d7ec65c26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,10 @@ export interface BalanceResponse { - New build target for creating a browser bundle without Axios dependency. Set USE_AXIOS=false environment variable to build stellar-sdk-no-axios.js and stellar-sdk-no-axios.min.js in the dist/ directory. Use yarn build:browser:no-axios to generate these files. - Similarly, a new import path for the node package without the Axios dependency can be used. Import the SDK using `@stellar/stellar-sdk/no-axios`. For Node.js environments that don't support the package.json `exports` configuration, use `@stellar/stellar-sdk/lib/no-axios/index`. +- There is also a new build target for creating a browser bundle without EventSource dependency. Set USE_EVENTSOURCE=false environment variable to build stellar-sdk-no-eventsource.js and stellar-sdk-no-eventsource.min.js in the dist/ directory. Use yarn build:browser:no-eventsource to generate these files. +- A new import path for the node package without the EventSource dependency can be used with `@stellar/stellar-sdk/no-eventsource`. For Node.js environments that don't support the package.json `exports` configuration, use `@stellar/stellar-sdk/lib/no-eventsource/index`. +- To use a minimal build without Axios and EventSource, use `stellar-sdk-minimal.js` for the browser build and import from `@stellar/stellar-sdk/minimal` for the node package. + ## [Version Number] - YYYY-MM-DD - Updated the Node.js target in the Babel configuration from 16 to 18 for production builds. diff --git a/README.md b/README.md index 4d2a9216d..058119954 100644 --- a/README.md +++ b/README.md @@ -87,10 +87,25 @@ If you don't want to use or install Bower, you can copy the packaged JS files fr ### Custom Installation -You can configure whether or not to build the browser bundle with the axios dependency. In order to turn off the axios dependency, set the USE_AXIOS environment variable to false. +You can configure whether or not to build the browser bundle with the axios dependency. In order to turn off the axios dependency, set the USE_AXIOS environment variable to false. You can also turn off the eventsource dependency by setting USE_EVENTSOURCE to false. #### Build without Axios -USE_AXIOS=false npm run build:browser +``` +npm run build:browser:no-axios +``` +This will create `stellar-sdk-no-axios.js` in `dist/`. + +#### Build without EventSource +``` +npm run build:browser:no-eventsource +``` +This will create `stellar-sdk-no-eventsource.js` in `dist/`. + +#### Build without Axios and Eventsource +``` +npm run build:browser:minimal +``` +This will create `stellar-sdk-minimal.js` in `dist/`. ## Usage diff --git a/src/horizon/call_builder.ts b/src/horizon/call_builder.ts index 49bdec810..2a8a648e9 100644 --- a/src/horizon/call_builder.ts +++ b/src/horizon/call_builder.ts @@ -6,11 +6,15 @@ import { BadRequestError, NetworkError, NotFoundError } from "../errors"; import { HorizonApi } from "./horizon_api"; import { AxiosClient, version } from "./horizon_axios_client"; import { ServerApi } from "./server_api"; +import type { Server } from "../federation"; // Resources which can be included in the Horizon response via the `join` // query-param. const JOINABLE = ["transaction"]; +// eslint-disable-next-line @typescript-eslint/naming-convention +declare const __USE_EVENTSOURCE__: boolean; + export interface EventSourceOptions { onmessage?: (value: T) => void; onerror?: (event: MessageEvent) => void; @@ -19,28 +23,31 @@ export interface EventSourceOptions { const anyGlobal = global as any; type Constructable = new (e: string) => T; -// require("eventsource") for Node and React Native environment -/* eslint-disable global-require */ -/* eslint-disable prefer-import/prefer-import-over-require */ -const EventSource: Constructable = anyGlobal.EventSource ?? - anyGlobal.window?.EventSource ?? - require("eventsource"); + +// Declare EventSource as a potentially undefined variable +let EventSource: Constructable | undefined; + +// Only define EventSource if __USE_EVENTSOURCE__ is true +if (typeof __USE_EVENTSOURCE__ !== 'undefined' && __USE_EVENTSOURCE__) { + /* eslint-disable global-require */ + /* eslint-disable prefer-import/prefer-import-over-require */ + EventSource = anyGlobal.EventSource ?? + anyGlobal.window?.EventSource ?? + require("eventsource"); +} /** * Creates a new {@link CallBuilder} pointed to server defined by serverUrl. * - * This is an **abstract** class. Do not create this object directly, use {@link module:Horizon.Server | Horizon.Server} class. - * - * @private - * @class - * + * This is an **abstract** class. Do not create this object directly, use {@link Server} class. * @param {string} serverUrl URL of Horizon server + * @class CallBuilder */ export class CallBuilder< T extends - | HorizonApi.FeeStatsResponse - | HorizonApi.BaseResponse - | ServerApi.CollectionPage + | HorizonApi.FeeStatsResponse + | HorizonApi.BaseResponse + | ServerApi.CollectionPage > { protected url: URI; @@ -87,8 +94,8 @@ export class CallBuilder< /** * Creates an EventSource that listens for incoming messages from the server. To stop listening for new * events call the function returned by this method. - * @see {@link https://developers.stellar.org/docs/data/horizon/api-reference/structure/response-format|Horizon Response Format} - * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/EventSource|MDN EventSource} + * @see [Horizon Response Format](https://developers.stellar.org/api/introduction/response-format/) + * @see [MDN EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) * @param {object} [options] EventSource options. * @param {Function} [options.onmessage] Callback function to handle incoming messages. * @param {Function} [options.onerror] Callback function to handle errors. @@ -96,6 +103,11 @@ export class CallBuilder< * @returns {Function} Close function. Run to close the connection and stop listening for new events. */ public stream(options: EventSourceOptions = {}): () => void { + // Check if EventSource use is enabled + if (EventSource === undefined){ + throw new Error("Streaming requires eventsource to be enabled. If you need this functionality, compile with USE_EVENTSOURCE=true."); + } + this.checkFilter(); this.url.setQuery("X-Client-Name", "js-stellar-sdk"); @@ -194,7 +206,7 @@ export class CallBuilder< /** * Sets `cursor` parameter for the current call. Returns the CallBuilder object on which this method has been called. - * @see {@link https://developers.stellar.org/docs/data/horizon/api-reference/structure/pagination|Paging} + * @see [Paging](https://developers.stellar.org/api/introduction/pagination/) * @param {string} cursor A cursor is a value that points to a specific location in a collection of resources. * @returns {object} current CallBuilder instance */ @@ -205,7 +217,7 @@ export class CallBuilder< /** * Sets `limit` parameter for the current call. Returns the CallBuilder object on which this method has been called. - * @see {@link https://developers.stellar.org/docs/data/horizon/api-reference/structure/pagination|Paging} + * @see [Paging](https://developers.stellar.org/api/introduction/pagination/) * @param {number} recordsNumber Number of records the server should return. * @returns {object} current CallBuilder instance */ @@ -416,4 +428,4 @@ export class CallBuilder< return Promise.reject(new Error(error.message)); } } -} +} \ No newline at end of file