-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
327 lines (310 loc) · 8.37 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
require("./patch");
const config = require("./config.loader");
const fs = require("fs-extra");
const fetch = require("isomorphic-fetch");
const Koa = require("koa");
const serve = require("koa-static");
const mount = require("koa-mount");
const send = require("koa-send");
const bodyParser = require("koa-bodyparser");
const proxy = require("koa-proxies");
const React = require("react");
const ReactDOMServer = require("react-dom/server");
const app = new Koa();
const { getEbclientCp, getDicts, query } = require("./api");
const {
translate,
kanaKannji,
fixJapanese,
initJapaneseAnalyzer,
kuroshiroConvert,
furiganaWithAnalyzeText,
furiganaAnalyzeTextOnly,
analyzeJapaneseText,
correctJapaneseText,
} = require("./api/japaneseAnalyzer");
const { googleAuth } = require("./api/cloud");
const {
parseSitePath,
normalizeQ,
getCanonicalUrlSearch,
} = require("./client/functions");
const getPreloadedState = require("./getPreloadedState");
const {
default: IndexComponent,
getStore,
VERSION,
COMMIT_HASH,
} = require("./dist/bundle_server");
const assets = require("./dist/assets.json");
const MANIFEST = require("./dist/assets/manifest.json");
Object.assign(MANIFEST, {
name: config.SITENAME,
short_name: config.SHORTNAME || config.SITENAME,
scope: config.ROOTPATH,
start_url: config.ROOTPATH + "default/",
});
MANIFEST.share_target.action = config.ROOTPATH + "default/";
const OPENSEARCHXML = fs
.readFileSync(__dirname + "/opensearch.xml", "utf8")
.replace(/__SITENAME__/g, config.SITENAME)
.replace(/__DESCRIPTION__/g, config.DESCRIPTION)
.replace(/__PUBLIC_URL__/g, config.PUBLIC_URL)
.replace(/__ROOTPATH__/g, config.ROOTPATH);
const api2Handles = {
0: kuroshiroConvert,
1: analyzeJapaneseText,
2: correctJapaneseText,
3: translate,
4: furiganaWithAnalyzeText,
5: furiganaAnalyzeTextOnly,
6: kanaKannji,
7: fixJapanese,
};
const DEFAULT_PARAMS = {
q: "",
dict: "",
marker: "",
type: 0,
romaji: 1,
page: null,
offset: null,
};
const NO_CACHE_HEADERS = {
"Cache-control": "no-store",
Pragma: "no-cache",
Expires: "0",
};
const PROXIES = [
{ id: "jisho_api", url: "https://jisho.org/api/v1/search/words" },
];
if (config.PROXIES) {
PROXIES.push(...config.PROXIES);
}
app.proxy = !!config.PROXY;
PROXIES.forEach((p) => {
var url = new URL(p.url);
var proxypath = `${config.REALROOT}proxy/${p.id}`;
app.use(
proxy(proxypath, {
target: url.origin,
rewrite: (path) => url.pathname + path.slice(proxypath.length),
changeOrigin: true,
// logs: true,
})
);
});
app.use(
bodyParser({ enableTypes: ["json", "form", "text"], textLimit: "8mb" })
); // ctx.request.body;
app.use(async (ctx, next) => {
if (
(ctx.method === "HEAD" || ctx.method === "GET") &&
ctx.path == config.REALROOT + "service-worker.js"
) {
// overwrite service-worker.js cache-control header
return await send(ctx, ctx.path.slice(config.REALROOT.length - 1), {
root: __dirname + "/dist",
maxage: 0,
});
}
if (ctx.path == config.REALROOT + "assets/manifest.json") {
if (ctx.query.dict) {
return (ctx.body = Object.assign({}, MANIFEST, {
start_url: config.ROOTPATH + encodeURIComponent(ctx.query.dict) + "/",
}));
}
return (ctx.body = MANIFEST);
}
await next();
});
app.use(
mount(
config.REALROOT + "mod/",
serve(__dirname + "/dist/mod", { maxAge: config.SHORT_CACHE_AGE * 1000 })
)
);
app.use(
mount(
config.REALROOT,
serve(__dirname + "/dist", { maxAge: config.CACHE_AGE * 1000 })
)
);
app.use(async (ctx, next) => {
if (ctx.path == config.REALROOT + "opensearch.xml") {
ctx.type = "xml";
return (ctx.body = OPENSEARCHXML);
}
let {
q,
dict,
marker,
type,
max,
romaji,
page,
offset,
endpage,
endoffset,
binary,
width,
height,
...otherParams
} = ctx.query;
if (ctx.path.startsWith(config.REALROOT)) {
ctx.relativePath = ctx.path.slice(config.REALROOT.length);
ctx.params = Object.assign(
{
q: q || "",
dict: dict || "",
marker: marker || "",
type: type != null ? parseInt(type) || 0 : 0,
page: page != null && page !== "" ? parseInt(page) || 0 : null,
offset: offset != null && offset !== "" ? parseInt(offset) || 0 : null,
binary,
width,
height,
endpage,
endoffset,
},
ctx.query.__nossr__ === undefined ? parseSitePath(ctx.relativePath) : {}
);
ctx.params.romaji =
romaji != null
? parseInt(romaji) || 0
: ctx.query.api ||
(ctx.params.q &&
normalizeQ(ctx.params.q, 0) != normalizeQ(ctx.params.q, 1))
? 0
: 1;
if (max == null) {
max = config.MAX_DEFAULT;
}
ctx.params.max = ctx.params.dict.match(/[^+_][+_]+[^+_]/)
? Math.max(0, Math.min(parseInt(max) || 0, config.MAX_CAP2))
: Math.max(0, Math.min(parseInt(max) || 0, config.MAX_CAP));
} else {
ctx.redirect((config.PUBLIC_URL || ctx.origin) + config.ROOTPATH);
return;
}
if (!ctx.query.api && !ctx.params.binary) return next();
// text to furigana :
if (ctx.query.api == 2) {
ctx.body = await api2Handles[ctx.params.type](
typeof ctx.request.body == "string" ? ctx.request.body : ctx.params.q,
otherParams
);
return;
} else if (ctx.query.api == 3) {
ctx.set(NO_CACHE_HEADERS);
ctx.body = {
VERSION,
CONFIG_HASH: config.$$hash,
COMMIT_HASH,
};
return;
} else if (ctx.query.api == 4) {
let { status, redirect, body } = await googleAuth(
ctx.query,
ctx.request.body
);
// console.log("----api4 body", status, body);
if (redirect) {
ctx.redirect(redirect);
}
if (status) {
ctx.status = status;
}
ctx.body = body || "";
return;
} else if (ctx.query.api == 5) {
let res = await fetch(
`https://customsearch.googleapis.com/customsearch/v1?key=${config.GOOGLE_SEARCH_API_KEY}&cx=${config.GOOGLE_SEARCH_API_ENGINE}&${ctx.querystring}`
);
ctx.status = res.status;
ctx.type = "json";
ctx.body = res.body;
return;
}
if (!ctx.params.dict && !ctx.params.q) {
ctx.body = await getDicts();
} else {
let body = await query(ctx.params);
if (
ctx.params.binary &&
body &&
(Buffer.isBuffer(body) || typeof body.on === "function")
) {
if (ctx.params.binary == "mono") ctx.type = "bmp";
else if (ctx.params.binary == "gaiji") ctx.type = "png";
else if (ctx.params.binary == "wav") ctx.type = "audio/wav";
// chrome do not support audio/wave well
else ctx.type = ctx.params.binary;
}
if (body === undefined) {
ctx.status = 404;
} else {
ctx.body = body;
}
}
});
app.use(async (ctx, next) => {
if (!ctx.path.startsWith(config.REALROOT)) {
return (ctx.status = 404);
}
ctx.type = "html";
ctx.set("Referrer-Policy", "same-origin");
let preloadedState = await getPreloadedState(ctx.params);
let { store } = getStore(preloadedState);
if (
config.REDIRECT_NON_CANONICAL_URL &&
ctx.hostname != "localhost" &&
ctx.query.__nossr__ === undefined &&
ctx.query.__nocache__ === undefined
) {
let canonicalUrl =
(config.PUBLIC_URL || ctx.origin) +
config.ROOTPATH +
getCanonicalUrlSearch(store.getState(), config.DEFAULTDICT);
if (
canonicalUrl !=
ctx.origin + config.ROOTPATH + ctx.relativePath + ctx.search
) {
// console.log("canonicalUrl", canonicalUrl, ctx.href);
ctx.status = 301;
ctx.redirect(canonicalUrl);
return;
}
}
//console.log("Initial state: ", store.getState());
ctx.body =
"<!DOCTYPE html>" +
ReactDOMServer.renderToStaticMarkup(
React.createElement(IndexComponent, {
store,
preloadedState,
config,
assets,
manifest: MANIFEST,
script: (await fs.exists(
__dirname + "/dist/mod/mod.js",
fs.constants.R_OK
))
? config.ROOTPATH + "mod/mod.js"
: null,
})
);
if (preloadedState.words.length == 0 && preloadedState.page != null) {
ctx.status = 404;
}
});
(async () => {
if (config.EBCLIENT_BIN) {
await getEbclientCp();
}
if (config.YAHOO_APPID) {
// for use Yahoo japanese morphological analyzer WebAPI
await initJapaneseAnalyzer({ yahooAppId: config.YAHOO_APPID });
}
app.listen(config.PORT, config.IP);
})();