Open Jack-Works opened 4 years ago
Workaround:
Run the following code before loading webcrypto-liner
const MaybeWorkerGlobalScope = Object.getPrototypeOf(Object.getPrototypeOf(globalThis))
const crypto = Object.getOwnPropertyDescriptor(MaybeWorkerGlobalScope, 'crypto')
// The crypto is defined in [WorkerGlobalScope], let's move it to [DedicatedWorkerGlobalScope]
if (crypto) {
delete MaybeWorkerGlobalScope.crypto
Object.defineProperty(globalThis, 'crypto', crypto)
}
https://github.com/PeculiarVentures/webcrypto-liner/blob/faae2d4b127c1830ceb2fad4736d6177feab3544/src/shim.ts#L11-18
Source code:
In the mainframe,
crypto.*
is defined on the Window interface andself
orwindow
has the prototype of Window therefore everything is fine.In Web Worker,
crypto.*
is defined on the WorkerGlobalScope, butself
is not direct instance ofWorkerGlobalScope
:The prototype chain is:
self
=> DedicatedWorkerGlobalScope => WorkerGlobalScopeTherefore
delete self.crypto
doesn't work sincecrypto
is not defined on theDedicatedWorkerGlobalScope
butWorkerGlobalScope
.After the deletion implicitly failed (
delete
op return true for non-existence property, in this case,crypto
)Line 13
window.crypto = new Crypto();
failed to run becauseWorkerGlobalScope.crypto
is a configurable getter but no setter.