@ohos-ports/expo-network 57.0.2-beta.0

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +391 -0
  2. package/LICENSE +21 -0
  3. package/README.md +44 -0
  4. package/android/build.gradle +27 -0
  5. package/android/src/main/AndroidManifest.xml +4 -0
  6. package/android/src/main/java/expo/modules/network/NetworkExceptions.kt +9 -0
  7. package/android/src/main/java/expo/modules/network/NetworkModule.kt +281 -0
  8. package/android/src/main/java/expo/modules/network/NetworkUtils.kt +52 -0
  9. package/build/ExpoNetwork.d.ts +3 -0
  10. package/build/ExpoNetwork.d.ts.map +1 -0
  11. package/build/ExpoNetwork.js +3 -0
  12. package/build/ExpoNetwork.js.map +1 -0
  13. package/build/ExpoNetwork.web.d.ts +14 -0
  14. package/build/ExpoNetwork.web.d.ts.map +1 -0
  15. package/build/ExpoNetwork.web.js +47 -0
  16. package/build/ExpoNetwork.web.js.map +1 -0
  17. package/build/Network.d.ts +80 -0
  18. package/build/Network.d.ts.map +1 -0
  19. package/build/Network.js +111 -0
  20. package/build/Network.js.map +1 -0
  21. package/build/Network.types.d.ts +85 -0
  22. package/build/Network.types.d.ts.map +1 -0
  23. package/build/Network.types.js +55 -0
  24. package/build/Network.types.js.map +1 -0
  25. package/expo-module.config.json +15 -0
  26. package/ios/Exceptions.swift +7 -0
  27. package/ios/ExpoNetwork.podspec +28 -0
  28. package/ios/NetworkModule.swift +155 -0
  29. package/ios/NetworkType.swift +30 -0
  30. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2-sources.jar +0 -0
  31. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2-sources.jar.md5 +1 -0
  32. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2-sources.jar.sha1 +1 -0
  33. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2-sources.jar.sha256 +1 -0
  34. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2-sources.jar.sha512 +1 -0
  35. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.aar +0 -0
  36. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.aar.md5 +1 -0
  37. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.aar.sha1 +1 -0
  38. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.aar.sha256 +1 -0
  39. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.aar.sha512 +1 -0
  40. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.module +101 -0
  41. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.module.md5 +1 -0
  42. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.module.sha1 +1 -0
  43. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.module.sha256 +1 -0
  44. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.module.sha512 +1 -0
  45. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.pom +59 -0
  46. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.pom.md5 +1 -0
  47. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.pom.sha1 +1 -0
  48. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.pom.sha256 +1 -0
  49. package/local-maven-repo/host/exp/exponent/expo.modules.network/57.0.2/expo.modules.network-57.0.2.pom.sha512 +1 -0
  50. package/local-maven-repo/host/exp/exponent/expo.modules.network/maven-metadata.xml +13 -0
  51. package/local-maven-repo/host/exp/exponent/expo.modules.network/maven-metadata.xml.md5 +1 -0
  52. package/local-maven-repo/host/exp/exponent/expo.modules.network/maven-metadata.xml.sha1 +1 -0
  53. package/local-maven-repo/host/exp/exponent/expo.modules.network/maven-metadata.xml.sha256 +1 -0
  54. package/local-maven-repo/host/exp/exponent/expo.modules.network/maven-metadata.xml.sha512 +1 -0
  55. package/package.json +41 -0
  56. package/spm.config.json +37 -0
  57. package/src/ExpoNetwork.ts +3 -0
  58. package/src/ExpoNetwork.web.ts +52 -0
  59. package/src/Network.ts +123 -0
  60. package/src/Network.types.ts +90 -0
  61. package/tsconfig.json +10 -0
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@ohos-ports/expo-network",
3
+ "version": "57.0.2-beta.0",
4
+ "description": "Provides useful information about the device's network such as its IP address, MAC address, and airplane mode status",
5
+ "main": "build/Network.js",
6
+ "types": "build/Network.d.ts",
7
+ "keywords": [
8
+ "react-native",
9
+ "expo",
10
+ "expo-network"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
15
+ "directory": "ports/expo-network/57.0.2"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
19
+ },
20
+ "author": "650 Industries, Inc.",
21
+ "license": "MIT",
22
+ "homepage": "https://docs.expo.dev/versions/latest/sdk/network/",
23
+ "dependencies": {},
24
+ "devDependencies": {
25
+ "@types/react": "~19.2.0",
26
+ "@ohos-ports/expo": "^57.0.24-beta.0",
27
+ "expo-module-scripts": "56.0.3"
28
+ },
29
+ "peerDependencies": {
30
+ "@ohos-ports/expo": "^57.0.24-beta.0",
31
+ "react": "*"
32
+ },
33
+ "gitHead": "9e5319c0f821a27b7924841903abae50e2b41790",
34
+ "scripts": {
35
+ "build": "expo-module build",
36
+ "clean": "expo-module clean",
37
+ "lint": "expo-module lint",
38
+ "test": "expo-module test",
39
+ "expo-module": "expo-module"
40
+ }
41
+ }
@@ -0,0 +1,37 @@
1
+ {
2
+ "$schema": "../../tools/src/prebuilds/schemas/spm.config.schema.json",
3
+ "products": [
4
+ {
5
+ "name": "ExpoNetwork",
6
+ "podName": "ExpoNetwork",
7
+ "platforms": [
8
+ "iOS(\"16.4\")"
9
+ ],
10
+ "externalDependencies": [
11
+ "ReactNativeDependencies",
12
+ "React",
13
+ "Hermes",
14
+ "expo-modules-core/ExpoModulesCore"
15
+ ],
16
+ "targets": [
17
+ {
18
+ "type": "swift",
19
+ "name": "ExpoNetwork",
20
+ "path": "ios",
21
+ "pattern": "*.swift",
22
+ "dependencies": [
23
+ "Hermes",
24
+ "React",
25
+ "ReactNativeDependencies",
26
+ "expo-modules-core/ExpoModulesCore"
27
+ ],
28
+ "linkedFrameworks": [
29
+ "Foundation",
30
+ "SystemConfiguration",
31
+ "Network"
32
+ ]
33
+ }
34
+ ]
35
+ }
36
+ ]
37
+ }
@@ -0,0 +1,3 @@
1
+ import { requireNativeModule } from 'expo-modules-core';
2
+
3
+ export default requireNativeModule('ExpoNetwork');
@@ -0,0 +1,52 @@
1
+ import { NativeModule, registerWebModule } from 'expo-modules-core';
2
+
3
+ import type { NetworkEvents, NetworkState } from './Network.types';
4
+ import { NetworkStateType } from './Network.types';
5
+
6
+ const onNetworkStateEventName = 'onNetworkStateChanged';
7
+
8
+ function getNetworkState(): NetworkState {
9
+ const isOnline = typeof navigator !== 'undefined' && navigator.onLine;
10
+ return {
11
+ type: isOnline ? NetworkStateType.UNKNOWN : NetworkStateType.NONE,
12
+ isConnected: isOnline,
13
+ isInternetReachable: isOnline,
14
+ };
15
+ }
16
+
17
+ class ExpoNetworkModule extends NativeModule<NetworkEvents> {
18
+ eventListener?: () => void;
19
+ updateNetworkState() {
20
+ const state = getNetworkState();
21
+ this.emit(onNetworkStateEventName, state);
22
+ }
23
+
24
+ async getIpAddressAsync(): Promise<string> {
25
+ try {
26
+ const resp = await fetch('https://api.ipify.org?format=json');
27
+ const data = await resp.json();
28
+ return data.ip;
29
+ } catch (e) {
30
+ throw e;
31
+ }
32
+ }
33
+ async getNetworkStateAsync(): Promise<NetworkState> {
34
+ return getNetworkState();
35
+ }
36
+ async isAirplaneModeEnabledAsync(): Promise<boolean> {
37
+ return false;
38
+ }
39
+ startObserving() {
40
+ this.eventListener = () => this.updateNetworkState();
41
+ window.addEventListener('online', this.eventListener);
42
+ window.addEventListener('offline', this.eventListener);
43
+ }
44
+ stopObserving() {
45
+ if (this.eventListener) {
46
+ window.removeEventListener('online', this.eventListener);
47
+ window.removeEventListener('offline', this.eventListener);
48
+ }
49
+ }
50
+ }
51
+
52
+ export default registerWebModule(ExpoNetworkModule, 'ExpoNetwork');
package/src/Network.ts ADDED
@@ -0,0 +1,123 @@
1
+ import { type EventSubscription, UnavailabilityError } from 'expo-modules-core';
2
+ import { useEffect, useState } from 'react';
3
+
4
+ import ExpoNetwork from './ExpoNetwork';
5
+ import type { NetworkState, NetworkStateEvent } from './Network.types';
6
+
7
+ export { type NetworkState, type NetworkStateEvent, NetworkStateType } from './Network.types';
8
+
9
+ const onNetworkStateEventName = 'onNetworkStateChanged';
10
+
11
+ // @needsAudit
12
+ /**
13
+ * Gets the device's current network connection state.
14
+ *
15
+ * On web, `navigator.connection.type` is not available on browsers. So if there is an active
16
+ * network connection, the field `type` returns `NetworkStateType.UNKNOWN`. Otherwise, it returns
17
+ * `NetworkStateType.NONE`.
18
+ * @return A `Promise` that fulfils with a `NetworkState` object.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * await Network.getNetworkStateAsync();
23
+ * // {
24
+ * // type: NetworkStateType.CELLULAR,
25
+ * // isConnected: true,
26
+ * // isInternetReachable: true,
27
+ * // }
28
+ * ```
29
+ */
30
+ export async function getNetworkStateAsync(): Promise<NetworkState> {
31
+ if (!ExpoNetwork.getNetworkStateAsync) {
32
+ throw new UnavailabilityError('expo-network', 'getNetworkStateAsync');
33
+ }
34
+ return await ExpoNetwork.getNetworkStateAsync();
35
+ }
36
+
37
+ // @needsAudit
38
+ /**
39
+ * Gets the device's current IPv4 address. Returns `0.0.0.0` if the IP address could not be retrieved.
40
+ *
41
+ * On web, this method uses the third-party [`ipify service`](https://www.ipify.org/) to get the
42
+ * public IP address of the current device.
43
+ * @return A `Promise` that fulfils with a `string` of the current IP address of the device's main
44
+ * network interface. Can only be IPv4 address.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * await Network.getIpAddressAsync();
49
+ * // "92.168.32.44"
50
+ * ```
51
+ */
52
+ export async function getIpAddressAsync(): Promise<string> {
53
+ if (!ExpoNetwork.getIpAddressAsync) {
54
+ throw new UnavailabilityError('expo-network', 'getIpAddressAsync');
55
+ }
56
+ return await ExpoNetwork.getIpAddressAsync();
57
+ }
58
+
59
+ // @needsAudit
60
+ /**
61
+ * Tells if the device is in airplane mode.
62
+ * @return Returns a `Promise` that fulfils with a `boolean` value for whether the device is in
63
+ * airplane mode or not.
64
+ * @platform android
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * await Network.isAirplaneModeEnabledAsync();
69
+ * // false
70
+ * ```
71
+ */
72
+ export async function isAirplaneModeEnabledAsync(): Promise<boolean> {
73
+ if (!ExpoNetwork.isAirplaneModeEnabledAsync) {
74
+ throw new UnavailabilityError('expo-network', 'isAirplaneModeEnabledAsync');
75
+ }
76
+ return await ExpoNetwork.isAirplaneModeEnabledAsync();
77
+ }
78
+
79
+ /**
80
+ * Adds a listener that will fire whenever the network state changes.
81
+ *
82
+ * @param listener Callback to execute when the network state changes. The callback is provided with
83
+ * a single argument that is an object containing information about the network state.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const subscription = addNetworkStateListener(({ type, isConnected, isInternetReachable }) => {
88
+ * console.log(`Network type: ${type}, Connected: ${isConnected}, Internet Reachable: ${isInternetReachable}`);
89
+ * });
90
+ * ```
91
+ *
92
+ * @returns A subscription object with a remove function to unregister the listener.
93
+ */
94
+ export function addNetworkStateListener(
95
+ listener: (event: NetworkStateEvent) => void
96
+ ): EventSubscription {
97
+ return ExpoNetwork.addListener(onNetworkStateEventName, listener);
98
+ }
99
+
100
+ // @needsAudit
101
+ /**
102
+ * Returns the current network state of the device. This method
103
+ * initiates a listener for network state changes and cleans up before unmounting.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * const networkState = useNetworkState();
108
+ * console.log(`Current network type: ${networkState.type}`);
109
+ * ```
110
+ *
111
+ * @return The current network state of the device, including connectivity and type.
112
+ */
113
+ export function useNetworkState(): NetworkState {
114
+ const [networkState, setNetworkState] = useState<NetworkState>({});
115
+
116
+ useEffect(() => {
117
+ getNetworkStateAsync().then(setNetworkState);
118
+ const listener = addNetworkStateListener((networkState) => setNetworkState(networkState));
119
+ return () => listener.remove();
120
+ }, []);
121
+
122
+ return networkState;
123
+ }
@@ -0,0 +1,90 @@
1
+ // @needsAudit
2
+ export type NetworkState = {
3
+ /**
4
+ * A [`NetworkStateType`](#networkstatetype) enum value that represents the current network
5
+ * connection type.
6
+ */
7
+ type?: NetworkStateType;
8
+ /**
9
+ * If there is an active network connection. Note that this does not mean that internet is reachable.
10
+ * This field is `false` if the type is either `Network.NetworkStateType.NONE` or `Network.NetworkStateType.UNKNOWN`,
11
+ * `true` otherwise.
12
+ */
13
+ isConnected?: boolean;
14
+ /**
15
+ * If the internet is reachable with the currently active network connection. On Android, this
16
+ * requires the active network to have internet capability (`NET_CAPABILITY_INTERNET`), confirmed
17
+ * internet access (`NET_CAPABILITY_VALIDATED`), and a usable connection state. VPN connections
18
+ * also require non-zero downstream bandwidth. On iOS, this value will always be the same as
19
+ * `isConnected`.
20
+ */
21
+ isInternetReachable?: boolean;
22
+ };
23
+
24
+ // @needsAudit
25
+ /**
26
+ * An enum of the different types of devices supported by Expo.
27
+ */
28
+ export enum NetworkStateType {
29
+ /**
30
+ * No active network connection detected.
31
+ */
32
+ NONE = 'NONE',
33
+ /**
34
+ * The connection type could not be determined.
35
+ */
36
+ UNKNOWN = 'UNKNOWN',
37
+ /**
38
+ * Active network connection over mobile data or [`DUN-specific`](https://developer.android.com/reference/android/net/ConnectivityManager#TYPE_MOBILE_DUN)
39
+ * mobile connection when setting an upstream connection for tethering.
40
+ * @platform android
41
+ * @platform ios
42
+ */
43
+ CELLULAR = 'CELLULAR',
44
+ /**
45
+ * Active network connection over Wi-Fi.
46
+ * @platform android
47
+ * @platform ios
48
+ */
49
+ WIFI = 'WIFI',
50
+ /**
51
+ * Active network connection over Bluetooth.
52
+ * @platform android
53
+ */
54
+ BLUETOOTH = 'BLUETOOTH',
55
+ /**
56
+ * Active network connection over Ethernet.
57
+ * @platform android
58
+ * @platform ios
59
+ */
60
+ ETHERNET = 'ETHERNET',
61
+ /**
62
+ * Active network connection over WiMAX.
63
+ * @platform android
64
+ */
65
+ WIMAX = 'WIMAX',
66
+ /**
67
+ * Active network connection over VPN.
68
+ * @platform android
69
+ */
70
+ VPN = 'VPN',
71
+ /**
72
+ * Active network connection over other network connection types.
73
+ * @platform android
74
+ */
75
+ OTHER = 'OTHER',
76
+ }
77
+
78
+ // @needsAudit
79
+ /**
80
+ * Represents an event that provides the updated network state when there is a change in the network status.
81
+ * This is passed as the argument to listeners registered with [`addNetworkStateListener()`](#networkaddnetworkstatelistenerlistener).
82
+ */
83
+ export type NetworkStateEvent = NetworkState;
84
+
85
+ /**
86
+ * @hidden
87
+ */
88
+ export type NetworkEvents = {
89
+ onNetworkStateChanged(event: NetworkStateEvent): void;
90
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ // @generated by expo-module-scripts
2
+ {
3
+ "extends": "expo-module-scripts/tsconfig.base",
4
+ "compilerOptions": {
5
+ "rootDir": "./src",
6
+ "outDir": "./build"
7
+ },
8
+ "include": ["./src"],
9
+ "exclude": ["**/__mocks__/*", "**/__tests__/*", "**/__rsc_tests__/*"]
10
+ }