@semiont/core 0.2.45 → 0.3.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.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Subject, Observable } from 'rxjs';
2
+ import { randomBytes } from 'crypto';
2
3
  import Ajv from 'ajv';
3
4
  import addFormats from 'ajv-formats';
4
5
 
@@ -94,46 +95,6 @@ function userId(id) {
94
95
  return id;
95
96
  }
96
97
 
97
- // src/uri-utils.ts
98
- function resourceIdToURI(id, publicURL) {
99
- const normalizedBase = publicURL.endsWith("/") ? publicURL.slice(0, -1) : publicURL;
100
- return resourceUri(`${normalizedBase}/resources/${id}`);
101
- }
102
- function uriToResourceId(uri) {
103
- const url = new URL(uri);
104
- const match = url.pathname.match(/\/resources\/([^/]+)/);
105
- if (!match || !match[1]) {
106
- throw new Error(`Invalid resource URI: ${uri}`);
107
- }
108
- return resourceId(match[1]);
109
- }
110
- function annotationIdToURI(id, publicURL) {
111
- const normalizedBase = publicURL.endsWith("/") ? publicURL.slice(0, -1) : publicURL;
112
- return annotationUri(`${normalizedBase}/annotations/${id}`);
113
- }
114
- function uriToAnnotationId(uri) {
115
- const url = new URL(uri);
116
- const match = url.pathname.match(/\/annotations\/([^/]+)/);
117
- if (!match || !match[1]) {
118
- throw new Error(`Invalid annotation URI: ${uri}`);
119
- }
120
- return annotationId(match[1]);
121
- }
122
- function uriToAnnotationIdOrPassthrough(uriOrId) {
123
- try {
124
- return uriToAnnotationId(uriOrId);
125
- } catch {
126
- return annotationId(uriOrId);
127
- }
128
- }
129
- function extractResourceUriFromAnnotationUri(annotationUri2) {
130
- const parts = annotationUri2.split("/annotations/");
131
- if (parts.length !== 2) {
132
- throw new Error(`Invalid annotation URI format: ${annotationUri2}`);
133
- }
134
- return resourceUri(parts[0]);
135
- }
136
-
137
98
  // src/events.ts
138
99
  function isResourceEvent(event) {
139
100
  return event && typeof event.id === "string" && typeof event.timestamp === "string" && (event.resourceId === void 0 || typeof event.resourceId === "string") && // resourceId now optional
@@ -401,6 +362,108 @@ function findBodyItem(body, targetItem) {
401
362
  }
402
363
  return -1;
403
364
  }
365
+ function generateUuid() {
366
+ return randomBytes(16).toString("hex");
367
+ }
368
+
369
+ // src/annotation-assembly.ts
370
+ function getTextPositionSelector(selector) {
371
+ if (!selector) return null;
372
+ const selectors = Array.isArray(selector) ? selector : [selector];
373
+ const found = selectors.find((s) => s.type === "TextPositionSelector");
374
+ if (!found) return null;
375
+ return found.type === "TextPositionSelector" ? found : null;
376
+ }
377
+ function getSvgSelector(selector) {
378
+ if (!selector) return null;
379
+ const selectors = Array.isArray(selector) ? selector : [selector];
380
+ const found = selectors.find((s) => s.type === "SvgSelector");
381
+ if (!found) return null;
382
+ return found.type === "SvgSelector" ? found : null;
383
+ }
384
+ function getFragmentSelector(selector) {
385
+ if (!selector) return null;
386
+ const selectors = Array.isArray(selector) ? selector : [selector];
387
+ const found = selectors.find((s) => s.type === "FragmentSelector");
388
+ if (!found) return null;
389
+ return found.type === "FragmentSelector" ? found : null;
390
+ }
391
+ function validateSvgMarkup(svg) {
392
+ if (!svg.includes('xmlns="http://www.w3.org/2000/svg"')) {
393
+ return 'SVG must include xmlns="http://www.w3.org/2000/svg" attribute';
394
+ }
395
+ if (!svg.includes("<svg") || !svg.includes("</svg>")) {
396
+ return "SVG must have opening and closing tags";
397
+ }
398
+ const shapeElements = ["rect", "circle", "ellipse", "polygon", "polyline", "path", "line"];
399
+ const hasShape = shapeElements.some(
400
+ (shape) => svg.includes(`<${shape}`) || svg.includes(`<${shape} `)
401
+ );
402
+ if (!hasShape) {
403
+ return "SVG must contain at least one shape element (rect, circle, ellipse, polygon, polyline, path, or line)";
404
+ }
405
+ return null;
406
+ }
407
+ function assembleAnnotation(request, creator) {
408
+ const newAnnotationId = generateUuid();
409
+ const posSelector = getTextPositionSelector(request.target.selector);
410
+ const svgSelector = getSvgSelector(request.target.selector);
411
+ const fragmentSelector = getFragmentSelector(request.target.selector);
412
+ if (!posSelector && !svgSelector && !fragmentSelector) {
413
+ throw new Error("Either TextPositionSelector, SvgSelector, or FragmentSelector is required for creating annotations");
414
+ }
415
+ if (svgSelector) {
416
+ const svgError = validateSvgMarkup(svgSelector.value);
417
+ if (svgError) {
418
+ throw new Error(`Invalid SVG markup: ${svgError}`);
419
+ }
420
+ }
421
+ if (!request.motivation) {
422
+ throw new Error("motivation is required");
423
+ }
424
+ const now = (/* @__PURE__ */ new Date()).toISOString();
425
+ const annotation = {
426
+ "@context": "http://www.w3.org/ns/anno.jsonld",
427
+ "type": "Annotation",
428
+ id: newAnnotationId,
429
+ motivation: request.motivation,
430
+ target: request.target,
431
+ body: request.body,
432
+ creator,
433
+ created: now,
434
+ modified: now
435
+ };
436
+ const bodyArray = Array.isArray(request.body) ? request.body : request.body ? [request.body] : [];
437
+ return { annotation, bodyArray };
438
+ }
439
+ function applyBodyOperations(body, operations) {
440
+ const bodyArray = Array.isArray(body) ? [...body] : [];
441
+ for (const op of operations) {
442
+ if (op.op === "add") {
443
+ const exists = bodyArray.some(
444
+ (item) => JSON.stringify(item) === JSON.stringify(op.item)
445
+ );
446
+ if (!exists) {
447
+ bodyArray.push(op.item);
448
+ }
449
+ } else if (op.op === "remove") {
450
+ const index = bodyArray.findIndex(
451
+ (item) => JSON.stringify(item) === JSON.stringify(op.item)
452
+ );
453
+ if (index !== -1) {
454
+ bodyArray.splice(index, 1);
455
+ }
456
+ } else if (op.op === "replace") {
457
+ const index = bodyArray.findIndex(
458
+ (item) => JSON.stringify(item) === JSON.stringify(op.oldItem)
459
+ );
460
+ if (index !== -1) {
461
+ bodyArray[index] = op.newItem;
462
+ }
463
+ }
464
+ }
465
+ return bodyArray;
466
+ }
404
467
 
405
468
  // src/type-guards.ts
406
469
  function isString(value) {
@@ -2375,6 +2438,6 @@ function getAllPlatformTypes() {
2375
2438
  var CORE_TYPES_VERSION = "0.1.0";
2376
2439
  var SDK_VERSION = "0.1.0";
2377
2440
 
2378
- export { APIError, CORE_TYPES_VERSION, CREATION_METHODS, ConfigurationError, ConflictError, EventBus, NotFoundError, SDK_VERSION, ScopedEventBus, ScriptError, SemiontError, UnauthorizedError, ValidationError, accessToken, annotationId, annotationIdToURI, annotationUri, authCode, baseUrl, burstBuffer, cloneToken, createConfigLoader, deepMerge, didToAgent, displayConfiguration, email, entityType, extractResourceUriFromAnnotationUri, findBodyItem, formatErrors, getAllPlatformTypes, getAnnotationUriFromEvent, getEventType, getNodeEnvForEnvironment, googleCredential, hasAWSConfig, isAnnotationId, isArray, isBoolean, isDefined, isEventRelatedToAnnotation, isFunction, isNull, isNullish, isNumber, isObject, isResourceEvent, isResourceId, isResourceScopedEvent, isResourceEvent2 as isStoredEvent, isString, isSystemEvent, isUndefined, isValidPlatformType, jobId, listEnvironmentNames, mcpToken, parseAndMergeConfigs, parseEnvironment, refreshToken, resolveEnvVars, resourceAnnotationUri, resourceId, resourceIdToURI, resourceUri, searchQuery, uriToAnnotationId, uriToAnnotationIdOrPassthrough, uriToResourceId, userDID, userId, userToAgent, userToDid, validateEnvironment, validateEnvironmentConfig, validateSemiontConfig, validateSiteConfig };
2441
+ export { APIError, CORE_TYPES_VERSION, CREATION_METHODS, ConfigurationError, ConflictError, EventBus, NotFoundError, SDK_VERSION, ScopedEventBus, ScriptError, SemiontError, UnauthorizedError, ValidationError, accessToken, annotationId, annotationUri, applyBodyOperations, assembleAnnotation, authCode, baseUrl, burstBuffer, cloneToken, createConfigLoader, deepMerge, didToAgent, displayConfiguration, email, entityType, findBodyItem, formatErrors, generateUuid, getAllPlatformTypes, getAnnotationUriFromEvent, getEventType, getFragmentSelector, getNodeEnvForEnvironment, getSvgSelector, getTextPositionSelector, googleCredential, hasAWSConfig, isAnnotationId, isArray, isBoolean, isDefined, isEventRelatedToAnnotation, isFunction, isNull, isNullish, isNumber, isObject, isResourceEvent, isResourceId, isResourceScopedEvent, isResourceEvent2 as isStoredEvent, isString, isSystemEvent, isUndefined, isValidPlatformType, jobId, listEnvironmentNames, mcpToken, parseAndMergeConfigs, parseEnvironment, refreshToken, resolveEnvVars, resourceAnnotationUri, resourceId, resourceUri, searchQuery, userDID, userId, userToAgent, userToDid, validateEnvironment, validateEnvironmentConfig, validateSemiontConfig, validateSiteConfig, validateSvgMarkup };
2379
2442
  //# sourceMappingURL=index.js.map
2380
2443
  //# sourceMappingURL=index.js.map