Skip to content

feat(preamble): support node.js getBinary on Electron renderer #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
FROM ojkwon/arch-nvm-node:4032238-node7.9-npm4
FROM ojkwon/arch-nvm-node:7b0d30e-node8.4-npm5.4.1
MAINTAINER OJ Kwon <[email protected]>

# Build time args
ARG BUILD_TARGET=""

# Upgrade system
RUN pacman --noconfirm -Syyu

# Install dependencies
RUN pacman --noconfirm -Syu \
RUN pacman --noconfirm -S \
emscripten \
unzip \
python \
Expand All @@ -16,6 +19,12 @@ RUN pacman --noconfirm -Syu \
# Change subsequent execution shell to bash
SHELL ["/bin/bash", "-l", "-c"]

# Patch preamble.js to support Electron's renderer process with node.js environment
# Refer https://github.com/kripken/emscripten/pull/5577 for detail.
# TODO: remove based on upstream PR status
COPY ./preamble.patch $TMPDIR/
RUN patch /usr/lib/emscripten/src/preamble.js $TMPDIR/preamble.patch

# Initialize emcc
RUN emcc

Expand All @@ -28,7 +37,7 @@ RUN if [[ "${BUILD_TARGET}" == "protobuf" ]]; then \
echo "installing protobuf 3.1 dependency" && \
mkdir $TMPDIR/proto31 && cd $TMPDIR/proto31 && \
curl "https://git.archlinux.org/svntogit/packages.git/plain/trunk/PKGBUILD?h=packages/protobuf&id=fa8b9da391b26b6ace1941e9871a6416db74d67b" > ./PKGBUILD && \
makepkg && sudo pacman --noconfirm -U *.pkg.tar.xz && \
makepkg --skipchecksums && sudo pacman --noconfirm -U *.pkg.tar.xz && \
cd $TMPDIR && git clone https://github.com/kwonoj/protobuf-emscripten && \
cd $TMPDIR/protobuf-emscripten/3.1.0 && \
sh autogen.sh && emconfigure ./configure && emmake make && \
Expand Down
15 changes: 15 additions & 0 deletions preamble.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/src/preamble.js b/src/preamble.js
index 70145dc0f..50b8e73c0 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -2231,7 +2231,9 @@ function integrateWasmJS(Module) {

function getBinaryPromise() {
// if we don't have the binary yet, and have the Fetch api, use that
- if (!Module['wasmBinary'] && typeof fetch === 'function') {
+ // if Module overridded its environment to Node in Electron's renderer process, do not use fetch
+ var electronNodeContext = Module["ENVIRONMENT"] === "NODE" && !!window.process && !!window.require;
+ if (!Module['wasmBinary'] && typeof fetch === 'function' && !electronNodeContext) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
if (!response['ok']) {
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";