Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 22f50b6

Browse files
authored
Merge pull request #17 from jakoblorz/feature/streaming-squashed
2 parents 0d6ac31 + a6a6bb5 commit 22f50b6

File tree

9 files changed

+59
-12
lines changed

9 files changed

+59
-12
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"peerDependencies": {
3636
"@remix-run/node": ">=1.4.0",
3737
"@remix-run/server-runtime": ">=1.4.0",
38+
"@remix-run/web-stream": ">=1.0.3",
3839
"electron": ">=16",
3940
"react": ">=17",
4041
"react-dom": ">=17"

pnpm-lock.yaml

Lines changed: 24 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/asset-files.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ test("collectAssetFiles", async () => {
1010
{
1111
"content": [Function],
1212
"path": "/a.txt",
13+
"stream": [Function],
1314
},
1415
{
1516
"content": [Function],
1617
"path": "/b.txt",
18+
"stream": [Function],
1719
},
1820
{
1921
"content": [Function],
2022
"path": "/c/nested.txt",
23+
"stream": [Function],
2124
},
2225
]
2326
`)

src/asset-files.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import glob from "fast-glob"
22
import mime from "mime"
3+
import { createReadStream } from "node:fs"
34
import { readFile } from "node:fs/promises"
45
import { relative } from "node:path"
6+
import { Readable } from "node:stream"
57

68
export type AssetFile = {
79
path: string
810
content: () => Promise<string | Buffer>
11+
stream: () => Readable
912
}
1013

1114
export async function collectAssetFiles(folder: string): Promise<AssetFile[]> {
@@ -18,6 +21,7 @@ export async function collectAssetFiles(folder: string): Promise<AssetFile[]> {
1821
return files.map((file) => ({
1922
path: "/" + relative(folder, file).replace(/\\/g, "/"),
2023
content: () => readFile(file),
24+
stream: () => createReadStream(file),
2125
}))
2226
}
2327

@@ -31,7 +35,7 @@ export async function serveAsset(
3135
if (!file) return
3236

3337
return {
34-
data: await file.content(),
38+
data: file.stream(),
3539
mimeType: mime.getType(file.path) ?? undefined,
3640
}
3741
}

src/browser-globals.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ Object.assign(globalThis, {
1515
FormData,
1616
AbortController,
1717
})
18+
19+
if (typeof globalThis.ReadableStream === "undefined") {
20+
const { ReadableStream } = require("@remix-run/web-stream")
21+
globalThis.ReadableStream = ReadableStream
22+
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function initRemix({
6363
? require.resolve(serverBuildOption)
6464
: undefined
6565

66-
protocol.interceptBufferProtocol("http", async (request, callback) => {
66+
protocol.interceptStreamProtocol("http", async (request, callback) => {
6767
try {
6868
if (mode === "development") {
6969
assetFiles = await collectAssetFiles(publicFolder)

src/serve-remix-response.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import type { RequestHandler } from "@remix-run/server-runtime"
2+
import { Readable, PassThrough } from "node:stream"
23
import "./browser-globals"
34

5+
function createPassThroughStream(text: any) {
6+
const readable = new PassThrough()
7+
readable.push(text)
8+
readable.push(null) // eslint-disable-line unicorn/no-null, unicorn/no-array-push-push
9+
return readable
10+
}
11+
412
export async function serveRemixResponse(
513
request: Electron.ProtocolRequest,
614
handleRequest: RequestHandler,
@@ -27,8 +35,17 @@ export async function serveRemixResponse(
2735
values.push(value)
2836
}
2937

38+
if (response.body instanceof ReadableStream) {
39+
return {
40+
// @ts-expect-error
41+
data: Readable.from(response.body),
42+
headers,
43+
statusCode: response.status,
44+
}
45+
}
46+
3047
return {
31-
data: Buffer.from(await response.arrayBuffer()),
48+
data: createPassThroughStream(Buffer.from(await response.arrayBuffer())),
3249
headers,
3350
statusCode: response.status,
3451
}

template/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@remix-run/node": "^1.4.0",
2424
"@remix-run/react": "^1.4.0",
2525
"@remix-run/server-runtime": "^1.4.0",
26+
"@remix-run/web-stream": ">=1.0.3",
2627
"fast-glob": "^3.2.11",
2728
"react": "^18.0.0",
2829
"react-dom": "^18.0.0",

tests/fixtures/test-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@remix-run/node": "^1.4.0",
1212
"@remix-run/react": "^1.4.0",
1313
"@remix-run/server-runtime": "^1.4.0",
14+
"@remix-run/web-stream": ">=1.0.3",
1415
"fast-glob": "^3.2.11",
1516
"react": "^18.0.0",
1617
"react-dom": "^18.0.0",

0 commit comments

Comments
 (0)