initial-commit
This commit is contained in:
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
.env
|
||||
node_modules
|
||||
.DS_Store
|
||||
dist
|
||||
dist-ssr
|
||||
coverage
|
||||
*.local
|
||||
.npmrc
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
*.tsbuildinfo
|
||||
|
||||
.eslintcache
|
||||
|
||||
# Cypress
|
||||
/cypress/videos/
|
||||
/cypress/screenshots/
|
||||
|
||||
# Vitest
|
||||
__screenshots__/
|
||||
20
README.md
Normal file
20
README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Taker of Notes Plugin: S3
|
||||
|
||||
Taker of Notes storage plugin for S3 compatible object storage
|
||||
|
||||
## Getting Started
|
||||
|
||||
1. Install dependencies: `npm install`
|
||||
2. Run tests: `npm test`
|
||||
3. Build: `npm run build`
|
||||
|
||||
## Project Structure
|
||||
|
||||
- `src/` - Source code
|
||||
- `test/` - Tests
|
||||
- `dist/` - Built output
|
||||
- `rollup.config.js` - Build configuration
|
||||
|
||||
## Development
|
||||
|
||||
Implement your storage adapter in `src/S3Adapter.js`.
|
||||
3558
package-lock.json
generated
Normal file
3558
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
package.json
Normal file
29
package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@takerofnotes/plugin-s3",
|
||||
"version": "0.1.0",
|
||||
"repository": {
|
||||
"type": "git"
|
||||
},
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"exports": "./dist/index.js",
|
||||
"scripts": {
|
||||
"build": "rollup -c",
|
||||
"test": "vitest",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"author": "nicwands",
|
||||
"license": "MIT",
|
||||
"description": "Taker of Notes storage plugin for S3 compatible object storage",
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.700.0",
|
||||
"@takerofnotes/plugin-sdk": "^0.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^29.0.0",
|
||||
"@rollup/plugin-json": "^6.1.0",
|
||||
"@rollup/plugin-node-resolve": "^16.0.3",
|
||||
"rollup": "^4.59.0",
|
||||
"vitest": "^4.0.18"
|
||||
}
|
||||
}
|
||||
15
rollup.config.js
Normal file
15
rollup.config.js
Normal file
@@ -0,0 +1,15 @@
|
||||
// rollup.config.js
|
||||
import resolve from '@rollup/plugin-node-resolve'
|
||||
import commonjs from '@rollup/plugin-commonjs'
|
||||
import json from '@rollup/plugin-json'
|
||||
|
||||
export default {
|
||||
input: 'src/index.js',
|
||||
output: {
|
||||
file: 'dist/index.js',
|
||||
format: 'esm',
|
||||
sourcemap: true,
|
||||
inlineDynamicImports: true,
|
||||
},
|
||||
plugins: [resolve(), commonjs(), json()],
|
||||
}
|
||||
104
src/S3Adapter.js
Normal file
104
src/S3Adapter.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import { BaseNotesAdapter } from '@takerofnotes/plugin-sdk'
|
||||
import {
|
||||
S3Client,
|
||||
ListObjectsV2Command,
|
||||
GetObjectCommand,
|
||||
PutObjectCommand,
|
||||
DeleteObjectCommand,
|
||||
HeadBucketCommand,
|
||||
} from '@aws-sdk/client-s3'
|
||||
|
||||
export default class S3Adapter extends BaseNotesAdapter {
|
||||
constructor(config) {
|
||||
super()
|
||||
|
||||
for (const field in config) {
|
||||
this[field] = config[field]
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.s3Client = new S3Client({
|
||||
endpoint: this.endpoint,
|
||||
region: this.region || 'us-east-1',
|
||||
credentials: {
|
||||
accessKeyId: this.accessKeyId,
|
||||
secretAccessKey: this.secretAccessKey,
|
||||
},
|
||||
})
|
||||
|
||||
this.bucket = this.bucket || 'notes'
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
const command = new ListObjectsV2Command({
|
||||
Bucket: this.bucket,
|
||||
})
|
||||
|
||||
const response = await this.s3Client.send(command)
|
||||
const notes = []
|
||||
|
||||
if (!response.Contents) return notes
|
||||
|
||||
for (const object of response.Contents) {
|
||||
if (!object.Key.endsWith('.json')) continue
|
||||
|
||||
const getCommand = new GetObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: object.Key,
|
||||
})
|
||||
|
||||
try {
|
||||
const objectResponse = await this.s3Client.send(getCommand)
|
||||
const bodyString = await objectResponse.Body.transformToString()
|
||||
const note = JSON.parse(bodyString)
|
||||
notes.push(note)
|
||||
} catch (error) {
|
||||
console.error(`Failed to download note ${object.Key}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
return notes
|
||||
}
|
||||
|
||||
async create(note) {
|
||||
await this._write(note)
|
||||
}
|
||||
|
||||
async update(note) {
|
||||
await this._write(note)
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
const command = new DeleteObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: `${id}.json`,
|
||||
})
|
||||
|
||||
await this.s3Client.send(command)
|
||||
}
|
||||
|
||||
async testConnection() {
|
||||
try {
|
||||
const command = new HeadBucketCommand({
|
||||
Bucket: this.bucket,
|
||||
})
|
||||
|
||||
await this.s3Client.send(command)
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async _write(note) {
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: `${note.id}.json`,
|
||||
Body: JSON.stringify(note, null, 2),
|
||||
ContentType: 'application/json',
|
||||
})
|
||||
|
||||
await this.s3Client.send(command)
|
||||
}
|
||||
}
|
||||
51
src/index.js
Normal file
51
src/index.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import { definePlugin } from '@takerofnotes/plugin-sdk'
|
||||
import S3Adapter from './S3Adapter'
|
||||
|
||||
export default definePlugin({
|
||||
id: 's3',
|
||||
name: 'S3',
|
||||
description:
|
||||
'Taker of Notes storage plugin for S3 compatible object storage',
|
||||
version: '0.1.0',
|
||||
apiVersion: '0.3.1',
|
||||
configSchema: [
|
||||
{
|
||||
key: 'endpoint',
|
||||
label: 'Endpoint URL',
|
||||
type: 'text',
|
||||
default: 'https://play.min.io',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'region',
|
||||
label: 'Region',
|
||||
type: 'text',
|
||||
default: 'us-east-1',
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
key: 'bucket',
|
||||
label: 'Bucket Name',
|
||||
type: 'text',
|
||||
default: 'notes',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'accessKeyId',
|
||||
label: 'Access Key ID',
|
||||
type: 'text',
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'secretAccessKey',
|
||||
label: 'Secret Access Key',
|
||||
type: 'password',
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
createAdapter(config) {
|
||||
return new S3Adapter(config)
|
||||
},
|
||||
})
|
||||
11
test/plugin.test.js
Normal file
11
test/plugin.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { describePluginTests } from '@takerofnotes/plugin-sdk'
|
||||
import plugin from '../src/index.js'
|
||||
|
||||
const adapter = plugin.createAdapter({
|
||||
endpoint: import.meta.env.VITE_S3_ENDPOINT,
|
||||
bucket: import.meta.env.VITE_S3_BUCKET,
|
||||
accessKeyId: import.meta.env.VITE_S3_ACCESS_KEY,
|
||||
secretAccessKey: import.meta.env.VITE_S3_SECRET_KEY,
|
||||
})
|
||||
|
||||
describePluginTests({ plugin, adapter }, describe, it, expect)
|
||||
8
vitest.config.js
Normal file
8
vitest.config.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
globals: true,
|
||||
testTimeout: 30000,
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user