Skip to content

Commit

Permalink
fix: memory file not exist, use disk file
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed May 30, 2018
1 parent 17dc5b9 commit bdd6e63
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const path = require('path');
const fs = require('fs');
const proxy = require('koa-proxy');
const proxy = require('./lib/proxy');
const Constant = require('./lib/constant');
module.exports = app => {
app.use(function* (next) {
Expand Down
142 changes: 142 additions & 0 deletions lib/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
'use strict';

const join = require('url').resolve;
const iconv = require('iconv-lite');
const coRequest = require('co-request');

module.exports = options => {
options || (options = {});
const request = coRequest.defaults({
jar: options.jar === true
});

if (!(options.host || options.map || options.url)) {
throw new Error('miss options');
}

return function* proxy(next) {
const url = resolve(this.path, options);
if (typeof options.suppressRequestHeaders === 'object') {
options.suppressRequestHeaders.forEach((h, i) => {
options.suppressRequestHeaders[i] = h.toLowerCase();
});
}

const suppressResponseHeaders = []; // We should not be overwriting the options object!
if (typeof options.suppressResponseHeaders === 'object') {
options.suppressResponseHeaders.forEach(h => {
suppressResponseHeaders.push(h.toLowerCase());
});
}

// don't match
if (!url) {
return yield* next;
}

// if match option supplied, restrict proxy to that match
if (options.match) {
if (!this.path.match(options.match)) {
return yield* next;
}
}

const parsedBody = getParsedBody(this);

let opt = {
url: url + (this.querystring ? '?' + this.querystring : ''),
headers: this.header,
encoding: null,
followRedirect: !(options.followRedirect === false),
method: this.method,
body: parsedBody,
};

// set 'Host' header to options.host (without protocol prefix), strip trailing slash
if (options.host) opt.headers.host = options.host.slice(options.host.indexOf('://') + 3).replace(/\/$/, '');

if (options.requestOptions) {
if (typeof options.requestOptions === 'function') {
opt = options.requestOptions(this.request, opt);
} else {
Object.keys(options.requestOptions).forEach(option => {
opt[option] = options.requestOptions[option];
});
}
}

for (const name in opt.headers) {
if (options.suppressRequestHeaders && options.suppressRequestHeaders.indexOf(name.toLowerCase()) >= 0) {
delete opt.headers[name];
}
}

const res = yield request(opt);
if (res.statusCode === 404) {
yield next;
} else {
this.status = res.statusCode;
for (const name in res.headers) {
// http://stackoverflow.com/questions/35525715/http-get-parse-error-code-hpe-unexpected-content-length
if (suppressResponseHeaders.indexOf(name.toLowerCase()) >= 0) {
continue;
}
if (name === 'transfer-encoding') {
continue;
}
this.set(name, res.headers[name]);
}

if (options.encoding === 'gbk') {
this.body = iconv.decode(res.body, 'gbk');
return;
}

this.body = res.body;

if (options.yieldNext) {
yield next;
}
}
};
};

function resolve(path, options) {
let url = options.url;
if (url) {
if (!/^http/.test(url)) {
url = options.host ? join(options.host, url) : null;
}
return ignoreQuery(url);
}

if (typeof options.map === 'object') {
if (options.map && options.map[path]) {
path = ignoreQuery(options.map[path]);
}
} else if (typeof options.map === 'function') {
path = options.map(path);
}

return options.host ? join(options.host, path) : null;
}

function ignoreQuery(url) {
return url ? url.split('?')[0] : null;
}

function getParsedBody(ctx) {
let body = ctx.request.body;
if (body === undefined || body === null) {
return undefined;
}
const contentType = ctx.request.header['content-type'];
if (!Buffer.isBuffer(body) && typeof body !== 'string') {
if (contentType && contentType.indexOf('json') !== -1) {
body = JSON.stringify(body);
} else {
body = body + '';
}
}
return body;
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-webpack",
"version": "4.1.1",
"version": "4.1.2",
"description": "webpack dev server plugin for egg, support read file in memory and hot reload.",
"eggPlugin": {
"name": "webpack",
Expand All @@ -17,9 +17,10 @@
"hot-reload"
],
"dependencies": {
"koa-proxy": "^0.9.0",
"mkdirp": "^0.5.1",
"webpack-tool": "^4.0.0"
"webpack-tool": "^4.0.0",
"co-request": "^0.2.0",
"iconv-lite": "^0.2.11"
},
"devDependencies": {
"autod": "^2.7.1",
Expand Down

0 comments on commit bdd6e63

Please sign in to comment.