Skip to content

Commit

Permalink
Activate the ability to build without eventsource (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron authored Sep 27, 2024
1 parent afb7814 commit 87b0fa1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 31 additions & 19 deletions src/horizon/call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
onmessage?: (value: T) => void;
onerror?: (event: MessageEvent) => void;
Expand All @@ -19,28 +23,31 @@ export interface EventSourceOptions<T> {

const anyGlobal = global as any;
type Constructable<T> = 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<EventSource> = anyGlobal.EventSource ??
anyGlobal.window?.EventSource ??
require("eventsource");

// Declare EventSource as a potentially undefined variable
let EventSource: Constructable<EventSource> | 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.BaseResponse>
| HorizonApi.FeeStatsResponse
| HorizonApi.BaseResponse
| ServerApi.CollectionPage<HorizonApi.BaseResponse>
> {
protected url: URI;

Expand Down Expand Up @@ -87,15 +94,20 @@ 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.
* @param {number} [options.reconnectTimeout] Custom stream connection timeout in ms, default is 15 seconds.
* @returns {Function} Close function. Run to close the connection and stop listening for new events.
*/
public stream(options: EventSourceOptions<T> = {}): () => 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");
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -416,4 +428,4 @@ export class CallBuilder<
return Promise.reject(new Error(error.message));
}
}
}
}

0 comments on commit 87b0fa1

Please sign in to comment.