weaviate-client
Version:
JS/TS client for Weaviate
59 lines (58 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const abort_controller_x_1 = require("abort-controller-x");
const nice_grpc_1 = require("nice-grpc");
const errors_js_1 = require("../errors.js");
const base_js_1 = require("../proto/v1/base.js");
class Base {
connection;
collection;
timeout;
consistencyLevel;
tenant;
metadata;
abortSignal;
constructor(connection, collection, metadata, timeout, consistencyLevel, tenant, abortSignal) {
this.connection = connection;
this.collection = collection;
this.metadata = metadata;
this.timeout = timeout;
this.consistencyLevel = this.mapConsistencyLevel(consistencyLevel);
this.tenant = tenant;
this.abortSignal = abortSignal;
}
mapConsistencyLevel(consistencyLevel) {
switch (consistencyLevel) {
case 'ALL':
return base_js_1.ConsistencyLevel.CONSISTENCY_LEVEL_ALL;
case 'QUORUM':
return base_js_1.ConsistencyLevel.CONSISTENCY_LEVEL_QUORUM;
case 'ONE':
return base_js_1.ConsistencyLevel.CONSISTENCY_LEVEL_ONE;
default:
return base_js_1.ConsistencyLevel.CONSISTENCY_LEVEL_UNSPECIFIED;
}
}
sendWithTimeout = (send, errMkr) => {
const controller = new AbortController();
const signal = this.abortSignal
? AbortSignal.any([controller.signal, this.abortSignal])
: controller.signal;
const timeoutId = setTimeout(() => controller.abort(), this.timeout * 1000);
return send(signal)
.catch((err) => {
if (err instanceof nice_grpc_1.ServerError && err.code === nice_grpc_1.Status.PERMISSION_DENIED) {
throw new errors_js_1.WeaviateInsufficientPermissionsError(7, err.message);
}
if ((0, abort_controller_x_1.isAbortError)(err) && this.abortSignal === undefined) {
throw new errors_js_1.WeaviateRequestTimeoutError(`timed out after ${this.timeout * 1000}ms`);
}
if ((0, abort_controller_x_1.isAbortError)(err) && this.abortSignal !== undefined) {
throw err; // if the error is an abort error caused by the caller's abort signal, we re-throw it so that the caller can handle it as they see fit
}
throw errMkr(err);
})
.finally(() => clearTimeout(timeoutId));
};
}
exports.default = Base;