-
Notifications
You must be signed in to change notification settings - Fork 20
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
Can't use with Angular #18
Comments
No idea I'm afraid, it sounds like this is an issue with either your deployment setup or your bundler configuration. Your bundler appears to be keeping the reference to the wasm file to load it at runtime (as it should) but if that error is appearing in the browser then it's preserving it with a For this to work in browsers, you need to:
It's hard to know every possible configuration I'm afraid! I suspect this isn't really an issue related to this project though - you'll have the same issue with any wasm-based library. If it helps, you can see the Webpack config (for Webpack v5) that this project uses during webpack browser testing here: brotli-wasm/karma-webpack.conf.js Lines 26 to 56 in 2ecde28
|
Hi, thanks for taking a look, that's understandable. I tried a bunch of stuff to get it to bundle with my app but I couldn't figure out what was wrong. I don't really want to bloat with extra bundling config as I'd have to add extra packages etc. so I've switched to brotli-dec-wasm as that worked without any issues. Thanks again. |
@daverickdunn , hi, does |
As the error comes from the web variant ( If you are looking for a quick and easy solution, you can just serve the WASM file on your own (by making the bundler copy it to the output dir, or even manually) and pass the URL string to the init function ( In import { init } from 'brotli-dec-wasm'
const wasmUrl = '...'
const brotli = await init(wasmUrl) Or refer to the asset.md document. PS: I am the author of And @pimterry , I are recently working on another kind of entry for the web environment of these packages with less "side-effects". |
Note that I'm encoding the brotli buffer as base64, you may have it encoded as something else. import { Injectable } from '@angular/core';
import { catchError, map, ReplaySubject, throwError } from 'rxjs';
function Base64ToArrayBuffer(base64: string) {
const binary_string = atob(base64);
const len = binary_string.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
}
@Injectable({
providedIn: 'root'
})
export class BrotliService {
public brotli = new ReplaySubject<any>();
private decoder = new TextDecoder();
constructor() {
this.init();
}
private async init() {
const brotli = await import('brotli-dec-wasm');
this.brotli.next(brotli)
}
public decompress<T = any>(data: string) {
return this.brotli.pipe(
map(brotli => {
const buff_array = Base64ToArrayBuffer(data); // base64 string to buffer (compressed)
const decompressed = brotli.brotliDec(buff_array); // compressed buffer to uncompressed buffer
const decoded = this.decoder.decode(decompressed); // uncompressed buffer to utf-8 string
return JSON.parse(decoded) as T;
}),
catchError(error => {
console.error(error)
return throwError(() => new Error(error))
})
)
}
} |
I'm trying to load this with an Angular app. TS is happy and the app compiles fine, but at runtime I'm getting an error:
zone.js:1518 Not allowed to load local resource: file:///C:/**/**/node_modules/brotli-wasm/pkg.web/brotli_wasm_bg.wasm
Importing as:
import brotliPromise from 'brotli-wasm';
Angular 13
Chrome 107
The text was updated successfully, but these errors were encountered: