-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(storage): new storage abstraction
more adapters to come
- Loading branch information
Showing
14 changed files
with
624 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# skeleton package | ||
|
||
This package can be copied when a new package should be created. | ||
|
||
### Steps after copying: | ||
|
||
- Adjust "name", "description", and "private" in `package.json`. | ||
- Adjust README.md | ||
- Put this package into root `/package.json` "jest.references". | ||
- Put this package into root `/tsconfig.json` "references". | ||
- Put this package into root `/tsconfig.esm.json` "references". | ||
- Add dependencies to `package.json` and run `node sync-tsconfig-deps.js` to adjust tsconfig automatically. | ||
- Add to .github/workflows/main.yml tsc build step if necessary. | ||
- Add to typedoc build in deepkit-website if necessary. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/storage.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "@deepkit/storage", | ||
"version": "1.0.1-alpha.13", | ||
"description": "Fileystem abstraction Deepkit", | ||
"type": "commonjs", | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"types": "./dist/cjs/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/cjs/index.d.ts", | ||
"require": "./dist/cjs/index.js", | ||
"default": "./dist/esm/index.js" | ||
} | ||
}, | ||
"repository": "https://github.com/deepkit/deepkit-framework", | ||
"author": "Marc J. Schmidt <[email protected]>", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build": "echo '{\"type\": \"module\"}' > ./dist/esm/package.json" | ||
}, | ||
"peerDependencies": { | ||
"@deepkit/core": "^1.0.1-alpha.13" | ||
}, | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"@deepkit/core": "^1.0.1-alpha.13" | ||
}, | ||
"jest": { | ||
"testEnvironment": "node", | ||
"transform": { | ||
"^.+\\.(ts|tsx)$": [ | ||
"ts-jest", | ||
{ | ||
"tsconfig": "<rootDir>/tsconfig.json" | ||
} | ||
] | ||
}, | ||
"moduleNameMapper": { | ||
"(.+)\\.js": "$1" | ||
}, | ||
"testMatch": [ | ||
"**/tests/**/*.spec.ts" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { File, FileType, pathDirectory, pathNormalize, Reporter, StorageAdapter } from './storage.js'; | ||
import type * as fs from 'fs/promises'; | ||
|
||
export class StorageNodeLocalAdapter implements StorageAdapter { | ||
fs?: typeof fs; | ||
|
||
constructor(public path: string) { | ||
this.path = pathNormalize(path); | ||
} | ||
|
||
protected async getFs(): Promise<typeof fs> { | ||
if (!this.fs) this.fs = await import('fs/promises'); | ||
return this.fs; | ||
} | ||
|
||
getPath(path: string): string { | ||
return this.path + path; | ||
} | ||
|
||
async copy(source: string, destination: string, reporter: Reporter): Promise<void> { | ||
source = this.getPath(source); | ||
destination = this.getPath(destination); | ||
const fs = await this.getFs(); | ||
await fs.cp(source, destination, { recursive: true }); | ||
} | ||
|
||
async delete(path: string): Promise<void> { | ||
path = this.getPath(path); | ||
const fs = await this.getFs(); | ||
await fs.rm(path); | ||
} | ||
|
||
async deleteDirectory(path: string, reporter: Reporter): Promise<void> { | ||
path = this.getPath(path); | ||
const fs = await this.getFs(); | ||
await fs.rm(path, { recursive: true }); | ||
} | ||
|
||
async exists(path: string): Promise<boolean> { | ||
path = this.getPath(path); | ||
const fs = await this.getFs(); | ||
try { | ||
const res = await fs.stat(path); | ||
return res.isFile() || res.isDirectory(); | ||
} catch (error: any) { | ||
return false; | ||
} | ||
} | ||
|
||
async files(path: string): Promise<File[]> { | ||
const localPath = this.getPath(path); | ||
const files: File[] = []; | ||
const fs = await this.getFs(); | ||
|
||
for (const name of await fs.readdir(localPath)) { | ||
const file = new File(path + '/' + name); | ||
const stat = await fs.stat(localPath + '/' + name); | ||
file.size = stat.size; | ||
file.lastModified = new Date(stat.mtime); | ||
file.type = stat.isFile() ? FileType.File : FileType.Directory; | ||
files.push(file); | ||
} | ||
|
||
return files; | ||
} | ||
|
||
async get(path: string): Promise<File | undefined> { | ||
const localPath = this.getPath(path); | ||
const fs = await this.getFs(); | ||
const file = new File(path); | ||
try { | ||
const stat = await fs.stat(localPath); | ||
file.size = stat.size; | ||
file.lastModified = new Date(stat.mtime); | ||
file.type = stat.isFile() ? FileType.File : FileType.Directory; | ||
return file; | ||
} catch (error: any) { | ||
return undefined; | ||
} | ||
} | ||
|
||
async move(source: string, destination: string, reporter: Reporter): Promise<void> { | ||
source = this.getPath(source); | ||
destination = this.getPath(destination); | ||
const fs = await this.getFs(); | ||
await fs.rename(source, destination); | ||
} | ||
|
||
async read(path: string, reporter: Reporter): Promise<Uint8Array> { | ||
path = this.getPath(path); | ||
const fs = await this.getFs(); | ||
const content = await fs.readFile(path); | ||
return content; | ||
} | ||
|
||
async write(path: string, contents: Uint8Array, reporter: Reporter): Promise<void> { | ||
path = this.getPath(path); | ||
const fs = await this.getFs(); | ||
await fs.mkdir(pathDirectory(path), { recursive: true }); | ||
await fs.writeFile(path, contents); | ||
} | ||
} | ||
|
||
export const StorageLocalAdapter = StorageNodeLocalAdapter; |
Oops, something went wrong.