PeculiarVentures / webcrypto-liner

webcrypto-liner is a polyfill that let's down-level User Agents (like IE/Edge) use libraries that depend on WebCrypto. (Keywords: Javascript, WebCrypto, Shim, Polyfill)
MIT License
148 stars 26 forks source link

Method scope 'this' undefined when destructuring methods from SubtleCrypto instance #84

Open TJKoury opened 3 years ago

TJKoury commented 3 years ago

In Node >=15.x, the WebCrypto API allows destructuring of SubtleCrypto methods:

import { webcrypto } from 'crypto';
...
let { importKey, exportKey } = webcrypto.subtle;
const keys = await importKey(...);

However, doing this using the liner throws errors (methods and properties called on 'undefined').

A quick workaround is to bind this when destructuring:

import { crypto } from "webcrypto-liner";
...
let { importKey } = crypto.subtle;
importKey = importKey.bind(crypto.subtle);

Bad code smell though, and side-effects unknown.