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

Expose and use once #664

Merged
merged 5 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not 100% sure that 'socket' event can only be sent once, and the body of the function seems to imply it might be more than once. Do you know of a guarantee that this is at-most-once?

Copy link
Contributor Author

@tinovyatkin tinovyatkin Jul 30, 2019

Choose a reason for hiding this comment

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

Look at the code - it checks a boolean flag socket.connecting (https://nodejs.org/api/net.html#net_socket_connecting) and in case of true waits for connection to be established by setting other event listener (socket.once).
In both cases this function are finishing with res.end() - so, I'm pretty sure it's designed to be running by once.
I can keep an on, as you wish, but see no error in this refactoring.

Copy link
Contributor Author

@tinovyatkin tinovyatkin Jul 30, 2019

Choose a reason for hiding this comment

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

@rattrayalex-stripe In fact, not, keeping on is an error. Just trace what will happen if this event handler will be called more than once - it will attempt to write to the req after calling req.end in previous call or in socket.once event handler.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay great, I think that looks right. Thanks for digging here.

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,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🧐

Copy link
Contributor

Choose a reason for hiding this comment

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

heh, oops! Good catch.


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