|
| 1 | +// @ts-nocheck |
| 2 | +// Files and blobs in server function arguments, on the codec path. |
| 3 | +// |
| 4 | +// The client sends a call whose arguments hold files as multipart form data: |
| 5 | +// - the codec output in the `UPLOAD_ARGS_PART` field; |
| 6 | +// - each file in its own `UPLOAD_FILE_PART<index>` part, as it is, without |
| 7 | +// base64. |
| 8 | +// The codec output holds only each file's index. The request keeps the |
| 9 | +// `Serialized` tag; its multipart content type is what marks an upload. |
| 10 | +// |
| 11 | +// A seroval plugin made for the call finds the files wherever the codec |
| 12 | +// meets one: arguments, arrays, objects, maps, sets, and `FormData` entries. |
| 13 | +// The server decodes with a plugin made for the request, which turns each |
| 14 | +// index back into its part. Any other body decodes without that plugin, so |
| 15 | +// an index in a plain codec body fails to decode. |
| 16 | +// |
| 17 | +// Ported from SolidStart (solidjs/solid-start#2359). |
| 18 | + |
| 19 | +export const UPLOAD_ARGS_PART = "args"; |
| 20 | +export const UPLOAD_FILE_PART = "file"; |
| 21 | + |
| 22 | +const UPLOAD_TAG = "solid/server-function-upload"; |
| 23 | + |
| 24 | +/** |
| 25 | + * True for a file or a blob. One from another frame is an instance of that |
| 26 | + * frame's `Blob`, so its shape tells instead. |
| 27 | + */ |
| 28 | +function isBlob(value) { |
| 29 | + if (typeof Blob !== "undefined" && value instanceof Blob) return true; |
| 30 | + return ( |
| 31 | + typeof value === "object" && |
| 32 | + value !== null && |
| 33 | + typeof value.size === "number" && |
| 34 | + typeof value.type === "string" && |
| 35 | + typeof value.arrayBuffer === "function" && |
| 36 | + typeof value.slice === "function" |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function malformed() { |
| 41 | + return new TypeError("Malformed server function upload"); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * The client half. Each file or blob in the arguments serializes as its |
| 46 | + * index in `files`. A file that appears twice is pushed once, so it arrives |
| 47 | + * as one value. |
| 48 | + */ |
| 49 | +export function createUploadSerializer(files) { |
| 50 | + const indexOf = value => { |
| 51 | + const index = files.indexOf(value); |
| 52 | + return index === -1 ? files.push(value) - 1 : index; |
| 53 | + }; |
| 54 | + return { |
| 55 | + tag: UPLOAD_TAG, |
| 56 | + test: isBlob, |
| 57 | + parse: { |
| 58 | + sync: (value, ctx) => ({ i: ctx.parse(indexOf(value)) }), |
| 59 | + async: async (value, ctx) => ({ i: await ctx.parse(indexOf(value)) }), |
| 60 | + stream: (value, ctx) => ({ i: ctx.parse(indexOf(value)) }) |
| 61 | + }, |
| 62 | + serialize: (node, ctx) => ctx.serialize(node.i), |
| 63 | + deserialize: () => { |
| 64 | + throw malformed(); |
| 65 | + } |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * The server half, made for one request. Each index turns back into its |
| 71 | + * part of `form`. A bad index or a missing part fails the decode. |
| 72 | + */ |
| 73 | +export function createUploadDeserializer(form) { |
| 74 | + const refuse = () => { |
| 75 | + throw malformed(); |
| 76 | + }; |
| 77 | + return { |
| 78 | + tag: UPLOAD_TAG, |
| 79 | + test: () => false, |
| 80 | + parse: { sync: refuse, async: refuse, stream: refuse }, |
| 81 | + serialize: refuse, |
| 82 | + deserialize: (node, ctx) => { |
| 83 | + const index = ctx.deserialize(node.i); |
| 84 | + if (typeof index !== "number" || !Number.isSafeInteger(index) || index < 0) refuse(); |
| 85 | + const part = form.get(UPLOAD_FILE_PART + index); |
| 86 | + if (!isBlob(part)) refuse(); |
| 87 | + return part; |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * The request body for a call with files. `FormData` only takes blobs of its |
| 94 | + * own frame, so a blob from another frame is copied first. |
| 95 | + */ |
| 96 | +export async function createUploadBody(serialized, files) { |
| 97 | + const form = new FormData(); |
| 98 | + form.set(UPLOAD_ARGS_PART, serialized); |
| 99 | + const blobs = await Promise.all( |
| 100 | + files.map(async file => |
| 101 | + file instanceof Blob ? file : new Blob([await file.arrayBuffer()], { type: file.type }) |
| 102 | + ) |
| 103 | + ); |
| 104 | + for (let i = 0; i < blobs.length; i++) { |
| 105 | + form.set(UPLOAD_FILE_PART + i, blobs[i], files[i].name ?? "blob"); |
| 106 | + } |
| 107 | + return form; |
| 108 | +} |
0 commit comments