From 51059502a788df7c5737840d898c1ce7c4d621fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Iv=C3=A1n=20Vieitez=20Parra?= <3857362+corrideat@users.noreply.github.com> Date: Fri, 6 Oct 2023 15:55:01 +0200 Subject: [PATCH] Node 20 support --- .github/workflows/npm-publish.yml | 2 +- .github/workflows/pull-request.yml | 4 ++++ .mocharc.json | 2 +- loader.mjs | 16 +++++++++---- package.json | 2 +- src/test/lib/buildTimeSettings.ts | 23 +++++++++++++++++++ src/test/tsconfig.json | 10 ++++++++ src/trusted/lib/setupSandboxListeners.spec.ts | 5 +--- src/trusted/tsconfig.json | 3 ++- src/types/tsconfig.json | 1 + src/untrusted/lib/createContext.spec.ts | 6 +---- src/untrusted/lib/hardenGlobals.spec.ts | 5 +--- src/untrusted/lib/performTaskFactory.spec.ts | 5 +--- src/untrusted/lib/requestHandler.spec.ts | 5 +--- src/untrusted/tsconfig.json | 3 ++- test/e2e/bare/bareSandbox.spec.ts | 11 ++++----- test/e2e/nodejs/nodejsSandbox.spec.ts | 2 +- test/lib/runBrowserTest.ts | 3 +++ test/lib/runNodejsTests.ts | 2 +- test/lib/webdriverTestSuites.ts | 10 ++++---- tsconfig.json | 7 ++++-- 21 files changed, 80 insertions(+), 47 deletions(-) create mode 100644 src/test/lib/buildTimeSettings.ts create mode 100644 src/test/tsconfig.json diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index d35ed74..eddfa7d 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -22,7 +22,7 @@ jobs: java-version: '11' - uses: actions/setup-node@v3 with: - node-version: '19' + node-version: '20' registry-url: https://registry.npmjs.org/ - run: npm ci - run: npm publish --provenance --access public diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 1b104f1..1b99984 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -28,6 +28,8 @@ jobs: - run: npm run lint - run: npm test - run: deno test --import-map=import_map.json test + env: + DENO_NO_PACKAGE_JSON=1 run-tests-windows: runs-on: windows-latest @@ -47,3 +49,5 @@ jobs: - run: npm run lint - run: npm test - run: deno test --import-map=import_map.json test + env: + DENO_NO_PACKAGE_JSON=1 diff --git a/.mocharc.json b/.mocharc.json index 2f18849..290c705 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -1,4 +1,4 @@ { "extension": ["ts"], - "loader": "./loader.mjs" + "node-option": ["no-warnings=ExperimentalWarning", "loader=./loader.mjs"] } diff --git a/loader.mjs b/loader.mjs index 53c75f0..09e9253 100644 --- a/loader.mjs +++ b/loader.mjs @@ -14,10 +14,10 @@ */ import module from 'node:module'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { resolve as resolveTs } from 'ts-node/esm'; import importMap from './import_map.json' assert { type: 'json' }; -import { fileURLToPath } from 'node:url'; -import { join } from 'node:path'; export * from 'ts-node/esm'; @@ -47,10 +47,16 @@ module._resolveFilename = (...args) => { export const resolve = (specifier, context, defaultResolver) => { if (typeof specifier === 'string') { Object.entries(importMap.imports).forEach(([k, v]) => { - if (specifier.startsWith(absoluteBaseUrl + k)) - specifier = specifier.replace(k, v); + if (specifier.startsWith(k)) { + specifier = specifier.replace(k, absoluteBaseUrl + v); + } }); } - return resolveTs(specifier, context, defaultResolver); + return resolveTs(specifier, context, defaultResolver).then((v) => { + if (v.url.endsWith('.ts')) { + v.format = 'module'; + } + return v; + }); }; diff --git a/package.json b/package.json index 0ea6179..4c08400 100644 --- a/package.json +++ b/package.json @@ -132,7 +132,7 @@ "test:unit": "nyc mocha src/**/*.spec.ts", "test:e2e": "mocha test/**/*.spec.ts", "test": "npm run test:unit && npm run build && npm run test:e2e", - "prepack": "npm run build && rimraf -g -- 'dist/**/*.tsbuildinfo'", + "prepack": "npm run build", "prepublishOnly": "npm test && npm run lint", "preversion": "npm run lint", "version": "npm run lint && git add -A src", diff --git a/src/test/lib/buildTimeSettings.ts b/src/test/lib/buildTimeSettings.ts new file mode 100644 index 0000000..e9f90d8 --- /dev/null +++ b/src/test/lib/buildTimeSettings.ts @@ -0,0 +1,23 @@ +/* Copyright © 2023 Exact Realty Limited. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +if (typeof __buildtimeSettings__ !== 'object') + Reflect.set(globalThis, '__buildtimeSettings__', {}); + +Reflect.set(__buildtimeSettings__, 'bidirectionalMessaging', true); +Reflect.set(__buildtimeSettings__, 'buildType', 'debug'); +Reflect.set(__buildtimeSettings__, 'defaultAllowedGlobalProps', []); +Reflect.set(__buildtimeSettings__, 'hardenGlobals', true); +Reflect.set(__buildtimeSettings__, 'sandboxInitDeadlineInMs', 100); diff --git a/src/test/tsconfig.json b/src/test/tsconfig.json new file mode 100644 index 0000000..1e4aba0 --- /dev/null +++ b/src/test/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "composite": true, + "lib": ["es2022"], + "outDir": "../../build/test" + }, + "include": ["**/*"] +} diff --git a/src/trusted/lib/setupSandboxListeners.spec.ts b/src/trusted/lib/setupSandboxListeners.spec.ts index 17ec9f8..1db0051 100644 --- a/src/trusted/lib/setupSandboxListeners.spec.ts +++ b/src/trusted/lib/setupSandboxListeners.spec.ts @@ -13,10 +13,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -if (typeof __buildtimeSettings__ !== 'object') - Reflect.set(globalThis, '__buildtimeSettings__', {}); - -Reflect.set(__buildtimeSettings__, 'sandboxInitDeadlineInMs', 100); +import '~/test/lib/buildTimeSettings.js'; import setupSandboxListeners from './setupSandboxListeners.js'; diff --git a/src/trusted/tsconfig.json b/src/trusted/tsconfig.json index 0d6571b..ac5b388 100644 --- a/src/trusted/tsconfig.json +++ b/src/trusted/tsconfig.json @@ -3,8 +3,9 @@ "compilerOptions": { "rootDir": ".", "outDir": "../../dist/trusted", + "tsBuildInfoFile": "../../build/trusted.tsbuildinfo", "composite": true }, - "references": [{ "path": "../types" }, { "path": "../untrusted" }], + "references": [{ "path": "../types" }, { "path": "../untrusted" }, { "path": "../test" }], "include": ["**/*", "**/*.json"] } diff --git a/src/types/tsconfig.json b/src/types/tsconfig.json index faedcb6..29afb8e 100644 --- a/src/types/tsconfig.json +++ b/src/types/tsconfig.json @@ -3,6 +3,7 @@ "compilerOptions": { "rootDir": ".", "outDir": "../../dist/types", + "tsBuildInfoFile": "../../build/types.tsbuildinfo", "composite": true }, "include": ["**/*", "**/*.json"] diff --git a/src/untrusted/lib/createContext.spec.ts b/src/untrusted/lib/createContext.spec.ts index 84d7a34..844b517 100644 --- a/src/untrusted/lib/createContext.spec.ts +++ b/src/untrusted/lib/createContext.spec.ts @@ -13,11 +13,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -if (typeof __buildtimeSettings__ !== 'object') - Reflect.set(globalThis, '__buildtimeSettings__', {}); - -Reflect.set(__buildtimeSettings__, 'bidirectionalMessaging', true); -Reflect.set(__buildtimeSettings__, 'defaultAllowedGlobalProps', []); +import '~/test/lib/buildTimeSettings.js'; import assert from 'node:assert/strict'; import createContext, { setupExternalMethods } from './createContext.js'; diff --git a/src/untrusted/lib/hardenGlobals.spec.ts b/src/untrusted/lib/hardenGlobals.spec.ts index b00f149..c313e0e 100644 --- a/src/untrusted/lib/hardenGlobals.spec.ts +++ b/src/untrusted/lib/hardenGlobals.spec.ts @@ -13,10 +13,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -if (typeof __buildtimeSettings__ !== 'object') - Reflect.set(globalThis, '__buildtimeSettings__', {}); - -Reflect.set(__buildtimeSettings__, 'hardenGlobals', true); +import '~/test/lib/buildTimeSettings.js'; import assert from 'node:assert/strict'; import hardenGlobals, { disableURLStaticMethods } from './hardenGlobals.js'; diff --git a/src/untrusted/lib/performTaskFactory.spec.ts b/src/untrusted/lib/performTaskFactory.spec.ts index 3999e32..ddfc2f2 100644 --- a/src/untrusted/lib/performTaskFactory.spec.ts +++ b/src/untrusted/lib/performTaskFactory.spec.ts @@ -13,10 +13,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -if (typeof __buildtimeSettings__ !== 'object') - Reflect.set(globalThis, '__buildtimeSettings__', {}); - -Reflect.set(__buildtimeSettings__, 'buildType', 'debug'); +import '~/test/lib/buildTimeSettings.js'; import assert from 'node:assert/strict'; import performTaskFactory from './performTaskFactory.js'; diff --git a/src/untrusted/lib/requestHandler.spec.ts b/src/untrusted/lib/requestHandler.spec.ts index 3162da6..5aa25b7 100644 --- a/src/untrusted/lib/requestHandler.spec.ts +++ b/src/untrusted/lib/requestHandler.spec.ts @@ -13,10 +13,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -if (typeof __buildtimeSettings__ !== 'object') - Reflect.set(globalThis, '__buildtimeSettings__', {}); - -Reflect.set(__buildtimeSettings__, 'buildType', 'debug'); +import '~/test/lib/buildTimeSettings.js'; import assert from 'node:assert/strict'; import { reconstructErrorInformation } from './errorModem.js'; diff --git a/src/untrusted/tsconfig.json b/src/untrusted/tsconfig.json index aaf6d5d..53bf462 100644 --- a/src/untrusted/tsconfig.json +++ b/src/untrusted/tsconfig.json @@ -3,8 +3,9 @@ "compilerOptions": { "rootDir": ".", "outDir": "../../dist/untrusted", + "tsBuildInfoFile": "../../build/untrusted.tsbuildinfo", "composite": true }, - "references": [{ "path": "../types" }], + "references": [{ "path": "../types" }, { "path": "../test" }], "include": ["**/*", "**/*.json"] } diff --git a/test/e2e/bare/bareSandbox.spec.ts b/test/e2e/bare/bareSandbox.spec.ts index d3d2fec..050fae5 100644 --- a/test/e2e/bare/bareSandbox.spec.ts +++ b/test/e2e/bare/bareSandbox.spec.ts @@ -15,7 +15,7 @@ import runNodejsTests from '@test/lib/runNodejsTests.js'; -import * as bare from '@dist/exports/bare'; +import * as bare from '@dist/exports/bare.js'; // TODO: Import from '@dist/exports/bare' import { hardenGlobals, freezePrototypes } from '@dist/index.js'; @@ -28,13 +28,10 @@ if (process.version) { .slice(1) .split('.', 2) .map((n) => parseInt(n)); - if ( - (major === 18 && minor >= 18) || - (major === 20 && minor >= 6) || - major > 20 - ) { + if ((major === 20 && minor >= 6) || major > 20) { freezePrototypes(); } } -runNodejsTests('Bare', bare.default); +// Due to Node's CJS-from-ESM implementation, the import is bare.default.default +runNodejsTests('Bare', (bare.default as unknown as typeof bare).default); diff --git a/test/e2e/nodejs/nodejsSandbox.spec.ts b/test/e2e/nodejs/nodejsSandbox.spec.ts index d10e0d0..6e56f5c 100644 --- a/test/e2e/nodejs/nodejsSandbox.spec.ts +++ b/test/e2e/nodejs/nodejsSandbox.spec.ts @@ -15,6 +15,6 @@ import runNodejsTests from '@test/lib/runNodejsTests.js'; -import { nodejsSandbox as m } from '@dist/index'; +import { nodejsSandbox as m } from '@dist/index.js'; runNodejsTests('Node.js', m); diff --git a/test/lib/runBrowserTest.ts b/test/lib/runBrowserTest.ts index e04f4f9..57148d6 100644 --- a/test/lib/runBrowserTest.ts +++ b/test/lib/runBrowserTest.ts @@ -13,9 +13,12 @@ * PERFORMANCE OF THIS SOFTWARE. */ +import { fileURLToPath } from 'node:url'; import getCodeHelper from './getCodeHelper.js'; import { enabledBrowsers, webdriverTestSuites } from './webdriverTestSuites.js'; +const __dirname = fileURLToPath(new URL('.', import.meta.url)); + const runBrowserTest = (m: string, commonBundle?: boolean) => { const code = commonBundle ? getCodeHelper(__dirname, '../../dist/index.mjs', m) diff --git a/test/lib/runNodejsTests.ts b/test/lib/runNodejsTests.ts index ca68a61..4640ee8 100644 --- a/test/lib/runNodejsTests.ts +++ b/test/lib/runNodejsTests.ts @@ -16,7 +16,7 @@ import '~untrusted/lib/nodejsLoadWebcrypto.js'; // MUST BE AT THE TOP import assertRejectsWithFactory from '@test/lib/assertRejectsWithFactory.js'; -import baseTests from '@test/lib/baseTests.json'; +import baseTests from '@test/lib/baseTests.json' assert { type: 'json' }; import wrapper from '@test/lib/wrapper.js'; import assert from 'node:assert/strict'; import type { ISandbox } from '~/types/index.js'; diff --git a/test/lib/webdriverTestSuites.ts b/test/lib/webdriverTestSuites.ts index c1169d0..58422ef 100644 --- a/test/lib/webdriverTestSuites.ts +++ b/test/lib/webdriverTestSuites.ts @@ -15,11 +15,11 @@ import assert from 'node:assert/strict'; import webdriver from 'selenium-webdriver'; -import { Options as ChromeOptions } from 'selenium-webdriver/chrome'; -import { Options as EdgeOptions } from 'selenium-webdriver/edge'; -import { Options as FirefoxOptions } from 'selenium-webdriver/firefox'; -import { Options as SafariOptions } from 'selenium-webdriver/safari'; -import baseTests from './baseTests.json'; +import { Options as ChromeOptions } from 'selenium-webdriver/chrome.js'; +import { Options as EdgeOptions } from 'selenium-webdriver/edge.js'; +import { Options as FirefoxOptions } from 'selenium-webdriver/firefox.js'; +import { Options as SafariOptions } from 'selenium-webdriver/safari.js'; +import baseTests from './baseTests.json' assert { type: 'json' }; export const enabledBrowsers = () => { const webdriverBrowsers = new Set( diff --git a/tsconfig.json b/tsconfig.json index bf0b607..ba10a9c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es6", "composite": true, - "module": "commonjs", + "module": "esnext", "moduleResolution": "node", "outDir": "dist", "rootDir": "src", @@ -35,7 +35,10 @@ "checkJs": false }, "ts-node": { - "experimentalResolver": true + "experimentalResolver": true, + "compilerOptions": { + "module": "esnext" + } }, "references": [ { "path": "src/trusted" },