@@ -43,7 +43,14 @@ const UPLOAD_TAG = "solid-start/upload";
4343 * bodies for a while, because an open tab can call a newer server.
4444 */
4545
46- type UploadNode = { i : SerovalNode } ;
46+ /**
47+ * The index of the part, and what a multipart part does not keep: the type
48+ * (an empty one arrives as `application/octet-stream`), whether the value was
49+ * a blob or a file (every part arrives as a file), and the file's name (some
50+ * characters are escaped) and `lastModified`. Clients before these fields
51+ * send only the index.
52+ */
53+ type UploadNode = { i : SerovalNode ; t ?: SerovalNode ; n ?: SerovalNode ; l ?: SerovalNode } ;
4754
4855/**
4956 * True for a file or a blob. One from another frame is an instance of that
@@ -66,20 +73,43 @@ function malformed(): Error {
6673 return new Error ( "Malformed server function upload." ) ;
6774}
6875
76+ function isFile ( value : Blob ) : value is File {
77+ const file = value as Partial < File > ;
78+ return typeof file . name === "string" && typeof file . lastModified === "number" ;
79+ }
80+
6981/**
7082 * Serializes each file or blob in the arguments as its index in `files`, so
7183 * the call can send it as a form part. Seroval calls this wherever it finds
7284 * one: in arrays, objects, maps, sets and form data.
7385 */
7486export function createUploadSerializer ( files : Blob [ ] ) : Plugin < Blob , UploadNode > {
75- const parse = ( value : Blob ) => files . push ( value ) - 1 ;
87+ // The fields are plain values, so the sync parser works for every mode.
88+ const parse = ( value : Blob , ctx : { parse ( value : unknown ) : SerovalNode } ) : UploadNode => {
89+ const node : UploadNode = { i : ctx . parse ( files . push ( value ) - 1 ) , t : ctx . parse ( value . type ) } ;
90+ if ( isFile ( value ) ) {
91+ node . n = ctx . parse ( value . name ) ;
92+ node . l = ctx . parse ( value . lastModified ) ;
93+ }
94+ return node ;
95+ } ;
7696 return createPlugin < Blob , UploadNode > ( {
7797 tag : UPLOAD_TAG ,
7898 test : isBlob ,
7999 parse : {
80- sync : ( value , ctx ) => ( { i : ctx . parse ( parse ( value ) ) } ) ,
81- async : async ( value , ctx ) => ( { i : await ctx . parse ( parse ( value ) ) } ) ,
82- stream : ( value , ctx ) => ( { i : ctx . parse ( parse ( value ) ) } ) ,
100+ sync : parse ,
101+ async : async ( value , ctx ) => {
102+ const node : UploadNode = {
103+ i : await ctx . parse ( files . push ( value ) - 1 ) ,
104+ t : await ctx . parse ( value . type ) ,
105+ } ;
106+ if ( isFile ( value ) ) {
107+ node . n = await ctx . parse ( value . name ) ;
108+ node . l = await ctx . parse ( value . lastModified ) ;
109+ }
110+ return node ;
111+ } ,
112+ stream : parse ,
83113 } ,
84114 serialize : ( node , ctx ) => ctx . serialize ( node . i ) ,
85115 deserialize : ( ) => {
@@ -118,7 +148,16 @@ export function createUploadDeserializer(form: FormData): Plugin<Blob, UploadNod
118148 }
119149 const part = form . get ( `${ FILE_PART } ${ index } ` ) ;
120150 if ( ! ( part instanceof Blob ) ) throw malformed ( ) ;
121- return part ;
151+ // A client before the metadata fields sends only the index.
152+ if ( node . t === undefined ) return part ;
153+ const type = ctx . deserialize ( node . t ) ;
154+ if ( typeof type !== "string" ) throw malformed ( ) ;
155+ // The new blob or file refers to the bytes of the part instead of copying them.
156+ if ( node . n === undefined ) return new Blob ( [ part ] , { type } ) ;
157+ const name = ctx . deserialize ( node . n ) ;
158+ const lastModified = node . l === undefined ? undefined : ctx . deserialize ( node . l ) ;
159+ if ( typeof name !== "string" || typeof lastModified !== "number" ) throw malformed ( ) ;
160+ return new File ( [ part ] , name , { type, lastModified } ) ;
122161 } ,
123162 } ) ;
124163}
0 commit comments