earendil-works/pi · error · CborError

CBOR text strings must contain valid Unicode scalar values

Error message

CBOR text strings must contain valid Unicode scalar values

What it means

Round-trip check in encodeText: re-decoding the UTF-8 bytes with a fatal decoder does not reproduce the original string. This catches lone surrogates (invalid Unicode scalar values) that the encoder would otherwise emit as invalid UTF-8, which the receiving decoder would reject.

Solutions

  1. Remove lone surrogates and invalid Unicode scalar values from the string before encoding.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/protocol/src/cbor/encoder.ts:114 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/fe8318db95a03d3e. Report an issue: GitHub.

Appendix: source

Thrown at packages/protocol/src/cbor/encoder.ts:114

	} else {
		writer.writeByte(prefix | 27);
		writer.writeUint64(value);
	}
}

function isPlainObject(value: unknown): value is Record<string, unknown> {
	if (typeof value !== "object" || value === null) return false;
	const prototype = Object.getPrototypeOf(value);
	return prototype === Object.prototype || prototype === null;
}

function encodeText(writer: CborWriter, value: string, options: ResolvedCborOptions): void {
	const bytes = textEncoder.encode(value);
	if (bytes.byteLength > options.maxByteLength) {
		throw new CborError(`CBOR text string length exceeds configured limit of ${options.maxByteLength}`);
	}
	if (textDecoder.decode(bytes) !== value)
		throw new CborError("CBOR text strings must contain valid Unicode scalar values");
	writeArgument(writer, 3, bytes.byteLength);
	writer.writeBytes(bytes);
}

function encodeValue(
	writer: CborWriter,
	value: unknown,
	options: ResolvedCborOptions,
	depth: number,
	ancestors: Set<object>,
): void {
	if (depth > options.maxDepth)
		throw new CborError(`CBOR nesting depth exceeds configured limit of ${options.maxDepth}`);

	if (value === null) {
		writer.writeByte(0xf6);
		return;
	}

View on GitHub (pinned to 4af9d21d3b)