@h-ai/tools 0.0.0-stage → 1.0.0-beta.31
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/LICENSE +202 -0
- package/README.en.md +37 -0
- package/README.ja.md +37 -0
- package/README.md +36 -2
- package/dist/chunk-UA7MOYTT.js +413 -0
- package/dist/chunk-UA7MOYTT.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.d.ts +306 -0
- package/dist/server/index.js +1051 -0
- package/dist/server/index.js.map +1 -0
- package/dist/tools-types-CM0lGuJr.d.ts +199 -0
- package/jobs/.dockerignore +4 -0
- package/jobs/README.md +223 -0
- package/jobs/archives.py +56 -0
- package/jobs/browser/Dockerfile +20 -0
- package/jobs/browser/operation.py +17 -0
- package/jobs/browser/render.mjs +90 -0
- package/jobs/browser_policy.py +36 -0
- package/jobs/documents/Dockerfile +23 -0
- package/jobs/documents/generate.py +165 -0
- package/jobs/documents/operation.py +84 -0
- package/jobs/documents/requirements.txt +3 -0
- package/jobs/hyperframes-runtime.mjs +73 -0
- package/jobs/media_files.py +78 -0
- package/jobs/ocr/Dockerfile +19 -0
- package/jobs/ocr/operation.py +18 -0
- package/jobs/office/Dockerfile +21 -0
- package/jobs/office/office_job.py +65 -0
- package/jobs/office/operation.py +43 -0
- package/jobs/preview_build/Dockerfile +19 -0
- package/jobs/preview_build/build.mjs +552 -0
- package/jobs/preview_build/operation.py +62 -0
- package/jobs/preview_build/package.json +11 -0
- package/jobs/tests/image-smoke.mjs +83 -0
- package/jobs/tests/requirements.txt +4 -0
- package/jobs/tests/test_documents_generate.py +107 -0
- package/jobs/tests/test_hai_tool.py +82 -0
- package/jobs/tests/test_media_jobs.py +449 -0
- package/jobs/tests/test_preview_build.py +213 -0
- package/jobs/tool.py +145 -0
- package/jobs/video_render/Dockerfile +20 -0
- package/jobs/video_render/operation.py +109 -0
- package/jobs/video_render/render.mjs +36 -0
- package/jobs/web_capture/Dockerfile +20 -0
- package/jobs/web_capture/capture.mjs +122 -0
- package/jobs/web_capture/operation.py +65 -0
- package/package.json +62 -3
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
/** 固定浏览器构建器:只读项目源码,不执行项目脚本、配置或依赖安装。 */
|
|
2
|
+
import { Buffer } from 'node:buffer'
|
|
3
|
+
import { readFile, realpath, stat, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { createRequire } from 'node:module'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import process from 'node:process'
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
import { parse as parseJavaScript } from 'acorn'
|
|
9
|
+
import * as esbuild from 'esbuild'
|
|
10
|
+
import { parse, serialize } from 'parse5'
|
|
11
|
+
import { compile, parse as parseComponent } from 'svelte/compiler'
|
|
12
|
+
|
|
13
|
+
const LIMIT = 1024 * 1024
|
|
14
|
+
const expectedVersions = {
|
|
15
|
+
acorn: '8.19.0',
|
|
16
|
+
esbuild: '0.28.2',
|
|
17
|
+
parse5: '8.1.0',
|
|
18
|
+
svelte: '5.57.3',
|
|
19
|
+
}
|
|
20
|
+
const require = createRequire(import.meta.url)
|
|
21
|
+
const toolDirectory = path.dirname(fileURLToPath(import.meta.url))
|
|
22
|
+
const runtimeImports = new Set([
|
|
23
|
+
'svelte',
|
|
24
|
+
'svelte/animate',
|
|
25
|
+
'svelte/easing',
|
|
26
|
+
'svelte/events',
|
|
27
|
+
'svelte/legacy',
|
|
28
|
+
'svelte/motion',
|
|
29
|
+
'svelte/store',
|
|
30
|
+
'svelte/transition',
|
|
31
|
+
'svelte/internal/client',
|
|
32
|
+
'svelte/internal/disclose-version',
|
|
33
|
+
'svelte/internal/flags/legacy',
|
|
34
|
+
'svelte/internal/flags/tracing',
|
|
35
|
+
])
|
|
36
|
+
const sourceLoaders = {
|
|
37
|
+
'.js': 'js',
|
|
38
|
+
'.mjs': 'js',
|
|
39
|
+
'.cjs': 'js',
|
|
40
|
+
'.ts': 'ts',
|
|
41
|
+
'.tsx': 'tsx',
|
|
42
|
+
'.jsx': 'jsx',
|
|
43
|
+
'.css': 'css',
|
|
44
|
+
'.json': 'json',
|
|
45
|
+
}
|
|
46
|
+
const media = {
|
|
47
|
+
'.png': 'image/png',
|
|
48
|
+
'.jpg': 'image/jpeg',
|
|
49
|
+
'.jpeg': 'image/jpeg',
|
|
50
|
+
'.gif': 'image/gif',
|
|
51
|
+
'.webp': 'image/webp',
|
|
52
|
+
'.avif': 'image/avif',
|
|
53
|
+
'.ico': 'image/x-icon',
|
|
54
|
+
'.svg': 'image/svg+xml',
|
|
55
|
+
'.woff': 'font/woff',
|
|
56
|
+
'.woff2': 'font/woff2',
|
|
57
|
+
'.ttf': 'font/ttf',
|
|
58
|
+
'.otf': 'font/otf',
|
|
59
|
+
'.mp3': 'audio/mpeg',
|
|
60
|
+
'.wav': 'audio/wav',
|
|
61
|
+
'.ogg': 'audio/ogg',
|
|
62
|
+
'.mp4': 'video/mp4',
|
|
63
|
+
'.webm': 'video/webm',
|
|
64
|
+
}
|
|
65
|
+
const unsupported = () => Object.assign(new Error('Unsupported preview project'), { code: 'UNSUPPORTED_PROJECT' })
|
|
66
|
+
const invalid = () => Object.assign(new Error('Invalid preview project'), { code: 'INVALID_PROJECT' })
|
|
67
|
+
const outputLimit = () => Object.assign(new Error('Preview output exceeds its budget'), { code: 'OUTPUT_LIMIT' })
|
|
68
|
+
function inside(root, file) {
|
|
69
|
+
const relative = path.relative(root, file)
|
|
70
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative))
|
|
71
|
+
}
|
|
72
|
+
function unsafeCharacters(value) {
|
|
73
|
+
return [...value].some(character => character === '\\' || character.charCodeAt(0) < 32)
|
|
74
|
+
}
|
|
75
|
+
const ignored = file => /^(?:package(?:-lock)?\.json|(?:pnpm-lock|yarn)\..+|tsconfig(?:\..+)?\.json|(?:vite|svelte|rollup|webpack)\.config\..+)$/i.test(path.basename(file))
|
|
76
|
+
|
|
77
|
+
/** 编译版本由镜像预装,不能从用户项目中解析工具链。 */
|
|
78
|
+
async function verifyToolchain() {
|
|
79
|
+
for (const [name, version] of Object.entries(expectedVersions)) {
|
|
80
|
+
let directory = path.dirname(require.resolve(name === 'svelte'
|
|
81
|
+
? 'svelte/compiler'
|
|
82
|
+
: name))
|
|
83
|
+
let found = false
|
|
84
|
+
while (path.dirname(directory) !== directory) {
|
|
85
|
+
try {
|
|
86
|
+
const metadata = JSON.parse(await readFile(path.join(directory, 'package.json'), 'utf8'))
|
|
87
|
+
if (metadata.name === name) {
|
|
88
|
+
found = metadata.version === version
|
|
89
|
+
break
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch {}
|
|
93
|
+
directory = path.dirname(directory)
|
|
94
|
+
}
|
|
95
|
+
if (!found)
|
|
96
|
+
throw Object.assign(new Error('Fixed preview toolchain version mismatch'), { code: 'ENVIRONMENT_UNAVAILABLE' })
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 最终代码不能保留需要运行期下载的 import、Node require 或 import.meta。 */
|
|
101
|
+
function checkBrowserCode(source) {
|
|
102
|
+
const ast = parseJavaScript(source, { ecmaVersion: 'latest', sourceType: 'module' })
|
|
103
|
+
const pending = [ast]
|
|
104
|
+
while (pending.length) {
|
|
105
|
+
const node = pending.pop()
|
|
106
|
+
if (!node || typeof node !== 'object')
|
|
107
|
+
continue
|
|
108
|
+
if (node.type === 'ImportExpression' || node.type === 'ImportDeclaration' || node.type === 'MetaProperty'
|
|
109
|
+
|| (node.type === 'CallExpression' && node.callee?.type === 'Identifier' && ['require', '__require'].includes(node.callee.name))) {
|
|
110
|
+
throw unsupported()
|
|
111
|
+
}
|
|
112
|
+
for (const value of Object.values(node)) {
|
|
113
|
+
if (Array.isArray(value))
|
|
114
|
+
pending.push(...value)
|
|
115
|
+
else if (value && typeof value === 'object')
|
|
116
|
+
pending.push(value)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** HTML 中的脚本/样式内容不能意外终止所在标签。 */
|
|
122
|
+
const scriptText = source => source.replace(/<\/script/gi, '<\\/script')
|
|
123
|
+
const styleText = source => source.replace(/<\/style/gi, '<\\/style')
|
|
124
|
+
const attribute = (node, name) => node.attrs?.find(item => item.name === name)?.value
|
|
125
|
+
function setAttribute(node, name, value) {
|
|
126
|
+
const current = node.attrs.find(item => item.name === name)
|
|
127
|
+
if (current)
|
|
128
|
+
current.value = value
|
|
129
|
+
else node.attrs.push({ name, value })
|
|
130
|
+
}
|
|
131
|
+
function setText(node, value) {
|
|
132
|
+
node.childNodes = [
|
|
133
|
+
{ nodeName: '#text', value, parentNode: node },
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
function remove(node) {
|
|
137
|
+
if (node.parentNode)
|
|
138
|
+
node.parentNode.childNodes = node.parentNode.childNodes.filter(item => item !== node)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export async function buildPreview(directory) {
|
|
142
|
+
await verifyToolchain()
|
|
143
|
+
const root = await realpath(directory)
|
|
144
|
+
const source = await readFile(path.join(root, 'index.html'))
|
|
145
|
+
if (source.byteLength > LIMIT)
|
|
146
|
+
throw invalid()
|
|
147
|
+
const document = parse(new TextDecoder('utf-8', { fatal: true }).decode(source))
|
|
148
|
+
const elements = []
|
|
149
|
+
const visit = (node) => {
|
|
150
|
+
if (node.tagName)
|
|
151
|
+
elements.push(node)
|
|
152
|
+
for (const child of node.childNodes ?? []) visit(child)
|
|
153
|
+
if (node.content)
|
|
154
|
+
visit(node.content)
|
|
155
|
+
}
|
|
156
|
+
visit(document)
|
|
157
|
+
const head = elements.find(node => node.tagName === 'head')
|
|
158
|
+
let assetBytes = 0
|
|
159
|
+
let modules = 0
|
|
160
|
+
|
|
161
|
+
async function local(specifier, base, extensions = false) {
|
|
162
|
+
if (typeof specifier !== 'string' || !specifier || unsafeCharacters(specifier)
|
|
163
|
+
|| /^[a-z][a-z\d+.-]*:/i.test(specifier) || specifier.startsWith('//') || specifier.includes('?')) {
|
|
164
|
+
throw unsupported()
|
|
165
|
+
}
|
|
166
|
+
let decoded
|
|
167
|
+
try {
|
|
168
|
+
decoded = decodeURIComponent(specifier.split('#')[0])
|
|
169
|
+
}
|
|
170
|
+
catch { throw unsupported() }
|
|
171
|
+
if (!decoded || unsafeCharacters(decoded) || decoded.startsWith('//') || /^[a-z][a-z\d+.-]*:/i.test(decoded))
|
|
172
|
+
throw unsupported()
|
|
173
|
+
const target = decoded.startsWith('/')
|
|
174
|
+
? path.resolve(root, `.${decoded}`)
|
|
175
|
+
: path.resolve(base, decoded)
|
|
176
|
+
if (!inside(root, target))
|
|
177
|
+
throw unsupported()
|
|
178
|
+
const candidates = extensions
|
|
179
|
+
? [
|
|
180
|
+
target,
|
|
181
|
+
...[
|
|
182
|
+
'.js',
|
|
183
|
+
'.mjs',
|
|
184
|
+
'.ts',
|
|
185
|
+
'.tsx',
|
|
186
|
+
'.jsx',
|
|
187
|
+
'.json',
|
|
188
|
+
'.svelte',
|
|
189
|
+
'/index.js',
|
|
190
|
+
'/index.ts',
|
|
191
|
+
].map(suffix => target + suffix),
|
|
192
|
+
]
|
|
193
|
+
: [target]
|
|
194
|
+
for (const candidate of candidates) {
|
|
195
|
+
try {
|
|
196
|
+
const resolved = await realpath(candidate)
|
|
197
|
+
const info = await stat(resolved)
|
|
198
|
+
if (!inside(root, resolved) || !info.isFile() || ignored(resolved) || resolved.split(path.sep).includes('node_modules'))
|
|
199
|
+
throw unsupported()
|
|
200
|
+
return resolved
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (!['ENOENT', 'ENOTDIR'].includes(error.code))
|
|
204
|
+
throw error
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
throw unsupported()
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function inspectSvg(contents) {
|
|
211
|
+
const tree = parse(contents)
|
|
212
|
+
const pending = [tree]
|
|
213
|
+
while (pending.length) {
|
|
214
|
+
const node = pending.pop()
|
|
215
|
+
if ([
|
|
216
|
+
'script',
|
|
217
|
+
'foreignObject',
|
|
218
|
+
'iframe',
|
|
219
|
+
'object',
|
|
220
|
+
'embed',
|
|
221
|
+
].includes(node.tagName)) {
|
|
222
|
+
throw unsupported()
|
|
223
|
+
}
|
|
224
|
+
for (const item of node.attrs ?? []) {
|
|
225
|
+
if (item.name.startsWith('on') || (['href', 'src'].includes(item.name) && !item.value.startsWith('#'))
|
|
226
|
+
|| (item.name === 'style' && /@import|url\(\s*[^#]/i.test(item.value))) {
|
|
227
|
+
throw unsupported()
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
pending.push(...(node.childNodes ?? []))
|
|
231
|
+
}
|
|
232
|
+
if (/@import|url\(\s*["']?[^\s#"']/i.test(contents))
|
|
233
|
+
throw unsupported()
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function readMedia(file) {
|
|
237
|
+
const type = media[path.extname(file).toLowerCase()]
|
|
238
|
+
if (!type)
|
|
239
|
+
throw unsupported()
|
|
240
|
+
const data = await readFile(file)
|
|
241
|
+
assetBytes += data.byteLength
|
|
242
|
+
if (assetBytes > 8 * LIMIT)
|
|
243
|
+
throw outputLimit()
|
|
244
|
+
if (type === 'image/svg+xml')
|
|
245
|
+
inspectSvg(new TextDecoder('utf-8', { fatal: true }).decode(data))
|
|
246
|
+
return { data, type }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function dataUrl(specifier, base) {
|
|
250
|
+
const file = await local(specifier, base)
|
|
251
|
+
const { data, type } = await readMedia(file)
|
|
252
|
+
return `data:${type};base64,${data.toString('base64')}${specifier.includes('#')
|
|
253
|
+
? `#${specifier.split('#').slice(1).join('#')}`
|
|
254
|
+
: ''}`
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** 仅重写模板 AST 的静态资源属性;动态资源继续使用源码中的本地 import。 */
|
|
258
|
+
async function componentResources(contents, file) {
|
|
259
|
+
const tree = parseComponent(contents, { modern: true })
|
|
260
|
+
const pending = [tree.fragment]
|
|
261
|
+
const replacements = []
|
|
262
|
+
while (pending.length) {
|
|
263
|
+
const node = pending.pop()
|
|
264
|
+
if (!node || typeof node !== 'object')
|
|
265
|
+
continue
|
|
266
|
+
if (['RegularElement', 'SvelteElement'].includes(node.type)) {
|
|
267
|
+
for (const item of node.attributes) {
|
|
268
|
+
if (item.type !== 'Attribute')
|
|
269
|
+
continue
|
|
270
|
+
if (item.name === 'srcset')
|
|
271
|
+
throw unsupported()
|
|
272
|
+
if (!['src', 'poster'].includes(item.name))
|
|
273
|
+
continue
|
|
274
|
+
let value
|
|
275
|
+
if (Array.isArray(item.value) && item.value.every(part => part.type === 'Text'))
|
|
276
|
+
value = item.value.map(part => part.data).join('')
|
|
277
|
+
else if (item.value?.type === 'ExpressionTag' && item.value.expression.type === 'Literal' && typeof item.value.expression.value === 'string')
|
|
278
|
+
value = item.value.expression.value
|
|
279
|
+
if (value) {
|
|
280
|
+
const embedded = await dataUrl(value, path.dirname(file))
|
|
281
|
+
replacements.push({ start: item.start, end: item.end, contents: `${item.name}={${JSON.stringify(embedded)}}` })
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
for (const child of Object.values(node)) {
|
|
286
|
+
if (Array.isArray(child))
|
|
287
|
+
pending.push(...child)
|
|
288
|
+
else if (child && typeof child === 'object')
|
|
289
|
+
pending.push(child)
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
for (const replacement of replacements.sort((left, right) => right.start - left.start))
|
|
293
|
+
contents = contents.slice(0, replacement.start) + replacement.contents + contents.slice(replacement.end)
|
|
294
|
+
return contents
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const componentStyles = new Map()
|
|
298
|
+
const plugin = {
|
|
299
|
+
name: 'fixed-local-project',
|
|
300
|
+
setup(build) {
|
|
301
|
+
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
302
|
+
if (args.pluginData?.trustedResolve)
|
|
303
|
+
return undefined
|
|
304
|
+
if (args.importer && !inside(root, args.importer))
|
|
305
|
+
return undefined
|
|
306
|
+
if (args.path === 'hai-component-style' && componentStyles.has(args.importer))
|
|
307
|
+
return { path: args.importer, namespace: 'component-style' }
|
|
308
|
+
if (runtimeImports.has(args.path)) {
|
|
309
|
+
const resolved = await build.resolve(args.path, {
|
|
310
|
+
resolveDir: toolDirectory,
|
|
311
|
+
kind: args.kind,
|
|
312
|
+
pluginData: { trustedResolve: true },
|
|
313
|
+
})
|
|
314
|
+
if (resolved.errors.length || !resolved.path)
|
|
315
|
+
throw unsupported()
|
|
316
|
+
return { path: resolved.path }
|
|
317
|
+
}
|
|
318
|
+
if (args.kind === 'url-token' && args.path.startsWith('#'))
|
|
319
|
+
return { path: args.path, external: true }
|
|
320
|
+
if (!args.path.startsWith('.') && !args.path.startsWith('/') && !['url-token', 'import-rule'].includes(args.kind))
|
|
321
|
+
throw unsupported()
|
|
322
|
+
const file = await local(args.path, args.resolveDir || root, true)
|
|
323
|
+
return { path: file }
|
|
324
|
+
})
|
|
325
|
+
build.onLoad({ filter: /.*/, namespace: 'component-style' }, args => ({
|
|
326
|
+
contents: componentStyles.get(args.path),
|
|
327
|
+
loader: 'css',
|
|
328
|
+
resolveDir: path.dirname(args.path),
|
|
329
|
+
}))
|
|
330
|
+
build.onLoad({ filter: /.*/ }, async (args) => {
|
|
331
|
+
if (!inside(root, args.path))
|
|
332
|
+
return undefined
|
|
333
|
+
const extension = path.extname(args.path).toLowerCase()
|
|
334
|
+
const bytes = await readFile(args.path)
|
|
335
|
+
if (bytes.byteLength > LIMIT && !media[extension])
|
|
336
|
+
throw invalid()
|
|
337
|
+
if (media[extension])
|
|
338
|
+
return { contents: (await readMedia(args.path)).data, loader: 'dataurl' }
|
|
339
|
+
const contents = new TextDecoder('utf-8', { fatal: true }).decode(bytes)
|
|
340
|
+
if (extension === '.svelte') {
|
|
341
|
+
const compiled = compile(await componentResources(contents, args.path), {
|
|
342
|
+
filename: path.relative(root, args.path),
|
|
343
|
+
generate: 'client',
|
|
344
|
+
dev: false,
|
|
345
|
+
css: 'external',
|
|
346
|
+
})
|
|
347
|
+
if (compiled.css?.code)
|
|
348
|
+
componentStyles.set(args.path, compiled.css.code)
|
|
349
|
+
return {
|
|
350
|
+
contents: compiled.js.code + (compiled.css?.code ? '\nimport "hai-component-style";' : ''),
|
|
351
|
+
loader: 'js',
|
|
352
|
+
resolveDir: path.dirname(args.path),
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (!sourceLoaders[extension])
|
|
356
|
+
throw unsupported()
|
|
357
|
+
return {
|
|
358
|
+
contents,
|
|
359
|
+
loader: sourceLoaders[extension],
|
|
360
|
+
resolveDir: path.dirname(args.path),
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
},
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async function bundle(contents, loader, sourcefile, asModule = true) {
|
|
367
|
+
try {
|
|
368
|
+
const result = await esbuild.build({
|
|
369
|
+
stdin: {
|
|
370
|
+
contents,
|
|
371
|
+
loader,
|
|
372
|
+
sourcefile,
|
|
373
|
+
resolveDir: path.dirname(sourcefile),
|
|
374
|
+
},
|
|
375
|
+
absWorkingDir: root,
|
|
376
|
+
bundle: asModule,
|
|
377
|
+
write: false,
|
|
378
|
+
outfile: path.join(root, '__preview_out.js'),
|
|
379
|
+
...(loader === 'css'
|
|
380
|
+
? {
|
|
381
|
+
outfile: path.join(root, '__preview_out.css'),
|
|
382
|
+
}
|
|
383
|
+
: {
|
|
384
|
+
format: asModule ? 'iife' : undefined,
|
|
385
|
+
}),
|
|
386
|
+
platform: 'browser',
|
|
387
|
+
target: 'es2022',
|
|
388
|
+
conditions: ['browser', 'production'],
|
|
389
|
+
tsconfigRaw: { compilerOptions: {} },
|
|
390
|
+
plugins: [plugin],
|
|
391
|
+
logLevel: 'silent',
|
|
392
|
+
metafile: true,
|
|
393
|
+
define: {
|
|
394
|
+
'import.meta.env': JSON.stringify({
|
|
395
|
+
MODE: 'production',
|
|
396
|
+
BASE_URL: './',
|
|
397
|
+
PROD: true,
|
|
398
|
+
DEV: false,
|
|
399
|
+
SSR: false,
|
|
400
|
+
}),
|
|
401
|
+
},
|
|
402
|
+
})
|
|
403
|
+
let code = ''
|
|
404
|
+
let css = ''
|
|
405
|
+
for (const output of result.outputFiles) {
|
|
406
|
+
if (output.path.endsWith('.css')) {
|
|
407
|
+
css += output.text
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
checkBrowserCode(output.text)
|
|
411
|
+
code += output.text
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
for (const output of Object.values(result.metafile.outputs)) {
|
|
415
|
+
if (output.imports.some(item => item.external && !item.path.startsWith('#')))
|
|
416
|
+
throw unsupported()
|
|
417
|
+
}
|
|
418
|
+
return { code, css }
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
if (error.code)
|
|
422
|
+
throw error
|
|
423
|
+
if (error.errors?.some(item => item.detail?.code === 'UNSUPPORTED_PROJECT'))
|
|
424
|
+
throw unsupported()
|
|
425
|
+
throw invalid()
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function appendStyle(css) {
|
|
430
|
+
if (!css)
|
|
431
|
+
return
|
|
432
|
+
const node = {
|
|
433
|
+
nodeName: 'style',
|
|
434
|
+
tagName: 'style',
|
|
435
|
+
namespaceURI: 'http://www.w3.org/1999/xhtml',
|
|
436
|
+
attrs: [],
|
|
437
|
+
childNodes: [],
|
|
438
|
+
parentNode: head,
|
|
439
|
+
}
|
|
440
|
+
setText(node, styleText(css))
|
|
441
|
+
head.childNodes.push(node)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
for (const node of elements) {
|
|
445
|
+
const tag = node.tagName
|
|
446
|
+
if (['base', 'iframe', 'object', 'embed'].includes(tag) || (tag === 'meta' && attribute(node, 'http-equiv')?.toLowerCase() === 'refresh'))
|
|
447
|
+
throw unsupported()
|
|
448
|
+
if (tag === 'script') {
|
|
449
|
+
const type = (attribute(node, 'type') ?? '').toLowerCase()
|
|
450
|
+
if (![
|
|
451
|
+
'',
|
|
452
|
+
'module',
|
|
453
|
+
'text/javascript',
|
|
454
|
+
'application/javascript',
|
|
455
|
+
].includes(type)) {
|
|
456
|
+
if (attribute(node, 'src'))
|
|
457
|
+
throw unsupported()
|
|
458
|
+
continue
|
|
459
|
+
}
|
|
460
|
+
if (attribute(node, 'async') !== undefined || attribute(node, 'defer') !== undefined)
|
|
461
|
+
throw unsupported()
|
|
462
|
+
if (type === 'module' && ++modules > 1)
|
|
463
|
+
throw unsupported()
|
|
464
|
+
const src = attribute(node, 'src')
|
|
465
|
+
const file = src
|
|
466
|
+
? await local(src, root)
|
|
467
|
+
: path.join(root, '__inline.js')
|
|
468
|
+
const code = src
|
|
469
|
+
? new TextDecoder('utf-8', { fatal: true }).decode(await readFile(file))
|
|
470
|
+
: (node.childNodes ?? []).map(child => child.value ?? '').join('')
|
|
471
|
+
const built = await bundle(code, sourceLoaders[path.extname(file)] ?? 'js', file, type === 'module')
|
|
472
|
+
node.attrs = node.attrs.filter(item => !['src', 'integrity', 'crossorigin'].includes(item.name))
|
|
473
|
+
setText(node, scriptText(built.code))
|
|
474
|
+
appendStyle(built.css)
|
|
475
|
+
}
|
|
476
|
+
else if (tag === 'link') {
|
|
477
|
+
const rel = (attribute(node, 'rel') ?? '').toLowerCase()
|
|
478
|
+
const href = attribute(node, 'href')
|
|
479
|
+
if (rel === 'modulepreload') {
|
|
480
|
+
remove(node)
|
|
481
|
+
continue
|
|
482
|
+
}
|
|
483
|
+
if (rel === 'stylesheet') {
|
|
484
|
+
if (!href)
|
|
485
|
+
throw unsupported()
|
|
486
|
+
const file = await local(href, root)
|
|
487
|
+
const css = new TextDecoder('utf-8', { fatal: true }).decode(await readFile(file))
|
|
488
|
+
const built = await bundle(css, 'css', file)
|
|
489
|
+
node.nodeName = node.tagName = 'style'
|
|
490
|
+
node.attrs = node.attrs.filter(item => item.name === 'media' || item.name === 'title')
|
|
491
|
+
setText(node, styleText(built.css))
|
|
492
|
+
}
|
|
493
|
+
else if (rel.includes('icon') && href) {
|
|
494
|
+
setAttribute(node, 'href', await dataUrl(href, root))
|
|
495
|
+
}
|
|
496
|
+
else if (href && !['canonical', 'alternate'].includes(rel)) {
|
|
497
|
+
throw unsupported()
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
else if (tag === 'style') {
|
|
501
|
+
const css = (node.childNodes ?? []).map(child => child.value ?? '').join('')
|
|
502
|
+
setText(node, styleText((await bundle(css, 'css', path.join(root, '__inline.css'))).css))
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
for (const key of ['src', 'poster']) {
|
|
506
|
+
const value = attribute(node, key)
|
|
507
|
+
if (value)
|
|
508
|
+
setAttribute(node, key, await dataUrl(value, root))
|
|
509
|
+
}
|
|
510
|
+
if (attribute(node, 'srcset'))
|
|
511
|
+
throw unsupported()
|
|
512
|
+
}
|
|
513
|
+
const styles = attribute(node, 'style')
|
|
514
|
+
if (styles) {
|
|
515
|
+
const css = (await bundle(`x{${styles}}`, 'css', path.join(root, '__attribute.css'))).css
|
|
516
|
+
setAttribute(node, 'style', css.slice(css.indexOf('{') + 1, css.lastIndexOf('}')).trim())
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
document.childNodes = document.childNodes.filter(node => node.nodeName !== '#documentType')
|
|
520
|
+
const html = `<!doctype html>${serialize(document)}`
|
|
521
|
+
if (Buffer.byteLength(html) > LIMIT)
|
|
522
|
+
throw outputLimit()
|
|
523
|
+
return html
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
async function main() {
|
|
527
|
+
const [directory, output, status] = process.argv.slice(2)
|
|
528
|
+
let result
|
|
529
|
+
try {
|
|
530
|
+
const html = await buildPreview(directory)
|
|
531
|
+
await writeFile(output, html, { flag: 'wx' })
|
|
532
|
+
result = { success: true }
|
|
533
|
+
}
|
|
534
|
+
catch (error) {
|
|
535
|
+
result = {
|
|
536
|
+
success: false,
|
|
537
|
+
code: [
|
|
538
|
+
'UNSUPPORTED_PROJECT',
|
|
539
|
+
'INVALID_PROJECT',
|
|
540
|
+
'OUTPUT_LIMIT',
|
|
541
|
+
'ENVIRONMENT_UNAVAILABLE',
|
|
542
|
+
].includes(error.code)
|
|
543
|
+
? error.code
|
|
544
|
+
: 'INVALID_PROJECT',
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
finally { esbuild.stop() }
|
|
548
|
+
await writeFile(status, JSON.stringify(result), { flag: 'wx' })
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url))
|
|
552
|
+
main().catch(() => { process.exitCode = 1 })
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""离线前端预览构建作业,项目代码只编译,不在构建宿主执行。"""
|
|
2
|
+
import json
|
|
3
|
+
import pathlib
|
|
4
|
+
import zipfile
|
|
5
|
+
from media_files import archive_path, extract_project
|
|
6
|
+
from tool import ToolError, publish, run_command
|
|
7
|
+
|
|
8
|
+
PROJECT_LIMIT = 8 * 1024 * 1024
|
|
9
|
+
SOURCE_LIMIT = 1024 * 1024
|
|
10
|
+
SOURCE_EXTENSIONS = frozenset({'.html', '.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.svelte', '.css', '.json'})
|
|
11
|
+
ERROR_CODES = frozenset({'INVALID_PROJECT', 'UNSUPPORTED_PROJECT', 'OUTPUT_LIMIT', 'ENVIRONMENT_UNAVAILABLE'})
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def build(request, source, artifact, workspace):
|
|
15
|
+
if source.suffix.lower() != '.zip' or artifact.suffix.lower() != '.html':
|
|
16
|
+
raise ToolError('INVALID_REQUEST', 'Preview building requires a ZIP project and HTML output')
|
|
17
|
+
try:
|
|
18
|
+
with zipfile.ZipFile(source) as archive:
|
|
19
|
+
entries = archive.infolist()
|
|
20
|
+
if sum(entry.file_size for entry in entries) > PROJECT_LIMIT:
|
|
21
|
+
raise ToolError('UNSAFE_PROJECT', 'Preview project exceeds the extraction budget')
|
|
22
|
+
for entry in entries:
|
|
23
|
+
name = archive_path(entry.filename)
|
|
24
|
+
if any(part.casefold() == 'node_modules' for part in name.parts):
|
|
25
|
+
raise ToolError('UNSUPPORTED_PROJECT', 'Project dependencies must not be bundled in the archive')
|
|
26
|
+
if name.suffix.lower() in SOURCE_EXTENSIONS and entry.file_size > SOURCE_LIMIT:
|
|
27
|
+
raise ToolError('UNSAFE_PROJECT', 'Preview source exceeds its size budget')
|
|
28
|
+
except (zipfile.BadZipFile, RuntimeError, EOFError, NotImplementedError):
|
|
29
|
+
raise ToolError('UNSAFE_PROJECT', 'Invalid or unsupported project archive') from None
|
|
30
|
+
directory = workspace / 'project'
|
|
31
|
+
directory.mkdir()
|
|
32
|
+
extract_project(source, directory)
|
|
33
|
+
if not (directory / 'index.html').is_file():
|
|
34
|
+
raise ToolError('INVALID_PROJECT', 'Preview project requires index.html at its root')
|
|
35
|
+
result = workspace / 'preview.html'
|
|
36
|
+
status = workspace / 'build-result.json'
|
|
37
|
+
worker = pathlib.Path(__file__).with_name('build.mjs')
|
|
38
|
+
run_command(['node', str(worker), str(directory), str(result), str(status)], 60, workspace)
|
|
39
|
+
try:
|
|
40
|
+
if status.stat().st_size > 4096:
|
|
41
|
+
raise ValueError
|
|
42
|
+
outcome = json.loads(status.read_text(encoding='utf-8'))
|
|
43
|
+
if not isinstance(outcome, dict) or type(outcome.get('success')) is not bool:
|
|
44
|
+
raise ValueError
|
|
45
|
+
if not outcome['success']:
|
|
46
|
+
code = outcome.get('code')
|
|
47
|
+
if code not in ERROR_CODES:
|
|
48
|
+
raise ValueError
|
|
49
|
+
raise ToolError(code, {
|
|
50
|
+
'INVALID_PROJECT': 'Project source could not be compiled',
|
|
51
|
+
'UNSUPPORTED_PROJECT': 'Project contains an unsupported dependency, resource or browser feature',
|
|
52
|
+
'OUTPUT_LIMIT': 'Self-contained preview exceeds the output budget',
|
|
53
|
+
'ENVIRONMENT_UNAVAILABLE': 'The fixed preview toolchain is unavailable',
|
|
54
|
+
}[code])
|
|
55
|
+
if not result.is_file() or not 0 < result.stat().st_size <= SOURCE_LIMIT:
|
|
56
|
+
raise ValueError
|
|
57
|
+
except (OSError, ValueError, KeyError, TypeError):
|
|
58
|
+
raise ToolError('INVALID_OUTPUT', 'Preview builder did not produce a valid artifact') from None
|
|
59
|
+
return publish(result, artifact, b'<!doctype html>')
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
commands = {'preview.build': ({'input', 'output'}, set(), build)}
|