All files common.ts

97.87% Statements 46/47
88.23% Branches 15/17
94.11% Functions 16/17
97.82% Lines 45/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                        4x                         76x 76x   76x 76x 4x   76x       1x   1x 1x 1x   1x       39x       39x       1x       37x       1x       9x 8x 8x   3x 3x     1x       108x 1x         23x       3x       19x 19x       48x 48x 48x 48x 48x       48x 48x 8x   48x         53x 212x   53x 53x 53x 53x   53x    
// SPDX-FileCopyrightText: 2025 Anaconda, Inc
// SPDX-License-Identifier: Apache-2.0
 
import * as fs from 'fs';
import { Configuration, InternalConfiguration, toImpl as toImplCfg } from './config.js';
import { ResourceAttributes, InternalResourceAttributes, toImpl as toImplAttrs } from './attributes.js';
 
// value import (namespace gives us both ESM & CJS shapes)
import * as resourcesNS from '@opentelemetry/resources';
 
// ---- v2 export OR v1 fallback to `new Resource(attrs)` ----
const resourceFromAttributes =
  (resourcesNS as any).resourceFromAttributes ??
  ((attrs: Record<string, unknown>) => new (resourcesNS as any).Resource(attrs));
 
// type import + alias (so you can keep using `Resource` in type positions)
import type { Resource as _Resource } from '@opentelemetry/resources';
type Resource = _Resource;
 
export class AnacondaCommon {
    config: InternalConfiguration
    attributes: InternalResourceAttributes
    resources: Resource
 
    protected constructor(config: Configuration, attributes: ResourceAttributes) {
        this.config = toImplCfg(config)
        this.attributes = toImplAttrs(attributes)
 
        let resourceObject = this.attributes.getAttributes()
        if (this.config.getEntropy() !== '') {
            resourceObject['session.id'] = this.config.getEntropy()
        }
        this.resources = resourceFromAttributes(resourceObject)
    }
 
    protected makeNewResource(newAttributes: ResourceAttributes): void {
        this.attributes = toImplAttrs(newAttributes)
 
        let resourceObject = this.attributes.getAttributes()
        Eif (this.config.getEntropy() !== '') {
            resourceObject['session.id'] = this.config.getEntropy()
        }
        this.resources = resourceFromAttributes(resourceObject)
    }
 
    protected get serviceName(): string {
        return this.attributes.getServiceName()
    }
 
    protected get serviceVersion(): string {
        return this.attributes.getServiceVersion()
    }
 
    protected get useConsole(): boolean {
        return this.config.getUseConsole()
    }
 
    protected get metricsExportIntervalMs(): number {
        return this.config.getMetricsExportIntervalMs()
    }
 
    protected get skipInternetCheck(): boolean {
        return this.config.getSkipInternetCheck()
    }
 
    protected readCertFile(certFile: string): string | undefined {
        if (certFile && certFile.length > 0) {
            try {
                return fs.readFileSync(certFile, 'utf8')
            } catch (error) {
                this.error(`Failed to read certificate file: ${certFile}: ${error}`)
                return undefined
            }
        }
        return undefined
    }
 
    protected debug(line: string) {
        if (this.config.getUseDebug()) {
            console.debug(`${localTimeString()} > *** ATEL DEBUG: ${line}`)
        }
    }
 
    protected warn(line: string) {
        console.warn(`${localTimeString()} > *** ATEL ERROR: ${line}`)
    }
 
    protected error(line: string) {
        console.error(`${localTimeString()} > *** ATEL WARNING: ${line}`)
    }
 
    protected isValidName(name: string): boolean {
        const regex = /^[A-Za-z][A-Za-z_0-9]+$/;
        return regex.test(name);
    }
 
    protected transformURL(url: URL): [string, URL] {
        const scheme = url.protocol
        const ep = new URL(url.href)
        ep.protocol = ep.protocol.replace("grpcs:", "https:")
        ep.protocol = ep.protocol.replace("grpc:", "http:")
        return [scheme, ep]
    }
 
    protected makeHeaders(scheme: string, authToken: string | undefined): Record<string,string> {
        var headers: Record<string,string> = authToken ? { 'Authorization': `Bearer ${authToken}` } : {}
        if (scheme.startsWith('http')) {
            headers['Content-Type'] = 'application/x-protobuf'
        }
        return headers
    }
}
 
export function localTimeString(): string {
    const d = new Date();
    const pad = (n: number, width = 2) => String(n).padStart(width, "0");
 
    const hh = pad(d.getHours());
    const mm = pad(d.getMinutes());
    const ss = pad(d.getSeconds());
    const ms = pad(d.getMilliseconds(), 3);
 
    return `${hh}:${mm}:${ss}.${ms}`;
}