From 053e1ea7c37f92da3843e197e755379c2544087b Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 22 Feb 2021 13:14:11 +0100 Subject: [PATCH 01/26] build: Install @sentry/browser, @sentry/node and @sentry/types --- packages/nextjs/.eslintrc.js | 1 + packages/nextjs/package.json | 6 ++- yarn.lock | 78 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/.eslintrc.js b/packages/nextjs/.eslintrc.js index 6fd81c68b7af..7b4b6d87a032 100644 --- a/packages/nextjs/.eslintrc.js +++ b/packages/nextjs/.eslintrc.js @@ -27,5 +27,6 @@ module.exports = { ], rules: { 'max-lines': 'off', + '@sentry-internal/sdk/no-async-await': 'off', }, }; diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index c8cbbecc0b43..8cfb6f8e1a36 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -15,8 +15,12 @@ "publishConfig": { "access": "public" }, - "dependencies": {}, + "dependencies": { + "@sentry/browser": "^6.2.0", + "@sentry/node": "^6.2.0" + }, "devDependencies": { + "@sentry/types": "^6.2.0", "eslint": "^7.20.0", "rimraf": "^3.0.2" }, diff --git a/yarn.lock b/yarn.lock index 7c8771dc4672..cadd6f9e6b91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3276,6 +3276,84 @@ resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570" integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA= +"@sentry/browser@^6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.2.0.tgz#4113a92bc82f55e63f30cb16a94f717bd0b95817" + integrity sha512-4r3paHcHXLemj471BtNDhUs2kvJxk5XDRplz1dbC/LHXN5PWEXP4anhGILxOlxqi4y33r53PIZu3xXFjznaVZA== + dependencies: + "@sentry/core" "6.2.0" + "@sentry/types" "6.2.0" + "@sentry/utils" "6.2.0" + tslib "^1.9.3" + +"@sentry/core@6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.2.0.tgz#be1c33854fc94e1a4d867f7c2c311cf1cefb8ee9" + integrity sha512-oTr2b25l+0bv/+d6IgMamPuGleWV7OgJb0NFfd+WZhw6UDRgr7CdEJy2gW6tK8SerwXgPHdn4ervxsT3WIBiXw== + dependencies: + "@sentry/hub" "6.2.0" + "@sentry/minimal" "6.2.0" + "@sentry/types" "6.2.0" + "@sentry/utils" "6.2.0" + tslib "^1.9.3" + +"@sentry/hub@6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-6.2.0.tgz#e7502652bc9608cf8fb63e43cd49df9019a28f29" + integrity sha512-BDTEFK8vlJydWXp/KMX0stvv73V7od224iLi+w3k7BcPwMKXBuURBXPU8d5XIC4G8nwg8X6cnDvwL+zBBlBbkg== + dependencies: + "@sentry/types" "6.2.0" + "@sentry/utils" "6.2.0" + tslib "^1.9.3" + +"@sentry/minimal@6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.2.0.tgz#718b70babb55912eeb38babaf7823d1bcdd77d1e" + integrity sha512-haxsx8/ZafhZUaGeeMtY7bJt9HbDlqeiaXrRMp1CxGtd0ZRQwHt60imEjl6IH1I73SEWxNfqScGsX2s3HzztMg== + dependencies: + "@sentry/hub" "6.2.0" + "@sentry/types" "6.2.0" + tslib "^1.9.3" + +"@sentry/node@^6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/node/-/node-6.2.0.tgz#4c1860822a4a73d24242e5254124bd3bf9028d52" + integrity sha512-02lXk+56tPA3lWTvNLMGorp77wUVti8wOs+TlYARkJ+N+16dwqEBSBTy3hCDxlxriB+qHchSIS+ovPGi6WNiYA== + dependencies: + "@sentry/core" "6.2.0" + "@sentry/hub" "6.2.0" + "@sentry/tracing" "6.2.0" + "@sentry/types" "6.2.0" + "@sentry/utils" "6.2.0" + cookie "^0.4.1" + https-proxy-agent "^5.0.0" + lru_map "^0.3.3" + tslib "^1.9.3" + +"@sentry/tracing@6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/tracing/-/tracing-6.2.0.tgz#76c17e5dd3f1e61c8a4e3bd090f904a63d674765" + integrity sha512-pzgM1dePPJysVnzaFCMp+BKtjM5q46HZeyShiR+KcQYvneD3fmUPJigDkkcsB2DcrY3mFvDcswjoqxaTIW7ZBQ== + dependencies: + "@sentry/hub" "6.2.0" + "@sentry/minimal" "6.2.0" + "@sentry/types" "6.2.0" + "@sentry/utils" "6.2.0" + tslib "^1.9.3" + +"@sentry/types@6.2.0", "@sentry/types@^6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.2.0.tgz#ca020ff42913c6b9f88a9d0c375b5ee3965a2590" + integrity sha512-vN4P/a+QqAuVfWFB9G3nQ7d6bgnM9jd/RLVi49owMuqvM24pv5mTQHUk2Hk4S3k7ConrHFl69E7xH6Dv5VpQnQ== + +"@sentry/utils@6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.2.0.tgz#39c81ad5ba92cec54d690e3fa8ea4e777d8e9c2b" + integrity sha512-YToUC7xYf2E/pIluI7upYTlj8fKXOtdwoOBkcQZifHgX/dP+qDaHibbBFe5PyZwdmU2UiLnWFsBr0gjo0QFo1g== + dependencies: + "@sentry/types" "6.2.0" + tslib "^1.9.3" + "@simple-dom/interface@^1.4.0": version "1.4.0" resolved "https://registry.yarnpkg.com/@simple-dom/interface/-/interface-1.4.0.tgz#e8feea579232017f89b0138e2726facda6fbb71f" From 8357ff2dc04efa0a49cace18a9a032641628fc84 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 22 Feb 2021 13:17:23 +0100 Subject: [PATCH 02/26] feat: Add `flush` and `close` functionality --- packages/nextjs/src/common/NextjsClient.ts | 10 +++++ packages/nextjs/src/sdk.ts | 47 +++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 packages/nextjs/src/common/NextjsClient.ts diff --git a/packages/nextjs/src/common/NextjsClient.ts b/packages/nextjs/src/common/NextjsClient.ts new file mode 100644 index 000000000000..8e7ad7bbb822 --- /dev/null +++ b/packages/nextjs/src/common/NextjsClient.ts @@ -0,0 +1,10 @@ +import { ReportDialogOptions } from '@sentry/browser'; +import { Client } from '@sentry/types'; + +import { NextjsOptions } from './NextjsOptions'; + +/** Common interface for NextJS clients. */ +export interface NextjsClient extends Client { + /** Shows the report dialog. */ + showReportDialog(dialogOptions: ReportDialogOptions): void; +} diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index dede4d0a8897..ff90f8b91dc8 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,2 +1,45 @@ -// eslint-disable-next-line no-console -console.log('Sentaurs greet NextJS!'); +import { ReportDialogOptions } from '@sentry/browser'; +import { getCurrentHub } from '@sentry/node'; + +import { NextjsClient } from './common/NextjsClient'; + +/** + * TODO + */ +export function init(): void { + // TODO +} + +/** + * Present the user with a report dialog. + * + * @param dialogOptions Everything is optional, we try to fetch all the info we need from the global scope. + */ +export function showReportDialog(dialogOptions: ReportDialogOptions = {}): void { + const client = getCurrentHub().getClient(); + if (client) { + client.showReportDialog(dialogOptions); + } +} + +/** + * A promise that resolves when all current events have been sent. + * If you provide a timeout and the queue takes longer to drain the promise returns false. + * + * @param timeout Maximum time in ms the client should wait. + */ +export async function flush(timeout?: number): Promise { + const client = getCurrentHub().getClient(); + return client ? client.flush(timeout) : Promise.reject(false); +} + +/** + * A promise that resolves when all current events have been sent. + * If you provide a timeout and the queue takes longer to drain the promise returns false. + * + * @param timeout Maximum time in ms the client should wait. + */ +export async function close(timeout?: number): Promise { + const client = getCurrentHub().getClient(); + return client ? client.close(timeout) : Promise.reject(false); +} From 56e6056a1e49875466357f280cfc5651579f6763 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 22 Feb 2021 13:20:05 +0100 Subject: [PATCH 03/26] feat: Add NextJS options --- packages/nextjs/src/common/NextjsOptions.ts | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 packages/nextjs/src/common/NextjsOptions.ts diff --git a/packages/nextjs/src/common/NextjsOptions.ts b/packages/nextjs/src/common/NextjsOptions.ts new file mode 100644 index 000000000000..84943373602b --- /dev/null +++ b/packages/nextjs/src/common/NextjsOptions.ts @@ -0,0 +1,7 @@ +import { BrowserOptions } from '@sentry/browser'; +import { NodeOptions } from '@sentry/node'; +import { Options } from '@sentry/types'; + +export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { + // TODO: options for NextJS +} From a60448b48b3504a3e6aa0a63f2964313d16d17fd Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 22 Feb 2021 13:34:34 +0100 Subject: [PATCH 04/26] feat: Add init --- packages/nextjs/package.json | 3 ++- packages/nextjs/src/sdk.ts | 22 +++++++++++++++++--- yarn.lock | 39 +++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 8cfb6f8e1a36..883c2a241d6a 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -17,7 +17,8 @@ }, "dependencies": { "@sentry/browser": "^6.2.0", - "@sentry/node": "^6.2.0" + "@sentry/node": "^6.2.0", + "@sentry/utils": "^6.2.0" }, "devDependencies": { "@sentry/types": "^6.2.0", diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index ff90f8b91dc8..c9b3ad488949 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,13 +1,29 @@ import { ReportDialogOptions } from '@sentry/browser'; import { getCurrentHub } from '@sentry/node'; +import { dynamicRequire } from '@sentry/utils'; import { NextjsClient } from './common/NextjsClient'; +import { NextjsOptions } from './common/NextjsOptions'; /** - * TODO + * The Sentry NextJS SDK Client. + * + * TODO: docs, examples... + * */ -export function init(): void { - // TODO +export function init(initOptions: NextjsOptions): void { + if (isRunningInNode()) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + dynamicRequire(module, 'node module').init(initOptions); // TODO: init correct node module + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + dynamicRequire(module, 'browser module').init(initOptions); // TODO: init correct node module + } +} + +/** Returns whether this is being run in Node. */ +function isRunningInNode(): boolean { + return typeof process !== 'undefined' && process.versions != null && process.versions.node != null; } /** diff --git a/yarn.lock b/yarn.lock index cadd6f9e6b91..b228d3ecfac0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3346,7 +3346,7 @@ resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.2.0.tgz#ca020ff42913c6b9f88a9d0c375b5ee3965a2590" integrity sha512-vN4P/a+QqAuVfWFB9G3nQ7d6bgnM9jd/RLVi49owMuqvM24pv5mTQHUk2Hk4S3k7ConrHFl69E7xH6Dv5VpQnQ== -"@sentry/utils@6.2.0": +"@sentry/utils@6.2.0", "@sentry/utils@^6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.2.0.tgz#39c81ad5ba92cec54d690e3fa8ea4e777d8e9c2b" integrity sha512-YToUC7xYf2E/pIluI7upYTlj8fKXOtdwoOBkcQZifHgX/dP+qDaHibbBFe5PyZwdmU2UiLnWFsBr0gjo0QFo1g== @@ -4620,11 +4620,32 @@ after@0.8.2: resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f" integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8= -agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1: +agent-base@4, agent-base@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee" + integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg== + dependencies: + es6-promisify "^5.0.0" + +agent-base@5: version "5.1.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c" integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g== +agent-base@6: + version "6.0.2" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" + integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== + dependencies: + debug "4" + +agent-base@~4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" + integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg== + dependencies: + es6-promisify "^5.0.0" + agentkeepalive@^3.4.1: version "3.5.2" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67" @@ -9756,6 +9777,18 @@ es6-object-assign@^1.1.0: resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= +es6-promise@^4.0.3: + version "4.2.8" + resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" + integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== + +es6-promisify@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" + integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM= + dependencies: + es6-promise "^4.0.3" + escalade@^3.0.1: version "3.0.2" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4" @@ -13066,7 +13099,7 @@ jest-environment-jsdom@^24.9.0: jest-util "^24.9.0" jsdom "^11.5.1" -jest-environment-node@24, "jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: +"jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3" integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA== From 9657701e2fa19cab8a11e78be7f13a8eb994ffa5 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 23 Feb 2021 13:43:54 +0100 Subject: [PATCH 05/26] feat: Add NextJS browser entry point and exports for @sentry/types and @sentry/core --- packages/nextjs/src/browser/exports.ts | 34 ++++++++++++++++++++++++++ packages/nextjs/src/browser/index.ts | 14 +++++++++++ 2 files changed, 48 insertions(+) create mode 100644 packages/nextjs/src/browser/exports.ts create mode 100644 packages/nextjs/src/browser/index.ts diff --git a/packages/nextjs/src/browser/exports.ts b/packages/nextjs/src/browser/exports.ts new file mode 100644 index 000000000000..4dd1bf12ebf8 --- /dev/null +++ b/packages/nextjs/src/browser/exports.ts @@ -0,0 +1,34 @@ +export { + Breadcrumb, + Request, + SdkInfo, + Event, + Exception, + Response, + Severity, + StackFrame, + Stacktrace, + Status, + Thread, + User, +} from '@sentry/types'; + +export { + addGlobalEventProcessor, + addBreadcrumb, + captureException, + captureEvent, + captureMessage, + configureScope, + getHubFromCarrier, + getCurrentHub, + Hub, + Scope, + setContext, + setExtra, + setExtras, + setTag, + setTags, + setUser, + withScope, +} from '@sentry/core'; diff --git a/packages/nextjs/src/browser/index.ts b/packages/nextjs/src/browser/index.ts new file mode 100644 index 000000000000..0cd3da011cb3 --- /dev/null +++ b/packages/nextjs/src/browser/index.ts @@ -0,0 +1,14 @@ +import { initAndBind } from '@sentry/core'; + +import { NextjsOptions } from '../common/nextjsOptions'; +import { NextjsBrowserClient } from './browserClient'; + +export * from './exports'; + +/** Initializes the Sentry browser SDK. */ +export function init(initOptions: NextjsOptions): void { + // TODO: handle integrations + initAndBind(NextjsBrowserClient, initOptions); +} + +// TODO: same export functions as in `src/sdk.ts` ? From f62ecc7623641a9863c8f1ba47e70752892033e5 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 23 Feb 2021 13:47:17 +0100 Subject: [PATCH 06/26] feat: Add NextJS entry point --- packages/nextjs/src/index.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/nextjs/src/index.ts diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts new file mode 100644 index 000000000000..ed1cb9ad5706 --- /dev/null +++ b/packages/nextjs/src/index.ts @@ -0,0 +1,36 @@ +export { + Breadcrumb, + Request, + SdkInfo, + Event, + Exception, + Response, + Severity, + StackFrame, + Stacktrace, + Status, + Thread, + User, +} from '@sentry/types'; + +export { + addGlobalEventProcessor, + addBreadcrumb, + captureException, + captureEvent, + captureMessage, + configureScope, + getHubFromCarrier, + getCurrentHub, + Hub, + Scope, + setContext, + setExtra, + setExtras, + setTag, + setTags, + setUser, + withScope, +} from '@sentry/core'; + +export { init, showReportDialog, flush, close } from './sdk'; From 0a7e96b779658f95a21255036ceec9c42eff9c6a Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 23 Feb 2021 13:47:38 +0100 Subject: [PATCH 07/26] ref: Extract dynamic module requiring --- packages/nextjs/src/sdk.ts | 28 ++++++++++------------------ packages/nextjs/src/utils.ts | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 packages/nextjs/src/utils.ts diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index c9b3ad488949..8b10eab901fa 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,9 +1,9 @@ import { ReportDialogOptions } from '@sentry/browser'; import { getCurrentHub } from '@sentry/node'; -import { dynamicRequire } from '@sentry/utils'; -import { NextjsClient } from './common/NextjsClient'; -import { NextjsOptions } from './common/NextjsOptions'; +import { NextjsClientInterface } from './common/nextjsClient'; +import { NextjsOptions } from './common/nextjsOptions'; +import { dynamicRequireNextjsModule } from './utils'; /** * The Sentry NextJS SDK Client. @@ -12,18 +12,10 @@ import { NextjsOptions } from './common/NextjsOptions'; * */ export function init(initOptions: NextjsOptions): void { - if (isRunningInNode()) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - dynamicRequire(module, 'node module').init(initOptions); // TODO: init correct node module - } else { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - dynamicRequire(module, 'browser module').init(initOptions); // TODO: init correct node module - } -} - -/** Returns whether this is being run in Node. */ -function isRunningInNode(): boolean { - return typeof process !== 'undefined' && process.versions != null && process.versions.node != null; + // eslint-disable-next-line no-console + console.log('Initializing NextJS...'); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + dynamicRequireNextjsModule().init(initOptions); } /** @@ -32,7 +24,7 @@ function isRunningInNode(): boolean { * @param dialogOptions Everything is optional, we try to fetch all the info we need from the global scope. */ export function showReportDialog(dialogOptions: ReportDialogOptions = {}): void { - const client = getCurrentHub().getClient(); + const client = getCurrentHub().getClient(); if (client) { client.showReportDialog(dialogOptions); } @@ -45,7 +37,7 @@ export function showReportDialog(dialogOptions: ReportDialogOptions = {}): void * @param timeout Maximum time in ms the client should wait. */ export async function flush(timeout?: number): Promise { - const client = getCurrentHub().getClient(); + const client = getCurrentHub().getClient(); return client ? client.flush(timeout) : Promise.reject(false); } @@ -56,6 +48,6 @@ export async function flush(timeout?: number): Promise { * @param timeout Maximum time in ms the client should wait. */ export async function close(timeout?: number): Promise { - const client = getCurrentHub().getClient(); + const client = getCurrentHub().getClient(); return client ? client.close(timeout) : Promise.reject(false); } diff --git a/packages/nextjs/src/utils.ts b/packages/nextjs/src/utils.ts new file mode 100644 index 000000000000..92d27f2f4821 --- /dev/null +++ b/packages/nextjs/src/utils.ts @@ -0,0 +1,20 @@ +import { dynamicRequire } from '@sentry/utils'; + +/** + * Dynamically require the correct NextJS module, and return it. + * + * The correct module depends on the environment the app is running in; + * either browser, or node. + */ +export function dynamicRequireNextjsModule(): any { + if (isRunningInNode()) { + return dynamicRequire(module, './module'); + } else { + return dynamicRequire(module, './browser'); + } +} + +/** Returns whether this is being run in Node. */ +function isRunningInNode(): boolean { + return typeof process !== 'undefined' && process.versions != null && process.versions.node != null; +} From 8931f677a127878822aa0a005a9f88cd681edac4 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 23 Feb 2021 13:51:09 +0100 Subject: [PATCH 08/26] build: Add dependencies and update `yarn.lock` --- packages/nextjs/package.json | 10 ++++----- yarn.lock | 39 +++--------------------------------- 2 files changed, 8 insertions(+), 41 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 883c2a241d6a..4f72d9e9c7f3 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -9,16 +9,16 @@ "engines": { "node": ">=6" }, - "main": "dist/index.js", - "module": "esm/index.js", - "types": "dist/index.d.ts", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", "publishConfig": { "access": "public" }, "dependencies": { - "@sentry/browser": "^6.2.0", + "@sentry/core": "^6.2.0", + "@sentry/utils": "^6.2.0", "@sentry/node": "^6.2.0", - "@sentry/utils": "^6.2.0" + "@sentry/browser": "^6.2.0" }, "devDependencies": { "@sentry/types": "^6.2.0", diff --git a/yarn.lock b/yarn.lock index b228d3ecfac0..56e3c9eca167 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3286,7 +3286,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/core@6.2.0": +"@sentry/core@6.2.0", "@sentry/core@^6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.2.0.tgz#be1c33854fc94e1a4d867f7c2c311cf1cefb8ee9" integrity sha512-oTr2b25l+0bv/+d6IgMamPuGleWV7OgJb0NFfd+WZhw6UDRgr7CdEJy2gW6tK8SerwXgPHdn4ervxsT3WIBiXw== @@ -4620,32 +4620,11 @@ after@0.8.2: resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f" integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8= -agent-base@4, agent-base@^4.3.0: - version "4.3.0" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee" - integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg== - dependencies: - es6-promisify "^5.0.0" - -agent-base@5: +agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1: version "5.1.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c" integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g== -agent-base@6: - version "6.0.2" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" - integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== - dependencies: - debug "4" - -agent-base@~4.2.1: - version "4.2.1" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" - integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg== - dependencies: - es6-promisify "^5.0.0" - agentkeepalive@^3.4.1: version "3.5.2" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67" @@ -9777,18 +9756,6 @@ es6-object-assign@^1.1.0: resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= -es6-promise@^4.0.3: - version "4.2.8" - resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" - integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== - -es6-promisify@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" - integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM= - dependencies: - es6-promise "^4.0.3" - escalade@^3.0.1: version "3.0.2" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4" @@ -13099,7 +13066,7 @@ jest-environment-jsdom@^24.9.0: jest-util "^24.9.0" jsdom "^11.5.1" -"jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: +jest-environment-node@24, "jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3" integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA== From 633a67aa8241da51f763b8788d2cee014f6cc801 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Tue, 23 Feb 2021 15:42:58 +0100 Subject: [PATCH 09/26] feat: NextjsClient wrapper and interface --- packages/nextjs/src/common/NextjsClient.ts | 84 +++++++++++++++++++++- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/packages/nextjs/src/common/NextjsClient.ts b/packages/nextjs/src/common/NextjsClient.ts index 8e7ad7bbb822..a49f37cf842b 100644 --- a/packages/nextjs/src/common/NextjsClient.ts +++ b/packages/nextjs/src/common/NextjsClient.ts @@ -1,10 +1,88 @@ import { ReportDialogOptions } from '@sentry/browser'; -import { Client } from '@sentry/types'; +import { ClientClass } from '@sentry/core'; +import { Event, EventHint, Severity } from '@sentry/node'; +import { Client, Dsn, Integration, IntegrationClass, Scope } from '@sentry/types'; -import { NextjsOptions } from './NextjsOptions'; +import { dynamicRequireNextjsModule } from '../utils'; +import { NextjsOptions } from './nextjsOptions'; /** Common interface for NextJS clients. */ -export interface NextjsClient extends Client { +export interface NextjsClientInterface extends Client { + /** + * Uploads a native crash dump (Minidump) to Sentry. + * + * @param path The relative or absolute path to the minidump. + * @param event Optional event payload to attach to the minidump. + * @param scope Optional The SDK scope used to upload. + */ + captureMinidump(path: string, event?: Event, scope?: Scope): string | undefined; + /** Shows the report dialog. */ showReportDialog(dialogOptions: ReportDialogOptions): void; } + +/** A wrapper around the actual NextJS client. */ +export class NextjsClientWrapper implements NextjsClientInterface { + private readonly _actualClient: NextjsClientInterface; + + constructor(options: NextjsOptions) { + const clientClass: ClientClass = dynamicRequireNextjsModule(); + this._actualClient = new clientClass(options); + } + + /** @inheritdoc */ + public captureMinidump(path: string, event?: Event, scope?: Scope): string | undefined { + return this._actualClient.captureMinidump(path, event, scope); + } + + /** @inheritdoc */ + public showReportDialog(dialogOptions: ReportDialogOptions): void { + return this._actualClient.showReportDialog(dialogOptions); + } + + /** @inheritdoc */ + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined { + return this._actualClient.captureException(exception, hint, scope); + } + + /** @inheritdoc */ + public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined { + return this._actualClient.captureMessage(message, level, hint, scope); + } + + /** @inheritdoc */ + public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined { + return this._actualClient.captureEvent(event, hint, scope); + } + + /** @inheritdoc */ + public getDsn(): Dsn | undefined { + return this._actualClient.getDsn(); + } + + /** @inheritdoc */ + public getOptions(): NextjsOptions { + return this._actualClient.getOptions(); + } + + /** @inheritdoc */ + public close(timeout?: number): PromiseLike { + return this._actualClient.close(timeout); + } + + /** @inheritdoc */ + public flush(timeout?: number): PromiseLike { + return this._actualClient.flush(timeout); + } + + /** @inheritdoc */ + public getIntegration(integration: IntegrationClass): T | null { + return this._actualClient.getIntegration(integration); + } + + /** @inheritdoc */ + public setupIntegrations(): void { + return this._actualClient.setupIntegrations(); + } +} From 62ceb09c59cbc2d6ca70680c63fb0e642d289a83 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 10:57:31 +0100 Subject: [PATCH 10/26] feat: Add browser client and backend --- packages/nextjs/src/browser/browserBackend.ts | 44 +++++++++++++++++++ packages/nextjs/src/browser/browserClient.ts | 41 +++++++++++++++++ packages/nextjs/src/browser/index.ts | 2 + packages/nextjs/src/common/NextjsClient.ts | 26 ++++++----- packages/nextjs/src/common/NextjsOptions.ts | 4 +- packages/nextjs/src/common/nextjsBackend.ts | 1 + packages/nextjs/src/module/index.ts | 2 + .../nextjs/src/{utils.ts => moduleLoader.ts} | 3 +- packages/nextjs/src/sdk.ts | 4 +- 9 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 packages/nextjs/src/browser/browserBackend.ts create mode 100644 packages/nextjs/src/browser/browserClient.ts create mode 100644 packages/nextjs/src/common/nextjsBackend.ts create mode 100644 packages/nextjs/src/module/index.ts rename packages/nextjs/src/{utils.ts => moduleLoader.ts} (81%) diff --git a/packages/nextjs/src/browser/browserBackend.ts b/packages/nextjs/src/browser/browserBackend.ts new file mode 100644 index 000000000000..e46d89a7e78c --- /dev/null +++ b/packages/nextjs/src/browser/browserBackend.ts @@ -0,0 +1,44 @@ +import { eventFromException, eventFromMessage } from '@sentry/browser'; +import { BaseBackend } from '@sentry/core'; +import { Event, EventHint, Scope, Severity } from '@sentry/types'; + +import { CommonBackend } from '../common/nextjsBackend'; +import { NextjsOptions } from '../common/nextjsOptions'; + +/** + * Store the IPC interface on a window, so that both regular and isolated contexts are the same. + */ +declare global { + interface Window { + __SENTRY_IPC__: + | { + sendScope: (scope: Scope) => void; + sendEvent: (event: Event) => void; + pingMain: (success: () => void) => void; + } + | undefined; + } +} + +/** */ +export class NextjsBrowserBackend extends BaseBackend implements CommonBackend { + public constructor(options: NextjsOptions) { + super(options); + } + + /** @inheritDoc */ + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + public eventFromException(_exception: any, _hint?: EventHint): PromiseLike { + return eventFromException(this._options, _exception, _hint); + } + + /** @inheritDoc */ + public eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): PromiseLike { + return eventFromMessage(this._options, _message, _level, _hint); + } + + /** @inheritDoc */ + public sendEvent(event: Event): void { + window.__SENTRY_IPC__?.sendEvent(event); + } +} diff --git a/packages/nextjs/src/browser/browserClient.ts b/packages/nextjs/src/browser/browserClient.ts new file mode 100644 index 000000000000..e766a69932cb --- /dev/null +++ b/packages/nextjs/src/browser/browserClient.ts @@ -0,0 +1,41 @@ +import { injectReportDialog, ReportDialogOptions } from '@sentry/browser'; +import { BaseClient, getCurrentHub } from '@sentry/core'; +import { logger } from '@sentry/utils'; + +import { NextjsClientInterface } from '../common/nextjsClient'; +import { NextjsOptions } from '../common/nextjsOptions'; +import { NextjsBrowserBackend } from './browserBackend'; + +/** */ +export class NextjsBrowserClient extends BaseClient + implements NextjsClientInterface { + /** + * Creates a new NextJS SDK instace. + * @param options config options for the NextJS SDK. + */ + public constructor(options: NextjsOptions) { + super(NextjsBrowserBackend, options); + } + + /** + * Uploads a native crash dump (Minidump) to Sentry. + * + * @param path The relative or absolute path to the minidump. + * @param event Optional event payload to attach to the minidump. + * @param scope The SDK scope used to upload. + */ + captureMinidump(): string | undefined { + logger.warn('captureMinidump is a NOOP in the browser'); + return undefined; + } + + /** @inheritDoc */ + showReportDialog(dialogOptions: ReportDialogOptions): void { + if (!dialogOptions.eventId) { + dialogOptions.eventId = getCurrentHub().lastEventId(); + } + injectReportDialog(dialogOptions); + } + + // TODO: override `_prepareEvent` ? +} diff --git a/packages/nextjs/src/browser/index.ts b/packages/nextjs/src/browser/index.ts index 0cd3da011cb3..8e986bc600b2 100644 --- a/packages/nextjs/src/browser/index.ts +++ b/packages/nextjs/src/browser/index.ts @@ -8,6 +8,8 @@ export * from './exports'; /** Initializes the Sentry browser SDK. */ export function init(initOptions: NextjsOptions): void { // TODO: handle integrations + // eslint-disable-next-line no-console + console.log('initializing browser...'); initAndBind(NextjsBrowserClient, initOptions); } diff --git a/packages/nextjs/src/common/NextjsClient.ts b/packages/nextjs/src/common/NextjsClient.ts index a49f37cf842b..cde7afcd957a 100644 --- a/packages/nextjs/src/common/NextjsClient.ts +++ b/packages/nextjs/src/common/NextjsClient.ts @@ -3,7 +3,7 @@ import { ClientClass } from '@sentry/core'; import { Event, EventHint, Severity } from '@sentry/node'; import { Client, Dsn, Integration, IntegrationClass, Scope } from '@sentry/types'; -import { dynamicRequireNextjsModule } from '../utils'; +import { dynamicRequireNextjsModule } from '../moduleLoader'; import { NextjsOptions } from './nextjsOptions'; /** Common interface for NextJS clients. */ @@ -21,6 +21,8 @@ export interface NextjsClientInterface extends Client { showReportDialog(dialogOptions: ReportDialogOptions): void; } +// This class below is only used when export + /** A wrapper around the actual NextJS client. */ export class NextjsClientWrapper implements NextjsClientInterface { private readonly _actualClient: NextjsClientInterface; @@ -30,58 +32,58 @@ export class NextjsClientWrapper implements NextjsClientInterface { this._actualClient = new clientClass(options); } - /** @inheritdoc */ + /** @inheritDoc */ public captureMinidump(path: string, event?: Event, scope?: Scope): string | undefined { return this._actualClient.captureMinidump(path, event, scope); } - /** @inheritdoc */ + /** @inheritDoc */ public showReportDialog(dialogOptions: ReportDialogOptions): void { return this._actualClient.showReportDialog(dialogOptions); } - /** @inheritdoc */ + /** @inheritDoc */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined { return this._actualClient.captureException(exception, hint, scope); } - /** @inheritdoc */ + /** @inheritDoc */ public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined { return this._actualClient.captureMessage(message, level, hint, scope); } - /** @inheritdoc */ + /** @inheritDoc */ public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined { return this._actualClient.captureEvent(event, hint, scope); } - /** @inheritdoc */ + /** @inheritDoc */ public getDsn(): Dsn | undefined { return this._actualClient.getDsn(); } - /** @inheritdoc */ + /** @inheritDoc */ public getOptions(): NextjsOptions { return this._actualClient.getOptions(); } - /** @inheritdoc */ + /** @inheritDoc */ public close(timeout?: number): PromiseLike { return this._actualClient.close(timeout); } - /** @inheritdoc */ + /** @inheritDoc */ public flush(timeout?: number): PromiseLike { return this._actualClient.flush(timeout); } - /** @inheritdoc */ + /** @inheritDoc */ public getIntegration(integration: IntegrationClass): T | null { return this._actualClient.getIntegration(integration); } - /** @inheritdoc */ + /** @inheritDoc */ public setupIntegrations(): void { return this._actualClient.setupIntegrations(); } diff --git a/packages/nextjs/src/common/NextjsOptions.ts b/packages/nextjs/src/common/NextjsOptions.ts index 84943373602b..fde4c260d739 100644 --- a/packages/nextjs/src/common/NextjsOptions.ts +++ b/packages/nextjs/src/common/NextjsOptions.ts @@ -1,7 +1,7 @@ import { BrowserOptions } from '@sentry/browser'; -import { NodeOptions } from '@sentry/node'; +// import { NodeOptions } from '@sentry/node'; import { Options } from '@sentry/types'; -export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { +export interface NextjsOptions extends Options, BrowserOptions { // TODO: options for NextJS } diff --git a/packages/nextjs/src/common/nextjsBackend.ts b/packages/nextjs/src/common/nextjsBackend.ts new file mode 100644 index 000000000000..25cdd6a0e35c --- /dev/null +++ b/packages/nextjs/src/common/nextjsBackend.ts @@ -0,0 +1 @@ +export { BaseBackend as CommonBackend } from '@sentry/core'; diff --git a/packages/nextjs/src/module/index.ts b/packages/nextjs/src/module/index.ts new file mode 100644 index 000000000000..294358cdf046 --- /dev/null +++ b/packages/nextjs/src/module/index.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line no-console +console.log('importing for node...'); diff --git a/packages/nextjs/src/utils.ts b/packages/nextjs/src/moduleLoader.ts similarity index 81% rename from packages/nextjs/src/utils.ts rename to packages/nextjs/src/moduleLoader.ts index 92d27f2f4821..26347e092fdf 100644 --- a/packages/nextjs/src/utils.ts +++ b/packages/nextjs/src/moduleLoader.ts @@ -8,7 +8,8 @@ import { dynamicRequire } from '@sentry/utils'; */ export function dynamicRequireNextjsModule(): any { if (isRunningInNode()) { - return dynamicRequire(module, './module'); + // return dynamicRequire(module, './module'); + return dynamicRequire(module, './browser'); // TODO: this should be removed } else { return dynamicRequire(module, './browser'); } diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index 8b10eab901fa..2d87814b7927 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -3,7 +3,7 @@ import { getCurrentHub } from '@sentry/node'; import { NextjsClientInterface } from './common/nextjsClient'; import { NextjsOptions } from './common/nextjsOptions'; -import { dynamicRequireNextjsModule } from './utils'; +import { dynamicRequireNextjsModule } from './moduleLoader'; /** * The Sentry NextJS SDK Client. @@ -13,7 +13,7 @@ import { dynamicRequireNextjsModule } from './utils'; */ export function init(initOptions: NextjsOptions): void { // eslint-disable-next-line no-console - console.log('Initializing NextJS...'); + // console.log('Initializing NextJS...'); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access dynamicRequireNextjsModule().init(initOptions); } From 205c2282d05efb224d52fd3a85ac4cd33c597250 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 13:55:48 +0100 Subject: [PATCH 11/26] ref: Simplify approach with package.json --- packages/nextjs/package.json | 7 +- packages/nextjs/src/browser.ts | 8 ++ packages/nextjs/src/browser/browserBackend.ts | 44 --------- packages/nextjs/src/browser/browserClient.ts | 41 --------- packages/nextjs/src/browser/exports.ts | 34 ------- packages/nextjs/src/browser/index.ts | 16 ---- packages/nextjs/src/common/NextjsClient.ts | 90 ------------------- packages/nextjs/src/common/nextjsBackend.ts | 1 - packages/nextjs/src/index.ts | 37 +------- packages/nextjs/src/module/index.ts | 2 - packages/nextjs/src/moduleLoader.ts | 21 ----- packages/nextjs/src/node.ts | 8 ++ .../{common/NextjsOptions.ts => options.ts} | 2 +- packages/nextjs/src/sdk.ts | 59 ++++-------- yarn.lock | 39 +++++++- 15 files changed, 74 insertions(+), 335 deletions(-) create mode 100644 packages/nextjs/src/browser.ts delete mode 100644 packages/nextjs/src/browser/browserBackend.ts delete mode 100644 packages/nextjs/src/browser/browserClient.ts delete mode 100644 packages/nextjs/src/browser/exports.ts delete mode 100644 packages/nextjs/src/browser/index.ts delete mode 100644 packages/nextjs/src/common/NextjsClient.ts delete mode 100644 packages/nextjs/src/common/nextjsBackend.ts delete mode 100644 packages/nextjs/src/module/index.ts delete mode 100644 packages/nextjs/src/moduleLoader.ts create mode 100644 packages/nextjs/src/node.ts rename packages/nextjs/src/{common/NextjsOptions.ts => options.ts} (66%) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 4f72d9e9c7f3..a765e6b5e290 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -10,15 +10,18 @@ "node": ">=6" }, "main": "./dist/index.js", + "module": "./esm/node.js", + "browser": "./esm/browser.js", "types": "./dist/index.d.ts", "publishConfig": { "access": "public" }, "dependencies": { + "@sentry/browser": "^6.2.0", "@sentry/core": "^6.2.0", - "@sentry/utils": "^6.2.0", + "@sentry/minimal": "^6.2.0", "@sentry/node": "^6.2.0", - "@sentry/browser": "^6.2.0" + "@sentry/utils": "^6.2.0" }, "devDependencies": { "@sentry/types": "^6.2.0", diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts new file mode 100644 index 000000000000..b356813bfb96 --- /dev/null +++ b/packages/nextjs/src/browser.ts @@ -0,0 +1,8 @@ +import * as SentryBrowser from '@sentry/browser'; + +import { NextjsOptions } from './options'; + +/** */ +export function init(options: NextjsOptions): any { + SentryBrowser.init(options); +} diff --git a/packages/nextjs/src/browser/browserBackend.ts b/packages/nextjs/src/browser/browserBackend.ts deleted file mode 100644 index e46d89a7e78c..000000000000 --- a/packages/nextjs/src/browser/browserBackend.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { eventFromException, eventFromMessage } from '@sentry/browser'; -import { BaseBackend } from '@sentry/core'; -import { Event, EventHint, Scope, Severity } from '@sentry/types'; - -import { CommonBackend } from '../common/nextjsBackend'; -import { NextjsOptions } from '../common/nextjsOptions'; - -/** - * Store the IPC interface on a window, so that both regular and isolated contexts are the same. - */ -declare global { - interface Window { - __SENTRY_IPC__: - | { - sendScope: (scope: Scope) => void; - sendEvent: (event: Event) => void; - pingMain: (success: () => void) => void; - } - | undefined; - } -} - -/** */ -export class NextjsBrowserBackend extends BaseBackend implements CommonBackend { - public constructor(options: NextjsOptions) { - super(options); - } - - /** @inheritDoc */ - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - public eventFromException(_exception: any, _hint?: EventHint): PromiseLike { - return eventFromException(this._options, _exception, _hint); - } - - /** @inheritDoc */ - public eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): PromiseLike { - return eventFromMessage(this._options, _message, _level, _hint); - } - - /** @inheritDoc */ - public sendEvent(event: Event): void { - window.__SENTRY_IPC__?.sendEvent(event); - } -} diff --git a/packages/nextjs/src/browser/browserClient.ts b/packages/nextjs/src/browser/browserClient.ts deleted file mode 100644 index e766a69932cb..000000000000 --- a/packages/nextjs/src/browser/browserClient.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { injectReportDialog, ReportDialogOptions } from '@sentry/browser'; -import { BaseClient, getCurrentHub } from '@sentry/core'; -import { logger } from '@sentry/utils'; - -import { NextjsClientInterface } from '../common/nextjsClient'; -import { NextjsOptions } from '../common/nextjsOptions'; -import { NextjsBrowserBackend } from './browserBackend'; - -/** */ -export class NextjsBrowserClient extends BaseClient - implements NextjsClientInterface { - /** - * Creates a new NextJS SDK instace. - * @param options config options for the NextJS SDK. - */ - public constructor(options: NextjsOptions) { - super(NextjsBrowserBackend, options); - } - - /** - * Uploads a native crash dump (Minidump) to Sentry. - * - * @param path The relative or absolute path to the minidump. - * @param event Optional event payload to attach to the minidump. - * @param scope The SDK scope used to upload. - */ - captureMinidump(): string | undefined { - logger.warn('captureMinidump is a NOOP in the browser'); - return undefined; - } - - /** @inheritDoc */ - showReportDialog(dialogOptions: ReportDialogOptions): void { - if (!dialogOptions.eventId) { - dialogOptions.eventId = getCurrentHub().lastEventId(); - } - injectReportDialog(dialogOptions); - } - - // TODO: override `_prepareEvent` ? -} diff --git a/packages/nextjs/src/browser/exports.ts b/packages/nextjs/src/browser/exports.ts deleted file mode 100644 index 4dd1bf12ebf8..000000000000 --- a/packages/nextjs/src/browser/exports.ts +++ /dev/null @@ -1,34 +0,0 @@ -export { - Breadcrumb, - Request, - SdkInfo, - Event, - Exception, - Response, - Severity, - StackFrame, - Stacktrace, - Status, - Thread, - User, -} from '@sentry/types'; - -export { - addGlobalEventProcessor, - addBreadcrumb, - captureException, - captureEvent, - captureMessage, - configureScope, - getHubFromCarrier, - getCurrentHub, - Hub, - Scope, - setContext, - setExtra, - setExtras, - setTag, - setTags, - setUser, - withScope, -} from '@sentry/core'; diff --git a/packages/nextjs/src/browser/index.ts b/packages/nextjs/src/browser/index.ts deleted file mode 100644 index 8e986bc600b2..000000000000 --- a/packages/nextjs/src/browser/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { initAndBind } from '@sentry/core'; - -import { NextjsOptions } from '../common/nextjsOptions'; -import { NextjsBrowserClient } from './browserClient'; - -export * from './exports'; - -/** Initializes the Sentry browser SDK. */ -export function init(initOptions: NextjsOptions): void { - // TODO: handle integrations - // eslint-disable-next-line no-console - console.log('initializing browser...'); - initAndBind(NextjsBrowserClient, initOptions); -} - -// TODO: same export functions as in `src/sdk.ts` ? diff --git a/packages/nextjs/src/common/NextjsClient.ts b/packages/nextjs/src/common/NextjsClient.ts deleted file mode 100644 index cde7afcd957a..000000000000 --- a/packages/nextjs/src/common/NextjsClient.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { ReportDialogOptions } from '@sentry/browser'; -import { ClientClass } from '@sentry/core'; -import { Event, EventHint, Severity } from '@sentry/node'; -import { Client, Dsn, Integration, IntegrationClass, Scope } from '@sentry/types'; - -import { dynamicRequireNextjsModule } from '../moduleLoader'; -import { NextjsOptions } from './nextjsOptions'; - -/** Common interface for NextJS clients. */ -export interface NextjsClientInterface extends Client { - /** - * Uploads a native crash dump (Minidump) to Sentry. - * - * @param path The relative or absolute path to the minidump. - * @param event Optional event payload to attach to the minidump. - * @param scope Optional The SDK scope used to upload. - */ - captureMinidump(path: string, event?: Event, scope?: Scope): string | undefined; - - /** Shows the report dialog. */ - showReportDialog(dialogOptions: ReportDialogOptions): void; -} - -// This class below is only used when export - -/** A wrapper around the actual NextJS client. */ -export class NextjsClientWrapper implements NextjsClientInterface { - private readonly _actualClient: NextjsClientInterface; - - constructor(options: NextjsOptions) { - const clientClass: ClientClass = dynamicRequireNextjsModule(); - this._actualClient = new clientClass(options); - } - - /** @inheritDoc */ - public captureMinidump(path: string, event?: Event, scope?: Scope): string | undefined { - return this._actualClient.captureMinidump(path, event, scope); - } - - /** @inheritDoc */ - public showReportDialog(dialogOptions: ReportDialogOptions): void { - return this._actualClient.showReportDialog(dialogOptions); - } - - /** @inheritDoc */ - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined { - return this._actualClient.captureException(exception, hint, scope); - } - - /** @inheritDoc */ - public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined { - return this._actualClient.captureMessage(message, level, hint, scope); - } - - /** @inheritDoc */ - public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined { - return this._actualClient.captureEvent(event, hint, scope); - } - - /** @inheritDoc */ - public getDsn(): Dsn | undefined { - return this._actualClient.getDsn(); - } - - /** @inheritDoc */ - public getOptions(): NextjsOptions { - return this._actualClient.getOptions(); - } - - /** @inheritDoc */ - public close(timeout?: number): PromiseLike { - return this._actualClient.close(timeout); - } - - /** @inheritDoc */ - public flush(timeout?: number): PromiseLike { - return this._actualClient.flush(timeout); - } - - /** @inheritDoc */ - public getIntegration(integration: IntegrationClass): T | null { - return this._actualClient.getIntegration(integration); - } - - /** @inheritDoc */ - public setupIntegrations(): void { - return this._actualClient.setupIntegrations(); - } -} diff --git a/packages/nextjs/src/common/nextjsBackend.ts b/packages/nextjs/src/common/nextjsBackend.ts deleted file mode 100644 index 25cdd6a0e35c..000000000000 --- a/packages/nextjs/src/common/nextjsBackend.ts +++ /dev/null @@ -1 +0,0 @@ -export { BaseBackend as CommonBackend } from '@sentry/core'; diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts index ed1cb9ad5706..78065ace4cd4 100644 --- a/packages/nextjs/src/index.ts +++ b/packages/nextjs/src/index.ts @@ -1,36 +1 @@ -export { - Breadcrumb, - Request, - SdkInfo, - Event, - Exception, - Response, - Severity, - StackFrame, - Stacktrace, - Status, - Thread, - User, -} from '@sentry/types'; - -export { - addGlobalEventProcessor, - addBreadcrumb, - captureException, - captureEvent, - captureMessage, - configureScope, - getHubFromCarrier, - getCurrentHub, - Hub, - Scope, - setContext, - setExtra, - setExtras, - setTag, - setTags, - setUser, - withScope, -} from '@sentry/core'; - -export { init, showReportDialog, flush, close } from './sdk'; +export { init } from './sdk'; diff --git a/packages/nextjs/src/module/index.ts b/packages/nextjs/src/module/index.ts deleted file mode 100644 index 294358cdf046..000000000000 --- a/packages/nextjs/src/module/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -// eslint-disable-next-line no-console -console.log('importing for node...'); diff --git a/packages/nextjs/src/moduleLoader.ts b/packages/nextjs/src/moduleLoader.ts deleted file mode 100644 index 26347e092fdf..000000000000 --- a/packages/nextjs/src/moduleLoader.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { dynamicRequire } from '@sentry/utils'; - -/** - * Dynamically require the correct NextJS module, and return it. - * - * The correct module depends on the environment the app is running in; - * either browser, or node. - */ -export function dynamicRequireNextjsModule(): any { - if (isRunningInNode()) { - // return dynamicRequire(module, './module'); - return dynamicRequire(module, './browser'); // TODO: this should be removed - } else { - return dynamicRequire(module, './browser'); - } -} - -/** Returns whether this is being run in Node. */ -function isRunningInNode(): boolean { - return typeof process !== 'undefined' && process.versions != null && process.versions.node != null; -} diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts new file mode 100644 index 000000000000..92008f4df786 --- /dev/null +++ b/packages/nextjs/src/node.ts @@ -0,0 +1,8 @@ +import * as SentryNode from '@sentry/node'; + +import { NextjsOptions } from './options'; + +/** */ +export function init(options: NextjsOptions): any { + SentryNode.init(options); +} diff --git a/packages/nextjs/src/common/NextjsOptions.ts b/packages/nextjs/src/options.ts similarity index 66% rename from packages/nextjs/src/common/NextjsOptions.ts rename to packages/nextjs/src/options.ts index fde4c260d739..0c40abcb3d27 100644 --- a/packages/nextjs/src/common/NextjsOptions.ts +++ b/packages/nextjs/src/options.ts @@ -2,6 +2,6 @@ import { BrowserOptions } from '@sentry/browser'; // import { NodeOptions } from '@sentry/node'; import { Options } from '@sentry/types'; -export interface NextjsOptions extends Options, BrowserOptions { +export interface NextjsOptions extends Options, BrowserOptions /** , NodeOptions */ { // TODO: options for NextJS } diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index 2d87814b7927..c2369c60c74b 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,9 +1,6 @@ -import { ReportDialogOptions } from '@sentry/browser'; -import { getCurrentHub } from '@sentry/node'; +import { init as browserInit, SDK_VERSION } from '@sentry/browser'; -import { NextjsClientInterface } from './common/nextjsClient'; -import { NextjsOptions } from './common/nextjsOptions'; -import { dynamicRequireNextjsModule } from './moduleLoader'; +import { NextjsOptions } from './options'; /** * The Sentry NextJS SDK Client. @@ -11,43 +8,17 @@ import { dynamicRequireNextjsModule } from './moduleLoader'; * TODO: docs, examples... * */ -export function init(initOptions: NextjsOptions): void { - // eslint-disable-next-line no-console - // console.log('Initializing NextJS...'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - dynamicRequireNextjsModule().init(initOptions); -} - -/** - * Present the user with a report dialog. - * - * @param dialogOptions Everything is optional, we try to fetch all the info we need from the global scope. - */ -export function showReportDialog(dialogOptions: ReportDialogOptions = {}): void { - const client = getCurrentHub().getClient(); - if (client) { - client.showReportDialog(dialogOptions); - } -} - -/** - * A promise that resolves when all current events have been sent. - * If you provide a timeout and the queue takes longer to drain the promise returns false. - * - * @param timeout Maximum time in ms the client should wait. - */ -export async function flush(timeout?: number): Promise { - const client = getCurrentHub().getClient(); - return client ? client.flush(timeout) : Promise.reject(false); -} - -/** - * A promise that resolves when all current events have been sent. - * If you provide a timeout and the queue takes longer to drain the promise returns false. - * - * @param timeout Maximum time in ms the client should wait. - */ -export async function close(timeout?: number): Promise { - const client = getCurrentHub().getClient(); - return client ? client.close(timeout) : Promise.reject(false); +export function init(options: NextjsOptions): void { + options._metadata = options._metadata || {}; + options._metadata.sdk = { + name: 'sentry.javascript.nextjs', + packages: [ + { + name: '', // TODO + version: SDK_VERSION, + }, + ], + version: SDK_VERSION, + }; + browserInit(options); } diff --git a/yarn.lock b/yarn.lock index 56e3c9eca167..e0506236a0bd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3306,7 +3306,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/minimal@6.2.0": +"@sentry/minimal@6.2.0", "@sentry/minimal@^6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.2.0.tgz#718b70babb55912eeb38babaf7823d1bcdd77d1e" integrity sha512-haxsx8/ZafhZUaGeeMtY7bJt9HbDlqeiaXrRMp1CxGtd0ZRQwHt60imEjl6IH1I73SEWxNfqScGsX2s3HzztMg== @@ -4620,11 +4620,32 @@ after@0.8.2: resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f" integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8= -agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1: +agent-base@4, agent-base@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee" + integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg== + dependencies: + es6-promisify "^5.0.0" + +agent-base@5: version "5.1.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c" integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g== +agent-base@6: + version "6.0.2" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" + integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== + dependencies: + debug "4" + +agent-base@~4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" + integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg== + dependencies: + es6-promisify "^5.0.0" + agentkeepalive@^3.4.1: version "3.5.2" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67" @@ -9756,6 +9777,18 @@ es6-object-assign@^1.1.0: resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= +es6-promise@^4.0.3: + version "4.2.8" + resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" + integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== + +es6-promisify@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" + integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM= + dependencies: + es6-promise "^4.0.3" + escalade@^3.0.1: version "3.0.2" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4" @@ -13066,7 +13099,7 @@ jest-environment-jsdom@^24.9.0: jest-util "^24.9.0" jsdom "^11.5.1" -jest-environment-node@24, "jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: +"jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3" integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA== From 33f5a5045e0811d7ec22d5ae9007d64e3ecb6d1f Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 14:10:15 +0100 Subject: [PATCH 12/26] feat: Export @sentry/minimal --- packages/nextjs/src/browser.ts | 2 ++ packages/nextjs/src/index.ts | 1 + packages/nextjs/src/node.ts | 2 ++ packages/nextjs/src/options.ts | 4 ++-- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index b356813bfb96..e35efc122ea1 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -6,3 +6,5 @@ import { NextjsOptions } from './options'; export function init(options: NextjsOptions): any { SentryBrowser.init(options); } + +export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts index 78065ace4cd4..385f67171cad 100644 --- a/packages/nextjs/src/index.ts +++ b/packages/nextjs/src/index.ts @@ -1 +1,2 @@ export { init } from './sdk'; +export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index 92008f4df786..2195cea749e6 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -6,3 +6,5 @@ import { NextjsOptions } from './options'; export function init(options: NextjsOptions): any { SentryNode.init(options); } + +export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/options.ts b/packages/nextjs/src/options.ts index 0c40abcb3d27..84943373602b 100644 --- a/packages/nextjs/src/options.ts +++ b/packages/nextjs/src/options.ts @@ -1,7 +1,7 @@ import { BrowserOptions } from '@sentry/browser'; -// import { NodeOptions } from '@sentry/node'; +import { NodeOptions } from '@sentry/node'; import { Options } from '@sentry/types'; -export interface NextjsOptions extends Options, BrowserOptions /** , NodeOptions */ { +export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { // TODO: options for NextJS } From 627387700bf9bd67d62016a86c22903bc2d6e6d8 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 14:15:35 +0100 Subject: [PATCH 13/26] build: Remove @sentry/core and @sentry/utils from dependencies --- packages/nextjs/package.json | 4 +--- yarn.lock | 41 ++++-------------------------------- 2 files changed, 5 insertions(+), 40 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index a765e6b5e290..fa201226514a 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -18,10 +18,8 @@ }, "dependencies": { "@sentry/browser": "^6.2.0", - "@sentry/core": "^6.2.0", - "@sentry/minimal": "^6.2.0", "@sentry/node": "^6.2.0", - "@sentry/utils": "^6.2.0" + "@sentry/minimal": "^6.2.0" }, "devDependencies": { "@sentry/types": "^6.2.0", diff --git a/yarn.lock b/yarn.lock index e0506236a0bd..8d6999cf827d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3286,7 +3286,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/core@6.2.0", "@sentry/core@^6.2.0": +"@sentry/core@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.2.0.tgz#be1c33854fc94e1a4d867f7c2c311cf1cefb8ee9" integrity sha512-oTr2b25l+0bv/+d6IgMamPuGleWV7OgJb0NFfd+WZhw6UDRgr7CdEJy2gW6tK8SerwXgPHdn4ervxsT3WIBiXw== @@ -3346,7 +3346,7 @@ resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.2.0.tgz#ca020ff42913c6b9f88a9d0c375b5ee3965a2590" integrity sha512-vN4P/a+QqAuVfWFB9G3nQ7d6bgnM9jd/RLVi49owMuqvM24pv5mTQHUk2Hk4S3k7ConrHFl69E7xH6Dv5VpQnQ== -"@sentry/utils@6.2.0", "@sentry/utils@^6.2.0": +"@sentry/utils@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.2.0.tgz#39c81ad5ba92cec54d690e3fa8ea4e777d8e9c2b" integrity sha512-YToUC7xYf2E/pIluI7upYTlj8fKXOtdwoOBkcQZifHgX/dP+qDaHibbBFe5PyZwdmU2UiLnWFsBr0gjo0QFo1g== @@ -4620,32 +4620,11 @@ after@0.8.2: resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f" integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8= -agent-base@4, agent-base@^4.3.0: - version "4.3.0" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee" - integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg== - dependencies: - es6-promisify "^5.0.0" - -agent-base@5: +agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1: version "5.1.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c" integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g== -agent-base@6: - version "6.0.2" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" - integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== - dependencies: - debug "4" - -agent-base@~4.2.1: - version "4.2.1" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" - integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg== - dependencies: - es6-promisify "^5.0.0" - agentkeepalive@^3.4.1: version "3.5.2" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67" @@ -9777,18 +9756,6 @@ es6-object-assign@^1.1.0: resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= -es6-promise@^4.0.3: - version "4.2.8" - resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" - integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== - -es6-promisify@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" - integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM= - dependencies: - es6-promise "^4.0.3" - escalade@^3.0.1: version "3.0.2" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4" @@ -13099,7 +13066,7 @@ jest-environment-jsdom@^24.9.0: jest-util "^24.9.0" jsdom "^11.5.1" -"jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: +jest-environment-node@24, "jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3" integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA== From 57a7c94ea0ec5c80b17f602c3bd2982ea5be7768 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 15:45:56 +0100 Subject: [PATCH 14/26] docs: Add docs to browser and node inits --- packages/nextjs/src/browser.ts | 2 +- packages/nextjs/src/node.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index e35efc122ea1..00c08ccdad9a 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -2,7 +2,7 @@ import * as SentryBrowser from '@sentry/browser'; import { NextjsOptions } from './options'; -/** */ +/** Inits the Sentry NextJS SDK on the browser. */ export function init(options: NextjsOptions): any { SentryBrowser.init(options); } diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index 2195cea749e6..c2be86706759 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -2,7 +2,7 @@ import * as SentryNode from '@sentry/node'; import { NextjsOptions } from './options'; -/** */ +/** Inits the Sentry NextJS SDK on node. */ export function init(options: NextjsOptions): any { SentryNode.init(options); } From 0d4c93749abdaa482e68e7dc542587a928db20c2 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 17:08:43 +0100 Subject: [PATCH 15/26] ref: Import only `init` from @sentry/browser and @sentry/node --- packages/nextjs/src/browser.ts | 4 ++-- packages/nextjs/src/node.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index 00c08ccdad9a..487d3a850a65 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -1,10 +1,10 @@ -import * as SentryBrowser from '@sentry/browser'; +import { init as browserInit } from '@sentry/browser'; import { NextjsOptions } from './options'; /** Inits the Sentry NextJS SDK on the browser. */ export function init(options: NextjsOptions): any { - SentryBrowser.init(options); + browserInit(options); } export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index c2be86706759..77654e28391d 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -1,10 +1,10 @@ -import * as SentryNode from '@sentry/node'; +import { init as nodeInit } from '@sentry/node'; import { NextjsOptions } from './options'; /** Inits the Sentry NextJS SDK on node. */ export function init(options: NextjsOptions): any { - SentryNode.init(options); + nodeInit(options); } export * from '@sentry/minimal'; From 949a5f452da33e0ba3f454f562db2fb88d131b8c Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 17:10:26 +0100 Subject: [PATCH 16/26] build: Set exact versions for dependencies --- packages/nextjs/package.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index fa201226514a..da8c60f32b5a 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -17,14 +17,14 @@ "access": "public" }, "dependencies": { - "@sentry/browser": "^6.2.0", - "@sentry/node": "^6.2.0", - "@sentry/minimal": "^6.2.0" + "@sentry/browser": "6.2.0", + "@sentry/node": "6.2.0", + "@sentry/minimal": "6.2.0" }, "devDependencies": { - "@sentry/types": "^6.2.0", - "eslint": "^7.20.0", - "rimraf": "^3.0.2" + "@sentry/types": "6.2.0", + "eslint": "7.20.0", + "rimraf": "3.0.2" }, "scripts": { "build": "run-p build:es5 build:esm", From 0c75211bdef9bfff63e0497e4fa51f9a7dad2c46 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 17:24:57 +0100 Subject: [PATCH 17/26] build: Update package version --- packages/nextjs/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index da8c60f32b5a..b2241c05cf53 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -1,6 +1,6 @@ { "name": "@sentry/nextjs", - "version": "6.1.0", + "version": "6.2.0", "description": "Official Sentry SDK for Next.js", "repository": "git://github.com/getsentry/sentry-javascript.git", "homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/nextjs", From 8435adb9fad0c3b7f4c6e44348b6c3bd3595ce9a Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Wed, 24 Feb 2021 18:20:19 +0100 Subject: [PATCH 18/26] ref: Add MetadataBuilder and use it to build the metadata before SDKs are initialized --- packages/nextjs/.eslintrc.js | 1 + packages/nextjs/src/browser.ts | 4 +++- packages/nextjs/src/node.ts | 4 +++- packages/nextjs/src/options.ts | 42 ++++++++++++++++++++++++++++++++-- packages/nextjs/src/sdk.ts | 17 ++++---------- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/packages/nextjs/.eslintrc.js b/packages/nextjs/.eslintrc.js index 7b4b6d87a032..acae3b2df6bf 100644 --- a/packages/nextjs/.eslintrc.js +++ b/packages/nextjs/.eslintrc.js @@ -28,5 +28,6 @@ module.exports = { rules: { 'max-lines': 'off', '@sentry-internal/sdk/no-async-await': 'off', + 'jsdoc/require-jsdoc': 0, }, }; diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index 487d3a850a65..fb64382392cf 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -1,9 +1,11 @@ import { init as browserInit } from '@sentry/browser'; -import { NextjsOptions } from './options'; +import { MetadataBuilder, NextjsOptions } from './options'; /** Inits the Sentry NextJS SDK on the browser. */ export function init(options: NextjsOptions): any { + const metadataBuilder = new MetadataBuilder(options, 'browser'); + metadataBuilder.addSdkMetadata(); browserInit(options); } diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index 77654e28391d..a21e44f0fd95 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -1,9 +1,11 @@ import { init as nodeInit } from '@sentry/node'; -import { NextjsOptions } from './options'; +import { MetadataBuilder, NextjsOptions } from './options'; /** Inits the Sentry NextJS SDK on node. */ export function init(options: NextjsOptions): any { + const metadataBuilder = new MetadataBuilder(options, 'node'); + metadataBuilder.addSdkMetadata(); nodeInit(options); } diff --git a/packages/nextjs/src/options.ts b/packages/nextjs/src/options.ts index 84943373602b..4daf3dbcf351 100644 --- a/packages/nextjs/src/options.ts +++ b/packages/nextjs/src/options.ts @@ -1,7 +1,45 @@ -import { BrowserOptions } from '@sentry/browser'; +import { BrowserOptions, SDK_VERSION } from '@sentry/browser'; import { NodeOptions } from '@sentry/node'; -import { Options } from '@sentry/types'; +import { Options, SdkInfo } from '@sentry/types'; + +const SDK_NAME = 'sentry.javascript.nextjs'; +const PACKAGE_NAME_PREFIX = 'npm:@sentry/'; export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { // TODO: options for NextJS } + +/** + * A builder for the SDK metadata in the options for the SDK initialization. + */ +export class MetadataBuilder { + private _options: NextjsOptions; + private _packageName: string; + + constructor(options: NextjsOptions, packageName: string) { + this._options = options; + this._packageName = packageName; + } + + public addSdkMetadata(): void { + this._options._metadata = this._options._metadata || {}; + this._options._metadata.sdk = this._getSdkInfo(); + } + + private _getSdkInfo(): SdkInfo { + return { + name: SDK_NAME, + version: SDK_VERSION, + packages: [ + { + name: this._getPackageName(), + version: SDK_VERSION, + }, + ], + }; + } + + private _getPackageName(): string { + return PACKAGE_NAME_PREFIX + this._packageName; + } +} diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index c2369c60c74b..1279570fa8ce 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,6 +1,6 @@ -import { init as browserInit, SDK_VERSION } from '@sentry/browser'; +import { init as browserInit } from '@sentry/browser'; -import { NextjsOptions } from './options'; +import { MetadataBuilder, NextjsOptions } from './options'; /** * The Sentry NextJS SDK Client. @@ -9,16 +9,7 @@ import { NextjsOptions } from './options'; * */ export function init(options: NextjsOptions): void { - options._metadata = options._metadata || {}; - options._metadata.sdk = { - name: 'sentry.javascript.nextjs', - packages: [ - { - name: '', // TODO - version: SDK_VERSION, - }, - ], - version: SDK_VERSION, - }; + const metadataBuilder = new MetadataBuilder(options, 'nextjs'); + metadataBuilder.addSdkMetadata(); browserInit(options); } From 241ee5a1edf8c4687a673160b226415a85b8a053 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Thu, 25 Feb 2021 12:37:09 +0100 Subject: [PATCH 19/26] fix: Include all used SDKs in the event payload --- packages/nextjs/src/browser.ts | 2 +- packages/nextjs/src/node.ts | 2 +- packages/nextjs/src/options.ts | 24 ++++++++++++------------ packages/nextjs/src/sdk.ts | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index fb64382392cf..da49c2089482 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -4,7 +4,7 @@ import { MetadataBuilder, NextjsOptions } from './options'; /** Inits the Sentry NextJS SDK on the browser. */ export function init(options: NextjsOptions): any { - const metadataBuilder = new MetadataBuilder(options, 'browser'); + const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'browser']); metadataBuilder.addSdkMetadata(); browserInit(options); } diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index a21e44f0fd95..aefb8d6557a1 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -4,7 +4,7 @@ import { MetadataBuilder, NextjsOptions } from './options'; /** Inits the Sentry NextJS SDK on node. */ export function init(options: NextjsOptions): any { - const metadataBuilder = new MetadataBuilder(options, 'node'); + const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']); metadataBuilder.addSdkMetadata(); nodeInit(options); } diff --git a/packages/nextjs/src/options.ts b/packages/nextjs/src/options.ts index 4daf3dbcf351..2d7424db17aa 100644 --- a/packages/nextjs/src/options.ts +++ b/packages/nextjs/src/options.ts @@ -1,6 +1,6 @@ import { BrowserOptions, SDK_VERSION } from '@sentry/browser'; import { NodeOptions } from '@sentry/node'; -import { Options, SdkInfo } from '@sentry/types'; +import { Options, Package, SdkInfo } from '@sentry/types'; const SDK_NAME = 'sentry.javascript.nextjs'; const PACKAGE_NAME_PREFIX = 'npm:@sentry/'; @@ -14,11 +14,11 @@ export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { */ export class MetadataBuilder { private _options: NextjsOptions; - private _packageName: string; + private _packageNames: string[]; - constructor(options: NextjsOptions, packageName: string) { + constructor(options: NextjsOptions, packages: string[]) { this._options = options; - this._packageName = packageName; + this._packageNames = packages; } public addSdkMetadata(): void { @@ -30,16 +30,16 @@ export class MetadataBuilder { return { name: SDK_NAME, version: SDK_VERSION, - packages: [ - { - name: this._getPackageName(), - version: SDK_VERSION, - }, - ], + packages: this._getPackages(), }; } - private _getPackageName(): string { - return PACKAGE_NAME_PREFIX + this._packageName; + private _getPackages(): Package[] { + return this._packageNames.map((pkgName: string) => { + return { + name: PACKAGE_NAME_PREFIX + pkgName, + version: SDK_VERSION, + }; + }); } } diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index 1279570fa8ce..0cbfbc1e472f 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -9,7 +9,7 @@ import { MetadataBuilder, NextjsOptions } from './options'; * */ export function init(options: NextjsOptions): void { - const metadataBuilder = new MetadataBuilder(options, 'nextjs'); + const metadataBuilder = new MetadataBuilder(options, ['nextjs']); metadataBuilder.addSdkMetadata(); browserInit(options); } From b53ed5a3cad51b59dfd5b406671a31416b0ecd14 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Fri, 26 Feb 2021 11:16:54 +0100 Subject: [PATCH 20/26] ref: Split options into modules and use @sentry/react in the browser --- packages/nextjs/package.json | 5 +- packages/nextjs/src/browser.ts | 11 +- packages/nextjs/src/node.ts | 3 +- packages/nextjs/src/sdk.ts | 7 +- .../{options.ts => utils/metadataBuilder.ts} | 11 +- packages/nextjs/src/utils/nextjsOptions.ts | 7 ++ yarn.lock | 111 ++++++++++++------ 7 files changed, 104 insertions(+), 51 deletions(-) rename packages/nextjs/src/{options.ts => utils/metadataBuilder.ts} (76%) create mode 100644 packages/nextjs/src/utils/nextjsOptions.ts diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index b2241c05cf53..60198a3935a2 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -17,9 +17,10 @@ "access": "public" }, "dependencies": { - "@sentry/browser": "6.2.0", + "@sentry/core": "6.2.0", + "@sentry/minimal": "6.2.0", "@sentry/node": "6.2.0", - "@sentry/minimal": "6.2.0" + "@sentry/react": "6.2.0" }, "devDependencies": { "@sentry/types": "6.2.0", diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index da49c2089482..f9d487f202b9 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -1,12 +1,13 @@ -import { init as browserInit } from '@sentry/browser'; +import { init as reactInit } from '@sentry/react'; -import { MetadataBuilder, NextjsOptions } from './options'; +import { MetadataBuilder } from './utils/metadataBuilder'; +import { NextjsOptions } from './utils/nextjsOptions'; -/** Inits the Sentry NextJS SDK on the browser. */ +/** Inits the Sentry NextJS SDK on the browser with the React SDK. */ export function init(options: NextjsOptions): any { - const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'browser']); + const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']); metadataBuilder.addSdkMetadata(); - browserInit(options); + reactInit(options); } export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index aefb8d6557a1..f5d6d582513e 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -1,6 +1,7 @@ import { init as nodeInit } from '@sentry/node'; -import { MetadataBuilder, NextjsOptions } from './options'; +import { MetadataBuilder } from './utils/metadataBuilder'; +import { NextjsOptions } from './utils/nextjsOptions'; /** Inits the Sentry NextJS SDK on node. */ export function init(options: NextjsOptions): any { diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index 0cbfbc1e472f..2c490d30dd8c 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,6 +1,7 @@ -import { init as browserInit } from '@sentry/browser'; +import { init as reactInit } from '@sentry/react'; -import { MetadataBuilder, NextjsOptions } from './options'; +import { MetadataBuilder } from './utils/metadataBuilder'; +import { NextjsOptions } from './utils/nextjsOptions'; /** * The Sentry NextJS SDK Client. @@ -11,5 +12,5 @@ import { MetadataBuilder, NextjsOptions } from './options'; export function init(options: NextjsOptions): void { const metadataBuilder = new MetadataBuilder(options, ['nextjs']); metadataBuilder.addSdkMetadata(); - browserInit(options); + reactInit(options); } diff --git a/packages/nextjs/src/options.ts b/packages/nextjs/src/utils/metadataBuilder.ts similarity index 76% rename from packages/nextjs/src/options.ts rename to packages/nextjs/src/utils/metadataBuilder.ts index 2d7424db17aa..a20ca13b5700 100644 --- a/packages/nextjs/src/options.ts +++ b/packages/nextjs/src/utils/metadataBuilder.ts @@ -1,14 +1,11 @@ -import { BrowserOptions, SDK_VERSION } from '@sentry/browser'; -import { NodeOptions } from '@sentry/node'; -import { Options, Package, SdkInfo } from '@sentry/types'; +import { SDK_VERSION } from '@sentry/core'; +import { Package, SdkInfo } from '@sentry/types'; + +import { NextjsOptions } from './nextjsOptions'; const SDK_NAME = 'sentry.javascript.nextjs'; const PACKAGE_NAME_PREFIX = 'npm:@sentry/'; -export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { - // TODO: options for NextJS -} - /** * A builder for the SDK metadata in the options for the SDK initialization. */ diff --git a/packages/nextjs/src/utils/nextjsOptions.ts b/packages/nextjs/src/utils/nextjsOptions.ts new file mode 100644 index 000000000000..8dd227efbd3d --- /dev/null +++ b/packages/nextjs/src/utils/nextjsOptions.ts @@ -0,0 +1,7 @@ +import { NodeOptions } from '@sentry/node'; +import { BrowserOptions } from '@sentry/react'; +import { Options } from '@sentry/types'; + +export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { + // TODO: options for NextJS +} diff --git a/yarn.lock b/yarn.lock index 8d6999cf827d..3ba85b22a6cb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3276,7 +3276,7 @@ resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570" integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA= -"@sentry/browser@^6.2.0": +"@sentry/browser@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.2.0.tgz#4113a92bc82f55e63f30cb16a94f717bd0b95817" integrity sha512-4r3paHcHXLemj471BtNDhUs2kvJxk5XDRplz1dbC/LHXN5PWEXP4anhGILxOlxqi4y33r53PIZu3xXFjznaVZA== @@ -3286,7 +3286,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/core@6.2.0": +"@sentry/core@6.2.0", "@sentry/core@^6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.2.0.tgz#be1c33854fc94e1a4d867f7c2c311cf1cefb8ee9" integrity sha512-oTr2b25l+0bv/+d6IgMamPuGleWV7OgJb0NFfd+WZhw6UDRgr7CdEJy2gW6tK8SerwXgPHdn4ervxsT3WIBiXw== @@ -3306,7 +3306,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/minimal@6.2.0", "@sentry/minimal@^6.2.0": +"@sentry/minimal@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.2.0.tgz#718b70babb55912eeb38babaf7823d1bcdd77d1e" integrity sha512-haxsx8/ZafhZUaGeeMtY7bJt9HbDlqeiaXrRMp1CxGtd0ZRQwHt60imEjl6IH1I73SEWxNfqScGsX2s3HzztMg== @@ -3315,7 +3315,7 @@ "@sentry/types" "6.2.0" tslib "^1.9.3" -"@sentry/node@^6.2.0": +"@sentry/node@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/node/-/node-6.2.0.tgz#4c1860822a4a73d24242e5254124bd3bf9028d52" integrity sha512-02lXk+56tPA3lWTvNLMGorp77wUVti8wOs+TlYARkJ+N+16dwqEBSBTy3hCDxlxriB+qHchSIS+ovPGi6WNiYA== @@ -3330,6 +3330,18 @@ lru_map "^0.3.3" tslib "^1.9.3" +"@sentry/react@6.2.0": + version "6.2.0" + resolved "https://registry.yarnpkg.com/@sentry/react/-/react-6.2.0.tgz#bf46c38762554f246ca9dec527e6a5b3168fb0b0" + integrity sha512-Jf3s7om1iLpApkN26O7c3Ult3lS91ekZNC4WKtcPb6b+KOBQ36sB0d1KhL3hGZ55UKLmgZu3jn2hd7bJ9EY3yA== + dependencies: + "@sentry/browser" "6.2.0" + "@sentry/minimal" "6.2.0" + "@sentry/types" "6.2.0" + "@sentry/utils" "6.2.0" + hoist-non-react-statics "^3.3.2" + tslib "^1.9.3" + "@sentry/tracing@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/tracing/-/tracing-6.2.0.tgz#76c17e5dd3f1e61c8a4e3bd090f904a63d674765" @@ -3341,7 +3353,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/types@6.2.0", "@sentry/types@^6.2.0": +"@sentry/types@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.2.0.tgz#ca020ff42913c6b9f88a9d0c375b5ee3965a2590" integrity sha512-vN4P/a+QqAuVfWFB9G3nQ7d6bgnM9jd/RLVi49owMuqvM24pv5mTQHUk2Hk4S3k7ConrHFl69E7xH6Dv5VpQnQ== @@ -4620,11 +4632,32 @@ after@0.8.2: resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f" integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8= -agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1: +agent-base@4, agent-base@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee" + integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg== + dependencies: + es6-promisify "^5.0.0" + +agent-base@5: version "5.1.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c" integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g== +agent-base@6: + version "6.0.2" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" + integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== + dependencies: + debug "4" + +agent-base@~4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" + integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg== + dependencies: + es6-promisify "^5.0.0" + agentkeepalive@^3.4.1: version "3.5.2" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67" @@ -9756,6 +9789,18 @@ es6-object-assign@^1.1.0: resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= +es6-promise@^4.0.3: + version "4.2.8" + resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" + integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== + +es6-promisify@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" + integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM= + dependencies: + es6-promise "^4.0.3" + escalade@^3.0.1: version "3.0.2" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4" @@ -9967,25 +10012,26 @@ eslint-visitor-keys@^2.0.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz#21fdc8fbcd9c795cc0321f0563702095751511a8" integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ== -eslint@7.6.0: - version "7.6.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.6.0.tgz#522d67cfaea09724d96949c70e7a0550614d64d6" - integrity sha512-QlAManNtqr7sozWm5TF4wIH9gmUm2hE3vNRUvyoYAa4y1l5/jxD/PQStEjBMQtCqZmSep8UxrcecI60hOpe61w== +eslint@7.20.0: + version "7.20.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.20.0.tgz#db07c4ca4eda2e2316e7aa57ac7fc91ec550bdc7" + integrity sha512-qGi0CTcOGP2OtCQBgWZlQjcTuP0XkIpYFj25XtRTQSHC+umNnp7UMshr2G8SLsRFYDdAPFeHOsiteadmMH02Yw== dependencies: - "@babel/code-frame" "^7.0.0" + "@babel/code-frame" "7.12.11" + "@eslint/eslintrc" "^0.3.0" ajv "^6.10.0" chalk "^4.0.0" cross-spawn "^7.0.2" debug "^4.0.1" doctrine "^3.0.0" enquirer "^2.3.5" - eslint-scope "^5.1.0" + eslint-scope "^5.1.1" eslint-utils "^2.1.0" - eslint-visitor-keys "^1.3.0" - espree "^7.2.0" - esquery "^1.2.0" + eslint-visitor-keys "^2.0.0" + espree "^7.3.1" + esquery "^1.4.0" esutils "^2.0.2" - file-entry-cache "^5.0.1" + file-entry-cache "^6.0.0" functional-red-black-tree "^1.0.1" glob-parent "^5.0.0" globals "^12.1.0" @@ -9996,7 +10042,7 @@ eslint@7.6.0: js-yaml "^3.13.1" json-stable-stringify-without-jsonify "^1.0.1" levn "^0.4.1" - lodash "^4.17.19" + lodash "^4.17.20" minimatch "^3.0.4" natural-compare "^1.4.0" optionator "^0.9.1" @@ -10005,30 +10051,29 @@ eslint@7.6.0: semver "^7.2.1" strip-ansi "^6.0.0" strip-json-comments "^3.1.0" - table "^5.2.3" + table "^6.0.4" text-table "^0.2.0" v8-compile-cache "^2.0.3" -eslint@^7.20.0: - version "7.20.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.20.0.tgz#db07c4ca4eda2e2316e7aa57ac7fc91ec550bdc7" - integrity sha512-qGi0CTcOGP2OtCQBgWZlQjcTuP0XkIpYFj25XtRTQSHC+umNnp7UMshr2G8SLsRFYDdAPFeHOsiteadmMH02Yw== +eslint@7.6.0: + version "7.6.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.6.0.tgz#522d67cfaea09724d96949c70e7a0550614d64d6" + integrity sha512-QlAManNtqr7sozWm5TF4wIH9gmUm2hE3vNRUvyoYAa4y1l5/jxD/PQStEjBMQtCqZmSep8UxrcecI60hOpe61w== dependencies: - "@babel/code-frame" "7.12.11" - "@eslint/eslintrc" "^0.3.0" + "@babel/code-frame" "^7.0.0" ajv "^6.10.0" chalk "^4.0.0" cross-spawn "^7.0.2" debug "^4.0.1" doctrine "^3.0.0" enquirer "^2.3.5" - eslint-scope "^5.1.1" + eslint-scope "^5.1.0" eslint-utils "^2.1.0" - eslint-visitor-keys "^2.0.0" - espree "^7.3.1" - esquery "^1.4.0" + eslint-visitor-keys "^1.3.0" + espree "^7.2.0" + esquery "^1.2.0" esutils "^2.0.2" - file-entry-cache "^6.0.0" + file-entry-cache "^5.0.1" functional-red-black-tree "^1.0.1" glob-parent "^5.0.0" globals "^12.1.0" @@ -10039,7 +10084,7 @@ eslint@^7.20.0: js-yaml "^3.13.1" json-stable-stringify-without-jsonify "^1.0.1" levn "^0.4.1" - lodash "^4.17.20" + lodash "^4.17.19" minimatch "^3.0.4" natural-compare "^1.4.0" optionator "^0.9.1" @@ -10048,7 +10093,7 @@ eslint@^7.20.0: semver "^7.2.1" strip-ansi "^6.0.0" strip-json-comments "^3.1.0" - table "^6.0.4" + table "^5.2.3" text-table "^0.2.0" v8-compile-cache "^2.0.3" @@ -13066,7 +13111,7 @@ jest-environment-jsdom@^24.9.0: jest-util "^24.9.0" jsdom "^11.5.1" -jest-environment-node@24, "jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: +"jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3" integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA== @@ -17947,7 +17992,7 @@ rimraf@2, rimraf@2.6.3, rimraf@^2.2.8, rimraf@^2.3.4, rimraf@^2.4.3, rimraf@^2.4 dependencies: glob "^7.1.3" -rimraf@^3.0.0, rimraf@^3.0.1, rimraf@^3.0.2: +rimraf@3.0.2, rimraf@^3.0.0, rimraf@^3.0.1, rimraf@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a" integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA== From c89442329746bc03602672ffd3e5b188d628718f Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Fri, 26 Feb 2021 12:16:15 +0100 Subject: [PATCH 21/26] feat: Only initialize the NextJS SDK in production environments --- packages/nextjs/src/browser.ts | 9 +++++- packages/nextjs/src/node.ts | 9 +++++- packages/nextjs/src/sdk.ts | 9 +++++- packages/nextjs/src/utils/initDecider.ts | 20 ++++++++++++ yarn.lock | 39 ++---------------------- 5 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 packages/nextjs/src/utils/initDecider.ts diff --git a/packages/nextjs/src/browser.ts b/packages/nextjs/src/browser.ts index f9d487f202b9..7c431144f3fd 100644 --- a/packages/nextjs/src/browser.ts +++ b/packages/nextjs/src/browser.ts @@ -1,5 +1,6 @@ import { init as reactInit } from '@sentry/react'; +import { InitDecider } from './utils/initDecider'; import { MetadataBuilder } from './utils/metadataBuilder'; import { NextjsOptions } from './utils/nextjsOptions'; @@ -7,7 +8,13 @@ import { NextjsOptions } from './utils/nextjsOptions'; export function init(options: NextjsOptions): any { const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']); metadataBuilder.addSdkMetadata(); - reactInit(options); + const initDecider = new InitDecider(options); + if (initDecider.shouldInitSentry()) { + reactInit(options); + } else { + // eslint-disable-next-line no-console + console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.'); + } } export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/node.ts b/packages/nextjs/src/node.ts index f5d6d582513e..d17a6b2b5cd2 100644 --- a/packages/nextjs/src/node.ts +++ b/packages/nextjs/src/node.ts @@ -1,5 +1,6 @@ import { init as nodeInit } from '@sentry/node'; +import { InitDecider } from './utils/initDecider'; import { MetadataBuilder } from './utils/metadataBuilder'; import { NextjsOptions } from './utils/nextjsOptions'; @@ -7,7 +8,13 @@ import { NextjsOptions } from './utils/nextjsOptions'; export function init(options: NextjsOptions): any { const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']); metadataBuilder.addSdkMetadata(); - nodeInit(options); + const initDecider = new InitDecider(options); + if (initDecider.shouldInitSentry()) { + nodeInit(options); + } else { + // eslint-disable-next-line no-console + console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.'); + } } export * from '@sentry/minimal'; diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index 2c490d30dd8c..880a45edc4f2 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,5 +1,6 @@ import { init as reactInit } from '@sentry/react'; +import { InitDecider } from './utils/initDecider'; import { MetadataBuilder } from './utils/metadataBuilder'; import { NextjsOptions } from './utils/nextjsOptions'; @@ -12,5 +13,11 @@ import { NextjsOptions } from './utils/nextjsOptions'; export function init(options: NextjsOptions): void { const metadataBuilder = new MetadataBuilder(options, ['nextjs']); metadataBuilder.addSdkMetadata(); - reactInit(options); + const initDecider = new InitDecider(options); + if (initDecider.shouldInitSentry()) { + reactInit(options); + } else { + // eslint-disable-next-line no-console + console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.'); + } } diff --git a/packages/nextjs/src/utils/initDecider.ts b/packages/nextjs/src/utils/initDecider.ts new file mode 100644 index 000000000000..2763c6d04f94 --- /dev/null +++ b/packages/nextjs/src/utils/initDecider.ts @@ -0,0 +1,20 @@ +import { NextjsOptions } from './nextjsOptions'; + +export class InitDecider { + private _options: NextjsOptions; + + constructor(options: NextjsOptions) { + this._options = options; + } + + public shouldInitSentry(): boolean { + if (this._options) { + // noop + } + return this._isInProduction(); + } + + private _isInProduction(): boolean { + return process.env.NODE_ENV !== undefined && process.env.NODE_ENV === 'production'; + } +} diff --git a/yarn.lock b/yarn.lock index 3ba85b22a6cb..afc2984a37f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3286,7 +3286,7 @@ "@sentry/utils" "6.2.0" tslib "^1.9.3" -"@sentry/core@6.2.0", "@sentry/core@^6.2.0": +"@sentry/core@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.2.0.tgz#be1c33854fc94e1a4d867f7c2c311cf1cefb8ee9" integrity sha512-oTr2b25l+0bv/+d6IgMamPuGleWV7OgJb0NFfd+WZhw6UDRgr7CdEJy2gW6tK8SerwXgPHdn4ervxsT3WIBiXw== @@ -4632,32 +4632,11 @@ after@0.8.2: resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f" integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8= -agent-base@4, agent-base@^4.3.0: - version "4.3.0" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee" - integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg== - dependencies: - es6-promisify "^5.0.0" - -agent-base@5: +agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1: version "5.1.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c" integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g== -agent-base@6: - version "6.0.2" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" - integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== - dependencies: - debug "4" - -agent-base@~4.2.1: - version "4.2.1" - resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" - integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg== - dependencies: - es6-promisify "^5.0.0" - agentkeepalive@^3.4.1: version "3.5.2" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67" @@ -9789,18 +9768,6 @@ es6-object-assign@^1.1.0: resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= -es6-promise@^4.0.3: - version "4.2.8" - resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" - integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== - -es6-promisify@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" - integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM= - dependencies: - es6-promise "^4.0.3" - escalade@^3.0.1: version "3.0.2" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4" @@ -13111,7 +13078,7 @@ jest-environment-jsdom@^24.9.0: jest-util "^24.9.0" jsdom "^11.5.1" -"jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: +jest-environment-node@24, "jest-environment-node@>=24 && <=26", jest-environment-node@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3" integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA== From 97ba3a0df461ca9d00cad2d5127b9f86b7889c52 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Fri, 26 Feb 2021 12:58:00 +0100 Subject: [PATCH 22/26] feat: Add `forceInit` option to enable using Sentry in development environments --- packages/nextjs/src/utils/initDecider.ts | 25 ++++++++++++++++++---- packages/nextjs/src/utils/nextjsOptions.ts | 6 +++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/src/utils/initDecider.ts b/packages/nextjs/src/utils/initDecider.ts index 2763c6d04f94..1bf8653b45da 100644 --- a/packages/nextjs/src/utils/initDecider.ts +++ b/packages/nextjs/src/utils/initDecider.ts @@ -7,14 +7,31 @@ export class InitDecider { this._options = options; } + /** + * Returns a boolean representing whether the NextJS SDK should be initialised. + * + * The SDK should be initialised if the `forceInit` option is set to true. + * `forceInit` is optional, so if it isn't set or is set to false, the SDK will only + * be initialised in a production environment. + */ public shouldInitSentry(): boolean { - if (this._options) { - // noop + if (this._isForceInit() || this._isProdEnv()) { + return true; } - return this._isInProduction(); + return false; } - private _isInProduction(): boolean { + /** + * Returns true if the option `forceInit` is true, and false otherwise. + */ + private _isForceInit(): boolean { + return this._options.forceInit || false; + } + + /** + * Returns whether the environment is a production environment. + */ + private _isProdEnv(): boolean { return process.env.NODE_ENV !== undefined && process.env.NODE_ENV === 'production'; } } diff --git a/packages/nextjs/src/utils/nextjsOptions.ts b/packages/nextjs/src/utils/nextjsOptions.ts index 8dd227efbd3d..959b3df12d7c 100644 --- a/packages/nextjs/src/utils/nextjsOptions.ts +++ b/packages/nextjs/src/utils/nextjsOptions.ts @@ -3,5 +3,9 @@ import { BrowserOptions } from '@sentry/react'; import { Options } from '@sentry/types'; export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { - // TODO: options for NextJS + /** + * A flag forcing the initialization of the SDK. + * By default, the SDK is only initialised in production. + */ + forceInit?: boolean; } From d479df6e74ae7e5eab38e91f31f4e12991f3daea Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Fri, 26 Feb 2021 15:40:50 +0100 Subject: [PATCH 23/26] test: Add test for initDecider --- packages/nextjs/test/initDecider.test.ts | 60 ++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 packages/nextjs/test/initDecider.test.ts diff --git a/packages/nextjs/test/initDecider.test.ts b/packages/nextjs/test/initDecider.test.ts new file mode 100644 index 000000000000..2e81dd26ecf1 --- /dev/null +++ b/packages/nextjs/test/initDecider.test.ts @@ -0,0 +1,60 @@ +import { InitDecider } from '../src/utils/initDecider'; +import { NextjsOptions } from '../src/utils/nextjsOptions'; + +function setDevEnv(): void { + process.env.NODE_ENV = 'development'; +} + +function setProdEnv(): void { + process.env.NODE_ENV = 'production'; +} + +function getEmptyOptions(): NextjsOptions { + return {}; +} + +function getForceInitTrueOptions(): NextjsOptions { + return { forceInit: true }; +} + +function getForceInitFalseOptions(): NextjsOptions { + return { forceInit: false }; +} + +describe('decide initialization in development', () => { + beforeEach(setDevEnv); + + test('without options', () => { + const initDecider = new InitDecider(getEmptyOptions()); + expect(initDecider.shouldInitSentry()).toBeFalsy(); + }); + + test('without forcing init', () => { + const initDecider = new InitDecider(getForceInitFalseOptions()); + expect(initDecider.shouldInitSentry()).toBeFalsy(); + }); + + test('forcing init', () => { + const initDecider = new InitDecider(getForceInitTrueOptions()); + expect(initDecider.shouldInitSentry()).toBeTruthy(); + }); +}); + +describe('decide initialization in production', () => { + beforeEach(setProdEnv); + + test('without options', () => { + const initDecider = new InitDecider(getEmptyOptions()); + expect(initDecider.shouldInitSentry()).toBeTruthy(); + }); + + test('without forcing init', () => { + const initDecider = new InitDecider(getForceInitFalseOptions()); + expect(initDecider.shouldInitSentry()).toBeTruthy(); + }); + + test('forcing init', () => { + const initDecider = new InitDecider(getForceInitTrueOptions()); + expect(initDecider.shouldInitSentry()).toBeTruthy(); + }); +}); From ef013b570ed3c2427fe0b3292312ed1e2b9d15ee Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 1 Mar 2021 16:17:16 +0100 Subject: [PATCH 24/26] ref: Use `dev` flag in the NextJS options instead of `forceInit` --- packages/nextjs/src/utils/initDecider.ts | 12 +++++------ packages/nextjs/src/utils/nextjsOptions.ts | 7 ++++--- packages/nextjs/test/initDecider.test.ts | 24 +++++++++++----------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/nextjs/src/utils/initDecider.ts b/packages/nextjs/src/utils/initDecider.ts index 1bf8653b45da..eca6ed1b9382 100644 --- a/packages/nextjs/src/utils/initDecider.ts +++ b/packages/nextjs/src/utils/initDecider.ts @@ -10,22 +10,22 @@ export class InitDecider { /** * Returns a boolean representing whether the NextJS SDK should be initialised. * - * The SDK should be initialised if the `forceInit` option is set to true. - * `forceInit` is optional, so if it isn't set or is set to false, the SDK will only + * The SDK should be initialised if the `dev` option is set to true. + * `dev` is optional, so if it isn't set or is set to false, the SDK will only * be initialised in a production environment. */ public shouldInitSentry(): boolean { - if (this._isForceInit() || this._isProdEnv()) { + if (this._isEnabledInDev() || this._isProdEnv()) { return true; } return false; } /** - * Returns true if the option `forceInit` is true, and false otherwise. + * Returns true if the option `dev` is true, and false otherwise. */ - private _isForceInit(): boolean { - return this._options.forceInit || false; + private _isEnabledInDev(): boolean { + return this._options.dev || false; } /** diff --git a/packages/nextjs/src/utils/nextjsOptions.ts b/packages/nextjs/src/utils/nextjsOptions.ts index 959b3df12d7c..b9eefefbfb20 100644 --- a/packages/nextjs/src/utils/nextjsOptions.ts +++ b/packages/nextjs/src/utils/nextjsOptions.ts @@ -4,8 +4,9 @@ import { Options } from '@sentry/types'; export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { /** - * A flag forcing the initialization of the SDK. - * By default, the SDK is only initialised in production. + * A flag enabling the initialization of the SDK in development and other + * non-production environments. By default, the SDK is only initialised in + * production. */ - forceInit?: boolean; + dev?: boolean; } diff --git a/packages/nextjs/test/initDecider.test.ts b/packages/nextjs/test/initDecider.test.ts index 2e81dd26ecf1..eaafa7ef3397 100644 --- a/packages/nextjs/test/initDecider.test.ts +++ b/packages/nextjs/test/initDecider.test.ts @@ -13,12 +13,12 @@ function getEmptyOptions(): NextjsOptions { return {}; } -function getForceInitTrueOptions(): NextjsOptions { - return { forceInit: true }; +function getDevTrueOptions(): NextjsOptions { + return { dev: true }; } -function getForceInitFalseOptions(): NextjsOptions { - return { forceInit: false }; +function getDevFalseOptions(): NextjsOptions { + return { dev: false }; } describe('decide initialization in development', () => { @@ -29,13 +29,13 @@ describe('decide initialization in development', () => { expect(initDecider.shouldInitSentry()).toBeFalsy(); }); - test('without forcing init', () => { - const initDecider = new InitDecider(getForceInitFalseOptions()); + test('without development', () => { + const initDecider = new InitDecider(getDevFalseOptions()); expect(initDecider.shouldInitSentry()).toBeFalsy(); }); - test('forcing init', () => { - const initDecider = new InitDecider(getForceInitTrueOptions()); + test('with development', () => { + const initDecider = new InitDecider(getDevTrueOptions()); expect(initDecider.shouldInitSentry()).toBeTruthy(); }); }); @@ -48,13 +48,13 @@ describe('decide initialization in production', () => { expect(initDecider.shouldInitSentry()).toBeTruthy(); }); - test('without forcing init', () => { - const initDecider = new InitDecider(getForceInitFalseOptions()); + test('without development', () => { + const initDecider = new InitDecider(getDevFalseOptions()); expect(initDecider.shouldInitSentry()).toBeTruthy(); }); - test('forcing init', () => { - const initDecider = new InitDecider(getForceInitTrueOptions()); + test('with development', () => { + const initDecider = new InitDecider(getDevTrueOptions()); expect(initDecider.shouldInitSentry()).toBeTruthy(); }); }); From 5b43edb07b60f70cfbb4c0e9aaf66e91a4107eea Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 1 Mar 2021 16:41:36 +0100 Subject: [PATCH 25/26] build: Remove empty line from `yarn.lock` --- yarn.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index 6d42999d9883..afc2984a37f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3276,7 +3276,6 @@ resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570" integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA= - "@sentry/browser@6.2.0": version "6.2.0" resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.2.0.tgz#4113a92bc82f55e63f30cb16a94f717bd0b95817" From 7efbd9ac866483eb0b7ca6c3754f82f30c962b91 Mon Sep 17 00:00:00 2001 From: iker-barriocanal <32816711+iker-barriocanal@users.noreply.github.com> Date: Mon, 1 Mar 2021 16:44:02 +0100 Subject: [PATCH 26/26] ref: Delete options.ts This file was supposes to be deleted in a prior refactor, but it was messed up in a merge. --- packages/nextjs/src/options.ts | 45 ---------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 packages/nextjs/src/options.ts diff --git a/packages/nextjs/src/options.ts b/packages/nextjs/src/options.ts deleted file mode 100644 index 2d7424db17aa..000000000000 --- a/packages/nextjs/src/options.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { BrowserOptions, SDK_VERSION } from '@sentry/browser'; -import { NodeOptions } from '@sentry/node'; -import { Options, Package, SdkInfo } from '@sentry/types'; - -const SDK_NAME = 'sentry.javascript.nextjs'; -const PACKAGE_NAME_PREFIX = 'npm:@sentry/'; - -export interface NextjsOptions extends Options, BrowserOptions, NodeOptions { - // TODO: options for NextJS -} - -/** - * A builder for the SDK metadata in the options for the SDK initialization. - */ -export class MetadataBuilder { - private _options: NextjsOptions; - private _packageNames: string[]; - - constructor(options: NextjsOptions, packages: string[]) { - this._options = options; - this._packageNames = packages; - } - - public addSdkMetadata(): void { - this._options._metadata = this._options._metadata || {}; - this._options._metadata.sdk = this._getSdkInfo(); - } - - private _getSdkInfo(): SdkInfo { - return { - name: SDK_NAME, - version: SDK_VERSION, - packages: this._getPackages(), - }; - } - - private _getPackages(): Package[] { - return this._packageNames.map((pkgName: string) => { - return { - name: PACKAGE_NAME_PREFIX + pkgName, - version: SDK_VERSION, - }; - }); - } -}