@vercel/node 3.0.7 → 3.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -528,7 +528,7 @@ function createNodeCompatPlugin() {
528
528
 
529
529
  // src/edge-functions/edge-handler.mts
530
530
  import { EdgeRuntime, runServer } from "edge-runtime";
531
- import { fetch, Headers as Headers2 } from "undici";
531
+ import { Headers as Headers2, request as undiciRequest } from "undici";
532
532
  import { isError } from "@vercel/error-utils";
533
533
  import { readFileSync } from "fs";
534
534
 
@@ -589,6 +589,7 @@ async function serializeBody(request) {
589
589
  // src/edge-functions/edge-handler.mts
590
590
  import esbuild from "esbuild";
591
591
  import exitHook from "exit-hook";
592
+ import { buildToHeaders } from "@edge-runtime/node-utils";
592
593
  import { fileURLToPath } from "url";
593
594
  var NODE_VERSION_MAJOR = process.version.match(/^v(\d+)\.\d+/)?.[1];
594
595
  var NODE_VERSION_IDENTIFIER = `node${NODE_VERSION_MAJOR}`;
@@ -597,6 +598,7 @@ if (!NODE_VERSION_MAJOR) {
597
598
  `Unable to determine current node version: process.version=${process.version}`
598
599
  );
599
600
  }
601
+ var toHeaders = buildToHeaders({ Headers: Headers2 });
600
602
  var __dirname = fileURLToPath(new URL(".", import.meta.url));
601
603
  var edgeHandlerTemplate = readFileSync(
602
604
  `${__dirname}/edge-handler-template.js`
@@ -724,20 +726,19 @@ async function createEdgeEventHandler(entrypointFullPath, entrypointRelativePath
724
726
  if (!server) {
725
727
  process.exit(1);
726
728
  }
727
- const headers = new Headers2(request.headers);
728
729
  const body = await serializeBody(request);
729
730
  if (body !== void 0)
730
- headers.set("content-length", String(body.length));
731
+ request.headers["content-length"] = String(body.length);
731
732
  const url = new URL(request.url ?? "/", server.url);
732
- const response = await fetch(url, {
733
+ const response = await undiciRequest(url, {
733
734
  body,
734
- headers,
735
- method: request.method,
736
- redirect: "manual"
735
+ headers: request.headers,
736
+ method: request.method || "GET"
737
737
  });
738
- const isUserError = response.headers.get("x-vercel-failed") === "edge-wrapper";
739
- if (isUserError && response.status >= 500) {
740
- const body2 = await response.text();
738
+ const resHeaders = toHeaders(response.headers);
739
+ const isUserError = resHeaders.get("x-vercel-failed") === "edge-wrapper";
740
+ if (isUserError && response.statusCode >= 500) {
741
+ const body2 = await response.body.text();
741
742
  const fakeStackTrace = ` at (${entrypointRelativePath})`;
742
743
  const requestPath = entrypointToRequestPath(
743
744
  entrypointRelativePath,
@@ -750,8 +751,8 @@ ${fakeStackTrace}`
750
751
  process.exit(1);
751
752
  }
752
753
  return {
753
- status: response.status,
754
- headers: response.headers,
754
+ status: response.statusCode,
755
+ headers: resHeaders,
755
756
  body: response.body,
756
757
  encoding: "utf8"
757
758
  };
@@ -972,11 +973,12 @@ async function addHelpers(_req, _res) {
972
973
  // src/serverless-functions/serverless-handler.mts
973
974
  import { createServer } from "http";
974
975
  import exitHook2 from "exit-hook";
975
- import { Headers as Headers3, fetch as fetch2 } from "undici";
976
+ import { Headers as Headers3, request as undiciRequest2 } from "undici";
976
977
  import { listen } from "async-listen";
977
978
  import { isAbsolute } from "path";
978
979
  import { pathToFileURL } from "url";
979
- import zlib from "zlib";
980
+ import { buildToHeaders as buildToHeaders2 } from "@edge-runtime/node-utils";
981
+ var toHeaders2 = buildToHeaders2({ Headers: Headers3 });
980
982
  var [NODE_MAJOR] = process.versions.node.split(".").map((v) => Number(v));
981
983
  var HTTP_METHODS = [
982
984
  "GET",
@@ -987,26 +989,6 @@ var HTTP_METHODS = [
987
989
  "DELETE",
988
990
  "PATCH"
989
991
  ];
990
- function compress(body, encoding) {
991
- switch (encoding) {
992
- case "br":
993
- return zlib.brotliCompressSync(body, {
994
- params: {
995
- [zlib.constants.BROTLI_PARAM_QUALITY]: 0
996
- }
997
- });
998
- case "gzip":
999
- return zlib.gzipSync(body, {
1000
- level: zlib.constants.Z_BEST_SPEED
1001
- });
1002
- case "deflate":
1003
- return zlib.deflateSync(body, {
1004
- level: zlib.constants.Z_BEST_SPEED
1005
- });
1006
- default:
1007
- throw new Error(`encoding '${encoding}' not supported`);
1008
- }
1009
- }
1010
992
  async function createServerlessServer(userCode) {
1011
993
  const server = createServer(userCode);
1012
994
  exitHook2(() => server.close());
@@ -1040,38 +1022,23 @@ async function createServerlessEventHandler(entrypointPath, options) {
1040
1022
  const isStreaming = options.mode === "streaming";
1041
1023
  return async function(request) {
1042
1024
  const url = new URL(request.url ?? "/", server.url);
1043
- const response = await fetch2(url, {
1025
+ const response = await undiciRequest2(url, {
1044
1026
  body: await serializeBody(request),
1045
- compress: !isStreaming,
1046
- // @ts-expect-error
1047
1027
  headers: {
1048
1028
  ...request.headers,
1049
1029
  host: request.headers["x-forwarded-host"]
1050
1030
  },
1051
- method: request.method,
1052
- redirect: "manual"
1031
+ method: request.method || "GET"
1053
1032
  });
1054
1033
  let body = null;
1055
- let headers = response.headers;
1034
+ let headers = toHeaders2(response.headers);
1056
1035
  if (isStreaming) {
1057
1036
  body = response.body;
1058
1037
  } else {
1059
- body = Buffer.from(await response.arrayBuffer());
1060
- const contentEncoding = response.headers.get("content-encoding");
1061
- if (contentEncoding) {
1062
- body = compress(body, contentEncoding);
1063
- const clonedHeaders = [];
1064
- for (const [key, value] of response.headers.entries()) {
1065
- if (key !== "transfer-encoding") {
1066
- clonedHeaders.push([key, value]);
1067
- }
1068
- }
1069
- clonedHeaders.push(["content-length", String(Buffer.byteLength(body))]);
1070
- headers = new Headers3(clonedHeaders);
1071
- }
1038
+ body = Buffer.from(await response.body.arrayBuffer());
1072
1039
  }
1073
1040
  return {
1074
- status: response.status,
1041
+ status: response.statusCode,
1075
1042
  headers,
1076
1043
  body,
1077
1044
  encoding: "utf8"
@@ -1080,7 +1047,6 @@ async function createServerlessEventHandler(entrypointPath, options) {
1080
1047
  }
1081
1048
 
1082
1049
  // src/dev-server.mts
1083
- import { toToReadable } from "@edge-runtime/node-utils";
1084
1050
  import { getConfig } from "@vercel/static-config";
1085
1051
  import { Project } from "ts-morph";
1086
1052
  import { listen as listen2 } from "async-listen";
@@ -1157,7 +1123,7 @@ async function onDevRequest(req, res) {
1157
1123
  } else if (body instanceof Buffer) {
1158
1124
  res.end(body);
1159
1125
  } else {
1160
- toToReadable(body).pipe(res);
1126
+ body.pipe(res);
1161
1127
  }
1162
1128
  } catch (error) {
1163
1129
  res.statusCode = 500;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ServerResponse, IncomingMessage } from 'http';
2
2
  import type { Headers } from 'undici';
3
+ import type { Readable } from 'stream';
3
4
 
4
5
  export type VercelRequestCookies = { [key: string]: string };
5
6
  export type VercelRequestQuery = { [key: string]: string | string[] };
@@ -44,6 +45,6 @@ export type NowApiHandler = VercelApiHandler;
44
45
  export interface VercelProxyResponse {
45
46
  status: number;
46
47
  headers: Headers;
47
- body: ReadableStream<Uint8Array> | Buffer | null;
48
+ body: Readable | Buffer | null;
48
49
  encoding: BufferEncoding;
49
50
  }