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Q,EAAU,QAGvC,IAAI,EAAY,GAChB,EAAQ,GAAG,UAAW,IACpB,IAAM,GAAa,OAAO,EAAO,CAAE,MAAO,GAAI,KAGhD,EAAQ,GAAG,QAAS,IAElB,KAAK,SAAW,EAChB,KAAK,KAAO,EACZ,IACM,OAAO,SAAS,GAClB,EAAQ,GAER,EAAQ,OAAO,KAAK,OAAO,GAAY,UAE3C,CAAE,MAAO,GACP,EAAO,IAAI,MAAM,2CAA2C,EAAI,WAClE,KAGF,EAAQ,GAAG,SAAU,IAEnB,KAAK,SAAW,EAChB,KAAK,KAAO,EACZ,EAAO,IAAI,MAAM,mBAAmB,EAAM,oBAAoB,KAAa,GAC3E,GAEN","file":"/npm/@bascanada/openscad-compiler@0.0.5/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@bascanada/openscad-compiler@0.0.5/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { compileWasm, getWasmVersion } from './compilers/wasm.js';\nimport { compileNative, getNativeVersion } from './compilers/native.js';\nimport { extractDateVersion } from './utils.js';\nimport stl from 'node-stl';\n\n/**\n * @typedef {'wasm' | 'native' | 'webworker'} EngineType\n */\n\n\n/**\n * Main compiler class that provides a unified interface.\n */\nexport class Compiler {\n  /**\n   * @param {object} options\n   * @param {EngineType} options.engine - The compiler engine to use.\n   * @param {string} [options.nativePath='openscad'] - Path to the native executable.\n   * @param {string} [options.fileType='stl'] - Output file type (e.g., 'stl', 'amf').\n   * @param {object} [options.args] - Optional: { fast: [...], full: [...] } extra args for each mode.\n   */\n  constructor({ engine = 'wasm', nativePath = 'openscad', fileType = 'stl', args = {\n    fast: undefined,\n    full: undefined\n  } } = {}) {\n    if (engine !== 'wasm' && engine !== 'native' && engine !== 'webworker') {\n      throw new Error(\"Engine must be 'wasm' or 'native' or 'webworker'\");\n    }\n    this.engine = engine;\n    this.nativePath = nativePath;\n    this.fileType = fileType;\n    this.args = args;\n\n    if (!this.args.fast) {\n      this.args.fast = this.getDefaultFastArgs();\n    } else if (!this.args.full) {\n      this.args.full = this.getDefaultArgs();\n    }\n\n  }\n\n  /**\n   * Compiles OpenSCAD code to a string (STL or other file type).\n   * @param {string} scadCode - The OpenSCAD code to compile.\n   * @param {string} [mode='fast'] - Compilation mode ('fast', 'full', etc.).\n   * @returns {EventEmitter} Emits output events.\n   */\n  compile(scadCode, mode = 'fast') {\n    const fileType = this.fileType || 'stl';\n    const args = (this.args && this.args[mode]) ? this.args[mode] : [];\n    if (this.engine === 'native') {\n      return compileNative(scadCode, this.nativePath, fileType, args);\n    } else if (this.engine === 'wasm') {\n      return compileWasm(scadCode, fileType, args);\n    } else if (this.engine === 'webworker') {\n      throw new Error(\"WebWorker engine is not supporte yet\");\n    } else {\n      throw new Error(`Unknown engine type: ${this.engine}`);\n    }\n  }\n\n  /**\n   * Gets the date-style version of the underlying OpenSCAD engine.\n   * @returns {Promise<string|null>} Resolves with the date-style version string, or null if not found.\n   */\n  async getVersion() {\n    let versionString;\n    if (this.engine === 'native') {\n      versionString = await getNativeVersion(this.nativePath);\n    } else if (this.engine === 'wasm') {\n      versionString = await getWasmVersion();\n    } else if (this.engine === 'webworker') {\n      throw new Error('WebWorker engine does not support version query yet');\n    } else {\n      throw new Error(`Unknown engine type: ${this.engine}`);\n    }\n    return extractDateVersion(versionString);\n  }\n\n\n  getDefaultFastArgs() {\n    return [ \"--enable=lazy-union\", \"--backend=manifold\" ];\n  }\n\n  getDefaultArgs() {\n    return [];\n  }\n\n  /**\n   * Generates the OpenSCAD scene graph (CSG) as raw file output (no JSON conversion).\n   * @param {string} scadCode - The OpenSCAD code to compile.\n   * @returns {Promise<Buffer>} A promise that resolves with the raw CSG file Buffer.\n   */\n  getSceneGraph(scadCode) {\n    return new Promise((resolve, reject) => {\n      const originalFileType = this.fileType;\n      this.fileType = 'csg';\n\n      // Always use 'full' mode for an accurate scene graph\n      const emitter = this.compile(scadCode, 'full');\n\n      emitter.on('done', (csgOutput) => {\n        this.fileType = originalFileType; // Restore fileType\n        try {\n          // Return raw Buffer for the CSG file. Convert string outputs to Buffer.\n          const buf = Buffer.isBuffer(csgOutput) ? csgOutput : Buffer.from(String(csgOutput), 'utf8');\n          resolve(buf);\n        } catch (error) {\n          reject(new Error(`Failed to return CSG output: ${error.message}`));\n        }\n      });\n\n      emitter.on('error', (error) => {\n        this.fileType = originalFileType; // Restore on error\n        reject(error);\n      });\n    });\n  }\n\n  /**\n   * Compiles the model to STL and returns its physical dimensions and properties.\n   * @param {string} scadCode - The OpenSCAD code to compile.\n   * @returns {Promise<object>} A promise resolving with the dimension information.\n   */\n  getDimensions(scadCode) {\n    return new Promise((resolve, reject) => {\n      const originalFileType = this.fileType;\n      this.fileType = 'stl';\n\n      const emitter = this.compile(scadCode, 'full');\n\n      emitter.on('done', (stlOutput) => {\n        this.fileType = originalFileType; // Restore\n        try {\n          // Handle Buffer from native engine or string from other engines\n          const stlBuffer = Buffer.isBuffer(stlOutput) ? stlOutput : Buffer.from(String(stlOutput), 'utf8');\n          // node-stl exports a class; construct it with new\n          const modelInfo = new stl(stlBuffer);\n\n          const dimensions = {\n            volume_cm3: modelInfo.volume,\n            surfaceArea_cm2: modelInfo.area,\n            boundingBox_mm: modelInfo.boundingBox,\n            centerOfMass_mm: modelInfo.centerOfMass,\n          };\n          resolve(dimensions);\n        } catch (error) {\n          reject(new Error(`Failed to analyze STL data: ${error.message}`));\n        }\n      });\n\n      emitter.on('error', (error) => {\n        this.fileType = originalFileType; // Restore\n        reject(error);\n      });\n    });\n  }\n\n  /**\n   * Generates a PNG preview of the model using OpenSCAD's autocenter/viewall flags.\n   * @param {string} scadCode - The OpenSCAD code.\n   * @param {object} [options] - Preview options.\n   * @param {string} [options.camera] - Optional camera string to override auto view.\n   * @param {string} [options.imgsize='1024,768'] - Image dimensions.\n   * @returns {Promise<Buffer>} Resolves with the PNG image buffer.\n   */\n  getPreview(scadCode, options = {}) {\n    return new Promise((resolve, reject) => {\n      const originalFileType = this.fileType;\n      const originalArgs = JSON.parse(JSON.stringify(this.args));\n      this.fileType = 'png';\n\n      const previewArgs = [\n        '--autocenter', // Center the model\n        '--viewall',    // Zoom to fit the model in the view\n        `--imgsize=${options.imgsize || '1024,768'}`,\n      ];\n\n      // Allow optional camera override\n      if (options.camera) {\n        previewArgs.push(`--camera=${options.camera}`);\n      }\n\n      // Merge preview args into the 'full' arg set for the call, preserving originals\n      this.args.full = (originalArgs.full || []).concat(previewArgs);\n\n      const emitter = this.compile(scadCode, 'full');\n\n      // collect stderr for debugging\n      let stderrBuf = '';\n      emitter.on('stderr', (data) => {\n        try { stderrBuf += String(data); } catch (e) {}\n      });\n\n      emitter.on('done', (pngOutput) => {\n        // Restore state\n        this.fileType = originalFileType;\n        this.args = originalArgs;\n        try {\n          if (Buffer.isBuffer(pngOutput)) {\n            resolve(pngOutput);\n          } else {\n            resolve(Buffer.from(String(pngOutput), 'binary'));\n          }\n        } catch (err) {\n          reject(new Error(`Failed to convert PNG output to Buffer: ${err.message}`));\n        }\n      });\n\n      emitter.on('error', (error) => {\n        // Restore state\n        this.fileType = originalFileType;\n        this.args = originalArgs;\n        reject(new Error(`OpenSCAD error: ${error.message}. Stderr: ${stderrBuf}`));\n      });\n    });\n  }\n}"]}