Skip to content

Commit 2227e0f

Browse files
Add React Native support to the Typescript SDK (#2955)
This PR helps to support React Native in the Typescript SDK. We have identified two issues: 1. Certain versions of React Native exhibit a bug where the constructor mistakenly treats a URL object as a string. This causes an error when the constructor attempts to call `.endsWith()` on the URL object, leading to runtime errors. This PR adds a .toString() call when passing a URL instance to the URL constructor. 2. React Native doesn't provide an implementation for TextEncoder and TextDecoder which are used to read/write strings in our binary reader and writer. This PR use introduce the usage of a library for these two types. Note: this still requires the use of a Polyfill as React Native URL implementation is missing most methods in version older than 0.79 (>= 6 month old)
1 parent 136d5dd commit 2227e0f

File tree

5 files changed

+16
-1
lines changed

5 files changed

+16
-1
lines changed

sdks/typescript/packages/sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"undici": "^6.19.2"
4848
},
4949
"dependencies": {
50+
"@zxing/text-encoding": "^0.9.0",
5051
"base64-js": "^1.5.1"
5152
}
5253
}

sdks/typescript/packages/sdk/src/binary_reader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { TextDecoder } from '@zxing/text-encoding';
2+
13
export default class BinaryReader {
24
#buffer: DataView;
35
#offset: number = 0;

sdks/typescript/packages/sdk/src/binary_writer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { fromByteArray } from 'base64-js';
2+
import { TextEncoder } from '@zxing/text-encoding';
23

34
export default class BinaryWriter {
45
#buffer: Uint8Array;

sdks/typescript/packages/sdk/src/db_connection_impl.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ export class DbConnectionImpl<
202202
}: DbConnectionConfig) {
203203
stdbLogger('info', 'Connecting to SpacetimeDB WS...');
204204

205-
let url = new URL(uri);
205+
// We use .toString() here because some versions of React Native contain a bug where the URL constructor
206+
// incorrectly treats a URL instance as a plain string.
207+
// This results in an attempt to call .endsWith() on it, leading to an error.
208+
let url = new URL(uri.toString());
206209
if (!/^wss?:/.test(uri.protocol)) {
207210
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
208211
}

sdks/typescript/pnpm-lock.yaml

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

0 commit comments

Comments
 (0)