Closed mrbrianevans closed 2 years ago
Thank you so much @mrbrianevans for such a detailed issue. The release of the code has been long pending at our end.
As the first step, will target to release the code in next couple of days so that you can send the PR using TextEncoder, which we will also review in the mean time.
Have tried couple of options including the TextEncoder
; following seems to work:
const bufferFromBase64 = function ( data ) {
const decodedData = atob( data );
var size = decodedData.length;
var bytes = new Uint8Array( size );
for ( let k = 0; k < size; k += 1 ) {
bytes[ k ] = decodedData.charCodeAt( k );
}
return bytes;
}; // bufferFromBase64()
The above function is equivalent to Buffer.from( data, 'base64' )
.
Need to perform detailed testing.
What is your opinion @mrbrianevans?
Thank you very much for publishing source code.
I will try the function you've put above in the source code and see if I can get it to work. If I do, I'll submit a PR.
Okay, submitted a PR #6
Closed by #6
Released the Version 1.3.3 on NPM 👍
In the file https://github.com/winkjs/wink-eng-lite-web-model/blob/master/dist/read-core-model.js#L1 there is usage of the
Buffer
API only available in Nodejs. This causes an error when used in the browser. (mentioned in https://github.com/winkjs/wink-nlp/issues/67). The precise operation causing the error is loading the base64 encoded string (of lexicon and expansions) from the model JSON file into aUInt32Array
. This is done usingBuffer.from()
(which is not natively available in web browsers). In other issues/discussions you have suggested usingbrowserify
to overcome this, however there are many situations where its not practical, where another build tool is already in use for a project. Another solution is toimport from 'https://cdn.skypack.dev/wink-eng-lite-web-model'
, but again this does not fit in with build tools which bundle up vendor dependencies at compile time. This requires loading the module from a CDN for every user, and does not support tree shaking (https://github.com/skypackjs/skypack-cdn/issues/91).I propose that the nodejs specific code be modified to be browser compatible.
I have looked at the code, and tried to come up with a solution using
atob
to decode the base64 string. I have not opened a pull request because as far as I can tell, the published code in github is not the source code, but rather a minified build output and I am unable to find the source code (if it is hosted publicly).Credit to https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string#12713326 for base64 converter.
I have tested this code locally and its not quite right. I think it might be better to use a
TextEncoder
to do the base64 conversion.Please could you consider changing the implementation of this to be directly compatible with browsers without a polyfill