Skip to content

Commit

Permalink
Expose and use once (#664)
Browse files Browse the repository at this point in the history
* expose `once` method and fix misspeling

* use `once`

* remove off

* remove more `off`

* fix test
  • Loading branch information
tinovyatkin authored and rattrayalex-stripe committed Jul 30, 2019
1 parent c61ee46 commit ce71a59
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
19 changes: 11 additions & 8 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ StripeResource.prototype = {
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', () => {
res.once('end', () => {
const headers = res.headers || {};
// NOTE: Stripe responds with lowercase header names/keys.

Expand Down Expand Up @@ -365,7 +365,7 @@ StripeResource.prototype = {

req.setTimeout(timeout, this._timeoutHandler(timeout, req, callback));

req.on('response', (res) => {
req.once('response', (res) => {
if (this._shouldRetry(res, requestRetries)) {
return retryRequest(makeRequest, apiVersion, headers, requestRetries);
} else {
Expand All @@ -381,13 +381,16 @@ StripeResource.prototype = {
}
});

req.on('socket', (socket) => {
req.once('socket', (socket) => {
if (socket.connecting) {
socket.on(isInsecureConnection ? 'connect' : 'secureConnect', () => {
// Send payload; we're safe:
req.write(requestData);
req.end();
});
socket.once(
isInsecureConnection ? 'connect' : 'secureConnect',
() => {
// Send payload; we're safe:
req.write(requestData);
req.end();
}
);
} else {
// we're already connected
req.write(requestData);
Expand Down
2 changes: 1 addition & 1 deletion lib/resources/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = StripeResource.extend({
.on('data', (line) => {
bufferArray.push(line);
})
.on('end', () => {
.once('end', () => {
const bufferData = Object.assign({}, data);
bufferData.file.data = Buffer.concat(bufferArray);
const buffer = fn(method, bufferData, headers);
Expand Down
3 changes: 2 additions & 1 deletion lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ function Stripe(key, version) {
value: new EventEmitter(),
enumerable: false,
configurable: false,
writeable: false,
writable: false,
});

this.on = this._emitter.on.bind(this._emitter);
this.once = this._emitter.once.bind(this._emitter);
this.off = this._emitter.removeListener.bind(this._emitter);

this._api = {
Expand Down
6 changes: 2 additions & 4 deletions test/flows.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ describe('Flows', function() {
.slice(2);

function onRequest(request) {
stripe.off('request', onRequest);

expect(request).to.eql({
api_version: 'latest',
idempotency_key: idempotencyKey,
Expand All @@ -339,7 +337,7 @@ describe('Flows', function() {
done();
}

stripe.on('request', onRequest);
stripe.once('request', onRequest);

stripe.charges
.create(
Expand Down Expand Up @@ -411,7 +409,7 @@ describe('Flows', function() {
done(new Error('How did you get here?'));
}

stripe.on('response', onResponse);
stripe.once('response', onResponse);
stripe.off('response', onResponse);

stripe.charges
Expand Down

0 comments on commit ce71a59

Please sign in to comment.