node build fixed

This commit is contained in:
ra_ma
2025-09-20 14:08:38 +01:00
parent c6ebbe069d
commit 3d298fa434
1516 changed files with 535727 additions and 2 deletions

View File

@@ -0,0 +1,73 @@
package discordrpc_ipc
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"os"
)
// GetIpcPath chooses the correct directory to the ipc socket and returns it
func GetIpcPath() string {
vn := []string{"XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP"}
for _, name := range vn {
path, exists := os.LookupEnv(name)
if exists {
return path
}
}
return "/tmp"
}
// Socket extends net.Conn methods
type Socket struct {
net.Conn
}
// Read the socket response
func (socket *Socket) Read() (string, error) {
buf := make([]byte, 512)
payloadLength, err := socket.Conn.Read(buf)
if err != nil {
return "", err
}
buffer := new(bytes.Buffer)
for i := 8; i < payloadLength; i++ {
buffer.WriteByte(buf[i])
}
r := buffer.String()
if r == "" {
return "", fmt.Errorf("empty response")
}
return r, nil
}
// Send opcode and payload to the unix socket
func (socket *Socket) Send(opcode int, payload string) (string, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, int32(opcode))
if err != nil {
return "", err
}
err = binary.Write(buf, binary.LittleEndian, int32(len(payload)))
if err != nil {
return "", err
}
buf.Write([]byte(payload))
_, err = socket.Write(buf.Bytes())
if err != nil {
return "", err
}
return socket.Read()
}

View File

@@ -0,0 +1,19 @@
//go:build !windows
// +build !windows
package discordrpc_ipc
import (
"net"
"time"
)
// NewConnection opens the discord-ipc-0 unix socket
func NewConnection() (*Socket, error) {
sock, err := net.DialTimeout("unix", GetIpcPath()+"/discord-ipc-0", time.Second*2)
if err != nil {
return nil, err
}
return &Socket{sock}, nil
}

View File

@@ -0,0 +1,24 @@
//go:build windows
// +build windows
package discordrpc_ipc
import (
"time"
"github.com/Microsoft/go-winio"
)
// NewConnection opens the discord-ipc-0 named pipe
func NewConnection() (*Socket, error) {
// Connect to the Windows named pipe, this is a well known name
// We use DialTimeout since it will block forever (or very, very long) on Windows
// if the pipe is not available (Discord not running)
t := 2 * time.Second
sock, err := winio.DialPipe(`\\.\pipe\discord-ipc-0`, &t)
if err != nil {
return nil, err
}
return &Socket{sock}, nil
}