@nice-code/action 0.4.11 → 0.5.1

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.
@@ -4,9 +4,11 @@ export interface INiceActionDevtoolsProps {
4
4
  core: ActionDevtoolsCore;
5
5
  position?: TDevtoolsPosition;
6
6
  initialOpen?: boolean;
7
+ /** Show the panel even when NODE_ENV is not "development". */
8
+ forceEnable?: boolean;
7
9
  }
8
10
  export interface IEntryGroup {
9
11
  representative: IDevtoolsActionEntry;
10
12
  rest: IDevtoolsActionEntry[];
11
13
  }
12
- export declare function NiceActionDevtools(props: INiceActionDevtoolsProps): import("react/jsx-runtime").JSX.Element | null;
14
+ export declare function NiceActionDevtools({ forceEnable, ...props }: INiceActionDevtoolsProps): import("react/jsx-runtime").JSX.Element | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice-code/action",
3
- "version": "0.4.11",
3
+ "version": "0.5.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -44,9 +44,9 @@
44
44
  "build-types": "tsc --project tsconfig.build.json"
45
45
  },
46
46
  "dependencies": {
47
- "@nice-code/common-errors": "0.4.11",
48
- "@nice-code/error": "0.4.11",
49
- "@nice-code/util": "0.4.11",
47
+ "@nice-code/common-errors": "0.5.1",
48
+ "@nice-code/error": "0.5.1",
49
+ "@nice-code/util": "0.5.1",
50
50
  "@standard-schema/spec": "^1.1.0",
51
51
  "@tanstack/react-virtual": "^3.13.26",
52
52
  "http-status-codes": "^2.3.0",
@@ -1,265 +0,0 @@
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
- const result = update.response?.result;
54
- if (result != null && !result.ok) {
55
- this._log("action-error", actionPath, runningAction.cuid, {
56
- ...duration != null ? { duration: `${duration}ms` } : {},
57
- error: serializeError(result.error)
58
- });
59
- } else {
60
- this._log("success", actionPath, runningAction.cuid, {
61
- ...duration != null ? { duration: `${duration}ms` } : {},
62
- ...this._options.logPayloads ? { output: result?.output } : {}
63
- });
64
- }
65
- } else if (finishType === "failed" /* failed */) {
66
- this._log("failed", actionPath, runningAction.cuid, {
67
- ...duration != null ? { duration: `${duration}ms` } : {},
68
- error: serializeError(update.error)
69
- });
70
- } else {
71
- this._log("aborted", actionPath, runningAction.cuid, {
72
- ...duration != null ? { duration: `${duration}ms` } : {},
73
- ...update.reason != null ? { reason: String(update.reason) } : {}
74
- });
75
- }
76
- }
77
- });
78
- }
79
- _log(event, actionPath, cuid, data) {
80
- const { logger, format } = this._options;
81
- if (format === "json") {
82
- logger(JSON.stringify({ time: new Date().toISOString(), event, action: actionPath, cuid, ...data }));
83
- return;
84
- }
85
- const prefix = PRETTY_PREFIX[event] ?? `[${event}]`;
86
- const suffix = Object.keys(data).length > 0 ? ` ${formatPrettyData(data)}` : "";
87
- logger(`${prefix} ${actionPath} cuid=${cuid}${suffix}`);
88
- }
89
- }
90
- var PRETTY_PREFIX = {
91
- started: "[nice-action] ►",
92
- progress: "[nice-action] ",
93
- success: "[nice-action] ✓",
94
- failed: "[nice-action] ✗",
95
- aborted: "[nice-action] ○"
96
- };
97
- function formatPrettyData(data) {
98
- return Object.entries(data).map(([k, v]) => `${k}=${safeStringify(v)}`).join(" ");
99
- }
100
- function safeStringify(value) {
101
- if (value === undefined)
102
- return "undefined";
103
- if (value === null)
104
- return "null";
105
- if (typeof value === "string")
106
- return `"${value}"`;
107
- try {
108
- return JSON.stringify(value);
109
- } catch {
110
- return String(value);
111
- }
112
- }
113
- function serializeError(err) {
114
- if (err == null)
115
- return err;
116
- if (err instanceof Error)
117
- return { message: err.message, name: err.name, stack: err.stack };
118
- if (typeof err === "object") {
119
- try {
120
- return JSON.parse(JSON.stringify(err));
121
- } catch {
122
- return String(err);
123
- }
124
- }
125
- return err;
126
- }
127
- function defaultConsoleLogger(message) {
128
- console.log(message);
129
- }
130
- // src/devtools/core/ActionDevtoolsCore.ts
131
- function serializeErrorForDisplay(error) {
132
- if (error != null && typeof error === "object" && error.name === "NiceError" && typeof error.toJsonObject === "function") {
133
- return error.toJsonObject();
134
- }
135
- return error;
136
- }
137
- function extractRouting(context) {
138
- return (context?.routing ?? []).map((item) => {
139
- const handler = item.handler;
140
- const isExternal = handler?.type === "external";
141
- return {
142
- runtime: {
143
- envId: item.runtime?.envId ?? "unknown",
144
- perId: item.runtime?.perId,
145
- insId: item.runtime?.insId
146
- },
147
- handlerType: isExternal ? "external" : "local",
148
- handlerClient: isExternal && handler.client != null ? {
149
- envId: handler.client.envId ?? "unknown",
150
- perId: handler.client.perId,
151
- insId: handler.client.insId
152
- } : undefined,
153
- transport: isExternal ? handler.transType : undefined,
154
- transportSummary: isExternal ? handler.transInfo?.summary : undefined,
155
- transportUrl: isExternal ? handler.transInfo?.url : undefined,
156
- time: item.time
157
- };
158
- });
159
- }
160
- function extractMeta(context) {
161
- return {
162
- timeCreated: context?.timeCreated ?? Date.now(),
163
- originClient: {
164
- envId: context?.originClient?.envId ?? "unknown",
165
- perId: context?.originClient?.perId,
166
- insId: context?.originClient?.insId
167
- },
168
- routing: extractRouting(context)
169
- };
170
- }
171
-
172
- class ActionDevtoolsCore {
173
- _entries = [];
174
- _listeners = new Set;
175
- constructor(_options = {}) {}
176
- attachToDomain(domain) {
177
- return domain.addActionListener((update) => {
178
- const { runningAction, type, time } = update;
179
- if (type === "started" /* started */) {
180
- const entry = {
181
- cuid: runningAction.cuid,
182
- actionId: runningAction.id,
183
- domain: runningAction.domain,
184
- allDomains: [...runningAction.allDomains],
185
- status: "running",
186
- startTime: time,
187
- input: runningAction.state?.request?.input,
188
- inputHash: runningAction.state?.request?.inputHash,
189
- progressUpdates: [],
190
- meta: extractMeta(runningAction.context),
191
- parentCuid: runningAction.parentCuid,
192
- callSite: runningAction.callSite
193
- };
194
- this._entries = [entry, ...this._entries];
195
- this._notify();
196
- } else if (type === "progress" /* progress */) {
197
- this._updateEntry(runningAction.cuid, (e) => ({
198
- ...e,
199
- progressUpdates: [...e.progressUpdates, update.progress]
200
- }));
201
- } else if (type === "finished" /* finished */) {
202
- this._updateEntry(runningAction.cuid, (e) => {
203
- const finishedRoutingContext = update.response?.context ?? runningAction.context;
204
- const base = {
205
- ...e,
206
- endTime: time,
207
- meta: { ...e.meta, routing: extractRouting(finishedRoutingContext) }
208
- };
209
- const finishType = update.finishType;
210
- if (finishType === "success" /* success */) {
211
- const result = update.response?.result;
212
- const outputHash = update.response?.outputHash;
213
- if (result != null && !result.ok) {
214
- const rawError = result.error;
215
- const errorStack2 = rawError instanceof Error ? rawError.stack : undefined;
216
- return {
217
- ...base,
218
- status: "action-error",
219
- outputHash,
220
- error: serializeErrorForDisplay(rawError),
221
- errorStack: errorStack2
222
- };
223
- }
224
- return { ...base, status: "success", output: result?.output, outputHash };
225
- }
226
- if (finishType === "failed" /* failed */) {
227
- const rawError = update.error;
228
- const errorStack2 = rawError instanceof Error ? rawError.stack : undefined;
229
- return { ...base, status: "failed", error: serializeErrorForDisplay(rawError), errorStack: errorStack2 };
230
- }
231
- const abortReason = update.reason;
232
- const errorStack = abortReason instanceof Error ? abortReason.stack : undefined;
233
- return { ...base, status: "aborted", abortReason: serializeErrorForDisplay(abortReason), errorStack };
234
- });
235
- }
236
- });
237
- }
238
- getEntries() {
239
- return this._entries;
240
- }
241
- subscribe(listener) {
242
- this._listeners.add(listener);
243
- listener(this._entries);
244
- return () => {
245
- this._listeners.delete(listener);
246
- };
247
- }
248
- clear() {
249
- this._entries = [];
250
- this._notify();
251
- }
252
- _updateEntry(cuid, updater) {
253
- this._entries = this._entries.map((e) => e.cuid === cuid ? updater(e) : e);
254
- this._notify();
255
- }
256
- _notify() {
257
- const snapshot = this._entries;
258
- for (const listener of this._listeners)
259
- listener(snapshot);
260
- }
261
- }
262
- export {
263
- ActionServerDevtools,
264
- ActionDevtoolsCore
265
- };