11import type { NextRequest } from "next/server" ;
22import type { Bundle } from "@cloud-push/cloud" ;
3- import { dbNodeClient , storageNodeClient } from "@/cloud-push.server" ;
4- import { createManifest } from "@cloud-push/next/node" ;
3+ import cloudPushConfig , {
4+ dbNodeClient ,
5+ storageNodeClient ,
6+ } from "@/cloud-push.server" ;
7+ import { createManifest , createSignature } from "@cloud-push/next/node" ;
58import {
69 type Directive ,
710 ErrorResponse ,
@@ -21,6 +24,7 @@ export async function GET(request: NextRequest) {
2124 platform,
2225 protocolVersion,
2326 runtimeVersion,
27+ expectSignature,
2428 } = parseHeaders ( {
2529 headers : request . headers ,
2630 url : new URL ( request . url ) ,
@@ -76,14 +80,41 @@ export async function GET(request: NextRequest) {
7680 }
7781 }
7882
79- if ( ! nextBundle ) {
83+ if ( ! currentBundle && ! nextBundle ) {
8084 const directive : Directive = {
8185 type : "rollBackToEmbedded" ,
86+ parameters : {
87+ commitTime : new Date ( ) . toISOString ( ) ,
88+ } ,
8289 } ;
83- return UpdateResponse ( { bundleId : embeddedUpdateId , directive } ) ;
90+ console . log ( "rollBackToEmbedded" ) ;
91+
92+ const sig =
93+ cloudPushConfig . codeSigningPrivateKey && expectSignature
94+ ? createSignature (
95+ expectSignature . alg ,
96+ JSON . stringify ( directive ) ,
97+ cloudPushConfig . codeSigningPrivateKey ,
98+ )
99+ : undefined ;
100+
101+ return UpdateResponse ( {
102+ bundleId : embeddedUpdateId ,
103+ directive,
104+ signature :
105+ sig && expectSignature ?. keyid
106+ ? { sig, keyid : expectSignature . keyid }
107+ : undefined ,
108+ } ) ;
109+ }
110+
111+ if ( ! nextBundle ) {
112+ console . log ( "NoUpdateResponse" ) ;
113+ return NoUpdateResponse ( ) ;
84114 }
85115
86116 if ( nextBundle . bundleId === currentBundle ?. bundleId ) {
117+ console . log ( "NoUpdateResponse" ) ;
87118 return NoUpdateResponse ( ) ;
88119 }
89120
@@ -94,9 +125,27 @@ export async function GET(request: NextRequest) {
94125 storageClient : storageNodeClient ,
95126 channel,
96127 } ) ;
128+ console . log ( "UpdateResponse" ) ;
129+
130+ const sig =
131+ cloudPushConfig . codeSigningPrivateKey && expectSignature
132+ ? createSignature (
133+ expectSignature . alg ,
134+ JSON . stringify ( manifest ) ,
135+ cloudPushConfig . codeSigningPrivateKey ,
136+ )
137+ : undefined ;
97138
98- return UpdateResponse ( { manifest, bundleId : nextBundle . bundleId } ) ;
139+ return UpdateResponse ( {
140+ manifest,
141+ bundleId : nextBundle . bundleId ,
142+ signature :
143+ sig && expectSignature ?. keyid
144+ ? { sig, keyid : expectSignature . keyid }
145+ : undefined ,
146+ } ) ;
99147 } catch ( error ) {
148+ console . error ( error ) ;
100149 return ErrorResponse ( error as Error ) ;
101150 }
102151}
0 commit comments