@libo11y/react-native 0.0.0-stage → 0.4.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.
- package/LICENSE +202 -0
- package/README.md +68 -2
- package/dist/contracts/env.d.ts +38 -0
- package/dist/contracts/env.js +5 -0
- package/dist/contracts/storage.d.ts +18 -0
- package/dist/contracts/storage.js +4 -0
- package/dist/device-class.d.ts +7 -0
- package/dist/device-class.js +22 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +39 -0
- package/dist/expo/app-info.d.ts +39 -0
- package/dist/expo/app-info.js +40 -0
- package/dist/expo/file-storage.d.ts +45 -0
- package/dist/expo/file-storage.js +93 -0
- package/dist/expo/options.d.ts +23 -0
- package/dist/expo/options.js +40 -0
- package/dist/expo.d.ts +38 -0
- package/dist/expo.js +57 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +24 -0
- package/dist/instrumentation/network.d.ts +34 -0
- package/dist/instrumentation/network.js +69 -0
- package/dist/internal.d.ts +20 -0
- package/dist/internal.js +18 -0
- package/dist/lifecycle.d.ts +3 -0
- package/dist/lifecycle.js +51 -0
- package/dist/options.d.ts +109 -0
- package/dist/options.js +1 -0
- package/dist/platform.d.ts +32 -0
- package/dist/platform.js +47 -0
- package/dist/random.d.ts +8 -0
- package/dist/random.js +17 -0
- package/dist/resource.d.ts +18 -0
- package/dist/resource.js +22 -0
- package/dist/setup.d.ts +52 -0
- package/dist/setup.js +201 -0
- package/dist/storage/memory.d.ts +7 -0
- package/dist/storage/memory.js +19 -0
- package/dist/storage/queue.d.ts +23 -0
- package/dist/storage/queue.js +241 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +73 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @libo11y/react-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The libo11y platform for React Native apps on Hermes. It sets up the
|
|
4
|
+
OpenTelemetry JS providers with a mobile resource, sends OTLP/HTTP JSON to
|
|
5
|
+
a collector or the RUM gateway, saves every batch on device storage
|
|
6
|
+
before it is sent, flushes when the app goes to the background, gates
|
|
7
|
+
everything on consent, and records fetch and XHR client spans.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @libo11y/react-native
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
It needs React Native 0.88 or later as a peer dependency.
|
|
14
|
+
|
|
15
|
+
## Expo
|
|
16
|
+
|
|
17
|
+
An Expo app imports `setupExpo` from `@libo11y/react-native/expo`:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { setupExpo } from "@libo11y/react-native/expo";
|
|
21
|
+
|
|
22
|
+
const telemetry = await setupExpo({
|
|
23
|
+
serviceName: "my-app",
|
|
24
|
+
environment: "production",
|
|
25
|
+
endpoint: "https://app.example.com/_o11y",
|
|
26
|
+
consent: { initial: "pending" },
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`setupExpo` is `setupReactNative` with these Expo options under the app's
|
|
31
|
+
own:
|
|
32
|
+
|
|
33
|
+
- `storage` is `expoFileStorage()`, one file per key in the `libo11y`
|
|
34
|
+
directory of the app's documents (`expo-file-system`). The directory is
|
|
35
|
+
created on the first write, not before. Each value goes to a temporary
|
|
36
|
+
file that then replaces the key's file, so an app killed mid-write keeps
|
|
37
|
+
the old value.
|
|
38
|
+
- `serviceVersion` is the native app version (`expo-application`).
|
|
39
|
+
- `o11y.rum.app.update_id` is the ID of the over-the-air update the app
|
|
40
|
+
runs (`expo-updates`). It is absent for the bundle embedded in the
|
|
41
|
+
build, in development and when updates are disabled. The RUM gateway
|
|
42
|
+
uses it to find the source maps of that update.
|
|
43
|
+
- `o11y.rum.device.class` comes from `expo-device` when it knows the
|
|
44
|
+
device type.
|
|
45
|
+
|
|
46
|
+
An option the app sets wins, and `resourceAttributes` merge key by key.
|
|
47
|
+
An attribute the app sets in `env` (`OTEL_RESOURCE_ATTRIBUTES`) also beats
|
|
48
|
+
Expo's, `service.version` included.
|
|
49
|
+
`expoOptions()`, `expoAppInfo()` and `expoFileStorage()` are exported for
|
|
50
|
+
apps that call `setupReactNative` themselves.
|
|
51
|
+
|
|
52
|
+
The entry imports `expo-application`, `expo-device`, `expo-file-system`
|
|
53
|
+
and `expo-updates`. They are optional peer dependencies: an app that
|
|
54
|
+
imports `./expo` installs them, at Expo SDK 58 or later. The main entry
|
|
55
|
+
imports none of them. Tested on Expo SDK 58 only.
|
|
56
|
+
|
|
57
|
+
## Status
|
|
58
|
+
|
|
59
|
+
A preview. The package first ships in `ts/v0.4.0`, and its API can change
|
|
60
|
+
in a minor release until the RUM package for React Native,
|
|
61
|
+
`@libo11y/rum-react-native`, ships on top of it. It is not on npm yet.
|
|
62
|
+
Until its first npm publish, install it from the tarball attached to the
|
|
63
|
+
[`ts/v0.4.0` GitHub Release](https://github.com/jamesainslie/libo11y/releases/tag/ts/v0.4.0),
|
|
64
|
+
whose notes list the exact URL:
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
npm install \
|
|
68
|
+
https://github.com/jamesainslie/libo11y/releases/download/ts/v0.4.0/libo11y-react-native-0.4.0.tgz
|
|
69
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract. What React Native telemetry reads from React Native. Internal: env.ts reads the real
|
|
3
|
+
* modules, and tests pass a fake, so no module a test imports loads react-native.
|
|
4
|
+
*/
|
|
5
|
+
/** The part of React Native's AppState the telemetry uses. */
|
|
6
|
+
export interface AppStateLike {
|
|
7
|
+
readonly currentState: string | null | undefined;
|
|
8
|
+
addEventListener(type: "change", listener: (state: string) => void): {
|
|
9
|
+
remove(): void;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** What React Native telemetry reads from React Native. */
|
|
13
|
+
export interface ReactNativeEnv {
|
|
14
|
+
readonly appState: AppStateLike;
|
|
15
|
+
/** Platform.OS, such as "ios" or "android". */
|
|
16
|
+
readonly os: string;
|
|
17
|
+
/** Platform.isPad (iOS). */
|
|
18
|
+
readonly isPad: boolean;
|
|
19
|
+
/** Platform.isTV. */
|
|
20
|
+
readonly isTV: boolean;
|
|
21
|
+
/** Dimensions.get("screen") in points (dp), or undefined when it is not known. */
|
|
22
|
+
readonly screen: () => {
|
|
23
|
+
readonly width: number;
|
|
24
|
+
readonly height: number;
|
|
25
|
+
} | undefined;
|
|
26
|
+
/** __DEV__. */
|
|
27
|
+
readonly dev: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** When the app is in the foreground. */
|
|
30
|
+
export interface AppLifecycle {
|
|
31
|
+
/** False while the app is in the background, or inactive on iOS. */
|
|
32
|
+
visible(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Calls listener once per change between visible and hidden, in registration order. Returns the
|
|
35
|
+
* function that unsubscribes.
|
|
36
|
+
*/
|
|
37
|
+
subscribe(listener: (visible: boolean) => void): () => void;
|
|
38
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract. Where the persistent retry queue, and later the RUM session, live on the device.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* A key-value storage of strings, with the AsyncStorage shape, so
|
|
6
|
+
* `@react-native-async-storage/async-storage` satisfies it as it is. Every method may reject;
|
|
7
|
+
* callers treat a rejection as a storage failure and carry on.
|
|
8
|
+
*/
|
|
9
|
+
export interface KeyValueStorage {
|
|
10
|
+
/** The value stored under key, or null when there is none. */
|
|
11
|
+
getItem(key: string): Promise<string | null>;
|
|
12
|
+
/** Stores value under key. Resolves once the value is saved. */
|
|
13
|
+
setItem(key: string, value: string): Promise<void>;
|
|
14
|
+
/** Removes the value under key, if any. */
|
|
15
|
+
removeItem(key: string): Promise<void>;
|
|
16
|
+
/** Every key stored, the queue's and any other's. */
|
|
17
|
+
getAllKeys(): Promise<readonly string[]>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** The o11y.rum.device.class of the device a React Native app runs on. */
|
|
2
|
+
import type { ReactNativeEnv } from "./contracts/env.ts";
|
|
3
|
+
/**
|
|
4
|
+
* "tv" on a TV, "tablet" on an iPad or an Android screen whose shorter side is 600 dp or more, else
|
|
5
|
+
* "mobile", also when the screen size is unknown.
|
|
6
|
+
*/
|
|
7
|
+
export declare function deviceClassOf(env: Pick<ReactNativeEnv, "os" | "isPad" | "isTV" | "screen">): "mobile" | "tablet" | "tv";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** An Android screen whose shorter side is at least this many dp is a tablet (Android's sw600dp). */
|
|
2
|
+
const TABLET_MIN_DP = 600;
|
|
3
|
+
/**
|
|
4
|
+
* "tv" on a TV, "tablet" on an iPad or an Android screen whose shorter side is 600 dp or more, else
|
|
5
|
+
* "mobile", also when the screen size is unknown.
|
|
6
|
+
*/
|
|
7
|
+
export function deviceClassOf(env) {
|
|
8
|
+
if (env.isTV)
|
|
9
|
+
return "tv";
|
|
10
|
+
if (env.os === "ios")
|
|
11
|
+
return env.isPad ? "tablet" : "mobile";
|
|
12
|
+
let shorter = Number.NaN;
|
|
13
|
+
try {
|
|
14
|
+
const s = env.screen();
|
|
15
|
+
if (s !== undefined)
|
|
16
|
+
shorter = Math.min(s.width, s.height);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// An unknown screen is a phone.
|
|
20
|
+
}
|
|
21
|
+
return shorter >= TABLET_MIN_DP ? "tablet" : "mobile";
|
|
22
|
+
}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ReactNativeEnv } from "./contracts/env.ts";
|
|
2
|
+
import type { ReactNativeDeps } from "./setup.ts";
|
|
3
|
+
/** React Native's AppState, Platform and Dimensions, read when called. */
|
|
4
|
+
export declare function defaultReactNativeEnv(): ReactNativeEnv;
|
|
5
|
+
/** The real dependencies of setupReactNativeWith over env. */
|
|
6
|
+
export declare function defaultReactNativeDeps(env?: ReactNativeEnv): ReactNativeDeps;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @libo11y/react-native/env: the real React Native dependencies, for @libo11y/rum-react-native's
|
|
3
|
+
* binding, which wraps them. For libo11y packages only: it follows the package version, carries no
|
|
4
|
+
* compatibility promise and has no API report. The only module of this package that imports a
|
|
5
|
+
* value from react-native, so node tests never load it (boundary.test.ts enforces that).
|
|
6
|
+
*/
|
|
7
|
+
import { createConsentController, createDelivery, createExporterSet, createHttpTransport, } from "@libo11y/web/portable";
|
|
8
|
+
import { AppState, Dimensions, Platform } from "react-native";
|
|
9
|
+
import { installNetworkInstrumentation } from "./instrumentation/network.js";
|
|
10
|
+
import { appStateLifecycle } from "./lifecycle.js";
|
|
11
|
+
import { openStorageQueue } from "./storage/queue.js";
|
|
12
|
+
/** React Native's AppState, Platform and Dimensions, read when called. */
|
|
13
|
+
export function defaultReactNativeEnv() {
|
|
14
|
+
const p = Platform;
|
|
15
|
+
return {
|
|
16
|
+
appState: AppState,
|
|
17
|
+
os: p.OS,
|
|
18
|
+
isPad: p.isPad === true,
|
|
19
|
+
isTV: p.isTV === true,
|
|
20
|
+
screen: () => {
|
|
21
|
+
const s = Dimensions.get("screen");
|
|
22
|
+
return { width: s.width, height: s.height };
|
|
23
|
+
},
|
|
24
|
+
dev: typeof __DEV__ === "boolean" ? __DEV__ : false,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** The real dependencies of setupReactNativeWith over env. */
|
|
28
|
+
export function defaultReactNativeDeps(env = defaultReactNativeEnv()) {
|
|
29
|
+
return {
|
|
30
|
+
env,
|
|
31
|
+
lifecycle: appStateLifecycle(env.appState),
|
|
32
|
+
createConsentController,
|
|
33
|
+
openQueue: openStorageQueue,
|
|
34
|
+
createHttpTransport,
|
|
35
|
+
createDelivery,
|
|
36
|
+
createExporterSet,
|
|
37
|
+
installNetworkInstrumentation,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an Expo app knows about itself: the native app version (expo-application), the over-the-air
|
|
3
|
+
* update it runs (expo-updates) and the kind of device (expo-device). expo.ts passes the real
|
|
4
|
+
* modules; tests pass plain objects, so this module imports nothing from Expo.
|
|
5
|
+
*/
|
|
6
|
+
/** The parts of expo-application, expo-updates and expo-device the telemetry reads. */
|
|
7
|
+
export interface ExpoModulesLike {
|
|
8
|
+
readonly application: {
|
|
9
|
+
readonly nativeApplicationVersion: string | null;
|
|
10
|
+
};
|
|
11
|
+
readonly updates: {
|
|
12
|
+
readonly isEnabled: boolean;
|
|
13
|
+
readonly isEmbeddedLaunch: boolean;
|
|
14
|
+
readonly updateId: string | null;
|
|
15
|
+
};
|
|
16
|
+
/** expo-device's deviceType: 0 unknown, 1 phone, 2 tablet, 3 desktop, 4 TV. */
|
|
17
|
+
readonly device: {
|
|
18
|
+
readonly deviceType: number | null;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* What the telemetry takes from Expo.
|
|
23
|
+
*/
|
|
24
|
+
export interface ExpoAppInfo {
|
|
25
|
+
/** expo-application's nativeApplicationVersion, such as "1.0.0". */
|
|
26
|
+
readonly serviceVersion?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The ID of the over-the-air update the app runs, from expo-updates. Absent for the bundle
|
|
29
|
+
* embedded in the build, in development and when updates are disabled.
|
|
30
|
+
*/
|
|
31
|
+
readonly updateId?: string;
|
|
32
|
+
/** The device class expo-device reports. Absent when it does not know. */
|
|
33
|
+
readonly deviceClass?: "mobile" | "tablet" | "desktop" | "tv";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The app version, the update ID only for an over-the-air launch (updates enabled, not the
|
|
37
|
+
* embedded bundle, a non-empty ID) and the device class. A field a module cannot give is left out.
|
|
38
|
+
*/
|
|
39
|
+
export declare function expoAppInfoFrom(m: ExpoModulesLike): ExpoAppInfo;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an Expo app knows about itself: the native app version (expo-application), the over-the-air
|
|
3
|
+
* update it runs (expo-updates) and the kind of device (expo-device). expo.ts passes the real
|
|
4
|
+
* modules; tests pass plain objects, so this module imports nothing from Expo.
|
|
5
|
+
*/
|
|
6
|
+
const DEVICE_CLASSES = new Map([
|
|
7
|
+
[1, "mobile"],
|
|
8
|
+
[2, "tablet"],
|
|
9
|
+
[3, "desktop"],
|
|
10
|
+
[4, "tv"],
|
|
11
|
+
]);
|
|
12
|
+
/** read(), or undefined when it throws, as a native module that is not linked does. */
|
|
13
|
+
function safely(read) {
|
|
14
|
+
try {
|
|
15
|
+
return read();
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The app version, the update ID only for an over-the-air launch (updates enabled, not the
|
|
23
|
+
* embedded bundle, a non-empty ID) and the device class. A field a module cannot give is left out.
|
|
24
|
+
*/
|
|
25
|
+
export function expoAppInfoFrom(m) {
|
|
26
|
+
const version = safely(() => m.application.nativeApplicationVersion);
|
|
27
|
+
const updateId = safely(() => {
|
|
28
|
+
const u = m.updates;
|
|
29
|
+
return u.isEnabled && !u.isEmbeddedLaunch && typeof u.updateId === "string" && u.updateId !== ""
|
|
30
|
+
? u.updateId
|
|
31
|
+
: undefined;
|
|
32
|
+
});
|
|
33
|
+
const deviceType = safely(() => m.device.deviceType);
|
|
34
|
+
const deviceClass = typeof deviceType === "number" ? DEVICE_CLASSES.get(deviceType) : undefined;
|
|
35
|
+
return {
|
|
36
|
+
...(typeof version === "string" && version !== "" ? { serviceVersion: version } : {}),
|
|
37
|
+
...(updateId === undefined ? {} : { updateId }),
|
|
38
|
+
...(deviceClass === undefined ? {} : { deviceClass }),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A KeyValueStorage with one file per key in one directory, over the slice of expo-file-system's
|
|
3
|
+
* File and Directory API (SDK 54 and later) it uses. expo.ts passes the real module; tests pass a
|
|
4
|
+
* fake, so this module imports nothing from expo-file-system.
|
|
5
|
+
*/
|
|
6
|
+
import type { KeyValueStorage } from "../contracts/storage.ts";
|
|
7
|
+
/** The part of expo-file-system's File the storage uses. */
|
|
8
|
+
export interface FileLike {
|
|
9
|
+
/** Whether the file exists. */
|
|
10
|
+
readonly exists: boolean;
|
|
11
|
+
/** The file's text. */
|
|
12
|
+
text(): Promise<string>;
|
|
13
|
+
/** Replaces the file's content, creating the file when it does not exist. */
|
|
14
|
+
write(content: string): void | Promise<void>;
|
|
15
|
+
/** Deletes the file. */
|
|
16
|
+
delete(): void;
|
|
17
|
+
}
|
|
18
|
+
/** The storage's directory, and its files by name. */
|
|
19
|
+
export interface FileSystemLike {
|
|
20
|
+
/** Whether the directory exists. */
|
|
21
|
+
directoryExists(): boolean;
|
|
22
|
+
/** Creates the directory and any missing parents, succeeding when it exists already. */
|
|
23
|
+
createDirectory(): void;
|
|
24
|
+
/** The names of the files directly in the directory, without subdirectories. */
|
|
25
|
+
fileNames(): readonly string[];
|
|
26
|
+
/** The file called name in the directory, which need not exist. */
|
|
27
|
+
file(name: string): FileLike;
|
|
28
|
+
/**
|
|
29
|
+
* Moves the file called from over the file called to. expo-file-system deletes the target first,
|
|
30
|
+
* then moves the file in, so a crash between the two leaves from in place and to missing.
|
|
31
|
+
*/
|
|
32
|
+
replace(from: string, to: string): void | Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A KeyValueStorage with one file per key in fs's directory. Reads, removes and getAllKeys create
|
|
36
|
+
* nothing; the directory is created on the first setItem, so nothing is written to the device
|
|
37
|
+
* before the first write, which the delivery makes only once consent is granted. getAllKeys lists
|
|
38
|
+
* only the keys of files this storage wrote, never subdirectories or other files.
|
|
39
|
+
*
|
|
40
|
+
* A value is written whole to a temporary file that then replaces the key's file, because neither
|
|
41
|
+
* platform's write is atomic: an app killed during a write keeps the old value. Killed between the
|
|
42
|
+
* replace's delete and move, the key reads the new value from the temporary file. The operations
|
|
43
|
+
* on one key run one at a time, in call order.
|
|
44
|
+
*/
|
|
45
|
+
export declare function expoFileStorageWith(fs: FileSystemLike): KeyValueStorage;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The suffix of the file a value is written to before it replaces the key's file. "." never
|
|
3
|
+
* appears in a key's file name, so a temporary file is never taken for a key.
|
|
4
|
+
*/
|
|
5
|
+
const TEMPORARY = ".tmp";
|
|
6
|
+
/** Characters a file name keeps as they are: lowercase, so no two names differ only in case. */
|
|
7
|
+
const PLAIN = /^[a-z0-9_-]$/;
|
|
8
|
+
/**
|
|
9
|
+
* The file name of key: every character other than a lowercase letter, a digit, "_" and "-" is
|
|
10
|
+
* percent-encoded as UTF-8 with lowercase hex. So no name is "." or "..", none holds a path
|
|
11
|
+
* separator, and keys that differ only in case get different names on a case-insensitive file
|
|
12
|
+
* system, such as iOS's.
|
|
13
|
+
*/
|
|
14
|
+
function fileNameOf(key) {
|
|
15
|
+
let name = "";
|
|
16
|
+
for (const ch of key) {
|
|
17
|
+
if (PLAIN.test(ch)) {
|
|
18
|
+
name += ch;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
for (const byte of new TextEncoder().encode(ch))
|
|
22
|
+
name += `%${byte.toString(16).padStart(2, "0")}`;
|
|
23
|
+
}
|
|
24
|
+
return name;
|
|
25
|
+
}
|
|
26
|
+
/** The key whose file name is name, or undefined for a file this storage did not write. */
|
|
27
|
+
function keyOf(name) {
|
|
28
|
+
let key;
|
|
29
|
+
try {
|
|
30
|
+
key = decodeURIComponent(name);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
return fileNameOf(key) === name ? key : undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A KeyValueStorage with one file per key in fs's directory. Reads, removes and getAllKeys create
|
|
39
|
+
* nothing; the directory is created on the first setItem, so nothing is written to the device
|
|
40
|
+
* before the first write, which the delivery makes only once consent is granted. getAllKeys lists
|
|
41
|
+
* only the keys of files this storage wrote, never subdirectories or other files.
|
|
42
|
+
*
|
|
43
|
+
* A value is written whole to a temporary file that then replaces the key's file, because neither
|
|
44
|
+
* platform's write is atomic: an app killed during a write keeps the old value. Killed between the
|
|
45
|
+
* replace's delete and move, the key reads the new value from the temporary file. The operations
|
|
46
|
+
* on one key run one at a time, in call order.
|
|
47
|
+
*/
|
|
48
|
+
export function expoFileStorageWith(fs) {
|
|
49
|
+
const pending = new Map();
|
|
50
|
+
const inOrder = (key, op) => {
|
|
51
|
+
const result = (pending.get(key) ?? Promise.resolve()).then(op, op);
|
|
52
|
+
const settled = result.then(() => { }, () => { });
|
|
53
|
+
pending.set(key, settled);
|
|
54
|
+
void settled.then(() => {
|
|
55
|
+
if (pending.get(key) === settled)
|
|
56
|
+
pending.delete(key);
|
|
57
|
+
});
|
|
58
|
+
return result;
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
getItem: (key) => inOrder(key, async () => {
|
|
62
|
+
const name = fileNameOf(key);
|
|
63
|
+
for (const f of [fs.file(name), fs.file(name + TEMPORARY)])
|
|
64
|
+
if (f.exists)
|
|
65
|
+
return await f.text();
|
|
66
|
+
return null;
|
|
67
|
+
}),
|
|
68
|
+
setItem: (key, value) => inOrder(key, async () => {
|
|
69
|
+
if (!fs.directoryExists())
|
|
70
|
+
fs.createDirectory();
|
|
71
|
+
const name = fileNameOf(key);
|
|
72
|
+
await fs.file(name + TEMPORARY).write(value);
|
|
73
|
+
await fs.replace(name + TEMPORARY, name);
|
|
74
|
+
}),
|
|
75
|
+
removeItem: (key) => inOrder(key, async () => {
|
|
76
|
+
const name = fileNameOf(key);
|
|
77
|
+
for (const f of [fs.file(name), fs.file(name + TEMPORARY)])
|
|
78
|
+
if (f.exists)
|
|
79
|
+
f.delete();
|
|
80
|
+
}),
|
|
81
|
+
async getAllKeys() {
|
|
82
|
+
if (!fs.directoryExists())
|
|
83
|
+
return [];
|
|
84
|
+
const keys = new Set();
|
|
85
|
+
for (const file of fs.fileNames()) {
|
|
86
|
+
const key = keyOf(file.endsWith(TEMPORARY) ? file.slice(0, -TEMPORARY.length) : file);
|
|
87
|
+
if (key !== undefined)
|
|
88
|
+
keys.add(key);
|
|
89
|
+
}
|
|
90
|
+
return [...keys];
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|