mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.1k stars 336 forks source link

TypeError [Error]: Cannot set property crypto of #<Object> which has only a getter #909

Closed jlarmstrongiv closed 2 years ago

jlarmstrongiv commented 2 years ago

When using NodeJS 18.x or NodeJS 16.x with --experimental-global-webcrypto, I receive the error:

TypeError [Error]: Cannot set property crypto of #<Object> which has only a getter

This error is caused occurs due to node_modules/sh-syntax/lib/shim.js:

import { randomFillSync } from 'node:crypto';
import _fs from 'node:fs';
import { performance } from 'node:perf_hooks';
globalThis.fs = _fs;
globalThis.crypto = {
    // @ts-expect-error
    getRandomValues: randomFillSync,
};
// @ts-expect-error
globalThis.performance = performance;
//# sourceMappingURL=shim.js.map

Specifically:

globalThis.crypto = {
    // @ts-expect-error
    getRandomValues: randomFillSync,
};

Perhaps a workaround would be:

if (!globalThis.crypto) {
    globalThis.crypto = {
        // @ts-expect-error
        getRandomValues: randomFillSync,
    };
}
jlarmstrongiv commented 2 years ago

Related https://github.com/un-ts/prettier/issues/216

jlarmstrongiv commented 2 years ago

This may be fixed by https://github.com/gopherjs/gopherjs/pull/1026#issuecomment-843582252 in https://github.com/gopherjs/gopherjs/releases/tag/1.16.2%2Bgo1.16.4

Nope