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

Replace the eventsource library #239

Merged
merged 15 commits into from
Mar 6, 2019
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
As this project is pre 1.0, breaking changes may happen for minor version bumps.
A breaking change will get clearly marked in this log.

## In master

- **Breaking change**: `stellar-sdk` no longer ships with an `EventSource`
polyfill. If you plan to support IE11 / Edge, please use
[`event-source-polyfill`](https://www.npmjs.com/package/event-source-polyfill)
to set `window.EventSource`.
- Upgrade `stellar-base` to a version that doesn't use the `crypto` library,
fixing a bug with Angular 6

## [v0.14.0](https://github.com/stellar/js-stellar-sdk/compare/v0.13.0...v0.14.0)

- Updated some out-of-date dependencies
Expand Down
105 changes: 63 additions & 42 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var webpack = require('webpack');
var fs = require('fs');
var clear = require('clear');
var plumber = require('gulp-plumber');
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin;

gulp.task('lint:src', function lintSrc() {
return gulp
Expand Down Expand Up @@ -48,49 +50,68 @@ gulp.task(
gulp.task(
'build:browser',
gulp.series('lint:src', function buildBrowser() {
return (
gulp
.src('src/browser.js')
.pipe(plumber())
.pipe(
plugins.webpack({
output: { library: 'StellarSdk' },
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{ test: /\.json$/, loader: 'json-loader' }
]
},
plugins: [
// Ignore native modules (ed25519)
new webpack.IgnorePlugin(/ed25519/)
return gulp
.src('src/browser.js')
.pipe(plumber())
.pipe(
plugins.webpack({
output: { library: 'StellarSdk' },
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{ test: /\.json$/, loader: 'json-loader' }
]
})
)
// Add EventSource polyfill for IE11
.pipe(
plugins.insert.prepend(
fs.readFileSync(
'./node_modules/event-source-polyfill/src/eventsource.min.js'
)
)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Diff is tricky to read, it looks like this is the only change? 👍 on removing polyfill

.pipe(plugins.rename('stellar-sdk.js'))
.pipe(gulp.dest('dist'))
.pipe(
plugins.uglify({
output: {
ascii_only: true
}
})
)
.pipe(plugins.rename('stellar-sdk.min.js'))
.pipe(gulp.dest('dist'))
);
},
plugins: [
// Ignore native modules (ed25519)
new webpack.IgnorePlugin(/ed25519/)
]
})
)
.pipe(plugins.rename('stellar-sdk.js'))
.pipe(gulp.dest('dist'))
.pipe(
plugins.uglify({
output: {
ascii_only: true
}
})
)
.pipe(plugins.rename('stellar-sdk.min.js'))
.pipe(gulp.dest('dist'));
})
);

gulp.task(
'analyze:browser',
gulp.series('lint:src', function analyzeBrowser() {
return gulp
.src('src/browser.js')
.pipe(plumber())
.pipe(
plugins.webpack({
output: { library: 'StellarSdk' },
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{ test: /\.json$/, loader: 'json-loader' }
]
},
plugins: [
// Ignore native modules (ed25519)
new webpack.IgnorePlugin(/ed25519/),
new BundleAnalyzerPlugin()
]
})
);
})
);

Expand Down
7 changes: 6 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ module.exports = function(config) {
frameworks: ['mocha', 'chai-as-promised', 'chai', 'sinon'],
browsers: ['Firefox'],

files: ['dist/stellar-sdk.js', 'test/test-helper.js', 'test/unit/**/*.js'],
files: [
'dist/stellar-sdk.js',
'test/test-helper.js',
'test/unit/**/*.js',
'test/integration/server_test.js'
],

preprocessors: {
'test/**/*.js': ['webpack']
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"**/*.{js,json}": [
"prettier --write",
"git add"
],
"**/*.js": [
"eslint --fix --max-warnings 0",
"git add"
]
},
"keywords": [
Expand Down Expand Up @@ -99,13 +103,14 @@
"run-sequence": "^1.0.2",
"sinon": "^1.14.1",
"sinon-chai": "^2.7.0",
"webpack": "^1.13.2"
"webpack": "^1.13.2",
"webpack-bundle-analyzer": "2.13.1"
},
"dependencies": {
"axios": "^0.18.0",
"detect-node": "^2.0.4",
"es6-promise": "^4.2.4",
"event-source-polyfill": "^0.0.12",
"eventsource": "^1.0.5",
"eventsource": "^1.0.7",
"lodash": "^4.17.11",
"stellar-base": "^0.12.0",
"toml": "^2.3.0",
Expand Down
20 changes: 13 additions & 7 deletions src/call_builder.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import forEach from 'lodash/forEach';
import URI from 'urijs';
import URITemplate from 'urijs/src/URITemplate';
import isNode from 'detect-node';

import HorizonAxiosClient from './horizon_axios_client';
import { version } from '../package.json';
import { NotFoundError, NetworkError, BadRequestError } from './errors';

const EventSource =
// eslint-disable-next-line no-undef, prefer-import/prefer-import-over-require
typeof window === 'undefined' ? require('eventsource') : window.EventSource;
let EventSource;

if (isNode) {
// eslint-disable-next-line
EventSource = require('eventsource');
} else {
// eslint-disable-next-line
EventSource = window.EventSource;
}

/**
* Creates a new {@link CallBuilder} pointed to server defined by serverUrl.
Expand Down Expand Up @@ -65,8 +72,8 @@ export class CallBuilder {
stream(options = {}) {
this.checkFilter();

this.url.setQuery("X-Client-Name", 'js-stellar-sdk');
this.url.setQuery("X-Client-Version", version);
this.url.setQuery('X-Client-Name', 'js-stellar-sdk');
this.url.setQuery('X-Client-Version', version);

// EventSource object
let es;
Expand Down Expand Up @@ -184,8 +191,7 @@ export class CallBuilder {

// Temp fix for: https://github.com/stellar/js-stellar-sdk/issues/15
url.setQuery('c', Math.random());
return HorizonAxiosClient
.get(url.toString())
return HorizonAxiosClient.get(url.toString())
.then((response) => response.data)
.catch(this._handleNetworkError);
}
Expand Down
3 changes: 2 additions & 1 deletion test/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
axios: true,
chai: true,
sinon: true,
expect: true
expect: true,
HorizonAxiosClient: true
},
rules: {
'no-unused-vars': 0
Expand Down
9 changes: 7 additions & 2 deletions test/integration/client_headers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const http = require('http');
const url = require('url');
const port = 3000;

describe('integration tests', function() {
describe('integration tests', function(done) {
if (typeof window !== 'undefined') {
done();
return;
Expand Down Expand Up @@ -38,6 +38,7 @@ describe('integration tests', function() {
let closeStream;

const requestHandler = (request, response) => {
// eslint-disable-next-line node/no-deprecated-api
let query = url.parse(request.url, true).query;
expect(query['X-Client-Name']).to.be.equal('js-stellar-sdk');
expect(query['X-Client-Version']).to.match(/^[0-9]+\.[0-9]+\.[0-9]+$/);
Expand All @@ -59,7 +60,11 @@ describe('integration tests', function() {
allowHttp: true
})
.operations()
.stream();
.stream({
onerror: (err) => {
done(err);
}
});
});
});
});
3 changes: 3 additions & 0 deletions test/integration/server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ describe('integration tests', function() {
expect(operation.account).to.equal(randomAccount.publicKey());
eventStreamClose();
done();
},
onerror: (err) => {
done(err);
}
});

Expand Down
3 changes: 2 additions & 1 deletion test/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-undef */

if (typeof window === 'undefined') {
require('babel-register');
global.StellarSdk = require('../src/index');
Expand All @@ -10,7 +12,6 @@ if (typeof window === 'undefined') {
global.sinon = require('sinon');
global.expect = global.chai.expect;
} else {
// eslint-disable-next-line no-undef
window.axios = StellarSdk.axios;
window.HorizonAxiosClient = StellarSdk.HorizonAxiosClient;
}
Loading