FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ZCode/scripts/desktop-agent-bytecode-runtime.cjs at main · DeepenKernel/ZCode · GitHub
DeepenKernel
ZCode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ZCode
/
scripts
/
desktop-agent-bytecode-runtime.cjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (60 loc) · 2.4 KB
Breadcrumbs
ZCode
/
scripts
/
desktop-agent-bytecode-runtime.cjs
Copy path
File metadata and controls
65 lines (60 loc) · 2.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use strict"
;
const
v8
=
require
(
"node:v8"
)
;
const
vm
=
require
(
"node:vm"
)
;
const
{
createHash
}
=
require
(
"node:crypto"
)
;
const
{
readFile
}
=
require
(
"node:fs/promises"
)
;
const
{
basename
,
dirname
,
join
}
=
require
(
"node:path"
)
;
function
configureBytecodeRuntime
(
)
{
// 字节码不带可重新编译的源码,必须完整编译并保留字节码;编译器和加载器共用这一处配置。
v8
.
setFlagsFromString
(
"--no-lazy --no-flush-bytecode"
)
;
return
{
electron
:
process
.
versions
.
electron
??
null
,
node
:
process
.
versions
.
node
,
v8
:
process
.
versions
.
v8
,
platform
:
process
.
platform
,
arch
:
process
.
arch
,
cachedDataVersionTag
:
v8
.
cachedDataVersionTag
(
)
,
}
;
}
function
bytecodeDigest
(
buffer
)
{
return
createHash
(
"sha256"
)
.
update
(
buffer
)
.
digest
(
"hex"
)
;
}
async
function
loadBytecode
(
metadata
,
targetModule
,
targetRequire
)
{
const
runtime
=
configureBytecodeRuntime
(
)
;
if
(
JSON
.
stringify
(
runtime
)
!==
JSON
.
stringify
(
metadata
.
runtime
)
)
{
throw
new
Error
(
"字节码运行时不匹配,请用当前 Electron 重新运行 pnpm build:desktop-agent:bytecode"
,
)
;
}
const
directory
=
dirname
(
targetModule
.
filename
)
;
if
(
basename
(
metadata
.
bytecodeFile
)
!==
metadata
.
bytecodeFile
)
{
throw
new
Error
(
"无效的字节码文件名"
)
;
}
const
cachedData
=
await
readFile
(
join
(
directory
,
metadata
.
bytecodeFile
)
)
;
if
(
bytecodeDigest
(
cachedData
)
!==
metadata
.
bytecodeSha256
)
{
throw
new
Error
(
"字节码摘要不匹配,请重新构建桌面 Agent"
)
;
}
// 使用 ASCII 空格而非双字节零宽字符。这里只消除明文源码,仍保留等长占位内存。
const
source
=
" "
.
repeat
(
metadata
.
sourceLength
)
;
const
filename
=
join
(
directory
,
metadata
.
sourceFile
)
;
const
script
=
new
vm
.
Script
(
source
,
{
cachedData
,
filename
,
// 动态 import 必须交回 Node,继续按原 bundle 的 URL 解析外置 ESM 与原生依赖。
importModuleDynamically
:
vm
.
constants
.
USE_MAIN_CONTEXT_DEFAULT_LOADER
,
}
)
;
if
(
script
.
cachedDataRejected
)
{
throw
new
Error
(
"V8 拒绝字节码缓存,请重新构建桌面 Agent"
)
;
}
const
run
=
script
.
runInThisContext
(
)
;
if
(
typeof
run
!==
"function"
)
throw
new
Error
(
"字节码不是 CommonJS 模块"
)
;
run
.
call
(
targetModule
.
exports
,
targetModule
.
exports
,
targetRequire
,
targetModule
,
filename
,
directory
,
)
;
}
module
.
exports
=
{
configureBytecodeRuntime
,
bytecodeDigest
,
loadBytecode
}
;
Back
|
FazBrowse Home
|
New Git URL