{"id":17216887,"url":"https://github.com/markusjx/shared_memory","last_synced_at":"2025-06-22T05:34:21.616Z","repository":{"id":47317187,"uuid":"375711066","full_name":"MarkusJx/shared_memory","owner":"MarkusJx","description":"Shared memory for node.js","archived":false,"fork":false,"pushed_at":"2022-05-24T20:23:34.000Z","size":102,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T23:28:59.451Z","etag":null,"topics":["nodejs","shared-memory"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarkusJx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-10T13:40:22.000Z","updated_at":"2024-11-03T10:50:14.000Z","dependencies_parsed_at":"2022-07-20T20:48:41.201Z","dependency_job_id":null,"html_url":"https://github.com/MarkusJx/shared_memory","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/MarkusJx/shared_memory","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fshared_memory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fshared_memory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fshared_memory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fshared_memory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarkusJx","download_url":"https://codeload.github.com/MarkusJx/shared_memory/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fshared_memory/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261243140,"owners_count":23129585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","shared-memory"],"created_at":"2024-10-15T03:42:36.974Z","updated_at":"2025-06-22T05:34:16.580Z","avatar_url":"https://github.com/MarkusJx.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![shared_memory](https://socialify.git.ci/MarkusJx/shared_memory/image?description=1\u0026language=1\u0026owner=1\u0026theme=Light)](https://github.com/MarkusJx/shared_memory#readme)\n\n## Installation\n```sh\nnpm install @markusjx/shared_memory\n```\n*Requires Node.js 12 or later*\n\n## Note on any ``global`` properties\nThe global property only have an effect on Windows\nmachines, when running on any other machine, the passed\nvalue will be ignored. When passing true as an argument,\nthe shared memory block will be accessible globally.\nIn that case, the program must be run with administrator rights.\nOn linux, any program creating a shared memory block must be executed with root rights.\n\nRead more [here](https://docs.microsoft.com/en-us/windows/win32/termserv/kernel-object-namespaces).\n\n## Usage\n### Import the module\n```js\nconst shared_memory = require('@markusjx/shared_memory');\n```\n\n### Class ``shared_memory``\n#### ``shared_memory.generateId``\nGenerate an id for a shared memory block.\n\n**Arguments:**\n* ``global?: boolean`` - Whether the memory block can be read globally. Defaults to ``false``.\n\n**Returns**\n\n``string`` - The generated id\n\n#### ``shared_memory.generateIdAsync``\nGenerate an id for a shared memory block. Async call.\n\n**Arguments**\n* ``global?: boolean`` - Whether the memory block can be read globally. Defaults to ``false``.\n\n**Returns**\n\n``Promise\u003cstring\u003e`` - The generated id\n\n#### ``new shared_memory``\nCreate a new shared memory block.\n\n**Arguments**\n* ``name: string`` - The name (id) of the memory block\n* ``sizeInBytes: number`` - The size of the memory block in bytes\n* ``global?: boolean`` - Whether the memory block can be read globally. Defaults to ``false``.\n* ``host?: boolean`` - Whether to create the memory block. Defaults to ``true``.\n\n**Throws**\n\n``Error`` - If a memory block with the given id already exists (if ``host`` is set to ``true``), or the block couldn't be attached.\n\n#### ``shared_memory.set data``\nCopy a string to the shared memory block.\n\n**Arguments**\n* ``data: string`` - The data to copy\n\n**Throws**\n\n``Error`` - If the string is too big for the buffer\n\n#### ``shared_memory.get data``\nGet a string from the memory block. Reads ``std::max(strlen(buffer), sizeof(buffer))`` bytes from the buffer.\nThe string length therefore is determined by either a delimiting ``\\0`` or the length of the buffer.\n\n**Returns**\n\n``string`` - The read data\n\n#### ``shared_memory.set buffer``\nCopy data from a node ``Buffer`` to the memory block.\n\n**Arguments**\n* ``data: Buffer`` - The data to copy\n\n**Throws**\n\n``Error`` - If the Buffer is larger than the shared memory block\n\n#### ``shared_memory.get buffer``\nRead data from the memory block into a node ``Buffer``.\nAlways reads ``sizeof(buffer)`` into the result ``Buffer``.\n\n**Returns**\n\n``Buffer`` - The read data\n\n#### ``shared_memory.write``\nWrite some data to the buffer. Writes either a ``string`` or a node ``Buffer``.\n\n**Arguments**\n* ``data: string | Buffer`` - The data to copy\n\n**Returns**\n\n``void``\n\n**Throws**\n\n``Error`` - If the shared memory block is too small for the data to write\n\n#### ``shared_memory.read``\nRead a string from the memory. Reads ``std::max(strlen(buffer), sizeof(buffer))`` bytes from the buffer.\nThe string length therefore is determined by either a delimiting ``\\0`` or the length of the buffer.\n\n**Returns**\n\n``string`` - the read data\n\n#### ``shared_memory.readBuffer``\nRead data from the memory block into a node ``Buffer``.\nAlways reads ``sizeof(buffer)`` into the result ``Buffer``.\n\n**Returns**\n\n``Buffer`` - The read data\n\n## Example\nHost code:\n```ts\nconst shared_memory = require('@markusjx/shared_memory');\n\n// Generate a memory id\nconst id = shared_memory.generateId();\n\n// Create a new shared memory block\n// with a size of 1024 bytes\nconst memory = new shared_memory(id, 1024, false, true);\n\n// Write data to the memory\nmemory.data = \"Some data\";\n\n// Signal the client that the data is ready\n// Wait until the client has read all the data\n\n// Write some new data in form of a buffer\nmemory.buffer = Buffer.from(\"Some data\");\n\n// Signal the client that the data is ready\n```\n\nClient code:\n\n```ts\nimport shared_memory from \"./index\";\n\n// Attach the created memory\nconst memory = new shared_memory(id, 1024, false, false);\n\n// Wait until the data is ready\n\n// Read the data \nconst read = memory.data;\n\n// Signal the host that the data has been read\n// Wait until the next data is ready\n\n// Read the data into a buffer.\n// This will contain all of the data in the memory block.\nconst buf = memory.buffer;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkusjx%2Fshared_memory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkusjx%2Fshared_memory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkusjx%2Fshared_memory/lists"}