Skip to content

Commit d359e75

Browse files
committed
fix: restore remaining source and test files from fix/node24
Assisted-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7c1f346 commit d359e75

45 files changed

Lines changed: 734 additions & 209 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/browser-logs/src/browserScript.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import path from 'path';
44
const REGEXP_SOURCE_MAP = /\/\/# sourceMappingURL=.*/;
55

66
const serializeScript = fs
7-
.readFileSync(path.resolve(__dirname, 'serialize.js'), 'utf-8')
7+
.readFileSync(path.resolve(import.meta.dirname, 'serialize.js'), 'utf-8')
88
.replace(REGEXP_SOURCE_MAP, '');
99
const logUncaughtErrorsScript = fs
10-
.readFileSync(path.resolve(__dirname, 'logUncaughtErrors.js'), 'utf-8')
10+
.readFileSync(path.resolve(import.meta.dirname, 'logUncaughtErrors.js'), 'utf-8')
1111
.replace(REGEXP_SOURCE_MAP, '');
1212

1313
/**
14-
* Create the browser script. This project is compiled as CJS because it also needs to run in node, so
15-
* we create a small wrapper.
16-
*
17-
* It can't be ESM anyway, because it should work on older browsers as well.
14+
* Create the browser script as an IIFE wrapper.
15+
* It can't be ESM because it should work on older browsers as well.
1816
*/
1917
export const browserScript =
2018
'(function () { var module={};var exports={};\n' +
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
module.exports = class ConfigLoaderError extends Error {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default class ConfigLoaderError extends Error {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import fs from 'fs/promises';
2+
import path from 'path';
3+
import { fileExists } from './utils.ts';
4+
5+
/**
6+
* Gets the package type for a given directory. Walks up the file system, looking
7+
* for a package.json file and returns the package type.
8+
*/
9+
async function getPackageType(basedir: string): Promise<string> {
10+
let currentPath = basedir;
11+
try {
12+
while (await fileExists(currentPath)) {
13+
const pkgJsonPath = path.join(currentPath, 'package.json');
14+
15+
if (await fileExists(pkgJsonPath)) {
16+
const pkgJsonString = await fs.readFile(pkgJsonPath, { encoding: 'utf-8' });
17+
const pkgJson = JSON.parse(pkgJsonString);
18+
return pkgJson.type || 'commonjs';
19+
}
20+
21+
currentPath = path.resolve(currentPath, '..');
22+
}
23+
} catch (e) {
24+
// don't log any error
25+
}
26+
return 'commonjs';
27+
}
28+
29+
export default getPackageType;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { pathToFileURL } from 'url';
2+
import ConfigLoaderError from './ConfigLoaderError.ts';
3+
4+
// These strings may be node-version dependent and need updating over time
5+
// They're just to display a helpful error message
6+
const CJS_ERRORS = [
7+
'ReferenceError: module is not defined',
8+
'ReferenceError: require is not defined',
9+
'ReferenceError: exports is not defined',
10+
];
11+
12+
async function importConfig(path: string): Promise<object> {
13+
try {
14+
const config = await import(pathToFileURL(path).href);
15+
16+
if (typeof config.default !== 'object') {
17+
throw new ConfigLoaderError(
18+
`Config at ${path} should have a default export that is an object.`,
19+
);
20+
}
21+
22+
return config.default;
23+
} catch (e) {
24+
if (CJS_ERRORS.some(msg => (e as Error).stack?.includes(msg))) {
25+
throw new ConfigLoaderError(
26+
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module. ' +
27+
'Use import/export syntax, or load the file as a CommonJS module by ' +
28+
'using the .cjs file extension or by removing type="module" from your package.json.',
29+
);
30+
}
31+
throw e;
32+
}
33+
}
34+
35+
export default importConfig;
Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +0,0 @@
1-
const getPackageType = require('./getPackageType');
2-
const path = require('path');
3-
const requireConfig = require('./requireConfig');
4-
5-
/**
6-
* @param {string} configPath
7-
*/
8-
function importConfig(configPath) {
9-
// Conditionally requires importConfig function to avoid logging a warning on node v12
10-
// when not using an es modules
11-
const importConfigFunction = require('./importConfig');
12-
return importConfigFunction(configPath);
13-
}
14-
15-
/**
16-
* @param {string} configPath
17-
* @param {string} basedir
18-
*/
19-
async function importOrRequireConfig(configPath, basedir) {
20-
const ext = path.extname(configPath);
21-
22-
switch (ext) {
23-
case '.mjs':
24-
return importConfig(configPath);
25-
case '.cjs':
26-
return requireConfig(configPath);
27-
default:
28-
const packageType = await getPackageType(basedir);
29-
return packageType === 'module' ? importConfig(configPath) : requireConfig(configPath);
30-
}
31-
}
32-
33-
module.exports = importOrRequireConfig;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import getPackageType from './getPackageType.ts';
2+
import path from 'path';
3+
import requireConfig from './requireConfig.ts';
4+
import importConfigFunction from './importConfig.ts';
5+
6+
async function importOrRequireConfig(configPath: string, basedir: string): Promise<object> {
7+
const ext = path.extname(configPath);
8+
9+
switch (ext) {
10+
case '.mjs':
11+
return importConfigFunction(configPath);
12+
case '.cjs':
13+
return requireConfig(configPath);
14+
default:
15+
const packageType = await getPackageType(basedir);
16+
return packageType === 'module' ? importConfigFunction(configPath) : requireConfig(configPath);
17+
}
18+
}
19+
20+
export default importOrRequireConfig;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import path from 'path';
2+
import { fileExists } from './utils.ts';
3+
import ConfigLoaderError from './ConfigLoaderError.ts';
4+
import importOrRequireConfig from './importOrRequireConfig.ts';
5+
6+
const EXTENSIONS = ['.mjs', '.cjs', '.js'];
7+
8+
async function readConfig(name: string, customPath?: string, basedir: string = process.cwd()): Promise<any> {
9+
const resolvedCustomPath = customPath ? path.resolve(basedir, customPath) : undefined;
10+
if (resolvedCustomPath && !(await fileExists(resolvedCustomPath))) {
11+
throw new ConfigLoaderError(`Could not find a config file at ${resolvedCustomPath}`);
12+
}
13+
14+
// load the custom config file
15+
if (resolvedCustomPath) {
16+
return importOrRequireConfig(resolvedCustomPath, basedir);
17+
}
18+
19+
// iterate file extensions in order, load the config if it exists
20+
for (const extension of EXTENSIONS) {
21+
const configPath = path.join(basedir, name + extension);
22+
if (await fileExists(configPath)) {
23+
return importOrRequireConfig(configPath, basedir);
24+
}
25+
}
26+
27+
return null;
28+
}
29+
30+
export { readConfig, ConfigLoaderError };
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createRequire } from 'module';
2+
import ConfigLoaderError from './ConfigLoaderError.ts';
3+
4+
const require = createRequire(import.meta.url);
5+
6+
// These strings may be node-version dependent and need updating over time
7+
// They're just to display a helpful error message
8+
const ESM_ERRORS = [
9+
"SyntaxError: Unexpected token 'export'",
10+
'SyntaxError: Cannot use import statement outside a module',
11+
];
12+
13+
function requireConfig(path: string): object {
14+
try {
15+
return require(path);
16+
} catch (e) {
17+
if (ESM_ERRORS.some(msg => (e as Error).stack?.includes(msg))) {
18+
throw new ConfigLoaderError(
19+
'You are using es module syntax in a config loaded as CommonJS module. ' +
20+
'Use require/module.exports syntax, or load the file as es module by using the .mjs ' +
21+
'file extension or by setting type="module" in your package.json.',
22+
);
23+
}
24+
throw e;
25+
}
26+
}
27+
28+
export default requireConfig;
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
const fs = require('fs').promises;
2-
3-
/**
4-
* @param {string} path
5-
*/
6-
async function fileExists(path) {
7-
try {
8-
await fs.access(path);
9-
return true;
10-
} catch (e) {
11-
return false;
12-
}
13-
}
14-
15-
module.exports = { fileExists };

0 commit comments

Comments
 (0)