Skip to content

Commit 19ddc8e

Browse files
committed
fix(cli): preserve upload errors and support early Node 20
1 parent b38b556 commit 19ddc8e

5 files changed

Lines changed: 72 additions & 34 deletions

File tree

‎apps/docs/scripts/fixtures/content-baseline.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11262,7 +11262,7 @@
1126211262
"meta": null,
1126311263
"sha256": "48d4019bc1905094aec776873af1d5a4e94ec2f52d043c51bbbb559b6a876944",
1126411264
"bytes": 276,
11265-
"line": 824
11265+
"line": 788
1126611266
},
1126711267
{
1126811268
"lang": "bash",

‎apps/sim/scripts/test-gitlab-csv-permissions-e2e.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const admin = generateId()
4141
const member = generateId()
4242
const otherAdmin = generateId()
4343
const billingOwner = generateId()
44+
const otherBillingOwner = generateId()
4445
const workspace = generateId()
4546
const kb = generateId()
4647
const source = generateId()
@@ -102,7 +103,7 @@ async function writeReport(status: 'running' | 'failed' | 'passed') {
102103
try {
103104
await writeReport('running')
104105
await sql.begin(async (tx) => {
105-
for (const id of [admin, member, otherAdmin, billingOwner]) {
106+
for (const id of [admin, member, otherAdmin, billingOwner, otherBillingOwner]) {
106107
await tx`insert into "user" (id, name, email, email_verified, created_at, updated_at)
107108
values (${id}, 'GitLab CSV fixture', ${`${id}@fixture.test`}, true, now(), now())`
108109
}
@@ -111,7 +112,7 @@ try {
111112
await tx`insert into member (id, organization_id, user_id, role)
112113
values (${generateId()}, ${id}, ${id === org ? admin : otherAdmin}, 'admin')`
113114
await tx`insert into member (id, organization_id, user_id, role)
114-
values (${generateId()}, ${id}, ${billingOwner}, 'owner')`
115+
values (${generateId()}, ${id}, ${id === org ? billingOwner : otherBillingOwner}, 'owner')`
115116
}
116117
await tx`insert into member (id, organization_id, user_id, role)
117118
values (${generateId()}, ${org}, ${member}, 'member')`
@@ -741,7 +742,7 @@ try {
741742
await tx`delete from workspace where id = ${workspace}`
742743
await tx`delete from project where id = ${workspace}`
743744
await tx`delete from organization where id in (${org}, ${otherOrg})`
744-
await tx`delete from "user" where id in (${admin}, ${member}, ${otherAdmin}, ${billingOwner})`
745+
await tx`delete from "user" where id in (${admin}, ${member}, ${otherAdmin}, ${billingOwner}, ${otherBillingOwner})`
745746
})
746747
} catch (error) {
747748
failure ??= getErrorMessage(error)

‎packages/sim-cli/src/commands/source-permissions.test.ts‎

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,70 @@ function snapshot(content: string, dispose: () => Promise<void>) {
3333
}
3434

3535
describe('Search permission CSV upload', () => {
36-
it('cancels a stalled snapshot and releases its read lease', async () => {
36+
it('cancels a stalled snapshot on Node 20 without AbortSignal.any and releases its lease', async () => {
3737
const controller = new AbortController()
38+
const snapshotController = new AbortController()
39+
const originalAny = Object.getOwnPropertyDescriptor(AbortSignal, 'any')
40+
Object.defineProperty(AbortSignal, 'any', { value: undefined, configurable: true })
3841
let disposed = false
3942
let cancelled = false
43+
try {
44+
const result = await runEmbeddedCli(
45+
args,
46+
{ ...identity, signal: controller.signal },
47+
{
48+
openFile: async () => ({
49+
size: 100,
50+
signal: snapshotController.signal,
51+
stream: async () =>
52+
new ReadableStream({
53+
pull() {
54+
controller.abort()
55+
},
56+
cancel() {
57+
cancelled = true
58+
},
59+
}),
60+
dispose: async () => {
61+
disposed = true
62+
},
63+
}),
64+
}
65+
)
66+
expect(result.exitCode).toBe(1)
67+
expect(disposed).toBe(true)
68+
expect(cancelled).toBe(true)
69+
expect(result.stderr).toContain('cancelled')
70+
} finally {
71+
if (originalAny) Object.defineProperty(AbortSignal, 'any', originalAny)
72+
}
73+
})
74+
75+
it('preserves snapshot I/O failures instead of blaming CSV syntax', async () => {
76+
let requests = 0
4077
const result = await runEmbeddedCli(
4178
args,
42-
{ ...identity, signal: controller.signal },
79+
{
80+
...identity,
81+
transport: async () => {
82+
requests++
83+
return Response.json({})
84+
},
85+
},
4386
{
4487
openFile: async () => ({
4588
size: 100,
46-
stream: async () =>
47-
new ReadableStream({
48-
pull() {
49-
controller.abort()
50-
},
51-
cancel() {
52-
cancelled = true
53-
},
54-
}),
55-
dispose: async () => {
56-
disposed = true
89+
stream: async () => {
90+
throw new Error('Remote snapshot reader unavailable')
5791
},
92+
dispose: async () => {},
5893
}),
5994
}
6095
)
6196
expect(result.exitCode).toBe(1)
62-
expect(disposed).toBe(true)
63-
expect(cancelled).toBe(true)
64-
expect(result.stderr).toContain('cancelled')
97+
expect(requests).toBe(0)
98+
expect(result.stderr).toContain('Remote snapshot reader unavailable')
99+
expect(result.stderr).not.toContain('quoting')
65100
})
66101

67102
it('splits multibyte records by bytes before the row limit without corrupting quoted fields', async () => {

‎packages/sim-cli/src/http/client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ function resolveTimeoutMs(env: NodeJS.ProcessEnv = process.env): number {
401401
* on the earliest 20.x releases calling it would throw a bare `TypeError`
402402
* before the request was ever made — turning a supported runtime into a crash.
403403
*/
404-
function combineSignals(
404+
export function combineSignals(
405405
caller: AbortSignal | undefined,
406406
timeout: AbortSignal | undefined
407407
): AbortSignal | undefined {

‎packages/sim-cli/src/transfer/permission-csv.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { mkdtemp, open, rm, writeFile } from 'node:fs/promises'
33
import { tmpdir } from 'node:os'
44
import { basename, join } from 'node:path'
55
import { addAbortSignal, Readable } from 'node:stream'
6-
import { parse } from 'csv-parse'
6+
import { CsvError, parse } from 'csv-parse'
77
import { embedStore } from '#sim-cli/embed-context'
8-
import { SimApiError } from '#sim-cli/http/client'
8+
import { combineSignals, SimApiError } from '#sim-cli/http/client'
99
import { embeddedFileKey } from '#sim-cli/transfer/local-file'
1010

1111
const 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

Comments
 (0)