Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
Using subtle in a Javascript module, from both the browser and NodeJS

NodeJS has an experimental implementation of the browser's crypto.subtle.

I have written a Javascript module which runs in both the browser and the NodeJS. It needs access to subtle.

For the moment, every function which needs to use subtle awaits the following function at the beginning of execution:

async function getSubtle() {
  if (typeof window === 'undefined') { // running in NodeJS
    const crypto = await import('crypto');
    return crypto.webcrypto.subtle;
  } else { // running in a browser
    return crypto.subtle;
  }
}

This works, but is slow and ugly as I'm re-importing the code on every function call (in NodeJS).

My project is simple enough that I do not use a build system. (I use express and symlink the modules I require, e.g. from public/js/modules/foo.mjs to lib/foo.js.)

Can I replace getSubtle() with a module-level non-async conditional import? If so, how?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda