@xmcl/client
Advanced tools
Comparing version 3.1.1 to 3.2.0
@@ -8,3 +8,3 @@ /// <reference types="node" /> | ||
import { NetConnectOpts } from 'net'; | ||
import { Transform, TransformCallback, Writable } from 'stream'; | ||
import { Transform, TransformCallback, TransformOptions, Writable } from 'stream'; | ||
import { Coder } from './coders'; | ||
@@ -28,2 +28,3 @@ import { PacketRegistryEntry, Side } from './packet'; | ||
private inbound; | ||
private listened; | ||
private enableCompression; | ||
@@ -33,4 +34,4 @@ private compressionThreshold; | ||
/** | ||
* Is the connection ready to read and write | ||
*/ | ||
* Is the connection ready to read and write | ||
*/ | ||
get ready(): boolean; | ||
@@ -51,3 +52,3 @@ findCoderById(packetId: number, side: Side): Coder<any>; | ||
*/ | ||
send<T>(message: T, skeleton?: Partial<T>): void; | ||
send<T>(message: T, skeleton?: Partial<T>): Promise<void>; | ||
/** | ||
@@ -90,3 +91,5 @@ * Listen for sepcific packet by its class name. | ||
export declare abstract class PacketOutbound extends Transform { | ||
private channelWidth; | ||
protected abstract writePacketLength(bb: ByteBuffer, len: number): void; | ||
constructor(channelWidth?: number, opts?: TransformOptions); | ||
_transform(packet: Buffer, encoding: string, callback: TransformCallback): void; | ||
@@ -93,0 +96,0 @@ } |
@@ -331,2 +331,3 @@ "use strict"; | ||
inbound; | ||
listened = false; | ||
enableCompression = false; | ||
@@ -350,6 +351,6 @@ compressionThreshold = -1; | ||
/** | ||
* Is the connection ready to read and write | ||
*/ | ||
* Is the connection ready to read and write | ||
*/ | ||
get ready() { | ||
return this.connection.readable && this.connection.writable; | ||
return this.connection.readable && this.connection.writable && this.listened; | ||
} | ||
@@ -403,7 +404,9 @@ findCoderById(packetId, side) { | ||
this.emit("listen"); | ||
this.listened = true; | ||
} | ||
disconnect() { | ||
if (!this.ready) { | ||
if (!this.listened || !this.ready) { | ||
return Promise.resolve(); | ||
} | ||
this.listened = false; | ||
return new Promise((resolve, reject) => { | ||
@@ -430,4 +433,12 @@ this.connection.once("close", (err) => { | ||
} | ||
this.outbound.write(message); | ||
this.emit("send", message); | ||
return new Promise((resolve, reject) => { | ||
this.outbound.write(message, (err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
this.emit("send", message); | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
@@ -566,2 +577,6 @@ /** | ||
var PacketOutbound = class extends import_stream.Transform { | ||
constructor(channelWidth = Number.MAX_SAFE_INTEGER, opts) { | ||
super(opts); | ||
this.channelWidth = channelWidth; | ||
} | ||
_transform(packet, encoding, callback) { | ||
@@ -572,3 +587,9 @@ const buffer = new import_bytebuffer.ByteBuffer(); | ||
buffer.flip(); | ||
this.push(Buffer.from(buffer.buffer.slice(0, buffer.limit))); | ||
let bytesToSend = buffer.remaining(); | ||
while (bytesToSend > 0) { | ||
const toSend = Math.min(bytesToSend, this.channelWidth); | ||
const chunk = buffer.readBytes(toSend); | ||
this.push(Buffer.from(chunk.toBuffer())); | ||
bytesToSend -= toSend; | ||
} | ||
callback(); | ||
@@ -751,3 +772,3 @@ } | ||
const address = sock.address(); | ||
sock.addMembership(this.#group, address.address); | ||
sock.addMembership(this.#group, type === "udp4" ? address.address : void 0); | ||
sock.setMulticastTTL(128); | ||
@@ -754,0 +775,0 @@ sock.setBroadcast(true); |
@@ -1,2 +0,2 @@ | ||
import { State } from './channel'; | ||
import type { State } from './channel'; | ||
import { Coder } from './coders'; | ||
@@ -12,3 +12,3 @@ export type Side = 'server' | 'client'; | ||
export type FieldType<T> = (type: Coder<T>) => (target: any, key: string) => void; | ||
export type PacketType = (side: Side, id: number, state: State) => (constructor: Function) => void; | ||
export type PacketType = (side: Side, id: number, state: State) => (constructor: (...args: any[]) => any) => void; | ||
export declare const PacketMetadata: unique symbol; | ||
@@ -37,3 +37,3 @@ export declare const PacketFieldsMetadata: unique symbol; | ||
*/ | ||
export declare function Packet(side: Side, id: number, state: State, name?: string): (constructor: Function) => void; | ||
export declare function Packet(side: Side, id: number, state: State, name?: string): (constructor: new (...args: any[]) => any) => void; | ||
//# sourceMappingURL=packet.d.ts.map |
@@ -29,9 +29,9 @@ import { TextComponent } from '@xmcl/text-component'; | ||
/** | ||
* The name of the version, might be standard version, like 1.14.4. | ||
* Or it can be modified content, just be any string the server hoster like. | ||
*/ | ||
* The name of the version, might be standard version, like 1.14.4. | ||
* Or it can be modified content, just be any string the server hoster like. | ||
*/ | ||
name: string; | ||
/** | ||
* The protocol version | ||
*/ | ||
* The protocol version | ||
*/ | ||
protocol: number; | ||
@@ -38,0 +38,0 @@ }; |
{ | ||
"name": "@xmcl/client", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"main": "./dist/index.js", | ||
"description": "Minecraft socket pipeline utilities. Support Minecraft lan server discovery.", | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=20" | ||
}, | ||
@@ -14,4 +14,4 @@ "publishConfig": { | ||
"@xmcl/bytebuffer": "0.1.1", | ||
"@xmcl/text-component": "2.1.3", | ||
"@xmcl/nbt": "3.0.2" | ||
"@xmcl/nbt": "3.0.2", | ||
"@xmcl/text-component": "2.1.3" | ||
}, | ||
@@ -38,6 +38,6 @@ "repository": { | ||
"esbuild": "^0.17.16", | ||
"eslint": "^8.37.0", | ||
"oxlint": "^0.15.5", | ||
"tslib": "^2.5.0", | ||
"typescript": "^5.2.2", | ||
"@xmcl/eslint-config": "0.0.1" | ||
"typescript": "^5.3.3", | ||
"@xmcl/oxlint-config": "0.0.1" | ||
}, | ||
@@ -44,0 +44,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
156011
1968