Merge branch 'master' into BUDI-9016/extract-componenterrors-from-client

This commit is contained in:
Adria Navarro 2025-02-03 10:27:32 +01:00 committed by GitHub
commit 2a7d7d5ac2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 86 additions and 15 deletions

View File

@ -107,4 +107,15 @@ describe("Test isolated vm directly", () => {
)
expect(result).toEqual([])
})
it("should ensure error results are cleared between runs", () => {
const context = {}
// throw error
// Ensure the first execution throws an error
expect(() => runJSWithIsolatedVM(`test.foo.bar = 123`, context)).toThrow()
// Ensure the error is not persisted across VMs
const secondResult = runJSWithIsolatedVM(`return {}`, context)
expect(secondResult).toEqual({})
})
})

View File

@ -186,6 +186,7 @@ export class IsolatedVM implements VM {
code = `
try {
results = {}
results['${this.runResultKey}']=${this.codeWrapper(code)}
} catch (e) {
results['${this.runErrorKey}']=e

View File

@ -1,22 +1,52 @@
import csv from "csvtojson"
export async function jsonFromCsvString(csvString: string) {
const castedWithEmptyValues = await csv({ ignoreEmpty: true }).fromString(
csvString
)
const possibleDelimiters = [",", ";", ":", "|", "~", "\t", " "]
// By default the csvtojson library casts empty values as empty strings. This
// is causing issues on conversion. ignoreEmpty will remove the key completly
// if empty, so creating this empty object will ensure we return the values
// with the keys but empty values
const result = await csv({ ignoreEmpty: false }).fromString(csvString)
result.forEach((r, i) => {
for (const [key] of Object.entries(r).filter(([, value]) => value === "")) {
if (castedWithEmptyValues[i][key] === undefined) {
r[key] = null
for (let i = 0; i < possibleDelimiters.length; i++) {
let headers: string[] | undefined = undefined
let headerMismatch = false
try {
// By default the csvtojson library casts empty values as empty strings. This
// is causing issues on conversion. ignoreEmpty will remove the key completly
// if empty, so creating this empty object will ensure we return the values
// with the keys but empty values
const result = await csv({
ignoreEmpty: false,
delimiter: possibleDelimiters[i],
}).fromString(csvString)
for (const [, row] of result.entries()) {
// The purpose of this is to find rows that have been split
// into the wrong number of columns - Any valid .CSV file will have
// the same number of colums in each row
// If the number of columms in each row is different to
// the number of headers, this isn't the right delimiter
const columns = Object.keys(row)
if (headers == null) {
headers = columns
}
if (headers.length === 1 || headers.length !== columns.length) {
headerMismatch = true
break
}
for (const header of headers) {
if (row[header] === undefined || row[header] === "") {
row[header] = null
}
}
}
if (headerMismatch) {
continue
} else {
return result
}
} catch (err) {
// Splitting on the wrong delimiter sometimes throws CSV parsing error
// (eg unterminated strings), which tells us we've picked the wrong delimiter
continue
}
})
return result
}
throw new Error("Unable to determine delimiter")
}

View File

@ -29,5 +29,34 @@ describe("csv", () => {
expect(Object.keys(r)).toEqual(["id", "optional", "title"])
)
})
const possibleDelimeters = [",", ";", ":", "|", "~", "\t", " "]
const csvArray = [
["id", "title"],
["1", "aaa"],
["2", "bbb"],
["3", "c ccc"],
["", ""],
[":5", "eee5:e"],
]
test.each(possibleDelimeters)(
"Should parse with delimiter %s",
async delimiter => {
const csvString = csvArray
.map(row => row.map(col => `"${col}"`).join(delimiter))
.join("\n")
const result = await jsonFromCsvString(csvString)
expect(result).toEqual([
{ id: "1", title: "aaa" },
{ id: "2", title: "bbb" },
{ id: "3", title: "c ccc" },
{ id: null, title: null },
{ id: ":5", title: "eee5:e" },
])
}
)
})
})