UNPKG

2.04 kBJavaScriptView Raw
1import { BASE_URL } from '@env'
2
3// React Native requires manual configuration of the ShareDB client
4import ShareDB from 'sharedb/lib/client'
5
6// isomorphic ShareDB initialization
7import commonInit from './common'
8
9// BASE_URL must be specified, otherwise Native app doesn't know which server to connect to
10if (!BASE_URL) {
11 console.error(`
12 !!!WARNING!!! No BASE_URL specified in .env
13
14 Please read below how to fix:
15
16 BASE_URL is not specified.
17 Please create a file '.env.local' with BASE_URL of your machine/server:
18
19 BASE_URL="http://YOUR_MACHINE_IP:3000"
20
21 When developing locally, make sure YOUR_MACHINE_IP is not localhost but the actual IP address
22 on your local network (like 192.168.0.100). Your smartphone should be connected to the same
23 network as your machine. The native app will use YOUR_MACHINE_IP to connect to your local server.
24
25 By default the server is configured to run on port 3000, so if you didn't change the PORT of the server, make
26 sure to specify ':3000' in the BASE_URL.
27
28 When running a production build, specify the proper BASE_URL in a '.env.production' file:
29
30 BASE_URL="https://example.com"
31 `)
32}
33
34// Make it behave like a browser to trick Racer
35// https://github.com/derbyjs/racer/blob/master/lib/util.js#L3
36process.title = 'browser'
37
38// Polyfill process.nextTick
39process.nextTick = process.nextTick || setImmediate
40
41// Get configuration from BASE_URL env var
42window.__racerHighwayClientOptions = {
43 base: '/channel',
44 reconnect: true,
45 browserChannelOnly: false,
46 srvProtocol: getProtocol(),
47 srvHost: getHost(),
48 srvPort: getPort(),
49 srvSecurePort: getPort(),
50 timeout: 10000,
51 timeoutIncrement: 10000
52}
53function getHost () {
54 return (BASE_URL.match(/\/\/([^/:]+)/) || [])[1] || 'localhost'
55}
56function getProtocol () {
57 return /https:/.test(BASE_URL) ? 'https:' : 'http:'
58}
59function getPort () {
60 let port = ~~(BASE_URL.match(/:(\d+)/) || [])[1]
61 if (!port) {
62 let protocol = getProtocol()
63 port = protocol === 'https:' ? 443 : 80
64 }
65 return port
66}
67
68commonInit(ShareDB)