Skip to content

Commit 660dcc0

Browse files
authored
File packager: base64_encode embedded data files (#14526)
Reduces the embedded data size by about 75% It also plays nicer with the acorn optimizer, making it use less RAM when there is embedded data. Fixes #14482
1 parent 3f64c11 commit 660dcc0

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,4 @@ a license to everyone to use it as detailed in LICENSE.)
572572
* Camillo Lugaresi <[email protected]> (copyright owned by Google LLC)
573573
* Chris Craig <[email protected]>
574574
* Le Yao <[email protected]> (copyright owned by Intel Corporation)
575+
* José Cadete <[email protected]>

src/jsifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function JSify(functionsOnly) {
398398
print(preprocess(read('arrayUtils.js')));
399399
}
400400

401-
if (SUPPORT_BASE64_EMBEDDING && !MINIMAL_RUNTIME) {
401+
if ((SUPPORT_BASE64_EMBEDDING || FORCE_FILESYSTEM) && !MINIMAL_RUNTIME) {
402402
print(preprocess(read('base64Utils.js')));
403403
}
404404

tools/file_packager.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
subdir\file, in JS it will be subdir/file. For simplicity we treat the web platform as a *NIX.
5858
"""
5959

60+
import base64
6061
import os
6162
import sys
6263
import shutil
@@ -95,6 +96,11 @@
9596
new_data_files = []
9697

9798

99+
def base64_encode(b):
100+
b64 = base64.b64encode(b)
101+
return b64.decode('ascii')
102+
103+
98104
def has_hidden_attribute(filepath):
99105
"""Win32 code to test whether the given file has the hidden property set."""
100106

@@ -464,18 +470,9 @@ def was_seen(name):
464470
basename = os.path.basename(filename)
465471
if file_['mode'] == 'embed':
466472
# Embed
467-
data = list(bytearray(utils.read_binary(file_['srcpath'])))
468-
code += '''var fileData%d = [];\n''' % counter
469-
if data:
470-
parts = []
471-
chunk_size = 10240
472-
start = 0
473-
while start < len(data):
474-
parts.append('''fileData%d.push.apply(fileData%d, %s);\n'''
475-
% (counter, counter, str(data[start:start + chunk_size])))
476-
start += chunk_size
477-
code += ''.join(parts)
478-
code += ('''Module['FS_createDataFile']('%s', '%s', fileData%d, true, true, false);\n'''
473+
data = base64_encode(utils.read_binary(file_['srcpath']))
474+
code += '''var fileData%d = '%s';\n''' % (counter, data)
475+
code += ('''Module['FS_createDataFile']('%s', '%s', decodeBase64(fileData%d), true, true, false);\n'''
479476
% (dirname, basename, counter))
480477
counter += 1
481478
elif file_['mode'] == 'preload':

0 commit comments

Comments
 (0)