-
Notifications
You must be signed in to change notification settings - Fork 19
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
Usage in Browser with alternative Buffer #16
Comments
So you should be using: SmartBuffer.fromBuffer(theBuffer); What does your setup look like for how you're using the module in the browser? Setting up a simple webpack config with babel-loader and the es2015 preset, it seems to work just fine. import { Buffer } from 'buffer';
import { SmartBuffer } from 'smart-buffer';
const someArray = [1,2,3,4,5,6,7,8,9,0];
const buff = Buffer.from(someArray);
console.log(buff);
const sBuff = SmartBuffer.fromBuffer(buff);
console.log(sBuff); |
Does your example work if you import The following fails for me: const SmartBuffer = require('smart-buffer').SmartBuffer;
const Buffer = require('buffer/').Buffer;
let buff = Buffer.from('hello');
let smartBuff = SmartBuffer.fromBuffer(buff); I am using browserify to bundle everything and then am running as a Chrome App. My tests fail using |
It's definately loading Feross' buffer with my webpack version. From what I can tell from the test failures in your code, the Buffer that SmartBuffer sees is not the same as the Buffer that is being passed in. It's failing an instanceof test, so for some reason whatever global Buffer SmartBuffer is seeing is different. Not sure if it's related to this: #13 (comment) |
Also found this: feross/buffer#40 I might have to use https://github.com/feross/is-buffer for checking if something is a Buffer. Unless you can figure something else out? I'm not sure why this is an issue. |
Man am I ever puzzled. It looks like for whatever reason the
It looks like the version of
Using I'm not sure what to expect of SmartBuffer. This seems more like an artifact of my build that |
So internally Buffer.isBuffer() seems to just use instanceof, so that won't really help. SmartBuffer should just be using whatever the global Buffer is defined as. Since it's working with the webpack setup I put together, I think it definitely is coming down to what Browserify is choosing to do, but I've never really used it. |
So I ran this through browserify: var Buffer = require('buffer').Buffer;
var SmartBuffer = require('smart-buffer').SmartBuffer;
var someArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var buff = Buffer.from(someArray);
console.log(buff);
var sBuff = SmartBuffer.fromBuffer(buff);
console.log(sBuff); And it seems to have worked since the same Buffer (feross/buffer) is being passed to SmartBuffer. https://gist.github.com/JoshGlazebrook/594711741c285131a24e92d441ba0cbb But! If I instead require('buffer/').Buffer above ^, it passes SmartBuffer a different Buffer than the one being used in that file. Here is a diff between the two: |
Interesting.... I believe what is happening is that Browserify exposes an option, However, looking again at the feross/buffer README, it sounds like I have been using it incorrectly. Several times in the README it says that, if using browserify, either rely on the Literally the first line:
Later:
It sounds like the thing to do is rely on node's Yesterday I swapped out SmartBuffer with my own lightweight implementation for a tiny subset of features and got working what I needed. I just put SmartBuffer back in and removed all Sorry for the confusion, and thanks for the help. |
Ah glad you got it working. Also as a side note, I've been working on v4 for a while now (on and off), which includes a few major breaking changes. Most notably, all of the writeXXX(value, offset) functions now mirror Buffer and overwrite data at the given offset as opposed to inserting. But now I've added .insertXXX(value, offset) functions to behave like how the write functions currently do. You still can just use .writeXXX(value) without an offset and it will still work without specifying an offset (which is why I made this library in the first place), but it just made more sense to keep inserts separate so you can still overwrite data if needed. |
To anyone in the future who got the Webpack 5 Buffer error: Install https://www.npmjs.com/package/buffer Then insert the following code into import { Buffer } from "buffer";
globalThis.Buffer = Buffer; Just to let you know, I am using CRACO. It is a better version of CRA. You can install it from here: https://craco.js.org/ I tried something else but it didn't work for me: DetailsI inserted the following code to file `craco.config.js` in the project: const { addAfterLoader, loaderByName } = require("@craco/craco");
module.exports = async env => {
const remarkGfm = (await import("remark-gfm")).default;
return {
plugins: [
{ plugin: require("./craco-fallback-buffer-plugin.js") },
]
};
}; And created a file // See: https://github.com/WalletConnect/walletconnect-monorepo/issues/748
module.exports = {
overrideWebpackConfig: ({ webpackConfig, cracoConfig, pluginOptions, context: { env, paths } }) => {
/** @type {import('webpack/types'.Configuration)} */
const newConfig = { ...webpackConfig };
newConfig.resolve.fallback = webpackConfig.resolve.fallback || {};
newConfig.resolve.fallback.buffer = require.resolve("buffer/");
return newConfig;
}
}; Hope this can help you :) |
I would love to be able to use this in a browser using Feross's
buffer
module. It has API parity with node'sBuffer
. I thought I could get around this withsmartBuffer.from(new Buffer(1024))
, but I am getting an error:Invalid buffer provided in SmartBufferOptions
.Is there some way to specify an alternative
Buffer
that I am missing?The text was updated successfully, but these errors were encountered: