Skip to content

Commit

Permalink
Revert "Revert "[expo-assets] search for key without file extension (… (
Browse files Browse the repository at this point in the history
  • Loading branch information
jkh authored and Felipe committed Sep 18, 2021
1 parent 1608a90 commit 83f8551
Show file tree
Hide file tree
Showing 21 changed files with 117 additions and 84 deletions.
2 changes: 2 additions & 0 deletions packages/expo-asset/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### 🎉 New features

- Reapply [#12624](https://github.com/expo/expo/pull/12624) ([#13789](https://github.com/expo/expo/pull/13789) by [@jkhales](https://github.com/jkhales))

### 🐛 Bug fixes

## 8.3.2 — 2021-04-21
Expand Down
4 changes: 2 additions & 2 deletions packages/expo-asset/build/Asset.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-asset/build/Asset.js.map

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions packages/expo-asset/build/EmbeddedAssets.d.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/expo-asset/build/EmbeddedAssets.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/expo-asset/build/EmbeddedAssets.js.map

This file was deleted.

1 change: 0 additions & 1 deletion packages/expo-asset/build/EmbeddedAssets.web.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/expo-asset/build/EmbeddedAssets.web.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/expo-asset/build/EmbeddedAssets.web.js.map

This file was deleted.

4 changes: 4 additions & 0 deletions packages/expo-asset/build/LocalAssets.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions packages/expo-asset/build/LocalAssets.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/expo-asset/build/LocalAssets.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/expo-asset/build/LocalAssets.web.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/expo-asset/build/LocalAssets.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/expo-asset/build/LocalAssets.web.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/expo-asset/src/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Platform } from '@unimodules/core';
import { getAssetByID } from './AssetRegistry';
import * as AssetSources from './AssetSources';
import * as AssetUris from './AssetUris';
import { getEmbeddedAssetUri } from './EmbeddedAssets';
import * as ImageAssets from './ImageAssets';
import { getLocalAssetUri } from './LocalAssets';
import { downloadAsync, IS_ENV_WITH_UPDATES_ENABLED } from './PlatformUtils';
import resolveAssetSource from './resolveAssetSource';

Expand Down Expand Up @@ -53,7 +53,7 @@ export class Asset {
}

if (hash) {
this.localUri = getEmbeddedAssetUri(hash, type);
this.localUri = getLocalAssetUri(hash, type);
if (this.localUri) {
this.downloaded = true;
}
Expand Down
28 changes: 0 additions & 28 deletions packages/expo-asset/src/EmbeddedAssets.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/expo-asset/src/EmbeddedAssets.web.ts

This file was deleted.

39 changes: 39 additions & 0 deletions packages/expo-asset/src/LocalAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Constants from 'expo-constants';
import * as FileSystem from 'expo-file-system';

import { getLocalAssets } from './PlatformUtils';

// Fast lookup check if assets are available in the local bundle in managed apps
const bundledAssets = new Set(FileSystem.bundledAssets || []);

// localAssets are provided by the expo-updates module
const localAssets = getLocalAssets();

/**
* Returns the URI of a local asset from its hash, or null if the asset is not available locally
*/
export function getLocalAssetUri(hash: string, type: string | null): string | null {
const localAssetsKey = hash;
const legacyLocalAssetsKey = `${hash}.${type ?? ''}`;

switch (true) {
case localAssetsKey in localAssets: {
return localAssets[localAssetsKey];
}
case legacyLocalAssetsKey in localAssets: {
// legacy updates store assets with an extension
return localAssets[legacyLocalAssetsKey];
}
case !__DEV__: {
// check legacy location in case we're in Expo client/managed workflow
// TODO(eric): remove this once bundledAssets is no longer exported from FileSystem
const assetName = `asset_${hash}${type ? `.${type}` : ''}`;
if (Constants.appOwnership !== 'standalone' || !bundledAssets.has(assetName)) {
return null;
}
return `${FileSystem.bundleDirectory}${assetName}`;
}
default:
return null;
}
}
4 changes: 4 additions & 0 deletions packages/expo-asset/src/LocalAssets.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getLocalAssetUri(hash: string, type: string | null): string | null {
// noop on web
return null;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as EmbeddedAssets from '../EmbeddedAssets';
import { getLocalAssetUri } from '../LocalAssets';

jest.mock('expo-constants', () => {
const Constants = jest.requireActual('expo-constants');
Expand Down Expand Up @@ -31,15 +31,17 @@ jest.mock('@unimodules/core', () => {
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/asset_test3.png',
'test4.':
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/asset_test4',
'file-hash':
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/file-hash',
},
},
},
};
});

describe('getEmbeddedAssetUri', () => {
describe('getLocalAssetUri', () => {
it(`returns null in __DEV__`, () => {
const uri = EmbeddedAssets.getEmbeddedAssetUri('hash', 'png');
const uri = getLocalAssetUri('hash', 'png');
expect(__DEV__).toBeTruthy();
expect(uri).toBeNull();
});
Expand All @@ -56,38 +58,47 @@ describe('getEmbeddedAssetUri', () => {
});

it(`returns a URI when an asset is bundled`, () => {
const uri = EmbeddedAssets.getEmbeddedAssetUri('test3', 'png');
const uri = getLocalAssetUri('test3', 'png');
expect(uri).toBe(
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/asset_test3.png'
);
});

it(`returns a URI without a file extension if there is one, reqardless of whether an extension is specified`, () => {
const uri = getLocalAssetUri('file-hash', 'jpg');
const uri2 = getLocalAssetUri('file-hash', null);
expect(uri).toBe(uri2);
expect(uri2).toBe(
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/file-hash'
);
});

it(`returns a URI when an asset is bundled with a hash and no file extension`, () => {
const uri = EmbeddedAssets.getEmbeddedAssetUri('test4', null);
const uri = getLocalAssetUri('test4', null);
expect(uri).toBe(
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/asset_test4'
);
});

it(`returns a URI when an asset is bundled in the legacy location`, () => {
const uri = EmbeddedAssets.getEmbeddedAssetUri('test1', null);
const uri = getLocalAssetUri('test1', null);
expect(uri).toBe(
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/asset_test1'
);
});

it(`returns a URI for an asset with the given hash and file extension`, () => {
const uri = EmbeddedAssets.getEmbeddedAssetUri('test2', 'png');
const uri = getLocalAssetUri('test2', 'png');
expect(uri).toBe(
'file:///Containers/Bundle/Application/00A4A2F0-E268-40DC-A1AD-2F3A90BA2340/Expo.app/asset_test2.png'
);
});

it(`returns null when no asset exists with the given hash and file extension`, () => {
const uri1 = EmbeddedAssets.getEmbeddedAssetUri('test1', 'png');
const uri1 = getLocalAssetUri('test1', 'png');
expect(uri1).toBeNull();

const uri2 = EmbeddedAssets.getEmbeddedAssetUri('test2', 'xxx');
const uri2 = getLocalAssetUri('test2', 'xxx');
expect(uri2).toBeNull();
});
});
Expand Down

0 comments on commit 83f8551

Please sign in to comment.