Skip to content

Commit

Permalink
remove more decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-Arrowood committed Mar 18, 2024
1 parent a47aa8a commit c34c21e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
8 changes: 7 additions & 1 deletion packages/next/src/server/stream-utils/encodedTags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export const ENCODED_TAGS = {
OPENING: {},
// opening tags do not have the closing `>` since they can contain other attributes such as `<body className=''>`
OPENING: {
// <html
HTML: new Uint8Array([60, 104, 116, 109, 108]),
// <body
BODY: new Uint8Array([60, 98, 111, 100, 121]),
},
CLOSED: {
// </head>
HEAD: new Uint8Array([60, 47, 104, 101, 97, 100, 62]),
Expand Down
24 changes: 11 additions & 13 deletions packages/next/src/server/stream-utils/node-web-streams-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DetachedPromise } from '../../lib/detached-promise'
import { scheduleImmediate, atLeastOneTask } from '../../lib/scheduler'
import { ENCODED_TAGS } from './encodedTags'
import {
concatUint8Arrays,
indexOfUint8Array,
isEquivalentUint8Arrays,
removeFromUint8Array,
Expand Down Expand Up @@ -444,31 +445,28 @@ export function createRootLayoutValidatorStream(): TransformStream<
let foundHtml = false
let foundBody = false

const decoder = new TextDecoder()

let content = ''
let content = new Uint8Array(0)
return new TransformStream({
async transform(chunk, controller) {
content = concatUint8Arrays(content, chunk)
// Peek into the streamed chunk to see if the tags are present.
if (!foundHtml || !foundBody) {
content += decoder.decode(chunk, { stream: true })
if (!foundHtml && content.includes('<html')) {
if (
!foundHtml &&
indexOfUint8Array(content, ENCODED_TAGS.OPENING.HTML)
) {
foundHtml = true
}
if (!foundBody && content.includes('<body')) {
if (
!foundBody &&
indexOfUint8Array(content, ENCODED_TAGS.OPENING.BODY)
) {
foundBody = true
}
}
controller.enqueue(chunk)
},
flush(controller) {
// Flush the decoder.
if (!foundHtml || !foundBody) {
content += decoder.decode()
if (!foundHtml && content.includes('<html')) foundHtml = true
if (!foundBody && content.includes('<body')) foundBody = true
}

const missingTags: typeof window.__next_root_layout_missing_tags = []
if (!foundHtml) missingTags.push('html')
if (!foundBody) missingTags.push('body')
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/server/stream-utils/uint8array-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ export function removeFromUint8Array(a: Uint8Array, b: Uint8Array) {
return a
}
}

/**
* Combines two Uint8Arrays together in order.
*
* `(a, b)` will be returned as `a + b`
*/
export function concatUint8Arrays(a: Uint8Array, b: Uint8Array) {
const result = new Uint8Array(a.length + b.length)
result.set(a)
result.set(b, a.length)
return result
}

0 comments on commit c34c21e

Please sign in to comment.