Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listAssets for deploy with pagination #103

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type {SatelliteConfig} from '@junobuild/config';
import {
listAssets,
uploadBlob,
type AssetKey,
type Assets,
type ENCODING_TYPE
} from '@junobuild/core-peer';
import {uploadBlob, type Asset, type AssetKey, type ENCODING_TYPE} from '@junobuild/core-peer';
import {isNullish, nonNullish} from '@junobuild/utils';
import {Blob} from 'buffer';
import crypto from 'crypto';
Expand All @@ -19,15 +13,15 @@ import {lstatSync} from 'node:fs';
import {readFile} from 'node:fs/promises';
import {basename, extname, join, relative} from 'node:path';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {COLLECTION_DAPP, DAPP_COLLECTION, UPLOAD_BATCH_SIZE} from '../constants/constants';
import {COLLECTION_DAPP, UPLOAD_BATCH_SIZE} from '../constants/constants';
import {
DEPLOY_DEFAULT_ENCODING,
DEPLOY_DEFAULT_GZIP,
DEPLOY_DEFAULT_IGNORE,
DEPLOY_DEFAULT_SOURCE
} from '../constants/deploy.constants';
import {clear} from '../services/clear.services';
import {assertSatelliteMemorySize} from '../services/deploy.services';
import {assertSatelliteMemorySize, listAssets} from '../services/deploy.services';
import {links} from '../services/links.services';
import type {SatelliteConfigEnv} from '../types/config';
import {hasArgs} from '../utils/args.utils';
Expand Down Expand Up @@ -160,10 +154,7 @@ const filterFilesToUpload = async ({
files: FileDetails[];
sourceAbsolutePath: string;
} & SatelliteConfigEnv): Promise<FileDetails[]> => {
const existingAssets = await listAssets({
collection: DAPP_COLLECTION,
satellite: satelliteParameters(env)
});
const existingAssets = await listAssets({env});

const promises = files.map(
async (file: FileDetails) => await fileNeedUpload({file, sourceAbsolutePath, existingAssets})
Expand All @@ -184,15 +175,15 @@ const fileNeedUpload = async ({
sourceAbsolutePath
}: {
file: FileDetails;
existingAssets: Assets;
existingAssets: Asset[];
sourceAbsolutePath: string;
}): Promise<{
file: FileDetails;
upload: boolean;
}> => {
const effectiveFilePath = file.alternateFile ?? file.file;

const asset = existingAssets.assets.find(
const asset = existingAssets.find(
({fullPath: f}) => f === fullPath({file: effectiveFilePath, sourceAbsolutePath})
);

Expand Down
3 changes: 3 additions & 0 deletions src/constants/deploy.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export const DEPLOY_DEFAULT_GZIP = '**/*.+(css|js|mjs)';

export const MEMORY_SIZE_ENDPOINT_VERSION = '0.0.14';
export const MEMORY_HEAP_WARNING = 900_000_000n;

// Observed empirical value, the threshold might be higher, but at the time I wrote this, pagination there were a bit more than 700 files deployed on the Juno website without any issues.
export const DEPLOY_LIST_ASSETS_PAGINATION = 500;
48 changes: 47 additions & 1 deletion src/services/deploy.services.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {satelliteMemorySize, satelliteVersion} from '@junobuild/admin';
import type {Asset} from '@junobuild/core-peer';
import {listAssets as listAssetsLib} from '@junobuild/core-peer';
import {yellow} from 'kleur';
import {compare} from 'semver';
import {readJunoConfig} from '../configs/juno.config';
import {MEMORY_HEAP_WARNING, MEMORY_SIZE_ENDPOINT_VERSION} from '../constants/deploy.constants';
import {DAPP_COLLECTION} from '../constants/constants';
import {
DEPLOY_LIST_ASSETS_PAGINATION,
MEMORY_HEAP_WARNING,
MEMORY_SIZE_ENDPOINT_VERSION
} from '../constants/deploy.constants';
import type {SatelliteConfigEnv} from '../types/config';
import {configEnv} from '../utils/config.utils';
import {NEW_CMD_LINE, confirmAndExit} from '../utils/prompt.utils';
import {satelliteParameters} from '../utils/satellite.utils';
Expand Down Expand Up @@ -56,3 +64,41 @@ export const assertSatelliteMemorySize = async (args?: string[]) => {
)} MB. Are you sure you want to proceed with the deployment?`
);
};

export const listAssets = async ({
startAfter,
env
}: {
startAfter?: string;
env: SatelliteConfigEnv;
}): Promise<Asset[]> => {
const {assets, items_page, matches_pages} = await listAssetsLib({
collection: DAPP_COLLECTION,
satellite: satelliteParameters(env),
filter: {
order: {
desc: true,
field: 'keys'
},
paginate: {
startAfter,
limit: DEPLOY_LIST_ASSETS_PAGINATION
}
}
});

const last = <T>(elements: T[]): T | undefined => {
const {length, [length - 1]: last} = elements;
return last;
};

if ((items_page ?? 0n) < (matches_pages ?? 0n)) {
const nextAssets = await listAssets({
startAfter: last(assets)?.fullPath,
env
});
return [...assets, ...nextAssets];
}

return assets;
};
Loading