Skip to content

Instantly share code, notes, and snippets.

@rvagg
Created August 28, 2013 06:41
Show Gist options
  • Save rvagg/6362798 to your computer and use it in GitHub Desktop.
Save rvagg/6362798 to your computer and use it in GitHub Desktop.
Replacing persona with stubbyid (http://toolness.github.io/stubbyid/) for testing when using Doorknob & Browserify
// replace persona-id's persona.js with stubbyid.js, modified to have an `exports`
// ./_persona-id_stubbyid_js is a copy of https://github.com/toolness/stubbyid/blob/gh-pages/stubbyid.js
const through = require('through2')
, fs = require('fs')
, path = require('path')
, stubbyidFile = path.join(__dirname, '_persona-id_stubbyid_js')
var stubbyid
module.exports = function (file) {
if (!stubbyid) {
stubbyid = fs.readFileSync(stubbyidFile, 'utf8')
stubbyid += ';module.exports = navigator.id;'
}
if (/\/persona\.js$/.test(file)) {
console.log('replacing', file, 'with', stubbyidFile)
return through(
function (chunk, enc, callback) { callback() }
, function (callback) { this.push(stubbyid); callback() }
)
}
return through()
}
// wherever doorknob is set up as a filter/middleware
const Doorknob = require('doorknob')
, audience = process.env.PERSONA_AUDIENCE || 'http://localhost:3000'
, devMode = process.env.PERSONA_STUB
var doorknob = Doorknob(audience)
if (devMode) {
doorknob.persona.verify = function (assertion, cb) {
console.log('stubbing out auth call to persona for (', assertion, ')')
cb(null, { email: assertion })
}
}
function filter (req, res, next) {
doorknob(req, res, function(err, profile) {
// whatever needs to be done for your app...
req.profile = profile
if (profile.loggingIn)
return // bypass
next()
})
}
// route that handles /js/script.js or whatever
const path = require('path')
, browserify = require('browserify')
const index = path.join(__dirname, '../../browser-lib/index.js')
, devMode = process.env.PERSONA_STUB
// replace persona-id's persona.js with stubbyid.js
function personaStubFilter (p) {
if (p.name == 'persona-id') {
if (!p.browserify)
p.browserify = {}
;(p.browserify.transform || (p.browserify.transform = []))
.push(path.join(__dirname, './_persona-id_transform.js'))
}
return p
}
// replace with whatever your web 'framework' uses, this is a splinky handler
function handler (context, callback) {
context.setContentType('text/javascript')
browserify(index)
.bundle(devMode ? { packageFilter: personaStubFilter } : {})
.pipe(context.response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment