Skip to content

Commit b55101f

Browse files
author
Mariano Goldman
committed
draft impl of v2 protocol
1 parent f6ca947 commit b55101f

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/client/ContentClient.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ export function createContentClient(options: ClientOptions): ContentClient {
107107
return form
108108
}
109109

110-
async function buildEntityFormDataForDeploymentV2(
110+
async function buildFileUploadRequestsForDeploymentV2(
111111
deployData: DeploymentData,
112112
options?: RequestOptions
113-
): Promise<Promise<Response>[]> {
113+
): Promise<(() => Promise<Response>)[]> {
114114
// Check if we are running in node or browser
115115
const areWeRunningInNode = isNode()
116116

117-
const requests: Promise<Response>[] = []
117+
const requests: (() => Promise<Response>)[] = []
118118
const alreadyUploadedHashes = await hashesAlreadyOnServer(Array.from(deployData.files.keys()), options)
119119
for (const [fileHash, file] of deployData.files) {
120120
if (!alreadyUploadedHashes.has(fileHash) || fileHash === deployData.entityId) {
@@ -125,13 +125,14 @@ export function createContentClient(options: ClientOptions): ContentClient {
125125
: arrayBufferFrom(file) // Browser
126126

127127
requests.push(
128-
fetcher.fetch(`${contentUrl}/v2/entities/${deployData.entityId}/files/${fileHash}`, {
129-
headers: {
130-
'Content-Type': 'application/octet-stream'
131-
},
132-
method: 'POST',
133-
body: content
134-
})
128+
(): Promise<Response> =>
129+
fetcher.fetch(`${contentUrl}/v2/entities/${deployData.entityId}/files/${fileHash}`, {
130+
headers: {
131+
'Content-Type': 'application/octet-stream'
132+
},
133+
method: 'POST',
134+
body: content
135+
})
135136
)
136137
}
137138
}
@@ -165,7 +166,7 @@ export function createContentClient(options: ClientOptions): ContentClient {
165166
}
166167

167168
async function deployV2(deployData: DeploymentData, options: RequestOptions = {}): Promise<unknown> {
168-
const fileUploadRequests = await buildEntityFormDataForDeploymentV2(deployData, options)
169+
const fileUploadRequests = await buildFileUploadRequestsForDeploymentV2(deployData, options)
169170

170171
const response = await fetcher.fetch(`${contentUrl}/v2/entities/${deployData.entityId}`, {
171172
method: 'POST',
@@ -177,13 +178,12 @@ export function createContentClient(options: ClientOptions): ContentClient {
177178
files: Object.fromEntries(Array.from(deployData.files, ([key, value]) => [key, value.byteLength]))
178179
})
179180
})
180-
181181
if (!response.ok) {
182182
throw new Error(`Failed to deploy entity with id '${deployData.entityId}'.`)
183183
}
184184
console.log('Deployment started successfully! Uploading files...')
185185

186-
await Promise.all(fileUploadRequests)
186+
await Promise.all(fileUploadRequests.map((request) => request()))
187187
console.log('Files uploaded successfully! Finishing deployment...')
188188

189189
const response2 = await fetcher.fetch(`${contentUrl}/v2/entities/${deployData.entityId}`, {

test/integration/deployment-v2.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import { runServerBasedE2ETest } from '../components'
55
runServerBasedE2ETest('test deployment v2 protocol', ({ components }) => {
66
let client: ContentClient
77
let expectedFiles: Record<string, Uint8Array>
8+
let stage = 1
9+
let filesPendingUpload: Record<string, Uint8Array>
810

911
beforeEach(async () => {
1012
expectedFiles = {
1113
bafkreiecsxue6isqiozm7kgrxd35qcmb5teulk6rwo6ux3b4mhvhyyoe6y: new Uint8Array([111, 112, 113]),
1214
bafkreidiq6d5r7yujricy72476vp4lgfrdmga6pz32edatbgwdfztturyy: Buffer.from('asd', 'utf-8')
1315
}
16+
filesPendingUpload = { ...expectedFiles }
1417

1518
components.router.get('/available-content', async (ctx) => {
19+
expect(stage++).toBe(2)
1620
const params = new URLSearchParams(ctx.url.search)
1721
const cids = params.getAll('cid')
1822

@@ -26,6 +30,8 @@ runServerBasedE2ETest('test deployment v2 protocol', ({ components }) => {
2630
})
2731

2832
components.router.post('/v2/entities/:entityId', async (ctx) => {
33+
expect(stage++).toBe(3)
34+
console.log(`Estamos acá: /v2/entities/${ctx.params.entityId}`)
2935
expect(ctx.params.entityId).toBe('QmENTITY')
3036

3137
const body = await ctx.request.json()
@@ -43,22 +49,31 @@ runServerBasedE2ETest('test deployment v2 protocol', ({ components }) => {
4349
})
4450

4551
components.router.options('/v2/entities/:entityId/files/:fileHash', async (ctx) => {
52+
expect(stage++).toBe(1)
53+
4654
return {
4755
status: 200
4856
}
4957
})
5058

5159
components.router.post('/v2/entities/:entityId/files/:fileHash', async (ctx) => {
60+
expect(stage).toBe(4)
5261
expect(ctx.params.entityId).toBe('QmENTITY')
5362
expect(expectedFiles).toHaveProperty(ctx.params.fileHash)
5463
expect(Buffer.from(await ctx.request.arrayBuffer())).toEqual(Buffer.from(expectedFiles[ctx.params.fileHash]))
5564

65+
delete filesPendingUpload[ctx.params.fileHash]
66+
if (Object.keys(filesPendingUpload).length === 0) {
67+
stage++
68+
}
69+
5670
return {
5771
status: 204
5872
}
5973
})
6074

6175
components.router.put('/v2/entities/:entityId', async (ctx) => {
76+
expect(stage++).toBe(5)
6277
expect(ctx.params.entityId).toBe('QmENTITY')
6378

6479
return {
@@ -83,5 +98,6 @@ runServerBasedE2ETest('test deployment v2 protocol', ({ components }) => {
8398
const res = await client.deploy({ authChain: [], entityId: 'QmENTITY', files })
8499

85100
expect(res).toBe(true)
101+
expect(stage).toBe(6)
86102
})
87103
})

0 commit comments

Comments
 (0)