jww-parser 1.0.0

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/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # jww-parser
2
+
3
+ JWW (Jw_cad) file parser and DXF converter for JavaScript/TypeScript.
4
+
5
+ This package uses WebAssembly to parse JWW binary files (created by [Jw_cad](https://www.jwcad.net/), a popular Japanese CAD software) and convert them to DXF format.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install jww-parser
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Node.js
16
+
17
+ ```typescript
18
+ import { createParser } from 'jww-parser';
19
+ import { readFileSync } from 'fs';
20
+
21
+ async function main() {
22
+ // Create and initialize the parser
23
+ const parser = await createParser();
24
+
25
+ // Read a JWW file
26
+ const jwwData = readFileSync('drawing.jww');
27
+ const data = new Uint8Array(jwwData);
28
+
29
+ // Parse JWW to get document structure
30
+ const doc = parser.parse(data);
31
+ console.log('Entities:', doc.Entities.length);
32
+ console.log('Layers:', doc.LayerGroups.length);
33
+
34
+ // Convert to DXF string
35
+ const dxfString = parser.toDxfString(data);
36
+ writeFileSync('output.dxf', dxfString);
37
+ }
38
+
39
+ main();
40
+ ```
41
+
42
+ ### Browser
43
+
44
+ ```html
45
+ <!-- Include wasm_exec.js from Go -->
46
+ <script src="wasm_exec.js"></script>
47
+ <script type="module">
48
+ import { createParser } from 'jww-parser';
49
+
50
+ async function convertFile(file) {
51
+ const parser = await createParser('path/to/jww-parser.wasm');
52
+
53
+ const buffer = await file.arrayBuffer();
54
+ const data = new Uint8Array(buffer);
55
+
56
+ // Get DXF content
57
+ const dxfString = parser.toDxfString(data);
58
+
59
+ // Download as file
60
+ const blob = new Blob([dxfString], { type: 'application/dxf' });
61
+ const url = URL.createObjectURL(blob);
62
+ const a = document.createElement('a');
63
+ a.href = url;
64
+ a.download = file.name.replace('.jww', '.dxf');
65
+ a.click();
66
+ }
67
+ </script>
68
+ ```
69
+
70
+ ## API
71
+
72
+ ### `createParser(wasmPath?: string): Promise<JwwParser>`
73
+
74
+ Create and initialize a JWW parser instance.
75
+
76
+ - `wasmPath` - Optional path to the `jww-parser.wasm` file
77
+
78
+ ### `JwwParser`
79
+
80
+ #### `init(): Promise<void>`
81
+
82
+ Initialize the WASM module. Called automatically by `createParser()`.
83
+
84
+ #### `parse(data: Uint8Array): JwwDocument`
85
+
86
+ Parse a JWW file and return the document structure.
87
+
88
+ #### `toDxf(data: Uint8Array): DxfDocument`
89
+
90
+ Parse a JWW file and convert to DXF document structure (JSON).
91
+
92
+ #### `toDxfString(data: Uint8Array): string`
93
+
94
+ Parse a JWW file and convert to DXF file content string.
95
+
96
+ ## Types
97
+
98
+ ### JwwDocument
99
+
100
+ ```typescript
101
+ interface JwwDocument {
102
+ Version: number;
103
+ Memo: string;
104
+ PaperSize: number;
105
+ LayerGroups: LayerGroup[];
106
+ Entities: JwwEntity[];
107
+ Blocks: JwwBlock[];
108
+ }
109
+ ```
110
+
111
+ ### JwwEntity
112
+
113
+ Supported entity types:
114
+ - `JwwLine` - Line segments
115
+ - `JwwArc` - Arcs and circles
116
+ - `JwwPoint` - Points
117
+ - `JwwText` - Text annotations
118
+ - `JwwSolid` - Solid fills
119
+ - `JwwBlockRef` - Block references
120
+
121
+ ### DxfDocument
122
+
123
+ ```typescript
124
+ interface DxfDocument {
125
+ Layers: DxfLayer[];
126
+ Entities: DxfEntity[];
127
+ Blocks: DxfBlock[];
128
+ }
129
+ ```
130
+
131
+ ## Requirements
132
+
133
+ - Node.js >= 18.0.0 (for Node.js usage)
134
+ - Modern browser with WebAssembly support (for browser usage)
135
+
136
+ ## License
137
+
138
+ AGPL-3.0
@@ -0,0 +1,168 @@
1
+ /**
2
+ * JWW (Jw_cad) file parser and DXF converter
3
+ *
4
+ * This module provides functionality to parse JWW binary files
5
+ * and convert them to DXF format using WebAssembly.
6
+ */
7
+ interface JwwDocument {
8
+ Version: number;
9
+ Memo: string;
10
+ PaperSize: number;
11
+ LayerGroups: LayerGroup[];
12
+ Entities: JwwEntity[];
13
+ Blocks: JwwBlock[];
14
+ }
15
+ interface LayerGroup {
16
+ Name: string;
17
+ Layers: Layer[];
18
+ }
19
+ interface Layer {
20
+ Name: string;
21
+ Visible: boolean;
22
+ Locked: boolean;
23
+ }
24
+ interface JwwEntityBase {
25
+ Type: string;
26
+ Group: number;
27
+ PenStyle: number;
28
+ PenColor: number;
29
+ PenWidth: number;
30
+ Layer: number;
31
+ LayerGroup: number;
32
+ }
33
+ interface JwwLine extends JwwEntityBase {
34
+ Type: "Line";
35
+ X1: number;
36
+ Y1: number;
37
+ X2: number;
38
+ Y2: number;
39
+ }
40
+ interface JwwArc extends JwwEntityBase {
41
+ Type: "Arc";
42
+ CenterX: number;
43
+ CenterY: number;
44
+ Radius: number;
45
+ StartAngle: number;
46
+ EndAngle: number;
47
+ Flatness: number;
48
+ }
49
+ interface JwwPoint extends JwwEntityBase {
50
+ Type: "Point";
51
+ X: number;
52
+ Y: number;
53
+ Code: number;
54
+ }
55
+ interface JwwText extends JwwEntityBase {
56
+ Type: "Text";
57
+ X: number;
58
+ Y: number;
59
+ Text: string;
60
+ FontName: string;
61
+ Height: number;
62
+ Width: number;
63
+ Angle: number;
64
+ }
65
+ interface JwwSolid extends JwwEntityBase {
66
+ Type: "Solid";
67
+ Points: [number, number][];
68
+ }
69
+ interface JwwBlockRef extends JwwEntityBase {
70
+ Type: "Block";
71
+ X: number;
72
+ Y: number;
73
+ ScaleX: number;
74
+ ScaleY: number;
75
+ Angle: number;
76
+ BlockNumber: number;
77
+ }
78
+ type JwwEntity = JwwLine | JwwArc | JwwPoint | JwwText | JwwSolid | JwwBlockRef;
79
+ interface JwwBlock {
80
+ Name: string;
81
+ Entities: JwwEntity[];
82
+ }
83
+ interface DxfDocument {
84
+ Layers: DxfLayer[];
85
+ Entities: DxfEntity[];
86
+ Blocks: DxfBlock[];
87
+ }
88
+ interface DxfLayer {
89
+ Name: string;
90
+ Color: number;
91
+ Frozen: boolean;
92
+ Locked: boolean;
93
+ }
94
+ interface DxfEntity {
95
+ Type: string;
96
+ Layer: string;
97
+ Color?: number;
98
+ [key: string]: unknown;
99
+ }
100
+ interface DxfBlock {
101
+ Name: string;
102
+ Entities: DxfEntity[];
103
+ }
104
+ interface WasmResult {
105
+ ok: boolean;
106
+ data?: string;
107
+ error?: string;
108
+ }
109
+ declare global {
110
+ var Go: new () => GoInstance;
111
+ var jwwParse: (data: Uint8Array) => WasmResult;
112
+ var jwwToDxf: (data: Uint8Array) => WasmResult;
113
+ var jwwToDxfString: (data: Uint8Array) => WasmResult;
114
+ }
115
+ interface GoInstance {
116
+ importObject: WebAssembly.Imports;
117
+ run(instance: WebAssembly.Instance): Promise<void>;
118
+ }
119
+ declare class JwwParser {
120
+ private initialized;
121
+ private initPromise;
122
+ private wasmPath;
123
+ /**
124
+ * Create a new JWW parser instance
125
+ * @param wasmPath - Path to the jww-parser.wasm file
126
+ */
127
+ constructor(wasmPath?: string);
128
+ private getDefaultWasmPath;
129
+ /**
130
+ * Initialize the WASM module
131
+ * Must be called before using parse methods
132
+ */
133
+ init(): Promise<void>;
134
+ private loadWasm;
135
+ private loadWasmExec;
136
+ private waitForWasmFunctions;
137
+ private ensureInitialized;
138
+ /**
139
+ * Parse a JWW file and return the document structure
140
+ * @param data - JWW file content as Uint8Array
141
+ * @returns Parsed JWW document
142
+ */
143
+ parse(data: Uint8Array): JwwDocument;
144
+ /**
145
+ * Parse a JWW file and convert to DXF document structure
146
+ * @param data - JWW file content as Uint8Array
147
+ * @returns DXF document object
148
+ */
149
+ toDxf(data: Uint8Array): DxfDocument;
150
+ /**
151
+ * Parse a JWW file and convert to DXF file content string
152
+ * @param data - JWW file content as Uint8Array
153
+ * @returns DXF file content as string
154
+ */
155
+ toDxfString(data: Uint8Array): string;
156
+ }
157
+ /**
158
+ * Create and initialize a JWW parser instance
159
+ * @param wasmPath - Optional path to the jww-parser.wasm file
160
+ * @returns Initialized JwwParser instance
161
+ */
162
+ declare function createParser(wasmPath?: string): Promise<JwwParser>;
163
+ declare const _default: {
164
+ JwwParser: typeof JwwParser;
165
+ createParser: typeof createParser;
166
+ };
167
+
168
+ export { type DxfBlock, type DxfDocument, type DxfEntity, type DxfLayer, type JwwArc, type JwwBlock, type JwwBlockRef, type JwwDocument, type JwwEntity, type JwwEntityBase, type JwwLine, JwwParser, type JwwPoint, type JwwSolid, type JwwText, type Layer, type LayerGroup, createParser, _default as default };
@@ -0,0 +1,168 @@
1
+ /**
2
+ * JWW (Jw_cad) file parser and DXF converter
3
+ *
4
+ * This module provides functionality to parse JWW binary files
5
+ * and convert them to DXF format using WebAssembly.
6
+ */
7
+ interface JwwDocument {
8
+ Version: number;
9
+ Memo: string;
10
+ PaperSize: number;
11
+ LayerGroups: LayerGroup[];
12
+ Entities: JwwEntity[];
13
+ Blocks: JwwBlock[];
14
+ }
15
+ interface LayerGroup {
16
+ Name: string;
17
+ Layers: Layer[];
18
+ }
19
+ interface Layer {
20
+ Name: string;
21
+ Visible: boolean;
22
+ Locked: boolean;
23
+ }
24
+ interface JwwEntityBase {
25
+ Type: string;
26
+ Group: number;
27
+ PenStyle: number;
28
+ PenColor: number;
29
+ PenWidth: number;
30
+ Layer: number;
31
+ LayerGroup: number;
32
+ }
33
+ interface JwwLine extends JwwEntityBase {
34
+ Type: "Line";
35
+ X1: number;
36
+ Y1: number;
37
+ X2: number;
38
+ Y2: number;
39
+ }
40
+ interface JwwArc extends JwwEntityBase {
41
+ Type: "Arc";
42
+ CenterX: number;
43
+ CenterY: number;
44
+ Radius: number;
45
+ StartAngle: number;
46
+ EndAngle: number;
47
+ Flatness: number;
48
+ }
49
+ interface JwwPoint extends JwwEntityBase {
50
+ Type: "Point";
51
+ X: number;
52
+ Y: number;
53
+ Code: number;
54
+ }
55
+ interface JwwText extends JwwEntityBase {
56
+ Type: "Text";
57
+ X: number;
58
+ Y: number;
59
+ Text: string;
60
+ FontName: string;
61
+ Height: number;
62
+ Width: number;
63
+ Angle: number;
64
+ }
65
+ interface JwwSolid extends JwwEntityBase {
66
+ Type: "Solid";
67
+ Points: [number, number][];
68
+ }
69
+ interface JwwBlockRef extends JwwEntityBase {
70
+ Type: "Block";
71
+ X: number;
72
+ Y: number;
73
+ ScaleX: number;
74
+ ScaleY: number;
75
+ Angle: number;
76
+ BlockNumber: number;
77
+ }
78
+ type JwwEntity = JwwLine | JwwArc | JwwPoint | JwwText | JwwSolid | JwwBlockRef;
79
+ interface JwwBlock {
80
+ Name: string;
81
+ Entities: JwwEntity[];
82
+ }
83
+ interface DxfDocument {
84
+ Layers: DxfLayer[];
85
+ Entities: DxfEntity[];
86
+ Blocks: DxfBlock[];
87
+ }
88
+ interface DxfLayer {
89
+ Name: string;
90
+ Color: number;
91
+ Frozen: boolean;
92
+ Locked: boolean;
93
+ }
94
+ interface DxfEntity {
95
+ Type: string;
96
+ Layer: string;
97
+ Color?: number;
98
+ [key: string]: unknown;
99
+ }
100
+ interface DxfBlock {
101
+ Name: string;
102
+ Entities: DxfEntity[];
103
+ }
104
+ interface WasmResult {
105
+ ok: boolean;
106
+ data?: string;
107
+ error?: string;
108
+ }
109
+ declare global {
110
+ var Go: new () => GoInstance;
111
+ var jwwParse: (data: Uint8Array) => WasmResult;
112
+ var jwwToDxf: (data: Uint8Array) => WasmResult;
113
+ var jwwToDxfString: (data: Uint8Array) => WasmResult;
114
+ }
115
+ interface GoInstance {
116
+ importObject: WebAssembly.Imports;
117
+ run(instance: WebAssembly.Instance): Promise<void>;
118
+ }
119
+ declare class JwwParser {
120
+ private initialized;
121
+ private initPromise;
122
+ private wasmPath;
123
+ /**
124
+ * Create a new JWW parser instance
125
+ * @param wasmPath - Path to the jww-parser.wasm file
126
+ */
127
+ constructor(wasmPath?: string);
128
+ private getDefaultWasmPath;
129
+ /**
130
+ * Initialize the WASM module
131
+ * Must be called before using parse methods
132
+ */
133
+ init(): Promise<void>;
134
+ private loadWasm;
135
+ private loadWasmExec;
136
+ private waitForWasmFunctions;
137
+ private ensureInitialized;
138
+ /**
139
+ * Parse a JWW file and return the document structure
140
+ * @param data - JWW file content as Uint8Array
141
+ * @returns Parsed JWW document
142
+ */
143
+ parse(data: Uint8Array): JwwDocument;
144
+ /**
145
+ * Parse a JWW file and convert to DXF document structure
146
+ * @param data - JWW file content as Uint8Array
147
+ * @returns DXF document object
148
+ */
149
+ toDxf(data: Uint8Array): DxfDocument;
150
+ /**
151
+ * Parse a JWW file and convert to DXF file content string
152
+ * @param data - JWW file content as Uint8Array
153
+ * @returns DXF file content as string
154
+ */
155
+ toDxfString(data: Uint8Array): string;
156
+ }
157
+ /**
158
+ * Create and initialize a JWW parser instance
159
+ * @param wasmPath - Optional path to the jww-parser.wasm file
160
+ * @returns Initialized JwwParser instance
161
+ */
162
+ declare function createParser(wasmPath?: string): Promise<JwwParser>;
163
+ declare const _default: {
164
+ JwwParser: typeof JwwParser;
165
+ createParser: typeof createParser;
166
+ };
167
+
168
+ export { type DxfBlock, type DxfDocument, type DxfEntity, type DxfLayer, type JwwArc, type JwwBlock, type JwwBlockRef, type JwwDocument, type JwwEntity, type JwwEntityBase, type JwwLine, JwwParser, type JwwPoint, type JwwSolid, type JwwText, type Layer, type LayerGroup, createParser, _default as default };
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ JwwParser: () => JwwParser,
34
+ createParser: () => createParser,
35
+ default: () => index_default
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+ var import_meta = {};
39
+ var JwwParser = class {
40
+ /**
41
+ * Create a new JWW parser instance
42
+ * @param wasmPath - Path to the jww-parser.wasm file
43
+ */
44
+ constructor(wasmPath) {
45
+ this.initialized = false;
46
+ this.initPromise = null;
47
+ this.wasmPath = wasmPath || this.getDefaultWasmPath();
48
+ }
49
+ getDefaultWasmPath() {
50
+ if (typeof process !== "undefined" && process.versions?.node) {
51
+ return new URL("../wasm/jww-parser.wasm", import_meta.url).pathname;
52
+ }
53
+ return "jww-parser.wasm";
54
+ }
55
+ /**
56
+ * Initialize the WASM module
57
+ * Must be called before using parse methods
58
+ */
59
+ async init() {
60
+ if (this.initialized) return;
61
+ if (this.initPromise) return this.initPromise;
62
+ this.initPromise = this.loadWasm();
63
+ await this.initPromise;
64
+ this.initialized = true;
65
+ }
66
+ async loadWasm() {
67
+ if (typeof Go === "undefined") {
68
+ await this.loadWasmExec();
69
+ }
70
+ const go = new Go();
71
+ let wasmInstance;
72
+ if (typeof process !== "undefined" && process.versions?.node) {
73
+ const fs = await import("fs");
74
+ const path = await import("path");
75
+ const wasmBuffer = fs.readFileSync(this.wasmPath);
76
+ const wasmModule = await WebAssembly.compile(wasmBuffer);
77
+ wasmInstance = await WebAssembly.instantiate(wasmModule, go.importObject);
78
+ } else {
79
+ const result = await WebAssembly.instantiateStreaming(
80
+ fetch(this.wasmPath),
81
+ go.importObject
82
+ ).catch(async () => {
83
+ const response = await fetch(this.wasmPath);
84
+ const bytes = await response.arrayBuffer();
85
+ return WebAssembly.instantiate(bytes, go.importObject);
86
+ });
87
+ wasmInstance = result.instance;
88
+ }
89
+ go.run(wasmInstance);
90
+ await this.waitForWasmFunctions();
91
+ }
92
+ async loadWasmExec() {
93
+ if (typeof process !== "undefined" && process.versions?.node) {
94
+ const wasmExecPath = new URL("../wasm/wasm_exec.js", import_meta.url).pathname;
95
+ await import(wasmExecPath);
96
+ } else {
97
+ throw new Error(
98
+ "Go runtime not loaded. Please include wasm_exec.js in your HTML."
99
+ );
100
+ }
101
+ }
102
+ async waitForWasmFunctions(timeout = 5e3, interval = 50) {
103
+ const start = Date.now();
104
+ while (Date.now() - start < timeout) {
105
+ if (typeof globalThis.jwwParse === "function" && typeof globalThis.jwwToDxf === "function" && typeof globalThis.jwwToDxfString === "function") {
106
+ return;
107
+ }
108
+ await new Promise((resolve) => setTimeout(resolve, interval));
109
+ }
110
+ throw new Error("WASM functions not available after timeout");
111
+ }
112
+ ensureInitialized() {
113
+ if (!this.initialized) {
114
+ throw new Error("Parser not initialized. Call init() first.");
115
+ }
116
+ }
117
+ /**
118
+ * Parse a JWW file and return the document structure
119
+ * @param data - JWW file content as Uint8Array
120
+ * @returns Parsed JWW document
121
+ */
122
+ parse(data) {
123
+ this.ensureInitialized();
124
+ const result = globalThis.jwwParse(data);
125
+ if (!result.ok) {
126
+ throw new Error(result.error || "Parse failed");
127
+ }
128
+ return JSON.parse(result.data);
129
+ }
130
+ /**
131
+ * Parse a JWW file and convert to DXF document structure
132
+ * @param data - JWW file content as Uint8Array
133
+ * @returns DXF document object
134
+ */
135
+ toDxf(data) {
136
+ this.ensureInitialized();
137
+ const result = globalThis.jwwToDxf(data);
138
+ if (!result.ok) {
139
+ throw new Error(result.error || "Conversion failed");
140
+ }
141
+ return JSON.parse(result.data);
142
+ }
143
+ /**
144
+ * Parse a JWW file and convert to DXF file content string
145
+ * @param data - JWW file content as Uint8Array
146
+ * @returns DXF file content as string
147
+ */
148
+ toDxfString(data) {
149
+ this.ensureInitialized();
150
+ const result = globalThis.jwwToDxfString(data);
151
+ if (!result.ok) {
152
+ throw new Error(result.error || "Conversion failed");
153
+ }
154
+ return result.data;
155
+ }
156
+ };
157
+ async function createParser(wasmPath) {
158
+ const parser = new JwwParser(wasmPath);
159
+ await parser.init();
160
+ return parser;
161
+ }
162
+ var index_default = { JwwParser, createParser };
163
+ // Annotate the CommonJS export names for ESM import in node:
164
+ 0 && (module.exports = {
165
+ JwwParser,
166
+ createParser
167
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,130 @@
1
+ // src/index.ts
2
+ var JwwParser = class {
3
+ /**
4
+ * Create a new JWW parser instance
5
+ * @param wasmPath - Path to the jww-parser.wasm file
6
+ */
7
+ constructor(wasmPath) {
8
+ this.initialized = false;
9
+ this.initPromise = null;
10
+ this.wasmPath = wasmPath || this.getDefaultWasmPath();
11
+ }
12
+ getDefaultWasmPath() {
13
+ if (typeof process !== "undefined" && process.versions?.node) {
14
+ return new URL("../wasm/jww-parser.wasm", import.meta.url).pathname;
15
+ }
16
+ return "jww-parser.wasm";
17
+ }
18
+ /**
19
+ * Initialize the WASM module
20
+ * Must be called before using parse methods
21
+ */
22
+ async init() {
23
+ if (this.initialized) return;
24
+ if (this.initPromise) return this.initPromise;
25
+ this.initPromise = this.loadWasm();
26
+ await this.initPromise;
27
+ this.initialized = true;
28
+ }
29
+ async loadWasm() {
30
+ if (typeof Go === "undefined") {
31
+ await this.loadWasmExec();
32
+ }
33
+ const go = new Go();
34
+ let wasmInstance;
35
+ if (typeof process !== "undefined" && process.versions?.node) {
36
+ const fs = await import("fs");
37
+ const path = await import("path");
38
+ const wasmBuffer = fs.readFileSync(this.wasmPath);
39
+ const wasmModule = await WebAssembly.compile(wasmBuffer);
40
+ wasmInstance = await WebAssembly.instantiate(wasmModule, go.importObject);
41
+ } else {
42
+ const result = await WebAssembly.instantiateStreaming(
43
+ fetch(this.wasmPath),
44
+ go.importObject
45
+ ).catch(async () => {
46
+ const response = await fetch(this.wasmPath);
47
+ const bytes = await response.arrayBuffer();
48
+ return WebAssembly.instantiate(bytes, go.importObject);
49
+ });
50
+ wasmInstance = result.instance;
51
+ }
52
+ go.run(wasmInstance);
53
+ await this.waitForWasmFunctions();
54
+ }
55
+ async loadWasmExec() {
56
+ if (typeof process !== "undefined" && process.versions?.node) {
57
+ const wasmExecPath = new URL("../wasm/wasm_exec.js", import.meta.url).pathname;
58
+ await import(wasmExecPath);
59
+ } else {
60
+ throw new Error(
61
+ "Go runtime not loaded. Please include wasm_exec.js in your HTML."
62
+ );
63
+ }
64
+ }
65
+ async waitForWasmFunctions(timeout = 5e3, interval = 50) {
66
+ const start = Date.now();
67
+ while (Date.now() - start < timeout) {
68
+ if (typeof globalThis.jwwParse === "function" && typeof globalThis.jwwToDxf === "function" && typeof globalThis.jwwToDxfString === "function") {
69
+ return;
70
+ }
71
+ await new Promise((resolve) => setTimeout(resolve, interval));
72
+ }
73
+ throw new Error("WASM functions not available after timeout");
74
+ }
75
+ ensureInitialized() {
76
+ if (!this.initialized) {
77
+ throw new Error("Parser not initialized. Call init() first.");
78
+ }
79
+ }
80
+ /**
81
+ * Parse a JWW file and return the document structure
82
+ * @param data - JWW file content as Uint8Array
83
+ * @returns Parsed JWW document
84
+ */
85
+ parse(data) {
86
+ this.ensureInitialized();
87
+ const result = globalThis.jwwParse(data);
88
+ if (!result.ok) {
89
+ throw new Error(result.error || "Parse failed");
90
+ }
91
+ return JSON.parse(result.data);
92
+ }
93
+ /**
94
+ * Parse a JWW file and convert to DXF document structure
95
+ * @param data - JWW file content as Uint8Array
96
+ * @returns DXF document object
97
+ */
98
+ toDxf(data) {
99
+ this.ensureInitialized();
100
+ const result = globalThis.jwwToDxf(data);
101
+ if (!result.ok) {
102
+ throw new Error(result.error || "Conversion failed");
103
+ }
104
+ return JSON.parse(result.data);
105
+ }
106
+ /**
107
+ * Parse a JWW file and convert to DXF file content string
108
+ * @param data - JWW file content as Uint8Array
109
+ * @returns DXF file content as string
110
+ */
111
+ toDxfString(data) {
112
+ this.ensureInitialized();
113
+ const result = globalThis.jwwToDxfString(data);
114
+ if (!result.ok) {
115
+ throw new Error(result.error || "Conversion failed");
116
+ }
117
+ return result.data;
118
+ }
119
+ };
120
+ async function createParser(wasmPath) {
121
+ const parser = new JwwParser(wasmPath);
122
+ await parser.init();
123
+ return parser;
124
+ }
125
+ var index_default = { JwwParser, createParser };
126
+ export {
127
+ JwwParser,
128
+ createParser,
129
+ index_default as default
130
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "jww-parser",
3
+ "version": "1.0.0",
4
+ "description": "JWW (Jw_cad) file parser and DXF converter for JavaScript/TypeScript",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "wasm"
18
+ ],
19
+ "keywords": [
20
+ "jww",
21
+ "jw-cad",
22
+ "jwcad",
23
+ "dxf",
24
+ "cad",
25
+ "converter",
26
+ "parser",
27
+ "webassembly",
28
+ "wasm"
29
+ ],
30
+ "author": "f12o",
31
+ "license": "AGPL-3.0",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/f4ah6o/jww-dxf.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/f4ah6o/jww-dxf/issues"
38
+ },
39
+ "homepage": "https://github.com/f4ah6o/jww-dxf#readme",
40
+ "devDependencies": {
41
+ "@types/node": "^20.0.0",
42
+ "tsup": "^8.0.0",
43
+ "typescript": "^5.3.0"
44
+ },
45
+ "engines": {
46
+ "node": ">=18.0.0"
47
+ },
48
+ "scripts": {
49
+ "build": "npm run build:wasm && npm run build:js",
50
+ "build:wasm": "mkdir -p wasm && cd .. && make build-wasm && cp dist/jww-parser.wasm npm/wasm/",
51
+ "build:js": "tsup src/index.ts --format cjs,esm --dts"
52
+ }
53
+ }
Binary file
Binary file