diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 5388de456..bf6aacf1c 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -2,6 +2,11 @@ const { overrides } = require('@netlify/eslint-config-node') module.exports = { extends: '@netlify/eslint-config-node', + rules: { + // This rule enforces using Buffers with `JSON.parse()`. However, TypeScript + // does not recognize yet that `JSON.parse()` accepts Buffers as argument. + 'unicorn/prefer-json-parse-buffer': 'off', + }, overrides: [ ...overrides, { diff --git a/src/runtimes/node/bundlers/zisi/traverse.ts b/src/runtimes/node/bundlers/zisi/traverse.ts index d2322067f..8a4a57022 100644 --- a/src/runtimes/node/bundlers/zisi/traverse.ts +++ b/src/runtimes/node/bundlers/zisi/traverse.ts @@ -1,3 +1,4 @@ +import { promises as fs } from 'fs' import { dirname } from 'path' import { nonNullable } from '../../../../utils/non_nullable' @@ -74,8 +75,7 @@ const getDependenciesForModuleName = async function ({ state.modulePaths.add(modulePath) // The path depends on the user's build, i.e. must be dynamic - // eslint-disable-next-line import/no-dynamic-require, node/global-require, @typescript-eslint/no-var-requires - const packageJson = require(packagePath) + const packageJson = JSON.parse(await fs.readFile(packagePath, 'utf8')) const [publishedFiles, sideFiles, depsPaths] = await Promise.all([ getPublishedFiles(modulePath), diff --git a/src/runtimes/node/utils/package_json.ts b/src/runtimes/node/utils/package_json.ts index c3c3bfbfa..03df3545e 100644 --- a/src/runtimes/node/utils/package_json.ts +++ b/src/runtimes/node/utils/package_json.ts @@ -1,3 +1,5 @@ +import { promises as fs } from 'fs' + import pkgDir from 'pkg-dir' interface PackageJson { @@ -38,8 +40,7 @@ const getPackageJson = async function (srcDir: string): Promise { const packageJsonPath = `${packageRoot}/package.json` try { // The path depends on the user's build, i.e. must be dynamic - // eslint-disable-next-line import/no-dynamic-require, node/global-require, @typescript-eslint/no-var-requires - const packageJson = require(packageJsonPath) + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')) return sanitisePackageJson(packageJson) } catch (error) { throw new Error(`${packageJsonPath} is invalid JSON: ${error.message}`) diff --git a/tests/bin.js b/tests/bin.js index a37ef9437..44e74f7a5 100644 --- a/tests/bin.js +++ b/tests/bin.js @@ -1,18 +1,19 @@ +const { promises: fs } = require('fs') const { join } = require('path') const test = require('ava') const execa = require('execa') const { tmpName } = require('tmp-promise') -const { version } = require('../package.json') - const { FIXTURES_DIR, BINARY_PATH } = require('./helpers/main') +const ROOT_PACKAGE_JSON = `${__dirname}/../package.json` + const exec = (args, options) => execa('node', [BINARY_PATH, ...args], options) test('CLI | --version', async (t) => { const { stdout } = await exec(['--version']) - + const { version } = JSON.parse(await fs.readFile(ROOT_PACKAGE_JSON)) t.is(stdout, version) }) diff --git a/tests/main.js b/tests/main.js index 96537e411..82da2e867 100644 --- a/tests/main.js +++ b/tests/main.js @@ -2461,8 +2461,7 @@ test('Creates a manifest file with the list of created functions if the `manifes }, }) - // eslint-disable-next-line import/no-dynamic-require, node/global-require - const manifest = require(manifestPath) + const manifest = JSON.parse(await readFile(manifestPath)) t.is(manifest.version, 1) t.is(manifest.system.arch, arch) @@ -2584,8 +2583,7 @@ testMany( files.every((file) => t.is(file.schedule, schedule)) - // eslint-disable-next-line import/no-dynamic-require, node/global-require - const manifest = require(manifestPath) + const manifest = JSON.parse(await readFile(manifestPath)) manifest.functions.forEach((fn) => { t.is(fn.schedule, schedule)