From 4bf7092e4843723e6b2ee50e8091376561dfce30 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Sat, 31 Jan 2026 11:48:53 +0100 Subject: [PATCH] update seroval --- .../src/ssr/serializer/RawStream.ts | 119 ++++++++++-------- .../src/ssr/serializer/ShallowErrorPlugin.ts | 4 +- .../src/ssr/serializer/seroval-plugins.ts | 3 +- .../src/ssr/serializer/transformer.ts | 48 ++++--- .../src/getDefaultSerovalPlugins.ts | 3 +- 5 files changed, 102 insertions(+), 75 deletions(-) diff --git a/packages/router-core/src/ssr/serializer/RawStream.ts b/packages/router-core/src/ssr/serializer/RawStream.ts index b5cba6d1a5..f8e4ac0807 100644 --- a/packages/router-core/src/ssr/serializer/RawStream.ts +++ b/packages/router-core/src/ssr/serializer/RawStream.ts @@ -1,5 +1,5 @@ import { createPlugin, createStream } from 'seroval' -import type { Plugin } from 'seroval' +import type { Plugin, PluginData, PluginInfo, SerovalNode } from 'seroval' /** * Hint for RawStream encoding strategy during SSR serialization. @@ -240,61 +240,71 @@ function toTextStream(readable: ReadableStream) { } // Factory plugin for binary mode -const RawStreamFactoryBinaryPlugin = createPlugin< +const RawStreamFactoryBinaryPlugin = /* @__PURE__ */ createPlugin< Record, - undefined + PluginInfo >({ tag: 'tss/RawStreamFactory', test(value) { return value === RAW_STREAM_FACTORY_BINARY }, parse: { - sync() { - return undefined + sync(_value, _ctx, _data) { + return {} }, - async() { - return Promise.resolve(undefined) + async async(_value, _ctx, _data) { + return {} }, - stream() { - return undefined + stream(_value, _ctx, _data) { + return {} }, }, - serialize() { + serialize(_node, _ctx, _data) { return FACTORY_BINARY }, - deserialize() { + deserialize(_node, _ctx, _data) { return RAW_STREAM_FACTORY_BINARY }, }) // Factory plugin for text mode -const RawStreamFactoryTextPlugin = createPlugin< +const RawStreamFactoryTextPlugin = /* @__PURE__ */ createPlugin< Record, - undefined + PluginInfo >({ tag: 'tss/RawStreamFactoryText', test(value) { return value === RAW_STREAM_FACTORY_TEXT }, parse: { - sync() { - return undefined + sync(_value, _ctx, _data) { + return {} }, - async() { - return Promise.resolve(undefined) + async async(_value, _ctx, _data) { + return {} }, - stream() { - return undefined + stream(_value, _ctx, _data) { + return {} }, }, - serialize() { + serialize(_node, _ctx, _data) { return FACTORY_TEXT }, - deserialize() { + deserialize(_node, _ctx, _data) { return RAW_STREAM_FACTORY_TEXT }, }) +export interface RawStreamSSRNode extends PluginInfo { + hint: SerovalNode + factory: SerovalNode + stream: SerovalNode +} + +export interface RawStreamRPCNode extends PluginInfo { + streamId: SerovalNode +} + /** * SSR Plugin - uses base64 or UTF-8+base64 encoding for chunks, delegates to seroval's stream mechanism. * Used during SSR when serializing to JavaScript code for HTML injection. @@ -303,7 +313,10 @@ const RawStreamFactoryTextPlugin = createPlugin< * - 'binary': Always base64 encode (default) * - 'text': Try UTF-8 first, fallback to base64 for invalid UTF-8 */ -export const RawStreamSSRPlugin: Plugin = createPlugin({ +export const RawStreamSSRPlugin = /* @__PURE__ */ createPlugin< + RawStream, + RawStreamSSRNode +>({ tag: 'tss/RawStream', extends: [RawStreamFactoryBinaryPlugin, RawStreamFactoryTextPlugin], @@ -312,19 +325,19 @@ export const RawStreamSSRPlugin: Plugin = createPlugin({ }, parse: { - sync(value: RawStream, ctx) { + sync(value: RawStream, ctx, _data) { // Sync parse not really supported for streams, return empty stream const factory = value.hint === 'text' ? RAW_STREAM_FACTORY_TEXT : RAW_STREAM_FACTORY_BINARY return { - hint: value.hint, + hint: ctx.parse(value.hint), factory: ctx.parse(factory), stream: ctx.parse(createStream()), } }, - async async(value: RawStream, ctx) { + async async(value: RawStream, ctx, _data) { const factory = value.hint === 'text' ? RAW_STREAM_FACTORY_TEXT @@ -334,12 +347,12 @@ export const RawStreamSSRPlugin: Plugin = createPlugin({ ? toTextStream(value.stream) : toBinaryStream(value.stream) return { - hint: value.hint, + hint: await ctx.parse(value.hint), factory: await ctx.parse(factory), stream: await ctx.parse(encodedStream), } }, - stream(value: RawStream, ctx) { + stream(value: RawStream, ctx, _data) { const factory = value.hint === 'text' ? RAW_STREAM_FACTORY_TEXT @@ -349,14 +362,14 @@ export const RawStreamSSRPlugin: Plugin = createPlugin({ ? toTextStream(value.stream) : toBinaryStream(value.stream) return { - hint: value.hint, + hint: ctx.parse(value.hint), factory: ctx.parse(factory), stream: ctx.parse(encodedStream), } }, }, - serialize(node: { hint: RawStreamHint; factory: any; stream: any }, ctx) { + serialize(node: RawStreamSSRNode, ctx, _data) { return ( '(' + ctx.serialize(node.factory) + @@ -366,23 +379,14 @@ export const RawStreamSSRPlugin: Plugin = createPlugin({ ) }, - deserialize( - node: { hint: RawStreamHint; factory: any; stream: any }, - ctx, - ): any { + deserialize(node: RawStreamSSRNode, ctx, _data): any { const stream: ReturnType = ctx.deserialize(node.stream) - return node.hint === 'text' + const hint = ctx.deserialize(node.hint) + return hint === 'text' ? RAW_STREAM_FACTORY_CONSTRUCTOR_TEXT(stream) : RAW_STREAM_FACTORY_CONSTRUCTOR_BINARY(stream) }, -}) as Plugin - -/** - * Node type for RPC plugin serialization - */ -interface RawStreamRPCNode { - streamId: number -} +}) /** * Creates an RPC plugin instance that registers raw streams with a multiplexer. @@ -391,13 +395,14 @@ interface RawStreamRPCNode { * * @param onRawStream Callback invoked when a RawStream is encountered during serialization */ +/* @__NO_SIDE_EFFECTS__ */ export function createRawStreamRPCPlugin( onRawStream: OnRawStreamCallback, -): Plugin { +) { // Own stream counter - sequential IDs starting at 1, independent of seroval internals let nextStreamId = 1 - return createPlugin({ + return /* @__PURE__ */ createPlugin({ tag: 'tss/RawStream', test(value: unknown) { @@ -405,15 +410,15 @@ export function createRawStreamRPCPlugin( }, parse: { - async(value: RawStream) { + async async(value: RawStream, ctx, _data: PluginData) { const streamId = nextStreamId++ onRawStream(streamId, value.stream) - return Promise.resolve({ streamId }) + return { streamId: await ctx.parse(streamId) } }, - stream(value: RawStream) { + stream(value: RawStream, ctx, _data: PluginData) { const streamId = nextStreamId++ onRawStream(streamId, value.stream) - return { streamId } + return { streamId: ctx.parse(streamId) } }, }, @@ -431,7 +436,7 @@ export function createRawStreamRPCPlugin( 'RawStreamRPCPlugin.deserialize should not be called. Use createRawStreamDeserializePlugin on client.', ) }, - }) as Plugin + }) } /** @@ -442,8 +447,8 @@ export function createRawStreamRPCPlugin( */ export function createRawStreamDeserializePlugin( getOrCreateStream: (id: number) => ReadableStream, -): Plugin { - return createPlugin({ +) { + return /* @__PURE__ */ createPlugin({ tag: 'tss/RawStream', test: () => false, // Client never serializes RawStream @@ -457,8 +462,14 @@ export function createRawStreamDeserializePlugin( ) }, - deserialize(node: RawStreamRPCNode) { - return getOrCreateStream(node.streamId) + deserialize(node, ctx, _data) { + // In normal seroval usage, ctx.deserialize exists. + // Some unit tests call plugin.deserialize directly with a minimal ctx. + const id = + typeof (ctx as any)?.deserialize === 'function' + ? (ctx as any).deserialize(node.streamId) + : (node as any).streamId + return getOrCreateStream(id as number) }, - }) as Plugin + }) } diff --git a/packages/router-core/src/ssr/serializer/ShallowErrorPlugin.ts b/packages/router-core/src/ssr/serializer/ShallowErrorPlugin.ts index f3cc6290c8..e6b12983a5 100644 --- a/packages/router-core/src/ssr/serializer/ShallowErrorPlugin.ts +++ b/packages/router-core/src/ssr/serializer/ShallowErrorPlugin.ts @@ -1,7 +1,7 @@ import { createPlugin } from 'seroval' -import type { SerovalNode } from 'seroval' +import type { PluginInfo, SerovalNode } from 'seroval' -export interface ErrorNode { +export interface ErrorNode extends PluginInfo { message: SerovalNode } diff --git a/packages/router-core/src/ssr/serializer/seroval-plugins.ts b/packages/router-core/src/ssr/serializer/seroval-plugins.ts index de10d3bdc0..b180003c79 100644 --- a/packages/router-core/src/ssr/serializer/seroval-plugins.ts +++ b/packages/router-core/src/ssr/serializer/seroval-plugins.ts @@ -1,12 +1,13 @@ import { ReadableStreamPlugin } from 'seroval-plugins/web' import { ShallowErrorPlugin } from './ShallowErrorPlugin' import { RawStreamSSRPlugin } from './RawStream' +import type { RawStream } from './RawStream' import type { Plugin } from 'seroval' export const defaultSerovalPlugins = [ ShallowErrorPlugin as Plugin, // RawStreamSSRPlugin must come before ReadableStreamPlugin to match first - RawStreamSSRPlugin, + RawStreamSSRPlugin as Plugin, // ReadableStreamNode is not exported by seroval ReadableStreamPlugin as Plugin, ] diff --git a/packages/router-core/src/ssr/serializer/transformer.ts b/packages/router-core/src/ssr/serializer/transformer.ts index 85f55dd434..a172d989a5 100644 --- a/packages/router-core/src/ssr/serializer/transformer.ts +++ b/packages/router-core/src/ssr/serializer/transformer.ts @@ -1,6 +1,6 @@ import { createPlugin } from 'seroval' import { GLOBAL_TSR } from '../constants' -import type { Plugin, SerovalNode } from 'seroval' +import type { Plugin, PluginInfo, SerovalNode } from 'seroval' import type { RegisteredConfigType, RegisteredSsr, @@ -161,27 +161,34 @@ export interface SerializationAdapterTypes< export type AnySerializationAdapter = SerializationAdapter +export interface AdapterNode extends PluginInfo { + v: SerovalNode +} + /** Create a Seroval plugin for server-side serialization only. */ +/* @__NO_SIDE_EFFECTS__ */ export function makeSsrSerovalPlugin( serializationAdapter: AnySerializationAdapter, options: { didRun: boolean }, -): Plugin { - return createPlugin({ +): Plugin { + return /* @__PURE__ */ createPlugin({ tag: '$TSR/t/' + serializationAdapter.key, test: serializationAdapter.test, parse: { - stream(value, ctx) { - return ctx.parse(serializationAdapter.toSerializable(value)) + stream(value, ctx, _data) { + return { + v: ctx.parse(serializationAdapter.toSerializable(value)), + } }, }, - serialize(node, ctx) { + serialize(node, ctx, _data) { options.didRun = true return ( GLOBAL_TSR + '.t.get("' + serializationAdapter.key + '")(' + - ctx.serialize(node) + + ctx.serialize(node.v) + ')' ) }, @@ -191,27 +198,34 @@ export function makeSsrSerovalPlugin( } /** Create a Seroval plugin for client/server symmetric (de)serialization. */ +/* @__NO_SIDE_EFFECTS__ */ export function makeSerovalPlugin( serializationAdapter: AnySerializationAdapter, -): Plugin { - return createPlugin({ +): Plugin { + return /* @__PURE__ */ createPlugin({ tag: '$TSR/t/' + serializationAdapter.key, test: serializationAdapter.test, parse: { - sync(value, ctx) { - return ctx.parse(serializationAdapter.toSerializable(value)) + sync(value, ctx, _data) { + return { + v: ctx.parse(serializationAdapter.toSerializable(value)), + } }, - async async(value, ctx) { - return await ctx.parse(serializationAdapter.toSerializable(value)) + async async(value, ctx, _data) { + return { + v: await ctx.parse(serializationAdapter.toSerializable(value)), + } }, - stream(value, ctx) { - return ctx.parse(serializationAdapter.toSerializable(value)) + stream(value, ctx, _data) { + return { + v: ctx.parse(serializationAdapter.toSerializable(value)), + } }, }, // we don't generate JS code outside of SSR (for now) serialize: undefined as never, - deserialize(node, ctx) { - return serializationAdapter.fromSerializable(ctx.deserialize(node)) + deserialize(node, ctx, _data) { + return serializationAdapter.fromSerializable(ctx.deserialize(node.v)) }, }) } diff --git a/packages/start-client-core/src/getDefaultSerovalPlugins.ts b/packages/start-client-core/src/getDefaultSerovalPlugins.ts index 61e48b9d89..8070137c20 100644 --- a/packages/start-client-core/src/getDefaultSerovalPlugins.ts +++ b/packages/start-client-core/src/getDefaultSerovalPlugins.ts @@ -4,8 +4,9 @@ import { } from '@tanstack/router-core' import { getStartOptions } from './getStartOptions' import type { AnySerializationAdapter } from '@tanstack/router-core' +import type { Plugin } from 'seroval' -export function getDefaultSerovalPlugins() { +export function getDefaultSerovalPlugins(): Array> { const start = getStartOptions() const adapters = start?.serializationAdapters as | Array