tstyche 8.0.0-beta.0 → 8.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,7 +24,7 @@ export class Environment {
24
24
  if (process.env["TSTYCHE_FETCH_TIMEOUT"] != null) {
25
25
  return Number.parseFloat(process.env["TSTYCHE_FETCH_TIMEOUT"]);
26
26
  }
27
- return 30;
27
+ return process.env["CI"] != null && process.env["CI"] !== "" ? 180 : 60;
28
28
  }
29
29
  static #resolveIsCi() {
30
30
  if (process.env["CI"] != null) {
package/dist/glob/Glob.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Braces } from "./Braces.js";
2
2
  export class Glob {
3
- static #reservedCharacterRegex = /[^\w\s/]/g;
3
+ static #tokenRegex = /[*?]|[^*?]+/g;
4
4
  static #parse(pattern, usageTarget) {
5
5
  const segments = pattern.split("/");
6
6
  let resultPattern = "\\.";
@@ -18,23 +18,25 @@ export class Glob {
18
18
  optionalSegmentCount++;
19
19
  }
20
20
  resultPattern += "\\/";
21
- const segmentPattern = segment.replace(Glob.#reservedCharacterRegex, Glob.#replaceReservedCharacter);
22
- if (segmentPattern !== segment) {
21
+ if (segment.includes("*") || segment.includes("?")) {
23
22
  resultPattern += "(?!(node_modules)(\\/|$))";
24
23
  }
25
- resultPattern += segmentPattern;
24
+ if (segment.startsWith("*") || segment.startsWith("?")) {
25
+ resultPattern += "(?!\\.)";
26
+ }
27
+ resultPattern += segment.replace(Glob.#tokenRegex, Glob.#replaceToken);
26
28
  }
27
29
  resultPattern += ")?".repeat(optionalSegmentCount);
28
30
  return resultPattern;
29
31
  }
30
- static #replaceReservedCharacter(match, offset) {
31
- switch (match) {
32
+ static #replaceToken(token) {
33
+ switch (token) {
32
34
  case "*":
33
- return offset === 0 ? "([^./][^/]*)?" : "([^/]*)?";
35
+ return "[^/]*";
34
36
  case "?":
35
- return offset === 0 ? "[^./]" : "[^/]";
37
+ return "[^/]";
36
38
  default:
37
- return `\\${match}`;
39
+ return RegExp.escape(token);
38
40
  }
39
41
  }
40
42
  static toRegex(patterns, target) {
@@ -86,13 +86,13 @@ export class NativeProjectService {
86
86
  }
87
87
  }
88
88
  break;
89
- default:
89
+ default: {
90
90
  if (Options.isJsonString(this.#resolvedConfig.tsconfig)) {
91
- if (this.#api
92
- .parseJsonConfigFileContent(JSON.parse(this.#resolvedConfig.tsconfig), {
91
+ const { config } = this.#api.parseConfigFileTextToJson(this.#tsconfigSyntheticPath, this.#resolvedConfig.tsconfig);
92
+ const { fileNames } = this.#api.parseJsonConfigFileContent(config, {
93
93
  configFileName: this.#tsconfigSyntheticPath,
94
- })
95
- .fileNames.includes(filePath)) {
94
+ });
95
+ if (fileNames.includes(filePath)) {
96
96
  this.#fileMap.add(this.#tsconfigSyntheticPath, this.#resolvedConfig.tsconfig);
97
97
  compilerOptions = {};
98
98
  kind = 3;
@@ -104,6 +104,7 @@ export class NativeProjectService {
104
104
  kind = 2;
105
105
  specifier = this.#resolvedConfig.tsconfig;
106
106
  }
107
+ }
107
108
  }
108
109
  if (kind !== 1 && specifier === this.#currentSpecifier && !this.#fileMap.hasChanged()) {
109
110
  return { kind, project: this.#currentProject, specifier };
@@ -201,7 +202,7 @@ export class NativeProjectService {
201
202
  }
202
203
  }
203
204
  openLayer(filePath, fileText) {
204
- const snapshot = this.#currentSnapshot.update({
205
+ using snapshot = this.#currentSnapshot.update({
205
206
  fileSystem: {
206
207
  kind: "layer",
207
208
  files: { [filePath]: fileText },
@@ -210,7 +211,6 @@ export class NativeProjectService {
210
211
  });
211
212
  const project = snapshot.getConfiguredProject(this.#tsconfigPath);
212
213
  const diagnostics = project.program.getSemanticDiagnostics(filePath);
213
- snapshot.dispose();
214
214
  return diagnostics;
215
215
  }
216
216
  }
@@ -12,7 +12,7 @@ import { FileRunner } from "./FileRunner.js";
12
12
  export class Runner {
13
13
  #eventEmitter = new EventEmitter();
14
14
  #resolvedConfig;
15
- static version = "8.0.0-beta.0";
15
+ static version = "8.0.0-beta.2";
16
16
  constructor(resolvedConfig) {
17
17
  this.#resolvedConfig = resolvedConfig;
18
18
  }
@@ -1,8 +1,25 @@
1
- import type { Diagnostic, DiagnosticsHandler } from "#diagnostic";
1
+ import { Diagnostic, type DiagnosticsHandler } from "#diagnostic";
2
+ export interface VersionMetadata {
3
+ dist: {
4
+ integrity: string;
5
+ tarball: string;
6
+ };
7
+ optionalDependencies?: Record<string, string>;
8
+ }
9
+ export interface PackageMetadata {
10
+ ["dist-tags"]: Record<string, string>;
11
+ modified: string;
12
+ name: string;
13
+ versions: Record<string, VersionMetadata>;
14
+ }
15
+ export type ResponseHandler<T> = (response: Response) => Promise<T>;
2
16
  export declare class Fetcher {
3
17
  #private;
4
- constructor(onDiagnostics: DiagnosticsHandler, retries: number, timeout: number);
5
- get(request: Request, diagnostic: () => Diagnostic, options?: {
18
+ constructor(onDiagnostics: DiagnosticsHandler, registry: string, retries: number, timeout: number);
19
+ get<T>(request: Request, onResponse: ResponseHandler<T>, diagnostic: () => Diagnostic, options?: {
20
+ suppressErrors?: boolean | undefined;
21
+ }): Promise<T | undefined>;
22
+ getMetadata<T>(packageSpecifier: string, options?: {
6
23
  suppressErrors?: boolean | undefined;
7
- }): Promise<Response | undefined>;
24
+ }): Promise<T | undefined>;
8
25
  }
@@ -1,26 +1,30 @@
1
1
  import process from "node:process";
2
+ import { Diagnostic } from "#diagnostic";
2
3
  import { sleep } from "./helpers.js";
3
4
  import { StoreDiagnosticText } from "./StoreDiagnosticText.js";
4
5
  export class Fetcher {
5
6
  #onDiagnostics;
7
+ #registry;
6
8
  #retries;
7
9
  #timeout;
8
- constructor(onDiagnostics, retries, timeout) {
10
+ constructor(onDiagnostics, registry, retries, timeout) {
9
11
  this.#onDiagnostics = onDiagnostics;
12
+ this.#registry = registry;
10
13
  this.#retries = retries;
11
14
  this.#timeout = timeout;
12
15
  }
13
- async get(request, diagnostic, options) {
16
+ async get(request, onResponse, diagnostic, options) {
14
17
  let delay = 3000;
15
- request.headers.set("User-Agent", `tstyche/8.0.0-beta.0 ${process.platform} ${process.arch}`);
18
+ request.headers.set("User-Agent", `tstyche/8.0.0-beta.2 ${process.platform} ${process.arch}`);
16
19
  for (let attempt = 0; attempt <= this.#retries; attempt++) {
17
20
  const isLastAttempt = attempt === this.#retries;
18
21
  const suppressErrors = options?.suppressErrors || !isLastAttempt;
19
22
  try {
20
23
  const response = await fetch(request, { signal: AbortSignal.timeout(this.#timeout) });
21
24
  if (response.ok) {
22
- return response;
25
+ return await onResponse(response);
23
26
  }
27
+ await response.body?.cancel();
24
28
  if (response.status >= 400 && response.status < 500 && response.status !== 429) {
25
29
  if (options?.suppressErrors) {
26
30
  return;
@@ -49,4 +53,14 @@ export class Fetcher {
49
53
  }
50
54
  return;
51
55
  }
56
+ async getMetadata(packageSpecifier, options) {
57
+ const request = new Request(new URL(packageSpecifier, this.#registry), {
58
+ headers: {
59
+ ["Accept"]: "application/vnd.npm.install-v1+json;q=1.0, application/json;q=0.8, */*",
60
+ },
61
+ });
62
+ const onResponse = (response) => response.json();
63
+ const diagnostic = () => Diagnostic.error(StoreDiagnosticText.failedToFetchMetadata(this.#registry));
64
+ return await this.get(request, onResponse, diagnostic, { suppressErrors: options?.suppressErrors });
65
+ }
52
66
  }
@@ -1,5 +1,5 @@
1
- export declare class Lock {
1
+ export declare class Lock implements Disposable {
2
2
  #private;
3
- constructor(lockFilePath: string);
4
- release(): void;
3
+ constructor(filePath: string);
4
+ [Symbol.dispose](): void;
5
5
  }
@@ -2,16 +2,23 @@ import { mkdirSync, rmSync, writeFileSync } from "node:fs";
2
2
  import process from "node:process";
3
3
  import { Path } from "#path";
4
4
  export class Lock {
5
- #lockFilePath;
6
- constructor(lockFilePath) {
7
- this.#lockFilePath = lockFilePath;
8
- mkdirSync(Path.dirname(this.#lockFilePath), { recursive: true });
9
- writeFileSync(this.#lockFilePath, "");
10
- process.on("exit", () => {
11
- this.release();
12
- });
5
+ #filePath;
6
+ #isDisposed = false;
7
+ constructor(filePath) {
8
+ this.#filePath = filePath;
9
+ mkdirSync(Path.dirname(this.#filePath), { recursive: true });
10
+ writeFileSync(this.#filePath, "");
11
+ process.on("exit", this.#onExit);
13
12
  }
14
- release() {
15
- rmSync(this.#lockFilePath, { force: true });
13
+ #onExit = () => {
14
+ this[Symbol.dispose]();
15
+ };
16
+ [Symbol.dispose]() {
17
+ if (this.#isDisposed) {
18
+ return;
19
+ }
20
+ rmSync(this.#filePath, { force: true });
21
+ process.off("exit", this.#onExit);
22
+ this.#isDisposed = true;
16
23
  }
17
24
  }
@@ -2,17 +2,12 @@ export interface Resource {
2
2
  integrity: string;
3
3
  tarball: string;
4
4
  }
5
- export interface PackageResource extends Resource {
6
- binary?: Resource & {
7
- name: string;
8
- };
9
- }
10
5
  export interface ManifestData {
11
6
  $version?: string;
12
7
  lastUpdated?: number;
13
8
  minorVersions: Array<string>;
14
9
  npmRegistry: string;
15
- packages: Record<string, PackageResource>;
10
+ packages: Record<string, Resource>;
16
11
  resolutions: Record<string, string>;
17
12
  versions: Array<string>;
18
13
  }
@@ -22,7 +17,7 @@ export declare class Manifest {
22
17
  lastUpdated: number;
23
18
  minorVersions: Array<string>;
24
19
  npmRegistry: string;
25
- packages: Record<string, PackageResource>;
20
+ packages: Record<string, Resource>;
26
21
  resolutions: Record<string, string>;
27
22
  versions: Array<string>;
28
23
  constructor(data: ManifestData);
@@ -1,11 +1,8 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import fs from "node:fs/promises";
3
- import process from "node:process";
4
- import { Diagnostic } from "#diagnostic";
5
3
  import { Path } from "#path";
6
4
  import { Version } from "#version";
7
5
  import { Manifest } from "./Manifest.js";
8
- import { StoreDiagnosticText } from "./StoreDiagnosticText.js";
9
6
  export class ManifestService {
10
7
  #fetcher;
11
8
  #manifestFilePath;
@@ -24,21 +21,8 @@ export class ManifestService {
24
21
  }
25
22
  return manifest;
26
23
  }
27
- async #fetchMetadata(packageSpecifier, options) {
28
- const diagnostic = () => Diagnostic.error(StoreDiagnosticText.failedToFetchMetadata(this.#npmRegistry));
29
- const request = new Request(new URL(packageSpecifier, this.#npmRegistry), {
30
- headers: {
31
- ["Accept"]: "application/vnd.npm.install-v1+json;q=1.0, application/json;q=0.8, */*",
32
- },
33
- });
34
- const response = await this.#fetcher.get(request, diagnostic, { suppressErrors: options?.suppressErrors });
35
- if (!response) {
36
- return;
37
- }
38
- return (await response.json());
39
- }
40
24
  async #fetch(options) {
41
- const packageMetadata = await this.#fetchMetadata("typescript", {
25
+ const packageMetadata = await this.#fetcher.getMetadata("typescript", {
42
26
  suppressErrors: options?.suppressErrors,
43
27
  });
44
28
  if (!packageMetadata) {
@@ -70,28 +54,6 @@ export class ManifestService {
70
54
  }
71
55
  }
72
56
  }
73
- for (const tag in packages) {
74
- if (!Version.isSatisfiedWith(tag, "7.0")) {
75
- continue;
76
- }
77
- const packageBinaryName = `@typescript/typescript-${process.platform}-${process.arch}`;
78
- const packageBinaryVersion = packageMetadata.versions[tag]?.optionalDependencies?.[packageBinaryName];
79
- if (!packageBinaryVersion) {
80
- continue;
81
- }
82
- const packageSpecifier = `${packageBinaryName}/${packageBinaryVersion}`;
83
- const versionMetadata = await this.#fetchMetadata(packageSpecifier, {
84
- suppressErrors: options?.suppressErrors,
85
- });
86
- if (!versionMetadata) {
87
- continue;
88
- }
89
- packages[tag].binary = {
90
- name: packageBinaryName,
91
- integrity: versionMetadata.dist.integrity,
92
- tarball: versionMetadata.dist.tarball,
93
- };
94
- }
95
57
  return new Manifest({
96
58
  minorVersions,
97
59
  npmRegistry: this.#npmRegistry,
@@ -1,8 +1,9 @@
1
+ import { type DiagnosticsHandler } from "#diagnostic";
1
2
  import type { Fetcher } from "./Fetcher.js";
2
3
  import type { LockService } from "./LockService.js";
3
4
  import type { Manifest } from "./Manifest.js";
4
5
  export declare class PackageService {
5
6
  #private;
6
- constructor(storePath: string, fetcher: Fetcher, lockService: LockService);
7
+ constructor(onDiagnostics: DiagnosticsHandler, storePath: string, fetcher: Fetcher, lockService: LockService);
7
8
  ensure(packageVersion: string, manifest: Manifest): Promise<string | undefined>;
8
9
  }
@@ -1,16 +1,20 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import fs from "node:fs/promises";
3
+ import process from "node:process";
3
4
  import { pathToFileURL } from "node:url";
4
5
  import { Diagnostic } from "#diagnostic";
5
6
  import { EventEmitter } from "#events";
6
7
  import { Path } from "#path";
8
+ import { Version } from "#version";
7
9
  import { StoreDiagnosticText } from "./StoreDiagnosticText.js";
8
10
  import { TarReader } from "./TarReader.js";
9
11
  export class PackageService {
12
+ #onDiagnostics;
10
13
  #fetcher;
11
14
  #lockService;
12
15
  #storePath;
13
- constructor(storePath, fetcher, lockService) {
16
+ constructor(onDiagnostics, storePath, fetcher, lockService) {
17
+ this.#onDiagnostics = onDiagnostics;
14
18
  this.#storePath = storePath;
15
19
  this.#fetcher = fetcher;
16
20
  this.#lockService = lockService;
@@ -18,52 +22,69 @@ export class PackageService {
18
22
  async ensure(packageVersion, manifest) {
19
23
  const packageSpecifier = `typescript@${packageVersion}`;
20
24
  const packagePath = Path.join(this.#storePath, packageSpecifier);
25
+ const packageResource = manifest.packages[packageVersion];
21
26
  const diagnostic = () => Diagnostic.error(StoreDiagnosticText.failedToFetchPackage(packageSpecifier));
22
27
  if (await this.#lockService.isLocked(packagePath, diagnostic)) {
23
28
  return;
24
29
  }
25
30
  if (!existsSync(packagePath)) {
26
31
  EventEmitter.dispatch(["store:adds", { packagePath, packageVersion }]);
27
- const resource = manifest.packages[packageVersion];
28
- let success = await this.#fetch(packagePath, packageSpecifier, resource);
29
- if (success && resource.binary) {
30
- const binaryPackagePath = Path.join(packagePath, "node_modules", resource.binary.name);
31
- const binaryPackageSpecifier = `${resource.binary.name}@${packageVersion}`;
32
- success = await this.#fetch(binaryPackagePath, binaryPackageSpecifier, resource.binary);
33
- }
34
- if (!success) {
35
- await fs.rm(packagePath, { recursive: true, force: true });
32
+ using _lock = this.#lockService.getLock(packagePath);
33
+ await using temp = await fs.mkdtempDisposable(`${packagePath}-`);
34
+ const packageJson = await this.#fetch(temp.path, packageSpecifier, packageResource);
35
+ if (!packageJson) {
36
36
  return;
37
37
  }
38
+ if (Version.isSatisfiedWith(packageVersion, "7.0")) {
39
+ const binaryPackageJson = await this.#fetchBinary(temp.path, packageVersion, packageJson);
40
+ if (!binaryPackageJson) {
41
+ return;
42
+ }
43
+ }
44
+ await fs.rename(temp.path, packagePath);
38
45
  }
39
46
  return `${pathToFileURL(packagePath)}/`;
40
47
  }
41
- async #fetch(packagePath, packageSpecifier, resource) {
48
+ async #fetch(packagePath, packageSpecifier, packageResource) {
49
+ const request = new Request(packageResource.tarball, { integrity: packageResource.integrity });
42
50
  const diagnostic = () => Diagnostic.error(StoreDiagnosticText.failedToFetchPackage(packageSpecifier));
43
- const lock = this.#lockService.getLock(packagePath);
44
- try {
45
- const request = new Request(resource.tarball, { integrity: resource.integrity });
46
- const response = await this.#fetcher.get(request, diagnostic);
47
- if (!response?.body) {
48
- return false;
51
+ const onResponse = (response) => response.arrayBuffer();
52
+ const buffer = await this.#fetcher.get(request, onResponse, diagnostic);
53
+ if (!buffer) {
54
+ return;
55
+ }
56
+ let packageJson;
57
+ const stream = new Blob([buffer]).stream().pipeThrough(new DecompressionStream("gzip"));
58
+ const tarReader = new TarReader(stream);
59
+ for await (const file of tarReader.extract()) {
60
+ const fileName = file.name.replace(/^package\//, "");
61
+ const filePath = Path.join(packagePath, fileName);
62
+ const directoryPath = Path.dirname(filePath);
63
+ if (fileName === "package.json") {
64
+ packageJson = file.content;
49
65
  }
50
- const targetPath = await fs.mkdtemp(`${packagePath}-`);
51
- const stream = response.body.pipeThrough(new DecompressionStream("gzip"));
52
- const tarReader = new TarReader(stream);
53
- for await (const file of tarReader.extract()) {
54
- const filePath = Path.join(targetPath, file.name.replace(/^package\//, ""));
55
- const directoryPath = Path.dirname(filePath);
56
- await fs.mkdir(directoryPath, { recursive: true });
57
- await fs.writeFile(filePath, file.content);
58
- if ((file.mode & fs.constants.S_IXUSR) !== 0) {
59
- await fs.chmod(filePath, file.mode);
60
- }
66
+ await fs.mkdir(directoryPath, { recursive: true });
67
+ await fs.writeFile(filePath, file.content);
68
+ if ((file.mode & fs.constants.S_IXUSR) !== 0) {
69
+ await fs.chmod(filePath, file.mode);
61
70
  }
62
- await fs.rename(targetPath, packagePath);
63
- return true;
64
71
  }
65
- finally {
66
- lock.release();
72
+ return packageJson;
73
+ }
74
+ async #fetchBinary(tempPath, packageVersion, packageJson) {
75
+ const { optionalDependencies } = JSON.parse(new TextDecoder().decode(packageJson));
76
+ const binaryName = `@typescript/typescript-${process.platform}-${process.arch}`;
77
+ const binaryVersion = optionalDependencies?.[binaryName];
78
+ if (!binaryVersion) {
79
+ this.#onDiagnostics(Diagnostic.error(StoreDiagnosticText.platformIsNotSupported(packageVersion)));
80
+ return;
81
+ }
82
+ const metadata = await this.#fetcher.getMetadata(`${binaryName}/${binaryVersion}`);
83
+ if (!metadata) {
84
+ return;
67
85
  }
86
+ const binaryPath = Path.join(tempPath, "node_modules", binaryName);
87
+ const binarySpecifier = `${binaryName}@${binaryVersion}`;
88
+ return this.#fetch(binaryPath, binarySpecifier, metadata.dist);
68
89
  }
69
90
  }
@@ -21,9 +21,9 @@ export class Store {
21
21
  static #storePath = environmentOptions.storePath;
22
22
  static #supportedTags;
23
23
  static {
24
- Store.#fetcher = new Fetcher(Store.#onDiagnostics, Store.#fetchRetries, Store.#fetchTimeout);
24
+ Store.#fetcher = new Fetcher(Store.#onDiagnostics, Store.#npmRegistry, Store.#fetchRetries, Store.#fetchTimeout);
25
25
  Store.#lockService = new LockService(Store.#onDiagnostics, Store.#fetchTimeout);
26
- Store.#packageService = new PackageService(Store.#storePath, Store.#fetcher, Store.#lockService);
26
+ Store.#packageService = new PackageService(Store.#onDiagnostics, Store.#storePath, Store.#fetcher, Store.#lockService);
27
27
  Store.#manifestService = new ManifestService(Store.#storePath, Store.#npmRegistry, Store.#fetcher);
28
28
  }
29
29
  static async #ensure(tag) {
@@ -5,6 +5,7 @@ export declare class StoreDiagnosticText {
5
5
  static failedToUpdateMetadata(registry: string): string;
6
6
  static maybeOutdatedResolution(tag: string): string;
7
7
  static networkFailure(retries: number): string;
8
+ static platformIsNotSupported(tag: string): Array<string>;
8
9
  static requestFailed(code: number): string;
9
10
  static requestTimeoutWasExceeded(timeout: number): string;
10
11
  static lockWaitTimeoutWasExceeded(timeout: number): string;
@@ -17,6 +17,12 @@ export class StoreDiagnosticText {
17
17
  static networkFailure(retries) {
18
18
  return `The network connection failed after ${retries + 1} attempts.`;
19
19
  }
20
+ static platformIsNotSupported(tag) {
21
+ return [
22
+ `Unable to resolve the binary for the 'typescript@${tag}'.`,
23
+ "The binary is not available for your platform.",
24
+ ];
25
+ }
20
26
  static requestFailed(code) {
21
27
  return `The request failed with status code ${code}.`;
22
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tstyche",
3
- "version": "8.0.0-beta.0",
3
+ "version": "8.0.0-beta.2",
4
4
  "description": "Everything You Need for Type Testing.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -45,7 +45,7 @@
45
45
  "schemas/*.json"
46
46
  ],
47
47
  "engines": {
48
- "node": ">=22"
48
+ "node": ">=24"
49
49
  },
50
50
  "peerDependenciesMeta": {
51
51
  "typescript": {