-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdigitalWaste.js
More file actions
80 lines (65 loc) · 2.41 KB
/
Copy pathdigitalWaste.js
File metadata and controls
80 lines (65 loc) · 2.41 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.digitalWaste = factory();
}
}(typeof self !== 'undefined' ? self : this, function() {
const MB = 1024 * 1024;
function digitalWaste(size = 100 * MB, progressCallback = null) {
const bufferSize = Math.max(1, size);
const buffer = new ArrayBuffer(bufferSize);
const view = new Uint8Array(buffer);
const chunkSize = 65536;
const totalChunks = Math.ceil(bufferSize / chunkSize);
for (let i = 0; i < totalChunks; i++) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, bufferSize);
const chunk = view.subarray(start, end);
if (typeof window !== 'undefined' && window.crypto) {
// Browser environment
window.crypto.getRandomValues(chunk);
} else if (typeof require === 'function') {
// Node.js environment
const crypto = require('crypto');
crypto.randomFillSync(chunk);
} else {
// Fallback (less secure but still high entropy)
for (let j = 0; j < chunk.length; j++) {
chunk[j] = Math.floor(Math.random() * 256);
}
}
if (progressCallback && typeof progressCallback === 'function') {
progressCallback({
bytesGenerated: end,
totalBytes: bufferSize,
percentComplete: Math.floor((end / bufferSize) * 100)
});
}
}
return buffer;
}
digitalWaste.saveToFile = function(filePath, size = 100 * MB, callback) {
if (typeof require !== 'function') {
throw new Error('saveToFile is only available in Node.js environment');
}
const fs = require('fs');
const buffer = digitalWaste(size);
fs.writeFile(filePath, Buffer.from(buffer), callback);
};
digitalWaste.download = function(filename = 'data-waste.bin', size = 100 * MB) {
if (typeof window === 'undefined') {
throw new Error('download is only available in browser environment');
}
const buffer = digitalWaste(size);
const blob = new Blob([buffer], { type: 'application/octet-stream' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
setTimeout(() => URL.revokeObjectURL(link.href), 100);
};
return digitalWaste;
}));