summaryrefslogtreecommitdiff
path: root/node_modules/snekfetch/src/node/transports/file.js
blob: a30c5fe113adf8a5f4afdf8653aa7d09cb701bc9 (plain) (blame)
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
const fs = require('fs');
const path = require('path');
const mime = require('../mime');
const EventEmitter = require('events');
const ResponseStream = require('./ResponseStream');

const methods = {
  GET: (filename, req) => {
    req.end = () => {
      const stream = should404(filename) ?
        new ResponseStream().error(404, `ENOENT: no such file or directory, open '${filename}'`) :
        fs.createReadStream(filename);
      req.res = stream;
      stream.headers = {
        'content-length': 0,
        'content-type': mime.lookup(path.extname(filename)),
      };
      stream.on('open', () => {
        req.emit('response', stream);
      });
      if (stream instanceof ResponseStream)
        return;
      stream.statusCode = 200;
      stream.on('end', () => {
        stream.headers['content-length'] = stream.bytesRead;
      });
      /* istanbul ignore next */
      stream.on('error', (err) => {
        stream.statusCode = 400;
        stream.status = err.message;
      });
    };
  },
  POST: (filename, req) => {
    const chunks = [];
    /* istanbul ignore next */
    req.write = (data) => {
      chunks.push(data);
    };
    req.end = (data) => {
      chunks.push(data);
      const stream = fs.createWriteStream(filename);
      const standin = new ResponseStream();
      req.res = standin;
      standin.headers = {
        'content-length': 0,
        'content-type': mime.lookup(path.extname(filename)),
      };
      stream.on('finish', () => {
        req.emit('response', standin);
      });
      stream.on('open', () => {
        (function write() {
          const chunk = chunks.shift();
          if (!chunk)
            return;
          /* istanbul ignore next */
          if (!stream.write(chunk))
            stream.once('drain', write);
          else
            write();
        }());
        stream.end();
      });
    };
  },
  DELETE: (filename, req) => {
    req.end = () => {
      const stream = new ResponseStream();
      req.res = stream;
      stream.headers = {
        'content-length': 0,
        'content-type': mime.lookup(path.extname(filename)),
      };
      fs.unlink(filename, (err) => {
        req.emit('response', err ? stream.error(400, err.message) : stream);
      });
    };
  },
};

class Req extends EventEmitter {
  constructor() {
    super();
    this._headers = {};
  }

  setHeader() {} // eslint-disable-line no-empty-function
  getHeader() {} // eslint-disable-line no-empty-function
}

function request(options) {
  const method = methods[options.method];
  if (!method)
    throw new Error(`Invalid request method for file: "${options.method}"`);
  const filename = options.href.replace('file://', '');

  const req = new Req();
  method(filename, req, options);
  return req;
}

function should404(p) {
  try {
    return fs.lstatSync(p).isDirectory();
  } catch (err) {
    return true;
  }
}

module.exports = {
  request,
};