Skip to content

Commit 1cf6eba

Browse files
lxsmnsycclaude
andcommitted
fix(start): keep file and blob metadata in server function uploads
A multipart part always arrives as a file, with `application/octet-stream` for an empty type, a `lastModified` of the time the server parsed it, and `%22` in the name decoded to `"`. The upload node now carries the type, and for a file the name and `lastModified`, and the server rebuilds the value from them over the part's bytes. A node with only the index, from an older client, still reads as the part. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 998ff3a commit 1cf6eba

3 files changed

Lines changed: 94 additions & 6 deletions

File tree

‎.changeset/upload-blob-metadata.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Keep the metadata of files and blobs sent to server functions. A blob arrives as a `Blob` instead of a `File` named `blob`, an empty type stays empty instead of `application/octet-stream`, and a file keeps its exact name and its `lastModified`. The server still reads uploads from clients that send only the part index.

‎packages/start/src/fns/uploads.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createPlugin, type SerovalNode } from "seroval";
12
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
23

34
import { deserializeFromJSONString, serializeToJSONString } from "./serialization.ts";
@@ -109,6 +110,49 @@ describe("server function uploads", () => {
109110
expect(await (received as Blob).text()).toBe("{}");
110111
});
111112

113+
it("keeps a blob a blob, with its empty type", async () => {
114+
const [received] = await roundTrip(new Blob(["raw"]), 1);
115+
116+
// Every part arrives as a file, and an empty type as application/octet-stream.
117+
expect(received).toBeInstanceOf(Blob);
118+
expect(received).not.toBeInstanceOf(File);
119+
expect((received as Blob).type).toBe("");
120+
expect(await (received as Blob).text()).toBe("raw");
121+
});
122+
123+
it("keeps the name and lastModified of a file", async () => {
124+
// The form encoder escapes a quote as %22, and the parser decodes every %22.
125+
const file = new File(["x"], 'a"b%22.txt', { type: "text/plain", lastModified: 1234 });
126+
127+
const [received] = await roundTrip(file, 1);
128+
129+
expect((received as File).name).toBe('a"b%22.txt');
130+
expect((received as File).lastModified).toBe(1234);
131+
});
132+
133+
it("reads an upload from a client that sends only the index", async () => {
134+
// The plugin of clients before the metadata fields.
135+
const legacy = createPlugin<Blob, { i: SerovalNode }>({
136+
tag: "solid-start/upload",
137+
test: value => value instanceof Blob,
138+
parse: {
139+
stream: (_value, ctx) => ({ i: ctx.parse(0) }),
140+
},
141+
serialize: () => "",
142+
deserialize: () => {
143+
throw new Error("unused");
144+
},
145+
});
146+
const form = new FormData();
147+
form.set(ARGS_PART, await serializeToJSONString([new File(["x"], "x.txt")], [legacy]));
148+
form.set(`${FILE_PART}0`, new File(["x"], "x.txt"));
149+
150+
const [received] = (await extractBody("", false, uploadRequest(form))) as [File];
151+
152+
expect(received.name).toBe("x.txt");
153+
expect(await received.text()).toBe("x");
154+
});
155+
112156
it("copies a blob from another frame", async () => {
113157
const source = new Blob(["other"], { type: "text/plain" });
114158
// Not an instance of this realm's Blob, as one from another frame isn't.

‎packages/start/src/fns/uploads.ts‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
7486
export 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

Comments
 (0)