Skip to content

Buffer detection #7

@achingbrain

Description

@achingbrain

I'm trying to build this module with webpack in js-ipfs/examples/browser-webpack and the Buffer detection doesn't work:

ERROR in ../../node_modules/cborg/esm/lib/byte-utils.js 1:32-39
Module not found: Error: Can't resolve 'process/browser' in '/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/cborg/esm/lib'
Did you mean 'browser.js'?
BREAKING CHANGE: The request 'process/browser' failed to resolve only because it was resolved as fully specified
(probably because the origin is a '*.mjs' file or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.
 @ ../../node_modules/cborg/esm/lib/encode.js 9:0-40 192:13-18
 @ ../../node_modules/cborg/esm/cborg.js 1:0-41 7:0-12:2
 @ ../../packages/ipfs-core/src/components/pin/pin-manager.js
 @ ../../packages/ipfs-core/src/components/index.js 93:20-48
 @ ../../packages/ipfs-core/src/index.js 27:4-27
 @ ../../packages/ipfs/src/index.js 12:35-55
 @ ./src/components/app.js 11:11-26
 @ ./src/components/index.js 7:10-26

It's good to use buffer because Buffer.allocUnsafe(size) is significantly faster than new Uint8Array(size), but it's bad to use node globals.

Solutions:

  1. Use the process module:
import process from 'process'

export const useBuffer = !process.browser && global.Buffer && typeof global.Buffer.isBuffer === 'function';
  1. Use the buffer module:
import { Buffer } from 'buffer'

export const useBuffer = true;

// replace global.Buffer with Buffer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions