Skip to content

Commit 7dcafa2

Browse files
authored
Prevent usage of node or browser APIs in @segment/analytics-core and @segment/analytics-node (#846)
1 parent 151a58f commit 7dcafa2

File tree

17 files changed

+433
-83
lines changed

17 files changed

+433
-83
lines changed

.changeset/giant-gifts-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-core': patch
3+
---
4+
5+
Remove browser-specific isOffline() logic from core

.eslintrc.isomorphic.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** @type { import('eslint').Linter.Config } */
2+
module.exports = {
3+
extends: ['./.eslintrc'],
4+
plugins: ['import'],
5+
overrides: [
6+
{
7+
// this library should not have any node OR browser runtime dependencies.
8+
// In particular, nextjs and vercel edge functions will break if _any_ node dependencies are introduced.
9+
files: ['src/**'],
10+
excludedFiles: ['**/__tests__/**'],
11+
rules: {
12+
'no-restricted-globals': [
13+
'error',
14+
'document',
15+
'window',
16+
'self',
17+
'process',
18+
'global',
19+
'navigator',
20+
],
21+
'import/no-nodejs-modules': 'error',
22+
},
23+
},
24+
],
25+
}

.eslintrc.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,13 @@ module.exports = {
55
parserOptions: {
66
ecmaVersion: 2020,
77
},
8-
env: {
9-
es6: true,
10-
node: true,
11-
browser: true,
12-
},
138
extends: [
149
// Turn on on eslint recommended rules https://github.com/eslint/eslint/blob/main/conf/eslint-recommended.js
1510
'eslint:recommended',
1611
// Turn off eslint rules that conflict with prettier https://github.com/prettier/eslint-config-prettier/blob/main/index.js
1712
'prettier',
1813
],
1914
overrides: [
20-
{
21-
files: ['*.{js,mjs}'],
22-
extends: [
23-
// Handle prettier rules through eslint https://github.com/prettier/eslint-plugin-prettier/blob/master/eslint-plugin-prettier.js#L65
24-
'plugin:prettier/recommended',
25-
],
26-
},
2715
{
2816
files: ['*.{ts,tsx}'],
2917
parser: '@typescript-eslint/parser',
@@ -65,13 +53,25 @@ module.exports = {
6553
},
6654
overrides: [
6755
{
68-
files: ['*.test.*'],
56+
files: ['**/__tests__/**', '**/scripts/**'],
6957
rules: {
7058
'require-await': 'off',
7159
'@typescript-eslint/require-await': 'off',
7260
},
7361
},
7462
],
7563
},
64+
{
65+
files: ['*.{js,mjs}'],
66+
env: {
67+
// Config files and possible scripts. Allow anything, we don't really care.
68+
browser: true,
69+
node: true,
70+
},
71+
extends: [
72+
// Handle prettier rules through eslint https://github.com/prettier/eslint-plugin-prettier/blob/master/eslint-plugin-prettier.js#L65
73+
'plugin:prettier/recommended',
74+
],
75+
},
7676
],
7777
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"concurrently": "^7.6.0",
4747
"eslint": "^8.14.0",
4848
"eslint-config-prettier": "^8.5.0",
49+
"eslint-plugin-import": "^2.27.5",
4950
"eslint-plugin-prettier": "^4.0.0",
5051
"express": "^4.18.2",
5152
"get-monorepo-packages": "^1.2.0",

packages/browser/src/core/queue/event-queue.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
33
import { Context } from '../context'
44
import { AnyBrowserPlugin } from '../plugin'
55
import { CoreEventQueue } from '@segment/analytics-core'
6+
import { isOffline } from '../connection'
67

78
export class EventQueue extends CoreEventQueue<Context, AnyBrowserPlugin> {
89
constructor(priorityQueue?: PriorityQueue<Context>) {
910
super(priorityQueue ?? new PersistedPriorityQueue(4, 'event-queue'))
1011
}
12+
async flush(): Promise<Context[]> {
13+
if (isOffline()) return []
14+
return super.flush()
15+
}
1116
}

packages/core/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/** @type { import('eslint').Linter.Config } */
22
module.exports = {
3-
extends: ['../../.eslintrc'],
3+
extends: ['../../.eslintrc.isomorphic'],
44
}

packages/core/src/analytics/__tests__/dispatch.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ type JestMockedFn<Fn> = Fn extends (...args: infer Args) => infer ReturnT
88
? jest.Mock<ReturnT, Args>
99
: never
1010

11-
const isOnline = jest.fn().mockReturnValue(true)
12-
const isOffline = jest.fn().mockReturnValue(false)
13-
14-
jest.mock('../../connection', () => ({
15-
isOnline,
16-
isOffline,
17-
}))
18-
19-
const fetcher: JestMockedFn<typeof import('node-fetch')['default']> = jest.fn()
20-
jest.mock('node-fetch', () => fetcher)
21-
2211
const invokeCallback: JestMockedFn<
2312
typeof import('../../callback')['invokeCallback']
2413
> = jest.fn()

packages/core/src/connection/__tests__/index.test.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/core/src/connection/index.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/core/src/queue/event-queue.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Integrations, JSONObject } from '../events/interfaces'
88
import { CorePlugin } from '../plugins'
99
import { createTaskGroup, TaskGroup } from '../task/task-group'
1010
import { attempt, ensure } from './delivery'
11-
import { isOffline } from '../connection'
1211

1312
export type EventQueueEmitterContract<Ctx extends CoreContext> = {
1413
message_delivered: [ctx: Ctx]
@@ -190,7 +189,7 @@ export abstract class CoreEventQueue<
190189
}
191190

192191
async flush(): Promise<Ctx[]> {
193-
if (this.queue.length === 0 || isOffline()) {
192+
if (this.queue.length === 0) {
194193
return []
195194
}
196195

0 commit comments

Comments
 (0)