bundledNpmAssets.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { execFileSync } from "node:child_process";
  2. import { createHash } from "node:crypto";
  3. import { existsSync } from "node:fs";
  4. import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  5. import { tmpdir } from "node:os";
  6. import { join } from "node:path";
  7. import { afterEach, describe, expect, it, vi } from "vitest";
  8. import {
  9. BUNDLED_NPM_INTEGRITY,
  10. BUNDLED_NPM_VERSION,
  11. bundledNpmTarballUrl,
  12. stageBundledNpm,
  13. } from "../scripts/bundled-npm-assets.mjs";
  14. const roots: string[] = [];
  15. async function makeRoot(): Promise<string> {
  16. const root = await mkdtemp(join(tmpdir(), "zcode-bundled-npm-"));
  17. roots.push(root);
  18. return root;
  19. }
  20. afterEach(async () => {
  21. await Promise.all(roots.splice(0).map((root) => rm(root, { force: true, recursive: true })));
  22. });
  23. /** 按 npm registry tarball 布局(顶层 package/)打一个最小 npm 包。 */
  24. async function createFakeNpmTarball(root: string, options: { withNpx?: boolean } = {}) {
  25. const sourceRoot = join(root, "source");
  26. const packageRoot = join(sourceRoot, "package");
  27. await mkdir(join(packageRoot, "bin"), { recursive: true });
  28. await mkdir(join(packageRoot, "docs"), { recursive: true });
  29. await mkdir(join(packageRoot, "man", "man1"), { recursive: true });
  30. await mkdir(join(packageRoot, "node_modules", "dep"), { recursive: true });
  31. await writeFile(join(packageRoot, "package.json"), '{"name":"npm"}');
  32. await writeFile(join(packageRoot, "bin", "npm-cli.js"), "// npm");
  33. if (options.withNpx !== false) {
  34. await writeFile(join(packageRoot, "bin", "npx-cli.js"), "// npx");
  35. }
  36. await writeFile(join(packageRoot, "docs", "index.html"), "docs");
  37. await writeFile(join(packageRoot, "man", "man1", "npm.1"), "man");
  38. await writeFile(join(packageRoot, "node_modules", "dep", "index.js"), "// dep");
  39. const tarballPath = join(root, "npm.tgz");
  40. execFileSync("tar", ["-czf", tarballPath, "-C", sourceRoot, "package"]);
  41. const integrity = `sha512-${createHash("sha512")
  42. .update(await readFile(tarballPath))
  43. .digest("base64")}`;
  44. return { integrity, tarballPath };
  45. }
  46. describe("bundled npm constants", () => {
  47. it("pins an exact npm version and its registry sha512", () => {
  48. expect(BUNDLED_NPM_VERSION).toMatch(/^\d+\.\d+\.\d+$/);
  49. expect(BUNDLED_NPM_INTEGRITY).toMatch(/^sha512-[A-Za-z0-9+/]+=*$/);
  50. expect(bundledNpmTarballUrl("12.2.0")).toBe("https://registry.npmjs.org/npm/-/npm-12.2.0.tgz");
  51. });
  52. });
  53. describe("stageBundledNpm", () => {
  54. it("downloads, verifies and stages npm without docs and man pages", async () => {
  55. const root = await makeRoot();
  56. const fake = await createFakeNpmTarball(root);
  57. const download = vi.fn(async (_url: string, destination: string) => {
  58. await copyFile(fake.tarballPath, destination);
  59. });
  60. const glmDir = join(root, "glm");
  61. const stagedRoot = await stageBundledNpm({
  62. cacheDir: join(root, "cache"),
  63. download,
  64. glmDir,
  65. integrity: fake.integrity,
  66. version: "1.2.3",
  67. });
  68. expect(stagedRoot).toBe(join(glmDir, "node-runtime", "npm"));
  69. expect(download).toHaveBeenCalledWith(
  70. "https://registry.npmjs.org/npm/-/npm-1.2.3.tgz",
  71. expect.any(String),
  72. );
  73. expect(existsSync(join(stagedRoot, "bin", "npm-cli.js"))).toBe(true);
  74. expect(existsSync(join(stagedRoot, "bin", "npx-cli.js"))).toBe(true);
  75. expect(existsSync(join(stagedRoot, "node_modules", "dep", "index.js"))).toBe(true);
  76. expect(existsSync(join(stagedRoot, "docs"))).toBe(false);
  77. expect(existsSync(join(stagedRoot, "man"))).toBe(false);
  78. });
  79. it("reuses a verified cached tarball without downloading", async () => {
  80. const root = await makeRoot();
  81. const fake = await createFakeNpmTarball(root);
  82. const cacheDir = join(root, "cache");
  83. await mkdir(cacheDir, { recursive: true });
  84. await copyFile(fake.tarballPath, join(cacheDir, "npm-1.2.3.tgz"));
  85. const download = vi.fn();
  86. await stageBundledNpm({
  87. cacheDir,
  88. download,
  89. glmDir: join(root, "glm"),
  90. integrity: fake.integrity,
  91. version: "1.2.3",
  92. });
  93. expect(download).not.toHaveBeenCalled();
  94. });
  95. it("replaces a corrupt cached tarball", async () => {
  96. const root = await makeRoot();
  97. const fake = await createFakeNpmTarball(root);
  98. const cacheDir = join(root, "cache");
  99. await mkdir(cacheDir, { recursive: true });
  100. await writeFile(join(cacheDir, "npm-1.2.3.tgz"), "corrupt");
  101. const download = vi.fn(async (_url: string, destination: string) => {
  102. await copyFile(fake.tarballPath, destination);
  103. });
  104. await stageBundledNpm({
  105. cacheDir,
  106. download,
  107. glmDir: join(root, "glm"),
  108. integrity: fake.integrity,
  109. version: "1.2.3",
  110. });
  111. expect(download).toHaveBeenCalledTimes(1);
  112. expect(await readFile(join(cacheDir, "npm-1.2.3.tgz"))).toEqual(
  113. await readFile(fake.tarballPath),
  114. );
  115. });
  116. it("rejects a download whose sha512 does not match and keeps no cache", async () => {
  117. const root = await makeRoot();
  118. const fake = await createFakeNpmTarball(root);
  119. const cacheDir = join(root, "cache");
  120. await expect(
  121. stageBundledNpm({
  122. cacheDir,
  123. download: async (_url: string, destination: string) => {
  124. await copyFile(fake.tarballPath, destination);
  125. },
  126. glmDir: join(root, "glm"),
  127. integrity: "sha512-AAAA",
  128. version: "1.2.3",
  129. }),
  130. ).rejects.toThrow(/sha512 mismatch/);
  131. expect(existsSync(join(cacheDir, "npm-1.2.3.tgz"))).toBe(false);
  132. });
  133. it("fails when the package has no npx entry", async () => {
  134. const root = await makeRoot();
  135. const fake = await createFakeNpmTarball(root, { withNpx: false });
  136. await expect(
  137. stageBundledNpm({
  138. cacheDir: join(root, "cache"),
  139. download: async (_url: string, destination: string) => {
  140. await copyFile(fake.tarballPath, destination);
  141. },
  142. glmDir: join(root, "glm"),
  143. integrity: fake.integrity,
  144. version: "1.2.3",
  145. }),
  146. ).rejects.toThrow(/npx-cli\.js/);
  147. });
  148. });