@bandeira-tech/b3nd-web 0.2.4 → 0.2.5

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.
@@ -0,0 +1,25 @@
1
+ import { S as Schema, N as NodeProtocolInterface, g as WriteResult, R as ReadResult, b as ListOptions, c as ListResult, H as HealthStatus, D as DeleteResult } from '../../types-oQCx3U-_.js';
2
+
3
+ type MemoryClientStorageNode<T> = {
4
+ value?: T;
5
+ children?: Map<string, MemoryClientStorageNode<unknown>>;
6
+ };
7
+ type MemoryClientStorage = Map<string, MemoryClientStorageNode<unknown>>;
8
+ interface MemoryClientConfig {
9
+ schema: Schema;
10
+ storage?: MemoryClientStorage;
11
+ }
12
+ declare class MemoryClient implements NodeProtocolInterface {
13
+ protected storage: MemoryClientStorage;
14
+ protected schema: Schema;
15
+ constructor(config: MemoryClientConfig);
16
+ write<T = unknown>(uri: string, payload: T): Promise<WriteResult<T>>;
17
+ read<T>(uri: string): Promise<ReadResult<T>>;
18
+ list(uri: string, options?: ListOptions): Promise<ListResult>;
19
+ health(): Promise<HealthStatus>;
20
+ getSchema(): Promise<string[]>;
21
+ cleanup(): Promise<void>;
22
+ delete(uri: string): Promise<DeleteResult>;
23
+ }
24
+
25
+ export { MemoryClient, type MemoryClientConfig };
@@ -0,0 +1,203 @@
1
+ import "../../chunk-MLKGABMK.js";
2
+
3
+ // clients/memory/mod.ts
4
+ function target(uri, schema, storage) {
5
+ const url = URL.parse(uri);
6
+ const program = `${url.protocol}//${url.hostname}`;
7
+ if (!schema[program]) {
8
+ return { success: false, error: "Program not found" };
9
+ }
10
+ const node = storage.get(program);
11
+ if (!node) {
12
+ return { success: false, error: "Storage not initialized for program" };
13
+ }
14
+ const parts = url.pathname.substring(1).split("/");
15
+ return {
16
+ success: true,
17
+ program,
18
+ path: url.pathname,
19
+ node,
20
+ parts
21
+ };
22
+ }
23
+ var MemoryClient = class {
24
+ constructor(config) {
25
+ this.schema = config.schema;
26
+ this.storage = config.storage || /* @__PURE__ */ new Map();
27
+ this.cleanup();
28
+ }
29
+ async write(uri, payload) {
30
+ const result = target(uri, this.schema, this.storage);
31
+ if (!result.success) {
32
+ return result;
33
+ }
34
+ const { program, node, parts } = result;
35
+ const validator = this.schema[program];
36
+ const validation = await validator({ uri, value: payload, read: this.read.bind(this) });
37
+ if (!validation.valid) {
38
+ return {
39
+ success: false,
40
+ error: validation.error || "Validation failed"
41
+ };
42
+ }
43
+ const record = {
44
+ ts: Date.now(),
45
+ data: payload
46
+ };
47
+ let prev = node;
48
+ parts.filter(Boolean).forEach((ns) => {
49
+ if (!prev.children) prev.children = /* @__PURE__ */ new Map();
50
+ if (!prev.children.get(ns)) {
51
+ const newnode = {};
52
+ prev.children.set(ns, newnode);
53
+ prev = newnode;
54
+ } else {
55
+ prev = prev.children.get(ns);
56
+ }
57
+ });
58
+ prev.value = record;
59
+ return {
60
+ success: true,
61
+ record
62
+ };
63
+ }
64
+ read(uri) {
65
+ const result = target(uri, this.schema, this.storage);
66
+ if (!result.success) {
67
+ return Promise.resolve(result);
68
+ }
69
+ const { node, parts } = result;
70
+ let current = node;
71
+ for (const part of parts.filter(Boolean)) {
72
+ current = current?.children?.get(part);
73
+ if (!current) {
74
+ return Promise.resolve({
75
+ success: false,
76
+ error: `Path not found: ${part}`
77
+ });
78
+ }
79
+ }
80
+ if (!current.value) {
81
+ return Promise.resolve({
82
+ success: false,
83
+ error: "Not found"
84
+ });
85
+ }
86
+ return Promise.resolve({
87
+ success: true,
88
+ record: current.value
89
+ });
90
+ }
91
+ list(uri, options) {
92
+ const result = target(uri, this.schema, this.storage);
93
+ if (!result.success) {
94
+ return Promise.resolve(result);
95
+ }
96
+ const { node, parts, program, path } = result;
97
+ let current = node;
98
+ const filteredParts = parts.filter(Boolean);
99
+ for (const part of filteredParts) {
100
+ current = current?.children?.get(part);
101
+ if (!current) {
102
+ return Promise.resolve({
103
+ success: false,
104
+ error: `Path not found: ${part}`
105
+ });
106
+ }
107
+ }
108
+ if (!current.children?.size) {
109
+ return Promise.resolve({
110
+ success: true,
111
+ data: [],
112
+ pagination: {
113
+ page: options?.page ?? 1,
114
+ limit: options?.limit ?? 50,
115
+ total: 0
116
+ }
117
+ });
118
+ }
119
+ let items = Array.from(current.children.entries()).map(([key, child]) => ({
120
+ uri: path.endsWith("/") ? `${program}://${path}${key}` : `${program}://${path}/${key}`,
121
+ type: child.value ? "file" : "directory"
122
+ }));
123
+ if (options?.pattern) {
124
+ const regex = new RegExp(options.pattern);
125
+ items = items.filter((item) => regex.test(item.uri));
126
+ }
127
+ if (options?.sortBy === "name") {
128
+ items.sort((a, b) => a.uri.localeCompare(b.uri));
129
+ } else if (options?.sortBy === "timestamp") {
130
+ items.sort((a, b) => {
131
+ const aTs = current.children?.get(a.uri.split("/").pop())?.value?.ts ?? 0;
132
+ const bTs = current.children?.get(b.uri.split("/").pop())?.value?.ts ?? 0;
133
+ return aTs - bTs;
134
+ });
135
+ }
136
+ if (options?.sortOrder === "desc") {
137
+ items.reverse();
138
+ }
139
+ const page = options?.page ?? 1;
140
+ const limit = options?.limit ?? 50;
141
+ const offset = (page - 1) * limit;
142
+ const paginated = items.slice(offset, offset + limit);
143
+ return Promise.resolve({
144
+ success: true,
145
+ data: paginated,
146
+ pagination: { page, limit, total: items.length }
147
+ });
148
+ }
149
+ health() {
150
+ return Promise.resolve({
151
+ status: "healthy"
152
+ });
153
+ }
154
+ getSchema() {
155
+ return Promise.resolve(Object.keys(this.schema));
156
+ }
157
+ cleanup() {
158
+ Object.keys(this.schema).forEach((program) => {
159
+ if (!this.storage.get(program)) {
160
+ this.storage.set(program, { children: /* @__PURE__ */ new Map() });
161
+ }
162
+ });
163
+ return Promise.resolve();
164
+ }
165
+ delete(uri) {
166
+ const result = target(uri, this.schema, this.storage);
167
+ if (!result.success) {
168
+ return Promise.resolve(result);
169
+ }
170
+ const { node, parts } = result;
171
+ const filteredParts = parts.filter(Boolean);
172
+ if (filteredParts.length === 0) {
173
+ return Promise.resolve({
174
+ success: false,
175
+ error: "Cannot delete root path"
176
+ });
177
+ }
178
+ let current = node;
179
+ const lastPart = filteredParts.pop();
180
+ for (const part of filteredParts) {
181
+ current = current?.children?.get(part);
182
+ if (!current) {
183
+ return Promise.resolve({
184
+ success: false,
185
+ error: `Path not found: ${part}`
186
+ });
187
+ }
188
+ }
189
+ if (!current.children?.has(lastPart)) {
190
+ return Promise.resolve({
191
+ success: false,
192
+ error: `Item not found: ${lastPart}`
193
+ });
194
+ }
195
+ current.children.delete(lastPart);
196
+ return Promise.resolve({
197
+ success: true
198
+ });
199
+ }
200
+ };
201
+ export {
202
+ MemoryClient
203
+ };
@@ -1,3 +1,6 @@
1
+ import {
2
+ WebSocketClient
3
+ } from "../chunk-T43IWAQK.js";
1
4
  import {
2
5
  AppsClient
3
6
  } from "../chunk-VAZUCGED.js";
@@ -13,9 +16,6 @@ import {
13
16
  import {
14
17
  LocalStorageClient
15
18
  } from "../chunk-7U5JDFQW.js";
16
- import {
17
- WebSocketClient
18
- } from "../chunk-T43IWAQK.js";
19
19
  import "../chunk-MLKGABMK.js";
20
20
  export {
21
21
  AppsClient,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bandeira-tech/b3nd-web",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Browser-focused B3nd SDK bundle",
5
5
  "type": "module",
6
6
  "main": "./dist/src/mod.web.js",
@@ -27,6 +27,10 @@
27
27
  "import": "./dist/clients/websocket/mod.js",
28
28
  "types": "./dist/clients/websocket/mod.d.ts"
29
29
  },
30
+ "./clients/memory": {
31
+ "import": "./dist/clients/memory/mod.js",
32
+ "types": "./dist/clients/memory/mod.d.ts"
33
+ },
30
34
  "./wallet": {
31
35
  "import": "./dist/wallet/mod.js",
32
36
  "types": "./dist/wallet/mod.d.ts"
@@ -48,7 +52,7 @@
48
52
  "dist"
49
53
  ],
50
54
  "scripts": {
51
- "build": "tsup src/mod.web.ts wallet/mod.ts apps/mod.ts encrypt/mod.ts clients/http/mod.ts clients/local-storage/mod.ts clients/websocket/mod.ts wallet-server/mod.ts wallet-server/adapters/browser.ts --dts --format esm --out-dir dist --clean --tsconfig tsconfig.web.json",
55
+ "build": "tsup src/mod.web.ts wallet/mod.ts apps/mod.ts encrypt/mod.ts clients/http/mod.ts clients/local-storage/mod.ts clients/websocket/mod.ts clients/memory/mod.ts wallet-server/mod.ts wallet-server/adapters/browser.ts --dts --format esm --out-dir dist --clean --tsconfig tsconfig.web.json",
52
56
  "clean": "rm -rf dist",
53
57
  "lint": "deno lint src/",
54
58
  "format": "deno fmt src/"