@xlinklight/publishing 3.2.0 → 3.3.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.
- package/README.md +1 -1
- package/dist/src/acceptance-delivery-binding.js +59 -0
- package/dist/src/acceptance.js +79 -0
- package/dist/src/client.js +146 -0
- package/dist/src/index.js +3 -0
- package/dist/src/node/acceptance-delivery.js +88 -0
- package/dist/src/node/activation.js +3 -0
- package/dist/src/node/authorization.js +76 -0
- package/dist/src/node/cli.js +20 -0
- package/dist/src/node/index.js +9 -0
- package/dist/src/node/journal.js +282 -0
- package/dist/src/node/publishing-command.js +220 -0
- package/dist/src/node/session.js +223 -0
- package/dist/src/node/solution-package.js +217 -0
- package/dist/src/node/source-error.js +15 -0
- package/dist/src/node/source-producer.js +145 -0
- package/dist/src/node/workflow.js +455 -0
- package/dist/src/package.js +74 -0
- package/dist/src/protocol.js +105 -0
- package/dist/src/shared/activation.js +111 -0
- package/dist/src/shared/records.js +26 -0
- package/dist/src/worker/activation.js +10 -0
- package/dist/src/worker/index.js +6 -0
- package/dist/src/worker/journal.js +301 -0
- package/dist/src/worker/package.js +116 -0
- package/dist/src/worker/release.js +59 -0
- package/dist/src/worker/runtime.js +98 -0
- package/dist/src/worker/service.js +494 -0
- package/package.json +17 -14
- package/{src/acceptance.d.mts → types/acceptance.d.ts} +2 -0
- package/{src/client.d.mts → types/client.d.ts} +1 -1
- package/{src/index.d.mts → types/index.d.ts} +6 -6
- package/{src/node/acceptance-delivery.d.mts → types/node/acceptance-delivery.d.ts} +1 -1
- package/{src/node/activation.d.mts → types/node/activation.d.ts} +4 -4
- package/{src/node/authorization.d.mts → types/node/authorization.d.ts} +4 -4
- package/types/node/index.d.ts +17 -0
- package/{src/node/publishing-command.d.mts → types/node/publishing-command.d.ts} +4 -4
- package/{src/node/session.d.mts → types/node/session.d.ts} +7 -7
- package/{src/node/solution-package.d.mts → types/node/solution-package.d.ts} +2 -2
- package/{src/node/workflow.d.mts → types/node/workflow.d.ts} +7 -7
- package/{src/protocol.d.mts → types/protocol.d.ts} +1 -1
- package/{src/worker/activation.d.mts → types/worker/activation.d.ts} +3 -3
- package/types/worker/index.d.ts +6 -0
- package/{src/worker/package.d.mts → types/worker/package.d.ts} +2 -2
- package/{src/worker/release.d.mts → types/worker/release.d.ts} +3 -3
- package/{src/worker/runtime.d.mts → types/worker/runtime.d.ts} +2 -2
- package/{src/worker/service.d.mts → types/worker/service.d.ts} +7 -7
- package/src/acceptance.mjs +0 -66
- package/src/client.mjs +0 -74
- package/src/index.mjs +0 -3
- package/src/node/acceptance-delivery.mjs +0 -109
- package/src/node/activation.mjs +0 -3
- package/src/node/authorization.mjs +0 -49
- package/src/node/cli.mjs +0 -12
- package/src/node/index.d.mts +0 -17
- package/src/node/index.mjs +0 -9
- package/src/node/journal.mjs +0 -135
- package/src/node/publishing-command.mjs +0 -104
- package/src/node/session.mjs +0 -134
- package/src/node/solution-package.mjs +0 -113
- package/src/node/source-error.mjs +0 -9
- package/src/node/source-producer.mjs +0 -103
- package/src/node/workflow.mjs +0 -320
- package/src/package.mjs +0 -36
- package/src/protocol.mjs +0 -50
- package/src/shared/activation.mjs +0 -64
- package/src/worker/activation.mjs +0 -4
- package/src/worker/index.d.mts +0 -6
- package/src/worker/index.mjs +0 -6
- package/src/worker/journal.mjs +0 -151
- package/src/worker/package.mjs +0 -32
- package/src/worker/release.mjs +0 -21
- package/src/worker/runtime.mjs +0 -23
- package/src/worker/service.mjs +0 -251
- /package/{src/node/journal.d.mts → types/node/journal.d.ts} +0 -0
- /package/{src/node/source-error.d.mts → types/node/source-error.d.ts} +0 -0
- /package/{src/package.d.mts → types/package.d.ts} +0 -0
- /package/{src/shared/activation.d.mts → types/shared/activation.d.ts} +0 -0
- /package/{src/worker/journal.d.mts → types/worker/journal.d.ts} +0 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { publicPublisherMetadata, validateSolutionReleasePayload, solutionReleaseDigest } from '@xlinklight/platform/worker';
|
|
3
|
+
import { parsePublishingConfiguration, publishingError } from '../protocol.js';
|
|
4
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
5
|
+
import { createArtifactContentManifest } from '@openge/forge-artifact-publish';
|
|
6
|
+
import { parseSolutionPackageDescriptor, parseSolutionPackageLimits } from '../package.js';
|
|
7
|
+
const fail = (code) => publishingError('RELEASE_' + code);
|
|
8
|
+
function address(value) { try {
|
|
9
|
+
if (typeof value !== 'string' || value.length > 2048 || value.includes('?') || value.includes('#'))
|
|
10
|
+
throw Error();
|
|
11
|
+
const url = new URL(value);
|
|
12
|
+
if (url.protocol !== 'https:' || url.href !== value || url.username || url.password)
|
|
13
|
+
throw Error();
|
|
14
|
+
return url;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
throw fail('URL_INVALID');
|
|
18
|
+
} }
|
|
19
|
+
/** The service has already verified this immutable lazy snapshot in bounded batches. */
|
|
20
|
+
export async function preparePublishingRelease({ prepared, configuration: config, serverUrl, uiUrl, serverReceiptPath, uiReceiptPath, limits: inputLimits, signal: inputSignal }) {
|
|
21
|
+
const limits = parseSolutionPackageLimits(inputLimits), signal = AbortSignal.any([inputSignal, AbortSignal.timeout(limits.timeoutMs)].filter(s => s !== undefined));
|
|
22
|
+
signal.throwIfAborted();
|
|
23
|
+
const configuration = parsePublishingConfiguration(config), server = address(serverUrl), ui = address(uiUrl);
|
|
24
|
+
if (server.origin === configuration.coreOrigin || ui.origin === configuration.coreOrigin || typeof prepared?.readFile !== 'function' || typeof serverReceiptPath !== 'string' || !serverReceiptPath.startsWith('server/') || typeof uiReceiptPath !== 'string' || !uiReceiptPath.startsWith('ui/'))
|
|
25
|
+
throw fail('BINDING_INVALID');
|
|
26
|
+
const { descriptor } = prepared;
|
|
27
|
+
if (descriptor.publisherId !== configuration.publisher.publisherId)
|
|
28
|
+
throw fail('BINDING_INVALID');
|
|
29
|
+
try {
|
|
30
|
+
if (!(prepared.manifestBytes instanceof Uint8Array) || prepared.manifestBytes.byteLength > limits.maximumManifestBytes || prepared.manifest.files.length > limits.maximumFiles || prepared.manifest.files.some(file => file.byteLength > limits.maximumFileBytes))
|
|
31
|
+
throw Error();
|
|
32
|
+
const canonical = await createArtifactContentManifest(prepared.manifest), total = prepared.manifest.files.reduce((sum, file) => sum + file.byteLength, 0);
|
|
33
|
+
if (!isDeepStrictEqual(canonical.content, prepared.content) || !Buffer.from(canonical.bytes).equals(prepared.manifestBytes) || total !== prepared.payloadBytes || total + canonical.bytes.byteLength > limits.maximumTotalBytes)
|
|
34
|
+
throw Error();
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
throw fail('PACKAGE_INVALID');
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const original = parseSolutionPackageDescriptor(JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(await prepared.readFile('solution.json', { maximumBytes: 1048576, signal }))));
|
|
41
|
+
if (!isDeepStrictEqual(original, descriptor))
|
|
42
|
+
throw Error();
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
throw fail('PACKAGE_INVALID');
|
|
46
|
+
}
|
|
47
|
+
const receipt = async (path, role) => { if (prepared.manifest.files.find(file => file.path === path)?.role !== role)
|
|
48
|
+
throw fail('RECEIPT_INVALID'); return 'sha256:' + createHash('sha256').update(await prepared.readFile(path, { maximumBytes: 1048576, signal })).digest('hex'); };
|
|
49
|
+
const uiDigest = await receipt(uiReceiptPath, 'asset'), serverDigest = await receipt(serverReceiptPath, 'control');
|
|
50
|
+
signal?.throwIfAborted();
|
|
51
|
+
try {
|
|
52
|
+
const publisher = publicPublisherMetadata({ ...configuration.publisher, enabled: true, serverOrigins: [server.origin], uiOrigins: [ui.origin] }), { resourcesDigest, ...declarations } = descriptor.release;
|
|
53
|
+
const payload = validateSolutionReleasePayload({ schemaVersion: 1, solutionId: descriptor.solutionId, publisherId: descriptor.publisherId, version: descriptor.version, sdkApiVersion: descriptor.sdkApiVersion, server: { url: serverUrl, protocolVersion: descriptor.runtime.protocolVersion }, ui: { url: uiUrl, integrity: uiDigest, bridgeVersion: descriptor.ui.bridgeVersion }, artifacts: { server: serverDigest, resources: resourcesDigest }, ...declarations }, publisher);
|
|
54
|
+
return { payload, releaseDigest: solutionReleaseDigest(payload), publisher };
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw fail('DECLARATIONS_INVALID');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const fail = (code) => Error('PUBLISHING_RUNTIME_' + code);
|
|
2
|
+
/** One alarm owns one finite verification batch or original provider operation. */
|
|
3
|
+
export function createPublishingDurableRuntime({ ctx, journal, service, worker, now = Date.now, limits = { pollMs: 1000, jobMs: 60000 } }) {
|
|
4
|
+
const maximumPolls = limits.maximumPolls ?? 18;
|
|
5
|
+
if (!ctx?.storage || typeof ctx.abort !== 'function' || typeof service?.handle !== 'function' || typeof worker?.run !== 'function' || typeof journal?.take !== 'function' || !Number.isSafeInteger(limits.pollMs) || limits.pollMs < 1 || limits.pollMs > 30000 || !Number.isSafeInteger(limits.jobMs) || limits.jobMs < 1 || limits.jobMs > 300000 || !Number.isSafeInteger(maximumPolls) || maximumPolls < 1 || maximumPolls > 128)
|
|
6
|
+
throw fail('INPUT_INVALID');
|
|
7
|
+
let arming = Promise.resolve(), stepping = false, activeDeadline = 0;
|
|
8
|
+
const schedule = () => { const pending = arming.catch(() => { }).then(async () => { const next = journal.nextWake(), previous = await ctx.storage.getAlarm(); if (next === null) {
|
|
9
|
+
if (previous !== null) {
|
|
10
|
+
await ctx.storage.deleteAlarm();
|
|
11
|
+
await ctx.storage.sync();
|
|
12
|
+
}
|
|
13
|
+
return;
|
|
14
|
+
} const wanted = Math.max(now() + 1, next, stepping ? activeDeadline : 0); if (previous === null || previous > wanted)
|
|
15
|
+
await ctx.storage.setAlarm(wanted); await ctx.storage.sync(); }); arming = pending; return pending; };
|
|
16
|
+
const reset = () => { if (journal.blocked)
|
|
17
|
+
ctx.abort('Publishing transport closure is unconfirmed; observe the durable original claim in a fresh actor'); };
|
|
18
|
+
const run = async () => {
|
|
19
|
+
if (stepping || journal.running)
|
|
20
|
+
return { state: 'busy' };
|
|
21
|
+
stepping = true;
|
|
22
|
+
activeDeadline = now() + limits.jobMs;
|
|
23
|
+
let job, completed, failure, failed = false;
|
|
24
|
+
try {
|
|
25
|
+
job = await journal.take();
|
|
26
|
+
if (!job)
|
|
27
|
+
return { state: 'idle' };
|
|
28
|
+
try {
|
|
29
|
+
completed = { value: await worker.run({ projectId: job.projectId, requestId: job.requestId, mode: job.mode, signal: AbortSignal.timeout(limits.jobMs) }) };
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
failed = true;
|
|
33
|
+
failure = error;
|
|
34
|
+
}
|
|
35
|
+
const value = completed?.value;
|
|
36
|
+
if (!journal.blocked) {
|
|
37
|
+
const progressing = value?.next === 'perform' || value?.next === 'verify', pending = value?.receipt?.outcome === 'pending' && value.state !== 'awaiting-acceptance', observations = pending ? (job.observations ?? 0) + (job.mode === 'observe' ? 1 : 0) : 0, next = job.continue && !failed && (progressing || pending && observations < maximumPolls);
|
|
38
|
+
try {
|
|
39
|
+
await journal.finish(job, { again: !!next, delayMs: limits.pollMs, mode: progressing ? 'perform' : 'observe', advance: job.continue, observations });
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
failure = failed ? new AggregateError([failure, error], 'PUBLISHING_RUNTIME_FINISH_FAILED', { cause: failure }) : error;
|
|
43
|
+
failed = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (failed || !completed)
|
|
47
|
+
throw failure;
|
|
48
|
+
return completed.value;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
stepping = false;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
async function owned(work) {
|
|
55
|
+
let outcome;
|
|
56
|
+
try {
|
|
57
|
+
outcome = { ok: true, value: await work() };
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
outcome = { ok: false, failure: error };
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
await schedule();
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
outcome = { ok: false, failure: outcome.ok ? error : new AggregateError([outcome.failure, error], 'PUBLISHING_RUNTIME_SCHEDULE_FAILED', { cause: outcome.failure }) };
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
reset();
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
outcome = { ok: false, failure: outcome.ok ? error : new AggregateError([outcome.failure, error], 'PUBLISHING_RUNTIME_RESET_FAILED', { cause: outcome.failure }) };
|
|
73
|
+
}
|
|
74
|
+
if (!outcome.ok)
|
|
75
|
+
throw outcome.failure;
|
|
76
|
+
return outcome.value;
|
|
77
|
+
}
|
|
78
|
+
return Object.freeze({ schedule, run: () => owned(run), alarm: () => owned(run), status: () => ({ running: stepping, blocked: journal.blocked, nextWake: journal.nextWake() }), fetch: request => owned(async () => {
|
|
79
|
+
if (request.method === 'GET' || request.method === 'HEAD')
|
|
80
|
+
return service.handle(request);
|
|
81
|
+
try {
|
|
82
|
+
return await journal.withIngress(() => service.handle(request));
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error instanceof Error && /BUSY$/.test(error.message)) {
|
|
86
|
+
let timer;
|
|
87
|
+
try {
|
|
88
|
+
await Promise.race([request.body?.cancel(), new Promise((_, reject) => { timer = setTimeout(() => reject(fail('BODY_CLOSE_UNCONFIRMED')), 1000); })]);
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
}
|
|
93
|
+
return Response.json({ error: { code: 'BUSY' } }, { status: 409 });
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}) });
|
|
98
|
+
}
|