Add `debuglogs` module

This commit is contained in:
Daniel Gasienica 2018-03-06 18:56:01 -05:00
parent 04afb6a318
commit 52f7de6a10
2 changed files with 65 additions and 0 deletions

48
js/modules/debuglogs.js Normal file
View File

@ -0,0 +1,48 @@
/* eslint-env node */
const FormData = require('form-data');
const got = require('got');
const BASE_URL = 'https://debuglogs.org';
// Workaround: Submitting `FormData` using native `FormData::submit` procedure
// as integration with `got` results in S3 error saying we havent set the
// `Content-Length` header:
const submitFormData = (form, url) =>
new Promise((resolve, reject) => {
form.submit(url, (error) => {
if (error) {
return reject(error);
}
return resolve();
});
});
// upload :: String -> Promise URL
exports.upload = async (content) => {
const signedForm = await got.get(BASE_URL, { json: true });
const { fields, url } = signedForm.body;
const form = new FormData();
form.append('key', fields.key);
Object.entries(fields)
.filter(([key]) => key !== 'key')
.forEach(([key, value]) => {
form.append(key, value);
});
const contentBuffer = Buffer.from(content, 'utf8');
const contentType = 'text/plain';
form.append('Content-Type', contentType);
form.append('file', contentBuffer, {
contentType,
filename: 'signal-desktop-debug-log.txt',
});
await submitFormData(form, url);
return `${BASE_URL}/${fields.key}`;
};

View File

@ -0,0 +1,17 @@
const { assert } = require('chai');
const got = require('got');
const debuglogs = require('../../js/modules/debuglogs');
describe('debuglogs', () => {
describe('upload', () => {
it('should upload log content', async () => {
const nonce = Math.random().toString().slice(2);
const url = await debuglogs.upload(nonce);
const { body } = await got.get(url);
assert.equal(nonce, body);
});
});
});