Skip to content

Commit

Permalink
Remove Bookshop CDN dependency and use a site-local gzipped WASM file
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Nov 29, 2022
1 parent cf33c35 commit 17a40f5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ jobs:
env:
GOOS: js
GOARCH: wasm
- name: Gzip Hugo wasm module
run: gzip --keep hugo_renderer.wasm
working-directory: ${{ env.cwd }}/engines/hugo-engine/hugo-renderer
- run: yarn run itest
working-directory: ${{ env.cwd }}/integration-tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ coverage
javascript-modules/integration-tests/support/starters/jekyll/*.lock
javascript-modules/integration-tests/.bookshop-tmp-test-dir/*
hugo_renderer.wasm
hugo_renderer.wasm.gz
.env
3 changes: 3 additions & 0 deletions javascript-modules/engines/hugo-engine/hugo-renderer/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ if [ ! -f $OUTPUTFILENAME ]; then
fi
echo "Done. Repo Hugo WASM available at hugo_renderer.wasm"

gzip --keep $OUTPUTFILENAME
echo "Repo Hugo WASM also available at hugo_renderer.wasm.gz"

# If we're just in a test run we don't want to touch the CDN repo
if [[ -z "${PUBLISH_BOOKSHOP_CDN}" ]]; then
echo "Not publishing to CDN."
Expand Down
3 changes: 2 additions & 1 deletion javascript-modules/engines/hugo-engine/lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const esbuildConfigFn = (esbuildConfig, options) => {
esbuildConfig.loader = {
...esbuildConfig.loader,
".hugo.html": "text",
".wasm": options?.hosted ? "file" : "binary"
".wasm": options?.hosted ? "file" : "binary",
".wasm.gz": "file"
};

const wasm_exec_banner = fs.readFileSync(path.join(__dirname, "../hugo-renderer/wasm_exec.js"));
Expand Down
40 changes: 9 additions & 31 deletions javascript-modules/engines/hugo-engine/lib/engine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hugoWasm from "../hugo-renderer/hugo_renderer.wasm";
import compressedHugoWasm from "../hugo-renderer/hugo_renderer.wasm.gz";
import { gunzipSync } from 'fflate';
import translateTextTemplate from './translateTextTemplate.js';
import { IdentifierParser } from './hugoIdentifierParser.js';
Expand Down Expand Up @@ -32,16 +33,11 @@ export class Engine {
}

async initializeHugo() {
const useLocalHugo = window.CloudCannon?.isMocked || /localhost|127\.0\.0\.1/i.test(window.location.host);
// When this script is run locally, the hugo wasm is loaded as binary rather than output as a file.
if (hugoWasm?.constructor === Uint8Array) {
await this.initializeInlineHugo();
} else {
if (useLocalHugo) {
await this.initializeLocalHugo();
} else {
await this.initializeRemoteCompressedHugo();
}
await this.initializeLocalCompressedHugo();
}

// TODO: Tidy
Expand All @@ -55,38 +51,20 @@ export class Engine {
const success = window.loadHugoBookshopPartials(JSON.stringify(mappedFiles));
}

async initializeRemoteCompressedHugo() {
async initializeLocalCompressedHugo() {
try {
const go = new Go();
const remoteWasmOrigin = `https://cdn.bookshop.build/hugo/hugo_renderer_${version}.wasm.gz`;
const remoteResponse = await fetch(remoteWasmOrigin);
const remoteBuffer = await remoteResponse.arrayBuffer();
const renderer = gunzipSync(new Uint8Array(remoteBuffer));
const compressedWasmOrigin = this.origin.replace(/\/[^\.\/]+\.(min\.)?js/, compressedHugoWasm.replace(/^\./, ''));
const compressedResponse = await fetch(compressedWasmOrigin);
const compressedBuffer = await compressedResponse.arrayBuffer();
const renderer = gunzipSync(new Uint8Array(compressedBuffer));

// Check the magic word at the start of the buffer as a way to verify the CDN download worked.
const isWasm = ([...renderer.slice(0, 4)]).map(g => g.toString(16).padStart(2, '0')).join('') === "0061736d";
if (!isWasm) throw "Not WASM";

const remoteResult = await WebAssembly.instantiate(renderer, go.importObject);
go.run(remoteResult.instance);
} catch (e) {
await this.initializeRemoteHugo();
}
}

async initializeRemoteHugo() {
try {
const go = new Go();
const remoteWasmOrigin = `https://cdn.bookshop.build/hugo/hugo_renderer_${version}.wasm`;
const remoteResponse = await fetch(remoteWasmOrigin);
const remoteBuffer = await remoteResponse.arrayBuffer();

// Check the magic word at the start of the buffer as a way to verify the CDN download worked.
const isWasm = ([...new Uint8Array(remoteBuffer, 0, 4)]).map(g => g.toString(16).padStart(2, '0')).join('') === "0061736d";
if (!isWasm) throw "Not WASM";

const remoteResult = await WebAssembly.instantiate(remoteBuffer, go.importObject);
go.run(remoteResult.instance);
const compressedResult = await WebAssembly.instantiate(renderer, go.importObject);
go.run(compressedResult.instance);
} catch (e) {
await this.initializeLocalHugo();
}
Expand Down
5 changes: 3 additions & 2 deletions javascript-modules/engines/hugo-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"*.js",
"lib/*.js",
"!**/*.test.js",
"hugo-renderer/hugo_renderer.wasm"
"hugo-renderer/hugo_renderer.wasm",
"hugo-renderer/hugo_renderer.wasm.gz"
],
"scripts": {
"test": "nyc ava -v"
Expand All @@ -37,4 +38,4 @@
"engines": {
"node": ">=14.16"
}
}
}

0 comments on commit 17a40f5

Please sign in to comment.