capdag 0.102.232 → 0.106.243

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.
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env node
2
+ // Build browser-compatible bundles of tagged-urn, capdag, and
3
+ // cap-graph-renderer from the local sources + resolved tagged-urn
4
+ // dependency. Outputs three self-contained IIFE-wrapped JS files to
5
+ // `dist/` that each expose their exported classes as window globals.
6
+ //
7
+ // The browser build exists because capdag-js source files are CJS:
8
+ // they use `require()` and `module.exports`, which a plain <script tag>
9
+ // cannot load. For browser consumers (capdag-dot-com, machfab-mac
10
+ // WKWebViews) we strip those CJS hooks and wrap the result in an IIFE
11
+ // that assigns to window.*.
12
+ //
13
+ // Load order at the consumer:
14
+ // 1. tagged-urn.js — defines window.TaggedUrn, etc.
15
+ // 2. capdag.js — reads window.TaggedUrn, defines CapUrn,
16
+ // MediaUrn, Cap, CapGraph, createCap, …
17
+ // 3. cap-graph-renderer.js — reads window.cytoscape + capdag globals
18
+ // at call time, defines CapGraphRenderer.
19
+ //
20
+ // Running: `node build-browser.js [outDir]`. Default outDir is ./dist.
21
+
22
+ 'use strict';
23
+
24
+ const fs = require('fs');
25
+ const path = require('path');
26
+
27
+ const here = __dirname;
28
+ const outDir = process.argv[2]
29
+ ? path.resolve(process.argv[2])
30
+ : path.join(here, 'dist');
31
+ fs.mkdirSync(outDir, { recursive: true });
32
+
33
+ function stripCJS(src) {
34
+ // Strip the CJS `const { TaggedUrn } = require('tagged-urn')` line and
35
+ // the trailing `module.exports = {...}` block. Both are at known
36
+ // positions in the source; the regex matches the full single-line
37
+ // require and the multi-line module.exports object literal.
38
+ return src
39
+ .replace(/^\/\/.*Import TaggedUrn.*\n/m, '')
40
+ .replace(/^const\s*\{\s*TaggedUrn\s*\}\s*=\s*require\s*\(\s*['"]tagged-urn['"]\s*\)\s*;?\s*$/m, '')
41
+ .replace(/^module\.exports\s*=\s*\{[\s\S]*?\};?\s*$/m, '');
42
+ }
43
+
44
+ function buildTaggedUrn() {
45
+ // tagged-urn is a sibling npm dependency. Resolve its main file.
46
+ const srcPath = require.resolve('tagged-urn');
47
+ const src = fs.readFileSync(srcPath, 'utf8');
48
+ const processed = src.replace(/^module\.exports\s*=\s*\{[\s\S]*?\};?\s*$/m, '');
49
+ const wrapped = `// tagged-urn — browser build
50
+ // Generated from the tagged-urn npm package by capdag-js/build-browser.js.
51
+ // Do not edit directly.
52
+
53
+ (function() {
54
+ 'use strict';
55
+
56
+ ${processed}
57
+
58
+ window.TaggedUrn = TaggedUrn;
59
+ window.TaggedUrnBuilder = TaggedUrnBuilder;
60
+ window.UrnMatcher = UrnMatcher;
61
+ window.TaggedUrnError = TaggedUrnError;
62
+ window.TaggedUrnErrorCodes = ErrorCodes;
63
+
64
+ })();
65
+ `;
66
+ fs.writeFileSync(path.join(outDir, 'tagged-urn.js'), wrapped);
67
+ console.log(` wrote ${path.join(outDir, 'tagged-urn.js')}`);
68
+ }
69
+
70
+ function buildCapdag() {
71
+ const srcPath = path.join(here, 'capdag.js');
72
+ const src = fs.readFileSync(srcPath, 'utf8');
73
+
74
+ const parserPath = path.join(here, 'machine-parser.js');
75
+ if (!fs.existsSync(parserPath)) {
76
+ throw new Error(`machine-parser.js not found at ${parserPath} — run 'npm run build:parser' first`);
77
+ }
78
+ const parserSrc = fs.readFileSync(parserPath, 'utf8');
79
+
80
+ let processed = stripCJS(src);
81
+ // Replace the `const machineParser = require('./machine-parser.js')`
82
+ // line with a local variable the inlined parser assigns below.
83
+ processed = processed.replace(
84
+ /^const\s+machineParser\s*=\s*require\s*\(\s*['"]\.\/machine-parser\.js['"]\s*\)\s*;?\s*$/m,
85
+ '// machineParser is inlined above'
86
+ );
87
+
88
+ const inlinedParser = `
89
+ // Inlined Peggy-generated machine parser.
90
+ var machineParser = (function() {
91
+ var module = { exports: {} };
92
+ var exports = module.exports;
93
+ ${parserSrc}
94
+ return module.exports;
95
+ })();
96
+ `;
97
+
98
+ const wrapped = `// capdag — browser build
99
+ // Generated from capdag-js/capdag.js by capdag-js/build-browser.js.
100
+ // Do not edit directly. Requires tagged-urn.js loaded first.
101
+
102
+ (function() {
103
+ 'use strict';
104
+
105
+ const { TaggedUrn } = window;
106
+ if (!TaggedUrn) {
107
+ throw new Error('TaggedUrn global is not defined. Load tagged-urn.js before capdag.js.');
108
+ }
109
+
110
+ ${inlinedParser}
111
+
112
+ ${processed}
113
+
114
+ // Expose every public class and function as a window global.
115
+ window.CapUrn = CapUrn;
116
+ window.CapUrnBuilder = CapUrnBuilder;
117
+ window.CapMatcher = CapMatcher;
118
+ window.CapUrnError = CapUrnError;
119
+ window.CapUrnErrorCodes = ErrorCodes;
120
+ window.MediaUrn = MediaUrn;
121
+ window.MediaUrnError = MediaUrnError;
122
+ window.MediaUrnErrorCodes = MediaUrnErrorCodes;
123
+ window.Cap = Cap;
124
+ window.CapArg = CapArg;
125
+ window.ArgSource = ArgSource;
126
+ window.RegisteredBy = RegisteredBy;
127
+ window.createCap = createCap;
128
+ window.createCapWithDescription = createCapWithDescription;
129
+ window.createCapWithMetadata = createCapWithMetadata;
130
+ window.createCapWithDescriptionAndMetadata = createCapWithDescriptionAndMetadata;
131
+ window.ValidationError = ValidationError;
132
+ window.InputValidator = InputValidator;
133
+ window.OutputValidator = OutputValidator;
134
+ window.CapValidator = CapValidator;
135
+ window.validateCapArgs = validateCapArgs;
136
+ window.RESERVED_CLI_FLAGS = RESERVED_CLI_FLAGS;
137
+ window.MediaSpec = MediaSpec;
138
+ window.MediaSpecError = MediaSpecError;
139
+ window.MediaSpecErrorCodes = MediaSpecErrorCodes;
140
+ window.isBinaryCapUrn = isBinaryCapUrn;
141
+ window.isJSONCapUrn = isJSONCapUrn;
142
+ window.isStructuredCapUrn = isStructuredCapUrn;
143
+ window.resolveMediaUrn = resolveMediaUrn;
144
+ window.buildExtensionIndex = buildExtensionIndex;
145
+ window.mediaUrnsForExtension = mediaUrnsForExtension;
146
+ window.getExtensionMappings = getExtensionMappings;
147
+ window.CapMatrixError = CapMatrixError;
148
+ window.CapMatrix = CapMatrix;
149
+ window.BestCapSetMatch = BestCapSetMatch;
150
+ window.CompositeCapSet = CompositeCapSet;
151
+ window.CapBlock = CapBlock;
152
+ window.CapGraphEdge = CapGraphEdge;
153
+ window.CapGraphStats = CapGraphStats;
154
+ window.CapGraph = CapGraph;
155
+ window.StdinSource = StdinSource;
156
+ window.StdinSourceKind = StdinSourceKind;
157
+ window.CapArgumentValue = CapArgumentValue;
158
+ window.MachineSyntaxError = MachineSyntaxError;
159
+ window.MachineSyntaxErrorCodes = MachineSyntaxErrorCodes;
160
+ window.MachineEdge = MachineEdge;
161
+ window.Machine = Machine;
162
+ window.MachineBuilder = MachineBuilder;
163
+ window.parseMachine = parseMachine;
164
+
165
+ })();
166
+ `;
167
+
168
+ fs.writeFileSync(path.join(outDir, 'capdag.js'), wrapped);
169
+ console.log(` wrote ${path.join(outDir, 'capdag.js')}`);
170
+ }
171
+
172
+ function buildCapGraphRenderer() {
173
+ const srcPath = path.join(here, 'cap-graph-renderer.js');
174
+ const src = fs.readFileSync(srcPath, 'utf8');
175
+ // The file's CJS exports block is at the bottom, guarded by
176
+ // `typeof module !== 'undefined'`. Strip everything from that guard
177
+ // to the end of the file.
178
+ const stripped = src.replace(
179
+ /if\s*\(\s*typeof\s+module\s*!==\s*'undefined'[\s\S]*$/,
180
+ ''
181
+ );
182
+ const wrapped = `// cap-graph-renderer — browser build
183
+ // Generated from capdag-js/cap-graph-renderer.js by capdag-js/build-browser.js.
184
+ // Do not edit directly. Requires cytoscape, cytoscape-elk, tagged-urn.js,
185
+ // and capdag.js to be loaded first.
186
+
187
+ (function() {
188
+ 'use strict';
189
+
190
+ ${stripped}
191
+
192
+ window.CapGraphRenderer = CapGraphRenderer;
193
+
194
+ })();
195
+ `;
196
+ fs.writeFileSync(path.join(outDir, 'cap-graph-renderer.js'), wrapped);
197
+ console.log(` wrote ${path.join(outDir, 'cap-graph-renderer.js')}`);
198
+ }
199
+
200
+ buildTaggedUrn();
201
+ buildCapdag();
202
+ buildCapGraphRenderer();
203
+ console.log(`browser bundles written to ${outDir}`);