@bkbdev/core 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +416 -2
- package/android/build.gradle +38 -0
- package/android/src/debug/java/dev/bkb/network/BKBNetworkCapture.java +13 -0
- package/android/src/debug/java/dev/bkb/network/BKBNetworkInterceptor.java +85 -0
- package/android/src/debug/java/dev/bkb/network/BKBNetworkModule.java +72 -0
- package/android/src/debugOptimized/java/dev/bkb/network/BKBNetworkCapture.java +13 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/dev/bkb/network/BKBNetworkPackage.java +22 -0
- package/android/src/release/java/dev/bkb/network/BKBNetworkCapture.java +13 -0
- package/assets/package.json +6 -0
- package/auto/package.json +6 -0
- package/babel.cjs +181 -0
- package/dist/assets.cjs +614 -0
- package/dist/assets.cjs.map +7 -0
- package/dist/assets.js +594 -0
- package/dist/assets.js.map +7 -0
- package/dist/auto.cjs +1534 -0
- package/dist/auto.cjs.map +7 -0
- package/dist/auto.js +1516 -0
- package/dist/auto.js.map +7 -0
- package/dist/images-automatic.cjs +361 -0
- package/dist/images-automatic.cjs.map +7 -0
- package/dist/images-automatic.js +343 -0
- package/dist/images-automatic.js.map +7 -0
- package/dist/images-react.cjs +313 -0
- package/dist/images-react.cjs.map +7 -0
- package/dist/images-react.js +295 -0
- package/dist/images-react.js.map +7 -0
- package/dist/images.cjs +939 -0
- package/dist/images.cjs.map +7 -0
- package/dist/images.js +919 -0
- package/dist/images.js.map +7 -0
- package/dist/index.cjs +1401 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +1381 -0
- package/dist/index.js.map +7 -0
- package/dist/inspector.cjs +437 -0
- package/dist/inspector.cjs.map +7 -0
- package/dist/inspector.js +417 -0
- package/dist/inspector.js.map +7 -0
- package/dist/network.cjs +906 -0
- package/dist/network.cjs.map +7 -0
- package/dist/network.js +886 -0
- package/dist/network.js.map +7 -0
- package/dist/plugin.cjs +192 -0
- package/dist/plugin.cjs.map +7 -0
- package/dist/plugin.js +169 -0
- package/dist/plugin.js.map +7 -0
- package/dist/preview.cjs +143 -0
- package/dist/preview.cjs.map +7 -0
- package/dist/preview.js +120 -0
- package/dist/preview.js.map +7 -0
- package/dist/protocol/src/asset-registration.d.ts +14 -0
- package/dist/protocol/src/bkb-config.d.ts +29 -0
- package/dist/protocol/src/callsite.d.ts +8 -0
- package/dist/protocol/src/diagnostics/storage-provider.d.ts +27 -0
- package/dist/protocol/src/image-provider.d.ts +70 -0
- package/dist/protocol/src/image-source.d.ts +1 -0
- package/dist/protocol/src/inspector-provider.d.ts +44 -0
- package/dist/protocol/src/log-values.d.ts +10 -0
- package/dist/protocol/src/metro-client-log.d.ts +2 -0
- package/dist/protocol/src/network-provider.d.ts +38 -0
- package/dist/protocol/src/plugin-channels.d.ts +22 -0
- package/dist/protocol/src/renders-provider.d.ts +88 -0
- package/dist/protocol/src/sensitive-key.d.ts +1 -0
- package/dist/protocol/src/storage-privacy.d.ts +2 -0
- package/dist/protocol/src/storage-provider.d.ts +66 -0
- package/dist/protocol/src/value-preview.d.ts +9 -0
- package/dist/protocol/src/wire.d.ts +47 -0
- package/dist/register.cjs +3413 -0
- package/dist/register.cjs.map +7 -0
- package/dist/register.js +3397 -0
- package/dist/register.js.map +7 -0
- package/dist/renders-react.cjs +162 -0
- package/dist/renders-react.cjs.map +7 -0
- package/dist/renders-react.js +144 -0
- package/dist/renders-react.js.map +7 -0
- package/dist/renders.cjs +991 -0
- package/dist/renders.cjs.map +7 -0
- package/dist/renders.js +971 -0
- package/dist/renders.js.map +7 -0
- package/dist/sdk/src/asset-registration.d.ts +2 -0
- package/dist/sdk/src/assets.d.ts +9 -0
- package/dist/sdk/src/auto.d.ts +44 -0
- package/dist/sdk/src/body-queue.d.ts +11 -0
- package/dist/sdk/src/bundle-origin.d.ts +1 -0
- package/dist/sdk/src/callsite.d.ts +1 -0
- package/dist/sdk/src/channel.d.ts +5 -0
- package/dist/sdk/src/configuration.d.ts +7 -0
- package/dist/sdk/src/console-capture.d.ts +10 -0
- package/dist/sdk/src/console-values.d.ts +9 -0
- package/dist/sdk/src/diagnostic-channels.d.ts +8 -0
- package/dist/sdk/src/environment.d.ts +2 -0
- package/dist/sdk/src/event-hub.d.ts +6 -0
- package/dist/sdk/src/images/automatic.d.ts +14 -0
- package/dist/sdk/src/images/callbacks.d.ts +3 -0
- package/dist/sdk/src/images/client.d.ts +16 -0
- package/dist/sdk/src/images/commands.d.ts +4 -0
- package/dist/sdk/src/images/index.d.ts +6 -0
- package/dist/sdk/src/images/observer.d.ts +36 -0
- package/dist/sdk/src/images/privacy.d.ts +3 -0
- package/dist/sdk/src/images/react.d.ts +12 -0
- package/dist/sdk/src/index.d.ts +6 -0
- package/dist/sdk/src/inspector-channel.d.ts +15 -0
- package/dist/sdk/src/inspector-error.d.ts +4 -0
- package/dist/sdk/src/inspector-native.d.ts +8 -0
- package/dist/sdk/src/inspector-orientation.d.ts +2 -0
- package/dist/sdk/src/inspector-renderer.d.ts +1 -0
- package/dist/sdk/src/inspector-values.d.ts +4 -0
- package/dist/sdk/src/logger-internals.d.ts +12 -0
- package/dist/sdk/src/native-binding.d.ts +2 -0
- package/dist/sdk/src/network/android.d.ts +18 -0
- package/dist/sdk/src/network/dedupe.d.ts +6 -0
- package/dist/sdk/src/network/fetch.d.ts +2 -0
- package/dist/sdk/src/network/full-bodies.d.ts +13 -0
- package/dist/sdk/src/network/index.d.ts +22 -0
- package/dist/sdk/src/network/metadata.d.ts +9 -0
- package/dist/sdk/src/network/patch.d.ts +3 -0
- package/dist/sdk/src/network/privacy.d.ts +7 -0
- package/dist/sdk/src/network/property.d.ts +2 -0
- package/dist/sdk/src/network/reporter.d.ts +21 -0
- package/dist/sdk/src/network/response.d.ts +6 -0
- package/dist/sdk/src/network/session.d.ts +24 -0
- package/dist/sdk/src/network/xhr.d.ts +2 -0
- package/dist/sdk/src/plugin-control-channel.d.ts +15 -0
- package/dist/sdk/src/plugin.d.ts +15 -0
- package/dist/sdk/src/preview.d.ts +3 -0
- package/dist/sdk/src/register.d.ts +34 -0
- package/dist/sdk/src/renders/access.d.ts +3 -0
- package/dist/sdk/src/renders/backend.d.ts +9 -0
- package/dist/sdk/src/renders/geometry.d.ts +10 -0
- package/dist/sdk/src/renders/hook.d.ts +7 -0
- package/dist/sdk/src/renders/hosts.d.ts +4 -0
- package/dist/sdk/src/renders/index.d.ts +8 -0
- package/dist/sdk/src/renders/native-events.d.ts +19 -0
- package/dist/sdk/src/renders/observer.d.ts +43 -0
- package/dist/sdk/src/renders/overlay.d.ts +50 -0
- package/dist/sdk/src/renders/react.d.ts +32 -0
- package/dist/sdk/src/renders/snapshot.d.ts +15 -0
- package/dist/sdk/src/renders/values.d.ts +14 -0
- package/dist/sdk/src/renders-channel.d.ts +18 -0
- package/dist/sdk/src/runtime-reload.d.ts +7 -0
- package/dist/sdk/src/serialize.d.ts +2 -0
- package/dist/sdk/src/storage/async-storage.d.ts +11 -0
- package/dist/sdk/src/storage/commands.d.ts +4 -0
- package/dist/sdk/src/storage/index.d.ts +10 -0
- package/dist/sdk/src/storage/listing.d.ts +11 -0
- package/dist/sdk/src/transport.d.ts +49 -0
- package/dist/sdk/src/types.d.ts +84 -0
- package/dist/sdk/src/value-preview.d.ts +9 -0
- package/dist/storage.cjs +864 -0
- package/dist/storage.cjs.map +7 -0
- package/dist/storage.js +844 -0
- package/dist/storage.js.map +7 -0
- package/images/automatic/package.json +6 -0
- package/images/babel.cjs +111 -0
- package/images/package.json +6 -0
- package/images/react/package.json +6 -0
- package/inspector/BKBInspector.podspec +12 -0
- package/inspector/babel.cjs +45 -0
- package/inspector/expo.cjs +30 -0
- package/inspector/ios/BKBInspector.m +53 -0
- package/inspector/package.json +6 -0
- package/metro-events.json +14 -0
- package/metro-middleware.cjs +77 -0
- package/metro-reporter.cjs +107 -0
- package/network/package.json +6 -0
- package/package.json +143 -3
- package/plugin/package.json +6 -0
- package/register/package.json +6 -0
- package/renders/package.json +6 -0
- package/renders/react/package.json +6 -0
- package/storage/package.json +6 -0
- package/wrap.cjs +146 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bobby Echo Prasetyo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,417 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bkbdev/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The BKB development SDK: logs, network, console, element inspector, Reload, Storage and
|
|
4
|
+
Images for the BKB desktop, started from one import. Development only; release builds
|
|
5
|
+
contain no configuration and do not connect.
|
|
6
|
+
|
|
7
|
+
Install it in your app as a devDependency: `npm install -D @bkbdev/core`, `pnpm add -D @bkbdev/core`
|
|
8
|
+
or `yarn add -D @bkbdev/core` (`npx @bkbdev/cli init` does this). Until it is published, build the
|
|
9
|
+
repository and use the archive `pnpm test:sdk` writes to `.bkb-local/artifacts`. Do not install
|
|
10
|
+
the desktop workspace into the app.
|
|
11
|
+
|
|
12
|
+
Bundling needs the package in every build, release included: Metro loads the Babel plugin and
|
|
13
|
+
resolves `@bkbdev/core/register`, which is empty in a release bundle. Do not prune devDependencies
|
|
14
|
+
(`--omit=dev`, `NODE_ENV=production` installs) before Metro runs.
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
`npx @bkbdev/cli init` makes the changes below and writes `bkb.config.json`. By hand it is two
|
|
19
|
+
changes per project, then connect the project in BKB (it writes `bkb.config.json` with the
|
|
20
|
+
detected native application ID; keep that file out of version control):
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
// babel.config.js
|
|
24
|
+
plugins: ['@bkbdev/core/babel'],
|
|
25
|
+
|
|
26
|
+
// index.js: the first import
|
|
27
|
+
import '@bkbdev/core/register';
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Restart Metro with `--reset-cache` once. In development the plugin gives `register` the
|
|
31
|
+
project's configuration, the element inspector and Reload start by themselves, and logging
|
|
32
|
+
needs no configuration import: `getLogger().log('Loaded orders', { count: 3 })`.
|
|
33
|
+
|
|
34
|
+
- The plugin acts only for a Metro development bundle. Release builds, Jest and other tools
|
|
35
|
+
get an empty module, no configuration and no token.
|
|
36
|
+
- Another configuration location: `['@bkbdev/core/babel', { config: './config/bkb.json' }]` or the
|
|
37
|
+
`BKB_CONFIG` environment variable (the file must be inside the project, so Metro can read it).
|
|
38
|
+
- `bkb.config.json` holds `projectId`, `token`, `app` and `metro.port`. The desktop app listens on
|
|
39
|
+
`ws://127.0.0.1:8347` unless that port is taken, so the file needs no endpoint. LAN setup adds
|
|
40
|
+
`connection.endpoint`.
|
|
41
|
+
- Without `bkb.config.json` (a teammate who does not use BKB) the app builds and runs unchanged.
|
|
42
|
+
- List BKB plugin packages with `bkbPlugins`; switch built-in features off with `inspector: false`
|
|
43
|
+
or `reload: false`.
|
|
44
|
+
- Every entry point reads that configuration by itself: `createLogger()`, `createStorageDiagnostics(providers)`,
|
|
45
|
+
`createImageDiagnostics(providers)`, `createAssetRegistry()` and `startBkb(ReactNative)` need no
|
|
46
|
+
arguments for it. Pass `{ config }` (the content of the file) only when the Babel plugin cannot
|
|
47
|
+
find it; `enabled` is a separate option beside `config` and defaults to the file's value.
|
|
48
|
+
Never import the configuration file statically in code that ships: Metro bundles a static
|
|
49
|
+
import into release builds, token included.
|
|
50
|
+
|
|
51
|
+
## What starts by itself
|
|
52
|
+
|
|
53
|
+
| Feature | Needs | Switch (Babel option) |
|
|
54
|
+
| --------------------------------- | ----------------------------------------------------------------- | ------------------------ |
|
|
55
|
+
| Logger, `getLogger()` | nothing | none |
|
|
56
|
+
| Console capture | nothing | `console: false` |
|
|
57
|
+
| Network (JS `fetch` and XHR) | nothing | `network: false` |
|
|
58
|
+
| Network (Android native, images) | nothing (debug builds only; other build types ship an empty stub) | `nativeNetwork: false` |
|
|
59
|
+
| Element inspector | nothing | `inspector: false` |
|
|
60
|
+
| Reload (Cmd+R in BKB) | nothing | `reload: false` |
|
|
61
|
+
| Source locations for jump to code | nothing | `source: false` |
|
|
62
|
+
| Storage | `@react-native-async-storage/async-storage` installed | `storage: false` |
|
|
63
|
+
| Images | `Image`, `ImageBackground` or `expo-image` in your JSX | `images: false` |
|
|
64
|
+
| Asset registry | nothing | `assets: false` |
|
|
65
|
+
| Re-renders | `@bkbdev/plugin-renders` listed in `bkbPlugins` | leave it out of the list |
|
|
66
|
+
| Zustand | `@bkbdev/plugin-zustand` listed in `bkbPlugins` | leave it out of the list |
|
|
67
|
+
| React Query | `@bkbdev/plugin-react-query` listed in `bkbPlugins` | leave it out of the list |
|
|
68
|
+
|
|
69
|
+
Each feature is checked by the BKB desktop from what the running app reports; the Plugins
|
|
70
|
+
group of its sidebar shows which plugins are on. What each plugin shows, hides and can do is in
|
|
71
|
+
[PLUGINS.md](../../docs/PLUGINS.md).
|
|
72
|
+
|
|
73
|
+
## Advanced
|
|
74
|
+
|
|
75
|
+
Everything below is the explicit API behind the setup above, for projects that manage their
|
|
76
|
+
own configuration, add custom channels or need a feature the automatic setup does not cover.
|
|
77
|
+
It predates the Babel setup and still works.
|
|
78
|
+
|
|
79
|
+
## Generic logging contract
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { createLogger, installConsoleCapture } from '@bkbdev/core';
|
|
83
|
+
|
|
84
|
+
// Needs `import '@bkbdev/core/register'` first; extras such as `tags` go in the options object.
|
|
85
|
+
const logger = createLogger();
|
|
86
|
+
logger.log('Loaded orders', { count: 3 }, { level: 'info', correlationId: 'load-orders' });
|
|
87
|
+
logger.channel('sqlite').log('Query complete', { statement: 'SELECT id FROM orders' });
|
|
88
|
+
logger.table('Orders', [{ id: 1, status: 'ready' }]);
|
|
89
|
+
const span = logger.startSpan('Load orders');
|
|
90
|
+
span.end({ count: 3 });
|
|
91
|
+
// Alternatively, call span.fail(error). Only the first settlement is recorded.
|
|
92
|
+
|
|
93
|
+
const capture = installConsoleCapture({ logger });
|
|
94
|
+
capture.dispose();
|
|
95
|
+
// Disconnect when the development integration is disposed.
|
|
96
|
+
logger.disconnect();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Channels are arbitrary names. SQLite/Redux examples are application-authored logs,
|
|
100
|
+
not built-in adapters or access to those systems. Span durations describe the
|
|
101
|
+
explicit operation; this API does not start profiler recordings.
|
|
102
|
+
|
|
103
|
+
Repeated valid initialization with the same project ID, endpoint, token and app
|
|
104
|
+
identity returns the same logger. The first configuration's tags/metadata/reload
|
|
105
|
+
callback remain active. Configuration is snapshotted; changing the caller's object
|
|
106
|
+
does not redirect an existing logger. Disconnect before deliberately replacing its
|
|
107
|
+
configuration. A stale disconnected handle cannot remove its replacement. Channels
|
|
108
|
+
and spans created before disconnect become inert. At most 16 generic loggers are
|
|
109
|
+
registered per JS runtime; invalid configuration/capacity rejection yields an inert
|
|
110
|
+
`disconnected` logger instead of opening a socket.
|
|
111
|
+
|
|
112
|
+
Use `getConnectionState()` for `disabled`, `connecting`, `connected` or `disconnected`.
|
|
113
|
+
Transient failure retains a bounded queue and retries with backoff up to 30 seconds.
|
|
114
|
+
Connection/handshake and ACK waits are five seconds. Policy close 1008 stops retries;
|
|
115
|
+
explicitly disconnect and recreate with corrected pairing configuration. There is no
|
|
116
|
+
durable delivery guarantee. `getDroppedEventCount()` counts queue eviction, oversized
|
|
117
|
+
rejection and buffered events discarded on disconnect; an unacknowledged event may
|
|
118
|
+
already have reached BKB. Agent retention/native-drop counters are separate.
|
|
119
|
+
|
|
120
|
+
Capture is bounded to 200 queued/in-flight events and 1 MiB of serialized UTF-8,
|
|
121
|
+
with one unacknowledged batch. One event must fit 59,000 bytes and batches fit 60,000
|
|
122
|
+
bytes including overhead. Payloads have depth/node/string/property/array limits and
|
|
123
|
+
visible truncation markers; getters/functions are not executed. Tags are bounded
|
|
124
|
+
and known sensitive keys are redacted. Logging methods contain serialization faults.
|
|
125
|
+
These heuristics do not detect every secret; avoid logging sensitive application data.
|
|
126
|
+
|
|
127
|
+
## Console fallback and production behavior
|
|
128
|
+
|
|
129
|
+
Set `installConsoleCapture({enabled: __DEV__, logger})` only when intentionally using
|
|
130
|
+
SDK console fallback instead of CDP console collection for that runtime. Importing
|
|
131
|
+
the SDK or calling createLogger does not patch console. The opt-in installer captures
|
|
132
|
+
log/info/debug/warn/error into the `console` channel under the Custom SDK
|
|
133
|
+
source, preserving original console calls, returns and exceptions. It does not capture
|
|
134
|
+
console.table, engine exceptions that bypass console, or native logs.
|
|
135
|
+
|
|
136
|
+
There is one console owner per JS runtime, including duplicate SDK module copies.
|
|
137
|
+
Repeated installation for the same logger returns its handle; another owner receives
|
|
138
|
+
`unavailable`. The handle reports `installed`, `disabled` or `unavailable`. Dispose or
|
|
139
|
+
logger shutdown restores owned methods; third-party replacements are preserved.
|
|
140
|
+
Reentrant logging is suppressed. Unbound CDP and SDK transports are not deduplicated
|
|
141
|
+
by message text or app name: choose one collection path to avoid intentional overlap.
|
|
142
|
+
Metro build-output collection remains independent.
|
|
143
|
+
|
|
144
|
+
`enabled: false` returns inert APIs before reading connection/provider/payload data.
|
|
145
|
+
In React Native, `__DEV__ === false` also forces SDK entry points and console capture
|
|
146
|
+
off even if enabled was true. The guard does not promise zero release bundle bytes;
|
|
147
|
+
keep application setup development-only and let the app bundler remove unused code.
|
|
148
|
+
Outside RN, where **DEV** is absent, the explicit enabled flag controls activation.
|
|
149
|
+
|
|
150
|
+
## Optional asset metadata registration
|
|
151
|
+
|
|
152
|
+
`@bkbdev/core/assets` is independent of logs and Images. Create one registry outside
|
|
153
|
+
render using the development pairing configuration. It opens a separate authenticated
|
|
154
|
+
runtime connection; matching app names do not associate transports or devices.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { createAssetRegistry } from '@bkbdev/core/assets';
|
|
158
|
+
|
|
159
|
+
export const assets = createAssetRegistry();
|
|
160
|
+
|
|
161
|
+
export function registerKnownAssets(): boolean {
|
|
162
|
+
return assets.register([{ id: 'brand-logo', name: 'logo', type: 'png', scales: [1, 2] }]);
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Call registration when `getConnectionState()` is `connected`. `register` returns
|
|
167
|
+
false when disabled/disconnected, invalid, too large or busy with an unacknowledged
|
|
168
|
+
batch. A true return means sent, not durable delivery. Batches contain 1–20 metadata
|
|
169
|
+
entries and must fit the transport frame budget; the SDK retains no asset history
|
|
170
|
+
or offline replay queue. Later batches upsert the same IDs. Omit dimensions unless
|
|
171
|
+
known. This API accepts metadata only, not file contents, URLs, previews or native
|
|
172
|
+
handles. It reports registered assets, not which assets were displayed or unused.
|
|
173
|
+
|
|
174
|
+
The agent acknowledges accepted batches in order. Missing acknowledgement stops
|
|
175
|
+
the connection after five seconds. Heartbeats expire abandoned JS connections.
|
|
176
|
+
After reconnect/reload, register the app's known metadata again; previous pages are
|
|
177
|
+
invalidated by runtime/registry generations. `disconnect()` disposes the connection
|
|
178
|
+
and timers. A disabled registry does not inspect metadata or access WebSocket.
|
|
179
|
+
The desktop inventory is bounded to 1,000 entries and 1 MiB; capacity violations
|
|
180
|
+
produce an error rather than an apparently complete truncated inventory.
|
|
181
|
+
|
|
182
|
+
The Assets SDK entry has an explicit legacy subpath package for Metro versions that
|
|
183
|
+
do not support package exports. ES2015 parsing, TypeScript 3.8 declarations and
|
|
184
|
+
bundling are verified in the three approved fixtures; no new device-execution claim
|
|
185
|
+
is added. Visual Asset Explorer UI and previews remain separate work.
|
|
186
|
+
|
|
187
|
+
## Optional read-only Storage
|
|
188
|
+
|
|
189
|
+
`@bkbdev/core/storage` exports `createStorageDiagnostics`, `createAsyncStorageProvider`
|
|
190
|
+
and the small project-defined `ReadOnlyStorageProvider`/`StorageIntegration` types.
|
|
191
|
+
Register providers once outside render with development-only enablement. The SDK
|
|
192
|
+
accepts readers through dependency injection and never imports AsyncStorage or a
|
|
193
|
+
native library itself. Providers expose only `listKeys` and `readValues`; no mutation
|
|
194
|
+
command exists. See [Storage setup and limits](../../docs/STORAGE.md) in the source
|
|
195
|
+
repository for namespace identity, disposal, redaction and native compatibility.
|
|
196
|
+
|
|
197
|
+
## Logging API
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { createLogger } from '@bkbdev/core';
|
|
201
|
+
|
|
202
|
+
// Normally nothing else is needed: the project's bkb.config.json is used. Pass it yourself
|
|
203
|
+
// only when the Babel plugin cannot find the file:
|
|
204
|
+
export const debug = createLogger({
|
|
205
|
+
config: {
|
|
206
|
+
projectId: 'copy-from-bkb',
|
|
207
|
+
token: 'copy-from-bkb',
|
|
208
|
+
app: { id: 'com.example.app.dev', name: 'Example application' },
|
|
209
|
+
// Only when BKB listens somewhere other than ws://127.0.0.1:8347, for example over the LAN:
|
|
210
|
+
// connection: { endpoint: 'ws://192.168.1.20:52000' },
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
debug.log('checkout', { orderId: 'example-order' });
|
|
215
|
+
debug.channel('sqlite').log('query', { statement: 'SELECT ...', durationMs: 12 });
|
|
216
|
+
debug.table('results', [{ id: 1, name: 'Example' }]);
|
|
217
|
+
const span = debug.startSpan('load-products');
|
|
218
|
+
span.end({ count: 1 });
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
The examples contain placeholders, not working pairing credentials. Connect the
|
|
222
|
+
application folder in BKB, open Connections, and use Copy SDK setup. Keep local
|
|
223
|
+
tokens outside committed production configuration. Android can reach the desktop's
|
|
224
|
+
loopback listener using `adb -s DEVICE_SERIAL reverse tcp:PORT tcp:PORT` for the
|
|
225
|
+
actual BKB port. Direct LAN access is not enabled in this implementation.
|
|
226
|
+
|
|
227
|
+
Public operations:
|
|
228
|
+
|
|
229
|
+
- `log(name, payload?, options?)`: emit a named event, with optional level, tags,
|
|
230
|
+
and correlationId. Root events use channel `custom`.
|
|
231
|
+
- `channel(name)`: obtain the same operations within a named channel.
|
|
232
|
+
- `table(name, rows, options?)`: emit bounded tabular data; never executes a query.
|
|
233
|
+
- `startSpan(name, payload?, options?)`: emit a start event and return an ID plus
|
|
234
|
+
`end(payload?)` and `fail(error)`. A span settles once; repeated terminal calls are ignored.
|
|
235
|
+
- `getConnectionState()`: disabled, connecting, connected, or disconnected.
|
|
236
|
+
- `getDroppedEventCount()`: events discarded from the SDK's bounded queue.
|
|
237
|
+
- `disconnect()`: close transport, cancel retries, and release this logger identity.
|
|
238
|
+
|
|
239
|
+
Log methods are synchronous and best effort, not persistence acknowledgments.
|
|
240
|
+
Payload limits, cycles, inaccessible fields, and sensitive keys receive explicit
|
|
241
|
+
markers/redaction. Logging failures must not replace the application's original
|
|
242
|
+
return value or error. Disabled loggers do not connect or inspect payloads.
|
|
243
|
+
|
|
244
|
+
The transport bounds queued and in-flight events to 200 and retries with capped
|
|
245
|
+
backoff. One batch waits for an agent acknowledgment before another batch is sent;
|
|
246
|
+
that acknowledgment means in-memory acceptance, not durable persistence. Delivery
|
|
247
|
+
is not guaranteed after disconnect or a failed socket send. The desktop
|
|
248
|
+
deduplicates retained event IDs per target and displays capture drops.
|
|
249
|
+
|
|
250
|
+
Logs contain app-declared identity. Device/process binding requires separate proof;
|
|
251
|
+
the SDK alone does not establish a device serial, UDID, or native process. Expo Go
|
|
252
|
+
is excluded; use an application-specific Expo development/native build.
|
|
253
|
+
|
|
254
|
+
## Optional Images integration
|
|
255
|
+
|
|
256
|
+
`@bkbdev/core/images` supplies `createImageDiagnostics` and the bounded image lifecycle
|
|
257
|
+
observer. `@bkbdev/core/images/react` supplies `createTrackedImage` for RN Image,
|
|
258
|
+
ImageBackground and the separate Expo Image provider. React is an optional peer;
|
|
259
|
+
the base logging SDK still imports no React or native module. Disabled instrumentation
|
|
260
|
+
opens no socket and preserves original callbacks. See [application setup and precise
|
|
261
|
+
coverage](../../docs/IMAGES.md), including legacy Metro entries, source metadata,
|
|
262
|
+
privacy, captured thumbnails, producer drops and cleanup. Expo Go is unsupported.
|
|
263
|
+
|
|
264
|
+
## Automatic image diagnostics (development only)
|
|
265
|
+
|
|
266
|
+
Add `[require.resolve('@bkbdev/core/images/babel'), {root: __dirname}]` to the existing
|
|
267
|
+
Babel plugins array, preserving the application's preset/plugins. Create one
|
|
268
|
+
`createImageDiagnostics` connection from `@bkbdev/core/images`, declaring the installed
|
|
269
|
+
RN/Expo Image providers with `instrumentation: 'babel-jsx'`. Call
|
|
270
|
+
`installAutomaticImages(images, {pixelRatio: PixelRatio.get()})` from
|
|
271
|
+
`@bkbdev/core/images/automatic` before the first app render. Keep enabled gated by **DEV**.
|
|
272
|
+
The setup uses public callbacks and does not require changing every Image import.
|
|
273
|
+
|
|
274
|
+
Coverage is transformed application JSX for supported named/namespace imports only.
|
|
275
|
+
Dynamic createElement/require, dependency components and prefetch are not intercepted.
|
|
276
|
+
Production Babel output excludes the automatic helper. Late registration does not
|
|
277
|
+
remount images; reload with setup before the first render. Explicit wrappers in
|
|
278
|
+
`@bkbdev/core/images/react` remain the fallback. At integration shutdown, unmount, release
|
|
279
|
+
the automatic registration and disconnect the images connection. Expo Go is excluded.
|
|
280
|
+
Use SDK and desktop artifacts from the same build. Native evidence currently covers
|
|
281
|
+
RN 0.62.3, RN 0.70.15 and Expo 58/RN 0.88.1 with expo-image 58.0.18 on Android only.
|
|
282
|
+
|
|
283
|
+
## Console and native process binding
|
|
284
|
+
|
|
285
|
+
Base SDK connections advertise a guarded development native-log challenge when the
|
|
286
|
+
RN host provides it. Use the actual native package/bundle ID in `app.id`. Select the
|
|
287
|
+
SDK runtime and device in BKB, then verify binding before Native Logs capture. The
|
|
288
|
+
SDK does not replace the native hook or patch console. Capability and coverage limits,
|
|
289
|
+
including Android evidence and unverified Simulator support, are documented in
|
|
290
|
+
[Console and Native Logs](../../docs/CONSOLE-NATIVE.md).
|
|
291
|
+
|
|
292
|
+
For Metro bundle build/error output, the optional Node-side
|
|
293
|
+
`require('@bkbdev/core/metro-reporter')` exposes `middleware`, `update` and `close`.
|
|
294
|
+
Compose the existing `server.enhanceMiddleware` and explicitly attach its JSONL output
|
|
295
|
+
in Connections. See the setup above for complete ownership, retention and restart
|
|
296
|
+
rules. This integration does not forward client JS logs or all terminal output.
|
|
297
|
+
|
|
298
|
+
## Refresh a selected development runtime
|
|
299
|
+
|
|
300
|
+
Pass `reload: () => DevSettings.reload()` to the enabled development logger, importing
|
|
301
|
+
`DevSettings` from `react-native`. Register it on the first createLogger call; existing
|
|
302
|
+
logger instances retain their initial configuration. BKB advertises Refresh only on
|
|
303
|
+
connections with this callback. The SDK remains independent of React Native imports.
|
|
304
|
+
An acknowledgement means the callback returned, not that the new runtime is ready.
|
|
305
|
+
The desktop never falls back from one selected runtime to a Metro broadcast.
|
|
306
|
+
See [Refresh](../../docs/REFRESH.md) for shortcut and broadcast scope.
|
|
307
|
+
|
|
308
|
+
## Optional runtime identity metadata
|
|
309
|
+
|
|
310
|
+
Logger configuration accepts `runtimeMetadata` with `platform`, `reactNativeVersion`,
|
|
311
|
+
`reactVersion`, `engine`, `architecture`, `build` and `workflow`. Register it on the
|
|
312
|
+
first logger creation. Metadata is a declaration, not evidence of a native device,
|
|
313
|
+
process, Metro relationship or compatibility. Omit unknown fields rather than copying
|
|
314
|
+
workspace dependency versions as if they were observed runtime values.
|
|
315
|
+
|
|
316
|
+
The SDK advertises capability version 1; older SDK connections remain usable with
|
|
317
|
+
no capability version declared. Unsupported capability versions and explicitly
|
|
318
|
+
declared `workflow: 'expo-go'` are rejected before registration. See
|
|
319
|
+
[identity and capability contracts](../../docs/IDENTITY-REGISTRY.md).
|
|
320
|
+
|
|
321
|
+
## Optional element inspector
|
|
322
|
+
|
|
323
|
+
`@bkbdev/core/inspector` exports `createReactNativeInspector`. Create the provider before
|
|
324
|
+
the first logger for the app, including loggers initialized by imported modules:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
import * as ReactNative from 'react-native';
|
|
328
|
+
import { createReactNativeInspector } from '@bkbdev/core/inspector';
|
|
329
|
+
|
|
330
|
+
let applicationRoot: unknown;
|
|
331
|
+
const inspector = createReactNativeInspector({
|
|
332
|
+
root: () => applicationRoot,
|
|
333
|
+
native: () => ReactNative,
|
|
334
|
+
});
|
|
335
|
+
const logger = createLogger({ inspector });
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Set `applicationRoot` from a mounted native root View's callback ref, preserving
|
|
339
|
+
application refs, and use `collapsable={false}`. Clear it on unmount. The native
|
|
340
|
+
callback supplies the application's own React Native module; this optional entry
|
|
341
|
+
has no hard React Native import. Disabled/production initialization does not read
|
|
342
|
+
these callbacks or open transport. Call `inspector.dispose()` and disconnect the
|
|
343
|
+
logger when removing the integration. Logger reuse does not hot-swap providers.
|
|
344
|
+
|
|
345
|
+
The provider contract has `enabled`, stable `instanceId`, `measure()`,
|
|
346
|
+
`inspect(x, y, index?)` and `dispose()`. BKB sends bounded requests over that logger's
|
|
347
|
+
authenticated connection. Supported development renderer hooks supply hierarchy,
|
|
348
|
+
read-only props and source metadata; unsupported/missing roots return errors.
|
|
349
|
+
Children and accessors are omitted, functions become markers, and known credential
|
|
350
|
+
keys are redacted. Hierarchies and props have explicit budgets; oversized results
|
|
351
|
+
fail instead of returning partial JSON. No Fiber tree or profiler recording is sent.
|
|
352
|
+
|
|
353
|
+
Compose `@bkbdev/core/inspector/babel` with the existing RN/Expo Babel preset and
|
|
354
|
+
`{root: __dirname}` for development JSX file/line/column metadata. Dependencies,
|
|
355
|
+
outside-root sources and production transforms are excluded. This identifies JSX
|
|
356
|
+
usage, not a component function definition. Desktop inspect mode supports configurable
|
|
357
|
+
mouse/keyboard navigation and Open in editor. Expo Go is unsupported.
|
|
358
|
+
|
|
359
|
+
For automatic iOS Simulator orientation, the optional `BKBInspector` pod is included
|
|
360
|
+
under `@bkbdev/core/inspector`. Add it to the existing application Podfile target with
|
|
361
|
+
`:path => '../node_modules/@bkbdev/core/inspector', :configurations => ['Debug']`, run
|
|
362
|
+
`pod install` and rebuild the development app. For Expo CNG, append the optional
|
|
363
|
+
`@bkbdev/core/inspector/expo` config plugin to existing plugins and rebuild the development
|
|
364
|
+
client. Keep the existing React Native/Expo presets and native configuration.
|
|
365
|
+
|
|
366
|
+
The debug-only module reads foreground UIKit interface orientation on the main queue.
|
|
367
|
+
The provider reports it as optional `InspectorGeometry.interfaceOrientation` using
|
|
368
|
+
the authenticated inspector connection. There are no orientation subscriptions or
|
|
369
|
+
new transport sockets. Native module lookup errors/unavailable orientation become
|
|
370
|
+
bounded inspector failures. Generic SDK logging and Android inspection do not require
|
|
371
|
+
this pod, and disabled/production initialization does not access it. Legacy iOS
|
|
372
|
+
portrait inspection remains available without it; landscape requires the native
|
|
373
|
+
measurement. See [setup, tested combinations and limits](../../docs/ELEMENT-INSPECTOR.md).
|
|
374
|
+
|
|
375
|
+
The built-in provider now reports an opaque optional `InspectorGeometry.rootId` for
|
|
376
|
+
the active native View. Weak keys avoid retaining a history of roots. Returning a
|
|
377
|
+
different native object invalidates older inspection frames even with identical
|
|
378
|
+
bounds; missing/replaced roots and asynchronous lookup failures produce safe errors.
|
|
379
|
+
For Modal/portal surfaces, return the active modal View ref and restore the main ref
|
|
380
|
+
after dismissal. Use `collapsable={false}` and suspend with null while no root is ready.
|
|
381
|
+
|
|
382
|
+
The Babel metadata plugin accepts optional `sourceRoots`, up to 16 additional absolute
|
|
383
|
+
source directories, for shared JSX outside the app. Keep `root: __dirname` at the app;
|
|
384
|
+
file hints remain app-relative and the desktop separately enforces workspace/sibling
|
|
385
|
+
file ownership. Dependencies and production JSX stay excluded. Compose the existing
|
|
386
|
+
Metro defaults and shared React resolution. See [inspector acceptance](../../docs/INSPECTOR-ACCEPTANCE.md)
|
|
387
|
+
for setup and the native matrix. Renderer APIs report native descendant props/source
|
|
388
|
+
for some composite entries, and RN 0.62 owner ancestry can stop at memo/forwardRef.
|
|
389
|
+
|
|
390
|
+
## Network lifecycle capture
|
|
391
|
+
|
|
392
|
+
`@bkbdev/core/network` exports `installNetworkCapture({enabled, onEvent})`,
|
|
393
|
+
`createNetworkReporter(logger)` and `javascriptCoverage(capture)`. Typed lifecycle
|
|
394
|
+
events populate the Electron Network table through the existing paired logger.
|
|
395
|
+
Headers and already-materialized text/JSON bodies have bounded redacted previews;
|
|
396
|
+
binary/stream bodies are omitted. Capture never reads application response streams.
|
|
397
|
+
Disposal restores owned hooks without cancelling application requests.
|
|
398
|
+
|
|
399
|
+
`installAndroidNetworkCapture` supports optional debug-only OkHttp metadata capture
|
|
400
|
+
with the supplied Java helpers and an app-owned client builder/native rebuild.
|
|
401
|
+
Choose JS or native mode for the same HTTP path. See [Network inspector](../../docs/NETWORK.md)
|
|
402
|
+
for setup, exact per-API coverage, native limits and recorded verification.
|
|
403
|
+
|
|
404
|
+
## Committed re-render observer
|
|
405
|
+
|
|
406
|
+
`@bkbdev/core/renders` exports `createRenderObserver({enabled: __DEV__})`. Pass the
|
|
407
|
+
provider as `renders` to the existing logger. Tracking starts explicitly through
|
|
408
|
+
the scoped agent start/reset/stop commands; registration does not start profiling
|
|
409
|
+
or enable an overlay. The observer emits committed-work counts, bounded before/after
|
|
410
|
+
changes, conservative causes and available source labels over the paired SDK socket.
|
|
411
|
+
Call `logger.disconnect()` and `renders.dispose()` when the owning debug session ends.
|
|
412
|
+
See [Re-renders setup](../../docs/RENDERS.md) for supported generations,
|
|
413
|
+
context/hook limits, production guards and native evidence. Optional `createRenderOverlay` and `/renders/react` host setup add non-interactive
|
|
414
|
+
outline/flash highlights, with reduced motion and bounded geometry updates.
|
|
415
|
+
Forward scroll/layout changes to `overlay.invalidate()` and dispose the controller
|
|
416
|
+
on teardown. The desktop Re-renders tool provides scoped controls, ranked instances,
|
|
417
|
+
bounded change history and source navigation.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// BKB's Android library (autolinked). Only the `debug` build type carries the real
|
|
2
|
+
// network capture; every other build type compiles an empty stub with no manifest entries.
|
|
3
|
+
apply plugin: 'com.android.library'
|
|
4
|
+
|
|
5
|
+
def safeExtGet(name, fallback) {
|
|
6
|
+
rootProject.ext.has(name) ? rootProject.ext.get(name) : fallback
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// AGP 8 needs a package namespace and AGP 7.2 rejects the property, so it is set only
|
|
10
|
+
// when available. The word is kept out of quoted text and plain assignments on purpose:
|
|
11
|
+
// the autolinking scripts search this file with a regular expression, and they should
|
|
12
|
+
// fall through to the manifest `package` attribute.
|
|
13
|
+
def bkbNamespace = 'dev.bkb.network'
|
|
14
|
+
|
|
15
|
+
android {
|
|
16
|
+
if (project.android.respondsTo('setNamespace')) project.android.namespace = bkbNamespace
|
|
17
|
+
compileSdkVersion safeExtGet('compileSdkVersion', 31)
|
|
18
|
+
defaultConfig {
|
|
19
|
+
minSdkVersion safeExtGet('minSdkVersion', 21)
|
|
20
|
+
targetSdkVersion safeExtGet('targetSdkVersion', 31)
|
|
21
|
+
}
|
|
22
|
+
buildTypes {
|
|
23
|
+
debug { }
|
|
24
|
+
release { }
|
|
25
|
+
// React Native 0.82 and later define this build type in app projects.
|
|
26
|
+
debugOptimized { }
|
|
27
|
+
}
|
|
28
|
+
lintOptions { abortOnError false }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
repositories {
|
|
32
|
+
google()
|
|
33
|
+
mavenCentral()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
dependencies {
|
|
37
|
+
compileOnly 'com.facebook.react:react-native:+'
|
|
38
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
package dev.bkb.network;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.NativeModule;
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
5
|
+
import java.util.Collections;
|
|
6
|
+
import java.util.List;
|
|
7
|
+
|
|
8
|
+
final class BKBNetworkCapture {
|
|
9
|
+
static List<NativeModule> modules(ReactApplicationContext context) {
|
|
10
|
+
BKBNetworkInterceptor.installOnSharedClients();
|
|
11
|
+
return Collections.singletonList(new BKBNetworkModule(context));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
package dev.bkb.network;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.modules.network.OkHttpClientFactory;
|
|
4
|
+
import com.facebook.react.modules.network.OkHttpClientProvider;
|
|
5
|
+
import java.io.IOException;
|
|
6
|
+
import java.util.UUID;
|
|
7
|
+
import okhttp3.Interceptor;
|
|
8
|
+
import okhttp3.OkHttpClient;
|
|
9
|
+
import okhttp3.Request;
|
|
10
|
+
import okhttp3.Response;
|
|
11
|
+
import org.json.JSONObject;
|
|
12
|
+
|
|
13
|
+
public final class BKBNetworkInterceptor implements Interceptor {
|
|
14
|
+
private static boolean installed;
|
|
15
|
+
|
|
16
|
+
// React Native builds one shared client (networking module and Fresco both use it) from
|
|
17
|
+
// this factory. Wrapping the factory keeps any factory the app already set.
|
|
18
|
+
static synchronized void installOnSharedClients() {
|
|
19
|
+
if (installed) return;
|
|
20
|
+
installed = true;
|
|
21
|
+
final OkHttpClientFactory base = new OkHttpClientFactory() {
|
|
22
|
+
@Override
|
|
23
|
+
public OkHttpClient createNewNetworkModuleClient() {
|
|
24
|
+
return OkHttpClientProvider.createClientBuilder().build();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
OkHttpClientProvider.setOkHttpClientFactory(new OkHttpClientFactory() {
|
|
28
|
+
@Override
|
|
29
|
+
public OkHttpClient createNewNetworkModuleClient() {
|
|
30
|
+
return base.createNewNetworkModuleClient().newBuilder()
|
|
31
|
+
.addInterceptor(new BKBNetworkInterceptor()).build();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@Override
|
|
37
|
+
public Response intercept(Chain chain) throws IOException {
|
|
38
|
+
// React Native's networking module tags its requests with an Integer id; those are
|
|
39
|
+
// reported by the JavaScript hook with their bodies, so they are skipped here.
|
|
40
|
+
if (!BKBNetworkModule.isEnabled() || chain.request().tag() instanceof Integer) {
|
|
41
|
+
return chain.proceed(chain.request());
|
|
42
|
+
}
|
|
43
|
+
String id = UUID.randomUUID().toString();
|
|
44
|
+
long started = System.nanoTime();
|
|
45
|
+
Request request = chain.request();
|
|
46
|
+
JSONObject start = event(id, request, "start");
|
|
47
|
+
BKBNetworkModule.publish(start);
|
|
48
|
+
try {
|
|
49
|
+
Response response = chain.proceed(request);
|
|
50
|
+
JSONObject end = event(id, request, "end");
|
|
51
|
+
put(end, "outcome", "response");
|
|
52
|
+
put(end, "status", response.code());
|
|
53
|
+
put(end, "durationMs", (System.nanoTime() - started) / 1000000.0);
|
|
54
|
+
put(end, "responseUrl", safeUrl(response.request()));
|
|
55
|
+
put(end, "redirected", !response.request().url().equals(request.url()));
|
|
56
|
+
BKBNetworkModule.publish(end);
|
|
57
|
+
return response;
|
|
58
|
+
} catch (IOException error) {
|
|
59
|
+
JSONObject end = event(id, request, "end");
|
|
60
|
+
put(end, "outcome", chain.call().isCanceled() ? "abort" : "error");
|
|
61
|
+
put(end, "durationMs", (System.nanoTime() - started) / 1000000.0);
|
|
62
|
+
BKBNetworkModule.publish(end);
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
private static JSONObject event(String id, Request request, String phase) {
|
|
67
|
+
JSONObject value = new JSONObject();
|
|
68
|
+
put(value, "requestId", id);
|
|
69
|
+
put(value, "timestamp", System.currentTimeMillis());
|
|
70
|
+
put(value, "method", request.method());
|
|
71
|
+
put(value, "url", safeUrl(request));
|
|
72
|
+
put(value, "phase", phase);
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
private static String safeUrl(Request request) {
|
|
76
|
+
String url = request.url().newBuilder()
|
|
77
|
+
.username("").password("").query(null).fragment(null).build().toString();
|
|
78
|
+
return url.substring(0, Math.min(url.length(), 2048));
|
|
79
|
+
}
|
|
80
|
+
private static void put(JSONObject object, String key, Object value) {
|
|
81
|
+
try {
|
|
82
|
+
object.put(key, value);
|
|
83
|
+
} catch (Exception ignored) { }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package dev.bkb.network;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.Arguments;
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
5
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
6
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
7
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
8
|
+
import java.util.Collections;
|
|
9
|
+
import java.util.Map;
|
|
10
|
+
import org.json.JSONObject;
|
|
11
|
+
|
|
12
|
+
public final class BKBNetworkModule extends ReactContextBaseJavaModule {
|
|
13
|
+
private static final int MAX_EVENTS_PER_SECOND = 100;
|
|
14
|
+
private static volatile BKBNetworkModule owner;
|
|
15
|
+
private static long windowStarted;
|
|
16
|
+
private static int emitted;
|
|
17
|
+
public BKBNetworkModule(ReactApplicationContext context) {
|
|
18
|
+
super(context);
|
|
19
|
+
}
|
|
20
|
+
@Override
|
|
21
|
+
public String getName() {
|
|
22
|
+
return "BKBNetwork";
|
|
23
|
+
}
|
|
24
|
+
@Override
|
|
25
|
+
public Map<String, Object> getConstants() {
|
|
26
|
+
return Collections.singletonMap("version", 1);
|
|
27
|
+
}
|
|
28
|
+
@ReactMethod
|
|
29
|
+
public void start() {
|
|
30
|
+
owner = this;
|
|
31
|
+
}
|
|
32
|
+
@ReactMethod
|
|
33
|
+
public void stop() {
|
|
34
|
+
if (owner == this) owner = null;
|
|
35
|
+
}
|
|
36
|
+
// Required by React Native's event-emitter contract; JS owns subscriptions.
|
|
37
|
+
@ReactMethod
|
|
38
|
+
public void addListener(String name) { }
|
|
39
|
+
@ReactMethod
|
|
40
|
+
public void removeListeners(double count) { }
|
|
41
|
+
@Override
|
|
42
|
+
public void invalidate() {
|
|
43
|
+
stop();
|
|
44
|
+
super.invalidate();
|
|
45
|
+
}
|
|
46
|
+
static boolean isEnabled() {
|
|
47
|
+
return owner != null;
|
|
48
|
+
}
|
|
49
|
+
static synchronized void publish(JSONObject event) {
|
|
50
|
+
BKBNetworkModule current = owner;
|
|
51
|
+
if (current == null || !current.getReactApplicationContext().hasActiveCatalystInstance()) return;
|
|
52
|
+
long now = android.os.SystemClock.elapsedRealtime();
|
|
53
|
+
if (now - windowStarted >= 1000) {
|
|
54
|
+
windowStarted = now;
|
|
55
|
+
emitted = 0;
|
|
56
|
+
}
|
|
57
|
+
if (++emitted > MAX_EVENTS_PER_SECOND) return;
|
|
58
|
+
try {
|
|
59
|
+
current.getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
60
|
+
.emit("bkbNetwork", Arguments.makeNativeMap(jsonMap(event)));
|
|
61
|
+
} catch (RuntimeException ignored) { }
|
|
62
|
+
}
|
|
63
|
+
private static Map<String, Object> jsonMap(JSONObject event) {
|
|
64
|
+
Map<String, Object> result = new java.util.HashMap<>();
|
|
65
|
+
java.util.Iterator<String> keys = event.keys();
|
|
66
|
+
while (keys.hasNext()) {
|
|
67
|
+
String key = keys.next();
|
|
68
|
+
result.put(key, event.opt(key));
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
package dev.bkb.network;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.NativeModule;
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
5
|
+
import java.util.Collections;
|
|
6
|
+
import java.util.List;
|
|
7
|
+
|
|
8
|
+
// Empty on purpose: this build type carries no BKB network capture.
|
|
9
|
+
final class BKBNetworkCapture {
|
|
10
|
+
static List<NativeModule> modules(ReactApplicationContext context) {
|
|
11
|
+
return Collections.emptyList();
|
|
12
|
+
}
|
|
13
|
+
}
|