├── .gitignore ├── index.js ├── .travis.yml ├── LICENSE ├── test ├── sample-content │ └── static-resource.js └── substitution-test.js ├── lib └── middleware.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/middleware'); 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 0.8 5 | 6 | notifications: 7 | email: 8 | - shane@shanetomlinson.com 9 | - stomlinson@mozilla.com 10 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This Source Code Form is subject to the terms of the Mozilla Public 2 | License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | You can obtain one at http://mozilla.org/MPL/2.0/. 4 | -------------------------------------------------------------------------------- /test/sample-content/static-resource.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | // connect-substitute will insert a dev before shanetomlinson 6 | window.urlToReplace = "http://shanetomlinson.com"; 7 | 8 | // it must replace both 9 | window.secondURL = "http://shanetomlinson.com"; 10 | -------------------------------------------------------------------------------- /lib/middleware.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | const postprocess = require('postprocess'); 6 | 7 | exports.setup = function(config) { 8 | var from = new RegExp(config.from, 'g'); 9 | var to = config.to; 10 | 11 | return postprocess(function(req, buffer) { 12 | return String(buffer).replace(from, to); 13 | }); 14 | }; 15 | 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "Author": "Shane Tomlinson (https://shanetomlinson.com)", 3 | "name": "connect-substitute", 4 | "description": "A simple Connect middleware to perform dynamic text substitution on served resources", 5 | "keywords": ["connect", "middleware"], 6 | "homepage": "https://github.com/shane-tomlinson/connect-substitute", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/shane-tomlinson/connect-substitute.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/shane-tomlinson/connect-substitute/issues" 13 | }, 14 | "version": "0.0.1-alpha2", 15 | "engines": { 16 | "node": ">= 0.8.0" 17 | }, 18 | "main": "index", 19 | "dependencies": { 20 | "postprocess": "0.2.4" 21 | }, 22 | "devDependencies": { 23 | "connect": "2.7.2", 24 | "nodeunit": ">=0.6.4" 25 | }, 26 | "scripts": { 27 | "test": "nodeunit test" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/substitution-test.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | const substitute = require('../index'), 6 | connect = require('connect'), 7 | path = require('path'), 8 | http = require('http'); 9 | 10 | var app = connect(); 11 | 12 | 13 | // substitute middleware must come before static middleware 14 | app.use(substitute.setup({ 15 | from: "http://shanetomlinson.com", 16 | to: "http://dev.shanetomlinson.com" 17 | })); 18 | 19 | app.use(connect.static(path.join(__dirname, 'sample-content'))); 20 | 21 | var port; 22 | 23 | exports.substitution_performed = function(test) { 24 | var server = http.createServer(app); 25 | server.listen(3000, function() { 26 | http.get("http://127.0.0.1:3000/static-resource.js", function(res) { 27 | var body = ""; 28 | res.on('data', function(chunk) { 29 | body += chunk.toString(); 30 | }); 31 | res.on('end', function() { 32 | test.equal(body.indexOf("http://shanetomlinson.com"), -1); 33 | test.ok(body.indexOf("http://dev.shanetomlinson.com") > -1); 34 | 35 | server.close(); 36 | test.done(); 37 | }); 38 | }); 39 | }); 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # connect-substitute 2 | 3 | A middleware to perform dynamic text substitution on served resources. Useful to automatically replace URLs within resources depending on whether a server is running in dev or production mode. 4 | 5 | ## Usage 6 | 1. Include connect-substitute in a node module. 7 | ``` 8 | const substitute = require("connect-substitute"); 9 | ``` 10 | 11 | 12 | 2. Add a middleware by calling the `setup` function. 13 | ``` 14 | app.use(substitute.setup({ 15 | from: "https://browserid.org", 16 | to: "https://login.persona.org" 17 | })); 18 | ``` 19 | 20 | `from` is the text to be changed. 21 | `to` is what you want to change `from` to. 22 | 23 | The substitution middleware must be added *before* other middlewares that serve resources, including static middleware. 24 | 25 | ## Credits: 26 | This is an *extremely* thin wrapper around [Lloyd Hilaiel's](https://github.com/lloyd/) [connect-postprocess](https://github.com/lloyd/connect-postprocess). He is the brains behind it, I only added a slightly more restrictive but simpler API. 27 | 28 | ## Build Status 29 | [![Build Status](https://secure.travis-ci.org/shane-tomlinson/connect-substitute.png?branch=master)](https://github.com/shane-tomlinson/connect-substitute) 30 | ## Author: 31 | * Shane Tomlinson 32 | * shane@shanetomlinson.com 33 | * stomlinson@mozilla.com 34 | * set117@yahoo.com 35 | * https://shanetomlinson.com 36 | * http://github.com/stomlinson 37 | * http://github.com/shane-tomlinson 38 | * @shane_tomlinson 39 | 40 | ## Getting involved: 41 | Any updates are appreciated 42 | 43 | ## License: 44 | This software is available under version 2.0 of the MPL: 45 | 46 | https://www.mozilla.org/MPL/ 47 | 48 | 49 | --------------------------------------------------------------------------------