Skip to content

Commit

Permalink
[teraslice, teraslice-client-js, terafoundation, scripts] update got …
Browse files Browse the repository at this point in the history
…from 11.8.3 to 13.0.0 (#3776)

This PR makes the following changes:
- update got from 11.8.3 to 13.0.0.
- remove `gotESM` module workaround from teraslice `api.ts`
- replace use of the removed `query` option field with `searchParams`.
- since `query` is no longer an option `RequestOptions` and
`SearchOptions` types were identical. They are just a partial list of
all got options. I chose to keep `RequestOptions` because that is more
general. Requests that make a search still use a parameter named
`searchOptions`, but it is now of type `RequestOptions`.
- `timeout` and `reply` options now take objects.
- add path to got types in `tsconfig.js`. Without this types are taken
from `@types/got` which is a dependency of `kubernetes-client`. We can
remove this once we go to `kubernetesV2`.

Note: got 14 requires node 20+, so we can only update to 13 at this
time.
  • Loading branch information
busma13 authored Sep 30, 2024
1 parent b3aaece commit fc80a6d
Show file tree
Hide file tree
Showing 18 changed files with 1,319 additions and 1,473 deletions.
4 changes: 3 additions & 1 deletion e2e/test/teraslice-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ export class TerasliceHarness {
let nodes = -1;
try {
const result = await this.teraslice.cluster.get('cluster/state', {
timeout: 500,
timeout: {
request: 500
},
responseType: 'json',
});
nodes = Object.keys(result).length;
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"execa": "9.4.0",
"fs-extra": "^11.2.0",
"globby": "^11.0.4",
"got": "^11.8.3",
"got": "^13.0.0",
"ip": "^2.0.1",
"js-yaml": "^4.1.0",
"kafkajs": "^2.2.4",
Expand Down
24 changes: 18 additions & 6 deletions packages/scripts/src/helpers/test-runner/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ async function checkRestrainedOpensearch(
https: { rejectUnauthorized: false },
responseType: 'json',
throwHttpErrors: true,
retry: 0
retry: {
limit: 0
}
}));
} catch (err) {
error = err.message;
Expand Down Expand Up @@ -468,7 +470,9 @@ async function checkOpensearch(options: TestOptions, startTime: number): Promise
https: { rejectUnauthorized: false },
responseType: 'json',
throwHttpErrors: true,
retry: 0,
retry: {
limit: 0
}
}));
} catch (err) {
error = err.message;
Expand Down Expand Up @@ -532,7 +536,9 @@ async function checkRestrainedElasticsearch(
({ body } = await got(host, {
responseType: 'json',
throwHttpErrors: true,
retry: 0,
retry: {
limit: 0
}
}));
} catch (err) {
error = err.message;
Expand Down Expand Up @@ -594,7 +600,9 @@ async function checkElasticsearch(options: TestOptions, startTime: number): Prom
({ body } = await got(host, {
responseType: 'json',
throwHttpErrors: true,
retry: 0,
retry: {
limit: 0
}
}));
} catch (err) {
error = err.message;
Expand Down Expand Up @@ -661,7 +669,9 @@ async function checkMinio(options: TestOptions, startTime: number): Promise<void
https: config.ENCRYPT_MINIO
? { certificateAuthority: fs.readFileSync(rootCaPath) }
: {},
retry: 0,
retry: {
limit: 0
}
}));
} catch (err) {
error = err.message;
Expand Down Expand Up @@ -712,7 +722,9 @@ async function checkRabbitMQ(options: TestOptions, startTime: number): Promise<v
prefixUrl: managementEndpoint,
responseType: 'json',
throwHttpErrors: false,
retry: 0,
retry: {
limit: 0
},
username: config.RABBITMQ_USER,
password: config.RABBITMQ_PASSWORD
}));
Expand Down
2 changes: 1 addition & 1 deletion packages/terafoundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@types/js-yaml": "^4.0.9",
"@types/mongoose": "^5.10.3",
"@types/redis": "^4.0.11",
"got": "^11.8.3"
"got": "^13.0.0"
},
"engines": {
"node": ">=18.18.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/teraslice-client-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@terascope/types": "^1.1.0",
"@terascope/utils": "^1.1.0",
"auto-bind": "^4.0.0",
"got": "^11.8.3"
"got": "^13.0.0"
},
"devDependencies": {
"nock": "^13.5.5"
Expand Down
10 changes: 5 additions & 5 deletions packages/teraslice-client-js/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Teraslice } from '@terascope/types';
import path from 'node:path';
import autoBind from 'auto-bind';
import Client from './client.js';
import { PostData, SearchOptions, ClientConfig } from './interfaces.js';
import { PostData, RequestOptions, ClientConfig } from './interfaces.js';

export default class Assets extends Client {
constructor(config: ClientConfig) {
Expand All @@ -30,7 +30,7 @@ export default class Assets extends Client {

async remove(
id: string,
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Teraslice.AssetIDResponse> {
if (isEmpty(id)) {
throw new TSError('Asset delete requires a ID', {
Expand All @@ -44,7 +44,7 @@ export default class Assets extends Client {

async list(
query: Teraslice.SearchQuery = {},
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Teraslice.AssetRecord[]> {
const options = { ...searchOptions, searchParams: query };
return this.get('/assets', options);
Expand All @@ -53,7 +53,7 @@ export default class Assets extends Client {
async getAsset(
name: string,
version = '',
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Teraslice.AssetRecord[]> {
if (!name || !isString(name)) {
throw new TSError('Name is required, and must be of type string', {
Expand All @@ -75,7 +75,7 @@ export default class Assets extends Client {
name = '',
version = '',
query: Teraslice.TxtSearchParams = {},
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<string> {
if (name && !isString(name)) {
throw new TSError('Name must be of type string', {
Expand Down
22 changes: 12 additions & 10 deletions packages/teraslice-client-js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
} from '@terascope/utils';
import { STATUS_CODES } from 'node:http';
import { URL } from 'node:url';
import got, { Got } from 'got';
import { ClientConfig, SearchOptions, RequestOptions } from './interfaces.js';
import got, { type Got, HTTPError } from 'got';
import { ClientConfig, RequestOptions } from './interfaces.js';

export default class Client {
private readonly _apiVersion: string;
Expand All @@ -32,12 +32,14 @@ export default class Client {
limit: isTest ? 0 : 3,
maxRetryAfter: 15000 // 15 seconds
},
timeout: config.timeout,
timeout: {
request: config.timeout
},
responseType: 'json'
});
}

async get<T = any>(endpoint: string, options?: SearchOptions): Promise<T> {
async get<T = any>(endpoint: string, options?: RequestOptions): Promise<T> {
return this._makeRequest<T>('get', endpoint, options);
}

Expand All @@ -49,14 +51,14 @@ export default class Client {
return this._makeRequest<T>('put', endpoint, options, data);
}

async delete<T = any>(endpoint: string, options?: SearchOptions): Promise<T> {
async delete<T = any>(endpoint: string, options?: RequestOptions): Promise<T> {
return this._makeRequest<T>('delete', endpoint, options);
}

private async _makeRequest<T = any>(
method: 'get' | 'post' | 'put' | 'delete',
endpoint: string,
searchOptions?: RequestOptions | SearchOptions,
searchOptions?: RequestOptions,
data?: any
): Promise<T> {
const errorMsg = validateRequestOptions(endpoint, searchOptions);
Expand Down Expand Up @@ -93,7 +95,7 @@ export default class Client {
...options,
} as any) as any as T;
} catch (err) {
if (err instanceof got.HTTPError) {
if (err instanceof HTTPError) {
throw makeErrorFromResponse(err.response);
}

Expand All @@ -106,7 +108,7 @@ export default class Client {
}

protected makeOptions(
searchParams: Record<string, any> | undefined, options: RequestOptions | SearchOptions
searchParams: Record<string, any> | undefined, options: RequestOptions
): RequestOptions {
return { ...options, searchParams };
}
Expand Down Expand Up @@ -175,7 +177,7 @@ function makeErrorFromResponse(response: any): OldErrorOutput {
}

// TODO: do more validations
function validateRequestOptions(endpoint: string, _options?: RequestOptions | SearchOptions) {
function validateRequestOptions(endpoint: string, _options?: RequestOptions) {
if (!endpoint) {
return 'endpoint must not be empty';
}
Expand All @@ -186,7 +188,7 @@ function validateRequestOptions(endpoint: string, _options?: RequestOptions | Se
}

function getRequestOptionsWithData(
data: any, options: RequestOptions | SearchOptions
data: any, options: RequestOptions
): RequestOptions {
if (isPlainObject(data) || Array.isArray(data)) {
return { ...options, json: data };
Expand Down
16 changes: 9 additions & 7 deletions packages/teraslice-client-js/src/ex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { Teraslice } from '@terascope/types';
import autoBind from 'auto-bind';
import Client from './client.js';
import { ClientConfig, SearchOptions, RequestOptions } from './interfaces.js';
import { ClientConfig, RequestOptions } from './interfaces.js';

export default class Ex extends Client {
private readonly _exId: string;
Expand All @@ -25,31 +25,31 @@ export default class Ex extends Client {

async stop(
query?: Teraslice.StopQuery,
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Teraslice.ApiStoppedResponse> {
const options = this.makeOptions(query, searchOptions);
return this.post(`/ex/${this._exId}/_stop`, null, options);
}

async pause(
query?: Teraslice.SearchQuery,
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Teraslice.ApiPausedResponse> {
const options = this.makeOptions(query, searchOptions);
return this.post(`/ex/${this._exId}/_pause`, null, options);
}

async resume(
query?: Teraslice.SearchQuery,
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Teraslice.ApiResumeResponse> {
const options = this.makeOptions(query, searchOptions);
return this.post(`/ex/${this._exId}/_resume`, null, options);
}

async recover(
query: Teraslice.RecoverQuery = {},
searchOptions: SearchOptions = {}
searchOptions: RequestOptions = {}
): Promise<Ex> {
const options = this.makeOptions(query, searchOptions);
const result: Teraslice.ApiJobCreateResponse = await this.post(`/ex/${this._exId}/_recover`, null, options);
Expand Down Expand Up @@ -88,7 +88,7 @@ export default class Ex extends Client {
async errors(options?: Teraslice.SearchQuery): Promise<Teraslice.ErrorRecord[]> {
return this.get(`/ex/${this._exId}/errors`, {
searchParams: options,
} as SearchOptions);
});
}

async changeWorkers(
Expand Down Expand Up @@ -141,7 +141,9 @@ export default class Ex extends Client {
const startTime = Date.now();
const options = Object.assign({}, {
responseType: 'json',
timeout: intervalMs < 1000 ? 1000 : intervalMs,
timeout: {
request: intervalMs < 1000 ? 1000 : intervalMs
},
}, requestOptions);

const checkStatus = async (): Promise<Teraslice.ExecutionStatus> => {
Expand Down
8 changes: 4 additions & 4 deletions packages/teraslice-client-js/src/executions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import autoBind from 'auto-bind';
import Client from './client.js';
import Ex from './ex.js';

import { ClientConfig, SearchOptions } from './interfaces';
import { ClientConfig, RequestOptions } from './interfaces';

type ListOptions = undefined | string | Teraslice.SearchQuery;

Expand Down Expand Up @@ -39,7 +39,7 @@ export default class Executions extends Client {

async list(options?: ListOptions): Promise<Teraslice.ExecutionConfig[]> {
const query = _parseListOptions(options);
return this.get('/ex', { searchParams: query } as SearchOptions);
return this.get('/ex', { searchParams: query } as RequestOptions);
}

async errors(
Expand All @@ -52,14 +52,14 @@ export default class Executions extends Client {
options.searchParams = opts;
}

return this.get(`/ex/${exId}/errors`, options as SearchOptions);
return this.get(`/ex/${exId}/errors`, options as RequestOptions);
}

if (isPlainObject(exId)) {
options.searchParams = exId;
}

return this.get('/ex/errors', options as SearchOptions);
return this.get('/ex/errors', options as RequestOptions);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/teraslice-client-js/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Omit } from '@terascope/utils';
import * as got from 'got';
import { Options } from 'got';

export interface ClientConfig {
host?: string;
Expand All @@ -8,8 +7,6 @@ export interface ClientConfig {
timeout?: number;
}

export type RequestOptions = got.Options;

export type SearchOptions = Omit<got.Options, 'searchParams'>;
export type RequestOptions = Partial<Options>;

export type PostData = string | NodeJS.ReadableStream | Buffer;
Loading

0 comments on commit fc80a6d

Please sign in to comment.