fix: make client integration kit self-contained and clarify capabilities
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-13 15:22:09 +08:00
parent d42067746c
commit 5353a4a128
15 changed files with 198 additions and 32 deletions

View File

@@ -8,11 +8,11 @@ import { UpdateClient, UpdateError, verifyCheck } from './update-sdk.mjs';
const { privateKey, publicKey } = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });
const now = Date.now();
const payload = {version:'update-v1',instance_id:'i1',product_id:1,customer_id:2,current_version:'1.0',platform:'linux',arch:'x64',channel:'stable',server_time:new Date(now).toISOString().replace('.000Z','Z'),expires_at:new Date(now+600000).toISOString().replace('.000Z','Z'),policy_revision:3,policy_enabled:true,recommended_version:'2.0',minimum_supported_version:'1.0',enforce_after:null,mandatory:false,required_now:false,update_available:true,download_allowed:true,release_id:7,sha256:createHash('sha256').update('good').digest('hex'),size_bytes:4,message:''};
const payload = {version:'update-v1',instance_id:'i1',product_id:1,customer_id:2,current_version:'1.0.0',platform:'linux',arch:'x64',channel:'stable',server_time:new Date(now).toISOString().replace('.000Z','Z'),expires_at:new Date(now+600000).toISOString().replace('.000Z','Z'),policy_revision:3,policy_enabled:true,recommended_version:'2.0.0',minimum_supported_version:'1.0.0',enforce_after:'',mandatory:false,required_now:false,update_available:true,download_allowed:true,release_id:7,sha256:createHash('sha256').update('good').digest('hex'),size_bytes:4,message:''};
function envelope(p = payload) { const raw = Buffer.from(JSON.stringify(p)); return {payload:raw.toString('base64').replace(/=+$/,''),signature:sign('sha256',raw,privateKey).toString('base64').replace(/=+$/,''),key_id:'k1'}; }
const keys = {k1:publicKey.export({type:'spki',format:'pem'})};
const signed = envelope();
verifyCheck(signed, keys, 'i1', {now,expectedPlatform:'linux',expectedArch:'x64',expectedChannel:'stable',expectedCurrentVersion:'1.0'});
verifyCheck(signed, keys, 'i1', {now,expectedPlatform:'linux',expectedArch:'x64',expectedChannel:'stable',expectedCurrentVersion:'1.0.0'});
const tampered = {...signed, payload:Buffer.from('{"version":"update-v1"}').toString('base64').replace(/=+$/,'')};
assert.throws(() => verifyCheck(tampered, keys, 'i1', {now}), UpdateError);
assert.throws(() => verifyCheck(signed, keys, 'wrong', {now}), UpdateError);
@@ -33,5 +33,5 @@ const state = {load:()=>persisted,saveAtomic:value=>{persisted=value;}};
const fetchImpl = async () => Response.json({data:signed});
const client = new UpdateClient({baseUrl:'https://license.example',instanceId:'i1',secret:'secret',publicKeys:keys,state,fetchImpl,clock:()=>now});
persisted={scope:JSON.stringify(['i1','windows','x64','stable']),policy_revision:99};
assert.equal((await client.check('1.0','linux','x64','stable')).payload.policy_revision,3);
assert.equal((await client.check('1.0.0','linux','x64','stable')).payload.policy_revision,3);
console.log('Node update SDK negative tests passed');