@mak-98/xml-dirvi-app 0.0.0-stage → 0.11.0-beta.13
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 +674 -0
- package/dist/domain/xml-tree.d.ts +146 -0
- package/dist/domain/xml-tree.d.ts.map +1 -0
- package/dist/domain/xml-tree.js +140 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/infrastructure/create-xml-file-resync.d.ts +2 -0
- package/dist/infrastructure/create-xml-file-resync.d.ts.map +1 -0
- package/dist/infrastructure/create-xml-file-resync.js +68 -0
- package/dist/infrastructure/get-xml-entries.d.ts +9 -0
- package/dist/infrastructure/get-xml-entries.d.ts.map +1 -0
- package/dist/infrastructure/get-xml-entries.js +134 -0
- package/dist/infrastructure/load-node-xml-app-api.d.ts +11 -0
- package/dist/infrastructure/load-node-xml-app-api.d.ts.map +1 -0
- package/dist/infrastructure/load-node-xml-app-api.js +114 -0
- package/dist/infrastructure/load-node-xml-document.d.ts +6 -0
- package/dist/infrastructure/load-node-xml-document.d.ts.map +1 -0
- package/dist/infrastructure/load-node-xml-document.js +9 -0
- package/dist/infrastructure/parse-xml-document.d.ts +11 -0
- package/dist/infrastructure/parse-xml-document.d.ts.map +1 -0
- package/dist/infrastructure/parse-xml-document.js +23 -0
- package/dist/xml-dirvi-app.d.ts +3 -0
- package/dist/xml-dirvi-app.d.ts.map +1 -0
- package/dist/xml-dirvi-app.js +30 -0
- package/package.json +34 -3
- package/README.md +0 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { TreeNode, TreeMethods } from 'dirvi-lib';
|
|
3
|
+
/**
|
|
4
|
+
* A node key containing its UUID and display name.
|
|
5
|
+
*
|
|
6
|
+
* This is not XML document order. XML sibling order is held by the source
|
|
7
|
+
* TreeNode child sequence.
|
|
8
|
+
*
|
|
9
|
+
* Changing the name changes the complete tree key.
|
|
10
|
+
*/
|
|
11
|
+
export declare const xmlIdSchema: z.ZodString;
|
|
12
|
+
export type XmlId = z.infer<typeof xmlIdSchema>;
|
|
13
|
+
export declare function createXmlId(name: string, uuid: string): XmlId;
|
|
14
|
+
export declare function xmlIdName(id: XmlId): string;
|
|
15
|
+
export declare function xmlIdUuid(id: XmlId): string;
|
|
16
|
+
export type XmlAttribute = Readonly<{
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly value: string;
|
|
19
|
+
}>;
|
|
20
|
+
export type XmlDocumentValue = Readonly<{
|
|
21
|
+
readonly kind: 'document';
|
|
22
|
+
}>;
|
|
23
|
+
export type XmlDeclarationValue = Readonly<{
|
|
24
|
+
readonly kind: 'declaration';
|
|
25
|
+
readonly version: string;
|
|
26
|
+
readonly encoding: string | null;
|
|
27
|
+
readonly standalone: 'yes' | 'no' | null;
|
|
28
|
+
}>;
|
|
29
|
+
export type XmlDoctypeValue = Readonly<{
|
|
30
|
+
readonly kind: 'doctype';
|
|
31
|
+
readonly name: string;
|
|
32
|
+
readonly publicId: string | null;
|
|
33
|
+
readonly systemId: string | null;
|
|
34
|
+
readonly internalSubset: string | null;
|
|
35
|
+
}>;
|
|
36
|
+
export type XmlElementValue = Readonly<{
|
|
37
|
+
readonly kind: 'element';
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly attributes: readonly XmlAttribute[];
|
|
40
|
+
}>;
|
|
41
|
+
export type XmlTextValue = Readonly<{
|
|
42
|
+
readonly kind: 'text';
|
|
43
|
+
readonly text: string;
|
|
44
|
+
}>;
|
|
45
|
+
export type XmlCommentValue = Readonly<{
|
|
46
|
+
readonly kind: 'comment';
|
|
47
|
+
readonly text: string;
|
|
48
|
+
}>;
|
|
49
|
+
export type XmlCdataValue = Readonly<{
|
|
50
|
+
readonly kind: 'cdata';
|
|
51
|
+
readonly text: string;
|
|
52
|
+
}>;
|
|
53
|
+
export type XmlProcessingInstructionValue = Readonly<{
|
|
54
|
+
readonly kind: 'processing-instruction';
|
|
55
|
+
readonly target: string;
|
|
56
|
+
readonly data: string;
|
|
57
|
+
}>;
|
|
58
|
+
/**
|
|
59
|
+
* XML semantics carried by a generic tree node.
|
|
60
|
+
*
|
|
61
|
+
* Child ordering is deliberately not represented here.
|
|
62
|
+
*/
|
|
63
|
+
export type XmlNodeValue = XmlDocumentValue | XmlDeclarationValue | XmlDoctypeValue | XmlElementValue | XmlTextValue | XmlCommentValue | XmlCdataValue | XmlProcessingInstructionValue;
|
|
64
|
+
export type XmlTreeNode = TreeNode<XmlId, XmlNodeValue>;
|
|
65
|
+
export declare const XmlAttributeSchema: z.ZodObject<{
|
|
66
|
+
name: z.ZodString;
|
|
67
|
+
value: z.ZodString;
|
|
68
|
+
}, z.core.$strip>;
|
|
69
|
+
export declare const XmlDocumentValueSchema: z.ZodObject<{
|
|
70
|
+
kind: z.ZodLiteral<"document">;
|
|
71
|
+
}, z.core.$strip>;
|
|
72
|
+
export declare const XmlElementValueSchema: z.ZodObject<{
|
|
73
|
+
kind: z.ZodLiteral<"element">;
|
|
74
|
+
name: z.ZodString;
|
|
75
|
+
attributes: z.ZodReadonly<z.ZodArray<z.ZodObject<{
|
|
76
|
+
name: z.ZodString;
|
|
77
|
+
value: z.ZodString;
|
|
78
|
+
}, z.core.$strip>>>;
|
|
79
|
+
}, z.core.$strip>;
|
|
80
|
+
export declare const XmlDeclarationValueSchema: z.ZodObject<{
|
|
81
|
+
kind: z.ZodLiteral<"declaration">;
|
|
82
|
+
version: z.ZodString;
|
|
83
|
+
encoding: z.ZodNullable<z.ZodString>;
|
|
84
|
+
standalone: z.ZodNullable<z.ZodEnum<{
|
|
85
|
+
yes: "yes";
|
|
86
|
+
no: "no";
|
|
87
|
+
}>>;
|
|
88
|
+
}, z.core.$strip>;
|
|
89
|
+
export declare const XmlDoctypeValueSchema: z.ZodObject<{
|
|
90
|
+
kind: z.ZodLiteral<"doctype">;
|
|
91
|
+
name: z.ZodString;
|
|
92
|
+
publicId: z.ZodNullable<z.ZodString>;
|
|
93
|
+
systemId: z.ZodNullable<z.ZodString>;
|
|
94
|
+
internalSubset: z.ZodNullable<z.ZodString>;
|
|
95
|
+
}, z.core.$strip>;
|
|
96
|
+
export declare const XmlTextValueSchema: z.ZodObject<{
|
|
97
|
+
kind: z.ZodLiteral<"text">;
|
|
98
|
+
text: z.ZodString;
|
|
99
|
+
}, z.core.$strip>;
|
|
100
|
+
export declare const XmlCommentValueSchema: z.ZodObject<{
|
|
101
|
+
kind: z.ZodLiteral<"comment">;
|
|
102
|
+
text: z.ZodString;
|
|
103
|
+
}, z.core.$strip>;
|
|
104
|
+
export declare const XmlCdataValueSchema: z.ZodObject<{
|
|
105
|
+
kind: z.ZodLiteral<"cdata">;
|
|
106
|
+
text: z.ZodString;
|
|
107
|
+
}, z.core.$strip>;
|
|
108
|
+
export declare const XmlProcessingInstructionValueSchema: z.ZodObject<{
|
|
109
|
+
kind: z.ZodLiteral<"processing-instruction">;
|
|
110
|
+
target: z.ZodString;
|
|
111
|
+
data: z.ZodString;
|
|
112
|
+
}, z.core.$strip>;
|
|
113
|
+
export declare const xmlValueSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
114
|
+
kind: z.ZodLiteral<"document">;
|
|
115
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
116
|
+
kind: z.ZodLiteral<"element">;
|
|
117
|
+
name: z.ZodString;
|
|
118
|
+
attributes: z.ZodReadonly<z.ZodArray<z.ZodObject<{
|
|
119
|
+
name: z.ZodString;
|
|
120
|
+
value: z.ZodString;
|
|
121
|
+
}, z.core.$strip>>>;
|
|
122
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
123
|
+
kind: z.ZodLiteral<"text">;
|
|
124
|
+
text: z.ZodString;
|
|
125
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
126
|
+
kind: z.ZodLiteral<"comment">;
|
|
127
|
+
text: z.ZodString;
|
|
128
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
129
|
+
kind: z.ZodLiteral<"cdata">;
|
|
130
|
+
text: z.ZodString;
|
|
131
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
132
|
+
kind: z.ZodLiteral<"processing-instruction">;
|
|
133
|
+
target: z.ZodString;
|
|
134
|
+
data: z.ZodString;
|
|
135
|
+
}, z.core.$strip>], "kind">;
|
|
136
|
+
export declare function createConstructors(xmlTreeNodeApi: TreeMethods<XmlId, XmlNodeValue>): {
|
|
137
|
+
readonly document: (id: XmlId, children: Iterable<XmlTreeNode>) => XmlTreeNode | undefined;
|
|
138
|
+
readonly declaration: (id: XmlId, version: string, encoding: string | null, standalone: "yes" | "no" | null) => XmlTreeNode;
|
|
139
|
+
readonly doctype: (id: XmlId, name: string, publicId: string | null, systemId: string | null, internalSubset: string | null) => XmlTreeNode;
|
|
140
|
+
readonly element: (id: XmlId, name: string, attributes: readonly XmlAttribute[], children: Iterable<XmlTreeNode>) => XmlTreeNode | undefined;
|
|
141
|
+
readonly text: (id: XmlId, text: string) => XmlTreeNode;
|
|
142
|
+
readonly comment: (id: XmlId, text: string) => XmlTreeNode;
|
|
143
|
+
readonly cdata: (id: XmlId, text: string) => XmlTreeNode;
|
|
144
|
+
readonly processingInstruction: (id: XmlId, target: string, data: string) => XmlTreeNode;
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=xml-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xml-tree.d.ts","sourceRoot":"","sources":["../../src/domain/xml-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAmB,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAcxE;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,aAavB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC;AAMhD,wBAAgB,WAAW,CAAC,IAAI,EAAE,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C;;;;iBAIK,CAAC;AAEtD,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;2BAOS,CAAC;AAMrC,wBAAgB,kBAAkB,CAChC,cAAc,EAAE,WAAW,CAAC,KAAK,EAAE,YAAY,CAAC;4BAIxC,KAAK,YACC,QAAQ,CAAC,WAAW,CAAC,KAC9B,WAAW,GAAG,SAAS;+BASpB,KAAK,WACA,MAAM,YACL,MAAM,GAAG,IAAI,cACX,KAAK,GAAG,IAAI,GAAG,IAAI,KAC9B,WAAW;2BAUR,KAAK,QACH,MAAM,YACF,MAAM,GAAG,IAAI,YACb,MAAM,GAAG,IAAI,kBACP,MAAM,GAAG,IAAI,KAC5B,WAAW;2BAWR,KAAK,QACH,MAAM,cACA,SAAS,YAAY,EAAE,YACzB,QAAQ,CAAC,WAAW,CAAC,KAC9B,WAAW,GAAG,SAAS;wBAYjB,KAAK,QAAQ,MAAM,KAAG,WAAW;2BAO9B,KAAK,QAAQ,MAAM,KAAG,WAAW;yBAOnC,KAAK,QAAQ,MAAM,KAAG,WAAW;yCAQrC,KAAK,UACD,MAAM,QACR,MAAM,KACX,WAAW;EAQjB"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
// -----------------------------------------------------------------------------
|
|
3
|
+
// Position: Id extends SerializableKey
|
|
4
|
+
// -----------------------------------------------------------------------------
|
|
5
|
+
const uuidSchema = z.uuid();
|
|
6
|
+
/**
|
|
7
|
+
* A node key containing its UUID and display name.
|
|
8
|
+
*
|
|
9
|
+
* This is not XML document order. XML sibling order is held by the source
|
|
10
|
+
* TreeNode child sequence.
|
|
11
|
+
*
|
|
12
|
+
* Changing the name changes the complete tree key.
|
|
13
|
+
*/
|
|
14
|
+
export const xmlIdSchema = z.string().refine((value) => {
|
|
15
|
+
const separator = value.indexOf(':');
|
|
16
|
+
return (separator > 0 &&
|
|
17
|
+
uuidSchema.safeParse(value.slice(0, separator)).success &&
|
|
18
|
+
value.slice(separator + 1).trim().length > 0);
|
|
19
|
+
}, {
|
|
20
|
+
message: 'Expected an XML node ID in the form <uuid>:<node-name>.',
|
|
21
|
+
});
|
|
22
|
+
export function createXmlId(name, uuid) {
|
|
23
|
+
return xmlIdSchema.parse(`${uuid}:${name}`);
|
|
24
|
+
}
|
|
25
|
+
export function xmlIdName(id) {
|
|
26
|
+
return id.slice(id.indexOf(':') + 1);
|
|
27
|
+
}
|
|
28
|
+
export function xmlIdUuid(id) {
|
|
29
|
+
return id.slice(0, id.indexOf(':'));
|
|
30
|
+
}
|
|
31
|
+
// Schema ----------------------------------------------------------------------
|
|
32
|
+
export const XmlAttributeSchema = z.object({
|
|
33
|
+
name: z.string().min(1),
|
|
34
|
+
value: z.string(),
|
|
35
|
+
});
|
|
36
|
+
export const XmlDocumentValueSchema = z.object({
|
|
37
|
+
kind: z.literal('document'),
|
|
38
|
+
});
|
|
39
|
+
export const XmlElementValueSchema = z.object({
|
|
40
|
+
kind: z.literal('element'),
|
|
41
|
+
name: z.string().min(1),
|
|
42
|
+
attributes: z.array(XmlAttributeSchema).readonly(),
|
|
43
|
+
});
|
|
44
|
+
export const XmlDeclarationValueSchema = z.object({
|
|
45
|
+
kind: z.literal('declaration'),
|
|
46
|
+
version: z.string(),
|
|
47
|
+
encoding: z.string().nullable(),
|
|
48
|
+
standalone: z.enum(['yes', 'no']).nullable(),
|
|
49
|
+
});
|
|
50
|
+
export const XmlDoctypeValueSchema = z.object({
|
|
51
|
+
kind: z.literal('doctype'),
|
|
52
|
+
name: z.string(),
|
|
53
|
+
publicId: z.string().nullable(),
|
|
54
|
+
systemId: z.string().nullable(),
|
|
55
|
+
internalSubset: z.string().nullable(),
|
|
56
|
+
});
|
|
57
|
+
export const XmlTextValueSchema = z.object({
|
|
58
|
+
kind: z.literal('text'),
|
|
59
|
+
text: z.string(),
|
|
60
|
+
});
|
|
61
|
+
export const XmlCommentValueSchema = z.object({
|
|
62
|
+
kind: z.literal('comment'),
|
|
63
|
+
text: z.string(),
|
|
64
|
+
});
|
|
65
|
+
export const XmlCdataValueSchema = z.object({
|
|
66
|
+
kind: z.literal('cdata'),
|
|
67
|
+
text: z.string(),
|
|
68
|
+
});
|
|
69
|
+
export const XmlProcessingInstructionValueSchema = z.object({
|
|
70
|
+
kind: z.literal('processing-instruction'),
|
|
71
|
+
target: z.string().min(1),
|
|
72
|
+
data: z.string(),
|
|
73
|
+
});
|
|
74
|
+
export const xmlValueSchema = z.discriminatedUnion('kind', [
|
|
75
|
+
XmlDocumentValueSchema,
|
|
76
|
+
XmlElementValueSchema,
|
|
77
|
+
XmlTextValueSchema,
|
|
78
|
+
XmlCommentValueSchema,
|
|
79
|
+
XmlCdataValueSchema,
|
|
80
|
+
XmlProcessingInstructionValueSchema,
|
|
81
|
+
]);
|
|
82
|
+
// -----------------------------------------------------------------------------
|
|
83
|
+
// XML domain constructors
|
|
84
|
+
// -----------------------------------------------------------------------------
|
|
85
|
+
export function createConstructors(xmlTreeNodeApi) {
|
|
86
|
+
return {
|
|
87
|
+
document(id, children) {
|
|
88
|
+
return xmlTreeNodeApi.createLoadedBranch(id, { kind: 'document' }, children);
|
|
89
|
+
},
|
|
90
|
+
declaration(id, version, encoding, standalone) {
|
|
91
|
+
return xmlTreeNodeApi.createLeaf(id, {
|
|
92
|
+
kind: 'declaration',
|
|
93
|
+
version,
|
|
94
|
+
encoding,
|
|
95
|
+
standalone,
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
doctype(id, name, publicId, systemId, internalSubset) {
|
|
99
|
+
return xmlTreeNodeApi.createLeaf(id, {
|
|
100
|
+
kind: 'doctype',
|
|
101
|
+
name,
|
|
102
|
+
publicId,
|
|
103
|
+
systemId,
|
|
104
|
+
internalSubset,
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
element(id, name, attributes, children) {
|
|
108
|
+
return xmlTreeNodeApi.createLoadedBranch(id, {
|
|
109
|
+
kind: 'element',
|
|
110
|
+
name,
|
|
111
|
+
attributes,
|
|
112
|
+
}, children);
|
|
113
|
+
},
|
|
114
|
+
text(id, text) {
|
|
115
|
+
return xmlTreeNodeApi.createLeaf(id, {
|
|
116
|
+
kind: 'text',
|
|
117
|
+
text,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
comment(id, text) {
|
|
121
|
+
return xmlTreeNodeApi.createLeaf(id, {
|
|
122
|
+
kind: 'comment',
|
|
123
|
+
text,
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
cdata(id, text) {
|
|
127
|
+
return xmlTreeNodeApi.createLeaf(id, {
|
|
128
|
+
kind: 'cdata',
|
|
129
|
+
text,
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
processingInstruction(id, target, data) {
|
|
133
|
+
return xmlTreeNodeApi.createLeaf(id, {
|
|
134
|
+
kind: 'processing-instruction',
|
|
135
|
+
target,
|
|
136
|
+
data,
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-xml-file-resync.d.ts","sourceRoot":"","sources":["../../src/infrastructure/create-xml-file-resync.ts"],"names":[],"mappings":"AAGA,wBAAgB,+BAA+B,CAC7C,eAAe,EAAE,MAAM,GACtB,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAiFtC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { watch } from 'node:fs';
|
|
2
|
+
import { basename, dirname } from 'node:path';
|
|
3
|
+
export function createXmlFileResyncSubscription(xmlAbsolutePath) {
|
|
4
|
+
const listeners = new Set();
|
|
5
|
+
const parentDirectory = dirname(xmlAbsolutePath);
|
|
6
|
+
const fileName = basename(xmlAbsolutePath);
|
|
7
|
+
let watcher;
|
|
8
|
+
let notificationTimer;
|
|
9
|
+
function notifyListeners() {
|
|
10
|
+
for (const listener of listeners) {
|
|
11
|
+
listener();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function scheduleNotification() {
|
|
15
|
+
if (notificationTimer !== undefined) {
|
|
16
|
+
clearTimeout(notificationTimer);
|
|
17
|
+
}
|
|
18
|
+
notificationTimer = setTimeout(() => {
|
|
19
|
+
notificationTimer = undefined;
|
|
20
|
+
if (listeners.size > 0) {
|
|
21
|
+
notifyListeners();
|
|
22
|
+
}
|
|
23
|
+
}, 100);
|
|
24
|
+
}
|
|
25
|
+
function startWatcher() {
|
|
26
|
+
if (watcher !== undefined) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
watcher = watch(parentDirectory, (_eventType, changedName) => {
|
|
30
|
+
/*
|
|
31
|
+
* A null filename means the platform could not identify the changed
|
|
32
|
+
* entry. Conservatively resync in that case.
|
|
33
|
+
*/
|
|
34
|
+
if (changedName === null || changedName.toString() === fileName) {
|
|
35
|
+
scheduleNotification();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
watcher.on('error', () => {
|
|
39
|
+
/*
|
|
40
|
+
* The next createRoot() call will report the filesystem or XML parsing
|
|
41
|
+
* failure through normal application error handling.
|
|
42
|
+
*/
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function stopWatcher() {
|
|
46
|
+
if (notificationTimer !== undefined) {
|
|
47
|
+
clearTimeout(notificationTimer);
|
|
48
|
+
notificationTimer = undefined;
|
|
49
|
+
}
|
|
50
|
+
watcher?.close();
|
|
51
|
+
watcher = undefined;
|
|
52
|
+
}
|
|
53
|
+
return (listener) => {
|
|
54
|
+
listeners.add(listener);
|
|
55
|
+
startWatcher();
|
|
56
|
+
let subscribed = true;
|
|
57
|
+
return () => {
|
|
58
|
+
if (!subscribed) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
subscribed = false;
|
|
62
|
+
listeners.delete(listener);
|
|
63
|
+
if (listeners.size === 0) {
|
|
64
|
+
stopWatcher();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AppChild, AppRoot } from 'dirvi-apis';
|
|
2
|
+
import type { XmlId, XmlNodeValue } from '../domain/xml-tree.js';
|
|
3
|
+
export declare function createXmlRoot(filePath: string): AppRoot<XmlId, XmlNodeValue>;
|
|
4
|
+
/**
|
|
5
|
+
* Returns direct children in parser order. Paths are relative to the document
|
|
6
|
+
* root; an empty path selects the document itself.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getXmlEntries(filePath: string, source: string, path: readonly XmlId[]): readonly AppChild<XmlId, XmlNodeValue>[];
|
|
9
|
+
//# sourceMappingURL=get-xml-entries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-xml-entries.d.ts","sourceRoot":"","sources":["../../src/infrastructure/get-xml-entries.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAiIjE,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,YAAY,CAAC,CAK5E;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,KAAK,EAAE,GACrB,SAAS,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC,EAAE,CA2B1C"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { parseXml, XmlCdata, XmlComment, XmlDeclaration, XmlDocumentType, XmlElement, XmlProcessingInstruction, XmlText, } from '@rgrove/parse-xml';
|
|
3
|
+
import { createXmlId } from '../domain/xml-tree.js';
|
|
4
|
+
// RFC 4122 URL namespace, used to derive deterministic version-5 UUIDs.
|
|
5
|
+
const uuidNamespace = Buffer.from('6ba7b8119dad11d180b400c04fd430c8', 'hex');
|
|
6
|
+
function nodeId(filePath, position, name) {
|
|
7
|
+
const hash = createHash('sha1')
|
|
8
|
+
.update(uuidNamespace)
|
|
9
|
+
.update(JSON.stringify([filePath, position, name]), 'utf8')
|
|
10
|
+
.digest();
|
|
11
|
+
const bytes = Buffer.from(hash.subarray(0, 16));
|
|
12
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x50; // UUID version 5
|
|
13
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80; // RFC variant
|
|
14
|
+
const hex = bytes.toString('hex');
|
|
15
|
+
const uuid = [
|
|
16
|
+
hex.slice(0, 8),
|
|
17
|
+
hex.slice(8, 12),
|
|
18
|
+
hex.slice(12, 16),
|
|
19
|
+
hex.slice(16, 20),
|
|
20
|
+
hex.slice(20, 32),
|
|
21
|
+
].join('-');
|
|
22
|
+
return createXmlId(name, uuid);
|
|
23
|
+
}
|
|
24
|
+
function nodeName(node) {
|
|
25
|
+
if (node instanceof XmlElement ||
|
|
26
|
+
node instanceof XmlProcessingInstruction ||
|
|
27
|
+
node instanceof XmlDocumentType) {
|
|
28
|
+
return node.name;
|
|
29
|
+
}
|
|
30
|
+
if (node instanceof XmlText)
|
|
31
|
+
return '#text';
|
|
32
|
+
if (node instanceof XmlCdata)
|
|
33
|
+
return '#cdata';
|
|
34
|
+
if (node instanceof XmlComment)
|
|
35
|
+
return '#comment';
|
|
36
|
+
if (node instanceof XmlDeclaration)
|
|
37
|
+
return '#declaration';
|
|
38
|
+
throw new Error(`Unsupported XML node type "${node.type}".`);
|
|
39
|
+
}
|
|
40
|
+
function describeChild(filePath, position, node) {
|
|
41
|
+
const id = nodeId(filePath, position, nodeName(node));
|
|
42
|
+
if (node instanceof XmlElement) {
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
kind: 'unloaded-branch',
|
|
46
|
+
value: {
|
|
47
|
+
kind: 'element',
|
|
48
|
+
name: node.name,
|
|
49
|
+
attributes: Object.entries(node.attributes).map(([name, value]) => ({
|
|
50
|
+
name,
|
|
51
|
+
value,
|
|
52
|
+
})),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (node instanceof XmlText) {
|
|
57
|
+
return { id, kind: 'leaf', value: { kind: 'text', text: node.text } };
|
|
58
|
+
}
|
|
59
|
+
if (node instanceof XmlCdata) {
|
|
60
|
+
return { id, kind: 'leaf', value: { kind: 'cdata', text: node.text } };
|
|
61
|
+
}
|
|
62
|
+
if (node instanceof XmlComment) {
|
|
63
|
+
return {
|
|
64
|
+
id,
|
|
65
|
+
kind: 'leaf',
|
|
66
|
+
value: { kind: 'comment', text: node.content },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (node instanceof XmlProcessingInstruction) {
|
|
70
|
+
return {
|
|
71
|
+
id,
|
|
72
|
+
kind: 'leaf',
|
|
73
|
+
value: {
|
|
74
|
+
kind: 'processing-instruction',
|
|
75
|
+
target: node.name,
|
|
76
|
+
data: node.content,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (node instanceof XmlDeclaration) {
|
|
81
|
+
return {
|
|
82
|
+
id,
|
|
83
|
+
kind: 'leaf',
|
|
84
|
+
value: {
|
|
85
|
+
kind: 'declaration',
|
|
86
|
+
version: node.version,
|
|
87
|
+
encoding: node.encoding,
|
|
88
|
+
standalone: node.standalone,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
if (node instanceof XmlDocumentType) {
|
|
93
|
+
return {
|
|
94
|
+
id,
|
|
95
|
+
kind: 'leaf',
|
|
96
|
+
value: {
|
|
97
|
+
kind: 'doctype',
|
|
98
|
+
name: node.name,
|
|
99
|
+
publicId: node.publicId,
|
|
100
|
+
systemId: node.systemId,
|
|
101
|
+
internalSubset: node.internalSubset,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
throw new Error(`Unsupported XML node type "${node.type}".`);
|
|
106
|
+
}
|
|
107
|
+
export function createXmlRoot(filePath) {
|
|
108
|
+
return {
|
|
109
|
+
id: nodeId(filePath, [], '#document'),
|
|
110
|
+
value: { kind: 'document' },
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Returns direct children in parser order. Paths are relative to the document
|
|
115
|
+
* root; an empty path selects the document itself.
|
|
116
|
+
*/
|
|
117
|
+
export function getXmlEntries(filePath, source, path) {
|
|
118
|
+
const document = parseXml(source);
|
|
119
|
+
let children = document.children;
|
|
120
|
+
let position = [];
|
|
121
|
+
for (const segment of path) {
|
|
122
|
+
const index = children.findIndex((node, i) => nodeId(filePath, [...position, i], nodeName(node)) === segment);
|
|
123
|
+
if (index === -1) {
|
|
124
|
+
throw new Error(`XML path not found: ${path.join(' / ')}`);
|
|
125
|
+
}
|
|
126
|
+
const node = children[index];
|
|
127
|
+
if (!(node instanceof XmlElement)) {
|
|
128
|
+
throw new Error(`XML path addresses a leaf: ${path.join(' / ')}`);
|
|
129
|
+
}
|
|
130
|
+
position = [...position, index];
|
|
131
|
+
children = node.children;
|
|
132
|
+
}
|
|
133
|
+
return children.map((node, index) => describeChild(filePath, [...position, index], node));
|
|
134
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AppApi } from 'dirvi-apis';
|
|
2
|
+
import { type Position, type XmlNodeValue } from '../domain/xml-tree.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates the AppApi for one XML document.
|
|
5
|
+
*
|
|
6
|
+
* XML is parsed eagerly as one complete tree. Child order is retained by the
|
|
7
|
+
* XML domain tree and displayed through xmlNavApi's source-order navigation.
|
|
8
|
+
*/
|
|
9
|
+
export declare function loadNodeXmlAppApi(filePath: string): AppApi<Position, XmlNodeValue, string>;
|
|
10
|
+
export declare function createXmlFileResyncSubscription(xmlAbsolutePath: string): (listener: () => void) => () => void;
|
|
11
|
+
//# sourceMappingURL=load-node-xml-app-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-node-xml-app-api.d.ts","sourceRoot":"","sources":["../../src/infrastructure/load-node-xml-app-api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAGzC,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,YAAY,EAGlB,MAAM,uBAAuB,CAAC;AAI/B;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,MAAM,GACf,MAAM,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,CAAC,CA6CxC;AAED,wBAAgB,+BAA+B,CAC7C,eAAe,EAAE,MAAM,GACtB,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAiFtC"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { watch } from 'node:fs';
|
|
2
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
3
|
+
import { createAppStateApis } from 'dirvi-lib';
|
|
4
|
+
import { xmlFoldsApi, xmlNavApi, } from '../domain/xml-tree.js';
|
|
5
|
+
import { readFile } from 'node:fs/promises';
|
|
6
|
+
import { parseXmlDocument } from './parse-xml-document.js';
|
|
7
|
+
/**
|
|
8
|
+
* Creates the AppApi for one XML document.
|
|
9
|
+
*
|
|
10
|
+
* XML is parsed eagerly as one complete tree. Child order is retained by the
|
|
11
|
+
* XML domain tree and displayed through xmlNavApi's source-order navigation.
|
|
12
|
+
*/
|
|
13
|
+
export function loadNodeXmlAppApi(filePath) {
|
|
14
|
+
const xmlAbsolutePath = resolve(filePath);
|
|
15
|
+
const apis = createAppStateApis();
|
|
16
|
+
return {
|
|
17
|
+
name: basename(xmlAbsolutePath),
|
|
18
|
+
/*
|
|
19
|
+
* parseXmlDocument() reserves position 0 for the synthetic document root.
|
|
20
|
+
*/
|
|
21
|
+
rootId: 0,
|
|
22
|
+
createRoot: async () => {
|
|
23
|
+
const source = await readFile(xmlAbsolutePath, 'utf8');
|
|
24
|
+
return parseXmlDocument(source);
|
|
25
|
+
},
|
|
26
|
+
/*
|
|
27
|
+
* XML documents are parsed completely when their root is created. No XML
|
|
28
|
+
* branch is represented as an unloaded branch.
|
|
29
|
+
*/
|
|
30
|
+
async loadBranches() {
|
|
31
|
+
throw new Error('Cannot load XML branches: parsed XML documents have no unloaded branches.');
|
|
32
|
+
},
|
|
33
|
+
/*
|
|
34
|
+
* Watch the parent directory rather than the file directly. Editors often
|
|
35
|
+
* save atomically by replacing the document file with a renamed temporary
|
|
36
|
+
* file, which can invalidate a direct file watcher.
|
|
37
|
+
*/
|
|
38
|
+
subscribeToResync: createXmlFileResyncSubscription(xmlAbsolutePath),
|
|
39
|
+
/*
|
|
40
|
+
* The absolute filename identifies this app instance. View persistence is
|
|
41
|
+
* intentionally not wired yet.
|
|
42
|
+
*/
|
|
43
|
+
// viewKey: xmlAbsolutePath,
|
|
44
|
+
foldsApi: xmlFoldsApi,
|
|
45
|
+
navNodeApi: xmlNavApi,
|
|
46
|
+
...apis,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export function createXmlFileResyncSubscription(xmlAbsolutePath) {
|
|
50
|
+
const listeners = new Set();
|
|
51
|
+
const parentDirectory = dirname(xmlAbsolutePath);
|
|
52
|
+
const fileName = basename(xmlAbsolutePath);
|
|
53
|
+
let watcher;
|
|
54
|
+
let notificationTimer;
|
|
55
|
+
function notifyListeners() {
|
|
56
|
+
for (const listener of listeners) {
|
|
57
|
+
listener();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function scheduleNotification() {
|
|
61
|
+
if (notificationTimer !== undefined) {
|
|
62
|
+
clearTimeout(notificationTimer);
|
|
63
|
+
}
|
|
64
|
+
notificationTimer = setTimeout(() => {
|
|
65
|
+
notificationTimer = undefined;
|
|
66
|
+
if (listeners.size > 0) {
|
|
67
|
+
notifyListeners();
|
|
68
|
+
}
|
|
69
|
+
}, 100);
|
|
70
|
+
}
|
|
71
|
+
function startWatcher() {
|
|
72
|
+
if (watcher !== undefined) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
watcher = watch(parentDirectory, (_eventType, changedName) => {
|
|
76
|
+
/*
|
|
77
|
+
* A null filename means the platform could not identify the changed
|
|
78
|
+
* entry. Conservatively resync in that case.
|
|
79
|
+
*/
|
|
80
|
+
if (changedName === null || changedName.toString() === fileName) {
|
|
81
|
+
scheduleNotification();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
watcher.on('error', () => {
|
|
85
|
+
/*
|
|
86
|
+
* The next createRoot() call will report the filesystem or XML parsing
|
|
87
|
+
* failure through normal application error handling.
|
|
88
|
+
*/
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function stopWatcher() {
|
|
92
|
+
if (notificationTimer !== undefined) {
|
|
93
|
+
clearTimeout(notificationTimer);
|
|
94
|
+
notificationTimer = undefined;
|
|
95
|
+
}
|
|
96
|
+
watcher?.close();
|
|
97
|
+
watcher = undefined;
|
|
98
|
+
}
|
|
99
|
+
return (listener) => {
|
|
100
|
+
listeners.add(listener);
|
|
101
|
+
startWatcher();
|
|
102
|
+
let subscribed = true;
|
|
103
|
+
return () => {
|
|
104
|
+
if (!subscribed) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
subscribed = false;
|
|
108
|
+
listeners.delete(listener);
|
|
109
|
+
if (listeners.size === 0) {
|
|
110
|
+
stopWatcher();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { XmlTreeNode } from '../domain/xml-tree.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reads one UTF-8 XML file and parses it into the XML domain tree.
|
|
4
|
+
*/
|
|
5
|
+
export declare function loadNodeXmlDocument(filePath: string): Promise<XmlTreeNode>;
|
|
6
|
+
//# sourceMappingURL=load-node-xml-document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-node-xml-document.d.ts","sourceRoot":"","sources":["../../src/infrastructure/load-node-xml-document.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGzD;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,WAAW,CAAC,CAItB"}
|