Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KeKs0r
Created February 27, 2018 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeKs0r/657788f951fec9909f8c53d515d074d0 to your computer and use it in GitHub Desktop.
Save KeKs0r/657788f951fec9909f8c53d515d074d0 to your computer and use it in GitHub Desktop.
Koa Stream Response
/*
* In this example I get an error from my rethinkdb database
* ReqlRuntimeError: Connection is closed in.... (basically the first query within exportText())
*/
export default async function downloadTexts (ctx, next) {
const { qid } = this.params
console.log('start')
const stream = await exportText(qid, this._rdbConn, true)
ctx.body = stream.on('error', err => {
console.log('Error-handler')
ctx.onerror(err)
})
ctx.set('Content-disposition', 'attachment; filename=' + qid + '.csv')
ctx.set('Content-type', 'text/csv')
await next()
}
/*
* This file is responsible for generating the text
* Just illustrative to see what is happening
*/
const r = require('rethinkdb')
const rethinkdbStream = require('rethinkdb-stream')
const through2 = require('through2')
const stringify = require('csv-stringify')
const stringifier = stringify()
export default async function exportText (qid, conn, all = false) {
const someData = await fetchSomeDataCompletelyBefore()
const textQuery = r.table('texts')
const textCursor = await textQuery.run(conn)
const stream = rethinkdbStream(textCursor)
// Body
return stream
.pipe(
through2.obj(function (t, enc, next) {
this.push(transform(t))
next()
})
)
.pipe(stringifier)
}
/*
* In this example the resulting file is empty. It seams that the body is sent back before the stream can actually start populating data
*/
export default function* downloadTexts (next) {
const { qid } = this.params
console.log('Pre')
console.time('exportText')
const stream = yield exportText(qid, this._rdbConn, false)
console.timeEnd('exportText')
console.log('Post')
stream.on('error', err => {
console.log('Error-handler')
console.error(err)
})
this.body = stream
this.set('Content-disposition', 'attachment; filename=' + qid + '.csv')
this.set('Content-type', 'text/csv')
yield next
}
/*
* Creates and closes the rethinkdb connection. Somehow the connection is still open, but already "closing".
*/
/*
* Create a RethinkDB connection, and save it in req._rdbConn
*/
export function* createConnection (next) {
const options = makeConfig(Config.rethinkdb)
try {
var conn = yield r.connect(options)
this._rdbConn = conn
} catch (err) {
console.log('this error')
this.status = 500
this.body = err.message || http.STATUS_CODES[this.status]
}
yield next
}
/*
* Close the RethinkDB connection
*/
export function* closeConnection (next) {
console.log('Closing')
this._rdbConn.close()
yield next
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment