Skip to content

Commit 3cbc44c

Browse files
committed
Add published distinction
1 parent 495d006 commit 3cbc44c

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

.config/rollup.dist.config.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
isBuiltin,
3030
normalizeId
3131
} from '../scripts/utils/packages.js'
32+
import { envAsBoolean } from '@socketsecurity/registry/lib/env'
3233

3334
const {
3435
BABEL_RUNTIME,
@@ -55,6 +56,10 @@ const editablePkgJson = readPackageJsonSync(rootPath, { editable: true })
5556

5657
const processEnvTapRegExp =
5758
/\bprocess\.env(?:\.TAP|\[['"]TAP['"]\])(\s*\?[^:]+:\s*)?/g
59+
const processEnvSocketIsPublishedRegExp =
60+
/\bprocess\.env(?:\.SOCKET_IS_PUBLISHED|\[['"]SOCKET_IS_PUBLISHED['"]\])/g
61+
const processEnvSocketCliVersionRegExp =
62+
/\bprocess\.env(?:\.SOCKET_CLI_VERSION|\[['"]SOCKET_CLI_VERSION['"]\])/g
5863

5964
function createStubCode(relFilepath) {
6065
return `'use strict'\n\nmodule.exports = require('${relFilepath}')\n`
@@ -147,6 +152,7 @@ function versionBanner(_chunk) {
147152
var SOCKET_CLI_GIT_HASH = "${gitHash}"
148153
var SOCKET_CLI_BUILD_RNG = "${rng}"
149154
var SOCKET_CLI_VERSION = "${pkgJsonVersion}:${gitHash}:${rng}"
155+
var SOCKET_PUB = ${envAsBoolean(process.env['SOCKET_IS_PUBLISHED'])}
150156
`.trim().split('\n').map(s => s.trim()).join('\n')
151157
}
152158

@@ -231,6 +237,19 @@ export default () => {
231237
find: processEnvTapRegExp,
232238
replace: (_match, ternary) => (ternary ? '' : 'false')
233239
}),
240+
// Replace `process.env.SOCKET_IS_PUBLISHED` with a boolean
241+
socketModifyPlugin({
242+
find: processEnvSocketIsPublishedRegExp,
243+
// Note: these are going to be bools in JS, not strings
244+
replace: () => (envAsBoolean(process.env['SOCKET_IS_PUBLISHED']) ? 'true' : 'false')
245+
}),
246+
// Replace `process.env.SOCKET_CLI_VERSION` with var ref that rollup
247+
// adds to the top of each file.
248+
socketModifyPlugin({
249+
find: processEnvSocketCliVersionRegExp,
250+
replace: 'SOCKET_CLI_VERSION'
251+
}),
252+
234253
{
235254
generateBundle(_options, bundle) {
236255
for (const basename of Object.keys(bundle)) {

.github/workflows/provenance.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
scope: "@socketsecurity"
2222
- run: npm install -g npm@latest
2323
- run: npm ci
24-
- run: npm run build:dist
24+
- run: SOCKET_IS_PUBLISHED=1 npm run build:dist
2525
- run: npm publish --provenance --access public
2626
env:
2727
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Constants = Omit<
5757
readonly ENV: ENV
5858
readonly DIST_TYPE: 'module-sync' | 'require'
5959
readonly IPC: IPC
60+
readonly IS_PUBLISHED: boolean
6061
readonly LOCK_EXT: '.lock'
6162
readonly MODULE_SYNC: 'module-sync'
6263
readonly NPM_REGISTRY_URL: 'https://registry.npmjs.org'
@@ -95,6 +96,7 @@ const BUN = 'bun'
9596
const CVE_ALERT_PROPS_FIRST_PATCHED_VERSION_IDENTIFIER =
9697
'firstPatchedVersionIdentifier'
9798
const CVE_ALERT_PROPS_VULNERABLE_VERSION_RANGE = 'vulnerableVersionRange'
99+
const IS_PUBLISHED = process.env['SOCKET_IS_PUBLISHED']
98100
const LOCK_EXT = '.lock'
99101
const MODULE_SYNC = 'module-sync'
100102
const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
@@ -178,6 +180,7 @@ const constants = <Constants>createConstantsObject(
178180
// Lazily defined values are initialized as `undefined` to keep their key order.
179181
DIST_TYPE: undefined,
180182
ENV: undefined,
183+
IS_PUBLISHED,
181184
LOCK_EXT,
182185
MODULE_SYNC,
183186
NPM_REGISTRY_URL,

src/utils/initialize-sentry.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,39 @@
44
// ```
55
// (no "from"; this doesn't export anything). That will setup the error hooks.
66

7+
// Note: KEEP DEPS HERE TO A MINIMUM. Sentry should be first thing to run.
78
import * as Sentry from '@sentry/node'
89

9-
const ENABLE_SENTRY = !(process.env['SOCKET_CLI_SENTRY'] === '0')
10-
const debugging = process.env['SOCKET_CLI_DEBUG'] === '1'
10+
// Rollup will inline this as true/false
11+
const IS_PUB = process.env['SOCKET_IS_PUBLISHED']
12+
const ENABLE_SENTRY =
13+
// Enable unless explicitly disabled
14+
(IS_PUB && process.env['SOCKET_ENABLE_SENTRY'] !== '0') ||
15+
// Disable unless explicitly enabled
16+
(!IS_PUB && process.env['SOCKET_ENABLE_SENTRY'] === '1')
1117

12-
// Note: this should not change the value if set from rollup but if somehow
13-
// running raw sources, at least it won't cause a runtime error (implicit global)
14-
// See rollup config.
15-
// DO NOT ADD INITIALIZER TO THIS VAR!
16-
// @ts-ignore
17-
var SOCKET_CLI_VERSION
18+
const debugging = process.env['SOCKET_CLI_DEBUG'] === '1'
1819

1920
if (ENABLE_SENTRY) {
2021
if (debugging) {
2122
console.log('[DEBUG] Setting up Sentry...')
2223
}
2324
Sentry.init({
2425
// debug: true,
25-
// onFatalError(error: Error) {
26-
// console.log('[Sentry onFatalError]: oops', error)
27-
// },
26+
onFatalError(error: Error) {
27+
if (debugging) {
28+
console.error('[DEBUG] [Sentry onFatalError]:', error)
29+
}
30+
},
2831
enabled: ENABLE_SENTRY,
2932

3033
dsn: 'https://66736701db8e4ffac046bd09fa6aaced@o555220.ingest.us.sentry.io/4508846967619585',
3134
integrations: []
3235
})
3336
Sentry.setTag('environment', 'dev') // TBD: how to determine this?
34-
Sentry.setTag('version', SOCKET_CLI_VERSION) // Generated by rollup
37+
Sentry.setTag('version', process.env['SOCKET_CLI_VERSION']) // Generated by rollup
3538
Sentry.setTag('debugging', debugging)
39+
Sentry.setTag('published', IS_PUB)
3640

3741
if (debugging) {
3842
console.log('[DEBUG] Set up Sentry.')

0 commit comments

Comments
 (0)