9
9
10
10
package com .facebook .react .devsupport ;
11
11
12
- import android .util .JsonReader ;
13
- import android .util .JsonToken ;
14
12
import android .util .Log ;
15
13
import com .facebook .common .logging .FLog ;
16
14
import com .facebook .infer .annotation .Assertions ;
17
15
import com .facebook .react .common .DebugServerException ;
18
16
import com .facebook .react .common .ReactConstants ;
19
17
import com .facebook .react .devsupport .interfaces .DevBundleDownloadListener ;
20
18
import java .io .File ;
21
- import java .io .FileOutputStream ;
22
19
import java .io .IOException ;
23
- import java .io .InputStreamReader ;
24
- import java .util .LinkedHashMap ;
25
20
import java .util .Map ;
26
21
import java .util .regex .Matcher ;
27
22
import java .util .regex .Pattern ;
@@ -47,11 +42,8 @@ public class BundleDownloader {
47
42
48
43
private final OkHttpClient mClient ;
49
44
50
- private final LinkedHashMap <Number , byte []> mPreModules = new LinkedHashMap <>();
51
- private final LinkedHashMap <Number , byte []> mDeltaModules = new LinkedHashMap <>();
52
- private final LinkedHashMap <Number , byte []> mPostModules = new LinkedHashMap <>();
45
+ private final BundleDeltaClient mBundleDeltaClient = new BundleDeltaClient ();
53
46
54
- private @ Nullable String mDeltaId ;
55
47
private @ Nullable Call mDownloadBundleFromURLCall ;
56
48
57
49
public static class BundleInfo {
@@ -110,15 +102,9 @@ public void downloadBundleFromURL(
110
102
final String bundleURL ,
111
103
final @ Nullable BundleInfo bundleInfo ) {
112
104
113
- String finalUrl = bundleURL ;
114
-
115
- if (isDeltaUrl (bundleURL ) && mDeltaId != null ) {
116
- finalUrl += "&deltaBundleId=" + mDeltaId ;
117
- }
118
-
119
105
final Request request =
120
106
new Request .Builder ()
121
- .url (finalUrl )
107
+ .url (mBundleDeltaClient . toDeltaUrl ( bundleURL ) )
122
108
// FIXME: there is a bug that makes MultipartStreamReader to never find the end of the
123
109
// multipart message. This temporarily disables the multipart mode to work around it,
124
110
// but
@@ -253,11 +239,11 @@ private void processBundleResult(
253
239
254
240
boolean bundleUpdated ;
255
241
256
- if (isDeltaUrl (url )) {
242
+ if (BundleDeltaClient . isDeltaUrl (url )) {
257
243
// If the bundle URL has the delta extension, we need to use the delta patching logic.
258
- bundleUpdated = storeDeltaInFile (body , tmpFile );
244
+ bundleUpdated = mBundleDeltaClient . storeDeltaInFile (body , tmpFile );
259
245
} else {
260
- resetDeltaCache ();
246
+ mBundleDeltaClient . reset ();
261
247
bundleUpdated = storePlainJSInFile (body , tmpFile );
262
248
}
263
249
@@ -286,101 +272,6 @@ private static boolean storePlainJSInFile(BufferedSource body, File outputFile)
286
272
return true ;
287
273
}
288
274
289
- private synchronized boolean storeDeltaInFile (BufferedSource body , File outputFile ) throws IOException {
290
-
291
- JsonReader jsonReader = new JsonReader (new InputStreamReader (body .inputStream ()));
292
-
293
- jsonReader .beginObject ();
294
-
295
- int numChangedModules = 0 ;
296
-
297
- while (jsonReader .hasNext ()) {
298
- String name = jsonReader .nextName ();
299
- if (name .equals ("id" )) {
300
- mDeltaId = jsonReader .nextString ();
301
- } else if (name .equals ("pre" )) {
302
- numChangedModules += patchDelta (jsonReader , mPreModules );
303
- } else if (name .equals ("post" )) {
304
- numChangedModules += patchDelta (jsonReader , mPostModules );
305
- } else if (name .equals ("delta" )) {
306
- numChangedModules += patchDelta (jsonReader , mDeltaModules );
307
- } else {
308
- jsonReader .skipValue ();
309
- }
310
- }
311
-
312
- jsonReader .endObject ();
313
- jsonReader .close ();
314
-
315
- if (numChangedModules == 0 ) {
316
- // If we receive an empty delta, we don't need to save the file again (it'll have the
317
- // same content).
318
- return false ;
319
- }
320
-
321
- FileOutputStream fileOutputStream = new FileOutputStream (outputFile );
322
-
323
- try {
324
- for (byte [] code : mPreModules .values ()) {
325
- fileOutputStream .write (code );
326
- fileOutputStream .write ('\n' );
327
- }
328
-
329
- for (byte [] code : mDeltaModules .values ()) {
330
- fileOutputStream .write (code );
331
- fileOutputStream .write ('\n' );
332
- }
333
-
334
- for (byte [] code : mPostModules .values ()) {
335
- fileOutputStream .write (code );
336
- fileOutputStream .write ('\n' );
337
- }
338
- } finally {
339
- fileOutputStream .flush ();
340
- fileOutputStream .close ();
341
- }
342
-
343
- return true ;
344
- }
345
-
346
- private static int patchDelta (JsonReader jsonReader , LinkedHashMap <Number , byte []> map )
347
- throws IOException {
348
- jsonReader .beginArray ();
349
-
350
- int numModules = 0 ;
351
- while (jsonReader .hasNext ()) {
352
- jsonReader .beginArray ();
353
-
354
- int moduleId = jsonReader .nextInt ();
355
-
356
- if (jsonReader .peek () == JsonToken .NULL ) {
357
- jsonReader .skipValue ();
358
- map .remove (moduleId );
359
- } else {
360
- map .put (moduleId , jsonReader .nextString ().getBytes ());
361
- }
362
-
363
- jsonReader .endArray ();
364
- numModules ++;
365
- }
366
-
367
- jsonReader .endArray ();
368
-
369
- return numModules ;
370
- }
371
-
372
- private void resetDeltaCache () {
373
- mDeltaId = null ;
374
-
375
- mDeltaModules .clear ();
376
- mPreModules .clear ();
377
- mPostModules .clear ();
378
- }
379
-
380
- private static boolean isDeltaUrl (String bundleUrl ) {
381
- return bundleUrl .indexOf (".delta?" ) != -1 ;
382
- }
383
-
384
275
private static void populateBundleInfo (String url , Headers headers , BundleInfo bundleInfo ) {
385
276
bundleInfo .mUrl = url ;
386
277
0 commit comments