Skip to content

Commit

Permalink
fix: always use Unix newline as a separator
Browse files Browse the repository at this point in the history
This should avoid problems where the producer is on a different architecture than the consumer.
  • Loading branch information
robertrossmann committed Dec 7, 2018
1 parent 2425310 commit be3fd8c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const SEPARATOR = '\n'

export {
SEPARATOR,
}
4 changes: 2 additions & 2 deletions src/consumer/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Server } from 'net'
import { EventEmitter } from 'events'
import { EOL } from 'os'
import split from 'binary-split'
import { SEPARATOR } from '../constants'
import { JSONParser } from './deserialisers'


Expand Down Expand Up @@ -38,7 +38,7 @@ class Consumer extends EventEmitter {
socket
.once('error', err => source.emit('error', err))
.once('close', (...args) => setImmediate(() => source.emit('close', ...args)))
.pipe(split(EOL))
.pipe(split(SEPARATOR))
.pipe(new JSONParser())
.on('data', ({ event, args }) => source.emit(event, ...args))

Expand Down
9 changes: 5 additions & 4 deletions src/consumer/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Server } from 'net'
import { EventEmitter } from 'events'
import { PassThrough } from 'stream'
import { SEPARATOR } from '../constants'
import { Consumer } from '.'

describe('Consumer', () => {
Expand Down Expand Up @@ -141,7 +142,7 @@ describe('Consumer', () => {

it('forwards serialised payloads as events with arguments', async () => {
const payload = { event: 'test', args: [{ first: true }, { second: 'yup' }] }
const raw = `${JSON.stringify(payload)}\n`
const raw = `${JSON.stringify(payload)}${SEPARATOR}`

const socket = new PassThrough()
setImmediate(() => server.emit('connection', socket))
Expand All @@ -161,7 +162,7 @@ describe('Consumer', () => {
JSON.stringify({ event: 'test', args: [{ first: 'event' }] }),
JSON.stringify({ event: 'test', args: [{ second: true }] }),
JSON.stringify({ event: 'test', args: [{ third: 'of course' }] }),
].join('\n')
].join(SEPARATOR)

const socket = new PassThrough()
setImmediate(() => server.emit('connection', socket))
Expand Down Expand Up @@ -193,7 +194,7 @@ describe('Consumer', () => {
JSON.stringify({ event: 'test', args: [{ first: 'event' }] }),
JSON.stringify({ event: 'test', args: [{ second: true }] }),
JSON.stringify({ event: 'test', args: [{ third: 'of course' }] }),
].join('\n')
].join(SEPARATOR)

const chunks = [raw.slice(0, 12), raw.slice(12)]
const socket = new PassThrough()
Expand Down Expand Up @@ -244,7 +245,7 @@ describe('Consumer', () => {
await new Promise(resolve => setImmediate(resolve))

expect(events).to.equal(0)
socket.end('\n')
socket.end(SEPARATOR)
await new Promise(resolve => setImmediate(resolve))
await new Promise(resolve => setImmediate(resolve))
expect(events).to.equal(1)
Expand Down
4 changes: 3 additions & 1 deletion src/provider/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import net from 'net'
import { EventEmitter } from 'events'
import { SEPARATOR } from '../constants'
import { Provider } from '.'


const destination = '/tmp/remote-event-emitter.test.sock'

describe('Provider', () => {
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('Provider', () => {
const raw = received.toString('utf8')
const payload = JSON.parse(raw)

expect(raw.endsWith('\n')).to.equal(true)
expect(raw.endsWith(SEPARATOR)).to.equal(true)
expect(payload).to.have.all.keys([
'event',
'args',
Expand Down
3 changes: 2 additions & 1 deletion src/provider/serialiser.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Transform } from 'stream'
import { SEPARATOR } from '../constants'

/**
* Given a standard JS value, attempt to serialise it into JSON and send it out, with a newline at
Expand All @@ -23,7 +24,7 @@ class Serialiser extends Transform {
return void done(err)
}

return void done(null, `${stringified}\n`)
return void done(null, `${stringified}${SEPARATOR}`)
}
}

Expand Down

0 comments on commit be3fd8c

Please sign in to comment.