Skip to content

Commit 501066c

Browse files
author
Andre Rabold
committed
Changed code generation to ES5 with commonjs for better compatibility with legacy code
1 parent 8934e71 commit 501066c

File tree

2 files changed

+82
-61
lines changed

2 files changed

+82
-61
lines changed

dist/index.js

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
let isSentryInstalled = false;
1+
"use strict";
2+
var __assign = (this && this.__assign) || function () {
3+
__assign = Object.assign || function(t) {
4+
for (var s, i = 1, n = arguments.length; i < n; i++) {
5+
s = arguments[i];
6+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7+
t[p] = s[p];
8+
}
9+
return t;
10+
};
11+
return __assign.apply(this, arguments);
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
var isSentryInstalled = false;
215
function installSentry(pluginConfig) {
316
var _a, _b, _c;
4-
const sentryClient = pluginConfig.sentryClient;
17+
var sentryClient = pluginConfig.sentryClient;
518
if (!sentryClient) {
619
console.error("Sentry client not found.");
720
}
8-
const isLocalEnv = process.env.IS_OFFLINE || process.env.IS_LOCAL || !process.env.LAMBDA_TASK_ROOT;
21+
var isLocalEnv = process.env.IS_OFFLINE || process.env.IS_LOCAL || !process.env.LAMBDA_TASK_ROOT;
922
if (pluginConfig.filterLocal && isLocalEnv) {
1023
console.warn("Sentry disabled in local environment");
1124
delete process.env.SENTRY_DSN;
@@ -14,27 +27,27 @@ function installSentry(pluginConfig) {
1427
return;
1528
}
1629
if (pluginConfig.sourceMaps) {
17-
const RewriteFramesExists = Array.isArray((_a = pluginConfig.init) === null || _a === void 0 ? void 0 : _a.integrations) && ((_b = pluginConfig.init) === null || _b === void 0 ? void 0 : _b.integrations.find((integration) => integration.name === "RewriteFrames"));
30+
var RewriteFramesExists = Array.isArray((_a = pluginConfig.init) === null || _a === void 0 ? void 0 : _a.integrations) && ((_b = pluginConfig.init) === null || _b === void 0 ? void 0 : _b.integrations.find(function (integration) { return integration.name === "RewriteFrames"; }));
1831
if (!RewriteFramesExists) {
1932
pluginConfig.init = (_c = pluginConfig.init) !== null && _c !== void 0 ? _c : {};
2033
if (!Array.isArray(pluginConfig.init.integrations)) {
2134
pluginConfig.init.integrations = [];
2235
}
23-
const { RewriteFrames } = require("@sentry/integrations");
24-
const path = require("path");
36+
var RewriteFrames = require("@sentry/integrations").RewriteFrames;
37+
var path_1 = require("path");
2538
pluginConfig.init.integrations.push(new RewriteFrames({
26-
iteratee: (frame) => {
39+
iteratee: function (frame) {
2740
var _a;
2841
if (((_a = frame.filename) === null || _a === void 0 ? void 0 : _a.startsWith("/")) && !frame.filename.includes("/node_modules/")) {
29-
frame.filename = "app:///" + path.basename(frame.filename);
42+
frame.filename = "app:///" + path_1.basename(frame.filename);
3043
}
3144
return frame;
3245
},
3346
}));
3447
}
3548
}
36-
sentryClient.init(Object.assign({ dsn: process.env.SENTRY_DSN, release: process.env.SENTRY_RELEASE, environment: isLocalEnv ? "Local" : process.env.SENTRY_ENVIRONMENT }, pluginConfig.init));
37-
const tags = {
49+
sentryClient.init(__assign({ dsn: process.env.SENTRY_DSN, release: process.env.SENTRY_RELEASE, environment: isLocalEnv ? "Local" : process.env.SENTRY_ENVIRONMENT }, pluginConfig.init));
50+
var tags = {
3851
lambda: String(process.env.AWS_LAMBDA_FUNCTION_NAME),
3952
version: String(process.env.AWS_LAMBDA_FUNCTION_VERSION),
4053
memory_size: String(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE),
@@ -48,65 +61,65 @@ function installSentry(pluginConfig) {
4861
tags.stage = process.env.SERVERLESS_STAGE;
4962
if (process.env.SERVERLESS_ALIAS)
5063
tags.alias = process.env.SERVERLESS_ALIAS;
51-
sentryClient.configureScope((scope) => {
64+
sentryClient.configureScope(function (scope) {
5265
var _a;
53-
scope.setTags(Object.assign(Object.assign({}, tags), (_a = pluginConfig.scope) === null || _a === void 0 ? void 0 : _a.tags));
66+
scope.setTags(__assign(__assign({}, tags), (_a = pluginConfig.scope) === null || _a === void 0 ? void 0 : _a.tags));
5467
});
5568
isSentryInstalled = true;
5669
console.log("Sentry installed.");
5770
}
58-
let memoryWatch;
59-
let timeoutWarning;
60-
let timeoutError;
71+
var memoryWatch;
72+
var timeoutWarning;
73+
var timeoutError;
6174
function installTimers(pluginConfig, lambdaContext) {
62-
const timeRemaining = lambdaContext.getRemainingTimeInMillis();
63-
const memoryLimit = Number(lambdaContext.memoryLimitInMB);
75+
var timeRemaining = lambdaContext.getRemainingTimeInMillis();
76+
var memoryLimit = Number(lambdaContext.memoryLimitInMB);
6477
function timeoutWarningFunc(cb) {
65-
const Sentry = pluginConfig.sentryClient;
78+
var Sentry = pluginConfig.sentryClient;
6679
if (isSentryInstalled) {
67-
Sentry.withScope((scope) => {
80+
Sentry.withScope(function (scope) {
6881
scope.setLevel("warning");
6982
scope.setExtras({
7083
TimeRemainingInMsec: lambdaContext.getRemainingTimeInMillis(),
7184
});
7285
Sentry.captureMessage("Function Execution Time Warning");
7386
});
7487
Sentry.flush(5000)
75-
.then(() => cb === null || cb === void 0 ? void 0 : cb())
88+
.then(function () { return cb === null || cb === void 0 ? void 0 : cb(); })
7689
.catch(null);
7790
}
7891
}
7992
function timeoutErrorFunc(cb) {
80-
const Sentry = pluginConfig.sentryClient;
93+
var Sentry = pluginConfig.sentryClient;
8194
if (isSentryInstalled) {
82-
Sentry.withScope((scope) => {
95+
Sentry.withScope(function (scope) {
8396
scope.setLevel("error");
8497
scope.setExtras({
8598
TimeRemainingInMsec: lambdaContext.getRemainingTimeInMillis(),
8699
});
87100
Sentry.captureMessage("Function Timed Out");
88101
});
89102
Sentry.flush(5000)
90-
.then(() => cb === null || cb === void 0 ? void 0 : cb())
103+
.then(function () { return cb === null || cb === void 0 ? void 0 : cb(); })
91104
.catch(null);
92105
}
93106
}
94107
function memoryWatchFunc(cb) {
95-
const used = process.memoryUsage().rss / 1048576;
96-
const p = used / memoryLimit;
108+
var used = process.memoryUsage().rss / 1048576;
109+
var p = used / memoryLimit;
97110
if (p >= 0.75) {
98-
const Sentry = pluginConfig.sentryClient;
111+
var Sentry_1 = pluginConfig.sentryClient;
99112
if (isSentryInstalled) {
100-
Sentry.withScope((scope) => {
113+
Sentry_1.withScope(function (scope) {
101114
scope.setLevel("warning");
102115
scope.setExtras({
103116
MemoryLimitInMB: memoryLimit,
104117
MemoryUsedInMB: Math.floor(used),
105118
});
106-
Sentry.captureMessage("Low Memory Warning");
119+
Sentry_1.captureMessage("Low Memory Warning");
107120
});
108-
Sentry.flush(5000)
109-
.then(() => cb === null || cb === void 0 ? void 0 : cb())
121+
Sentry_1.flush(5000)
122+
.then(function () { return cb === null || cb === void 0 ? void 0 : cb(); })
110123
.catch(null);
111124
}
112125
}
@@ -137,13 +150,13 @@ function clearTimers() {
137150
}
138151
}
139152
function wrapCallback(pluginConfig, cb) {
140-
return (err, data) => {
153+
return function (err, data) {
141154
clearTimers();
142155
if (err && err !== "__emptyFailParamBackCompat" && pluginConfig.captureErrors && isSentryInstalled) {
143-
const Sentry = pluginConfig.sentryClient;
144-
Sentry.captureException(err);
145-
Sentry.flush(5000)
146-
.then(() => cb(err))
156+
var Sentry_2 = pluginConfig.sentryClient;
157+
Sentry_2.captureException(err);
158+
Sentry_2.flush(5000)
159+
.then(function () { return cb(err); })
147160
.catch(null);
148161
return;
149162
}
@@ -156,7 +169,7 @@ function wrapCallback(pluginConfig, cb) {
156169
};
157170
}
158171
function parseBoolean(value, defaultValue) {
159-
const v = String(value).trim().toLowerCase();
172+
var v = String(value).trim().toLowerCase();
160173
if (["true", "t", "1", "yes", "y"].includes(v)) {
161174
return true;
162175
}
@@ -170,19 +183,21 @@ function parseBoolean(value, defaultValue) {
170183
function isSentryInstance(value) {
171184
return typeof (value === null || value === void 0 ? void 0 : value.captureException) === "function" && typeof (value === null || value === void 0 ? void 0 : value.captureMessage) === "function";
172185
}
173-
export default class SentryLambdaWrapper {
174-
static handler(pluginConfigOrSentry, handler) {
186+
var SentryLambdaWrapper = (function () {
187+
function SentryLambdaWrapper() {
188+
}
189+
SentryLambdaWrapper.handler = function (pluginConfigOrSentry, handler) {
175190
var _a, _b, _c;
176-
let pluginConfig;
191+
var pluginConfig;
177192
if (isSentryInstance(pluginConfigOrSentry)) {
178193
pluginConfig = {
179194
sentryClient: pluginConfigOrSentry,
180195
};
181196
}
182197
else {
183-
pluginConfig = Object.assign(Object.assign({}, pluginConfigOrSentry), { init: Object.assign({}, pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.init), scope: Object.assign(Object.assign({}, pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope), { tags: Object.assign({}, (_a = pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope) === null || _a === void 0 ? void 0 : _a.tags), extras: Object.assign({}, (_b = pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope) === null || _b === void 0 ? void 0 : _b.extras), user: Object.assign({}, (_c = pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope) === null || _c === void 0 ? void 0 : _c.user) }) });
198+
pluginConfig = __assign(__assign({}, pluginConfigOrSentry), { init: __assign({}, pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.init), scope: __assign(__assign({}, pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope), { tags: __assign({}, (_a = pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope) === null || _a === void 0 ? void 0 : _a.tags), extras: __assign({}, (_b = pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope) === null || _b === void 0 ? void 0 : _b.extras), user: __assign({}, (_c = pluginConfigOrSentry === null || pluginConfigOrSentry === void 0 ? void 0 : pluginConfigOrSentry.scope) === null || _c === void 0 ? void 0 : _c.user) }) });
184199
}
185-
const pluginConfigDefaults = {
200+
var pluginConfigDefaults = {
186201
init: {},
187202
scope: { tags: {}, extras: {}, user: {} },
188203
captureErrors: parseBoolean(process.env.SENTRY_CAPTURE_ERRORS, true),
@@ -200,12 +215,12 @@ export default class SentryLambdaWrapper {
200215
if (process.env.SENTRY_DSN && !isSentryInstalled) {
201216
installSentry(pluginConfig);
202217
}
203-
return (event, context, callback) => {
218+
return function (event, context, callback) {
204219
var _a, _b, _c, _d;
205220
if (!isSentryInstalled) {
206221
return handler(event, context, callback);
207222
}
208-
const originalCallbacks = {
223+
var originalCallbacks = {
209224
done: (_a = context.done) === null || _a === void 0 ? void 0 : _a.bind(context),
210225
succeed: (_b = context.succeed) === null || _b === void 0 ? void 0 : _b.bind(context),
211226
fail: (_c = context.fail) === null || _c === void 0 ? void 0 : _c.bind(context),
@@ -221,19 +236,19 @@ export default class SentryLambdaWrapper {
221236
: originalCallbacks.fail;
222237
context.succeed =
223238
typeof originalCallbacks.succeed === "function"
224-
? wrapCallback(pluginConfig, (err, result) => originalCallbacks.succeed(result)).bind(null, null)
239+
? wrapCallback(pluginConfig, function (err, result) { return originalCallbacks.succeed(result); }).bind(null, null)
225240
: originalCallbacks.succeed;
226241
callback = originalCallbacks.callback
227242
? wrapCallback(pluginConfig, originalCallbacks.callback)
228243
: originalCallbacks.callback;
229-
const sentryScope = {
244+
var sentryScope = {
230245
extras: {
231246
Event: event,
232247
Context: context,
233248
},
234249
tags: {},
235250
};
236-
const identity = ((_d = context.identity) === null || _d === void 0 ? void 0 : _d.constructor) === Object && Object.keys(context.identity).length > 0
251+
var identity = ((_d = context.identity) === null || _d === void 0 ? void 0 : _d.constructor) === Object && Object.keys(context.identity).length > 0
237252
? context.identity
238253
: event.requestContext
239254
? event.requestContext.identity
@@ -249,41 +264,45 @@ export default class SentryLambdaWrapper {
249264
};
250265
}
251266
if (event.requestContext) {
252-
sentryScope.tags = Object.assign(Object.assign({}, sentryScope.tags), { api_id: event.requestContext.apiId, api_stage: event.requestContext.stage, http_method: event.requestContext.httpMethod });
267+
sentryScope.tags = __assign(__assign({}, sentryScope.tags), { api_id: event.requestContext.apiId, api_stage: event.requestContext.stage, http_method: event.requestContext.httpMethod });
253268
}
254-
const captureUnhandled = wrapCallback(pluginConfig, (err) => {
269+
var captureUnhandled = wrapCallback(pluginConfig, function (err) {
255270
err._sentryHandled = true;
256271
throw err;
257272
});
258-
const Sentry = pluginConfig.sentryClient;
259-
Sentry.configureScope((scope) => {
273+
var Sentry = pluginConfig.sentryClient;
274+
Sentry.configureScope(function (scope) {
260275
sentryScope.user && scope.setUser(sentryScope.user);
261276
sentryScope.extras && scope.setExtras(sentryScope.extras);
262277
sentryScope.tags && scope.setTags(sentryScope.tags);
263278
});
264279
installTimers(pluginConfig, context);
265280
try {
266281
if (pluginConfig.autoBreadcrumbs) {
267-
const breadcrumb = {
282+
var breadcrumb = {
268283
message: process.env.AWS_LAMBDA_FUNCTION_NAME,
269284
category: "lambda",
270285
level: "info",
271286
data: {},
272287
};
273288
if (event.requestContext) {
274-
breadcrumb.data = Object.assign(Object.assign({}, breadcrumb.data), { http_method: event.requestContext && event.requestContext.httpMethod, host: event.headers && event.headers.Host, path: event.path, user_agent: event.headers && event.headers["User-Agent"] });
289+
breadcrumb.data = __assign(__assign({}, breadcrumb.data), { http_method: event.requestContext && event.requestContext.httpMethod, host: event.headers && event.headers.Host, path: event.path, user_agent: event.headers && event.headers["User-Agent"] });
275290
}
276-
const sentryClient = pluginConfig.sentryClient;
291+
var sentryClient = pluginConfig.sentryClient;
277292
sentryClient.addBreadcrumb(breadcrumb);
278293
}
279-
const promise = handler(event, context, callback);
294+
var promise = handler(event, context, callback);
280295
if (promise && typeof promise.then === "function") {
281296
return promise
282-
.then((...data) => {
297+
.then(function () {
298+
var data = [];
299+
for (var _i = 0; _i < arguments.length; _i++) {
300+
data[_i] = arguments[_i];
301+
}
283302
clearTimers();
284-
return Promise.resolve(...data);
303+
return Promise.resolve.apply(Promise, data);
285304
})
286-
.catch((err) => {
305+
.catch(function (err) {
287306
clearTimers();
288307
return Promise.reject(err);
289308
});
@@ -294,5 +313,7 @@ export default class SentryLambdaWrapper {
294313
captureUnhandled(err);
295314
}
296315
};
297-
}
298-
}
316+
};
317+
return SentryLambdaWrapper;
318+
}());
319+
exports.default = SentryLambdaWrapper;

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"compilerOptions": {
33
"allowSyntheticDefaultImports": true,
44
"declaration": true,
5-
"lib": ["dom", "esnext"],
5+
"module": "commonjs",
66
"moduleResolution": "node",
77
"noImplicitAny": true,
88
"outDir": "./dist",
99
"removeComments": true,
1010
"resolveJsonModule": true,
1111
"skipLibCheck": true,
1212
"strictNullChecks": true,
13-
"target": "es6"
13+
"target": "es5"
1414
},
1515
"include": ["src/**/*"],
1616
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)