deno.land / x / sheetjs@v0.18.3 / demos / server / koa.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
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
const Koa = require('koa'), app = new Koa();const { sprintf } = require('printj');const { IncomingForm } = require('formidable');const { fork } = require('child_process');const logit = require('./_logit');const subprocess = fork('koasub.js');
const get_data = async (ctx, type) => { await new Promise((resolve, reject) => { const cb = (data) => { ctx.response.body = Buffer(data); subprocess.removeListener('message', cb); resolve(); }; subprocess.on('message', cb); subprocess.send(['get data', type]); });};
const get_file = async (ctx, file) => { await new Promise((resolve, reject) => { const cb = (data) => { ctx.response.body = Buffer(data); subprocess.removeListener('message', cb); resolve(); }; subprocess.on('message', cb); subprocess.send(['get file', file]); });};
const load_data = async (ctx, file) => { await new Promise((resolve, reject) => { const cb = (data) => { ctx.response.body = "ok\n"; subprocess.removeListener('message', cb); resolve(); }; subprocess.on('message', cb); subprocess.send(['load data', file]); });};
const post_data = async (ctx) => { const keys = Object.keys(ctx.request._files), k = keys[0]; await load_data(ctx, ctx.request._files[k].path);};
app.use(async (ctx, next) => { logit(ctx.req, ctx.res); await next(); });app.use(async (ctx, next) => { const form = new IncomingForm(); await new Promise((resolve, reject) => { form.parse(ctx.req, (err, fields, files) => { if(err) return reject(err); ctx.request._fields = fields; ctx.request._files = files; resolve(); }); }); await next();});app.use(async (ctx, next) => { if(ctx.request.method !== 'GET') await next(); else if(ctx.request.path !== '/') await next(); else if(ctx.request.query.t) await get_data(ctx, ctx.request.query.t); else if(ctx.request.query.f) await get_file(ctx, ctx.request.query.f); else ctx.throw(403, "Forbidden");});app.use(async (ctx, next) => { if(ctx.request.method !== 'POST') await next(); else if(ctx.request.path !== '/') await next(); else if(ctx.request.query.f) await load_data(ctx, ctx.request.query.f); else await post_data(ctx);});
const port = +process.argv[2] || +process.env.PORT || 7262;app.listen(port, () => { console.log('Serving HTTP on port ' + port); });
sheetjs

Version Info

Tagged at
2 years ago