-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
167 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//go:build js | ||
|
||
package nostr | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
ws "github.com/coder/websocket" | ||
) | ||
|
||
type Connection struct { | ||
conn *ws.Conn | ||
} | ||
|
||
func NewConnection(ctx context.Context, url string, requestHeader http.Header, tlsConfig *tls.Config) (*Connection, error) { | ||
c, _, err := ws.Dial(ctx, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Connection{ | ||
conn: c, | ||
}, nil | ||
} | ||
|
||
func (c *Connection) WriteMessage(ctx context.Context, data []byte) error { | ||
if err := c.conn.Write(ctx, ws.MessageBinary, data); err != nil { | ||
return fmt.Errorf("failed to write message: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Connection) ReadMessage(ctx context.Context, buf io.Writer) error { | ||
_, reader, err := c.conn.Reader(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to get reader: %w", err) | ||
} | ||
if _, err := io.Copy(buf, reader); err != nil { | ||
return fmt.Errorf("failed to read message: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Connection) Close() error { | ||
return c.conn.Close(ws.StatusNormalClosure, "") | ||
} | ||
|
||
func (c *Connection) Ping(ctx context.Context) error { | ||
return c.conn.Ping(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !js | ||
|
||
package nip96 | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//go:build js | ||
|
||
package nostr | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConnectContext(t *testing.T) { | ||
url := os.Getenv("TEST_RELAY_URL") | ||
if url == "" { | ||
t.Fatal("please set the environment: $TEST_RELAY_URL") | ||
} | ||
|
||
// relay client | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
r, err := RelayConnect(ctx, url) | ||
assert.NoError(t, err) | ||
|
||
defer r.Close() | ||
} | ||
|
||
func TestConnectContextCanceled(t *testing.T) { | ||
url := os.Getenv("TEST_RELAY_URL") | ||
if url == "" { | ||
t.Fatal("please set the environment: $TEST_RELAY_URL") | ||
} | ||
|
||
// relay client | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() // make ctx expired | ||
_, err := RelayConnect(ctx, url) | ||
assert.ErrorIs(t, err, context.Canceled) | ||
} | ||
|
||
func TestPublish(t *testing.T) { | ||
url := os.Getenv("TEST_RELAY_URL") | ||
if url == "" { | ||
t.Fatal("please set the environment: $TEST_RELAY_URL") | ||
} | ||
|
||
// test note to be sent over websocket | ||
priv, pub := makeKeyPair(t) | ||
textNote := Event{ | ||
Kind: KindTextNote, | ||
Content: "hello", | ||
CreatedAt: Timestamp(1672068534), // random fixed timestamp | ||
Tags: Tags{[]string{"foo", "bar"}}, | ||
PubKey: pub, | ||
} | ||
err := textNote.Sign(priv) | ||
assert.NoError(t, err) | ||
|
||
// connect a client and send the text note | ||
rl := mustRelayConnect(t, url) | ||
err = rl.Publish(context.Background(), textNote) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func makeKeyPair(t *testing.T) (priv, pub string) { | ||
t.Helper() | ||
|
||
privkey := GeneratePrivateKey() | ||
pubkey, err := GetPublicKey(privkey) | ||
assert.NoError(t, err) | ||
|
||
return privkey, pubkey | ||
} | ||
|
||
func mustRelayConnect(t *testing.T, url string) *Relay { | ||
t.Helper() | ||
|
||
rl, err := RelayConnect(context.Background(), url) | ||
require.NoError(t, err) | ||
|
||
return rl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !js | ||
|
||
package nostr | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build !sqlite_math_functions | ||
//go:build !js && !sqlite_math_functions | ||
|
||
package test | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !js | ||
|
||
package test | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build !sqlite_math_functions | ||
//go:build !js && !sqlite_math_functions | ||
|
||
package test | ||
|
||
|