Skip to content
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

Patch libh3 to include a document undefined check #169

Merged
merged 4 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
All notable changes to this project will be documented in this file. This library adheres to a versioning policy described in [the README](./README.md#versioning). The public API of this library consists of the functions exported in [h3core.js](./lib/h3core.js).

## [Unreleased]
### Fixed
- Patch libh3 bundles to check for `typeof document != "undefined"` before accessing `document`. This allows h3-js to be used in a Web Worker and React Native

## [4.0.1] - 2022-09-19
### Changed
Expand Down
2 changes: 1 addition & 1 deletion out/libh3.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion scripts/update-emscripten.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ emcc -O3 -I ../include *.c -DH3_HAVE_VLA --memory-init-file 0 \
"$@"

for file in *.js ; do
cat ../../../../build/pre.js "$file" > ../../../../out/"$file"
# Patch libh3 bundle to contain a fix to allow h3-js to be imported in a web worker and react-native
# See #117 and #163 for more details.
cat ../../../../build/pre.js "$file" \
| sed 's/if(document.currentScript)/if(typeof document!=="undefined" \&\& document.currentScript)/g' \
> ../../../../out/"$file"
done

popd
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
import './h3core.spec';
import './errors.spec';
import './test-parity';
import './test-build';
34 changes: 34 additions & 0 deletions test/test-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2018-2019 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import test from 'tape';
import fs from 'fs';
import path from 'path';

const LIBH3_PATH = path.join(__dirname, '../out/libh3.js');

const ORIGINAL_DOCUMENT_REGEX = /if\(document.currentScript\)/;
const PATCHED_DOCUMENT_REGEX = /if\(typeof document!=="undefined" && document.currentScript\)/;

test('validateLIBH3HasDocumentUndefinedFix', assert => {
// Validate that the bundle does not contain a bug that prevents h3-js to be imported into webworkers and react native.
// Problem was fixed in a later version of emscripten however there are significant performance regressions when upgrading.
// See #163 and #117 for more details.
const fileContents = fs.readFileSync(LIBH3_PATH, {encoding: 'utf8', flag: 'r'});
assert.doesNotMatch(fileContents, ORIGINAL_DOCUMENT_REGEX, 'bundle was not patched');
assert.match(fileContents, PATCHED_DOCUMENT_REGEX, 'bundle was not patched');
assert.end();
});
4nthonylin marked this conversation as resolved.
Show resolved Hide resolved