diff --git a/bin/server b/bin/server index f8ce8b9..9071a88 100755 --- a/bin/server +++ b/bin/server @@ -47,7 +47,7 @@ async function init() { return logger.info('Server running at %s', server.info.uri); } catch (err) { - logger.error(err); + logger.error(err.toString()); return process.exit(1); } diff --git a/config/default.yaml b/config/default.yaml index ae5df1d..cb79764 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -62,6 +62,8 @@ strategy: accessKeyId: null # Amazon secret access key secretAccessKey: null + # Amazon Session Token from federated credentials + sessionToken: null # Amazon S3 region region: YOUR-REGION # Amazon S3 bucket that you have write access to diff --git a/helpers/aws.js b/helpers/aws.js index 7389148..3243ff7 100644 --- a/helpers/aws.js +++ b/helpers/aws.js @@ -19,6 +19,7 @@ class AwsClient { * @param {String} config.segment S3 segment * @param {Integer} config.partSize aws-sdk upload option * @param {Integer} config.httpTimeout HTTP timeout in ms + * @param {String } config.sessionToken Session token for federated AWS login */ constructor(config) { const options = { @@ -31,6 +32,10 @@ class AwsClient { } }; + if (config.sessionToken) { + options.sessionToken = config.sessionToken; + } + if (config.endpoint) { options.endpoint = config.endpoint; } diff --git a/helpers/mime.js b/helpers/mime.js index cbc9179..56196b3 100644 --- a/helpers/mime.js +++ b/helpers/mime.js @@ -2,13 +2,19 @@ const mime = require('mime-types'); +const FORCE_EXTENSION_MAPPING = { + yidf: 'txt', + state: 'txt', + diff: 'txt' +}; + /** * getMimeFromFileExtension * @param {String} fileExtension File extension (e.g. css, txt, html) * @return {String} text/html */ function getMimeFromFileExtension(fileExtension) { - return mime.lookup(fileExtension) || ''; + return mime.lookup(FORCE_EXTENSION_MAPPING[fileExtension] || fileExtension) || ''; } const knownMimes = ['text/css', 'text/javascript', 'image/png', 'image/jpeg', 'application/json', diff --git a/plugins/auth.js b/plugins/auth.js index 8281b34..ed4cf04 100644 --- a/plugins/auth.js +++ b/plugins/auth.js @@ -36,7 +36,7 @@ exports.plugin = { key: pluginOptions.jwtPublicKey, verifyOptions: { algorithms: ['RS256'], - maxAge: '12h' + maxAge: '13h' }, // This function is run once the Token has been decoded with signature validate diff --git a/plugins/builds.js b/plugins/builds.js index 8ad81b3..2ff963f 100644 --- a/plugins/builds.js +++ b/plugins/builds.js @@ -58,6 +58,7 @@ exports.plugin = { let value; let response; + let isStreamOutput = false; // for old json files, the value is hidden in an object, we cannot stream it directly if (usingS3) { @@ -65,6 +66,7 @@ exports.plugin = { value = await awsClient.getDownloadStream({ cacheKey: id }); response = h.response(value); response.headers['content-type'] = 'application/octet-stream'; + isStreamOutput = true; } catch (err) { request.log([id, 'error'], `Failed to stream the cache: ${err}`); throw err; @@ -104,7 +106,14 @@ exports.plugin = { `attachment; filename="${encodeURI(fileName)}"`; } else if (request.query.type === 'preview') { if (displayableMimes.includes(mime)) { - const $ = cheerio.load(Buffer.from(value)); + let htmlContent; + + if (isStreamOutput) { + htmlContent = await streamToBuffer(value); + } else { + htmlContent = Buffer.from(value); + } + const $ = cheerio.load(htmlContent); const scriptNode = ``; // inject postMessage into code diff --git a/test/plugins/builds.test.js b/test/plugins/builds.test.js index 72d735b..4353bc0 100644 --- a/test/plugins/builds.test.js +++ b/test/plugins/builds.test.js @@ -14,6 +14,7 @@ sinon.assert.expose(assert, { prefix: '' }); describe('builds plugin test', () => { let plugin; let server; + let configMock; before(() => { mockery.enable({ @@ -23,6 +24,13 @@ describe('builds plugin test', () => { }); beforeEach(() => { + configMock = { + get: sinon.stub().returns({ + plugin: 'memory', + s3: {} + }) + }; + mockery.registerMock('config', configMock); // eslint-disable-next-line global-require plugin = require('../../plugins/builds');