@vercel/node 2.15.3 → 2.15.4

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.
@@ -7,6 +7,7 @@ import fetch from 'node-fetch';
7
7
  import { listen } from 'async-listen';
8
8
  import { isAbsolute } from 'path';
9
9
  import { pathToFileURL } from 'url';
10
+ import zlib from 'zlib';
10
11
  const [NODE_MAJOR] = process.versions.node.split('.').map(v => Number(v));
11
12
  /* https://nextjs.org/docs/app/building-your-application/routing/router-handlers#supported-http-methods */
12
13
  const HTTP_METHODS = [
@@ -18,6 +19,26 @@ const HTTP_METHODS = [
18
19
  'DELETE',
19
20
  'PATCH',
20
21
  ];
22
+ function compress(body, encoding) {
23
+ switch (encoding) {
24
+ case 'br':
25
+ return zlib.brotliCompressSync(body, {
26
+ params: {
27
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 0,
28
+ },
29
+ });
30
+ case 'gzip':
31
+ return zlib.gzipSync(body, {
32
+ level: zlib.constants.Z_BEST_SPEED,
33
+ });
34
+ case 'deflate':
35
+ return zlib.deflateSync(body, {
36
+ level: zlib.constants.Z_BEST_SPEED,
37
+ });
38
+ default:
39
+ throw new Error(`encoding '${encoding}' not supported`);
40
+ }
41
+ }
21
42
  async function createServerlessServer(userCode) {
22
43
  const server = createServer(userCode);
23
44
  exitHook(() => server.close());
@@ -51,11 +72,13 @@ async function compileUserCode(entrypointPath, options) {
51
72
  export async function createServerlessEventHandler(entrypointPath, options) {
52
73
  const userCode = await compileUserCode(entrypointPath, options);
53
74
  const server = await createServerlessServer(userCode);
75
+ const isStreaming = options.mode === 'streaming';
54
76
  return async function (request) {
55
77
  const url = new URL(request.url ?? '/', server.url);
56
78
  // @ts-expect-error
57
79
  const response = await fetch(url, {
58
80
  body: await serializeBody(request),
81
+ compress: isStreaming,
59
82
  headers: {
60
83
  ...request.headers,
61
84
  host: request.headers['x-forwarded-host'],
@@ -64,13 +87,21 @@ export async function createServerlessEventHandler(entrypointPath, options) {
64
87
  redirect: 'manual',
65
88
  });
66
89
  let body;
67
- if (options.mode === 'streaming') {
90
+ if (isStreaming) {
68
91
  body = response.body;
69
92
  }
70
93
  else {
71
94
  body = await streamToBuffer(response.body);
95
+ const contentEncoding = response.headers.get('content-encoding');
96
+ if (contentEncoding) {
97
+ body = compress(body, contentEncoding);
98
+ response.headers.set('content-length', Buffer.byteLength(body));
99
+ }
100
+ /**
101
+ * `transfer-encoding` is related to streaming chunks.
102
+ * Since we are buffering the response.body, it should be stripped.
103
+ */
72
104
  response.headers.delete('transfer-encoding');
73
- response.headers.set('content-length', body.length);
74
105
  }
75
106
  return {
76
107
  status: response.status,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/node",
3
- "version": "2.15.3",
3
+ "version": "2.15.4",
4
4
  "license": "Apache-2.0",
5
5
  "main": "./dist/index",
6
6
  "homepage": "https://vercel.com/docs/runtimes#official-runtimes/node-js",
@@ -18,7 +18,7 @@
18
18
  "@edge-runtime/vm": "3.0.1",
19
19
  "@types/node": "14.18.33",
20
20
  "@types/node-fetch": "2.6.3",
21
- "@vercel/build-utils": "6.8.0",
21
+ "@vercel/build-utils": "6.8.1",
22
22
  "@vercel/error-utils": "1.0.10",
23
23
  "@vercel/static-config": "2.0.17",
24
24
  "async-listen": "3.0.0",