signal-desktop/js/modules/debuglogs.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-03-06 23:56:01 +00:00
/* 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:
// https://github.com/sindresorhus/got/pull/466
2018-03-06 23:56:01 +00:00
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();
2018-03-07 19:50:32 +00:00
// The API expects `key` to be the first field:
2018-03-06 23:56:01 +00:00
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',
});
// WORKAROUND: See comment on `submitFormData`:
// await got.post(url, { body: form });
2018-03-06 23:56:01 +00:00
await submitFormData(form, url);
return `${BASE_URL}/${fields.key}`;
};