@netlify/functions
Advanced tools
Comparing version
@@ -13,2 +13,5 @@ // @ts-check | ||
// https://github.com/nodejs/undici/blob/a36e299d544863c5ade17d4090181be894366024/lib/web/fetch/constants.js#L6 | ||
const nullBodyStatus = new Set([101, 204, 205, 304]) | ||
/** | ||
@@ -50,8 +53,16 @@ * @typedef HandlerResponse | ||
/** @type {number | undefined} */ | ||
let streamPort | ||
/** | ||
* When the result body is a stream and result status code allow to have a body, | ||
* open up a http server that proxies back to the main thread and resolve with server port. | ||
* Otherwise, resolve with undefined. | ||
* | ||
* @param {HandlerResponse} invocationResult | ||
* @returns {Promise<number | undefined>} | ||
*/ | ||
async function getStreamPortForStreamingResponse(invocationResult) { | ||
// if we don't have result or result's body is not a stream, we do not need a stream port | ||
if (!invocationResult || !isStream(invocationResult.body)) { | ||
return undefined | ||
} | ||
// When the result body is a StreamResponse | ||
// we open up a http server that proxies back to the main thread. | ||
if (invocationResult && isStream(invocationResult.body)) { | ||
const { body } = invocationResult | ||
@@ -61,3 +72,11 @@ | ||
await new Promise((resolve, reject) => { | ||
// For streaming responses, lambda-local always returns a result with body stream. | ||
// We need to discard it if result's status code does not allow response to have a body. | ||
const shouldNotHaveABody = nullBodyStatus.has(invocationResult.statusCode) | ||
if (shouldNotHaveABody) { | ||
return undefined | ||
} | ||
// create a server that will proxy the body stream back to the main thread | ||
return await new Promise((resolve, reject) => { | ||
const server = createServer((socket) => { | ||
@@ -72,2 +91,4 @@ body.pipe(socket).on('end', () => server.close()) | ||
/** @type {number | undefined} */ | ||
let streamPort | ||
if (address && typeof address !== 'string') { | ||
@@ -77,3 +98,3 @@ streamPort = address.port | ||
resolve(undefined) | ||
resolve(streamPort) | ||
}) | ||
@@ -83,2 +104,4 @@ }) | ||
const streamPort = await getStreamPortForStreamingResponse(invocationResult) | ||
if (parentPort) { | ||
@@ -85,0 +108,0 @@ /** @type {WorkerResult} */ |
import { PipelineSource } from 'node:stream'; | ||
export { Context } from '@netlify/serverless-functions-api'; | ||
export { Context } from '@netlify/types'; | ||
@@ -4,0 +4,0 @@ interface HandlerContext { |
@@ -34,3 +34,3 @@ { | ||
}, | ||
"version": "4.1.13", | ||
"version": "4.1.14", | ||
"description": "TypeScript utilities for interacting with Netlify Functions", | ||
@@ -86,3 +86,3 @@ "files": [ | ||
"@netlify/dev-utils": "4.0.0", | ||
"@netlify/serverless-functions-api": "2.1.3", | ||
"@netlify/types": "2.0.2", | ||
"@netlify/zip-it-and-ship-it": "^14.1.0", | ||
@@ -89,0 +89,0 @@ "cron-parser": "^4.9.0", |
Sorry, the diff of this file is not supported yet
77715
1.28%1935
1.04%+ Added
+ Added