@genrojs/bag 0.10.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 +176 -0
- package/NOTICE +7 -0
- package/README.md +191 -0
- package/docs/python-alignment.md +156 -0
- package/package.json +51 -0
- package/src/bag-node-container.js +459 -0
- package/src/bag-node.js +665 -0
- package/src/bag.js +2497 -0
- package/src/browser.js +7 -0
- package/src/devtools/html-repr.css +69 -0
- package/src/devtools/html-repr.js +78 -0
- package/src/devtools/index.js +1 -0
- package/src/index.js +9 -0
- package/src/resolver-wire.js +128 -0
- package/src/resolver.js +538 -0
- package/src/resolvers/StorageResolver.js +102 -0
- package/src/resolvers/UrlResolver.js +209 -0
- package/src/resolvers/UuidResolver.js +37 -0
- package/src/resolvers/index.js +9 -0
- package/src/subtype-wire.js +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# genro-bag-js
|
|
2
|
+
|
|
3
|
+
TypeScript/JavaScript port of [genro-bag](https://github.com/nicofirst1/genro-bag) - hierarchical data container with XML serialization, Builder pattern, and Compiler system.
|
|
4
|
+
|
|
5
|
+
## Status
|
|
6
|
+
|
|
7
|
+
**Development Status**: Pre-Alpha (documentation and planning)
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
genro-bag-js brings the Python genro-bag architecture to JavaScript/TypeScript for:
|
|
12
|
+
|
|
13
|
+
- **Browser**: DOM manipulation, UI frameworks, reactive updates
|
|
14
|
+
- **Node.js**: Server-side processing, CLI tools
|
|
15
|
+
- **Interoperability**: Same XML/JSON format as Python version
|
|
16
|
+
- **Modern runtimes**: Deno, Bun support
|
|
17
|
+
|
|
18
|
+
## Architecture
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
22
|
+
│ SERVER (Python) │
|
|
23
|
+
│ │
|
|
24
|
+
│ Builder Python → creates "recipe" (structured Bag) │
|
|
25
|
+
│ bag.to_xml() → serializes for transport │
|
|
26
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
27
|
+
│
|
|
28
|
+
▼ (HTTP / WebSocket)
|
|
29
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
30
|
+
│ BROWSER (JavaScript) │
|
|
31
|
+
│ │
|
|
32
|
+
│ Bag.fromXml() → reconstructs structure │
|
|
33
|
+
│ Expander → expands macros/components (optional) │
|
|
34
|
+
│ DOMCompiler → builds DOM/widgets dynamically │
|
|
35
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Core Classes
|
|
39
|
+
|
|
40
|
+
| Class | Description |
|
|
41
|
+
|-------|-------------|
|
|
42
|
+
| `Bag` | Hierarchical container with labeled nodes |
|
|
43
|
+
| `BagNode` | Individual node with value and attributes |
|
|
44
|
+
| `BagBuilderBase` | Base class for declarative structure builders |
|
|
45
|
+
| `BagCompilerBase` | Base class for output generators (DOM, HTML, etc.) |
|
|
46
|
+
| `BagResolver` | Lazy loading support |
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
The same package is published on two registries:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# npm
|
|
54
|
+
npm install @genrojs/bag
|
|
55
|
+
|
|
56
|
+
# JSR
|
|
57
|
+
npx jsr add @genro/bag
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
From npm, import from `@genrojs/bag`; from JSR, import from `@genro/bag`.
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { Bag } from '@genrojs/bag';
|
|
66
|
+
|
|
67
|
+
// Create a Bag
|
|
68
|
+
const bag = new Bag();
|
|
69
|
+
bag.set('config.name', 'My App');
|
|
70
|
+
bag.set('config.version', '1.0.0');
|
|
71
|
+
|
|
72
|
+
// Serialize
|
|
73
|
+
const xml = bag.toXml();
|
|
74
|
+
const json = bag.toJson();
|
|
75
|
+
|
|
76
|
+
// Parse
|
|
77
|
+
const restored = Bag.fromXml(xml);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### With Builder
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { Bag, BagBuilderBase, element } from '@genrojs/bag';
|
|
84
|
+
|
|
85
|
+
class GUIBuilder extends BagBuilderBase {
|
|
86
|
+
@element({ subTags: 'button,textfield' })
|
|
87
|
+
panel(title: string) {}
|
|
88
|
+
|
|
89
|
+
@element({ subTags: '' })
|
|
90
|
+
button(label: string, action?: string) {}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const gui = new Bag({ builder: GUIBuilder });
|
|
94
|
+
const panel = gui.panel('Settings');
|
|
95
|
+
panel.button('Save', 'doSave');
|
|
96
|
+
panel.button('Cancel', 'doCancel');
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### With Compiler
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { BagCompilerBase, compiler } from '@genrojs/bag';
|
|
103
|
+
|
|
104
|
+
class DOMCompiler extends BagCompilerBase {
|
|
105
|
+
@compiler
|
|
106
|
+
panel(node, children): HTMLElement {
|
|
107
|
+
const div = document.createElement('div');
|
|
108
|
+
div.className = 'panel';
|
|
109
|
+
children.forEach(child => div.appendChild(child));
|
|
110
|
+
return div;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@compiler
|
|
114
|
+
button(node, children): HTMLElement {
|
|
115
|
+
const btn = document.createElement('button');
|
|
116
|
+
btn.textContent = node.attr.label;
|
|
117
|
+
btn.onclick = () => this.handleAction(node.attr.action);
|
|
118
|
+
return btn;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Documentation
|
|
124
|
+
|
|
125
|
+
- [Python alignment and TYTX compatibility](docs/python-alignment.md)
|
|
126
|
+
|
|
127
|
+
- [Architecture Analysis](docs/analysis/analisi_bag_js_builder_js.md) - Detailed analysis of existing JS implementation
|
|
128
|
+
- [Plan](docs/plan.md) - Implementation roadmap
|
|
129
|
+
|
|
130
|
+
## Development
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
# Install dependencies
|
|
134
|
+
pnpm install
|
|
135
|
+
|
|
136
|
+
# Run tests
|
|
137
|
+
pnpm test
|
|
138
|
+
|
|
139
|
+
# Build
|
|
140
|
+
pnpm build
|
|
141
|
+
|
|
142
|
+
# Type check
|
|
143
|
+
pnpm typecheck
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
Apache License 2.0 - see [LICENSE](LICENSE) for details.
|
|
149
|
+
|
|
150
|
+
Copyright 2025 Softwell S.r.l.
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
## JSR distribution
|
|
154
|
+
|
|
155
|
+
The JavaScript package is published as `@genro/bag` on JSR. Use
|
|
156
|
+
`bunx jsr add @genro/bag` or `npx jsr add @genro/bag`.
|
|
157
|
+
Node.js 22+, Bun and modern browsers with Web Crypto are the supported targets.
|
|
158
|
+
TYTX is resolved from JSR; no GitHub package checkout is needed.
|
|
159
|
+
|
|
160
|
+
[](https://jsr.io/@genro/bag)
|
|
161
|
+
|
|
162
|
+
Deno publication keeps a 24-hour cooldown for external dependencies and exempts
|
|
163
|
+
Genro JSR packages (`jsr:@genro/*` and their `npm:@jsr/genro__*` compatibility
|
|
164
|
+
names). Releases are published only to JSR; the npm form downloads from
|
|
165
|
+
`npm.jsr.io`, not npmjs.com. Verify each published dependency before adopting it.
|
|
166
|
+
|
|
167
|
+
### Optional developer HTML representation
|
|
168
|
+
|
|
169
|
+
The experimental inspector is a separate entry point, excluded from the main
|
|
170
|
+
module and browser bundle. It may be removed in a future release.
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
import { htmlRepr } from '@genrojs/bag/devtools';
|
|
174
|
+
// With a bundler that supports CSS imports:
|
|
175
|
+
import '@genrojs/bag/devtools/html-repr.css';
|
|
176
|
+
|
|
177
|
+
const html = htmlRepr(bag, {title: 'Inspect Bag', showTypes: true});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Load the stylesheet separately when not using a CSS-aware bundler. The function
|
|
181
|
+
returns an escaped HTML fragment; rendering it requires no additional script.
|
|
182
|
+
Bag panels and attributes start collapsed. Options `openDepth` (default 0),
|
|
183
|
+
`openAttributes` (default false), `showTypes` (default true), and `className`
|
|
184
|
+
control presentation. The renderer reads static values without invoking
|
|
185
|
+
resolvers. An optional `resolverParameters(resolver)` hook supplies additional
|
|
186
|
+
metadata, including named callbacks; the hook must not execute the resolver.
|
|
187
|
+
Resolver badges use the constructor name, which minification may change.
|
|
188
|
+
|
|
189
|
+
All nodes are emitted even when collapsed: this is a static developer inspector,
|
|
190
|
+
not a virtualized viewer for large datasets. Ancestor cycles are marked without
|
|
191
|
+
mutating the Bag. The inspector is deliberately not installed on `Bag.prototype`.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Python/JavaScript alignment
|
|
2
|
+
|
|
3
|
+
Implemented against genro-bag Python 0.22.0. The runtime differences remain
|
|
4
|
+
intentional: callbacks may return values or Promises, Web Storage replaces
|
|
5
|
+
process environment variables, and there is no DataChangeCollector.
|
|
6
|
+
|
|
7
|
+
## Release dependency
|
|
8
|
+
|
|
9
|
+
Bag JS 0.4.0 uses the published TYTX `v0.15.0` Git tag over HTTPS. The lockfile
|
|
10
|
+
pins its commit; no sibling checkout is required. TYTX exposes the public
|
|
11
|
+
`getRegisteredType(suffix)` API and supports the Node 18 runtime declared by
|
|
12
|
+
this package. Install with `npm ci` for development. `UuidResolver` selects
|
|
13
|
+
Node crypto or browser Web Crypto through a conditional import; its API
|
|
14
|
+
remains synchronous on both runtimes.
|
|
15
|
+
|
|
16
|
+
## Registered Bag classes
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { Bag } from '@genrojs/bag';
|
|
20
|
+
import { registerClass } from '@genrojs/tytx';
|
|
21
|
+
|
|
22
|
+
class SourceBag extends Bag {
|
|
23
|
+
static tytxSuffix = 'SOURCE';
|
|
24
|
+
}
|
|
25
|
+
registerClass(SourceBag);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Python registers its counterpart with `__tytx_suffix__ = 'SOURCE'`. Each
|
|
29
|
+
runtime constructs its own class. Bag itself owns `X`; a registered subclass
|
|
30
|
+
must own a distinct suffix. Registration uses exact constructors.
|
|
31
|
+
|
|
32
|
+
`SourceBag.fromTytx(data)` reconstructs a SourceBag root and retains mixed
|
|
33
|
+
branch types. JSON and MessagePack support normal and compact Bag rows.
|
|
34
|
+
Bags also travel inside ordinary TYTX objects and arrays. Legacy subclasses
|
|
35
|
+
that inherit `X` retain the Python root-class factory behavior for `X` branches.
|
|
36
|
+
|
|
37
|
+
Unknown or missing parents throw `BagSerializationError` instead of moving
|
|
38
|
+
children to the root. An unknown empty marker without descendants can still
|
|
39
|
+
be indistinguishable from ordinary text. MessagePack scalar strings are not
|
|
40
|
+
indiscriminately decoded as structural markers.
|
|
41
|
+
|
|
42
|
+
## Tags and attribute queries
|
|
43
|
+
|
|
44
|
+
`update(otherBag)` copies `nodeTag` and `xmlTag` on inserted nodes. On collisions,
|
|
45
|
+
non-null incoming tags win; null tags retain the destination metadata. This
|
|
46
|
+
also applies to recursive merges. JSON and TYTX round-trips retain `nodeTag`.
|
|
47
|
+
|
|
48
|
+
`getItem('node?')` returns all attributes as an object. `node?a` returns one
|
|
49
|
+
attribute, and `node?a&b` returns an array in the requested order. Attribute
|
|
50
|
+
resolvers are resolved using their own parameters, without passing attributes
|
|
51
|
+
from the containing node. A query returns immediately if all values are
|
|
52
|
+
synchronous; otherwise it returns a Promise of the complete result. Rejected
|
|
53
|
+
resolver Promises reject the query. Static reads return resolver objects intact.
|
|
54
|
+
|
|
55
|
+
## Resolver descriptions
|
|
56
|
+
|
|
57
|
+
TYTX, JSON and XML carry Python's `::RSLV:` descriptions. JSON node objects
|
|
58
|
+
use `resolver`; XML uses `_resolver`. Resolver-valued attributes use the same
|
|
59
|
+
marker. Reading a format does not call `load()`. Resolver caches are not
|
|
60
|
+
serialized as independent data branches.
|
|
61
|
+
|
|
62
|
+
Register JS counterparts explicitly, with adapters for argument naming and
|
|
63
|
+
runtime differences. The module and name identify a wire type; they never
|
|
64
|
+
cause dynamic imports or code evaluation.
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import { BagResolver, registerResolver } from '@genrojs/bag';
|
|
68
|
+
|
|
69
|
+
class NumberResolver extends BagResolver {
|
|
70
|
+
load(options) { return options.number; }
|
|
71
|
+
}
|
|
72
|
+
registerResolver(NumberResolver, {
|
|
73
|
+
module: 'myapp.resolvers',
|
|
74
|
+
name: 'NumberResolver',
|
|
75
|
+
encode: resolver => ({ kwargs: { number: resolver._kw.number } }),
|
|
76
|
+
decode: ({ kwargs }) => new NumberResolver({ number: kwargs.number }),
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Adapters must construct inert resolvers. `encode` returns JSON-compatible
|
|
81
|
+
`args` and `kwargs`; callbacks and non-JSON parameter objects cannot travel.
|
|
82
|
+
Unregistered JS resolver instances fail explicitly when serialized.
|
|
83
|
+
|
|
84
|
+
Unknown descriptions become `OpaqueResolver` instances. Their read-only
|
|
85
|
+
`payload` preserves the exact received marker, including whitespace and
|
|
86
|
+
signatures. Attempting to resolve them locally throws. Signed tokens remain
|
|
87
|
+
opaque even if their class has a JS counterpart: the browser has no signing
|
|
88
|
+
key and does not authenticate them. They travel unchanged back to the server,
|
|
89
|
+
which verifies signature and expiry. Remote execution is outside this API.
|
|
90
|
+
|
|
91
|
+
The existing XML API emits fragments; wrap them in `<GenRoBag>...</GenRoBag>`
|
|
92
|
+
for a round-trip with Python (which recognizes that wrapper). JS `fromXml`
|
|
93
|
+
reads the children of the document element.
|
|
94
|
+
|
|
95
|
+
## Web Storage conversion
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
new StorageResolver({
|
|
99
|
+
key: 'preferences.zoom', storageType: 'local', dtype: 'L', defaultValue: '100',
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The existing `storageType: 'local' | 'session'` selects the backend. `dtype`
|
|
104
|
+
optionally converts the string or non-null default through TYTX. With no dtype,
|
|
105
|
+
values retain the original behavior; null defaults remain null. Invalid numeric
|
|
106
|
+
strings throw instead of returning a partially parsed number. No separate
|
|
107
|
+
LocalStorageResolver, SessionStorageResolver or EnvResolver is introduced.
|
|
108
|
+
|
|
109
|
+
## Verification
|
|
110
|
+
|
|
111
|
+
- `npm test`: existing JS tests and new alignment contracts.
|
|
112
|
+
- `node --test js/test/test_registry.js js/test/test_registry_lookup.js` in TYTX.
|
|
113
|
+
- `python -m pytest tests/cross-lang`: requires Python 3.11+, pytest, msgpack,
|
|
114
|
+
and the current Python genro-bag, genro-tytx and genro-toolbox packages.
|
|
115
|
+
The alignment bridge checks mixed branch identities inside JS and Python,
|
|
116
|
+
resolver counterparts, and signed payloads verified on return to Python.
|
|
117
|
+
|
|
118
|
+
## Legacy JavaScript digest calls
|
|
119
|
+
|
|
120
|
+
The signature remains `digest(what, condition, asColumns)`. For compatibility
|
|
121
|
+
with legacy JavaScript callers, a boolean second argument is interpreted as
|
|
122
|
+
`asColumns`, with no filter, and emits a `console.warn` deprecation notice.
|
|
123
|
+
Migrate `digest(what, true)` to `digest(what, null, true)` (and likewise for
|
|
124
|
+
`false`). Callable filters retain their existing behavior and do not warn.
|
|
125
|
+
The `columns()` helper already uses the explicit three-argument form.
|
|
126
|
+
|
|
127
|
+
## JavaScript items contract
|
|
128
|
+
|
|
129
|
+
`Bag.items()` returns an ordered array of `{key, value}` objects, matching
|
|
130
|
+
legacy JavaScript Bag. Values are resolved through `getValue()` and nested
|
|
131
|
+
Bags retain their identity. This differs from Python's key/value pairs.
|
|
132
|
+
|
|
133
|
+
## JavaScript update contract
|
|
134
|
+
|
|
135
|
+
`update(source, mode, reason, ignoreNone=false)` retains the legacy JavaScript
|
|
136
|
+
positional contract. With `mode='static'`, incoming resolvers are preserved and
|
|
137
|
+
not executed, including newly inserted nodes. Otherwise their results are
|
|
138
|
+
copied; an incoming resolved result replaces a destination resolver. Nested
|
|
139
|
+
Bags merge recursively unless the incoming `__replace` marker requests
|
|
140
|
+
replacement. The marker is consumed and `reason` is propagated to events.
|
|
141
|
+
Null values overwrite existing values by default. The previous standalone
|
|
142
|
+
`ignoreNone` extension is now explicitly the fourth argument, not the mode.
|
|
143
|
+
This JS signature intentionally differs from Python's `resolved` flag.
|
|
144
|
+
|
|
145
|
+
Unlike the historical implementation, insertion does not drop static resolvers.
|
|
146
|
+
This repairs the lazy-copy contract instead of retaining that historical bug.
|
|
147
|
+
|
|
148
|
+
## Constructor null attributes
|
|
149
|
+
|
|
150
|
+
`BagNode` accepts `removeNullAttributes` as its eighth constructor argument,
|
|
151
|
+
matching Python's `_remove_null_attributes` position and default (`true`).
|
|
152
|
+
Default construction removes null attributes; passing `false` preserves them.
|
|
153
|
+
Zero, false and empty strings are preserved in either mode. `Bag.setItem`
|
|
154
|
+
forwards its existing null-removal option when constructing a new node as well
|
|
155
|
+
as when updating one. This is an optional argument and a propagation bug fix,
|
|
156
|
+
not a new default policy. See `constructor-null-attributes.test.js`.
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@genrojs/bag",
|
|
3
|
+
"version": "0.10.1",
|
|
4
|
+
"description": "JavaScript port of genro-bag - hierarchical data container",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./devtools": "./src/devtools/index.js",
|
|
10
|
+
"./devtools/html-repr.css": "./src/devtools/html-repr.css"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"NOTICE",
|
|
17
|
+
"docs/python-alignment.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test tests/*.test.js",
|
|
21
|
+
"test:coverage": "c8 --reporter=lcov --reporter=text node --test tests/*.test.js"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"bag",
|
|
25
|
+
"hierarchical",
|
|
26
|
+
"xml",
|
|
27
|
+
"builder",
|
|
28
|
+
"genro"
|
|
29
|
+
],
|
|
30
|
+
"author": "Genropy Team",
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/genro-org/genro-bag-js.git"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@genrojs/tytx": "^0.16.1",
|
|
41
|
+
"@msgpack/msgpack": "^3.1.3",
|
|
42
|
+
"@xmldom/xmldom": "^0.9.12",
|
|
43
|
+
"big.js": "^7.0.1"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"c8": "^10.1.2"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|