All files / server-old/reply reply.js

100% Statements 94/94
91.43% Branches 32/35
100% Functions 32/32
100% Lines 90/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 18227x 27x     27x 115x 115x 107x         27x 2x 2x   2x         27x     6x 1x     5x 1x     4x 1x     3x 3x 2x     3x 3x 1x   2x 2x       3x         27x 4x 4x   4x         27x     10x 1x     9x 1x     8x 8x 7x     8x 3x 1x   2x 2x       8x         27x 1x 1x   1x     27x 1x 1x   1x     27x 1x 1x   1x     27x 1x 1x   1x     27x     3x 1x     2x 1x     1x   1x   1x 1x   1x     27x     84x 1x     83x 83x   83x     27x 18x   18x 18x   18x     27x 1x 1x   1x     27x 103x 119x   99x       27x  
const path = require('path');
const fs = require('mz/fs');
 
 
const Reply = function (name, ...args) {
  this.stack = [];
  this[name](...args);
  return this;
};
 
 
 
Reply.prototype.cookie = function (...args) {
  this.stack.push(ctx => {
    ctx.res.cookie(...args);
  });
  return this;
};
 
 
 
Reply.prototype.download = function (...args) {
 
  // Guard clauses
  if (args.length < 1) {
    throw new Error('download() expects a path as the first argument');
  }
 
  if (args.length < 2) {
    throw new Error('download() expects a filename as the second argument');
  }
 
  if (args.length > 2) {
    throw new Error('download() only expects two arguments, path and filename. The rest of them will be ignored');
  }
 
  let [file, opts] = args;
  if (!path.isAbsolute(file)) {
    file = path.resolve(process.cwd(), file);
  }
 
  this.stack.push(async ctx => {
    if (!await fs.exists(file)) {
      throw new Error(`The file "${file}" does not exist. Make sure that you set an absolute path or a relative path to the root of your project`);
    }
    return new Promise((resolve, reject) => {
      ctx.res.download(file, opts, err => err ? reject(err) : resolve());
    });
  });
 
  return this;
};
 
 
 
Reply.prototype.end = function () {
  this.stack.push(ctx => {
    ctx.res.end();
  });
  return this;
};
 
 
 
Reply.prototype.file = function (...args) {
 
  // Guard clauses
  if (args.length < 1) {
    throw new Error('file() expects a path as the first argument');
  }
 
  if (args.length > 2) {
    throw new Error(`file() only expects two arguments, the path and options, but ${args.length} were provided.`);
  }
 
  let [file, opts = {}] = args;
  if (!path.isAbsolute(file)) {
    file = path.resolve(process.cwd(), file);
  }
 
  this.stack.push(async ctx => {
    if (!await fs.exists(file)) {
      throw new Error(`The file "${file}" does not exist. Make sure that you set an absolute path or a relative path to the root of your project`);
    }
    return new Promise((resolve, reject) => {
      ctx.res.sendFile(file, opts, err => err ? reject(err) : resolve());
    });
  });
 
  return this;
};
 
 
 
Reply.prototype.header = function (...args) {
  this.stack.push(ctx => {
    ctx.res.header(...args);
  });
  return this;
};
 
Reply.prototype.json = function (...args) {
  this.stack.push(ctx => {
    ctx.res.json(...args);
  });
  return this;
};
 
Reply.prototype.jsonp = function (...args) {
  this.stack.push(ctx => {
    ctx.res.jsonp(...args);
  });
  return this;
};
 
Reply.prototype.redirect = function (...args) {
  this.stack.push(ctx => {
    ctx.res.redirect(...args);
  });
  return this;
};
 
Reply.prototype.render = function (...args) {
 
  // Guard clauses
  if (args.length < 1) {
    throw new Error('file() expects a path');
  }
 
  if (args.length > 2) {
    throw new Error('file() expects a path and options but nothing else');
  }
 
  let [file, opts = {}] = args;
 
  this.stack.push(ctx => new Promise((resolve, reject) => {
    // Note: if callback is provided, it does not send() automatically
    const cb = (err, html) => err ? reject(err) : resolve(ctx.res.send(html));
    ctx.res.render(file, opts, cb);
  }));
  return this;
};
 
Reply.prototype.send = function (...args) {
 
  // If we are trying to send the context
  if (args[0] && args[0].close && args[0].close instanceof Function) {
    throw new Error('Never send the context, request or response as those are a security risk');
  }
 
  this.stack.push(ctx => {
    ctx.res.send(...args);
  });
  return this;
};
 
Reply.prototype.status = function (...args) {
  this.stack.push(ctx => {
    // In case there is no response, it'll respond with the status
    ctx.res.explicitStatus = true;
    ctx.res.status(...args);
  });
  return this;
};
 
Reply.prototype.type = function (...args) {
  this.stack.push(ctx => {
    ctx.res.type(...args);
  });
  return this;
};
 
Reply.prototype.exec = async function (ctx) {
  for (let cb of this.stack) {
    await cb(ctx);
  }
  this.stack = [];
};
 
// This will make that the first time a function is called it starts a new stack
module.exports = Reply;