Skip to content

Commit a7f58d0

Browse files
Remove conditional bun guards
Co-authored-by: me <me@kentcdodds.com>
1 parent 60cb2be commit a7f58d0

3 files changed

Lines changed: 41 additions & 111 deletions

File tree

app/entry.server.tsx

Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createReadableStreamFromReadable } from '@react-router/node'
33
import * as Sentry from '@sentry/react-router'
44
import chalk from 'chalk'
55
import { isbot } from 'isbot'
6-
import * as ReactDOMServer from 'react-dom/server'
6+
import { renderToPipeableStream } from 'react-dom/server.node'
77
import {
88
type ActionFunctionArgs,
99
type HandleDocumentRequestFunction,
@@ -19,16 +19,6 @@ import { authSessionStorage } from './utils/session.server.ts'
1919
import { makeTimings } from './utils/timing.server.ts'
2020

2121
const ABORT_DELAY = 5000
22-
const renderToPipeableStream = (
23-
ReactDOMServer as {
24-
renderToPipeableStream?: typeof import('react-dom/server').renderToPipeableStream
25-
}
26-
).renderToPipeableStream
27-
const renderToReadableStream = (
28-
ReactDOMServer as {
29-
renderToReadableStream?: (...args: any[]) => Promise<ReadableStream>
30-
}
31-
).renderToReadableStream
3222

3323
initEnv()
3424
global.ENV = getEnv()
@@ -85,74 +75,40 @@ export default async function handleRequest(...args: DocRequestArgs) {
8575
: 'onShellReady'
8676

8777
const nonce = loadContext.cspNonce?.toString() ?? ''
88-
if (renderToPipeableStream) {
89-
return new Promise((resolve, reject) => {
90-
let didError = false
91-
// NOTE: this timing will only include things that are rendered in the shell
92-
// and will not include suspended components and deferred loaders
93-
const timings = makeTimings('render', 'renderToPipeableStream')
94-
95-
const { pipe, abort } = renderToPipeableStream(
96-
<NonceProvider value={nonce}>
97-
<ServerRouter context={remixContext} url={request.url} nonce={nonce} />
98-
</NonceProvider>,
99-
{
100-
[callbackName]: () => {
101-
const body = new PassThrough()
102-
responseHeaders.set('Content-Type', 'text/html')
103-
responseHeaders.append('Server-Timing', timings.toString())
104-
resolve(
105-
new Response(createReadableStreamFromReadable(body), {
106-
headers: responseHeaders,
107-
status: didError ? 500 : responseStatusCode,
108-
}),
109-
)
110-
pipe(body)
111-
},
112-
onShellError: (err: unknown) => {
113-
reject(err)
114-
},
115-
onError: () => {
116-
didError = true
117-
},
118-
nonce,
78+
return new Promise((resolve, reject) => {
79+
let didError = false
80+
// NOTE: this timing will only include things that are rendered in the shell
81+
// and will not include suspended components and deferred loaders
82+
const timings = makeTimings('render', 'renderToPipeableStream')
83+
84+
const { pipe, abort } = renderToPipeableStream(
85+
<NonceProvider value={nonce}>
86+
<ServerRouter context={remixContext} url={request.url} nonce={nonce} />
87+
</NonceProvider>,
88+
{
89+
[callbackName]: () => {
90+
const body = new PassThrough()
91+
responseHeaders.set('Content-Type', 'text/html')
92+
responseHeaders.append('Server-Timing', timings.toString())
93+
resolve(
94+
new Response(createReadableStreamFromReadable(body), {
95+
headers: responseHeaders,
96+
status: didError ? 500 : responseStatusCode,
97+
}),
98+
)
99+
pipe(body)
119100
},
120-
)
121-
122-
setTimeout(abort, ABORT_DELAY)
123-
})
124-
}
125-
126-
if (!renderToReadableStream) {
127-
throw new Error('No compatible React DOM server renderer found.')
128-
}
129-
130-
let didError = false
131-
const timings = makeTimings('render', 'renderToReadableStream')
132-
const abortController = new AbortController()
133-
setTimeout(() => abortController.abort(), ABORT_DELAY)
134-
const body = await renderToReadableStream(
135-
<NonceProvider value={nonce}>
136-
<ServerRouter context={remixContext} url={request.url} nonce={nonce} />
137-
</NonceProvider>,
138-
{
139-
nonce,
140-
signal: abortController.signal,
141-
onError: () => {
142-
didError = true
101+
onShellError: (err: unknown) => {
102+
reject(err)
103+
},
104+
onError: () => {
105+
didError = true
106+
},
107+
nonce,
143108
},
144-
},
145-
)
146-
147-
if (callbackName === 'onAllReady' && 'allReady' in body) {
148-
await (body as ReadableStream & { allReady?: Promise<void> }).allReady
149-
}
109+
)
150110

151-
responseHeaders.set('Content-Type', 'text/html')
152-
responseHeaders.append('Server-Timing', timings.toString())
153-
return new Response(body, {
154-
headers: responseHeaders,
155-
status: didError ? 500 : responseStatusCode,
111+
setTimeout(abort, ABORT_DELAY)
156112
})
157113
}
158114

app/utils/cache.server.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,19 @@ import {
1111
type CreateReporter,
1212
} from '@epic-web/cachified'
1313
import { remember } from '@epic-web/remember'
14+
import { Database } from 'bun:sqlite'
1415
import { LRUCache } from 'lru-cache'
1516
import { z } from 'zod'
1617
import { updatePrimaryCacheValue } from '#app/routes/_app+/admin+/cache_.sqlite.server.js'
1718
import { getInstanceInfo, getInstanceInfoSync } from './litefs.server.ts'
1819
import { cachifiedTimingReporter, type Timings } from './timing.server.ts'
1920

20-
type CacheDatabase = import('bun:sqlite').Database
21-
22-
const isBunRuntime = typeof process.versions?.bun === 'string'
23-
const BunDatabase = isBunRuntime ? (await import('bun:sqlite')).Database : null
24-
2521
const CACHE_DATABASE_PATH = process.env.CACHE_DATABASE_PATH
2622

27-
const cacheDb = remember<CacheDatabase | null>('cacheDb', createDatabase)
23+
const cacheDb = remember('cacheDb', createDatabase)
2824

29-
function createDatabase(tryAgain = true): CacheDatabase | null {
30-
if (!BunDatabase) return null
31-
const db = new BunDatabase(CACHE_DATABASE_PATH)
25+
function createDatabase(tryAgain = true): Database {
26+
const db = new Database(CACHE_DATABASE_PATH)
3227
const { currentIsPrimary } = getInstanceInfoSync()
3328
if (!currentIsPrimary) return db
3429

@@ -87,11 +82,8 @@ const cacheQueryResultSchema = z.object({
8782
})
8883

8984
export const cache: CachifiedCache = {
90-
name: cacheDb ? 'SQLite cache' : 'LRU cache',
85+
name: 'SQLite cache',
9186
get(key) {
92-
if (!cacheDb) {
93-
return lruCache.get(key) ?? null
94-
}
9587
const result = cacheDb
9688
.prepare('SELECT value, metadata FROM cache WHERE key = ?')
9789
.get(key)
@@ -108,10 +100,6 @@ export const cache: CachifiedCache = {
108100
return { metadata, value }
109101
},
110102
async set(key, entry) {
111-
if (!cacheDb) {
112-
lruCache.set(key, entry)
113-
return
114-
}
115103
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
116104
if (currentIsPrimary) {
117105
cacheDb
@@ -139,10 +127,6 @@ export const cache: CachifiedCache = {
139127
}
140128
},
141129
async delete(key) {
142-
if (!cacheDb) {
143-
lruCache.delete(key)
144-
return
145-
}
146130
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
147131
if (currentIsPrimary) {
148132
cacheDb.prepare('DELETE FROM cache WHERE key = ?').run(key)
@@ -164,10 +148,8 @@ export const cache: CachifiedCache = {
164148

165149
export async function getAllCacheKeys(limit: number) {
166150
const sqliteRows = cacheDb
167-
? (cacheDb
168-
.prepare('SELECT key FROM cache LIMIT ?')
169-
.all(limit) as Array<{ key: string }>)
170-
: []
151+
.prepare('SELECT key FROM cache LIMIT ?')
152+
.all(limit) as Array<{ key: string }>
171153
return {
172154
sqlite: sqliteRows.map((row) => row.key),
173155
lru: [...lru.keys()],
@@ -176,10 +158,8 @@ export async function getAllCacheKeys(limit: number) {
176158

177159
export async function searchCacheKeys(search: string, limit: number) {
178160
const sqliteRows = cacheDb
179-
? (cacheDb
180-
.prepare('SELECT key FROM cache WHERE key LIKE ? LIMIT ?')
181-
.all(`%${search}%`, limit) as Array<{ key: string }>)
182-
: []
161+
.prepare('SELECT key FROM cache WHERE key LIKE ? LIMIT ?')
162+
.all(`%${search}%`, limit) as Array<{ key: string }>
183163
return {
184164
sqlite: sqliteRows.map((row) => row.key),
185165
lru: [...lru.keys()].filter((key) => key.includes(search)),

tests/setup/global-setup.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ import { execaCommand } from 'execa'
33
import fsExtra from 'fs-extra'
44
import 'dotenv/config'
55
import '#app/utils/env.server.ts'
6-
// Avoid loading bun:sqlite when Playwright runs under Node.
7-
const shouldInitCache = typeof process.versions?.bun === 'string'
86

97
export const BASE_DATABASE_PATH = path.join(
108
process.cwd(),
119
`./tests/prisma/base.db`,
1210
)
1311

1412
export async function setup() {
15-
if (shouldInitCache) {
16-
await import('#app/utils/cache.server.ts')
17-
}
18-
1913
const databaseExists = await fsExtra.pathExists(BASE_DATABASE_PATH)
2014

2115
if (databaseExists) {

0 commit comments

Comments
 (0)