Skip to content

Commit 5a0b5a4

Browse files
committed
Fix -sPROXY_TO_WORKER + -sSINGLE_FILE + node
The proxyClient needs to jump through a few more hoops here to load the embedded generated JS file which is encoded as a data URL. This change also fixes the mime type used when we embed JS as a data URL.
1 parent 04a72c2 commit 5a0b5a4

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

src/proxyClient.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,23 @@
1818
#if ENVIRONMENT_MAY_BE_NODE
1919
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string';
2020
if (ENVIRONMENT_IS_NODE) {
21-
global.Worker = require('worker_threads').Worker;
21+
var NodeWorker = require('worker_threads').Worker;
22+
global.Worker = function(url, options) {
23+
// Special handling for `data:` URL argument, to match the behaviour
24+
// of the Web API.
25+
if (typeof url == 'string' && url.startsWith('data:')) {
26+
#if EXPORT_ES6
27+
// worker_threads always assume data URLs are ES6 modules
28+
url = new URL(url);
29+
#else
30+
// For class modules we decode the data URL and use `eval: true`.
31+
url = Buffer.from(url.split(",")[1], 'base64').toString();
32+
options ||= {}
33+
options.eval = true;
34+
#endif
35+
}
36+
return new NodeWorker(url, options);
37+
}
2238
var Module = Module || {}
2339
} else
2440
#endif

src/shell.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ if (ENVIRONMENT_IS_NODE) {
136136
// TODO: Swap all `require()`'s with `import()`'s?
137137
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_WEB
138138
const { createRequire } = await import('module');
139+
let dirname = import.meta.url;
140+
if (dirname.startsWith("data:")) {
141+
dirname = '/';
142+
}
139143
/** @suppress{duplicate} */
140-
var require = createRequire(import.meta.url);
144+
var require = createRequire(dirname);
141145
#endif
142146

143147
#if PTHREADS || WASM_WORKERS
@@ -235,7 +239,10 @@ if (ENVIRONMENT_IS_NODE) {
235239
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
236240
// since there's no way getting the current absolute path of the module when
237241
// support for that is not available.
238-
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
242+
if (!import.meta.url.startsWith('data:')) {
243+
// includes trailing slash
244+
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url));
245+
}
239246
#else
240247
scriptDirectory = __dirname + '/';
241248
#endif

test/test_other.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14707,8 +14707,13 @@ def test_standalone_whole_archive(self):
1470714707
self.emcc_args += ['-sSTANDALONE_WASM', '-pthread', '-Wl,--whole-archive', '-lbulkmemory', '-lstandalonewasm', '-Wl,--no-whole-archive']
1470814708
self.do_runf('hello_world.c')
1470914709

14710-
def test_proxy_to_worker(self):
14711-
self.do_runf('hello_world.c', emcc_args=['--proxy-to-worker'])
14710+
@parameterized({
14711+
'': ([],),
14712+
'_single_file': (['-sSINGLE_FILE'],),
14713+
'_single_file_es6': (['-sSINGLE_FILE', '-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')],),
14714+
})
14715+
def test_proxy_to_worker(self, args):
14716+
self.do_runf('hello_world.c', emcc_args=['--proxy-to-worker'] + args)
1471214717

1471314718
@also_with_standalone_wasm()
1471414719
def test_console_out(self):

tools/link.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,7 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
24972497
var filename = '%s';
24982498
if ((',' + window.location.search.substr(1) + ',').indexOf(',noProxy,') < 0) {
24992499
console.log('running code in a web worker');
2500-
''' % get_subresource_location(proxy_worker_filename)) + worker_js + '''
2500+
''' % get_subresource_location_js(proxy_worker_filename)) + worker_js + '''
25012501
} else {
25022502
console.log('running code on the main thread');
25032503
var fileBytes = tryParseAsDataURI(filename);
@@ -2565,7 +2565,7 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
25652565
// Current browser supports Wasm, proceed with loading the main JS runtime.
25662566
loadMainJs();
25672567
}
2568-
''' % (script.inline, get_subresource_location(wasm_target) + '.js')
2568+
''' % (script.inline, get_subresource_location_js(wasm_target + '.js'))
25692569

25702570
shell = do_replace(shell, '{{{ SCRIPT }}}', script.replacement())
25712571
shell = shell.replace('{{{ SHELL_CSS }}}', utils.read_file(utils.path_from_root('src/shell.css')))
@@ -2641,7 +2641,7 @@ def generate_html(target, options, js_target, target_basename, wasm_target):
26412641
def generate_worker_js(target, js_target, target_basename):
26422642
if settings.SINGLE_FILE:
26432643
# compiler output is embedded as base64 data URL
2644-
proxy_worker_filename = get_subresource_location(js_target)
2644+
proxy_worker_filename = get_subresource_location_js(js_target)
26452645
else:
26462646
# compiler output goes in .worker.js file
26472647
move_file(js_target, shared.replace_suffix(js_target, get_worker_js_suffix()))
@@ -2954,13 +2954,17 @@ def move_file(src, dst):
29542954

29552955

29562956
# Returns the subresource location for run-time access
2957-
def get_subresource_location(path):
2957+
def get_subresource_location(path, mimetype='application/octet-stream'):
29582958
if settings.SINGLE_FILE:
2959-
return 'data:application/octet-stream;base64,' + base64_encode(path)
2959+
return f'data:{mimetype};base64,{base64_encode(path)}'
29602960
else:
29612961
return os.path.basename(path)
29622962

29632963

2964+
def get_subresource_location_js(path):
2965+
return get_subresource_location(path, 'text/javascript')
2966+
2967+
29642968
@ToolchainProfiler.profile()
29652969
def package_files(options, target):
29662970
rtn = []

0 commit comments

Comments
 (0)