@nocobase/plugin-async-task-manager 1.5.0-beta.20

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.
Files changed (52) hide show
  1. package/LICENSE.txt +159 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +2 -0
  4. package/client.js +1 -0
  5. package/dist/client/AsyncTaskManagerProvider.d.ts +4 -0
  6. package/dist/client/TaskResultRendererManager.d.ts +6 -0
  7. package/dist/client/components/AsyncTasks.d.ts +3 -0
  8. package/dist/client/index.d.ts +7 -0
  9. package/dist/client/index.js +10 -0
  10. package/dist/client/locale.d.ts +2 -0
  11. package/dist/externalVersion.js +19 -0
  12. package/dist/index.d.ts +2 -0
  13. package/dist/index.js +48 -0
  14. package/dist/locale/en-US.json +5 -0
  15. package/dist/locale/zh-CN.json +45 -0
  16. package/dist/node_modules/uuid/AUTHORS +5 -0
  17. package/dist/node_modules/uuid/bin/uuid +65 -0
  18. package/dist/node_modules/uuid/index.js +1 -0
  19. package/dist/node_modules/uuid/lib/bytesToUuid.js +26 -0
  20. package/dist/node_modules/uuid/lib/md5-browser.js +216 -0
  21. package/dist/node_modules/uuid/lib/md5.js +25 -0
  22. package/dist/node_modules/uuid/lib/rng-browser.js +34 -0
  23. package/dist/node_modules/uuid/lib/rng.js +8 -0
  24. package/dist/node_modules/uuid/lib/sha1-browser.js +89 -0
  25. package/dist/node_modules/uuid/lib/sha1.js +25 -0
  26. package/dist/node_modules/uuid/lib/v35.js +57 -0
  27. package/dist/node_modules/uuid/package.json +1 -0
  28. package/dist/node_modules/uuid/v1.js +109 -0
  29. package/dist/node_modules/uuid/v3.js +4 -0
  30. package/dist/node_modules/uuid/v4.js +29 -0
  31. package/dist/node_modules/uuid/v5.js +3 -0
  32. package/dist/server/base-task-manager.d.ts +23 -0
  33. package/dist/server/base-task-manager.js +135 -0
  34. package/dist/server/command-task-type.d.ts +8 -0
  35. package/dist/server/command-task-type.js +121 -0
  36. package/dist/server/index.d.ts +3 -0
  37. package/dist/server/index.js +50 -0
  38. package/dist/server/interfaces/async-task-manager.d.ts +58 -0
  39. package/dist/server/interfaces/async-task-manager.js +41 -0
  40. package/dist/server/interfaces/task.d.ts +37 -0
  41. package/dist/server/interfaces/task.js +24 -0
  42. package/dist/server/plugin.d.ts +7 -0
  43. package/dist/server/plugin.js +163 -0
  44. package/dist/server/resourcers/async-tasks.d.ts +8 -0
  45. package/dist/server/resourcers/async-tasks.js +73 -0
  46. package/dist/server/static-import.d.ts +1 -0
  47. package/dist/server/static-import.js +39 -0
  48. package/dist/server/task-type.d.ts +93 -0
  49. package/dist/server/task-type.js +197 -0
  50. package/package.json +16 -0
  51. package/server.d.ts +2 -0
  52. package/server.js +1 -0
@@ -0,0 +1,73 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var async_tasks_exports = {};
38
+ __export(async_tasks_exports, {
39
+ default: () => async_tasks_default
40
+ });
41
+ module.exports = __toCommonJS(async_tasks_exports);
42
+ var import_fs = __toESM(require("fs"));
43
+ var import_path = require("path");
44
+ var async_tasks_default = {
45
+ name: "asyncTasks",
46
+ actions: {
47
+ async get(ctx, next) {
48
+ const { filterByTk } = ctx.action.params;
49
+ const taskManager = ctx.app.container.get("AsyncTaskManager");
50
+ const taskStatus = await taskManager.getTaskStatus(filterByTk);
51
+ ctx.body = taskStatus;
52
+ await next();
53
+ },
54
+ async fetchFile(ctx, next) {
55
+ const { filterByTk } = ctx.action.params;
56
+ const taskManager = ctx.app.container.get("AsyncTaskManager");
57
+ const taskStatus = await taskManager.getTaskStatus(filterByTk);
58
+ if (taskStatus.type !== "success") {
59
+ throw new Error("Task is not success status");
60
+ }
61
+ const { filePath } = taskStatus.payload;
62
+ if (!filePath) {
63
+ throw new Error("not a file task");
64
+ }
65
+ ctx.body = import_fs.default.createReadStream(filePath);
66
+ ctx.set({
67
+ "Content-Type": "application/octet-stream",
68
+ "Content-Disposition": `attachment; filename=${(0, import_path.basename)(filePath)}`
69
+ });
70
+ await next();
71
+ }
72
+ }
73
+ };
@@ -0,0 +1 @@
1
+ export declare function staticImport(): Promise<void>;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var static_import_exports = {};
28
+ __export(static_import_exports, {
29
+ staticImport: () => staticImport
30
+ });
31
+ module.exports = __toCommonJS(static_import_exports);
32
+ var import_server = require("@nocobase/server");
33
+ async function staticImport() {
34
+ await (0, import_server.appendToBuiltInPlugins)("@nocobase/plugin-async-task-manager");
35
+ }
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ staticImport
39
+ });
@@ -0,0 +1,93 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import { Logger } from '@nocobase/logger';
4
+ import { TaskOptions, TaskStatus } from './interfaces/async-task-manager';
5
+ import { ITask } from './interfaces/task';
6
+ import Application from '@nocobase/server';
7
+ export declare abstract class TaskType extends EventEmitter implements ITask {
8
+ protected options: TaskOptions;
9
+ static type: string;
10
+ status: TaskStatus;
11
+ protected logger: Logger;
12
+ protected app: Application;
13
+ progress: {
14
+ total: number;
15
+ current: number;
16
+ };
17
+ startedAt: Date;
18
+ fulfilledAt: Date;
19
+ taskId: string;
20
+ tags: Record<string, string>;
21
+ createdAt: Date;
22
+ context?: any;
23
+ title: any;
24
+ protected abortController: AbortController;
25
+ private _isCancelled;
26
+ get isCancelled(): boolean;
27
+ constructor(options: TaskOptions, tags?: Record<string, string>);
28
+ setLogger(logger: Logger): void;
29
+ setApp(app: Application): void;
30
+ setContext(context: any): void;
31
+ /**
32
+ * Cancel the task
33
+ */
34
+ cancel(): Promise<boolean>;
35
+ /**
36
+ * Execute the task implementation
37
+ * @returns Promise that resolves with the task result
38
+ */
39
+ abstract execute(): Promise<any>;
40
+ /**
41
+ * Report task progress
42
+ * @param progress Progress information containing total and current values
43
+ */
44
+ reportProgress(progress: {
45
+ total: number;
46
+ current: number;
47
+ }): void;
48
+ /**
49
+ * Run the task
50
+ * This method handles task lifecycle, including:
51
+ * - Status management
52
+ * - Error handling
53
+ * - Progress tracking
54
+ * - Event emission
55
+ */
56
+ run(): Promise<void>;
57
+ private renderErrorMessage;
58
+ toJSON(options?: {
59
+ raw?: boolean;
60
+ }): {
61
+ taskId: string;
62
+ status: {
63
+ type: "pending";
64
+ indicator?: "spinner";
65
+ } | {
66
+ type: "success";
67
+ indicator?: "success";
68
+ resultType?: "file" | "data";
69
+ payload?: any;
70
+ } | {
71
+ type: "running";
72
+ indicator: "progress";
73
+ } | {
74
+ type: "failed";
75
+ indicator?: "error";
76
+ errors: {
77
+ message: string;
78
+ code?: number;
79
+ }[];
80
+ } | {
81
+ type: "cancelled";
82
+ };
83
+ progress: {
84
+ total: number;
85
+ current: number;
86
+ };
87
+ tags: Record<string, string>;
88
+ createdAt: Date;
89
+ startedAt: Date;
90
+ fulfilledAt: Date;
91
+ title: any;
92
+ };
93
+ }
@@ -0,0 +1,197 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var task_type_exports = {};
38
+ __export(task_type_exports, {
39
+ TaskType: () => TaskType
40
+ });
41
+ module.exports = __toCommonJS(task_type_exports);
42
+ var import_uuid = require("uuid");
43
+ var import_events = __toESM(require("events"));
44
+ var import_async_task_manager = require("./interfaces/async-task-manager");
45
+ class TaskType extends import_events.default {
46
+ constructor(options, tags) {
47
+ super();
48
+ this.options = options;
49
+ this.status = {
50
+ type: "pending",
51
+ indicator: "spinner"
52
+ };
53
+ this.taskId = (0, import_uuid.v4)();
54
+ this.tags = tags || {};
55
+ this.createdAt = /* @__PURE__ */ new Date();
56
+ }
57
+ static type;
58
+ status;
59
+ logger;
60
+ app;
61
+ progress = {
62
+ total: 0,
63
+ current: 0
64
+ };
65
+ startedAt;
66
+ fulfilledAt;
67
+ taskId;
68
+ tags;
69
+ createdAt;
70
+ context;
71
+ title;
72
+ abortController = new AbortController();
73
+ _isCancelled = false;
74
+ get isCancelled() {
75
+ return this._isCancelled;
76
+ }
77
+ setLogger(logger) {
78
+ this.logger = logger;
79
+ }
80
+ setApp(app) {
81
+ this.app = app;
82
+ }
83
+ setContext(context) {
84
+ this.context = context;
85
+ }
86
+ /**
87
+ * Cancel the task
88
+ */
89
+ async cancel() {
90
+ var _a;
91
+ this._isCancelled = true;
92
+ this.abortController.abort();
93
+ (_a = this.logger) == null ? void 0 : _a.debug(`Task ${this.taskId} cancelled`);
94
+ return true;
95
+ }
96
+ /**
97
+ * Report task progress
98
+ * @param progress Progress information containing total and current values
99
+ */
100
+ reportProgress(progress) {
101
+ var _a;
102
+ this.progress = progress;
103
+ (_a = this.logger) == null ? void 0 : _a.debug(`Task ${this.taskId} progress update - current: ${progress.current}, total: ${progress.total}`);
104
+ this.emit("progress", progress);
105
+ }
106
+ /**
107
+ * Run the task
108
+ * This method handles task lifecycle, including:
109
+ * - Status management
110
+ * - Error handling
111
+ * - Progress tracking
112
+ * - Event emission
113
+ */
114
+ async run() {
115
+ var _a, _b, _c, _d, _e, _f;
116
+ this.startedAt = /* @__PURE__ */ new Date();
117
+ (_a = this.logger) == null ? void 0 : _a.info(`Starting task ${this.taskId}, type: ${this.constructor.type}`);
118
+ this.status = {
119
+ type: "running",
120
+ indicator: "progress"
121
+ };
122
+ this.emit("statusChange", this.status);
123
+ try {
124
+ if (this._isCancelled) {
125
+ (_b = this.logger) == null ? void 0 : _b.info(`Task ${this.taskId} was cancelled before execution`);
126
+ this.status = {
127
+ type: "cancelled"
128
+ };
129
+ this.emit("statusChange", this.status);
130
+ return;
131
+ }
132
+ const executePromise = this.execute();
133
+ const result = await executePromise;
134
+ this.status = {
135
+ type: "success",
136
+ indicator: "success",
137
+ payload: result
138
+ };
139
+ (_c = this.logger) == null ? void 0 : _c.info(`Task ${this.taskId} completed successfully with result: ${JSON.stringify(result)}`);
140
+ this.emit("statusChange", this.status);
141
+ } catch (error) {
142
+ if (error instanceof import_async_task_manager.CancelError) {
143
+ this.status = {
144
+ type: "cancelled"
145
+ };
146
+ (_d = this.logger) == null ? void 0 : _d.info(`Task ${this.taskId} was cancelled during execution`);
147
+ } else {
148
+ this.status = {
149
+ type: "failed",
150
+ indicator: "error",
151
+ errors: [{ message: this.renderErrorMessage(error) }]
152
+ };
153
+ (_e = this.logger) == null ? void 0 : _e.error(`Task ${this.taskId} failed with error: ${error.message}`);
154
+ }
155
+ this.emit("statusChange", this.status);
156
+ } finally {
157
+ this.fulfilledAt = /* @__PURE__ */ new Date();
158
+ const duration = this.fulfilledAt.getTime() - this.startedAt.getTime();
159
+ (_f = this.logger) == null ? void 0 : _f.info(`Task ${this.taskId} finished in ${duration}ms`);
160
+ }
161
+ }
162
+ renderErrorMessage(error) {
163
+ const errorHandlerPlugin = this.app.pm.get("error-handler");
164
+ if (!errorHandlerPlugin || !this.context) {
165
+ return error.message;
166
+ }
167
+ const errorHandler = errorHandlerPlugin.errorHandler;
168
+ errorHandler.renderError(error, this.context);
169
+ return this.context.body.errors[0].message;
170
+ }
171
+ toJSON(options) {
172
+ var _a;
173
+ const json = {
174
+ taskId: this.taskId,
175
+ status: { ...this.status },
176
+ progress: this.progress,
177
+ tags: this.tags,
178
+ createdAt: this.createdAt,
179
+ startedAt: this.startedAt,
180
+ fulfilledAt: this.fulfilledAt,
181
+ title: this.title
182
+ };
183
+ if (!(options == null ? void 0 : options.raw) && json.status.type === "success" && ((_a = json.status.payload) == null ? void 0 : _a.filePath)) {
184
+ json.status = {
185
+ type: "success",
186
+ indicator: "success",
187
+ resultType: "file",
188
+ payload: {}
189
+ };
190
+ }
191
+ return json;
192
+ }
193
+ }
194
+ // Annotate the CommonJS export names for ESM import in node:
195
+ 0 && (module.exports = {
196
+ TaskType
197
+ });
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@nocobase/plugin-async-task-manager",
3
+ "displayName": "Async task manager",
4
+ "displayName.zh-CN": "异步任务管理器",
5
+ "description": "Manage and monitor asynchronous tasks such as data import/export. Support task progress tracking and notification.",
6
+ "description.zh-CN": "管理和监控数据导入导出等异步任务。支持任务进度跟踪和通知。",
7
+ "version": "1.5.0-beta.20",
8
+ "main": "dist/server/index.js",
9
+ "peerDependencies": {
10
+ "@nocobase/client": "1.x",
11
+ "@nocobase/plugin-error-handler": "1.x",
12
+ "@nocobase/server": "1.x",
13
+ "@nocobase/test": "1.x"
14
+ },
15
+ "gitHead": "211221dfc5014bf44543a080b56c3337396940ec"
16
+ }
package/server.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './dist/server';
2
+ export { default } from './dist/server';
package/server.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/server/index.js');