diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-01-23 17:52:57 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-01-23 17:52:57 -0800 |
commit | c99a28dedf06cd1114f0b4f782b2e1e1d6c10051 (patch) | |
tree | 3932b2651304d777a3170dcf259ed73e2d8deded | |
parent | 91de0c9e7848d370d3113592c6d22b5b22c4ef09 (diff) |
allow adding a custom prefix to file package downloads, so they can happen from a different url; fixes #2049
-rw-r--r-- | tests/test_browser.py | 39 | ||||
-rw-r--r-- | tools/file_packager.py | 8 |
2 files changed, 43 insertions, 4 deletions
diff --git a/tests/test_browser.py b/tests/test_browser.py index 02bcecbd..2c9407db 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -580,6 +580,45 @@ If manually bisecting: shutil.rmtree(os.path.join(self.get_dir(), 'subdirr')) self.run_browser('page.html', 'You should see two cool numbers', '/report_result?1') + def test_custom_file_package_url(self): + # a few files inside a directory + self.clear() + os.makedirs(os.path.join(self.get_dir(), 'subdirr')); + os.makedirs(os.path.join(self.get_dir(), 'cdn')); + open(os.path.join(self.get_dir(), 'subdirr', 'data1.txt'), 'w').write('''1214141516171819''') + # change the file package base dir to look in a "cdn". note that normally you would add this in your own custom html file etc., and not by + # modifying the existing shell in this manner + open(self.in_dir('shell.html'), 'w').write(open(path_from_root('src', 'shell.html')).read().replace('var Module = {', 'var Module = { filePackageURL: "cdn/", ')) + open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(self.with_report_result(r''' + #include <stdio.h> + #include <string.h> + #include <emscripten.h> + int main() { + char buf[17]; + + FILE *f = fopen("subdirr/data1.txt", "r"); + fread(buf, 1, 16, f); + buf[16] = 0; + fclose(f); + printf("|%s|\n", buf); + int result = !strcmp("1214141516171819", buf); + + REPORT_RESULT(); + return 0; + } + ''')) + + def test(): + Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--shell-file', 'shell.html', '--preload-file', 'subdirr/data1.txt', '-o', 'test.html']).communicate() + shutil.move('test.data', os.path.join('cdn', 'test.data')) + self.run_browser('test.html', '', '/report_result?1') + + test() + + # TODO: CORS, test using a full url for filePackageURL + #open(self.in_dir('shell.html'), 'w').write(open(path_from_root('src', 'shell.html')).read().replace('var Module = {', 'var Module = { filePackageURL: "http:/localhost:8888/cdn/", ')) + #test() + def test_compressed_file(self): open(os.path.join(self.get_dir(), 'datafile.txt'), 'w').write('compress this please' + (2000*'.')) open(os.path.join(self.get_dir(), 'datafile2.txt'), 'w').write('moar' + (100*'!')) diff --git a/tools/file_packager.py b/tools/file_packager.py index 8b65b219..6c60db58 100644 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -462,7 +462,7 @@ if has_preloaded: package_uuid = uuid.uuid4(); remote_package_name = os.path.basename(Compression.compressed_name(data_target) if Compression.on else data_target) - code += r''' + ret += r''' var PACKAGE_PATH; if (typeof window === 'object') { PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/'); @@ -471,7 +471,7 @@ if has_preloaded: PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/'); } var PACKAGE_NAME = '%s'; - var REMOTE_PACKAGE_NAME = '%s'; + var REMOTE_PACKAGE_NAME = (Module['filePackageURL'] || '') + '%s'; var PACKAGE_UUID = '%s'; ''' % (data_target, remote_package_name, package_uuid) @@ -666,7 +666,7 @@ if has_preloaded: # Only tricky bit is the fetch is async, but also when runWithFS is called is async, so we handle both orderings. ret += r''' var fetched = null, fetchedCallback = null; - fetchRemotePackage('%s', function(data) { + fetchRemotePackage(REMOTE_PACKAGE_NAME, function(data) { if (fetchedCallback) { fetchedCallback(data); fetchedCallback = null; @@ -674,7 +674,7 @@ if has_preloaded: fetched = data; } }, handleError); - ''' % os.path.basename(Compression.compressed_name(data_target) if Compression.on else data_target) + ''' code += r''' Module.preloadResults[PACKAGE_NAME] = {fromCache: false}; |