Map memory to buffers via mmap
npm install @k13engineering/po6-mmapA Node.js library for memory-mapped file I/O using the mmap system call. This package provides a TypeScript-first interface for mapping files into memory with automatic resource management and garbage collection safety.
- πΊοΈ Map files into memory using the mmap syscall
- π Support for shared and private mappings
- β‘ Zero-copy access to file data via Uint8Array
- π‘οΈ Automatic detection of unmapped buffers during garbage collection
- π¦ TypeScript-first with full type definitions
- π§ Flexible memory protection flags (read, write, execute)
``bash`
npm install @k13engineering/po6-mmap
`typescript
import { mmapFd, determinePageSize } from "@k13engineering/po6-mmap";
import nodeFs from "node:fs";
const fd = nodeFs.openSync("/dev/zero", "r+");
const length = determinePageSize();
const { errno, mapping } = mmapFd({
fd,
mappingVisibility: "MAP_PRIVATE",
memoryProtectionFlags: {
PROT_READ: true,
PROT_WRITE: false,
PROT_EXEC: false,
},
genericFlags: {},
offsetInFd: 0,
length,
});
nodeFs.closeSync(fd);
if (errno !== undefined) {
throw Error(mmapFd failed with errno ${errno});
}
console.log(mapped buffer of length ${mapping.length} at address 0x${mapping.address.toString(16)});buffer:
console.log(, mapping.createArrayBuffer());
mapping.unmap();
`
Maps a file descriptor into memory.
Parameters:
- fd (number): File descriptor to mapmappingVisibility
- ("MAP_SHARED" | "MAP_PRIVATE"): Mapping visibilityMAP_SHARED
- : Changes are shared with other processesMAP_PRIVATE
- : Changes are private (copy-on-write)memoryProtectionFlags
- (object): Memory protection flagsPROT_READ
- (boolean): Allow read accessPROT_WRITE
- (boolean): Allow write accessPROT_EXEC
- (boolean): Allow executiongenericFlags
- (object): Additional flagsMAP_32BIT
- (boolean): Map into 32-bit address spaceMAP_LOCKED
- (boolean): Lock pages in memoryMAP_NORESERVE
- (boolean): Don't reserve swap spaceoffsetInFd
- (number): Offset in file (must be page-aligned)length
- (number): Length of mapping in bytes
Returns: Object with either:
- { errno: number, mapping: undefined } on error{ errno: undefined, mapping: TMemoryMapping }
- on success
Note: Not all mmap options are currently exposed. Additional flags and options may be added in future versions.
An object representing a memory mapping with the following properties:
- address (bigint): Memory address of the mappinglength
- (number): Length of the mapping in bytescreateArrayBuffer()
- (function): Returns an ArrayBuffer for the mapped memory regionunmap()
- (function): Unmaps the memory region
Important: Always call unmap() when done to avoid memory leaks. If a buffer is garbage collected without being unmapped, the library will throw an uncaught exception.
Returns the system page size (currently hardcoded to 4096).
This library includes a finalization registry that detects if a memory-mapped buffer is garbage collected without calling unmap(). If this happens, a MemoryMappedBufferGarbageCollectedWithoutUnmapError is thrown to prevent silent memory leaks.
Always ensure you call unmap() on buffers when you're done with them:
`typescript``
const result = mmapFd({ / ... / });
if (result.errno === undefined) {
try {
// Use result.mapping
const buffer = result.mapping.createArrayBuffer();
} finally {
result.mapping.unmap(); // Always unmap in finally block
}
}
The main code in this library is primarily hand-crafted, while the test suite has been mostly generated by AI. This approach combines careful manual implementation with comprehensive AI-assisted testing coverage.
See LICENSE file.