ya-struct 0.0.9 → 0.0.11

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.
@@ -42,12 +42,12 @@ import nodeUtil from "node:util";
42
42
 
43
43
  const pointerSizeInBitsByDataModel = ({ dataModel }/*: { dataModel: TAbi["dataModel"] }*/)/*: number*/ => {
44
44
  switch (dataModel) {
45
- case "LP64":
46
- return 64;
47
- case "ILP32":
48
- return 32;
49
- default:
50
- throw Error(`unsupported data model "${dataModel}" for pointer size determination`);
45
+ case "LP64":
46
+ return 64;
47
+ case "ILP32":
48
+ return 32;
49
+ default:
50
+ throw Error(`unsupported data model "${dataModel}" for pointer size determination`);
51
51
  }
52
52
  };
53
53
 
@@ -82,53 +82,53 @@ const layoutStruct = ({
82
82
 
83
83
  if (!definition.packed) {
84
84
  switch (normalizedField.type) {
85
- case "integer":
86
- case "float": {
87
- const sizeInBits = normalizedField.sizeInBits;
88
-
89
- if (abi.compiler === "gcc" && abi.dataModel === "ILP32" && sizeInBits === 64) {
90
- // special handling for gcc 64-bit integers on ILP32 data model (alignment to 32 bits)
91
- currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: 32 });
92
- } else {
93
- currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: sizeInBits });
94
- }
95
-
96
- break;
85
+ case "integer":
86
+ case "float": {
87
+ const sizeInBits = normalizedField.sizeInBits;
88
+
89
+ if (abi.compiler === "gcc" && abi.dataModel === "ILP32" && sizeInBits === 64) {
90
+ // special handling for gcc 64-bit integers on ILP32 data model (alignment to 32 bits)
91
+ currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: 32 });
92
+ } else {
93
+ currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: sizeInBits });
97
94
  }
98
- case "array": {
99
95
 
100
- // TODO: this is probably not sufficient for all cases
96
+ break;
97
+ }
98
+ case "array": {
101
99
 
102
- // eslint-disable-next-line no-use-before-define
103
- const { sizeInBits } = layout({ definition: normalizedField.elementType, abi, currentOffsetInBits: 0 });
100
+ // TODO: this is probably not sufficient for all cases
104
101
 
105
- if (abi.compiler === "gcc" && abi.dataModel === "ILP32" && sizeInBits === 64) {
106
- // special handling for gcc 64-bit integers on ILP32 data model (alignment to 32 bits)
107
- currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: 32 });
108
- } else {
109
- currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: sizeInBits });
110
- }
102
+ // eslint-disable-next-line no-use-before-define
103
+ const { sizeInBits } = layout({ definition: normalizedField.elementType, abi, currentOffsetInBits: 0 });
111
104
 
112
- break;
105
+ if (abi.compiler === "gcc" && abi.dataModel === "ILP32" && sizeInBits === 64) {
106
+ // special handling for gcc 64-bit integers on ILP32 data model (alignment to 32 bits)
107
+ currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: 32 });
108
+ } else {
109
+ currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: sizeInBits });
113
110
  }
114
- case "struct": {
115
111
 
116
- if (currentOffsetInBits % 64 !== 0) {
117
- throw Error("nested struct alignment handling not implemented yet");
118
- }
112
+ break;
113
+ }
114
+ case "struct": {
119
115
 
120
- break;
121
- }
122
- case "string": {
123
- // no special alignment needed
124
- break;
125
- }
126
- case "pointer": {
127
- currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: pointerSizeInBits });
128
- break;
116
+ if (currentOffsetInBits % 64 !== 0) {
117
+ throw Error("nested struct alignment handling not implemented yet");
129
118
  }
130
- default:
131
- throw Error(`unsupported field type for struct layout: ${nodeUtil.inspect(field.definition)}`);
119
+
120
+ break;
121
+ }
122
+ case "string": {
123
+ // no special alignment needed
124
+ break;
125
+ }
126
+ case "pointer": {
127
+ currentOffsetInBits = align({ offset: currentOffsetInBits, alignment: pointerSizeInBits });
128
+ break;
129
+ }
130
+ default:
131
+ throw Error(`unsupported field type for struct layout: ${nodeUtil.inspect(field.definition)}`);
132
132
  }
133
133
  }
134
134
 
@@ -19,7 +19,12 @@ type FieldValue<T extends TFieldType> = T extends {
19
19
  } ? StructValue<F & readonly {
20
20
  name: string;
21
21
  definition: TFieldType;
22
- }[]> : never;
22
+ }[]> : T extends {
23
+ type: "c-type";
24
+ cType: "float" | "double" | "long double";
25
+ } ? number : T extends {
26
+ type: "c-type";
27
+ } ? bigint : never;
23
28
  type StructValue<F extends readonly {
24
29
  name: string;
25
30
  definition: TFieldType;
@@ -13,6 +13,8 @@ import { createStructParser } from "./types/struct.js";
13
13
  ? FieldValue<E & TFieldType>[] :
14
14
  T extends { type: "struct"; fields: infer F }
15
15
  ? StructValue<F & readonly { name: string; definition: TFieldType }[]> :
16
+ T extends { type: "c-type"; cType: "float" | "double" | "long double" } ? number :
17
+ T extends { type: "c-type" } ? bigint :
16
18
  never;*/
17
19
 
18
20
  /*type StructValue<
@@ -51,6 +53,7 @@ const define = /*<const T extends TFieldType>*/({ definition }/*: { definition:
51
53
  if (l.type === "struct") {
52
54
  valueParser = createStructParser({
53
55
  layoutedFields: l.fields,
56
+ structOffsetInBits: l.offsetInBits,
54
57
  endianness: abi.endianness
55
58
  }) /*as unknown*/ /*as TValueParser<TParsedValueOfDefinition<T>>*/;
56
59
  } else {
@@ -43,6 +43,7 @@ const createArrayParser = ({
43
43
  for (let i = 0; i < length; i += 1) {
44
44
  const fieldData = data.subarray(i * elementSizeInBytes, (i + 1) * elementSizeInBytes);
45
45
  const fieldValue = fieldParser.parse({ data: fieldData, offsetInBits: 0 });
46
+ // eslint-disable-next-line fp/no-mutating-methods -- performance
46
47
  result.push(fieldValue);
47
48
  }
48
49
 
@@ -42,6 +42,7 @@ const createStringParser = ({ length }/*: { length: number }*/)/*: TStringParser
42
42
  }
43
43
 
44
44
  target.set(encoded, 0);
45
+ // eslint-disable-next-line immutable/no-mutation -- performance
45
46
  target[encoded.length] = 0;
46
47
  };
47
48
 
@@ -2,10 +2,11 @@ import type { TEndianness } from "../common.js";
2
2
  import type { TLayoutedField } from "../layout.js";
3
3
  import type { TValueParser } from "./value.js";
4
4
  type TStructParser = TValueParser<Record<string, unknown>>;
5
- declare const createStructParser: ({ layoutedFields, endianness }: {
5
+ declare const createStructParser: ({ layoutedFields, structOffsetInBits, endianness }: {
6
6
  layoutedFields: (TLayoutedField & {
7
7
  type: "struct";
8
8
  })["fields"];
9
+ structOffsetInBits: number;
9
10
  endianness: TEndianness;
10
11
  }) => TStructParser;
11
12
  export { createStructParser };
@@ -9,11 +9,20 @@ import { createArrayParser } from "./array.js";
9
9
 
10
10
  /*type TStructParser = TValueParser<Record<string, unknown>>;*/
11
11
 
12
+ const subData = ({ data, offsetInBits, sizeInBits }/*: { data: Uint8Array, offsetInBits: number, sizeInBits: number }*/) => {
13
+ return {
14
+ data: new Uint8Array(data.buffer, data.byteOffset + Math.floor(offsetInBits / 8), Math.ceil(sizeInBits / 8)),
15
+ offsetInBits: offsetInBits % 8
16
+ };
17
+ };
18
+
12
19
  const createStructParser = ({
13
20
  layoutedFields,
21
+ structOffsetInBits,
14
22
  endianness
15
23
  }/*: {
16
24
  layoutedFields: (TLayoutedField & { type: "struct" })["fields"];
25
+ structOffsetInBits: number;
17
26
  endianness: TEndianness
18
27
  }*/)/*: TStructParser*/ => {
19
28
 
@@ -37,6 +46,7 @@ const createStructParser = ({
37
46
  if (field.definition.type === "struct") {
38
47
  return createStructParser({
39
48
  layoutedFields: field.definition.fields,
49
+ structOffsetInBits: field.definition.offsetInBits,
40
50
  endianness
41
51
  });
42
52
  }
@@ -70,19 +80,36 @@ const createStructParser = ({
70
80
  layoutedFields.forEach((field, idx) => {
71
81
  const fieldParser = fieldParsers[idx];
72
82
 
83
+ // field definition is absolute, subtracting struct offset gives bit offset inside struct
84
+ // adding data bit offset gives bit offset inside provided data
85
+ const offsetToProvidedDataInBits = field.definition.offsetInBits - structOffsetInBits + offsetInBits;
86
+
73
87
  const offsetInBitsInByte = field.definition.offsetInBits % 8;
74
88
  if (offsetInBitsInByte !== 0) {
75
89
  throw Error("not implemented yet: unaligned field parsing");
76
90
  }
77
91
 
78
- const fieldData = new Uint8Array(data.buffer, data.byteOffset + (field.definition.offsetInBits / 8));
79
- result[field.name] = fieldParser.parse({ data: fieldData, offsetInBits: offsetInBitsInByte });
92
+ const {
93
+ data: fieldData,
94
+ offsetInBits: fieldOffsetInBits
95
+ } = subData({
96
+ data,
97
+ offsetInBits: offsetToProvidedDataInBits,
98
+ sizeInBits: field.definition.sizeInBits
99
+ });
100
+
101
+ // eslint-disable-next-line immutable/no-mutation -- performance
102
+ result[field.name] = fieldParser.parse({ data: fieldData, offsetInBits: fieldOffsetInBits });
80
103
  });
81
104
 
82
105
  return result;
83
106
  };
84
107
 
85
108
  const format/*: TStructParser["format"]*/ = ({ value, target, offsetInBits }) => {
109
+ if (offsetInBits % 8 !== 0) {
110
+ throw Error("unaligned struct formatting not supported yet");
111
+ }
112
+
86
113
  layoutedFields.forEach((field, idx) => {
87
114
  const fieldValue = value[field.name];
88
115
  const fieldParser = fieldParsers[idx];
@@ -92,10 +119,17 @@ const createStructParser = ({
92
119
  throw Error("not implemented yet: unaligned field formatting");
93
120
  }
94
121
 
95
- const fieldTarget = new Uint8Array(target.buffer, target.byteOffset + (field.definition.offsetInBits / 8));
122
+ const {
123
+ data: fieldTarget,
124
+ offsetInBits: fieldOffsetInBits
125
+ } = subData({
126
+ data: target,
127
+ offsetInBits: field.definition.offsetInBits - structOffsetInBits + offsetInBits,
128
+ sizeInBits: field.definition.sizeInBits
129
+ });
96
130
 
97
131
  try {
98
- fieldParser.format({ value: fieldValue, target: fieldTarget, offsetInBits });
132
+ fieldParser.format({ value: fieldValue, target: fieldTarget, offsetInBits: fieldOffsetInBits });
99
133
  } catch (ex) {
100
134
  throw Error(`failed to format field "${field.name}"`, { cause: ex });
101
135
  }
package/package.json CHANGED
@@ -1,26 +1,31 @@
1
1
  {
2
- "version": "0.0.9",
2
+ "version": "0.0.11",
3
3
  "name": "ya-struct",
4
4
  "type": "module",
5
5
  "description": "Yet Another Node.js Structure API",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "main": "dist/lib/index.js",
7
10
  "scripts": {
8
11
  "build": "rm -rf dist/ && deno-node-build --root . --out dist/ --entry lib/index.ts",
9
12
  "test": "c8 --reporter lcov --reporter html --reporter text --all --src lib/ mocha test/**/*.ts",
10
- "lint": "eslint ."
13
+ "lint": "eslint .",
14
+ "type-check": "tsc --noEmit",
15
+ "update-deps": "npm-check-updates -u"
11
16
  },
12
17
  "dependencies": {},
13
18
  "devDependencies": {
14
- "@eslint/js": "^9.39.2",
19
+ "@eslint/js": "^10.0.1",
20
+ "@k13engineering/eslint-rules": "^0.0.5",
15
21
  "@k13engineering/releasetool": "^0.0.5",
16
22
  "@types/mocha": "^10.0.10",
17
- "@types/node": "^25.0.3",
18
- "c8": "^10.1.3",
23
+ "@types/node": "^25.6.0",
24
+ "c8": "^11.0.0",
19
25
  "deno-node": "^0.0.12",
20
- "eslint": "^9.39.2",
21
26
  "mocha": "^11.7.5",
22
27
  "tmp-promise": "^3.0.3",
23
- "typescript-eslint": "^8.52.0"
28
+ "typescript": "^6.0.3"
24
29
  },
25
30
  "repository": {
26
31
  "type": "git",
package/.editorconfig DELETED
@@ -1,8 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- end_of_line = lf
5
- insert_final_newline = true
6
- indent_style = space
7
- indent_size = 2
8
- trim_trailing_whitespace = true
@@ -1,23 +0,0 @@
1
- name: CI
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- ci:
7
- runs-on: ubuntu-latest
8
- name: Continous integration tests
9
- steps:
10
- - uses: actions/checkout@v4
11
- - uses: actions/setup-node@v4
12
- with:
13
- node-version: '24'
14
- - name: Install libc6-dev-i386
15
- run: sudo apt-get update && sudo apt-get install -y libc6-dev-i386
16
- - name: Install dependencies
17
- run: npm ci
18
- - name: Build
19
- run: npm run build
20
- - name: Run tests
21
- run: npm run test
22
- - name: Verify code with linter
23
- run: npm run lint
@@ -1,28 +0,0 @@
1
- name: Publish Package to npmjs
2
- on:
3
- push:
4
- tags:
5
- - '*'
6
-
7
- permissions:
8
- id-token: write # Required for OIDC
9
- contents: read
10
-
11
- jobs:
12
- publish:
13
- runs-on: ubuntu-latest
14
- permissions:
15
- contents: read
16
- id-token: write
17
- steps:
18
- - uses: actions/checkout@v4
19
- # Setup .npmrc file to publish to npm
20
- - uses: actions/setup-node@v4
21
- with:
22
- node-version: '24'
23
- registry-url: 'https://registry.npmjs.org'
24
- - run: npm ci
25
- - run: npm run build
26
- - run: node node_modules/.bin/releasetool merge --local-package-json package.json --npm-package-json package.npm.json --output package.json
27
- - run: node node_modules/.bin/releasetool patch-version --package-json package.json --package-version ${{ github.ref_name }}
28
- - run: npm publish
package/eslint.config.js DELETED
@@ -1,127 +0,0 @@
1
- /* c8 ignore start */
2
- import eslint from "@eslint/js";
3
- import tseslint from "typescript-eslint";
4
-
5
- export default tseslint.config(
6
- {
7
- ignores: [
8
- "dist/**/*"
9
- ]
10
- },
11
- eslint.configs.recommended,
12
- ...tseslint.configs.recommended,
13
- {
14
- rules: {
15
- "global-require": "off",
16
- "quote-props": ["warn", "consistent-as-needed"],
17
-
18
- "quotes": ["error", "double", {
19
- allowTemplateLiterals: true,
20
- }],
21
-
22
- "no-plusplus": "error",
23
- "no-nested-ternary": "error",
24
- "no-multiple-empty-lines": "error",
25
- "no-inline-comments": "error",
26
- "no-lonely-if": "error",
27
- "no-array-constructor": "error",
28
- "no-delete-var": "error",
29
- "no-param-reassign": "error",
30
- "no-return-assign": "error",
31
- "no-import-assign": "error",
32
- "no-multi-assign": "error",
33
- "keyword-spacing": "error",
34
-
35
- "max-len": ["warn", {
36
- code: 140,
37
- }],
38
-
39
- "max-params": ["error", 4],
40
- "max-statements": ["error", 15],
41
- "no-loss-of-precision": "error",
42
- "no-unreachable-loop": "error",
43
- "require-atomic-updates": "error",
44
- "complexity": ["error", 4],
45
-
46
- "max-statements-per-line": ["error", {
47
- max: 1,
48
- }],
49
-
50
- "no-tabs": "error",
51
- "no-underscore-dangle": "error",
52
- "no-negated-condition": "error",
53
- "no-use-before-define": "error",
54
-
55
- "no-shadow": "off",
56
- "@typescript-eslint/no-shadow": "error",
57
-
58
- "no-labels": "error",
59
- "no-throw-literal": "error",
60
- "default-case": "error",
61
- "default-case-last": "error",
62
- "no-caller": "error",
63
- "no-eval": "error",
64
- "no-implied-eval": "error",
65
- "no-new": "error",
66
- "no-new-func": "error",
67
- "no-new-object": "error",
68
- "no-new-wrappers": "error",
69
- "no-useless-concat": "error",
70
-
71
- "no-unused-vars": "off",
72
- "@typescript-eslint/no-unused-vars": ["error", {
73
- ignoreRestSiblings: true,
74
- }],
75
-
76
- "array-bracket-newline": ["error", "consistent"],
77
- "func-names": ["error", "never"],
78
-
79
- "func-style": ["error", "expression", {
80
- allowArrowFunctions: true,
81
- }],
82
-
83
- "max-depth": ["error", 4],
84
- "arrow-parens": "error",
85
- "no-confusing-arrow": "error",
86
- "prefer-const": "error",
87
- "rest-spread-spacing": ["error", "never"],
88
- "template-curly-spacing": ["error", "never"],
89
- "prefer-rest-params": "error",
90
- "prefer-spread": "error",
91
- "prefer-template": "error",
92
- "object-shorthand": ["error", "properties"],
93
- "no-var": "error",
94
- "no-useless-computed-key": "error",
95
- "array-callback-return": "error",
96
- "consistent-return": "error",
97
- "dot-notation": "error",
98
- "eqeqeq": "error",
99
- "no-eq-null": "error",
100
- "no-implicit-coercion": "error",
101
- "no-multi-spaces": "error",
102
- "no-proto": "error",
103
- "yoda": "error",
104
- "indent": ["error", 2, { SwitchCase: 1 }],
105
- "object-curly-spacing": ["error", "always"],
106
-
107
- "object-curly-newline": ["error", {
108
- consistent: true,
109
- multiline: true,
110
- }],
111
-
112
- "space-before-blocks": "error",
113
- "space-before-function-paren": ["error", "always"],
114
- "spaced-comment": "error",
115
- "no-whitespace-before-property": "error",
116
-
117
- "brace-style": ["error", "1tbs", {
118
- allowSingleLine: false,
119
- }],
120
-
121
- "eol-last": ["error", "always"],
122
- "func-call-spacing": ["error", "never"],
123
- "semi": ["error", "always"],
124
- }
125
- }
126
- );
127
- /* c8 ignore stop */
package/lib/bit-buffer.ts DELETED
@@ -1,70 +0,0 @@
1
- type TBitBuffer = {
2
- sizeInBits: number;
3
- slice: (args: { startOffsetInBits: number, endOffsetInBits: number }) => TBitBuffer;
4
- readBits: (args: { offsetInBits: number, lengthInBits: number }) => { data: Uint8Array, offsetInBits: number };
5
- }
6
-
7
- const bitBufferFromUint8Array = ({
8
- data,
9
- offsetInBits: dataOffsetInBits,
10
- sizeInBits
11
- }: {
12
- data: Uint8Array,
13
- offsetInBits: number,
14
- sizeInBits: number
15
- }): TBitBuffer => {
16
-
17
- const slice: TBitBuffer["slice"] = ({ startOffsetInBits, endOffsetInBits }) => {
18
- const totalStartOffsetInBits = dataOffsetInBits + startOffsetInBits;
19
- const totalEndOffsetInBits = dataOffsetInBits + endOffsetInBits;
20
-
21
- const byteStartOffset = (totalStartOffsetInBits) / 8;
22
- const byteEndOffset = (totalEndOffsetInBits) / 8;
23
-
24
- const newData = data.slice(byteStartOffset, byteEndOffset);
25
- const newOffsetInBits = totalStartOffsetInBits % 8;
26
-
27
- return bitBufferFromUint8Array({
28
- data: newData,
29
- offsetInBits: newOffsetInBits,
30
- sizeInBits: endOffsetInBits - startOffsetInBits
31
- });
32
- };
33
-
34
- const readBits: TBitBuffer["readBits"] = ({ offsetInBits: readOffsetInBits, lengthInBits }) => {
35
- const totalOffsetInBits = dataOffsetInBits + readOffsetInBits;
36
- const totalEndOffsetInBits = totalOffsetInBits + lengthInBits;
37
-
38
- const byteOffset = (totalOffsetInBits) / 8;
39
- const byteEndOffset = (totalEndOffsetInBits) / 8;
40
-
41
- const bitData = data.slice(byteOffset, byteEndOffset);
42
- const bitOffset = totalOffsetInBits % 8;
43
-
44
- return {
45
- offsetInBits: bitOffset,
46
- data: bitData
47
- };
48
- };
49
-
50
- return {
51
- sizeInBits,
52
- readBits,
53
- slice,
54
- };
55
- };
56
-
57
- const createBitBuffer = ({ sizeInBits, offsetInBits }: { sizeInBits: number, offsetInBits: number }) => {
58
- const byteLength = Math.ceil(sizeInBits / 8);
59
- const buffer = new Uint8Array(byteLength);
60
- return bitBufferFromUint8Array({ data: buffer, offsetInBits, sizeInBits });
61
- };
62
-
63
- export {
64
- createBitBuffer,
65
- bitBufferFromUint8Array
66
- };
67
-
68
- export type {
69
- TBitBuffer
70
- };
package/lib/common.ts DELETED
@@ -1,30 +0,0 @@
1
- type TDataModel = "LP64" | "ILP32";
2
- type TCompiler = "gcc";
3
- type TEndianness = "little" | "big";
4
-
5
- type TAbi = {
6
- endianness: TEndianness;
7
- compiler: TCompiler;
8
- dataModel: TDataModel;
9
- };
10
-
11
- const align = ({ offset, alignment }: { offset: number; alignment: number }): number => {
12
- const remainder = offset % alignment;
13
- if (remainder === 0) {
14
- return offset;
15
- }
16
-
17
- return offset + (alignment - remainder);
18
- };
19
-
20
- export type {
21
- TDataModel,
22
- TCompiler,
23
- TEndianness,
24
-
25
- TAbi
26
- };
27
-
28
- export {
29
- align
30
- };
package/lib/index.ts DELETED
@@ -1,21 +0,0 @@
1
- import { define } from "./parser.ts";
2
- import { types } from "./types/index.ts";
3
-
4
- import type {
5
- TAbi,
6
- TCompiler,
7
- TDataModel,
8
- TEndianness,
9
- } from "./common.ts";
10
-
11
- export {
12
- define,
13
- types
14
- };
15
-
16
- export type {
17
- TAbi,
18
- TEndianness,
19
- TCompiler,
20
- TDataModel
21
- };