11import { hashV0 , hashV1 } from '@dcl/hashing'
22import { Entity } from '@dcl/schemas'
3- import { IFetchComponent , RequestOptions } from '@well-known-components/interfaces'
3+ import { IFetchComponent , RequestOptions as OriginalRequestOptions } from '@well-known-components/interfaces'
44import FormData from 'form-data'
55import { ClientOptions , DeploymentData } from './types'
66import { addModelToFormData , isNode , mergeRequestOptions , sanitizeUrl , splitAndFetch } from './utils/Helper'
77import { retry } from './utils/retry'
88import { Response } from '@well-known-components/interfaces/dist/components/fetcher'
99
10+ export type RequestOptions = OriginalRequestOptions & {
11+ deploymentProtocolVersion ?: 'v1' | 'v2'
12+ }
13+
1014function arrayBufferFrom ( value : Buffer | Uint8Array ) {
1115 if ( value . buffer ) {
1216 return value . buffer
@@ -70,7 +74,6 @@ async function supportsDeploymentsV2(serverBaseUrl: string): Promise<boolean> {
7074 const response = await fetch ( `${ serverBaseUrl } /v2/entities/:entityId/files/:fileHash` , { method : 'OPTIONS' } )
7175 return response . ok // returns true if response status is 200-299
7276 } catch ( error ) {
73- console . log ( error )
7477 console . error ( `Error: ${ error } ` )
7578 return false
7679 }
@@ -140,19 +143,19 @@ export function createContentClient(options: ClientOptions): ContentClient {
140143 }
141144
142145 async function deploy ( deployData : DeploymentData , options ?: RequestOptions ) : Promise < unknown > {
143- // TODO We could also check the deployment size (if too small, may not be worth to use V2)
144- // One file is always the entity itself, so we check more than one for using v2
145- if ( deployData . files . size > 1 ) {
146+ if ( options ?. deploymentProtocolVersion === 'v2' ) {
146147 const supportsV2 = await supportsDeploymentsV2 ( contentUrl )
147148 if ( supportsV2 ) {
148149 return deployV2 ( deployData , options )
150+ } else {
151+ throw new Error ( 'The server does not support deployments v2.' )
149152 }
150153 }
151154
152- return deployTraditional ( deployData , options )
155+ return deployV1 ( deployData , options )
153156 }
154157
155- async function deployTraditional ( deployData : DeploymentData , options ?: RequestOptions ) : Promise < unknown > {
158+ async function deployV1 ( deployData : DeploymentData , options ?: RequestOptions ) : Promise < unknown > {
156159 const form = await buildEntityFormDataForDeployment ( deployData , options )
157160
158161 const requestOptions = mergeRequestOptions ( options ? options : { } , {
@@ -164,7 +167,6 @@ export function createContentClient(options: ClientOptions): ContentClient {
164167 }
165168
166169 async function deployV2 ( deployData : DeploymentData , options ?: RequestOptions ) : Promise < unknown > {
167- console . log ( 'deployData' , deployData )
168170 const areWeRunningInNode = isNode ( )
169171
170172 const fileSizesManifest : Record < string , number > = Object . fromEntries (
@@ -183,7 +185,6 @@ export function createContentClient(options: ClientOptions): ContentClient {
183185 formData . append ( 'fileSizesManifest' , JSON . stringify ( fileSizesManifest ) , {
184186 contentType : 'application/json'
185187 } )
186- console . log ( 'form' , formData )
187188
188189 const requestOptions = mergeRequestOptions ( options ? options : { } , {
189190 body : formData as any ,
@@ -194,14 +195,11 @@ export function createContentClient(options: ClientOptions): ContentClient {
194195 if ( ! response . ok ) {
195196 throw new Error ( `Failed to deploy entity with id '${ deployData . entityId } '.` )
196197 }
197- console . log ( 'Deployment started successfully! Uploading files...' )
198198
199199 const res = await response . json ( )
200- console . log ( 'res' , res )
201200
202201 const fileUploadRequests = await buildFileUploadRequestsForDeploymentV2 ( deployData , res . missingFiles )
203202 await Promise . all ( fileUploadRequests . map ( ( request ) => request ( ) ) )
204- console . log ( 'Files uploaded successfully! Finishing deployment...' )
205203
206204 const response2 = await fetcher . fetch ( `${ contentUrl } /v2/entities/${ deployData . entityId } ` , {
207205 method : 'PUT' ,
0 commit comments