yeow-api 0.3.1 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/core.ts +6 -0
- package/src/event.ts +2 -1
- package/src/player.ts +9 -1
- package/src/util.ts +77 -0
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -103,3 +103,9 @@ export { registerPermission } from './permission.js';
|
|
|
103
103
|
export type { Permission, PermissionOptions, PermissionDefault } from './permission.js';
|
|
104
104
|
export { createWorker, Worker, onMessage, postMessage } from './worker.js';
|
|
105
105
|
export type { WorkerOptions } from './worker.js';
|
|
106
|
+
export {
|
|
107
|
+
stringToBytes, stringToBytesAsync,
|
|
108
|
+
bytesToString, bytesToStringAsync,
|
|
109
|
+
gzipCompress, gzipCompressSync,
|
|
110
|
+
gzipDecompress, gzipDecompressSync,
|
|
111
|
+
} from './util.js';
|
package/src/event.ts
CHANGED
|
@@ -376,7 +376,8 @@ function adaptEvent<K extends keyof EventMap>(type: K, data: RawEvent): { event:
|
|
|
376
376
|
'playerAdvancementDone', 'playerToggleSneak', 'playerToggleFlight',
|
|
377
377
|
'inventoryClick', 'playerResourcePackStatus',
|
|
378
378
|
] as K[];
|
|
379
|
-
|
|
379
|
+
// 直接以 uuid 构造 Player(零往返);name 在首次访问时惰性获取(见 Player.name)
|
|
380
|
+
if (hasPlayer.includes(type) && data.player) initial.player = new Player(data.player as string);
|
|
380
381
|
if (data.from) initial.from = loc(data.from as Record<string, unknown>);
|
|
381
382
|
if (data.to) initial.to = loc(data.to as Record<string, unknown>);
|
|
382
383
|
if (data.respawnLocation) initial.respawnLocation = loc(data.respawnLocation as Record<string, unknown>);
|
package/src/player.ts
CHANGED
|
@@ -32,7 +32,15 @@ export class Player {
|
|
|
32
32
|
|
|
33
33
|
constructor(public readonly uuid: string, private _name?: string) {}
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
/** 玩家名。未提供时首次访问惰性同步获取并缓存(仅一次往返)。 */
|
|
36
|
+
get name(): string {
|
|
37
|
+
if (this._name === undefined) {
|
|
38
|
+
let n = '';
|
|
39
|
+
try { n = call<PlayerData>('player.get', { identifier: this.uuid })?.name ?? ''; } catch { /* 离线/异常 → '' */ }
|
|
40
|
+
this._name = n;
|
|
41
|
+
}
|
|
42
|
+
return this._name;
|
|
43
|
+
}
|
|
36
44
|
|
|
37
45
|
/** 玩家物品栏(统一 Inventory 容器抽象)。 */
|
|
38
46
|
get inventory(): Inventory {
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// util 通道:gzip 压缩/解压 + UTF-8 ↔ 字节转换。
|
|
2
|
+
//
|
|
3
|
+
// 协议层字节数据以 base64 字符串承载(引擎原生 Uint8Array.toBase64()/fromBase64()
|
|
4
|
+
// 负责转换)——本模块输入输出一律 Uint8Array / string,**不暴露 base64**。
|
|
5
|
+
// encode/decode 的语义是 buffer ↔ 字符串;无流式接口(一次性整体处理)。
|
|
6
|
+
|
|
7
|
+
/** util 通道同步调用:err → 抛 Error。 */
|
|
8
|
+
function send<T>(t: string, p: Record<string, unknown>): T {
|
|
9
|
+
const r = $send('util', { t, p }) as T | { err?: string } | null;
|
|
10
|
+
if (r == null) return undefined as T;
|
|
11
|
+
if ((r as { err?: string }).err) throw new Error((r as { err?: string }).err);
|
|
12
|
+
return r as T;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** util 通道异步调用(ioExecutor 上执行,不阻塞 JS 线程)。 */
|
|
16
|
+
function sendAsync<T>(t: string, p: Record<string, unknown>): Promise<T> {
|
|
17
|
+
return new Promise<T>((resolve, reject) => {
|
|
18
|
+
const cbId = _registerCallback((r: T | { err?: string }) => {
|
|
19
|
+
if ((r as { err?: string })?.err) reject(new Error((r as { err?: string }).err));
|
|
20
|
+
else resolve(r as T);
|
|
21
|
+
});
|
|
22
|
+
$send('util', { t, p, cb: cbId });
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 输入规范化:string 视为 UTF-8 文本(经 encode.utf8 字节化);Uint8Array 直接转换。 */
|
|
27
|
+
function toB64(data: Uint8Array | string): string {
|
|
28
|
+
return typeof data === 'string'
|
|
29
|
+
? send<{ data: string }>('encode.utf8', { data }).data
|
|
30
|
+
: data.toBase64();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── 字符串 ↔ 字节(UTF-8)────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
/** UTF-8 字符串 → 字节(同步)。 */
|
|
36
|
+
export function stringToBytes(text: string): Uint8Array {
|
|
37
|
+
return Uint8Array.fromBase64(send<{ data: string }>('encode.utf8', { data: text }).data);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 字节 → UTF-8 字符串(同步;非法序列替换为 U+FFFD,不抛错)。 */
|
|
41
|
+
export function bytesToString(bytes: Uint8Array): string {
|
|
42
|
+
return send<{ data: string }>('decode.utf8', { data: bytes.toBase64() }).data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** UTF-8 字符串 → 字节(异步,ioExecutor 执行)。 */
|
|
46
|
+
export function stringToBytesAsync(text: string): Promise<Uint8Array> {
|
|
47
|
+
return sendAsync<{ data: string }>('encode.utf8', { data: text }).then((r) => Uint8Array.fromBase64(r.data));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 字节 → UTF-8 字符串(异步)。 */
|
|
51
|
+
export function bytesToStringAsync(bytes: Uint8Array): Promise<string> {
|
|
52
|
+
return sendAsync<{ data: string }>('decode.utf8', { data: bytes.toBase64() }).then((r) => r.data);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ── gzip ──────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
/** gzip 压缩(level 0-9,默认引擎默认级别)。输入 string 视为 UTF-8 文本。 */
|
|
58
|
+
export function gzipCompress(data: Uint8Array | string, level?: number): Promise<Uint8Array> {
|
|
59
|
+
return sendAsync<{ data: string }>('gzip.compress', { data: toB64(data), level })
|
|
60
|
+
.then((r) => Uint8Array.fromBase64(r.data));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** gzip 压缩(同步版)。 */
|
|
64
|
+
export function gzipCompressSync(data: Uint8Array | string, level?: number): Uint8Array {
|
|
65
|
+
return Uint8Array.fromBase64(send<{ data: string }>('gzip.compress', { data: toB64(data), level }).data);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** gzip 解压(输出上限 256 MiB,超限报错——防压缩炸弹)。 */
|
|
69
|
+
export function gzipDecompress(data: Uint8Array | string): Promise<Uint8Array> {
|
|
70
|
+
return sendAsync<{ data: string }>('gzip.decompress', { data: toB64(data) })
|
|
71
|
+
.then((r) => Uint8Array.fromBase64(r.data));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** gzip 解压(同步版)。 */
|
|
75
|
+
export function gzipDecompressSync(data: Uint8Array | string): Uint8Array {
|
|
76
|
+
return Uint8Array.fromBase64(send<{ data: string }>('gzip.decompress', { data: toB64(data) }).data);
|
|
77
|
+
}
|