Unverified Commit 07c434d5 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Web Streams cleanup (#56819)

This updates some code related to web streams and encoding.

- Removes some unused code related to base64 encoding/decoding (Edge runtime currently supports it natively via `Buffer`)
- Prefer readable stream `pull` versus `.on("data", (chunk) => { ... })` event handlers (simplifies execution)
- Utilize `pipeTo` and `pipeThrough` on web streams to remove custom code related to stream pumping
- Updates pipe readable function to utilize web streams first class rather than relying on manual pumping + stream management
  - This also takes advantage of the `AbortController` when piping so that the response can use it to cancel the stream
parent 40dd14ff
...@@ -5,17 +5,14 @@ import { UNHANDLED } from './types' ...@@ -5,17 +5,14 @@ import { UNHANDLED } from './types'
import type { FetchHandler } from './fetch-api' import type { FetchHandler } from './fetch-api'
import { handleFetch } from './fetch-api' import { handleFetch } from './fetch-api'
function readBody(req: IncomingMessage): Promise<Buffer> { async function readBody(req: IncomingMessage): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => { const acc: Buffer[] = []
const acc: Buffer[] = []
req.on('data', (chunk) => { for await (const chunk of req) {
acc.push(chunk) acc.push(chunk)
}) }
req.on('end', () => {
resolve(Buffer.concat(acc)) return Buffer.concat(acc)
})
req.on('error', reject)
})
} }
export async function createProxyServer({ export async function createProxyServer({
......
...@@ -49,10 +49,7 @@ export async function generatePrefetchRsc( ...@@ -49,10 +49,7 @@ export async function generatePrefetchRsc(
renderOpts renderOpts
) )
prefetchRenderResult.pipe(res) const prefetchRscData = await prefetchRenderResult.toUnchunkedString(true)
await res.hasStreamed
const prefetchRscData = Buffer.concat(res.buffers)
if ((renderOpts as any).store.staticPrefetchBailout) return if ((renderOpts as any).store.staticPrefetchBailout) return
......
...@@ -23,7 +23,11 @@ export interface AmpValidation { ...@@ -23,7 +23,11 @@ export interface AmpValidation {
export type FileWriter = ( export type FileWriter = (
type: string, type: string,
path: string, path: string,
content: any, content:
| string
| NodeJS.ArrayBufferView
| Iterable<string | NodeJS.ArrayBufferView>
| AsyncIterable<string | NodeJS.ArrayBufferView>,
encodingOptions?: WriteFileOptions encodingOptions?: WriteFileOptions
) => Promise<void> ) => Promise<void>
......
...@@ -45,19 +45,20 @@ function nodeToWebReadableStream(nodeReadable: import('stream').Readable) { ...@@ -45,19 +45,20 @@ function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
return Readable.toWeb(nodeReadable) return Readable.toWeb(nodeReadable)
} }
const iterator = nodeReadable[Symbol.asyncIterator]()
return new ReadableStream({ return new ReadableStream({
start(controller) { pull: async (controller) => {
nodeReadable.on('data', (chunk) => { const { value, done } = await iterator.next()
controller.enqueue(chunk)
})
nodeReadable.on('end', () => { if (done) {
controller.close() controller.close()
}) } else {
controller.enqueue(value)
nodeReadable.on('error', (error) => { }
controller.error(error) },
}) cancel: () => {
iterator.return?.()
}, },
}) })
} else { } else {
......
...@@ -20,7 +20,6 @@ import { ...@@ -20,7 +20,6 @@ import {
renderToInitialFizzStream, renderToInitialFizzStream,
createBufferedTransformStream, createBufferedTransformStream,
continueFizzStream, continueFizzStream,
streamToBufferedResult,
cloneTransformStream, cloneTransformStream,
} from '../stream-utils/node-web-streams-helper' } from '../stream-utils/node-web-streams-helper'
import { canSegmentBeOverridden } from '../../client/components/match-segments' import { canSegmentBeOverridden } from '../../client/components/match-segments'
...@@ -908,7 +907,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -908,7 +907,7 @@ async function renderToHTMLOrFlightImpl(
}) })
if (staticGenerationStore.isStaticGeneration) { if (staticGenerationStore.isStaticGeneration) {
const htmlResult = await streamToBufferedResult(renderResult) const htmlResult = await renderResult.toUnchunkedString(true)
// if we encountered any unexpected errors during build // if we encountered any unexpected errors during build
// we fail the prerendering phase and the build // we fail the prerendering phase and the build
...@@ -918,9 +917,9 @@ async function renderToHTMLOrFlightImpl( ...@@ -918,9 +917,9 @@ async function renderToHTMLOrFlightImpl(
// TODO-APP: derive this from same pass to prevent additional // TODO-APP: derive this from same pass to prevent additional
// render during static generation // render during static generation
const stringifiedFlightPayload = await streamToBufferedResult( const stringifiedFlightPayload = await (
await generateFlight(ctx) await generateFlight(ctx)
) ).toUnchunkedString(true)
if (staticGenerationStore.forceStatic === false) { if (staticGenerationStore.forceStatic === false) {
staticGenerationStore.revalidate = 0 staticGenerationStore.revalidate = 0
......
import type { RenderOpts } from './types' import type { RenderOpts } from './types'
import type { FlightResponseRef } from './flight-response-ref' import type { FlightResponseRef } from './flight-response-ref'
import type { AppPageModule } from '../future/route-modules/app-page/module' import type { AppPageModule } from '../future/route-modules/app-page/module'
import type { createErrorHandler } from './create-error-handler'
import React, { use } from 'react' import React, { use } from 'react'
import type { createErrorHandler } from './create-error-handler'
import { useFlightResponse } from './use-flight-response' import { useFlightResponse } from './use-flight-response'
/** /**
......
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin' import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'
import type { FlightResponseRef } from './flight-response-ref' import type { FlightResponseRef } from './flight-response-ref'
import { encodeText, decodeText } from '../stream-utils/encode-decode'
import { htmlEscapeJsonString } from '../htmlescape' import { htmlEscapeJsonString } from '../htmlescape'
import {
createDecodeTransformStream,
createEncodeTransformStream,
} from '../stream-utils/encode-decode'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
...@@ -9,6 +13,35 @@ const INLINE_FLIGHT_PAYLOAD_BOOTSTRAP = 0 ...@@ -9,6 +13,35 @@ const INLINE_FLIGHT_PAYLOAD_BOOTSTRAP = 0
const INLINE_FLIGHT_PAYLOAD_DATA = 1 const INLINE_FLIGHT_PAYLOAD_DATA = 1
const INLINE_FLIGHT_PAYLOAD_FORM_STATE = 2 const INLINE_FLIGHT_PAYLOAD_FORM_STATE = 2
function createFlightTransformer(
nonce: string | undefined,
formState: unknown | null
) {
const startScriptTag = nonce
? `<script nonce=${JSON.stringify(nonce)}>`
: '<script>'
return new TransformStream<string, string>({
// Bootstrap the flight information.
start(controller) {
controller.enqueue(
`${startScriptTag}(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)});self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})</script>`
)
},
transform(chunk, controller) {
const scripts = `${startScriptTag}self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_DATA, chunk])
)})</script>`
controller.enqueue(scripts)
},
})
}
/** /**
* Render Flight stream. * Render Flight stream.
* This is only used for renderToHTML, the Flight response does not need additional wrappers. * This is only used for renderToHTML, the Flight response does not need additional wrappers.
...@@ -49,50 +82,19 @@ export function useFlightResponse( ...@@ -49,50 +82,19 @@ export function useFlightResponse(
}) })
flightResponseRef.current = res flightResponseRef.current = res
let bootstrapped = false forwardStream
// We only attach CSS chunks to the inlined data. .pipeThrough(createDecodeTransformStream())
const forwardReader = forwardStream.getReader() .pipeThrough(createFlightTransformer(nonce, formState))
const writer = writable.getWriter() .pipeThrough(createEncodeTransformStream())
const startScriptTag = nonce .pipeTo(writable)
? `<script nonce=${JSON.stringify(nonce)}>` .finally(() => {
: '<script>' // Once the last encoding stream has flushed, then unset the flight
const textDecoder = new TextDecoder() // response ref.
flightResponseRef.current = null
function read() { })
forwardReader.read().then(({ done, value }) => { .catch((err) => {
if (!bootstrapped) { console.error('Unexpected error while rendering Flight stream', err)
bootstrapped = true
writer.write(
encodeText(
`${startScriptTag}(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)});self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})</script>`
)
)
}
if (done) {
// Add a setTimeout here because the error component is too small, the first forwardReader.read() read will return the full chunk
// and then it immediately set flightResponseRef.current as null.
// react renders the component twice, the second render will run into the state with useFlightResponse where flightResponseRef.current is null,
// so it tries to render the flight payload again
setTimeout(() => {
flightResponseRef.current = null
})
writer.close()
} else {
const responsePartial = decodeText(value, textDecoder)
const scripts = `${startScriptTag}self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_DATA, responsePartial])
)})</script>`
writer.write(encodeText(scripts))
read()
}
}) })
}
read()
return res return res
} }
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http' import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'
import type { FetchMetrics } from './index' import type { FetchMetrics } from './index'
import { toNodeOutgoingHttpHeaders } from '../web/utils' import { toNodeOutgoingHttpHeaders } from '../web/utils'
import { BaseNextRequest, BaseNextResponse } from './index' import { BaseNextRequest, BaseNextResponse } from './index'
import { DetachedPromise } from '../../lib/detached-promise'
export class WebNextRequest extends BaseNextRequest<ReadableStream | null> { export class WebNextRequest extends BaseNextRequest<ReadableStream | null> {
public request: Request public request: Request
...@@ -32,27 +34,10 @@ export class WebNextRequest extends BaseNextRequest<ReadableStream | null> { ...@@ -32,27 +34,10 @@ export class WebNextRequest extends BaseNextRequest<ReadableStream | null> {
export class WebNextResponse extends BaseNextResponse<WritableStream> { export class WebNextResponse extends BaseNextResponse<WritableStream> {
private headers = new Headers() private headers = new Headers()
private textBody: string | undefined = undefined private textBody: string | undefined = undefined
private _sent = false
private sendPromise = new Promise<void>((resolve) => {
this.sendResolve = resolve
})
private sendResolve?: () => void
private response = this.sendPromise.then(() => {
return new Response(this.textBody ?? this.transformStream.readable, {
headers: this.headers,
status: this.statusCode,
statusText: this.statusMessage,
})
})
public statusCode: number | undefined public statusCode: number | undefined
public statusMessage: string | undefined public statusMessage: string | undefined
get sent() {
return this._sent
}
constructor(public transformStream = new TransformStream()) { constructor(public transformStream = new TransformStream()) {
super(transformStream.writable) super(transformStream.writable)
} }
...@@ -99,12 +84,25 @@ export class WebNextResponse extends BaseNextResponse<WritableStream> { ...@@ -99,12 +84,25 @@ export class WebNextResponse extends BaseNextResponse<WritableStream> {
return this return this
} }
send() { private readonly sendPromise = new DetachedPromise<void>()
this.sendResolve?.() private _sent = false
public send() {
this.sendPromise.resolve()
this._sent = true this._sent = true
} }
toResponse() { get sent() {
return this.response return this._sent
}
public async toResponse() {
// If we haven't called `send` yet, wait for it to be called.
if (!this.sent) await this.sendPromise.promise
return new Response(this.textBody ?? this.transformStream.readable, {
headers: this.headers,
status: this.statusCode,
statusText: this.statusMessage,
})
} }
} }
...@@ -8,12 +8,11 @@ export function requestToBodyStream( ...@@ -8,12 +8,11 @@ export function requestToBodyStream(
stream: Readable stream: Readable
) { ) {
return new context.ReadableStream({ return new context.ReadableStream({
start(controller) { start: async (controller) => {
stream.on('data', (chunk) => for await (const chunk of stream) {
controller.enqueue(new KUint8Array([...new Uint8Array(chunk)])) controller.enqueue(new KUint8Array([...new Uint8Array(chunk)]))
) }
stream.on('end', () => controller.close()) controller.close()
stream.on('error', (err) => controller.error(err))
}, },
}) })
} }
......
...@@ -5,7 +5,6 @@ import { ...@@ -5,7 +5,6 @@ import {
DEFAULT_SANS_SERIF_FONT, DEFAULT_SANS_SERIF_FONT,
} from '../shared/lib/constants' } from '../shared/lib/constants'
const capsizeFontsMetrics = require('next/dist/server/capsize-font-metrics.json') const capsizeFontsMetrics = require('next/dist/server/capsize-font-metrics.json')
const https = require('https')
const CHROME_UA = const CHROME_UA =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36' 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
...@@ -22,30 +21,9 @@ function isGoogleFont(url: string): boolean { ...@@ -22,30 +21,9 @@ function isGoogleFont(url: string): boolean {
return url.startsWith(GOOGLE_FONT_PROVIDER) return url.startsWith(GOOGLE_FONT_PROVIDER)
} }
function getFontForUA(url: string, UA: string): Promise<String> { async function getFontForUA(url: string, UA: string): Promise<string> {
return new Promise((resolve, reject) => { const res = await fetch(url, { headers: { 'user-agent': UA } })
let rawData: any = '' return await res.text()
https
.get(
url,
{
headers: {
'user-agent': UA,
},
},
(res: any) => {
res.on('data', (chunk: any) => {
rawData += chunk
})
res.on('end', () => {
resolve(rawData.toString('utf8'))
})
}
)
.on('error', (e: Error) => {
reject(e)
})
})
} }
export async function getFontDefinitionFromNetwork( export async function getFontDefinitionFromNetwork(
......
...@@ -7,10 +7,10 @@ import type { ...@@ -7,10 +7,10 @@ import type {
import FetchCache from './fetch-cache' import FetchCache from './fetch-cache'
import FileSystemCache from './file-system-cache' import FileSystemCache from './file-system-cache'
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import { encodeText } from '../../stream-utils/encode-decode'
import { encode } from '../../../shared/lib/base64-arraybuffer'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import '../../node-polyfill-web-streams'
import { import {
CACHE_ONE_YEAR, CACHE_ONE_YEAR,
NEXT_CACHE_REVALIDATED_TAGS_HEADER, NEXT_CACHE_REVALIDATED_TAGS_HEADER,
...@@ -287,45 +287,49 @@ export class IncrementalCache { ...@@ -287,45 +287,49 @@ export class IncrementalCache {
let cacheKey: string let cacheKey: string
const bodyChunks: string[] = [] const bodyChunks: string[] = []
const encoder = new TextEncoder()
const decoder = new TextDecoder()
if (init.body) { if (init.body) {
// handle ReadableStream body // handle ReadableStream body
if (typeof (init.body as any).getReader === 'function') { if (typeof (init.body as any).getReader === 'function') {
const readableBody = init.body as ReadableStream const readableBody = init.body as ReadableStream<Uint8Array | string>
const reader = readableBody.getReader()
let arrayBuffer = new Uint8Array() const chunks: Uint8Array[] = []
function processValue({ try {
done, await readableBody.pipeTo(
value, new WritableStream({
}: { write(chunk) {
done?: boolean if (typeof chunk === 'string') {
value?: ArrayBuffer | string chunks.push(encoder.encode(chunk))
}): any { bodyChunks.push(chunk)
if (done) { } else {
return chunks.push(chunk)
} bodyChunks.push(decoder.decode(chunk, { stream: true }))
if (value) { }
try { },
bodyChunks.push(typeof value === 'string' ? value : encode(value)) })
const curBuffer: Uint8Array = )
typeof value === 'string'
? encodeText(value) // Flush the decoder.
: new Uint8Array(value) bodyChunks.push(decoder.decode())
const prevBuffer = arrayBuffer // Create a new buffer with all the chunks.
arrayBuffer = new Uint8Array( const length = chunks.reduce((total, arr) => total + arr.length, 0)
prevBuffer.byteLength + curBuffer.byteLength const arrayBuffer = new Uint8Array(length)
)
arrayBuffer.set(prevBuffer) // Push each of the chunks into the new array buffer.
arrayBuffer.set(curBuffer, prevBuffer.byteLength) let offset = 0
} catch (err) { for (const chunk of chunks) {
console.error(err) arrayBuffer.set(chunk, offset)
} offset += chunk.length
} }
reader.read().then(processValue)
;(init as any)._ogBody = arrayBuffer
} catch (err) {
console.error('Problem reading body', err)
} }
await reader.read().then(processValue)
;(init as any)._ogBody = arrayBuffer
} // handle FormData or URLSearchParams bodies } // handle FormData or URLSearchParams bodies
else if (typeof (init.body as any).keys === 'function') { else if (typeof (init.body as any).keys === 'function') {
const formData = init.body as FormData const formData = init.body as FormData
...@@ -350,7 +354,7 @@ export class IncrementalCache { ...@@ -350,7 +354,7 @@ export class IncrementalCache {
} else if (typeof (init.body as any).arrayBuffer === 'function') { } else if (typeof (init.body as any).arrayBuffer === 'function') {
const blob = init.body as Blob const blob = init.body as Blob
const arrayBuffer = await blob.arrayBuffer() const arrayBuffer = await blob.arrayBuffer()
bodyChunks.push(encode(await (init.body as Blob).arrayBuffer())) bodyChunks.push(await blob.text())
;(init as any)._ogBody = new Blob([arrayBuffer], { type: blob.type }) ;(init as any)._ogBody = new Blob([arrayBuffer], { type: blob.type })
} else if (typeof init.body === 'string') { } else if (typeof init.body === 'string') {
bodyChunks.push(init.body) bodyChunks.push(init.body)
...@@ -382,7 +386,7 @@ export class IncrementalCache { ...@@ -382,7 +386,7 @@ export class IncrementalCache {
.call(new Uint8Array(buffer), (b) => b.toString(16).padStart(2, '0')) .call(new Uint8Array(buffer), (b) => b.toString(16).padStart(2, '0'))
.join('') .join('')
} }
const buffer = encodeText(cacheString) const buffer = encoder.encode(cacheString)
cacheKey = bufferToHex(await crypto.subtle.digest('SHA-256', buffer)) cacheKey = bufferToHex(await crypto.subtle.digest('SHA-256', buffer))
} else { } else {
const crypto = require('crypto') as typeof import('crypto') const crypto = require('crypto') as typeof import('crypto')
......
...@@ -367,6 +367,11 @@ export class MockedResponse extends Stream.Writable implements ServerResponse { ...@@ -367,6 +367,11 @@ export class MockedResponse extends Stream.Writable implements ServerResponse {
this.headers.delete(name) this.headers.delete(name)
} }
public flushHeaders(): void {
// This is a no-op because we don't actually have a socket to flush the
// headers to.
}
// The following methods are not implemented as they are not used in the // The following methods are not implemented as they are not used in the
// Next.js codebase. // Next.js codebase.
...@@ -425,10 +430,6 @@ export class MockedResponse extends Stream.Writable implements ServerResponse { ...@@ -425,10 +430,6 @@ export class MockedResponse extends Stream.Writable implements ServerResponse {
public addTrailers(): void { public addTrailers(): void {
throw new Error('Method not implemented.') throw new Error('Method not implemented.')
} }
public flushHeaders(): void {
throw new Error('Method not implemented.')
}
} }
interface RequestResponseMockerOptions { interface RequestResponseMockerOptions {
......
import type { StaticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage.external' import type {
StaticGenerationAsyncStorage,
StaticGenerationStore,
} from '../../client/components/static-generation-async-storage.external'
import type * as ServerHooks from '../../client/components/hooks-server-context' import type * as ServerHooks from '../../client/components/hooks-server-context'
import { AppRenderSpan, NextNodeServerSpan } from './trace/constants' import { AppRenderSpan, NextNodeServerSpan } from './trace/constants'
...@@ -195,7 +198,7 @@ export function patchFetch({ ...@@ -195,7 +198,7 @@ export function patchFetch({
}, },
}, },
async () => { async () => {
const staticGenerationStore = const staticGenerationStore: StaticGenerationStore =
staticGenerationAsyncStorage.getStore() || staticGenerationAsyncStorage.getStore() ||
(fetch as any).__nextGetStaticStore?.() (fetch as any).__nextGetStaticStore?.()
const isRequestInput = const isRequestInput =
...@@ -534,15 +537,6 @@ export function patchFetch({ ...@@ -534,15 +537,6 @@ export function patchFetch({
) )
} }
const resData = entry.value.data const resData = entry.value.data
let decodedBody: ArrayBuffer
if (process.env.NEXT_RUNTIME === 'edge') {
const { decode } =
require('../../shared/lib/base64-arraybuffer') as typeof import('../../shared/lib/base64-arraybuffer')
decodedBody = decode(resData.body)
} else {
decodedBody = Buffer.from(resData.body, 'base64').subarray()
}
trackFetchMetric(staticGenerationStore, { trackFetchMetric(staticGenerationStore, {
start: fetchStart, start: fetchStart,
...@@ -553,10 +547,13 @@ export function patchFetch({ ...@@ -553,10 +547,13 @@ export function patchFetch({
method: init?.method || 'GET', method: init?.method || 'GET',
}) })
const response = new Response(decodedBody, { const response = new Response(
headers: resData.headers, Buffer.from(resData.body, 'base64'),
status: resData.status, {
}) headers: resData.headers,
status: resData.status,
}
)
Object.defineProperty(response, 'url', { Object.defineProperty(response, 'url', {
value: entry.value.data.url, value: entry.value.data.url,
}) })
......
...@@ -19,7 +19,7 @@ import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants' ...@@ -19,7 +19,7 @@ import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants'
import { formatHostname } from './format-hostname' import { formatHostname } from './format-hostname'
import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request' import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request'
import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher' import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher'
import { pipeReadable } from '../pipe-readable' import { pipeToNodeResponse } from '../pipe-readable'
type RouteResult = type RouteResult =
| { | {
...@@ -227,7 +227,7 @@ export async function makeResolver( ...@@ -227,7 +227,7 @@ export async function makeResolver(
// handle middleware body response // handle middleware body response
if (bodyStream) { if (bodyStream) {
res.statusCode = statusCode || 200 res.statusCode = statusCode || 200
return await pipeReadable(bodyStream, res) return await pipeToNodeResponse(bodyStream, res)
} }
if (finished && parsedUrl.protocol) { if (finished && parsedUrl.protocol) {
......
...@@ -17,7 +17,7 @@ import { DecodeError } from '../../shared/lib/utils' ...@@ -17,7 +17,7 @@ import { DecodeError } from '../../shared/lib/utils'
import { findPagesDir } from '../../lib/find-pages-dir' import { findPagesDir } from '../../lib/find-pages-dir'
import { setupFsCheck } from './router-utils/filesystem' import { setupFsCheck } from './router-utils/filesystem'
import { proxyRequest } from './router-utils/proxy-request' import { proxyRequest } from './router-utils/proxy-request'
import { isAbortError, pipeReadable } from '../pipe-readable' import { isAbortError, pipeToNodeResponse } from '../pipe-readable'
import { getResolveRoutes } from './router-utils/resolve-routes' import { getResolveRoutes } from './router-utils/resolve-routes'
import { getRequestMeta } from '../request-meta' import { getRequestMeta } from '../request-meta'
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix' import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix'
...@@ -363,7 +363,7 @@ export async function initialize(opts: { ...@@ -363,7 +363,7 @@ export async function initialize(opts: {
// handle middleware body response // handle middleware body response
if (bodyStream) { if (bodyStream) {
res.statusCode = statusCode || 200 res.statusCode = statusCode || 200
return await pipeReadable(bodyStream, res) return await pipeToNodeResponse(bodyStream, res)
} }
if (finished && parsedUrl.protocol) { if (finished && parsedUrl.protocol) {
......
...@@ -91,8 +91,8 @@ import { NextNodeServerSpan } from './lib/trace/constants' ...@@ -91,8 +91,8 @@ import { NextNodeServerSpan } from './lib/trace/constants'
import { nodeFs } from './lib/node-fs-methods' import { nodeFs } from './lib/node-fs-methods'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex' import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { invokeRequest } from './lib/server-ipc/invoke-request' import { invokeRequest } from './lib/server-ipc/invoke-request'
import { pipeReadable } from './pipe-readable'
import { filterReqHeaders, ipcForbiddenHeaders } from './lib/server-ipc/utils' import { filterReqHeaders, ipcForbiddenHeaders } from './lib/server-ipc/utils'
import { pipeToNodeResponse } from './pipe-readable'
import { createRequestResponseMocks } from './lib/mock-request' import { createRequestResponseMocks } from './lib/mock-request'
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers' import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'
import { signalFromNodeResponse } from './web/spec-extension/adapters/next-request' import { signalFromNodeResponse } from './web/spec-extension/adapters/next-request'
...@@ -560,7 +560,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -560,7 +560,7 @@ export default class NextNodeServer extends BaseServer {
newRes.statusCode = invokeRes.status || 200 newRes.statusCode = invokeRes.status || 200
if (invokeRes.body) { if (invokeRes.body) {
await pipeReadable(invokeRes.body, newRes) await pipeToNodeResponse(invokeRes.body, newRes)
} else { } else {
res.send() res.send()
} }
...@@ -1657,7 +1657,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1657,7 +1657,7 @@ export default class NextNodeServer extends BaseServer {
const { originalResponse } = res as NodeNextResponse const { originalResponse } = res as NodeNextResponse
if (result.response.body) { if (result.response.body) {
await pipeReadable(result.response.body, originalResponse) await pipeToNodeResponse(result.response.body, originalResponse)
} else { } else {
originalResponse.end() originalResponse.end()
} }
...@@ -1886,7 +1886,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1886,7 +1886,7 @@ export default class NextNodeServer extends BaseServer {
const nodeResStream = (params.res as NodeNextResponse).originalResponse const nodeResStream = (params.res as NodeNextResponse).originalResponse
if (result.response.body) { if (result.response.body) {
await pipeReadable(result.response.body, nodeResStream) await pipeToNodeResponse(result.response.body, nodeResStream)
} else { } else {
nodeResStream.end() nodeResStream.end()
} }
......
// Polyfill Web Streams for the Node.js runtime. // NOTE: any changes to this file should be mirrored in: test/__mocks__/node-polyfill-web-streams.js
if (!global.ReadableStream) {
// In Node v16, ReadableStream is available natively but under the `stream` namespace. if (process.env.NEXT_RUNTIME !== 'edge') {
// In Node v18+, it's available under global. // Polyfill Web Streams for the Node.js runtime.
if (require('stream/web').ReadableStream) { if (!global.ReadableStream) {
global.ReadableStream = require('stream/web').ReadableStream // In Node v16, ReadableStream is available natively but under the `stream` namespace.
} else { // In Node v18+, it's available under global.
const { ReadableStream } = if (require('stream/web').ReadableStream) {
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') global.ReadableStream = require('stream/web').ReadableStream
global.ReadableStream = ReadableStream } else {
const { ReadableStream } =
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill')
global.ReadableStream = ReadableStream
}
} }
} if (!global.WritableStream) {
if (!global.TransformStream) { // In Node v16, WritableStream is available natively but under the `stream` namespace.
// Same as ReadableStream above. // In Node v18+, it's available under global.
if (require('stream/web').TransformStream) { if (require('stream/web').WritableStream) {
global.TransformStream = require('stream/web').TransformStream global.WritableStream = require('stream/web').WritableStream
} else { } else {
const { TransformStream } = const { WritableStream } =
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill')
global.TransformStream = TransformStream global.WritableStream = WritableStream
}
}
if (!global.TransformStream) {
// Same as ReadableStream above.
if (require('stream/web').TransformStream) {
global.TransformStream = require('stream/web').TransformStream
} else {
const { TransformStream } =
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill')
global.TransformStream = TransformStream
}
} }
} }
import type { ServerResponse } from 'node:http'
import './node-polyfill-web-streams'
import {
ResponseAbortedName,
createAbortController,
} from './web/spec-extension/adapters/next-request'
import { DetachedPromise } from '../lib/detached-promise'
export function isAbortError(e: any): e is Error & { name: 'AbortError' } { export function isAbortError(e: any): e is Error & { name: 'AbortError' } {
return e?.name === 'AbortError' return e?.name === 'AbortError' || e?.name === ResponseAbortedName
} }
/** function createWriterFromResponse(
* This is a minimal implementation of a Writable with just enough res: ServerResponse
* functionality to handle stream cancellation. ): WritableStream<Uint8Array> {
*/ let started = false
export interface PipeTarget<R = any> {
/**
* Called when new data is read from readable source.
*/
write: (chunk: R) => unknown
/**
* Always called once we read all data (if the writable isn't already
* destroyed by a client disconnect).
*/
end: () => unknown
/**
* An optional method which is called after every write, to support
* immediately streaming in gzip responses.
*/
flush?: () => unknown
/**
* The close event listener is necessary for us to detect an early client
* disconnect while we're attempting to read data. This must be done
* out-of-band so that we can cancel the readable (else we'd have to wait for
* the readable to produce more data before we could tell it to cancel).
*/
on: (event: 'close', cb: () => void) => void
/**
* Allows us to cleanup our onClose listener.
*/
off: (event: 'close', cb: () => void) => void
closed?: boolean
}
export async function pipeReadable( // Create a promise that will resolve once the response has drained. See
readable: ReadableStream<Uint8Array>, // https://nodejs.org/api/stream.html#stream_event_drain
writable: PipeTarget<Uint8Array>, let drained = new DetachedPromise<void>()
waitUntilForEnd?: Promise<void> function onDrain() {
) { drained.resolve()
const reader = readable.getReader()
let readerDone = false
let writableClosed = false
// It's not enough just to check for `writable.destroyed`, because the client
// may disconnect while we're waiting for a read. We need to immediately
// cancel the readable, and that requires an out-of-band listener.
function onClose() {
writableClosed = true
writable.off('close', onClose)
// If the reader is not yet done, we need to cancel it so that the stream
// source's resources can be cleaned up. If a read is in-progress, this
// will also ensure the read promise rejects and frees our resources.
if (!readerDone) {
readerDone = true
reader.cancel().catch(() => {})
}
} }
writable.on('close', onClose) res.on('drain', onDrain)
try { // If the finish event fires, it means we shouldn't block and wait for the
while (true) { // drain event.
const { done, value } = await reader.read() res.once('close', () => {
readerDone = done res.off('drain', onDrain)
drained.resolve()
})
// Create a promise that will resolve once the response has finished. See
// https://nodejs.org/api/http.html#event-finish_1
const finished = new DetachedPromise<void>()
res.once('finish', () => {
finished.resolve()
})
if (done || writableClosed) { // Create a writable stream that will write to the response.
break return new WritableStream<Uint8Array>({
write: async (chunk) => {
// You'd think we'd want to use `start` instead of placing this in `write`
// but this ensures that we don't actually flush the headers until we've
// started writing chunks.
if (!started) {
started = true
res.flushHeaders()
} }
if (value) { try {
writable.write(value) const ok = res.write(chunk)
writable.flush?.()
// Added by the `compression` middleware, this is a function that will
// flush the partially-compressed response to the client.
if ('flush' in res && typeof res.flush === 'function') {
res.flush()
}
// If the write returns false, it means there's some backpressure, so
// wait until it's streamed before continuing.
if (!ok) {
await drained.promise
// Reset the drained promise so that we can wait for the next drain event.
drained = new DetachedPromise<void>()
}
} catch (err) {
res.end()
throw new Error('failed to write chunk to response', { cause: err })
} }
} },
} catch (e) { abort: (err) => {
// If the client disconnects, we don't want to emit an unhandled error. if (res.writableFinished) return
if (!isAbortError(e)) {
throw e res.destroy(err)
} },
} finally { close: () => {
writable.off('close', onClose) if (res.writableFinished) return
// If we broke out of the loop because of a client disconnect, and the res.end()
// close event hasn't yet fired, we can early cancel. return finished.promise
if (!readerDone) { },
reader.cancel().catch(() => {}) })
} }
// If the client hasn't disconnected yet, end the writable so that the export async function pipeToNodeResponse(
// response sends the final bytes. readable: ReadableStream<Uint8Array>,
if (waitUntilForEnd) { res: ServerResponse
await waitUntilForEnd ) {
} try {
// If the response has already errored, then just return now.
if (!writableClosed) { const { errored, destroyed } = res
writable.end() if (errored || destroyed) return
}
// Create a new AbortController so that we can abort the readable if the
// client disconnects.
const controller = createAbortController(res)
const writer = createWriterFromResponse(res)
await readable.pipeTo(writer, { signal: controller.signal })
} catch (err: any) {
// If this isn't related to an abort error, re-throw it.
if (isAbortError(err)) return
throw new Error('failed to pipe response', { cause: err })
} }
} }
import type { ServerResponse } from 'http'
import type { StaticGenerationStore } from '../client/components/static-generation-async-storage.external' import type { StaticGenerationStore } from '../client/components/static-generation-async-storage.external'
import type { Revalidate } from './lib/revalidate' import type { Revalidate } from './lib/revalidate'
import type { PipeTarget } from './pipe-readable'
import { pipeReadable } from './pipe-readable' import { streamToString } from './stream-utils/node-web-streams-helper'
import { isAbortError, pipeToNodeResponse } from './pipe-readable'
type ContentTypeOption = string | undefined type ContentTypeOption = string | undefined
...@@ -90,31 +91,81 @@ export default class RenderResult { ...@@ -90,31 +91,81 @@ export default class RenderResult {
} }
/** /**
* Returns true if the response is a stream. If the page was dynamic, this * Returns the response if it is a string. If the page was dynamic, this will
* will throw an error. * return a promise if the `stream` option is true, or it will throw an error.
* *
* @param stream Whether or not to return a promise if the response is dynamic
* @returns The response as a string * @returns The response as a string
*/ */
public toUnchunkedString(): string { public toUnchunkedString(stream?: false): string
public toUnchunkedString(stream: true): Promise<string>
public toUnchunkedString(stream = false): Promise<string> | string {
if (this.response === null) {
throw new Error('Invariant: null responses cannot be unchunked')
}
if (typeof this.response !== 'string') { if (typeof this.response !== 'string') {
throw new Error( if (!stream) {
'Invariant: dynamic responses cannot be unchunked. This is a bug in Next.js' throw new Error(
) 'Invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'
)
}
return streamToString(this.response)
} }
return this.response return this.response
} }
public async pipe(res: PipeTarget<Uint8Array>): Promise<void> { /**
* Returns the response if it is a stream, or throws an error if it is a
* string.
*/
private get readable(): ReadableStream<Uint8Array> {
if (this.response === null) { if (this.response === null) {
throw new Error('Invariant: response is null. This is a bug in Next.js') throw new Error('Invariant: null responses cannot be streamed')
} }
if (typeof this.response === 'string') { if (typeof this.response === 'string') {
throw new Error( throw new Error('Invariant: static responses cannot be streamed')
'Invariant: static responses cannot be piped. This is a bug in Next.js'
)
} }
return await pipeReadable(this.response, res, this.waitUntil) return this.response
}
/**
* Pipes the response to a writable stream. This will close/cancel the
* writable stream if an error is encountered.
*
* @param writable Writable stream to pipe the response to
*/
public async pipeTo(writable: WritableStream<Uint8Array>): Promise<void> {
try {
await this.readable.pipeTo(writable)
} catch (err) {
// If this isn't a client abort, then re-throw the error.
if (!isAbortError(err)) {
throw err
}
} finally {
if (this.waitUntil) {
await this.waitUntil
}
}
}
/**
* Pipes the response to a node response. This will close/cancel the node
* response if an error is encountered.
*
* @param res
*/
public async pipeToNodeResponse(res: ServerResponse) {
try {
await pipeToNodeResponse(this.readable, res)
} finally {
if (this.waitUntil) {
await this.waitUntil
}
}
} }
} }
...@@ -113,8 +113,9 @@ let postProcessHTML: typeof import('./post-process').postProcessHTML ...@@ -113,8 +113,9 @@ let postProcessHTML: typeof import('./post-process').postProcessHTML
const DOCTYPE = '<!DOCTYPE html>' const DOCTYPE = '<!DOCTYPE html>'
import './node-polyfill-web-streams'
if (process.env.NEXT_RUNTIME !== 'edge') { if (process.env.NEXT_RUNTIME !== 'edge') {
require('./node-polyfill-web-streams')
tryGetPreviewData = tryGetPreviewData =
require('./api-utils/node/try-get-preview-data').tryGetPreviewData require('./api-utils/node/try-get-preview-data').tryGetPreviewData
warn = require('../build/output/log').warn warn = require('../build/output/log').warn
......
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type RenderResult from '../render-result'
import { isResSent } from '../../shared/lib/utils' import { isResSent } from '../../shared/lib/utils'
import { generateETag } from '../lib/etag' import { generateETag } from '../lib/etag'
import fresh from 'next/dist/compiled/fresh' import fresh from 'next/dist/compiled/fresh'
import type RenderResult from '../render-result'
import { setRevalidateHeaders } from './revalidate-headers' import { setRevalidateHeaders } from './revalidate-headers'
import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers' import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers'
...@@ -67,7 +67,7 @@ export async function sendRenderResult({ ...@@ -67,7 +67,7 @@ export async function sendRenderResult({
setRevalidateHeaders(res, options) setRevalidateHeaders(res, options)
} }
const payload = result.isDynamic ? null : await result.toUnchunkedString() const payload = result.isDynamic ? null : result.toUnchunkedString()
if (payload !== null) { if (payload !== null) {
const etag = generateEtags ? generateETag(payload) : undefined const etag = generateEtags ? generateETag(payload) : undefined
...@@ -95,9 +95,14 @@ export async function sendRenderResult({ ...@@ -95,9 +95,14 @@ export async function sendRenderResult({
if (req.method === 'HEAD') { if (req.method === 'HEAD') {
res.end(null) res.end(null)
} else if (payload !== null) { return
}
if (payload !== null) {
res.end(payload) res.end(payload)
} else { return
await result.pipe(res)
} }
// Pipe the render result to the response after we get a writer for it.
await result.pipeToNodeResponse(res)
} }
import type { BaseNextRequest, BaseNextResponse } from './base-http' import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { NodeNextResponse } from './base-http/node' import type { NodeNextResponse } from './base-http/node'
import { pipeReadable } from './pipe-readable'
import { pipeToNodeResponse } from './pipe-readable'
import { splitCookiesString } from './web/utils' import { splitCookiesString } from './web/utils'
/** /**
...@@ -46,7 +47,13 @@ export async function sendResponse( ...@@ -46,7 +47,13 @@ export async function sendResponse(
// A response body must not be sent for HEAD requests. See https://httpwg.org/specs/rfc9110.html#HEAD // A response body must not be sent for HEAD requests. See https://httpwg.org/specs/rfc9110.html#HEAD
if (response.body && req.method !== 'HEAD') { if (response.body && req.method !== 'HEAD') {
await pipeReadable(response.body, originalResponse, waitUntil) try {
await pipeToNodeResponse(response.body, originalResponse)
} finally {
if (waitUntil) {
await waitUntil
}
}
} else { } else {
originalResponse.end() originalResponse.end()
} }
......
export function encodeText(input: string) { export function createDecodeTransformStream(decoder = new TextDecoder()) {
return new TextEncoder().encode(input) return new TransformStream<Uint8Array, string>({
transform(chunk, controller) {
return controller.enqueue(decoder.decode(chunk, { stream: true }))
},
flush(controller) {
return controller.enqueue(decoder.decode())
},
})
} }
export function decodeText( export function createEncodeTransformStream(encoder = new TextEncoder()) {
input: Uint8Array | undefined, return new TransformStream<string, Uint8Array>({
textDecoder: TextDecoder transform(chunk, controller) {
) { return controller.enqueue(encoder.encode(chunk))
return textDecoder.decode(input, { stream: true }) },
})
} }
...@@ -256,31 +256,11 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -256,31 +256,11 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
) )
} }
let promise: Promise<void> | undefined
if (options.result.isDynamic) { if (options.result.isDynamic) {
const writer = res.transformStream.writable.getWriter() promise = options.result.pipeTo(res.transformStream.writable)
let innerClose: undefined | (() => void)
const target = {
write: (chunk: Uint8Array) => writer.write(chunk),
end: () => writer.close(),
on(_event: 'close', cb: () => void) {
innerClose = cb
},
off(_event: 'close', _cb: () => void) {
innerClose = undefined
},
}
const onClose = () => {
innerClose?.()
}
// No, this cannot be replaced with `finally`, because early cancelling
// the stream will create a rejected promise, and finally will create an
// unhandled rejection.
writer.closed.then(onClose, onClose)
options.result.pipe(target)
} else { } else {
const payload = await options.result.toUnchunkedString() const payload = options.result.toUnchunkedString()
res.setHeader('Content-Length', String(byteLength(payload))) res.setHeader('Content-Length', String(byteLength(payload)))
if (options.generateEtags) { if (options.generateEtags) {
res.setHeader('ETag', generateETag(payload)) res.setHeader('ETag', generateETag(payload))
...@@ -289,6 +269,9 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -289,6 +269,9 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
} }
res.send() res.send()
// If we have a promise, wait for it to resolve.
if (promise) await promise
} }
protected async findPageComponents({ protected async findPageComponents({
......
...@@ -7,6 +7,33 @@ import { getRequestMeta } from '../../../request-meta' ...@@ -7,6 +7,33 @@ import { getRequestMeta } from '../../../request-meta'
import { fromNodeOutgoingHttpHeaders } from '../../utils' import { fromNodeOutgoingHttpHeaders } from '../../utils'
import { NextRequest } from '../request' import { NextRequest } from '../request'
export const ResponseAbortedName = 'ResponseAborted'
export class ResponseAborted extends Error {
public readonly name = ResponseAbortedName
}
/**
* Creates an AbortController tied to the closing of a ServerResponse (or other
* appropriate Writable).
*
* If the `close` event is fired before the `finish` event, then we'll send the
* `abort` signal.
*/
export function createAbortController(response: Writable): AbortController {
const controller = new AbortController()
// If `finish` fires first, then `res.end()` has been called and the close is
// just us finishing the stream on our side. If `close` fires first, then we
// know the client disconnected before we finished.
response.once('close', () => {
if (response.writableFinished) return
controller.abort(new ResponseAborted())
})
return controller
}
/** /**
* Creates an AbortSignal tied to the closing of a ServerResponse (or other * Creates an AbortSignal tied to the closing of a ServerResponse (or other
* appropriate Writable). * appropriate Writable).
...@@ -15,26 +42,14 @@ import { NextRequest } from '../request' ...@@ -15,26 +42,14 @@ import { NextRequest } from '../request'
* the `abort` event will not fire if to data has been fully read (because that * the `abort` event will not fire if to data has been fully read (because that
* will "close" the readable stream and nothing fires after that). * will "close" the readable stream and nothing fires after that).
*/ */
export function signalFromNodeResponse(response: Writable) { export function signalFromNodeResponse(response: Writable): AbortSignal {
const { errored, destroyed } = response const { errored, destroyed } = response
if (errored || destroyed) return AbortSignal.abort(errored) if (errored || destroyed) {
return AbortSignal.abort(errored ?? new ResponseAborted())
const controller = new AbortController()
// If `finish` fires first, then `res.end()` has been called and the close is
// just us finishing the stream on our side. If `close` fires first, then we
// know the client disconnected before we finished.
function onClose() {
controller.abort()
// eslint-disable-next-line @typescript-eslint/no-use-before-define
response.off('finish', onFinish)
}
function onFinish() {
response.off('close', onClose)
} }
response.once('close', onClose)
response.once('finish', onFinish)
return controller.signal const { signal } = createAbortController(response)
return signal
} }
export class NextRequestAdapter { export class NextRequestAdapter {
......
// original source: https://github.com/niklasvh/base64-arraybuffer/blob/master/src/index.ts
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
// Use a lookup table to find the index.
const lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256)
for (let i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i
}
export const encode = (arraybuffer: ArrayBuffer): string => {
let bytes = new Uint8Array(arraybuffer),
i,
len = bytes.length,
base64 = ''
for (i = 0; i < len; i += 3) {
base64 += chars[bytes[i] >> 2]
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]
base64 += chars[bytes[i + 2] & 63]
}
if (len % 3 === 2) {
base64 = base64.substring(0, base64.length - 1) + '='
} else if (len % 3 === 1) {
base64 = base64.substring(0, base64.length - 2) + '=='
}
return base64
}
export const decode = (base64: string): ArrayBuffer => {
let bufferLength = base64.length * 0.75,
len = base64.length,
i,
p = 0,
encoded1,
encoded2,
encoded3,
encoded4
if (base64[base64.length - 1] === '=') {
bufferLength--
if (base64[base64.length - 2] === '=') {
bufferLength--
}
}
const arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer)
for (i = 0; i < len; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)]
encoded2 = lookup[base64.charCodeAt(i + 1)]
encoded3 = lookup[base64.charCodeAt(i + 2)]
encoded4 = lookup[base64.charCodeAt(i + 3)]
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4)
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2)
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63)
}
return arraybuffer
}
const { const {
ReadableStream, ReadableStream,
WritableStream,
TransformStream, TransformStream,
WritableStreamDefaultWriter, WritableStreamDefaultWriter,
} = require('next/dist/compiled/@edge-runtime/ponyfill') } = require('next/dist/compiled/@edge-runtime/ponyfill')
...@@ -16,4 +17,5 @@ WritableStreamDefaultWriter.prototype.write = function (chunk) { ...@@ -16,4 +17,5 @@ WritableStreamDefaultWriter.prototype.write = function (chunk) {
} }
global.ReadableStream = ReadableStream global.ReadableStream = ReadableStream
global.WritableStream = WritableStream
global.TransformStream = TransformStream global.TransformStream = TransformStream
...@@ -57,7 +57,7 @@ createNextDescribe( ...@@ -57,7 +57,7 @@ createNextDescribe(
const after = Date.now() const after = Date.now()
const timeToComplete = after - before const timeToComplete = after - before
expect(timeToComplete < 1000).toBe(true) expect(timeToComplete).toBeLessThan(1000)
expect(await browser.elementByCss('#dashboard-layout').text()).toBe( expect(await browser.elementByCss('#dashboard-layout').text()).toBe(
'Dashboard Hello World' 'Dashboard Hello World'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment