Skip to content

Commit 5d20923

Browse files
fix: update serialize-javascript
1 parent 2f86b7f commit 5d20923

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

scripts/copy-serialize-javascript.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ const path = require("path");
44

55
/* eslint-disable no-console */
66

7+
const randomBytesFallback = `
8+
var g = typeof globalThis !== 'undefined' ? globalThis : global;
9+
var crypto = g.crypto || {};
10+
11+
if (typeof crypto.getRandomValues !== 'function') {
12+
var nodeCrypto = require('crypto');
13+
14+
crypto.getRandomValues = function(typedArray) {
15+
var bytes = nodeCrypto.randomBytes(typedArray.byteLength);
16+
17+
new Uint8Array(
18+
typedArray.buffer,
19+
typedArray.byteOffset,
20+
typedArray.byteLength
21+
).set(bytes);
22+
23+
return typedArray;
24+
};
25+
}
26+
`;
27+
728
/**
829
* @param {string} src source path
930
* @param {string} dest destination path
@@ -14,7 +35,7 @@ async function copyIfChanged(src, dest) {
1435

1536
try {
1637
srcContent = await fs.readFile(src, "utf8");
17-
srcContent = `// @ts-nocheck\n${srcContent}`;
38+
srcContent = `// @ts-nocheck\n${randomBytesFallback}${srcContent}`;
1839
} catch (_err) {
1940
srcContent = null;
2041
}

src/serialize-javascript.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
// @ts-nocheck
2+
3+
var g = typeof globalThis !== 'undefined' ? globalThis : global;
4+
var crypto = g.crypto || {};
5+
6+
if (typeof crypto.getRandomValues !== 'function') {
7+
var nodeCrypto = require('crypto');
8+
9+
crypto.getRandomValues = function(typedArray) {
10+
// Генерируем буфер случайных байтов нужной длины
11+
var bytes = nodeCrypto.randomBytes(typedArray.byteLength);
12+
13+
// Копируем байты в типизированный массив через Uint8Array View
14+
new Uint8Array(
15+
typedArray.buffer,
16+
typedArray.byteOffset,
17+
typedArray.byteLength
18+
).set(bytes);
19+
20+
return typedArray;
21+
};
22+
}
223
/*
324
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
425
Copyrights licensed under the New BSD License.

0 commit comments

Comments
 (0)