Skip to content

Commit

Permalink
Include root objectId to mutliGetObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzal3x committed Oct 21, 2024
1 parent dbba775 commit c0bc5fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions portal/common/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export const SITE_NAMES: { [key: string]: string } = {
export const FALLBACK_PORTAL = "blob.store";
// The string representing the ResourcePath struct in the walrus_site package.
export const RESOURCE_PATH_MOVE_TYPE = SITE_PACKAGE + "::site::ResourcePath";
export const SITE_OBJECT_TYPE = SITE_PACKAGE + "::site::Site";
31 changes: 19 additions & 12 deletions portal/common/lib/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

import { HttpStatusCodes } from "./http/http_status_codes";
import { SuiClient, SuiObjectData } from "@mysten/sui/client";
import { SuiClient, SuiObjectData, SuiObjectResponse } from "@mysten/sui/client";
import { Resource, VersionedResource } from "./types";
import { MAX_REDIRECT_DEPTH, RESOURCE_PATH_MOVE_TYPE } from "./constants";
import { MAX_REDIRECT_DEPTH, RESOURCE_PATH_MOVE_TYPE, SITE_OBJECT_TYPE } from "./constants";
import { checkRedirect } from "./redirects";
import { fromBase64 } from "@mysten/bcs";
import { ResourcePathStruct, DynamicFieldStruct, ResourceStruct } from "./bcs_data_parsing";
Expand Down Expand Up @@ -43,27 +43,34 @@ export async function fetchResource(
return HttpStatusCodes.TOO_MANY_REDIRECTS;
}

const object = await client.getObject({ id: objectId, options: { showDisplay: true } });
const redirectPromise = checkRedirect(object);
seenResources.add(objectId);

const dynamicFieldId = deriveDynamicFieldID(
objectId,
RESOURCE_PATH_MOVE_TYPE,
bcs.string().serialize(path).toBytes(),
);
console.log("Derived dynamic field objectID: ", dynamicFieldId);

// Fetch page data.
const pageData = await client.multiGetObjects(

Check failure on line 53 in portal/common/lib/resource.ts

View workflow job for this annotation

GitHub Actions / test

lib/resource.test.ts > fetchResource > should fetch resource without redirect

TypeError: client.multiGetObjects is not a function ❯ Module.fetchResource lib/resource.ts:53:35 ❯ lib/resource.test.ts:74:30

Check failure on line 53 in portal/common/lib/resource.ts

View workflow job for this annotation

GitHub Actions / test

lib/resource.test.ts > fetchResource > should follow redirect and recursively fetch resource

TypeError: client.multiGetObjects is not a function ❯ Module.fetchResource lib/resource.ts:53:35 ❯ lib/resource.test.ts:113:30

Check failure on line 53 in portal/common/lib/resource.ts

View workflow job for this annotation

GitHub Actions / test

lib/resource.test.ts > fetchResource > should return NOT_FOUND if the resource does not contain a blob_id

TypeError: client.multiGetObjects is not a function ❯ Module.fetchResource lib/resource.ts:53:35 ❯ lib/resource.test.ts:158:30

Check failure on line 53 in portal/common/lib/resource.ts

View workflow job for this annotation

GitHub Actions / test

lib/resource.test.ts > fetchResource > should return NOT_FOUND if dynamic fields are not found

TypeError: client.multiGetObjects is not a function ❯ Module.fetchResource lib/resource.ts:53:35 ❯ lib/resource.test.ts:173:30
{
ids: [dynamicFieldId],
options: { showBcs: true }
ids: [
objectId,
dynamicFieldId
],
options: { showBcs: true, showDisplay: true }
},
);
if (!pageData) {
return HttpStatusCodes.NOT_FOUND;
}
// MultiGetObjects returns the objects *always* in the order they were requested.
const rootPageData: SuiObjectResponse = pageData[0];
const dynamicFieldData: SuiObjectResponse = pageData[1];

const redirectPromise = checkRedirect(rootPageData);
seenResources.add(objectId);

// If no page data found.
if (!pageData || !pageData[0].data) {
if (!dynamicFieldData.data) {
const redirectId = await redirectPromise;
if (redirectId) {
return fetchResource(client, redirectId, path, seenResources, depth + 1);
Expand All @@ -72,14 +79,14 @@ export async function fetchResource(
return HttpStatusCodes.NOT_FOUND;
}

const siteResource = getResourceFields(pageData[0].data);
const siteResource = getResourceFields(dynamicFieldData.data);
if (!siteResource || !siteResource.blob_id) {
return HttpStatusCodes.NOT_FOUND;
}

return {
...siteResource,
version: pageData[0].data?.version,
version: dynamicFieldData.data.version,
objectId: dynamicFieldId,
} as VersionedResource;
}
Expand Down

0 comments on commit c0bc5fb

Please sign in to comment.