instaserve 1.1.11 → 1.1.12

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
@@ -238,3 +238,49 @@ export default {
238
238
  }
239
239
  }
240
240
  ```
241
+
242
+ ## Library Modules
243
+
244
+ The `lib/` directory contains useful modules for authentication, storage, and database manipulation.
245
+
246
+ ### Auth0 (`lib/auth0.js`)
247
+ Provides authentication routes and `req.user` handling via Auth0.
248
+ - **Routes:** `GET /login`, `GET /logout`, `GET /callback`
249
+ - **Usage:** Import and spread into your routes.
250
+ ```javascript
251
+ import auth0 from './lib/auth0.js';
252
+
253
+ export default {
254
+ ...auth0,
255
+ // other routes
256
+ }
257
+ ```
258
+
259
+ ### R2 Storage (`lib/r2.js`)
260
+ Utilities for Cloudflare R2 or S3-compatible object storage.
261
+ - **Features:** `uploadToR2`, `downloadFromR2`, `listR2Files`
262
+ - **Routes:** `fileRoutes` exports helpers for file management.
263
+ - **Usage:**
264
+ ```javascript
265
+ import { fileRoutes } from './lib/r2.js';
266
+
267
+ export default {
268
+ ...fileRoutes,
269
+ // other routes
270
+ }
271
+ ```
272
+
273
+ ### SQLite (`lib/sqlite.js`)
274
+ Provides a local SQLite database with a per-user Key-Value store.
275
+ - **Features:** User management table, KV table.
276
+ - **Routes:** `_auth` middleware, CRUD for KV store (`/api`, `/all`).
277
+ - **Usage:**
278
+ ```javascript
279
+ import sqliteRoutes from './lib/sqlite.js';
280
+
281
+ export default {
282
+ ...sqliteRoutes,
283
+ // other routes
284
+ }
285
+ ```
286
+ ```
package/lib/auth0.js CHANGED
@@ -1,6 +1,20 @@
1
+ /**
2
+ * Auth0 Authentication Module
3
+ *
4
+ * Usage:
5
+ * 1. Import this module in your routes.js file.
6
+ * 2. Merge it into your default export routes object: `...auth0`.
7
+ * 3. Ensure 'secrets' is globally available or imported with `auth0_domain`, `auth0_clientid`, and `auth0_clientsecret`.
8
+ * 4. Ensure `global.sqlite` is initialized (typically by importing lib/sqlite.js).
9
+ *
10
+ * Routes provided:
11
+ * - GET /login: Initiates Auth0 login flow.
12
+ * - GET /logout: Clears session and token.
13
+ * - GET /callback: Handles Auth0 callback, validates token, creates user in DB.
14
+ */
1
15
  export default {
2
16
  "GET /login": (req, res) => {
3
- const REDIRECT_URI = "https://${req.headers.host}/callback";
17
+ const REDIRECT_URI = `https://${req.headers.host}/callback`;
4
18
  const state = crypto.randomBytes(16).toString("hex");
5
19
  global.a0state = state;
6
20
  const url = `https://${secrets.auth0_domain}/authorize?response_type=code&client_id=${secrets.auth0_clientid}&redirect_uri=${encodeURIComponent(REDIRECT_URI)}&scope=openid%20profile%20email&state=${encodeURIComponent(state)}`;
@@ -18,8 +32,8 @@ export default {
18
32
  res.end();
19
33
  },
20
34
  "GET /callback": async (req, res, data) => {
21
- const REDIRECT_URI = "https://${req.headers.host}/callback";
22
- const tokenRes = await fetch("https://digplan.auth0.com/oauth/token", {
35
+ const REDIRECT_URI = `https://${req.headers.host}/callback`;
36
+ const tokenRes = await fetch(`https://${secrets.auth0_domain}/oauth/token`, {
23
37
  method: "POST",
24
38
  headers: { "content-type": "application/json" },
25
39
  body: JSON.stringify({
package/lib/r2.js CHANGED
@@ -1,3 +1,24 @@
1
+ /**
2
+ * Cloudflare R2 / S3-Compatible Storage Module
3
+ *
4
+ * Usage:
5
+ * 1. Import desired functions or `fileRoutes` from this file.
6
+ * 2. Requires a `secrets` object with:
7
+ * - cloudflareAccessKeyId
8
+ * - cloudflareSecretAccessKey
9
+ * - cloudflareAccountId
10
+ *
11
+ * Exports:
12
+ * - uploadToR2(bucket, key, body, contentType, secrets)
13
+ * - downloadFromR2(bucket, key, secrets)
14
+ * - deleteFromR2(bucket, key, secrets)
15
+ * - listR2Files(bucket, prefix, secrets)
16
+ * - getSignedDownloadUrl(bucket, key, secrets)
17
+ *
18
+ * fileRoutes:
19
+ * Contains helper handlers for file operations. Note that `upload`, `download`, `delete`
20
+ * are generic async functions awaiting `data` parameters, while `POST /files` is a standard route.
21
+ */
1
22
  import https from 'https';
2
23
  import crypto from 'crypto';
3
24
  import { URL } from 'url';
package/lib/sqlite.js CHANGED
@@ -1,3 +1,22 @@
1
+ /**
2
+ * SQLite Database & Key-Value Store Module
3
+ *
4
+ * Usage:
5
+ * 1. Import this module to initialize the database ('data.db') and tables ('users', 'kv').
6
+ * 2. Access the database instance via `global.sqlite`.
7
+ * 3. Exported routes provide a per-user Key-Value store mechanism guarded by the `_auth` middleware.
8
+ *
9
+ * Tables created:
10
+ * - users: id, username, pass, token, picture, name, email
11
+ * - kv: key (format: "username:key"), value
12
+ *
13
+ * Routes provided:
14
+ * - _auth: Middleware for protecting routes (assigns `req.user`).
15
+ * - POST /api: Set a KV pair for the logged-in user.
16
+ * - GET /api: Get a value by key.
17
+ * - GET /all: List all keys/values for the user.
18
+ * - DELETE /api: Delete a key.
19
+ */
1
20
  import Database from "better-sqlite3";
2
21
  import { fileRoutes } from "./lib/file-routes.js";
3
22
  import secrets from "./secrets.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instaserve",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
4
4
  "description": "Instant web stack",
5
5
  "main": "module.mjs",
6
6
  "bin": "./instaserve",