learnyounode
Version:
Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.
99 lines (75 loc) • 2.69 kB
JavaScript
var through2 = require('through2')
var hyperquest = require('hyperquest')
var bl = require('bl')
var exercise = require('workshopper-exercise')()
var filecheck = require('workshopper-exercise/filecheck')
var execute = require('workshopper-exercise/execute')
var comparestdout = require('workshopper-exercise/comparestdout')
var rndport = require('../../lib/rndport')
var date = new Date(Date.now() - 100000)
// the output will be long lines so make the comparison take that into account
exercise.longCompareOutput = true
// checks that the submission file actually exists
exercise = filecheck(exercise)
// execute the solution and submission in parallel with spawn()
exercise = execute(exercise)
// set up the data file to be passed to the submission
exercise.addSetup(function (mode, callback) {
this.submissionPort = rndport()
this.solutionPort = this.submissionPort + 1
this.submissionArgs = [ this.submissionPort ]
this.solutionArgs = [ this.solutionPort ]
process.nextTick(callback)
})
// add a processor for both run and verify calls, added *before*
// the comparestdout processor so we can mess with the stdouts
exercise.addProcessor(function (mode, callback) {
this.submissionStdout.pipe(process.stdout)
// replace stdout with our own streams
this.submissionStdout = through2()
if (mode === 'verify') {
this.solutionStdout = through2()
}
setTimeout(query.bind(this, mode), 500)
process.nextTick(function () {
callback(null, true)
})
})
// compare stdout of solution and submission
exercise = comparestdout(exercise)
function normalizeJSON (data) {
return JSON.stringify(JSON.parse(data))
}
// delayed for 500ms to wait for servers to start so we can start
// playing with them
function query (mode) {
var exercise = this
function verify (port, stream) {
function timeRequest (method, callback) {
var url = 'http://localhost:' + port + '/api/' + method + '?iso=' + date.toISOString()
function onData (err, _data) {
if (err) {
exercise.emit('fail', exercise.__('fail.connection', { address: url, message: err.message }))
} else {
var data = _data.toString()
try {
data = normalizeJSON(data.toString())
} catch (e) {}
stream.write(data + '\n')
}
callback()
}
return hyperquest.get(url).pipe(bl(onData))
}
timeRequest('parsetime', function () {
timeRequest('unixtime', function () {
stream.end()
})
})
}
verify(this.submissionPort, this.submissionStdout)
if (mode === 'verify') {
verify(this.solutionPort, this.solutionStdout)
}
}
module.exports = exercise