Files
bun.sh/src/js/node/v8.ts
Meghan Denny fd75ca7585 implement node:cluster (#11492)
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: nektro <nektro@users.noreply.github.com>
Co-authored-by: cirospaciari <ciro.spaciari@gmail.com>
2024-08-18 00:12:42 -07:00

168 lines
3.8 KiB
TypeScript

// Hardcoded module "node:v8"
// This is a stub! None of this is actually implemented yet.
const { hideFromStack, throwNotImplemented } = require("internal/shared");
const jsc: typeof import("bun:jsc") = require("bun:jsc");
function notimpl(message) {
throwNotImplemented("node:v8 " + message);
}
class Deserializer {
constructor() {
notimpl("Deserializer");
}
}
class Serializer {
constructor() {
notimpl("Serializer");
}
}
class DefaultDeserializer extends Deserializer {}
class DefaultSerializer extends Serializer {}
class GCProfiler {
constructor() {
notimpl("GCProfiler");
}
}
function cachedDataVersionTag() {
notimpl("cachedDataVersionTag");
}
function getHeapSnapshot() {
notimpl("getHeapSnapshot");
}
let totalmem_ = -1;
function totalmem() {
if (totalmem_ === -1) {
totalmem_ = require("node:os").totalmem();
}
return totalmem_;
}
function getHeapStatistics() {
const stats = jsc.heapStats();
const memory = jsc.memoryUsage();
// These numbers need to be plausible, even if incorrect
// From npm's codebase:
//
// > static #heapLimit = Math.floor(getHeapStatistics().heap_size_limit)
//
return {
total_heap_size: stats.heapSize,
total_heap_size_executable: stats.heapSize >> 1,
total_physical_size: memory.peak,
total_available_size: totalmem() - stats.heapSize,
used_heap_size: stats.heapSize,
heap_size_limit: Math.min(memory.peak * 10, totalmem()),
malloced_memory: stats.heapSize,
peak_malloced_memory: memory.peak,
// -- Copied from Node:
does_zap_garbage: 0,
number_of_native_contexts: 1,
number_of_detached_contexts: 0,
total_global_handles_size: 8192,
used_global_handles_size: 2208,
// ---- End of copied from Node
external_memory: stats.extraMemorySize,
};
}
function getHeapSpaceStatistics() {
notimpl("getHeapSpaceStatistics");
}
function getHeapCodeStatistics() {
notimpl("getHeapCodeStatistics");
}
function setFlagsFromString() {
notimpl("setFlagsFromString");
}
function deserialize(value) {
return jsc.deserialize(value);
}
function takeCoverage() {
notimpl("takeCoverage");
}
function stopCoverage() {
notimpl("stopCoverage");
}
function serialize(arg1) {
return jsc.serialize(arg1, { binaryType: "nodebuffer" });
}
function writeHeapSnapshot() {
notimpl("writeHeapSnapshot");
}
function setHeapSnapshotNearHeapLimit() {
notimpl("setHeapSnapshotNearHeapLimit");
}
const promiseHooks = {
createHook: () => {
notimpl("createHook");
},
onInit: () => {
notimpl("onInit");
},
onBefore: () => {
notimpl("onBefore");
},
onAfter: () => {
notimpl("onAfter");
},
onSettled: () => {
notimpl("onSettled");
},
},
startupSnapshot = {
addDeserializeCallback: () => notimpl("addDeserializeCallback"),
addSerializeCallback: () => notimpl("addSerializeCallback"),
setDeserializeMainFunction: () => notimpl("setDeserializeMainFunction"),
isBuildingSnapshot: () => notimpl("isBuildingSnapshot"),
};
export default {
cachedDataVersionTag,
getHeapSnapshot,
getHeapStatistics,
getHeapSpaceStatistics,
getHeapCodeStatistics,
setFlagsFromString,
deserialize,
takeCoverage,
stopCoverage,
serialize,
writeHeapSnapshot,
setHeapSnapshotNearHeapLimit,
promiseHooks,
startupSnapshot,
Deserializer,
Serializer,
DefaultDeserializer,
DefaultSerializer,
};
hideFromStack(
notimpl,
cachedDataVersionTag,
getHeapSnapshot,
getHeapStatistics,
getHeapSpaceStatistics,
getHeapCodeStatistics,
setFlagsFromString,
deserialize,
takeCoverage,
stopCoverage,
serialize,
writeHeapSnapshot,
setHeapSnapshotNearHeapLimit,
Deserializer,
Serializer,
DefaultDeserializer,
DefaultSerializer,
GCProfiler,
DefaultDeserializer,
DefaultSerializer,
);