@playcraft/build 0.2.17 → 0.2.19-test-a
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/dist/audio-source-files.d.ts +5 -0
- package/dist/audio-source-files.js +135 -0
- package/dist/base-builder.js +42 -2
- package/dist/delivery/agent-build-runner.d.ts +44 -0
- package/dist/delivery/agent-build-runner.js +365 -0
- package/dist/delivery/agent-build-source.d.ts +8 -0
- package/dist/delivery/agent-build-source.js +25 -0
- package/dist/delivery/artifact-descriptor.d.ts +31 -0
- package/dist/delivery/artifact-descriptor.js +237 -0
- package/dist/delivery/artifact-package.d.ts +43 -0
- package/dist/delivery/artifact-package.js +283 -0
- package/dist/delivery/build-plan.d.ts +4 -0
- package/dist/delivery/build-plan.js +189 -0
- package/dist/delivery/build-plan.types.d.ts +51 -0
- package/dist/delivery/build-plan.types.js +1 -0
- package/dist/delivery/command-isolation.d.ts +21 -0
- package/dist/delivery/command-isolation.js +327 -0
- package/dist/delivery/commands.d.ts +5 -0
- package/dist/delivery/commands.js +146 -0
- package/dist/delivery/files.d.ts +9 -0
- package/dist/delivery/files.js +144 -0
- package/dist/delivery/git-source-fingerprint.d.ts +2 -0
- package/dist/delivery/git-source-fingerprint.js +58 -0
- package/dist/delivery/isolated-source.d.ts +5 -0
- package/dist/delivery/isolated-source.js +148 -0
- package/dist/delivery/output-publication.d.ts +21 -0
- package/dist/delivery/output-publication.js +444 -0
- package/dist/delivery/project-delivery.d.ts +6 -0
- package/dist/delivery/project-delivery.js +287 -0
- package/dist/delivery/recipe-build-plan.d.ts +4 -0
- package/dist/delivery/recipe-build-plan.js +53 -0
- package/dist/delivery/recipe.d.ts +20 -0
- package/dist/delivery/recipe.js +211 -0
- package/dist/delivery/safe-path.d.ts +2 -0
- package/dist/delivery/safe-path.js +18 -0
- package/dist/delivery/types.d.ts +81 -0
- package/dist/delivery/types.js +1 -0
- package/dist/engines/engine-detector.js +4 -0
- package/dist/engines/generic-adapter.js +14 -19
- package/dist/engines/playable-scripts-adapter.js +2 -5
- package/dist/index.d.ts +21 -4
- package/dist/index.js +12 -2
- package/dist/platforms/adikteev.js +1 -0
- package/dist/platforms/applovin.js +1 -0
- package/dist/platforms/base.d.ts +4 -0
- package/dist/platforms/base.js +25 -6
- package/dist/platforms/bigo.js +1 -0
- package/dist/platforms/facebook.js +1 -0
- package/dist/platforms/google.js +1 -0
- package/dist/platforms/inmobi.js +1 -0
- package/dist/platforms/ironsource.js +1 -0
- package/dist/platforms/liftoff.js +1 -0
- package/dist/platforms/mintegral.js +1 -0
- package/dist/platforms/moloco.js +1 -0
- package/dist/platforms/playcraft.js +1 -0
- package/dist/platforms/remerge.js +1 -0
- package/dist/platforms/snapchat.js +1 -0
- package/dist/platforms/tiktok.js +1 -0
- package/dist/platforms/unity.js +1 -0
- package/dist/production-animation-presence.d.ts +2 -0
- package/dist/production-animation-presence.js +146 -0
- package/dist/production-inputs.d.ts +30 -0
- package/dist/production-inputs.js +262 -0
- package/dist/production-source-paths.d.ts +5 -0
- package/dist/production-source-paths.js +27 -0
- package/dist/redirect-policy.d.ts +23 -0
- package/dist/redirect-policy.js +25 -0
- package/dist/types.d.ts +12 -2
- package/dist/validation/artifact-qa.d.ts +36 -0
- package/dist/validation/artifact-qa.js +263 -0
- package/dist/validation/artifact-rules.d.ts +39 -0
- package/dist/validation/artifact-rules.js +451 -0
- package/dist/validation/artifact.d.ts +14 -0
- package/dist/validation/artifact.js +188 -0
- package/dist/validation/html-validator.d.ts +5 -0
- package/dist/validation/html-validator.js +19 -17
- package/dist/validation/index.d.ts +5 -0
- package/dist/validation/index.js +5 -0
- package/dist/validation/package-validator.d.ts +2 -0
- package/dist/validation/package-validator.js +25 -64
- package/dist/validation/playwright-driver.d.ts +37 -0
- package/dist/validation/playwright-driver.js +302 -0
- package/dist/validation/profiles.d.ts +13 -1
- package/dist/validation/profiles.js +27 -0
- package/dist/validation/runtime-cover.d.ts +32 -0
- package/dist/validation/runtime-cover.js +120 -0
- package/dist/validation/runtime-load.d.ts +141 -0
- package/dist/validation/runtime-load.js +812 -0
- package/dist/validation/source-scanner.d.ts +11 -0
- package/dist/validation/source-scanner.js +32 -2
- package/dist/validation/types.d.ts +201 -2
- package/dist/validation/types.js +1 -1
- package/dist/validation/validation-runner.d.ts +22 -2
- package/dist/validation/validation-runner.js +64 -3
- package/dist/validation/zip-reader.d.ts +7 -0
- package/dist/validation/zip-reader.js +159 -26
- package/dist/vite/config-builder-generic.d.ts +3 -1
- package/dist/vite/config-builder-generic.js +9 -7
- package/dist/vite/config-builder.js +1 -1
- package/dist/vite/plugin-platform.js +16 -30
- package/dist/vite/plugin-source-builder.d.ts +2 -14
- package/dist/vite/plugin-source-builder.js +61 -997
- package/dist/vite/source-builder-asset-paths.d.ts +25 -0
- package/dist/vite/source-builder-asset-paths.js +154 -0
- package/dist/vite/source-builder-esm.d.ts +7 -0
- package/dist/vite/source-builder-esm.js +877 -0
- package/dist/vite/source-builder-types.d.ts +14 -0
- package/dist/vite/source-builder-types.js +1 -0
- package/dist/vite/workspace.d.ts +6 -0
- package/dist/vite/workspace.js +41 -0
- package/dist/vite-builder.d.ts +4 -5
- package/dist/vite-builder.js +33 -34
- package/dist/workspace-css-imports.d.ts +2 -0
- package/dist/workspace-css-imports.js +139 -0
- package/dist/workspace-model-source.d.ts +14 -0
- package/dist/workspace-model-source.js +184 -0
- package/dist/workspace-public-imports.d.ts +11 -0
- package/dist/workspace-public-imports.js +232 -0
- package/dist/workspace-public-source.d.ts +15 -0
- package/dist/workspace-public-source.js +564 -0
- package/package.json +30 -4
- package/templates/patches/playcraft-redirect-policy.js +115 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { access, stat } from 'node:fs/promises';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { ProjectDeliveryError } from './recipe.js';
|
|
6
|
+
import { safeExistingPath } from './safe-path.js';
|
|
7
|
+
import { wrapDeliveryCommand } from './command-isolation.js';
|
|
8
|
+
function isolationUnavailable(code, output) {
|
|
9
|
+
return code === 125 && output.includes('delivery_isolation_unavailable:') ||
|
|
10
|
+
// Bubblewrap reports its own setup failures as "bwrap: ..." with exit 1.
|
|
11
|
+
code === 1 && /(?:^|\n)bwrap: /.test(output) ||
|
|
12
|
+
/sandbox-exec:.*(?:sandbox_(?:init|apply)|Operation not permitted)/i.test(output);
|
|
13
|
+
}
|
|
14
|
+
/** Keep the launcher's concrete reason so a blocked preparation can be diagnosed. */
|
|
15
|
+
function isolationError(output) {
|
|
16
|
+
const reason = /delivery_isolation_unavailable:(\w{1,80})/.exec(output)?.[1] ??
|
|
17
|
+
/(?:^|\n)bwrap: ([^\n]{1,200})/.exec(output)?.[1]?.replace(/^/, 'bubblewrap: ') ??
|
|
18
|
+
/sandbox-exec: ([^\n]{1,200})/.exec(output)?.[1]?.replace(/^/, 'sandbox-exec: ');
|
|
19
|
+
return new ProjectDeliveryError('delivery_isolation_unavailable', `The operating system could not enforce delivery file isolation${reason ? ` (${reason})` : ''}`);
|
|
20
|
+
}
|
|
21
|
+
export async function requireExecutable(root, executable, cwd, options, isolation) {
|
|
22
|
+
if (options.runCommand) {
|
|
23
|
+
const checkArgv = [
|
|
24
|
+
'/bin/sh',
|
|
25
|
+
'-c',
|
|
26
|
+
'case "$1" in */*) test -f "$1" && test -x "$1" ;; *) command -v "$1" >/dev/null 2>&1 ;; esac',
|
|
27
|
+
'delivery-toolchain',
|
|
28
|
+
executable,
|
|
29
|
+
];
|
|
30
|
+
const check = await options.runCommand({
|
|
31
|
+
argv: isolation ? await wrapDeliveryCommand(checkArgv, isolation) : checkArgv,
|
|
32
|
+
cwd,
|
|
33
|
+
timeoutMs: 10000,
|
|
34
|
+
});
|
|
35
|
+
if (isolation && isolationUnavailable(check.code, `${check.stderr ?? ''}${check.stdout ?? ''}`))
|
|
36
|
+
throw isolationError(`${check.stderr ?? ''}${check.stdout ?? ''}`);
|
|
37
|
+
if (check.code !== 0 || check.canceled || check.timedOut)
|
|
38
|
+
throw new ProjectDeliveryError('delivery_toolchain_missing', `Required executable is unavailable: ${executable}`);
|
|
39
|
+
return executable;
|
|
40
|
+
}
|
|
41
|
+
const search = executable.includes('/') || executable.includes('\\')
|
|
42
|
+
? [path.resolve(cwd, executable)]
|
|
43
|
+
: [
|
|
44
|
+
path.join(cwd, 'node_modules', '.bin', executable),
|
|
45
|
+
path.join(root, 'node_modules', '.bin', executable),
|
|
46
|
+
...(process.env.PATH ?? '')
|
|
47
|
+
.split(path.delimiter)
|
|
48
|
+
.map((directory) => path.join(directory, executable)),
|
|
49
|
+
];
|
|
50
|
+
for (const candidate of search) {
|
|
51
|
+
try {
|
|
52
|
+
await access(candidate, process.platform === 'win32' ? constants.F_OK : constants.X_OK);
|
|
53
|
+
if ((await stat(candidate)).isFile())
|
|
54
|
+
return candidate;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
/* Check the next PATH entry. */
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw new ProjectDeliveryError('delivery_toolchain_missing', `Required executable is unavailable: ${executable}`);
|
|
61
|
+
}
|
|
62
|
+
export async function runDeliveryCommand(root, command, replace, options, isolation) {
|
|
63
|
+
if (options.signal?.aborted)
|
|
64
|
+
throw new ProjectDeliveryError('delivery_canceled', 'Delivery preparation was canceled');
|
|
65
|
+
const cwd = await safeExistingPath(root, replace(command.cwd ?? '.'));
|
|
66
|
+
const argv = command.argv.map(replace);
|
|
67
|
+
const executable = await requireExecutable(root, argv[0], cwd, options, isolation);
|
|
68
|
+
const isolatedArgv = isolation
|
|
69
|
+
? await wrapDeliveryCommand([executable, ...argv.slice(1)], isolation)
|
|
70
|
+
: undefined;
|
|
71
|
+
const timeoutMs = command.timeoutMs ?? 300000;
|
|
72
|
+
if (options.runCommand) {
|
|
73
|
+
const result = await options.runCommand({ argv: isolatedArgv ?? argv, cwd, timeoutMs });
|
|
74
|
+
if (isolation && isolationUnavailable(result.code, `${result.stderr ?? ''}${result.stdout ?? ''}`))
|
|
75
|
+
throw isolationError(`${result.stderr ?? ''}${result.stdout ?? ''}`);
|
|
76
|
+
if (result.canceled || options.signal?.aborted)
|
|
77
|
+
throw new ProjectDeliveryError('delivery_canceled', 'Delivery preparation was canceled');
|
|
78
|
+
if (result.timedOut)
|
|
79
|
+
throw new ProjectDeliveryError('delivery_command_timeout', `Delivery command exceeded ${timeoutMs}ms`);
|
|
80
|
+
if (result.code !== 0)
|
|
81
|
+
throw new ProjectDeliveryError('delivery_command_failed', `Delivery command exited with ${result.code}: ${(result.stderr || result.stdout || '').slice(-4000)}`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
await new Promise((resolve, reject) => {
|
|
85
|
+
let tail = '';
|
|
86
|
+
let timedOut = false;
|
|
87
|
+
const child = spawn(isolatedArgv?.[0] ?? executable, isolatedArgv?.slice(1) ?? argv.slice(1), {
|
|
88
|
+
cwd,
|
|
89
|
+
shell: false,
|
|
90
|
+
detached: process.platform !== 'win32',
|
|
91
|
+
env: {
|
|
92
|
+
...process.env,
|
|
93
|
+
CI: 'true',
|
|
94
|
+
PLAYCRAFT_DELIVERY_EXECUTING: '1',
|
|
95
|
+
PATH: [
|
|
96
|
+
path.join(cwd, 'node_modules', '.bin'),
|
|
97
|
+
path.join(root, 'node_modules', '.bin'),
|
|
98
|
+
process.env.PATH ?? '',
|
|
99
|
+
].join(path.delimiter),
|
|
100
|
+
},
|
|
101
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
102
|
+
});
|
|
103
|
+
const stop = () => {
|
|
104
|
+
try {
|
|
105
|
+
if (process.platform !== 'win32' && child.pid)
|
|
106
|
+
process.kill(-child.pid, 'SIGTERM');
|
|
107
|
+
else
|
|
108
|
+
child.kill('SIGTERM');
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
/* The command may already have exited. */
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const timer = setTimeout(() => {
|
|
115
|
+
timedOut = true;
|
|
116
|
+
stop();
|
|
117
|
+
}, timeoutMs);
|
|
118
|
+
options.signal?.addEventListener('abort', stop, { once: true });
|
|
119
|
+
const collect = (chunk) => {
|
|
120
|
+
tail = (tail + chunk.toString()).slice(-4000);
|
|
121
|
+
};
|
|
122
|
+
child.stdout?.on('data', collect);
|
|
123
|
+
child.stderr?.on('data', collect);
|
|
124
|
+
const cleanup = () => {
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
options.signal?.removeEventListener('abort', stop);
|
|
127
|
+
};
|
|
128
|
+
child.once('error', (error) => {
|
|
129
|
+
cleanup();
|
|
130
|
+
reject(new ProjectDeliveryError('delivery_command_failed', error.message));
|
|
131
|
+
});
|
|
132
|
+
child.once('close', (code) => {
|
|
133
|
+
cleanup();
|
|
134
|
+
if (options.signal?.aborted)
|
|
135
|
+
reject(new ProjectDeliveryError('delivery_canceled', 'Delivery preparation was canceled'));
|
|
136
|
+
else if (timedOut)
|
|
137
|
+
reject(new ProjectDeliveryError('delivery_command_timeout', `Delivery command exceeded ${timeoutMs}ms`));
|
|
138
|
+
else if (isolation && isolationUnavailable(code, tail))
|
|
139
|
+
reject(isolationError(tail));
|
|
140
|
+
else if (code !== 0)
|
|
141
|
+
reject(new ProjectDeliveryError('delivery_command_failed', `Delivery command exited with ${code}: ${tail}`));
|
|
142
|
+
else
|
|
143
|
+
resolve();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function inside(root: string, target: string): boolean;
|
|
2
|
+
export declare function safeExistingPath(root: string, relative: string): Promise<string>;
|
|
3
|
+
export declare function checkOutputDirectory(root: string, directory: string): Promise<void>;
|
|
4
|
+
/** Hash content and relative names, never mtime or Git HEAD, so receipts survive a clean clone. */
|
|
5
|
+
export declare function deliverySourceFingerprint(root: string, outputDirectory: string, receiptPath?: string, generatedDirectories?: readonly string[]): Promise<string>;
|
|
6
|
+
/** Versioned projects use the same exact source set as save, batch capture and a clean Worker checkout. */
|
|
7
|
+
export declare function deliveryProductionSourceSnapshot(root: string, outputDirectory: string, receiptPath?: string, generatedDirectories?: readonly string[]): Promise<import("../production-inputs.js").ProductionSourceSnapshot | undefined>;
|
|
8
|
+
export declare function outputFingerprint(root: string, directory: string, entry: string): Promise<string>;
|
|
9
|
+
export declare function entryRevision(root: string, directory: string, entry: string): Promise<string | undefined>;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { lstat, readFile, readdir, realpath } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { unzipSync } from 'fflate';
|
|
5
|
+
import { ProjectDeliveryError } from './recipe.js';
|
|
6
|
+
import { readProductionSourceSnapshot, ProductionSourceError } from '../production-inputs.js';
|
|
7
|
+
import { deliveryGitSourceFingerprint } from './git-source-fingerprint.js';
|
|
8
|
+
export function inside(root, target) {
|
|
9
|
+
const relative = path.relative(root, target);
|
|
10
|
+
return (relative === '' ||
|
|
11
|
+
(!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative)));
|
|
12
|
+
}
|
|
13
|
+
export async function safeExistingPath(root, relative) {
|
|
14
|
+
const base = await realpath(root);
|
|
15
|
+
const target = await realpath(path.resolve(root, relative)).catch(() => {
|
|
16
|
+
throw new ProjectDeliveryError('delivery_path_missing', `Required delivery path is missing: ${relative}`, relative);
|
|
17
|
+
});
|
|
18
|
+
if (!inside(base, target))
|
|
19
|
+
throw new ProjectDeliveryError('delivery_path_outside_project', 'Delivery paths must stay inside the project', relative);
|
|
20
|
+
return target;
|
|
21
|
+
}
|
|
22
|
+
export async function checkOutputDirectory(root, directory) {
|
|
23
|
+
const base = await realpath(root);
|
|
24
|
+
let candidate = path.resolve(root, directory);
|
|
25
|
+
while (candidate !== path.dirname(candidate)) {
|
|
26
|
+
try {
|
|
27
|
+
const resolved = await realpath(candidate);
|
|
28
|
+
if (!inside(base, resolved))
|
|
29
|
+
throw new ProjectDeliveryError('delivery_path_outside_project', 'Delivery output must stay inside the project', directory);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error.code !== 'ENOENT')
|
|
34
|
+
throw error;
|
|
35
|
+
candidate = path.dirname(candidate);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function validateEntry(output, primary) {
|
|
40
|
+
const content = await readFile(primary);
|
|
41
|
+
if (path.extname(primary).toLowerCase() === '.zip') {
|
|
42
|
+
try {
|
|
43
|
+
let totalBytes = 0;
|
|
44
|
+
const files = unzipSync(content, {
|
|
45
|
+
filter(file) {
|
|
46
|
+
totalBytes += file.originalSize;
|
|
47
|
+
if (totalBytes > 128 * 1024 * 1024 ||
|
|
48
|
+
file.name.startsWith('/') ||
|
|
49
|
+
file.name.includes('\\') ||
|
|
50
|
+
file.name.split('/').includes('..'))
|
|
51
|
+
throw new Error('Invalid delivery ZIP entries');
|
|
52
|
+
return true;
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
if (!Object.keys(files).some((name) => name.endsWith('.html')))
|
|
56
|
+
throw new Error('Delivery ZIP has no HTML entry');
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new ProjectDeliveryError('delivery_package_invalid', 'The declared ZIP must be a valid Web package', path.basename(primary));
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const html = content.toString('utf8');
|
|
64
|
+
if (!/<(?:!doctype\s+html|html|body|script|canvas)\b/i.test(html))
|
|
65
|
+
throw new ProjectDeliveryError('delivery_html_invalid', 'The declared HTML entry is not a Web document', path.basename(primary));
|
|
66
|
+
for (const tag of html.match(/<(?:script|link|img|source|audio|video)\b[^>]*>/gi) ?? []) {
|
|
67
|
+
const attribute = /\b(?:src|href)\s*=\s*['"]([^'"]+)['"]/i.exec(tag)?.[1];
|
|
68
|
+
if (!attribute || /^(?:[a-z][a-z\d+.-]*:|\/\/|#)/i.test(attribute))
|
|
69
|
+
continue;
|
|
70
|
+
let relative;
|
|
71
|
+
try {
|
|
72
|
+
relative = decodeURIComponent(attribute.split(/[?#]/)[0]);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
throw new ProjectDeliveryError('delivery_resource_missing', 'A delivery resource URL is invalid', attribute);
|
|
76
|
+
}
|
|
77
|
+
const target = relative.startsWith('/')
|
|
78
|
+
? path.join(output, relative)
|
|
79
|
+
: path.resolve(path.dirname(primary), relative);
|
|
80
|
+
const resolved = await realpath(target).catch(() => undefined);
|
|
81
|
+
if (!resolved || !inside(output, resolved))
|
|
82
|
+
throw new ProjectDeliveryError('delivery_resource_missing', 'A referenced delivery resource is missing or outside the output directory', attribute);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Hash content and relative names, never mtime or Git HEAD, so receipts survive a clean clone. */
|
|
86
|
+
export async function deliverySourceFingerprint(root, outputDirectory, receiptPath, generatedDirectories = []) {
|
|
87
|
+
const production = await deliveryProductionSourceSnapshot(root, outputDirectory, receiptPath, generatedDirectories);
|
|
88
|
+
if (production)
|
|
89
|
+
return production.fingerprint;
|
|
90
|
+
return deliveryGitSourceFingerprint(root, outputDirectory, receiptPath, generatedDirectories);
|
|
91
|
+
}
|
|
92
|
+
/** Versioned projects use the same exact source set as save, batch capture and a clean Worker checkout. */
|
|
93
|
+
export async function deliveryProductionSourceSnapshot(root, outputDirectory, receiptPath, generatedDirectories = []) {
|
|
94
|
+
const pattern = new RegExp(`^${path.posix.normalize(outputDirectory).replace(/[.*+?^$()|[\]\\]/g, '\\$&')
|
|
95
|
+
.replace(/\{channel\}/g, '[a-z][a-z0-9-]{0,63}').replace(/\{format\}/g, '(?:html|zip)')}(?:/|$)`);
|
|
96
|
+
try {
|
|
97
|
+
return await readProductionSourceSnapshot(root, { excludedDirectories: generatedDirectories,
|
|
98
|
+
excludedPath: name => pattern.test(name) || Boolean(receiptPath && path.resolve(root, name) === path.resolve(receiptPath)) });
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (error instanceof ProductionSourceError)
|
|
102
|
+
throw new ProjectDeliveryError(error.code, error.message, error.file);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export async function outputFingerprint(root, directory, entry) {
|
|
107
|
+
const output = await safeExistingPath(root, directory);
|
|
108
|
+
const primary = await safeExistingPath(root, path.posix.join(directory, entry));
|
|
109
|
+
if (!inside(output, primary) ||
|
|
110
|
+
!(await lstat(primary)).isFile() ||
|
|
111
|
+
(await lstat(primary)).size < 1) {
|
|
112
|
+
throw new ProjectDeliveryError('delivery_output_invalid', 'The declared delivery entry must be a nonempty file', entry);
|
|
113
|
+
}
|
|
114
|
+
await validateEntry(output, primary);
|
|
115
|
+
const hash = createHash('sha256');
|
|
116
|
+
const visit = async (current) => {
|
|
117
|
+
for (const child of (await readdir(current, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name, 'en'))) {
|
|
118
|
+
const absolute = path.join(current, child.name);
|
|
119
|
+
if (child.isSymbolicLink())
|
|
120
|
+
throw new ProjectDeliveryError('delivery_output_symlink', 'Delivery outputs must contain files instead of symlinks', path.relative(output, absolute));
|
|
121
|
+
if (child.isDirectory())
|
|
122
|
+
await visit(absolute);
|
|
123
|
+
else if (child.isFile()) {
|
|
124
|
+
const content = await readFile(absolute);
|
|
125
|
+
hash
|
|
126
|
+
.update(path.relative(output, absolute).split(path.sep).join('/'))
|
|
127
|
+
.update('\0')
|
|
128
|
+
.update(content)
|
|
129
|
+
.update('\0');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
await visit(output);
|
|
134
|
+
return hash.digest('hex');
|
|
135
|
+
}
|
|
136
|
+
export async function entryRevision(root, directory, entry) {
|
|
137
|
+
try {
|
|
138
|
+
const info = await lstat(path.join(root, directory, entry), { bigint: true });
|
|
139
|
+
return [info.ino, info.size, info.mtimeNs, info.ctimeNs].join(':');
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { ProjectDeliveryError } from './recipe.js';
|
|
5
|
+
const SOURCE_IGNORED = new Set([
|
|
6
|
+
'.git',
|
|
7
|
+
'node_modules',
|
|
8
|
+
'dist',
|
|
9
|
+
'.cache',
|
|
10
|
+
'.turbo',
|
|
11
|
+
'.next',
|
|
12
|
+
'.vite',
|
|
13
|
+
'.parcel-cache',
|
|
14
|
+
'.pnpm-store',
|
|
15
|
+
'.eslintcache',
|
|
16
|
+
'.DS_Store',
|
|
17
|
+
'.playcraft',
|
|
18
|
+
'.playcraft-temp',
|
|
19
|
+
'.tmp',
|
|
20
|
+
'.agents',
|
|
21
|
+
'.claude',
|
|
22
|
+
'.codex',
|
|
23
|
+
'.playcraft.json',
|
|
24
|
+
]);
|
|
25
|
+
/** The clean-Git fallback has no production audio or native package dependencies. */
|
|
26
|
+
export async function deliveryGitSourceFingerprint(root, outputDirectory, receiptPath, generatedDirectories = []) {
|
|
27
|
+
const hash = createHash('sha256');
|
|
28
|
+
const outputPattern = new RegExp(`^${path.posix
|
|
29
|
+
.normalize(outputDirectory)
|
|
30
|
+
.replace(/[.*+?^$()|[\]\\]/g, '\\$&')
|
|
31
|
+
.replace(/\{channel\}/g, '[a-z][a-z0-9-]{0,63}')
|
|
32
|
+
.replace(/\{format\}/g, '(?:html|zip)')}(?:/|$)`);
|
|
33
|
+
const receipt = receiptPath ? path.resolve(receiptPath) : undefined;
|
|
34
|
+
const visit = async (directory) => {
|
|
35
|
+
const entries = (await readdir(directory, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name, 'en'));
|
|
36
|
+
for (const entry of entries) {
|
|
37
|
+
const absolute = path.join(directory, entry.name);
|
|
38
|
+
const relative = path.relative(root, absolute).split(path.sep).join('/');
|
|
39
|
+
if (SOURCE_IGNORED.has(entry.name) ||
|
|
40
|
+
/^\.env(?:\..*)?$/.test(entry.name) ||
|
|
41
|
+
absolute === receipt ||
|
|
42
|
+
generatedDirectories.some((directory) => relative === directory || relative.startsWith(`${directory}/`)) ||
|
|
43
|
+
outputPattern.test(relative))
|
|
44
|
+
continue;
|
|
45
|
+
if (entry.isDirectory())
|
|
46
|
+
await visit(absolute);
|
|
47
|
+
else if (entry.isSymbolicLink()) {
|
|
48
|
+
throw new ProjectDeliveryError('delivery_source_symlink', 'Source symlinks require a reproducible project build input', relative);
|
|
49
|
+
}
|
|
50
|
+
else if (entry.isFile()) {
|
|
51
|
+
const content = await readFile(absolute);
|
|
52
|
+
hash.update(`${relative.length}:${relative}:${content.length}:`).update(content);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
await visit(root);
|
|
57
|
+
return hash.digest('hex');
|
|
58
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ProductionSourceSnapshot } from '../production-inputs.js';
|
|
2
|
+
import type { ProjectDeliveryOptions, ProjectDeliveryRecipe } from './types.js';
|
|
3
|
+
import type { DeliveryCommandIsolation } from './commands.js';
|
|
4
|
+
/** A clean source directory contains only the versioned production snapshot and controlled dependencies. */
|
|
5
|
+
export declare function buildIsolatedDelivery(root: string, snapshot: ProductionSourceSnapshot, recipe: ProjectDeliveryRecipe, options: ProjectDeliveryOptions, build: (isolated: string, isolation: DeliveryCommandIsolation) => Promise<string>): Promise<string>;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { chown, chmod, cp, lstat, mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { readProductionSourceSnapshot } from '../production-inputs.js';
|
|
5
|
+
import { ProjectDeliveryError, deliveryContext, recipeDigest, readProjectDeliveryRecipe } from './recipe.js';
|
|
6
|
+
import { outputFingerprint } from './files.js';
|
|
7
|
+
import { publishDeliveryOutput, recoverDeliveryOutputPublication } from './output-publication.js';
|
|
8
|
+
/** A clean source directory contains only the versioned production snapshot and controlled dependencies. */
|
|
9
|
+
export async function buildIsolatedDelivery(root, snapshot, recipe, options, build) {
|
|
10
|
+
root = await realpath(root);
|
|
11
|
+
const context = deliveryContext(recipe, options);
|
|
12
|
+
const outputDirectory = path.posix.normalize(context.outputDirectory).replace(/\/+$/, '');
|
|
13
|
+
if (!outputDirectory.startsWith('dist/'))
|
|
14
|
+
throw new ProjectDeliveryError('delivery_output_overlaps_artifacts', 'Use a dedicated output directory below dist; original artifacts must be preserved');
|
|
15
|
+
await assertOutputPath(root, outputDirectory);
|
|
16
|
+
const publication = { root, outputDirectory, entry: context.entry,
|
|
17
|
+
sourceFingerprint: snapshot.fingerprint, recipeDigest: recipeDigest(recipe), key: context.key,
|
|
18
|
+
...(options.receiptDirectory || options.receiptPath ? {
|
|
19
|
+
receiptDirectory: options.receiptDirectory ?? path.dirname(options.receiptPath),
|
|
20
|
+
} : {}) };
|
|
21
|
+
if (await recoverDeliveryOutputPublication(publication) === 'published')
|
|
22
|
+
return outputFingerprint(root, outputDirectory, context.entry);
|
|
23
|
+
const group = (await lstat(root)).gid;
|
|
24
|
+
const shared = async (target, directory, executable = false) => {
|
|
25
|
+
if (process.platform !== 'win32') {
|
|
26
|
+
await chown(target, process.getuid(), group);
|
|
27
|
+
await chmod(target, directory ? 0o2770 : executable ? 0o770 : 0o660);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const temporary = path.join(root, '.playcraft-temp');
|
|
31
|
+
const existing = await lstat(temporary).catch((error) => { if (error.code === 'ENOENT')
|
|
32
|
+
return undefined; throw error; });
|
|
33
|
+
if (existing && (!existing.isDirectory() || existing.isSymbolicLink()))
|
|
34
|
+
throw new ProjectDeliveryError('delivery_path_outside_project', 'Unsafe delivery staging directory');
|
|
35
|
+
if (!existing) {
|
|
36
|
+
await mkdir(temporary, { mode: 0o2770 });
|
|
37
|
+
await shared(temporary, true);
|
|
38
|
+
}
|
|
39
|
+
const scratch = await mkdtemp(path.join(temporary, 'delivery-source-'));
|
|
40
|
+
await shared(scratch, true);
|
|
41
|
+
const directories = new Set([scratch]);
|
|
42
|
+
const parent = async (relative) => {
|
|
43
|
+
let directory = scratch;
|
|
44
|
+
for (const segment of relative.split('/').slice(0, -1)) {
|
|
45
|
+
directory = path.join(directory, segment);
|
|
46
|
+
if (!directories.has(directory)) {
|
|
47
|
+
await mkdir(directory);
|
|
48
|
+
await shared(directory, true);
|
|
49
|
+
directories.add(directory);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
// On failure retain only this bounded staging directory. A managed process may still be
|
|
54
|
+
// reconciling its original identity; deleting its cwd is not a stop acknowledgement.
|
|
55
|
+
for (const [name, file] of snapshot.files) {
|
|
56
|
+
await parent(name);
|
|
57
|
+
const target = path.join(scratch, name);
|
|
58
|
+
await writeFile(target, file.body, { flag: 'wx', mode: 0o660 });
|
|
59
|
+
await shared(target, false, file.mode === '100755');
|
|
60
|
+
}
|
|
61
|
+
const projection = path.join(scratch, 'playcraft.config.json');
|
|
62
|
+
await writeFile(projection, `${JSON.stringify({ build: { delivery: recipe } }, null, 2)}\n`, { flag: 'wx', mode: 0o660 });
|
|
63
|
+
await shared(projection, false);
|
|
64
|
+
const modules = await dependencyDirectory(root, options.trustedDependencyRoot);
|
|
65
|
+
if (modules)
|
|
66
|
+
await symlink(modules, path.join(scratch, 'node_modules'), 'dir');
|
|
67
|
+
// Tool temporaries (for example Node's 0600 compile cache) must not land in the
|
|
68
|
+
// rescanned production source; .playcraft-temp is a process path excluded from it.
|
|
69
|
+
const toolTemporary = path.join(scratch, '.playcraft-temp');
|
|
70
|
+
await mkdir(toolTemporary);
|
|
71
|
+
await shared(toolTemporary, true);
|
|
72
|
+
await build(scratch, { projectRoot: root, isolatedRoot: scratch, temporaryRoot: toolTemporary,
|
|
73
|
+
dependencyRoots: modules ? [modules] : [] });
|
|
74
|
+
const current = await readProductionSourceSnapshot(root, { excludedDirectories: recipe.generatedDirectories,
|
|
75
|
+
excludedPath: name => name === context.outputDirectory || name.startsWith(`${context.outputDirectory}/`) });
|
|
76
|
+
if (!current || current.fingerprint !== snapshot.fingerprint)
|
|
77
|
+
throw new ProjectDeliveryError('delivery_source_changed', 'Production source changed while preparing delivery');
|
|
78
|
+
const currentRecipe = await readProjectDeliveryRecipe(root);
|
|
79
|
+
if (!currentRecipe || recipeDigest(currentRecipe) !== publication.recipeDigest)
|
|
80
|
+
throw new ProjectDeliveryError('delivery_source_changed', 'The delivery recipe changed while preparing delivery');
|
|
81
|
+
await assertOutputPath(scratch, outputDirectory, true);
|
|
82
|
+
await assertOutputPath(root, outputDirectory);
|
|
83
|
+
const source = path.join(scratch, outputDirectory);
|
|
84
|
+
const expectedNewFingerprint = await outputFingerprint(scratch, context.outputDirectory, context.entry);
|
|
85
|
+
// Publish only the declared output subtree. Never clean the root dist/ or other artifacts.
|
|
86
|
+
const staged = path.join(temporary, `delivery-output-${randomUUID()}`);
|
|
87
|
+
await cp(source, staged, { recursive: true, force: false, dereference: false });
|
|
88
|
+
await assertOutputPath(root, outputDirectory, false, shared);
|
|
89
|
+
const fingerprint = await publishDeliveryOutput({ ...publication, stagePath: staged, expectedNewFingerprint });
|
|
90
|
+
await rm(scratch, { recursive: true, force: true });
|
|
91
|
+
return fingerprint;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Installed dependencies are a real node_modules directory, or a link the caller declared by its
|
|
95
|
+
* resolved target. The snapshot links straight to that real path so the command mount view and
|
|
96
|
+
* dependency allowance see one directory.
|
|
97
|
+
*/
|
|
98
|
+
async function dependencyDirectory(root, trusted) {
|
|
99
|
+
const modules = path.join(root, 'node_modules');
|
|
100
|
+
const info = await lstat(modules).catch(() => undefined);
|
|
101
|
+
if (!info)
|
|
102
|
+
return undefined;
|
|
103
|
+
if (info.isDirectory())
|
|
104
|
+
return modules;
|
|
105
|
+
if (info.isSymbolicLink() && trusted) {
|
|
106
|
+
const [actual, expected] = await Promise.all([realpath(modules), realpath(trusted)]).catch(() => []);
|
|
107
|
+
const target = actual ? await lstat(actual).catch(() => undefined) : undefined;
|
|
108
|
+
if (actual && actual === expected && path.basename(actual) === 'node_modules' && target?.isDirectory())
|
|
109
|
+
return actual;
|
|
110
|
+
}
|
|
111
|
+
throw new ProjectDeliveryError('delivery_dependency_path_unsafe', 'Installed dependencies must be a directory');
|
|
112
|
+
}
|
|
113
|
+
/** Neither an output root nor any parent may alias source files or another directory. */
|
|
114
|
+
async function assertOutputPath(root, relative, required = false, create) {
|
|
115
|
+
const segments = relative.split('/');
|
|
116
|
+
let current = root;
|
|
117
|
+
for (let index = 0; index < segments.length; index++) {
|
|
118
|
+
current = path.join(current, segments[index]);
|
|
119
|
+
let info = await lstat(current).catch((error) => {
|
|
120
|
+
if (error.code === 'ENOENT')
|
|
121
|
+
return undefined;
|
|
122
|
+
throw error;
|
|
123
|
+
});
|
|
124
|
+
if (!info && create && index < segments.length - 1) {
|
|
125
|
+
let created = false;
|
|
126
|
+
try {
|
|
127
|
+
await mkdir(current, { mode: 0o2770 });
|
|
128
|
+
created = true;
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (error.code !== 'EEXIST')
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
if (created)
|
|
135
|
+
await create(current, true);
|
|
136
|
+
info = await lstat(current);
|
|
137
|
+
}
|
|
138
|
+
if (!info) {
|
|
139
|
+
if (required)
|
|
140
|
+
throw new ProjectDeliveryError('delivery_output_invalid', 'The output directory is missing');
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (info.isSymbolicLink())
|
|
144
|
+
throw new ProjectDeliveryError('delivery_output_symlink', 'Delivery output paths must not contain symlinks');
|
|
145
|
+
if (!info.isDirectory())
|
|
146
|
+
throw new ProjectDeliveryError('delivery_output_invalid', 'Delivery output paths must be directories');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type DeliveryOutputPublicationScope = {
|
|
2
|
+
root: string;
|
|
3
|
+
outputDirectory: string;
|
|
4
|
+
entry: string;
|
|
5
|
+
sourceFingerprint: string;
|
|
6
|
+
recipeDigest: string;
|
|
7
|
+
receiptDirectory?: string;
|
|
8
|
+
key: string;
|
|
9
|
+
};
|
|
10
|
+
export type DeliveryOutputPublicationInput = DeliveryOutputPublicationScope & {
|
|
11
|
+
stagePath: string;
|
|
12
|
+
expectedNewFingerprint: string;
|
|
13
|
+
/** Controller-only fault injection; never part of a recipe or model tool schema. */
|
|
14
|
+
checkpoint?: (phase: 'intent-durable' | 'old-moved' | 'new-published' | 'committed' | 'backup-removed') => Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
/** Read-only: a pending or corrupt publication makes inspection stale; it never restores files. */
|
|
17
|
+
export declare function hasPendingDeliveryOutputPublication(scope: DeliveryOutputPublicationScope): Promise<boolean>;
|
|
18
|
+
/** Call only after the existing preparation/execution owner has exclusive directory access. */
|
|
19
|
+
export declare function recoverDeliveryOutputPublication(scope: DeliveryOutputPublicationScope): Promise<'none' | 'published' | 'restored'>;
|
|
20
|
+
/** A durable, conditional publication of a pre-copied output tree; unknown values are never removed. */
|
|
21
|
+
export declare function publishDeliveryOutput(input: DeliveryOutputPublicationInput): Promise<string>;
|