UNPKG

vlayer-web-proof

Version:

It is a npm package for vlayer web proof mechanism. It uses native rust bindings for maximum efficiency.

106 lines (105 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseUrl = parseUrl; exports.parseNotaryUrl = parseNotaryUrl; exports.formatHeaders = formatHeaders; exports.buildWebProofRequest = buildWebProofRequest; exports.createPerformanceMetrics = createPerformanceMetrics; exports.validateWebProofOptions = validateWebProofOptions; exports.isValidUrl = isValidUrl; function parseUrl(url) { if (!url || typeof url !== 'string') { throw new Error('URL must be a non-empty string'); } try { const urlObj = new URL(url); const port = urlObj.port ? parseInt(urlObj.port, 10) : (urlObj.protocol === 'https:' ? 443 : 80); if (isNaN(port) || port < 1 || port > 65535) { throw new Error(`Invalid port number: ${urlObj.port}`); } return { domain: urlObj.hostname, uri: urlObj.pathname + urlObj.search + urlObj.hash, protocol: urlObj.protocol, port }; } catch (error) { throw new Error(`Invalid URL format: ${url}. ${error instanceof Error ? error.message : ''}`); } } function parseNotaryUrl(notaryUrl) { if (!notaryUrl || typeof notaryUrl !== 'string') { throw new Error('Notary URL must be a non-empty string'); } try { const urlObj = new URL(notaryUrl); const host = urlObj.hostname; const port = urlObj.port ? parseInt(urlObj.port, 10) : (urlObj.protocol === 'https:' ? 443 : 80); const pathPrefix = urlObj.pathname.replace(/^\/+|\/+$/g, ''); const enableTls = urlObj.protocol === 'https:'; if (isNaN(port) || port < 1 || port > 65535) { throw new Error(`Invalid notary port number: ${urlObj.port}`); } if (!host) { throw new Error('Notary URL must contain a valid hostname'); } return { host, port, pathPrefix, enableTls }; } catch (error) { throw new Error(`Invalid notary URL format: ${notaryUrl}. ${error instanceof Error ? error.message : ''}`); } } function formatHeaders(headers) { if (!headers) return []; return headers.filter(header => typeof header === 'string' && header.trim().length > 0); } function buildWebProofRequest(url, options = {}) { var _a, _b, _c, _d, _e, _f; const headersList = formatHeaders(options.headers); return { url, host: (_a = options.host) !== null && _a !== void 0 ? _a : undefined, notary_url: (_b = options.notary_url) !== null && _b !== void 0 ? _b : undefined, method: (_c = options.method) !== null && _c !== void 0 ? _c : undefined, headers: headersList, data: (_d = options.data) !== null && _d !== void 0 ? _d : undefined, max_sent_data: (_e = options.max_sent_data) !== null && _e !== void 0 ? _e : undefined, max_recv_data: (_f = options.max_recv_data) !== null && _f !== void 0 ? _f : undefined }; } function createPerformanceMetrics(startTime, endTime) { var _a, _b; const end = endTime !== null && endTime !== void 0 ? endTime : performance.now(); return { startTime, endTime: end, duration: end - startTime, memoryUsage: (_b = (_a = process.memoryUsage) === null || _a === void 0 ? void 0 : _a.call(process)) === null || _b === void 0 ? void 0 : _b.heapUsed }; } function validateWebProofOptions(options) { if (options.max_sent_data !== undefined) { if (!Number.isInteger(options.max_sent_data) || options.max_sent_data <= 0) { throw new Error('max_sent_data must be a positive integer'); } } if (options.max_recv_data !== undefined) { if (!Number.isInteger(options.max_recv_data) || options.max_recv_data <= 0) { throw new Error('max_recv_data must be a positive integer'); } } if (options.method && !['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes(options.method)) { throw new Error(`Invalid HTTP method: ${options.method}`); } } function isValidUrl(url) { try { new URL(url); return true; } catch (_a) { return false; } }