{"id":21128840,"url":"https://github.com/ragarwal33/struct-ts","last_synced_at":"2026-04-13T09:31:17.263Z","repository":{"id":143781007,"uuid":"421321308","full_name":"ragarwal33/struct-ts","owner":"ragarwal33","description":"C/C++ style struct definition, serialization in Typescript","archived":false,"fork":false,"pushed_at":"2021-10-28T12:00:43.000Z","size":275,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-01T05:47:37.973Z","etag":null,"topics":["c","cplusplus","serialization","struct","structure","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/ragarwal33.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-26T07:19:02.000Z","updated_at":"2025-04-25T02:58:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"4bb89167-2f1c-475f-a492-79128eaeb99c","html_url":"https://github.com/ragarwal33/struct-ts","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ragarwal33/struct-ts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragarwal33%2Fstruct-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragarwal33%2Fstruct-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragarwal33%2Fstruct-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragarwal33%2Fstruct-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ragarwal33","download_url":"https://codeload.github.com/ragarwal33/struct-ts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragarwal33%2Fstruct-ts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31746290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-13T09:16:15.125Z","status":"ssl_error","status_checked_at":"2026-04-13T09:16:05.023Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["c","cplusplus","serialization","struct","structure","typescript"],"created_at":"2024-11-20T05:10:29.554Z","updated_at":"2026-04-13T09:31:17.258Z","avatar_url":"https://github.com/ragarwal33.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"struct-ts\n\nCreate C compatible struct with automatic padding and array member support\n\nInstallation\n------------\n    npm install @ragarwal33/struct-ts\n\nSupports\n--------\n1. Automatic Padding\n2. Multiple Packing Support (1, 2, 4, 8, 16)\n3. Constant size array as member\n4. Dynamic size array as last member\n5. Serialization/Deserialiation\n6. Easy member access\n7. Can embed the created structure into another structure\n\nUsage\n-----\n```cpp\n//Definition of C++ Struct\nstruct Test {\n    char x;\n    short y;\n    int z;\n    double w;\n}\n```\n```typescript\n//Definition in struct-ts\nvar Test = Struct.Create([\n    {name: 'x', type: 'char', val: 1},\n    {name: 'y', type: 'short', val: 2},\n    {name: 'z', type: 'int', val: 3},\n    {name: 'w', type: 'double', val: 4.0},\n]);\n\n//instantiate object of struct Test\nvar instance = new Test();  //this will create instance with the default value specified while creating the definition\n//Or\nvar instance = new Test ({x: 2, z: 5}); //this will override the default value of x and z with the new values provided\n```\nThis will create a Test class having abi compatibility with the C Struct for default packing size of 8 bytes\n\nThe full definition of Struct.Create is \u003cbr\u003e\n```typescript\nCreate(mems: MemberDefinition[], memfns?: MemberFunctionObject, packing?: number): typeof Struct\n```\nHere\n* mems is the member of the structs\n* memfns is optional functions that the struct object can have\n* packing is structure packing size (default is 8)\n\nSerialization\n--\n```typescript\nvar buffer: ArrayBuffer = Struct.buffer(instance);\n```\nDeserialization\n--\n```typescript\nvar instance: Test = new Test(buffer);\n```\nMember function Example\n------\n```typescript\nvar Test = Struct.Create([\n        {name: 'x', type: 'int8', val: 1},\n        {name: 'y', type: 'int16', val: 2},\n        {name: 'z', type: 'int32', val: 3},\n        {name: 'w', type: 'float64', val: 4},\n    ], {\n        print: function(arg1: number) {\n            var thisArg: any = this;    //to avoid typescript error\n            console.log(thisArg.x + thisArg.y + thisArg.z + thisArg.w + arg1);\n        }\n    }\n);\nvar instance = new Test();\ninstance.x = 3;\ninstance.print(5); //prints  (3 + 2 + 3 + 4 + 5) = 17\n```\nPacking Example\n--\n```typescript\n//the above struct defined with packing 1, 2, 4, 8\nvar Test1 = Struct.Create(..., {}, 1);\nvar Test2 = Struct.Create(..., {}, 2);\nvar Test4 = Struct.Create(..., {}, 4);\nvar Test8 = Struct.Create(..., {}, 8);\n\nconsole.log(Test1.size == 15);\nconsole.log(Test2.size == 16);\nconsole.log(Test4.size == 16);\nconsole.log(Test8.size == 16);\n\nconsole.log(Test4.offsetof('x') == 0)\nconsole.log(Test4.offsetof('y') == 2)\nconsole.log(Test4.offsetof('z') == 4)\nconsole.log(Test4.offsetof('w') == 8)\n```\nMore Examples\n--\n```typescript\nvar Test = Struct.Create([\n    {name: 'x', type: 'char[2]', val: \"ab\"},\n    {name: 'y', type: 'int[2]', val: [2, 3]},\n    {name: 'z', type: 'ushort[3]', val: Struct.ArrayOf('ushort', 3)}\n]);\nvar TestEmbedded = Struct.Create([\n    {name: 'arrTest', type: `${Test.type}[2]`, val: Struct.ArrayOf(Test, 2)},\n    {name: 'anotherArr', type: `${Test.type}[2]`, val: [new Test(), new Test()]}\n    {name: 'yetAnotherMember', type: Test.type, val: new Test({x: \"ac\"})},\n])\n```\nMember Access\n--\nMembers can be simply accessed or assigned as any other property of the object\n\nDynamic Array member\n--\nMotivation\n*Often we have to send data with some header field and dynamically allocated data. take for example the simplest case*\n```cpp\n//C++ Example\nstruct Test {\n    int num;\n    char[1] data;\n}\nTest* mem = (Test*)malloc(sizeof(Test) + 49);\nmem-\u003enum = 50;\nchar buffer[50] = \"\u003cyour favourite buffer\u003e\";\nmemcpy(mem-\u003edata, buffer, 50);\n```\nWe can easily define the same structure through struct-ts as\n```typescript\n//on the typescript side\nvar Test = Struct.Create([\n    {name: 'num', type: 'int', val: 0},\n    {name: 'data', type: 'char[*]', val: []}\n])\n\nvar instance = new Test(\u003creceived buff\u003e);\nconsole.log(instance.data.length == 50);\n```\nIf the dynamic type is char[*] then the type of member created is string\notherwise for any other type the typed array is created for native type liek Uint8Array, Int8Array and so on\nfor struct type Struct[] type is created\n\nNote\n--\n*The dynamic size member can only be the last member of the structure and the type of dynamic member should not contain any other dynamic size member*\n\nFor more examples see Test.ts file\n\nAPI\n--\n```typescript\nexport declare class Struct {\n    [key: string]: any;\n\n    //create an instance of Defined struct taking either the named key value member variables or\n    //a serialized buffer serialized through Struct.buffer or otherwise received from C/C++ side\n    constructor(initialzationArgs?: { [key: string]: any} | ArrayBuffer);\n    //Assign a named key value pair object or a serialized buffer\n    assign(namedVal?: {[key: string]: any} | ArrayBuffer): void;\n\n    //static size of the struct.. for structure containing dynamic array member use sizeof function\n    static size: number;\n    //returns the size of the structure\n    //if there is no dynamic array member in the struct then it simply return above size field\n    static sizeof(obj: typeof Struct | Struct): number;\n    //return the offset of member in the structure\n    static offsetof(member: string): number;\n    static CreateMatchHeader(includeMember: {[key: string]: any}): ArrayBuffer;\n    //create typed array (useful when initializing a struct definition)\n    static ArrayOf(type: string | typeof Struct, numElem: number): any[];\n    //serialize the struct instance to array buffer\n    static buffer(obj: Struct): ArrayBuffer;\n\n    //create a structure definition\n    static Create(mems: MemberDefinition[], memfns?: MemberFunctionObject, packing?: number): typeof Struct;\n}\n```\nSupported Native Member Types\n---\n* char, int8, uint8,\n* int16, short uint16, ushort,\n* int32, uint32, int, uint\n* float32, float64. float, double\n\nLicense\n--\n    MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fragarwal33%2Fstruct-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fragarwal33%2Fstruct-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fragarwal33%2Fstruct-ts/lists"}