zelos-recad 0.1.0 → 0.2.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 +18 -5
- package/UPSTREAM-LICENSE +21 -21
- package/build-info.json +26 -0
- package/mcp/index.js +2 -1
- package/package.json +22 -10
- package/protocol/src/bundle.d.ts +7 -1
- package/protocol/src/contracts.d.ts +18 -6
- package/protocol/src/contracts.js +20 -7
- package/protocol/src/native-evidence.d.ts +20 -0
- package/service/app.js +4 -2
- package/service/options.d.ts +2 -1
- package/service/options.js +2 -1
- package/shared/build-identity.d.mts +6 -0
- package/shared/build-identity.mjs +63 -0
- package/shared/client.d.mts +1 -0
- package/shared/client.mjs +9 -0
- package/shared/command.d.mts +1 -0
- package/shared/command.mjs +22 -4
- package/shared/contracts/src/paths.d.ts +3 -0
- package/shared/contracts/src/paths.js +31 -0
- package/shared/mcp.d.mts +1 -0
- package/shared/service-storage/src/capacity.d.ts +36 -0
- package/shared/service-storage/src/capacity.js +71 -0
- package/shared/service-storage/src/consumption.d.ts +15 -0
- package/shared/service-storage/src/consumption.js +27 -0
- package/shared/service-storage/src/documents.d.ts +43 -0
- package/shared/service-storage/src/documents.js +210 -0
- package/shared/service-storage/src/downloads.d.ts +10 -0
- package/shared/service-storage/src/downloads.js +58 -0
- package/shared/service-storage/src/files.d.ts +18 -0
- package/shared/service-storage/src/files.js +118 -0
- package/shared/service-storage/src/footprint.d.ts +14 -0
- package/shared/service-storage/src/footprint.js +72 -0
- package/shared/service-storage/src/process-temp.d.ts +22 -0
- package/shared/service-storage/src/process-temp.js +181 -0
- package/shared/service-storage/src/quota.d.ts +30 -0
- package/shared/service-storage/src/quota.js +166 -0
- package/shared/service-storage/src/retention.d.ts +38 -0
- package/shared/service-storage/src/retention.js +148 -0
- package/shared/service-storage/src/staging.d.ts +15 -0
- package/shared/service-storage/src/staging.js +116 -0
- package/source/README.md +1 -3
- package/source/mcp/index.ts +2 -1
- package/source/package.json +4 -2
- package/source/protocol/src/contracts.ts +20 -7
- package/source/protocol/src/native-evidence.ts +21 -0
- package/source/rebuild.mjs +12 -4
- package/source/service/app.ts +6 -2
- package/source/service/options.ts +3 -2
- package/source/shared/build-identity.d.mts +6 -0
- package/source/shared/build-identity.mjs +63 -0
- package/source/shared/client.d.mts +1 -0
- package/source/shared/client.mjs +9 -0
- package/source/shared/command.d.mts +1 -0
- package/source/shared/command.mjs +22 -4
- package/source/shared/contracts/src/paths.ts +33 -0
- package/source/shared/mcp.d.mts +1 -0
- package/source/shared/service-storage/src/capacity.ts +101 -0
- package/source/shared/service-storage/src/consumption.ts +45 -0
- package/source/shared/service-storage/src/documents.ts +262 -0
- package/source/shared/service-storage/src/downloads.ts +62 -0
- package/source/shared/service-storage/src/files.ts +135 -0
- package/source/shared/service-storage/src/footprint.ts +78 -0
- package/source/shared/service-storage/src/process-temp.ts +205 -0
- package/source/shared/service-storage/src/quota.ts +198 -0
- package/source/shared/service-storage/src/retention.ts +180 -0
- package/source/shared/service-storage/src/staging.ts +106 -0
- package/source/src/application-objects.ts +2 -2
- package/source/src/assets.ts +15 -0
- package/source/src/block-clip.ts +1 -1
- package/source/src/block-metadata.ts +12 -5
- package/source/src/build-info.ts +15 -0
- package/source/src/cad-engine.ts +18 -8
- package/source/src/command.ts +2 -2
- package/source/src/constraints.ts +2 -2
- package/source/src/conversion.ts +2 -0
- package/source/src/dwg-fallback.ts +2 -0
- package/source/src/dwg.ts +7 -0
- package/source/src/dxf-export.ts +3 -3
- package/source/src/dxf.ts +7 -4
- package/source/src/native-evidence.ts +33 -1
- package/source/src/native-segmented-path.ts +22 -35
- package/source/src/point-dictionaries.ts +65 -0
- package/source/src/readback.ts +7 -6
- package/source/src/reader.ts +19 -10
- package/source/src/render-operation.ts +39 -7
- package/source/src/runner.ts +5 -4
- package/source/src/runtime/documents.ts +23 -142
- package/source/src/runtime/index.ts +71 -17
- package/source/src/runtime/jobs.ts +176 -153
- package/source/src/runtime/managed-worker.ts +37 -0
- package/source/src/runtime/storage-policy.ts +39 -0
- package/source/src/runtime/storage.ts +9 -44
- package/source/src/source-scene.ts +125 -23
- package/source/src/text/glyphs.ts +63 -0
- package/source/src/text/ink-bounds.ts +7 -36
- package/source/src/text/scene-geometry.ts +92 -0
- package/source/src/text/source-fonts.ts +31 -0
- package/source/src/vendor-object-data.ts +265 -0
- package/source/src/xref.ts +4 -3
- package/source/tsconfig.json +4 -2
- package/src/application-objects.js +2 -2
- package/src/assets.d.ts +9 -0
- package/src/assets.js +14 -0
- package/src/block-clip.js +1 -1
- package/src/block-metadata.js +11 -5
- package/src/build-info.d.ts +2 -0
- package/src/build-info.js +14 -0
- package/src/cad-engine.js +19 -8
- package/src/command.js +2 -2
- package/src/constraints.js +2 -2
- package/src/conversion.d.ts +2 -0
- package/src/conversion.js +2 -0
- package/src/dwg-fallback.d.ts +1 -0
- package/src/dwg-fallback.js +2 -0
- package/src/dwg.d.ts +3 -0
- package/src/dwg.js +7 -0
- package/src/dxf-export.js +3 -3
- package/src/dxf.d.ts +14 -1
- package/src/dxf.js +7 -3
- package/src/native-evidence.d.ts +33 -1
- package/src/native-evidence.js +33 -0
- package/src/native-segmented-path.js +1 -3
- package/src/point-dictionaries.d.ts +12 -0
- package/src/point-dictionaries.js +88 -0
- package/src/readback.d.ts +12 -4
- package/src/readback.js +7 -6
- package/src/reader.d.ts +12 -1
- package/src/reader.js +22 -11
- package/src/render-operation.js +36 -7
- package/src/runner.js +5 -4
- package/src/runtime/documents.d.ts +8 -20
- package/src/runtime/documents.js +17 -116
- package/src/runtime/index.d.ts +41 -4
- package/src/runtime/index.js +72 -18
- package/src/runtime/jobs.d.ts +21 -2
- package/src/runtime/jobs.js +120 -100
- package/src/runtime/managed-worker.d.ts +8 -0
- package/src/runtime/managed-worker.js +29 -0
- package/src/runtime/storage-policy.d.ts +25 -0
- package/src/runtime/storage-policy.js +29 -0
- package/src/runtime/storage.d.ts +1 -7
- package/src/runtime/storage.js +1 -38
- package/src/source-scene.d.ts +4 -2
- package/src/source-scene.js +136 -20
- package/src/text/glyphs.d.ts +12 -0
- package/src/text/glyphs.js +53 -0
- package/src/text/ink-bounds.d.ts +2 -1
- package/src/text/ink-bounds.js +6 -34
- package/src/text/scene-geometry.d.ts +21 -0
- package/src/text/scene-geometry.js +70 -0
- package/src/text/source-fonts.d.ts +7 -0
- package/src/text/source-fonts.js +30 -0
- package/src/vendor-object-data.d.ts +19 -0
- package/src/vendor-object-data.js +241 -0
- package/src/xref.js +3 -3
- package/vendor/libredwg/COPYING +674 -674
- package/vendor/libredwg/Dockerfile +2 -0
- package/vendor/libredwg/README.md +41 -28
- package/vendor/libredwg/build-local.sh +1 -1
- package/vendor/libredwg/libredwg-web.wasm +0 -0
- package/vendor/libredwg/manifest.json +5 -3
- package/vendor/libredwg/opaque-object-binding.patch +29 -0
- package/vendor/libredwg/rebuild.mjs +2 -1
- package/source/service/README.md +0 -152
- package/source/src/text/UPSTREAM.md +0 -26
package/src/runtime/jobs.js
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { JobRetention } from "../../shared/service-storage/src/retention.js";
|
|
2
|
+
import { StorageQuota } from "../../shared/service-storage/src/quota.js";
|
|
3
|
+
import { StagedDirectories } from "../../shared/service-storage/src/staging.js";
|
|
4
|
+
import { storeSource } from "../../shared/service-storage/src/documents.js";
|
|
5
|
+
import { consumptionHash } from "../../shared/service-storage/src/consumption.js";
|
|
6
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
7
|
+
import { mkdirSync, readFileSync, readdirSync, existsSync, writeFileSync, lstatSync } from 'node:fs';
|
|
3
8
|
import path from 'node:path';
|
|
4
9
|
import { CadError, requestSchema } from "../contracts.js";
|
|
5
|
-
import { writeJson, hashFile
|
|
10
|
+
import { writeJson, hashFile } from "../io.js";
|
|
6
11
|
import { runWorker } from "../runner.js";
|
|
7
12
|
import { CAD_INPUT_LIMITS } from 'zelos-recad/protocol';
|
|
8
13
|
import { serverResourceFingerprint } from "../base-fonts.js";
|
|
9
|
-
import { atomicJson, canonical, checkedId,
|
|
14
|
+
import { atomicJson, dataRoot, canonical, checkedId, jobIdPattern } from "./storage.js";
|
|
10
15
|
export function sourceTextPolicy(options) {
|
|
11
16
|
return options.missingText === 'strict'
|
|
12
17
|
? undefined
|
|
13
18
|
: (options.missingText ?? (options.baseFonts === false ? undefined : 'best-effort'));
|
|
14
19
|
}
|
|
20
|
+
function jobConsumption(job) {
|
|
21
|
+
return {
|
|
22
|
+
status: job.status,
|
|
23
|
+
sourceSha256: job.sourceSha256 ?? job.result?.sourceSha256,
|
|
24
|
+
artifacts: [...new Map([...(job.result?.artifacts ?? []), ...(job.diagnostics ?? [])].map(a => [a.name, a])).values()],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
15
27
|
export class JobQueue {
|
|
16
28
|
options;
|
|
29
|
+
quota;
|
|
30
|
+
staging;
|
|
17
31
|
jobs = new Map();
|
|
18
32
|
running = new Map();
|
|
33
|
+
retention;
|
|
19
34
|
closing = false;
|
|
20
35
|
cleanupTimer;
|
|
21
36
|
storageWarnings = [];
|
|
22
|
-
constructor(options) {
|
|
37
|
+
constructor(options, quota) {
|
|
23
38
|
this.options = options;
|
|
24
|
-
|
|
39
|
+
options.data = dataRoot(options.data);
|
|
40
|
+
this.quota = quota ?? new StorageQuota(options.data, options.maxStorageBytes, options.storageCapacity);
|
|
41
|
+
this.staging = new StagedDirectories(options.data, path.join(options.data, '.staging-jobs'), 'job.json', (directory, error) => {
|
|
42
|
+
const message = String(error);
|
|
43
|
+
if (!this.storageWarnings.some((w) => w.directory === directory && w.message === message))
|
|
44
|
+
this.storageWarnings.push({ directory, message });
|
|
45
|
+
});
|
|
46
|
+
this.staging.cleanup();
|
|
25
47
|
for (const name of ['.retired', '.cancelled', '.owners']) {
|
|
26
48
|
const directory = path.join(options.data, name);
|
|
27
49
|
mkdirSync(directory, { recursive: true });
|
|
@@ -38,6 +60,8 @@ export class JobQueue {
|
|
|
38
60
|
});
|
|
39
61
|
continue;
|
|
40
62
|
}
|
|
63
|
+
if (existsSync(this.marker('.retired', directory.name)))
|
|
64
|
+
continue;
|
|
41
65
|
const file = path.join(options.data, directory.name, 'job.json');
|
|
42
66
|
if (!existsSync(file)) {
|
|
43
67
|
this.storageWarnings.push({
|
|
@@ -63,15 +87,6 @@ export class JobQueue {
|
|
|
63
87
|
this.storageWarnings.push({ directory: directory.name, message: String(error) });
|
|
64
88
|
continue;
|
|
65
89
|
}
|
|
66
|
-
if (existsSync(this.marker('.retired', job.id))) {
|
|
67
|
-
try {
|
|
68
|
-
removeJobDirectory(options.data, job.id);
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
this.storageWarnings.push({ directory: job.id, message: String(error) });
|
|
72
|
-
}
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
90
|
if (['queued', 'running'].includes(job.status)) {
|
|
76
91
|
job.status = 'interrupted';
|
|
77
92
|
job.error = { code: 'SERVICE_RESTARTED', message: '服务重启中断了任务,请重新提交' };
|
|
@@ -93,13 +108,36 @@ export class JobQueue {
|
|
|
93
108
|
}
|
|
94
109
|
atomicJson(file, job);
|
|
95
110
|
}
|
|
111
|
+
if (!job.consumed && job.consumedAt && job.status === 'succeeded' && Number.isFinite(Date.parse(job.consumedAt)))
|
|
112
|
+
job.consumed = { at: job.consumedAt, sha256: consumptionHash(jobConsumption(job)) };
|
|
96
113
|
this.jobs.set(job.id, job);
|
|
97
114
|
}
|
|
115
|
+
this.retention = new JobRetention({
|
|
116
|
+
data: options.data,
|
|
117
|
+
options,
|
|
118
|
+
jobs: this.jobs,
|
|
119
|
+
running: (id) => this.running.has(id),
|
|
120
|
+
consumption: jobConsumption,
|
|
121
|
+
receipt: (job, now) => ({
|
|
122
|
+
id: job.id,
|
|
123
|
+
owner: job.owner,
|
|
124
|
+
fingerprint: job.fingerprint,
|
|
125
|
+
status: job.status,
|
|
126
|
+
endedAt: job.endedAt,
|
|
127
|
+
cleanedAt: new Date(now).toISOString(),
|
|
128
|
+
sourceSha256: job.result?.sourceSha256,
|
|
129
|
+
error: job.error,
|
|
130
|
+
}),
|
|
131
|
+
warn: (directory, error) => {
|
|
132
|
+
const message = String(error);
|
|
133
|
+
if (!this.storageWarnings.some((w) => w.directory === directory && w.message === message))
|
|
134
|
+
this.storageWarnings.push({ directory, message });
|
|
135
|
+
},
|
|
136
|
+
temporaryWorkspaces: (job) => ['', 'fallback-convert', 'fallback-read'].map((name) => path.join(options.data, job.id, name)),
|
|
137
|
+
});
|
|
98
138
|
this.cleanup();
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.cleanupTimer.unref();
|
|
102
|
-
}
|
|
139
|
+
this.cleanupTimer = setInterval(() => this.cleanup(), options.cleanupIntervalMs ?? 60000);
|
|
140
|
+
this.cleanupTimer.unref();
|
|
103
141
|
}
|
|
104
142
|
marker(kind, id) {
|
|
105
143
|
return path.join(this.options.data, kind, checkedId(id) + '.json');
|
|
@@ -133,20 +171,27 @@ export class JobQueue {
|
|
|
133
171
|
bundled: this.options.baseFonts !== false,
|
|
134
172
|
missingText: sourceTextPolicy(this.options),
|
|
135
173
|
});
|
|
136
|
-
const
|
|
174
|
+
const sourceSha256 = Buffer.isBuffer(bytes)
|
|
175
|
+
? createHash('sha256').update(bytes).digest('hex')
|
|
176
|
+
: bytes.sha256;
|
|
177
|
+
const fingerprint = createHash('sha256')
|
|
178
|
+
.update(JSON.stringify(canonical({
|
|
137
179
|
fileName,
|
|
138
|
-
source:
|
|
180
|
+
source: sourceSha256,
|
|
139
181
|
request,
|
|
140
182
|
owner,
|
|
141
183
|
resources: resources
|
|
142
184
|
? {
|
|
143
185
|
manifest: resources.manifest,
|
|
144
|
-
files: [...resources.files]
|
|
186
|
+
files: [...resources.files]
|
|
187
|
+
.map(([name, data]) => [name, createHash('sha256').update(data).digest('hex')])
|
|
188
|
+
.sort(),
|
|
145
189
|
}
|
|
146
190
|
: {
|
|
147
191
|
serverResourceFingerprint: serverFingerprint,
|
|
148
192
|
},
|
|
149
|
-
})))
|
|
193
|
+
})))
|
|
194
|
+
.digest('hex');
|
|
150
195
|
const old = this.get(id);
|
|
151
196
|
if (old) {
|
|
152
197
|
if (old.fingerprint !== fingerprint)
|
|
@@ -159,44 +204,45 @@ export class JobQueue {
|
|
|
159
204
|
if ([...this.jobs.values()].filter((j) => j.status === 'queued').length >= 20)
|
|
160
205
|
throw new CadError('QUEUE_FULL', '待处理任务已满');
|
|
161
206
|
this.cleanup();
|
|
162
|
-
if (this.options.maxStorageBytes &&
|
|
163
|
-
this.storageBytes() +
|
|
164
|
-
byteLength +
|
|
165
|
-
[...(resources?.files.values() ?? [])].reduce((sum, b) => sum + b.length, 0) >
|
|
166
|
-
this.options.maxStorageBytes)
|
|
167
|
-
throw new CadError('STORAGE_FULL', 'CAD 服务存储容量不足,活跃任务和下载保护期成果不会自动删除');
|
|
168
207
|
if (existsSync(directory))
|
|
169
208
|
throw new CadError('JOB_STORAGE_CONFLICT', '任务目录已存在但没有有效提交记录,请检查服务存储诊断');
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
mkdirSync(
|
|
177
|
-
|
|
209
|
+
const stage = this.staging.begin(id);
|
|
210
|
+
try {
|
|
211
|
+
if (resources) {
|
|
212
|
+
this.quota.require([...resources.files.values()].reduce((sum, b) => sum + b.length, 0) +
|
|
213
|
+
Buffer.byteLength(JSON.stringify(resources.manifest, null, 2) + '\n'));
|
|
214
|
+
const resourceRoot = path.join(directory, 'resources');
|
|
215
|
+
mkdirSync(resourceRoot);
|
|
216
|
+
for (const [name, bytes] of resources.files) {
|
|
217
|
+
const target = path.join(resourceRoot, name);
|
|
218
|
+
mkdirSync(path.dirname(target), { recursive: true });
|
|
219
|
+
writeFileSync(target, bytes, { flag: 'wx', flush: true });
|
|
220
|
+
}
|
|
221
|
+
writeJson(path.join(resourceRoot, 'manifest.json'), resources.manifest);
|
|
178
222
|
}
|
|
179
|
-
|
|
223
|
+
const sourcePath = path.join(directory, 'source' + path.extname(fileName).toLowerCase());
|
|
224
|
+
storeSource(bytes, sourcePath, this.quota);
|
|
225
|
+
const job = {
|
|
226
|
+
id,
|
|
227
|
+
status: 'queued',
|
|
228
|
+
fileName,
|
|
229
|
+
request,
|
|
230
|
+
createdAt: new Date().toISOString(),
|
|
231
|
+
fingerprint,
|
|
232
|
+
sourceSha256,
|
|
233
|
+
serverResourceFingerprint: serverFingerprint,
|
|
234
|
+
...(owner ? { owner } : {}),
|
|
235
|
+
};
|
|
236
|
+
this.quota.require(Buffer.byteLength(JSON.stringify(job, null, 2) + '\n'));
|
|
237
|
+
atomicJson(path.join(directory, 'job.json'), job);
|
|
238
|
+
stage.commit();
|
|
239
|
+
this.jobs.set(id, job);
|
|
240
|
+
queueMicrotask(() => this.pump());
|
|
241
|
+
return job;
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
stage.close();
|
|
180
245
|
}
|
|
181
|
-
const sourcePath = path.join(directory, 'source' + path.extname(fileName).toLowerCase());
|
|
182
|
-
if (Buffer.isBuffer(bytes))
|
|
183
|
-
writeFileSync(sourcePath, bytes, { flag: 'wx' });
|
|
184
|
-
else
|
|
185
|
-
copyFileSync(bytes.path, sourcePath);
|
|
186
|
-
const job = {
|
|
187
|
-
id,
|
|
188
|
-
status: 'queued',
|
|
189
|
-
fileName,
|
|
190
|
-
request,
|
|
191
|
-
createdAt: new Date().toISOString(),
|
|
192
|
-
fingerprint,
|
|
193
|
-
serverResourceFingerprint: serverFingerprint,
|
|
194
|
-
...(owner ? { owner } : {}),
|
|
195
|
-
};
|
|
196
|
-
atomicJson(path.join(directory, 'job.json'), job);
|
|
197
|
-
this.jobs.set(id, job);
|
|
198
|
-
queueMicrotask(() => this.pump());
|
|
199
|
-
return job;
|
|
200
246
|
}
|
|
201
247
|
save(job) {
|
|
202
248
|
try {
|
|
@@ -250,7 +296,7 @@ export class JobQueue {
|
|
|
250
296
|
job.serverResourceFingerprint !==
|
|
251
297
|
serverResourceFingerprint(this.options.resources, serverFonts))
|
|
252
298
|
throw new CadError('RESOURCE_CHANGED', '提交后服务字体或资源发生改变,请重新检查图纸并提交');
|
|
253
|
-
job.result = await (this.options.worker || runWorker)({
|
|
299
|
+
job.result = await this.quota.runWorker(directory, controller, () => (this.options.worker || runWorker)({
|
|
254
300
|
source: path.join(directory, 'source' + path.extname(job.fileName).toLowerCase()),
|
|
255
301
|
output: directory,
|
|
256
302
|
request: job.request,
|
|
@@ -269,7 +315,7 @@ export class JobQueue {
|
|
|
269
315
|
if (!this.save(job))
|
|
270
316
|
throw new CadError('STORAGE_WRITE', 'DWG 兜底进度保存失败');
|
|
271
317
|
},
|
|
272
|
-
});
|
|
318
|
+
}), this.options.workerStorageBytes);
|
|
273
319
|
job.status = 'succeeded';
|
|
274
320
|
}
|
|
275
321
|
catch (error) {
|
|
@@ -317,49 +363,23 @@ export class JobQueue {
|
|
|
317
363
|
jobs: [...this.jobs.values()].filter((job) => job.owner === owner),
|
|
318
364
|
};
|
|
319
365
|
}
|
|
320
|
-
|
|
321
|
-
|
|
366
|
+
holdDownload(id) {
|
|
367
|
+
checkedId(id);
|
|
368
|
+
return this.retention.downloads.hold(id);
|
|
322
369
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const candidates = [...this.jobs.values()]
|
|
331
|
-
.filter((job) => !['queued', 'running'].includes(job.status) &&
|
|
332
|
-
!this.running.has(job.id) &&
|
|
333
|
-
job.endedAt &&
|
|
334
|
-
now - Date.parse(job.endedAt) >= grace)
|
|
335
|
-
.sort((a, b) => Date.parse(a.endedAt) - Date.parse(b.endedAt));
|
|
336
|
-
for (const job of candidates) {
|
|
337
|
-
const expired = this.options.retentionMs && now - Date.parse(job.endedAt) >= this.options.retentionMs;
|
|
338
|
-
if (!expired && !(this.options.maxStorageBytes && bytes > this.options.maxStorageBytes))
|
|
339
|
-
continue;
|
|
340
|
-
const size = directoryBytes(this.directory(job.id));
|
|
341
|
-
atomicJson(this.marker('.retired', job.id), {
|
|
342
|
-
id: job.id,
|
|
343
|
-
owner: job.owner,
|
|
344
|
-
fingerprint: job.fingerprint,
|
|
345
|
-
status: job.status,
|
|
346
|
-
endedAt: job.endedAt,
|
|
347
|
-
cleanedAt: new Date(now).toISOString(),
|
|
348
|
-
sourceSha256: job.result?.sourceSha256,
|
|
349
|
-
error: job.error,
|
|
350
|
-
});
|
|
351
|
-
removeJobDirectory(this.options.data, job.id);
|
|
352
|
-
this.jobs.delete(job.id);
|
|
353
|
-
bytes -= size;
|
|
354
|
-
removed.push(job.id);
|
|
355
|
-
}
|
|
370
|
+
consume(id, receipt) {
|
|
371
|
+
const raw = receipt;
|
|
372
|
+
if (raw && raw.status === undefined && typeof raw.owner === 'string') {
|
|
373
|
+
const job = this.get(id);
|
|
374
|
+
if (!job || job.status !== 'succeeded' || !job.owner || job.owner !== raw.owner)
|
|
375
|
+
throw new CadError('CONSUMPTION_CONFLICT', '成果消费回执与任务不一致');
|
|
376
|
+
return { ...this.retention.consume(id, { ...raw, status: 'succeeded' }), consumed: true };
|
|
356
377
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
return { removed };
|
|
378
|
+
return this.retention.consume(id, receipt);
|
|
379
|
+
}
|
|
380
|
+
cleanup(now = Date.now()) {
|
|
381
|
+
this.staging.cleanup();
|
|
382
|
+
return this.retention.cleanup(now);
|
|
363
383
|
}
|
|
364
384
|
async close() {
|
|
365
385
|
this.closing = true;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type WorkerOptions, type WorkerControls } from '../runner.js';
|
|
2
|
+
import { type StorageSettings } from './storage-policy.js';
|
|
3
|
+
export interface ManagedStorageOptions extends StorageSettings {
|
|
4
|
+
data: string;
|
|
5
|
+
}
|
|
6
|
+
/** The caller owns its temporary directory; the shared quota owns admission, growth and release. */
|
|
7
|
+
export declare function withRecadStorage<T>(options: ManagedStorageOptions, operation: (signal: AbortSignal) => Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
8
|
+
export declare function runManagedWorker(options: WorkerOptions, controls?: WorkerControls, storage?: Partial<ManagedStorageOptions>): Promise<any>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { StorageQuota } from "../../shared/service-storage/src/quota.js";
|
|
4
|
+
import { runWorker } from "../runner.js";
|
|
5
|
+
import { recadStorageOptions } from "./storage-policy.js";
|
|
6
|
+
import { CadError } from "../contracts.js";
|
|
7
|
+
/** The caller owns its temporary directory; the shared quota owns admission, growth and release. */
|
|
8
|
+
export async function withRecadStorage(options, operation, signal) {
|
|
9
|
+
const data = path.resolve(options.data);
|
|
10
|
+
mkdirSync(data, { recursive: true });
|
|
11
|
+
const storage = recadStorageOptions(options);
|
|
12
|
+
const quota = new StorageQuota(data, storage.maxStorageBytes, storage.storageCapacity);
|
|
13
|
+
const controller = new AbortController();
|
|
14
|
+
const abort = () => controller.abort(signal?.reason);
|
|
15
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
16
|
+
if (signal?.aborted)
|
|
17
|
+
abort();
|
|
18
|
+
try {
|
|
19
|
+
if (controller.signal.aborted)
|
|
20
|
+
throw new CadError('CANCELLED', '任务已取消');
|
|
21
|
+
return await quota.runWorker(data, controller, () => operation(controller.signal), storage.workerStorageBytes);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
signal?.removeEventListener('abort', abort);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function runManagedWorker(options, controls = {}, storage = {}) {
|
|
28
|
+
return withRecadStorage({ ...storage, data: storage.data ?? options.output }, signal => runWorker(options, { ...controls, signal }), controls.signal);
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type ServiceCapacityOptions } from '../../shared/service-storage/src/quota.js';
|
|
2
|
+
export interface StorageSettings {
|
|
3
|
+
retentionMs?: number;
|
|
4
|
+
maxStorageBytes?: number;
|
|
5
|
+
workerStorageBytes?: number;
|
|
6
|
+
storageCapacity?: ServiceCapacityOptions;
|
|
7
|
+
minimumFreeBytes?: number;
|
|
8
|
+
resumeExtraBytes?: number;
|
|
9
|
+
storageEstimateFactor?: number;
|
|
10
|
+
}
|
|
11
|
+
/** One set of defaults for services and managed one-shot work; explicit options take precedence. */
|
|
12
|
+
export declare function recadStorageOptions(options?: StorageSettings, env?: NodeJS.ProcessEnv): {
|
|
13
|
+
retentionMs: number;
|
|
14
|
+
maxStorageBytes: number | undefined;
|
|
15
|
+
workerStorageBytes: number;
|
|
16
|
+
storageCapacity: {
|
|
17
|
+
policy: {
|
|
18
|
+
safetyBytes: number;
|
|
19
|
+
resumeExtraBytes: number;
|
|
20
|
+
estimateFactor: number;
|
|
21
|
+
version: string;
|
|
22
|
+
};
|
|
23
|
+
probe?: typeof import("../../shared/service-storage/src/capacity.js").volumeSpace;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { capacityPolicy } from "../../shared/service-storage/src/capacity.js";
|
|
2
|
+
import { workerStorageBytes } from "../../shared/service-storage/src/quota.js";
|
|
3
|
+
import { DEFAULT_RETENTION_MS } from "../../shared/service-storage/src/retention.js";
|
|
4
|
+
import { CadError } from "../contracts.js";
|
|
5
|
+
/** One set of defaults for services and managed one-shot work; explicit options take precedence. */
|
|
6
|
+
export function recadStorageOptions(options = {}, env = process.env) {
|
|
7
|
+
const number = (name, factor, minimum) => {
|
|
8
|
+
const raw = env[name];
|
|
9
|
+
if (raw === undefined || raw === '')
|
|
10
|
+
return undefined;
|
|
11
|
+
const value = Number(raw);
|
|
12
|
+
if (!Number.isFinite(value) || value < minimum || value * factor > Number.MAX_SAFE_INTEGER)
|
|
13
|
+
throw new CadError('CONFIG', name + ' is outside its supported range');
|
|
14
|
+
return factor === 1 ? value : Math.ceil(value * factor);
|
|
15
|
+
};
|
|
16
|
+
const selected = options.storageCapacity?.policy;
|
|
17
|
+
const policy = {
|
|
18
|
+
...capacityPolicy, ...selected,
|
|
19
|
+
safetyBytes: options.minimumFreeBytes ?? selected?.safetyBytes ?? number('RECAD_MIN_FREE_GIB', 1024 ** 3, 0) ?? capacityPolicy.safetyBytes,
|
|
20
|
+
resumeExtraBytes: options.resumeExtraBytes ?? selected?.resumeExtraBytes ?? number('RECAD_RESUME_EXTRA_GIB', 1024 ** 3, 0) ?? capacityPolicy.resumeExtraBytes,
|
|
21
|
+
estimateFactor: options.storageEstimateFactor ?? selected?.estimateFactor ?? number('RECAD_STORAGE_ESTIMATE_FACTOR', 1, 1) ?? capacityPolicy.estimateFactor,
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
retentionMs: options.retentionMs ?? number('RECAD_RETENTION_DAYS', 86400000, 0.001) ?? DEFAULT_RETENTION_MS,
|
|
25
|
+
maxStorageBytes: options.maxStorageBytes ?? number('RECAD_MAX_STORAGE_MIB', 1024 ** 2, 0),
|
|
26
|
+
workerStorageBytes: options.workerStorageBytes ?? number('RECAD_WORKER_STORAGE_MIB', 1024 ** 2, 0.001) ?? workerStorageBytes,
|
|
27
|
+
storageCapacity: { ...options.storageCapacity, policy },
|
|
28
|
+
};
|
|
29
|
+
}
|
package/src/runtime/storage.d.ts
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export declare function checkedId(id: unknown): string;
|
|
3
|
-
export { writeJson as atomicJson } from '../io.js';
|
|
4
|
-
/** Reject links before measuring or removing a service-owned job directory. */
|
|
5
|
-
export declare function directoryBytes(directory: string): number;
|
|
6
|
-
export declare function removeJobDirectory(data: string, id: string): void;
|
|
7
|
-
export declare function canonical(value: any): any;
|
|
1
|
+
export { atomicJson, canonical, checkedId, directoryBytes, idPattern as jobIdPattern, removeJob as removeJobDirectory, dataRoot, } from '../../shared/service-storage/src/files.js';
|
package/src/runtime/storage.js
CHANGED
|
@@ -1,38 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { CadError } from "../contracts.js";
|
|
4
|
-
export const jobIdPattern = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
|
|
5
|
-
export function checkedId(id) {
|
|
6
|
-
if (typeof id !== 'string' || !jobIdPattern.test(id))
|
|
7
|
-
throw new CadError('JOB_ID', '任务标识必须是小写 UUID');
|
|
8
|
-
return id;
|
|
9
|
-
}
|
|
10
|
-
export { writeJson as atomicJson } from "../io.js";
|
|
11
|
-
/** Reject links before measuring or removing a service-owned job directory. */
|
|
12
|
-
export function directoryBytes(directory) {
|
|
13
|
-
const stat = lstatSync(directory);
|
|
14
|
-
if (stat.isSymbolicLink())
|
|
15
|
-
throw new CadError('STORAGE_LINK', '任务目录包含链接,拒绝自动清理');
|
|
16
|
-
if (!stat.isDirectory())
|
|
17
|
-
return stat.size;
|
|
18
|
-
return readdirSync(directory).reduce((sum, name) => sum + directoryBytes(path.join(directory, name)), 0);
|
|
19
|
-
}
|
|
20
|
-
export function removeJobDirectory(data, id) {
|
|
21
|
-
checkedId(id);
|
|
22
|
-
const root = realpathSync(data), target = path.resolve(root, id);
|
|
23
|
-
if (path.dirname(target) !== root ||
|
|
24
|
-
lstatSync(target).isSymbolicLink() ||
|
|
25
|
-
realpathSync(target) !== target)
|
|
26
|
-
throw new CadError('STORAGE_PATH', '清理目标不在服务任务目录中');
|
|
27
|
-
directoryBytes(target);
|
|
28
|
-
rmSync(target, { recursive: true });
|
|
29
|
-
}
|
|
30
|
-
export function canonical(value) {
|
|
31
|
-
if (Array.isArray(value))
|
|
32
|
-
return value.map(canonical);
|
|
33
|
-
if (value && typeof value === 'object')
|
|
34
|
-
return Object.fromEntries(Object.keys(value)
|
|
35
|
-
.sort()
|
|
36
|
-
.map((k) => [k, canonical(value[k])]));
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
1
|
+
export { atomicJson, canonical, checkedId, directoryBytes, idPattern as jobIdPattern, removeJob as removeJobDirectory, dataRoot, } from "../../shared/service-storage/src/files.js";
|
package/src/source-scene.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { TextInkBounds } from './text/ink-bounds.js';
|
|
2
2
|
type Box = [number, number, number, number];
|
|
3
|
+
/** Size the scene's plain JSON tree without allocating an oversized UTF-16 string. */
|
|
4
|
+
export declare function sceneJsonBytes(value: unknown): number;
|
|
3
5
|
/** Same sheet policy as XDTerminal: outer rectangular frames, reading order,
|
|
4
6
|
* intact overlapping frames and a separate page for every unassigned object.
|
|
5
7
|
* Bounds of clipped/specialized objects remain conservative and are labelled. */
|
|
6
|
-
export declare function splitSourceSheets(entities: any[]): {
|
|
8
|
+
export declare function splitSourceSheets(entities: any[], mode?: 'auto' | 'single'): {
|
|
7
9
|
box: Box;
|
|
8
10
|
entityIds: any[];
|
|
9
11
|
frame: boolean;
|
|
@@ -11,5 +13,5 @@ export declare function splitSourceSheets(entities: any[]): {
|
|
|
11
13
|
}[];
|
|
12
14
|
/** Export collected draw operations BEFORE print transforms. Instances use the
|
|
13
15
|
* renderer's composed affine matrices; raw entity extents are never substituted. */
|
|
14
|
-
export declare function sourceScene(roots: any[], db: any, textInk: TextInkBounds, provenance: any, native?: import('./native-evidence.js').NativeCapture): any;
|
|
16
|
+
export declare function sourceScene(roots: any[], db: any, textInk: TextInkBounds, provenance: any, native?: import('./native-evidence.js').NativeCapture, textGeometry?: ReturnType<typeof import('./text/scene-geometry.js').textSceneGeometry>): any;
|
|
15
17
|
export {};
|