Skip to content

Commit

Permalink
Fix opensearch-project#253 Cannot read property 'then' of null in Tra…
Browse files Browse the repository at this point in the history
…nsport.js issue. (opensearch-project#283)

* Fix opensearch-project#253 Cannot read property 'then' of null in Transport.js issue.

Co-authored-by: aizaguirre <aizaguirre@tesla.com>
Signed-off-by: rawpixel-vincent <vincent@rawpixel.com>

* fix linter issues

Signed-off-by: rawpixel-vincent <vincent@rawpixel.com>

Signed-off-by: rawpixel-vincent <vincent@rawpixel.com>
Co-authored-by: aizaguirre <aizaguirre@tesla.com>
  • Loading branch information
2 people authored and nhtruong committed Feb 27, 2023
1 parent e3c8b63 commit 7253db0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
12 changes: 9 additions & 3 deletions lib/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ class Transport {
let request = { abort: noop };
const transportReturn = {
then(onFulfilled, onRejected) {
return p.then(onFulfilled, onRejected);
if (p != null) {
return p.then(onFulfilled, onRejected);
}
},
catch(onRejected) {
return p.catch(onRejected);
if (p != null) {
return p.catch(onRejected);
}
},
abort() {
meta.aborted = true;
Expand All @@ -194,7 +198,9 @@ class Transport {
return this;
},
finally(onFinally) {
return p.finally(onFinally);
if (p != null) {
return p.finally(onFinally);
}
},
};

Expand Down
98 changes: 96 additions & 2 deletions test/unit/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ const { test } = require('tap');
const { URL } = require('url');
const buffer = require('buffer');
const intoStream = require('into-stream');
const { ConnectionPool, Transport, Connection, errors } = require('../../index');
const { Client, buildServer } = require('../utils');
const {
ConnectionPool,
Transport,
Connection,
errors,
Client: ProductClient,
} = require('../../index');
const { Client, buildServer, connection } = require('../utils');
const { buildMockConnection } = connection;
let clientVersion = require('../../package.json').version;
if (clientVersion.includes('-')) {
clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p';
Expand Down Expand Up @@ -1189,3 +1196,90 @@ test('API compatibility header (x-ndjson)', (t) => {
});
});
});

test('Issue #253 with promises', async (t) => {
t.plan(1);

const delay = () => new Promise((resolve) => setTimeout(resolve, 10));

const MockConnection = buildMockConnection({
onRequest(params) {
return {
statusCode: 200,
headers: {},
body: {},
};
},
});

class MyTransport extends Transport {
request(params, options = {}, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}

if (typeof callback === 'undefined') {
return delay().then(() => super.request(params, options));
}

// Callback support
delay().then(() => super.request(params, options, callback));
}
}

const client = new ProductClient({
node: 'http://localhost:9200',
Transport: MyTransport,
Connection: MockConnection,
});

try {
await client.search({});
t.pass('ok');
} catch (err) {
t.fail(err);
}
});

test('Issue #253 with callbacks', (t) => {
t.plan(1);

const delay = () => new Promise((resolve) => setTimeout(resolve, 10));

const MockConnection = buildMockConnection({
onRequest(params) {
return {
statusCode: 200,
headers: {},
body: {},
};
},
});

class MyTransport extends Transport {
request(params, options = {}, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}

if (typeof callback === 'undefined') {
return delay().then(() => super.request(params, options));
}

// Callback support
delay().then(() => super.request(params, options, callback));
}
}

const client = new ProductClient({
node: 'http://localhost:9200',
Transport: MyTransport,
Connection: MockConnection,
});

client.search({}, (err, result) => {
t.error(err);
});
});

0 comments on commit 7253db0

Please sign in to comment.