@@ -3,9 +3,9 @@ import { mkdtemp, open, rm, writeFile } from 'node:fs/promises'
33import { tmpdir } from 'node:os'
44import { basename , join } from 'node:path'
55import { addAbortSignal , Readable } from 'node:stream'
6- import { parse } from 'csv-parse'
6+ import { CsvError , parse } from 'csv-parse'
77import { embedStore } from '#sim-cli/embed-context'
8- import { SimApiError } from '#sim-cli/http/client'
8+ import { combineSignals , SimApiError } from '#sim-cli/http/client'
99import { embeddedFileKey } from '#sim-cli/transfer/local-file'
1010
1111const PART_BYTES = 1024 * 1024
@@ -94,10 +94,7 @@ export async function stagePermissionCsvs(
9494 )
9595 throw new SimApiError ( 'CSV files must be nonempty and total at most 1 GiB.' , 0 )
9696 inputBytes += size
97- const readSignal =
98- snapshot ?. signal && signal
99- ? AbortSignal . any ( [ snapshot . signal , signal ] )
100- : ( snapshot ?. signal ?? signal )
97+ const readSignal = combineSignals ( snapshot ?. signal , signal )
10198 snapshotStream = await snapshot ?. stream ( )
10299 stream = snapshotStream
103100 ? Readable . from ( snapshotChunks ( snapshotStream , readSignal ) )
@@ -107,6 +104,13 @@ export async function stagePermissionCsvs(
107104 const source = stream
108105 const decode = async function * ( ) {
109106 const decoder = new TextDecoder ( 'utf-8' , { fatal : true } )
107+ const decodeText = ( chunk ?: Uint8Array ) : string => {
108+ try {
109+ return decoder . decode ( chunk , { stream : chunk !== undefined } )
110+ } catch {
111+ throw new SimApiError ( `${ field } : CSV must contain valid UTF-8.` , 0 )
112+ }
113+ }
110114 let bytes = 0
111115 for await ( const chunk of source ) {
112116 signal ?. throwIfAborted ( )
@@ -115,11 +119,11 @@ export async function stagePermissionCsvs(
115119 if ( bytes > size ) throw new SimApiError ( `${ field } : input changed during staging.` , 0 )
116120 for ( let offset = 0 ; offset < chunk . length ; offset += 65_536 ) {
117121 readSignal ?. throwIfAborted ( )
118- yield decoder . decode ( chunk . subarray ( offset , offset + 65_536 ) , { stream : true } )
122+ yield decodeText ( chunk . subarray ( offset , offset + 65_536 ) )
119123 }
120124 }
121125 if ( bytes !== size ) throw new SimApiError ( `${ field } : input changed during staging.` , 0 )
122- yield decoder . decode ( )
126+ yield decodeText ( )
123127 }
124128 const definition = CSV_PROVIDERS [ provider ] [ field ]
125129 const header = `${ definition . header . join ( ',' ) } \n`
@@ -182,11 +186,9 @@ export async function stagePermissionCsvs(
182186 } catch ( error ) {
183187 if ( signal ?. aborted || snapshot ?. signal ?. aborted )
184188 throw new SimApiError ( 'Permission CSV upload cancelled.' , 0 )
185- if ( error instanceof SimApiError ) throw error
186- throw new SimApiError (
187- `${ field } : could not read valid UTF-8 CSV. Check the file, quoting, and record lengths.` ,
188- 0
189- )
189+ if ( error instanceof CsvError )
190+ throw new SimApiError ( `${ field } : invalid CSV quoting or record length.` , 0 )
191+ throw error
190192 } finally {
191193 stream ?. destroy ( )
192194 if ( snapshotStream && ! snapshotStream . locked ) await snapshotStream . cancel ( )
0 commit comments