Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/get-file-arguments-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/web": patch
---

A `GET` server function called with a file among its arguments (with `enableRichArguments`) now throws a clear error before sending, instead of putting `[object FormData]` in the url and failing with a 400. A `GET` call whose codec-encoded arguments are too long for a url and go over POST now sends the encoding it already made instead of encoding the arguments a second time, so a one-shot argument such as an async iterator arrives with all its chunks.
16 changes: 13 additions & 3 deletions packages/web/server-functions/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,14 @@ async function initializeResponse(base, id, options, args, meta) {
// The encoder answers `FormData` when the arguments hold files (see
// uploads.js). No content type is set here: fetch gives a string body
// `text/plain` and a form its multipart boundary, which is how the handler
// tells the two apart.
// tells the two apart. A GET call too long for its url brings the encoding
// it already made, so a one-shot argument is read once.
return createRequest(
base,
id,
{
...options,
body: await serializeArguments(args),
body: options.body ?? (await serializeArguments(args)),
headers: {
...options.headers,
[BODY_FORMAT_HEADER]: BodyFormat.Serialized
Expand Down Expand Up @@ -1210,6 +1211,9 @@ export function GET(fn) {
// The handler accepts both encodings: plain JSON and the codec's framed
// string (distinguished by the `;0x` frame prefix).
const encoded = isJSONSafe(args) ? JSON.stringify(args) : await serializeArguments(args);
if (typeof encoded !== "string") {
throw new Error("A GET server function cannot send files: use POST");
}
const url = `${address}?args=${encodeURIComponent(encoded)}`;
const absolute = new URL(url, globalThis.location?.href || "http://localhost").href;
// Arguments too long for a url call over POST instead: a cache miss, not
Expand All @@ -1220,7 +1224,13 @@ export function GET(fn) {
// says so, and a POST-shaped read must not ask the server for
// single-flight collection, which is mutation policy.
if (absolute.length > MAX_GET_URL_LENGTH) {
return fetchServerFunction(address, id, { ...opts, read: true }, args, metadata);
return fetchServerFunction(
address,
id,
{ ...opts, read: true, body: encoded },
args,
metadata
);
}
return fetchServerFunction(url, id, { ...opts, method: "GET" }, [], metadata, args);
};
Expand Down
23 changes: 21 additions & 2 deletions packages/web/test/server/server-functions-body-formats.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
GET as serverGET,
createServerReference as createServerSideReference,
handleServerFunctionRequest,
registerServerFunction
registerServerFunction,
registerServerReference
} from "@solidjs/web/server-functions/server";
import { createServerReference } from "@solidjs/web/server-functions/client";
import { GET, createServerReference } from "@solidjs/web/server-functions/client";

const RequestContext = Symbol.for("solid.RequestContext");
const BODY_FORMAT_HEADER = "X-Server-Function-Format";
Expand Down Expand Up @@ -173,6 +176,22 @@ describe("an argument with a natural HTTP encoding", () => {
});
});

describe("a GET call with a File argument", () => {
it("asks for the codec without sending a request", async () => {
serverGET(
createServerSideReference(registerServerReference("rich-get-file", async () => "ran"))
);
const transport = connectTransport();
try {
const read = GET(createServerReference("rich-get-file"));
await expect(read(new File(["hello"], "a.txt"))).rejects.toThrow(/enableRichArguments/);
} finally {
transport.restore();
}
expect(transport.requests).toEqual([]);
});
});

describe("a bound call ending in a natural encoding", () => {
it("puts the leading arguments in the url and the body last, in declared order", async () => {
let ran = 0;
Expand Down
54 changes: 52 additions & 2 deletions packages/web/test/server/server-functions-uploads.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
GET as serverGET,
createServerReference as createServerSideReference,
handleServerFunctionRequest,
registerServerFunction
registerServerFunction,
registerServerReference
} from "@solidjs/web/server-functions/server";
import { createServerReference } from "@solidjs/web/server-functions/client";
import { GET, createServerReference } from "@solidjs/web/server-functions/client";
// @ts-ignore — the rich-args dist has no adjacent type declarations.
import { enableRichArguments } from "../../server-functions/dist/rich-args.js";

Expand Down Expand Up @@ -286,6 +289,53 @@ describe("files in codec arguments", () => {
});
});

describe("a GET call with the codec", () => {
const declareRead = <A extends unknown[]>(id: string, fn: (...args: A) => unknown) => {
serverGET(createServerSideReference(registerServerReference(id, fn)));
return GET(createServerReference(id));
};

it("refuses arguments holding a file without sending a request", async () => {
const read = declareRead("get-upload-files", async (..._args: unknown[]) => "ran");
const file = () => new File(["hello"], "a.txt", { type: "text/plain" });
const form = new FormData();
form.set("doc", file());
const transport = connectTransport();
try {
for (const args of [[file()], [1, file()], [form], [{ doc: new Blob(["hi"]) }]]) {
await expect(read(...args)).rejects.toThrow("A GET server function cannot send files");
}
} finally {
transport.restore();
}
expect(transport.requests).toEqual([]);
});

it("reads a one-shot argument once when long arguments go over POST", async () => {
const read = declareRead(
"get-upload-once",
async (input: { padding: string; source: AsyncIterable<string> }) => {
const seen: string[] = [];
for await (const chunk of input.source) seen.push(chunk);
return seen;
}
);
async function* chunks() {
yield "one";
yield "two";
}
const transport = connectTransport();
try {
expect(await read({ padding: "x".repeat(3000), source: chunks() })).toEqual(["one", "two"]);
} finally {
transport.restore();
}
const [request] = transport.requests;
expect(request.method).toBe("POST");
expect(new URL(request.url).search).toBe("");
});
});

describe("malformed uploads", () => {
// A real upload body, captured, so each case changes one thing about it.
async function capture() {
Expand Down
8 changes: 4 additions & 4 deletions scripts/size/floor-caps.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"minified": 54141
},
"page: base server components (hydrating + dynamic + frames + sf reference)": {
"cap": "34.24 KB",
"minified": 102403
"cap": "34.36 KB",
"minified": 102542
},
"page: live server components (base + live/GET + action + isPending/latest)": {
"cap": "41.68 KB",
"minified": 125252
"cap": "41.70 KB",
"minified": 125407
},
"server: floor (getRequestEvent + isServer)": {
"cap": "1.33 KB",
Expand Down
Loading
Loading