Skip to content

repo sync #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/hydro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const crypto = require('crypto')
const fetch = require('node-fetch')

module.exports = class Hydro {
constructor ({ secret, endpoint }) {
this.secret = secret || process.env.HYDRO_SECRET
this.endpoint = endpoint || process.env.HYDRO_ENDPOINT
}

/**
* Generate a SHA256 hash of the payload using the secret
* to authenticate with Hydro
* @param {string} body
*/
generatePayloadHmac (body) {
return crypto.createHmac('sha256', this.secret)
.update(body)
.digest('hex')
}

/**
* Publish a single event to Hydro
* @param {string} schema
* @param {any} value
*/
async publish (schema, value) {
return this.publishMany([{ schema, value }])
}

/**
* Publish multiple events to Hydro
* @param {[{ schema: string, value: any }]} events
*/
async publishMany (events) {
const body = JSON.stringify({ events })
const token = this.generatePayloadHmac(body)

return fetch(this.endpoint, {
method: 'POST',
body,
headers: {
Authorization: `Hydro ${token}`,
'Content-Type': 'application/json',
'X-Hydro-App': 'docs'
}
})
}
}
61 changes: 61 additions & 0 deletions tests/unit/hydro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const nock = require('nock')
const Hydro = require('../../lib/hydro')

describe('hydro', () => {
let hydro, params

beforeEach(() => {
hydro = new Hydro({ secret: '123', endpoint: 'https://real-hydro.com' })

nock(hydro.endpoint, {
reqheaders: {
Authorization: /^Hydro [\d\w]{64}$/,
'Content-Type': 'application/json',
'X-Hydro-App': 'docs'
}
})
// Respond with a 201 and store the body we sent
.post('/').reply(201, (_, body) => { params = body })
})

describe('#publish', () => {
it('publishes a single event to Hydro', async () => {
await hydro.publish('event-name', { pizza: true })
expect(params).toEqual({
events: [{ schema: 'event-name', value: { pizza: true } }]
})
})
})

describe('#publishMany', () => {
it('publishes multiple events to Hydro', async () => {
await hydro.publishMany([
{ schema: 'event-name', value: { pizza: true } },
{ schema: 'other-name', value: { salad: false } }
])

expect(params).toEqual({
events: [
{ schema: 'event-name', value: { pizza: true } },
{ schema: 'other-name', value: { salad: false } }
]
})
})
})

describe('#generatePayloadHmac', () => {
it('returns a SHA256 HMAC string', () => {
const body = JSON.stringify({ pizza: true })
const hash = hydro.generatePayloadHmac(body)
expect(hash).toEqual(expect.any(String))
expect(hash).toHaveLength(64)
})

it('generates the same string for the same payload', () => {
const body = JSON.stringify({ pizza: true })
const one = hydro.generatePayloadHmac(body)
const two = hydro.generatePayloadHmac(body)
expect(one).toBe(two)
})
})
})