Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require node: prefixes for Node.js builtins #1080

Merged
merged 2 commits into from Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Expand Up @@ -51,6 +51,7 @@ module.exports = {
env: { [ECMASCRIPT_ENV]: true, node: true },
// the prettier config disables all eslint rules known to conflict with prettier
extends: ['eslint:recommended', 'plugin:n/recommended', 'prettier'],
plugins: ['node-import'],
rules: {
// base rules; none of these are in eslint/recommended
'no-empty': ['error', { allowEmptyCatch: true }],
Expand Down Expand Up @@ -79,6 +80,9 @@ module.exports = {
// we should probably actually fix these three and turn these back on
'n/no-sync': 'off', // very few reasons to actually use this; "CLI tool" is not one of them
'n/no-process-exit': 'off', // should not be used with async code

// require node: prefix for builtin modules
'node-import/prefer-node-protocol': 'error',
},
settings: {
node: {
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jsonc": "2.13.0",
"eslint-plugin-n": "16.6.2",
"eslint-plugin-node-import": "1.0.4",
"eslint-plugin-react": "7.34.0",
"glob": "10.3.10",
"husky": "9.0.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/aa/test/bench.js
Expand Up @@ -47,7 +47,7 @@ const recordMemorySpike = (frequency = 10) => {

const history = (name, value) => {
const fs = require('node:fs')
const { execSync } = require('child_process')
const { execSync } = require('node:child_process')
const filePath = `.bench-${name}.log` // the intention is for the file to be gitignored and only referred locally
let log = {}
if (fs.existsSync(filePath)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/allow-scripts/src/index.js
@@ -1,7 +1,7 @@
// @ts-check

const { promises: fs } = require('fs')
const path = require('path')
const { promises: fs } = require('node:fs')
const path = require('node:path')
const npmRunScript = require('@npmcli/run-script')
const normalizeBin = require('npm-normalize-package-bin')
const { linkBinAbsolute, linkBinRelative } = require('./linker.js')
Expand Down
2 changes: 1 addition & 1 deletion packages/allow-scripts/src/linker.js
Expand Up @@ -7,7 +7,7 @@ const linkBin = isWindows
? require('bin-links/lib/shim-bin.js')
: require('bin-links/lib/link-bin.js')

const { dirname, resolve, relative } = require('path')
const { dirname, resolve, relative } = require('node:path')

/**
* @param {LinkBinOpts} opts
Expand Down
6 changes: 3 additions & 3 deletions packages/allow-scripts/src/setup.js
Expand Up @@ -3,9 +3,9 @@ const {
appendFileSync,
readFileSync,
writeFileSync,
} = require('fs')
const { spawnSync } = require('child_process')
const path = require('path')
} = require('node:fs')
const { spawnSync } = require('node:child_process')
const path = require('node:path')
const { FEATURE } = require('./toggles')

const NPM = {
Expand Down
2 changes: 1 addition & 1 deletion packages/allow-scripts/src/whichbin.js
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable n/shebang */

// @ts-check
const path = require('path')
const path = require('node:path')
const { getOptionsForBin } = require('./index.js')
const { FEATURE } = require('./toggles.js')

Expand Down
2 changes: 1 addition & 1 deletion packages/browserify/src/createPackageDataStream.js
@@ -1,4 +1,4 @@
const { callbackify } = require('util')
const { callbackify } = require('node:util')
const through = require('through2').obj
const { getPackageNameForModulePath } = require('@lavamoat/aa')

Expand Down
4 changes: 2 additions & 2 deletions packages/browserify/src/index.js
@@ -1,5 +1,5 @@
const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const { getDefaultPaths } = require('lavamoat-core')
const jsonStringify = require('json-stable-stringify')
const { createModuleInspectorSpy } = require('./createModuleInspectorSpy.js')
Expand Down
2 changes: 1 addition & 1 deletion packages/browserify/src/sesTransforms.js
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('node:path')
const { PassThrough } = require('readable-stream')
const duplexify = require('duplexify')
const concatStream = require('concat-stream')
Expand Down
Expand Up @@ -109,13 +109,13 @@
},
"@lavamoat/lavapack": {
"builtin": {
"assert": true,
"buffer.Buffer.from": true,
"fs.promises.readFile": true,
"fs.promises.writeFile": true,
"fs.readFileSync": true,
"path.join": true,
"path.relative": true
"node:assert": true,
"node:buffer.Buffer.from": true,
"node:fs.promises.readFile": true,
"node:fs.promises.writeFile": true,
"node:fs.readFileSync": true,
"node:path.join": true,
"node:path.relative": true
},
"globals": {
"__dirname": true,
Expand Down Expand Up @@ -1031,12 +1031,11 @@
},
"lavamoat-core": {
"builtin": {
"events": true,
"fs.readFileSync": true,
"node:events": true,
"node:fs.readFileSync": true,
"node:fs/promises.writeFile": true,
"path.extname": true,
"path.join": true
"node:path.extname": true,
"node:path.join": true
},
"globals": {
"__dirname": true,
Expand Down
4 changes: 2 additions & 2 deletions packages/browserify/test/lavamoatNode.spec.js
@@ -1,6 +1,6 @@
const test = require('ava')
const { execSync } = require('child_process')
const path = require('path')
const { execSync } = require('node:child_process')
const path = require('node:path')
const { evalBundle } = require('./util')

test('lavamoat-node compat - bundle works under lavamoat node', (t) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/browserify/test/policy.spec.js
@@ -1,7 +1,7 @@
/* eslint-disable ava/use-t-well */
const test = require('ava')
const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const {
createScenarioFromScaffold,
runAndTestScenario,
Expand Down
12 changes: 6 additions & 6 deletions packages/browserify/test/util.js
@@ -1,8 +1,8 @@
const { runInNewContext } = require('vm')
const { runInNewContext } = require('node:vm')
const browserify = require('browserify')
const pify = require('pify')
const { promises: fs } = require('fs')
const path = require('path')
const { promises: fs } = require('node:fs')
const path = require('node:path')
const watchify = require('watchify')
const lavamoatPlugin = require('../src/index')
const { verifySourceMaps } = require('./sourcemaps')
Expand All @@ -12,10 +12,10 @@ const {
evaluateWithSourceUrl,
createHookedConsole,
} = require('lavamoat-core/test/util.js')
const util = require('util')
const util = require('node:util')
const tmp = require('tmp-promise')
const { spawnSync } = require('child_process')
const execFile = util.promisify(require('child_process').execFile)
const { spawnSync } = require('node:child_process')
const execFile = util.promisify(require('node:child_process').execFile)

const WORKSPACE_ROOT = path.resolve(__dirname, '..', '..', '..')

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/generateKernel.js
Expand Up @@ -4,8 +4,8 @@
// The prelude is defined in the `preludeTemplate` file, and here we inject the dynamic components,
// such as the upgradeable SES and app specific config.

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const kernelTemplate = fs.readFileSync(
path.join(__dirname, '/kernelTemplate.js'),
'utf-8'
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/generatePolicy.js
@@ -1,7 +1,7 @@
// @ts-check

const EventEmitter = require('events')
const path = require('path')
const EventEmitter = require('node:events')
const path = require('node:path')
const jsonStringify = require('json-stable-stringify')
const {
parse,
Expand Down
10 changes: 5 additions & 5 deletions packages/core/test/builtin.spec.js
Expand Up @@ -68,7 +68,7 @@ test('builtin - access via paths', async (t) => {
test('builtin - paths soft-bindings preserve "this" but allow override', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
const { Buffer } = require('buffer')
const { Buffer } = require('node:buffer')
const two = require('two')
module.exports = {
overrideCheck: two.overrideCheck(Buffer.from([1, 2, 3])),
Expand All @@ -78,7 +78,7 @@ test('builtin - paths soft-bindings preserve "this" but allow override', async (
},

defineTwo: () => {
const { Buffer } = require('buffer')
const { Buffer } = require('node:buffer')
const thisChecker = require('thisChecker')
const { SomeClass } = require('someClass')
// this test ensures "Buffer.prototype.slice" is copied in a way that allows "this" to be overridden
Expand All @@ -101,13 +101,13 @@ test('builtin - paths soft-bindings preserve "this" but allow override', async (
resources: {
one: {
builtin: {
'buffer.Buffer.from': true,
'node:buffer.Buffer.from': true,
},
},
two: {
builtin: {
// these paths are carefully constructed to try and split the fn from its parent
'buffer.Buffer.prototype.slice': true,
'node:buffer.Buffer.prototype.slice': true,
'thisChecker.check': true,
'someClass.SomeClass': true,
},
Expand All @@ -120,7 +120,7 @@ test('builtin - paths soft-bindings preserve "this" but allow override', async (
classCheck: true,
},
builtin: {
buffer: require('buffer'),
'node:buffer': require('node:buffer'),
thisChecker: (() => {
const parent = {}
parent.check = function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/lavapack/src/builder-runtime.js
@@ -1,7 +1,7 @@
const { join: pathJoin } = require('path')
const { join: pathJoin } = require('node:path')
const {
promises: { readFile, writeFile },
} = require('fs')
} = require('node:fs')
const { generateKernel, makeInitStatsHook } = require('lavamoat-core')
const {
getStrictScopeTerminatorShimSrc,
Expand Down
8 changes: 4 additions & 4 deletions packages/lavapack/src/pack.js
Expand Up @@ -9,14 +9,14 @@
// - cleanup: var -> const/let
// - cleanup/refactor

const fs = require('fs')
const path = require('path')
const assert = require('assert')
const fs = require('node:fs')
const path = require('node:path')
const assert = require('node:assert')
const JSONStream = require('JSONStream')
const through = require('through2')
const umd = require('umd')
// eslint-disable-next-line n/prefer-global/buffer
const { Buffer } = require('buffer')
const { Buffer } = require('node:buffer')
const combineSourceMap = require('combine-source-map')
const convertSourceMap = require('convert-source-map')
const jsonStringify = require('json-stable-stringify')
Expand Down
2 changes: 1 addition & 1 deletion packages/lavapack/test/index.spec.js
@@ -1,4 +1,4 @@
const fs = require('fs')
const fs = require('node:fs')
const test = require('ava')
const { pipe, concat } = require('mississippi')
const { SourceMapConsumer } = require('source-map')
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/freshRealmCompartment.js
@@ -1,4 +1,4 @@
const vm = require('vm')
const vm = require('node:vm')
const { getSesShimSrc } = require('lavamoat-core/src/generateKernel')

module.exports = { createFreshRealmCompartment }
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/index.js
@@ -1,7 +1,7 @@
/* eslint-disable no-eval */

const path = require('path')
const fs = require('fs')
const path = require('node:path')
const fs = require('node:fs')
const jsonStringify = require('json-stable-stringify')
const { loadPolicy, loadPolicyAndApplyOverrides } = require('lavamoat-core')
const { loadCanonicalNameMap } = require('@lavamoat/aa')
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/kernel.js
@@ -1,6 +1,6 @@
/* eslint no-eval: 0 */
const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const resolve = require('resolve')
const { sanitize } = require('htmlescape')
const {
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/parseForPolicy.js
@@ -1,6 +1,6 @@
const path = require('path')
const { promises: fs } = require('fs')
const { builtinModules } = require('module')
const path = require('node:path')
const { promises: fs } = require('node:fs')
const { builtinModules } = require('node:module')
const resolve = require('resolve')
const bindings = require('bindings')
const gypBuild = require('node-gyp-build')
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/run-command.js
@@ -1,8 +1,8 @@
#!/usr/bin/env node
/* eslint-disable no-eval */

const path = require('path')
const fs = require('fs')
const path = require('node:path')
const fs = require('node:fs')
const yargs = require('yargs')
const yargsFlags = require('./yargsFlags')
const { runLava } = require('./index')
Expand Down
5 changes: 3 additions & 2 deletions packages/node/test/util.js
@@ -1,7 +1,7 @@
// @ts-check

const util = require('util')
const execFile = util.promisify(require('child_process').execFile)
const util = require('node:util')
const execFile = util.promisify(require('node:child_process').execFile)
const { prepareScenarioOnDisk } = require('lavamoat-core/test/util.js')
module.exports = {
runLavamoat,
Expand All @@ -12,6 +12,7 @@ module.exports = {
* Add one or more CLI options to an array of args
*
* Mutates `args`
*
* @param {string[]} args - Array of args
* @param {string} key - Option name
* @param {any} value - Value; if array, `key` will be set multiple times
Expand Down
4 changes: 2 additions & 2 deletions packages/node/test/waysToRun.spec.js
@@ -1,6 +1,6 @@
const test = require('ava')
const util = require('util')
const execFile = util.promisify(require('child_process').execFile)
const util = require('node:util')
const execFile = util.promisify(require('node:child_process').execFile)

const { runLava } = require('../src/index')

Expand Down
4 changes: 2 additions & 2 deletions packages/perf/build.js
@@ -1,7 +1,7 @@
const fs = require('fs')
const fs = require('node:fs')
const browserify = require('browserify')
const lavamoatPlugin = require('lavamoat-browserify')
const { pipeline } = require('stream')
const { pipeline } = require('node:stream')

// "autogen": "--plugin [ lavamoat-browserify --writeAutoPolicy --policy ./lavamoat-policy.json ] > /dev/null",
// "default": "--plugin [ lavamoat-browserify --policy ./lavamoat-policy.json ] > bundle.js",
Expand Down