haxxnz / nzcp-js

A verification library for COVID-19 Vaccine Passes in New Zealand
MIT License
36 stars 7 forks source link

Get the library to work in React Native #2

Open noway opened 2 years ago

noway commented 2 years ago

Tests should pass in React Native environment

NeedsAdjustment commented 2 years ago

Polyfilling TextEncoding (seems to) work currently, stuck on finding a suitable implementation of BigInt currently (that supports 0x... notation). I'm not sure what other core libraries don't have inbuilt React Native equivalents.

Also (attempting to) ping @meshcollider

NeedsAdjustment commented 2 years ago

Successfully got polyfills working for a react native implementation (soon to be public), based on modified code from this. Imo using a helper package is a bit clunky, but it's a possible way forward.

NeedsAdjustment commented 2 years ago

Specifically:

import 'text-encoding-polyfill'
global.Buffer = global.Buffer || require('buffer').Buffer

if (typeof BigInt === 'undefined') {
    const bi = require('big-integer')

    function myBigInt(value) {
        if (typeof value === 'string') {
            const match = value.match(/^0([xo])([0-9a-f]+)$/i)
            if (match) {
                return bi(match[2], match[1].toLowerCase() === 'x' ? 16 : 8)
            }
        }
        return bi(value)
    }

    global.BigInt = myBigInt

    if (process === undefined) {
        process = require('process')
    } else if (process.nextTick === undefined) {
        process.nextTick = require('process').nextTick
    }
}
rafcontreras commented 2 years ago

We have two React Native apps now using this package, I suggest this closes.

noway commented 2 years ago

Let's write down some documentation, maybe create REACT_NATIVE.md and link it in README.md?