HeaderHostTransformer.js 613 B

1234567891011121314151617181920212223
  1. const { Transform } = require('stream');
  2. class HeaderHostTransformer extends Transform {
  3. constructor(opts = {}) {
  4. super(opts);
  5. this.host = opts.host || 'localhost';
  6. this.replaced = false;
  7. }
  8. _transform(data, encoding, callback) {
  9. callback(
  10. null,
  11. this.replaced // after replacing the first instance of the Host header we just become a regular passthrough
  12. ? data
  13. : data.toString().replace(/(\r\n[Hh]ost: )\S+/, (match, $1) => {
  14. this.replaced = true;
  15. return $1 + this.host;
  16. })
  17. );
  18. }
  19. }
  20. module.exports = HeaderHostTransformer;