dexie-encrypted icon indicating copy to clipboard operation
dexie-encrypted copied to clipboard

dexie-encryped query data that has not been encrypted before will report an error

Open baobao12356 opened this issue 3 years ago • 1 comments

I use dexie-encryped to query data that has not been encrypted before, and an error will be reported. The error message is Error utf8: invalid string, as shown in the figure below. How to solve it? image

baobao12356 avatar Aug 04 '21 09:08 baobao12356

I ran into the same problem, and here are my learnings:

Observations

This problems seems to originate from the @stablelib/utf8 dependency. In fact, it's quite easily reproducible with:

const { encode } = require('@stablelib/utf8')
encode('‍🤷‍♂️') // This is an emoji with a modifier

Which results in:

Uncaught Error: utf8: invalid string
    at encodedLength

In my limited knowledge of UTF-8, the above string should be encodable.

Temporary workaround

To unblock myself, I swapped the @stablelib/utf8 dependency with utf8 instead. I wrote a simple adapter like so:

import utf8 from 'utf8';

export function decode(arr: Uint8Array): string {
    return utf8.decode(new TextDecoder().decode(arr));
}

export function encode(s: string): Uint8Array {
    return new TextEncoder().encode(utf8.encode(s));
}

Then aliased the dependency in my build system (in my case, I use webpack):

resolve: {
    alias: {
        '@stablelib/utf8': path.join(__dirname, '<path-to>/Utf8Patch'),
    },
},

This is of course less than ideal, but it seems to work for now.

Proposed solution

I would recommend the maintainers of this project to either move from @stablelib/utf8 to utf8 or help fix that issue directly in @stablelib/utf8.

RaffaeleCanale avatar Feb 02 '22 07:02 RaffaeleCanale