Skip to content

Commit

Permalink
fix(upload request): start s3 signer request earlier in pre:invoke
Browse files Browse the repository at this point in the history
- refactor(s3 signer util): use util from core

fix #57
  • Loading branch information
coreylight authored Jul 18, 2018
1 parent 712ffd6 commit f59c22d
Show file tree
Hide file tree
Showing 8 changed files with 1,284 additions and 1,099 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
"node": ">=8.10.0"
},
"devDependencies": {
"@iopipe/core": "1.11.0",
"@iopipe/scripts": "^1.4.1",
"aws-lambda-mock-context": "^3.1.1",
"lodash": "^4.17.4",
"nock": "^9.4.1",
"pre-commit": "^1.2.2"
},
"dependencies": {
"@iopipe/core": "^1.13",
"archiver": "^2.1.1",
"lodash.get": "^4.4.2"
},
"peerDependencies": {
"@iopipe/core": "^1.5.0"
"lodash.get": "^4.4.2",
"simple-get": "^3.0.2"
},
"peerDependencies": {},
"jest": {
"coveragePathIgnorePatterns": [
"/node_modules/",
Expand Down
17 changes: 0 additions & 17 deletions src/__mocks__/request.js

This file was deleted.

62 changes: 26 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { promisify } from 'util';
import * as inspector from 'inspector';
import * as urlLib from 'url';
import get from 'lodash.get';
import * as archiver from 'archiver';
import { util as coreUtil } from '@iopipe/core';
import { concat } from 'simple-get';

import request from './request';
import enabled from './enabled';
import getSignerHostname from './signer';

const request = promisify(concat);

const pkg = require('../package.json');

Expand Down Expand Up @@ -78,7 +80,8 @@ class ProfilerPlugin {
return;
}

const promises = [];
// kick off the S3 signer request early as we have the info necessary
const promises = [this.getFileUploadMeta()];

try {
this.session.connect();
Expand All @@ -96,52 +99,39 @@ class ProfilerPlugin {
this.sessionPost('Profiler.start')
]);
}

this.pluginReadyPromise = Promise.all(promises);
} catch (err) {
this.log(err);
}
}

async getSignedUrl(obj = this.invocationInstance) {
const { startTimestamp, context = {} } = obj;
const hostname = getSignerHostname();
this.log(`Requesting signed url from ${hostname}`);
const signingRes = await request(
JSON.stringify({
arn: context.invokedFunctionArn,
requestId: context.awsRequestId,
timestamp: startTimestamp,
extension: '.zip'
}),
'POST',
{
hostname,
path: '/'
},
this.token
);

// Parse response to get signed url
const response = JSON.parse(signingRes);
// attach uploads to plugin data
this.uploads.push(response.jwtAccess);
return response.signedRequest;
getFileUploadMeta() {
// returns a promise here
return coreUtil.getFileUploadMeta({
auth: this.token
});
}

async postInvoke() {
if (!this.enabled) return false;

try {
await this.pluginReadyPromise;
const [fileUploadMeta = {}] = await this.pluginReadyPromise;
if (fileUploadMeta.jwtAccess) {
this.uploads.push(fileUploadMeta.jwtAccess);
this.signedRequestUrl = fileUploadMeta.signedRequest;
} else {
return this.log(`S3 signer service error. Response: ${fileUploadMeta}`);
}
} catch (err) {
this.log(err);
// if there is an error setting things up, bail early
return false;
}

return new Promise(async resolve => {
return new Promise(resolve => {
try {
const signedRequestURL = await this.getSignedUrl();
const archive = archiver.default('zip');

/* NodeJS's Buffer has a fixed-size heap allocation.
Expand All @@ -158,11 +148,11 @@ class ProfilerPlugin {
/* Here uploads to S3 are incompatible with streams.
Chunked Encoding is not supported for uploads
to a pre-signed url. */
await request(
Buffer.concat(archiveBuffer),
'PUT',
urlLib.parse(signedRequestURL)
);
await request({
url: this.signedRequestUrl,
method: 'PUT',
body: Buffer.concat(archiveBuffer).toString()
});
resolve();
});

Expand Down
22 changes: 17 additions & 5 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import mockContext from 'aws-lambda-mock-context';
import _ from 'lodash';
import iopipe from '@iopipe/core';
import nock from 'nock';

import pkg from '../package';
import { putData } from './request';

const profiler = require('./index');

jest.mock('./request');

process.env.IOPIPE_TOKEN = 'test';

const putData = [];

beforeEach(() => {
// reset mock data holder for each test
putData.length = 0;

// intercept http calls
nock(/signer/)
.post('/')
.reply(200, { jwtAccess: '1234', signedRequest: 'https://aws.com' });

nock(/aws\.com/)
.put('/', body => {
putData.push(body);
return body;
})
.reply(200);
});

test('Can instantiate plugin without options', () => {
Expand Down Expand Up @@ -55,9 +67,9 @@ async function runFn(opts, fn = (e, ctx) => ctx.succeed('pass')) {
plugins: [profiler(opts), inv => (inspectableInv = inv)]
})(fn)({}, context);
const val = await context.Promise;
expect(inspectableInv.report.report.labels).toEqual([
expect(inspectableInv.report.report.labels).toContain(
'@iopipe/plugin-profiler'
]);
);
return val;
}

Expand Down
49 changes: 0 additions & 49 deletions src/request.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/signer.js

This file was deleted.

50 changes: 0 additions & 50 deletions src/signer.test.js

This file was deleted.

Loading

0 comments on commit f59c22d

Please sign in to comment.