@iopipe/profiler
Advanced tools
Comparing version 2.0.2 to 2.0.3
'use strict'; | ||
var _util = require('util'); | ||
var _inspector = require('inspector'); | ||
@@ -7,6 +9,2 @@ | ||
var _url = require('url'); | ||
var urlLib = _interopRequireWildcard(_url); | ||
var _lodash = require('lodash.get'); | ||
@@ -20,5 +18,5 @@ | ||
var _request = require('./request'); | ||
var _core = require('@iopipe/core'); | ||
var _request2 = _interopRequireDefault(_request); | ||
var _simpleGet = require('simple-get'); | ||
@@ -29,6 +27,2 @@ var _enabled = require('./enabled'); | ||
var _signer = require('./signer'); | ||
var _signer2 = _interopRequireDefault(_signer); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -40,2 +34,4 @@ | ||
const request = (0, _util.promisify)(_simpleGet.concat); | ||
const pkg = require('../package.json'); | ||
@@ -102,3 +98,4 @@ | ||
const promises = []; | ||
// kick off the S3 signer request early as we have the info necessary | ||
const promises = [this.getFileUploadMeta()]; | ||
@@ -117,2 +114,3 @@ try { | ||
} | ||
this.pluginReadyPromise = Promise.all(promises); | ||
@@ -124,37 +122,32 @@ } catch (err) { | ||
getSignedUrl(obj = this.invocationInstance) { | ||
var _this = this; | ||
getFileUploadMeta() { | ||
// returns a promise here | ||
const { | ||
invokedFunctionArn: arn, | ||
awsRequestId: requestId | ||
} = this.invocationInstance.context; | ||
return _asyncToGenerator(function* () { | ||
const { startTimestamp, context = {} } = obj; | ||
const hostname = (0, _signer2.default)(); | ||
_this.log(`Requesting signed url from ${hostname}`); | ||
const signingRes = yield (0, _request2.default)(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; | ||
})(); | ||
return _core.util.getFileUploadMeta({ | ||
auth: this.token, | ||
arn, | ||
requestId | ||
}); | ||
} | ||
postInvoke() { | ||
var _this2 = this; | ||
var _this = this; | ||
return _asyncToGenerator(function* () { | ||
if (!_this2.enabled) return false; | ||
if (!_this.enabled) return false; | ||
try { | ||
yield _this2.pluginReadyPromise; | ||
const [fileUploadMeta = {}] = yield _this.pluginReadyPromise; | ||
if (fileUploadMeta.jwtAccess) { | ||
_this.uploads.push(fileUploadMeta.jwtAccess); | ||
_this.signedRequestUrl = fileUploadMeta.signedRequest; | ||
} else { | ||
return _this.log(`S3 signer service error. Response: ${JSON.stringify(fileUploadMeta)}`); | ||
} | ||
} catch (err) { | ||
_this2.log(err); | ||
_this.log(err); | ||
// if there is an error setting things up, bail early | ||
@@ -164,71 +157,68 @@ return false; | ||
return new Promise((() => { | ||
var _ref = _asyncToGenerator(function* (resolve) { | ||
try { | ||
const signedRequestURL = yield _this2.getSignedUrl(); | ||
const archive = archiver.default('zip'); | ||
return new Promise(function (resolve) { | ||
try { | ||
const archive = archiver.default('zip'); | ||
/* NodeJS's Buffer has a fixed-size heap allocation. | ||
Here an Array, which has dynamic allocation, | ||
is used to buffer (hold) data received from a stream | ||
then used to construct a Buffer via Buffer.concat(Array), | ||
a constructor of Buffer. */ | ||
const archiveBuffer = []; | ||
const heapSnapshotBufferArr = []; | ||
/* NodeJS's Buffer has a fixed-size heap allocation. | ||
Here an Array, which has dynamic allocation, | ||
is used to buffer (hold) data received from a stream | ||
then used to construct a Buffer via Buffer.concat(Array), | ||
a constructor of Buffer. */ | ||
const archiveBuffer = []; | ||
const heapSnapshotBufferArr = []; | ||
archive.on('data', function (chunk) { | ||
return archiveBuffer.push(chunk); | ||
archive.on('data', function (chunk) { | ||
return archiveBuffer.push(chunk); | ||
}); | ||
archive.on('finish', _asyncToGenerator(function* () { | ||
/* Here uploads to S3 are incompatible with streams. | ||
Chunked Encoding is not supported for uploads | ||
to a pre-signed url. */ | ||
yield request({ | ||
url: _this.signedRequestUrl, | ||
method: 'PUT', | ||
body: Buffer.concat(archiveBuffer) | ||
}); | ||
archive.on('finish', _asyncToGenerator(function* () { | ||
/* Here uploads to S3 are incompatible with streams. | ||
Chunked Encoding is not supported for uploads | ||
to a pre-signed url. */ | ||
yield (0, _request2.default)(Buffer.concat(archiveBuffer), 'PUT', urlLib.parse(signedRequestURL)); | ||
resolve(); | ||
})); | ||
resolve(); | ||
})); | ||
// Generate 1 or 2 files total depending on options | ||
// We will use this number to know when we should finish up all the work | ||
const totalWantedFiles = [_this2.profilerEnabled, _this2.heapEnabled].filter(Boolean).length; | ||
// Generate 1 or 2 files total depending on options | ||
// We will use this number to know when we should finish up all the work | ||
const totalWantedFiles = [_this.profilerEnabled, _this.heapEnabled].filter(Boolean).length; | ||
let filesSeen = 0; | ||
archive.on('entry', function () { | ||
filesSeen++; | ||
if (filesSeen === totalWantedFiles) { | ||
if (typeof _this2.invocationInstance.context.iopipe.label === 'function') { | ||
_this2.invocationInstance.context.iopipe.label('@iopipe/plugin-profiler'); | ||
} | ||
archive.finalize(); | ||
let filesSeen = 0; | ||
archive.on('entry', function () { | ||
filesSeen++; | ||
if (filesSeen === totalWantedFiles) { | ||
if (typeof _this.invocationInstance.context.iopipe.label === 'function') { | ||
_this.invocationInstance.context.iopipe.label('@iopipe/plugin-profiler'); | ||
} | ||
}); | ||
if (_this2.profilerEnabled) { | ||
_this2.sessionPost('Profiler.stop').then(function ({ profile }) { | ||
archive.append(JSON.stringify(profile), { | ||
name: 'profile.cpuprofile' | ||
}); | ||
}); | ||
archive.finalize(); | ||
} | ||
}); | ||
if (_this2.heapEnabled) { | ||
_this2.session.on('HeapProfiler.addHeapSnapshotChunk', function (obj) { | ||
return heapSnapshotBufferArr.push(obj && obj.params && obj.params.chunk && Buffer.from(obj.params.chunk)); | ||
if (_this.profilerEnabled) { | ||
_this.sessionPost('Profiler.stop').then(function ({ profile }) { | ||
archive.append(JSON.stringify(profile), { | ||
name: 'profile.cpuprofile' | ||
}); | ||
}); | ||
} | ||
_this2.sessionPost('HeapProfiler.takeHeapSnapshot').then(function () { | ||
archive.append(Buffer.concat(heapSnapshotBufferArr.filter(Boolean)), { | ||
name: 'profile.heapsnapshot' | ||
}); | ||
if (_this.heapEnabled) { | ||
_this.session.on('HeapProfiler.addHeapSnapshotChunk', function (obj) { | ||
return heapSnapshotBufferArr.push(obj && obj.params && obj.params.chunk && Buffer.from(obj.params.chunk)); | ||
}); | ||
_this.sessionPost('HeapProfiler.takeHeapSnapshot').then(function () { | ||
archive.append(Buffer.concat(heapSnapshotBufferArr.filter(Boolean)), { | ||
name: 'profile.heapsnapshot' | ||
}); | ||
} | ||
} catch (e) { | ||
_this2.log(`Error in upload: ${e}`); | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
return function () { | ||
return _ref.apply(this, arguments); | ||
}; | ||
})()); | ||
} catch (e) { | ||
_this.log(`Error in upload: ${e}`); | ||
resolve(); | ||
} | ||
}); | ||
})(); | ||
@@ -235,0 +225,0 @@ } |
{ | ||
"name": "@iopipe/profiler", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "IOpipe plugin for running the v8 profiler on your functions", | ||
@@ -37,15 +37,15 @@ "main": "dist/index.js", | ||
"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" | ||
"lodash.get": "^4.4.2", | ||
"simple-get": "^3.0.2" | ||
}, | ||
"peerDependencies": { | ||
"@iopipe/core": "^1.5.0" | ||
}, | ||
"peerDependencies": {}, | ||
"jest": { | ||
@@ -52,0 +52,0 @@ "coveragePathIgnorePatterns": [ |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
22616
4
5
206
+ Added@iopipe/core@^1.13
+ Addedsimple-get@^3.0.2