mcode-tools-artifact.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { createHash } from 'node:crypto';
  2. import { mkdir, readFile, writeFile } from 'node:fs/promises';
  3. import path from 'node:path';
  4. import { Readable } from 'node:stream';
  5. import { Parser } from 'tar';
  6. export const MCODE_TOOLS_ARTIFACT = Object.freeze({
  7. url: 'https://registry.npmjs.org/@minimax-ai/code/-/code-0.3.11.tgz',
  8. integrity: 'sBOd8yvQRVuQNoGvzCDwXKaj5rQxLI3gKnojWS4+0wnMU9IXtxObSqbfBxz30Lze/zitRtSG2v76iJGHOmHRUA==',
  9. version: '0.0.4',
  10. sha256: 'e5a59ec5362e395b519317ad6fecd99f6e92308b9d985be44b64eb2f2b73a722',
  11. });
  12. // Extract unchanged artifacts from the public release package without running package scripts or extracting other files to disk.
  13. export async function extractMcodeToolsArtifact(bytes) {
  14. if (createHash('sha512').update(bytes).digest('base64') !== MCODE_TOOLS_ARTIFACT.integrity)
  15. throw new Error('Public MCode archive integrity mismatch.');
  16. const files = new Map();
  17. const prefix = 'package/embedded/mcode-tools/';
  18. await new Promise((resolve, reject) => {
  19. const parser = new Parser({
  20. strict: true,
  21. onReadEntry(entry) {
  22. if (![`${prefix}cli.mjs`, `${prefix}manifest.json`, 'package/THIRD_PARTY_NOTICES.md'].includes(entry.path)) {
  23. entry.resume();
  24. return;
  25. }
  26. if (entry.type !== 'File' || files.has(entry.path)) {
  27. reject(new Error('Unexpected mcode-tools archive entry.'));
  28. entry.resume();
  29. return;
  30. }
  31. const chunks = [];
  32. entry.on('data', chunk => chunks.push(chunk));
  33. entry.on('end', () => files.set(entry.path, Buffer.concat(chunks)));
  34. },
  35. });
  36. parser.on('error', reject);
  37. parser.on('end', resolve);
  38. Readable.from([bytes]).pipe(parser);
  39. });
  40. const cli = files.get(`${prefix}cli.mjs`);
  41. const manifestBytes = files.get(`${prefix}manifest.json`);
  42. const notices = files.get('package/THIRD_PARTY_NOTICES.md');
  43. if (!cli || !manifestBytes || !notices) throw new Error('Public archive is missing mcode-tools or its notices.');
  44. const manifest = JSON.parse(manifestBytes.toString('utf8'));
  45. if (manifest.version !== MCODE_TOOLS_ARTIFACT.version || manifest.buildEnv !== 'prod' ||
  46. manifest.entry !== 'cli.mjs' || manifest.packageName !== '@minimax/mcode-tools' ||
  47. createHash('sha256').update(cli).digest('hex') !== MCODE_TOOLS_ARTIFACT.sha256)
  48. throw new Error('Embedded mcode-tools manifest or entry integrity mismatch.');
  49. return { cli, manifest: manifestBytes, notices };
  50. }
  51. export async function copyMcodeToolsArtifact(root, outdir, fetchImpl = fetch) {
  52. const cache = path.join(root, '.cache', 'artifacts', 'code-0.3.11.tgz');
  53. let bytes;
  54. try { bytes = await readFile(cache); }
  55. catch (error) {
  56. if (error.code !== 'ENOENT') throw error;
  57. const response = await fetchImpl(MCODE_TOOLS_ARTIFACT.url, { signal: AbortSignal.timeout(120_000) });
  58. if (!response.ok) throw new Error(`Cannot download public mcode-tools artifact: HTTP ${response.status}`);
  59. bytes = Buffer.from(await response.arrayBuffer());
  60. }
  61. const artifact = await extractMcodeToolsArtifact(bytes);
  62. await mkdir(path.dirname(cache), { recursive: true });
  63. await writeFile(cache, bytes);
  64. const destination = path.join(outdir, 'embedded', 'mcode-tools');
  65. await mkdir(destination, { recursive: true });
  66. await writeFile(path.join(destination, 'cli.mjs'), artifact.cli);
  67. await writeFile(path.join(destination, 'manifest.json'), artifact.manifest);
  68. await writeFile(path.join(outdir, 'MCODE_TOOLS_NOTICES.md'), artifact.notices);
  69. }