Skip to content

Commit b14e574

Browse files
committed
refactor: update package name and documentation
- Updated README.md to reference correct npm package name (nostr-websocket-utils) - Added repository, bugs, and homepage fields to package.json - Fixed WebSocket mock implementation in tests - Updated import statements in documentation examples - Added UUID functionality documentation
1 parent 36006b4 commit b14e574

File tree

9 files changed

+450
-138
lines changed

9 files changed

+450
-138
lines changed

README.md

+77-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Nostr Websocket Utils
22

3-
[![npm version](https://img.shields.io/npm/v/@humanjavaenterprises/nostr-websocket-utils.svg)](https://www.npmjs.com/package/@humanjavaenterprises/nostr-websocket-utils)
4-
[![License](https://img.shields.io/npm/l/@humanjavaenterprises/nostr-websocket-utils.svg)](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/blob/main/LICENSE)
3+
[![npm version](https://img.shields.io/npm/v/nostr-websocket-utils.svg)](https://www.npmjs.com/package/nostr-websocket-utils)
4+
[![License](https://img.shields.io/npm/l/nostr-websocket-utils.svg)](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/blob/main/LICENSE)
55
[![Build Status](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/workflows/CI/badge.svg)](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/actions)
66
[![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org)
77
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
@@ -22,15 +22,15 @@ A TypeScript library providing WebSocket utilities for Nostr applications, with
2222
## Installation
2323

2424
```bash
25-
npm install @humanjavaenterprises/nostr-websocket-utils
25+
npm install nostr-websocket-utils
2626
```
2727

28-
## Breaking Changes in v0.2.0
28+
## Breaking Changes in v0.2.1
2929

30-
- Introduced required handlers pattern for better type safety
31-
- Removed individual event handler properties (onMessage, onError, onClose)
32-
- Message handler is now required in server options
33-
- Client updated to match server interface
30+
- Added UUID support for message tracking and correlation
31+
- Fixed WebSocket mock implementation in tests
32+
- Improved TypeScript type safety across the codebase
33+
- Enhanced error handling for WebSocket connections
3434

3535
## Usage
3636

@@ -39,7 +39,7 @@ npm install @humanjavaenterprises/nostr-websocket-utils
3939
```typescript
4040
import express from 'express';
4141
import { createServer } from 'http';
42-
import { NostrWSServer } from '@humanjavaenterprises/nostr-websocket-utils';
42+
import { NostrWSServer } from 'nostr-websocket-utils';
4343
import winston from 'winston';
4444

4545
const app = express();
@@ -89,10 +89,53 @@ const wss = new NostrWSServer(server, {
8989
server.listen(3000);
9090
```
9191

92+
### Server Example with UUID
93+
94+
```typescript
95+
import express from 'express';
96+
import { createServer } from 'http';
97+
import { NostrWSServer } from 'nostr-websocket-utils';
98+
import winston from 'winston';
99+
100+
const app = express();
101+
const server = createServer(app);
102+
103+
// Create a logger
104+
const logger = winston.createLogger({
105+
level: 'info',
106+
format: winston.format.json(),
107+
transports: [new winston.transports.Console()]
108+
});
109+
110+
// Initialize the WebSocket server with UUID support
111+
const wsServer = new NostrWSServer({
112+
server,
113+
logger,
114+
messageHandler: async (message, client) => {
115+
// message.uuid will contain a unique identifier for the message
116+
logger.info(`Received message with UUID: ${message.uuid}`);
117+
118+
// Handle the message based on type
119+
switch (message.type) {
120+
case 'subscribe':
121+
// Handle subscription
122+
break;
123+
case 'event':
124+
// Handle event
125+
break;
126+
default:
127+
// Handle unknown message type
128+
}
129+
}
130+
});
131+
132+
server.listen(3000);
133+
```
134+
92135
### Client Example
93136

94137
```typescript
95-
import { NostrWSClient } from '@humanjavaenterprises/nostr-websocket-utils';
138+
import { NostrWSClient } from 'nostr-websocket-utils';
96139
import winston from 'winston';
97140

98141
// Create a logger
@@ -130,6 +173,30 @@ client.on('connect', () => {
130173
client.connect();
131174
```
132175

176+
### Client Example with UUID
177+
178+
```typescript
179+
import { NostrWSClient } from 'nostr-websocket-utils';
180+
181+
const client = new NostrWSClient('ws://localhost:3000', {
182+
logger: console
183+
});
184+
185+
client.on('message', (message) => {
186+
// Access the UUID of the received message
187+
console.log(`Received message with UUID: ${message.uuid}`);
188+
});
189+
190+
// Connect to the server
191+
client.connect();
192+
193+
// Send a message (UUID will be automatically generated)
194+
client.send({
195+
type: 'event',
196+
data: { content: 'Hello, World!' }
197+
});
198+
```
199+
133200
## Interface Reference
134201

135202
### NostrWSOptions

package-lock.json

+23-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@humanjavaenterprises/nostr-websocket-utils",
3-
"version": "0.2.0",
2+
"name": "nostr-websocket-utils",
3+
"version": "0.2.2",
44
"description": "WebSocket utilities for Nostr applications",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -20,14 +20,24 @@
2020
"utils",
2121
"maiqr"
2222
],
23-
"author": "Human Java Enterprises",
23+
"author": "vveerrgg",
2424
"license": "MIT",
25+
"repository": {
26+
"type": "git",
27+
"url": "git+https://github.com/HumanjavaEnterprises/nostr-websocket-utils.git"
28+
},
29+
"bugs": {
30+
"url": "https://github.com/HumanjavaEnterprises/nostr-websocket-utils/issues"
31+
},
32+
"homepage": "https://github.com/HumanjavaEnterprises/nostr-websocket-utils#readme",
2533
"dependencies": {
26-
"ws": "^8.16.0",
27-
"winston": "^3.11.0",
2834
"@noble/curves": "^1.3.0",
2935
"@noble/hashes": "^1.3.3",
30-
"nostr-tools": "^2.1.4"
36+
"@types/uuid": "^10.0.0",
37+
"nostr-tools": "^2.1.4",
38+
"uuid": "^11.0.3",
39+
"winston": "^3.11.0",
40+
"ws": "^8.16.0"
3141
},
3242
"peerDependencies": {
3343
"nostr-tools": "^2.1.4"
@@ -48,14 +58,6 @@
4858
"README.md",
4959
"LICENSE"
5060
],
51-
"repository": {
52-
"type": "git",
53-
"url": "git+https://github.com/humanjavaenterprises/nostr-websocket-utils.git"
54-
},
55-
"bugs": {
56-
"url": "https://github.com/humanjavaenterprises/nostr-websocket-utils/issues"
57-
},
58-
"homepage": "https://github.com/humanjavaenterprises/nostr-websocket-utils#readme",
5961
"publishConfig": {
6062
"access": "public"
6163
}

src/__mocks__/extendedWsMock.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import WebSocket from 'ws';
2+
3+
const extendedMockWebSocket = {
4+
binaryType: 'nodebuffer',
5+
bufferedAmount: 0,
6+
extensions: '',
7+
protocol: '',
8+
readyState: WebSocket.OPEN as number,
9+
url: 'ws://test.com',
10+
isPaused: false,
11+
CONNECTING: 0,
12+
OPEN: 1,
13+
CLOSING: 2,
14+
CLOSED: 3,
15+
on(event: string, listener: (...args: unknown[]) => void) {
16+
return this;
17+
},
18+
send: jest.fn(),
19+
close: jest.fn(),
20+
ping: jest.fn(),
21+
pong: jest.fn(),
22+
terminate: jest.fn(),
23+
removeAllListeners: jest.fn(),
24+
removeListener: jest.fn(),
25+
addEventListener: jest.fn(),
26+
removeEventListener: jest.fn(),
27+
emit: jest.fn(),
28+
addListener: jest.fn(),
29+
once: jest.fn(),
30+
prependListener: jest.fn(),
31+
prependOnceListener: jest.fn(),
32+
eventNames: jest.fn(),
33+
listenerCount: jest.fn(),
34+
// Additional methods for extended behavior
35+
simulateOpen() {
36+
this.readyState = WebSocket.OPEN;
37+
this.emit('open');
38+
},
39+
simulateClose() {
40+
this.readyState = WebSocket.CLOSED;
41+
this.emit('close');
42+
},
43+
};
44+
45+
export default extendedMockWebSocket;

src/__mocks__/wsMock.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import WebSocket from 'ws';
2+
3+
const mockWebSocket = {
4+
binaryType: 'nodebuffer',
5+
bufferedAmount: 0,
6+
extensions: '',
7+
protocol: '',
8+
readyState: WebSocket.OPEN,
9+
url: 'ws://test.com',
10+
isPaused: false,
11+
CONNECTING: 0,
12+
OPEN: 1,
13+
CLOSING: 2,
14+
CLOSED: 3,
15+
on(event: string, listener: (...args: unknown[]) => void) {
16+
return this;
17+
},
18+
send: jest.fn(),
19+
close: jest.fn(),
20+
ping: jest.fn(),
21+
pong: jest.fn(),
22+
terminate: jest.fn(),
23+
removeAllListeners: jest.fn(),
24+
removeListener: jest.fn(),
25+
addEventListener: jest.fn(),
26+
removeEventListener: jest.fn(),
27+
emit: jest.fn(),
28+
addListener: jest.fn(),
29+
once: jest.fn(),
30+
prependListener: jest.fn(),
31+
prependOnceListener: jest.fn(),
32+
eventNames: jest.fn(),
33+
listenerCount: jest.fn(),
34+
};
35+
36+
export default mockWebSocket;

0 commit comments

Comments
 (0)