Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/sim-cli/src/runtime/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ describe('JSON flags that name a file', () => {
})
rmSync(path)
})

it.each([
['UTF-16LE, as a Windows PowerShell 5.1 redirect writes it', 'utf16le'],
['UTF-8 with a byte-order mark', 'utf8'],
] as const)('reads @path saved as %s', async (_, encoding) => {
const path = join(tmpdir(), `sim-cli-arg-${encoding}.json`)
writeFileSync(path, Buffer.from('{"name":"Café","state":{"blocks":{}}}\r\n', encoding))
try {
expect(await coerce(`@${path}`, field, {}, 'workflow')).toEqual({
name: 'Café',
state: { blocks: {} },
})
} finally {
rmSync(path)
}
})
})

describe('folder paths are typed by the name the app shows', () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/sim-cli/src/runtime/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,17 @@ function readArgumentDescriptor(descriptor: number): string {
chunks.push(Buffer.from(buffer.subarray(0, read)))
}

return Buffer.concat(chunks).toString('utf8')
return decodeArgumentBytes(Buffer.concat(chunks))
}

/**
* Decodes UTF-8, or UTF-16LE when the bytes start with its byte-order mark, and drops the mark.
* Windows PowerShell 5.1 writes `>` redirects as UTF-16LE and `Out-File -Encoding utf8` with a
* UTF-8 mark, so a file saved from `sim … > workflow.json` would otherwise not parse as JSON.
*/
function decodeArgumentBytes(bytes: Buffer): string {
const utf16le = bytes[0] === 0xff && bytes[1] === 0xfe
return new TextDecoder(utf16le ? 'utf-16le' : 'utf-8').decode(bytes)
}

/**
Expand Down