@simplysm/core-node 13.0.69 → 13.0.70
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 +17 -354
- package/dist/features/fs-watcher.d.ts +21 -21
- package/dist/features/fs-watcher.d.ts.map +1 -1
- package/dist/features/fs-watcher.js +9 -9
- package/dist/utils/fs.d.ts +96 -96
- package/dist/utils/path.d.ts +22 -22
- package/dist/utils/path.js +1 -1
- package/dist/utils/path.js.map +1 -1
- package/dist/worker/create-worker.d.ts +3 -3
- package/dist/worker/create-worker.js +3 -3
- package/dist/worker/create-worker.js.map +1 -1
- package/dist/worker/types.d.ts +14 -14
- package/dist/worker/worker.d.ts +5 -5
- package/dist/worker/worker.js +12 -12
- package/dist/worker/worker.js.map +1 -1
- package/package.json +6 -5
- package/src/features/fs-watcher.ts +38 -38
- package/src/utils/fs.ts +108 -108
- package/src/utils/path.ts +26 -26
- package/src/worker/create-worker.ts +10 -10
- package/src/worker/types.ts +14 -14
- package/src/worker/worker.ts +29 -29
- package/tests/utils/fs-watcher.spec.ts +339 -0
- package/tests/utils/fs.spec.ts +755 -0
- package/tests/utils/path.spec.ts +192 -0
- package/tests/worker/fixtures/test-worker.ts +35 -0
- package/tests/worker/sd-worker.spec.ts +189 -0
package/dist/utils/fs.d.ts
CHANGED
|
@@ -2,196 +2,196 @@ import fs from "fs";
|
|
|
2
2
|
import { type GlobOptions } from "glob";
|
|
3
3
|
import "@simplysm/core-common";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param targetPath -
|
|
5
|
+
* Checks if a file or directory exists (synchronous).
|
|
6
|
+
* @param targetPath - Path to check
|
|
7
7
|
*/
|
|
8
8
|
export declare function fsExistsSync(targetPath: string): boolean;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param targetPath -
|
|
10
|
+
* Checks if a file or directory exists (asynchronous).
|
|
11
|
+
* @param targetPath - Path to check
|
|
12
12
|
*/
|
|
13
13
|
export declare function fsExists(targetPath: string): Promise<boolean>;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
* @param targetPath -
|
|
15
|
+
* Creates a directory (recursive).
|
|
16
|
+
* @param targetPath - Directory path to create
|
|
17
17
|
*/
|
|
18
18
|
export declare function fsMkdirSync(targetPath: string): void;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @param targetPath -
|
|
20
|
+
* Creates a directory (recursive, asynchronous).
|
|
21
|
+
* @param targetPath - Directory path to create
|
|
22
22
|
*/
|
|
23
23
|
export declare function fsMkdir(targetPath: string): Promise<void>;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @param targetPath -
|
|
27
|
-
* @remarks
|
|
25
|
+
* Deletes a file or directory.
|
|
26
|
+
* @param targetPath - Path to delete
|
|
27
|
+
* @remarks The synchronous version fails immediately without retries. Use fsRm for cases with potential transient errors like file locks.
|
|
28
28
|
*/
|
|
29
29
|
export declare function fsRmSync(targetPath: string): void;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @param targetPath -
|
|
33
|
-
* @remarks
|
|
31
|
+
* Deletes a file or directory (asynchronous).
|
|
32
|
+
* @param targetPath - Path to delete
|
|
33
|
+
* @remarks The asynchronous version retries up to 6 times (500ms interval) for transient errors like file locks.
|
|
34
34
|
*/
|
|
35
35
|
export declare function fsRm(targetPath: string): Promise<void>;
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Copies a file or directory.
|
|
38
38
|
*
|
|
39
|
-
* sourcePath
|
|
39
|
+
* If sourcePath does not exist, no action is performed and the function returns.
|
|
40
40
|
*
|
|
41
|
-
* @param sourcePath
|
|
42
|
-
* @param targetPath
|
|
43
|
-
* @param filter
|
|
44
|
-
*
|
|
45
|
-
* true
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
41
|
+
* @param sourcePath Path of the source to copy
|
|
42
|
+
* @param targetPath Destination path for the copy
|
|
43
|
+
* @param filter A filter function that determines whether to copy.
|
|
44
|
+
* The **absolute path** of each file/directory is passed.
|
|
45
|
+
* Returns true to copy, false to exclude.
|
|
46
|
+
* **Note**: The top-level sourcePath is not subject to filtering;
|
|
47
|
+
* the filter function is applied recursively to all children (direct and indirect).
|
|
48
|
+
* Returning false for a directory skips that directory and all its contents.
|
|
49
49
|
*/
|
|
50
50
|
export declare function fsCopySync(sourcePath: string, targetPath: string, filter?: (absolutePath: string) => boolean): void;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
52
|
+
* Copies a file or directory (asynchronous).
|
|
53
53
|
*
|
|
54
|
-
* sourcePath
|
|
54
|
+
* If sourcePath does not exist, no action is performed and the function returns.
|
|
55
55
|
*
|
|
56
|
-
* @param sourcePath
|
|
57
|
-
* @param targetPath
|
|
58
|
-
* @param filter
|
|
59
|
-
*
|
|
60
|
-
* true
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
56
|
+
* @param sourcePath Path of the source to copy
|
|
57
|
+
* @param targetPath Destination path for the copy
|
|
58
|
+
* @param filter A filter function that determines whether to copy.
|
|
59
|
+
* The **absolute path** of each file/directory is passed.
|
|
60
|
+
* Returns true to copy, false to exclude.
|
|
61
|
+
* **Note**: The top-level sourcePath is not subject to filtering;
|
|
62
|
+
* the filter function is applied recursively to all children (direct and indirect).
|
|
63
|
+
* Returning false for a directory skips that directory and all its contents.
|
|
64
64
|
*/
|
|
65
65
|
export declare function fsCopy(sourcePath: string, targetPath: string, filter?: (absolutePath: string) => boolean): Promise<void>;
|
|
66
66
|
/**
|
|
67
|
-
*
|
|
68
|
-
* @param targetPath -
|
|
67
|
+
* Reads a file as a UTF-8 string.
|
|
68
|
+
* @param targetPath - Path of the file to read
|
|
69
69
|
*/
|
|
70
70
|
export declare function fsReadSync(targetPath: string): string;
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
73
|
-
* @param targetPath -
|
|
72
|
+
* Reads a file as a UTF-8 string (asynchronous).
|
|
73
|
+
* @param targetPath - Path of the file to read
|
|
74
74
|
*/
|
|
75
75
|
export declare function fsRead(targetPath: string): Promise<string>;
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
78
|
-
* @param targetPath -
|
|
77
|
+
* Reads a file as a Buffer.
|
|
78
|
+
* @param targetPath - Path of the file to read
|
|
79
79
|
*/
|
|
80
80
|
export declare function fsReadBufferSync(targetPath: string): Buffer;
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @param targetPath -
|
|
82
|
+
* Reads a file as a Buffer (asynchronous).
|
|
83
|
+
* @param targetPath - Path of the file to read
|
|
84
84
|
*/
|
|
85
85
|
export declare function fsReadBuffer(targetPath: string): Promise<Buffer>;
|
|
86
86
|
/**
|
|
87
|
-
* JSON
|
|
88
|
-
* @param targetPath -
|
|
87
|
+
* Reads a JSON file (using JsonConvert).
|
|
88
|
+
* @param targetPath - Path of the JSON file to read
|
|
89
89
|
*/
|
|
90
90
|
export declare function fsReadJsonSync<TData = unknown>(targetPath: string): TData;
|
|
91
91
|
/**
|
|
92
|
-
* JSON
|
|
93
|
-
* @param targetPath -
|
|
92
|
+
* Reads a JSON file (using JsonConvert, asynchronous).
|
|
93
|
+
* @param targetPath - Path of the JSON file to read
|
|
94
94
|
*/
|
|
95
95
|
export declare function fsReadJson<TData = unknown>(targetPath: string): Promise<TData>;
|
|
96
96
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param targetPath -
|
|
99
|
-
* @param data -
|
|
97
|
+
* Writes data to a file (auto-creates parent directories).
|
|
98
|
+
* @param targetPath - Path of the file to write
|
|
99
|
+
* @param data - Data to write (string or binary)
|
|
100
100
|
*/
|
|
101
101
|
export declare function fsWriteSync(targetPath: string, data: string | Uint8Array): void;
|
|
102
102
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @param targetPath -
|
|
105
|
-
* @param data -
|
|
103
|
+
* Writes data to a file (auto-creates parent directories, asynchronous).
|
|
104
|
+
* @param targetPath - Path of the file to write
|
|
105
|
+
* @param data - Data to write (string or binary)
|
|
106
106
|
*/
|
|
107
107
|
export declare function fsWrite(targetPath: string, data: string | Uint8Array): Promise<void>;
|
|
108
108
|
/**
|
|
109
|
-
* JSON
|
|
110
|
-
* @param targetPath -
|
|
111
|
-
* @param data -
|
|
112
|
-
* @param options - JSON
|
|
109
|
+
* Writes data to a JSON file (using JsonConvert).
|
|
110
|
+
* @param targetPath - Path of the JSON file to write
|
|
111
|
+
* @param data - Data to write
|
|
112
|
+
* @param options - JSON serialization options
|
|
113
113
|
*/
|
|
114
114
|
export declare function fsWriteJsonSync(targetPath: string, data: unknown, options?: {
|
|
115
115
|
replacer?: (this: unknown, key: string | undefined, value: unknown) => unknown;
|
|
116
116
|
space?: string | number;
|
|
117
117
|
}): void;
|
|
118
118
|
/**
|
|
119
|
-
* JSON
|
|
120
|
-
* @param targetPath -
|
|
121
|
-
* @param data -
|
|
122
|
-
* @param options - JSON
|
|
119
|
+
* Writes data to a JSON file (using JsonConvert, asynchronous).
|
|
120
|
+
* @param targetPath - Path of the JSON file to write
|
|
121
|
+
* @param data - Data to write
|
|
122
|
+
* @param options - JSON serialization options
|
|
123
123
|
*/
|
|
124
124
|
export declare function fsWriteJson(targetPath: string, data: unknown, options?: {
|
|
125
125
|
replacer?: (this: unknown, key: string | undefined, value: unknown) => unknown;
|
|
126
126
|
space?: string | number;
|
|
127
127
|
}): Promise<void>;
|
|
128
128
|
/**
|
|
129
|
-
*
|
|
130
|
-
* @param targetPath -
|
|
129
|
+
* Reads the contents of a directory.
|
|
130
|
+
* @param targetPath - Path of the directory to read
|
|
131
131
|
*/
|
|
132
132
|
export declare function fsReaddirSync(targetPath: string): string[];
|
|
133
133
|
/**
|
|
134
|
-
*
|
|
135
|
-
* @param targetPath -
|
|
134
|
+
* Reads the contents of a directory (asynchronous).
|
|
135
|
+
* @param targetPath - Path of the directory to read
|
|
136
136
|
*/
|
|
137
137
|
export declare function fsReaddir(targetPath: string): Promise<string[]>;
|
|
138
138
|
/**
|
|
139
|
-
*
|
|
140
|
-
* @param targetPath -
|
|
139
|
+
* Gets file/directory information (follows symbolic links).
|
|
140
|
+
* @param targetPath - Path to query information for
|
|
141
141
|
*/
|
|
142
142
|
export declare function fsStatSync(targetPath: string): fs.Stats;
|
|
143
143
|
/**
|
|
144
|
-
*
|
|
145
|
-
* @param targetPath -
|
|
144
|
+
* Gets file/directory information (follows symbolic links, asynchronous).
|
|
145
|
+
* @param targetPath - Path to query information for
|
|
146
146
|
*/
|
|
147
147
|
export declare function fsStat(targetPath: string): Promise<fs.Stats>;
|
|
148
148
|
/**
|
|
149
|
-
*
|
|
150
|
-
* @param targetPath -
|
|
149
|
+
* Gets file/directory information (does not follow symbolic links).
|
|
150
|
+
* @param targetPath - Path to query information for
|
|
151
151
|
*/
|
|
152
152
|
export declare function fsLstatSync(targetPath: string): fs.Stats;
|
|
153
153
|
/**
|
|
154
|
-
*
|
|
155
|
-
* @param targetPath -
|
|
154
|
+
* Gets file/directory information (does not follow symbolic links, asynchronous).
|
|
155
|
+
* @param targetPath - Path to query information for
|
|
156
156
|
*/
|
|
157
157
|
export declare function fsLstat(targetPath: string): Promise<fs.Stats>;
|
|
158
158
|
/**
|
|
159
|
-
*
|
|
160
|
-
* @param pattern -
|
|
161
|
-
* @param options - glob
|
|
162
|
-
* @returns
|
|
159
|
+
* Searches for files using a glob pattern.
|
|
160
|
+
* @param pattern - Glob pattern (e.g., "**\/*.ts")
|
|
161
|
+
* @param options - glob options
|
|
162
|
+
* @returns Array of absolute paths for matched files
|
|
163
163
|
*/
|
|
164
164
|
export declare function fsGlobSync(pattern: string, options?: GlobOptions): string[];
|
|
165
165
|
/**
|
|
166
|
-
*
|
|
167
|
-
* @param pattern -
|
|
168
|
-
* @param options - glob
|
|
169
|
-
* @returns
|
|
166
|
+
* Searches for files using a glob pattern (asynchronous).
|
|
167
|
+
* @param pattern - Glob pattern (e.g., "**\/*.ts")
|
|
168
|
+
* @param options - glob options
|
|
169
|
+
* @returns Array of absolute paths for matched files
|
|
170
170
|
*/
|
|
171
171
|
export declare function fsGlob(pattern: string, options?: GlobOptions): Promise<string[]>;
|
|
172
172
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
173
|
+
* Recursively searches and deletes empty directories under a specified directory.
|
|
174
|
+
* If all child directories are deleted and a parent becomes empty, it will also be deleted.
|
|
175
175
|
*/
|
|
176
176
|
export declare function fsClearEmptyDirectory(dirPath: string): Promise<void>;
|
|
177
177
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* @param childGlob -
|
|
181
|
-
* @param fromPath -
|
|
182
|
-
* @param rootPath -
|
|
183
|
-
*
|
|
184
|
-
*
|
|
178
|
+
* Searches for files matching a glob pattern by traversing parent directories from a start path towards the root.
|
|
179
|
+
* Collects all file paths matching the childGlob pattern in each directory.
|
|
180
|
+
* @param childGlob - Glob pattern to search for in each directory
|
|
181
|
+
* @param fromPath - Path to start searching from
|
|
182
|
+
* @param rootPath - Path to stop searching at (if not specified, searches to filesystem root).
|
|
183
|
+
* **Note**: fromPath must be a child path of rootPath.
|
|
184
|
+
* Otherwise, searches to the filesystem root.
|
|
185
185
|
*/
|
|
186
186
|
export declare function fsFindAllParentChildPathsSync(childGlob: string, fromPath: string, rootPath?: string): string[];
|
|
187
187
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* @param childGlob -
|
|
191
|
-
* @param fromPath -
|
|
192
|
-
* @param rootPath -
|
|
193
|
-
*
|
|
194
|
-
*
|
|
188
|
+
* Searches for files matching a glob pattern by traversing parent directories from a start path towards the root (asynchronous).
|
|
189
|
+
* Collects all file paths matching the childGlob pattern in each directory.
|
|
190
|
+
* @param childGlob - Glob pattern to search for in each directory
|
|
191
|
+
* @param fromPath - Path to start searching from
|
|
192
|
+
* @param rootPath - Path to stop searching at (if not specified, searches to filesystem root).
|
|
193
|
+
* **Note**: fromPath must be a child path of rootPath.
|
|
194
|
+
* Otherwise, searches to the filesystem root.
|
|
195
195
|
*/
|
|
196
196
|
export declare function fsFindAllParentChildPaths(childGlob: string, fromPath: string, rootPath?: string): Promise<string[]>;
|
|
197
197
|
//# sourceMappingURL=fs.d.ts.map
|
package/dist/utils/path.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
declare const NORM: unique symbol;
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* pathNorm()
|
|
3
|
+
* Brand type representing a normalized path.
|
|
4
|
+
* Can only be created through pathNorm().
|
|
5
5
|
*/
|
|
6
6
|
export type NormPath = string & {
|
|
7
7
|
[NORM]: never;
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Converts to POSIX-style path (backslash → forward slash).
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* pathPosix("C:\\Users\\test"); // "C:/Users/test"
|
|
@@ -15,17 +15,17 @@ export type NormPath = string & {
|
|
|
15
15
|
*/
|
|
16
16
|
export declare function pathPosix(...args: string[]): string;
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* Changes the directory of a file path.
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
21
21
|
* pathChangeFileDirectory("/a/b/c.txt", "/a", "/x");
|
|
22
22
|
* // → "/x/b/c.txt"
|
|
23
23
|
*
|
|
24
|
-
* @throws
|
|
24
|
+
* @throws Error if the file is not inside fromDirectory
|
|
25
25
|
*/
|
|
26
26
|
export declare function pathChangeFileDirectory(filePath: string, fromDirectory: string, toDirectory: string): string;
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Returns the filename (basename) without extension.
|
|
29
29
|
*
|
|
30
30
|
* @example
|
|
31
31
|
* pathBasenameWithoutExt("file.txt"); // "file"
|
|
@@ -33,37 +33,37 @@ export declare function pathChangeFileDirectory(filePath: string, fromDirectory:
|
|
|
33
33
|
*/
|
|
34
34
|
export declare function pathBasenameWithoutExt(filePath: string): string;
|
|
35
35
|
/**
|
|
36
|
-
* childPath
|
|
37
|
-
*
|
|
36
|
+
* Checks if childPath is a child path of parentPath.
|
|
37
|
+
* Returns false if the paths are the same.
|
|
38
38
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
39
|
+
* Paths are internally normalized using `pathNorm()` and compared using
|
|
40
|
+
* platform-specific path separators (Windows: `\`, Unix: `/`).
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* pathIsChildPath("/a/b/c", "/a/b"); // true
|
|
44
44
|
* pathIsChildPath("/a/b", "/a/b/c"); // false
|
|
45
|
-
* pathIsChildPath("/a/b", "/a/b"); // false (
|
|
45
|
+
* pathIsChildPath("/a/b", "/a/b"); // false (same path)
|
|
46
46
|
*/
|
|
47
47
|
export declare function pathIsChildPath(childPath: string, parentPath: string): boolean;
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
49
|
+
* Normalizes the path and returns it as NormPath.
|
|
50
|
+
* Converts to absolute path and normalizes using platform-specific separators.
|
|
51
51
|
*
|
|
52
52
|
* @example
|
|
53
53
|
* pathNorm("/some/path"); // NormPath
|
|
54
|
-
* pathNorm("relative", "path"); // NormPath (
|
|
54
|
+
* pathNorm("relative", "path"); // NormPath (converted to absolute path)
|
|
55
55
|
*/
|
|
56
56
|
export declare function pathNorm(...paths: string[]): NormPath;
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
58
|
+
* Filters files based on a list of target paths.
|
|
59
|
+
* Includes files that match or are children of a target path.
|
|
60
60
|
*
|
|
61
|
-
* @param files -
|
|
62
|
-
*
|
|
63
|
-
* cwd
|
|
64
|
-
* @param targets -
|
|
65
|
-
* @param cwd -
|
|
66
|
-
* @returns targets
|
|
61
|
+
* @param files - File paths to filter.
|
|
62
|
+
* **Note**: Must be absolute paths under cwd.
|
|
63
|
+
* Paths outside cwd are converted to relative paths (../) for processing.
|
|
64
|
+
* @param targets - Target paths (relative to cwd, POSIX style recommended)
|
|
65
|
+
* @param cwd - Current working directory (absolute path)
|
|
66
|
+
* @returns If targets is empty, returns files as-is; otherwise returns only files under target paths
|
|
67
67
|
*
|
|
68
68
|
* @example
|
|
69
69
|
* const files = ["/proj/src/a.ts", "/proj/src/b.ts", "/proj/tests/c.ts"];
|
package/dist/utils/path.js
CHANGED
|
@@ -10,7 +10,7 @@ function pathChangeFileDirectory(filePath, fromDirectory, toDirectory) {
|
|
|
10
10
|
return toDirectory;
|
|
11
11
|
}
|
|
12
12
|
if (!pathIsChildPath(filePath, fromDirectory)) {
|
|
13
|
-
throw new ArgumentError(`'${filePath}'
|
|
13
|
+
throw new ArgumentError(`'${filePath}' is not inside ${fromDirectory}.`, {
|
|
14
14
|
filePath,
|
|
15
15
|
fromDirectory
|
|
16
16
|
});
|
package/dist/utils/path.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/path.ts"],
|
|
4
|
-
"mappings": "AAAA,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAI9B,MAAM,OAAO,uBAAO,UAAU;AAqBvB,SAAS,aAAa,MAAwB;AACnD,QAAM,eAAe,KAAK,KAAK,GAAG,IAAI;AACtC,SAAO,aAAa,QAAQ,OAAO,GAAG;AACxC;AAWO,SAAS,wBACd,UACA,eACA,aACQ;AACR,MAAI,aAAa,eAAe;AAC9B,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,gBAAgB,UAAU,aAAa,GAAG;AAC7C,UAAM,IAAI,cAAc,IAAI,QAAQ,
|
|
4
|
+
"mappings": "AAAA,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAI9B,MAAM,OAAO,uBAAO,UAAU;AAqBvB,SAAS,aAAa,MAAwB;AACnD,QAAM,eAAe,KAAK,KAAK,GAAG,IAAI;AACtC,SAAO,aAAa,QAAQ,OAAO,GAAG;AACxC;AAWO,SAAS,wBACd,UACA,eACA,aACQ;AACR,MAAI,aAAa,eAAe;AAC9B,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,gBAAgB,UAAU,aAAa,GAAG;AAC7C,UAAM,IAAI,cAAc,IAAI,QAAQ,mBAAmB,aAAa,KAAK;AAAA,MACvE;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,KAAK,QAAQ,aAAa,KAAK,SAAS,eAAe,QAAQ,CAAC;AACzE;AASO,SAAS,uBAAuB,UAA0B;AAC/D,SAAO,KAAK,SAAS,UAAU,KAAK,QAAQ,QAAQ,CAAC;AACvD;AAcO,SAAS,gBAAgB,WAAmB,YAA6B;AAC9E,QAAM,kBAAkB,SAAS,SAAS;AAC1C,QAAM,mBAAmB,SAAS,UAAU;AAG5C,MAAI,oBAAoB,kBAAkB;AACxC,WAAO;AAAA,EACT;AAGA,QAAM,gBAAgB,iBAAiB,SAAS,KAAK,GAAG,IACpD,mBACA,mBAAmB,KAAK;AAE5B,SAAO,gBAAgB,WAAW,aAAa;AACjD;AAUO,SAAS,YAAY,OAA2B;AACrD,SAAO,KAAK,QAAQ,GAAG,KAAK;AAC9B;AAkBO,SAAS,oBAAoB,OAAiB,SAAmB,KAAuB;AAC7F,MAAI,QAAQ,WAAW,EAAG,QAAO;AACjC,QAAM,oBAAoB,QAAQ,IAAI,CAAC,MAAM,UAAU,CAAC,CAAC;AACzD,SAAO,MAAM,OAAO,CAAC,SAAS;AAC5B,UAAM,eAAe,UAAU,KAAK,SAAS,KAAK,IAAI,CAAC;AACvD,WAAO,kBAAkB;AAAA,MACvB,CAAC,WAAW,iBAAiB,UAAU,aAAa,WAAW,SAAS,GAAG;AAAA,IAC7E;AAAA,EACF,CAAC;AACH;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Worker factory for use in worker threads.
|
|
3
3
|
*
|
|
4
4
|
* @example
|
|
5
|
-
* //
|
|
5
|
+
* // Worker without events
|
|
6
6
|
* export default createWorker({
|
|
7
7
|
* add: (a: number, b: number) => a + b,
|
|
8
8
|
* });
|
|
9
9
|
*
|
|
10
|
-
* //
|
|
10
|
+
* // Worker with events
|
|
11
11
|
* interface MyEvents { progress: number; }
|
|
12
12
|
* const methods = {
|
|
13
13
|
* calc: (x: number) => { sender.send("progress", 50); return x * 2; },
|
|
@@ -2,7 +2,7 @@ import { parentPort } from "worker_threads";
|
|
|
2
2
|
import { SdError, transferableDecode, transferableEncode } from "@simplysm/core-common";
|
|
3
3
|
function createWorker(methods) {
|
|
4
4
|
if (parentPort === null) {
|
|
5
|
-
throw new SdError("
|
|
5
|
+
throw new SdError("This script must be executed in a worker thread (parentPort required).");
|
|
6
6
|
}
|
|
7
7
|
const port = parentPort;
|
|
8
8
|
process.stdout.write = (chunk, encodingOrCallback, callback) => {
|
|
@@ -28,7 +28,7 @@ function createWorker(methods) {
|
|
|
28
28
|
const errorResponse = {
|
|
29
29
|
type: "error",
|
|
30
30
|
request: { id: "unknown", method: "unknown", params: [] },
|
|
31
|
-
body: new SdError(
|
|
31
|
+
body: new SdError(`Invalid worker request format: ${decodedStr}`)
|
|
32
32
|
};
|
|
33
33
|
const serialized = transferableEncode(errorResponse);
|
|
34
34
|
port.postMessage(serialized.result, serialized.transferList);
|
|
@@ -40,7 +40,7 @@ function createWorker(methods) {
|
|
|
40
40
|
const response = {
|
|
41
41
|
request,
|
|
42
42
|
type: "error",
|
|
43
|
-
body: new SdError(
|
|
43
|
+
body: new SdError(`Unknown method: ${request.method}`)
|
|
44
44
|
};
|
|
45
45
|
const serialized = transferableEncode(response);
|
|
46
46
|
port.postMessage(serialized.result, serialized.transferList);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/worker/create-worker.ts"],
|
|
4
|
-
"mappings": "AAAA,SAAS,kBAAkB;AAC3B,SAAS,SAAS,oBAAoB,0BAA0B;AAsBzD,SAAS,aAId,SAKA;AACA,MAAI,eAAe,MAAM;AACvB,UAAM,IAAI,QAAQ,
|
|
4
|
+
"mappings": "AAAA,SAAS,kBAAkB;AAC3B,SAAS,SAAS,oBAAoB,0BAA0B;AAsBzD,SAAS,aAId,SAKA;AACA,MAAI,eAAe,MAAM;AACvB,UAAM,IAAI,QAAQ,wEAAwE;AAAA,EAC5F;AAEA,QAAM,OAAO;AAIb,UAAQ,OAAO,QAAQ,CACrB,OACA,oBACA,aACY;AACZ,UAAM,OAAO,OAAO,UAAU,WAAW,QAAQ,IAAI,YAAY,EAAE,OAAO,KAAK;AAC/E,UAAM,WAA2B,EAAE,MAAM,OAAO,KAAK;AACrD,UAAM,aAAa,mBAAmB,QAAQ;AAC9C,SAAK,YAAY,WAAW,QAAQ,WAAW,YAAY;AAE3D,UAAM,KAAK,OAAO,uBAAuB,aAAa,qBAAqB;AAC3E,QAAI,IAAI;AACN,qBAAe,MAAM,GAAG,CAAC;AAAA,IAC3B;AAEA,WAAO;AAAA,EACT;AAEA,OAAK,GAAG,WAAW,OAAO,sBAA+B;AACvD,UAAM,UAAU,mBAAmB,iBAAiB;AAGpD,QACE,WAAW,QACX,OAAO,YAAY,YACnB,EAAE,QAAQ,YACV,EAAE,YAAY,YACd,EAAE,YAAY,UACd;AACA,UAAI;AACJ,UAAI;AACF,qBAAa,KAAK,UAAU,OAAO;AAAA,MACrC,QAAQ;AACN,qBAAa,OAAO,OAAO;AAAA,MAC7B;AACA,YAAM,gBAAgC;AAAA,QACpC,MAAM;AAAA,QACN,SAAS,EAAE,IAAI,WAAW,QAAQ,WAAW,QAAQ,CAAC,EAAE;AAAA,QACxD,MAAM,IAAI,QAAQ,kCAAkC,UAAU,EAAE;AAAA,MAClE;AACA,YAAM,aAAa,mBAAmB,aAAa;AACnD,WAAK,YAAY,WAAW,QAAQ,WAAW,YAAY;AAC3D;AAAA,IACF;AACA,UAAM,UAAU;AAEhB,UAAM,WAAW,QAAQ,QAAQ,MAAM;AAEvC,QAAI,YAAY,MAAM;AACpB,YAAM,WAA2B;AAAA,QAC/B;AAAA,QACA,MAAM;AAAA,QACN,MAAM,IAAI,QAAQ,mBAAmB,QAAQ,MAAM,EAAE;AAAA,MACvD;AAEA,YAAM,aAAa,mBAAmB,QAAQ;AAC9C,WAAK,YAAY,WAAW,QAAQ,WAAW,YAAY;AAC3D;AAAA,IACF;AAEA,QAAI;AACF,YAAM,SAAS,MAAM,SAAS,GAAG,QAAQ,MAAM;AAE/C,YAAM,WAA2B;AAAA,QAC/B;AAAA,QACA,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAEA,YAAM,aAAa,mBAAmB,QAAQ;AAC9C,WAAK,YAAY,WAAW,QAAQ,WAAW,YAAY;AAAA,IAC7D,SAAS,KAAK;AACZ,YAAM,WAA2B;AAAA,QAC/B;AAAA,QACA,MAAM;AAAA,QACN,MAAM,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAAA,MAC1D;AAEA,YAAM,aAAa,mBAAmB,QAAQ;AAC9C,WAAK,YAAY,WAAW,QAAQ,WAAW,YAAY;AAAA,IAC7D;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL,WAAW;AAAA,IACX,UAAU,CAAC;AAAA,IACX,KAAuC,OAAU,MAAmB;AAClE,YAAM,WAA2B;AAAA,QAC/B,MAAM;AAAA,QACN;AAAA,QACA,MAAM;AAAA,MACR;AAEA,YAAM,aAAa,mBAAmB,QAAQ;AAC9C,WAAK,YAAY,WAAW,QAAQ,WAAW,YAAY;AAAA,IAC7D;AAAA,EACF;AACF;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/worker/types.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* `Worker.create<typeof import("./worker")>()
|
|
2
|
+
* Type structure of the worker module returned by `createWorker()`.
|
|
3
|
+
* Used for type inference in `Worker.create<typeof import("./worker")>()`.
|
|
4
4
|
*
|
|
5
|
-
* @see createWorker -
|
|
6
|
-
* @see Worker.create -
|
|
5
|
+
* @see createWorker - Create worker module
|
|
6
|
+
* @see Worker.create - Create worker proxy
|
|
7
7
|
*/
|
|
8
8
|
export interface WorkerModule {
|
|
9
9
|
default: {
|
|
@@ -12,33 +12,33 @@ export interface WorkerModule {
|
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* Mapping type that wraps method return values in Promise.
|
|
16
|
+
* Worker methods operate based on postMessage and are always asynchronous,
|
|
17
|
+
* so synchronous method types are also converted to `Promise<Awaited<R>>`.
|
|
18
18
|
*/
|
|
19
19
|
export type PromisifyMethods<TMethods> = {
|
|
20
20
|
[K in keyof TMethods]: TMethods[K] extends (...args: infer P) => infer R ? (...args: P) => Promise<Awaited<R>> : never;
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
23
|
+
* Proxy type returned by Worker.create().
|
|
24
|
+
* Provides promisified methods + on() + terminate().
|
|
25
25
|
*/
|
|
26
26
|
export type WorkerProxy<TModule extends WorkerModule> = PromisifyMethods<TModule["default"]["__methods"]> & {
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Registers a worker event listener.
|
|
29
29
|
*/
|
|
30
30
|
on<K extends keyof TModule["default"]["__events"] & string>(event: K, listener: (data: TModule["default"]["__events"][K]) => void): void;
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* Unregisters a worker event listener.
|
|
33
33
|
*/
|
|
34
34
|
off<K extends keyof TModule["default"]["__events"] & string>(event: K, listener: (data: TModule["default"]["__events"][K]) => void): void;
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* Terminates the worker.
|
|
37
37
|
*/
|
|
38
38
|
terminate(): Promise<void>;
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
41
|
+
* Internal worker request message.
|
|
42
42
|
*/
|
|
43
43
|
export interface WorkerRequest {
|
|
44
44
|
id: string;
|
|
@@ -46,7 +46,7 @@ export interface WorkerRequest {
|
|
|
46
46
|
params: unknown[];
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
49
|
+
* Internal worker response message.
|
|
50
50
|
*/
|
|
51
51
|
export type WorkerResponse = {
|
|
52
52
|
request: WorkerRequest;
|
package/dist/worker/worker.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { WorkerOptions as WorkerRawOptions } from "worker_threads";
|
|
2
2
|
import type { WorkerModule, WorkerProxy } from "./types";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Type-safe Worker wrapper.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* // worker.ts
|
|
@@ -16,11 +16,11 @@ import type { WorkerModule, WorkerProxy } from "./types";
|
|
|
16
16
|
*/
|
|
17
17
|
export declare const Worker: {
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* Creates a type-safe Worker Proxy.
|
|
20
20
|
*
|
|
21
|
-
* @param filePath -
|
|
22
|
-
* @param opt - Worker
|
|
23
|
-
* @returns Proxy
|
|
21
|
+
* @param filePath - Worker file path (file:// URL or absolute path)
|
|
22
|
+
* @param opt - Worker options
|
|
23
|
+
* @returns Proxy object (supports direct method calls, on(), and terminate())
|
|
24
24
|
*/
|
|
25
25
|
create<TModule extends WorkerModule>(filePath: string, opt?: Omit<WorkerRawOptions, "stdout" | "stderr">): WorkerProxy<TModule>;
|
|
26
26
|
};
|