Skip to content
Open
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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: node_js
sudo: false
node_js:
- "8"
- "10"
- "12"
services:
- redis
- mongodb
2 changes: 1 addition & 1 deletion examples/a.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Hapi = require('hapi')
const Hapi = require('@hapi/hapi')
const routes = require('./routes')

async function init () {
Expand Down
2 changes: 1 addition & 1 deletion examples/b.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Hapi = require('hapi')
const Hapi = require('@hapi/hapi')
const routes = require('./routes')

async function init () {
Expand Down
2 changes: 1 addition & 1 deletion examples/pub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Nes = require('nes')
const Nes = require('@hapi/nes')

async function pub () {
const client = new Nes.Client('ws://localhost:3001')
Expand Down
2 changes: 1 addition & 1 deletion examples/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Nes = require('nes')
const Nes = require('@hapi/nes')
const Multines = require('..')

module.exports = async (server) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/sub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Nes = require('nes')
const Nes = require('@hapi/nes')

async function sub () {
const client = new Nes.Client('ws://localhost:3000')
Expand Down
15 changes: 12 additions & 3 deletions multines.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ const mongodb = require('mqemitter-mongodb')

const kDeliver = Symbol.for('deliver')

function buildDeliver (socket, topic) {
function buildDeliver (socket, topic, filterOpts) {
return async function deliver (message, done) {
const filterArgs = {
socket,
credentials: socket.auth.credentials,
params: filterOpts.params
}

if (await filterOpts.filter('/' + topic, topic === message.topic ? message.body : message, filterArgs) === false) return

if (topic === message.topic) {
await socket.publish('/' + topic, message.body)
} else {
Expand Down Expand Up @@ -42,14 +50,15 @@ function getMq (options) {
}

async function register (server, options) {
server.dependency('nes')
server.dependency('@hapi/nes')
const mq = getMq(options)

server.decorate('server', 'subscriptionFar', (path, options) => {
options = options || {}

const wrapSubscribe = options.onSubscribe || (async (socket, path, params) => null)
const wrapUnsubscribe = options.onUnsubscribe || (async (socket, path, params) => null)
const filter = options.filter || (async (path, message, options) => true)

options.onSubscribe = async (socket, path, params) => {
const deliverMap = socket[kDeliver] || {}
Expand All @@ -58,7 +67,7 @@ async function register (server, options) {
const topic = path.replace(/^\//, '')

if (!deliverMap[path]) {
deliverMap[path] = buildDeliver(socket, topic)
deliverMap[path] = buildDeliver(socket, topic, { filter, params })
}

await wrapSubscribe(socket, path, params)
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multines",
"version": "1.0.0",
"version": "2.0.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems still a bump.

"description": "Multi-process nes backend, turn nes into a fully scalable solution",
"main": "multines.js",
"scripts": {
Expand All @@ -27,16 +27,16 @@
},
"homepage": "https://github.com/mcollina/multines#readme",
"devDependencies": {
"code": "^5.2.0",
"hapi": "^17.5.0",
"lab": "^15.4.5",
"nes": "^8.1.0",
"@hapi/code": "^8.0.1",
"@hapi/hapi": "^19.0.0",
"@hapi/lab": "^22.0.3",
"@hapi/nes": "^12.0.0",
"pre-commit": "^1.1.2",
"standard": "^11.0.0"
"standard": "^14.3.1"
},
"dependencies": {
"mqemitter": "^2.2.0",
"mqemitter-mongodb": "^3.0.2",
"mqemitter-redis": "^2.3.0"
"mqemitter": "^4.1.3",
"mqemitter-mongodb": "^7.0.0",
"mqemitter-redis": "^4.0.2"
}
}
31 changes: 25 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const mqemitter = require('mqemitter')
const Code = require('code')
const Lab = require('lab')
const Hapi = require('hapi')
const Nes = require('nes')
const Code = require('@hapi/code')
const Lab = require('@hapi/lab')
const Hapi = require('@hapi/hapi')
const Nes = require('@hapi/nes')
const Multines = require('.')

const lab = exports.lab = Lab.script()
Expand All @@ -21,7 +21,7 @@ function getServer (port) {
return server
}

async function start (server, opts) {
async function start (server, opts, subOpts) {
opts = opts || {}

const plugin = {
Expand All @@ -34,7 +34,7 @@ async function start (server, opts) {

await server.register([Nes, plugin])

server.subscriptionFar('/echo')
server.subscriptionFar('/echo', subOpts)
server.route({
path: '/echo',
method: 'POST',
Expand Down Expand Up @@ -185,6 +185,25 @@ experiment('nes work as normal', async () => {
pubSubTest()
})

experiment('with subscription filter', async () => {
let server

beforeEach(async () => {
server = await start(getServer(), null, {
filter: function (path, message, options) {
return true
}
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where the filter function return false? Also, you'll need to add some assertions on path, message and options.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the filter function returns false the test times out b/c the message doesn't arrive. I'm not sure how to assert this. I will add assertions for three params as soon as I got some time.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just create a different experiment where you do not call pubSubTest, but rather write actual tests for things. Also, it will require a test for promise form of the async function.

})

afterEach(async () => {
await server.stop()
server = null
})

pubSubTest()
})

experiment('with shared mqemitter', () => {
let server1
let server2
Expand Down
Loading