Skip to content

Commit 0c7e809

Browse files
authored
[ci skip] Fixed functions demos (#2025)
* Added test for CRLF newlines * Initialized firebase functions app * Updated gitignore * Implemented csv file conversion * CSV conversion * Modified README to include firebase * Added init-azure script to makefile * Updated Azure demo * Updated README
1 parent d0457b7 commit 0c7e809

File tree

9 files changed

+151
-5
lines changed

9 files changed

+151
-5
lines changed

demos/function/AzureHTTPTrigger/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ module.exports = (context, req) => {
2626
if(!f) {
2727
context.res = { status: 400, body: "Must submit a file for processing!" };
2828
} else {
29-
/* since the file is Base64-encoded, read the file and parse as "base64" */
30-
const b64 = fs.readFileSync(f.path).toString();
31-
const wb = XLSX.read(b64, {type:"base64"});
29+
/* file is stored in a temp directory, so we can point to that and read it */
30+
const wb = XLSX.read(f.path, {type:"file"});
3231

3332
/* convert to specified output type -- default CSV */
3433
const ext = (fields.bookType || "csv").toLowerCase();

demos/function/Firebase/.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
9+
# Firebase cache
10+
.firebase/
11+
12+
# Firebase config
13+
14+
# Uncomment this if you'd like others to create their own Firebase project.
15+
# For a team working on the same Firebase project(s), it is recommended to leave
16+
# it commented so all members can deploy to the same project(s) in .firebaserc.
17+
.firebaserc
18+
19+
# Runtime data
20+
pids
21+
*.pid
22+
*.seed
23+
*.pid.lock
24+
25+
# Directory for instrumented libs generated by jscoverage/JSCover
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
coverage
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (http://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
49+
# Optional npm cache directory
50+
.npm
51+
52+
# Optional eslint cache
53+
.eslintcache
54+
55+
# Optional REPL history
56+
.node_repl_history
57+
58+
# Output of 'npm pack'
59+
*.tgz
60+
61+
# Yarn Integrity file
62+
.yarn-integrity
63+
64+
# dotenv environment variables file
65+
.env
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const functions = require('firebase-functions');
2+
const Busboy = require('busboy');
3+
const XLSX = require('xlsx');
4+
5+
// // Create and Deploy Your First Cloud Functions
6+
// // https://firebase.google.com/docs/functions/write-firebase-functions
7+
//
8+
exports.helloWorld = functions.https.onRequest((request, response) => {
9+
response.send("Hello from Firebase!");
10+
});
11+
12+
exports.main = functions.https.onRequest((req, res) => {
13+
var bb = new Busboy({
14+
headers: {
15+
'content-type': req.headers['content-type']
16+
}
17+
});
18+
let fields = {};
19+
let files = {};
20+
bb.on('field', (fieldname, val) => {
21+
fields[fieldname] = val;
22+
});
23+
bb.on('file', (fieldname, file, filename) => {
24+
var buffers = [];
25+
file.on('data', (data) => {
26+
buffers.push(data);
27+
});
28+
file.on('end', () => {
29+
files[fieldname] = [Buffer.concat(buffers), filename];
30+
});
31+
});
32+
bb.on('finish', () => {
33+
let f = files[Object.keys(files)[0]];
34+
const wb = XLSX.read(f[0], { type: "buffer" });
35+
// Convert to CSV
36+
res.send(XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]));
37+
});
38+
bb.end(req.body)
39+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"scripts": {
5+
"serve": "firebase emulators:start --only functions",
6+
"shell": "firebase functions:shell",
7+
"start": "npm run shell",
8+
"deploy": "firebase deploy --only functions",
9+
"logs": "firebase functions:log"
10+
},
11+
"engines": {
12+
"node": "8"
13+
},
14+
"dependencies": {
15+
"busboy": "^0.3.1",
16+
"firebase-admin": "^8.6.0",
17+
"firebase-functions": "^3.3.0",
18+
"xlsx": "^0.16.2"
19+
},
20+
"devDependencies": {
21+
"firebase-functions-test": "^0.1.6"
22+
},
23+
"private": true
24+
}

demos/function/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ aws: lambda-proxy
99
lambda-proxy:
1010
cd LambdaProxy; mkdir -p node_modules; npm install xlsx busboy; sam local start-api; cd -
1111

12+
.PHONY: init-azure
13+
init-azure:
14+
cd AzureHTTPTrigger; mkdir -p node_modules; npm install xlsx formidable fs
15+
1216
.PHONY: azure
13-
azure:
17+
azure: init-azure
1418
func start
1519

1620
.PHONY: azure-server

demos/function/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,14 @@ HTTP trigger that converts the submitted file to CSV.
121121

122122
When deploying on Azure, be sure to install the module from the remote console,
123123
as described in the "Azure Functions JavaScript developer guide".
124+
125+
#### Firebase Functions
126+
127+
Firebase functions can be triggered via HTTP requests, similar to a REST API.
128+
In the `Firebase` directory, the example function reads files sent through
129+
HTTP and converts it to a CSV and sends the response in the form of a string.
130+
131+
To run this demo locally, run `npm i -g firebase-tools` to install the
132+
Firebase CLI and `npm i` to install the dependencies, then `firebase use --add`
133+
to connect to an existing Firebase project. Run `firebase emulators:start` to
134+
start the local server.

demos/function/host.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{ }
1+
{
2+
"version": "2.0"
3+
}

0 commit comments

Comments
 (0)