PeculiarVentures/webcrypto

Is there a way to bundle only subtle.digest() "SHA-1" support?

guest271314 opened this issue · 1 comments

I'm implementing a JavaScript runtime agnostic WebSocket server. txiki.js does not support subtle. The only capability I need is Web Cryptography API subtle for digest() function, e.g.,

// https://stackoverflow.com/a/77398427
async function digest(message, algo = "SHA-1") {
  const { promise, resolve } = Promise.withResolvers();
  const reader = new FileReader();
  reader.onload = () => resolve(reader.result);
  reader.readAsDataURL(
    new Blob([
      new Uint8Array(
        await crypto.subtle.digest(
          algo,
          new TextEncoder().encode(
            `${message}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`,
          ),
        ),
      ),
    ]),
  );
  const result = await promise;
  return result.split(",").pop();
}

I've fetched the repository and tried to bundle with

bun build ./src/index.ts --outfile=webcrypto.js --target=browser 

yet have to substitute every Buffer import for

import * as Buffer from "node:buffer";

where I'm not going to be using Node.js' Buffer at all, rather I'll be using Uint8Array and/or ArrayBuffer and DataView.