@react-native-oh/react-native-harmony 0.72.82 → 0.72.90

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.
@@ -1,11 +1,64 @@
1
- import {
2
- getAssetDestRelativePath,
3
- Asset,
4
- } from '@react-native-oh/react-native-harmony-cli/src/assetResolver';
5
- import {
6
- getBasePath,
7
- } from '@react-native/assets-registry/path-support';
8
- import { Platform } from 'react-native';
1
+ /**
2
+ * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type { AssetData } from 'metro';
9
+ // @ts-ignore
10
+ import { getBasePath } from '@react-native/assets-registry/path-support';
11
+ import { Dimensions, Platform } from 'react-native';
12
+
13
+ const ALLOWED_SCALES: number[] = [1, 2, 3, 4];
14
+
15
+ function filterAssetScales(
16
+ scales: readonly number[],
17
+ ): readonly number[] {
18
+ const result = scales.filter(scale => ALLOWED_SCALES.includes(scale));
19
+ if (result.length === 0 && scales.length > 0) {
20
+ // No matching scale found, but there are some available. Ideally we don't
21
+ // want to be in this situation and should throw, but for now as a fallback
22
+ // let's just use the closest larger image
23
+ const maxScale = ALLOWED_SCALES[ALLOWED_SCALES.length - 1];
24
+ for (const scale of scales) {
25
+ if (scale > maxScale) {
26
+ result.push(scale);
27
+ break;
28
+ }
29
+ }
30
+ // There is no larger scales available, use the largest we have
31
+ if (result.length === 0) {
32
+ result.push(scales[scales.length - 1]);
33
+ }
34
+ }
35
+ return result;
36
+ }
37
+
38
+ function pickScale(scales: Array<number>, deviceScale?: number): number {
39
+ const validScales = filterAssetScales(scales);
40
+ if (deviceScale == null) {
41
+ deviceScale = Dimensions.get('window').scale;
42
+ }
43
+ // Packager guarantees that `scales` array is sorted
44
+ for (let i = 0; i < validScales.length; i++) {
45
+ if (validScales[i] >= deviceScale) {
46
+ return validScales[i];
47
+ }
48
+ }
49
+
50
+ // If nothing matches, device scale is larger than any available
51
+ // scales, so we return the biggest one. Unless the array is empty,
52
+ // in which case we default to 1
53
+ return validScales[validScales.length - 1] || 1;
54
+ }
55
+
56
+ function getScaledAssetPath(asset: AssetData): string {
57
+ const scale = pickScale(asset.scales);
58
+ const scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
59
+ const assetDir = getBasePath(asset);
60
+ return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type;
61
+ }
9
62
 
10
63
  type ResolvedAssetSource = {
11
64
  readonly __packager_asset: boolean;
@@ -15,16 +68,11 @@ type ResolvedAssetSource = {
15
68
  readonly scale: number;
16
69
  };
17
70
 
18
- function getAssetPath(asset: Asset): string {
19
- const assetDir = getBasePath(asset);
20
- return assetDir + '/' + asset.name + '.' + asset.type;
21
- }
22
-
23
71
  class AssetSourceResolver {
24
72
  constructor(
25
73
  private serverUrl: string | undefined,
26
74
  private jsbundleUrl: string | undefined,
27
- private asset: Asset
75
+ private asset: AssetData
28
76
  ) {}
29
77
 
30
78
  isLoadedFromServer(): boolean {
@@ -40,15 +88,17 @@ class AssetSourceResolver {
40
88
  return this.assetServerURL();
41
89
  }
42
90
 
43
- const prefix = this.isLoadedFromFileSystem() ? this.jsbundleUrl : 'asset://';
91
+ const path = this.isLoadedFromFileSystem() ? this.jsbundleUrl : 'asset://';
44
92
 
45
- return {
46
- __packager_asset: this.asset.__packager_asset,
47
- uri: `${prefix}${getAssetDestRelativePath(this.asset)}`,
48
- scale: 1,
49
- width: this.asset.width,
50
- height: this.asset.height,
51
- };
93
+ // Assets can have relative paths outside of the project root.
94
+ // Replace `../` with `_` to make sure they don't end up outside of
95
+ // the expected assets directory.
96
+ return this.fromSource(
97
+ path +
98
+ getScaledAssetPath(this.asset)
99
+ .replace(/^assets\//, '')
100
+ .replace(/\.\.\//g, '_')
101
+ );
52
102
  }
53
103
 
54
104
  /**
@@ -62,11 +112,11 @@ class AssetSourceResolver {
62
112
 
63
113
  return this.fromSource(
64
114
  this.serverUrl +
65
- getAssetPath(this.asset) +
115
+ getScaledAssetPath(this.asset) +
66
116
  '?platform=' +
67
117
  Platform.OS +
68
118
  '&hash=' +
69
- this.asset.hash,
119
+ this.asset.hash
70
120
  );
71
121
  }
72
122
 
@@ -76,7 +126,7 @@ class AssetSourceResolver {
76
126
  width: this.asset.width,
77
127
  height: this.asset.height,
78
128
  uri: source,
79
- scale: 1,
129
+ scale: pickScale(this.asset.scales),
80
130
  };
81
131
  }
82
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-oh/react-native-harmony",
3
- "version": "0.72.82",
3
+ "version": "0.72.90",
4
4
  "description": "",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",
@@ -49,7 +49,7 @@
49
49
  "./*.har"
50
50
  ],
51
51
  "dependencies": {
52
- "@react-native-oh/react-native-harmony-cli": "^0.0.30",
52
+ "@react-native-oh/react-native-harmony-cli": "^0.0.37",
53
53
  "colors": "^1.4.0",
54
54
  "fs-extra": "^11.1.1",
55
55
  "metro": "^0.76.3",
Binary file