@@ -10,6 +10,8 @@ const API = "https://app.builderlab.xyz/api/goose";
1010// Builderlab checks Origin on identity binding; it also seeds the challenge origin.
1111export const BUILDERLAB_ORIGIN = "https://app.builderlab.xyz" ;
1212const LOGIN_TIMEOUT_MS = 10 * 60 * 1000 ;
13+ const MAX_RESPONSE_BYTES = 64 * 1024 ;
14+ const RESPONSE_STATUS = Symbol ( "builderlabResponseStatus" ) ;
1315const COMPLETE_HTML =
1416 "<!doctype html><meta charset=utf-8><title>Buzz authentication complete</title><p>You're signed in. You can close this window and return to Buzz." ;
1517
@@ -22,13 +24,21 @@ const ROUTES = {
2224 create : [ "/v1/buzz/communities" , [ "name" ] ] ,
2325 archive : [ "/v1/buzz/communities/archive" , [ "community_id" ] ] ,
2426 unarchive : [ "/v1/buzz/communities/unarchive" , [ "community_id" ] ] ,
27+ delete : [
28+ "/v1/buzz/communities/delete" ,
29+ [ "community_id" , "host" , "request_id" , "acknowledgement_version" ] ,
30+ ] ,
2531 // Builderlab's transfer endpoint takes camelCase keys.
2632 transfer : [
2733 "/v1/buzz/communities/transfer" ,
2834 [ "communityId" , "transfereeNpub" ] ,
2935 ] ,
3036} ;
3137
38+ /** Upstream status is metadata, not part of the public JSON body. */
39+ export const builderlabResponseStatus = ( value ) =>
40+ value ?. [ RESPONSE_STATUS ] ?? 200 ;
41+
3242/** Signs the kind 24243 challenge exactly as block/buzz desktop does, after the same checks. */
3343export function bindingEvent ( key , challenge , now = Date . now ( ) ) {
3444 const { challenge_id, nonce, verification_code, origin, expires_at } =
@@ -161,10 +171,23 @@ export function createBuilderlab({
161171 redirect : "error" ,
162172 signal : AbortSignal . timeout ( 60000 ) ,
163173 } ) ;
164- const value = await response . json ( ) . catch ( ( ) => undefined ) ;
174+ const text = await response . text ( ) ;
175+ if ( Buffer . byteLength ( text ) > MAX_RESPONSE_BYTES )
176+ throw new Error ( "Builderlab response was too large" ) ;
177+ let value ;
178+ try {
179+ value = JSON . parse ( text ) ;
180+ } catch {
181+ throw new Error ( "Builderlab returned an invalid response" ) ;
182+ }
165183 // Structured `{ error: { code, ... } }` bodies pass through for friendly UI messages.
166- if ( value && typeof value === "object" && ( response . ok || value . error ) )
184+ if ( value && typeof value === "object" && ( response . ok || value . error ) ) {
185+ Object . defineProperty ( value , RESPONSE_STATUS , {
186+ value : response . status ,
187+ enumerable : false ,
188+ } ) ;
167189 return value ;
190+ }
168191 throw new Error ( `Builderlab request failed (HTTP ${ response . status } ).` ) ;
169192 } ;
170193 const me = async ( session , signal ) => {
@@ -177,8 +200,16 @@ export function createBuilderlab({
177200 throw new Error (
178201 `Builderlab session check failed with HTTP ${ response . status } ` ,
179202 ) ;
180- const { email, name, expires_at } = await response . json ( ) ;
181- return { email, name, expiresAt : expires_at } ;
203+ const { email, name, expires_at, capabilities } = await response . json ( ) ;
204+ return {
205+ email,
206+ name,
207+ expiresAt : expires_at ,
208+ capabilities : {
209+ can_delete_buzz_communities :
210+ capabilities ?. can_delete_buzz_communities === true ,
211+ } ,
212+ } ;
182213 } ;
183214 // Sign-in and sign-out bump the generation; late results from an older one never
184215 // write, clear or describe the current session.
@@ -271,7 +302,12 @@ export function createBuilderlab({
271302 const body = { } ;
272303 for ( const field of fields ) {
273304 const value = input ?. [ field ] ;
274- if ( typeof value !== "string" || ! value || value . length > 200 )
305+ if ( field === "acknowledgement_version" ) {
306+ if ( ! Number . isInteger ( value ) ) throw new Error ( `Missing ${ field } ` ) ;
307+ body [ field ] = value ;
308+ continue ;
309+ }
310+ if ( typeof value !== "string" || ! value || value . length > 253 )
275311 throw new Error ( `Missing ${ field } ` ) ;
276312 body [ field ] = value ;
277313 }
0 commit comments