Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-Arrowood committed May 11, 2024
1 parent 4a090bb commit afdb710
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
37 changes: 36 additions & 1 deletion packages/next/src/server/stream-utils/stream-utils.node.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createBufferedTransformStream } from './stream-utils.node'
import {
createBufferedTransformStream,
createInsertedHTMLStream,
} from './stream-utils.node'
import { PassThrough } from 'node:stream'
import { renderToPipeableStream } from 'react-dom/server.node'
import { Suspense } from 'react'
Expand Down Expand Up @@ -55,3 +58,35 @@ describe('createBufferedTransformStream', () => {
})
})
})

describe('createInsertedHTMLStream', () => {
it('should insert html to the beginning of the stream', async () => {
const insertedHTML = '<foo></foo>'
const stream = createInsertedHTMLStream(() => Promise.resolve(insertedHTML))
const input = await createInput()
const output = input.pipe(stream)

const actualChunks = await new Promise<Buffer[]>((resolve) => {
const chunks: Buffer[] = []
output.on('readable', () => {
let chunk
while (null !== (chunk = output.read())) {
chunks.push(chunk)
}
})
output.on('end', () => {
resolve(chunks)
})
})

console.log(actualChunks)

expect(actualChunks.length).toBe(2)
const encoder = new TextEncoder()
const expected = encoder.encode(insertedHTML)
expect(actualChunks[0].indexOf(expected)).toBe(0)
expect(
new Uint8Array(actualChunks[0].subarray(expected.length))
).toStrictEqual(expected)
})
})
26 changes: 16 additions & 10 deletions packages/next/src/server/stream-utils/stream-utils.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,24 @@ export function createBufferedTransformStream(): Transform {
})
}

const encoder = new TextEncoder()

export function createInsertedHTMLStream(
getServerInsertedHTML: () => Promise<string>
): Transform {
return new Transform({
transform(chunk, _, callback) {
getServerInsertedHTML().then((html) => {
if (html) {
this.push(Buffer.from(html))
}
getServerInsertedHTML()
.then((html) => {
if (html) {
this.push(encoder.encode(html))
}

return callback(null, chunk)
})
return callback(null, chunk)
})
.catch((err) => {
return callback(err)
})
},
})
}
Expand All @@ -187,16 +193,16 @@ export function createHeadInsertionTransformStream(
.then((insertion) => {
if (inserted) {
if (insertion) {
this.push(Buffer.from(insertion))
this.push(encoder.encode(insertion))
}
this.push(chunk)
freezing = true
} else {
const index = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.HEAD)
if (index !== -1) {
if (insertion) {
const encodedInsertion = Buffer.from(insertion)
const insertedHeadContent = Buffer.alloc(
const encodedInsertion = encoder.encode(insertion)
const insertedHeadContent = new Uint8Array(
chunk.length + encodedInsertion.length
)
insertedHeadContent.set(chunk.slice(0, index))
Expand Down Expand Up @@ -233,7 +239,7 @@ export function createHeadInsertionTransformStream(
insert()
.then((insertion) => {
if (insertion) {
this.push(Buffer.from(insertion))
this.push(encoder.encode(insertion))
callback()
}
})
Expand Down

0 comments on commit afdb710

Please sign in to comment.