-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migrate): use safe JSON parser when streaming from Export HTTP A…
…PI (#5542) * refactor(util): create shared JSON parser for `@sanity/export` and `@sanity/migrate` * feat(migrate): add safe JSON parser * chore(util): update comment * chore(export): update comment * feat(migrate): use safe JSON parser when streaming from Export HTTP API * feat(migrate): add JSON options and type parameter to NDJSON utility * refactor(migrate): use type parameter * fix(migrate): add missing export
- Loading branch information
Showing
17 changed files
with
116 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,13 @@ | ||
module.exports = (line) => { | ||
try { | ||
return JSON.parse(line) | ||
} catch (err) { | ||
// Catch half-done lines with an error at the end | ||
const errorPosition = line.lastIndexOf('{"error":') | ||
if (errorPosition === -1) { | ||
err.message = `${err.message} (${line})` | ||
throw err | ||
} | ||
const {createSafeJsonParser} = require('@sanity/util/createSafeJsonParser') | ||
|
||
const errorJson = line.slice(errorPosition) | ||
const errorLine = JSON.parse(errorJson) | ||
const error = errorLine && errorLine.error | ||
if (error && error.description) { | ||
throw new Error(`Error streaming dataset: ${error.description}\n\n${errorJson}\n`) | ||
} | ||
|
||
throw err | ||
} | ||
} | ||
/** | ||
* Safe JSON parser that is able to handle lines interrupted by an error object. | ||
* | ||
* This may occur when streaming NDJSON from the Export HTTP API. | ||
* | ||
* @internal | ||
* @see {@link https://github.com/sanity-io/sanity/pull/1787 | Initial pull request} | ||
*/ | ||
module.exports = createSafeJsonParser({ | ||
errorLabel: 'Error streaming dataset', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 8 additions & 16 deletions
24
packages/@sanity/migrate/src/it-utils/__test__/json.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,31 @@ | ||
import {parseJSON} from '../json' | ||
import {createSafeJsonParser} from '../createSafeJsonParser' | ||
|
||
test('parse JSON', async () => { | ||
const gen = async function* () { | ||
yield '{"someString": "string"}' | ||
yield '{"someNumber": 42}' | ||
} | ||
|
||
const it = parseJSON(gen(), {}) | ||
const it = parseJSON(gen()) | ||
|
||
expect(await it.next()).toEqual({value: {someString: 'string'}, done: false}) | ||
expect(await it.next()).toEqual({value: {someNumber: 42}, done: false}) | ||
expect(await it.next()).toEqual({value: undefined, done: true}) | ||
}) | ||
|
||
test('parse JSON with interrupting error', async () => { | ||
test('parse JSON with a custom parser', async () => { | ||
const gen = async function* () { | ||
yield '{"someString": "string"}' | ||
yield '{"someString": "str{"error":{"description":"Some error"}}' | ||
yield '{"someNumber": 42}' | ||
} | ||
|
||
const it = parseJSON(gen(), { | ||
parse: createSafeJsonParser({ | ||
errorLabel: 'Error parsing JSON', | ||
parse: (line) => ({ | ||
parsed: JSON.parse(line), | ||
}), | ||
}) | ||
|
||
expect(await it.next()).toEqual({value: {someString: 'string'}, done: false}) | ||
|
||
await expect(async () => { | ||
await it.next() | ||
}).rejects.toThrowErrorMatchingInlineSnapshot(` | ||
"Error parsing JSON: Some error | ||
{\\"error\\":{\\"description\\":\\"Some error\\"}} | ||
" | ||
`) | ||
expect(await it.next()).toEqual({value: {parsed: {someString: 'string'}}, done: false}) | ||
expect(await it.next()).toEqual({value: {parsed: {someNumber: 42}}, done: false}) | ||
expect(await it.next()).toEqual({value: undefined, done: true}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import {split} from './split' | ||
import {decodeText} from './decodeText' | ||
import {parseJSON} from './json' | ||
import {type JSONOptions, parseJSON} from './json' | ||
import {filter} from './filter' | ||
|
||
export function ndjson(it: AsyncIterableIterator<Uint8Array>) { | ||
return parseJSON(filter(split(decodeText(it), '\n'), (line) => Boolean(line && line.trim()))) | ||
export function ndjson<Type>( | ||
it: AsyncIterableIterator<Uint8Array>, | ||
options?: JSONOptions<Type>, | ||
): AsyncIterableIterator<Type> { | ||
return parseJSON( | ||
filter(split(decodeText(it), '\n'), (line) => Boolean(line && line.trim())), | ||
options, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
# Exports | ||
/content.js | ||
/createSafeJsonParser.js | ||
/fs.js | ||
/legacyDateFormat.js | ||
/paths.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../src/createSafeJsonParser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {createSafeJsonParser} from '../src/createSafeJsonParser' | ||
|
||
const parse = createSafeJsonParser({ | ||
errorLabel: 'Error parsing JSON', | ||
}) | ||
|
||
test('parse JSON', () => { | ||
expect(parse('{"someString": "string"}')).toEqual({someString: 'string'}) | ||
expect(parse('{"someNumber": 42}')).toEqual({someNumber: 42}) | ||
}) | ||
|
||
test('parse JSON with interrupting error', () => { | ||
expect(() => parse('{"someString": "str{"error":{"description":"Some error"}}')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"Error parsing JSON: Some error | ||
{\\"error\\":{\\"description\\":\\"Some error\\"}} | ||
" | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters