@mak-98/xml-dirvi-app 0.11.0-beta.2 → 0.11.0-beta.20

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 (32) hide show
  1. package/README.md +122 -0
  2. package/dist/domain/xml-doc.d.ts +73 -0
  3. package/dist/domain/xml-doc.d.ts.map +1 -0
  4. package/dist/domain/xml-doc.js +15 -0
  5. package/dist/domain/xml-tree.d.ts +83 -249
  6. package/dist/domain/xml-tree.d.ts.map +1 -1
  7. package/dist/domain/xml-tree.js +63 -96
  8. package/dist/index.d.ts +2 -2
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +2 -2
  11. package/dist/infrastructure/create-xml-dom.d.ts +6 -0
  12. package/dist/infrastructure/create-xml-dom.d.ts.map +1 -0
  13. package/dist/infrastructure/create-xml-dom.js +166 -0
  14. package/dist/infrastructure/create-xml-file-resync.d.ts +2 -0
  15. package/dist/infrastructure/create-xml-file-resync.d.ts.map +1 -0
  16. package/dist/infrastructure/create-xml-file-resync.js +68 -0
  17. package/dist/infrastructure/get-xml-entries.d.ts +1 -18
  18. package/dist/infrastructure/get-xml-entries.d.ts.map +1 -1
  19. package/dist/infrastructure/get-xml-entries.js +118 -44
  20. package/dist/infrastructure/load-node-xml-app-api.d.ts +1 -0
  21. package/dist/infrastructure/load-node-xml-app-api.d.ts.map +1 -1
  22. package/dist/infrastructure/load-node-xml-app-api.js +7 -3
  23. package/dist/infrastructure/parse-xml-document.d.ts +6 -4
  24. package/dist/infrastructure/parse-xml-document.d.ts.map +1 -1
  25. package/dist/infrastructure/parse-xml-document.js +9 -12
  26. package/dist/services/xml-dom.d.ts +16 -0
  27. package/dist/services/xml-dom.d.ts.map +1 -0
  28. package/dist/services/xml-dom.js +1 -0
  29. package/dist/xml-dirvi-app.d.ts +2 -5
  30. package/dist/xml-dirvi-app.d.ts.map +1 -1
  31. package/dist/xml-dirvi-app.js +53 -13
  32. package/package.json +4 -3
@@ -2,7 +2,8 @@ import { watch } from 'node:fs';
2
2
  import { basename, dirname, resolve } from 'node:path';
3
3
  import { createAppStateApis } from 'dirvi-lib';
4
4
  import { xmlFoldsApi, xmlNavApi, } from '../domain/xml-tree.js';
5
- import { loadNodeXmlDocument } from './load-node-xml-document.js';
5
+ import { readFile } from 'node:fs/promises';
6
+ import { parseXmlDocument } from './parse-xml-document.js';
6
7
  /**
7
8
  * Creates the AppApi for one XML document.
8
9
  *
@@ -18,7 +19,10 @@ export function loadNodeXmlAppApi(filePath) {
18
19
  * parseXmlDocument() reserves position 0 for the synthetic document root.
19
20
  */
20
21
  rootId: 0,
21
- createRoot: () => loadNodeXmlDocument(xmlAbsolutePath),
22
+ createRoot: async () => {
23
+ const source = await readFile(xmlAbsolutePath, 'utf8');
24
+ return parseXmlDocument(source);
25
+ },
22
26
  /*
23
27
  * XML documents are parsed completely when their root is created. No XML
24
28
  * branch is represented as an unloaded branch.
@@ -42,7 +46,7 @@ export function loadNodeXmlAppApi(filePath) {
42
46
  ...apis,
43
47
  };
44
48
  }
45
- function createXmlFileResyncSubscription(xmlAbsolutePath) {
49
+ export function createXmlFileResyncSubscription(xmlAbsolutePath) {
46
50
  const listeners = new Set();
47
51
  const parentDirectory = dirname(xmlAbsolutePath);
48
52
  const fileName = basename(xmlAbsolutePath);
@@ -1,9 +1,11 @@
1
- import type { XmlTreeNode } from '../domain/xml-tree.js';
1
+ import type { TreeMethods } from 'dirvi-lib';
2
+ import type { XmlId, XmlNodeValue, XmlTreeNode } from '../domain/xml-tree.js';
3
+ export declare const createXmlId: (name: string) => XmlId;
2
4
  /**
3
5
  * Parses XML source into one Dirvi XML document tree.
4
6
  *
5
- * The synthetic document root always receives ID 0. Descendants receive
6
- * monotonically allocated IDs in parser encounter order.
7
+ * Each node receives an ID containing a fresh UUID and its node name.
8
+ * Source child order is preserved by the TreeNode child sequence.
7
9
  */
8
- export declare function parseXmlDocument(source: string): XmlTreeNode;
10
+ export declare function parseXmlDocument(treeNodeApi: TreeMethods<XmlId, XmlNodeValue>, source: string): XmlTreeNode;
9
11
  //# sourceMappingURL=parse-xml-document.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-xml-document.d.ts","sourceRoot":"","sources":["../../src/infrastructure/parse-xml-document.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAIzD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAwB5D"}
1
+ {"version":3,"file":"parse-xml-document.d.ts","sourceRoot":"","sources":["../../src/infrastructure/parse-xml-document.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAI9E,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,KACG,CAAC;AAE/C;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,WAAW,CAAC,KAAK,EAAE,YAAY,CAAC,EAC7C,MAAM,EAAE,MAAM,GACb,WAAW,CAiBb"}
@@ -1,24 +1,21 @@
1
+ import { randomUUID } from 'node:crypto';
1
2
  import { parseXml } from '@rgrove/parse-xml';
2
- import { XmlTreeNode as XmlTreeNodeDomain } from '../domain/xml-tree.js';
3
+ import { createConstructors, xmlIdSchema } from '../domain/xml-tree.js';
3
4
  import { getXmlEntries } from './get-xml-entries.js';
5
+ export const createXmlId = (name) => xmlIdSchema.parse(`${randomUUID()}:${name}`);
4
6
  /**
5
7
  * Parses XML source into one Dirvi XML document tree.
6
8
  *
7
- * The synthetic document root always receives ID 0. Descendants receive
8
- * monotonically allocated IDs in parser encounter order.
9
+ * Each node receives an ID containing a fresh UUID and its node name.
10
+ * Source child order is preserved by the TreeNode child sequence.
9
11
  */
10
- export function parseXmlDocument(source) {
12
+ export function parseXmlDocument(treeNodeApi, source) {
11
13
  const document = parseXml(source);
12
- let nextPosition = 1;
13
- const root = XmlTreeNodeDomain.document(0, getXmlEntries(document.children, () => {
14
- const position = nextPosition;
15
- nextPosition += 1;
16
- return position;
17
- }));
14
+ const root = createConstructors(treeNodeApi).document(createXmlId('#document'), getXmlEntries(treeNodeApi, document.children, createXmlId));
18
15
  if (root === undefined) {
19
16
  /*
20
- * Position allocation is unique, so this would indicate a defect in the
21
- * XML-to-domain conversion rather than invalid user XML.
17
+ * Each node receives a fresh UUID, so duplicate child IDs would indicate
18
+ * an ID collision or a defect in XML-to-domain conversion.
22
19
  */
23
20
  throw new Error('Cannot create XML document: duplicate child IDs.');
24
21
  }
@@ -0,0 +1,16 @@
1
+ import type { XmlDocument, XmlDocumentChild, XmlElementChild } from '../domain/xml-doc.js';
2
+ import { XmlId } from '../domain/xml-tree.js';
3
+ export type XmlDomEntry<Value> = {
4
+ id: XmlId;
5
+ value: Value;
6
+ };
7
+ export type XmlDom = {
8
+ /** Returns the document and its ID. */
9
+ getDocument: () => XmlDomEntry<XmlDocument>;
10
+ /**
11
+ * Loads direct children in document order.
12
+ * Omitting the ID addresses the document.
13
+ */
14
+ getChildren: (id?: XmlId) => readonly XmlDomEntry<XmlDocumentChild | XmlElementChild>[];
15
+ };
16
+ //# sourceMappingURL=xml-dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml-dom.d.ts","sourceRoot":"","sources":["../../src/services/xml-dom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,eAAe,EAChB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,MAAM,MAAM,WAAW,CAAC,KAAK,IAAI;IAC/B,EAAE,EAAE,KAAK,CAAC;IACV,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,uCAAuC;IACvC,WAAW,EAAE,MAAM,WAAW,CAAC,WAAW,CAAC,CAAC;IAE5C;;;OAGG;IACH,WAAW,EAAE,CACX,EAAE,CAAC,EAAE,KAAK,KACP,SAAS,WAAW,CAAC,gBAAgB,GAAG,eAAe,CAAC,EAAE,CAAC;CACjE,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,3 @@
1
- import type { AppPlugin } from 'dirvi-apis/plugin';
2
- /**
3
- * A dirvi app that exposes a POSIX filesystem as a navigable tree.
4
- */
5
- export declare const xmlAppPlugin: AppPlugin;
1
+ import type { CreateAppDefinitionApplicator } from 'dirvi-apis';
2
+ export declare const xmlApp: CreateAppDefinitionApplicator;
6
3
  //# sourceMappingURL=xml-dirvi-app.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"xml-dirvi-app.d.ts","sourceRoot":"","sources":["../src/xml-dirvi-app.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAInD;;GAEG;AACH,eAAO,MAAM,YAAY,EAAE,SAY1B,CAAC"}
1
+ {"version":3,"file":"xml-dirvi-app.d.ts","sourceRoot":"","sources":["../src/xml-dirvi-app.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAGV,6BAA6B,EAE9B,MAAM,YAAY,CAAC;AAQpB,eAAO,MAAM,MAAM,EAAE,6BA8DpB,CAAC"}
@@ -1,15 +1,55 @@
1
- import { loadNodeXmlAppApi } from './infrastructure/load-node-xml-app-api.js';
2
- /**
3
- * A dirvi app that exposes a POSIX filesystem as a navigable tree.
4
- */
5
- export const xmlAppPlugin = {
6
- createAppApiApplicator: (input) => {
7
- if (input === undefined) {
8
- throw new Error('xml-app: No file path to XML file given.');
1
+ import { readFile } from 'node:fs/promises';
2
+ import { resolve } from 'node:path';
3
+ import { xmlValueSchema } from './domain/xml-tree.js';
4
+ import { createXmlFileResyncSubscription } from './infrastructure/create-xml-file-resync.js';
5
+ import { createXmlDom } from './infrastructure/create-xml-dom.js';
6
+ export const xmlApp = (input) => {
7
+ if (input === undefined) {
8
+ throw new Error('xml-dirvi-app: No file path to XML file given.');
9
+ }
10
+ const xmlAbsolutePath = resolve(input);
11
+ // TODO: Make the `xmldom` nodes the IDs.
12
+ // Create dom async on first need, cache afterward
13
+ let dom;
14
+ let domPromise;
15
+ const getDom = async () => {
16
+ if (dom !== undefined) {
17
+ return dom;
9
18
  }
10
- const appApi = loadNodeXmlAppApi(input);
11
- return (appApiConsumer) => {
12
- return appApiConsumer(appApi);
13
- };
14
- },
19
+ if (domPromise === undefined) {
20
+ domPromise = readFile(xmlAbsolutePath, 'utf8')
21
+ .then((source) => {
22
+ dom = createXmlDom(source);
23
+ return dom;
24
+ })
25
+ .finally(() => {
26
+ domPromise = undefined;
27
+ });
28
+ }
29
+ return domPromise;
30
+ };
31
+ const appDefinition = {
32
+ semanticOrder: true,
33
+ valueSchema: xmlValueSchema,
34
+ loadChildren: async (path) => {
35
+ const dom = await getDom();
36
+ return dom.getChildren(path.at(-1)).map(({ id, value }) => ({
37
+ id,
38
+ kind: value.kind === 'element' ? 'unloaded-branch' : 'leaf',
39
+ value,
40
+ }));
41
+ },
42
+ loadRoot: async () => {
43
+ const dom = await getDom();
44
+ return dom.getDocument();
45
+ },
46
+ presentNode: (_id, value, nodeKind) => [
47
+ {
48
+ content: value.kind,
49
+ highlight: nodeKind,
50
+ },
51
+ ],
52
+ subscribeToResync: createXmlFileResyncSubscription(xmlAbsolutePath),
53
+ };
54
+ return (appDefinitionConsumer) => appDefinitionConsumer(appDefinition);
15
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mak-98/xml-dirvi-app",
3
- "version": "0.11.0-beta.2",
3
+ "version": "0.11.0-beta.20",
4
4
  "description": "XML file app for dirvi.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "type": "module",
@@ -20,13 +20,14 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@rgrove/parse-xml": "^5.0.0",
23
- "dirvi-apis": "0.11.0-beta.1",
24
- "dirvi-lib": "0.11.0-beta.1",
23
+ "@xmldom/xmldom": "^0.9.12",
24
+ "dirvi-apis": "0.11.0-beta.20",
25
25
  "env-paths": "^4.0.0",
26
26
  "zod": "^4.4.3"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "^22.0.0",
30
+ "dirvi-lib": "0.11.0-beta.20",
30
31
  "typescript": "^5.0.0"
31
32
  },
32
33
  "scripts": {