react-native-bspatch 3.0.6 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -4
- package/lib/commonjs/index.js +11 -14
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ota/OtaProvider.js +1 -12
- package/lib/commonjs/ota/OtaProvider.js.map +1 -1
- package/lib/commonjs/ota/otaCore.js +62 -95
- package/lib/commonjs/ota/otaCore.js.map +1 -1
- package/lib/module/index.js +8 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/ota/OtaProvider.js +1 -12
- package/lib/module/ota/OtaProvider.js.map +1 -1
- package/lib/module/ota/otaCore.js +62 -95
- package/lib/module/ota/otaCore.js.map +1 -1
- package/lib/typescript/index.d.ts +7 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/ota/OtaProvider.d.ts.map +1 -1
- package/lib/typescript/ota/otaCore.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +8 -1
- package/src/ota/OtaProvider.tsx +1 -12
- package/src/ota/otaCore.ts +64 -134
- package/tools/bump_version.js +1 -13
- package/lib/commonjs/ota/otaAssetResolver.js +0 -141
- package/lib/commonjs/ota/otaAssetResolver.js.map +0 -1
- package/lib/module/ota/otaAssetResolver.js +0 -134
- package/lib/module/ota/otaAssetResolver.js.map +0 -1
- package/lib/typescript/ota/otaAssetResolver.d.ts +0 -15
- package/lib/typescript/ota/otaAssetResolver.d.ts.map +0 -1
- package/src/ota/otaAssetResolver.ts +0 -179
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { Image } from 'react-native';
|
|
2
|
-
import ReactNativeBlobUtil from 'react-native-blob-util';
|
|
3
|
-
import NativeBsPatch from '../NativeBsPatch';
|
|
4
|
-
|
|
5
|
-
const ASSET_STORE_DIR = `${ReactNativeBlobUtil.fs.dirs.DocumentDir}/ota/assets`;
|
|
6
|
-
|
|
7
|
-
interface AssetMetadata {
|
|
8
|
-
originalPath: string;
|
|
9
|
-
hash: string;
|
|
10
|
-
ext: string;
|
|
11
|
-
_normPath: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface AssetManifest {
|
|
15
|
-
assets: AssetMetadata[];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
let assetManifest: AssetManifest | null = null;
|
|
19
|
-
let assetMap: Map<string, AssetMetadata> = new Map();
|
|
20
|
-
let assetList: AssetMetadata[] = [];
|
|
21
|
-
let assetLookupCache: Map<string, AssetMetadata | null> = new Map();
|
|
22
|
-
let isPatched = false;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Internal logger with native bridge integration.
|
|
26
|
-
*/
|
|
27
|
-
const log = (msg: string): void => {
|
|
28
|
-
const formatted = `[otaAssetResolver] ${msg}`;
|
|
29
|
-
console.log(formatted);
|
|
30
|
-
if (NativeBsPatch?.logNative) {
|
|
31
|
-
NativeBsPatch.logNative(formatted);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Initializes the OTA asset resolver for a specific version.
|
|
37
|
-
* Loads the asset manifest and prepares an O(1) lookup map for intercepted image requests.
|
|
38
|
-
*
|
|
39
|
-
* @param version - The OTA version number to load assets from.
|
|
40
|
-
*/
|
|
41
|
-
export const initOtaAssetResolver = async (version: number): Promise<void> => {
|
|
42
|
-
const otaDir = `${ReactNativeBlobUtil.fs.dirs.DocumentDir}/ota`;
|
|
43
|
-
const versionDir = `${otaDir}/v${version}`;
|
|
44
|
-
const manifestPath = `${versionDir}/asset-manifest-v${version}.json`;
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
if (await ReactNativeBlobUtil.fs.exists(manifestPath)) {
|
|
48
|
-
const content = await ReactNativeBlobUtil.fs.readFile(
|
|
49
|
-
manifestPath,
|
|
50
|
-
'utf8',
|
|
51
|
-
);
|
|
52
|
-
assetManifest = JSON.parse(content) as AssetManifest;
|
|
53
|
-
|
|
54
|
-
// Pre-compute normalized paths for O(1) lookup to prevent JS thread blocking during render
|
|
55
|
-
const map = new Map<string, AssetMetadata>();
|
|
56
|
-
const list = assetManifest.assets || [];
|
|
57
|
-
list.forEach((a: AssetMetadata) => {
|
|
58
|
-
const normPath = a.originalPath
|
|
59
|
-
.toLowerCase()
|
|
60
|
-
.replace(/\//g, '_')
|
|
61
|
-
.replace(/^(assets_)+/, '');
|
|
62
|
-
a._normPath = normPath; // cache for fast fallback
|
|
63
|
-
map.set(normPath, a);
|
|
64
|
-
});
|
|
65
|
-
assetMap = map;
|
|
66
|
-
assetList = list;
|
|
67
|
-
assetLookupCache = new Map();
|
|
68
|
-
|
|
69
|
-
log(
|
|
70
|
-
`Initialized version ${version} (${list.length} assets, mapped for O(1) lookup)`,
|
|
71
|
-
);
|
|
72
|
-
patchAssetResolver();
|
|
73
|
-
} else {
|
|
74
|
-
log(`Manifest NOT found at: ${manifestPath}`);
|
|
75
|
-
}
|
|
76
|
-
} catch (e) {
|
|
77
|
-
log(`Failed to load manifest: ${(e as Error).message}`);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Patches React Native's Image.resolveAssetSource to intercept and redirect
|
|
85
|
-
* asset requests to the locally downloaded OTA asset store.
|
|
86
|
-
*/
|
|
87
|
-
const patchAssetResolver = (): void => {
|
|
88
|
-
if (isPatched) return;
|
|
89
|
-
isPatched = true;
|
|
90
|
-
|
|
91
|
-
const resolveAssetSource = (Image as unknown as {
|
|
92
|
-
resolveAssetSource: {
|
|
93
|
-
setCustomSourceTransformer: (
|
|
94
|
-
transformer: (resolver: {
|
|
95
|
-
defaultAsset: () => { uri: string } | null;
|
|
96
|
-
}) => { uri: string } | null,
|
|
97
|
-
) => void;
|
|
98
|
-
};
|
|
99
|
-
}).resolveAssetSource;
|
|
100
|
-
|
|
101
|
-
if (
|
|
102
|
-
resolveAssetSource &&
|
|
103
|
-
typeof resolveAssetSource.setCustomSourceTransformer === 'function'
|
|
104
|
-
) {
|
|
105
|
-
resolveAssetSource.setCustomSourceTransformer((resolver) => {
|
|
106
|
-
// Get the default resolution result
|
|
107
|
-
const res = resolver.defaultAsset();
|
|
108
|
-
if (!res || !assetManifest) return res;
|
|
109
|
-
|
|
110
|
-
const uri = res.uri;
|
|
111
|
-
|
|
112
|
-
// Skip remote or data URIs
|
|
113
|
-
// Note: We DO NOT skip 'file://' because local bundles use it for relative asset resolution.
|
|
114
|
-
if (!uri || uri.startsWith('http') || uri.startsWith('data')) {
|
|
115
|
-
return res;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const normUri = uri
|
|
119
|
-
.toLowerCase()
|
|
120
|
-
.replace(/\//g, '_')
|
|
121
|
-
.replace(/^(assets_)+/, '');
|
|
122
|
-
|
|
123
|
-
const cached = assetLookupCache.get(normUri);
|
|
124
|
-
|
|
125
|
-
// 1. Fast cached lookup from previous exact/fuzzy resolution
|
|
126
|
-
let found = cached === undefined ? undefined : cached;
|
|
127
|
-
|
|
128
|
-
// 2. Fast O(1) Map lookup
|
|
129
|
-
if (found === undefined) {
|
|
130
|
-
found = assetMap.get(normUri) ?? undefined;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// 3. Fallback O(N) fuzzy matching (without regex overhead)
|
|
134
|
-
if (found === undefined) {
|
|
135
|
-
found = assetList.find((a: AssetMetadata) => {
|
|
136
|
-
return (
|
|
137
|
-
a._normPath.endsWith(normUri) ||
|
|
138
|
-
normUri.endsWith(a._normPath)
|
|
139
|
-
);
|
|
140
|
-
});
|
|
141
|
-
assetLookupCache.set(normUri, found ?? null);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (found) {
|
|
145
|
-
const localPath = `${ASSET_STORE_DIR}/${found.hash}.${found.ext}`;
|
|
146
|
-
// iOS & Android: requires file:// prefix for absolute filesystem paths
|
|
147
|
-
const finalUri = `file://${localPath}`;
|
|
148
|
-
log(`REDIRECTED: ${uri} -> ${found.hash} (Local: ${finalUri})`);
|
|
149
|
-
return { ...res, uri: finalUri };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Diagnostic logging for missed assets that appear to be internal resources
|
|
153
|
-
if (uri.includes('assets')) {
|
|
154
|
-
log(`MISS: ${uri} (Not found in manifest)`);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return res;
|
|
158
|
-
});
|
|
159
|
-
log('Successfully attached CustomSourceTransformer.');
|
|
160
|
-
} else {
|
|
161
|
-
log(
|
|
162
|
-
'ERROR: setCustomSourceTransformer is not available in this React Native version.',
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Resets the OTA asset resolver state.
|
|
169
|
-
* Call this before a major app upgrade or when unmounting the OTA context.
|
|
170
|
-
* Clears cached manifest data and resets the transformer patch guard so
|
|
171
|
-
* `initOtaAssetResolver` can re-patch cleanly on next call.
|
|
172
|
-
*/
|
|
173
|
-
export const resetOtaAssetResolver = (): void => {
|
|
174
|
-
assetManifest = null;
|
|
175
|
-
assetMap = new Map();
|
|
176
|
-
assetList = [];
|
|
177
|
-
assetLookupCache = new Map();
|
|
178
|
-
isPatched = false;
|
|
179
|
-
};
|