11// CommonJS fixture server for ClawHub package/install E2E scenarios.
2+ const { execFileSync } = require ( "node:child_process" ) ;
23const crypto = require ( "node:crypto" ) ;
34const fs = require ( "node:fs" ) ;
45const http = require ( "node:http" ) ;
@@ -8,12 +9,150 @@ const { createRequire } = require("node:module");
89
910const profile = process . argv [ 2 ] ;
1011const portFile = process . argv [ 3 ] ;
12+ const artifactManifestFile = process . argv [ 4 ] ;
1113const requireFromApp = createRequire ( path . join ( process . cwd ( ) , "package.json" ) ) ;
1214const JSZip = requireFromApp ( "jszip" ) ;
1315const tar = requireFromApp ( "tar" ) ;
1416const packageName = "@openclaw/kitchen-sink" ;
1517const pluginId = "openclaw-kitchen-sink-fixture" ;
1618
19+ function startPrepublishArtifactServer ( ) {
20+ const manifest = JSON . parse ( fs . readFileSync ( artifactManifestFile , "utf8" ) ) ;
21+ if ( ! Array . isArray ( manifest . packages ) || manifest . packages . length === 0 ) {
22+ throw new Error ( "prepublish artifact manifest must contain packages" ) ;
23+ }
24+ const artifacts = new Map (
25+ manifest . packages . map ( ( entry ) => {
26+ if (
27+ typeof entry . name !== "string" ||
28+ typeof entry . version !== "string" ||
29+ typeof entry . tarball !== "string" ||
30+ path . basename ( entry . tarball ) !== entry . tarball
31+ ) {
32+ throw new Error ( "invalid prepublish artifact manifest entry" ) ;
33+ }
34+ const tarballPath = path . join ( path . dirname ( artifactManifestFile ) , entry . tarball ) ;
35+ const archive = fs . readFileSync ( tarballPath ) ;
36+ const sha256 = crypto . createHash ( "sha256" ) . update ( archive ) . digest ( "hex" ) ;
37+ const packedPackage = JSON . parse (
38+ execFileSync ( "tar" , [ "-xOf" , tarballPath , "package/package.json" ] , {
39+ encoding : "utf8" ,
40+ } ) ,
41+ ) ;
42+ const packedPlugin = JSON . parse (
43+ execFileSync ( "tar" , [ "-xOf" , tarballPath , "package/openclaw.plugin.json" ] , {
44+ encoding : "utf8" ,
45+ } ) ,
46+ ) ;
47+ if (
48+ sha256 !== entry . sha256 ||
49+ packedPackage . name !== entry . name ||
50+ packedPackage . version !== entry . version ||
51+ typeof packedPlugin . id !== "string" ||
52+ packedPlugin . id . length === 0
53+ ) {
54+ throw new Error ( `prepublish artifact metadata mismatch for ${ entry . name } ` ) ;
55+ }
56+ return [
57+ entry . name ,
58+ {
59+ ...entry ,
60+ archive,
61+ runtimeId : packedPlugin . id ,
62+ npmIntegrity : `sha512-${ crypto . createHash ( "sha512" ) . update ( archive ) . digest ( "base64" ) } ` ,
63+ npmShasum : crypto . createHash ( "sha1" ) . update ( archive ) . digest ( "hex" ) ,
64+ } ,
65+ ] ;
66+ } ) ,
67+ ) ;
68+ const requestLog = [ ] ;
69+ const json = ( response , value , status = 200 ) => {
70+ response . writeHead ( status , { "content-type" : "application/json" } ) ;
71+ response . end ( `${ JSON . stringify ( value ) } \n` ) ;
72+ } ;
73+ const server = http . createServer ( ( request , response ) => {
74+ const url = new URL ( request . url , "http://127.0.0.1" ) ;
75+ if ( url . pathname === "/__fixture__/requests" ) {
76+ json ( response , { requests : requestLog } ) ;
77+ return ;
78+ }
79+ requestLog . push ( `${ request . method } ${ url . pathname } ${ url . search } ` ) ;
80+ const match =
81+ / ^ \/ a p i \/ v 1 \/ p a c k a g e s \/ ( [ ^ / ] + ) (?: \/ v e r s i o n s \/ ( [ ^ / ] + ) (?: \/ ( a r t i f a c t (?: \/ d o w n l o a d ) ? | s e c u r i t y ) ) ? ) ? $ / u. exec (
82+ url . pathname ,
83+ ) ;
84+ const entry = match ? artifacts . get ( decodeURIComponent ( match [ 1 ] ) ) : undefined ;
85+ if ( request . method !== "GET" || ! entry ) {
86+ response . writeHead ( request . method === "GET" ? 404 : 405 ) ;
87+ response . end ( request . method === "GET" ? "not found" : "method not allowed" ) ;
88+ return ;
89+ }
90+ const version = match [ 2 ] ? decodeURIComponent ( match [ 2 ] ) : undefined ;
91+ if ( version && version !== entry . version ) {
92+ json ( response , { error : "version not found" } , 404 ) ;
93+ return ;
94+ }
95+ const packageRecord = {
96+ name : entry . name ,
97+ family : "code-plugin" ,
98+ runtimeId : entry . runtimeId ,
99+ } ;
100+ const artifact = {
101+ kind : "npm-pack" ,
102+ sha256 : entry . sha256 ,
103+ npmIntegrity : entry . npmIntegrity ,
104+ npmShasum : entry . npmShasum ,
105+ } ;
106+ const versionRecord = {
107+ version : entry . version ,
108+ artifact,
109+ } ;
110+ if ( ! version ) {
111+ json ( response , {
112+ package : {
113+ ...packageRecord ,
114+ channel : "official" ,
115+ isOfficial : true ,
116+ latestVersion : entry . version ,
117+ tags : { latest : entry . version , beta : entry . version } ,
118+ } ,
119+ } ) ;
120+ } else if ( ! match [ 3 ] ) {
121+ json ( response , { package : packageRecord , version : versionRecord } ) ;
122+ } else if ( match [ 3 ] === "security" ) {
123+ json ( response , {
124+ trust : {
125+ blockedFromDownload : false ,
126+ reasons : [ ] ,
127+ pending : false ,
128+ stale : false ,
129+ } ,
130+ } ) ;
131+ } else if ( match [ 3 ] === "artifact" ) {
132+ json ( response , {
133+ version : versionRecord ,
134+ artifact : {
135+ artifactKind : "npm-pack" ,
136+ artifactSha256 : entry . sha256 ,
137+ npmIntegrity : entry . npmIntegrity ,
138+ } ,
139+ } ) ;
140+ } else {
141+ response . writeHead ( 200 , {
142+ "content-type" : "application/octet-stream" ,
143+ "X-ClawHub-Artifact-Sha256" : entry . sha256 ,
144+ "X-ClawHub-Npm-Integrity" : entry . npmIntegrity ,
145+ "X-ClawHub-Npm-Shasum" : entry . npmShasum ,
146+ "X-ClawHub-Npm-Tarball-Name" : entry . tarball ,
147+ } ) ;
148+ response . end ( entry . archive ) ;
149+ }
150+ } ) ;
151+ server . listen ( 0 , "127.0.0.1" , ( ) => {
152+ fs . writeFileSync ( portFile , String ( server . address ( ) . port ) ) ;
153+ } ) ;
154+ }
155+
17156const buildArtifactSummary = ( {
18157 clawpackSha256,
19158 clawpackSize,
@@ -440,8 +579,12 @@ profiles["catalog-search"] = {
440579
441580const fixture = profiles [ profile ] ;
442581if ( ! fixture || ! portFile ) {
582+ if ( profile === "prepublish-artifacts" && portFile && artifactManifestFile ) {
583+ startPrepublishArtifactServer ( ) ;
584+ return ;
585+ }
443586 console . error (
444- "usage: clawhub-fixture-server.cjs <catalog-search|kitchen-sink-plugin|plugins> <port-file>" ,
587+ "usage: clawhub-fixture-server.cjs <catalog-search|kitchen-sink-plugin|plugins|prepublish-artifacts > <port-file> [manifest-file] " ,
445588 ) ;
446589 process . exit ( 1 ) ;
447590}
0 commit comments