-
Notifications
You must be signed in to change notification settings - Fork 242
/
index.js
144 lines (123 loc) · 3.9 KB
/
index.js
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
// an express wrapper to run given handler
const path = require("path"),
url = require("url"),
Youch = require("youch"),
YouchTerminal = require("youch-terminal"),
express = require("express");
const FETCH = require("node-fetch");
const debug = require("debug")("core");
const GLOBALS = require("./globals");
const session = require("zero-express-session");
const vm = require("vm");
process.on("unhandledRejection", (reason, p) => {
console.log(reason);
});
module.exports = async (handler, pageData, buildInfo) => {
try {
buildInfo = JSON.parse(buildInfo);
} catch (e) {}
var app = await startServer(handler, pageData, buildInfo);
return app;
};
function generateFetch(req) {
const serverAddress = process.env.SERVERADDRESS;
return function fetch(uri, options) {
// fix relative path when running on server side.
if (uri && uri.indexOf("://") === -1) {
// see if it's a path from root of server
if (uri.startsWith("/")) {
uri = url.resolve(serverAddress, uri);
}
}
if (options && options.credentials && options.credentials === "include") {
options.headers = req.headers;
}
debug("paths", req.originalUrl, req.baseUrl, req.path);
debug("fetching", uri, options, serverAddress);
return FETCH(uri, options);
};
}
function startServer(handler, pageData, buildInfo) {
return new Promise((resolve, reject) => {
const file = path.resolve(pageData.entryFile);
const app = express();
app.disable("x-powered-by");
// bootstrap express app with session
session(app);
app.use(require("body-parser").urlencoded({ extended: true }));
app.use(require("body-parser").json());
// change $path into express-style :path/
const pathPattern = pageData.path
.split("/")
.map(p => {
if (p.startsWith("$")) return ":" + p.slice(1);
return p;
})
.join("/");
app.all(pathPattern, (req, res) => {
try {
var globals = Object.assign(
{
__Zero: {
app,
handler,
pageData,
buildInfo,
req,
res,
renderError,
__DIRNAME: path.dirname(file),
__FILENAME: file,
fetch: generateFetch(req)
}
},
GLOBALS
);
// we run the handler in it's own VM so we can inject some variables (__dirname and __filename) and global function (fetch()) to it.
vm.runInNewContext(
`
const { app, handler, pageData, buildInfo, req, res, fetch, renderError, __DIRNAME, __FILENAME } = __Zero;
global.fetch = fetch
global.app = app
global.__DIRNAME = __DIRNAME
global.__FILENAME = __FILENAME
handler(req, res, pageData, buildInfo)
`,
globals
);
} catch (error) {
renderError(error, req, res);
}
});
return resolve(app);
});
}
async function renderError(error, req, res) {
const youch = new Youch(error, req);
// print error in terminal
youch.toJSON().then(output => {
console.log(YouchTerminal(output));
});
var html = await youch
.addLink(({ message }) => {
var style = `text-decoration: none; border: 1px solid #dcdcdc; padding: 9px 12px;`;
const urlStack = `https://stackoverflow.com/search?q=${encodeURIComponent(
`${message}`
)}`;
const urlGoogle = `https://www.google.com/search?q=${encodeURIComponent(
`${message}`
)}`;
return `
<a style="${style}" href="${urlGoogle}" target="_blank" title="Search on Google">Search Google</a>
<a style="${style}" href="${urlStack}" target="_blank" title="Search on StackOverflow">Search StackOverflow</a>
`;
})
.toHTML();
try {
res.writeHead(200, { "content-type": "text/html" });
res.write(html);
res.end();
} catch (e) {
// ignore
}
}