build.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Modified by MiniMax for MCode Sandbox Runtime.
  2. /**
  3. * Build the JVM proxy agent (vendor/java-proxy-agent-src) into
  4. * vendor/java-proxy-agent/srt-proxy-agent.jar.
  5. *
  6. * Same model as the seccomp / srt-win helpers: only the source is in git,
  7. * the artifact is built by the release workflow and shipped in the npm
  8. * package, and consumers that bundle srt point at their own copy via the
  9. * `javaAgentJarPath` config key. The jar is reproducible: `jar --date`
  10. * pins entry timestamps, and STORED entries in a fixed order.
  11. *
  12. * npm run build:java-agent (needs a JDK >= 17 on PATH or JAVA_HOME)
  13. */
  14. import { spawnSync } from 'node:child_process'
  15. import {
  16. mkdtempSync,
  17. readdirSync,
  18. rmSync,
  19. statSync,
  20. writeFileSync,
  21. } from 'node:fs'
  22. import { tmpdir } from 'node:os'
  23. import { dirname, join } from 'node:path'
  24. import { fileURLToPath } from 'node:url'
  25. import { run } from '../build-common.js'
  26. const here = dirname(fileURLToPath(import.meta.url))
  27. const SRC = join(here, '..', 'java-proxy-agent-src')
  28. const OUT = join(here, 'srt-proxy-agent.jar')
  29. const AGENT_CLASS = 'com.minimax.mcode.srt.ProxyAgent'
  30. const bin = (tool: string) =>
  31. process.env.JAVA_HOME ? join(process.env.JAVA_HOME, 'bin', tool) : tool
  32. const javac = bin('javac')
  33. const jar = bin('jar')
  34. const version = spawnSync(javac, ['-version'], { encoding: 'utf8' })
  35. if (version.error) {
  36. console.error(
  37. `java-proxy-agent build: cannot run ${javac}: ${version.error.message}`,
  38. )
  39. process.exit(1)
  40. }
  41. function walk(dir: string): string[] {
  42. const out: string[] = []
  43. for (const name of readdirSync(dir).sort()) {
  44. const p = join(dir, name)
  45. if (statSync(p).isDirectory()) out.push(...walk(p))
  46. else out.push(p)
  47. }
  48. return out
  49. }
  50. const work = mkdtempSync(join(tmpdir(), 'mcode-sandbox-java-agent-'))
  51. try {
  52. const classes = join(work, 'classes')
  53. const sources = walk(SRC).filter(f => f.endsWith('.java'))
  54. // --release 8: the agent runs inside whatever JVM the sandboxed tool
  55. // brings, and Java 8 is still common in build tooling.
  56. run([
  57. javac,
  58. '--release',
  59. '8',
  60. '-Xlint:all,-options',
  61. '-Werror',
  62. '-g:none',
  63. '-d',
  64. classes,
  65. ...sources,
  66. ])
  67. const manifest = join(work, 'MANIFEST.MF')
  68. writeFileSync(
  69. manifest,
  70. `Premain-Class: ${AGENT_CLASS}\nAgent-Class: ${AGENT_CLASS}\n`,
  71. )
  72. // -0: STORED (no compression). --date: fixed entry timestamps. Entries
  73. // are added in a fixed order (walk() sorts) so the bytes are reproducible.
  74. const entries = walk(classes).map(f => f.slice(classes.length + 1))
  75. run([
  76. jar,
  77. '--create',
  78. '--file',
  79. OUT,
  80. '--manifest',
  81. manifest,
  82. '--no-compress',
  83. '--date',
  84. '1980-01-01T00:00:02Z',
  85. // -C applies to the single following file, so repeat it per entry.
  86. ...entries.flatMap(e => ['-C', classes, e]),
  87. ])
  88. console.log(`built ${OUT} (${statSync(OUT).size} bytes)`)
  89. } finally {
  90. rmSync(work, { recursive: true, force: true })
  91. }