Esempio n. 1
0
export function svgo(options) {
  const svgo = new SVGO(options);
  return interceptor((req, res) => {
    return {
      isInterceptable: function() {
        return /image\/svg\+xml(;|$)/.test(res.get("content-type"));
      },
      intercept: function(body, send) {
        svgo.optimize(body, (result) => {
          send(result.data);
        });
      }
    };
  });
}
module.exports = function(app, config) {
  if (config.newrelic) {
    var interceptor = require('express-interceptor');
    app.use(/^\/$/,interceptor(function() {
      return {
        isInterceptable: function() {
          return true;
        },
        intercept: function(body, send) {
          var title = '</title>';
          var newTitle = title + config.newrelic.getBrowserTimingHeader();
          var newBody = body.replace(title, newTitle);
          send(newBody);
        }
      };
    }));
  }
};
import { crunch } from "graphql-crunch"
import interceptor from "express-interceptor"

export const interceptorCallback = (req, res) => ({
  isInterceptable: () =>
    req.query.hasOwnProperty("crunch") || req.headers["x-crunch"],
  intercept: (body, send) => {
    body = JSON.parse(body) // eslint-disable-line no-param-reassign
    if (body) {
      body = crunch(body) // eslint-disable-line no-param-reassign
      res.set("X-Crunch", "true")
    }
    send(JSON.stringify(body))
  },
})

export default interceptor(interceptorCallback)
Esempio n. 4
0
module.exports = interceptor(function(req, res) {
	return {
		// Only HTML responses will be intercepted
		isInterceptable: function(){
			// exclude static files
			if(/^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF|js|css)$/.test(req.originalUrl)) {
				return false;
			}

			return /text\/html/.test(res.get('Content-Type'));
		},
		// Generate static html file
		intercept: function(body, send) {
			var fileName = normalizeFilename(req.originalUrl);
			fileName = path.join(config.dist.html, fileName);

			mkdirp(path.dirname(fileName), function (err) {
				if (err) {
					console.error(err);
				} else {
					var html = beautify(body, { indent_size: 2 });

					fs.writeFile(fileName, html, function(err) {
						if(err) {
							return console.log(err);
						}

						res.json({ result: true, file: fileName });
					});
				}
			});

			//send(body);
		}
	};
});
Esempio n. 5
0
let frameScriptInterceptor = interceptor((req, res) => {

  let contentTypeHeader = res.get('Content-Type');
  let injectFrameScriptParam = req.query['inject_frame_script'];
  let editTemplateParam = req.query['edit_template'];
  let addSourceIdsParam = req.query['add_source_ids'];

  let doEditTemplate = editTemplateParam != null;
  let doInjectFrameScript = (injectFrameScriptParam != null) || doEditTemplate;
  let doAddSourceIds = (addSourceIdsParam != null) || doInjectFrameScript;


  return {
    isInterceptable() {
      let isHtml = req.path.endsWith('.html');
      let mimeType = contentTypeHeader && contentType.parse(contentTypeHeader).type;
      let intercept = doAddSourceIds || doInjectFrameScript && isHtml;
      return intercept;
    },

    intercept(body, send) {
      // Create the source document. We need this even if we generate a synthetic
      // document
      let document = parse5.parse(body);
      if (doAddSourceIds) {
        addSourceIds(document, editTemplateParam);
      }

      if (doEditTemplate) {
        document = generateTemplateDocument(document, editTemplateParam);
      }

      if (doInjectFrameScript) {
        injectFrameScript(document);
      }

      let text = parse5.serialize(document);
      send(text);
    },
  }
});
    'drain,close,error,finish,pipe,unpipe,data'.split(',').forEach(function(evento){
        res.on(evento, function(data){
            fs.appendFile('res.log','\n--- on '+evento+'\n');
            fs.appendFile('res.log',data);
        });
    });
    next();
});
*/
var logInterceptor = interceptor(function(req, res){
  return {
    isInterceptable: function(){
      // return /text\/html/.test(res.get('Content-Type'));
      return true;
    },
    // Appends a paragraph at the end of the response body 
    intercept: function(body, send) {
      fs.appendFile('res.log','\n------ comienzo\n');
      fs.appendFile('res.log',body);
      send(body);
    }
  };
})

app.use(logInterceptor);

app.get('/hola.txt', function (req, res) {
    res.send('<b>Hola</b> Mundo!')
})

app.get('/hola.html', function (req, res) {
    res.setHeader('Content-Type', 'text/html');