capns 0.41.10876 → 0.43.10973

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 (2) hide show
  1. package/capns.js +53 -3
  2. package/package.json +2 -2
package/capns.js CHANGED
@@ -785,9 +785,54 @@ const MEDIA_XML = 'media:xml;textable';
785
785
  const MEDIA_JSON = 'media:json;textable;keyed';
786
786
  const MEDIA_YAML = 'media:yaml;textable;keyed';
787
787
 
788
+ // =============================================================================
789
+ // SCHEMA URL CONFIGURATION
790
+ // =============================================================================
791
+
792
+ const DEFAULT_SCHEMA_BASE = 'https://capns.org/schema';
793
+
794
+ /**
795
+ * Get the schema base URL from environment variables or default
796
+ *
797
+ * Checks in order:
798
+ * 1. CAPNS_SCHEMA_BASE_URL environment variable
799
+ * 2. CAPNS_REGISTRY_URL environment variable + "/schema"
800
+ * 3. Default: "https://capns.org/schema"
801
+ *
802
+ * @returns {string} The schema base URL
803
+ */
804
+ function getSchemaBaseURL() {
805
+ if (typeof process !== 'undefined' && process.env) {
806
+ if (process.env.CAPNS_SCHEMA_BASE_URL) {
807
+ return process.env.CAPNS_SCHEMA_BASE_URL;
808
+ }
809
+ if (process.env.CAPNS_REGISTRY_URL) {
810
+ return process.env.CAPNS_REGISTRY_URL + '/schema';
811
+ }
812
+ }
813
+ return DEFAULT_SCHEMA_BASE;
814
+ }
815
+
816
+ /**
817
+ * Get a profile URL for the given profile name
818
+ *
819
+ * @param {string} profileName - The profile name (e.g., 'str', 'int')
820
+ * @returns {string} The full profile URL
821
+ */
822
+ function getProfileURL(profileName) {
823
+ return `${getSchemaBaseURL()}/${profileName}`;
824
+ }
825
+
826
+ // =============================================================================
827
+ // BUILTIN MEDIA SPECS
828
+ // =============================================================================
829
+
788
830
  /**
789
831
  * Built-in media URN definitions - canonical media spec strings
790
832
  * Maps media URN to canonical format: <media-type>; profile=<url>
833
+ *
834
+ * NOTE: These use hardcoded URLs for static initialization.
835
+ * Use getSchemaBaseURL() and getProfileURL() for dynamic resolution.
791
836
  */
792
837
  const BUILTIN_SPECS = {
793
838
  [MEDIA_STRING]: 'text/plain; profile=https://capns.org/schema/str',
@@ -923,8 +968,9 @@ class MediaSpec {
923
968
  * @param {string|null} description - Optional description
924
969
  * @param {string|null} mediaUrn - Source media URN for tag-based checks
925
970
  * @param {Object|null} validation - Optional validation rules (min, max, min_length, max_length, pattern, allowed_values)
971
+ * @param {Object|null} metadata - Optional metadata (arbitrary key-value pairs for display/categorization)
926
972
  */
927
- constructor(contentType, profile = null, schema = null, title = null, description = null, mediaUrn = null, validation = null) {
973
+ constructor(contentType, profile = null, schema = null, title = null, description = null, mediaUrn = null, validation = null, metadata = null) {
928
974
  this.contentType = contentType;
929
975
  this.profile = profile;
930
976
  this.schema = schema;
@@ -932,6 +978,7 @@ class MediaSpec {
932
978
  this.description = description;
933
979
  this.mediaUrn = mediaUrn;
934
980
  this.validation = validation;
981
+ this.metadata = metadata;
935
982
  }
936
983
 
937
984
  /**
@@ -1114,13 +1161,14 @@ function resolveMediaUrn(mediaUrn, mediaSpecs = {}) {
1114
1161
  spec.mediaUrn = mediaUrn; // Attach source URN for tag-based checks
1115
1162
  return spec;
1116
1163
  } else if (typeof def === 'object') {
1117
- // Object form: { media_type, profile_uri, schema?, title?, description?, validation? }
1164
+ // Object form: { media_type, profile_uri, schema?, title?, description?, validation?, metadata? }
1118
1165
  const mediaType = def.media_type || def.mediaType;
1119
1166
  const profileUri = def.profile_uri || def.profileUri;
1120
1167
  const schema = def.schema || null;
1121
1168
  const title = def.title || null;
1122
1169
  const description = def.description || null;
1123
1170
  const validation = def.validation || null;
1171
+ const metadata = def.metadata || null;
1124
1172
 
1125
1173
  if (!mediaType) {
1126
1174
  throw new MediaSpecError(
@@ -1129,7 +1177,7 @@ function resolveMediaUrn(mediaUrn, mediaSpecs = {}) {
1129
1177
  );
1130
1178
  }
1131
1179
 
1132
- return new MediaSpec(mediaType, profileUri, schema, title, description, mediaUrn, validation);
1180
+ return new MediaSpec(mediaType, profileUri, schema, title, description, mediaUrn, validation, metadata);
1133
1181
  }
1134
1182
  }
1135
1183
 
@@ -3499,6 +3547,8 @@ module.exports = {
3499
3547
  resolveMediaUrn,
3500
3548
  isBuiltinMediaUrn,
3501
3549
  BUILTIN_SPECS,
3550
+ getSchemaBaseURL,
3551
+ getProfileURL,
3502
3552
  MEDIA_STRING,
3503
3553
  MEDIA_INTEGER,
3504
3554
  MEDIA_NUMBER,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Bahram Joharshamshiri",
3
3
  "dependencies": {
4
- "tagged-urn": "^0.10.2767"
4
+ "tagged-urn": "^0.11.2835"
5
5
  },
6
6
  "description": "JavaScript implementation of Cap URN (Capability Uniform Resource Names) with strict validation and matching",
7
7
  "engines": {
@@ -32,5 +32,5 @@
32
32
  "scripts": {
33
33
  "test": "node capns.test.js"
34
34
  },
35
- "version": "0.41.10876"
35
+ "version": "0.43.10973"
36
36
  }