@nice-code/action 0.2.10 → 0.2.12
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/build/devtools/browser/index.js +1275 -0
- package/build/devtools/server/index.js +232 -0
- package/build/index.js +2258 -43
- package/build/react-query/index.js +43 -3
- package/build/types/ActionRuntime/ActionRuntime.d.ts +4 -2
- package/build/types/ActionRuntime/RuntimeCoordinate.d.ts +9 -7
- package/build/types/devtools/browser/NiceActionDevtools.d.ts +8 -0
- package/build/types/devtools/browser/index.d.ts +3 -0
- package/build/types/devtools/core/ActionDevtools.types.d.ts +47 -0
- package/build/types/devtools/core/ActionDevtoolsCore.d.ts +16 -0
- package/build/types/devtools/server/NiceActionServerDevtools.d.ts +30 -0
- package/build/types/devtools/server/index.d.ts +3 -0
- package/build/types/index.d.ts +1 -1
- package/package.json +17 -3
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// src/ActionDefinition/Action/RunningAction.types.ts
|
|
2
|
+
var ERunningActionState;
|
|
3
|
+
((ERunningActionState2) => {
|
|
4
|
+
ERunningActionState2["running"] = "running";
|
|
5
|
+
ERunningActionState2["completed"] = "completed";
|
|
6
|
+
})(ERunningActionState ||= {});
|
|
7
|
+
var ERunningActionUpdateType;
|
|
8
|
+
((ERunningActionUpdateType2) => {
|
|
9
|
+
ERunningActionUpdateType2["started"] = "started";
|
|
10
|
+
ERunningActionUpdateType2["progress"] = "progress";
|
|
11
|
+
ERunningActionUpdateType2["finished"] = "finished";
|
|
12
|
+
})(ERunningActionUpdateType ||= {});
|
|
13
|
+
var ERunningActionFinishedType;
|
|
14
|
+
((ERunningActionFinishedType2) => {
|
|
15
|
+
ERunningActionFinishedType2["aborted"] = "aborted";
|
|
16
|
+
ERunningActionFinishedType2["failed"] = "failed";
|
|
17
|
+
ERunningActionFinishedType2["success"] = "success";
|
|
18
|
+
})(ERunningActionFinishedType ||= {});
|
|
19
|
+
|
|
20
|
+
// src/devtools/server/NiceActionServerDevtools.ts
|
|
21
|
+
class ActionServerDevtools {
|
|
22
|
+
_options;
|
|
23
|
+
_inFlight = new Map;
|
|
24
|
+
constructor(options = {}) {
|
|
25
|
+
const defaultEnabled = typeof process !== "undefined" ? true : true;
|
|
26
|
+
this._options = {
|
|
27
|
+
logger: options.logger ?? defaultConsoleLogger,
|
|
28
|
+
format: options.format ?? "pretty",
|
|
29
|
+
logPayloads: options.logPayloads ?? true,
|
|
30
|
+
enabled: options.enabled ?? defaultEnabled
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
attachToDomain(domain) {
|
|
34
|
+
if (!this._options.enabled) {
|
|
35
|
+
return () => {};
|
|
36
|
+
}
|
|
37
|
+
return domain.addActionListener((update) => {
|
|
38
|
+
const { runningAction, type, time } = update;
|
|
39
|
+
const actionPath = [...runningAction.allDomains, runningAction.id].join(".");
|
|
40
|
+
if (type === "started" /* started */) {
|
|
41
|
+
this._inFlight.set(runningAction.cuid, { startTime: time });
|
|
42
|
+
this._log("started", actionPath, runningAction.cuid, {
|
|
43
|
+
...this._options.logPayloads ? { input: runningAction.state?.request?.input } : {}
|
|
44
|
+
});
|
|
45
|
+
} else if (type === "progress" /* progress */) {
|
|
46
|
+
this._log("progress", actionPath, runningAction.cuid, { progress: update.progress });
|
|
47
|
+
} else if (type === "finished" /* finished */) {
|
|
48
|
+
const timing = this._inFlight.get(runningAction.cuid);
|
|
49
|
+
const duration = timing != null ? time - timing.startTime : undefined;
|
|
50
|
+
this._inFlight.delete(runningAction.cuid);
|
|
51
|
+
const finishType = update.finishType;
|
|
52
|
+
if (finishType === "success" /* success */) {
|
|
53
|
+
this._log("success", actionPath, runningAction.cuid, {
|
|
54
|
+
...duration != null ? { duration: `${duration}ms` } : {},
|
|
55
|
+
...this._options.logPayloads ? { output: update.response?.result?.output } : {}
|
|
56
|
+
});
|
|
57
|
+
} else if (finishType === "failed" /* failed */) {
|
|
58
|
+
this._log("failed", actionPath, runningAction.cuid, {
|
|
59
|
+
...duration != null ? { duration: `${duration}ms` } : {},
|
|
60
|
+
error: serializeError(update.error)
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
this._log("aborted", actionPath, runningAction.cuid, {
|
|
64
|
+
...duration != null ? { duration: `${duration}ms` } : {},
|
|
65
|
+
...update.reason != null ? { reason: String(update.reason) } : {}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
_log(event, actionPath, cuid, data) {
|
|
72
|
+
const { logger, format } = this._options;
|
|
73
|
+
if (format === "json") {
|
|
74
|
+
logger(JSON.stringify({ time: new Date().toISOString(), event, action: actionPath, cuid, ...data }));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const prefix = PRETTY_PREFIX[event] ?? `[${event}]`;
|
|
78
|
+
const suffix = Object.keys(data).length > 0 ? ` ${formatPrettyData(data)}` : "";
|
|
79
|
+
logger(`${prefix} ${actionPath} cuid=${cuid}${suffix}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
var PRETTY_PREFIX = {
|
|
83
|
+
started: "[nice-action] ►",
|
|
84
|
+
progress: "[nice-action] ",
|
|
85
|
+
success: "[nice-action] ✓",
|
|
86
|
+
failed: "[nice-action] ✗",
|
|
87
|
+
aborted: "[nice-action] ○"
|
|
88
|
+
};
|
|
89
|
+
function formatPrettyData(data) {
|
|
90
|
+
return Object.entries(data).map(([k, v]) => `${k}=${safeStringify(v)}`).join(" ");
|
|
91
|
+
}
|
|
92
|
+
function safeStringify(value) {
|
|
93
|
+
if (value === undefined)
|
|
94
|
+
return "undefined";
|
|
95
|
+
if (value === null)
|
|
96
|
+
return "null";
|
|
97
|
+
if (typeof value === "string")
|
|
98
|
+
return `"${value}"`;
|
|
99
|
+
try {
|
|
100
|
+
return JSON.stringify(value);
|
|
101
|
+
} catch {
|
|
102
|
+
return String(value);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function serializeError(err) {
|
|
106
|
+
if (err == null)
|
|
107
|
+
return err;
|
|
108
|
+
if (err instanceof Error)
|
|
109
|
+
return { message: err.message, name: err.name, stack: err.stack };
|
|
110
|
+
if (typeof err === "object") {
|
|
111
|
+
try {
|
|
112
|
+
return JSON.parse(JSON.stringify(err));
|
|
113
|
+
} catch {
|
|
114
|
+
return String(err);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return err;
|
|
118
|
+
}
|
|
119
|
+
function defaultConsoleLogger(message) {
|
|
120
|
+
console.log(message);
|
|
121
|
+
}
|
|
122
|
+
// src/devtools/core/ActionDevtoolsCore.ts
|
|
123
|
+
function extractRouting(context) {
|
|
124
|
+
return (context?.routing ?? []).map((item) => {
|
|
125
|
+
const handler = item.handler;
|
|
126
|
+
const isExternal = handler?.type === "external";
|
|
127
|
+
return {
|
|
128
|
+
runtime: {
|
|
129
|
+
envId: item.runtime?.envId ?? "unknown",
|
|
130
|
+
perId: item.runtime?.perId,
|
|
131
|
+
insId: item.runtime?.insId
|
|
132
|
+
},
|
|
133
|
+
handlerType: isExternal ? "external" : "local",
|
|
134
|
+
handlerClient: isExternal && handler.client != null ? {
|
|
135
|
+
envId: handler.client.envId ?? "unknown",
|
|
136
|
+
perId: handler.client.perId,
|
|
137
|
+
insId: handler.client.insId
|
|
138
|
+
} : undefined,
|
|
139
|
+
transport: isExternal ? handler.transType : undefined,
|
|
140
|
+
time: item.time
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function extractMeta(context) {
|
|
145
|
+
return {
|
|
146
|
+
timeCreated: context?.timeCreated ?? Date.now(),
|
|
147
|
+
originClient: {
|
|
148
|
+
envId: context?.originClient?.envId ?? "unknown",
|
|
149
|
+
perId: context?.originClient?.perId,
|
|
150
|
+
insId: context?.originClient?.insId
|
|
151
|
+
},
|
|
152
|
+
routing: extractRouting(context)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
class ActionDevtoolsCore {
|
|
157
|
+
_entries = [];
|
|
158
|
+
_listeners = new Set;
|
|
159
|
+
_maxEntries;
|
|
160
|
+
constructor(options = {}) {
|
|
161
|
+
this._maxEntries = options.maxEntries ?? 100;
|
|
162
|
+
}
|
|
163
|
+
attachToDomain(domain) {
|
|
164
|
+
return domain.addActionListener((update) => {
|
|
165
|
+
const { runningAction, type, time } = update;
|
|
166
|
+
if (type === "started" /* started */) {
|
|
167
|
+
const entry = {
|
|
168
|
+
cuid: runningAction.cuid,
|
|
169
|
+
actionId: runningAction.id,
|
|
170
|
+
domain: runningAction.domain,
|
|
171
|
+
allDomains: [...runningAction.allDomains],
|
|
172
|
+
status: "running",
|
|
173
|
+
startTime: time,
|
|
174
|
+
input: runningAction.state?.request?.input,
|
|
175
|
+
progressUpdates: [],
|
|
176
|
+
meta: extractMeta(runningAction.context)
|
|
177
|
+
};
|
|
178
|
+
this._entries = [entry, ...this._entries].slice(0, this._maxEntries);
|
|
179
|
+
this._notify();
|
|
180
|
+
} else if (type === "progress" /* progress */) {
|
|
181
|
+
this._updateEntry(runningAction.cuid, (e) => ({
|
|
182
|
+
...e,
|
|
183
|
+
progressUpdates: [...e.progressUpdates, update.progress]
|
|
184
|
+
}));
|
|
185
|
+
} else if (type === "finished" /* finished */) {
|
|
186
|
+
this._updateEntry(runningAction.cuid, (e) => {
|
|
187
|
+
const finishedRoutingContext = update.response?.context ?? runningAction.context;
|
|
188
|
+
const base = {
|
|
189
|
+
...e,
|
|
190
|
+
endTime: time,
|
|
191
|
+
meta: { ...e.meta, routing: extractRouting(finishedRoutingContext) }
|
|
192
|
+
};
|
|
193
|
+
const finishType = update.finishType;
|
|
194
|
+
if (finishType === "success" /* success */) {
|
|
195
|
+
return { ...base, status: "success", output: update.response?.result?.output };
|
|
196
|
+
}
|
|
197
|
+
if (finishType === "failed" /* failed */) {
|
|
198
|
+
return { ...base, status: "failed", error: update.error };
|
|
199
|
+
}
|
|
200
|
+
return { ...base, status: "aborted", abortReason: update.reason };
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
getEntries() {
|
|
206
|
+
return this._entries;
|
|
207
|
+
}
|
|
208
|
+
subscribe(listener) {
|
|
209
|
+
this._listeners.add(listener);
|
|
210
|
+
listener(this._entries);
|
|
211
|
+
return () => {
|
|
212
|
+
this._listeners.delete(listener);
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
clear() {
|
|
216
|
+
this._entries = [];
|
|
217
|
+
this._notify();
|
|
218
|
+
}
|
|
219
|
+
_updateEntry(cuid, updater) {
|
|
220
|
+
this._entries = this._entries.map((e) => e.cuid === cuid ? updater(e) : e);
|
|
221
|
+
this._notify();
|
|
222
|
+
}
|
|
223
|
+
_notify() {
|
|
224
|
+
const snapshot = this._entries;
|
|
225
|
+
for (const listener of this._listeners)
|
|
226
|
+
listener(snapshot);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
export {
|
|
230
|
+
ActionServerDevtools,
|
|
231
|
+
ActionDevtoolsCore
|
|
232
|
+
};
|