-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (31 loc) · 785 Bytes
/
index.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
"use strict";
const { Buffer } = require("node:buffer");
const createLazyBuffer = (size) => {
let initialized = false;
let buffer;
return new Proxy(
{},
{
get: (target, prop) => {
if (prop === "write" && !initialized) {
buffer = Buffer.alloc(size);
initialized = true;
}
if (!initialized) {
console.warn("Buffer not initialized yet! Call write method first!");
return () => {};
}
return (...args) => buffer[prop](...args);
},
}
);
};
const lazyBuffer = createLazyBuffer(256);
// print three times not initialized yet
lazyBuffer.alloc(29);
lazyBuffer.read(29);
lazyBuffer.of(29);
// write test
lazyBuffer.write("test");
// print test
console.log(lazyBuffer.toString());