@sleeperhq/mini-core 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/dev_server/useContext.tsx +0 -317
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleeperhq/mini-core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Core library frameworks for developing Sleeper Mini Apps.",
|
|
5
5
|
"main": "index.ts",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@callstack/repack": "blitzstudios/repack.git#callstack-repack-v2.6.
|
|
37
|
+
"@callstack/repack": "blitzstudios/repack.git#callstack-repack-v2.6.3-gitpkg",
|
|
38
38
|
"@react-native-community/netinfo": "9.3.7",
|
|
39
39
|
"axios": "0.15.3",
|
|
40
40
|
"lodash": "4.17.21",
|
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
import React, {useEffect, useRef, useState} from 'react';
|
|
2
|
-
import {Platform} from 'react-native';
|
|
3
|
-
import {Config, SocketMessage} from '../types';
|
|
4
|
-
import { ScriptLocatorResolver, ScriptManager, Federated } from '@callstack/repack/client';
|
|
5
|
-
import NetInfo from '@react-native-community/netinfo';
|
|
6
|
-
import TcpSocket from 'react-native-tcp-socket';
|
|
7
|
-
import { fetchMainVersionMap, getMainUrl } from './url_resolver';
|
|
8
|
-
|
|
9
|
-
let config: Config;
|
|
10
|
-
const RETRY_TIMER = 5000;
|
|
11
|
-
|
|
12
|
-
const DevServer = props => {
|
|
13
|
-
const connection = useRef<TcpSocket.Socket>();
|
|
14
|
-
const partialMessage = useRef('');
|
|
15
|
-
const messageLength = useRef(0);
|
|
16
|
-
const messageType = useRef('');
|
|
17
|
-
const _retryTimer = useRef<NodeJS.Timeout>();
|
|
18
|
-
|
|
19
|
-
const [data, setData] = useState({
|
|
20
|
-
platform: '',
|
|
21
|
-
binaryVersion: '',
|
|
22
|
-
dist: '',
|
|
23
|
-
isStaging: false,
|
|
24
|
-
});
|
|
25
|
-
const _dataRef = useRef<typeof data>();
|
|
26
|
-
const _versionMap = useRef<Record<string, string>>();
|
|
27
|
-
|
|
28
|
-
const _onConnected = async (value: boolean) => {
|
|
29
|
-
props.onConnected(value);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const onSocket = (handler) => msg => {
|
|
33
|
-
let msgString: string = msg.toString();
|
|
34
|
-
while (msgString.length > 0) {
|
|
35
|
-
if (messageLength.current === 0) {
|
|
36
|
-
const delimit = msgString.indexOf('\n');
|
|
37
|
-
if (delimit === -1) {
|
|
38
|
-
console.log("[Sleeper] Message header not found, throwing out message.");
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const header = msgString.substring(0, delimit);
|
|
43
|
-
try {
|
|
44
|
-
const headerObject = JSON.parse(header);
|
|
45
|
-
messageType.current = headerObject.type;
|
|
46
|
-
messageLength.current = headerObject.size;
|
|
47
|
-
} catch (e) {
|
|
48
|
-
console.log("[Sleeper] Message header malformed, throwing out message.");
|
|
49
|
-
messageLength.current = 0;
|
|
50
|
-
messageType.current = '';
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
msgString = msgString.substring(delimit + 1);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const partialLength = messageLength.current - partialMessage.current.length;
|
|
58
|
-
if (partialLength < 0) {
|
|
59
|
-
// We need to wait for more data
|
|
60
|
-
partialMessage.current += msgString;
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const remainingLength = msgString.length - partialLength;
|
|
65
|
-
if (remainingLength === 0) {
|
|
66
|
-
// We have the full message
|
|
67
|
-
partialMessage.current += msgString;
|
|
68
|
-
msgString = '';
|
|
69
|
-
if (config.logsEnabled) console.log("[Sleeper] Message built.", partialMessage.current.length);
|
|
70
|
-
|
|
71
|
-
} else {
|
|
72
|
-
// We have more than the full message
|
|
73
|
-
partialMessage.current += msgString.substring(0, partialLength);
|
|
74
|
-
msgString = msgString.substring(partialLength);
|
|
75
|
-
|
|
76
|
-
if (remainingLength <= 0) {
|
|
77
|
-
// We have less than the full message
|
|
78
|
-
if (config.logsEnabled) console.log("[Sleeper] Building message: ", partialMessage.current.length, messageLength.current, remainingLength);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
const json = JSON.parse(partialMessage.current);
|
|
85
|
-
partialMessage.current = '';
|
|
86
|
-
messageLength.current = 0;
|
|
87
|
-
|
|
88
|
-
// Set connection data
|
|
89
|
-
if (json._platform || json._binaryVersion || json._dist || json._isStaging) {
|
|
90
|
-
if (config.logsEnabled) console.log("[Sleeper] Processing context data:", json._platform, json._binaryVersion, json._dist, json._isStaging);
|
|
91
|
-
setData({
|
|
92
|
-
platform: json._platform,
|
|
93
|
-
binaryVersion: json._binaryVersion,
|
|
94
|
-
dist: json._dist,
|
|
95
|
-
isStaging: json._isStaging,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (messageType.current === 'context') {
|
|
100
|
-
// We should have a context object now
|
|
101
|
-
const context = new Proxy(json, handler);
|
|
102
|
-
props.onContextChanged(context);
|
|
103
|
-
} else if (messageType.current === `partialContext`) {
|
|
104
|
-
// We are updating a partial Context
|
|
105
|
-
props.onContextUpdated(json)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
messageType.current = '';
|
|
109
|
-
} catch (e) {
|
|
110
|
-
console.log("[Sleeper] Failed to parse message: ", e);
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const sendContextRequest = (socket, propertyPath) => {
|
|
117
|
-
const message: SocketMessage = {_contextGet: propertyPath};
|
|
118
|
-
const json = JSON.stringify(message);
|
|
119
|
-
try {
|
|
120
|
-
socket?.write(json + '\n');
|
|
121
|
-
} catch (e) {
|
|
122
|
-
console.log("[Sleeper] Failed to send context request: ", e);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const proxyHandler = (socket) => {
|
|
127
|
-
return {
|
|
128
|
-
get: (target, property) => {
|
|
129
|
-
let value = Reflect.get(target, property);
|
|
130
|
-
|
|
131
|
-
// Check if we need to add a proxy to this object
|
|
132
|
-
if (!!value && typeof value === 'object' && !value._isProxy && value._isProxyInternal) {
|
|
133
|
-
const isLeaf = !value._continueProxy;
|
|
134
|
-
// Adding proxies to objects
|
|
135
|
-
const handler = proxyHandlerChild(socket, property, isLeaf);
|
|
136
|
-
const proxiedValue = new Proxy(value, handler);
|
|
137
|
-
Reflect.set(target, property, proxiedValue);
|
|
138
|
-
value = proxiedValue;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return value;
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const proxyHandlerChild = (socket, path, isLeaf) => {
|
|
147
|
-
return {
|
|
148
|
-
get: (target, property) => {
|
|
149
|
-
// Check if a proxy was already added to this object
|
|
150
|
-
if (property === '_isProxy') {
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const value = Reflect.get(target, property);
|
|
155
|
-
const fullPropertyPath = `${path}.${property}`;
|
|
156
|
-
|
|
157
|
-
// If the value is undefined, we need to request it from the server
|
|
158
|
-
if (value === undefined && isLeaf) {
|
|
159
|
-
if (config.logsEnabled) console.log("[Sleeper] Requesting context value: ", fullPropertyPath);
|
|
160
|
-
sendContextRequest(socket, fullPropertyPath);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Check if we need to add a second layer proxy to this object
|
|
164
|
-
if (!isLeaf && !value?._isProxy) {
|
|
165
|
-
const nextLeaf = true; // Currently we only support 2 layers of proxies
|
|
166
|
-
// Adding proxies to objects
|
|
167
|
-
// These proxies aren't stored in the context object so we will regenerate them every time
|
|
168
|
-
const handler = proxyHandlerChild(socket, fullPropertyPath, nextLeaf);
|
|
169
|
-
if (value === undefined) {
|
|
170
|
-
return new Proxy({}, handler);
|
|
171
|
-
} else {
|
|
172
|
-
return new Proxy(value, handler);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return value;
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const startSocket = async () => {
|
|
182
|
-
const netInfo = await NetInfo.fetch();
|
|
183
|
-
const netInfoDetails = netInfo?.details;
|
|
184
|
-
const ipAddress = netInfoDetails?.ipAddress;
|
|
185
|
-
|
|
186
|
-
if (!netInfoDetails || !('ipAddress' in netInfoDetails)) {
|
|
187
|
-
console.error('[Sleeper] Failed to determine local IP address.');
|
|
188
|
-
return stopSocket();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
connection.current = TcpSocket.createConnection({
|
|
192
|
-
port: config.remoteSocketPort || 9092,
|
|
193
|
-
host: config.remoteIP,
|
|
194
|
-
localAddress: ipAddress,
|
|
195
|
-
reuseAddress: true,
|
|
196
|
-
}, () => {
|
|
197
|
-
// When we establish a connection, send the IP address to the server
|
|
198
|
-
const message: SocketMessage = {
|
|
199
|
-
_ip: ipAddress,
|
|
200
|
-
_name: config.name,
|
|
201
|
-
};
|
|
202
|
-
const json = JSON.stringify(message);
|
|
203
|
-
console.log('[Sleeper] Send IP address: ', ipAddress, config.name);
|
|
204
|
-
try {
|
|
205
|
-
connection.current?.write(json + '\n', "utf8", (error) => {
|
|
206
|
-
if (error) {
|
|
207
|
-
return stopSocket();
|
|
208
|
-
}
|
|
209
|
-
console.log('[Sleeper] Connected to the Sleeper App.');
|
|
210
|
-
_onConnected(true);
|
|
211
|
-
});
|
|
212
|
-
} catch (e) {
|
|
213
|
-
return stopSocket();
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
connection.current.on('data', (data, ...args) => {
|
|
218
|
-
const handler = proxyHandler(connection.current);
|
|
219
|
-
const onSocketHandler = onSocket(handler);
|
|
220
|
-
onSocketHandler(data);
|
|
221
|
-
});
|
|
222
|
-
connection.current.on('error', err => {
|
|
223
|
-
return stopSocket();
|
|
224
|
-
});
|
|
225
|
-
connection.current.on('close', (hadError) => {
|
|
226
|
-
return stopSocket();
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const stopSocket = (retry = true) => {
|
|
231
|
-
_onConnected(false);
|
|
232
|
-
|
|
233
|
-
if (connection.current) {
|
|
234
|
-
connection.current.destroy();
|
|
235
|
-
connection.current = undefined;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Any time the socket is closed, attempt to connect again.
|
|
239
|
-
if (retry) {
|
|
240
|
-
clearTimeout(_retryTimer.current);
|
|
241
|
-
_retryTimer.current = setTimeout(() => {
|
|
242
|
-
console.log('[Sleeper] Unable to connect to sleeper, retrying...');
|
|
243
|
-
startSocket();
|
|
244
|
-
}, RETRY_TIMER);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const _waitForInitialization = () => {
|
|
249
|
-
return new Promise<void>((resolve) => {
|
|
250
|
-
(function checkData() {
|
|
251
|
-
let isInitialized = !!_dataRef.current?.dist;
|
|
252
|
-
|
|
253
|
-
// Non-staging builds also require a version map to be defined.
|
|
254
|
-
if (!_dataRef.current?.isStaging) {
|
|
255
|
-
isInitialized = !!_versionMap.current;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (isInitialized) return resolve();
|
|
259
|
-
setTimeout(checkData, 1000);
|
|
260
|
-
})();
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
const _resolveRemoteChunk: ScriptLocatorResolver = async (scriptId: string, caller: string) => {
|
|
265
|
-
await _waitForInitialization();
|
|
266
|
-
|
|
267
|
-
const bundleName = !caller ? `${scriptId}.container.bundle` : `${scriptId}.chunk.bundle`;
|
|
268
|
-
|
|
269
|
-
// Try to resolve URL based on scriptId and caller
|
|
270
|
-
const url = getMainUrl(scriptId, caller, {
|
|
271
|
-
platform: _dataRef.current?.platform,
|
|
272
|
-
bundleVersion: _versionMap.current?.[bundleName],
|
|
273
|
-
binaryVersion: _dataRef.current?.binaryVersion,
|
|
274
|
-
dist: _dataRef.current?.dist,
|
|
275
|
-
isStaging: _dataRef.current?.isStaging,
|
|
276
|
-
remoteIP: config.remoteIP,
|
|
277
|
-
dev: config.dev,
|
|
278
|
-
});
|
|
279
|
-
const query = config.dev ? {platform: Platform.OS} : undefined;
|
|
280
|
-
|
|
281
|
-
if (config.logsEnabled) console.log('[Sleeper] load script:', scriptId, caller, url);
|
|
282
|
-
return {url, query};
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const _fetchVersionMap = async (platform, binaryVersion, dist) => {
|
|
286
|
-
_versionMap.current = await fetchMainVersionMap(platform, binaryVersion, dist);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
useEffect(() => {
|
|
290
|
-
_dataRef.current = data;
|
|
291
|
-
if (!data.platform || !data.binaryVersion || !data.dist) return;
|
|
292
|
-
|
|
293
|
-
_fetchVersionMap(data.platform, data.binaryVersion, data.dist);
|
|
294
|
-
}, [data.platform, data.binaryVersion, data.dist, data.isStaging]);
|
|
295
|
-
|
|
296
|
-
useEffect(() => {
|
|
297
|
-
if (!config) {
|
|
298
|
-
console.error('[Sleeper] No config file specified. Please make sure you call DevServer.init() early in the app lifecycle.');
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
ScriptManager.shared.addResolver(_resolveRemoteChunk.bind(this));
|
|
303
|
-
startSocket();
|
|
304
|
-
|
|
305
|
-
return () => {
|
|
306
|
-
stopSocket(false);
|
|
307
|
-
};
|
|
308
|
-
}, []);
|
|
309
|
-
|
|
310
|
-
return <></>;
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
DevServer.init = (_config: Config) => {
|
|
314
|
-
config = _config;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
export default DevServer;
|