capns 0.32.9247

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.
Files changed (5) hide show
  1. package/README.md +130 -0
  2. package/RULES.md +55 -0
  3. package/capns.js +2834 -0
  4. package/capns.test.js +1459 -0
  5. package/package.json +36 -0
package/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # Cap URN - JavaScript Implementation
2
+
3
+ JavaScript implementation of Cap URN (Capability Uniform Resource Names), built on [Tagged URN](../tagged-urn-js/).
4
+
5
+ ## Features
6
+
7
+ - **Required Direction Specifiers** - `in`/`out` tags for input/output media types
8
+ - **Media URN Validation** - Validates direction spec values are valid Media URNs
9
+ - **Cross-Language Compatible** - Identical behavior to Rust, Go, and Objective-C implementations
10
+ - **Production Ready** - No fallbacks, fails hard on invalid input
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install capns
16
+ ```
17
+
18
+ ## Quick Start
19
+
20
+ ```javascript
21
+ const { CapUrn, CapUrnBuilder, CapMatcher } = require('capns');
22
+
23
+ // Create from string (with required direction specifiers)
24
+ const cap = CapUrn.fromString('cap:in="media:type=binary;v=1";op=extract;out="media:type=object;v=1"');
25
+ console.log(cap.toString());
26
+
27
+ // Use builder pattern
28
+ const built = new CapUrnBuilder()
29
+ .inSpec('media:type=void;v=1')
30
+ .outSpec('media:type=object;v=1')
31
+ .tag('op', 'generate')
32
+ .tag('target', 'thumbnail')
33
+ .build();
34
+
35
+ // Matching
36
+ const request = CapUrn.fromString('cap:in="media:type=binary;v=1";op=extract;out="media:type=object;v=1"');
37
+ console.log(cap.matches(request)); // true
38
+
39
+ // Find best match by specificity
40
+ const caps = [
41
+ CapUrn.fromString('cap:in=*;op=extract;out=*'),
42
+ CapUrn.fromString('cap:in="media:type=binary;v=1";op=extract;out="media:type=object;v=1"'),
43
+ CapUrn.fromString('cap:ext=pdf;in="media:type=binary;v=1";op=extract;out="media:type=object;v=1"')
44
+ ];
45
+ const best = CapMatcher.findBestMatch(caps, request);
46
+ console.log(best.toString()); // Most specific match
47
+ ```
48
+
49
+ ## API Reference
50
+
51
+ ### CapUrn Class
52
+
53
+ #### Static Methods
54
+ - `CapUrn.fromString(s)` - Parse Cap URN from string
55
+ - Throws `CapUrnError` on invalid format or missing direction specifiers
56
+
57
+ #### Instance Methods
58
+ - `toString()` - Get canonical string representation
59
+ - `getTag(key)` - Get tag value (case-insensitive)
60
+ - `getInSpec()` - Get input media type
61
+ - `getOutSpec()` - Get output media type
62
+ - `hasTag(key, value)` - Check if tag exists with value
63
+ - `withTag(key, value)` - Add/update tag (returns new instance)
64
+ - `withoutTag(key)` - Remove tag (returns new instance)
65
+ - `matches(other)` - Check if this cap matches another
66
+ - `canHandle(request)` - Check if this cap can handle a request
67
+ - `specificity()` - Get specificity score for matching
68
+ - `isMoreSpecificThan(other)` - Compare specificity
69
+ - `equals(other)` - Check equality
70
+
71
+ ### CapUrnBuilder Class
72
+
73
+ Fluent builder for constructing Cap URNs:
74
+
75
+ ```javascript
76
+ const cap = new CapUrnBuilder()
77
+ .inSpec('media:type=binary;v=1')
78
+ .outSpec('media:type=object;v=1')
79
+ .tag('op', 'extract')
80
+ .tag('target', 'metadata')
81
+ .build();
82
+ ```
83
+
84
+ ### CapMatcher Class
85
+
86
+ Utility for matching sets of caps:
87
+
88
+ - `CapMatcher.findBestMatch(caps, request)` - Find most specific match
89
+ - `CapMatcher.findAllMatches(caps, request)` - Find all matches (sorted by specificity)
90
+
91
+ ### Error Handling
92
+
93
+ ```javascript
94
+ const { CapUrnError, ErrorCodes } = require('capns');
95
+
96
+ try {
97
+ const cap = CapUrn.fromString('cap:op=extract'); // Missing in/out
98
+ } catch (error) {
99
+ if (error instanceof CapUrnError) {
100
+ console.log(`Error code: ${error.code}`); // MISSING_IN_SPEC
101
+ }
102
+ }
103
+ ```
104
+
105
+ Cap-specific error codes:
106
+ - `ErrorCodes.MISSING_IN_SPEC` - Missing required `in` tag
107
+ - `ErrorCodes.MISSING_OUT_SPEC` - Missing required `out` tag
108
+ - `ErrorCodes.INVALID_MEDIA_URN` - Invalid Media URN in direction spec
109
+
110
+ For base Tagged URN error codes, see [Tagged URN documentation](../tagged-urn-js/).
111
+
112
+ ## Documentation
113
+
114
+ - [RULES.md](./RULES.md) - Cap-specific rules
115
+ - [Tagged URN RULES.md](../tagged-urn-js/RULES.md) - Base format rules (case, quoting, wildcards, etc.)
116
+
117
+ ## Testing
118
+
119
+ ```bash
120
+ npm test
121
+ ```
122
+
123
+ ## Cross-Language Compatibility
124
+
125
+ This JavaScript implementation produces identical results to:
126
+ - [Rust reference implementation](../capns/)
127
+ - [Go implementation](../capns-go/)
128
+ - [Objective-C implementation](../capns-objc/)
129
+
130
+ All implementations pass the same test cases and follow identical rules.
package/RULES.md ADDED
@@ -0,0 +1,55 @@
1
+ # Cap URN Rules (JavaScript)
2
+
3
+ ## Overview
4
+
5
+ Cap URNs extend Tagged URNs with capability-specific requirements. For base Tagged URN format rules (case handling, tag ordering, wildcards, quoting, value-less tags, etc.), see [Tagged URN RULES.md](../tagged-urn-js/RULES.md).
6
+
7
+ This document covers only cap-specific rules.
8
+
9
+ ## Cap-Specific Rules
10
+
11
+ ### 1. Required Direction Specifiers
12
+
13
+ Cap URNs **must** include `in` and `out` tags that specify input/output media types:
14
+
15
+ ```javascript
16
+ // Valid cap URN with direction specifiers
17
+ const cap = CapUrn.fromString('cap:in="media:type=binary;v=1";op=extract;out="media:type=object;v=1"');
18
+
19
+ // Invalid - missing direction specifiers
20
+ CapUrn.fromString('cap:op=extract'); // throws ErrorCodes.MISSING_IN_SPEC
21
+ ```
22
+
23
+ ### 2. No Value-less Tags
24
+
25
+ Unlike base Tagged URNs which allow value-less tags, Cap URNs require explicit `key=value` format for all tags.
26
+
27
+ ### 3. Media URN Validation
28
+
29
+ Direction specifier values must be valid Media URNs or wildcard `*`:
30
+
31
+ ```javascript
32
+ // Valid: Media URN value
33
+ 'cap:in="media:type=binary;v=1";op=extract;out="media:type=object;v=1"'
34
+
35
+ // Valid: Wildcard
36
+ 'cap:in=*;op=extract;out=*'
37
+
38
+ // Invalid: Not a Media URN
39
+ 'cap:in=binary;op=extract;out=object' // throws ErrorCodes.INVALID_MEDIA_URN
40
+ ```
41
+
42
+ ## Cap-Specific Error Codes
43
+
44
+ | Code | Name | Description |
45
+ |------|------|-------------|
46
+ | 10 | MISSING_IN_SPEC | Cap URN missing required `in` tag |
47
+ | 11 | MISSING_OUT_SPEC | Cap URN missing required `out` tag |
48
+ | 12 | INVALID_MEDIA_URN | Direction spec value is not a valid Media URN |
49
+
50
+ ## Cross-Language Compatibility
51
+
52
+ This JavaScript implementation follows the same rules as:
53
+ - [Rust reference implementation](../capns/)
54
+ - [Go implementation](../capns-go/)
55
+ - [Objective-C implementation](../capns-objc/)