pixifact-cli 0.3.0 → 0.5.0
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/README.md +32 -1
- package/editor/assets/browserAll-CdyZj0BN.js +1 -0
- package/editor/assets/index-CAe9Mkmr.js +1130 -0
- package/editor/assets/index-CVrKIfXK.css +1 -0
- package/editor/assets/init-C32NAhsC.js +1 -0
- package/editor/assets/webworkerAll-BEmxAAYf.js +1 -0
- package/editor/index.html +13 -0
- package/package.json +5 -3
- package/src/automation.ts +16 -7
- package/src/editorServer.ts +375 -0
- package/src/editorSession.ts +490 -0
- package/src/pixifact-cli.ts +127 -47
- package/src/wechatTarget.ts +578 -0
- package/src/liveBridge.ts +0 -28
- package/src/liveBridgeServer.ts +0 -163
package/src/liveBridgeServer.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
pixifactAgentBridgePath,
|
|
3
|
-
pixifactAgentBridgePort,
|
|
4
|
-
type LiveBridgeClientMessage,
|
|
5
|
-
type LiveBridgeRequestMessage,
|
|
6
|
-
type LiveBridgeResponseMessage,
|
|
7
|
-
} from './liveBridge';
|
|
8
|
-
|
|
9
|
-
interface BunServerSocket {
|
|
10
|
-
send(data: string): void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface BunServer {
|
|
14
|
-
upgrade(request: Request): boolean;
|
|
15
|
-
stop(force?: boolean): void;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface BunServeOptions {
|
|
19
|
-
hostname: string;
|
|
20
|
-
port: number;
|
|
21
|
-
fetch(request: Request, server: BunServer): Response | undefined;
|
|
22
|
-
websocket: {
|
|
23
|
-
message(socket: BunServerSocket, data: string | Uint8Array): void;
|
|
24
|
-
close(socket: BunServerSocket): void;
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
declare const Bun: {
|
|
29
|
-
serve(options: BunServeOptions): BunServer;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
interface PendingRequest {
|
|
33
|
-
resolve(result: unknown): void;
|
|
34
|
-
reject(error: Error): void;
|
|
35
|
-
timer: ReturnType<typeof setTimeout>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface LiveBridgeServerOptions {
|
|
39
|
-
port?: number;
|
|
40
|
-
requestTimeoutMs?: number;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function responseError(message: LiveBridgeResponseMessage) {
|
|
44
|
-
return new Error(message.error ?? 'Live editor request failed.');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function createLiveBridgeServer(options: LiveBridgeServerOptions = {}) {
|
|
48
|
-
const port = options.port ?? pixifactAgentBridgePort;
|
|
49
|
-
const requestTimeoutMs = options.requestTimeoutMs ?? 30000;
|
|
50
|
-
let editor: BunServerSocket | undefined;
|
|
51
|
-
const pending = new Map<string, PendingRequest>();
|
|
52
|
-
const connectionWaiters = new Set<() => void>();
|
|
53
|
-
let nextRequestId = 0;
|
|
54
|
-
|
|
55
|
-
function rejectPending(error: Error) {
|
|
56
|
-
for (const pendingRequest of pending.values()) {
|
|
57
|
-
clearTimeout(pendingRequest.timer);
|
|
58
|
-
pendingRequest.reject(error);
|
|
59
|
-
}
|
|
60
|
-
pending.clear();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const server = Bun.serve({
|
|
64
|
-
hostname: '127.0.0.1',
|
|
65
|
-
port,
|
|
66
|
-
fetch(request, server) {
|
|
67
|
-
const url = new URL(request.url);
|
|
68
|
-
if (url.pathname !== pixifactAgentBridgePath) {
|
|
69
|
-
return new Response('Not found.', { status: 404 });
|
|
70
|
-
}
|
|
71
|
-
if (server.upgrade(request)) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
return new Response('WebSocket upgrade failed.', { status: 400 });
|
|
75
|
-
},
|
|
76
|
-
websocket: {
|
|
77
|
-
message(socket, data) {
|
|
78
|
-
const message = JSON.parse(String(data)) as LiveBridgeClientMessage;
|
|
79
|
-
if (message.type === 'hello' && message.role === 'editor') {
|
|
80
|
-
editor = socket;
|
|
81
|
-
for (const resolve of connectionWaiters) {
|
|
82
|
-
resolve();
|
|
83
|
-
}
|
|
84
|
-
connectionWaiters.clear();
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (message.type !== 'response') {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const pendingRequest = pending.get(message.id);
|
|
91
|
-
if (!pendingRequest) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
pending.delete(message.id);
|
|
95
|
-
clearTimeout(pendingRequest.timer);
|
|
96
|
-
if (message.ok) {
|
|
97
|
-
pendingRequest.resolve(message.result);
|
|
98
|
-
} else {
|
|
99
|
-
pendingRequest.reject(responseError(message));
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
close(socket) {
|
|
103
|
-
if (socket === editor) {
|
|
104
|
-
editor = undefined;
|
|
105
|
-
rejectPending(new Error('Live editor disconnected.'));
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
get connected() {
|
|
113
|
-
return editor !== undefined;
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
async waitForConnection(timeoutMs = 3000) {
|
|
117
|
-
if (editor) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
return new Promise<void>((resolve, reject) => {
|
|
121
|
-
const timer = setTimeout(() => {
|
|
122
|
-
connectionWaiters.delete(done);
|
|
123
|
-
reject(new Error('No live Pixifact editor is connected.'));
|
|
124
|
-
}, timeoutMs);
|
|
125
|
-
function done() {
|
|
126
|
-
clearTimeout(timer);
|
|
127
|
-
resolve();
|
|
128
|
-
}
|
|
129
|
-
connectionWaiters.add(done);
|
|
130
|
-
});
|
|
131
|
-
},
|
|
132
|
-
|
|
133
|
-
async callAction(action: string, args: unknown) {
|
|
134
|
-
if (!editor) {
|
|
135
|
-
throw new Error('No live Pixifact editor is connected.');
|
|
136
|
-
}
|
|
137
|
-
const id = `live-${Date.now()}-${nextRequestId++}`;
|
|
138
|
-
const message: LiveBridgeRequestMessage = {
|
|
139
|
-
type: 'request',
|
|
140
|
-
id,
|
|
141
|
-
action,
|
|
142
|
-
arguments: args,
|
|
143
|
-
};
|
|
144
|
-
return new Promise((resolve, reject) => {
|
|
145
|
-
const timer = setTimeout(() => {
|
|
146
|
-
pending.delete(id);
|
|
147
|
-
reject(new Error(`Live editor action "${action}" timed out.`));
|
|
148
|
-
}, requestTimeoutMs);
|
|
149
|
-
pending.set(id, {
|
|
150
|
-
resolve,
|
|
151
|
-
reject,
|
|
152
|
-
timer,
|
|
153
|
-
});
|
|
154
|
-
editor!.send(JSON.stringify(message));
|
|
155
|
-
});
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
stop() {
|
|
159
|
-
rejectPending(new Error('Live bridge stopped.'));
|
|
160
|
-
server.stop(true);
|
|
161
|
-
},
|
|
162
|
-
};
|
|
163
|
-
}
|