Skip to content

Commit

Permalink
adding a shim
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Jul 14, 2022
1 parent 92ab18c commit 7a32ddc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bin/tweetnacl-esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ async function copyFromModulesDir(from: string, tail = ""): Promise<void> {
const head =`// this source from node_modules/tweetnacl@${version}/${from}\n
// deno-lint-ignore-file
// deno-fmt-ignore-file
// added by nkeys.js
// shim for crypto.randomBytes on nodejs when using the esm module of the nkeys.js library
if (!globalThis.crypto) {
console.log("shimming crypto.randomBytes")
if (typeof module !== "undefined") {
globalThis.crypto = require("crypto");
console.log("shimmed with node crypto");
} else {
globalThis.crypto = {
randomBytes: (a) => {
for (let i = 0; i < a.length; i++) {
a[i] = Math.floor(256 * Math.random());
}
},
};
}
}
`;

let src = await Deno.readTextFile(
Expand Down
17 changes: 17 additions & 0 deletions modules/esm/nacl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

// deno-lint-ignore-file
// deno-fmt-ignore-file

// added by nkeys.js
// shim for crypto.randomBytes on nodejs when using the esm module of the nkeys.js library
if (!globalThis.crypto) {
console.log("shimming crypto.randomBytes")
if (typeof module !== "undefined") {
globalThis.crypto = require("crypto");
} else {
globalThis.crypto = {
randomBytes: (a) => {
for (let i = 0; i < a.length; i++) {
a[i] = Math.floor(256 * Math.random());
}
},
};
}
}
// Type definitions for TweetNaCl.js

export as namespace nacl;
Expand Down
17 changes: 17 additions & 0 deletions modules/esm/nacl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

// deno-lint-ignore-file
// deno-fmt-ignore-file

// added by nkeys.js
// shim for crypto.randomBytes on nodejs when using the esm module of the nkeys.js library
if (!globalThis.crypto) {
console.log("shimming crypto.randomBytes")
if (typeof module !== "undefined") {
globalThis.crypto = require("crypto");
} else {
globalThis.crypto = {
randomBytes: (a) => {
for (let i = 0; i < a.length; i++) {
a[i] = Math.floor(256 * Math.random());
}
},
};
}
}
(function(nacl) {
'use strict';

Expand Down

0 comments on commit 7a32ddc

Please sign in to comment.