UNPKG

asn1tools-js

Version:

ASN.1 encoding and decoding library for TypeScript/JavaScript, compatible with Python asn1tools

148 lines 5.29 kB
"use strict"; /** * ASN.1 Tools for TypeScript/JavaScript * Main entry point providing the public API */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Specification = void 0; exports.compileFiles = compileFiles; exports.compileString = compileString; exports.hexToBytes = hexToBytes; exports.bytesToHex = bytesToHex; const fs = __importStar(require("fs")); const parser_1 = require("./parser"); const compiler_1 = require("./compiler"); const types_1 = require("./types"); __exportStar(require("./types"), exports); var compiler_2 = require("./compiler"); Object.defineProperty(exports, "Specification", { enumerable: true, get: function () { return compiler_2.Specification; } }); /** * Compile ASN.1 files into a specification object * * @param filenames Array of ASN.1 file paths to compile * @param options Compilation options * @returns Compiled specification object * * @example * ```typescript * import { compileFiles } from 'asn1tools-js'; * * const spec = compileFiles(['messages.asn']); * const encoded = spec.encode('MessageRequest', data); * const decoded = spec.decode('MessageResponse', encoded); * ``` */ function compileFiles(filenames, options = {}) { // Suppress unused parameter warning for now - options will be used in future versions void options; if (filenames.length === 0) { throw new types_1.CompileError('No files provided'); } const contents = []; for (const filename of filenames) { if (!fs.existsSync(filename)) { throw new types_1.CompileError(`File not found: ${filename}`); } try { const content = fs.readFileSync(filename, 'utf-8'); contents.push(content); } catch (error) { throw new types_1.CompileError(`Failed to read file ${filename}: ${error instanceof Error ? error.message : String(error)}`); } } return compileString(contents.join('\n\n')); } /** * Compile ASN.1 string content into a specification object * * @param content ASN.1 content as string * @param options Compilation options * @returns Compiled specification object */ function compileString(content, options = {}) { // Suppress unused parameter warning for now - options will be used in future versions void options; try { const parser = new parser_1.Asn1Parser(); const parsedModules = parser.parse(content); const compiler = new compiler_1.Asn1Compiler(); const specification = compiler.compile(parsedModules); return new compiler_1.Specification(specification); } catch (error) { if (error instanceof types_1.CompileError) { throw error; } throw new types_1.CompileError(`Compilation failed: ${error instanceof Error ? error.message : String(error)}`); } } /** * Utility function to convert hex string to Uint8Array */ function hexToBytes(hex) { const cleanHex = hex.replace(/[^0-9a-fA-F]/g, ''); if (cleanHex.length % 2 !== 0) { throw new Error('Hex string must have even length'); } const bytes = new Uint8Array(cleanHex.length / 2); for (let i = 0; i < cleanHex.length; i += 2) { bytes[i / 2] = parseInt(cleanHex.substr(i, 2), 16); } return bytes; } /** * Utility function to convert Uint8Array to hex string */ function bytesToHex(bytes) { return Array.from(bytes) .map(byte => byte.toString(16).padStart(2, '0')) .join(''); } /** * Default export providing same interface as Python asn1tools */ exports.default = { compileFiles, compileString, hexToBytes, bytesToHex, Specification: compiler_1.Specification }; //# sourceMappingURL=index.js.map