@gravito/nebula 1.0.0-beta.1 → 1.0.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
@@ -13,7 +13,7 @@ bun add @gravito/nebula
13
13
  ## 🚀 Usage
14
14
 
15
15
  ```typescript
16
- import { PlanetCore } from 'gravito-core';
16
+ import { PlanetCore } from '@gravito/core';
17
17
  import orbitStorage from '@gravito/nebula';
18
18
 
19
19
  const core = new PlanetCore();
package/README.zh-TW.md CHANGED
@@ -11,7 +11,7 @@ bun add @gravito/nebula
11
11
  ## 快速開始
12
12
 
13
13
  ```typescript
14
- import { PlanetCore } from 'gravito-core'
14
+ import { PlanetCore } from '@gravito/core'
15
15
  import orbitStorage from '@gravito/nebula'
16
16
 
17
17
  const core = new PlanetCore()
package/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
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
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -38,9 +28,11 @@ __export(index_exports, {
38
28
  module.exports = __toCommonJS(index_exports);
39
29
  var import_promises = require("fs/promises");
40
30
  var import_node_path = require("path");
31
+ var import_core = require("@gravito/core");
41
32
  var LocalStorageProvider = class {
42
33
  rootDir;
43
34
  baseUrl;
35
+ runtime = (0, import_core.getRuntimeAdapter)();
44
36
  /**
45
37
  * Create a new LocalStorageProvider.
46
38
  *
@@ -58,12 +50,12 @@ var LocalStorageProvider = class {
58
50
  * @param data - The data to store.
59
51
  */
60
52
  async put(key, data) {
61
- const path = (0, import_node_path.join)(this.rootDir, key);
53
+ const path = this.resolveKeyPath(key);
62
54
  const dir = path.substring(0, path.lastIndexOf("/"));
63
55
  if (dir && dir !== this.rootDir) {
64
56
  await (0, import_promises.mkdir)(dir, { recursive: true });
65
57
  }
66
- await Bun.write(path, data);
58
+ await this.runtime.writeFile(path, data);
67
59
  }
68
60
  /**
69
61
  * Retrieve a file.
@@ -72,11 +64,11 @@ var LocalStorageProvider = class {
72
64
  * @returns A promise resolving to the file Blob or null if not found.
73
65
  */
74
66
  async get(key) {
75
- const file = Bun.file((0, import_node_path.join)(this.rootDir, key));
76
- if (!await file.exists()) {
67
+ const path = this.resolveKeyPath(key);
68
+ if (!await this.runtime.exists(path)) {
77
69
  return null;
78
70
  }
79
- return file;
71
+ return await this.runtime.readFileAsBlob(path);
80
72
  }
81
73
  /**
82
74
  * Delete a file.
@@ -84,11 +76,7 @@ var LocalStorageProvider = class {
84
76
  * @param key - The storage key.
85
77
  */
86
78
  async delete(key) {
87
- const fs = await import("fs/promises");
88
- try {
89
- await fs.unlink((0, import_node_path.join)(this.rootDir, key));
90
- } catch {
91
- }
79
+ await this.runtime.deleteFile(this.resolveKeyPath(key));
92
80
  }
93
81
  /**
94
82
  * Get the public URL for a file.
@@ -97,7 +85,28 @@ var LocalStorageProvider = class {
97
85
  * @returns The public URL string.
98
86
  */
99
87
  getUrl(key) {
100
- return `${this.baseUrl}/${key}`;
88
+ const safeKey = this.normalizeKey(key);
89
+ return `${this.baseUrl}/${safeKey}`;
90
+ }
91
+ normalizeKey(key) {
92
+ if (!key || key.includes("\0")) {
93
+ throw new Error("Invalid storage key.");
94
+ }
95
+ const normalized = (0, import_node_path.normalize)(key).replace(/^[/\\]+/, "");
96
+ if (normalized === "." || normalized === ".." || normalized.startsWith(`..${import_node_path.sep}`) || (0, import_node_path.isAbsolute)(normalized)) {
97
+ throw new Error("Invalid storage key.");
98
+ }
99
+ return normalized.replace(/\\/g, "/");
100
+ }
101
+ resolveKeyPath(key) {
102
+ const normalized = this.normalizeKey(key);
103
+ const root = (0, import_node_path.resolve)(this.rootDir);
104
+ const resolved = (0, import_node_path.resolve)(root, normalized);
105
+ const rootPrefix = root.endsWith(import_node_path.sep) ? root : `${root}${import_node_path.sep}`;
106
+ if (!resolved.startsWith(rootPrefix) && resolved !== root) {
107
+ throw new Error("Invalid storage key.");
108
+ }
109
+ return resolved;
101
110
  }
102
111
  };
103
112
  var OrbitNebula = class {
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { PlanetCore, GravitoOrbit } from 'gravito-core';
1
+ import { PlanetCore, GravitoOrbit } from '@gravito/core';
2
2
 
3
3
  interface StorageProvider {
4
4
  put(key: string, data: Blob | Buffer | string): Promise<void>;
@@ -12,6 +12,7 @@ interface StorageProvider {
12
12
  declare class LocalStorageProvider implements StorageProvider {
13
13
  private rootDir;
14
14
  private baseUrl;
15
+ private runtime;
15
16
  /**
16
17
  * Create a new LocalStorageProvider.
17
18
  *
@@ -46,6 +47,8 @@ declare class LocalStorageProvider implements StorageProvider {
46
47
  * @returns The public URL string.
47
48
  */
48
49
  getUrl(key: string): string;
50
+ private normalizeKey;
51
+ private resolveKeyPath;
49
52
  }
50
53
  interface OrbitNebulaOptions {
51
54
  provider?: StorageProvider;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PlanetCore, GravitoOrbit } from 'gravito-core';
1
+ import { PlanetCore, GravitoOrbit } from '@gravito/core';
2
2
 
3
3
  interface StorageProvider {
4
4
  put(key: string, data: Blob | Buffer | string): Promise<void>;
@@ -12,6 +12,7 @@ interface StorageProvider {
12
12
  declare class LocalStorageProvider implements StorageProvider {
13
13
  private rootDir;
14
14
  private baseUrl;
15
+ private runtime;
15
16
  /**
16
17
  * Create a new LocalStorageProvider.
17
18
  *
@@ -46,6 +47,8 @@ declare class LocalStorageProvider implements StorageProvider {
46
47
  * @returns The public URL string.
47
48
  */
48
49
  getUrl(key: string): string;
50
+ private normalizeKey;
51
+ private resolveKeyPath;
49
52
  }
50
53
  interface OrbitNebulaOptions {
51
54
  provider?: StorageProvider;
package/dist/index.js CHANGED
@@ -1,9 +1,13 @@
1
1
  // src/index.ts
2
2
  import { mkdir } from "fs/promises";
3
- import { join } from "path";
3
+ import { isAbsolute, normalize, resolve, sep } from "path";
4
+ import {
5
+ getRuntimeAdapter
6
+ } from "@gravito/core";
4
7
  var LocalStorageProvider = class {
5
8
  rootDir;
6
9
  baseUrl;
10
+ runtime = getRuntimeAdapter();
7
11
  /**
8
12
  * Create a new LocalStorageProvider.
9
13
  *
@@ -21,12 +25,12 @@ var LocalStorageProvider = class {
21
25
  * @param data - The data to store.
22
26
  */
23
27
  async put(key, data) {
24
- const path = join(this.rootDir, key);
28
+ const path = this.resolveKeyPath(key);
25
29
  const dir = path.substring(0, path.lastIndexOf("/"));
26
30
  if (dir && dir !== this.rootDir) {
27
31
  await mkdir(dir, { recursive: true });
28
32
  }
29
- await Bun.write(path, data);
33
+ await this.runtime.writeFile(path, data);
30
34
  }
31
35
  /**
32
36
  * Retrieve a file.
@@ -35,11 +39,11 @@ var LocalStorageProvider = class {
35
39
  * @returns A promise resolving to the file Blob or null if not found.
36
40
  */
37
41
  async get(key) {
38
- const file = Bun.file(join(this.rootDir, key));
39
- if (!await file.exists()) {
42
+ const path = this.resolveKeyPath(key);
43
+ if (!await this.runtime.exists(path)) {
40
44
  return null;
41
45
  }
42
- return file;
46
+ return await this.runtime.readFileAsBlob(path);
43
47
  }
44
48
  /**
45
49
  * Delete a file.
@@ -47,11 +51,7 @@ var LocalStorageProvider = class {
47
51
  * @param key - The storage key.
48
52
  */
49
53
  async delete(key) {
50
- const fs = await import("fs/promises");
51
- try {
52
- await fs.unlink(join(this.rootDir, key));
53
- } catch {
54
- }
54
+ await this.runtime.deleteFile(this.resolveKeyPath(key));
55
55
  }
56
56
  /**
57
57
  * Get the public URL for a file.
@@ -60,7 +60,28 @@ var LocalStorageProvider = class {
60
60
  * @returns The public URL string.
61
61
  */
62
62
  getUrl(key) {
63
- return `${this.baseUrl}/${key}`;
63
+ const safeKey = this.normalizeKey(key);
64
+ return `${this.baseUrl}/${safeKey}`;
65
+ }
66
+ normalizeKey(key) {
67
+ if (!key || key.includes("\0")) {
68
+ throw new Error("Invalid storage key.");
69
+ }
70
+ const normalized = normalize(key).replace(/^[/\\]+/, "");
71
+ if (normalized === "." || normalized === ".." || normalized.startsWith(`..${sep}`) || isAbsolute(normalized)) {
72
+ throw new Error("Invalid storage key.");
73
+ }
74
+ return normalized.replace(/\\/g, "/");
75
+ }
76
+ resolveKeyPath(key) {
77
+ const normalized = this.normalizeKey(key);
78
+ const root = resolve(this.rootDir);
79
+ const resolved = resolve(root, normalized);
80
+ const rootPrefix = root.endsWith(sep) ? root : `${root}${sep}`;
81
+ if (!resolved.startsWith(rootPrefix) && resolved !== root) {
82
+ throw new Error("Invalid storage key.");
83
+ }
84
+ return resolved;
64
85
  }
65
86
  };
66
87
  var OrbitNebula = class {
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@gravito/nebula",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
7
  "description": "Standard Storage Orbit for Galaxy Architecture",
8
- "module": "./dist/index.mjs",
9
8
  "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
10
  "type": "module",
11
11
  "types": "./dist/index.d.ts",
12
12
  "exports": {
13
13
  ".": {
14
14
  "types": "./dist/index.d.ts",
15
- "import": "./dist/index.mjs",
15
+ "import": "./dist/index.js",
16
16
  "require": "./dist/index.cjs"
17
17
  }
18
18
  },
@@ -24,7 +24,9 @@
24
24
  "scripts": {
25
25
  "build": "bun run build.ts",
26
26
  "test": "bun test",
27
- "typecheck": "tsc --noEmit"
27
+ "typecheck": "bun tsc -p tsconfig.json --noEmit --skipLibCheck",
28
+ "test:coverage": "bun test --coverage --coverage-threshold=80",
29
+ "test:ci": "bun test --coverage --coverage-threshold=80"
28
30
  },
29
31
  "keywords": [
30
32
  "gravito",
@@ -35,12 +37,12 @@
35
37
  "author": "Carl Lee <carllee0520@gmail.com>",
36
38
  "license": "MIT",
37
39
  "peerDependencies": {
38
- "gravito-core": "1.0.0-beta.6"
40
+ "@gravito/core": "workspace:*"
39
41
  },
40
42
  "devDependencies": {
41
43
  "bun-types": "latest",
42
- "gravito-core": "1.0.0-beta.6",
43
- "@gravito/photon": "1.0.0-beta.1",
44
+ "@gravito/core": "workspace:*",
45
+ "@gravito/photon": "workspace:*",
44
46
  "tsup": "^8.5.1",
45
47
  "typescript": "^5.9.3"
46
48
  },
@@ -50,4 +52,4 @@
50
52
  "url": "git+https://github.com/gravito-framework/gravito.git",
51
53
  "directory": "packages/nebula"
52
54
  }
53
- }
55
+ }