@titanpl/core 2.0.0 → 2.0.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/README.md +33 -0
- package/index.js +299 -35
- package/native/target/release/titan_core.dll +0 -0
- package/package.json +3 -3
- package/titan.json +96 -1
package/README.md
CHANGED
|
@@ -49,6 +49,9 @@ Cryptographic utilities using native Rust implementations.
|
|
|
49
49
|
- `crypto.randomBytes(size: number): string` - Generate random bytes as hex string.
|
|
50
50
|
- `crypto.uuid(): string` - Generate a UUID v4.
|
|
51
51
|
- `crypto.compare(hash: string, target: string): boolean` - Constant-time comparison.
|
|
52
|
+
- `crypto.encrypt(algorithm: string, key: string, plaintext: string): string` - AES-256-GCM Encrypt (Returns Base64).
|
|
53
|
+
- `crypto.decrypt(algorithm: string, key: string, ciphertext: string): string` - AES-256-GCM Decrypt.
|
|
54
|
+
- `crypto.hashKeyed(algo: string, key: string, message: string): string` - HMAC-SHA256/512.
|
|
52
55
|
|
|
53
56
|
**Example:**
|
|
54
57
|
```javascript
|
|
@@ -59,6 +62,36 @@ const valid = t.core.crypto.compare(
|
|
|
59
62
|
);
|
|
60
63
|
```
|
|
61
64
|
|
|
65
|
+
### `buffer` (Buffer Utilities)
|
|
66
|
+
Utilities for binary data manipulation.
|
|
67
|
+
- `buffer.fromBase64(str: string): Uint8Array` - Decode Base64 string.
|
|
68
|
+
- `buffer.toBase64(bytes: Uint8Array|string): string` - Encode to Base64.
|
|
69
|
+
- `buffer.fromHex(str: string): Uint8Array` - Decode Hex string.
|
|
70
|
+
- `buffer.toHex(bytes: Uint8Array|string): string` - Encode to Hex.
|
|
71
|
+
- `buffer.fromUtf8(str: string): Uint8Array` - Encode UTF-8 string to bytes.
|
|
72
|
+
- `buffer.toUtf8(bytes: Uint8Array): string` - Decode bytes to UTF-8 string.
|
|
73
|
+
|
|
74
|
+
### `ls` / `localStorage` (Persistent Storage)
|
|
75
|
+
Key-value storage persisted to disk (via Sled).
|
|
76
|
+
- `ls.get(key: string): string|null` - Get value.
|
|
77
|
+
- `ls.set(key: string, value: string): void` - Set value.
|
|
78
|
+
- `ls.remove(key: string): void` - Remove key.
|
|
79
|
+
- `ls.clear(): void` - Clear all storage.
|
|
80
|
+
- `ls.keys(): string[]` - List all keys.
|
|
81
|
+
|
|
82
|
+
### `session` (Server-side Sessions)
|
|
83
|
+
Session management backed by persistent storage.
|
|
84
|
+
- `session.get(sessionId: string, key: string): string|null` - Get session value.
|
|
85
|
+
- `session.set(sessionId: string, key: string, value: string): void` - Set session value.
|
|
86
|
+
- `session.delete(sessionId: string, key: string): void` - Delete session value.
|
|
87
|
+
- `session.clear(sessionId: string): void` - Clear entire session.
|
|
88
|
+
|
|
89
|
+
### `cookies` (HTTP Cookies)
|
|
90
|
+
Cookie parsing and serialization.
|
|
91
|
+
- `cookies.get(req: object, name: string): string|null` - Parse cookie from request headers.
|
|
92
|
+
- `cookies.set(res: object, name: string, value: string, options: object): void` - Set `Set-Cookie` header on response. Options: `{ httpOnly, secure, sameSite, path, maxAge }`.
|
|
93
|
+
- `cookies.delete(res: object, name: string): void` - Delete cookie (expire).
|
|
94
|
+
|
|
62
95
|
### `os` (Operating System)
|
|
63
96
|
Get system information.
|
|
64
97
|
- `os.platform(): string` - OS platform (e.g., `linux`, `windows`).
|
package/index.js
CHANGED
|
@@ -1,10 +1,118 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
2
|
+
|
|
3
|
+
function local_btoa(input) {
|
|
4
|
+
let str = String(input);
|
|
5
|
+
let output = '';
|
|
6
|
+
|
|
7
|
+
for (let i = 0; i < str.length; i += 3) {
|
|
8
|
+
const char1 = str.charCodeAt(i);
|
|
9
|
+
const char2 = str.charCodeAt(i + 1);
|
|
10
|
+
const char3 = str.charCodeAt(i + 2);
|
|
11
|
+
|
|
12
|
+
const enc1 = char1 >> 2;
|
|
13
|
+
const enc2 = ((char1 & 3) << 4) | (char2 >> 4);
|
|
14
|
+
let enc3 = ((char2 & 15) << 2) | (char3 >> 6);
|
|
15
|
+
let enc4 = char3 & 63;
|
|
16
|
+
|
|
17
|
+
if (isNaN(char2)) {
|
|
18
|
+
enc3 = enc4 = 64;
|
|
19
|
+
} else if (isNaN(char3)) {
|
|
20
|
+
enc4 = 64;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
output += b64chars.charAt(enc1) + b64chars.charAt(enc2);
|
|
24
|
+
output += (enc3 === 64) ? '=' : b64chars.charAt(enc3);
|
|
25
|
+
output += (enc4 === 64) ? '=' : b64chars.charAt(enc4);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return output;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function local_atob(input) {
|
|
32
|
+
// Remove whitespace and padding '='
|
|
33
|
+
let str = String(input).replace(/[\t\n\f\r =]/g, "");
|
|
34
|
+
let output = '';
|
|
35
|
+
|
|
36
|
+
for (let i = 0; i < str.length; i += 4) {
|
|
37
|
+
const c1Str = str.charAt(i);
|
|
38
|
+
const c2Str = str.charAt(i + 1);
|
|
39
|
+
const c3Str = str.charAt(i + 2);
|
|
40
|
+
const c4Str = str.charAt(i + 3);
|
|
41
|
+
|
|
42
|
+
const e1 = b64chars.indexOf(c1Str);
|
|
43
|
+
const e2 = c2Str ? b64chars.indexOf(c2Str) : -1;
|
|
44
|
+
const e3 = c3Str ? b64chars.indexOf(c3Str) : -1;
|
|
45
|
+
const e4 = c4Str ? b64chars.indexOf(c4Str) : -1;
|
|
46
|
+
|
|
47
|
+
// e1 and e2 are required
|
|
48
|
+
if (e1 < 0 || e2 < 0) continue;
|
|
49
|
+
|
|
50
|
+
// Shift and mask to reconstruct bytes
|
|
51
|
+
const c1 = (e1 << 2) | (e2 >> 4);
|
|
52
|
+
output += String.fromCharCode(c1);
|
|
53
|
+
|
|
54
|
+
if (e3 !== -1) {
|
|
55
|
+
const c2 = ((e2 & 15) << 4) | (e3 >> 2);
|
|
56
|
+
output += String.fromCharCode(c2);
|
|
57
|
+
}
|
|
58
|
+
if (e4 !== -1) {
|
|
59
|
+
const c3 = ((e3 & 3) << 6) | e4;
|
|
60
|
+
output += String.fromCharCode(c3);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return output;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function local_utf8_encode(str) {
|
|
68
|
+
let result = [];
|
|
69
|
+
for (let i = 0; i < str.length; i++) {
|
|
70
|
+
let c = str.charCodeAt(i);
|
|
71
|
+
if (c < 0x80) { result.push(c); }
|
|
72
|
+
else if (c < 0x800) {
|
|
73
|
+
result.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
|
|
74
|
+
}
|
|
75
|
+
else if (c < 0xd800 || c >= 0xe000) {
|
|
76
|
+
result.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
i++;
|
|
80
|
+
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
|
|
81
|
+
result.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return new Uint8Array(result);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function local_utf8_decode(bytes) {
|
|
88
|
+
let str = "";
|
|
89
|
+
let i = 0;
|
|
90
|
+
while (i < bytes.length) {
|
|
91
|
+
let c = bytes[i++];
|
|
92
|
+
if (c > 127) {
|
|
93
|
+
if (c > 191 && c < 224) {
|
|
94
|
+
c = ((c & 31) << 6) | (bytes[i++] & 63);
|
|
95
|
+
} else if (c > 223 && c < 240) {
|
|
96
|
+
c = ((c & 15) << 12) | ((bytes[i++] & 63) << 6) | (bytes[i++] & 63);
|
|
97
|
+
} else if (c > 239 && c < 248) {
|
|
98
|
+
c = ((c & 7) << 18) | ((bytes[i++] & 63) << 12) | ((bytes[i++] & 63) << 6) | (bytes[i++] & 63);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (c <= 0xffff) str += String.fromCharCode(c);
|
|
102
|
+
else if (c <= 0x10ffff) {
|
|
103
|
+
c -= 0x10000;
|
|
104
|
+
str += String.fromCharCode(c >> 10 | 0xd800) + String.fromCharCode(c & 0x3ff | 0xdc00);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return str;
|
|
108
|
+
}
|
|
109
|
+
|
|
3
110
|
|
|
4
111
|
// Native bindings are loaded by the runtime into t["@titanpl/core"]
|
|
5
|
-
// We capture them here before defining our high-level API.
|
|
6
112
|
const natives = t["@titanpl/core"] || {};
|
|
7
113
|
|
|
114
|
+
|
|
115
|
+
// Native Function bindings
|
|
8
116
|
const native_fs_read_file = natives.fs_read_file;
|
|
9
117
|
const native_fs_write_file = natives.fs_write_file;
|
|
10
118
|
const native_fs_readdir = natives.fs_readdir;
|
|
@@ -13,45 +121,68 @@ const native_fs_exists = natives.fs_exists;
|
|
|
13
121
|
const native_fs_stat = natives.fs_stat;
|
|
14
122
|
const native_fs_remove = natives.fs_remove;
|
|
15
123
|
const native_path_cwd = natives.path_cwd;
|
|
124
|
+
|
|
16
125
|
const native_crypto_hash = natives.crypto_hash;
|
|
17
126
|
const native_crypto_random_bytes = natives.crypto_random_bytes;
|
|
18
127
|
const native_crypto_uuid = natives.crypto_uuid;
|
|
128
|
+
const native_crypto_encrypt = natives.crypto_encrypt;
|
|
129
|
+
const native_crypto_decrypt = natives.crypto_decrypt;
|
|
130
|
+
const native_crypto_hash_keyed = natives.crypto_hash_keyed;
|
|
131
|
+
const native_crypto_compare = natives.crypto_compare;
|
|
132
|
+
|
|
19
133
|
const native_os_info = natives.os_info;
|
|
20
134
|
const native_net_resolve = natives.net_resolve;
|
|
21
135
|
const native_net_ip = natives.net_ip;
|
|
22
136
|
const native_proc_info = natives.proc_info;
|
|
23
137
|
const native_time_sleep = natives.time_sleep;
|
|
24
138
|
|
|
139
|
+
const native_ls_get = natives.ls_get;
|
|
140
|
+
const native_ls_set = natives.ls_set;
|
|
141
|
+
const native_ls_remove = natives.ls_remove;
|
|
142
|
+
const native_ls_clear = natives.ls_clear;
|
|
143
|
+
const native_ls_keys = natives.ls_keys;
|
|
144
|
+
|
|
145
|
+
const native_session_get = natives.session_get;
|
|
146
|
+
const native_session_set = natives.session_set;
|
|
147
|
+
const native_session_delete = natives.session_delete;
|
|
148
|
+
const native_session_clear = natives.session_clear;
|
|
149
|
+
|
|
25
150
|
// --- FS ---
|
|
151
|
+
/** File System module */
|
|
26
152
|
const fs = {
|
|
153
|
+
/** Reads file content as UTF-8 string */
|
|
27
154
|
readFile: (path) => {
|
|
28
155
|
if (!native_fs_read_file) throw new Error("Native fs_read_file not found");
|
|
29
156
|
const res = native_fs_read_file(path);
|
|
30
|
-
if (res.startsWith("ERROR:")) throw new Error(res);
|
|
157
|
+
if (res && res.startsWith("ERROR:")) throw new Error(res);
|
|
31
158
|
return res;
|
|
32
159
|
},
|
|
160
|
+
/** Writes content to file */
|
|
33
161
|
writeFile: (path, content) => {
|
|
34
162
|
if (!native_fs_write_file) throw new Error("Native fs_write_file not found");
|
|
35
163
|
native_fs_write_file(path, content);
|
|
36
164
|
},
|
|
165
|
+
/** Reads directory contents */
|
|
37
166
|
readdir: (path) => {
|
|
38
167
|
if (!native_fs_readdir) throw new Error("Native fs_readdir not found");
|
|
39
168
|
const res = native_fs_readdir(path);
|
|
40
|
-
// Runtime might return empty string on error or failures, or "[]"
|
|
41
169
|
try {
|
|
42
170
|
return JSON.parse(res);
|
|
43
171
|
} catch (e) {
|
|
44
172
|
return [];
|
|
45
173
|
}
|
|
46
174
|
},
|
|
175
|
+
/** Creates direction recursively */
|
|
47
176
|
mkdir: (path) => {
|
|
48
177
|
if (!native_fs_mkdir) throw new Error("Native fs_mkdir not found");
|
|
49
178
|
native_fs_mkdir(path);
|
|
50
179
|
},
|
|
180
|
+
/** Checks if path exists */
|
|
51
181
|
exists: (path) => {
|
|
52
182
|
if (!native_fs_exists) throw new Error("Native fs_exists not found");
|
|
53
183
|
return native_fs_exists(path);
|
|
54
184
|
},
|
|
185
|
+
/** Returns file stats */
|
|
55
186
|
stat: (path) => {
|
|
56
187
|
if (!native_fs_stat) throw new Error("Native fs_stat not found");
|
|
57
188
|
const res = native_fs_stat(path);
|
|
@@ -61,6 +192,7 @@ const fs = {
|
|
|
61
192
|
return {};
|
|
62
193
|
}
|
|
63
194
|
},
|
|
195
|
+
/** Removes file or directory */
|
|
64
196
|
remove: (path) => {
|
|
65
197
|
if (!native_fs_remove) throw new Error("Native fs_remove not found");
|
|
66
198
|
native_fs_remove(path);
|
|
@@ -68,15 +200,12 @@ const fs = {
|
|
|
68
200
|
};
|
|
69
201
|
|
|
70
202
|
// --- Path ---
|
|
71
|
-
|
|
203
|
+
/** Path manipulation module */
|
|
72
204
|
const path = {
|
|
73
205
|
join: (...args) => {
|
|
74
|
-
// Normalize to forward slashes for internal join logic, or keep native?
|
|
75
|
-
// Let's normalize to / for consistency unless user requests native path separator
|
|
76
206
|
return args
|
|
77
207
|
.map((part, i) => {
|
|
78
208
|
if (!part) return '';
|
|
79
|
-
// Replace backslashes with forward slashes for easier joining
|
|
80
209
|
let p = part.replace(/\\/g, '/');
|
|
81
210
|
if (i === 0) return p.trim().replace(/[\/]*$/g, '');
|
|
82
211
|
return p.trim().replace(/(^[\/]*|[\/]*$)/g, '');
|
|
@@ -90,10 +219,8 @@ const path = {
|
|
|
90
219
|
resolved = path.join(resolved, arg);
|
|
91
220
|
}
|
|
92
221
|
if (!resolved.startsWith('/')) {
|
|
93
|
-
// If windows, check for drive letter C:\ or D:\ or start with \
|
|
94
222
|
const isWindowsAbs = /^[a-zA-Z]:\\/.test(resolved) || resolved.startsWith('\\');
|
|
95
223
|
if (!isWindowsAbs && native_path_cwd) {
|
|
96
|
-
// native_path_cwd returns result of std::env::current_dir()
|
|
97
224
|
const cwd = native_path_cwd();
|
|
98
225
|
if (cwd) {
|
|
99
226
|
resolved = path.join(cwd, resolved);
|
|
@@ -115,17 +242,42 @@ const path = {
|
|
|
115
242
|
};
|
|
116
243
|
|
|
117
244
|
// --- Crypto ---
|
|
245
|
+
/** Cryptography module */
|
|
118
246
|
const crypto = {
|
|
119
247
|
hash: (algo, data) => native_crypto_hash ? native_crypto_hash(algo, data) : "",
|
|
120
248
|
randomBytes: (size) => native_crypto_random_bytes ? native_crypto_random_bytes(size) : "",
|
|
121
249
|
uuid: () => native_crypto_uuid ? native_crypto_uuid() : "",
|
|
122
250
|
base64: {
|
|
123
|
-
encode: (str) =>
|
|
124
|
-
decode: (str) =>
|
|
251
|
+
encode: (str) => local_btoa(str),
|
|
252
|
+
decode: (str) => local_atob(str),
|
|
125
253
|
},
|
|
254
|
+
// Extended API
|
|
255
|
+
/** Encrypts data using AES-256-GCM. Returns Base64 string. */
|
|
256
|
+
encrypt: (algorithm, key, plaintext) => {
|
|
257
|
+
if (!native_crypto_encrypt) throw new Error("Native crypto_encrypt not found");
|
|
258
|
+
const res = native_crypto_encrypt(algorithm, JSON.stringify({ key, plaintext }));
|
|
259
|
+
if (res.startsWith("ERROR:")) throw new Error(res.substring(6));
|
|
260
|
+
return res;
|
|
261
|
+
},
|
|
262
|
+
/** Decrypts data using AES-256-GCM. Returns plaintext string. */
|
|
263
|
+
decrypt: (algorithm, key, ciphertext) => {
|
|
264
|
+
if (!native_crypto_decrypt) throw new Error("Native crypto_decrypt not found");
|
|
265
|
+
const res = native_crypto_decrypt(algorithm, JSON.stringify({ key, ciphertext }));
|
|
266
|
+
if (res.startsWith("ERROR:")) throw new Error(res.substring(6));
|
|
267
|
+
return res;
|
|
268
|
+
},
|
|
269
|
+
/** Computes HMAC-SHA256/512. Returns Hex string. */
|
|
270
|
+
hashKeyed: (algorithm, key, message) => {
|
|
271
|
+
if (!native_crypto_hash_keyed) throw new Error("Native crypto_hash_keyed not found");
|
|
272
|
+
const res = native_crypto_hash_keyed(algorithm, JSON.stringify({ key, message }));
|
|
273
|
+
if (res.startsWith("ERROR:")) throw new Error(res.substring(6));
|
|
274
|
+
return res;
|
|
275
|
+
},
|
|
276
|
+
/** Constant-time string comparison */
|
|
126
277
|
compare: (a, b) => {
|
|
278
|
+
if (native_crypto_compare) return native_crypto_compare(a, b);
|
|
279
|
+
// Fallback insecure
|
|
127
280
|
if (a.length !== b.length) return false;
|
|
128
|
-
// Constant time comparison not guaranteed here in JS easily without specialized tricks
|
|
129
281
|
let mismatch = 0;
|
|
130
282
|
for (let i = 0; i < a.length; ++i) {
|
|
131
283
|
mismatch |= (a.charCodeAt(i) ^ b.charCodeAt(i));
|
|
@@ -134,6 +286,126 @@ const crypto = {
|
|
|
134
286
|
}
|
|
135
287
|
};
|
|
136
288
|
|
|
289
|
+
// --- Buffer ---
|
|
290
|
+
// Helper for hex
|
|
291
|
+
function hexToBytes(hex) {
|
|
292
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
293
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
294
|
+
bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
|
|
295
|
+
}
|
|
296
|
+
return bytes;
|
|
297
|
+
}
|
|
298
|
+
function bytesToHex(bytes) {
|
|
299
|
+
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Buffer utility module */
|
|
303
|
+
const buffer = {
|
|
304
|
+
/** Creates Uint8Array from Base64 string */
|
|
305
|
+
fromBase64: (str) => {
|
|
306
|
+
const binary = local_atob(str);
|
|
307
|
+
const bytes = new Uint8Array(binary.length);
|
|
308
|
+
for (let i = 0; i < binary.length; i++) {
|
|
309
|
+
bytes[i] = binary.charCodeAt(i);
|
|
310
|
+
}
|
|
311
|
+
return bytes;
|
|
312
|
+
},
|
|
313
|
+
/** encoded Uint8Array or String to Base64 string */
|
|
314
|
+
toBase64: (bytes) => {
|
|
315
|
+
let binary = '';
|
|
316
|
+
if (typeof bytes === 'string') {
|
|
317
|
+
return local_btoa(bytes);
|
|
318
|
+
}
|
|
319
|
+
// Uint8Array
|
|
320
|
+
const len = bytes.byteLength;
|
|
321
|
+
for (let i = 0; i < len; i++) {
|
|
322
|
+
binary += String.fromCharCode(bytes[i]);
|
|
323
|
+
}
|
|
324
|
+
return local_btoa(binary);
|
|
325
|
+
},
|
|
326
|
+
/** Creates Uint8Array from Hex string */
|
|
327
|
+
fromHex: (str) => hexToBytes(str),
|
|
328
|
+
/** Encodes bytes to Hex string */
|
|
329
|
+
toHex: (bytes) => {
|
|
330
|
+
if (typeof bytes === 'string') {
|
|
331
|
+
return bytesToHex(local_utf8_encode(bytes));
|
|
332
|
+
}
|
|
333
|
+
return bytesToHex(bytes);
|
|
334
|
+
},
|
|
335
|
+
/** Creates Uint8Array from UTF-8 string */
|
|
336
|
+
fromUtf8: (str) => local_utf8_encode(str),
|
|
337
|
+
/** Decodes bytes to UTF-8 string */
|
|
338
|
+
toUtf8: (bytes) => local_utf8_decode(bytes)
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// --- Local Storage ---
|
|
342
|
+
/** Persistent Local Storage */
|
|
343
|
+
const ls = {
|
|
344
|
+
get: (key) => native_ls_get ? native_ls_get(key) || null : null,
|
|
345
|
+
set: (key, value) => native_ls_set && native_ls_set(key, value),
|
|
346
|
+
remove: (key) => native_ls_remove && native_ls_remove(key),
|
|
347
|
+
clear: () => native_ls_clear && native_ls_clear(),
|
|
348
|
+
keys: () => {
|
|
349
|
+
if (!native_ls_keys) return [];
|
|
350
|
+
try {
|
|
351
|
+
return JSON.parse(native_ls_keys());
|
|
352
|
+
} catch (e) { return []; }
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// --- Sessions ---
|
|
357
|
+
/** Server-side Session Management */
|
|
358
|
+
const session = {
|
|
359
|
+
get: (sessionId, key) => native_session_get ? native_session_get(sessionId, key) || null : null,
|
|
360
|
+
set: (sessionId, key, value) => native_session_set && native_session_set(sessionId, JSON.stringify({ key, value })),
|
|
361
|
+
delete: (sessionId, key) => native_session_delete && native_session_delete(sessionId, key),
|
|
362
|
+
clear: (sessionId) => native_session_clear && native_session_clear(sessionId)
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
// --- Cookies ---
|
|
367
|
+
/** HTTP Cookie Utilities */
|
|
368
|
+
const cookies = {
|
|
369
|
+
/** Parses cookie from request headers */
|
|
370
|
+
get: (req, name) => {
|
|
371
|
+
if (!req || !req.headers) return null;
|
|
372
|
+
const cookieHeader = req.headers.cookie;
|
|
373
|
+
if (!cookieHeader) return null;
|
|
374
|
+
const cookies = cookieHeader.split(';');
|
|
375
|
+
for (let c of cookies) {
|
|
376
|
+
const [k, v] = c.trim().split('=');
|
|
377
|
+
if (k === name) return decodeURIComponent(v);
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
},
|
|
381
|
+
/** Sets Set-Cookie header on response */
|
|
382
|
+
set: (res, name, value, options = {}) => {
|
|
383
|
+
if (!res || !res.setHeader) return;
|
|
384
|
+
let cookie = `${name}=${encodeURIComponent(value)}`;
|
|
385
|
+
if (options.maxAge) cookie += `; Max-Age=${options.maxAge}`;
|
|
386
|
+
if (options.path) cookie += `; Path=${options.path}`;
|
|
387
|
+
if (options.httpOnly) cookie += `; HttpOnly`;
|
|
388
|
+
if (options.secure) cookie += `; Secure`;
|
|
389
|
+
if (options.sameSite) cookie += `; SameSite=${options.sameSite}`;
|
|
390
|
+
|
|
391
|
+
let prev = res.getHeader ? res.getHeader('Set-Cookie') : null;
|
|
392
|
+
if (prev) {
|
|
393
|
+
if (Array.isArray(prev)) {
|
|
394
|
+
prev.push(cookie);
|
|
395
|
+
res.setHeader('Set-Cookie', prev);
|
|
396
|
+
} else {
|
|
397
|
+
res.setHeader('Set-Cookie', [prev, cookie]);
|
|
398
|
+
}
|
|
399
|
+
} else {
|
|
400
|
+
res.setHeader('Set-Cookie', cookie);
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
/** Deletes cookie by setting maxAge=0 */
|
|
404
|
+
delete: (res, name) => {
|
|
405
|
+
cookies.set(res, name, "", { maxAge: 0, path: '/' });
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
137
409
|
// --- OS ---
|
|
138
410
|
const os = {
|
|
139
411
|
platform: () => {
|
|
@@ -156,7 +428,7 @@ const os = {
|
|
|
156
428
|
const info = JSON.parse(native_os_info());
|
|
157
429
|
return info.freeMemory;
|
|
158
430
|
},
|
|
159
|
-
tmpdir: () => '/tmp'
|
|
431
|
+
tmpdir: () => '/tmp'
|
|
160
432
|
};
|
|
161
433
|
|
|
162
434
|
// --- Net ---
|
|
@@ -166,14 +438,10 @@ const net = {
|
|
|
166
438
|
return JSON.parse(native_net_resolve(hostname));
|
|
167
439
|
},
|
|
168
440
|
ip: () => native_net_ip ? native_net_ip() : "127.0.0.1",
|
|
169
|
-
ping: (host) =>
|
|
170
|
-
// Mock ping or simple verify
|
|
171
|
-
return true;
|
|
172
|
-
}
|
|
441
|
+
ping: (host) => true
|
|
173
442
|
};
|
|
174
443
|
|
|
175
444
|
// --- Proc ---
|
|
176
|
-
// Memoize static info if needed, but here we call native
|
|
177
445
|
const proc = {
|
|
178
446
|
pid: () => {
|
|
179
447
|
if (!native_proc_info) return 0;
|
|
@@ -185,27 +453,19 @@ const proc = {
|
|
|
185
453
|
const info = JSON.parse(native_proc_info());
|
|
186
454
|
return info.uptime;
|
|
187
455
|
},
|
|
188
|
-
memory: () => {
|
|
189
|
-
// Optional: return full memory usage if possible
|
|
190
|
-
return {};
|
|
191
|
-
}
|
|
456
|
+
memory: () => ({})
|
|
192
457
|
};
|
|
193
458
|
|
|
194
459
|
// --- Time ---
|
|
195
460
|
const time = {
|
|
196
461
|
sleep: (ms) => {
|
|
197
|
-
if (native_time_sleep)
|
|
198
|
-
native_time_sleep(ms);
|
|
199
|
-
} else {
|
|
200
|
-
console.log("[TitanCore] Warn: native_time_sleep missing");
|
|
201
|
-
}
|
|
462
|
+
if (native_time_sleep) native_time_sleep(ms);
|
|
202
463
|
},
|
|
203
464
|
now: () => Date.now(),
|
|
204
465
|
timestamp: () => new Date().toISOString()
|
|
205
466
|
};
|
|
206
467
|
|
|
207
468
|
// --- URL ---
|
|
208
|
-
// Simple URLSearchParams polyfill for V8 runtime
|
|
209
469
|
class TitanURLSearchParams {
|
|
210
470
|
constructor(init = '') {
|
|
211
471
|
this._params = {};
|
|
@@ -235,11 +495,9 @@ class TitanURLSearchParams {
|
|
|
235
495
|
|
|
236
496
|
const url = {
|
|
237
497
|
parse: (str) => {
|
|
238
|
-
// Basic URL parsing if native URL is available
|
|
239
498
|
if (typeof URL !== 'undefined') {
|
|
240
499
|
return new URL(str);
|
|
241
500
|
}
|
|
242
|
-
// Simple fallback parser
|
|
243
501
|
const match = str.match(/^(https?:)\/\/([^/:]+)(?::(\d+))?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/);
|
|
244
502
|
if (!match) throw new Error('Invalid URL');
|
|
245
503
|
return {
|
|
@@ -255,7 +513,6 @@ const url = {
|
|
|
255
513
|
SearchParams: TitanURLSearchParams
|
|
256
514
|
};
|
|
257
515
|
|
|
258
|
-
|
|
259
516
|
// Create the main core export object (following titan-valid pattern)
|
|
260
517
|
const core = {
|
|
261
518
|
fs,
|
|
@@ -265,10 +522,10 @@ const core = {
|
|
|
265
522
|
net,
|
|
266
523
|
proc,
|
|
267
524
|
time,
|
|
268
|
-
url
|
|
525
|
+
url,
|
|
526
|
+
buffer, // t.core.buffer
|
|
269
527
|
};
|
|
270
528
|
|
|
271
|
-
|
|
272
529
|
t.fs = fs;
|
|
273
530
|
t.path = path;
|
|
274
531
|
t.crypto = crypto;
|
|
@@ -278,6 +535,13 @@ t.proc = proc;
|
|
|
278
535
|
t.time = time;
|
|
279
536
|
t.url = url;
|
|
280
537
|
|
|
538
|
+
// New Global Modules
|
|
539
|
+
t.buffer = buffer;
|
|
540
|
+
t.ls = ls;
|
|
541
|
+
t.localStorage = ls;
|
|
542
|
+
t.session = session;
|
|
543
|
+
t.cookies = cookies;
|
|
544
|
+
|
|
281
545
|
// Attach core as unified namespace (main access point)
|
|
282
546
|
t.core = core;
|
|
283
547
|
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@titanpl/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "The official Core Standard Library for Titan Planet - provides fs, path, crypto, os, net, proc, time, and url modules",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "globals.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build:native": "cd native && cargo build --release",
|
|
9
|
-
"test": "
|
|
9
|
+
"test": "titan run ext"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
12
|
"titan",
|
|
@@ -41,4 +41,4 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@titanpl/valid": "^1.1.1"
|
|
43
43
|
}
|
|
44
|
-
}
|
|
44
|
+
}
|
package/titan.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@titanpl/core",
|
|
3
3
|
"description": "The official Core Standard Library for Titan Planet",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.1",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"native": {
|
|
7
7
|
"path": "native/target/release/titan_core.dll",
|
|
@@ -109,6 +109,101 @@
|
|
|
109
109
|
"f64"
|
|
110
110
|
],
|
|
111
111
|
"result": "void"
|
|
112
|
+
},
|
|
113
|
+
"crypto_encrypt": {
|
|
114
|
+
"symbol": "crypto_encrypt",
|
|
115
|
+
"parameters": [
|
|
116
|
+
"string",
|
|
117
|
+
"string"
|
|
118
|
+
],
|
|
119
|
+
"result": "string"
|
|
120
|
+
},
|
|
121
|
+
"crypto_decrypt": {
|
|
122
|
+
"symbol": "crypto_decrypt",
|
|
123
|
+
"parameters": [
|
|
124
|
+
"string",
|
|
125
|
+
"string"
|
|
126
|
+
],
|
|
127
|
+
"result": "string"
|
|
128
|
+
},
|
|
129
|
+
"crypto_hash_keyed": {
|
|
130
|
+
"symbol": "crypto_hash_keyed",
|
|
131
|
+
"parameters": [
|
|
132
|
+
"string",
|
|
133
|
+
"string"
|
|
134
|
+
],
|
|
135
|
+
"result": "string"
|
|
136
|
+
},
|
|
137
|
+
"crypto_compare": {
|
|
138
|
+
"symbol": "crypto_compare",
|
|
139
|
+
"parameters": [
|
|
140
|
+
"string",
|
|
141
|
+
"string"
|
|
142
|
+
],
|
|
143
|
+
"result": "bool"
|
|
144
|
+
},
|
|
145
|
+
"ls_get": {
|
|
146
|
+
"symbol": "ls_get",
|
|
147
|
+
"parameters": [
|
|
148
|
+
"string"
|
|
149
|
+
],
|
|
150
|
+
"result": "string"
|
|
151
|
+
},
|
|
152
|
+
"ls_set": {
|
|
153
|
+
"symbol": "ls_set",
|
|
154
|
+
"parameters": [
|
|
155
|
+
"string",
|
|
156
|
+
"string"
|
|
157
|
+
],
|
|
158
|
+
"result": "void"
|
|
159
|
+
},
|
|
160
|
+
"ls_remove": {
|
|
161
|
+
"symbol": "ls_remove",
|
|
162
|
+
"parameters": [
|
|
163
|
+
"string"
|
|
164
|
+
],
|
|
165
|
+
"result": "void"
|
|
166
|
+
},
|
|
167
|
+
"ls_clear": {
|
|
168
|
+
"symbol": "ls_clear",
|
|
169
|
+
"parameters": [],
|
|
170
|
+
"result": "void"
|
|
171
|
+
},
|
|
172
|
+
"ls_keys": {
|
|
173
|
+
"symbol": "ls_keys",
|
|
174
|
+
"parameters": [],
|
|
175
|
+
"result": "string"
|
|
176
|
+
},
|
|
177
|
+
"session_get": {
|
|
178
|
+
"symbol": "session_get",
|
|
179
|
+
"parameters": [
|
|
180
|
+
"string",
|
|
181
|
+
"string"
|
|
182
|
+
],
|
|
183
|
+
"result": "string"
|
|
184
|
+
},
|
|
185
|
+
"session_set": {
|
|
186
|
+
"symbol": "session_set",
|
|
187
|
+
"parameters": [
|
|
188
|
+
"string",
|
|
189
|
+
"string"
|
|
190
|
+
],
|
|
191
|
+
"result": "void"
|
|
192
|
+
},
|
|
193
|
+
"session_delete": {
|
|
194
|
+
"symbol": "session_delete",
|
|
195
|
+
"parameters": [
|
|
196
|
+
"string",
|
|
197
|
+
"string"
|
|
198
|
+
],
|
|
199
|
+
"result": "void"
|
|
200
|
+
},
|
|
201
|
+
"session_clear": {
|
|
202
|
+
"symbol": "session_clear",
|
|
203
|
+
"parameters": [
|
|
204
|
+
"string"
|
|
205
|
+
],
|
|
206
|
+
"result": "void"
|
|
112
207
|
}
|
|
113
208
|
}
|
|
114
209
|
}
|