Skip to content

Instantly share code, notes, and snippets.

@realistschuckle
Created February 23, 2014 03:41
Show Gist options
  • Select an option

  • Save realistschuckle/9166528 to your computer and use it in GitHub Desktop.

Select an option

Save realistschuckle/9166528 to your computer and use it in GitHub Desktop.

Revisions

  1. Curtis Schlak revised this gist Feb 23, 2014. 1 changed file with 62 additions and 62 deletions.
    124 changes: 62 additions & 62 deletions static_assets_from_couchdb.js
    Original file line number Diff line number Diff line change
    @@ -1,78 +1,78 @@
    /*jslint anon:true, sloppy:true, nomen:true, white:true, stupid:true*/
    var http = require('http')
    , path = require('path')
    , url = require('url')
    , ycb = require('ycb')
    , fs = require('fs')
    ;
    , path = require('path')
    , url = require('url')
    , ycb = require('ycb')
    , fs = require('fs')
    ;

    var uripattern = /^\/static\/([a-zA-Z0-9\-\._~]+)\/([a-zA-Z0-9\-\._~]+)\/([a-zA-Z0-9\-\._~]+)/
    , configPath = path.join(__dirname, '../', 'application.json')
    , appConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'))
    , appConfig = new ycb.Ycb(appConfig, {})
    , configPath = path.join(__dirname, '../', 'application.json')
    , appConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'))
    , appConfig = new ycb.Ycb(appConfig, {})
    ;

    function getBody(asseturi, res, next) {
    http.get(asseturi, function(dbfile) {
    var headers = dbfile.headers
    ;
    http.get(asseturi, function(dbfile) {
    var headers = dbfile.headers
    ;

    if (dbfile.statusCode !== 200) {
    return next();
    }
    if (dbfile.statusCode !== 200) {
    return next();
    }

    Object.keys(headers).forEach(function(key) {
    res.setHeader(key, headers[key]);
    });
    dbfile.pipe(res);
    }).on('error', function(e) {
    next(e);
    });
    Object.keys(headers).forEach(function(key) {
    res.setHeader(key, headers[key]);
    });
    dbfile.pipe(res);
    }).on('error', function(e) {
    next(e);
    });
    }

    module.exports = function(req, res, next) {
    var matches = req.url.match(uripattern)
    , noneMatch = req.headers['if-none-match']
    , config = appConfig.read(req.app.store.getStaticContext())
    , appName = config.staticHandling.appName
    , dburi = config[appName].database.server
    , chapter
    , id
    , attachment
    , headopts
    , asseturipath
    , asseturi
    ;
    var matches = req.url.match(uripattern)
    , noneMatch = req.headers['if-none-match']
    , config = appConfig.read(req.app.store.getStaticContext())
    , appName = config.staticHandling.appName
    , dburi = config[appName].database.server
    , chapter
    , id
    , attachment
    , headopts
    , asseturipath
    , asseturi
    ;

    if(!matches || matches.length === 0) {
    return next();
    }
    if(!matches || matches.length === 0) {
    return next();
    }

    chapter = matches[1];
    id = matches[2];
    attachment = matches[3];
    asseturipath = path.join(chapter, id, attachment);
    asseturi = url.resolve(dburi, asseturipath);
    chapter = matches[1];
    id = matches[2];
    attachment = matches[3];
    asseturipath = path.join(chapter, id, attachment);
    asseturi = url.resolve(dburi, asseturipath);

    if (noneMatch) {
    headopts = url.parse(asseturi);
    headopts.method = 'HEAD';
    http.request(headopts)
    .on('response', function(headRes) {
    var headers = headRes.headers
    ;
    headRes.resume();
    if (headRes.headers.etag === noneMatch) {
    headers['content-length'] = 0;
    res.writeHead(304, headers);
    res.end();
    } else {
    getBody(asseturi, res, next);
    }
    }).on('error', function(e) {
    next(e);
    }).end();
    } else {
    getBody(asseturi, res, next);
    }
    if (noneMatch) {
    headopts = url.parse(asseturi);
    headopts.method = 'HEAD';
    http.request(headopts)
    .on('response', function(headRes) {
    var headers = headRes.headers
    ;
    headRes.resume();
    if (headRes.headers.etag === noneMatch) {
    headers['content-length'] = 0;
    res.writeHead(304, headers);
    res.end();
    } else {
    getBody(asseturi, res, next);
    }
    }).on('error', function(e) {
    next(e);
    }).end();
    } else {
    getBody(asseturi, res, next);
    }
    };
  2. Curtis Schlak created this gist Feb 23, 2014.
    78 changes: 78 additions & 0 deletions static_assets_from_couchdb.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    /*jslint anon:true, sloppy:true, nomen:true, white:true, stupid:true*/
    var http = require('http')
    , path = require('path')
    , url = require('url')
    , ycb = require('ycb')
    , fs = require('fs')
    ;

    var uripattern = /^\/static\/([a-zA-Z0-9\-\._~]+)\/([a-zA-Z0-9\-\._~]+)\/([a-zA-Z0-9\-\._~]+)/
    , configPath = path.join(__dirname, '../', 'application.json')
    , appConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'))
    , appConfig = new ycb.Ycb(appConfig, {})
    ;

    function getBody(asseturi, res, next) {
    http.get(asseturi, function(dbfile) {
    var headers = dbfile.headers
    ;

    if (dbfile.statusCode !== 200) {
    return next();
    }

    Object.keys(headers).forEach(function(key) {
    res.setHeader(key, headers[key]);
    });
    dbfile.pipe(res);
    }).on('error', function(e) {
    next(e);
    });
    }

    module.exports = function(req, res, next) {
    var matches = req.url.match(uripattern)
    , noneMatch = req.headers['if-none-match']
    , config = appConfig.read(req.app.store.getStaticContext())
    , appName = config.staticHandling.appName
    , dburi = config[appName].database.server
    , chapter
    , id
    , attachment
    , headopts
    , asseturipath
    , asseturi
    ;

    if(!matches || matches.length === 0) {
    return next();
    }

    chapter = matches[1];
    id = matches[2];
    attachment = matches[3];
    asseturipath = path.join(chapter, id, attachment);
    asseturi = url.resolve(dburi, asseturipath);

    if (noneMatch) {
    headopts = url.parse(asseturi);
    headopts.method = 'HEAD';
    http.request(headopts)
    .on('response', function(headRes) {
    var headers = headRes.headers
    ;
    headRes.resume();
    if (headRes.headers.etag === noneMatch) {
    headers['content-length'] = 0;
    res.writeHead(304, headers);
    res.end();
    } else {
    getBody(asseturi, res, next);
    }
    }).on('error', function(e) {
    next(e);
    }).end();
    } else {
    getBody(asseturi, res, next);
    }
    };