From c2acee143f95484ab985b9378bafb6917f973125 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Sun, 2 Aug 2020 21:06:11 -0400 Subject: [PATCH] use vercel serverless --- .gitignore | 6 ++++ .nowignore | 2 ++ README.md | 4 +-- api/data/index.js | 36 +++++++++++++++++++++++ index.js | 51 --------------------------------- now.json | 3 ++ package.json | 5 ++-- index.html => public/index.html | 6 ++-- 8 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 .nowignore create mode 100644 api/data/index.js delete mode 100644 index.js create mode 100644 now.json rename index.html => public/index.html (90%) diff --git a/.gitignore b/.gitignore index 3c3629e..ac09275 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ +.now node_modules + +# Environment Variables +.env +.env.build +.vercel \ No newline at end of file diff --git a/.nowignore b/.nowignore new file mode 100644 index 0000000..06e3459 --- /dev/null +++ b/.nowignore @@ -0,0 +1,2 @@ +README.md +yarn.lock \ No newline at end of file diff --git a/README.md b/README.md index ad0d4c6..7725284 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # sheetaki -Spreadsheet CSV conversion microservice +Spreadsheet CSV conversion function. Deployed on 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) diff --git a/api/data/index.js b/api/data/index.js new file mode 100644 index 0000000..fc60e1d --- /dev/null +++ b/api/data/index.js @@ -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); +}; diff --git a/index.js b/index.js deleted file mode 100644 index 47cda93..0000000 --- a/index.js +++ /dev/null @@ -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; - } -}; diff --git a/now.json b/now.json new file mode 100644 index 0000000..f018fe5 --- /dev/null +++ b/now.json @@ -0,0 +1,3 @@ +{ + "version": 2 +} diff --git a/package.json b/package.json index b4973a1..e9a5d92 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/index.html b/public/index.html similarity index 90% rename from index.html rename to public/index.html index e216d6b..cccd588 100644 --- a/index.html +++ b/public/index.html @@ -8,7 +8,7 @@
 SheetJS Spreadsheet Conversion Service
 
-send a request to /data?url=<url>&N=<idx> to convert the spreadsheet at `url` to a simpler format
+send a request to /api/data?url=<url>&N=<idx> to convert the spreadsheet at `url` to a simpler format
 
 parameters:
 - url=<url>      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] + ' ' + path + '\n';
 }
@@ -51,7 +51,7 @@ for(var i = 0; i < URLS.length; ++i) {
 
 - Source code for this service
 
-- Source code for the js-xlsx spreadsheet library
+- Source code for the SheetJS spreadsheet library