From 430995ee561e5831774abe22951b179230f8f0ca Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:11:50 -0300 Subject: [PATCH 01/15] change type of rawBody to Uint8Array --- packages/kit/types/hooks.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index 1f18df4dd0b0..30893af77330 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -5,7 +5,7 @@ export type StrictBody = string | Uint8Array; export interface ServerRequest, Body = unknown> extends Location { method: string; headers: Headers; - rawBody: StrictBody; + rawBody: Uint8Array; body: ParameterizedBody; locals: Locals; } From 2a1973a19e34f255cd29bee31c23248cef6d1522 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:12:19 -0300 Subject: [PATCH 02/15] update raw body test --- packages/kit/test/apps/basics/src/routes/load/raw-body.json.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js b/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js index bb8c11432caf..eeb5387fca14 100644 --- a/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js +++ b/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js @@ -1,9 +1,10 @@ /** @type {import('@sveltejs/kit').RequestHandler} */ export function post(request) { + console.log('raw body:', request.rawBody, TextDecoder); return { body: { body: /** @type {string} */ (request.body), - rawBody: /** @type {string} */ (request.rawBody) + rawBody: new TextDecoder().decode(request.rawBody) } }; } From baba022c0a9d2a727fb4ddd8e8beedbe6aca3ac1 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:18:13 -0300 Subject: [PATCH 03/15] update `parse_body` --- .../src/runtime/server/parse_body/index.js | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/runtime/server/parse_body/index.js b/packages/kit/src/runtime/server/parse_body/index.js index f61bfddefd55..71851603d615 100644 --- a/packages/kit/src/runtime/server/parse_body/index.js +++ b/packages/kit/src/runtime/server/parse_body/index.js @@ -2,30 +2,37 @@ import { read_only_form_data } from './read_only_form_data.js'; /** * @param {import('types/hooks').StrictBody} raw - * @param {import('types/helper').Headers} headers + * @param {Partial} headers */ export function parse_body(raw, headers) { - if (!raw || typeof raw !== 'string') return raw; + if (!raw) return raw; - const [type, ...directives] = headers['content-type'].split(/;\s*/); + const content_type = headers['content-type']; + const [type, ...directives] = content_type ? content_type.split(/;\s*/) : []; + + const text = () => + typeof raw === 'string' + ? raw + : new TextDecoder(headers['content-encoding'] || 'utf-8').decode(raw); + const bytes = () => (typeof raw === 'string' ? new TextEncoder().encode(raw) : raw); switch (type) { case 'text/plain': - return raw; + return text(); case 'application/json': - return JSON.parse(raw); + return JSON.parse(text()); case 'application/x-www-form-urlencoded': - return get_urlencoded(raw); + return get_urlencoded(text()); case 'multipart/form-data': { const boundary = directives.find((directive) => directive.startsWith('boundary=')); if (!boundary) throw new Error('Missing boundary'); - return get_multipart(raw, boundary.slice('boundary='.length)); + return get_multipart(text(), boundary.slice('boundary='.length)); } default: - throw new Error(`Invalid Content-Type ${type}`); + return bytes(); } } From 82828cb50fa0e02686d90811ad0e6180cdbaf554 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:25:22 -0300 Subject: [PATCH 04/15] remove debug log from taw body test --- packages/kit/test/apps/basics/src/routes/load/raw-body.json.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js b/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js index eeb5387fca14..39cfb1ab9e1e 100644 --- a/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js +++ b/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js @@ -1,6 +1,5 @@ /** @type {import('@sveltejs/kit').RequestHandler} */ export function post(request) { - console.log('raw body:', request.rawBody, TextDecoder); return { body: { body: /** @type {string} */ (request.body), From 78d83d348a62d83f1bf87d9f73c814682af5c108 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:28:48 -0300 Subject: [PATCH 05/15] update `getRawBody` --- packages/kit/src/core/node/index.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/kit/src/core/node/index.js b/packages/kit/src/core/node/index.js index 1542dc278ee6..1469f26d8c22 100644 --- a/packages/kit/src/core/node/index.js +++ b/packages/kit/src/core/node/index.js @@ -1,5 +1,3 @@ -import { isContentTypeTextual } from '../adapter-utils.js'; - /** * @param {import('http').IncomingMessage} req * @returns {Promise} @@ -48,13 +46,6 @@ export function getRawBody(req) { } req.on('end', () => { - const [type] = (h['content-type'] || '').split(/;\s*/); - - if (isContentTypeTextual(type)) { - const encoding = h['content-encoding'] || 'utf-8'; - return fulfil(new TextDecoder(encoding).decode(data)); - } - fulfil(data); }); }); From 5f6d3943a9570a050f3b163e5d3479a847d45ca7 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:29:21 -0300 Subject: [PATCH 06/15] update adapter-cloudflare-workers --- packages/adapter-cloudflare-workers/files/entry.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/adapter-cloudflare-workers/files/entry.js b/packages/adapter-cloudflare-workers/files/entry.js index 083562dbaf65..2cba2395a393 100644 --- a/packages/adapter-cloudflare-workers/files/entry.js +++ b/packages/adapter-cloudflare-workers/files/entry.js @@ -1,7 +1,6 @@ // TODO hardcoding the relative location makes this brittle import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; // eslint-disable-line import/no-unresolved -import { isContentTypeTextual } from '@sveltejs/kit/adapter-utils'; // eslint-disable-line import/no-unresolved init(); @@ -34,7 +33,7 @@ async function handle(event) { host: request_url.host, path: request_url.pathname, query: request_url.searchParams, - rawBody: request.body ? await read(request) : null, + rawBody: await read(request), headers: Object.fromEntries(request.headers), method: request.method }); @@ -57,10 +56,5 @@ async function handle(event) { /** @param {Request} request */ async function read(request) { - const type = request.headers.get('content-type') || ''; - if (isContentTypeTextual(type)) { - return request.text(); - } - return new Uint8Array(await request.arrayBuffer()); } From 82eb36896596493359de122e966e2e8a9972b804 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:29:33 -0300 Subject: [PATCH 07/15] update adapter-netlify --- packages/adapter-netlify/files/entry.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/adapter-netlify/files/entry.js b/packages/adapter-netlify/files/entry.js index 4ad61b319f55..26f2965151d7 100644 --- a/packages/adapter-netlify/files/entry.js +++ b/packages/adapter-netlify/files/entry.js @@ -1,6 +1,5 @@ // TODO hardcoding the relative location makes this brittle import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved -import { isContentTypeTextual } from '@sveltejs/kit/adapter-utils'; // eslint-disable-line import/no-unresolved init(); @@ -9,13 +8,8 @@ export async function handler(event) { const query = new URLSearchParams(rawQuery); - const type = headers['content-type']; - const rawBody = - type && isContentTypeTextual(type) - ? isBase64Encoded - ? Buffer.from(body, 'base64').toString() - : body - : new TextEncoder('base64').encode(body); + const encoding = isBase64Encoded ? 'base64' : headers['content-encoding'] || 'utf-8'; + const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body; const rendered = await render({ method: httpMethod, From 53fc23304a8d636473e822be48ede50eedaa4976 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:30:55 -0300 Subject: [PATCH 08/15] ensure `rawBody` is `Uint8Array` --- packages/kit/src/runtime/server/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 6d9369d72150..03c42c8003dc 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -39,6 +39,10 @@ export async function respond(incoming, options, state = {}) { ...incoming, headers, body: parse_body(incoming.rawBody, headers), + rawBody: + typeof incoming.rawBody === 'string' + ? new TextEncoder().encode(incoming.rawBody) + : incoming.rawBody, params: {}, locals: {} }; From 584c72404f78d8d94ba59ad9752053a132a9c3c6 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:44:52 -0300 Subject: [PATCH 09/15] add changeset --- .changeset/short-beds-punch.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/short-beds-punch.md diff --git a/.changeset/short-beds-punch.md b/.changeset/short-beds-punch.md new file mode 100644 index 000000000000..841040549e31 --- /dev/null +++ b/.changeset/short-beds-punch.md @@ -0,0 +1,7 @@ +--- +'@sveltejs/adapter-cloudflare-workers': patch +'@sveltejs/adapter-netlify': patch +'@sveltejs/kit': patch +--- + +Ensure the raw body is an Uint8Array before passing it to request handlers From 4a3822e14a22d4514fea03530e43b3ca02710309 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Mon, 16 Aug 2021 08:45:15 -0300 Subject: [PATCH 10/15] update types inside docs --- documentation/docs/01-routing.md | 2 +- documentation/docs/04-hooks.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/documentation/docs/01-routing.md b/documentation/docs/01-routing.md index e48be5e49f32..852625458d72 100644 --- a/documentation/docs/01-routing.md +++ b/documentation/docs/01-routing.md @@ -60,7 +60,7 @@ type Request, Body = unknown> = { path: string; params: Record; query: URLSearchParams; - rawBody: string | Uint8Array; + rawBody: Uint8Array; body: ParameterizedBody; locals: Locals; // populated by hooks handle }; diff --git a/documentation/docs/04-hooks.md b/documentation/docs/04-hooks.md index 3af30a94bd00..614b13369aad 100644 --- a/documentation/docs/04-hooks.md +++ b/documentation/docs/04-hooks.md @@ -24,7 +24,7 @@ type Request> = { path: string; params: Record; query: URLSearchParams; - rawBody: string | Uint8Array; + rawBody: Uint8Array; body: ParameterizedBody; locals: Locals; // populated by hooks handle }; From 5c73398c12b7e1e1fe1b9d5da4a2107ca0dd3b02 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Mon, 16 Aug 2021 20:38:09 -0300 Subject: [PATCH 11/15] make isContentType textual private --- packages/kit/package.json | 3 --- packages/kit/rollup.config.js | 3 +-- packages/kit/src/core/adapter-utils.js | 18 ------------------ packages/kit/src/runtime/server/endpoint.js | 20 ++++++++++++++++++-- 4 files changed, 19 insertions(+), 25 deletions(-) delete mode 100644 packages/kit/src/core/adapter-utils.js diff --git a/packages/kit/package.json b/packages/kit/package.json index 9ca5d172ee0c..4dabf6cb6b1b 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -74,9 +74,6 @@ "./install-fetch": { "import": "./dist/install-fetch.js" }, - "./adapter-utils": { - "import": "./dist/adapter-utils.js" - }, "./types": "./types/index.d.ts" }, "types": "types/index.d.ts", diff --git a/packages/kit/rollup.config.js b/packages/kit/rollup.config.js index a24a103f267c..2abdbec70ff5 100644 --- a/packages/kit/rollup.config.js +++ b/packages/kit/rollup.config.js @@ -48,8 +48,7 @@ export default [ cli: 'src/cli.js', ssr: 'src/runtime/server/index.js', node: 'src/core/node/index.js', - 'install-fetch': 'src/install-fetch.js', - 'adapter-utils': 'src/core/adapter-utils.js' + 'install-fetch': 'src/install-fetch.js' }, output: { dir: 'dist', diff --git a/packages/kit/src/core/adapter-utils.js b/packages/kit/src/core/adapter-utils.js deleted file mode 100644 index ddd911eb4253..000000000000 --- a/packages/kit/src/core/adapter-utils.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Decides how the body should be parsed based on its mime type. Should match what's in parse_body - * - * This is intended to be used with both requests and responses, to have a consistent body parsing across adapters. - * - * @param {string|undefined|null} content_type The `content-type` header of a request/response. - * @returns {boolean} - */ -export function isContentTypeTextual(content_type) { - if (!content_type) return true; // defaults to json - const [type] = content_type.split(';'); // get the mime type - return ( - type === 'text/plain' || - type === 'application/json' || - type === 'application/x-www-form-urlencoded' || - type === 'multipart/form-data' - ); -} diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index f23bcc8fdcc5..f27f4ec4ca4a 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -1,4 +1,3 @@ -import { isContentTypeTextual } from '../../core/adapter-utils.js'; import { lowercase_keys } from './utils.js'; /** @param {string} body */ @@ -15,6 +14,23 @@ function is_string(s) { return typeof s === 'string' || s instanceof String; } +/** + * Decides how the body should be parsed based on its mime type. Should match what's in parse_body + * + * @param {string | undefined | null} content_type The `content-type` header of a request/response. + * @returns {boolean} + */ +function is_content_type_textual(content_type) { + if (!content_type) return true; // defaults to json + const [type] = content_type.split(';'); // get the mime type + return ( + type === 'text/plain' || + type === 'application/json' || + type === 'application/x-www-form-urlencoded' || + type === 'multipart/form-data' + ); +} + /** * @param {import('types/hooks').ServerRequest} request * @param {import('types/internal').SSREndpoint} route @@ -48,7 +64,7 @@ export async function render_endpoint(request, route, match) { headers = lowercase_keys(headers); const type = headers['content-type']; - const is_type_textual = isContentTypeTextual(type); + const is_type_textual = is_content_type_textual(type); if (!is_type_textual && !(body instanceof Uint8Array || is_string(body))) { return error( From 537ef57715a2a871b0cea4ac48466021133479a7 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Mon, 16 Aug 2021 22:42:30 -0300 Subject: [PATCH 12/15] ensure `rawBody` of `Incoming` is bytes --- packages/kit/src/core/adapt/prerender.js | 4 ++-- packages/kit/src/core/node/index.js | 6 +++--- packages/kit/src/runtime/server/page/load_node.js | 2 +- packages/kit/src/runtime/server/parse_body/index.js | 10 +++------- packages/kit/types/internal.d.ts | 5 ++--- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index e822858299a8..62cbe44e2164 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -156,7 +156,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a method: 'GET', headers: {}, path, - rawBody: '', + rawBody: new Uint8Array(), query: new URLSearchParams() }, { @@ -289,7 +289,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a method: 'GET', headers: {}, path: '[fallback]', // this doesn't matter, but it's easiest if it's a string - rawBody: '', + rawBody: new Uint8Array(), query: new URLSearchParams() }, { diff --git a/packages/kit/src/core/node/index.js b/packages/kit/src/core/node/index.js index 1469f26d8c22..a90969ad7d2b 100644 --- a/packages/kit/src/core/node/index.js +++ b/packages/kit/src/core/node/index.js @@ -1,13 +1,13 @@ /** * @param {import('http').IncomingMessage} req - * @returns {Promise} + * @returns {Promise} */ export function getRawBody(req) { return new Promise((fulfil, reject) => { const h = req.headers; if (!h['content-type']) { - return fulfil(''); + return fulfil(new Uint8Array()); } req.on('error', reject); @@ -16,7 +16,7 @@ export function getRawBody(req) { // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 if (isNaN(length) && h['transfer-encoding'] == null) { - return fulfil(''); + return fulfil(new Uint8Array()); } let data = new Uint8Array(length || 0); diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index 6a9ea64322fb..fdb5c2d96cac 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -149,7 +149,7 @@ export async function load_node({ method: opts.method || 'GET', headers, path: relative, - rawBody: /** @type {string} */ (opts.body), + rawBody: new TextEncoder().encode(/** @type {string} */ (opts.body)), query: new URLSearchParams(search) }, options, diff --git a/packages/kit/src/runtime/server/parse_body/index.js b/packages/kit/src/runtime/server/parse_body/index.js index 71851603d615..a7fd6041ae65 100644 --- a/packages/kit/src/runtime/server/parse_body/index.js +++ b/packages/kit/src/runtime/server/parse_body/index.js @@ -1,7 +1,7 @@ import { read_only_form_data } from './read_only_form_data.js'; /** - * @param {import('types/hooks').StrictBody} raw + * @param {Uint8Array} raw * @param {Partial} headers */ export function parse_body(raw, headers) { @@ -10,11 +10,7 @@ export function parse_body(raw, headers) { const content_type = headers['content-type']; const [type, ...directives] = content_type ? content_type.split(/;\s*/) : []; - const text = () => - typeof raw === 'string' - ? raw - : new TextDecoder(headers['content-encoding'] || 'utf-8').decode(raw); - const bytes = () => (typeof raw === 'string' ? new TextEncoder().encode(raw) : raw); + const text = () => new TextDecoder(headers['content-encoding'] || 'utf-8').decode(raw); switch (type) { case 'text/plain': @@ -32,7 +28,7 @@ export function parse_body(raw, headers) { return get_multipart(text(), boundary.slice('boundary='.length)); } default: - return bytes(); + return raw; } } diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 252c837728aa..7c29b78da0bf 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -6,8 +6,7 @@ import { HandleError, ServerFetch, ServerRequest, - ServerResponse, - StrictBody + ServerResponse } from './hooks'; import { Load } from './page'; @@ -16,7 +15,7 @@ type PageId = string; export interface Incoming extends Omit { method: string; headers: Headers; - rawBody: StrictBody; + rawBody: Uint8Array; body?: ParameterizedBody; } From a4c479851c7cf5c2664ce16e22705545d2564517 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Mon, 16 Aug 2021 22:44:55 -0300 Subject: [PATCH 13/15] remove unnecessary rawBody check --- packages/kit/src/runtime/server/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 03c42c8003dc..6d9369d72150 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -39,10 +39,6 @@ export async function respond(incoming, options, state = {}) { ...incoming, headers, body: parse_body(incoming.rawBody, headers), - rawBody: - typeof incoming.rawBody === 'string' - ? new TextEncoder().encode(incoming.rawBody) - : incoming.rawBody, params: {}, locals: {} }; From b572a8fc3fce7efa03bbf0c2ed53986b83a7b0e0 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Tue, 17 Aug 2021 10:18:13 -0300 Subject: [PATCH 14/15] make `null` assignable to `RawBody` --- packages/kit/src/core/adapt/prerender.js | 4 ++-- packages/kit/src/core/node/index.js | 6 +++--- packages/kit/src/runtime/server/parse_body/index.js | 2 +- .../kit/test/apps/basics/src/routes/load/raw-body.json.js | 2 +- packages/kit/types/helper.d.ts | 4 +++- packages/kit/types/hooks.d.ts | 4 +++- packages/kit/types/internal.d.ts | 3 ++- 7 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index 62cbe44e2164..b4565776f419 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -156,7 +156,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a method: 'GET', headers: {}, path, - rawBody: new Uint8Array(), + rawBody: null, query: new URLSearchParams() }, { @@ -289,7 +289,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a method: 'GET', headers: {}, path: '[fallback]', // this doesn't matter, but it's easiest if it's a string - rawBody: new Uint8Array(), + rawBody: null, query: new URLSearchParams() }, { diff --git a/packages/kit/src/core/node/index.js b/packages/kit/src/core/node/index.js index a90969ad7d2b..68e930d2c949 100644 --- a/packages/kit/src/core/node/index.js +++ b/packages/kit/src/core/node/index.js @@ -1,13 +1,13 @@ /** * @param {import('http').IncomingMessage} req - * @returns {Promise} + * @returns {Promise} */ export function getRawBody(req) { return new Promise((fulfil, reject) => { const h = req.headers; if (!h['content-type']) { - return fulfil(new Uint8Array()); + return fulfil(null); } req.on('error', reject); @@ -16,7 +16,7 @@ export function getRawBody(req) { // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 if (isNaN(length) && h['transfer-encoding'] == null) { - return fulfil(new Uint8Array()); + return fulfil(null); } let data = new Uint8Array(length || 0); diff --git a/packages/kit/src/runtime/server/parse_body/index.js b/packages/kit/src/runtime/server/parse_body/index.js index a7fd6041ae65..9bb17461538e 100644 --- a/packages/kit/src/runtime/server/parse_body/index.js +++ b/packages/kit/src/runtime/server/parse_body/index.js @@ -1,7 +1,7 @@ import { read_only_form_data } from './read_only_form_data.js'; /** - * @param {Uint8Array} raw + * @param {import('types/hooks.js').RawBody} raw * @param {Partial} headers */ export function parse_body(raw, headers) { diff --git a/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js b/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js index 39cfb1ab9e1e..abd0fb74908e 100644 --- a/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js +++ b/packages/kit/test/apps/basics/src/routes/load/raw-body.json.js @@ -3,7 +3,7 @@ export function post(request) { return { body: { body: /** @type {string} */ (request.body), - rawBody: new TextDecoder().decode(request.rawBody) + rawBody: new TextDecoder().decode(/** @type {Uint8Array} */ (request.rawBody)) } }; } diff --git a/packages/kit/types/helper.d.ts b/packages/kit/types/helper.d.ts index fe6d8264def4..619c802c9e94 100644 --- a/packages/kit/types/helper.d.ts +++ b/packages/kit/types/helper.d.ts @@ -1,3 +1,5 @@ +import { RawBody } from './hooks'; + interface ReadOnlyFormData { get(key: string): string; getAll(key: string): string[]; @@ -8,7 +10,7 @@ interface ReadOnlyFormData { [Symbol.iterator](): Generator<[string, string], void>; } -type BaseBody = string | Uint8Array | ReadOnlyFormData; +type BaseBody = string | RawBody | ReadOnlyFormData; export type ParameterizedBody = Body extends FormData ? ReadOnlyFormData : BaseBody & Body; diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index 30893af77330..3d48713e955b 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -2,10 +2,12 @@ import { Headers, Location, MaybePromise, ParameterizedBody } from './helper'; export type StrictBody = string | Uint8Array; +export type RawBody = null | Uint8Array; + export interface ServerRequest, Body = unknown> extends Location { method: string; headers: Headers; - rawBody: Uint8Array; + rawBody: RawBody; body: ParameterizedBody; locals: Locals; } diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 7c29b78da0bf..12c7f4232880 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -4,6 +4,7 @@ import { GetSession, Handle, HandleError, + RawBody, ServerFetch, ServerRequest, ServerResponse @@ -15,7 +16,7 @@ type PageId = string; export interface Incoming extends Omit { method: string; headers: Headers; - rawBody: Uint8Array; + rawBody: RawBody; body?: ParameterizedBody; } From bedda814614b373fef7a23f9fb54fd62c0c78f10 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Tue, 17 Aug 2021 10:26:03 -0300 Subject: [PATCH 15/15] use `Headers` type in `parse_body` --- packages/kit/src/runtime/server/parse_body/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/parse_body/index.js b/packages/kit/src/runtime/server/parse_body/index.js index 9bb17461538e..a6eb5ea49a2f 100644 --- a/packages/kit/src/runtime/server/parse_body/index.js +++ b/packages/kit/src/runtime/server/parse_body/index.js @@ -2,7 +2,7 @@ import { read_only_form_data } from './read_only_form_data.js'; /** * @param {import('types/hooks.js').RawBody} raw - * @param {Partial} headers + * @param {import('types/helper').Headers} headers */ export function parse_body(raw, headers) { if (!raw) return raw;