cross-org / jwt

A versatile JSON Web Token (JWT) library with cross-runtime compatibility
MIT License
5 stars 0 forks source link

Window is not defined on node/bun #2

Closed rebyatyonok closed 3 months ago

rebyatyonok commented 3 months ago

Hello! I run the following code with node/bun

#!/usr/bin/node

/* eslint no-console: off */
import { existsSync } from 'node:fs'
import { exportPEMKey, generateKeyPair } from '@cross/jwt'

const paths = {
    private: process.env.PRIVATE_KEY_PATH,
    public: process.env.PUBLIC_KEY_PATH
}

;(async () => {
    const isPrivateExists = existsSync(paths.private)
    const isPublicExists = existsSync(paths.public)

    if (isPrivateExists && isPublicExists) {
        console.log('Keys already exist. Skipping')
    }
    else {
        const { privateKey, publicKey } = await generateKeyPair('PS512')
        exportPEMKey(privateKey, paths.private).then(() => {
            console.log('Private key generated')
        })
        exportPEMKey(publicKey, paths.public).then(() => {
            console.log('Public key generated')
        })
    }
})()

and receive an error which is kinda makes sense

ReferenceError: window is not defined
    at exportPEMKey (file:///<skipped>/node_modules/@cross/jwt/src/cryptokeys.js:102:23)
    at file:///<skipped>/scripts/generate-keys.js:21:3

Node version: v22.2.0 Bun version: 1.1.13

Will you accept a PR to fix that?

Pinta365 commented 3 months ago

Yes, will gladly look at and accept a PR. :)

Pinta365 commented 3 months ago

@rebyatyonok version 0.4.9 got a fix using the global crypto object.

rebyatyonok commented 3 months ago

@Pinta365 Sorry, didn't have time to submit a PR. Thanks a lot!