@resourcexjs/core 0.4.0 → 0.7.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/dist/index.d.ts CHANGED
@@ -1,349 +1,213 @@
1
1
  /**
2
- * ResourceX Error Types
3
- */
4
- /**
5
- * Base error class for all ResourceX errors
2
+ * ResourceX Error hierarchy
6
3
  */
7
4
  declare class ResourceXError extends Error {
8
5
  constructor(message: string, options?: ErrorOptions);
9
6
  }
10
- /**
11
- * Error thrown when ARP URL parsing fails
12
- */
13
- declare class ParseError extends ResourceXError {
14
- readonly url?: string;
15
- constructor(message: string, url?: string);
7
+ declare class LocatorError extends ResourceXError {
8
+ readonly locator?: string;
9
+ constructor(message: string, locator?: string);
16
10
  }
17
- /**
18
- * Error thrown when transport layer fails
19
- */
20
- declare class TransportError extends ResourceXError {
21
- readonly transport?: string;
22
- constructor(message: string, transport?: string, options?: ErrorOptions);
11
+ declare class ManifestError extends ResourceXError {
12
+ constructor(message: string);
23
13
  }
24
- /**
25
- * Error thrown when semantic layer fails
26
- */
27
- declare class SemanticError extends ResourceXError {
28
- readonly semantic?: string;
29
- constructor(message: string, semantic?: string, options?: ErrorOptions);
14
+ declare class ContentError extends ResourceXError {
15
+ constructor(message: string);
30
16
  }
31
- interface ParsedARP {
32
- semantic: string;
33
- transport: string;
34
- location: string;
17
+ declare class ResourceTypeError extends ResourceXError {
18
+ constructor(message: string);
35
19
  }
36
20
  /**
37
- * Parse an ARP URL into its components
21
+ * RXL - ResourceX Locator
38
22
  *
39
- * @example
40
- * parseARP("arp:text:https://example.com/file.txt")
41
- * // { semantic: "text", transport: "https", location: "example.com/file.txt" }
42
- */
43
- declare function parseARP(url: string): ParsedARP;
44
- /**
45
- * Transport Handler Interface
46
- * Responsible for I/O primitives - read, write, list, exists, delete
47
- * Transport only handles WHERE and HOW to access bytes, not WHAT they mean
48
- */
49
- /**
50
- * Transport capabilities declaration
23
+ * Format: [domain/path/]name[.type][@version]
51
24
  */
52
- interface TransportCapabilities {
53
- readonly canRead: boolean;
54
- readonly canWrite: boolean;
55
- readonly canList: boolean;
56
- readonly canDelete: boolean;
57
- readonly canStat: boolean;
25
+ interface RXL {
26
+ readonly domain?: string;
27
+ readonly path?: string;
28
+ readonly name: string;
29
+ readonly type?: string;
30
+ readonly version?: string;
31
+ toString(): string;
58
32
  }
59
33
  /**
60
- * Resource stat information
34
+ * Parse a resource locator string into RXL object.
35
+ *
36
+ * Format: [domain/path/]name[.type][@version]
61
37
  */
62
- interface ResourceStat {
63
- size: number;
64
- modifiedAt?: Date;
65
- isDirectory?: boolean;
66
- }
38
+ declare function parseRXL(locator: string): RXL;
67
39
  /**
68
- * Transport Handler - provides I/O primitives
40
+ * RXM - ResourceX Manifest
69
41
  */
70
- interface TransportHandler {
71
- /**
72
- * Transport name (e.g., "https", "file", "s3")
73
- */
74
- readonly name: string;
75
- /**
76
- * Transport capabilities
77
- */
78
- readonly capabilities: TransportCapabilities;
79
- /**
80
- * Read content from location
81
- * @param location - The location string after ://
82
- * @returns Raw content as Buffer
83
- */
84
- read(location: string): Promise<Buffer>;
85
- /**
86
- * Write content to location
87
- * @param location - The location string after ://
88
- * @param content - Content to write
89
- */
90
- write?(location: string, content: Buffer): Promise<void>;
91
- /**
92
- * List entries at location (for directory-like transports)
93
- * @param location - The location string after ://
94
- * @returns Array of entry names
95
- */
96
- list?(location: string): Promise<string[]>;
97
- /**
98
- * Create directory at location
99
- * @param location - The location string after ://
100
- */
101
- mkdir?(location: string): Promise<void>;
102
- /**
103
- * Check if resource exists at location
104
- * @param location - The location string after ://
105
- */
106
- exists?(location: string): Promise<boolean>;
107
- /**
108
- * Get resource stat information
109
- * @param location - The location string after ://
110
- */
111
- stat?(location: string): Promise<ResourceStat>;
112
- /**
113
- * Delete resource at location
114
- * @param location - The location string after ://
115
- */
116
- delete?(location: string): Promise<void>;
117
- }
118
- declare class HttpTransportHandler implements TransportHandler {
42
+ interface RXM {
43
+ readonly domain: string;
44
+ readonly path?: string;
119
45
  readonly name: string;
120
- private readonly protocol;
121
- readonly capabilities: TransportCapabilities;
122
- constructor(protocol?: "http" | "https");
123
- read(location: string): Promise<Buffer>;
124
- }
125
- declare const httpsHandler: HttpTransportHandler;
126
- declare const httpHandler: HttpTransportHandler;
127
- declare class FileTransportHandler implements TransportHandler {
128
- readonly name = "file";
129
- readonly capabilities: TransportCapabilities;
130
- private resolvePath;
131
- read(location: string): Promise<Buffer>;
132
- write(location: string, content: Buffer): Promise<void>;
133
- list(location: string): Promise<string[]>;
134
- mkdir(location: string): Promise<void>;
135
- exists(location: string): Promise<boolean>;
136
- stat(location: string): Promise<ResourceStat>;
137
- delete(location: string): Promise<void>;
46
+ readonly type: string;
47
+ readonly version: string;
48
+ toLocator(): string;
49
+ toJSON(): ManifestData;
138
50
  }
139
- declare const fileHandler: FileTransportHandler;
140
- interface DeepracticeConfig {
141
- /**
142
- * Parent directory for .deepractice folder
143
- * @default homedir()
144
- */
145
- parentDir?: string;
51
+ interface ManifestData {
52
+ domain?: string;
53
+ path?: string;
54
+ name?: string;
55
+ type?: string;
56
+ version?: string;
146
57
  }
147
58
  /**
148
- * Create deepractice transport handler
149
- * Maps deepractice://path to parentDir/.deepractice/path
150
- *
151
- * @example
152
- * ```typescript
153
- * const handler = deepracticeHandler();
154
- * // → ~/.deepractice/
59
+ * Create a manifest from data object.
60
+ */
61
+ declare function createRXM(data: ManifestData): RXM;
62
+ /**
63
+ * RXC - ResourceX Content
155
64
  *
156
- * const handler = deepracticeHandler({ parentDir: "/var/data" });
157
- * // → /var/data/.deepractice/
158
- * ```
65
+ * Represents resource content as a stream.
159
66
  */
160
- declare function deepracticeHandler(config?: DeepracticeConfig): TransportHandler;
67
+ interface RXC {
68
+ /** Content as a readable stream */
69
+ readonly stream: ReadableStream<Uint8Array>;
70
+ /** Read content as text */
71
+ text(): Promise<string>;
72
+ /** Read content as Buffer */
73
+ buffer(): Promise<Buffer>;
74
+ /** Read content as JSON */
75
+ json<T>(): Promise<T>;
76
+ }
161
77
  /**
162
- * Get transport handler by name
78
+ * Create RXC from string, Buffer, or ReadableStream.
163
79
  */
164
- declare function getTransportHandler(name: string): TransportHandler;
80
+ declare function createRXC(data: string | Buffer | ReadableStream<Uint8Array>): RXC;
165
81
  /**
166
- * Register a custom transport handler
82
+ * Load RXC from file path or URL.
167
83
  */
168
- declare function registerTransportHandler(handler: TransportHandler): void;
84
+ declare function loadRXC(source: string): Promise<RXC>;
169
85
  /**
170
- * Resource metadata
86
+ * RXR (ResourceX Resource) - Complete resource object.
87
+ * A pure data transfer object combining locator, manifest, and content.
171
88
  */
172
- interface ResourceMeta {
173
- url: string;
174
- semantic: string;
175
- transport: string;
176
- location: string;
177
- size: number;
178
- encoding?: string;
179
- mimeType?: string;
180
- resolvedAt: string;
89
+ interface RXR {
90
+ locator: RXL;
91
+ manifest: RXM;
92
+ content: RXC;
181
93
  }
182
94
  /**
183
- * Context passed to semantic handler
95
+ * ResourceSerializer - Handles RXR serialization/deserialization for storage.
184
96
  */
185
- interface SemanticContext {
186
- url: string;
187
- semantic: string;
188
- transport: string;
189
- location: string;
190
- timestamp: Date;
97
+ interface ResourceSerializer {
98
+ /**
99
+ * Serialize RXR to storage format.
100
+ */
101
+ serialize(rxr: RXR): Promise<Buffer>;
102
+ /**
103
+ * Deserialize storage data to RXR.
104
+ */
105
+ deserialize(data: Buffer, manifest: RXM): Promise<RXR>;
191
106
  }
192
107
  /**
193
- * Base resource interface
108
+ * ResourceResolver - Transforms RXR into usable object.
194
109
  */
195
- interface Resource<T = unknown> {
196
- type: string;
197
- content: T;
198
- meta: ResourceMeta;
110
+ interface ResourceResolver<T = unknown> {
111
+ /**
112
+ * Resolve RXR content into a usable object.
113
+ */
114
+ resolve(rxr: RXR): Promise<T>;
199
115
  }
200
116
  /**
201
- * Semantic handler interface
202
- * Semantic orchestrates Transport primitives to resolve/deposit resources
117
+ * ResourceType - Defines how a resource type is handled.
203
118
  */
204
- interface SemanticHandler<T = unknown> {
119
+ interface ResourceType<T = unknown> {
205
120
  /**
206
- * Semantic name (e.g., "text", "json", "package")
121
+ * Type name (e.g., "text", "json", "binary").
207
122
  */
208
- readonly name: string;
123
+ name: string;
209
124
  /**
210
- * Resolve resource from location
211
- * Semantic controls how to use transport primitives to fetch and parse resource
212
- *
213
- * @param transport - Transport handler for I/O operations
214
- * @param location - Resource location
215
- * @param context - Semantic context
125
+ * Alternative names for this type (e.g., ["txt", "plaintext"]).
216
126
  */
217
- resolve(transport: TransportHandler, location: string, context: SemanticContext): Promise<Resource<T>>;
127
+ aliases?: string[];
218
128
  /**
219
- * Deposit resource to location
220
- * Semantic controls how to serialize data and use transport primitives to store
221
- *
222
- * @param transport - Transport handler for I/O operations
223
- * @param location - Resource location
224
- * @param data - Data to deposit
225
- * @param context - Semantic context
129
+ * Human-readable description.
226
130
  */
227
- deposit?(transport: TransportHandler, location: string, data: T, context: SemanticContext): Promise<void>;
131
+ description: string;
228
132
  /**
229
- * Check if resource exists at location
230
- *
231
- * @param transport - Transport handler for I/O operations
232
- * @param location - Resource location
233
- * @param context - Semantic context
133
+ * Serializer for storage operations.
234
134
  */
235
- exists?(transport: TransportHandler, location: string, context: SemanticContext): Promise<boolean>;
135
+ serializer: ResourceSerializer;
236
136
  /**
237
- * Delete resource at location
238
- *
239
- * @param transport - Transport handler for I/O operations
240
- * @param location - Resource location
241
- * @param context - Semantic context
137
+ * Resolver to transform RXR into usable object.
242
138
  */
243
- delete?(transport: TransportHandler, location: string, context: SemanticContext): Promise<void>;
244
- }
245
- interface TextResource extends Resource<string> {
246
- type: "text";
247
- content: string;
248
- }
249
- declare class TextSemanticHandler implements SemanticHandler<string> {
250
- readonly name = "text";
251
- resolve(transport: TransportHandler, location: string, context: SemanticContext): Promise<TextResource>;
252
- deposit(transport: TransportHandler, location: string, data: string, _context: SemanticContext): Promise<void>;
253
- exists(transport: TransportHandler, location: string, _context: SemanticContext): Promise<boolean>;
254
- delete(transport: TransportHandler, location: string, _context: SemanticContext): Promise<void>;
255
- }
256
- declare const textHandler: TextSemanticHandler;
257
- interface BinaryResource extends Resource<Buffer> {
258
- type: "binary";
259
- content: Buffer;
260
- }
261
- /**
262
- * Supported binary input types for deposit
263
- */
264
- type BinaryInput = Buffer | Uint8Array | ArrayBuffer | number[];
265
- declare class BinarySemanticHandler implements SemanticHandler<Buffer> {
266
- readonly name = "binary";
267
- resolve(transport: TransportHandler, location: string, context: SemanticContext): Promise<BinaryResource>;
268
- deposit(transport: TransportHandler, location: string, data: BinaryInput, _context: SemanticContext): Promise<void>;
269
- exists(transport: TransportHandler, location: string, _context: SemanticContext): Promise<boolean>;
270
- delete(transport: TransportHandler, location: string, _context: SemanticContext): Promise<void>;
139
+ resolver: ResourceResolver<T>;
271
140
  }
272
- declare const binaryHandler: BinarySemanticHandler;
273
- /**
274
- * Get semantic handler by name
275
- */
276
- declare function getSemanticHandler(name: string): SemanticHandler;
277
141
  /**
278
- * Register a custom semantic handler
142
+ * Define and register a resource type.
143
+ *
144
+ * @throws ResourceTypeError if type is already registered
279
145
  */
280
- declare function registerSemanticHandler(handler: SemanticHandler): void;
146
+ declare function defineResourceType<T>(config: ResourceType<T>): ResourceType<T>;
281
147
  /**
282
- * Resolve an ARP URL to a resource
148
+ * Get a registered resource type by name.
283
149
  *
284
- * @example
285
- * const resource = await resolve("arp:text:https://example.com/file.txt");
286
- * // { type: "text", content: "...", meta: { ... } }
150
+ * @returns ResourceType or undefined if not found
287
151
  */
288
- declare function resolve(url: string): Promise<Resource>;
152
+ declare function getResourceType<T = unknown>(name: string): ResourceType<T> | undefined;
289
153
  /**
290
- * Deposit data to an ARP URL
291
- *
292
- * @example
293
- * await deposit("arp:text:file://./data/config.txt", "hello world");
154
+ * Clear all registered resource types (for testing).
294
155
  */
295
- declare function deposit(url: string, data: unknown): Promise<void>;
156
+ declare function clearResourceTypes(): void;
296
157
  /**
297
- * Check if resource exists at ARP URL
298
- *
299
- * @example
300
- * const exists = await resourceExists("arp:text:file://./data/config.txt");
158
+ * Text resource type
301
159
  */
302
- declare function resourceExists(url: string): Promise<boolean>;
160
+ declare const textType: ResourceType<string>;
303
161
  /**
304
- * Delete resource at ARP URL
305
- *
306
- * @example
307
- * await resourceDelete("arp:text:file://./data/config.txt");
162
+ * JSON resource type
308
163
  */
309
- declare function resourceDelete(url: string): Promise<void>;
164
+ declare const jsonType: ResourceType<unknown>;
310
165
  /**
311
- * Resource Definition Types
166
+ * Binary resource type
312
167
  */
168
+ declare const binaryType: ResourceType<Buffer>;
313
169
  /**
314
- * Definition of a custom resource (shortcut for ARP URL)
170
+ * All built-in types
315
171
  */
316
- interface ResourceDefinition {
317
- /** Unique name for the resource (used in URL: name://location) */
318
- readonly name: string;
319
- /** Semantic type (e.g., "text", "json", "binary") */
320
- readonly semantic: string;
321
- /** Transport type (e.g., "file", "https") */
322
- readonly transport: string;
323
- /** Base path prepended to location (optional) */
324
- readonly basePath?: string;
325
- }
172
+ declare const builtinTypes: ResourceType[];
326
173
  /**
327
- * Create a new resource registry (isolated instance)
174
+ * TypeHandlerChain - Responsibility chain for resource type handling.
175
+ * Manages type registration and delegates serialization/deserialization.
328
176
  */
329
- declare function createResourceRegistry(): {
177
+ declare class TypeHandlerChain {
178
+ private handlers;
330
179
  /**
331
- * Register a resource definition
180
+ * Register a resource type handler.
181
+ * Registers both the type name and its aliases.
332
182
  */
333
- register(definition: ResourceDefinition): void
183
+ register(type: ResourceType): void;
334
184
  /**
335
- * Get a resource definition by name
185
+ * Register multiple type handlers.
336
186
  */
337
- get(name: string): ResourceDefinition | undefined
187
+ registerAll(types: ResourceType[]): void;
338
188
  /**
339
- * Check if a resource is registered
189
+ * Check if a type is supported.
340
190
  */
341
- has(name: string): boolean
191
+ canHandle(typeName: string): boolean;
342
192
  /**
343
- * Clear all registered resources
193
+ * Get handler for a type.
344
194
  */
345
- clear(): void
346
- };
347
- type ResourceRegistry = ReturnType<typeof createResourceRegistry>;
348
- declare const VERSION: string;
349
- export { textHandler, resourceExists, resourceDelete, resolve, registerTransportHandler, registerSemanticHandler, parseARP, httpsHandler, httpHandler, getTransportHandler, getSemanticHandler, fileHandler, deposit, deepracticeHandler, createResourceRegistry, binaryHandler, VERSION, TransportHandler, TransportError, TransportCapabilities, TextResource, SemanticHandler, SemanticError, SemanticContext, ResourceXError, ResourceStat, ResourceRegistry, ResourceMeta, ResourceDefinition, Resource, ParsedARP, ParseError, DeepracticeConfig, BinaryResource, BinaryInput };
195
+ getHandler(typeName: string): ResourceType | undefined;
196
+ /**
197
+ * Serialize RXR content using the appropriate type handler.
198
+ */
199
+ serialize(rxr: RXR): Promise<Buffer>;
200
+ /**
201
+ * Deserialize content into RXR using the appropriate type handler.
202
+ */
203
+ deserialize(data: Buffer, manifest: RXM): Promise<RXR>;
204
+ /**
205
+ * Resolve RXR content into usable object using the appropriate type handler.
206
+ */
207
+ resolve<T = unknown>(rxr: RXR): Promise<T>;
208
+ }
209
+ /**
210
+ * Create a new TypeHandlerChain with optional initial types.
211
+ */
212
+ declare function createTypeHandlerChain(types?: ResourceType[]): TypeHandlerChain;
213
+ export { textType, parseRXL, loadRXC, jsonType, getResourceType, defineResourceType, createTypeHandlerChain, createRXM, createRXC, clearResourceTypes, builtinTypes, binaryType, TypeHandlerChain, ResourceXError, ResourceTypeError, ResourceType, ResourceSerializer, ResourceResolver, RXR, RXM, RXL, RXC, ManifestError, ManifestData, LocatorError, ContentError };