Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 14c510a

Browse files
authored
Merge pull request #455 from cloudant/454-double-callback
454 double callback
2 parents 2d556d8 + bc62d7a commit 14c510a

File tree

12 files changed

+155
-111
lines changed

12 files changed

+155
-111
lines changed

.mocharc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"exclude": "test/typescript/*.js",
3+
"recursive": true,
4+
"timeout": 60000
5+
}

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ branches:
66
- rewrite
77
node_js:
88
- "node"
9-
- "10"
10-
- "8"
9+
- "14"
10+
- "12"
1111
os:
1212
- linux
1313
before_install:

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Unreleased
2+
- [FIXED] Parsing of max-age from Set-Cookie headers.
3+
- [FIXED] Double callback if plugin errors when updating state.
4+
- [NOTE] Updated engines to remove EOL Node.js versions (minimum is now Node.js 12).
5+
16
# 4.3.1 (2021-03-17)
27
- [NEW] Add migration guide to the newly supported cloudant-node-sdk
38
(package: @ibm-cloud/cloudant).

Jenkinsfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ stage('Build') {
7676

7777
stage('QA') {
7878
parallel([
79-
Node8x : {
80-
//8.x LTS
81-
setupNodeAndTest('lts/carbon')
79+
Node12x : {
80+
//12.x LTS
81+
setupNodeAndTest('lts/erbium')
8282
},
83-
Node10x : {
84-
//10.x LTS
85-
setupNodeAndTest('lts/dubnium')
83+
Node14x : {
84+
//14.x LTS
85+
setupNodeAndTest('lts/fermium')
8686
},
8787
Node : {
8888
// Current

lib/clientutils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017, 2018 IBM Corp. All rights reserved.
1+
// Copyright © 2017, 2021 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -67,7 +67,7 @@ var processState = function(r, callback) {
6767
if (r.abort) {
6868
// [1] => Client has called for the request to be aborted.
6969
abort();
70-
callback(new Error('Client issued abort')); // no retry
70+
setImmediate(callback, new Error('Client issued abort')); // no retry
7171
return;
7272
}
7373

@@ -77,15 +77,15 @@ var processState = function(r, callback) {
7777
sendResponseToClient(r.state.abortWithResponse, r.clientStream, r.clientCallback);
7878
var err = new Error('Plugin issued abort');
7979
err.skipClientCallback = true;
80-
callback(err); // no retry
80+
setImmediate(callback, err); // no retry
8181
return;
8282
}
8383

8484
if (r.state.retry && r.state.attempt < r.state.maxAttempt) {
8585
// [3] => One or more plugins have called for the request to be retried.
8686
abort();
8787
debug('Plugin issued a retry.');
88-
callback(); // retry
88+
setImmediate(callback); // retry
8989
return;
9090
}
9191

@@ -104,7 +104,7 @@ var processState = function(r, callback) {
104104

105105
if (!r.state.sending) {
106106
// [4] => Request has not yet been sent. Still processing 'onRequest' hooks.
107-
callback(); // continue
107+
setImmediate(callback); // continue
108108
return;
109109
}
110110

@@ -120,7 +120,7 @@ var processState = function(r, callback) {
120120
}
121121

122122
// [5] => Return response to awaiting client.
123-
callback(new Error('No retry requested')); // no retry
123+
setImmediate(callback, new Error('No retry requested')); // no retry
124124
};
125125

126126
// execute a specified hook for all plugins

lib/tokens/TokenManager.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2019 IBM Corp. All rights reserved.
1+
// Copyright © 2019, 2021 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -32,7 +32,9 @@ class TokenManager {
3232
_autoRenew(defaultMaxAgeSecs) {
3333
debug('Auto renewing token now...');
3434
this._renew().then((response) => {
35-
let maxAgeSecs = cookie.parse(response.headers['set-cookie'][0])['Max-Age'] || defaultMaxAgeSecs;
35+
let setCookieHeader = response.headers['set-cookie'];
36+
let headerValue = Array.isArray(setCookieHeader) ? setCookieHeader[0] : setCookieHeader;
37+
let maxAgeSecs = cookie.parse(headerValue)['Max-Age'] || defaultMaxAgeSecs;
3638
let delayMSecs = maxAgeSecs / 2 * 1000;
3739
debug(`Renewing token in ${delayMSecs} milliseconds.`);
3840
setTimeout(this._autoRenew.bind(this, defaultMaxAgeSecs), delayMSecs).unref();

package-lock.json

Lines changed: 35 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"eslint-plugin-standard": "^3.0.1",
4848
"md5-file": "^3.2.3",
4949
"mocha": "^7.1.2",
50-
"nock": "^9.2.5",
50+
"nock": "^13",
5151
"should": "6.0.3",
5252
"typescript": "^2.8.3",
5353
"uuid": "^3.0.1"
@@ -58,11 +58,12 @@
5858
"test-live": "NOCK_OFF=true mocha",
5959
"test-live-verbose": "env DEBUG='*,-mocha:*' npm run test-live",
6060
"tslint": "tslint --project types",
61-
"type-check": "tsc --project types/tsconfig.json"
61+
"type-check": "tsc --project types/tsconfig.json",
62+
"mocha": "mocha"
6263
},
6364
"main": "./cloudant.js",
6465
"types": "types",
6566
"engines": {
66-
"node": ">=6.13.0"
67+
"node": ">=12"
6768
}
6869
}

plugins/cookieauth.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2015, 2019 IBM Corp. All rights reserved.
1+
// Copyright © 2015, 2021 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -68,16 +68,13 @@ class CookiePlugin extends BasePlugin {
6868
delete req.url;
6969
req.uri = u.format(new u.URL(req.uri), {auth: false});
7070

71-
self._tokenManager.renewIfRequired().then(() => {
72-
callback(state);
73-
}).catch((error) => {
71+
self._tokenManager.renewIfRequired().catch((error) => {
7472
if (state.attempt < state.maxAttempt) {
7573
state.retry = true;
7674
} else {
7775
state.abortWithResponse = [ error ]; // return error to client
7876
}
79-
callback(state);
80-
});
77+
}).finally(() => callback(state));
8178
}
8279

8380
onResponse(state, response, callback) {

plugins/iamauth.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017, 2019 IBM Corp. All rights reserved.
1+
// Copyright © 2017, 2021 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -66,9 +66,7 @@ class IAMPlugin extends BasePlugin {
6666
delete req.url;
6767
req.uri = u.format(new u.URL(req.uri), {auth: false});
6868

69-
self._tokenManager.renewIfRequired().then(() => {
70-
callback(state);
71-
}).catch((error) => {
69+
self._tokenManager.renewIfRequired().catch((error) => {
7270
debug(error);
7371
if (state.attempt < state.maxAttempt) {
7472
state.retry = true;
@@ -85,8 +83,7 @@ class IAMPlugin extends BasePlugin {
8583
} else {
8684
state.abortWithResponse = [ error ]; // return error to client
8785
}
88-
callback(state);
89-
});
86+
}).finally(() => callback(state));
9087
}
9188

9289
onResponse(state, response, callback) {

0 commit comments

Comments
 (0)