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

Translate Auto-Drive Into CJS #199

Merged
merged 8 commits into from
Dec 11, 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
4 changes: 4 additions & 0 deletions packages/auto-dag-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

The **Autonomys Auto Dag Data SDK** (`@autonomys/auto-dag-data`) provides utilities for creating and managing IPLD DAGs (InterPlanetary Linked Data Directed Acyclic Graphs) for files and folders. It facilitates chunking large files, handling metadata, and creating folder structures suitable for distributed storage systems like IPFS.

### This package is an ES Module package and it's designed to work with ESM applications.

Check [this tutorial](https://dev.to/mangadev/set-up-a-backend-nodejs-typescript-jest-using-es-modules-1530) in how to setup a ES module application.

## Features

- **File Chunking and DAG Creation**: Efficiently split large files into smaller chunks and create IPLD DAGs.
Expand Down
6 changes: 1 addition & 5 deletions packages/auto-drive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

## Overview

The `auto-drive` package provides a set of tools to interact with the Autonomys Auto-Drive API. Below are some key functionalities:

### This package is an ES Module package and it's designed to work with ESM applications.

Check [this tutorial](https://dev.to/mangadev/set-up-a-backend-nodejs-typescript-jest-using-es-modules-1530) in how to setup a ES module application
The `auto-drive` package provides a set of tools to interact with the Autonomys Auto-Drive API.

### Installation

Expand Down
8 changes: 3 additions & 5 deletions packages/auto-drive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"packageManager": "[email protected]",
"version": "1.0.12",
"license": "MIT",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/autonomys/auto-sdk"
"type": "git",
"url": "https://github.com/autonomys/auto-sdk"
},
"author": {
"name": "Autonomys",
Expand All @@ -19,7 +18,7 @@
"node": ">=20.8.0"
},
"scripts": {
"build": "rollup -c"
"build": "yarn tsc"
},
"exports": {
".": {
Expand All @@ -35,7 +34,6 @@
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-typescript": "^12.1.1",
"@types/mime-types": "^2",
"rollup": "^4.28.0",
"rollup-plugin-jscc": "^2.0.0",
"rollup-plugin-terser": "^7.0.2",
"tslib": "^2.8.1",
Expand Down
40 changes: 0 additions & 40 deletions packages/auto-drive/rollup.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/auto-drive/src/api/calls/download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ArgsWithoutPagination } from '../../utils/types.js'
import { AutoDriveApi } from '../connection.js'
import { ArgsWithoutPagination } from '../../utils/types'
import { AutoDriveApi } from '../connection'

export const downloadObject = async (
api: AutoDriveApi,
Expand Down
8 changes: 4 additions & 4 deletions packages/auto-drive/src/api/calls/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './download.js'
export * from './read.js'
export * from './upload.js'
export * from './write.js'
export * from './download'
export * from './read'
export * from './upload'
export * from './write'
8 changes: 4 additions & 4 deletions packages/auto-drive/src/api/calls/read.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types.js'
import { AutoDriveApi } from '../connection.js'
import { PaginatedResult } from '../models/common.js'
import { ObjectInformation, ObjectSummary, Scope } from '../models/objects.js'
import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types'
import { AutoDriveApi } from '../connection'
import { PaginatedResult } from '../models/common'
import { ObjectInformation, ObjectSummary, Scope } from '../models/objects'

/**
* Retrieves the root objects based on the specified scope.
Expand Down
14 changes: 9 additions & 5 deletions packages/auto-drive/src/api/calls/upload.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { CID, FileUploadOptions } from '@autonomys/auto-dag-data'
import { ArgsWithoutPagination } from '../../utils/types.js'
import { AutoDriveApi } from '../connection.js'
import { FolderTree } from '../models/folderTree.js'
import { CompleteUploadResponse, FileUpload, FolderUpload } from '../models/uploads.js'
import { ArgsWithoutPagination } from '../../utils/types'
import { AutoDriveApi } from '../connection'
import { FolderTree } from '../models/folderTree'
import {
CompleteUploadResponse,
FileUpload,
FileUploadOptions,
FolderUpload,
} from '../models/uploads'

/**
* Initiates a file upload to the server.
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-drive/src/api/calls/write.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ArgsWithoutPagination } from '../../utils/types.js'
import { AutoDriveApi } from '../connection.js'
import { ArgsWithoutPagination } from '../../utils/types'
import { AutoDriveApi } from '../connection'

/**
* Shares an object with a specified public ID.
Expand Down
8 changes: 4 additions & 4 deletions packages/auto-drive/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './calls/index.js'
export * from './connection.js'
export * from './models/index.js'
export * from './wrappers.js'
export * from './calls/index'
export * from './connection'
export * from './models/index'
export * from './wrappers'
4 changes: 2 additions & 2 deletions packages/auto-drive/src/api/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './objects.js'
export * from './uploads.js'
export * from './objects'
export * from './uploads'
36 changes: 35 additions & 1 deletion packages/auto-drive/src/api/models/objects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
import { OffchainMetadata } from '@autonomys/auto-dag-data'
import { FileUploadOptions } from './uploads'

export type OffchainMetadata = OffchainFileMetadata | OffchainFolderMetadata
interface ChildrenMetadata {
type: 'folder' | 'file'
name?: string
cid: string
totalSize: bigint
}

export type OffchainFolderMetadata = {
type: 'folder'
dataCid: string
name?: string
totalSize: bigint
totalFiles: number
children: ChildrenMetadata[]
uploadOptions: FileUploadOptions
}

export type OffchainFileMetadata = {
type: 'file'
dataCid: string
name?: string
mimeType?: string
totalSize: bigint
totalChunks: number
chunks: ChunkInfo[]
uploadOptions?: FileUploadOptions
}

export interface ChunkInfo {
size: bigint
cid: string
}

export enum Scope {
Global = 'global',
Expand Down
30 changes: 26 additions & 4 deletions packages/auto-drive/src/api/models/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CID, FileUploadOptions } from '@autonomys/auto-dag-data'
import { z } from 'zod'
import { FolderTreeFolderSchema } from './folderTree.js'
import { FolderTreeFolderSchema } from './folderTree'

export enum UploadType {
FILE = 'file',
Expand Down Expand Up @@ -28,6 +27,29 @@ export const fileUploadSchema = z.object({
oauthUserId: z.string(),
})

export enum CompressionAlgorithm {
ZLIB = 'ZLIB',
}
export interface CompressionOptions {
algorithm: CompressionAlgorithm
level?: number
chunkSize?: number
}

export type FileUploadOptions = {
compression?: CompressionOptions
encryption?: EncryptionOptions
}

export enum EncryptionAlgorithm {
AES_256_GCM = 'AES_256_GCM',
}

export interface EncryptionOptions {
algorithm: EncryptionAlgorithm
chunkSize?: number
}

export type FileUpload = z.infer<typeof fileUploadSchema> & {
uploadOptions: FileUploadOptions | null
}
Expand Down Expand Up @@ -55,13 +77,13 @@ export type CompleteUploadResponse = {
export type UploadFileStatus = {
type: 'file'
progress: number
cid?: CID
cid?: string
}

export type UploadFolderStatus = {
type: 'folder'
progress: number
cid?: CID
cid?: string
}

export type UploadChunksStatus = {
Expand Down
41 changes: 21 additions & 20 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import {
compressFile,
CompressionAlgorithm,
decompressFile,
decryptFile,
encryptFile,
EncryptionAlgorithm,
stringToCid,
} from '@autonomys/auto-dag-data'
import fs from 'fs'
import mime from 'mime-types'
import { asyncByChunk, asyncFromStream, fileToIterable } from '../utils/async.js'
import { progressToPercentage } from '../utils/misc.js'
import { PromisedObservable } from '../utils/observable.js'
import { asyncByChunk, asyncFromStream, fileToIterable } from '../utils/async'
import { progressToPercentage } from '../utils/misc'
import { PromisedObservable } from '../utils/observable'

import {
completeUpload,
createFileUpload,
Expand All @@ -19,11 +12,11 @@ import {
downloadObject,
getObjectMetadata,
uploadFileChunk,
} from './calls/index.js'
import { AutoDriveApi } from './connection.js'
import { GenericFile } from './models/file.js'
import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree.js'
import { UploadChunksStatus, UploadFileStatus, UploadFolderStatus } from './models/uploads.js'
} from './calls/index'
import { AutoDriveApi } from './connection'
import { GenericFile } from './models/file'
import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree'
import { UploadChunksStatus, UploadFileStatus, UploadFolderStatus } from './models/uploads'

export type UploadFileOptions = {
password?: string
Expand Down Expand Up @@ -75,6 +68,8 @@ export const uploadFileFromInput = (
uploadChunkSize?: number,
): PromisedObservable<UploadFileStatus> => {
return new PromisedObservable<UploadFileStatus>(async (subscriber) => {
const { stringToCid, compressFile, CompressionAlgorithm, encryptFile, EncryptionAlgorithm } =
await import('@autonomys/auto-dag-data')
let asyncIterable: AsyncIterable<Buffer> = fileToIterable(file)

if (compression) {
Expand Down Expand Up @@ -115,7 +110,7 @@ export const uploadFileFromInput = (

const result = await completeUpload(api, { uploadId: fileUpload.id })

subscriber.next({ type: 'file', progress: 100, cid: stringToCid(result.cid) })
subscriber.next({ type: 'file', progress: 100, cid: result.cid })
subscriber.complete()
})
}
Expand Down Expand Up @@ -144,6 +139,8 @@ export const uploadFile = (
uploadChunkSize?: number,
): PromisedObservable<UploadFileStatus> => {
return new PromisedObservable<UploadFileStatus>(async (subscriber) => {
const { stringToCid, compressFile, CompressionAlgorithm, encryptFile, EncryptionAlgorithm } =
await import('@autonomys/auto-dag-data')
let asyncIterable: AsyncIterable<Buffer> = file.read()

if (compression) {
Expand Down Expand Up @@ -184,7 +181,7 @@ export const uploadFile = (

const result = await completeUpload(api, { uploadId: fileUpload.id })

subscriber.next({ type: 'file', progress: 100, cid: stringToCid(result.cid) })
subscriber.next({ type: 'file', progress: 100, cid: result.cid })
subscriber.complete()
})
}
Expand Down Expand Up @@ -270,7 +267,7 @@ export const uploadFolderFromInput = async (

const result = await completeUpload(api, { uploadId: folderUpload.id })

subscriber.next({ type: 'folder', progress: 100, cid: stringToCid(result.cid) })
subscriber.next({ type: 'folder', progress: 100, cid: result.cid })
subscriber.complete()
})
}
Expand Down Expand Up @@ -321,6 +318,10 @@ export const downloadFile = async (
cid: string,
password?: string,
): Promise<AsyncIterable<Buffer>> => {
const { decompressFile, CompressionAlgorithm, EncryptionAlgorithm, decryptFile } = await import(
'@autonomys/auto-dag-data'
)

const metadata = await getObjectMetadata(api, { cid })

let iterable = asyncFromStream(await downloadObject(api, { cid }))
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-drive/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './api/index.js'
export * from './utils/index.js'
export * from './api/index'
export * from './utils/index'
4 changes: 2 additions & 2 deletions packages/auto-drive/src/fs/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './utils.js'
export * from './wrappers.js'
export * from './utils'
export * from './wrappers'
2 changes: 1 addition & 1 deletion packages/auto-drive/src/fs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WriteStream, createReadStream } from 'fs'
import fs from 'fs/promises'
import JSZip from 'jszip'
import path from 'path'
import { FolderTree, FolderTreeFolder } from '../api/models/folderTree.js'
import { FolderTree, FolderTreeFolder } from '../api/models/folderTree'

export const getFiles = async (folderPath: string): Promise<string[]> => {
const stat = await fs.stat(folderPath)
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-drive/src/fs/wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CompressionAlgorithm, stringToCid } from '@autonomys/auto-dag-data'
import fs from 'fs'
import mime from 'mime-types'
import { AutoDriveApi } from '../api/connection.js'
Expand Down Expand Up @@ -101,6 +100,7 @@ export const uploadFolderFromFolderPath = async (
}

return new PromisedObservable<UploadFolderStatus>(async (subscriber) => {
const { CompressionAlgorithm } = await import('@autonomys/auto-dag-data')
const folderUpload = await createFolderUpload(api, {
fileTree,
uploadOptions: {
Expand Down Expand Up @@ -134,7 +134,7 @@ export const uploadFolderFromFolderPath = async (

const result = await completeUpload(api, { uploadId: folderUpload.id })

subscriber.next({ type: 'folder', progress: 100, cid: stringToCid(result.cid) })
subscriber.next({ type: 'folder', progress: 100, cid: result.cid })
subscriber.complete()
})
}
2 changes: 2 additions & 0 deletions packages/auto-drive/src/utils/async.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Buffer } from 'buffer'

export const asyncByChunk = async function* (
asyncIterable: AsyncIterable<Buffer>,
chunkSize: number,
Expand Down
Loading
Loading