streamby-core 0.26.9 β†’ 0.27.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,283 +1,283 @@
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 ws
25
- npm install -D @types/ws
26
- ```
27
-
28
- ---
29
-
30
- ## 🧱 Basic usage
31
-
32
- ```ts
33
- import http from 'http';
34
- import express from 'express';
35
- import { WebSocketServer } from 'ws';
36
- import { createStreamByRouter } from '@streamby/core';
37
-
38
- const app = express();
39
- const server = http.createServer(app);
40
- const wss = new WebSocketServer({ server, path: '/streamby/ws' });
41
-
42
- app.use('/streamby', express.json(), createStreamByRouter({
43
- authProvider: async (req) => {
44
- // validate token/session and return user info
45
- return { userId: 'user-id', username: 'username', role: 'admin' };
46
- },
47
- databases: [
48
- { id: 'mongo', type: 'nosql', connectionString: process.env.MONGO_URI! },
49
- { id: 'postgres', type: 'sql', connectionString: process.env.POSTGRES_URI!, main: true },
50
- ],
51
- storageProviders: [
52
- {
53
- type: 's3',
54
- config: {
55
- bucket: process.env.AWS_BUCKET!,
56
- region: process.env.AWS_REGION!,
57
- accessKeyId: process.env.AWS_ACCESS_KEY!,
58
- secretAccessKey: process.env.AWS_SECRET_KEY!,
59
- },
60
- },
61
- ],
62
- encrypt: process.env.STREAMBY_ENCRYPTION_KEY,
63
- websocket: { server: wss },
64
- }));
65
-
66
- server.listen(3000);
67
- ```
68
-
69
- > **Important:** use `http.createServer(app)` and pass `server.listen()` instead of `app.listen()`.
70
- > This is required so the `WebSocketServer` shares the same port as the HTTP server.
71
-
72
- ---
73
-
74
- ## πŸ—„οΈ Database Schema Setup
75
-
76
- `@streamby/core` requires a pre-configured database schema for its internal operations. It does not manage database connections directly; instead, it expects the host application to provide initialized connection instances. This ensures full control and flexibility for the host environment.
77
-
78
- The library will only interact with the specified schema and will not touch other schemas in your database. The `reset` option is opt-in and will only drop and recreate the specified `streamby` schema if explicitly set to `true`.
79
-
80
- ### PostgreSQL Example
81
-
82
- ```ts
83
- import { Pool } from "pg";
84
- import { setupStreambyPg } from "@streamby/core/pg/setup"; // Adjust path as per your package structure
85
-
86
- const pool = new Pool({ connectionString: process.env.DATABASE_URL });
87
-
88
- // Call setupStreambyPg to ensure the schema and tables are created
89
- // reset: false (default) ensures idempotency and no data loss
90
- await setupStreambyPg({ pool, schema: "streamby", reset: false });
91
- ```
92
-
93
- ### MongoDB Example
94
-
95
- ```ts
96
- import { MongoClient } from "mongodb";
97
- import { setupStreambyMongo } from "@streamby/core/mongo/setup"; // Adjust path as per your package structure
98
-
99
- const client = new MongoClient(process.env.MONGO_URL!);
100
-
101
- // Connect the MongoDB client
102
- await client.connect();
103
-
104
- // Call setupStreambyMongo to ensure collections and indexes are created
105
- await setupStreambyMongo({ client, dbName: "streamby" });
106
- ```
107
-
108
- ```ts
109
- import express from 'express';
110
- import { createStreamByRouter } from '@streamby/core';
111
-
112
- const app = express();
113
-
114
- app.use('/streamby', createStreamByRouter({
115
- storageProvider: {
116
- type: 's3',
117
- config: {
118
- bucket: 'your-bucket-name',
119
- region: 'your-region',
120
- accessKeyId: 'YOUR_AWS_KEY',
121
- secretAccessKey: 'YOUR_AWS_SECRET'
122
- }
123
- },
124
- authProvider: async (req) => {
125
- // extract token/cookie and validate
126
- return {
127
- userId: 'demo',
128
- projects: ['demo-project'],
129
- role: 'admin',
130
- };
131
- },
132
- projectProvider: async (projectId) => {
133
- // fetch project from your DB
134
- return {
135
- id: projectId,
136
- name: 'Demo Project',
137
- description: 'Demo for StreamBy integration',
138
- rootFolders: [],
139
- settings: { allowUpload: true }
140
- };
141
- }
142
- }));
143
-
144
- app.listen(3000);
145
- ```
146
-
147
- ---
148
-
149
- ## πŸ§ͺ Testing
150
-
151
- ```bash
152
- npm run test
153
- ```
154
-
155
- Unit tests use [Vitest](https://vitest.dev) and [Supertest](https://www.npmjs.com/package/supertest).
156
-
157
- ---
158
-
159
- ## πŸ“ Features
160
-
161
- - πŸ“‚ Multi-project access control
162
- - πŸ” Project-aware file uploads & listings
163
- - 🧹 Modular adapters per storage provider
164
- - 🧰 Built-in testability with mock auth & storage
165
- - ✨ Simple to extend for custom business logic
166
-
167
- ---
168
-
169
- ## πŸ—ΊοΈ API Endpoints
170
-
171
- Here's a summary of the key API endpoints provided by `@streamby/core`:
172
-
173
- ### `GET /streamby/projects`
174
-
175
- Lists all projects accessible by the authenticated user.
176
-
177
- **Query Parameters:**
178
- - `archived`: (Optional) Filter projects by their archived status.
179
- - `true`: Returns only archived projects.
180
- - `false`: Returns only unarchived projects.
181
- - (Omitted): Returns all projects (both archived and unarchived).
182
-
183
- **Example Response:**
184
- ```json
185
- {
186
- "projects": [
187
- {
188
- "id": "project1_id",
189
- "dbType": "nosql",
190
- "name": "My NoSQL Project",
191
- "image": "url_to_image",
192
- "archived": false
193
- },
194
- {
195
- "id": "project2_id",
196
- "dbType": "sql",
197
- "name": "My SQL Project",
198
- "image": "url_to_image",
199
- "archived": true
200
- }
201
- ]
202
- }
203
- ```
204
-
205
- ### `PATCH /streamby/projects/:id/archive`
206
-
207
- Archives a specific project. After archiving, it returns the complete list of projects for the user, including the updated project.
208
-
209
- **Example Response:**
210
- ```json
211
- {
212
- "success": true,
213
- "projects": [
214
- {
215
- "id": "project1_id",
216
- "dbType": "nosql",
217
- "name": "My NoSQL Project",
218
- "image": "url_to_image",
219
- "archived": true
220
- },
221
- {
222
- "id": "project2_id",
223
- "dbType": "sql",
224
- "name": "My SQL Project",
225
- "image": "url_to_image",
226
- "archived": false
227
- }
228
- ]
229
- }
230
- ```
231
-
232
- ### `PATCH /streamby/projects/:id/unarchive`
233
-
234
- Unarchives a specific project. After unarchiving, it returns the complete list of projects for the user, including the updated project.
235
-
236
- **Example Response:**
237
- ```json
238
- {
239
- "success": true,
240
- "projects": [
241
- {
242
- "id": "project1_id",
243
- "dbType": "nosql",
244
- "name": "My NoSQL Project",
245
- "image": "url_to_image",
246
- "archived": false
247
- },
248
- {
249
- "id": "project2_id",
250
- "dbType": "sql",
251
- "name": "My SQL Project",
252
- "image": "url_to_image",
253
- "archived": false
254
- }
255
- ]
256
- }
257
- ```
258
-
259
- ---
260
-
261
- ## 🚣 Roadmap
262
-
263
- - [ ] Support for Google Cloud Storage
264
- - [ ] CLI tool for uploading files
265
- - [ ] Role-based access control middleware
266
- - [ ] Plugin system for extended routes
267
- - [ ] Streaming support (HLS, audio, etc.)
268
-
269
- ---
270
-
271
- ## πŸ§‘β€πŸ’» Maintained by
272
-
273
- **TerminalCore Labs** – part of [Nhexa Entertainment](https://nhexa.cl)
274
- Developed as open-source infrastructure for creative and media-oriented apps.
275
-
276
- Visit us [here](https://terminalcore.cl)
277
-
278
- ---
279
-
280
- ## πŸ“Ÿ License
281
-
282
- [MIT](./LICENSE)
283
-
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 ws
25
+ npm install -D @types/ws
26
+ ```
27
+
28
+ ---
29
+
30
+ ## 🧱 Basic usage
31
+
32
+ ```ts
33
+ import http from 'http';
34
+ import express from 'express';
35
+ import { WebSocketServer } from 'ws';
36
+ import { createStreamByRouter } from '@streamby/core';
37
+
38
+ const app = express();
39
+ const server = http.createServer(app);
40
+ const wss = new WebSocketServer({ server, path: '/streamby/ws' });
41
+
42
+ app.use('/streamby', express.json(), createStreamByRouter({
43
+ authProvider: async (req) => {
44
+ // validate token/session and return user info
45
+ return { userId: 'user-id', username: 'username', role: 'admin' };
46
+ },
47
+ databases: [
48
+ { id: 'mongo', type: 'nosql', connectionString: process.env.MONGO_URI! },
49
+ { id: 'postgres', type: 'sql', connectionString: process.env.POSTGRES_URI!, main: true },
50
+ ],
51
+ storageProviders: [
52
+ {
53
+ type: 's3',
54
+ config: {
55
+ bucket: process.env.AWS_BUCKET!,
56
+ region: process.env.AWS_REGION!,
57
+ accessKeyId: process.env.AWS_ACCESS_KEY!,
58
+ secretAccessKey: process.env.AWS_SECRET_KEY!,
59
+ },
60
+ },
61
+ ],
62
+ encrypt: process.env.STREAMBY_ENCRYPTION_KEY,
63
+ websocket: { server: wss },
64
+ }));
65
+
66
+ server.listen(3000);
67
+ ```
68
+
69
+ > **Important:** use `http.createServer(app)` and pass `server.listen()` instead of `app.listen()`.
70
+ > This is required so the `WebSocketServer` shares the same port as the HTTP server.
71
+
72
+ ---
73
+
74
+ ## πŸ—„οΈ Database Schema Setup
75
+
76
+ `@streamby/core` requires a pre-configured database schema for its internal operations. It does not manage database connections directly; instead, it expects the host application to provide initialized connection instances. This ensures full control and flexibility for the host environment.
77
+
78
+ The library will only interact with the specified schema and will not touch other schemas in your database. The `reset` option is opt-in and will only drop and recreate the specified `streamby` schema if explicitly set to `true`.
79
+
80
+ ### PostgreSQL Example
81
+
82
+ ```ts
83
+ import { Pool } from "pg";
84
+ import { setupStreambyPg } from "@streamby/core/pg/setup"; // Adjust path as per your package structure
85
+
86
+ const pool = new Pool({ connectionString: process.env.DATABASE_URL });
87
+
88
+ // Call setupStreambyPg to ensure the schema and tables are created
89
+ // reset: false (default) ensures idempotency and no data loss
90
+ await setupStreambyPg({ pool, schema: "streamby", reset: false });
91
+ ```
92
+
93
+ ### MongoDB Example
94
+
95
+ ```ts
96
+ import { MongoClient } from "mongodb";
97
+ import { setupStreambyMongo } from "@streamby/core/mongo/setup"; // Adjust path as per your package structure
98
+
99
+ const client = new MongoClient(process.env.MONGO_URL!);
100
+
101
+ // Connect the MongoDB client
102
+ await client.connect();
103
+
104
+ // Call setupStreambyMongo to ensure collections and indexes are created
105
+ await setupStreambyMongo({ client, dbName: "streamby" });
106
+ ```
107
+
108
+ ```ts
109
+ import express from 'express';
110
+ import { createStreamByRouter } from '@streamby/core';
111
+
112
+ const app = express();
113
+
114
+ app.use('/streamby', createStreamByRouter({
115
+ storageProvider: {
116
+ type: 's3',
117
+ config: {
118
+ bucket: 'your-bucket-name',
119
+ region: 'your-region',
120
+ accessKeyId: 'YOUR_AWS_KEY',
121
+ secretAccessKey: 'YOUR_AWS_SECRET'
122
+ }
123
+ },
124
+ authProvider: async (req) => {
125
+ // extract token/cookie and validate
126
+ return {
127
+ userId: 'demo',
128
+ projects: ['demo-project'],
129
+ role: 'admin',
130
+ };
131
+ },
132
+ projectProvider: async (projectId) => {
133
+ // fetch project from your DB
134
+ return {
135
+ id: projectId,
136
+ name: 'Demo Project',
137
+ description: 'Demo for StreamBy integration',
138
+ rootFolders: [],
139
+ settings: { allowUpload: true }
140
+ };
141
+ }
142
+ }));
143
+
144
+ app.listen(3000);
145
+ ```
146
+
147
+ ---
148
+
149
+ ## πŸ§ͺ Testing
150
+
151
+ ```bash
152
+ npm run test
153
+ ```
154
+
155
+ Unit tests use [Vitest](https://vitest.dev) and [Supertest](https://www.npmjs.com/package/supertest).
156
+
157
+ ---
158
+
159
+ ## πŸ“ Features
160
+
161
+ - πŸ“‚ Multi-project access control
162
+ - πŸ” Project-aware file uploads & listings
163
+ - 🧹 Modular adapters per storage provider
164
+ - 🧰 Built-in testability with mock auth & storage
165
+ - ✨ Simple to extend for custom business logic
166
+
167
+ ---
168
+
169
+ ## πŸ—ΊοΈ API Endpoints
170
+
171
+ Here's a summary of the key API endpoints provided by `@streamby/core`:
172
+
173
+ ### `GET /streamby/projects`
174
+
175
+ Lists all projects accessible by the authenticated user.
176
+
177
+ **Query Parameters:**
178
+ - `archived`: (Optional) Filter projects by their archived status.
179
+ - `true`: Returns only archived projects.
180
+ - `false`: Returns only unarchived projects.
181
+ - (Omitted): Returns all projects (both archived and unarchived).
182
+
183
+ **Example Response:**
184
+ ```json
185
+ {
186
+ "projects": [
187
+ {
188
+ "id": "project1_id",
189
+ "dbType": "nosql",
190
+ "name": "My NoSQL Project",
191
+ "image": "url_to_image",
192
+ "archived": false
193
+ },
194
+ {
195
+ "id": "project2_id",
196
+ "dbType": "sql",
197
+ "name": "My SQL Project",
198
+ "image": "url_to_image",
199
+ "archived": true
200
+ }
201
+ ]
202
+ }
203
+ ```
204
+
205
+ ### `PATCH /streamby/projects/:id/archive`
206
+
207
+ Archives a specific project. After archiving, it returns the complete list of projects for the user, including the updated project.
208
+
209
+ **Example Response:**
210
+ ```json
211
+ {
212
+ "success": true,
213
+ "projects": [
214
+ {
215
+ "id": "project1_id",
216
+ "dbType": "nosql",
217
+ "name": "My NoSQL Project",
218
+ "image": "url_to_image",
219
+ "archived": true
220
+ },
221
+ {
222
+ "id": "project2_id",
223
+ "dbType": "sql",
224
+ "name": "My SQL Project",
225
+ "image": "url_to_image",
226
+ "archived": false
227
+ }
228
+ ]
229
+ }
230
+ ```
231
+
232
+ ### `PATCH /streamby/projects/:id/unarchive`
233
+
234
+ Unarchives a specific project. After unarchiving, it returns the complete list of projects for the user, including the updated project.
235
+
236
+ **Example Response:**
237
+ ```json
238
+ {
239
+ "success": true,
240
+ "projects": [
241
+ {
242
+ "id": "project1_id",
243
+ "dbType": "nosql",
244
+ "name": "My NoSQL Project",
245
+ "image": "url_to_image",
246
+ "archived": false
247
+ },
248
+ {
249
+ "id": "project2_id",
250
+ "dbType": "sql",
251
+ "name": "My SQL Project",
252
+ "image": "url_to_image",
253
+ "archived": false
254
+ }
255
+ ]
256
+ }
257
+ ```
258
+
259
+ ---
260
+
261
+ ## 🚣 Roadmap
262
+
263
+ - [ ] Support for Google Cloud Storage
264
+ - [ ] CLI tool for uploading files
265
+ - [ ] Role-based access control middleware
266
+ - [ ] Plugin system for extended routes
267
+ - [ ] Streaming support (HLS, audio, etc.)
268
+
269
+ ---
270
+
271
+ ## πŸ§‘β€πŸ’» Maintained by
272
+
273
+ **TerminalCore Labs** – part of [Nhexa Entertainment](https://nhexa.cl)
274
+ Developed as open-source infrastructure for creative and media-oriented apps.
275
+
276
+ Visit us [here](https://terminalcore.cl)
277
+
278
+ ---
279
+
280
+ ## πŸ“Ÿ License
281
+
282
+ [MIT](./LICENSE)
283
+