@glion/lint-max-message-size 0.14.1

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rethink Health
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # @glion/lint-max-message-size
2
+
3
+ A [`unified`][github-unified] lint rule that warns when an HL7v2 message exceeds a configurable maximum size in bytes or number of segments.
4
+
5
+ - **Default max size:** 10,000,000 bytes (10MB)
6
+ - **Optional:** Limit the number of segments
7
+
8
+ ## What is this?
9
+
10
+ This package validates the **maximum message size** in HL7v2 syntax trees produced by parsers like [`@glion/parser`][github-hl7v2-parser].
11
+
12
+ It reports a message when the HL7v2 message exceeds a configurable maximum size in bytes (default: 1,000,000 bytes) or, optionally, a maximum number of segments.
13
+
14
+ ## When should I use this?
15
+
16
+ Use this rule to enforce a maximum HL7v2 message size (in bytes) and/or segment count, helping to prevent oversized messages that may cause downstream processing issues or violate system constraints.
17
+
18
+ This linter is useful for:
19
+
20
+ - Ensuring HL7v2 messages do not exceed a safe or expected size limit (default: 10MB).
21
+ - Optionally restricting the number of segments in a message to catch unusually large or malformed messages.
22
+ - Improving reliability and performance by catching messages that could cause resource exhaustion or be rejected by receivers.
23
+
24
+ Configure the rule to match your system's requirements for message size and segment count.
25
+
26
+ ## Install
27
+
28
+ This package is **ESM only**. In Node.js (v16+), install with npm:
29
+
30
+ ```sh
31
+ npm install @glion/lint-max-message-size
32
+ ```
33
+
34
+ ## Use
35
+
36
+ On the API:
37
+
38
+ ```js
39
+ import { unified } from "unified";
40
+ import { hl7v2Parse } from "@glion/parser";
41
+ import hl7v2LintMaxMessageSize from "@glion/lint-max-message-size";
42
+ import { reporter } from "vfile-reporter";
43
+
44
+ const msg = `MSH|^~\\&|...`;
45
+ const file = await unified()
46
+ .use(hl7v2Parse)
47
+ .use(hl7v2LintMaxMessageSize)
48
+ .process(msg);
49
+
50
+ console.error(reporter([file]));
51
+ ```
52
+
53
+ ## API
54
+
55
+ ### `unified().use(hl7v2LintMaxMessageSize[, options])`
56
+
57
+ Warns when the HL7v2 message exceeds a configurable maximum size in bytes (default: 10,000,000 bytes) or, optionally, a maximum number of segments.
58
+ Reports a message if the message is too large or has too many segments.
59
+
60
+ ###### Returns
61
+
62
+ A `unified` Transformer that adds messages to the file.
63
+
64
+ ## Security
65
+
66
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
67
+
68
+ ## Contributing
69
+
70
+ We welcome contributions! Please see our [Contributing Guide][github-contributing] for more details.
71
+
72
+ 1. Fork the repository
73
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
74
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
75
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
76
+ 5. Open a Pull Request
77
+
78
+ ## Code of Conduct
79
+
80
+ To ensure a welcoming and positive environment, we have a [Code of Conduct][github-code-of-conduct] that all contributors and participants are expected to adhere to.
81
+
82
+ ## License
83
+
84
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
85
+
86
+ This program is licensed to you under the terms of the [MIT License](https://opensource.org/licenses/MIT). This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the [LICENSE][github-license] file for details.
87
+
88
+ [github-code-of-conduct]: https://github.com/rethinkhealth/glion/blob/main/CODE_OF_CONDUCT.md
89
+ [github-license]: https://github.com/rethinkhealth/glion/blob/main/LICENSE
90
+ [github-contributing]: https://github.com/rethinkhealth/glion/blob/main/CONTRIBUTING.md
91
+ [github-unified]: https://github.com/unifiedjs/unified
92
+ [github-hl7v2-parser]: https://github.com/rethinkhealth/glion/tree/main/packages/hl7v2-parser
@@ -0,0 +1,22 @@
1
+ import type { Node } from "@glion/ast";
2
+ export interface MaxMessageSizeOptions {
3
+ /**
4
+ * Max allowed size of the HL7v2 message in bytes (UTF-8). Default: 1_000_000
5
+ * (1MB).
6
+ */
7
+ maxBytes?: number;
8
+ /**
9
+ * Max allowed number of segments (counts nodes with `type: "segment"`).
10
+ * Default: undefined (disabled). Set to a number to enable.
11
+ */
12
+ maxSegments?: number;
13
+ }
14
+ /**
15
+ * Hl7v2-lint rule to warn when message size exceeds the maximum allowed size.
16
+ *
17
+ * This rule is useful for ensuring that HL7v2 messages do not exceed a safe
18
+ * or expected size limit (default: 10MB).
19
+ */
20
+ declare const hl7v2LintMaxMessageSize: import("unified-lint-rule").Plugin<Node, MaxMessageSizeOptions>;
21
+ export default hl7v2LintMaxMessageSize;
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAKvC,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AASD;;;;;GAKG;AACH,QAAA,MAAM,uBAAuB,iEAwC5B,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import pluralize from "pluralize";
2
+ import { lintRule } from "unified-lint-rule";
3
+ import { SKIP, visit } from "unist-util-visit";
4
+ //#region src/index.ts
5
+ const defaultOptions = {
6
+ maxBytes: 1e7,
7
+ maxSegments: void 0
8
+ };
9
+ /**
10
+ * Hl7v2-lint rule to warn when message size exceeds the maximum allowed size.
11
+ *
12
+ * This rule is useful for ensuring that HL7v2 messages do not exceed a safe
13
+ * or expected size limit (default: 10MB).
14
+ */
15
+ const hl7v2LintMaxMessageSize = lintRule({
16
+ origin: "hl7v2-lint:max-message-size",
17
+ url: "https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-max-message-size#readme"
18
+ }, (tree, file, opts) => {
19
+ const options = {
20
+ ...defaultOptions,
21
+ ...opts
22
+ };
23
+ const byteLength = Buffer.byteLength(String(file.value), "utf8");
24
+ if (byteLength > options.maxBytes) file.message(`Message size ${pluralize("byte", byteLength, true)} exceeds ${pluralize("byte", options.maxBytes, true)} limit — trim payload or raise "maxBytes"`);
25
+ let totalSegments = 0;
26
+ visit(tree, (node) => {
27
+ if (node.type === "segment") {
28
+ totalSegments += 1;
29
+ return SKIP;
30
+ }
31
+ });
32
+ if (options.maxSegments && totalSegments > options.maxSegments) file.message(`Message contains ${pluralize("segment", totalSegments, true)} (limit ${pluralize("segment", options.maxSegments, true)}) — reduce segment count or raise "maxSegments"`);
33
+ });
34
+ //#endregion
35
+ export { hl7v2LintMaxMessageSize as default };
36
+
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Node } from \"@glion/ast\";\nimport pluralize from \"pluralize\";\nimport { lintRule } from \"unified-lint-rule\";\nimport { SKIP, visit } from \"unist-util-visit\";\n\nexport interface MaxMessageSizeOptions {\n /**\n * Max allowed size of the HL7v2 message in bytes (UTF-8). Default: 1_000_000\n * (1MB).\n */\n maxBytes?: number;\n /**\n * Max allowed number of segments (counts nodes with `type: \"segment\"`).\n * Default: undefined (disabled). Set to a number to enable.\n */\n maxSegments?: number;\n}\n\nconst defaultOptions: Required<Omit<MaxMessageSizeOptions, \"maxSegments\">> & {\n maxSegments?: number;\n} = {\n maxBytes: 10_000_000, // 10MB\n maxSegments: undefined,\n};\n\n/**\n * Hl7v2-lint rule to warn when message size exceeds the maximum allowed size.\n *\n * This rule is useful for ensuring that HL7v2 messages do not exceed a safe\n * or expected size limit (default: 10MB).\n */\nconst hl7v2LintMaxMessageSize = lintRule<Node, MaxMessageSizeOptions>(\n {\n origin: \"hl7v2-lint:max-message-size\",\n url: \"https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-max-message-size#readme\",\n },\n (tree, file, opts) => {\n const options = { ...defaultOptions, ...opts };\n\n // Byte length of the message\n const byteLength = Buffer.byteLength(String(file.value), \"utf8\");\n if (byteLength > options.maxBytes) {\n file.message(\n `Message size ${pluralize(\"byte\", byteLength, true)} exceeds ${pluralize(\"byte\", options.maxBytes, true)} limit — trim payload or raise \"maxBytes\"`\n );\n }\n\n let totalSegments = 0;\n\n visit(tree, (node) => {\n // Count all segments including those in nested groups\n if (node.type === \"segment\") {\n totalSegments += 1;\n return SKIP;\n }\n });\n\n if (options.maxSegments && totalSegments > options.maxSegments) {\n file.message(\n `Message contains ${pluralize(\n \"segment\",\n totalSegments,\n true\n )} (limit ${pluralize(\n \"segment\",\n options.maxSegments,\n true\n )}) — reduce segment count or raise \"maxSegments\"`\n );\n }\n }\n);\n\nexport default hl7v2LintMaxMessageSize;\n"],"mappings":";;;;AAkBA,MAAM,iBAEF;CACF,UAAU;CACV,aAAa,KAAA;CACd;;;;;;;AAQD,MAAM,0BAA0B,SAC9B;CACE,QAAQ;CACR,KAAK;CACN,GACA,MAAM,MAAM,SAAS;CACpB,MAAM,UAAU;EAAE,GAAG;EAAgB,GAAG;EAAM;CAG9C,MAAM,aAAa,OAAO,WAAW,OAAO,KAAK,MAAM,EAAE,OAAO;AAChE,KAAI,aAAa,QAAQ,SACvB,MAAK,QACH,gBAAgB,UAAU,QAAQ,YAAY,KAAK,CAAC,WAAW,UAAU,QAAQ,QAAQ,UAAU,KAAK,CAAC,2CAC1G;CAGH,IAAI,gBAAgB;AAEpB,OAAM,OAAO,SAAS;AAEpB,MAAI,KAAK,SAAS,WAAW;AAC3B,oBAAiB;AACjB,UAAO;;GAET;AAEF,KAAI,QAAQ,eAAe,gBAAgB,QAAQ,YACjD,MAAK,QACH,oBAAoB,UAClB,WACA,eACA,KACD,CAAC,UAAU,UACV,WACA,QAAQ,aACR,KACD,CAAC,iDACH;EAGN"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@glion/lint-max-message-size",
3
+ "version": "0.14.1",
4
+ "description": "hl7v2-lint rule to warn when message size exceeds the maximum allowed size",
5
+ "keywords": [
6
+ "definition",
7
+ "hl7",
8
+ "hl7v2",
9
+ "hl7v2-lint",
10
+ "hl7v2-lint-rule",
11
+ "lint",
12
+ "nodejs",
13
+ "rule",
14
+ "typescript"
15
+ ],
16
+ "homepage": "https://glion.dev",
17
+ "license": "MIT",
18
+ "author": {
19
+ "name": "Melek Somai",
20
+ "email": "melek@rethinkhealth.io"
21
+ },
22
+ "repository": "rethinkhealth/glion.git",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "dependencies": {
35
+ "pluralize": "8.0.0",
36
+ "unified": "11.0.5",
37
+ "unified-lint-rule": "3.0.1",
38
+ "unist-util-visit": "5.1.0",
39
+ "unist-util-visit-parents": "6.0.2"
40
+ },
41
+ "devDependencies": {
42
+ "@types/node": "^25.5.0",
43
+ "@types/pluralize": "^0.0.33",
44
+ "@types/unist": "^3.0.3",
45
+ "@vitest/coverage-v8": "4.1.2",
46
+ "tsdown": "0.21.7",
47
+ "typescript": "^5.9.3",
48
+ "vfile": "^6.0.3",
49
+ "vitest": "4.1.2",
50
+ "@glion/tsconfig": "0.0.1",
51
+ "@glion/builder": "0.14.1",
52
+ "@glion/ast": "0.14.1",
53
+ "@glion/testing": "0.0.2"
54
+ },
55
+ "engines": {
56
+ "node": ">=18"
57
+ },
58
+ "scripts": {
59
+ "build": "tsdown && tsc --emitDeclarationOnly",
60
+ "check-types": "tsc --noEmit",
61
+ "test": "vitest run",
62
+ "test:coverage": "vitest run --coverage",
63
+ "test:watch": "vitest"
64
+ }
65
+ }