{"id":30180924,"url":"https://github.com/streamich/static-buffer","last_synced_at":"2025-08-12T08:06:17.764Z","repository":{"id":57370024,"uuid":"60033934","full_name":"streamich/static-buffer","owner":"streamich","description":"Execute machine code in your Buffer","archived":false,"fork":false,"pushed_at":"2018-01-05T09:31:27.000Z","size":6,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-09T16:48:20.967Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/streamich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-30T19:27:35.000Z","updated_at":"2024-06-08T01:18:44.000Z","dependencies_parsed_at":"2022-08-29T13:41:05.572Z","dependency_job_id":null,"html_url":"https://github.com/streamich/static-buffer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/streamich/static-buffer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fstatic-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fstatic-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fstatic-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fstatic-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/streamich","download_url":"https://codeload.github.com/streamich/static-buffer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fstatic-buffer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269803772,"owners_count":24477650,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-08-12T08:06:07.682Z","updated_at":"2025-08-12T08:06:17.754Z","avatar_url":"https://github.com/streamich.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `StaticArrayBuffer` and `StaticBuffer`\r\n\r\n## Hello World Example\r\n\r\nPrint `\"Hello World\"` to console, we do it by executing `write` system call to `1` file\r\ndescriptor which is `STDOUT`:\r\n\r\n```js\r\nvar StaticBuffer = require('static-buffer/buffer').StaticBuffer;\r\n\r\nvar sbuf = StaticBuffer.from([\r\n    0x48, 0xc7, 0xc0, 1, 0, 0, 0,       // mov    $0x1,%rax         # System call `1` -- SYS_write\r\n    0x48, 0xc7, 0xc7, 1, 0, 0, 0,       // mov    $0x1,%rdi         # File descriptor `1` -- STDOUT\r\n    0x48, 0x8d, 0x35, 10, 0, 0, 0,      // lea    0x1(%rip),%rsi    # Data address\r\n    0x48, 0xc7, 0xc2, 13, 0, 0, 0,      // mov    $13,%rdx          # Number of bytes to write -- 13\r\n    0x0f, 0x05,                         // syscall                  # Execute the system call.\r\n    0xc3,                               // retq                     # Return\r\n    0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello_\r\n    0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, // World!\r\n    0x0A, 0                             // \\n\\0\r\n], 'rwe'); // r - readable, w - writable, e - executable\r\n\r\nsbuf.call([], 0); // Hello World!\r\n```\r\n\r\n## How it works\r\n\r\n`StaticArrayBuffer` and `StaticBuffer` extend `ArrayBuffer` and `Buffer` classes and \r\nprovide such extra functionality:\r\n\r\n 1. Allocate executable memory.\r\n 2. Execute the machine code in `StaticBuffer` using `.call()` method.\r\n 3. Change protection to backing memory.\r\n 4. Promise that actual data in memory will never be moved by runtime.\r\n\r\nMemory protection flags:\r\n\r\n - `'r'` - readable\r\n - `'w'` - writable\r\n - `'e'` - executable\r\n\r\n## Usage\r\n\r\n```js\r\nvar StaticArrayBuffer = require('static-buffer/arraybuffer').StaticArrayBuffer;\r\nvar StaticBuffer = require('static-buffer/buffer').StaticArrayBuffer;\r\n```\r\n\r\n`StaticArrayBuffer` inherits from `ArrayBuffer` and provides the following\r\nextra functionality:\r\n\r\n```js\r\nvar sab = new StaticArrayBuffer(1024, 'rwe');\r\nStaticArrayBuffer.isStaticArrayBuffer(sab); // true\r\n\r\n// Call mathine code stored in `StaticArrayBuffer` at offset `offset`,\r\n// providing `number[]` arguments to that machine code using standard\r\n// calling conventions supported by your system, currently supports \r\n// up to 10 arguments.\r\nvar offset = 0;\r\nvar args = [1, 2, 3];\r\nsab.call(args, offset);\r\n\r\n// Change protection of the memory block.\r\nsab.setProtection('rw');\r\n\r\n// Get the actual address pointer of the memory that backs the `StaticArrayBuffer`.\r\noffset = 0;\r\nvar addr = sab.getAddress(offset); // [number, number]\r\n\r\n// Free the memory of the `StaticArrayBuffer`, after that your `StaticArrayBuffer`\r\n// should not be used.\r\nsab.free();\r\nsab = null;\r\n```\r\n\r\n`StaticBuffer` inherits from [`Buffer`](https://nodejs.org/docs/latest/api/buffer.html) and\r\nis backed by `StaticArrayBuffer` instead of `ArrayBuffer` that `Buffer` uses. It provides \r\nthe following extra functionality:\r\n\r\n```js\r\nvar sbuf = new StaticBuffer(1024, 'rwe');\r\n// or\r\nvar offset = 10, len = 10;\r\nsbuf = new StaticBuffer(sab, offset, len);\r\n// or use StaticBuffer.from() syntax\r\n\r\nStaticBuffer.isStaticBuffer(sbuf); // true\r\n\r\nsbuf.call([], 0);\r\nsbuf.getAddress(0);\r\n\r\nsbuf.buffer.free();\r\nsbuf = null;\r\n```\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreamich%2Fstatic-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstreamich%2Fstatic-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreamich%2Fstatic-buffer/lists"}