UNPKG

recoder-code

Version:

🚀 AI-powered development platform - Chat with 32+ models, build projects, automate workflows. Free models included!

266 lines • 10.6 kB
"use strict"; /** * Download Entity * Tracks package downloads for analytics and statistics */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var Download_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.Download = void 0; const typeorm_1 = require("typeorm"); const Package_1 = require("./Package"); const PackageVersion_1 = require("./PackageVersion"); const User_1 = require("./User"); let Download = Download_1 = class Download { // Methods static fromRequest(req, packageInfo) { const userAgent = req.headers['user-agent'] || ''; return { ip_address: req.ip || req.connection?.remoteAddress || 'unknown', user_agent: userAgent, referer: req.headers['referer'] || req.headers['referrer'] || undefined, download_method: Download_1.detectDownloadMethod(userAgent) || undefined, client_version: Download_1.extractClientVersion(userAgent) || undefined, node_version: Download_1.extractNodeVersion(userAgent) || undefined, os: Download_1.extractOS(userAgent) || undefined, arch: Download_1.extractArch(userAgent) || undefined, is_ci: Download_1.isCI(userAgent, req.headers), ci_name: Download_1.extractCIName(userAgent, req.headers) || undefined, is_bot: Download_1.isBot(userAgent), package_id: packageInfo.package_id, version_id: packageInfo.version_id, user_id: packageInfo.user_id, date_only: new Date().toISOString().split('T')[0] }; } static detectDownloadMethod(userAgent) { if (userAgent.includes('npm/')) return 'npm'; if (userAgent.includes('yarn/')) return 'yarn'; if (userAgent.includes('pnpm/')) return 'pnpm'; if (userAgent.includes('curl/')) return 'curl'; if (userAgent.includes('wget/')) return 'wget'; return 'unknown'; } static extractClientVersion(userAgent) { const patterns = [ /npm\/(\d+\.\d+\.\d+)/, /yarn\/(\d+\.\d+\.\d+)/, /pnpm\/(\d+\.\d+\.\d+)/ ]; for (const pattern of patterns) { const match = userAgent.match(pattern); if (match) return match[1]; } return undefined; } static extractNodeVersion(userAgent) { const match = userAgent.match(/node\/v?(\d+\.\d+\.\d+)/); return match ? match[1] : undefined; } static extractOS(userAgent) { if (userAgent.includes('win32')) return 'Windows'; if (userAgent.includes('darwin')) return 'macOS'; if (userAgent.includes('linux')) return 'Linux'; if (userAgent.includes('freebsd')) return 'FreeBSD'; return undefined; } static extractArch(userAgent) { if (userAgent.includes('x64')) return 'x64'; if (userAgent.includes('ia32')) return 'ia32'; if (userAgent.includes('arm64')) return 'arm64'; if (userAgent.includes('arm')) return 'arm'; return undefined; } static isCI(userAgent, headers) { const ciIndicators = [ 'ci=true', 'CONTINUOUS_INTEGRATION', 'BUILD_ID', 'CI_NAME', headers['x-forwarded-for']?.includes('github.com'), headers['x-forwarded-for']?.includes('travis-ci.org'), headers['x-forwarded-for']?.includes('circleci.com') ]; return ciIndicators.some(indicator => userAgent.includes(String(indicator)) || Object.values(headers).some(value => String(value).includes(String(indicator)))); } static extractCIName(userAgent, headers) { if (userAgent.includes('Travis-CI') || headers['x-travis']) return 'Travis CI'; if (userAgent.includes('CircleCI') || headers['x-circleci']) return 'CircleCI'; if (userAgent.includes('GitHub Actions') || headers['x-github-actions']) return 'GitHub Actions'; if (userAgent.includes('Jenkins') || headers['x-jenkins']) return 'Jenkins'; if (userAgent.includes('GitLab CI') || headers['x-gitlab-ci']) return 'GitLab CI'; if (userAgent.includes('Azure DevOps') || headers['x-azure-devops']) return 'Azure DevOps'; return undefined; } static isBot(userAgent) { const botPatterns = [ /bot/i, /crawler/i, /spider/i, /scraper/i, /scanner/i, /monitor/i, /uptime/i, /pingdom/i, /newrelic/i, /lighthouse/i ]; return botPatterns.some(pattern => pattern.test(userAgent)); } toAnalyticsFormat() { return { id: this.id, package_id: this.package_id, version_id: this.version_id, user_id: this.user_id, date: this.date, download_method: this.download_method, client_version: this.client_version, node_version: this.node_version, os: this.os, arch: this.arch, country: this.country, city: this.city, is_ci: this.is_ci, ci_name: this.ci_name, is_bot: this.is_bot, install_reason: this.install_reason }; } }; __decorate([ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'), __metadata("design:type", String) ], Download.prototype, "id", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 45 }), __metadata("design:type", String) ], Download.prototype, "ip_address", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'text', nullable: true }), __metadata("design:type", String) ], Download.prototype, "user_agent", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 10, nullable: true }), __metadata("design:type", String) ], Download.prototype, "country", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], Download.prototype, "city", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], Download.prototype, "referer", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 50, nullable: true }), __metadata("design:type", String) ], Download.prototype, "download_method", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 50, nullable: true }), __metadata("design:type", String) ], Download.prototype, "client_version", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 50, nullable: true }), __metadata("design:type", String) ], Download.prototype, "node_version", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], Download.prototype, "os", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], Download.prototype, "arch", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'boolean', default: false }), __metadata("design:type", Boolean) ], Download.prototype, "is_ci", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], Download.prototype, "ci_name", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'boolean', default: false }), __metadata("design:type", Boolean) ], Download.prototype, "is_bot", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }), __metadata("design:type", String) ], Download.prototype, "project_name", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'varchar', length: 100, nullable: true }), __metadata("design:type", String) ], Download.prototype, "install_reason", void 0); __decorate([ (0, typeorm_1.CreateDateColumn)(), __metadata("design:type", Date) ], Download.prototype, "date", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'date' }), (0, typeorm_1.Index)(), __metadata("design:type", String) ], Download.prototype, "date_only", void 0); __decorate([ (0, typeorm_1.ManyToOne)(() => Package_1.Package, pkg => pkg.downloads, { onDelete: 'CASCADE' }), __metadata("design:type", Package_1.Package) ], Download.prototype, "package", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'uuid' }), __metadata("design:type", String) ], Download.prototype, "package_id", void 0); __decorate([ (0, typeorm_1.ManyToOne)(() => PackageVersion_1.PackageVersion, { onDelete: 'CASCADE' }), __metadata("design:type", PackageVersion_1.PackageVersion) ], Download.prototype, "version", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'uuid' }), __metadata("design:type", String) ], Download.prototype, "version_id", void 0); __decorate([ (0, typeorm_1.ManyToOne)(() => User_1.User, user => user.downloads, { nullable: true }), __metadata("design:type", User_1.User) ], Download.prototype, "user", void 0); __decorate([ (0, typeorm_1.Column)({ type: 'uuid', nullable: true }), __metadata("design:type", String) ], Download.prototype, "user_id", void 0); Download = Download_1 = __decorate([ (0, typeorm_1.Entity)('downloads'), (0, typeorm_1.Index)(['package_id', 'date']), (0, typeorm_1.Index)(['version_id', 'date']), (0, typeorm_1.Index)(['user_id', 'date']), (0, typeorm_1.Index)(['date']), (0, typeorm_1.Index)(['ip_address']) ], Download); exports.Download = Download; //# sourceMappingURL=Download.js.map