File size: 2,097 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package idutil

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"time"
)

// Manager generates stable hash-based IDs for profiles, instances, and tabs
type Manager struct{}

// NewManager creates a new ID manager
func NewManager() *Manager {
	return &Manager{}
}

// ProfileID generates a stable hash-based ID for a profile from its name
// Format: prof_XXXXXXXX (12 chars total)
func (m *Manager) ProfileID(name string) string {
	return hashID("prof", name)
}

// InstanceID generates a stable hash-based ID for an instance
// Uses profile ID, instance name, and creation timestamp for uniqueness
// Format: inst_XXXXXXXX (12 chars total)
func (m *Manager) InstanceID(profileID, instanceName string) string {
	data := fmt.Sprintf("%s:%s:%d", profileID, instanceName, time.Now().UnixNano())
	return hashID("inst", data)
}

// TabID generates a stable hash-based ID for a tab within an instance
// Uses instance ID and tab number for uniqueness
// Format: tab_XXXXXXXX (12 chars total)
func (m *Manager) TabID(instanceID string, tabIndex int) string {
	data := fmt.Sprintf("%s:%d", instanceID, tabIndex)
	return hashID("tab", data)
}

// TabIDFromCDPTarget returns the CDP target ID as-is.
// Raw CDP IDs are used directly — no prefixing or hashing.
func (m *Manager) TabIDFromCDPTarget(cdpTargetID string) string {
	return cdpTargetID
}

// hashID creates a short hash-based ID with the given prefix
// Format: {prefix}_{first 8 hex chars of SHA256}
func hashID(prefix, data string) string {
	hash := sha256.Sum256([]byte(data))
	hexHash := hex.EncodeToString(hash[:])
	// Take first 8 characters of hash for readability (still extremely collision-resistant)
	return fmt.Sprintf("%s_%s", prefix, hexHash[:8])
}

// IsValidID checks if an ID matches the expected prefix format
func IsValidID(id, prefix string) bool {
	if len(id) < len(prefix)+1 {
		return false
	}
	return id[:len(prefix)] == prefix && id[len(prefix)] == '_'
}

// ExtractPrefix extracts the prefix from an ID
func ExtractPrefix(id string) string {
	for i, c := range id {
		if c == '_' {
			return id[:i]
		}
	}
	return ""
}