ya-struct 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/.editorconfig +8 -0
  2. package/.github/workflows/ci.yml +23 -0
  3. package/.github/workflows/npm-publish.yml +28 -0
  4. package/README.md +150 -18
  5. package/dist/lib/common.d.ts +14 -0
  6. package/dist/lib/common.js +30 -0
  7. package/dist/lib/index.d.ts +5 -0
  8. package/dist/lib/index.js +21 -0
  9. package/dist/lib/layout.d.ts +48 -0
  10. package/dist/lib/layout.js +262 -0
  11. package/dist/lib/parser.d.ts +51 -0
  12. package/dist/lib/parser.js +87 -0
  13. package/dist/lib/types/array.d.ts +10 -0
  14. package/dist/lib/types/array.js +90 -0
  15. package/dist/lib/types/c-types.d.ts +13 -0
  16. package/dist/lib/types/c-types.js +222 -0
  17. package/dist/lib/types/index.d.ts +93 -0
  18. package/dist/lib/types/index.js +122 -0
  19. package/dist/lib/types/integer.d.ts +9 -0
  20. package/dist/lib/types/integer.js +160 -0
  21. package/dist/lib/types/pointer.d.ts +8 -0
  22. package/dist/lib/types/pointer.js +27 -0
  23. package/dist/lib/types/string.d.ts +6 -0
  24. package/dist/lib/types/string.js +56 -0
  25. package/dist/lib/types/struct.d.ts +11 -0
  26. package/dist/lib/types/struct.js +113 -0
  27. package/dist/lib/types/value.d.ts +12 -0
  28. package/dist/lib/types/value.js +8 -0
  29. package/eslint.config.js +127 -0
  30. package/lib/bit-buffer.ts +70 -0
  31. package/lib/common.ts +30 -0
  32. package/lib/index.ts +21 -0
  33. package/lib/layout.ts +262 -0
  34. package/lib/parser.ts +87 -0
  35. package/lib/types/array.ts +90 -0
  36. package/lib/types/c-types.ts +222 -0
  37. package/lib/types/index.ts +122 -0
  38. package/lib/types/integer.ts +160 -0
  39. package/lib/types/pointer.ts +27 -0
  40. package/lib/types/string.ts +56 -0
  41. package/lib/types/struct.ts +113 -0
  42. package/lib/types/value.ts +8 -0
  43. package/package.json +19 -15
  44. package/package.npm.json +25 -0
  45. package/samples/basic.ts +40 -0
  46. package/test/c-structs.ts +399 -0
  47. package/test/compile-util.ts +92 -0
  48. package/tsconfig.json +10 -0
  49. package/.eslintrc +0 -92
  50. package/.github/workflows/CI.yml +0 -39
  51. package/.prettierrc +0 -5
  52. package/lib/builder.js +0 -62
  53. package/lib/index.js +0 -159
  54. package/lib/marshaller.js +0 -88
  55. package/lib/refbuf.js +0 -11
  56. package/lib/types/basic.js +0 -200
  57. package/lib/types/ctypes.js +0 -160
  58. package/test/abi.js +0 -203
  59. package/test/basic.js +0 -92
  60. package/test/ctypes.js +0 -166
  61. package/test/ref.js +0 -35
@@ -0,0 +1,122 @@
1
+ import type { TAbi } from "../common.ts";
2
+
3
+ type TCFieldType =
4
+ "char" | "unsigned char" |
5
+ "short" | "unsigned short" |
6
+ "int" | "unsigned int" |
7
+ "long" | "unsigned long" |
8
+ "long long" | "unsigned long long" |
9
+ "float" | "double" | "long double";
10
+
11
+ type TFieldType = {
12
+ readonly type: "integer";
13
+ readonly sizeInBits: number;
14
+ readonly signed: boolean;
15
+ readonly fixedAbi: Partial<TAbi>;
16
+ } | {
17
+ readonly type: "float";
18
+ readonly sizeInBits: number;
19
+ readonly fixedAbi: Partial<TAbi>;
20
+ } | {
21
+ readonly type: "pointer";
22
+ readonly fixedAbi: Partial<TAbi>;
23
+ } | {
24
+ readonly type: "array";
25
+ readonly elementType: TFieldType;
26
+ readonly length: number;
27
+ } | {
28
+ readonly type: "struct";
29
+ readonly fields: readonly { readonly name: string; readonly definition: TFieldType }[];
30
+ readonly packed: boolean;
31
+ readonly fixedAbi: Partial<TAbi>;
32
+ } | {
33
+ readonly type: "string";
34
+ readonly charSizeInBits: number;
35
+ readonly nullTerminatorMandatory: boolean;
36
+ readonly length: number;
37
+ } | {
38
+ readonly type: "c-type";
39
+ readonly cType: TCFieldType;
40
+ readonly fixedAbi: Partial<TAbi>;
41
+ };
42
+
43
+ type TBasicFieldType = Exclude<TFieldType, { type: "c-type" }>;
44
+ type TPrimitiveBasicFieldType = Exclude<TBasicFieldType, { type: "array" } | { type: "struct" } | { type: "string" }>;
45
+
46
+ const Int16: TFieldType = {
47
+ type: "integer",
48
+ sizeInBits: 16,
49
+ signed: true,
50
+ fixedAbi: {}
51
+ } as const;
52
+
53
+ const UInt16: TFieldType = {
54
+ type: "integer",
55
+ sizeInBits: 16,
56
+ signed: false,
57
+ fixedAbi: {}
58
+ } as const;
59
+
60
+ const Int32: TFieldType = {
61
+ type: "integer",
62
+ sizeInBits: 32,
63
+ signed: true,
64
+ fixedAbi: {}
65
+ } as const;
66
+
67
+ const Int64: TFieldType = {
68
+ type: "integer",
69
+ sizeInBits: 64,
70
+ signed: true,
71
+ fixedAbi: {}
72
+ } as const;
73
+
74
+ const UInt32: TFieldType = {
75
+ type: "integer",
76
+ sizeInBits: 32,
77
+ signed: false,
78
+ fixedAbi: {}
79
+ } as const;
80
+
81
+ const UInt64: TFieldType = {
82
+ type: "integer",
83
+ sizeInBits: 64,
84
+ signed: false,
85
+ fixedAbi: {}
86
+ } as const;
87
+
88
+ const ascii = ({ length }: { length: number }): TFieldType => {
89
+ return {
90
+ type: "string",
91
+ charSizeInBits: 8,
92
+ nullTerminatorMandatory: true,
93
+ length
94
+ } as const;
95
+ };
96
+
97
+ const pointer: TFieldType = {
98
+ type: "pointer",
99
+ fixedAbi: {}
100
+ } as const;
101
+
102
+ export type {
103
+ TBasicFieldType,
104
+ TFieldType,
105
+ TCFieldType,
106
+ TPrimitiveBasicFieldType
107
+ };
108
+
109
+ const types = {
110
+ Int16,
111
+ UInt16,
112
+ Int32,
113
+ Int64,
114
+ UInt32,
115
+ UInt64,
116
+ ascii,
117
+ pointer
118
+ } as const;
119
+
120
+ export {
121
+ types
122
+ };
@@ -0,0 +1,160 @@
1
+ import type { TEndianness } from "../common.ts";
2
+ import type { TValueParser } from "./value.ts";
3
+
4
+ type TIntegerParser = TValueParser<bigint>;
5
+
6
+ const createIntegerParser = ({
7
+ sizeInBits,
8
+ signed,
9
+ endianness
10
+ }: {
11
+ sizeInBits: number,
12
+ signed: boolean,
13
+ endianness: TEndianness
14
+ }): TIntegerParser => {
15
+
16
+ // eslint-disable-next-line max-statements, complexity
17
+ const parse: TIntegerParser["parse"] = ({ data, offsetInBits }) => {
18
+ if (data.length < sizeInBits / 8) {
19
+ throw Error("BUG: not enough data for integer parsing");
20
+ }
21
+
22
+ if (offsetInBits !== 0) {
23
+ throw Error("unaligned data in integer parser, not supported yet");
24
+ }
25
+
26
+ const view = new DataView(data.buffer, data.byteOffset);
27
+
28
+ if (sizeInBits === 8) {
29
+ if (signed) {
30
+ return BigInt(view.getInt8(0));
31
+ } else {
32
+ return BigInt(view.getUint8(0));
33
+ }
34
+ } else if (sizeInBits === 16) {
35
+ if (signed) {
36
+ if (endianness === "little") {
37
+ return BigInt(view.getInt16(0, true));
38
+ } else {
39
+ return BigInt(view.getInt16(0, false));
40
+ }
41
+ } else {
42
+ // eslint-disable-next-line no-lonely-if
43
+ if (endianness === "little") {
44
+ return BigInt(view.getUint16(0, true));
45
+ } else {
46
+ return BigInt(view.getUint16(0, false));
47
+ }
48
+ }
49
+ } else if (sizeInBits === 32) {
50
+ if (signed) {
51
+ if (endianness === "little") {
52
+ return BigInt(view.getInt32(0, true));
53
+ } else {
54
+ return BigInt(view.getInt32(0, false));
55
+ }
56
+ } else {
57
+ // eslint-disable-next-line no-lonely-if
58
+ if (endianness === "little") {
59
+ return BigInt(view.getUint32(0, true));
60
+ } else {
61
+ return BigInt(view.getUint32(0, false));
62
+ }
63
+ }
64
+ } else if (sizeInBits === 64) {
65
+ if (signed) {
66
+ if (endianness === "little") {
67
+ return view.getBigInt64(0, true);
68
+ } else {
69
+ return view.getBigInt64(0, false);
70
+ }
71
+ } else {
72
+ // eslint-disable-next-line no-lonely-if
73
+ if (endianness === "little") {
74
+ return view.getBigUint64(0, true);
75
+ } else {
76
+ return view.getBigUint64(0, false);
77
+ }
78
+ }
79
+ }
80
+
81
+ throw Error("not implemented yet");
82
+ };
83
+
84
+ // eslint-disable-next-line max-statements, complexity
85
+ const format: TIntegerParser["format"] = ({ value, target, offsetInBits }) => {
86
+ if (offsetInBits !== 0) {
87
+ throw Error("unaligned data in integer formatter, not supported yet");
88
+ }
89
+
90
+ if (typeof value !== "bigint") {
91
+ throw Error("invalid value type for integer formatter");
92
+ }
93
+
94
+ const view = new DataView(target.buffer, target.byteOffset);
95
+
96
+ if (sizeInBits === 8) {
97
+ if (signed) {
98
+ view.setInt8(0, Number(value));
99
+ } else {
100
+ view.setUint8(0, Number(value));
101
+ }
102
+ } else if (sizeInBits === 16) {
103
+ if (signed) {
104
+ if (endianness === "little") {
105
+ view.setInt16(0, Number(value), true);
106
+ } else {
107
+ view.setInt16(0, Number(value), false);
108
+ }
109
+ } else {
110
+ // eslint-disable-next-line no-lonely-if
111
+ if (endianness === "little") {
112
+ view.setUint16(0, Number(value), true);
113
+ } else {
114
+ view.setUint16(0, Number(value), false);
115
+ }
116
+ }
117
+ } else if (sizeInBits === 32) {
118
+ if (signed) {
119
+ if (endianness === "little") {
120
+ view.setInt32(0, Number(value), true);
121
+ } else {
122
+ view.setInt32(0, Number(value), false);
123
+ }
124
+ } else {
125
+ // eslint-disable-next-line no-lonely-if
126
+ if (endianness === "little") {
127
+ view.setUint32(0, Number(value), true);
128
+ } else {
129
+ view.setUint32(0, Number(value), false);
130
+ }
131
+ }
132
+ } else if (sizeInBits === 64) {
133
+ if (signed) {
134
+ if (endianness === "little") {
135
+ view.setBigInt64(0, value, true);
136
+ } else {
137
+ view.setBigInt64(0, value, false);
138
+ }
139
+ } else {
140
+ // eslint-disable-next-line no-lonely-if
141
+ if (endianness === "little") {
142
+ view.setBigUint64(0, value, true);
143
+ } else {
144
+ view.setBigUint64(0, value, false);
145
+ }
146
+ }
147
+ } else {
148
+ throw Error("not implemented yet");
149
+ }
150
+ };
151
+
152
+ return {
153
+ parse,
154
+ format
155
+ };
156
+ };
157
+
158
+ export {
159
+ createIntegerParser
160
+ };
@@ -0,0 +1,27 @@
1
+ import type { TEndianness } from "../common.ts";
2
+ import { createIntegerParser } from "./integer.ts";
3
+ import type { TValueParser } from "./value.ts";
4
+
5
+ type TPointerParser = TValueParser<bigint>;
6
+
7
+ const createPointerParser = ({ sizeInBits, endianness }: { sizeInBits: number, endianness: TEndianness }): TPointerParser => {
8
+
9
+ const integerParser = createIntegerParser({ sizeInBits, signed: false, endianness });
10
+
11
+ const parse: TPointerParser["parse"] = ({ data, offsetInBits }) => {
12
+ return integerParser.parse({ data, offsetInBits });
13
+ };
14
+
15
+ const format: TPointerParser["format"] = ({ value, target, offsetInBits }) => {
16
+ return integerParser.format({ value, target, offsetInBits });
17
+ };
18
+
19
+ return {
20
+ parse,
21
+ format
22
+ };
23
+ };
24
+
25
+ export {
26
+ createPointerParser
27
+ };
@@ -0,0 +1,56 @@
1
+ import type { TValueParser } from "./value.ts";
2
+
3
+ type TStringParser = TValueParser<string>;
4
+
5
+ const createStringParser = ({ length }: { length: number }): TStringParser => {
6
+
7
+ const decoder = new TextDecoder();
8
+ const encoder = new TextEncoder();
9
+
10
+ const parse: TStringParser["parse"] = ({ data, offsetInBits }) => {
11
+ if (offsetInBits !== 0) {
12
+ throw Error("unaligned data in string parser, not supported yet");
13
+ }
14
+
15
+ if (data.length < length) {
16
+ throw Error("not enough data for string parsing");
17
+ }
18
+
19
+ const stringData = data.subarray(0, length);
20
+ const nullTerminatorIndex = stringData.indexOf(0);
21
+
22
+ if (nullTerminatorIndex < 0) {
23
+ return decoder.decode(stringData);
24
+ }
25
+
26
+ return decoder.decode(stringData.subarray(0, nullTerminatorIndex));
27
+ };
28
+
29
+ const format: TStringParser["format"] = ({ value, target, offsetInBits }) => {
30
+ if (offsetInBits !== 0) {
31
+ throw Error("unaligned data in string formatter, not supported yet");
32
+ }
33
+
34
+ if (target.length < length) {
35
+ throw Error("not enough space in target for string formatting");
36
+ }
37
+
38
+ const encoded = encoder.encode(value);
39
+
40
+ if (encoded.length + 1 >= length) {
41
+ throw Error("string too long to fit in target");
42
+ }
43
+
44
+ target.set(encoded, 0);
45
+ target[encoded.length] = 0;
46
+ };
47
+
48
+ return {
49
+ parse,
50
+ format
51
+ };
52
+ };
53
+
54
+ export {
55
+ createStringParser
56
+ };
@@ -0,0 +1,113 @@
1
+ import type { TEndianness } from "../common.ts";
2
+ import type { TLayoutedField } from "../layout.ts";
3
+ import { createIntegerParser } from "./integer.ts";
4
+ import { createPointerParser } from "./pointer.ts";
5
+ import { createStringParser } from "./string.ts";
6
+ import { createArrayParser } from "./array.ts";
7
+ import type { TValueParser } from "./value.ts";
8
+ import type { TFieldType } from "./index.ts";
9
+
10
+ type TStructParser = TValueParser<Record<string, unknown>>;
11
+
12
+ const createStructParser = ({
13
+ layoutedFields,
14
+ endianness
15
+ }: {
16
+ layoutedFields: (TLayoutedField & { type: "struct" })["fields"];
17
+ endianness: TEndianness
18
+ }): TStructParser => {
19
+
20
+ // eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any
21
+ const fieldParsers: TValueParser<any>[] = layoutedFields.map((field) => {
22
+ if (field.definition.type === "integer") {
23
+ return createIntegerParser({
24
+ sizeInBits: field.definition.sizeInBits,
25
+ signed: field.definition.signed,
26
+ endianness
27
+ });
28
+ }
29
+
30
+ if (field.definition.type === "pointer") {
31
+ return createPointerParser({
32
+ sizeInBits: field.definition.sizeInBits,
33
+ endianness
34
+ });
35
+ }
36
+
37
+ if (field.definition.type === "struct") {
38
+ return createStructParser({
39
+ layoutedFields: field.definition.fields,
40
+ endianness
41
+ });
42
+ }
43
+
44
+ if (field.definition.type === "string") {
45
+ return createStringParser({
46
+ length: field.definition.length,
47
+ });
48
+ }
49
+
50
+ if (field.definition.type === "array") {
51
+ return createArrayParser({
52
+ // TODO: cast should not be necessary
53
+ elementType: field.definition.elementType as TFieldType,
54
+ endianness,
55
+ length: field.definition.length
56
+ });
57
+ }
58
+
59
+ throw Error("not implemented yet");
60
+ });
61
+
62
+ const parse: TStructParser["parse"] = ({ data, offsetInBits }) => {
63
+ if (offsetInBits !== 0) {
64
+ throw Error("unaligned struct parsing not supported yet");
65
+ }
66
+
67
+ // eslint-disable-next-line prefer-const
68
+ let result: Record<string, unknown> = {};
69
+
70
+ layoutedFields.forEach((field, idx) => {
71
+ const fieldParser = fieldParsers[idx];
72
+
73
+ const offsetInBitsInByte = field.definition.offsetInBits % 8;
74
+ if (offsetInBitsInByte !== 0) {
75
+ throw Error("not implemented yet: unaligned field parsing");
76
+ }
77
+
78
+ const fieldData = new Uint8Array(data.buffer, data.byteOffset + (field.definition.offsetInBits / 8));
79
+ result[field.name] = fieldParser.parse({ data: fieldData, offsetInBits: offsetInBitsInByte });
80
+ });
81
+
82
+ return result;
83
+ };
84
+
85
+ const format: TStructParser["format"] = ({ value, target, offsetInBits }) => {
86
+ layoutedFields.forEach((field, idx) => {
87
+ const fieldValue = value[field.name];
88
+ const fieldParser = fieldParsers[idx];
89
+
90
+ const offsetInBitsInByte = field.definition.offsetInBits % 8;
91
+ if (offsetInBitsInByte !== 0) {
92
+ throw Error("not implemented yet: unaligned field formatting");
93
+ }
94
+
95
+ const fieldTarget = new Uint8Array(target.buffer, target.byteOffset + (field.definition.offsetInBits / 8));
96
+
97
+ try {
98
+ fieldParser.format({ value: fieldValue, target: fieldTarget, offsetInBits });
99
+ } catch (ex) {
100
+ throw Error(`failed to format field "${field.name}"`, { cause: ex });
101
+ }
102
+ });
103
+ };
104
+
105
+ return {
106
+ parse,
107
+ format
108
+ };
109
+ };
110
+
111
+ export {
112
+ createStructParser
113
+ };
@@ -0,0 +1,8 @@
1
+ type TValueParser<T> = {
2
+ parse: ({ data }: { data: Uint8Array, offsetInBits: number }) => T;
3
+ format: ({ value, target }: { value: T, target: Uint8Array, offsetInBits: number }) => void;
4
+ };
5
+
6
+ export type {
7
+ TValueParser
8
+ };
package/package.json CHANGED
@@ -1,12 +1,26 @@
1
1
  {
2
+ "version": "0.0.6",
2
3
  "name": "ya-struct",
3
4
  "type": "module",
4
- "version": "0.0.5",
5
5
  "description": "Yet Another Node.js Structure API",
6
- "main": "lib/index.js",
6
+ "main": "dist/lib/index.js",
7
7
  "scripts": {
8
- "test": "c8 --reporter lcov --reporter html --reporter text mocha",
9
- "eslint": "eslint ."
8
+ "build": "rm -rf dist/ && deno-node-build --root . --out dist/ --entry lib/index.ts",
9
+ "test": "c8 --reporter lcov --reporter html --reporter text --all --src lib/ mocha test/**/*.ts",
10
+ "lint": "eslint ."
11
+ },
12
+ "dependencies": {},
13
+ "devDependencies": {
14
+ "@eslint/js": "^9.39.2",
15
+ "@k13engineering/releasetool": "^0.0.5",
16
+ "@types/mocha": "^10.0.10",
17
+ "@types/node": "^25.0.3",
18
+ "c8": "^10.1.3",
19
+ "deno-node": "^0.0.12",
20
+ "eslint": "^9.39.2",
21
+ "mocha": "^11.7.5",
22
+ "tmp-promise": "^3.0.3",
23
+ "typescript-eslint": "^8.52.0"
10
24
  },
11
25
  "repository": {
12
26
  "type": "git",
@@ -27,15 +41,5 @@
27
41
  "bugs": {
28
42
  "url": "https://github.com/k13-engineering/node-ya-struct/issues"
29
43
  },
30
- "homepage": "https://github.com/k13-engineering/node-ya-struct#readme",
31
- "dependencies": {
32
- "buffer2address": "^0.0.2"
33
- },
34
- "devDependencies": {
35
- "c8": "^7.9.0",
36
- "eslint": "^7.32.0",
37
- "mocha": "^9.1.1",
38
- "node-archibald": "^0.0.6",
39
- "tmp-promise": "^3.0.3"
40
- }
44
+ "homepage": "https://github.com/k13-engineering/node-ya-struct#readme"
41
45
  }
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "ya-struct",
3
+ "description": "Yet Another Node.js Structure API",
4
+ "main": "dist/lib/index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/k13-engineering/node-ya-struct.git"
8
+ },
9
+ "keywords": [
10
+ "structure",
11
+ "struct",
12
+ "abi",
13
+ "endian",
14
+ "alignment",
15
+ "c",
16
+ "c++",
17
+ "layout"
18
+ ],
19
+ "author": "Simon Kadisch",
20
+ "license": "LGPL 2.1",
21
+ "bugs": {
22
+ "url": "https://github.com/k13-engineering/node-ya-struct/issues"
23
+ },
24
+ "homepage": "https://github.com/k13-engineering/node-ya-struct#readme"
25
+ }
@@ -0,0 +1,40 @@
1
+ import { define, types } from "../lib/index.ts";
2
+
3
+ const def = define({
4
+ definition: {
5
+ type: "struct",
6
+ packed: false,
7
+ fixedAbi: {},
8
+ fields: [
9
+ { name: "a", definition: types.Int16 },
10
+ { name: "b", definition: types.UInt16 },
11
+ { name: "c", definition: types.UInt32 },
12
+ ]
13
+ }
14
+ });
15
+
16
+ const parser = def.parser({
17
+ abi: {
18
+ endianness: "little",
19
+ dataModel: "LP64",
20
+ compiler: "gcc",
21
+ }
22
+ });
23
+
24
+ const value: ReturnType<typeof parser.parse> = {
25
+ a: 0n,
26
+ b: 1n,
27
+ c: 2n,
28
+ };
29
+
30
+ console.log("value", value);
31
+
32
+ const formatted = parser.format({
33
+ value
34
+ });
35
+
36
+ console.log("formatted", formatted);
37
+
38
+ const reparsed = parser.parse({ data: formatted });
39
+
40
+ console.log("reparsed", reparsed);