Skip to content

Commit

Permalink
Cache the wasm binary
Browse files Browse the repository at this point in the history
  • Loading branch information
yan12125 committed Feb 8, 2025
1 parent 5faf637 commit e72dc88
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/routes/bilibili/manga-update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { Go } from './wasm-exec.js';

Expand Down Expand Up @@ -27,12 +28,18 @@ export const route: Route = {

// Based on https://github.com/SocialSisterYi/bilibili-API-collect/issues/1168#issuecomment-2620749895
async function genReqSign(query, body) {
const wasm_resp = await got('https://s1.hdslb.com/bfs/manga-static/manga-pc/6732b1bf426cfc634293.wasm', {
responseType: 'arrayBuffer',
// Cache the wasm binary as it's quite large (~2MB)
// Here the binary is saved as base64 as the cache stores strings
const wasm_buffer_base64 = await cache.tryGet('bilibili-manga-wasm-20250208', async () => {
const wasm_resp = await got('https://s1.hdslb.com/bfs/manga-static/manga-pc/6732b1bf426cfc634293.wasm', {
responseType: 'arrayBuffer',
});
return Buffer.from(wasm_resp.data).toString('base64');
});
const wasm_buffer = Buffer.from(wasm_buffer_base64, 'base64');

const go = new Go();
const { instance } = await WebAssembly.instantiate(wasm_resp.data, go.importObject);
const { instance } = await WebAssembly.instantiate(wasm_buffer, go.importObject);
go.run(instance);
if (void 0 === globalThis.genReqSign) {
throw new Error('WASM function not available');
Expand Down

0 comments on commit e72dc88

Please sign in to comment.