{"id":15690581,"url":"https://github.com/linusu/node-cstruct","last_synced_at":"2025-05-07T23:45:06.157Z","repository":{"id":35780532,"uuid":"40060601","full_name":"LinusU/node-cstruct","owner":"LinusU","description":"Use C  `struct`s in Javascript while keeping the syntax intact.","archived":false,"fork":false,"pushed_at":"2017-11-15T20:37:52.000Z","size":24,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-07T23:45:00.606Z","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/LinusU.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":"2015-08-01T21:30:08.000Z","updated_at":"2024-10-31T16:59:44.000Z","dependencies_parsed_at":"2022-09-17T12:21:55.312Z","dependency_job_id":null,"html_url":"https://github.com/LinusU/node-cstruct","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2Fnode-cstruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2Fnode-cstruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2Fnode-cstruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2Fnode-cstruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LinusU","download_url":"https://codeload.github.com/LinusU/node-cstruct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252973616,"owners_count":21834105,"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":[],"created_at":"2024-10-03T18:12:17.536Z","updated_at":"2025-05-07T23:45:06.117Z","avatar_url":"https://github.com/LinusU.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `cstruct`\n\nUse C  `struct`s in Javascript while keeping the syntax intact.\n\n## Installation\n\n```sh\nnpm install --save cstruct\n```\n\n## Usage\n\n```javascript\nconst struct = require('cstruct')\n\nconst Color = struct`\n  uint8 r, g, b;\n`\n\nconst Point = struct`\n  uint8 x, y;\n  ${Color} color;\n`\n\n// Read from buffer\nconst raw = Buffer.from('7823ff00ff', 'hex')\nconst data = Point.read(raw)\n\n// Write to buffer\nconst data = { x: 120, y: 35, color: { r: 255, g: 0, b: 255 } }\nconst raw = Point.write(data)\n```\n\n## API\n\n### `struct`\n\nThis is a function that is suppose to be used together with [tagged template\nstrings][1]. The template string is a struct definition as you write it in C.\n\nThe return value is a new instance of `Schema`.\n\n[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings\n\n### `Schema.byteLength`\n\nThe length of the struct in bytes.\n\n### `Schema.attributes`\n\nAn array of the attributes of the struct. Each attribute is an object with the\nfollowing properties: `name`, `type`, `offset`, `isArray`, `count`.\n\n### `Schema.linkedTypes`\n\nA `Map` with other types used inside of this type. The key is the name of the\nstruct and the value is a `Schema`.\n\n### `Schema.read(buffer[, offset])`\n\nRead the struct from a buffer into a structured javascript object. Optionally\nsupply an offset to start reading at that position.\n\n### `Schema.write(data[, targetBuffer[, targetOffset]])`\n\nWrite the javascript object `data` into a buffer. If no `targetBuffer` is\nsupplied, a new one will be created with the same length as the struct.\nOptionally supply an offset to start writing at that position.\n\n### `Schema.readArray(count, buffer[, offset])`\n\nRead `count` number of structs from a buffer into an array. Optionally\nsupply an offset to start reading at that position.\n\n### `Schema.writeArray(data[, targetBuffer[, targetOffset]])`\n\nWrite an array of javascript objects into a buffer. If no `targetBuffer` is\nsupplied, a new one will be created with the same length as the structs combined\nsize. Optionally supply an offset to start writing at that position.\n\n### `struct.read(typeName, buffer[, offset])`\n\nRead a primitive type from a buffer. Optionally supply an offset to start\nreading at that position.\n\n### `struct.write(typeName, data[, targetBuffer[, targetOffset]])`\n\nWrite a primitive type into a buffer. If no `targetBuffer` is supplied, a new\none will be created with the same length as the type. Optionally supply an\noffset to start writing at that position.\n\n### `struct.readArray(typeName, count, buffer[, offset])`\n\nRead `count` number of primitive types from a buffer into an array. Optionally\nsupply an offset to start reading at that position.\n\n### `struct.writeArray(typeName, data[, targetBuffer[, targetOffset]])`\n\nWrite an array of primitive types into a buffer. If no `targetBuffer` is\nsupplied, a new one will be created with the same length as the primitive types\ncombined size. Optionally supply an offset to start writing at that position.\n\n## Endianness\n\nBy default reading and writing is done in the current computer's endianness.\nAll function can be postfixed with `LE` or `BE` to force a specific endianness,\ne.g. `struct.readBE('uint32_t', buffer)`.\n\n## Compatibility\n\nTemplate strings is enabled by default in [io.js][2] but not in [Node.js][3]\nversion `0.12`. [Babel][4] is capable of transpiling for almost all\nenvironments, including Node.js.\n\n[2]: https://iojs.org/\n[3]: https://nodejs.org/\n[4]: https://babeljs.io/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinusu%2Fnode-cstruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinusu%2Fnode-cstruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinusu%2Fnode-cstruct/lists"}