Skip to content

Commit

Permalink
feat(connectors): Remove http timeout not properly taken into account…
Browse files Browse the repository at this point in the history
… by fetch spec
  • Loading branch information
burgerni10 committed Sep 4, 2023
1 parent 4271284 commit 00878f3
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 159 deletions.
8 changes: 0 additions & 8 deletions backend/src/north/north-oianalytics/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ const manifest: NorthConnectorManifest = {
defaultValue: 'https://instance_name.oianalytics.fr',
displayInViewMode: true
},
{
key: 'timeout',
type: 'OibNumber',
label: 'Timeout',
defaultValue: 30,
unitLabel: 's',
validators: [{ key: 'required' }]
},
{
key: 'acceptUnauthorized',
type: 'OibCheckbox',
Expand Down
13 changes: 1 addition & 12 deletions backend/src/north/north-oianalytics/north-oianalytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ describe('NorthOIAnalytics without proxy', () => {
enabled: true,
settings: {
host: 'https://hostname/',
timeout: 1000,
acceptUnauthorized: false,
accessKey: 'anyUser',
secretKey: 'anypass',
Expand Down Expand Up @@ -144,7 +143,6 @@ describe('NorthOIAnalytics without proxy', () => {
'Content-Type': 'application/json'
},
body: JSON.stringify(values),
timeout: configuration.settings.timeout * 1000,
agent: undefined
};

Expand Down Expand Up @@ -201,7 +199,6 @@ describe('NorthOIAnalytics without proxy', () => {
'Content-Type': 'application/json'
},
body: JSON.stringify(values),
timeout: configuration.settings.timeout * 1000,
agent: undefined
};

Expand Down Expand Up @@ -234,7 +231,6 @@ describe('NorthOIAnalytics without proxy', () => {
'content-type': expect.stringContaining('multipart/form-data; boundary=')
},
body: expect.anything(),
timeout: configuration.settings.timeout * 1000,
agent: undefined
};

Expand Down Expand Up @@ -292,7 +288,6 @@ describe('NorthOIAnalytics without proxy', () => {
'content-type': expect.stringContaining('multipart/form-data; boundary=')
},
body: expect.anything(),
timeout: configuration.settings.timeout * 1000,
agent: undefined
};

Expand Down Expand Up @@ -322,8 +317,7 @@ describe('NorthOIAnalytics without proxy', () => {
await expect(north.testConnection()).rejects.toThrow(`Fetch error ${new Error('Timeout error')}`);
expect(fetch).toHaveBeenCalledWith('https://hostname/info', {
headers: { authorization: 'Basic YW55VXNlcjphbnlwYXNz' },
method: 'POST',
timeout: 10000
method: 'POST'
});
expect(logger.error).toHaveBeenCalledWith(`Fetch error ${new Error('Timeout error')}`);
});
Expand Down Expand Up @@ -360,7 +354,6 @@ describe('NorthOIAnalytics without proxy but with acceptUnauthorized', () => {
enabled: true,
settings: {
host: 'https://hostname',
timeout: 1000,
acceptUnauthorized: true,
accessKey: 'anyUser',
secretKey: null,
Expand Down Expand Up @@ -408,7 +401,6 @@ describe('NorthOIAnalytics without proxy but with acceptUnauthorized', () => {
'Content-Type': 'application/json'
},
body: JSON.stringify(values),
timeout: configuration.settings.timeout * 1000,
agent: expect.any(https.Agent)
};

Expand All @@ -431,7 +423,6 @@ describe('NorthOIAnalytics without proxy but with acceptUnauthorized', () => {
'content-type': expect.stringContaining('multipart/form-data; boundary=')
},
body: expect.anything(),
timeout: configuration.settings.timeout * 1000,
agent: expect.any(https.Agent)
};

Expand All @@ -453,7 +444,6 @@ describe('NorthOIAnalytics with proxy', () => {
enabled: true,
settings: {
host: 'https://hostname',
timeout: 1000,
acceptUnauthorized: false,
accessKey: 'anyUser',
secretKey: 'anypass',
Expand Down Expand Up @@ -509,7 +499,6 @@ describe('NorthOIAnalytics with proxy but without proxy password', () => {
enabled: true,
settings: {
host: 'https://hostname',
timeout: 1000,
acceptUnauthorized: false,
accessKey: 'anyUser',
secretKey: 'anypass',
Expand Down
15 changes: 6 additions & 9 deletions backend/src/north/north-oianalytics/north-oianalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import pino from 'pino';
import { createReadStream } from 'node:fs';
import FormData from 'form-data';
import path from 'node:path';
import fetch from 'node-fetch';
import fetch, { HeadersInit, RequestInit } from 'node-fetch';
import https from 'node:https';
import { HandlesFile, HandlesValues } from '../north-interface';
import { filesExists } from '../../service/utils';
Expand Down Expand Up @@ -65,16 +65,15 @@ export default class NorthOIAnalytics extends NorthConnector<NorthOIAnalyticsSet
override async testConnection(): Promise<void> {
this.logger.info(`Testing connection on "${this.connector.settings.host}"`);

const headers: Record<string, string | number> = {};
const headers: HeadersInit = {};
const basic = Buffer.from(
`${this.connector.settings.accessKey}:${await this.encryptionService.decryptText(this.connector.settings.secretKey!)}`
).toString('base64');
headers.authorization = `Basic ${basic}`;
const fetchOptions: Record<string, any> = {
const fetchOptions: RequestInit = {
method: 'POST',
headers,
agent: this.proxyAgent,
timeout: 10000
agent: this.proxyAgent
};
const requestUrl = `${this.connector.settings.host}/info`;

Expand All @@ -96,7 +95,7 @@ export default class NorthOIAnalytics extends NorthConnector<NorthOIAnalyticsSet
* Handle values by sending them to OIAnalytics
*/
async handleValues(values: Array<OIBusDataValue>): Promise<void> {
const headers: Record<string, string> = {
const headers: HeadersInit = {
'Content-Type': 'application/json'
};

Expand All @@ -113,7 +112,6 @@ export default class NorthOIAnalytics extends NorthConnector<NorthOIAnalyticsSet
method: 'POST',
headers,
body: JSON.stringify(values),
timeout: this.connector.settings.timeout * 1000,
agent: this.proxyAgent
});
} catch (fetchError) {
Expand All @@ -135,7 +133,7 @@ export default class NorthOIAnalytics extends NorthConnector<NorthOIAnalyticsSet
* Handle the file by sending it to OIAnalytics.
*/
async handleFile(filePath: string): Promise<void> {
const headers: Record<string, string> = {};
const headers: HeadersInit = {};
headers.authorization = `Basic ${Buffer.from(
`${this.connector.settings.accessKey}:${
this.connector.settings.secretKey ? await this.encryptionService.decryptText(this.connector.settings.secretKey) : ''
Expand Down Expand Up @@ -164,7 +162,6 @@ export default class NorthOIAnalytics extends NorthConnector<NorthOIAnalyticsSet
method: 'POST',
headers,
body,
timeout: this.connector.settings.timeout * 1000,
agent: this.proxyAgent
});
readStream.close();
Expand Down
1 change: 0 additions & 1 deletion backend/src/north/north-oibus/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const manifest: NorthConnectorManifest = {
],
displayInViewMode: true
},
{ key: 'timeout', type: 'OibNumber', label: 'Timeout', unitLabel: 's', defaultValue: 30, validators: [{ key: 'required' }] },
{
key: 'acceptUnauthorized',
type: 'OibCheckbox',
Expand Down
12 changes: 1 addition & 11 deletions backend/src/north/north-oibus/north-oibus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ describe('NorthOIConnect with proxy', () => {
settings: {
host: 'https://hostname/',
acceptUnauthorized: false,
timeout: 10,
useProxy: true,
proxyUrl: 'http://localhost',
proxyUsername: 'my username',
Expand Down Expand Up @@ -158,7 +157,6 @@ describe('NorthOIConnect with proxy', () => {
'Content-Type': 'application/json'
},
body: JSON.stringify(values),
timeout: configuration.settings.timeout * 1000,
agent: {}
};

Expand Down Expand Up @@ -212,7 +210,6 @@ describe('NorthOIConnect with proxy', () => {
'Content-Type': 'application/json'
},
body: JSON.stringify(values),
timeout: configuration.settings.timeout * 1000,
agent: {}
};

Expand Down Expand Up @@ -242,7 +239,6 @@ describe('NorthOIConnect with proxy', () => {
'content-type': expect.stringContaining('multipart/form-data; boundary=')
},
body: expect.anything(),
timeout: configuration.settings.timeout * 1000,
agent: {}
};

Expand Down Expand Up @@ -295,7 +291,6 @@ describe('NorthOIConnect with proxy', () => {
'content-type': expect.stringContaining('multipart/form-data; boundary=')
},
body: expect.anything(),
timeout: configuration.settings.timeout * 1000,
agent: {}
};

Expand Down Expand Up @@ -323,8 +318,7 @@ describe('NorthOIConnect with proxy', () => {
expect(fetch).toHaveBeenCalledWith('https://hostname/api/info', {
agent: {},
headers: { authorization: 'Basic dXNlcjpwYXNz' },
method: 'GET',
timeout: 10000
method: 'GET'
});
expect(logger.error).toHaveBeenCalledWith(`Fetch error ${new Error('Timeout error')}`);
});
Expand Down Expand Up @@ -362,7 +356,6 @@ describe('NorthOIConnect with proxy but without proxy password', () => {
settings: {
host: 'https://hostname',
acceptUnauthorized: false,
timeout: 10,
useProxy: true,
proxyUrl: 'http://localhost',
proxyUsername: 'my username',
Expand Down Expand Up @@ -416,7 +409,6 @@ describe('NorthOIConnect without proxy with acceptUnauthorized', () => {
settings: {
host: 'https://hostname',
acceptUnauthorized: true,
timeout: 10,
useProxy: false,
username: 'user',
password: 'pass'
Expand Down Expand Up @@ -462,7 +454,6 @@ describe('NorthOIConnect without proxy with acceptUnauthorized', () => {
'Content-Type': 'application/json'
},
body: JSON.stringify(values),
timeout: configuration.settings.timeout * 1000,
agent: expect.any(https.Agent)
};

Expand All @@ -482,7 +473,6 @@ describe('NorthOIConnect without proxy with acceptUnauthorized', () => {
'content-type': expect.stringContaining('multipart/form-data; boundary=')
},
body: expect.anything(),
timeout: configuration.settings.timeout * 1000,
agent: expect.any(https.Agent)
};

Expand Down
15 changes: 6 additions & 9 deletions backend/src/north/north-oibus/north-oibus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NorthConnectorDTO } from '../../../../shared/model/north-connector.mode
import EncryptionService from '../../service/encryption.service';
import RepositoryService from '../../service/repository.service';
import pino from 'pino';
import fetch from 'node-fetch';
import fetch, { HeadersInit, RequestInit } from 'node-fetch';
import { createReadStream } from 'node:fs';
import path from 'node:path';
import FormData from 'form-data';
Expand Down Expand Up @@ -62,16 +62,15 @@ export default class NorthOibus extends NorthConnector<NorthOIBusSettings> imple
override async testConnection(): Promise<void> {
this.logger.info(`Testing connection on "${this.connector.settings.host}"`);

const headers: Record<string, string | number> = {};
const headers: HeadersInit = {};
const basic = Buffer.from(
`${this.connector.settings.username}:${await this.encryptionService.decryptText(this.connector.settings.password!)}`
).toString('base64');
headers.authorization = `Basic ${basic}`;
const fetchOptions: Record<string, any> = {
const fetchOptions: RequestInit = {
method: 'GET',
headers,
agent: this.proxyAgent,
timeout: 10000
agent: this.proxyAgent
};
const requestUrl = `${this.connector.settings.host}/api/info`;

Expand All @@ -93,7 +92,7 @@ export default class NorthOibus extends NorthConnector<NorthOIBusSettings> imple
* Handle values by sending them to the specified endpoint
*/
async handleValues(values: Array<OIBusDataValue>): Promise<void> {
const headers: Record<string, string> = {
const headers: HeadersInit = {
'Content-Type': 'application/json'
};
headers.authorization = `Basic ${Buffer.from(
Expand All @@ -107,7 +106,6 @@ export default class NorthOibus extends NorthConnector<NorthOIBusSettings> imple
method: 'POST',
headers,
body: JSON.stringify(values),
timeout: this.connector.settings.timeout * 1000,
agent: this.proxyAgent
});
} catch (fetchError) {
Expand All @@ -129,7 +127,7 @@ export default class NorthOibus extends NorthConnector<NorthOIBusSettings> imple
* Handle the file by sending it to the specified endpoint
*/
async handleFile(filePath: string): Promise<void> {
const headers: Record<string, string> = {};
const headers: HeadersInit = {};
headers.authorization = `Basic ${Buffer.from(
`${this.connector.settings.username}:${await this.encryptionService.decryptText(this.connector.settings.password!)}`
).toString('base64')}`;
Expand All @@ -156,7 +154,6 @@ export default class NorthOibus extends NorthConnector<NorthOIBusSettings> imple
method: 'POST',
headers,
body,
timeout: this.connector.settings.timeout * 1000,
agent: this.proxyAgent
});
readStream.close();
Expand Down
1 change: 0 additions & 1 deletion backend/src/north/north-rest-api/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const manifest: NorthConnectorManifest = {
validators: [{ key: 'required' }],
displayInViewMode: true
},
{ key: 'timeout', type: 'OibNumber', label: 'Timeout', newRow: true, validators: [{ key: 'required' }] },
...proxy,
{
key: 'authentication',
Expand Down
Loading

0 comments on commit 00878f3

Please sign in to comment.