Skip to content

Commit

Permalink
SINGLE_FILE option is now selectable depending on the context of node…
Browse files Browse the repository at this point in the history
… or browser during building
  • Loading branch information
fuzzc0re committed Sep 29, 2023
1 parent e73ffde commit c3008c3
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 197 deletions.
3 changes: 2 additions & 1 deletion examples/browser/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<!DOCTYPE html>
<html>
<body>
<script src="https://cdn.jsdelivr.net/npm/@deliberative/crypto@latest/lib/index.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/@deliberative/crypto@latest/lib/index.min.js"></script> -->
<script src="../../lib/index.min.js"></script>
<script type="text/javascript">
const uint8ToHex = (array) => {
return array.reduce(
Expand Down
292 changes: 143 additions & 149 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@deliberative/crypto",
"description": "Libsodium, Merkle trees, Shamir secret sharing wasm module for nodejs and the browser.",
"version": "1.0.3",
"version": "1.0.4",
"repository": {
"type": "git",
"url": "https://github.com/deliberative/crypto.git"
Expand Down Expand Up @@ -48,8 +48,9 @@
"import": "./lib/index.node.mjs"
},
"browser": {
"import": "./lib/index.mjs",
"require": "./lib/index.cjs"
"module": "./lib/index.mjs",
"require": "./lib/index.cjs",
"import": "./lib/index.mjs"
},
"default": "./lib/index.min.js"
},
Expand All @@ -66,9 +67,12 @@
"postvalgrind": "./examples/c/dcrypto_methods.o && rm -rf examples/c/dcrypto_methods.o",
"code": "cross-env NODE_ENV=production rollup --config rollup.config.ts --configPlugin typescript",
"code:debug": "cross-env NODE_ENV=development rollup --config rollup.config.ts --configPlugin typescript",
"build": "npm-run-all -s wasm code",
"build:browser": "cross-env NODE_OR_BROWSER=browser npm-run-all -s wasm code",
"build:node": "cross-env NODE_OR_BROWSER=node npm-run-all -s wasm code",
"build": "npm-run-all -s build:browser build:node",
"postbuild": "node scripts/post.js",
"build:debug": "npm-run-all -s wasm:debug code:debug postbuild",
"build:browser:debug": "cross-env NODE_OR_BROWSER=browser npm-run-all -s wasm:debug code:debug postbuild",
"build:node:debug": "cross-env NODE_OR_BROWSER=node npm-run-all -s wasm:debug code:debug postbuild",
"jest": "jest --config jest.config.json",
"test": "npm-run-all -s wasm jest",
"bench1": "node benchmarks/asymmetric/index.js",
Expand All @@ -77,7 +81,7 @@
"prebench": "npm run build",
"bench": "npm-run-all -s bench3 bench2 bench1",
"preformat": "clang-format-all src/",
"format": "prettier --write src examples/js __tests__",
"format": "prettier --write rollup.config.ts src examples/js __tests__",
"postformat": "clang-format-all examples/",
"lint": "eslint --ext .js,.ts src __tests__",
"prepublishOnly": "npm-run-all -s lint test build",
Expand Down
82 changes: 48 additions & 34 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import url from "@rollup/plugin-url";
// import { terser } from "rollup-plugin-terser";
import analyzer from "rollup-plugin-analyzer";
import copy from "rollup-plugin-copy";
import fs from "node:fs";

import pkg from "./package.json" assert { type: "json" };

const production = process.env.NODE_ENV === "production";
const browser = process.env.NODE_OR_BROWSER === "browser";
const dir = "lib";
const input = "src/index.ts";

Expand Down Expand Up @@ -52,14 +54,15 @@ const plugins = [
outDir: `${dir}`,
}),

copy({
targets: [
{
src: "src/c/build/dcryptoMethodsModule.wasm",
dest: `${dir}`,
},
],
}),
fs.existsSync("src/c/build/dcryptoMethodsModule.wasm") &&
copy({
targets: [
{
src: "src/c/build/dcryptoMethodsModule.wasm",
dest: `${dir}`,
},
],
}),

analyzer(),
];
Expand All @@ -85,31 +88,42 @@ export default [
input,
plugins,
external: ["module"],
output: [
{
file: pkg.module,
format: "es",
esModule: true,
interop: "esModule",
exports: "named",
sourcemap: true,
},
{
file: pkg.module.replace(".mjs", ".node.mjs"),
format: "es",
esModule: true,
interop: "esModule",
exports: "named",
sourcemap: true,
},
{
file: pkg.main,
format: "cjs",
esModule: false,
interop: "defaultOnly",
exports: "default",
sourcemap: true,
},
],
output: browser
? [
{
file: pkg.module,
format: "es",
esModule: true,
interop: "esModule",
exports: "named",
sourcemap: true,
},
{
file: pkg.main,
format: "cjs",
esModule: false,
interop: "defaultOnly",
exports: "default",
sourcemap: true,
},
]
: [
{
file: pkg.module.replace(".mjs", ".node.mjs"),
format: "es",
esModule: true,
interop: "esModule",
exports: "named",
sourcemap: true,
},
{
file: pkg.main.replace(".cjs", ".node.cjs"),
format: "cjs",
esModule: false,
interop: "defaultOnly",
exports: "default",
sourcemap: true,
},
],
},
];
4 changes: 1 addition & 3 deletions scripts/compileMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ const testing =
-s FILESYSTEM=0 \
-s ASSERTIONS=0 \
-s INVOKE_RUN=0 \
-s USE_CLOSURE_COMPILER=1 \
`
: `\
-O0 \
-Og \
-g3 \
--profiling \
-gsource-map \
-fsanitize=undefined \
-s ASSERTIONS=2 \
-s RUNTIME_LOGGING=1 \
-s RUNTIME_DEBUG=1 \
-s SAFE_HEAP=2 \
-s STACK_OVERFLOW_CHECK=2 \
Expand Down
13 changes: 10 additions & 3 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ const withJS = ` \
-s MODULARIZE=1 \
-s MAIN_MODULE=2 \
-s POLYFILL=0 \
-s SINGLE_FILE=1 \
-s BUILD_AS_WORKER=1 \
`;

const browser = process.env.NODE_OR_BROWSER === "browser" ? ` \
-s SINGLE_FILE=1 \
-s ENVIRONMENT=\'webview\' \
` : "";

const memory = `\
-s IMPORTED_MEMORY=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s INITIAL_MEMORY=256kb \
-s STACK_SIZE=128kb \
-s INITIAL_MEMORY=${process.env.NODE_ENV === "production" ? "256kb" : "10mb" } \
-s STACK_SIZE=${process.env.NODE_ENV === "production" ? "128kb" : "5mb" } \
-s MALLOC=emmalloc-memvalidate \
`;

const emcc = `\
Expand All @@ -63,6 +69,7 @@ emcc \
-s STRICT \
${memory} \
${withJS} \
${browser} \
-s NODEJS_CATCH_EXIT=0 \
-s NODEJS_CATCH_REJECTION=0 \
`;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/memoryLenToPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const MIN_PAGES =
process.env.NODE_ENV === "production"
? 4 // 256kb
: 160; // 10mb

/**
* Webassembly Memory is separated into 64kb contiguous memory "pages".
* This function takes memory length in bytes and converts it to pages.
Expand All @@ -22,7 +27,7 @@ const memoryLenToPages = (
minPages?: number,
maxPages?: number,
): number => {
minPages = minPages || 4; // 256kb // 48 = 3mb // 256 = 16mb // 6 = 384kb
minPages = minPages || MIN_PAGES;
maxPages = maxPages || 32768; // 2gb // 16384 = 1gb
const pageSize = 64 * 1024;
const ceil = Math.ceil(memoryLen / pageSize);
Expand Down

0 comments on commit c3008c3

Please sign in to comment.