streamby-core 0.1.0 โ†’ 0.3.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/README.md CHANGED
@@ -1,103 +1,114 @@
1
- # @streamby/core
2
-
3
- Middleware framework for building storage-agnostic media APIs.
4
- Part of the [StreamBy](https://streamby.nhexa.cl) ecosystem by TerminalCore Labs.
5
-
6
- ---
7
-
8
- ## ๐Ÿš€ What is it?
9
-
10
- `@streamby/core` is a plug-and-play middleware for Express (or compatible frameworks) that enables file uploads, listings and project-based access over services like:
11
-
12
- - โœ… AWS S3
13
- - โœ… Google Cloud Storage (soon)
14
- - โœ… Cloudflare R2 (soon)
15
- - โœ… Local/Personal servers (soon)
16
-
17
- It's designed to be installed inside your existing API as a library โ€” **no need to run a separate backend**.
18
-
19
- ---
20
-
21
- ## ๐Ÿ“ฆ Installation
22
-
23
- ```bash
24
- npm install @streamby/core
25
- ```
26
-
27
- ---
28
-
29
- ## ๐Ÿงฑ Basic usage
30
-
31
- ```ts
32
- import express from 'express';
33
- import { createStreamByRouter } from '@streamby/core';
34
-
35
- const app = express();
36
-
37
- app.use('/streamby', createStreamByRouter({
38
- storageProvider: {
39
- type: 's3',
40
- config: {
41
- bucket: 'your-bucket-name',
42
- region: 'your-region',
43
- accessKeyId: 'YOUR_AWS_KEY',
44
- secretAccessKey: 'YOUR_AWS_SECRET'
45
- }
46
- },
47
- authProvider: async (req) => {
48
- // validate token, session, etc.
49
- return {
50
- userId: 'demo',
51
- projectId: 'demo-project',
52
- role: 'admin',
53
- };
54
- }
55
- }));
56
-
57
- app.listen(3000);
58
- ```
59
-
60
- ---
61
-
62
- ## ๐Ÿงช Testing
63
-
64
- ```bash
65
- npm run test
66
- ```
67
-
68
- Unit tests use [Vitest](https://vitest.dev) and [Supertest](https://www.npmjs.com/package/supertest).
69
-
70
- ---
71
-
72
- ## ๐Ÿ“ Features
73
-
74
- - ๐Ÿงน Modular adapters per storage provider
75
- - ๐Ÿ” Auth/permission integration per request
76
- - ๐Ÿ“‚ File listing, uploading and deletion
77
- - ๐Ÿงฐ Built-in testability with mock adapters
78
- - โšก Easy to extend and compose
79
-
80
- ---
81
-
82
- ## ๐Ÿšฃ Roadmap
83
-
84
- - [ ] Support for Google Cloud Storage
85
- - [ ] CLI tool for uploading files
86
- - [ ] Role-based access control middleware
87
- - [ ] Plugin system for extended routes
88
-
89
- ---
90
-
91
- ## ๐Ÿง‘โ€๐Ÿ’ป Maintained by
92
-
93
- **TerminalCore Labs** โ€“ part of [Nhexa Entertainment](https://nhexa.cl)
94
- Developed as open-source infrastructure for creative and media-oriented apps.
95
-
96
- Visit us [here](https://terminalcore.cl)
97
-
98
- ---
99
-
100
- ## ๐Ÿ“Ÿ License
101
-
102
- [MIT](./LICENSE)
103
-
1
+ # @streamby/core
2
+
3
+ Middleware framework for building storage-agnostic media APIs.
4
+ Part of the [StreamBy](https://streamby.nhexa.cl) ecosystem by TerminalCore Labs.
5
+
6
+ ---
7
+
8
+ ## ๐Ÿš€ What is it?
9
+
10
+ `@streamby/core` is a plug-and-play middleware for Express (or compatible frameworks) that enables file uploads, listings and **multi-project access** over services like:
11
+
12
+ - โœ… AWS S3
13
+ - โœ… Google Cloud Storage (soon)
14
+ - โœ… Cloudflare R2 (soon)
15
+ - โœ… Local/Personal servers (soon)
16
+
17
+ It's designed to be installed inside your existing API as a library โ€” **no need to run a separate backend**.
18
+
19
+ ---
20
+
21
+ ## ๐Ÿ“ฆ Installation
22
+
23
+ ```bash
24
+ npm install @streamby/core
25
+ ```
26
+
27
+ ---
28
+
29
+ ## ๐Ÿงฑ Basic usage
30
+
31
+ ```ts
32
+ import express from 'express';
33
+ import { createStreamByRouter } from '@streamby/core';
34
+
35
+ const app = express();
36
+
37
+ app.use('/streamby', createStreamByRouter({
38
+ storageProvider: {
39
+ type: 's3',
40
+ config: {
41
+ bucket: 'your-bucket-name',
42
+ region: 'your-region',
43
+ accessKeyId: 'YOUR_AWS_KEY',
44
+ secretAccessKey: 'YOUR_AWS_SECRET'
45
+ }
46
+ },
47
+ authProvider: async (req) => {
48
+ // extract token/cookie and validate
49
+ return {
50
+ userId: 'demo',
51
+ projects: ['demo-project'],
52
+ role: 'admin',
53
+ };
54
+ },
55
+ projectProvider: async (projectId) => {
56
+ // fetch project from your DB
57
+ return {
58
+ id: projectId,
59
+ name: 'Demo Project',
60
+ description: 'Demo for StreamBy integration',
61
+ rootFolders: [],
62
+ settings: { allowUpload: true }
63
+ };
64
+ }
65
+ }));
66
+
67
+ app.listen(3000);
68
+ ```
69
+
70
+ ---
71
+
72
+ ## ๐Ÿงช Testing
73
+
74
+ ```bash
75
+ npm run test
76
+ ```
77
+
78
+ Unit tests use [Vitest](https://vitest.dev) and [Supertest](https://www.npmjs.com/package/supertest).
79
+
80
+ ---
81
+
82
+ ## ๐Ÿ“ Features
83
+
84
+ - ๐Ÿ“‚ Multi-project access control
85
+ - ๐Ÿ” Project-aware file uploads & listings
86
+ - ๐Ÿงน Modular adapters per storage provider
87
+ - ๐Ÿงฐ Built-in testability with mock auth & storage
88
+ - โœจ Simple to extend for custom business logic
89
+
90
+ ---
91
+
92
+ ## ๐Ÿšฃ Roadmap
93
+
94
+ - [ ] Support for Google Cloud Storage
95
+ - [ ] CLI tool for uploading files
96
+ - [ ] Role-based access control middleware
97
+ - [ ] Plugin system for extended routes
98
+ - [ ] Streaming support (HLS, audio, etc.)
99
+
100
+ ---
101
+
102
+ ## ๐Ÿง‘โ€๐Ÿ’ป Maintained by
103
+
104
+ **TerminalCore Labs** โ€“ part of [Nhexa Entertainment](https://nhexa.cl)
105
+ Developed as open-source infrastructure for creative and media-oriented apps.
106
+
107
+ Visit us [here](https://terminalcore.cl)
108
+
109
+ ---
110
+
111
+ ## ๐Ÿ“Ÿ License
112
+
113
+ [MIT](./LICENSE)
114
+
@@ -12,19 +12,23 @@ function createS3Adapter(config) {
12
12
  });
13
13
  return {
14
14
  async uploadFile(req, projectId) {
15
- const file = req.body.file;
15
+ const file = req.body?.file;
16
+ if (!file || !file.name || !file.buffer) {
17
+ throw new Error('Invalid file upload: missing name or buffer');
18
+ }
19
+ const key = `${projectId}/${file.name}`;
16
20
  const command = new client_s3_1.PutObjectCommand({
17
21
  Bucket: config.bucket,
18
- Key: `${projectId}/${file.name}`,
22
+ Key: key,
19
23
  Body: file.buffer,
20
24
  });
21
25
  await s3.send(command);
22
- return { success: true, key: command.input.Key };
26
+ return { success: true, key };
23
27
  },
24
28
  async listFiles(projectId) {
25
29
  const command = new client_s3_1.ListObjectsV2Command({
26
30
  Bucket: config.bucket,
27
- Prefix: `${projectId}/`
31
+ Prefix: `${projectId}/`,
28
32
  });
29
33
  const result = await s3.send(command);
30
34
  return result.Contents || [];
@@ -17,10 +17,23 @@ function createStreamByRouter(config) {
17
17
  throw new Error('Unsupported storage type');
18
18
  }
19
19
  })();
20
+ router.get('/auth', async (req, res) => {
21
+ try {
22
+ const auth = await config.authProvider(req);
23
+ res.status(200).json({ logged: true, ...auth });
24
+ }
25
+ catch (err) {
26
+ res.status(401).json({ logged: false });
27
+ }
28
+ });
20
29
  router.get('/files', async (req, res) => {
21
30
  try {
22
31
  const auth = await config.authProvider(req);
23
- const files = await (0, file_1.listFilesService)(adapter, req, auth.projectId);
32
+ const projectId = (req.query.projectId || req.headers['x-project-id']);
33
+ if (!projectId || !auth.projects.includes(projectId)) {
34
+ return res.status(403).json({ error: 'Unauthorized or missing projectId' });
35
+ }
36
+ const files = await (0, file_1.listFilesService)(adapter, req, projectId);
24
37
  res.json(files);
25
38
  }
26
39
  catch (err) {
@@ -30,12 +43,30 @@ function createStreamByRouter(config) {
30
43
  router.post('/upload', async (req, res) => {
31
44
  try {
32
45
  const auth = await config.authProvider(req);
33
- const result = await (0, file_1.uploadFileService)(adapter, req, auth.projectId);
46
+ const projectId = (req.query.projectId || req.headers['x-project-id']);
47
+ if (!projectId || !auth.projects.includes(projectId)) {
48
+ return res.status(403).json({ error: 'Unauthorized or missing projectId' });
49
+ }
50
+ const result = await (0, file_1.uploadFileService)(adapter, req, projectId);
34
51
  res.json(result);
35
52
  }
36
53
  catch (err) {
37
54
  res.status(500).json({ error: 'Failed to upload file', details: err });
38
55
  }
39
56
  });
57
+ router.get('/projects/:id', async (req, res) => {
58
+ try {
59
+ const auth = await config.authProvider(req);
60
+ const projectId = req.params.id;
61
+ if (!projectId || !auth.projects.includes(projectId)) {
62
+ return res.status(403).json({ error: 'Unauthorized project access' });
63
+ }
64
+ const project = await config.projectProvider(projectId);
65
+ res.json({ project });
66
+ }
67
+ catch (err) {
68
+ res.status(404).json({ error: 'Project not found', details: err });
69
+ }
70
+ });
40
71
  return router;
41
72
  }
@@ -4,7 +4,11 @@ exports.resolveAuth = resolveAuth;
4
4
  exports.checkRole = checkRole;
5
5
  async function resolveAuth(config, req) {
6
6
  const auth = await config.authProvider(req);
7
- if (!auth || !auth.userId || !auth.projectId || !auth.role) {
7
+ if (!auth ||
8
+ !auth.userId ||
9
+ !Array.isArray(auth.projects) ||
10
+ auth.projects.length === 0 ||
11
+ !auth.role) {
8
12
  throw new Error('Invalid or missing authentication context');
9
13
  }
10
14
  return auth;
@@ -10,20 +10,33 @@ export interface StorageAdapter {
10
10
  uploadFile(req: Request, projectId: string): Promise<any>;
11
11
  listFiles(projectId: string): Promise<any[]>;
12
12
  }
13
+ export interface ProjectInfo {
14
+ id: string;
15
+ name: string;
16
+ description?: string;
17
+ rootFolders?: FolderNode[];
18
+ settings?: {
19
+ allowUpload?: boolean;
20
+ allowSharing?: boolean;
21
+ };
22
+ }
23
+ export interface FolderNode {
24
+ id: string;
25
+ name: string;
26
+ children?: FolderNode[];
27
+ }
13
28
  export interface StreamByConfig {
14
29
  storageProvider: {
15
30
  type: StorageProviderType;
16
31
  config: S3Config;
17
32
  };
18
- authProvider: (req: Request) => Promise<{
19
- userId: string;
20
- projectId: string;
21
- role: 'viewer' | 'editor' | 'admin';
22
- }>;
33
+ authProvider: AuthProvider;
34
+ projectProvider: ProjectProvider;
23
35
  }
24
36
  export interface AuthContext {
25
37
  userId: string;
26
- projectId: string;
38
+ projects: string[];
27
39
  role: 'viewer' | 'editor' | 'admin';
28
40
  }
29
41
  export type AuthProvider = (req: Request) => Promise<AuthContext>;
42
+ export type ProjectProvider = (projectId: string) => Promise<ProjectInfo>;
package/package.json CHANGED
@@ -1,45 +1,45 @@
1
- {
2
- "name": "streamby-core",
3
- "version": "0.1.0",
4
- "description": "StreamBy middleware framework for media storage management",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": [
8
- "dist"
9
- ],
10
- "scripts": {
11
- "build": "tsc",
12
- "test": "vitest",
13
- "dev": "tsc --watch",
14
- "prepublishOnly": "npm run build"
15
- },
16
- "repository": {
17
- "type": "git",
18
- "url": "https://github.com/GwerhDev/StreamBy-Package.git"
19
- },
20
- "keywords": [
21
- "streamby",
22
- "middleware",
23
- "upload",
24
- "storage",
25
- "s3",
26
- "express",
27
- "typescript"
28
- ],
29
- "author": "TerminalCore Labs",
30
- "license": "MIT",
31
- "dependencies": {
32
- "@aws-sdk/client-s3": "^3.528.0",
33
- "express": "^4.0.0"
34
- },
35
- "devDependencies": {
36
- "@types/express": "^4.17.17",
37
- "@types/supertest": "^6.0.3",
38
- "supertest": "^7.1.0",
39
- "typescript": "^5.8.3",
40
- "vitest": "^3.1.1"
41
- },
42
- "peerDependencies": {
43
- "express": "^4.0.0"
44
- }
45
- }
1
+ {
2
+ "name": "streamby-core",
3
+ "version": "0.3.0",
4
+ "description": "StreamBy middleware framework for media storage management",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "test": "vitest",
13
+ "dev": "tsc --watch",
14
+ "prepublishOnly": "npm run build"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/GwerhDev/StreamBy-Package.git"
19
+ },
20
+ "keywords": [
21
+ "streamby",
22
+ "middleware",
23
+ "upload",
24
+ "storage",
25
+ "s3",
26
+ "express",
27
+ "typescript"
28
+ ],
29
+ "author": "TerminalCore Labs",
30
+ "license": "MIT",
31
+ "dependencies": {
32
+ "@aws-sdk/client-s3": "^3.528.0",
33
+ "express": "^4.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/express": "^4.17.17",
37
+ "@types/supertest": "^6.0.3",
38
+ "supertest": "^7.1.0",
39
+ "typescript": "^5.8.3",
40
+ "vitest": "^3.1.1"
41
+ },
42
+ "peerDependencies": {
43
+ "express": "^4.0.0"
44
+ }
45
+ }