Skip to content

Commit

Permalink
add s3fs class
Browse files Browse the repository at this point in the history
  • Loading branch information
ricsam committed Dec 16, 2024
1 parent 30ec60e commit 639e0c7
Show file tree
Hide file tree
Showing 19 changed files with 609 additions and 217 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ nafs.d.ts
nafs.js

types

docs
.vscode
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# nafs

## 0.1.1

### Patch Changes

- add s3fs class

## 0.1.0

### Minor Changes
Expand Down
49 changes: 23 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,31 @@ await localFs.promises.writeFile('/hello', 'Hello World');
await localFs.promises.readFile('/hello', 'utf8'); // Hello World
```



## Supported File System Methods
| Method | File System | Memory | S3 | PostgreSQL | Logstash | Kibana |
|---------------------|-------------|--------|-------|------------|-----------|---------|
| `promises.readFile` |||||||
| `promises.writeFile` |||||||
| `promises.unlink` |||||||
| `promises.rmdir` |||||||
| `promises.mkdir` |||||||
| `promises.readdir` |||||||
| `promises.stat` |||||||
| `promises.lstat` |||||||
| `promises.chmod` |||||||
| `promises.chown` |||||||
| `promises.utimes` |||||||
| `promises.rename` |||||||
| `promises.copyFile` |||||||
| `promises.symlink` |||||||
| `promises.readlink` |||||||
| `promises.truncate` |||||||
| `promises.access` |||||||
| `createReadStream` |||||||
| `createWriteStream` |||||||

| Method | File System | Memory | Amazon S3 | Google Cloud Storage | Azure Storage |
|---------------------|-------------|---------|-----------|---------------------|---------------|
| `promises.readFile` ||||||
| `promises.writeFile` ||||||
| `promises.unlink` ||||||
| `promises.rmdir` ||||||
| `promises.mkdir` ||||||
| `promises.readdir` ||||||
| `promises.stat` ||||||
| `promises.lstat` ||||||
| `promises.chmod` ||||||
| `promises.chown` ||||||
| `promises.utimes` ||||||
| `promises.rename` ||||||
| `promises.copyFile` ||||||
| `promises.symlink` ||||||
| `promises.readlink` ||||||
| `promises.truncate` ||||||
| `promises.access` ||||||
| `createReadStream` ||||||
| `createWriteStream` ||||||

✅ - Implemented
❌ - Not Implemented

File System and Memory implementations are provided via memfs and linkfs, supporting full Node.js fs API compatibility. S3 implementation currently supports basic file reading and writing operations. PostgreSQL, Logstash, and Kibana implementations are planned for future development.


File System and Memory implementations are provided via memfs and linkfs, supporting full Node.js fs API compatibility. Cloud storage implementations (Amazon S3, Google Cloud Storage, Azure Storage) are under development, with S3 currently supporting basic file reading and writing operations.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nafs",
"version": "0.1.0",
"version": "0.1.1",
"description": "Node Active FS - nafs. File system abstraction to read/write files to AWS S3 or local directory. Includes middleware for usage with express.",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand All @@ -15,7 +15,8 @@
"scripts": {
"start": "bun run src/index.ts",
"test": "bun test",
"build": "rm -rf ./dist ./types && tsc -p tsconfig.build.json && swc ./src --config-file .swcrc.cjs.json --out-dir ./dist/cjs --ignore **/*.test.ts --strip-leading-paths && swc ./src --config-file .swcrc.mjs.json --out-dir ./dist/mjs --ignore **/*.test.ts --strip-leading-paths && ./patch-dist-dirs.sh"
"build": "rm -rf ./dist ./types && tsc -p tsconfig.build.json && swc ./src --config-file .swcrc.cjs.json --out-dir ./dist/cjs --ignore **/*.test.ts --strip-leading-paths && swc ./src --config-file .swcrc.mjs.json --out-dir ./dist/mjs --ignore **/*.test.ts --strip-leading-paths && ./patch-dist-dirs.sh",
"docs": "typedoc src/index.ts"
},
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -46,7 +47,9 @@
"@changesets/cli": "2.27.10",
"typescript": "^5.7.2",
"@swc/cli": "^0.5.2",
"@swc/core": "^1.10.1"
"@swc/core": "^1.10.1",
"@aws-sdk/lib-storage": "^3.709.0",
"typedoc": "^0.27.5"
},
"dependencies": {}
}
16 changes: 11 additions & 5 deletions src/backends/local/file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { link } from '@ricsam/linkfs';
import type { IFs } from 'memfs';
import * as path from 'path';
import * as realFs from 'fs';
import * as realfs from 'fs';
import { parseUri } from '../../parse-uri';

export const createFileFs = (uri: string, fs?: IFs): IFs | typeof realFs => {
const lfs = link(fs ?? realFs, [
export const createLinkFs = <T>(uri: `file://${string}`, fs: T): T => {
const parsed = parseUri(uri);
const lfs = link(fs, [
'/',
uri.startsWith('/') ? uri : path.join(process.cwd(), uri),
parsed.path.startsWith('/')
? parsed.path
: path.join(process.cwd(), parsed.path),
]);
return lfs;
};
export const createFileFs = (uri: `file://${string}`) => {
return createLinkFs(uri, realfs);
};
16 changes: 8 additions & 8 deletions src/backends/local/local.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { expect, test } from 'bun:test';
import { createFsFromVolume, memfs, Volume } from 'memfs';
import { nafs } from '../../nafs';
import { createFsFromVolume, Volume } from 'memfs';
import { createLinkFs } from './file';
import { createMemFs } from './memory';
import { toTreeSync } from 'memfs/lib/print';

test('memfs', async () => {
const fs = await nafs(':memory:');
const fs = createMemFs();
await fs.promises.writeFile('/test', 'test');
expect(await fs.promises.readFile('/test', 'utf-8')).toBe('test');
});

test('fs', async () => {
const vol = new Volume();
const realFs = createFsFromVolume(vol);
const fs = await nafs('file:///tmp', {
fs: realFs,
});
const memfs = createFsFromVolume(vol);
const fs = createLinkFs('file:///tmp', memfs);
await fs.promises.mkdir('/tmp', { recursive: true });
await fs.promises.writeFile('/tmp/test', 'test');
expect(await fs.promises.readFile('/tmp/test', 'utf-8')).toBe('test');
expect(await realFs.promises.readFile('/tmp/tmp/test', 'utf-8')).toBe('test');
expect(await memfs.promises.readFile('/tmp/tmp/test', 'utf-8')).toBe('test');
});
4 changes: 2 additions & 2 deletions src/backends/local/memory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IFs } from 'memfs';
import { fs } from 'memfs';
import { fs, IFs } from 'memfs';

export const createMemFs = (): IFs => {
return fs;
};
52 changes: 52 additions & 0 deletions src/backends/s3/create-s3-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
CreateBucketCommand,
GetObjectCommand,
ListBucketsCommand,
ListObjectsV2Command,
S3Client,
} from '@aws-sdk/client-s3';
import { beforeAll, beforeEach, describe, expect, it } from 'bun:test';
import { S3Fs } from './s3fs';

describe('s3Fs LocalStack integration', () => {
const LOCALSTACK_ENDPOINT = 'http://127.0.0.1:4566';
const BUCKET_NAME = 'test-bucket';
let s3Client: S3Client;

beforeEach(() => {
process.env.AWS_DEFAULT_REGION = 'us-east-1';
process.env.AWS_ACCESS_KEY_ID = 'test';
process.env.AWS_SECRET_ACCESS_KEY = 'test';
process.env.AWS_ENDPOINT_URL = LOCALSTACK_ENDPOINT;
});

beforeAll(async () => {
// Create a client for test verification
s3Client = new S3Client({
endpoint: LOCALSTACK_ENDPOINT,
region: 'us-east-1',
credentials: {
accessKeyId: 'test',
secretAccessKey: 'test',
},
forcePathStyle: true,
});

// Create test bucket
try {
await s3Client.send(
new CreateBucketCommand({
Bucket: BUCKET_NAME,
})
);
} catch (err) {
// Bucket might already exist
}
});

it('should connect', async () => {
const command = new ListBucketsCommand();
const response = await s3Client.send(command);
expect(response.Buckets?.map(({ Name }) => Name)).toContain(BUCKET_NAME);
});
});
2 changes: 2 additions & 0 deletions src/backends/s3/create-s3-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ export function createS3Client(uri: string): S3Client {
process.env.AWS_ENDPOINT_URL_S3 ??
process.env.AWS_ENDPOINT_URL;

config.forcePathStyle = parsed.forcePathStyle ?? Boolean(config.endpoint);

return new S3Client(config);
}
Loading

0 comments on commit 639e0c7

Please sign in to comment.