question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Value of "this" must be of type SubtleCrypto

See original GitHub issue

When I run this simple code, it throws an error when running from miniflare. The same code works fine running from Cloudflare (or wrangler dev without the --local flag).

Code:

export default {
  async fetch(request, env, ctx) {
    const secret = new TextEncoder().encode('test')
    const key = await crypto.subtle.importKey('raw', secret, { name: 'HMAC', hash: { name: 'SHA-512' } }, false, [
      'sign',
      'verify',
    ])

    return new Response(JSON.stringify(key), { status: 200 })
  },
}

Error:

TypeError: Value of "this" must be of type SubtleCrypto
    at new NodeError (node:internal/errors:377:5)
    at Proxy.importKey (node:internal/crypto/webcrypto:517:30)
    at Object.fetch (/private/Users/leonardo/worker-test/src/index.ts:4:37)
    at /Users/leonardo/worker-test/node_modules/@miniflare/core/src/standards/event.ts:331:19
    at /Users/leonardo/worker-test/node_modules/@miniflare/shared/src/event.ts:29:9
    at ServiceWorkerGlobalScope.<anonymous> (/Users/leonardo/worker-test/node_modules/@miniflare/shared/src/event.ts:77:9)
    at ServiceWorkerGlobalScope.[nodejs.internal.kHybridDispatch] (node:internal/event_target:639:20)
    at ServiceWorkerGlobalScope.dispatchEvent (node:internal/event_target:581:26)
    at ServiceWorkerGlobalScope.dispatchEvent (/Users/leonardo/worker-test/node_modules/@miniflare/shared/src/event.ts:63:18)
    at ServiceWorkerGlobalScope.dispatchEvent (/Users/leonardo/worker-test/node_modules/@miniflare/shared/src/event.ts:87:26)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
mrbbotcommented, May 27, 2022

Hey! I’ve just released version 2.5.0 with this fix. Please let me know if you have any other issues.

1reaction
brandonpittmancommented, Jun 2, 2022

Even on 2.5.0, this error is still occurring for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error on importing a public key 'SubtleCrypto'
Uncaught (in promise) TypeError: Failed to execute 'importKey' on 'SubtleCrypto': The provided value cannot be converted to a sequence.
Read more >
Web Crypto API | Node.js v19.3.0 Documentation
If represented as an <Object>, the object must have a name property whose value is one of the above listed values. ecdsaParams.name #....
Read more >
SubtleCrypto.importKey() - Web APIs | MDN
Raised when keyUsages is empty but the unwrapped key is of type secret or private . TypeError. Raised when trying to use an...
Read more >
SubtleCrypto in web_sys - Rust
The `SubtleCrypto` class. ... pub struct SubtleCrypto { /* private fields */ }. Expand description ... Tests whether the type of this JS...
Read more >
Web Crypto · Cloudflare Workers docs
The Web Crypto API is implemented through the SubtleCrypto interface ... code that relies on Node's Crypto API, you will need to adapt...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found