Skip to content

Commit

Permalink
[feat] update
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-wyatt committed May 6, 2019
1 parent 15eb9d1 commit 2c0e7a6
Show file tree
Hide file tree
Showing 15 changed files with 3,015 additions and 2,326 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defaults: &defaults
working_directory: ~/fabrix
docker:
- image: circleci/node:10.0.0
- image: circleci/redis:5.0.0

jobs:
test:
Expand Down
5 changes: 5 additions & 0 deletions archetype/config/realtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const realtime = {
primus: {
options: {}
}
}
68 changes: 66 additions & 2 deletions lib/RealtimeSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const primusDefaults = {
transformer: 'engine.io'
}

import * as Validator from './validator'

import * as config from './config/index'
import * as pkg from '../package.json'

Expand Down Expand Up @@ -38,16 +40,78 @@ export class RealtimeSpool extends Spool {
return this._sockets
}

/**
* Validates Configs
*/
async validate () {

const requiredSpools = [
'router'
]

const spools = Object.keys(this.app.spools)

if (!spools.some(v => requiredSpools.indexOf(v) >= 0)) {
return Promise.reject(new Error(`spool-realtime requires spools: ${ requiredSpools.join(', ') }!`))
}

return Promise.all([
Validator.validateConfig.config(this.app.config.get('realtime')),
])
}

async initialize() {
const primusConfig = this.app.config.get('realtime.primus')
const plugins = this.app.config.get('realtime.plugins') || {}

return new Promise((resolve, reject) => {
this.app.once('webserver:http:ready', (httpServer) => {
if (Array.isArray(httpServer)) {
httpServer = httpServer[0]
}
const primusConfig = this.app.config.get('realtime.primus')
this._sockets = new Primus(httpServer, Object.assign(primusDefaults, primusConfig.options))

try {
this._sockets = new Primus(httpServer, Object.assign(primusDefaults, primusConfig.options))
}
catch (err) {
reject(err)
}

try {
Object.keys(plugins).forEach(k => {
this._sockets.use(k, plugins[k])
})
}
catch (err) {
reject(err)
}

return resolve()
})
})
}

/**
* Unload primus
*/
async unload() {
this._sockets.destroy({ timeout: 5000 })
return Promise.resolve()
}

/**
* Perform a Sanity Check
*/
async sanity() {
return Promise.resolve()
.then( () => {
if (!this._sockets) {
throw new Error('Sockets does not exist')
}
return
})
.catch(err => {
return Promise.reject(err)
})
}
}
1 change: 1 addition & 0 deletions lib/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { realtimeConfig } from './realtimeConfig'
7 changes: 7 additions & 0 deletions lib/schemas/realtimeConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import * as joi from 'joi'

export const realtimeConfig = joi.object().keys({
primus: joi.object(),
plugins: joi.object()
})
1 change: 1 addition & 0 deletions lib/validator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { validateConfig } from './validateConfig'
15 changes: 15 additions & 0 deletions lib/validator/validateConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as joi from 'joi'
import { realtimeConfig } from '../schemas/realtimeConfig'

export const validateConfig = {
config(config) {
return new Promise((resolve, reject) => {
joi.validate(config, realtimeConfig, (err, value) => {
if (err) {
return reject(new TypeError('config.realtime: ' + err))
}
return resolve(value)
})
})
}
}
Loading

0 comments on commit 2c0e7a6

Please sign in to comment.