Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created January 17, 2019 02:51
Show Gist options
  • Save lastday154/8c0b39560e22862ccd56cc3f10ba569a to your computer and use it in GitHub Desktop.
Save lastday154/8c0b39560e22862ccd56cc3f10ba569a to your computer and use it in GitHub Desktop.
paser csv
'use strict';
const Busboy = require('busboy');
const getContentType = (event) => {
const contentType = event.headers['content-type'];
if (!contentType) {
return event.headers['Content-Type'];
}
return contentType;
};
const parse = (event) => new Promise((resolve, reject) => {
const busboy = new Busboy({
headers: {
'content-type': getContentType(event),
}
});
const result = {};
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
file.on('data', (data) => {
result.file = data;
});
file.on('end', () => {
result.filename = filename;
result.contentType = mimetype;
});
});
busboy.on('field', (fieldname, value) => {
result[fieldname] = value;
});
busboy.on('error', (error) => reject(`Parse error: ${error}`));
busboy.on('finish', () => {
event.body = result;
resolve(event);
});
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
});
module.exports = {
parse
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment