peachsim 0.1.2470 → 0.1.2472

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.
Files changed (137) hide show
  1. package/cli/catalog/meta.ts +5 -0
  2. package/cli/commands/agent.ts +4 -5
  3. package/cli/internal-child-contract.ts +6 -0
  4. package/cli/internal-child.ts +38 -6
  5. package/dist-lib/agent-daemon-client.cjs +3 -3
  6. package/dist-lib/agent-events.cjs +1 -1
  7. package/dist-lib/attached-projects.cjs +1 -1
  8. package/dist-lib/auth/shared-session.cjs +1 -1
  9. package/dist-lib/backend-origin.cjs +1 -1
  10. package/dist-lib/beta.cjs +1 -1
  11. package/dist-lib/beta.mjs +2 -2
  12. package/dist-lib/bridge-constants.cjs +1 -1
  13. package/dist-lib/bridge-contract-input.cjs +1 -1
  14. package/dist-lib/bridge-contract-input.mjs +5 -5
  15. package/dist-lib/bridge-contract.cjs +1 -1
  16. package/dist-lib/bridge-contract.mjs +3 -3
  17. package/dist-lib/bundle-channel.cjs +24 -30
  18. package/dist-lib/capture-contract.cjs +1 -1
  19. package/dist-lib/capture-contract.mjs +3 -3
  20. package/dist-lib/cloud-contract.cjs +1 -1
  21. package/dist-lib/cloud-contract.mjs +3 -3
  22. package/dist-lib/cloud-test-args.cjs +1 -1
  23. package/dist-lib/cloud-test-args.mjs +2 -2
  24. package/dist-lib/cloud.cjs +1 -1
  25. package/dist-lib/cloud.mjs +7 -7
  26. package/dist-lib/config.cjs +1 -1
  27. package/dist-lib/config.mjs +5 -5
  28. package/dist-lib/detox/index.cjs +2 -2
  29. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  30. package/dist-lib/esm-chunks/{acorn-UYJCU4LQ.mjs → acorn-QIMLXJTI.mjs} +3 -3
  31. package/dist-lib/esm-chunks/{chunk-JCFAV27Q.mjs → chunk-45VKM7N5.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-Q6BHZLJE.mjs → chunk-4TLFZLBT.mjs} +3 -3
  33. package/dist-lib/esm-chunks/{chunk-63YE3HPT.mjs → chunk-4YR6CPOD.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-LI6FJRET.mjs → chunk-57KHHRE4.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-FEVD526T.mjs → chunk-BERSJG2N.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-XJBFYM6G.mjs → chunk-BLJMIS4D.mjs} +1 -1
  37. package/dist-lib/esm-chunks/{chunk-RTVCEFGY.mjs → chunk-BQ7AFT33.mjs} +4 -4
  38. package/dist-lib/esm-chunks/{chunk-ADSOZUR7.mjs → chunk-CIRUDWWA.mjs} +2 -2
  39. package/dist-lib/esm-chunks/{chunk-OBHGZUZU.mjs → chunk-DBL2SPLB.mjs} +1 -1
  40. package/dist-lib/esm-chunks/{chunk-5EVI2Q7R.mjs → chunk-GJI5PS7F.mjs} +14 -13
  41. package/dist-lib/esm-chunks/{chunk-BAJ5TJJJ.mjs → chunk-I4NQBHWT.mjs} +1 -1
  42. package/dist-lib/esm-chunks/{chunk-WYKU357M.mjs → chunk-KY5MCRDQ.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-3LUHAGKN.mjs → chunk-LODJ5JY3.mjs} +3 -3
  44. package/dist-lib/esm-chunks/{chunk-UATEBCVH.mjs → chunk-MER2MOVT.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-62GDPPRK.mjs → chunk-NOIPV2UU.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-SBGAGY36.mjs → chunk-NQ4WLOZ2.mjs} +1 -1
  47. package/dist-lib/esm-chunks/{chunk-K75XVJ6M.mjs → chunk-STZKGNIY.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-YHRVFO7U.mjs → chunk-TBT2N4IT.mjs} +4 -4
  49. package/dist-lib/esm-chunks/{chunk-CZUHIYNB.mjs → chunk-WLK6TCKI.mjs} +1 -1
  50. package/dist-lib/esm-chunks/{chunk-ZIO5YODN.mjs → chunk-ZSITGCQ4.mjs} +1 -1
  51. package/dist-lib/esm-chunks/{metro-layout-parser-O4IPDC42.mjs → metro-layout-parser-3LHDZADO.mjs} +3 -3
  52. package/dist-lib/esm-chunks/{swift-package-prebuilt-4M7BXJ3C.mjs → swift-package-prebuilt-3L57A5BV.mjs} +9 -9
  53. package/dist-lib/esm-chunks/{swift-sdk-app-module-LXZCH25K.mjs → swift-sdk-app-module-IOLCLSPU.mjs} +12 -12
  54. package/dist-lib/esm-chunks/{swift-sdk-image-OMB2EECT.mjs → swift-sdk-image-BDVQHA62.mjs} +12 -12
  55. package/dist-lib/expo-swift-bundle.cjs +22 -28
  56. package/dist-lib/expo-swift-metadata.cjs +1 -1
  57. package/dist-lib/expo-swift-metadata.mjs +7 -7
  58. package/dist-lib/home-paths.cjs +1 -1
  59. package/dist-lib/host/bridge-host.cjs +63 -37
  60. package/dist-lib/host/fetch-proxy-handler.cjs +5 -5
  61. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  62. package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
  63. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  64. package/dist-lib/host/websocket-proxy.cjs +1 -1
  65. package/dist-lib/index.cjs +1 -1
  66. package/dist-lib/kotlin-host.cjs +1 -1
  67. package/dist-lib/menu.cjs +1 -1
  68. package/dist-lib/menu.mjs +3 -3
  69. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  70. package/dist-lib/metro-fingerprint-registry.mjs +3 -3
  71. package/dist-lib/metro-production-bundle.cjs +1 -1
  72. package/dist-lib/metro-production-bundle.mjs +4 -4
  73. package/dist-lib/native-app-identity-project.cjs +2 -2
  74. package/dist-lib/native-app-identity-project.mjs +14 -14
  75. package/dist-lib/native-app-identity.cjs +1 -1
  76. package/dist-lib/native-app-identity.mjs +6 -6
  77. package/dist-lib/native-firebase-metadata.cjs +1 -1
  78. package/dist-lib/native-firebase-metadata.mjs +3 -3
  79. package/dist-lib/pdfium-raster.cjs +33 -25
  80. package/dist-lib/pdfium-raster.mjs +32 -25
  81. package/dist-lib/plist.cjs +1 -1
  82. package/dist-lib/plist.mjs +3 -3
  83. package/dist-lib/profiles.cjs +1 -1
  84. package/dist-lib/public-brand.cjs +1 -1
  85. package/dist-lib/public-origins-lib-esm.json +75 -75
  86. package/dist-lib/public-origins-lib.json +69 -69
  87. package/dist-lib/react-native-host-modules.cjs +1 -1
  88. package/dist-lib/react-native-host-modules.mjs +2 -2
  89. package/dist-lib/reap-browser.cjs +1 -1
  90. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  91. package/dist-lib/sdk.cjs +1 -1
  92. package/dist-lib/sdk.mjs +7 -7
  93. package/dist-lib/skills.cjs +69 -38
  94. package/dist-lib/swift-compile.cjs +4 -3
  95. package/dist-lib/swift-compile.mjs +9 -9
  96. package/dist-lib/swift-package.cjs +1 -1
  97. package/dist-lib/swift-package.mjs +3 -3
  98. package/dist-lib/swift-submit.cjs +1 -1
  99. package/dist-lib/swift-submit.mjs +5 -5
  100. package/dist-lib/swift-svg-raster.cjs +1 -1
  101. package/dist-lib/swift-svg-raster.mjs +2 -2
  102. package/dist-lib/swift-toolchain.cjs +1 -1
  103. package/dist-lib/swift.cjs +1 -1
  104. package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
  105. package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
  106. package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
  107. package/dist-lib/vendor/@xmldom/xmldom.cjs +1 -1
  108. package/dist-lib/vendor/acorn.cjs +1 -1
  109. package/dist-lib/vendor/bplist-parser.cjs +1 -1
  110. package/dist-lib/vendor/ignore.cjs +1 -1
  111. package/dist-lib/vendor/magic-string.cjs +1 -1
  112. package/dist-lib/vendor/plist.cjs +1 -1
  113. package/dist-lib/vendor/smol-toml.cjs +1 -1
  114. package/dist-lib/vendor/stream-json.cjs +1 -1
  115. package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
  116. package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
  117. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
  118. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
  119. package/dist-lib/vendor/sucrase.cjs +1 -1
  120. package/dist-lib/vendor/terser.cjs +1 -1
  121. package/dist-lib/vendor/valibot.cjs +1 -1
  122. package/dist-lib/vendor/ws.cjs +1 -1
  123. package/dist-lib/vendor/zod.cjs +1 -1
  124. package/dist-lib/worklet-executable.cjs +1 -1
  125. package/dist-lib/worklet-executable.mjs +3 -3
  126. package/dist-types/packages/peach-engine/src/engine/remote-display-protocol.d.ts +1 -0
  127. package/dist-types/packages/peach-engine/src/remote-stream/cloud-display.d.ts +1 -0
  128. package/dist-types/packages/peach-engine/src/remote-stream/scene-recording.d.ts +1 -0
  129. package/package.json +1 -1
  130. package/runtime-companion.json +1 -1
  131. package/src/agent-daemon-client.ts +3 -1
  132. package/src/agent-prompt.ts +7 -0
  133. package/src/canvas-agent-note-delivery.ts +16 -9
  134. package/src/case-insensitive-header-overlay.ts +4 -1
  135. package/src/host/agent-host.ts +40 -13
  136. package/src/pdfium-raster.ts +37 -24
  137. package/src/swift-pdf-raster.ts +17 -23
@@ -21,6 +21,7 @@ export interface SceneRecording {
21
21
  }
22
22
  export declare function readSceneRecordingHeader(value: unknown): SceneRecordingHeader | null;
23
23
  export declare function encodeSceneRecording(recording: SceneRecording): Promise<Uint8Array>;
24
+ export declare function encodeSceneRecordingWithin(recording: SceneRecording, maxBytes: number): Promise<Uint8Array>;
24
25
  export declare function decodeSceneRecording(compressed: Uint8Array): Promise<SceneRecording>;
25
26
  export declare function joinSceneRecordings(parts: readonly SceneRecording[]): SceneRecording;
26
27
  export declare function readSceneRecordingUpload(bytes: Uint8Array): Promise<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "peachsim",
3
- "version": "0.1.2470",
3
+ "version": "0.1.2472",
4
4
  "description": "Vite and Metro plugins, testing drivers, and SDK exports for peach.",
5
5
  "author": "Tamagui LLC",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -1 +1 @@
1
- {"abi":1,"sha256":"e7f1c9e8915dde6d33182604db7b13e169aa5c1b29cbe8f575b24818535e0b9f","file":"node/cli-e7f1c9e8915dde6d33182604db7b13e169aa5c1b29cbe8f575b24818535e0b9f.cjs"}
1
+ {"abi":1,"sha256":"44db3cfaca62afc6cb450c628036852b92ea5cd806d74b132c3aa3eccae58b04","file":"node/cli-44db3cfaca62afc6cb450c628036852b92ea5cd806d74b132c3aa3eccae58b04.cjs"}
@@ -22,6 +22,7 @@ import type {
22
22
  AgentInspectMessage,
23
23
  AgentPromptEnvelope,
24
24
  CanvasAgentNoteStatus,
25
+ CanvasAgentNoteWait,
25
26
  } from './agent-prompt.ts'
26
27
  import type { AgentSession, AttachedProject } from './attached-projects.ts'
27
28
  import type { CanvasAgentNote } from '@peach/globals'
@@ -202,8 +203,9 @@ export class AgentDaemonClient {
202
203
  async canvasNoteStatus(
203
204
  simId: string,
204
205
  messageId: string,
206
+ changedFrom: CanvasAgentNoteWait | null,
205
207
  ): Promise<CanvasAgentNoteStatus> {
206
- return this.send('agent:canvas-note-status', { simId, messageId })
208
+ return this.send('agent:canvas-note-status', { simId, messageId, changedFrom })
207
209
  }
208
210
 
209
211
  async replyCanvasNote(messageId: string, text: string): Promise<{ ok: true }> {
@@ -109,6 +109,13 @@ export function readCanvasNoteReply(value: unknown): string | null {
109
109
  : null
110
110
  }
111
111
 
112
+ // the statuses a delivery waits to leave: in the queue, then with the agent
113
+ export type CanvasAgentNoteWait = 'pending' | 'taken'
114
+
115
+ export function readCanvasAgentNoteWait(value: unknown): CanvasAgentNoteWait | null {
116
+ return value === 'pending' || value === 'taken' ? value : null
117
+ }
118
+
112
119
  export function readCanvasAgentNoteStatus(value: unknown): CanvasAgentNoteStatus | null {
113
120
  if (!value || typeof value !== 'object') return null
114
121
  const status: unknown = Reflect.get(value, 'status')
@@ -2,6 +2,7 @@ import {
2
2
  readCanvasAgentNoteStatus,
3
3
  type CanvasAgentNoteDelivery,
4
4
  type CanvasAgentNoteStatus,
5
+ type CanvasAgentNoteWait,
5
6
  } from './agent-prompt'
6
7
  import { cloudBoxIdFromSimulatorId } from './simulator-route'
7
8
  import type { CanvasAgentNote } from '@peach/globals'
@@ -10,9 +11,7 @@ export function isCanvasNoteSimulatorId(id: string): boolean {
10
11
  return /^[a-zA-Z0-9-]{1,64}$/.test(id) || cloudBoxIdFromSimulatorId(id) !== null
11
12
  }
12
13
 
13
- const STATUS_POLL_MS = 2_000
14
-
15
- function pause(signal: AbortSignal) {
14
+ function pause(ms: number, signal: AbortSignal) {
16
15
  return new Promise<void>((resolve, reject) => {
17
16
  const abort = () => {
18
17
  clearTimeout(timer)
@@ -21,34 +20,41 @@ function pause(signal: AbortSignal) {
21
20
  const timer = setTimeout(() => {
22
21
  signal.removeEventListener('abort', abort)
23
22
  resolve()
24
- }, STATUS_POLL_MS)
23
+ }, ms)
25
24
  signal.addEventListener('abort', abort, { once: true })
26
25
  if (signal.aborted) abort()
27
26
  })
28
27
  }
29
28
 
29
+ // a reader that can hold a read until the status leaves `changedFrom` answers
30
+ // as soon as it changes; `pollMs` spaces the reads that come back unchanged
30
31
  export function createCanvasAgentNoteDelivery(
31
32
  send: CanvasAgentNoteDelivery['send'],
32
- readStatus: (id: string, signal: AbortSignal) => Promise<CanvasAgentNoteStatus>,
33
+ readStatus: (
34
+ id: string,
35
+ signal: AbortSignal,
36
+ changedFrom: CanvasAgentNoteWait,
37
+ ) => Promise<CanvasAgentNoteStatus>,
38
+ pollMs: number,
33
39
  ): CanvasAgentNoteDelivery {
34
40
  return {
35
41
  send,
36
42
  async waitUntilTaken(id, signal) {
37
43
  while (true) {
38
44
  signal.throwIfAborted()
39
- const { status } = await readStatus(id, signal)
45
+ const { status } = await readStatus(id, signal, 'pending')
40
46
  if (status === 'taken' || status === 'replied') return
41
47
  if (status === 'missing') throw new Error('This note is no longer available.')
42
- await pause(signal)
48
+ await pause(pollMs, signal)
43
49
  }
44
50
  },
45
51
  async waitForReply(id, signal) {
46
52
  while (!signal.aborted) {
47
53
  // a reply can come hours later, so one failed read only waits for the next
48
- const read = await readStatus(id, signal).catch(() => null)
54
+ const read = await readStatus(id, signal, 'taken').catch(() => null)
49
55
  if (read?.status === 'replied') return read.reply
50
56
  if (read?.status === 'missing') return null
51
- await pause(signal).catch(() => {})
57
+ await pause(pollMs, signal).catch(() => {})
52
58
  }
53
59
  return null
54
60
  },
@@ -135,5 +141,6 @@ export function cloudCanvasAgentNoteDelivery(
135
141
  if (!status) throw new Error('The simulator did not report the note’s status.')
136
142
  return status
137
143
  },
144
+ 2_000,
138
145
  )
139
146
  }
@@ -40,7 +40,10 @@ function needsCaseOverlay(directory: string): boolean {
40
40
 
41
41
  function scan(directory: string, listed?: (file: string) => boolean): Node {
42
42
  const node: Node = { files: new Map(), directories: new Map() }
43
- for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
43
+ const entries = fs
44
+ .readdirSync(directory, { withFileTypes: true })
45
+ .sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
46
+ for (const entry of entries) {
44
47
  const full = path.join(directory, entry.name)
45
48
  const stat = entry.isSymbolicLink()
46
49
  ? fs.statSync(full, { throwIfNoEntry: false })
@@ -23,6 +23,7 @@ import {
23
23
  createCanvasAgentInspectMessage,
24
24
  readCanvasAgentId,
25
25
  readCanvasAgentNote,
26
+ readCanvasAgentNoteWait,
26
27
  readCanvasNoteReply,
27
28
  type CanvasAgentNoteStatus,
28
29
  type AgentInspectMessage,
@@ -59,6 +60,8 @@ import type { WebSocket } from 'ws'
59
60
  // ws.readyState constant — avoid importing the module just for this enum.
60
61
  const WS_OPEN = 1
61
62
  const SESSION_STATUS_PERSIST_DELAY_MS = 25
63
+ // under the 15 s a shell bridge command may take
64
+ const CANVAS_NOTE_STATUS_HOLD_MS = 10_000
62
65
 
63
66
  interface FanoutSubscription {
64
67
  unsubscribe: () => void
@@ -117,6 +120,8 @@ export class AgentHost {
117
120
  string,
118
121
  { simId: string; expiresAt: number; reply?: string }
119
122
  >()
123
+ // canvas status reads held until their note is taken or answered
124
+ private noteWaiters = new Map<string, Set<() => void>>()
120
125
  // agent wrappers also emit prompt-received from the FIFO reader, but the
121
126
  // shell/daemon already knows the user-facing display text when send-prompt
122
127
  // is accepted. emit the friendly prompt immediately and suppress the raw
@@ -222,6 +227,7 @@ export class AgentHost {
222
227
  this.inspectListeners.clear()
223
228
  this.inspectQueues.clear()
224
229
  this.takenNotes.clear()
230
+ for (const id of [...this.noteWaiters.keys()]) this.wakeNote(id)
225
231
  this.allSockets.clear()
226
232
  this.browserSockets.clear()
227
233
  }
@@ -296,19 +302,23 @@ export class AgentHost {
296
302
  }
297
303
  case 'agent:canvas-note-status': {
298
304
  const simId = this.requireSimId(msg, type)
299
- this.pruneTakenNotes()
300
- const pending = this.inspectQueue(simId).some(
301
- (message) => message.id === msg.messageId,
302
- )
303
- const taken = this.takenNotes.get(String(msg.messageId))
304
- const status: CanvasAgentNoteStatus = pending
305
- ? { status: 'pending' }
306
- : taken?.simId !== simId
307
- ? { status: 'missing' }
308
- : taken.reply
309
- ? { status: 'replied', reply: taken.reply }
310
- : { status: 'taken' }
311
- return status
305
+ const messageId = String(msg.messageId)
306
+ const status = this.canvasNoteStatus(simId, messageId)
307
+ if (status.status !== readCanvasAgentNoteWait(msg.changedFrom)) return status
308
+ await new Promise<void>((resolve) => {
309
+ const waiters = this.noteWaiters.get(messageId) ?? new Set()
310
+ this.noteWaiters.set(messageId, waiters)
311
+ const wake = () => {
312
+ clearTimeout(timer)
313
+ waiters.delete(wake)
314
+ if (!waiters.size && this.noteWaiters.get(messageId) === waiters)
315
+ this.noteWaiters.delete(messageId)
316
+ resolve()
317
+ }
318
+ const timer = setTimeout(wake, CANVAS_NOTE_STATUS_HOLD_MS)
319
+ waiters.add(wake)
320
+ })
321
+ return this.canvasNoteStatus(simId, messageId)
312
322
  }
313
323
  case 'agent:reply-canvas-note': {
314
324
  this.pruneTakenNotes()
@@ -317,6 +327,7 @@ export class AgentHost {
317
327
  if (!reply) throw new AgentSessionError('INVALID_REPLY', 'invalid note reply')
318
328
  if (!taken) throw new AgentSessionError('NOT_FOUND', 'no taken note with that id')
319
329
  taken.reply = reply
330
+ this.wakeNote(String(msg.messageId))
320
331
  return { ok: true }
321
332
  }
322
333
  case 'agent:get-inspect-listener-status': {
@@ -381,6 +392,21 @@ export class AgentHost {
381
392
  })
382
393
  }
383
394
 
395
+ private canvasNoteStatus(simId: string, messageId: string): CanvasAgentNoteStatus {
396
+ this.pruneTakenNotes()
397
+ if (this.inspectQueue(simId).some((message) => message.id === messageId))
398
+ return { status: 'pending' }
399
+ const taken = this.takenNotes.get(messageId)
400
+ if (taken?.simId !== simId) return { status: 'missing' }
401
+ return taken.reply ? { status: 'replied', reply: taken.reply } : { status: 'taken' }
402
+ }
403
+
404
+ private wakeNote(messageId: string) {
405
+ const waiters = this.noteWaiters.get(messageId)
406
+ this.noteWaiters.delete(messageId)
407
+ for (const wake of waiters ?? []) wake()
408
+ }
409
+
384
410
  private pruneTakenNotes() {
385
411
  const now = Date.now()
386
412
  for (const [id, receipt] of this.takenNotes) {
@@ -395,6 +421,7 @@ export class AgentHost {
395
421
  simId: message.simId,
396
422
  expiresAt: Date.now() + AGENT_NOTE_TTL_MS,
397
423
  })
424
+ this.wakeNote(message.id)
398
425
  }
399
426
 
400
427
  private inspectQueue(simId: string): AgentInspectMessage[] {
@@ -62,36 +62,49 @@ async function encodeRgbaPng(
62
62
  return png
63
63
  }
64
64
 
65
- export async function rasterizePdfBytes(
65
+ async function rasterizePage(
66
+ library: PDFiumLibrary,
66
67
  pdf: Uint8Array,
67
68
  scale: number,
68
69
  ): Promise<Uint8Array> {
69
- const library = await PDFiumLibrary.init({ disableBase64Warning: true })
70
+ const document = await library.loadDocument(pdf)
70
71
  try {
71
- const document = await library.loadDocument(pdf)
72
- try {
73
- const size = document.getPage(0).getOriginalSize()
74
- // pdfium floors a size before scaling, so ask for the pixel size at
75
- // scale 1, rounded up like the CoreGraphics rasterizer's.
76
- const width = Math.ceil(size.originalWidth * scale)
77
- const height = Math.ceil(size.originalHeight * scale)
78
- if (width <= 0 || height <= 0 || width > 12000 || height > 12000) {
79
- throw new Error(
80
- `pdf page ${size.originalWidth}x${size.originalHeight} is out of range`,
81
- )
82
- }
83
- const bitmap = await document.getPage(0).render({
84
- scale: 1,
85
- width,
86
- height,
87
- render: 'bitmap',
88
- transparent: true,
89
- })
90
- return await encodeRgbaPng(width, height, bitmap.data)
91
- } finally {
92
- document.destroy()
72
+ const size = document.getPage(0).getOriginalSize()
73
+ // pdfium floors a size before scaling, so ask for the pixel size at
74
+ // scale 1, rounded up like the CoreGraphics rasterizer's.
75
+ const width = Math.ceil(size.originalWidth * scale)
76
+ const height = Math.ceil(size.originalHeight * scale)
77
+ if (width <= 0 || height <= 0 || width > 12000 || height > 12000) {
78
+ throw new Error(
79
+ `pdf page ${size.originalWidth}x${size.originalHeight} is out of range`,
80
+ )
93
81
  }
82
+ const bitmap = await document.getPage(0).render({
83
+ scale: 1,
84
+ width,
85
+ height,
86
+ render: 'bitmap',
87
+ transparent: true,
88
+ })
89
+ return await encodeRgbaPng(width, height, bitmap.data)
90
+ } finally {
91
+ document.destroy()
92
+ }
93
+ }
94
+
95
+ export async function withPdfiumRasterizer<T>(
96
+ render: (
97
+ rasterize: (pdf: Uint8Array, scale: number) => Promise<Uint8Array>,
98
+ ) => Promise<T>,
99
+ ): Promise<T> {
100
+ const library = await PDFiumLibrary.init({ disableBase64Warning: true })
101
+ try {
102
+ return await render((pdf, scale) => rasterizePage(library, pdf, scale))
94
103
  } finally {
95
104
  library.destroy()
96
105
  }
97
106
  }
107
+
108
+ export function rasterizePdfBytes(pdf: Uint8Array, scale: number): Promise<Uint8Array> {
109
+ return withPdfiumRasterizer((rasterize) => rasterize(pdf, scale))
110
+ }
@@ -5,6 +5,7 @@ import path from 'node:path'
5
5
  import {
6
6
  RNX_INTERNAL_CHILDREN,
7
7
  RNX_INTERNAL_COMMAND,
8
+ type PdfRasterBatch,
8
9
  } from '../cli/internal-child-contract'
9
10
  import { rnxSelfInvocation } from '../cli/self-invocation'
10
11
  import { cacheDir } from './home-paths'
@@ -108,38 +109,31 @@ export function rasterizePdfFiles(
108
109
  file: path.resolve(input.file),
109
110
  output: path.join(work, `${index}.png`),
110
111
  }))
112
+ const manifest = path.join(work, 'render.json')
113
+ fs.writeFileSync(
114
+ manifest,
115
+ JSON.stringify({
116
+ requests,
117
+ imageBytes: Number.isFinite(imageBytes) ? imageBytes : null,
118
+ remainingBytes: Number.isFinite(remainingBytes) ? remainingBytes : null,
119
+ } satisfies PdfRasterBatch),
120
+ )
111
121
  if (process.platform === 'darwin') {
112
- const manifest = path.join(work, 'render.json')
113
- fs.writeFileSync(manifest, JSON.stringify({ requests, imageBytes, remainingBytes }))
114
122
  // runner-toolchain-platform: darwin
115
123
  execFileSync(
116
124
  '/usr/bin/osascript',
117
125
  ['-l', 'JavaScript', '-e', RASTER_PDF, manifest],
118
126
  { stdio: 'pipe' },
119
127
  )
128
+ } else {
129
+ const { executable, prefixArgs } = rnxSelfInvocation()
130
+ execFileSync(
131
+ executable,
132
+ [...prefixArgs, RNX_INTERNAL_COMMAND, RNX_INTERNAL_CHILDREN.pdfRaster, manifest],
133
+ { stdio: 'pipe' },
134
+ )
120
135
  }
121
136
  for (const request of requests) {
122
- if (process.platform !== 'darwin') {
123
- const { executable, prefixArgs } = rnxSelfInvocation()
124
- try {
125
- execFileSync(
126
- executable,
127
- [
128
- ...prefixArgs,
129
- RNX_INTERNAL_COMMAND,
130
- RNX_INTERNAL_CHILDREN.pdfRaster,
131
- request.file,
132
- '3',
133
- request.output,
134
- ],
135
- { stdio: 'pipe' },
136
- )
137
- } catch (error) {
138
- throw new Error(`swift pdf asset ${request.file} did not rasterize`, {
139
- cause: error,
140
- })
141
- }
142
- }
143
137
  record(request, fs.readFileSync(request.output))
144
138
  if (!writeCache) continue
145
139
  try {