Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: privatenumber/tsx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.0.0
Choose a base ref
...
head repository: privatenumber/tsx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.1.0
Choose a head ref
  • 4 commits
  • 9 files changed
  • 2 contributors

Commits on Nov 10, 2023

  1. Copy the full SHA
    4833f86 View commit details
  2. Copy the full SHA
    02b43e5 View commit details
  3. Copy the full SHA
    9835a9f View commit details
  4. fix(cache): use process.geteuid() for virtual users (#376)

    Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
    ST-DDT and privatenumber authored Nov 10, 2023
    Copy the full SHA
    bf033b0 View commit details
Showing with 66 additions and 24 deletions.
  1. +6 −1 package.json
  2. +14 −0 patches/@types__node@20.9.0.patch
  3. +19 −7 pnpm-lock.yaml
  4. +7 −3 src/esm/loaders.ts
  5. +0 −5 src/suppress-warnings.cts
  6. +2 −0 src/utils/node-features.ts
  7. +16 −3 src/utils/transform/cache.ts
  8. +0 −4 tests/utils/node-supports.ts
  9. +2 −1 tests/utils/node-versions.ts
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@
"@ampproject/remapping": "^2.2.1",
"@pvtnbr/eslint-config": "^0.37.0",
"@types/cross-spawn": "^6.0.3",
"@types/node": "^20.6.0",
"@types/node": "^20.9.0",
"@types/react": "^18.2.21",
"@types/semver": "^7.5.1",
"@types/source-map-support": "^0.5.7",
@@ -95,5 +95,10 @@
"ignorePatterns": [
"tests/fixtures"
]
},
"pnpm": {
"patchedDependencies": {
"@types/node@20.9.0": "patches/@types__node@20.9.0.patch"
}
}
}
14 changes: 14 additions & 0 deletions patches/@types__node@20.9.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/module.d.ts b/module.d.ts
index 3d04c3303f0eae7b23af318b68bc101ad6fe2613..d7797b36b268f7d4eca81dda508cc17b9f10d159 100644
--- a/module.d.ts
+++ b/module.d.ts
@@ -211,7 +211,8 @@ declare module "module" {
/**
* An object whose key-value pairs represent the assertions for the module to import
*/
- importAssertions: ImportAssertions;
+ importAssertions?: ImportAssertions;
+ importAttributes?: ImportAssertions;
}
interface LoadFnOutput {
format: ModuleFormat;
26 changes: 19 additions & 7 deletions pnpm-lock.yaml

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

10 changes: 7 additions & 3 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import type { TransformOptions } from 'esbuild';
import { transform, transformDynamicImport } from '../utils/transform';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { importAttributes } from '../utils/node-features';
import {
tsconfigPathsMatcher,
fileMatcher,
@@ -245,6 +246,8 @@ export const resolve: resolve = async function (
}
};

const contextAttributesProperty = importAttributes ? 'importAttributes' : 'importAssertions';

export const load: LoadHook = async function (
url,
context,
@@ -262,10 +265,11 @@ export const load: LoadHook = async function (
}

if (isJsonPattern.test(url)) {
if (!context.importAssertions) {
context.importAssertions = {};
if (!context[contextAttributesProperty]) {
context[contextAttributesProperty] = {};
}
context.importAssertions.type = 'json';

context[contextAttributesProperty]!.type = 'json';
}

const loaded = await defaultLoad(url, context);
5 changes: 0 additions & 5 deletions src/suppress-warnings.cts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// Deprecated: Move to preflight.cts & delete entry-point in next major

const ignoreWarnings = new Set([
'--experimental-loader is an experimental feature. This feature could change at any time',
'Custom ESM Loaders is an experimental feature. This feature could change at any time',

// Changed in Node v18.13.0 via https://github.com/nodejs/node/pull/45424
'Custom ESM Loaders is an experimental feature and might change at any time',

// For JSON modules via https://github.com/nodejs/node/pull/46901
'Import assertions are not a stable feature of the JavaScript language. Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.',

'`globalPreload` is planned for removal in favor of `initialize`. `globalPreload` is an experimental feature and might change at any time',
]);

const { emit } = process;
2 changes: 2 additions & 0 deletions src/utils/node-features.ts
Original file line number Diff line number Diff line change
@@ -16,3 +16,5 @@ const compareNodeVersion = (version: Version) => (
export const isolatedLoader = compareNodeVersion([20, 0, 0]) >= 0;

export const supportsModuleRegister = compareNodeVersion([20, 6, 0]) >= 0;

export const importAttributes = compareNodeVersion([21, 0, 0]) >= 0;
19 changes: 16 additions & 3 deletions src/utils/transform/cache.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,21 @@ const getTime = () => Math.floor(Date.now() / 1e8);

const tmpdir = os.tmpdir();
const noop = () => {};

/**
* Cache directory is based on the user's identifier
* to avoid permission issues when accessed by a different user
*/
const { geteuid } = process;
const userId = (
geteuid
// For Linux users with virtual users on CI (e.g. Docker)
? geteuid()

// Use username on Windows because it doesn't have id
: os.userInfo().username
);

class FileCache<ReturnType> extends Map<string, ReturnType> {
/**
* By using tmpdir, the expectation is for the OS to clean any files
@@ -22,9 +37,7 @@ class FileCache<ReturnType> extends Map<string, ReturnType> {
cacheDirectory = path.join(
// Write permissions by anyone
tmpdir,

// Write permissions only by current user
`tsx-${os.userInfo().uid}`,
`tsx-${userId}`,
);

// Maintained so we can remove it on Windows
4 changes: 0 additions & 4 deletions tests/utils/node-supports.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tests/utils/node-versions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export const nodeVersions = [
'20',
'21',
...(
(
process.env.CI
&& process.platform !== 'win32'
)
? [
'20',
'18',
] as const
: [] as const