@gravito/nebula 1.0.0-alpha.5 → 1.0.0-beta.1
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/dist/index.cjs +101 -64
- package/dist/index.d.cts +93 -0
- package/dist/index.d.ts +93 -0
- package/dist/{index.mjs → index.js} +67 -43
- package/package.json +5 -4
package/dist/index.cjs
CHANGED
|
@@ -1,141 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
7
|
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
8
|
var __export = (target, all) => {
|
|
33
9
|
for (var name in all)
|
|
34
|
-
__defProp(target, name, {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
40
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
41
29
|
|
|
42
30
|
// src/index.ts
|
|
43
|
-
var
|
|
44
|
-
__export(
|
|
45
|
-
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
LocalStorageProvider: () => LocalStorageProvider,
|
|
34
|
+
OrbitNebula: () => OrbitNebula,
|
|
46
35
|
OrbitStorage: () => OrbitStorage,
|
|
47
|
-
|
|
36
|
+
default: () => orbitStorage
|
|
48
37
|
});
|
|
49
|
-
module.exports = __toCommonJS(
|
|
50
|
-
var import_promises = require("
|
|
51
|
-
var import_node_path = require("
|
|
52
|
-
|
|
53
|
-
class LocalStorageProvider {
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
var import_promises = require("fs/promises");
|
|
40
|
+
var import_node_path = require("path");
|
|
41
|
+
var LocalStorageProvider = class {
|
|
54
42
|
rootDir;
|
|
55
43
|
baseUrl;
|
|
44
|
+
/**
|
|
45
|
+
* Create a new LocalStorageProvider.
|
|
46
|
+
*
|
|
47
|
+
* @param rootDir - The root directory for storage.
|
|
48
|
+
* @param baseUrl - The base URL for accessing stored files.
|
|
49
|
+
*/
|
|
56
50
|
constructor(rootDir, baseUrl = "/storage") {
|
|
57
51
|
this.rootDir = rootDir;
|
|
58
52
|
this.baseUrl = baseUrl;
|
|
59
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Store data in a file.
|
|
56
|
+
*
|
|
57
|
+
* @param key - The storage key (path).
|
|
58
|
+
* @param data - The data to store.
|
|
59
|
+
*/
|
|
60
60
|
async put(key, data) {
|
|
61
|
-
const path = import_node_path.join(this.rootDir, key);
|
|
61
|
+
const path = (0, import_node_path.join)(this.rootDir, key);
|
|
62
62
|
const dir = path.substring(0, path.lastIndexOf("/"));
|
|
63
63
|
if (dir && dir !== this.rootDir) {
|
|
64
|
-
await import_promises.mkdir(dir, { recursive: true });
|
|
64
|
+
await (0, import_promises.mkdir)(dir, { recursive: true });
|
|
65
65
|
}
|
|
66
66
|
await Bun.write(path, data);
|
|
67
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Retrieve a file.
|
|
70
|
+
*
|
|
71
|
+
* @param key - The storage key.
|
|
72
|
+
* @returns A promise resolving to the file Blob or null if not found.
|
|
73
|
+
*/
|
|
68
74
|
async get(key) {
|
|
69
|
-
const file = Bun.file(import_node_path.join(this.rootDir, key));
|
|
75
|
+
const file = Bun.file((0, import_node_path.join)(this.rootDir, key));
|
|
70
76
|
if (!await file.exists()) {
|
|
71
77
|
return null;
|
|
72
78
|
}
|
|
73
79
|
return file;
|
|
74
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Delete a file.
|
|
83
|
+
*
|
|
84
|
+
* @param key - The storage key.
|
|
85
|
+
*/
|
|
75
86
|
async delete(key) {
|
|
76
|
-
const fs = await import("
|
|
87
|
+
const fs = await import("fs/promises");
|
|
77
88
|
try {
|
|
78
|
-
await fs.unlink(import_node_path.join(this.rootDir, key));
|
|
79
|
-
} catch {
|
|
89
|
+
await fs.unlink((0, import_node_path.join)(this.rootDir, key));
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
80
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Get the public URL for a file.
|
|
95
|
+
*
|
|
96
|
+
* @param key - The storage key.
|
|
97
|
+
* @returns The public URL string.
|
|
98
|
+
*/
|
|
81
99
|
getUrl(key) {
|
|
82
100
|
return `${this.baseUrl}/${key}`;
|
|
83
101
|
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
class OrbitStorage {
|
|
87
|
-
options;
|
|
102
|
+
};
|
|
103
|
+
var OrbitNebula = class {
|
|
88
104
|
constructor(options) {
|
|
89
105
|
this.options = options;
|
|
90
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Install storage service into PlanetCore.
|
|
109
|
+
*
|
|
110
|
+
* @param core - The PlanetCore instance.
|
|
111
|
+
* @throws {Error} If configuration or provider is missing.
|
|
112
|
+
*/
|
|
91
113
|
install(core) {
|
|
92
114
|
const config = this.options || core.config.get("storage");
|
|
93
115
|
if (!config) {
|
|
94
|
-
throw new Error(
|
|
116
|
+
throw new Error(
|
|
117
|
+
'[OrbitNebula] Configuration is required. Please provide options or set "storage" in core config.'
|
|
118
|
+
);
|
|
95
119
|
}
|
|
96
120
|
const { exposeAs = "storage" } = config;
|
|
97
121
|
const logger = core.logger;
|
|
98
|
-
logger.info(`[
|
|
122
|
+
logger.info(`[OrbitNebula] Initializing Storage (Exposed as: ${exposeAs})`);
|
|
99
123
|
let provider = config.provider;
|
|
100
124
|
if (!provider && config.local) {
|
|
101
|
-
logger.info(`[
|
|
125
|
+
logger.info(`[OrbitNebula] Using LocalStorageProvider at ${config.local.root}`);
|
|
102
126
|
provider = new LocalStorageProvider(config.local.root, config.local.baseUrl);
|
|
103
127
|
}
|
|
104
128
|
if (!provider) {
|
|
105
|
-
throw new Error(
|
|
129
|
+
throw new Error(
|
|
130
|
+
"[OrbitNebula] No provider configured. Please provide a provider instance or local configuration."
|
|
131
|
+
);
|
|
106
132
|
}
|
|
107
133
|
const storageService = {
|
|
108
|
-
...provider,
|
|
109
134
|
put: async (key, data) => {
|
|
110
135
|
const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
|
|
111
136
|
await provider?.put(key, finalData);
|
|
112
137
|
await core.hooks.doAction("storage:uploaded", { key });
|
|
113
|
-
}
|
|
138
|
+
},
|
|
139
|
+
get: (key) => provider?.get(key),
|
|
140
|
+
delete: (key) => provider?.delete(key),
|
|
141
|
+
getUrl: (key) => provider?.getUrl(key)
|
|
114
142
|
};
|
|
115
143
|
core.adapter.use("*", async (c, next) => {
|
|
116
144
|
c.set(exposeAs, storageService);
|
|
117
145
|
await next();
|
|
118
|
-
return;
|
|
146
|
+
return void 0;
|
|
119
147
|
});
|
|
120
148
|
core.hooks.doAction("storage:init", storageService);
|
|
121
149
|
}
|
|
122
|
-
}
|
|
150
|
+
};
|
|
123
151
|
function orbitStorage(core, options) {
|
|
124
|
-
const orbit = new
|
|
152
|
+
const orbit = new OrbitNebula(options);
|
|
125
153
|
orbit.install(core);
|
|
126
154
|
let provider = options.provider;
|
|
127
155
|
if (!provider && options.local) {
|
|
128
156
|
provider = new LocalStorageProvider(options.local.root, options.local.baseUrl);
|
|
129
157
|
}
|
|
130
158
|
if (!provider) {
|
|
131
|
-
throw new Error("[
|
|
159
|
+
throw new Error("[OrbitNebula] No provider configured.");
|
|
132
160
|
}
|
|
133
161
|
return {
|
|
134
|
-
...provider,
|
|
135
162
|
put: async (key, data) => {
|
|
136
163
|
const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
|
|
137
164
|
await provider?.put(key, finalData);
|
|
138
165
|
await core.hooks.doAction("storage:uploaded", { key });
|
|
139
|
-
}
|
|
166
|
+
},
|
|
167
|
+
get: (key) => provider?.get(key),
|
|
168
|
+
delete: (key) => provider?.delete(key),
|
|
169
|
+
getUrl: (key) => provider?.getUrl(key)
|
|
140
170
|
};
|
|
141
171
|
}
|
|
172
|
+
var OrbitStorage = OrbitNebula;
|
|
173
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
174
|
+
0 && (module.exports = {
|
|
175
|
+
LocalStorageProvider,
|
|
176
|
+
OrbitNebula,
|
|
177
|
+
OrbitStorage
|
|
178
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { PlanetCore, GravitoOrbit } from 'gravito-core';
|
|
2
|
+
|
|
3
|
+
interface StorageProvider {
|
|
4
|
+
put(key: string, data: Blob | Buffer | string): Promise<void>;
|
|
5
|
+
get(key: string): Promise<Blob | null>;
|
|
6
|
+
delete(key: string): Promise<void>;
|
|
7
|
+
getUrl(key: string): string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Local storage provider implementation.
|
|
11
|
+
*/
|
|
12
|
+
declare class LocalStorageProvider implements StorageProvider {
|
|
13
|
+
private rootDir;
|
|
14
|
+
private baseUrl;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new LocalStorageProvider.
|
|
17
|
+
*
|
|
18
|
+
* @param rootDir - The root directory for storage.
|
|
19
|
+
* @param baseUrl - The base URL for accessing stored files.
|
|
20
|
+
*/
|
|
21
|
+
constructor(rootDir: string, baseUrl?: string);
|
|
22
|
+
/**
|
|
23
|
+
* Store data in a file.
|
|
24
|
+
*
|
|
25
|
+
* @param key - The storage key (path).
|
|
26
|
+
* @param data - The data to store.
|
|
27
|
+
*/
|
|
28
|
+
put(key: string, data: Blob | Buffer | string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Retrieve a file.
|
|
31
|
+
*
|
|
32
|
+
* @param key - The storage key.
|
|
33
|
+
* @returns A promise resolving to the file Blob or null if not found.
|
|
34
|
+
*/
|
|
35
|
+
get(key: string): Promise<Blob | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Delete a file.
|
|
38
|
+
*
|
|
39
|
+
* @param key - The storage key.
|
|
40
|
+
*/
|
|
41
|
+
delete(key: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Get the public URL for a file.
|
|
44
|
+
*
|
|
45
|
+
* @param key - The storage key.
|
|
46
|
+
* @returns The public URL string.
|
|
47
|
+
*/
|
|
48
|
+
getUrl(key: string): string;
|
|
49
|
+
}
|
|
50
|
+
interface OrbitNebulaOptions {
|
|
51
|
+
provider?: StorageProvider;
|
|
52
|
+
exposeAs?: string;
|
|
53
|
+
local?: {
|
|
54
|
+
root: string;
|
|
55
|
+
baseUrl?: string;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** @deprecated Use OrbitNebulaOptions instead */
|
|
59
|
+
type OrbitStorageOptions = OrbitNebulaOptions;
|
|
60
|
+
/**
|
|
61
|
+
* OrbitNebula - Storage Orbit
|
|
62
|
+
*
|
|
63
|
+
* Provides file storage functionality for Gravito applications.
|
|
64
|
+
*/
|
|
65
|
+
declare class OrbitNebula implements GravitoOrbit {
|
|
66
|
+
private options?;
|
|
67
|
+
constructor(options?: OrbitNebulaOptions | undefined);
|
|
68
|
+
/**
|
|
69
|
+
* Install storage service into PlanetCore.
|
|
70
|
+
*
|
|
71
|
+
* @param core - The PlanetCore instance.
|
|
72
|
+
* @throws {Error} If configuration or provider is missing.
|
|
73
|
+
*/
|
|
74
|
+
install(core: PlanetCore): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Functional API for installing OrbitNebula.
|
|
78
|
+
*
|
|
79
|
+
* @param core - The PlanetCore instance.
|
|
80
|
+
* @param options - Storage options.
|
|
81
|
+
* @returns The configured storage provider wrapper.
|
|
82
|
+
* @throws {Error} If provider is not configured.
|
|
83
|
+
*/
|
|
84
|
+
declare function orbitStorage(core: PlanetCore, options: OrbitNebulaOptions): {
|
|
85
|
+
put: (key: string, data: Blob | Buffer | string) => Promise<void>;
|
|
86
|
+
get: (key: string) => Promise<Blob | null>;
|
|
87
|
+
delete: (key: string) => Promise<void>;
|
|
88
|
+
getUrl: (key: string) => string;
|
|
89
|
+
};
|
|
90
|
+
/** @deprecated Use OrbitNebula instead */
|
|
91
|
+
declare const OrbitStorage: typeof OrbitNebula;
|
|
92
|
+
|
|
93
|
+
export { LocalStorageProvider, OrbitNebula, type OrbitNebulaOptions, OrbitStorage, type OrbitStorageOptions, type StorageProvider, orbitStorage as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { PlanetCore, GravitoOrbit } from 'gravito-core';
|
|
2
|
+
|
|
3
|
+
interface StorageProvider {
|
|
4
|
+
put(key: string, data: Blob | Buffer | string): Promise<void>;
|
|
5
|
+
get(key: string): Promise<Blob | null>;
|
|
6
|
+
delete(key: string): Promise<void>;
|
|
7
|
+
getUrl(key: string): string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Local storage provider implementation.
|
|
11
|
+
*/
|
|
12
|
+
declare class LocalStorageProvider implements StorageProvider {
|
|
13
|
+
private rootDir;
|
|
14
|
+
private baseUrl;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new LocalStorageProvider.
|
|
17
|
+
*
|
|
18
|
+
* @param rootDir - The root directory for storage.
|
|
19
|
+
* @param baseUrl - The base URL for accessing stored files.
|
|
20
|
+
*/
|
|
21
|
+
constructor(rootDir: string, baseUrl?: string);
|
|
22
|
+
/**
|
|
23
|
+
* Store data in a file.
|
|
24
|
+
*
|
|
25
|
+
* @param key - The storage key (path).
|
|
26
|
+
* @param data - The data to store.
|
|
27
|
+
*/
|
|
28
|
+
put(key: string, data: Blob | Buffer | string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Retrieve a file.
|
|
31
|
+
*
|
|
32
|
+
* @param key - The storage key.
|
|
33
|
+
* @returns A promise resolving to the file Blob or null if not found.
|
|
34
|
+
*/
|
|
35
|
+
get(key: string): Promise<Blob | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Delete a file.
|
|
38
|
+
*
|
|
39
|
+
* @param key - The storage key.
|
|
40
|
+
*/
|
|
41
|
+
delete(key: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Get the public URL for a file.
|
|
44
|
+
*
|
|
45
|
+
* @param key - The storage key.
|
|
46
|
+
* @returns The public URL string.
|
|
47
|
+
*/
|
|
48
|
+
getUrl(key: string): string;
|
|
49
|
+
}
|
|
50
|
+
interface OrbitNebulaOptions {
|
|
51
|
+
provider?: StorageProvider;
|
|
52
|
+
exposeAs?: string;
|
|
53
|
+
local?: {
|
|
54
|
+
root: string;
|
|
55
|
+
baseUrl?: string;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** @deprecated Use OrbitNebulaOptions instead */
|
|
59
|
+
type OrbitStorageOptions = OrbitNebulaOptions;
|
|
60
|
+
/**
|
|
61
|
+
* OrbitNebula - Storage Orbit
|
|
62
|
+
*
|
|
63
|
+
* Provides file storage functionality for Gravito applications.
|
|
64
|
+
*/
|
|
65
|
+
declare class OrbitNebula implements GravitoOrbit {
|
|
66
|
+
private options?;
|
|
67
|
+
constructor(options?: OrbitNebulaOptions | undefined);
|
|
68
|
+
/**
|
|
69
|
+
* Install storage service into PlanetCore.
|
|
70
|
+
*
|
|
71
|
+
* @param core - The PlanetCore instance.
|
|
72
|
+
* @throws {Error} If configuration or provider is missing.
|
|
73
|
+
*/
|
|
74
|
+
install(core: PlanetCore): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Functional API for installing OrbitNebula.
|
|
78
|
+
*
|
|
79
|
+
* @param core - The PlanetCore instance.
|
|
80
|
+
* @param options - Storage options.
|
|
81
|
+
* @returns The configured storage provider wrapper.
|
|
82
|
+
* @throws {Error} If provider is not configured.
|
|
83
|
+
*/
|
|
84
|
+
declare function orbitStorage(core: PlanetCore, options: OrbitNebulaOptions): {
|
|
85
|
+
put: (key: string, data: Blob | Buffer | string) => Promise<void>;
|
|
86
|
+
get: (key: string) => Promise<Blob | null>;
|
|
87
|
+
delete: (key: string) => Promise<void>;
|
|
88
|
+
getUrl: (key: string) => string;
|
|
89
|
+
};
|
|
90
|
+
/** @deprecated Use OrbitNebula instead */
|
|
91
|
+
declare const OrbitStorage: typeof OrbitNebula;
|
|
92
|
+
|
|
93
|
+
export { LocalStorageProvider, OrbitNebula, type OrbitNebulaOptions, OrbitStorage, type OrbitStorageOptions, type StorageProvider, orbitStorage as default };
|
|
@@ -1,33 +1,25 @@
|
|
|
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
1
|
// src/index.ts
|
|
21
|
-
import { mkdir } from "
|
|
22
|
-
import { join } from "
|
|
23
|
-
|
|
24
|
-
class LocalStorageProvider {
|
|
2
|
+
import { mkdir } from "fs/promises";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
var LocalStorageProvider = class {
|
|
25
5
|
rootDir;
|
|
26
6
|
baseUrl;
|
|
7
|
+
/**
|
|
8
|
+
* Create a new LocalStorageProvider.
|
|
9
|
+
*
|
|
10
|
+
* @param rootDir - The root directory for storage.
|
|
11
|
+
* @param baseUrl - The base URL for accessing stored files.
|
|
12
|
+
*/
|
|
27
13
|
constructor(rootDir, baseUrl = "/storage") {
|
|
28
14
|
this.rootDir = rootDir;
|
|
29
15
|
this.baseUrl = baseUrl;
|
|
30
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Store data in a file.
|
|
19
|
+
*
|
|
20
|
+
* @param key - The storage key (path).
|
|
21
|
+
* @param data - The data to store.
|
|
22
|
+
*/
|
|
31
23
|
async put(key, data) {
|
|
32
24
|
const path = join(this.rootDir, key);
|
|
33
25
|
const dir = path.substring(0, path.lastIndexOf("/"));
|
|
@@ -36,6 +28,12 @@ class LocalStorageProvider {
|
|
|
36
28
|
}
|
|
37
29
|
await Bun.write(path, data);
|
|
38
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Retrieve a file.
|
|
33
|
+
*
|
|
34
|
+
* @param key - The storage key.
|
|
35
|
+
* @returns A promise resolving to the file Blob or null if not found.
|
|
36
|
+
*/
|
|
39
37
|
async get(key) {
|
|
40
38
|
const file = Bun.file(join(this.rootDir, key));
|
|
41
39
|
if (!await file.exists()) {
|
|
@@ -43,75 +41,101 @@ class LocalStorageProvider {
|
|
|
43
41
|
}
|
|
44
42
|
return file;
|
|
45
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Delete a file.
|
|
46
|
+
*
|
|
47
|
+
* @param key - The storage key.
|
|
48
|
+
*/
|
|
46
49
|
async delete(key) {
|
|
47
|
-
const fs = await import("
|
|
50
|
+
const fs = await import("fs/promises");
|
|
48
51
|
try {
|
|
49
52
|
await fs.unlink(join(this.rootDir, key));
|
|
50
|
-
} catch {
|
|
53
|
+
} catch {
|
|
54
|
+
}
|
|
51
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the public URL for a file.
|
|
58
|
+
*
|
|
59
|
+
* @param key - The storage key.
|
|
60
|
+
* @returns The public URL string.
|
|
61
|
+
*/
|
|
52
62
|
getUrl(key) {
|
|
53
63
|
return `${this.baseUrl}/${key}`;
|
|
54
64
|
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
class OrbitStorage {
|
|
58
|
-
options;
|
|
65
|
+
};
|
|
66
|
+
var OrbitNebula = class {
|
|
59
67
|
constructor(options) {
|
|
60
68
|
this.options = options;
|
|
61
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Install storage service into PlanetCore.
|
|
72
|
+
*
|
|
73
|
+
* @param core - The PlanetCore instance.
|
|
74
|
+
* @throws {Error} If configuration or provider is missing.
|
|
75
|
+
*/
|
|
62
76
|
install(core) {
|
|
63
77
|
const config = this.options || core.config.get("storage");
|
|
64
78
|
if (!config) {
|
|
65
|
-
throw new Error(
|
|
79
|
+
throw new Error(
|
|
80
|
+
'[OrbitNebula] Configuration is required. Please provide options or set "storage" in core config.'
|
|
81
|
+
);
|
|
66
82
|
}
|
|
67
83
|
const { exposeAs = "storage" } = config;
|
|
68
84
|
const logger = core.logger;
|
|
69
|
-
logger.info(`[
|
|
85
|
+
logger.info(`[OrbitNebula] Initializing Storage (Exposed as: ${exposeAs})`);
|
|
70
86
|
let provider = config.provider;
|
|
71
87
|
if (!provider && config.local) {
|
|
72
|
-
logger.info(`[
|
|
88
|
+
logger.info(`[OrbitNebula] Using LocalStorageProvider at ${config.local.root}`);
|
|
73
89
|
provider = new LocalStorageProvider(config.local.root, config.local.baseUrl);
|
|
74
90
|
}
|
|
75
91
|
if (!provider) {
|
|
76
|
-
throw new Error(
|
|
92
|
+
throw new Error(
|
|
93
|
+
"[OrbitNebula] No provider configured. Please provide a provider instance or local configuration."
|
|
94
|
+
);
|
|
77
95
|
}
|
|
78
96
|
const storageService = {
|
|
79
|
-
...provider,
|
|
80
97
|
put: async (key, data) => {
|
|
81
98
|
const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
|
|
82
99
|
await provider?.put(key, finalData);
|
|
83
100
|
await core.hooks.doAction("storage:uploaded", { key });
|
|
84
|
-
}
|
|
101
|
+
},
|
|
102
|
+
get: (key) => provider?.get(key),
|
|
103
|
+
delete: (key) => provider?.delete(key),
|
|
104
|
+
getUrl: (key) => provider?.getUrl(key)
|
|
85
105
|
};
|
|
86
106
|
core.adapter.use("*", async (c, next) => {
|
|
87
107
|
c.set(exposeAs, storageService);
|
|
88
108
|
await next();
|
|
89
|
-
return;
|
|
109
|
+
return void 0;
|
|
90
110
|
});
|
|
91
111
|
core.hooks.doAction("storage:init", storageService);
|
|
92
112
|
}
|
|
93
|
-
}
|
|
113
|
+
};
|
|
94
114
|
function orbitStorage(core, options) {
|
|
95
|
-
const orbit = new
|
|
115
|
+
const orbit = new OrbitNebula(options);
|
|
96
116
|
orbit.install(core);
|
|
97
117
|
let provider = options.provider;
|
|
98
118
|
if (!provider && options.local) {
|
|
99
119
|
provider = new LocalStorageProvider(options.local.root, options.local.baseUrl);
|
|
100
120
|
}
|
|
101
121
|
if (!provider) {
|
|
102
|
-
throw new Error("[
|
|
122
|
+
throw new Error("[OrbitNebula] No provider configured.");
|
|
103
123
|
}
|
|
104
124
|
return {
|
|
105
|
-
...provider,
|
|
106
125
|
put: async (key, data) => {
|
|
107
126
|
const finalData = await core.hooks.applyFilters("storage:upload", data, { key });
|
|
108
127
|
await provider?.put(key, finalData);
|
|
109
128
|
await core.hooks.doAction("storage:uploaded", { key });
|
|
110
|
-
}
|
|
129
|
+
},
|
|
130
|
+
get: (key) => provider?.get(key),
|
|
131
|
+
delete: (key) => provider?.delete(key),
|
|
132
|
+
getUrl: (key) => provider?.getUrl(key)
|
|
111
133
|
};
|
|
112
134
|
}
|
|
135
|
+
var OrbitStorage = OrbitNebula;
|
|
113
136
|
export {
|
|
114
|
-
|
|
137
|
+
LocalStorageProvider,
|
|
138
|
+
OrbitNebula,
|
|
115
139
|
OrbitStorage,
|
|
116
|
-
|
|
140
|
+
orbitStorage as default
|
|
117
141
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gravito/nebula",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -35,12 +35,13 @@
|
|
|
35
35
|
"author": "Carl Lee <carllee0520@gmail.com>",
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"gravito-core": "1.0.0-beta.
|
|
38
|
+
"gravito-core": "1.0.0-beta.6"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"gravito-core": "1.0.0-beta.4",
|
|
42
|
-
"hono": "^4.11.1",
|
|
43
41
|
"bun-types": "latest",
|
|
42
|
+
"gravito-core": "1.0.0-beta.6",
|
|
43
|
+
"@gravito/photon": "1.0.0-beta.1",
|
|
44
|
+
"tsup": "^8.5.1",
|
|
44
45
|
"typescript": "^5.9.3"
|
|
45
46
|
},
|
|
46
47
|
"homepage": "https://github.com/gravito-framework/gravito#readme",
|