Skip to content

Commit

Permalink
feat: updates deps, improves types a bit, removes browser note from r…
Browse files Browse the repository at this point in the history
…eadme due to outstanding issues
  • Loading branch information
Enngage committed Dec 9, 2024
1 parent 48e94dc commit 446575e
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 353 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ simplify migration from external systems and also provides a built-in migration
> updates of content items, language variants, moving items through workflow, publishing, archiving, downloading binary data, uploading
> assets, `id` to `codename` translation and more.
This library can only be used as a library both in `node.js` & `browser` or as a `CLI` utility.
This library can only be used as a library both in `node.js` or as a `CLI` utility.

# Getting started

Expand Down
4 changes: 2 additions & 2 deletions lib/core/models/migration.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from 'buffer';
import { Buffer as BufferProxy } from 'buffer';
import { z } from 'zod';

interface Elements {
Expand Down Expand Up @@ -145,7 +145,7 @@ const BaseMigrationAssetSchema = z.strictObject({
});

export const MigrationAssetSchema = BaseMigrationAssetSchema.extend({
binary_data: z.union([z.instanceof(Buffer), z.instanceof(Blob)])
binary_data: z.union([z.instanceof(BufferProxy), z.instanceof(Blob)])
}).readonly();

export const ZipMigrationAssetSchema = BaseMigrationAssetSchema.extend({
Expand Down
12 changes: 6 additions & 6 deletions lib/core/utils/binary-data.utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Buffer } from 'buffer';
import { FileBinaryData } from '../../zip/index.js';
import { defaultHttpService, defaultRetryStrategy } from './http.utils.js';

export async function getBinaryDataFromUrlAsync(url: string): Promise<{ data: Buffer | Blob; contentLength: number }> {
export async function getBinaryDataFromUrlAsync(url: string): Promise<{ readonly data: FileBinaryData; readonly contentLength: number }> {
// temp fix for Kontent.ai Repository not validating url
url = url.replace('#', '%23');
const fixedUrl = url.replace('#', '%23');

const response = await defaultHttpService.getAsync<Buffer | Blob>(
const response = await defaultHttpService.getAsync<FileBinaryData>(
{
url
url: fixedUrl
},
{
responseType: 'arraybuffer',
Expand All @@ -20,6 +20,6 @@ export async function getBinaryDataFromUrlAsync(url: string): Promise<{ data: Bu
return { data: response.data, contentLength: contentLength };
}

export function geSizeInBytes(data: Blob | Buffer): number {
export function geSizeInBytes(data: FileBinaryData): number {
return data instanceof Blob ? data.size : data.byteLength;
}
13 changes: 8 additions & 5 deletions lib/core/utils/global.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Writeable<T> = { -readonly [P in keyof T]: T[P] };
export const isNotUndefined = <T>(item: T | undefined): item is T => item !== undefined;

export function formatBytes(bytes: number): string {
return format(bytes);
return format(bytes) ?? 'n/a';
}

export function sleepAsync(ms: number): Promise<void> {
Expand All @@ -36,9 +36,9 @@ export function getCurrentEnvironment(): EnvContext {
export const defaultZipFilename: string = 'data.zip';

export async function executeWithTrackingAsync<TResult>(data: {
func: () => Promise<TResult extends void ? void : Readonly<TResult>>;
event: Readonly<ITrackingEventData>;
logger?: Logger;
readonly func: () => Promise<TResult extends void ? void : Readonly<TResult>>;
readonly event: Readonly<ITrackingEventData>;
readonly logger?: Logger;
}): Promise<TResult extends void ? void : Readonly<TResult>> {
const trackingService = getTrackingService();
const logger = data.logger ?? getDefaultLogger();
Expand Down Expand Up @@ -83,7 +83,10 @@ export async function executeWithTrackingAsync<TResult>(data: {
}
}

async function runTrackingFuncWithErrorHadlingAsync<T>(data: { func: () => Promise<T>; logger: Logger }): Promise<T | void> {
async function runTrackingFuncWithErrorHadlingAsync<T>(data: {
readonly func: () => Promise<T>;
readonly logger: Logger;
}): Promise<T | void> {
try {
return await data.func();
} catch (trackingError) {
Expand Down
8 changes: 3 additions & 5 deletions lib/export/export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function exportManager(config: ExportConfig) {
codename: exportItem.workflow.codename
}
},
versions: exportItem.versions.map((version) => {
return <MigrationItemVersion>{
versions: exportItem.versions.map<MigrationItemVersion>((version) => {
return {
elements: getMigrationElements(context, exportItem.contentType, version.languageVariant.elements),
schedule: {
publish_time: version.languageVariant.schedule.publishTime ?? undefined,
Expand Down Expand Up @@ -199,13 +199,11 @@ export function exportManager(config: ExportConfig) {
message: `${asset.url}`
});

const binaryData = await getBinaryDataFromUrlAsync(asset.url);

const migrationAsset: MigrationAsset = {
filename: asset.fileName,
title: asset.title ?? '',
codename: asset.codename,
binary_data: binaryData.data,
binary_data: (await getBinaryDataFromUrlAsync(asset.url)).data,
collection: assetCollection ? { codename: assetCollection.codename } : undefined,
folder: assetFolder ? { codename: assetFolder.codename } : undefined,
descriptions: asset.descriptions.map((description) => {
Expand Down
7 changes: 4 additions & 3 deletions lib/file/file-manager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Buffer as BufferProxy } from 'buffer';
import chalk from 'chalk';
import { Logger } from '../core/index.js';
import { promises } from 'fs';
import { Logger } from '../core/index.js';

export function fileManager(logger: Logger) {
const getFilePath = (filename: string): string => {
return `./${filename}`;
};

const loadFileAsync = async (filename: string): Promise<Buffer> => {
const loadFileAsync = async (filename: string): Promise<BufferProxy> => {
const filePath = getFilePath(filename);

logger.log({
Expand All @@ -20,7 +21,7 @@ export function fileManager(logger: Logger) {
return file;
};

const writeFileAsync = async (fileNameWithoutExtension: string, content: string | Buffer): Promise<void> => {
const writeFileAsync = async (fileNameWithoutExtension: string, content: string | BufferProxy): Promise<void> => {
const filePath = getFilePath(fileNameWithoutExtension);

logger.log({
Expand Down
4 changes: 2 additions & 2 deletions lib/toolkit/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from 'buffer';
import { Buffer as BufferProxy } from 'buffer';
import { defaultZipFilename, executeWithTrackingAsync, getDefaultLogger, Logger, MigrationData } from '../core/index.js';
import { fileManager } from '../file/index.js';
import { libMetadata } from '../metadata.js';
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function storeAsync(config: StoreConfig): Promise<void> {
func: async () => {
const zipData = await zipManager(logger).createZipAsync(config.data);

if (zipData instanceof Buffer) {
if (zipData instanceof BufferProxy) {
await fileManager(logger).writeFileAsync(filename, zipData);
} else {
throw Error(`Cannot store '${filename}' on File system because the provided zip is not a Buffer`);
Expand Down
4 changes: 2 additions & 2 deletions lib/zip/zip-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from 'buffer';
import { Buffer as BufferProxy } from 'buffer';
import JSZip from 'jszip';
import { Logger, MigrationData, getDefaultLogger } from '../core/index.js';
import { zipPackager } from './zip-packager.js';
Expand All @@ -17,7 +17,7 @@ export function zipManager(logger?: Logger) {
return await zipTransformer(zipPackager(new JSZip()), loggerToUse).transformAsync(migrationData);
};

const parseZipAsync = async (zipFile: Buffer): Promise<MigrationData> => {
const parseZipAsync = async (zipFile: BufferProxy): Promise<MigrationData> => {
loggerToUse.log({
type: 'info',
message: `Parsing zip file`
Expand Down
7 changes: 3 additions & 4 deletions lib/zip/zip-packager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from 'buffer';
import { Buffer as BufferProxy } from 'buffer';
import chalk from 'chalk';
import JSZip from 'jszip';
import { match } from 'ts-pattern';
Expand All @@ -23,7 +23,7 @@ export function zipPackager(jsZip: JSZip): ZipPackager {
(data) => data.size
)
.when(
(data) => data instanceof Buffer,
(data) => data instanceof BufferProxy,
(data) => data.byteLength
)
.otherwise(() => {
Expand Down Expand Up @@ -75,8 +75,7 @@ export function zipPackager(jsZip: JSZip): ZipPackager {
return result;
},
async getBinaryDataAsync(filePath: string): Promise<FileBinaryData | undefined> {
const binaryData = await jsZip.file(filePath)?.async(getZipOutputType());
return binaryData;
return await jsZip.file(filePath)?.async(getZipOutputType());
},
async getFileContentAsync(filePath: string): Promise<string | undefined> {
return await jsZip.file(filePath)?.async('string');
Expand Down
2 changes: 1 addition & 1 deletion lib/zip/zip-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function zipTransformer(zip: ZipPackager, logger?: Logger) {
const assetsFilename: string = 'assets.json';
const assetsBinaryFolderName: string = 'binary_data';

const getAssetFolderConfig = (asset: MigrationAsset): { partialColder: string; fullPath: string } => {
const getAssetFolderConfig = (asset: MigrationAsset): { readonly partialColder: string; readonly fullPath: string } => {
const codenamePartialFolder: string = asset.codename.slice(0, 2);
return {
partialColder: codenamePartialFolder,
Expand Down
6 changes: 3 additions & 3 deletions lib/zip/zip.models.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Buffer } from 'buffer';
import { Buffer as BufferProxy } from 'buffer';
import { Logger } from '../core/index.js';

export type FileBinaryData = Buffer | Blob;
export type FileBinaryData = BufferProxy | Blob;
export type ZipCompressionLevel = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;

export type ZipPackager = {
addFile(filePath: string, data: string | FileBinaryData): void;
addFolder(name: string): ZipPackager;
generateZipAsync(config: { logger?: Logger; compressionLevel?: ZipCompressionLevel }): Promise<FileBinaryData>;
generateZipAsync(config: { readonly logger?: Logger; readonly compressionLevel?: ZipCompressionLevel }): Promise<FileBinaryData>;
getBinaryDataAsync(filePath: string): Promise<FileBinaryData | undefined>;
getFileContentAsync(filePath: string): Promise<string | undefined>;
};
Loading

0 comments on commit 446575e

Please sign in to comment.