Skip to content

Commit 09ebc1f

Browse files
committed
fix: exports on safari
1 parent f7b0302 commit 09ebc1f

File tree

5 files changed

+581
-11
lines changed

5 files changed

+581
-11
lines changed

apps/postgres-new/app/import/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
import { Button } from '~/components/ui/button'
1313
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '~/components/ui/dialog'
1414
import { Progress } from '~/components/ui/progress'
15-
import '~/polyfills/readable-stream'
1615

1716
import { useQueryClient } from '@tanstack/react-query'
1817
import { Semaphore } from 'async-mutex'

apps/postgres-new/next.config.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRequire } from 'module'
22
import { readFile } from 'node:fs/promises'
33
import { join } from 'node:path'
4+
import webpack from 'webpack'
45

56
/** @type {import('next').NextConfig} */
67
const nextConfig = {
@@ -17,6 +18,13 @@ const nextConfig = {
1718
},
1819
}
1920

21+
// Polyfill `ReadableStream`
22+
config.plugins.push(
23+
new webpack.ProvidePlugin({
24+
ReadableStream: [join(import.meta.dirname, 'polyfills/readable-stream.ts'), 'default'],
25+
})
26+
)
27+
2028
// See https://webpack.js.org/configuration/resolve/#resolvealias
2129
config.resolve.alias = {
2230
...config.resolve.alias,

apps/postgres-new/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"postcss": "^8",
8888
"tailwindcss": "^3.4.6",
8989
"tailwindcss-radix": "^3.0.3",
90-
"typescript": "^5.5.2"
90+
"typescript": "^5.5.2",
91+
"webpack": "^5.95.0"
9192
}
9293
}

apps/postgres-new/polyfills/readable-stream.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// `.from()` is expected by `@std/tar`
2-
;(ReadableStream as any).from ??= function <T>(iterator: Iterator<T> | AsyncIterator<T>) {
3-
return new ReadableStream<T>({
2+
;(globalThis as any).ReadableStream.from ??= function <T>(
3+
iterator: Iterator<T> | AsyncIterator<T>
4+
) {
5+
return new globalThis.ReadableStream<T>({
46
async pull(controller) {
57
try {
68
const { value, done } = await iterator.next()
@@ -16,8 +18,8 @@
1618
})
1719
}
1820

19-
// Some browsers don't yet make `ReadableStream` async iterable (eg. Safari), so polyfill
20-
ReadableStream.prototype.values ??= function <T>({
21+
// Some browsers don't make `ReadableStream` async iterable (eg. Safari), so polyfill
22+
globalThis.ReadableStream.prototype.values ??= function <T>({
2123
preventCancel = false,
2224
} = {}): AsyncIterableIterator<T> {
2325
const reader = this.getReader()
@@ -50,4 +52,17 @@ ReadableStream.prototype.values ??= function <T>({
5052
}
5153
}
5254

53-
ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values
55+
globalThis.ReadableStream.prototype[Symbol.asyncIterator] ??=
56+
globalThis.ReadableStream.prototype.values
57+
58+
// @std/tar conditionally uses `ReadableStreamBYOBReader` which isn't supported in Safari,
59+
// so patch `ReadableStream`'s constructor to prevent using BYOB.
60+
// Webpack's `ProvidePlugin` replaces `ReadableStream` references with this patch
61+
export default class PatchedReadableStream<T> extends globalThis.ReadableStream<T> {
62+
constructor(underlyingSource?: UnderlyingSource<T>, strategy?: QueuingStrategy<T>) {
63+
if (underlyingSource?.type === 'bytes' && !('ReadableStreamBYOBReader' in globalThis)) {
64+
underlyingSource.type = undefined
65+
}
66+
super(underlyingSource, strategy)
67+
}
68+
}

0 commit comments

Comments
 (0)