Skip to content

Commit 1fe6761

Browse files
authored
fix: maxListenersExceeded warning due to atomically, which is a peerdep of conf (#7182)
The conf dependency being bundled (not even executed) causes frequent HMR runs (around 10+) to throw multiple MaxListenersExceeded warnings in the console. This PR - fixes telemetry which was previously broken (threw an error which we ignored) due to a conf version upgrade - Removes the conf dependency (which is large and comes with a lot of unneeded dependencies from functionality we don't need, like dot notation or ajv validation). The important parts of the source code were copied over - it's now dependency-free - makes sure we only register the Next.js HMR websocket listener once, by adding it to the cache Before this PR: ![CleanShot 2024-07-16 at 19 35 22](https://github.com/user-attachments/assets/cfd926b7-fe5d-440a-9b35-91f61eaa69fd) After this PR: ![CleanShot 2024-07-16 at 19 37 41](https://github.com/user-attachments/assets/f5d0f0f3-4e00-4d28-8e32-be42db2f5f6c) Canary: 3.0.0-canary.ca3dd1c
1 parent f909f06 commit 1fe6761

File tree

7 files changed

+304
-79
lines changed

7 files changed

+304
-79
lines changed

packages/next/src/utilities/getPayloadHMR.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ let cached: {
77
payload: Payload | null
88
promise: Promise<Payload> | null
99
reload: Promise<void> | boolean
10+
ws: WebSocket | null
1011
} = global._payload
1112

1213
if (!cached) {
13-
cached = global._payload = { payload: null, promise: null, reload: false }
14+
cached = global._payload = { payload: null, promise: null, reload: false, ws: null }
1415
}
1516

1617
export const reload = async (config: SanitizedConfig, payload: Payload): Promise<void> => {
@@ -85,17 +86,18 @@ export const getPayloadHMR = async (options: InitOptions): Promise<Payload> => {
8586
cached.payload = await cached.promise
8687

8788
if (
89+
!cached.ws &&
8890
process.env.NODE_ENV !== 'production' &&
8991
process.env.NODE_ENV !== 'test' &&
9092
process.env.DISABLE_PAYLOAD_HMR !== 'true'
9193
) {
9294
try {
9395
const port = process.env.PORT || '3000'
94-
const ws = new WebSocket(
96+
cached.ws = new WebSocket(
9597
`ws://localhost:${port}${process.env.NEXT_BASE_PATH ?? ''}/_next/webpack-hmr`,
9698
)
9799

98-
ws.onmessage = (event) => {
100+
cached.ws.onmessage = (event) => {
99101
if (typeof event.data === 'string') {
100102
const data = JSON.parse(event.data)
101103

packages/payload/bundle.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ async function build() {
2323
'pino-pretty',
2424
'pino',
2525
//'ajv',
26-
//'conf',
2726
//'image-size',
2827
],
2928
minify: true,
@@ -50,7 +49,6 @@ async function build() {
5049
'pino-pretty',
5150
'pino',
5251
//'ajv',
53-
//'conf',
5452
//'image-size',
5553
],
5654
minify: true,

packages/payload/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"ajv": "8.14.0",
9292
"bson-objectid": "2.0.4",
9393
"ci-info": "^4.0.0",
94-
"conf": "12.0.0",
9594
"console-table-printer": "2.11.2",
9695
"dataloader": "2.2.2",
9796
"deepmerge": "4.3.1",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Taken from https://github.com/sindresorhus/env-paths/blob/main/index.js
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*/
14+
15+
import os from 'node:os'
16+
import path from 'node:path'
17+
import process from 'node:process'
18+
19+
const homedir = os.homedir()
20+
const tmpdir = os.tmpdir()
21+
const { env } = process
22+
23+
const macos = (name) => {
24+
const library = path.join(homedir, 'Library')
25+
26+
return {
27+
cache: path.join(library, 'Caches', name),
28+
config: path.join(library, 'Preferences', name),
29+
data: path.join(library, 'Application Support', name),
30+
log: path.join(library, 'Logs', name),
31+
temp: path.join(tmpdir, name),
32+
}
33+
}
34+
35+
const windows = (name) => {
36+
const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming')
37+
const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local')
38+
39+
return {
40+
// Data/config/cache/log are invented by me as Windows isn't opinionated about this
41+
cache: path.join(localAppData, name, 'Cache'),
42+
config: path.join(appData, name, 'Config'),
43+
data: path.join(localAppData, name, 'Data'),
44+
log: path.join(localAppData, name, 'Log'),
45+
temp: path.join(tmpdir, name),
46+
}
47+
}
48+
49+
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
50+
const linux = (name) => {
51+
const username = path.basename(homedir)
52+
53+
return {
54+
cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name),
55+
config: path.join(env.XDG_CONFIG_HOME || path.join(homedir, '.config'), name),
56+
data: path.join(env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'), name),
57+
// https://wiki.debian.org/XDGBaseDirectorySpecification#state
58+
log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name),
59+
temp: path.join(tmpdir, username, name),
60+
}
61+
}
62+
63+
export function envPaths(name, { suffix = 'nodejs' } = {}) {
64+
if (typeof name !== 'string') {
65+
throw new TypeError(`Expected a string, got ${typeof name}`)
66+
}
67+
68+
if (suffix) {
69+
// Add suffix to prevent possible conflict with native apps
70+
name += `-${suffix}`
71+
}
72+
73+
if (process.platform === 'darwin') {
74+
return macos(name)
75+
}
76+
77+
if (process.platform === 'win32') {
78+
return windows(name)
79+
}
80+
81+
return linux(name)
82+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/**
2+
* Taken & simplified from https://github.com/sindresorhus/conf/blob/main/source/index.ts
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*/
14+
15+
import assert from 'node:assert'
16+
import fs from 'node:fs'
17+
import path from 'node:path'
18+
19+
import { envPaths } from './envPaths.js'
20+
21+
const createPlainObject = <T = Record<string, unknown>>(): T => Object.create(null)
22+
23+
const checkValueType = (key: string, value: unknown): void => {
24+
const nonJsonTypes = new Set(['undefined', 'symbol', 'function'])
25+
26+
const type = typeof value
27+
28+
if (nonJsonTypes.has(type)) {
29+
throw new TypeError(
30+
`Setting a value of type \`${type}\` for key \`${key}\` is not allowed as it's not supported by JSON`,
31+
)
32+
}
33+
}
34+
35+
export class Conf<T extends Record<string, any> = Record<string, unknown>>
36+
implements Iterable<[keyof T, T[keyof T]]>
37+
{
38+
readonly #options: Readonly<Partial<Options>>
39+
private readonly _deserialize: Deserialize<T> = (value) => JSON.parse(value)
40+
private readonly _serialize: Serialize<T> = (value) => JSON.stringify(value, undefined, '\t')
41+
42+
readonly events: EventTarget
43+
44+
readonly path: string
45+
46+
constructor() {
47+
const options: Partial<Options> = {
48+
configFileMode: 0o666,
49+
configName: 'config',
50+
fileExtension: 'json',
51+
projectSuffix: 'nodejs',
52+
}
53+
54+
const cwd = envPaths('payload', { suffix: options.projectSuffix }).config
55+
56+
this.#options = options
57+
58+
this.events = new EventTarget()
59+
60+
const fileExtension = options.fileExtension ? `.${options.fileExtension}` : ''
61+
this.path = path.resolve(cwd, `${options.configName ?? 'config'}${fileExtension}`)
62+
63+
const fileStore = this.store
64+
const store = Object.assign(createPlainObject(), fileStore)
65+
66+
try {
67+
assert.deepEqual(fileStore, store)
68+
} catch {
69+
this.store = store
70+
}
71+
}
72+
73+
private _ensureDirectory(): void {
74+
// Ensure the directory exists as it could have been deleted in the meantime.
75+
fs.mkdirSync(path.dirname(this.path), { recursive: true })
76+
}
77+
78+
private _write(value: T): void {
79+
const data: Uint8Array | string = this._serialize(value)
80+
81+
fs.writeFileSync(this.path, data, { mode: this.#options.configFileMode })
82+
}
83+
84+
*[Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]> {
85+
for (const [key, value] of Object.entries(this.store)) {
86+
yield [key, value]
87+
}
88+
}
89+
90+
/**
91+
Delete an item.
92+
93+
@param key - The key of the item to delete.
94+
*/
95+
delete(key: string): void {
96+
const { store } = this
97+
delete store[key]
98+
99+
this.store = store
100+
}
101+
102+
/**
103+
Get an item.
104+
105+
@param key - The key of the item to get.
106+
*/
107+
get<Key extends keyof T>(key: Key): T[Key] {
108+
const { store } = this
109+
return store[key]
110+
}
111+
112+
/**
113+
Set an item or multiple items at once.
114+
115+
@param key - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. Or a hashmap of items to set at once.
116+
@param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`.
117+
*/
118+
set<Key extends keyof T>(key: string, value?: T[Key] | unknown): void {
119+
if (typeof key !== 'string' && typeof key !== 'object') {
120+
throw new TypeError(
121+
`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`,
122+
)
123+
}
124+
125+
if (typeof key !== 'object' && value === undefined) {
126+
throw new TypeError('Use `delete()` to clear values')
127+
}
128+
129+
const { store } = this
130+
131+
const set = (key: string, value?: T | T[Key] | unknown): void => {
132+
checkValueType(key, value)
133+
store[key as Key] = value as T[Key]
134+
}
135+
136+
if (typeof key === 'object') {
137+
const object = key
138+
for (const [key, value] of Object.entries(object)) {
139+
set(key, value)
140+
}
141+
} else {
142+
set(key, value)
143+
}
144+
145+
this.store = store
146+
}
147+
get size(): number {
148+
return Object.keys(this.store).length
149+
}
150+
get store(): T {
151+
try {
152+
const dataString = fs.readFileSync(this.path, 'utf8')
153+
const deserializedData = this._deserialize(dataString)
154+
return Object.assign(createPlainObject(), deserializedData)
155+
} catch (error: unknown) {
156+
if ((error as any)?.code === 'ENOENT') {
157+
this._ensureDirectory()
158+
return createPlainObject()
159+
}
160+
161+
throw error
162+
}
163+
}
164+
165+
set store(value: T) {
166+
this._ensureDirectory()
167+
168+
this._write(value)
169+
170+
this.events.dispatchEvent(new Event('change'))
171+
}
172+
}
173+
174+
export type Options = {
175+
/**
176+
The config is cleared if reading the config file causes a `SyntaxError`. This is a good behavior for unimportant data, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing.
177+
178+
@default false
179+
*/
180+
clearInvalidConfig?: boolean
181+
182+
/**
183+
The [mode](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) that will be used for the config file.
184+
185+
You would usually not need this, but it could be useful if you want to restrict the permissions of the config file. Setting a permission such as `0o600` would result in a config file that can only be accessed by the user running the program.
186+
187+
Note that setting restrictive permissions can cause problems if different users need to read the file. A common problem is a user running your tool with and without `sudo` and then not being able to access the config the second time.
188+
189+
@default 0o666
190+
*/
191+
readonly configFileMode?: number
192+
193+
/**
194+
Name of the config file (without extension).
195+
196+
Useful if you need multiple config files for your app or module. For example, different config files between two major versions.
197+
198+
@default 'config'
199+
*/
200+
configName?: string
201+
202+
/**
203+
Extension of the config file.
204+
205+
You would usually not need this, but could be useful if you want to interact with a file with a custom file extension that can be associated with your app. These might be simple save/export/preference files that are intended to be shareable or saved outside of the app.
206+
207+
@default 'json'
208+
*/
209+
fileExtension?: string
210+
211+
readonly projectSuffix?: string
212+
}
213+
214+
export type Serialize<T> = (value: T) => string
215+
export type Deserialize<T> = (text: string) => T

packages/payload/src/utilities/telemetry/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { execSync } from 'child_process'
22
import ciInfo from 'ci-info'
3-
import Conf from 'conf'
43
import { randomBytes } from 'crypto'
54
import { findUp } from 'find-up'
65
import fs from 'fs'
@@ -11,6 +10,7 @@ import type { Payload } from '../../types/index.js'
1110
import type { AdminInitEvent } from './events/adminInit.js'
1211
import type { ServerInitEvent } from './events/serverInit.js'
1312

13+
import { Conf } from './conf/index.js'
1414
import { oneWayHash } from './oneWayHash.js'
1515

1616
export type BaseEvent = {

0 commit comments

Comments
 (0)