Skip to content

Commit

Permalink
fix(): when using s3, parse stream output to buffer for html files (#117
Browse files Browse the repository at this point in the history
)

* fix(): when using s3, parse stream output to buffer for html files
increase token timeout to 13hrs
map yidf/state to txt extension

* fix: feedback fix
  • Loading branch information
jithine authored May 20, 2021
1 parent 7f5fda0 commit fe41985
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions helpers/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -31,6 +32,10 @@ class AwsClient {
}
};

if (config.sessionToken) {
options.sessionToken = config.sessionToken;
}

if (config.endpoint) {
options.endpoint = config.endpoint;
}
Expand Down
8 changes: 7 additions & 1 deletion helpers/mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion plugins/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion plugins/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ 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) {
try {
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;
Expand Down Expand Up @@ -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 = `<script>${iframeScript}</script>`;

// inject postMessage into code
Expand Down
8 changes: 8 additions & 0 deletions test/plugins/builds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sinon.assert.expose(assert, { prefix: '' });
describe('builds plugin test', () => {
let plugin;
let server;
let configMock;

before(() => {
mockery.enable({
Expand All @@ -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');

Expand Down

0 comments on commit fe41985

Please sign in to comment.