@elastic.io/object-storage-client
Advanced tools
Comparing version 0.0.1-dev.6 to 0.0.1-dev.7
@@ -9,3 +9,3 @@ /// <reference types="node" /> | ||
private message; | ||
private readonly jwtSecret; | ||
private readonly jwtSecret?; | ||
private static httpAgent; | ||
@@ -15,3 +15,3 @@ private static httpsAgent; | ||
uri: string; | ||
jwtSecret: string; | ||
jwtSecret?: string; | ||
cipher: { | ||
@@ -24,7 +24,7 @@ key: string; | ||
private getHeaders; | ||
getAsJSON(objectId: string, jwtPayload: JWTPayload): Promise<any>; | ||
getAsStream(objectId: string, jwtPayload: JWTPayload): Promise<Readable>; | ||
addAsStream(stream: Readable, jwtPayload: JWTPayload): Promise<string>; | ||
addAsJSON(data: any, jwtPayload: JWTPayload): Promise<string>; | ||
getAsJSON(objectId: string, jwtPayloadOrToken: JWTPayload | string): Promise<any>; | ||
getAsStream(objectId: string, jwtPayloadOrToken: JWTPayload | string): Promise<Readable>; | ||
addAsStream(stream: Readable, jwtPayloadOrToken: JWTPayload | string): Promise<string>; | ||
addAsJSON(data: any, jwtPayloadOrToken: JWTPayload | string): Promise<string>; | ||
} | ||
export {}; |
@@ -58,18 +58,21 @@ "use strict"; | ||
} | ||
async getHeaders(jwtPayload, override) { | ||
const token = await util_1.promisify(jsonwebtoken_1.sign)(jwtPayload, this.jwtSecret); | ||
async getHeaders(jwtPayloadOrToken, override) { | ||
if (typeof jwtPayloadOrToken !== 'string' && !this.jwtSecret) { | ||
throw new Error('Neither JWT token passed, nor JWT secret provided during initialization'); | ||
} | ||
const token = typeof jwtPayloadOrToken === 'string' ? jwtPayloadOrToken : await util_1.promisify(jsonwebtoken_1.sign)(jwtPayloadOrToken, this.jwtSecret); | ||
return Object.assign({ Authorization: `Bearer ${token}` }, override); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async getAsJSON(objectId, jwtPayload) { | ||
const content = await get_stream_1.default(await this.getAsStream(objectId, jwtPayload)); | ||
async getAsJSON(objectId, jwtPayloadOrToken) { | ||
const content = await get_stream_1.default(await this.getAsStream(objectId, jwtPayloadOrToken)); | ||
return JSON.parse(content); | ||
} | ||
async getAsStream(objectId, jwtPayload) { | ||
const res = await this.requestRetry(async () => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayload) })); | ||
async getAsStream(objectId, jwtPayloadOrToken) { | ||
const res = await this.requestRetry(async () => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayloadOrToken) })); | ||
return this.message.unpackStream(res.data); | ||
} | ||
async addAsStream(stream, jwtPayload) { | ||
async addAsStream(stream, jwtPayloadOrToken) { | ||
let objectId = uuid_1.default.v4(); | ||
const res = await this.api.put(`/objects/${objectId}`, this.message.packStream(stream), { headers: await this.getHeaders(jwtPayload, { 'content-type': 'application/octet-stream' }) }); | ||
const res = await this.api.put(`/objects/${objectId}`, this.message.packStream(stream), { headers: await this.getHeaders(jwtPayloadOrToken, { 'content-type': 'application/octet-stream' }) }); | ||
if (res.status >= 400) { | ||
@@ -81,3 +84,3 @@ throw new Error(`HTTP error during object add: ${res.status} (${res.statusText})`); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async addAsJSON(data, jwtPayload) { | ||
async addAsJSON(data, jwtPayloadOrToken) { | ||
let objectId = uuid_1.default.v4(); | ||
@@ -89,3 +92,3 @@ const dataString = JSON.stringify(data); | ||
dataStream.push(null); | ||
return this.api.put(`/objects/${objectId}`, this.message.packStream(dataStream), { headers: await this.getHeaders(jwtPayload, { 'content-type': 'application/octet-stream' }) }); | ||
return this.api.put(`/objects/${objectId}`, this.message.packStream(dataStream), { headers: await this.getHeaders(jwtPayloadOrToken, { 'content-type': 'application/octet-stream' }) }); | ||
}, { | ||
@@ -92,0 +95,0 @@ onResponse: (err, res) => { |
{ | ||
"name": "@elastic.io/object-storage-client", | ||
"version": "0.0.1-dev.6", | ||
"version": "0.0.1-dev.7", | ||
"description": "Elastic.io Message Store Client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -10,3 +10,3 @@ import nock from 'nock'; | ||
import { Readable } from 'stream'; | ||
import { verify } from 'jsonwebtoken'; | ||
import { verify, sign } from 'jsonwebtoken'; | ||
@@ -153,2 +153,47 @@ describe('ObjectStorage', () => { | ||
}); | ||
it('should accept jwt token on add', async () => { | ||
const objectStorage = new ObjectStorage({ uri: config.uri, cipher: config.cipher }); | ||
const jwtPayload = { tenantId: '12', contractId: '1' }; | ||
const objectStorageCalls = nock(config.uri) | ||
// @ts-ignore: Nock .d.ts are outdated. | ||
.matchHeader('authorization', authHeaderMatch(jwtPayload)) | ||
.put(/^\/objects\/[0-9a-z-]+$/, putData) | ||
.reply(200); | ||
const objectId = await objectStorage.addAsJSON(data, sign(jwtPayload, config.jwtSecret)); | ||
expect(objectStorageCalls.isDone()).to.be.true; | ||
expect(objectId).to.match(/^[0-9a-z-]+$/); | ||
}); | ||
it('should accept jwt token on get', async () => { | ||
const objectStorage = new ObjectStorage({ uri: config.uri, cipher: config.cipher }); | ||
const jwtPayload = { tenantId: '12', contractId: '1' }; | ||
const objectStorageCalls = nock(config.uri) | ||
// @ts-ignore: Nock .d.ts are outdated. | ||
.matchHeader('authorization', authHeaderMatch(jwtPayload)) | ||
.get('/objects/1') | ||
.reply(200, putData); | ||
const out = await objectStorage.getAsJSON('1', sign(jwtPayload, config.jwtSecret)); | ||
expect(objectStorageCalls.isDone()).to.be.true; | ||
expect(out).to.be.deep.equal(data); | ||
}); | ||
it('should throw exception if neither jwt secret, nor jwt token provided', async () => { | ||
const objectStorage = new ObjectStorage({ uri: config.uri, cipher: config.cipher }); | ||
let err; | ||
try { | ||
await objectStorage.getAsJSON('1', {}); | ||
} catch (e) { | ||
err = e; | ||
} | ||
expect(err.toString()).to.include('JWT'); | ||
}); | ||
}); | ||
@@ -155,0 +200,0 @@ |
@@ -17,3 +17,3 @@ import { Readable } from 'stream'; | ||
private message: Message; | ||
private readonly 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 | ||
@@ -23,3 +23,3 @@ private static httpAgent = new http.Agent({ keepAlive: true }); | ||
public constructor (config: {uri: string; jwtSecret: string; cipher: {key: string; iv: string}}) { | ||
public constructor (config: {uri: string; jwtSecret?: string; cipher: {key: string; iv: string}}) { | ||
this.api = axios.create({ | ||
@@ -65,4 +65,7 @@ baseURL: `${config.uri}/`, | ||
private async getHeaders (jwtPayload: JWTPayload, override?: { [index: string]: string }) { | ||
const token = await promisify(sign)(jwtPayload, this.jwtSecret); | ||
private async getHeaders (jwtPayloadOrToken: JWTPayload | string, override?: { [index: string]: string }) { | ||
if (typeof jwtPayloadOrToken !== 'string' && !this.jwtSecret) { | ||
throw new Error('Neither JWT token passed, nor JWT secret provided during initialization'); | ||
} | ||
const token = typeof jwtPayloadOrToken === 'string' ? jwtPayloadOrToken : await promisify(sign)(jwtPayloadOrToken, this.jwtSecret); | ||
return { Authorization: `Bearer ${token}`, ...override }; | ||
@@ -72,4 +75,4 @@ } | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
public async getAsJSON (objectId: string, jwtPayload: JWTPayload): Promise<any> { | ||
const content = await getStream(await this.getAsStream(objectId, jwtPayload)); | ||
public async getAsJSON (objectId: string, jwtPayloadOrToken: JWTPayload | string): Promise<any> { | ||
const content = await getStream(await this.getAsStream(objectId, jwtPayloadOrToken)); | ||
@@ -79,5 +82,5 @@ return JSON.parse(content); | ||
public async getAsStream (objectId: string, jwtPayload: JWTPayload): Promise<Readable> { | ||
public async getAsStream (objectId: string, jwtPayloadOrToken: JWTPayload | string): Promise<Readable> { | ||
const res = await this.requestRetry( | ||
async (): Promise<AxiosResponse> => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayload) }) | ||
async (): Promise<AxiosResponse> => this.api.get(`/objects/${objectId}`, { responseType: 'stream', headers: await this.getHeaders(jwtPayloadOrToken) }) | ||
); | ||
@@ -88,3 +91,3 @@ | ||
public async addAsStream (stream: Readable, jwtPayload: JWTPayload): Promise<string> { | ||
public async addAsStream (stream: Readable, jwtPayloadOrToken: JWTPayload | string): Promise<string> { | ||
let objectId = uuid.v4(); | ||
@@ -95,3 +98,3 @@ | ||
this.message.packStream(stream), | ||
{ headers: await this.getHeaders(jwtPayload, { 'content-type': 'application/octet-stream' }) }); | ||
{ headers: await this.getHeaders(jwtPayloadOrToken, { 'content-type': 'application/octet-stream' }) }); | ||
if (res.status >= 400) { | ||
@@ -105,3 +108,3 @@ throw new Error(`HTTP error during object add: ${res.status} (${res.statusText})`); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
public async addAsJSON (data: any, jwtPayload: JWTPayload): Promise<string> { | ||
public async addAsJSON (data: any, jwtPayloadOrToken: JWTPayload | string): Promise<string> { | ||
let objectId = uuid.v4(); | ||
@@ -118,3 +121,3 @@ const dataString = JSON.stringify(data); | ||
this.message.packStream(dataStream), | ||
{ headers: await this.getHeaders(jwtPayload, { 'content-type': 'application/octet-stream' }) } | ||
{ headers: await this.getHeaders(jwtPayloadOrToken, { 'content-type': 'application/octet-stream' }) } | ||
); | ||
@@ -121,0 +124,0 @@ }, |
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
29871
653