Skip to content

Commit a5a16cb

Browse files
committed
fix: add crypto.randomUUID and webstream polyfills
1 parent 77dfeaf commit a5a16cb

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"promise.allsettled": "^1.0.5",
5050
"react-native-url-polyfill": "^1.3.0",
5151
"typed-emitter": "^2.1.0",
52+
"web-streams-polyfill": "^4.1.0",
5253
"well-known-symbols": "^4.0.0"
5354
},
5455
"devDependencies": {

src/index.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { LogLevel, SetLogLevelOptions } from './logger';
1818
import RNE2EEManager from './e2ee/RNE2EEManager';
1919
import RNKeyProvider, { type RNKeyProviderOptions } from './e2ee/RNKeyProvider';
2020
import { setupNativeEvents } from './events/EventEmitter';
21+
import { ReadableStream, WritableStream } from 'web-streams-polyfill';
2122

2223
/**
2324
* Registers the required globals needed for LiveKit to work.
@@ -34,6 +35,8 @@ export function registerGlobals() {
3435
shimArrayAt();
3536
shimAsyncIterator();
3637
shimIterator();
38+
shimCryptoUuid();
39+
shimWebstreams();
3740
setupNativeEvents();
3841
}
3942

@@ -100,6 +103,33 @@ function shimIterator() {
100103
var shim = require('well-known-symbols/Symbol.iterator/shim');
101104
shim();
102105
}
106+
107+
function shimCryptoUuid() {
108+
if (typeof global.crypto?.randomUUID !== 'function') {
109+
global.crypto.randomUUID = () =>
110+
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
111+
/* eslint-disable no-bitwise */
112+
const r = (Math.random() * 16) | 0;
113+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
114+
return v.toString(16);
115+
}) as `${string}-${string}-${string}-${string}-${string}`;
116+
}
117+
}
118+
119+
function shimWebstreams() {
120+
// @ts-expect-error: global.WritableStream isn't typed here.
121+
if (typeof global.WritableStream === 'undefined') {
122+
// @ts-expect-error
123+
global.WritableStream = WritableStream;
124+
}
125+
126+
// @ts-expect-error: global.ReadableStream isn't typed here.
127+
if (typeof global.ReadableStream === 'undefined') {
128+
// @ts-expect-error
129+
global.WritableStream = ReadableStream;
130+
}
131+
}
132+
103133
export * from './hooks';
104134
export * from './components/BarVisualizer';
105135
export * from './components/LiveKitRoom';

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,7 @@ __metadata:
24862486
typed-emitter: ^2.1.0
24872487
typedoc: ^0.25.13
24882488
typescript: 5.0.4
2489+
web-streams-polyfill: ^4.1.0
24892490
well-known-symbols: ^4.0.0
24902491
peerDependencies:
24912492
"@livekit/react-native-webrtc": ^125.0.9
@@ -12739,6 +12740,13 @@ __metadata:
1273912740
languageName: node
1274012741
linkType: hard
1274112742

12743+
"web-streams-polyfill@npm:^4.1.0":
12744+
version: 4.1.0
12745+
resolution: "web-streams-polyfill@npm:4.1.0"
12746+
checksum: bce16f6bcca895439131fb21e4df15659a34d6f31fedd34b1680ab4fe9c438e02aabd336e4641eb1821e04a8da38dd03c6219fa8ca149ee9a002858a0d32f90d
12747+
languageName: node
12748+
linkType: hard
12749+
1274212750
"webidl-conversions@npm:^3.0.0":
1274312751
version: 3.0.1
1274412752
resolution: "webidl-conversions@npm:3.0.1"

0 commit comments

Comments
 (0)