Skip to content

Commit 84249c1

Browse files
authored
file_packager: Set data package_uuid to sha256 hash of preloaded contents (#16807)
1 parent c7b21c3 commit 84249c1

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ See docs/process.md for more on how version tagging works.
3333
are now exposed to native code and can be used to keep the runtime alive
3434
without immediately unwinding the event loop (as
3535
`emscripten_exit_with_live_runtime()` does). (#17160)
36+
- The file packager option `--use-preload-cache` now only invalidates the
37+
cache if the data contents has changed. (#16807)
3638

3739
3.1.13 - 06/02/2022
3840
-------------------

tests/test_other.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import time
2222
import tempfile
2323
import unittest
24-
import uuid
2524
from pathlib import Path
2625
from subprocess import PIPE, STDOUT
2726

@@ -2688,15 +2687,15 @@ def clean(txt):
26882687
# verify '--separate-metadata' option produces separate metadata file
26892688
os.chdir('..')
26902689

2691-
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata'])
2690+
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata', '--use-preload-cache'])
26922691
self.assertExists('immutable.js.metadata')
26932692
# verify js output JS file is not touched when the metadata is separated
26942693
orig_timestamp = os.path.getmtime('immutable.js')
26952694
orig_content = read_file('immutable.js')
26962695
# ensure some time passes before running the packager again so that if it does touch the
26972696
# js file it will end up with the different timestamp.
26982697
time.sleep(1.0)
2699-
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata'])
2698+
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata', '--use-preload-cache'])
27002699
# assert both file content and timestamp are the same as reference copy
27012700
self.assertTextDataIdentical(orig_content, read_file('immutable.js'))
27022701
self.assertEqual(orig_timestamp, os.path.getmtime('immutable.js'))
@@ -2707,8 +2706,7 @@ def clean(txt):
27072706
assert metadata['files'][1]['start'] == len('data1') and metadata['files'][1]['end'] == len('data1') + len('data2') and metadata['files'][1]['filename'] == '/subdir/data2.txt'
27082707
assert metadata['remote_package_size'] == len('data1') + len('data2')
27092708

2710-
# can only assert the uuid format is correct, the uuid's value is expected to differ in between invocation
2711-
uuid.UUID(metadata['package_uuid'], version=4)
2709+
self.assertEqual(metadata['package_uuid'], 'sha256-53ddc03623f867c7d4a631ded19c2613f2cb61d47b6aa214f47ff3cc15445bcd')
27122710

27132711
def test_file_packager_unicode(self):
27142712
unicode_name = 'unicode…☃'

tools/file_packager.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@
6464
import base64
6565
import ctypes
6666
import fnmatch
67+
import hashlib
6768
import json
6869
import os
6970
import posixpath
7071
import random
7172
import shutil
7273
import sys
73-
import uuid
7474
from subprocess import PIPE
7575
from textwrap import dedent
7676

@@ -600,7 +600,6 @@ def generate_js(data_target, data_files, metadata):
600600
start += len(curr)
601601
data.write(curr)
602602

603-
# TODO: sha256sum on data_target
604603
if start > 256 * 1024 * 1024:
605604
err('warning: file packager is creating an asset bundle of %d MB. '
606605
'this is very large, and browsers might have trouble loading it. '
@@ -735,7 +734,6 @@ def generate_js(data_target, data_files, metadata):
735734
Module['LZ4'].loadPackage({ 'metadata': metadata, 'compressedData': compressedData }, %s);
736735
Module['removeRunDependency']('datafile_%s');''' % (meta, "true" if options.use_preload_plugins else "false", js_manipulation.escape_for_js_string(data_target))
737736

738-
package_uuid = uuid.uuid4()
739737
package_name = data_target
740738
remote_package_size = os.path.getsize(package_name)
741739
remote_package_name = os.path.basename(package_name)
@@ -755,13 +753,17 @@ def generate_js(data_target, data_files, metadata):
755753
}
756754
var REMOTE_PACKAGE_NAME = Module['locateFile'] ? Module['locateFile'](REMOTE_PACKAGE_BASE, '') : REMOTE_PACKAGE_BASE;\n''' % (js_manipulation.escape_for_js_string(data_target), js_manipulation.escape_for_js_string(remote_package_name))
757755
metadata['remote_package_size'] = remote_package_size
758-
metadata['package_uuid'] = str(package_uuid)
759-
ret += '''
760-
var REMOTE_PACKAGE_SIZE = metadata['remote_package_size'];
761-
var PACKAGE_UUID = metadata['package_uuid'];\n'''
756+
ret += '''var REMOTE_PACKAGE_SIZE = metadata['remote_package_size'];\n'''
762757

763758
if options.use_preload_cache:
759+
# Set the id to a hash of the preloaded data, so that caches survive over multiple builds
760+
# if the data has not changed.
761+
data = utils.read_binary(data_target)
762+
package_uuid = 'sha256-' + hashlib.sha256(data).hexdigest()
763+
metadata['package_uuid'] = str(package_uuid)
764+
764765
code += r'''
766+
var PACKAGE_UUID = metadata['package_uuid'];
765767
var indexedDB;
766768
if (typeof window === 'object') {
767769
indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

0 commit comments

Comments
 (0)