UNPKG

@elastic.io/maester-client

Version:
92 lines (91 loc) 3.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectStorage = void 0; const get_stream_1 = __importDefault(require("get-stream")); const StorageClient_1 = require("./StorageClient"); const utils_1 = require("./utils"); class ObjectStorage { constructor(config, client) { this.client = client || new StorageClient_1.StorageClient(config); this.forwards = []; this.reverses = []; } async applyMiddlewares(getFreshStream, middlewares) { const stream = await getFreshStream(); return middlewares.reduce((_stream, middleware) => _stream.pipe(middleware()), stream); } async getDataByResponseType(data, responseType = 'json') { switch (responseType) { case 'stream': return data; case 'json': { const asJSON = await (0, get_stream_1.default)(data); return (0, utils_1.parseJson)(asJSON); } case 'arraybuffer': return get_stream_1.default.buffer(data); default: throw new Error(`Response type "${responseType}" is not supported`); } } payloadToStream(dataOrFunc) { if (typeof dataOrFunc === 'function') { return dataOrFunc; } return utils_1.streamFromData.bind({}, dataOrFunc); } async formStreamGetter(dataOrFunc) { const checkFreshStream = (0, utils_1.getFreshStreamChecker)(); return async () => { const getFreshStream = this.payloadToStream(dataOrFunc); const stream = await getFreshStream(); checkFreshStream(stream); return this.applyMiddlewares(getFreshStream, this.forwards); }; } use(forward, reverse) { this.forwards.push(forward); this.reverses.unshift(reverse); return this; } /** * @param dataOrFunc async function returning stream OR any data (except 'undefined') */ async add(dataOrFunc, reqWithBodyOptions) { const { data } = await this.client.post(await this.formStreamGetter(dataOrFunc), reqWithBodyOptions); return data.objectId; } /** * @param dataOrFunc async function returning stream OR any data (except 'undefined') */ async update(objectId, dataOrFunc, reqWithBodyOptions) { const { data } = await this.client.put(objectId, await this.formStreamGetter(dataOrFunc), reqWithBodyOptions); return data; } async getOne(objectId, reqOptions = {}) { const res = await this.client.get(objectId, reqOptions); const stream = await this.applyMiddlewares(() => res.data, this.reverses); return { data: await this.getDataByResponseType(stream, reqOptions.responseType), headers: res.headers }; } async getAllByParams(params, reqOptions = {}) { const getResultStream = async () => (await this.client.get(params, reqOptions)).data; const stream = await this.applyMiddlewares(getResultStream, this.reverses); return this.getDataByResponseType(stream, reqOptions.responseType); } async getHeaders(objectId, reqOptions = {}) { const { headers, data } = await this.client.get(objectId, reqOptions); // free resources, as we don't use the stream data.destroy(); return headers; } async deleteOne(objectId, reqOptions = {}) { return this.client.delete(objectId, reqOptions); } async deleteAllByParams(params, reqOptions = {}) { return this.client.delete(params, reqOptions); } } exports.ObjectStorage = ObjectStorage;