use vercel serverless

This commit is contained in:
SheetJS 2020-08-02 21:06:11 -04:00
parent 725c2b1795
commit c2acee143f
8 changed files with 54 additions and 59 deletions

6
.gitignore vendored

@ -1 +1,7 @@
.now
node_modules
# Environment Variables
.env
.env.build
.vercel

2
.nowignore Normal file

@ -0,0 +1,2 @@
README.md
yarn.lock

@ -1,8 +1,8 @@
# sheetaki
Spreadsheet CSV conversion microservice
Spreadsheet CSV conversion function. Deployed on <https://sheetaki.now.sh>
Examples:
- [https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls](https://sheetaki.now.sh/data/?url=https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls)
- [https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls](https://sheetaki.now.sh/api/data/?url=https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls)

36
api/data/index.js Normal file

@ -0,0 +1,36 @@
const XLSX = require('xlsx');
const URL = require('url');
const request = require('request');
const do_url = (req, url, res) => {
request(url.query.url, {encoding:null}, function(err, response, body) {
if(err) return res.status(500).send(err.toString());
switch(response.statusCode) {
case 200: break;
case 404: return res.status(404).send(`Cannot find ${url.query.url}`);
default: return res.status(500).send(`Unrecognized status code ${response.statusCode}`);
}
const wb = XLSX.read(body, {type:'buffer'});
const N = url.query.N ? parseInt(url.query.N,10) : 0;
if(N < 0) {
switch(url.query.t || "csv") {
case "json": return res.status(200).send(JSON.stringify(wb.SheetNames.join("\n")));
default: return res.status(200).send(wb.SheetNames.join("\n"));
}
}
if(N >= wb.SheetNames.length) return res.status(500).send(`Cannot find sheet ${N}`);
var ws = wb.Sheets[wb.SheetNames[N]];
switch(url.query.t) {
case "json": return res.status(200).json(XLSX.utils.sheet_to_json(ws, {header:1, raw:true}));
case "html": return XLSX.stream.to_html(ws).pipe(res);
default: XLSX.stream.to_csv(ws).pipe(res);
}
});
};
module.exports = function(req, res) {
var url = URL.parse(req.url, true);
res.setHeader('Access-Control-Allow-Origin', '*');
if(!url.query.url) return res.status(500).send("Must issue command");
do_url(req, url, res);
};

@ -1,51 +0,0 @@
var XLSX = require('xlsx');
var URL = require('url');
var request = require('request');
var micro = require('micro');
var fs = require("fs");
var HTML = fs.readFileSync("index.html");
function do_url(req, url, res) {
request(url.query.url, {encoding:null}, function(err, response, body) {
if(err) return micro.send(res, 500, err);
switch(response.statusCode) {
case 200: break;
case 404: return micro.send(res, 404, "Cannot find " + url.query.url);
default: return micro.send(res, 500, "Unrecognized status code " + response.statusCode);
}
var wb = XLSX.read(body, {type:'buffer'});
var N = url.query.N ? parseInt(url.query.N,10) : 0;
if(N < 0) {
switch(url.query.t || "csv") {
case "json": return micro.send(res, 200, JSON.stringify(wb.SheetNames.join("\n")));
default: return micro.send(res, 200, wb.SheetNames.join("\n"));
}
}
if(N >= wb.SheetNames.length) return micro.send(res, 500, "Cannot find sheet " + N);
var ws = wb.Sheets[wb.SheetNames[N]];
switch(url.query.t) {
case "json": return micro.send(res, 200, JSON.stringify(XLSX.utils.sheet_to_json(ws, {header:1, raw:true})));
case "html": return XLSX.stream.to_html(ws).pipe(res);
default: XLSX.stream.to_csv(ws).pipe(res);
}
});
}
module.exports = function(req, res) {
var url = URL.parse(req.url, true);
if(url.pathname == "/") {
res.writeHead(200, {
'Content-Type': 'text/html; charset=UTF-8'
});
res.end(HTML);
return;
}
res.setHeader('Access-Control-Allow-Origin', '*');
var mode = -1;
if(url.query.url) mode = 0;
if(mode == -1) { micro.send(res, 500, "Must issue command"); return; }
switch(mode) {
case 0: do_url(req, url, res); break;
}
};

3
now.json Normal file

@ -0,0 +1,3 @@
{
"version": 2
}

@ -10,9 +10,8 @@
"xlsx":""
},
"scripts": {
"start": "micro"
"build": "echo hai"
},
"homepage": "https://sheetaki.now.sh",
"license": "Apache-2.0",
"engines": { "node": ">=6" }
"license": "Apache-2.0"
}

@ -8,7 +8,7 @@
<pre>
<b><a href="http://sheetjs.com">SheetJS</a> Spreadsheet Conversion Service</b>
send a request to /data?url=&lt;url&gt;&amp;N=&lt;idx&gt; to convert the spreadsheet at `url` to a simpler format
send a request to /api/data?url=&lt;url&gt;&amp;N=&lt;idx&gt; to convert the spreadsheet at `url` to a simpler format
<b>parameters:</b>
- url=&lt;url&gt; the url to request
@ -39,7 +39,7 @@ var URLS = [
var parms = ["N", "t"];
for(var i = 0; i < URLS.length; ++i) {
if(URLS[i].length < 2) { document.getElementById("examples").innerHTML += (URLS[i][0]||"") + "\n"; continue; }
var path = '/data?url=' + URLS[i][1].url;
var path = '/api/data?url=' + URLS[i][1].url;
for(var j = 0; j < parms.length; ++j) if(URLS[i][1][parms[j]]) path += '&' + parms[j] + '=' + URLS[i][1][parms[j]];
document.getElementById("examples").innerHTML += '- ' + URLS[i][0] + ' <a href="' + path + '">' + path + '</a>\n';
}
@ -51,7 +51,7 @@ for(var i = 0; i < URLS.length; ++i) {
- <a href="https://github.com/SheetJS/sheetaki">Source code for this service</a>
- <a href="https://github.com/SheetJS/js-xlsx">Source code for the js-xlsx spreadsheet library</a>
- <a href="https://github.com/SheetJS/sheetjs">Source code for the SheetJS spreadsheet library</a>
</pre>
<script type="text/javascript">
var _gaq = _gaq || [];