@statewalker/webrun-files 0.4.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +179 -0
- package/dist/cjs/index.cjs +90 -497
- package/dist/esm/index.js +84 -490
- package/dist/file-utils.d.ts +47 -0
- package/dist/file-utils.d.ts.map +1 -0
- package/dist/index.d.ts +14 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/{utils/path-utils.d.ts → path-utils.d.ts} +10 -1
- package/dist/path-utils.d.ts.map +1 -0
- package/dist/types.d.ts +90 -44
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -6
- package/src/file-utils.ts +106 -0
- package/src/index.ts +33 -26
- package/src/{utils/path-utils.ts → path-utils.ts} +16 -2
- package/src/types.ts +93 -45
- package/dist/files-api.d.ts +0 -44
- package/dist/files-api.d.ts.map +0 -1
- package/dist/impl/index.d.ts +0 -6
- package/dist/impl/index.d.ts.map +0 -1
- package/dist/impl/mem-files-api.d.ts +0 -13
- package/dist/impl/mem-files-api.d.ts.map +0 -1
- package/dist/impl/node-files-api.d.ts +0 -24
- package/dist/impl/node-files-api.d.ts.map +0 -1
- package/dist/utils/collect-stream.d.ts +0 -16
- package/dist/utils/collect-stream.d.ts.map +0 -1
- package/dist/utils/index.d.ts +0 -7
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/normalize-path.d.ts +0 -21
- package/dist/utils/normalize-path.d.ts.map +0 -1
- package/dist/utils/path-utils.d.ts.map +0 -1
- package/src/files-api.ts +0 -167
- package/src/impl/index.ts +0 -6
- package/src/impl/mem-files-api.ts +0 -306
- package/src/impl/node-files-api.ts +0 -261
- package/src/utils/collect-stream.ts +0 -35
- package/src/utils/index.ts +0 -7
- package/src/utils/normalize-path.ts +0 -34
package/LICENSE
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# @statewalker/webrun-files
|
|
2
|
+
|
|
3
|
+
Core types and utilities for cross-platform file operations. This package defines the `FilesApi` interface that all storage backends implement, plus utility functions for common file operations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @statewalker/webrun-files
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
For actual filesystem implementations, install one of these packages:
|
|
12
|
+
- `@statewalker/webrun-files-mem` - In-memory storage
|
|
13
|
+
- `@statewalker/webrun-files-node` - Node.js filesystem
|
|
14
|
+
- `@statewalker/webrun-files-browser` - Browser File System Access API
|
|
15
|
+
- `@statewalker/webrun-files-s3` - AWS S3 / S3-compatible storage
|
|
16
|
+
|
|
17
|
+
## The FilesApi Interface
|
|
18
|
+
|
|
19
|
+
All implementations provide this interface:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
interface FilesApi {
|
|
23
|
+
// Read file content as async iterable of chunks
|
|
24
|
+
read(path: string, options?: ReadOptions): AsyncIterable<Uint8Array>;
|
|
25
|
+
|
|
26
|
+
// Write content to file (creates parent directories)
|
|
27
|
+
write(path: string, content: Iterable<Uint8Array> | AsyncIterable<Uint8Array>): Promise<void>;
|
|
28
|
+
|
|
29
|
+
// Create directory (and parents)
|
|
30
|
+
mkdir(path: string): Promise<void>;
|
|
31
|
+
|
|
32
|
+
// List directory contents
|
|
33
|
+
list(path: string, options?: ListOptions): AsyncIterable<FileInfo>;
|
|
34
|
+
|
|
35
|
+
// Get file/directory metadata
|
|
36
|
+
stats(path: string): Promise<FileStats | undefined>;
|
|
37
|
+
|
|
38
|
+
// Check if path exists
|
|
39
|
+
exists(path: string): Promise<boolean>;
|
|
40
|
+
|
|
41
|
+
// Remove file or directory (recursively)
|
|
42
|
+
remove(path: string): Promise<boolean>;
|
|
43
|
+
|
|
44
|
+
// Move/rename file or directory
|
|
45
|
+
move(source: string, target: string): Promise<boolean>;
|
|
46
|
+
|
|
47
|
+
// Copy file or directory (recursively)
|
|
48
|
+
copy(source: string, target: string): Promise<boolean>;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { MemFilesApi } from '@statewalker/webrun-files-mem';
|
|
56
|
+
import { readFile, writeText } from '@statewalker/webrun-files';
|
|
57
|
+
|
|
58
|
+
const files = new MemFilesApi();
|
|
59
|
+
|
|
60
|
+
// Write a file
|
|
61
|
+
await writeText(files, '/documents/notes.txt', 'Remember to water the plants');
|
|
62
|
+
|
|
63
|
+
// Read it back
|
|
64
|
+
const content = await readFile(files, '/documents/notes.txt');
|
|
65
|
+
console.log(new TextDecoder().decode(content));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Utility Functions
|
|
69
|
+
|
|
70
|
+
### Reading Files
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { readFile, readText, tryReadFile, tryReadText, readRange, readAt } from '@statewalker/webrun-files';
|
|
74
|
+
|
|
75
|
+
// Read entire file as Uint8Array
|
|
76
|
+
const content = await readFile(files, '/data.bin');
|
|
77
|
+
|
|
78
|
+
// Read as UTF-8 string
|
|
79
|
+
const text = await readText(files, '/config.json');
|
|
80
|
+
|
|
81
|
+
// Read with undefined return for missing files
|
|
82
|
+
const maybeContent = await tryReadFile(files, '/optional.txt');
|
|
83
|
+
const maybeText = await tryReadText(files, '/optional.txt');
|
|
84
|
+
|
|
85
|
+
// Read a specific byte range
|
|
86
|
+
const chunk = await readRange(files, '/large.bin', 1000, 500); // 500 bytes starting at position 1000
|
|
87
|
+
|
|
88
|
+
// Read into a buffer at specific offset (like fs.read)
|
|
89
|
+
const buffer = new Uint8Array(100);
|
|
90
|
+
const bytesRead = await readAt(files, '/data.bin', buffer, 0, 100, 500);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Writing Files
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { writeText } from '@statewalker/webrun-files';
|
|
97
|
+
|
|
98
|
+
// Write string content as UTF-8
|
|
99
|
+
await writeText(files, '/greeting.txt', 'Hello, World!');
|
|
100
|
+
|
|
101
|
+
// Write binary content directly
|
|
102
|
+
await files.write('/data.bin', [new Uint8Array([1, 2, 3, 4])]);
|
|
103
|
+
|
|
104
|
+
// Write from multiple chunks
|
|
105
|
+
await files.write('/large.bin', generateChunks());
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Path Utilities
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { normalizePath, joinPath, dirname, basename, extname } from '@statewalker/webrun-files';
|
|
112
|
+
|
|
113
|
+
normalizePath('//foo/../bar/./baz/'); // '/bar/baz'
|
|
114
|
+
joinPath('/foo', 'bar', 'baz.txt'); // '/foo/bar/baz.txt'
|
|
115
|
+
dirname('/foo/bar/baz.txt'); // '/foo/bar'
|
|
116
|
+
basename('/foo/bar/baz.txt'); // 'baz.txt'
|
|
117
|
+
extname('/foo/bar/baz.txt'); // '.txt'
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Working with Files
|
|
121
|
+
|
|
122
|
+
### Checking Existence
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
if (await files.exists('/config.json')) {
|
|
126
|
+
// Load configuration
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Getting Metadata
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
const info = await files.stats('/photo.jpg');
|
|
134
|
+
if (info) {
|
|
135
|
+
console.log(`Size: ${info.size} bytes`);
|
|
136
|
+
console.log(`Modified: ${new Date(info.lastModified)}`);
|
|
137
|
+
console.log(`Type: ${info.kind}`); // "file" or "directory"
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Listing Directories
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// List direct children
|
|
145
|
+
for await (const entry of files.list('/documents')) {
|
|
146
|
+
console.log(`${entry.name} (${entry.kind})`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// List recursively
|
|
150
|
+
for await (const entry of files.list('/project', { recursive: true })) {
|
|
151
|
+
console.log(entry.path);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### File Management
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
await files.mkdir('/archive/2024');
|
|
159
|
+
await files.copy('/report.pdf', '/archive/2024/report.pdf');
|
|
160
|
+
await files.move('/temp/draft.txt', '/documents/final.txt');
|
|
161
|
+
await files.remove('/old-stuff'); // Recursively deletes directories
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Type Reference
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import type {
|
|
168
|
+
FilesApi, // Core interface for backends
|
|
169
|
+
FileInfo, // Metadata from list() - includes name, path, kind, size, lastModified
|
|
170
|
+
FileStats, // Metadata from stats() - kind, size, lastModified
|
|
171
|
+
FileKind, // "file" | "directory"
|
|
172
|
+
ReadOptions, // { start?: number, length?: number, signal?: AbortSignal }
|
|
173
|
+
ListOptions, // { recursive?: boolean }
|
|
174
|
+
} from '@statewalker/webrun-files';
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## License
|
|
178
|
+
|
|
179
|
+
MIT
|