@gravito/nebula 1.0.0-alpha.2

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 ADDED
@@ -0,0 +1,47 @@
1
+ # @gravito/nebula
2
+
3
+ > The Standard Storage Orbit for Galaxy Architecture.
4
+
5
+ Provides an abstraction layer for file storage, with a built-in Local Disk provider.
6
+
7
+ ## 📦 Installation
8
+
9
+ ```bash
10
+ bun add @gravito/nebula
11
+ ```
12
+
13
+ ## 🚀 Usage
14
+
15
+ ```typescript
16
+ import { PlanetCore } from 'gravito-core';
17
+ import orbitStorage from '@gravito/nebula';
18
+
19
+ const core = new PlanetCore();
20
+
21
+ // Initialize Storage Orbit (Local)
22
+ const storage = orbitStorage(core, {
23
+ local: {
24
+ root: './uploads',
25
+ baseUrl: '/uploads'
26
+ },
27
+ exposeAs: 'storage' // Access via c.get('storage')
28
+ });
29
+
30
+ // Use in routes
31
+ core.app.post('/upload', async (c) => {
32
+ const body = await c.req.parseBody();
33
+ const file = body['file'];
34
+
35
+ if (file instanceof File) {
36
+ await storage.put(file.name, file);
37
+ return c.json({ url: storage.getUrl(file.name) });
38
+ }
39
+ return c.text('No file uploaded', 400);
40
+ });
41
+ ```
42
+
43
+ ## 🪝 Hooks
44
+
45
+ - `storage:init` - Fired when initialized.
46
+ - `storage:upload` - (Filter) Modify data before upload.
47
+ - `storage:uploaded` - (Action) Triggered after successful upload.
package/dist/index.cjs ADDED
@@ -0,0 +1,140 @@
1
+ var __create = Object.create;
2
+ var __getProtoOf = Object.getPrototypeOf;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __moduleCache = /* @__PURE__ */ new WeakMap;
19
+ var __toCommonJS = (from) => {
20
+ var entry = __moduleCache.get(from), desc;
21
+ if (entry)
22
+ return entry;
23
+ entry = __defProp({}, "__esModule", { value: true });
24
+ if (from && typeof from === "object" || typeof from === "function")
25
+ __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
26
+ get: () => from[key],
27
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
28
+ }));
29
+ __moduleCache.set(from, entry);
30
+ return entry;
31
+ };
32
+ var __export = (target, all) => {
33
+ for (var name in all)
34
+ __defProp(target, name, {
35
+ get: all[name],
36
+ enumerable: true,
37
+ configurable: true,
38
+ set: (newValue) => all[name] = () => newValue
39
+ });
40
+ };
41
+
42
+ // src/index.ts
43
+ var exports_src = {};
44
+ __export(exports_src, {
45
+ default: () => orbitStorage,
46
+ OrbitStorage: () => OrbitStorage,
47
+ LocalStorageProvider: () => LocalStorageProvider
48
+ });
49
+ module.exports = __toCommonJS(exports_src);
50
+ var import_promises = require("node:fs/promises");
51
+ var import_node_path = require("node:path");
52
+
53
+ class LocalStorageProvider {
54
+ rootDir;
55
+ baseUrl;
56
+ constructor(rootDir, baseUrl = "/storage") {
57
+ this.rootDir = rootDir;
58
+ this.baseUrl = baseUrl;
59
+ }
60
+ async put(key, data) {
61
+ const path = import_node_path.join(this.rootDir, key);
62
+ const dir = path.substring(0, path.lastIndexOf("/"));
63
+ if (dir && dir !== this.rootDir) {
64
+ await import_promises.mkdir(dir, { recursive: true });
65
+ }
66
+ await Bun.write(path, data);
67
+ }
68
+ async get(key) {
69
+ const file = Bun.file(import_node_path.join(this.rootDir, key));
70
+ if (!await file.exists()) {
71
+ return null;
72
+ }
73
+ return file;
74
+ }
75
+ async delete(key) {
76
+ const fs = await import("node:fs/promises");
77
+ try {
78
+ await fs.unlink(import_node_path.join(this.rootDir, key));
79
+ } catch {}
80
+ }
81
+ getUrl(key) {
82
+ return `${this.baseUrl}/${key}`;
83
+ }
84
+ }
85
+
86
+ class OrbitStorage {
87
+ options;
88
+ constructor(options) {
89
+ this.options = options;
90
+ }
91
+ install(core) {
92
+ const config = this.options || core.config.get("storage");
93
+ if (!config) {
94
+ throw new Error('[OrbitStorage] Configuration is required. Please provide options or set "storage" in core config.');
95
+ }
96
+ const { exposeAs = "storage" } = config;
97
+ const logger = core.logger;
98
+ logger.info(`[OrbitStorage] Initializing Storage (Exposed as: ${exposeAs})`);
99
+ let provider = config.provider;
100
+ if (!provider && config.local) {
101
+ logger.info(`[OrbitStorage] Using LocalStorageProvider at ${config.local.root}`);
102
+ provider = new LocalStorageProvider(config.local.root, config.local.baseUrl);
103
+ }
104
+ if (!provider) {
105
+ throw new Error("[OrbitStorage] No provider configured. Please provide a provider instance or local configuration.");
106
+ }
107
+ const storageService = {
108
+ ...provider,
109
+ put: async (key, data) => {
110
+ const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
111
+ await provider?.put(key, finalData);
112
+ await core.hooks.doAction("storage:uploaded", { key });
113
+ }
114
+ };
115
+ core.adapter.use("*", async (c, next) => {
116
+ c.set(exposeAs, storageService);
117
+ await next();
118
+ });
119
+ core.hooks.doAction("storage:init", storageService);
120
+ }
121
+ }
122
+ function orbitStorage(core, options) {
123
+ const orbit = new OrbitStorage(options);
124
+ orbit.install(core);
125
+ let provider = options.provider;
126
+ if (!provider && options.local) {
127
+ provider = new LocalStorageProvider(options.local.root, options.local.baseUrl);
128
+ }
129
+ if (!provider) {
130
+ throw new Error("[OrbitStorage] No provider configured.");
131
+ }
132
+ return {
133
+ ...provider,
134
+ put: async (key, data) => {
135
+ const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
136
+ await provider?.put(key, finalData);
137
+ await core.hooks.doAction("storage:uploaded", { key });
138
+ }
139
+ };
140
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,116 @@
1
+ import { createRequire } from "node:module";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
19
+
20
+ // src/index.ts
21
+ import { mkdir } from "node:fs/promises";
22
+ import { join } from "node:path";
23
+
24
+ class LocalStorageProvider {
25
+ rootDir;
26
+ baseUrl;
27
+ constructor(rootDir, baseUrl = "/storage") {
28
+ this.rootDir = rootDir;
29
+ this.baseUrl = baseUrl;
30
+ }
31
+ async put(key, data) {
32
+ const path = join(this.rootDir, key);
33
+ const dir = path.substring(0, path.lastIndexOf("/"));
34
+ if (dir && dir !== this.rootDir) {
35
+ await mkdir(dir, { recursive: true });
36
+ }
37
+ await Bun.write(path, data);
38
+ }
39
+ async get(key) {
40
+ const file = Bun.file(join(this.rootDir, key));
41
+ if (!await file.exists()) {
42
+ return null;
43
+ }
44
+ return file;
45
+ }
46
+ async delete(key) {
47
+ const fs = await import("node:fs/promises");
48
+ try {
49
+ await fs.unlink(join(this.rootDir, key));
50
+ } catch {}
51
+ }
52
+ getUrl(key) {
53
+ return `${this.baseUrl}/${key}`;
54
+ }
55
+ }
56
+
57
+ class OrbitStorage {
58
+ options;
59
+ constructor(options) {
60
+ this.options = options;
61
+ }
62
+ install(core) {
63
+ const config = this.options || core.config.get("storage");
64
+ if (!config) {
65
+ throw new Error('[OrbitStorage] Configuration is required. Please provide options or set "storage" in core config.');
66
+ }
67
+ const { exposeAs = "storage" } = config;
68
+ const logger = core.logger;
69
+ logger.info(`[OrbitStorage] Initializing Storage (Exposed as: ${exposeAs})`);
70
+ let provider = config.provider;
71
+ if (!provider && config.local) {
72
+ logger.info(`[OrbitStorage] Using LocalStorageProvider at ${config.local.root}`);
73
+ provider = new LocalStorageProvider(config.local.root, config.local.baseUrl);
74
+ }
75
+ if (!provider) {
76
+ throw new Error("[OrbitStorage] No provider configured. Please provide a provider instance or local configuration.");
77
+ }
78
+ const storageService = {
79
+ ...provider,
80
+ put: async (key, data) => {
81
+ const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
82
+ await provider?.put(key, finalData);
83
+ await core.hooks.doAction("storage:uploaded", { key });
84
+ }
85
+ };
86
+ core.adapter.use("*", async (c, next) => {
87
+ c.set(exposeAs, storageService);
88
+ await next();
89
+ });
90
+ core.hooks.doAction("storage:init", storageService);
91
+ }
92
+ }
93
+ function orbitStorage(core, options) {
94
+ const orbit = new OrbitStorage(options);
95
+ orbit.install(core);
96
+ let provider = options.provider;
97
+ if (!provider && options.local) {
98
+ provider = new LocalStorageProvider(options.local.root, options.local.baseUrl);
99
+ }
100
+ if (!provider) {
101
+ throw new Error("[OrbitStorage] No provider configured.");
102
+ }
103
+ return {
104
+ ...provider,
105
+ put: async (key, data) => {
106
+ const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
107
+ await provider?.put(key, finalData);
108
+ await core.hooks.doAction("storage:uploaded", { key });
109
+ }
110
+ };
111
+ }
112
+ export {
113
+ orbitStorage as default,
114
+ OrbitStorage,
115
+ LocalStorageProvider
116
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@gravito/nebula",
3
+ "version": "1.0.0-alpha.2",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Standard Storage Orbit for Galaxy Architecture",
8
+ "module": "./dist/index.mjs",
9
+ "main": "./dist/index.cjs",
10
+ "type": "module",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "scripts": {
25
+ "build": "bun run build.ts",
26
+ "test": "bun test",
27
+ "typecheck": "tsc --noEmit"
28
+ },
29
+ "keywords": [
30
+ "gravito",
31
+ "orbit",
32
+ "storage",
33
+ "upload"
34
+ ],
35
+ "author": "Carl Lee <carllee0520@gmail.com>",
36
+ "license": "MIT",
37
+ "peerDependencies": {
38
+ "gravito-core": "1.0.0-beta.2"
39
+ },
40
+ "devDependencies": {
41
+ "gravito-core": "1.0.0-beta.2",
42
+ "hono": "^4.11.1",
43
+ "bun-types": "latest",
44
+ "typescript": "^5.9.3"
45
+ },
46
+ "homepage": "https://github.com/gravito-framework/gravito#readme",
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/gravito-framework/gravito.git",
50
+ "directory": "packages/nebula"
51
+ }
52
+ }