@contrast/agentify
Advanced tools
Comparing version 1.42.1 to 1.42.2
@@ -34,4 +34,2 @@ /* | ||
install() { | ||
if (!core.config.agent.node.rewrite.enable) return; | ||
// don't define this prior to install(), since it will interfere with other | ||
@@ -48,2 +46,6 @@ // components that also need to instrument it e.g. dep-hooks. | ||
Module.prototype._compile = function (content, filename) { | ||
if (!core.config.agent.node.rewrite.enable) { | ||
return Reflect.apply(_compile, this, [content, filename]); | ||
} | ||
/** @type {import('@contrast/rewriter').RewriteOpts} */ | ||
@@ -62,5 +64,5 @@ const options = { | ||
const result = core.rewriter.rewriteSync(content, options); | ||
try { | ||
const result = core.rewriter.rewriteSync(content, options); | ||
const compiled = Reflect.apply(_compile, this, [result.code, filename]); | ||
@@ -76,3 +78,3 @@ | ||
{ err }, | ||
'Failed to compile rewritten code for %s, compiling original code.', | ||
'Failed to rewrite code for %s, continuing with original code.', | ||
filename, | ||
@@ -79,0 +81,0 @@ ); |
@@ -15,3 +15,3 @@ // @ts-check | ||
describe('agentify rewrite-hooks', function () { | ||
let core, complileSpy, rewriteHooks; | ||
let core, compileSpy, rewriteHooks; | ||
@@ -24,5 +24,6 @@ beforeEach(function () { | ||
complileSpy = sinon.spy(Module.prototype, '_compile'); | ||
compileSpy = sinon.spy(Module.prototype, '_compile'); | ||
rewriteHooks = require('./rewrite-hooks')(core); | ||
rewriteHooks.install(); | ||
}); | ||
@@ -37,3 +38,2 @@ | ||
core.rewriter.cache.readSync.returns(undefined); | ||
rewriteHooks.install(); | ||
@@ -48,3 +48,3 @@ require(testFilePath); | ||
expect(core.logger.warn).not.to.have.been.called; | ||
expect(complileSpy).to.have.been.calledWith( | ||
expect(compileSpy).to.have.been.calledWith( | ||
core.rewriter.rewriteSync.getCall(0).returnValue.code, | ||
@@ -57,14 +57,13 @@ testFilePath, | ||
core.config.agent.node.rewrite.enable = false; | ||
rewriteHooks.install(); | ||
core.config.agent.node.rewrite.cache.enable = false; | ||
require(testFilePath); | ||
expect(core.rewriter.rewriteSync).not.to.have.been.called; | ||
expect(compileSpy).to.have.been.calledWith(testFileContent, testFilePath); | ||
}); | ||
it('does not not rewrite code when the cache returns results', function () { | ||
rewriteHooks.install(); | ||
require(testFilePath); | ||
expect(core.rewriter.rewriteSync).not.to.have.been.called; | ||
expect(complileSpy).to.have.been.calledWith( | ||
expect(compileSpy).to.have.been.calledWith( | ||
core.rewriter.cache.readSync.getCall(0).returnValue, | ||
@@ -75,5 +74,3 @@ testFilePath, | ||
it('logs a debug message when a file is rewrite-deadzoned', function() { | ||
rewriteHooks.install(); | ||
it('logs a debug message when a file is rewrite-deadzoned', function () { | ||
require(deadzonedFilePath); | ||
@@ -93,4 +90,2 @@ | ||
rewriteHooks.install(); | ||
require(testFilePath); | ||
@@ -109,6 +104,6 @@ expect(core.rewriter.rewriteSync).to.have.been.calledWith(testFileContent, { | ||
}, | ||
'Failed to compile rewritten code for %s, compiling original code.', | ||
'Failed to rewrite code for %s, continuing with original code.', | ||
testFilePath, | ||
); | ||
expect(complileSpy).to.have.been.calledWith( | ||
expect(compileSpy).to.have.been.calledWith( | ||
testFileContent, | ||
@@ -124,4 +119,2 @@ testFilePath, | ||
rewriteHooks.install(); | ||
expect(() => require(testFilePath)).to.throw('Unexpected identifier'); | ||
@@ -140,6 +133,6 @@ expect(core.rewriter.rewriteSync).to.have.been.calledWith(testFileContent, { | ||
}, | ||
'Failed to compile rewritten code for %s, compiling original code.', | ||
'Failed to rewrite code for %s, continuing with original code.', | ||
testFilePath, | ||
); | ||
expect(complileSpy).to.have.been.calledWith( | ||
expect(compileSpy).to.have.been.calledWith( | ||
testFileContent, | ||
@@ -146,0 +139,0 @@ testFilePath, |
@@ -18,2 +18,3 @@ /* | ||
const { platform, arch } = require('os'); | ||
const path = require('path'); | ||
@@ -40,2 +41,3 @@ const process = require('process'); | ||
assertSupportedPreloadUsage(); | ||
assertEnvironmentMatchesBuild(); | ||
} | ||
@@ -63,2 +65,21 @@ | ||
/** | ||
* Checks that some of our precompiled dependencies are included and callable. | ||
* This should ensure that we're running in the same environment that we were | ||
* installed in. | ||
* @throws {Error} | ||
*/ | ||
function assertEnvironmentMatchesBuild() { | ||
try { | ||
/* eslint-disable node/no-extraneous-require */ | ||
// since swc is a codependency of the agent it should always be present but | ||
// doesn't necessarily need to be included in this module's dependencies. | ||
require('@swc/core').parseSync(''); | ||
} catch (cause) { | ||
throw (cause.message === 'Bindings not found.') ? | ||
new Error(`Contrast cannot detect the correct precompiled dependencies for the current environment: ${platform()}-${arch()}. This typically occurs when deploying an installation from one environment to a different execution environment.`, { cause }) | ||
: cause; | ||
} | ||
} | ||
/** | ||
* Checks that no experimental feature flags are used. | ||
@@ -175,3 +196,4 @@ * @throws {Error} | ||
assertNoExperimentalFeatureFlags, | ||
assertEnvironmentMatchesBuild, | ||
preStartupValidation, | ||
}; |
'use strict'; | ||
const { expect } = require('chai'); | ||
const proxyquire = require('proxyquire'); | ||
const sinon = require('sinon'); | ||
@@ -13,6 +14,6 @@ | ||
describe('preStartupValidation', function() { | ||
describe('preStartupValidation', function () { | ||
describe('assertValidOpts', function() { | ||
it('Does not validate when noValidate is true', function() { | ||
describe('assertValidOpts', function () { | ||
it('Does not validate when noValidate is true', function () { | ||
const opts = { | ||
@@ -24,3 +25,3 @@ noValidate: true | ||
it('Throws an error if install order is incorrect', function() { | ||
it('Throws an error if install order is incorrect', function () { | ||
const opts = { | ||
@@ -37,10 +38,10 @@ installOrder: [ | ||
describe('assertSupportedNodeVersion', function() { | ||
describe('assertSupportedNodeVersion', function () { | ||
let engines; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
engines = '>=16.9.1 <17 || >=18.7.0 <19 || >=20.6.0 <21 || >= 22.5.1 <23'; | ||
}); | ||
it('Does not throw an error if process.version is supported', function() { | ||
it('Does not throw an error if process.version is supported', function () { | ||
sinon.stub(process, 'version').value('v22.9.0'); | ||
@@ -50,3 +51,3 @@ expect(assertSupportedNodeVersion(engines)).to.be.undefined; | ||
it('Throws an error if process.version is unsupported', function() { | ||
it('Throws an error if process.version is unsupported', function () { | ||
sinon.stub(process, 'version').value('v14.0.0'); | ||
@@ -58,5 +59,5 @@ expect(() => assertSupportedNodeVersion(engines)).to.throw('Contrast only officially supports Node LTS versions between 16.9.1 and 17, 18.7.0 and 19, 20.6.0 and 21, 22.5.1 and 23, but detected v14.0.0'); | ||
describe('assertSupportedPreloadUsage', function() { | ||
describe('assertSupportedPreloadUsage', function () { | ||
it('does not throw an err if --import flag is used (>= 18.19.0)', function() { | ||
it('does not throw an err if --import flag is used (>= 18.19.0)', function () { | ||
sinon.stub(process, 'execArgv').value(['--import', '@contrast/agent']); | ||
@@ -67,3 +68,3 @@ sinon.stub(process, 'version').value('v22.9.0'); | ||
it('throws an err if --import flag is used (<= 18.19.0)', function() { | ||
it('throws an err if --import flag is used (<= 18.19.0)', function () { | ||
sinon.stub(process, 'execArgv').value(['--import', '@contrast/agent']); | ||
@@ -74,3 +75,3 @@ sinon.stub(process, 'version').value('v18.18.0'); | ||
it('throws an err if --loader flag is used (>= 18.19.0)', function() { | ||
it('throws an err if --loader flag is used (>= 18.19.0)', function () { | ||
sinon.stub(process, 'execArgv').value(['--loader', '@contrast/agent']); | ||
@@ -83,5 +84,5 @@ sinon.stub(process, 'version').value('v18.20.0'); | ||
describe('assertNoExperimentalFeatureFlags', function() { | ||
describe('process.execArgv', function() { | ||
it('Does not throw an error if no experimental feature flag is used', function() { | ||
describe('assertNoExperimentalFeatureFlags', function () { | ||
describe('process.execArgv', function () { | ||
it('Does not throw an error if no experimental feature flag is used', function () { | ||
sinon.stub(process, 'execArgv').value(['--import', '@contrast/agent']); | ||
@@ -91,3 +92,3 @@ expect(assertNoExperimentalFeatureFlags()).to.be.undefined; | ||
it('Throws an error if an experimental feature flag is used', function() { | ||
it('Throws an error if an experimental feature flag is used', function () { | ||
sinon.stub(process, 'execArgv').value(['--experimental-feature', '--import', '@contrast/agent']); | ||
@@ -98,11 +99,11 @@ expect(() => assertNoExperimentalFeatureFlags()).to.throw('Contrast Agent does not support experimental features.'); | ||
describe('NODE_OPTIONS', function() { | ||
describe('NODE_OPTIONS', function () { | ||
const origNodeOptions = process.env?.NODE_OPTIONS; | ||
afterEach(function() { | ||
afterEach(function () { | ||
process.env.NODE_OPTIONS = origNodeOptions; | ||
}); | ||
it('Does not throw an error if no experimental feature flag is used', function() { | ||
it('Does not throw an error if no experimental feature flag is used', function () { | ||
process.env.NODE_OPTIONS = '--import @contrast/agent'; | ||
@@ -112,3 +113,3 @@ expect(assertNoExperimentalFeatureFlags()).to.be.undefined; | ||
it('Throws an error if an experimental feature flag is used', function() { | ||
it('Throws an error if an experimental feature flag is used', function () { | ||
process.env.NODE_OPTIONS = '--experimental-feature --import @contrast/agent'; | ||
@@ -121,2 +122,22 @@ expect(() => assertNoExperimentalFeatureFlags()).to.throw('Contrast Agent does not support experimental features.'); | ||
describe('assertEnvironmentMatchesBuild', function () { | ||
let swcMock, utils; | ||
beforeEach(function () { | ||
swcMock = { parseSync: sinon.stub() }; | ||
utils = proxyquire('./utils', { | ||
'@swc/core': swcMock, | ||
}); | ||
}); | ||
it('does not throw when dependencies are present and callable', function () { | ||
expect(() => utils.assertEnvironmentMatchesBuild()).not.to.throw(); | ||
}); | ||
it('throws when a dependency is missing or throws', function () { | ||
swcMock.parseSync.throws(new Error('Bindings not found.')); | ||
expect(() => utils.assertEnvironmentMatchesBuild()).to.throw(); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@contrast/agentify", | ||
"version": "1.42.1", | ||
"version": "1.42.2", | ||
"description": "Configures Contrast agent services and instrumentation within an application", | ||
@@ -25,3 +25,3 @@ "license": "SEE LICENSE IN LICENSE", | ||
"@contrast/dep-hooks": "1.14.1", | ||
"@contrast/esm-hooks": "2.19.1", | ||
"@contrast/esm-hooks": "2.19.2", | ||
"@contrast/find-package-json": "^1.1.0", | ||
@@ -34,3 +34,3 @@ "@contrast/instrumentation": "1.24.1", | ||
"@contrast/reporter": "1.41.1", | ||
"@contrast/rewriter": "1.21.1", | ||
"@contrast/rewriter": "1.21.2", | ||
"@contrast/scopes": "1.15.1", | ||
@@ -37,0 +37,0 @@ "on-finished": "^2.4.1", |
66008
1667
+ Added@contrast/esm-hooks@2.19.2(transitive)
+ Added@contrast/rewriter@1.21.2(transitive)
- Removed@contrast/esm-hooks@2.19.1(transitive)
- Removed@contrast/rewriter@1.21.1(transitive)
Updated@contrast/esm-hooks@2.19.2
Updated@contrast/rewriter@1.21.2