3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 22:25:44 +01:00

Return a result where a conversion's text would pass the engine's longest string

This commit is contained in:
2026-10-11 20:30:42 +02:00
committed by Lillem4n
parent 0b3f6e8973
commit a9331b4125
14 changed files with 105 additions and 30 deletions
+11 -1
View File
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import type { JsonValue } from './json-value.ts'
import { serializeCanonicalJson } from './canonical-json.ts'
import { serializeCanonicalJson, spellsCanonicalJson } from './canonical-json.ts'
test('sorts object keys recursively', () => {
const value = { b: 1, a: { d: 2, c: 3 } }
@@ -52,3 +52,13 @@ test('spells a value nesting far past the levels a recursive walk survives', ()
assert.equal(serializeCanonicalJson(array, 'compact'), `${'['.repeat(levels)}1${']'.repeat(levels)}`)
assert.equal(serializeCanonicalJson(object, 'compact'), `${'{"a":'.repeat(levels)}1${'}'.repeat(levels)}`)
})
test('holds a text to the canonical spelling, whole', () => {
const value = { a: [1, 2] }
assert.ok(spellsCanonicalJson(value, 'compact', '{"a":[1,2]}'))
assert.ok(spellsCanonicalJson(value, 'two-space', serializeCanonicalJson(value, 'two-space')))
assert.ok(!spellsCanonicalJson(value, 'compact', '{"a":[1,2]} '))
assert.ok(!spellsCanonicalJson(value, 'compact', '{"a":[1,2]'))
assert.ok(!spellsCanonicalJson(value, 'two-space', '{"a":[1,2]}'))
assert.ok(!spellsCanonicalJson(1e21, 'compact', '1e21'))
})
+21 -4
View File
@@ -7,20 +7,37 @@ type Member = { label: string; value: JsonValue }
type Pending = string | { depth: number; value: JsonValue }
export function serializeCanonicalJson(value: JsonValue, spelling: JsonSpelling): string {
const indent = spelling === 'compact' ? '' : ' '
const text: string[] = []
writeCanonicalJson(value, spelling, (piece) => text.push(piece) > 0)
return text.join('')
}
// Stops at the first piece `text` does not hold: two-space indentation can spell a short compact text past the engine's longest string.
export function spellsCanonicalJson(value: JsonValue, spelling: JsonSpelling, text: string): boolean {
let offset = 0
const whole = writeCanonicalJson(value, spelling, (piece) => {
if (!text.startsWith(piece, offset)) return false
offset += piece.length
return true
})
return whole && offset === text.length
}
// `write` returns false to stop the walk, and the walk then returns false.
function writeCanonicalJson(value: JsonValue, spelling: JsonSpelling, write: (piece: string) => boolean): boolean {
const indent = spelling === 'compact' ? '' : ' '
const pending: Pending[] = [{ depth: 0, value }]
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
if (typeof next === 'string') {
text.push(next)
if (!write(next)) return false
continue
}
const { depth, value: held } = next
if (Array.isArray(held)) schedule(pending, '[', held.map((item) => ({ label: '', value: item })), ']', indent, depth)
else if (held !== null && typeof held === 'object') schedule(pending, '{', objectMembers(held, indent), '}', indent, depth)
else text.push(Object.is(held, -0) ? '-0' : JSON.stringify(held))
else if (!write(Object.is(held, -0) ? '-0' : JSON.stringify(held))) return false
}
return text.join('')
return true
}
function objectMembers(value: { [key: string]: JsonValue }, indent: string): Member[] {
@@ -44,6 +44,15 @@ test('refuses a value that is not an ADF document, naming the check it failed',
assert.equal(markdown(adfToLosslessMarkdown({ type: 'doc', version: Number.NaN })), 'not-an-adf-document: an ADF document holds a version number: found NaN')
})
// About 2.2 billion characters of carry JSON, past V8's, JavaScriptCore's and SpiderMonkey's longest string.
test('refuses a document whose markdown runs past the engine\'s longest string', () => {
let value: JsonValue = new Array<JsonValue>(2_200_000).fill(0)
for (let level = 0; level < 495; level += 1) value = [value]
const result = adfToLosslessMarkdown(document({ attrs: { value }, type: 'unknownBlock' }))
assert.match(markdown(result), /^unsupported-node-shape: the output runs past what this JavaScript engine holds: /)
assert.deepEqual(path(result), [])
})
test('refuses a document version the markdown cannot carry', () => {
assert.equal(code(adfToLosslessMarkdown({ type: 'doc', version: 2 })), 'unsupported-document-version')
})
+2 -2
View File
@@ -7,7 +7,7 @@ import { blockDirectiveForm, documentSpelling } from '../block-directive.ts'
import { blockNodeModel, blockNodes, isTaskItem } from '../../adf/block-nodes.ts'
import { carriedBlock } from '../opaque-carry.ts'
import { emitInlineLine } from './inline-line.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { failure, faulted, success, withinEngineLimits, type ConvertErrorPath, type Result } from '../../result.ts'
import { fencedCodeBlock } from '../commonmark/backtick-runs.ts'
import { holdsNullCharacter, isBlankLine, isThematicBreak, markerInterruptsParagraph } from '../commonmark/grammar.ts'
import { languageSlot, type LanguageSlot } from '../code-language.ts'
@@ -38,7 +38,7 @@ const listItemOpener = spellDirectiveOpener('listItem', undefined, '')
const orderedDelimiters: Delimiters = ['.', ')']
export function adfToLosslessMarkdown(document: AdfDocument): Result<string> {
return writeMarkdown(document, 'lossless')
return withinEngineLimits(() => writeMarkdown(document, 'lossless'))
}
export function writeMarkdown(document: AdfDocument, flavour: WrittenFlavour): Result<string> {
+2 -2
View File
@@ -10,7 +10,7 @@ import { isAdfNode } from '../adf/document.ts'
import { isJsonValue, nestingDepth, overNested } from '../json-value.ts'
import { largestNesting } from '../nesting.ts'
import { parseJsonText } from '../json-text.ts'
import { serializeCanonicalJson } from '../canonical-json.ts'
import { serializeCanonicalJson, spellsCanonicalJson } from '../canonical-json.ts'
export const carryFencePrefix = 'adf:'
@@ -75,7 +75,7 @@ function readCarriedJson(raw: string, spelling: JsonSpelling, levels: number, ty
if (overNested(held, levels)) {
return { fault: { code: 'unsupported-nesting-depth', message: `a carried node's JSON nests deeper than the ${levels} levels its position leaves` } }
}
if (serializeCanonicalJson(value, spelling) !== raw) {
if (!spellsCanonicalJson(value, spelling, raw)) {
const shape = spelling === 'compact' ? 'compact, keys sorted' : 'two-space indent, keys sorted'
return { fault: unsupportedNodeShape(`the opaque carry spells its node's JSON canonically: ${shape}; ${wayOut}`) }
}
@@ -448,6 +448,14 @@ test('names the canonical spelling a carried JSON reads alone', () => {
assert.equal(content(losslessMarkdownToAdf('!adf:carry{json="{\\"type\\":\\"blockCard\\",\\"attrs\\":{}}"}\n')), `${canonically}compact, keys sorted${literal}`)
})
test('refuses compact JSON in the carry fence without spelling it two-space past the engine\'s longest string', () => {
const json = `{"attrs":{"value":${'['.repeat(495)}${new Array(2_200_000).fill(0).join(',')}${']'.repeat(495)}}}`
assert.equal(
content(losslessMarkdownToAdf(`\`\`\`adf:unknownBlock\n${json}\n\`\`\`\n`)),
"unsupported-node-shape: the opaque carry spells its node's JSON canonically: two-space indent, keys sorted; !adf:codeBlock {language=\"adf:unknownBlock\"} around a bare fence keeps it a code block",
)
})
test('names the node JSON an opaque carry restores alone', () => {
const node = "unsupported-node-shape: the opaque carry holds one ADF node's JSON: this JSON is no ADF node; "
assert.equal(content(losslessMarkdownToAdf('```adf:x\n[]\n```\n')), `${node}!adf:codeBlock {language="adf:x"} around a bare fence keeps it a code block`)
@@ -70,6 +70,12 @@ test('refuses what the document guard refuses, and nothing else', () => {
assert.match(portableDocument(document(paragraph(deepInline))), /^unsupported-nesting-depth at /)
})
test('refuses a document whose text merges past the engine\'s longest string', () => {
const long = 'a'.repeat(2 ** 28)
const merged = paragraph(...Array.from({ length: 9 }, () => text(long)))
assert.equal(portableDocument(document(merged)), 'unsupported-node-shape at /')
})
test('refuses nesting past 500 levels wherever the reduction walks', () => {
const lowest = (bottom: AdfNode): string => {
let deep = bottom
@@ -3,7 +3,7 @@ import { adfDocumentFault, nodeAttrs, nodeContent, nodeMarks } from '../../adf/d
import { blockNodeModel, isTaskItem } from '../../adf/block-nodes.ts'
import { centeredImage, externalMedia } from '../external-image.ts'
import { commonMarkSpelling, largestListMarker, writeMarkdown, type SpellingMemo } from '../emit/adf-to-markdown.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { failure, faulted, success, withinEngineLimits, type ConvertErrorPath, type Result } from '../../result.ts'
import { flavourClaims, taskMarker } from './conventions.ts'
import { inlineLeaves, isBlockNodeType, oneLine, portableLink, reduceInline, writableHref } from './inline-reduction.ts'
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
@@ -46,8 +46,10 @@ const blockReducers: Readonly<Record<string, BlockReducer>> = {
}
export function adfToPortableMarkdown(document: AdfDocument): Result<string> {
const reduced = reduceToPortable(document)
return reduced.ok ? writeMarkdown(reduced.value, 'portable') : reduced
return withinEngineLimits(() => {
const reduced = reduceToPortable(document)
return reduced.ok ? writeMarkdown(reduced.value, 'portable') : reduced
})
}
export function reduceToPortable(document: AdfDocument): Result<AdfDocument> {
+22
View File
@@ -4,6 +4,8 @@ import { fileURLToPath } from 'node:url'
import { readFileSync, readdirSync } from 'node:fs'
import test from 'node:test'
import { success, withinEngineLimits } from './result.ts'
const sourceRoot = dirname(fileURLToPath(import.meta.url))
const union = /export type ConvertErrorCode =\n((?:\s+\| '[a-z-]+'\n)+)/
const declared = /'([a-z-]+)'/g
@@ -29,3 +31,23 @@ function calledCodes(): string[] {
test('every ConvertErrorCode is the code of a production call site, and every call site names a declared one', () => {
assert.deepEqual(calledCodes(), declaredCodes())
})
test('refuses where SpiderMonkey runs out of string, and lets any other throw through', () => {
const overflow = new Error('allocation size overflow')
overflow.name = 'InternalError'
assert.deepEqual(
withinEngineLimits(() => {
throw overflow
}),
{ error: { code: 'unsupported-node-shape', message: 'the output runs past what this JavaScript engine holds: allocation size overflow', path: [] }, ok: false },
)
assert.throws(() =>
withinEngineLimits(() => {
throw new TypeError('a bug')
}),
)
assert.deepEqual(
withinEngineLimits(() => success('x')),
{ ok: true, value: 'x' },
)
})
+10
View File
@@ -41,3 +41,13 @@ export function positioned<T>(result: Result<T>, position: SourcePosition): Resu
export function success<T>(value: T): { ok: true; value: T } {
return { ok: true, value }
}
// Any string concatenation throws past the engine's longest string: RangeError in V8 and JavaScriptCore, InternalError in SpiderMonkey.
export function withinEngineLimits<T>(convert: () => Result<T>): Result<T> {
try {
return convert()
} catch (error) {
if (!(error instanceof RangeError || (error instanceof Error && error.name === 'InternalError'))) throw error
return failure('unsupported-node-shape', `the output runs past what this JavaScript engine holds: ${error.message}`, [])
}
}