Skip to content

Commit

Permalink
Merge 3a68d08 into fa290ce
Browse files Browse the repository at this point in the history
  • Loading branch information
dfellis committed Sep 18, 2018
2 parents fa290ce + 3a68d08 commit d0a7cd1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
26 changes: 23 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const util = require('util')

const errorCode = require('./errorcode')

const setDefaultProperty = (data) => {
Expand Down Expand Up @@ -139,7 +141,7 @@ class JSONRPC {
} // TODO: Try to un-nest this
}

// ### The *register* function
// ### The *register* method
// allows one to attach a function to the current scope after the scope has
// been attached to the JSON-RPC server, for similar possible shenanigans as
// described above. This method in particular, though, by attaching new
Expand All @@ -148,11 +150,29 @@ class JSONRPC {
register(methodName, method) {
if (!this.scope || typeof(this.scope) !== 'object') {
this.scope = {}
}
} // TODO: Remove this behavior by 1.0.0
this.scope[methodName] = method
}

// Make a ``blocking`` helper method to async-ify them
// ### The *registerPromise* method
// allows async functions (and functions that return promises) to be registered as
// a server function. This will become the default path in 1.0.0
registerPromise(methodName, method) {
const callbackMethod = util.callbackify(method)
callbackMethod.blocking = true // Abuse this flag because wrapped methods will match arg length
this.scope[methodName] = callbackMethod
}

// ### The *registerCallback* method
// is currently an alias for the *register* method. Added to make the transition to
// 1.0.0 smoother (can write server code compatible with 1.0.0 before unsupported code is
// dropped
registerCallback(methodName, method) {
this.register(methodName, method)
}

// Make a ``blocking`` helper method to callback-ify them; will be deprecated in 1.0.0 as
// unnecessary (but kept for transitioning users)
blocking(func) {
const blockedFunc = (...args) => {
const callback = args.pop()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"realpublish": "./prepublish.sh",
"eslint": "eslint ./bin/jsonrpc-repl ./lib/*.js ./lib/**/*.js ./test/*.js ./test/**/*.js",
"test": "npm run eslint && nyc nodeunit test/*.js",
"view-cover": "nyc report --reporter=html && open ./coverage/index.html",
"view-cover": "nyc report --reporter=html && if command -v xdg-open; then xdg-open ./coverage/index.html; else open ./coverage/index.html; fi",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"engines": {
Expand Down
70 changes: 70 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,73 @@ exports.blockingFunction = (test) => {
req.write(testJSON)
req.end()
}

exports.asyncFunction = (test) => {
test.expect(3)
const jsonRpcServer = new JSONRPCserver(new HttpTransport(32766), {})
jsonRpcServer.registerPromise('answerToUltimateQuestion', async () => 42)
const testJSON = JSON.stringify({
id: 27,
method: 'answerToUltimateQuestion',
params: []
})
const req = http.request({
hostname: 'localhost',
port: 32766,
path: '/',
method: 'POST'
}, (res) => {
res.setEncoding('utf8')
let resultString = ''
res.on('data', (data) => resultString += data)
res.on('end', () => {
let resultObj
try {
resultObj = JSON.parse(resultString)
} catch(e) {
// Do nothing, test will fail
}
test.equal(resultObj.id, 27, 'The JSON-RPC server sent back the correct ID')
test.equal(resultObj.result, 42, 'The answer to life, the universe, and everything')
test.ok(resultObj.error === undefined, 'The error property is not defined')
jsonRpcServer.shutdown(test.done.bind(test))
})
})
req.write(testJSON)
req.end()
}

exports.callbackFunction = (test) => {
test.expect(3)
const jsonRpcServer = new JSONRPCserver(new HttpTransport(32765), {})
jsonRpcServer.registerCallback('answerToUltimateQuestion', (callback) => callback(null, 42))
const testJSON = JSON.stringify({
id: 28,
method: 'answerToUltimateQuestion',
params: []
})
const req = http.request({
hostname: 'localhost',
port: 32765,
path: '/',
method: 'POST'
}, (res) => {
res.setEncoding('utf8')
let resultString = ''
res.on('data', (data) => resultString += data)
res.on('end', () => {
let resultObj
try {
resultObj = JSON.parse(resultString)
} catch(e) {
// Do nothing, test will fail
}
test.equal(resultObj.id, 28, 'The JSON-RPC server sent back the correct ID')
test.equal(resultObj.result, 42, 'The answer to life, the universe, and everything')
test.ok(resultObj.error === undefined, 'The error property is not defined')
jsonRpcServer.shutdown(test.done.bind(test))
})
})
req.write(testJSON)
req.end()
}

0 comments on commit d0a7cd1

Please sign in to comment.