Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async submit(jobSpec: JobConfig, shouldNotStart?: boolean): Promise {
if (!jobSpec) throw new TSError('submit requires a jobSpec');
const job: JobIDResponse = await this.post('/jobs', jobSpec, { query: { start: !shouldNotStart } });
// support older version of teraslice
if (!job.ex_id) {
const { ex_id: exId } = await this.get(`/jobs/${job.job_id}/ex`);
return this.wrap(exId);
}
return this.wrap(job.ex_id);
}
function makeErrorFromResponse(response: any): OldErrorOutput {
const { statusCode } = response;
const stuff = getErrorFromResponse(response);
const {
message = STATUS_CODES[statusCode],
code = statusCode
} = stuff;
const error: Partial = new TSError(message);
error.error = code; // for legacy support
error.code = code;
error.statusCode = code;
return error as OldErrorOutput;
}
constructor(config: ClientConfig, jobId: string) {
super(config);
if (!jobId) {
throw new TSError('Job requires jobId');
}
if (!isString(jobId)) {
throw new TSError('Job requires jobId to be a string');
}
this._jobId = jobId;
this.slicer = _deprecateSlicerName(this.slicer);
// @ts-ignore
autoBind(this);
}
const newEndpoint = getAPIEndpoint(endpoint, this._apiVersion);
try {
const response = await this._request[method](newEndpoint, options);
const { body } = response;
return body;
} catch (err) {
const { statusCode } = err;
if (statusCode >= 400) {
const error = makeErrorFromResponse(err);
throw error;
}
// TODO: what additional parameters should we return here?
throw new TSError(err);
}
}
async changeWorkers(
action: ChangeWorkerQueryParams,
workerNum: number,
requestOptions: RequestOptions = {}
): Promise {
if (action == null || workerNum == null) {
throw new TSError('changeWorkers requires action and count');
}
if (!['add', 'remove', 'total'].includes(action)) {
throw new TSError('changeWorkers requires action to be one of add, remove, or total');
}
const query = { [action]: workerNum };
requestOptions.json = false;
const options = this.makeOptions(query, requestOptions);
const response = await this.post(`/ex/${this._exId}/_workers`, null, options);
// for backwards compatability
if (typeof response === 'string') {
try {
return this.parse(response);
} catch (err) {
// do nothing
function validateExId(exId?: string) {
if (!exId) {
throw new TSError('Ex requires exId');
}
if (!isString(exId)) {
throw new TSError('Ex requires exId to be a string');
}
}
private async _makeRequest(
method: string,
endpoint: string,
searchOptions: SearchOptions = {},
data?: any
) {
const errorMsg = validateRequestOptions(endpoint, searchOptions);
if (errorMsg) return Promise.reject(new TSError(errorMsg));
let options;
if (data) {
options = getRequestOptionsWithData(data, searchOptions);
} else {
options = searchOptions;
}
const newEndpoint = getAPIEndpoint(endpoint, this._apiVersion);
try {
const response = await this._request[method](newEndpoint, options);
const { body } = response;
return body;
} catch (err) {
const { statusCode } = err;
function validateExId(exId?: string) {
if (!exId) {
throw new TSError('Ex requires exId');
}
if (!isString(exId)) {
throw new TSError('Ex requires exId to be a string');
}
}