This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
build.js
61 lines (52 loc) · 1.7 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs')
const path = require('path')
const notice = '// section modified by isomorphic-webcrypto build';
let contents = fs.readFileSync(path.join(__dirname, 'node_modules/webcrypto-shim/webcrypto-shim.js'), 'utf8')
contents = contents.replace(
`self === 'undefined' ? this : self`,
`self === 'undefined' ? undefined : self`
)
contents += `\n export default {} ${notice} \n`
fs.writeFileSync(path.join(__dirname, 'src', 'webcrypto-shim.mjs'), contents)
let linerContents = fs.readFileSync(path.join(__dirname, 'node_modules/webcrypto-liner/build/webcrypto-liner.shim.js'), 'utf8')
linerContents = linerContents.replace(
`
let window;
if (typeof self === "undefined") {
const crypto = require("crypto");
window = {
crypto: {
subtle: {},
getRandomValues: array => {
const buf = array.buffer;
const uint8buf = new Uint8Array(buf);
const rnd = crypto.randomBytes(uint8buf.length);
rnd.forEach((octet, index) => uint8buf[index] = octet);
return array;
}
}
};
} else {
window = self;
}`,
notice
);
linerContents = linerContents.replace(/self\./g, 'window.');
linerContents = linerContents.replace(
`
const window$1 = self;
if (nativeCrypto) {
Object.freeze(nativeCrypto.getRandomValues);
}
try {
delete window.crypto;
window$1.crypto = new Crypto$1();
Object.freeze(window$1.crypto);
} catch (e) {
Debug.error(e);
}
const crypto = window$1.crypto;
exports.crypto = crypto;
`, `exports.crypto = new Crypto$1(); ${notice}`);
linerContents += `\n module.exports = liner; ${notice} \n`
fs.writeFileSync(path.join(__dirname, 'src', 'webcrypto-liner.js'), linerContents);