@sleeperhq/mini-core 1.0.0 → 1.0.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleeperhq/mini-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Core library frameworks for developing Sleeper Mini Apps.",
5
5
  "main": "index.ts",
6
6
  "types": "index.d.ts",
@@ -1,44 +1,12 @@
1
1
  import React, {useEffect} from 'react';
2
2
  import {Platform} from 'react-native';
3
+ import {Config} from '../types';
3
4
  import {ScriptManager, Federated} from '@callstack/repack/client';
4
5
  import NetInfo from '@react-native-community/netinfo';
5
6
  import dgram from 'react-native-udp';
6
- import {
7
- localSocketPort,
8
- remoteSocketPort,
9
- remoteBundlePort as _remoteBundlePort,
10
- release,
11
- remoteIP,
12
- } from '../../app.json';
13
7
  import axios from 'axios';
14
8
 
15
- const remoteBundleHost = release ? remoteIP : 'localhost';
16
- const remoteBundlePort = release ? _remoteBundlePort : 8081;
17
-
18
- ScriptManager.shared.addResolver(async (scriptId, caller) => {
19
- const extension =
20
- scriptId === 'sleeper' ? '.container.bundle' : '.chunk.bundle';
21
- const resolveURL = Federated.createURLResolver({
22
- containers: {
23
- sleeper: `http://${remoteBundleHost}:${remoteBundlePort}/[name]${extension}`,
24
- },
25
- });
26
-
27
- // Try to resolve URL based on scriptId and caller
28
- const url = resolveURL(scriptId, caller);
29
- const query = release ? undefined : {platform: Platform.OS};
30
-
31
- const response = await axios
32
- .get(url + '?' + new URLSearchParams(query), {method: 'HEAD'})
33
- .catch(() => ({
34
- status: 404,
35
- }));
36
-
37
- console.log('[Sleeper] load script:', scriptId, caller);
38
- if (response?.status === 200) {
39
- return {url, query};
40
- }
41
- });
9
+ let config: Config;
42
10
 
43
11
  const DevServer = props => {
44
12
  const onSocket = msg => {
@@ -63,7 +31,7 @@ const DevServer = props => {
63
31
  return;
64
32
  }
65
33
 
66
- socket.bind({port: localSocketPort, address: netInfoDetails.ipAddress});
34
+ socket.bind({port: config.localSocketPort, address: netInfoDetails.ipAddress});
67
35
  };
68
36
 
69
37
  const pingServer = socket => {
@@ -79,7 +47,7 @@ const DevServer = props => {
79
47
  const json = JSON.stringify({_ip: netInfoDetails.ipAddress});
80
48
 
81
49
  (async function ping() {
82
- socket.send(json, undefined, undefined, remoteSocketPort, remoteIP);
50
+ socket.send(json, undefined, undefined, config.remoteSocketPort, config.remoteIP);
83
51
  setTimeout(ping, 5000);
84
52
  })();
85
53
  });
@@ -87,6 +55,11 @@ const DevServer = props => {
87
55
  };
88
56
 
89
57
  useEffect(() => {
58
+ if (!config) {
59
+ console.error('[Sleeper] No config file specified. Please make sure you call DevServer.init() early in the app lifecycle.');
60
+ return;
61
+ }
62
+
90
63
  const socket = dgram.createSocket({type: 'udp4'});
91
64
  bindSocket(socket);
92
65
  pingServer(socket);
@@ -101,4 +74,36 @@ const DevServer = props => {
101
74
  return <></>;
102
75
  };
103
76
 
77
+ DevServer.init = (_config: Config) => {
78
+ config = _config;
79
+
80
+ const remoteBundleHost = config.release ? config.remoteIP : 'localhost';
81
+ const remoteBundlePort = config.release ? config.remoteBundlePort : 8081;
82
+
83
+ ScriptManager.shared.addResolver(async (scriptId, caller) => {
84
+ const extension =
85
+ scriptId === 'sleeper' ? '.container.bundle' : '.chunk.bundle';
86
+ const resolveURL = Federated.createURLResolver({
87
+ containers: {
88
+ sleeper: `http://${remoteBundleHost}:${remoteBundlePort}/[name]${extension}`,
89
+ },
90
+ });
91
+
92
+ // Try to resolve URL based on scriptId and caller
93
+ const url = resolveURL(scriptId, caller);
94
+ const query = config.release ? undefined : {platform: Platform.OS};
95
+
96
+ const response = await axios
97
+ .get(url + '?' + new URLSearchParams(query), {method: 'HEAD'})
98
+ .catch(() => ({
99
+ status: 404,
100
+ }));
101
+
102
+ console.log('[Sleeper] load script:', scriptId, caller);
103
+ if (response?.status === 200) {
104
+ return {url, query};
105
+ }
106
+ });
107
+ };
108
+
104
109
  export default DevServer;
@@ -15,6 +15,16 @@ export type Scalars = {
15
15
  SnowflakeSet: string[];
16
16
  };
17
17
 
18
+ export type Config = {
19
+ name: string,
20
+ displayName: string,
21
+ remoteIP: string,
22
+ localSocketPort: number,
23
+ remoteSocketPort: number,
24
+ remoteBundlePort: number,
25
+ release: boolean,
26
+ };
27
+
18
28
  export type User = {
19
29
  __typename?: 'User';
20
30
  async_bundles?: Maybe<Scalars['List']>;
package/app.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "name": "template",
3
- "displayName": "template",
4
- "remoteIP": "192.168.86.190",
5
- "localSocketPort": 8082,
6
- "remoteSocketPort": 9092,
7
- "remoteBundlePort": 9091,
8
- "release": true
9
- }