Code Monkey home page Code Monkey logo

web-encoding's Introduction

web-encoding

Node.js CI package downloads styled with prettier

This package provides TextEncoder and TextDecoder Encoding Standard APIs in a universal package. In the browsers it just exposes existing globals, in nodejs it exposes globals in newer node versions and ones from util module in older versions, and in the React Native environments it exposes these from the @zxing/text-encoding polyfill (installed as an optional dependency).

Package also works as ES module and CommonJS module.

Usage

import { TextEncoder, TextDecoder } from "web-encoding"

Install

npm install web-encoding

web-encoding's People

Contributors

achingbrain avatar auhau avatar gozala avatar hugomrdias avatar msftenhanceprovenance avatar somay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

web-encoding's Issues

jest --env=jsdom TypeError: TextDecoder is not a constructor

jest with --env=jsdom fails with TypeError: TextDecoder is not a constructor

@Gozala did not dig into it, but I assume this is a bug: it is fair to expect people use js-multiaddr (which depends on web-encoding) in apps tested with jest.

That's what we do in ipfs-webui – see below.

How to reproduce

Having below in multibase/src/util.js causes jest-based tests to fail in ipfs-webui:

const { TextEncoder, TextDecoder } = require('web-encoding')
const textDecoder = new TextDecoder()     
 FAIL  src/bundles/analytics.test.js
  ● Test suite failed to run

    TypeError: TextDecoder is not a constructor

    > 1 | import multiaddr from 'multiaddr'
        | ^
      2 | // @ts-ignore
      3 | import HttpClient from 'ipfs-http-client'
      4 | // @ts-ignore

      at Object.<anonymous> (node_modules/uint8arrays/node_modules/multibase/src/util.js:6:21)
      at Object.<anonymous> (node_modules/uint8arrays/node_modules/multibase/src/base.js:4:24)
      at Object.<anonymous> (node_modules/uint8arrays/node_modules/multibase/src/constants.js:5:14)
      at Object.<anonymous> (node_modules/uint8arrays/to-string.js:3:19)

Problems with jest in env=node and env=jsdom

As per multiformats/js-multibase#66 (quoting below):

Describe the bug
When running the test in jest (with either env=jsdom or env=node setting) the TextDecoder or TextEncoder is not a constructor error makes it impossible to write unit tests using this module

To Reproduce
This simple test and code fails to run in jest
code:
https://github.com/Uniswap/uniswap-interface/blob/0aabf4856e6e90041caf4233db4b7defb01574a2/src/utils/contenthashToUri.ts
test:
https://github.com/Uniswap/uniswap-interface/blob/0aabf4856e6e90041caf4233db4b7defb01574a2/src/utils/contenthashToUri.test.skip.ts

error:
TypeError: TextDecoder is not a constructor


    TypeError: TextDecoder is not a constructor

    > 1 | import CID from 'cids'
        | ^
      2 | import { getCodec, rmPrefix } from 'multicodec'
      3 | import { decode, toB58String } from 'multihashes'
      4 |

      at Object.<anonymous> (node_modules/multibase/src/util.js:6:21)
      at Object.<anonymous> (node_modules/multibase/src/base.js:4:24)
      at Object.<anonymous> (node_modules/multibase/src/constants.js:5:14)
      at Object.<anonymous> (node_modules/multibase/src/index.js:9:19)
      at Object.<anonymous> (node_modules/multihashes/src/index.js:10:19)
      at Object.<anonymous> (node_modules/cids/src/index.js:3:12)
      at Object.<anonymous> (src/utils/contenthashToUri.ts:1:1)
      at Object.<anonymous> (src/utils/contenthashToUri.test.ts:1:1)

Expected behavior
TextDecoder from the environment (specifically, global for node and window for jsdom jest setting) is successfully loaded

Screenshots
image

Desktop (please complete the following information):

  • OS: MacOS

Latest version is broken in browserify?

Seeing this in CI:

example-browser-browserify: > [email protected] test /home/travis/build/ipfs/js-ipfs/examples/browser-browserify
example-browser-browserify: > test-ipfs-example
example-browser-browserify: > [email protected] bundle /home/travis/build/ipfs/js-ipfs/examples/browser-browserify
example-browser-browserify: > browserify src/index.js > public/bundle.js
example-browser-browserify: /home/travis/build/ipfs/js-ipfs/node_modules/web-encoding/src/lib.js:7
example-browser-browserify: export { Encoder as TextEncoder, Decoder as TextDecoder }
example-browser-browserify: ^
example-browser-browserify: ParseError: 'import' and 'export' may appear only with 'sourceType: module'

TS compilation issue

When I run tsc in achingbrain's uint8arrays repo I get:

node_modules/web-encoding/src/lib.d.ts:1:10 - error TS2661: Cannot export 'TextEncoder'. Only local declarations can be exported from a module.

1 export { TextEncoder, TextDecoder }
           ~~~~~~~~~~~

node_modules/web-encoding/src/lib.d.ts:1:23 - error TS2661: Cannot export 'TextDecoder'. Only local declarations can be exported from a module.

1 export { TextEncoder, TextDecoder }
                        ~~~~~~~~~~~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.