UNPKG

@qbraid-core/base

Version:

Core functionality for interacting with qBraid Cloud Services.

101 lines 3.78 kB
"use strict"; // Copyright (c) 2025, qBraid Development Team // All rights reserved. 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_HUB_URL = exports.DEFAULT_ACCOUNT_URL = exports.DEFAULT_QBOOK_URL = exports.DEFAULT_LAB_URL = exports.DEFAULT_API_URL = void 0; exports.using = using; exports.ensureDirectory = ensureDirectory; const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Class to manage directory creation and cleanup */ class DirectoryManager { existed; path; removeIfCreated; constructor(dirPath, removeIfCreated) { this.path = dirPath; this.removeIfCreated = removeIfCreated; this.existed = fs.existsSync(dirPath); } async init() { if (!this.existed) { await fs.promises.mkdir(this.path, { recursive: true }); } } async dispose() { if (!this.existed && this.removeIfCreated) { const files = await fs.promises.readdir(this.path); for (const file of files) { await fs.promises.unlink(path.join(this.path, file)); } await fs.promises.rmdir(this.path); } } } /** * Utility function to handle resource management similar to Python's context manager * @param resource A disposable resource with init and dispose methods * @param fn The function to execute with the managed resource */ async function using(resource, fn) { await resource.init(); try { return await fn(resource); } finally { await resource.dispose(); } } /** * Ensures a directory exists during the execution of a function and optionally cleans it up * @param dirPath Path to the directory * @param removeIfCreated Whether to remove the directory if it was created by this function * @param fn Function to execute while directory is guaranteed to exist */ async function ensureDirectory(dirPath, removeIfCreated, fn) { return using(new DirectoryManager(dirPath, removeIfCreated), async () => await fn()); } /** * Default URLs for qBraid services */ exports.DEFAULT_API_URL = 'https://api.qbraid.com'; exports.DEFAULT_LAB_URL = 'https://lab.qbraid.com'; exports.DEFAULT_QBOOK_URL = 'https://qbook.qbraid.com'; exports.DEFAULT_ACCOUNT_URL = 'https://account.qbraid.com/'; exports.DEFAULT_HUB_URL = 'https://lab.qbraid.com'; //# sourceMappingURL=context.js.map