[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
This commit is contained in:
Garrett Luu 2020-06-25 12:30:56 -07:00 committed by GitHub
parent d0457b77c8
commit 0c7e809e9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 151 additions and 5 deletions

@ -26,9 +26,8 @@ module.exports = (context, req) => {
if(!f) {
context.res = { status: 400, body: "Must submit a file for processing!" };
} else {
/* since the file is Base64-encoded, read the file and parse as "base64" */
const b64 = fs.readFileSync(f.path).toString();
const wb = XLSX.read(b64, {type:"base64"});
/* file is stored in a temp directory, so we can point to that and read it */
const wb = XLSX.read(f.path, {type:"file"});
/* convert to specified output type -- default CSV */
const ext = (fields.bookType || "csv").toLowerCase();

65
demos/function/Firebase/.gitignore vendored Normal file

@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*
# Firebase cache
.firebase/
# Firebase config
# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
.firebaserc
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env

@ -0,0 +1 @@
{}

@ -0,0 +1 @@
node_modules/

@ -0,0 +1,39 @@
const functions = require('firebase-functions');
const Busboy = require('busboy');
const XLSX = require('xlsx');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
exports.main = functions.https.onRequest((req, res) => {
var bb = new Busboy({
headers: {
'content-type': req.headers['content-type']
}
});
let fields = {};
let files = {};
bb.on('field', (fieldname, val) => {
fields[fieldname] = val;
});
bb.on('file', (fieldname, file, filename) => {
var buffers = [];
file.on('data', (data) => {
buffers.push(data);
});
file.on('end', () => {
files[fieldname] = [Buffer.concat(buffers), filename];
});
});
bb.on('finish', () => {
let f = files[Object.keys(files)[0]];
const wb = XLSX.read(f[0], { type: "buffer" });
// Convert to CSV
res.send(XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]));
});
bb.end(req.body)
});

@ -0,0 +1,24 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"busboy": "^0.3.1",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.3.0",
"xlsx": "^0.16.2"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}

@ -9,8 +9,12 @@ aws: lambda-proxy
lambda-proxy:
cd LambdaProxy; mkdir -p node_modules; npm install xlsx busboy; sam local start-api; cd -
.PHONY: init-azure
init-azure:
cd AzureHTTPTrigger; mkdir -p node_modules; npm install xlsx formidable fs
.PHONY: azure
azure:
azure: init-azure
func start
.PHONY: azure-server

@ -121,3 +121,14 @@ HTTP trigger that converts the submitted file to CSV.
When deploying on Azure, be sure to install the module from the remote console,
as described in the "Azure Functions JavaScript developer guide".
#### Firebase Functions
Firebase functions can be triggered via HTTP requests, similar to a REST API.
In the `Firebase` directory, the example function reads files sent through
HTTP and converts it to a CSV and sends the response in the form of a string.
To run this demo locally, run `npm i -g firebase-tools` to install the
Firebase CLI and `npm i` to install the dependencies, then `firebase use --add`
to connect to an existing Firebase project. Run `firebase emulators:start` to
start the local server.

@ -1 +1,3 @@
{ }
{
"version": "2.0"
}