Skip to content

Commit b60f786

Browse files
author
Roux
committed
Adding fully tested File library ready for publishing.
1 parent 81e99e9 commit b60f786

File tree

8 files changed

+1108
-296
lines changed

8 files changed

+1108
-296
lines changed

.changeset/puny-ravens-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@smooai/file': patch
3+
---
4+
5+
Adding fully tested File library ready for publishing.

README.md

Lines changed: 174 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,200 @@ Learn more on [smoo.ai](https://smoo.ai)
3737

3838
## About @smooai/file
3939

40-
A template repository for creating new SmooAI libraries with standardized tooling, configuration, and best practices.
40+
A powerful file handling library for Node.js that provides a unified interface for working with files from local filesystem, S3, URLs, and more. Built with streaming in mind, it handles file bytes lazily where possible to minimize memory usage and improve performance.
41+
42+
![NPM Version](https://img.shields.io/npm/v/%40smooai%2Ffile?style=for-the-badge)
43+
![NPM Downloads](https://img.shields.io/npm/dw/%40smooai%2Ffile?style=for-the-badge)
44+
![NPM Last Update](https://img.shields.io/npm/last-update/%40smooai%2Ffile?style=for-the-badge)
4145

4246
![GitHub License](https://img.shields.io/github/license/SmooAI/file?style=for-the-badge)
4347
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/SmooAI/file/release.yml?style=for-the-badge)
4448
![GitHub Repo stars](https://img.shields.io/github/stars/SmooAI/file?style=for-the-badge)
4549

50+
### Install
51+
52+
```sh
53+
pnpm add @smooai/file
54+
```
55+
4656
### Key Features
4757

48-
- 📦 Preconfigured development environment with TypeScript, ESLint, and Prettier
49-
- 🧪 Testing setup with Vitest
50-
- 🔄 Changesets for version management
51-
- 📚 Integration with SmooAI core utilities
58+
#### 🚀 Stream-First Design
5259

53-
### Dependencies
60+
- Lazy loading of file contents
61+
- Memory-efficient processing
62+
- Automatic stream handling
63+
- Support for both Node.js and Web streams
5464

55-
This template comes pre-configured with essential SmooAI packages:
65+
#### 📦 Multiple File Sources
5666

57-
#### @smooai/logger
67+
- **Local Filesystem**
5868

59-
A structured logging utility for SmooAI applications that provides:
69+
- Read and write operations
70+
- File system checks
71+
- Metadata extraction
6072

61-
- Standardized log formatting
62-
- Log level management
63-
- Integration with SmooAI's logging infrastructure
73+
- **URLs**
6474

65-
#### @smooai/utils
75+
- Automatic download
76+
- Stream-based transfer
77+
- Header metadata extraction
6678

67-
Common utility functions and helpers used across SmooAI applications.
79+
- **S3 Objects**
6880

69-
### Development Setup
81+
- Direct S3 integration (download and upload)
82+
- Stream-based transfer
83+
- Header metadata extraction
84+
- Signed URL generation
7085

71-
1. Clone the repository
72-
2. Install dependencies:
86+
- **FormData**
87+
- Ease of use for Multipart FormData upload
7388

74-
```sh
75-
pnpm install
89+
#### 🔍 Intelligent File Type Detection
90+
91+
Powered by [file-type](https://github.com/sindresorhus/file-type), providing:
92+
93+
- Magic number detection
94+
- MIME type inference
95+
- File extension detection
96+
- Support for 100+ file types
97+
98+
#### 📝 Rich Metadata
99+
100+
- File name and extension
101+
- MIME type detection
102+
- File size
103+
- Last modified date
104+
- Creation date
105+
- File hash/checksum
106+
- URL and path information
107+
- Source type
108+
109+
### Examples
110+
111+
- [Basic Usage](#basic-usage)
112+
- [Streaming Operations](#streaming-operations)
113+
- [S3 Integration](#s3-integration)
114+
- [File Type Detection](#file-type-detection)
115+
- [FormData Support](#formdata-support)
116+
117+
#### Basic Usage <a name="basic-usage"></a>
118+
119+
```typescript
120+
import File from '@smooai/file';
121+
122+
// Create a file from a local path
123+
const file = await File.createFromFile('path/to/file.txt');
124+
125+
// Read file contents (streams automatically)
126+
const content = await file.readFileString();
127+
console.log(content);
128+
129+
// Get file metadata
130+
console.log(file.metadata);
131+
// {
132+
// name: 'file.txt',
133+
// mimeType: 'text/plain',
134+
// size: 1234,
135+
// extension: 'txt',
136+
// path: 'path/to/file.txt',
137+
// lastModified: Date,
138+
// createdAt: Date
139+
// }
140+
```
141+
142+
<p align="right">(<a href="#examples">back to examples</a>)</p>
143+
144+
#### Streaming Operations <a name="streaming-operations"></a>
145+
146+
```typescript
147+
import File from '@smooai/file';
148+
149+
// Create a file from a URL (streams automatically)
150+
const file = await File.createFromUrl('https://example.com/large-file.zip');
151+
152+
// Pipe to a destination (streams without loading entire file)
153+
await file.pipeTo(someWritableStream);
154+
155+
// Read as bytes (streams in chunks)
156+
const bytes = await file.readFileBytes();
157+
158+
// Save to filesystem (streams directly)
159+
const { original, newFile } = await file.saveToFile('downloads/file.zip');
76160
```
77161

78-
### Available Scripts
162+
<p align="right">(<a href="#examples">back to examples</a>)</p>
163+
164+
#### S3 Integration <a name="s3-integration"></a>
165+
166+
```typescript
167+
import File from '@smooai/file';
168+
169+
// Create from S3 (streams automatically)
170+
const file = await File.createFromS3('my-bucket', 'path/to/file.jpg');
171+
172+
// Upload to S3 (streams directly)
173+
await file.uploadToS3('my-bucket', 'remote/file.jpg');
174+
175+
// Save to S3 (creates new file instance)
176+
const { original, newFile } = await file.saveToS3('my-bucket', 'remote/file.jpg');
177+
178+
// Move to S3 (deletes local file if source was local)
179+
const s3File = await file.moveToS3('my-bucket', 'remote/file.jpg');
180+
181+
// Generate signed URL
182+
const signedUrl = await s3File.getSignedUrl(3600); // URL expires in 1 hour
183+
```
184+
185+
<p align="right">(<a href="#examples">back to examples</a>)</p>
186+
187+
#### File Type Detection <a name="file-type-detection"></a>
188+
189+
```typescript
190+
import File from '@smooai/file';
191+
192+
const file = await File.createFromFile('document.xml');
193+
194+
// Get file type information (detected via magic numbers)
195+
console.log(file.mimeType); // 'application/xml'
196+
console.log(file.extension); // 'xml'
197+
198+
// File type is automatically detected from:
199+
// - Magic numbers (via file-type)
200+
// - MIME type headers
201+
// - File extension
202+
// - Custom detectors
203+
```
204+
205+
<p align="right">(<a href="#examples">back to examples</a>)</p>
206+
207+
#### FormData Support <a name="formdata-support"></a>
208+
209+
```typescript
210+
import File from '@smooai/file';
211+
212+
const file = await File.createFromFile('document.pdf');
213+
214+
// Convert to FormData for uploads
215+
const formData = await file.toFormData('document');
216+
217+
// Use with fetch or other HTTP clients
218+
await fetch('https://api.example.com/upload', {
219+
method: 'POST',
220+
body: formData,
221+
});
222+
```
223+
224+
<p align="right">(<a href="#examples">back to examples</a>)</p>
225+
226+
### Built With
79227

80-
- `pnpm test` - Run tests using Vitest
81-
- `pnpm build` - Build the library using tsup
82-
- `pnpm lint` - Run ESLint
83-
- `pnpm format` - Format code with Prettier
228+
- TypeScript
229+
- Node.js File System API
230+
- AWS SDK v3
231+
- [file-type](https://github.com/sindresorhus/file-type) for magic number-based MIME type detection
232+
- [@smooai/fetch](https://github.com/SmooAI/fetch) for URL downloads
233+
- [@smooai/logger](https://github.com/SmooAI/logger) for structured logging
84234

85235
## Contributing
86236

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@smooai/file",
33
"version": "1.0.2",
44
"private": true,
5-
"description": "SmooAI Library Template",
5+
"description": "A powerful file handling library for Node.js that provides a unified interface for working with files from local filesystem, S3, URLs, and more. Built with streaming in mind, it handles file bytes lazily where possible to minimize memory usage and improve performance.",
66
"homepage": "https://github.com/SmooAI/file#readme",
77
"bugs": {
88
"url": "https://github.com/SmooAI/file/issues"
@@ -45,7 +45,7 @@
4545
"lint": "eslint src/",
4646
"lint:fix": "eslint src/ --fix",
4747
"prepare": "husky",
48-
"test": "vitest run File.spec",
48+
"test": "vitest run",
4949
"test:integration": "vitest run File.integration.spec",
5050
"typecheck": "tsc --noEmit --skipLibCheck",
5151
"watch": "tsup --watch"
@@ -74,6 +74,7 @@
7474
"eslint": "^9.21.0",
7575
"husky": "^9.1.7",
7676
"lint-staged": "^15.4.3",
77+
"msw": "^2.7.3",
7778
"prettier": "^3.5.3",
7879
"prettier-plugin-packagejson": "^2.5.10",
7980
"tsup": "^8.4.0",
@@ -83,5 +84,8 @@
8384
"vitest": "^3.0.9",
8485
"vitest-tsconfig-paths": "^3.4.1"
8586
},
86-
"packageManager": "pnpm@10.6.1+sha512.40ee09af407fa9fbb5fbfb8e1cb40fbb74c0af0c3e10e9224d7b53c7658528615b2c92450e74cfad91e3a2dcafe3ce4050d80bda71d757756d2ce2b66213e9a3"
87+
"packageManager": "pnpm@10.6.1+sha512.40ee09af407fa9fbb5fbfb8e1cb40fbb74c0af0c3e10e9224d7b53c7658528615b2c92450e74cfad91e3a2dcafe3ce4050d80bda71d757756d2ce2b66213e9a3",
88+
"publishConfig": {
89+
"access": "public"
90+
}
8791
}

0 commit comments

Comments
 (0)