@elastic.io/object-storage-client
Advanced tools
Comparing version 0.0.1-dev.5 to 0.0.1-dev.6
/// <reference types="node" /> | ||
import { Readable } from 'stream'; | ||
export default class Message { | ||
private config; | ||
private readonly config; | ||
private readonly algorithm; | ||
@@ -6,0 +6,0 @@ constructor(config: { |
@@ -9,3 +9,3 @@ /// <reference types="node" /> | ||
private message; | ||
private jwtSecret; | ||
private readonly jwtSecret; | ||
private static httpAgent; | ||
@@ -12,0 +12,0 @@ private static httpsAgent; |
@@ -28,3 +28,3 @@ "use strict"; | ||
} | ||
async requestRetry({ maxAttempts, delay, request, onResponse }) { | ||
async requestRetry(request, { maxAttempts = 3, delay = 100, onResponse } = {}) { | ||
let attempts = 0; | ||
@@ -69,7 +69,3 @@ let res; | ||
async getAsStream(objectId, jwtPayload) { | ||
const res = await this.requestRetry({ | ||
maxAttempts: 3, | ||
delay: 100, | ||
request: async () => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayload) }) | ||
}); | ||
const res = await this.requestRetry(async () => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayload) })); | ||
return this.message.unpackStream(res.data); | ||
@@ -89,11 +85,8 @@ } | ||
const dataString = JSON.stringify(data); | ||
await this.requestRetry({ | ||
maxAttempts: 3, | ||
delay: 100, | ||
request: async () => { | ||
const dataStream = new stream_1.Readable(); | ||
dataStream.push(dataString); | ||
dataStream.push(null); | ||
return this.api.put(`/objects/${objectId}`, this.message.packStream(dataStream), { headers: await this.getHeaders(jwtPayload, { 'content-type': 'application/octet-stream' }) }); | ||
}, | ||
await this.requestRetry(async () => { | ||
const dataStream = new stream_1.Readable(); | ||
dataStream.push(dataString); | ||
dataStream.push(null); | ||
return this.api.put(`/objects/${objectId}`, this.message.packStream(dataStream), { headers: await this.getHeaders(jwtPayload, { 'content-type': 'application/octet-stream' }) }); | ||
}, { | ||
onResponse: (err, res) => { | ||
@@ -100,0 +93,0 @@ if (!err && res.status === 409) { |
{ | ||
"name": "@elastic.io/object-storage-client", | ||
"version": "0.0.1-dev.5", | ||
"version": "0.0.1-dev.6", | ||
"description": "Elastic.io Message Store Client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -7,3 +7,3 @@ import log from './logger'; | ||
export default class Message { | ||
private config: {key: string; iv: string}; | ||
private readonly config: {key: string; iv: string}; | ||
private readonly algorithm = 'aes-256-cbc'; | ||
@@ -10,0 +10,0 @@ |
@@ -17,3 +17,3 @@ import { Readable } from 'stream'; | ||
private message: Message; | ||
private jwtSecret: string; | ||
private readonly jwtSecret: string; | ||
// you will be able to create a lot of ObjectStorage instances and they will all use single http agent | ||
@@ -34,3 +34,3 @@ private static httpAgent = new http.Agent({ keepAlive: true }); | ||
private async requestRetry ({ maxAttempts, delay, request, onResponse }: { maxAttempts: number; delay: number; request: () => Promise<AxiosResponse>; onResponse?: (err: Error, res: AxiosResponse) => boolean }): Promise<AxiosResponse> { | ||
private async requestRetry (request: () => Promise<AxiosResponse>, { maxAttempts = 3, delay = 100, onResponse }: { maxAttempts?: number; delay?: number; onResponse?: (err: Error, res: AxiosResponse) => boolean } = {}): Promise<AxiosResponse> { | ||
let attempts = 0; | ||
@@ -78,7 +78,5 @@ let res; | ||
public async getAsStream (objectId: string, jwtPayload: JWTPayload): Promise<Readable> { | ||
const res = await this.requestRetry({ | ||
maxAttempts: 3, | ||
delay: 100, | ||
request: async (): Promise<AxiosResponse> => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayload) }) | ||
}); | ||
const res = await this.requestRetry( | ||
async (): Promise<AxiosResponse> => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayload) }) | ||
); | ||
@@ -107,6 +105,4 @@ return this.message.unpackStream(res.data); | ||
await this.requestRetry({ | ||
maxAttempts: 3, | ||
delay: 100, | ||
request: async (): Promise<AxiosResponse> => { | ||
await this.requestRetry( | ||
async (): Promise<AxiosResponse> => { | ||
const dataStream = new Readable(); | ||
@@ -121,10 +117,12 @@ dataStream.push(dataString); | ||
}, | ||
onResponse: (err, res): boolean => { | ||
if (!err && res.status === 409) { | ||
log.warn('Generated already existing UUID'); | ||
objectId = uuid.v4(); | ||
return true; | ||
{ | ||
onResponse: (err, res): boolean => { | ||
if (!err && res.status === 409) { | ||
log.warn('Generated already existing UUID'); | ||
objectId = uuid.v4(); | ||
return true; | ||
} | ||
} | ||
} | ||
}); | ||
); | ||
@@ -131,0 +129,0 @@ return objectId; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27263
613