@semiont/core 0.2.43 → 0.2.46
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 +429 -69
- package/dist/index.js +111 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
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
|
|
|
@@ -401,6 +402,115 @@ function findBodyItem(body, targetItem) {
|
|
|
401
402
|
}
|
|
402
403
|
return -1;
|
|
403
404
|
}
|
|
405
|
+
function generateUuid() {
|
|
406
|
+
return randomBytes(16).toString("hex");
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// src/annotation-assembly.ts
|
|
410
|
+
function getTextPositionSelector(selector) {
|
|
411
|
+
if (!selector) return null;
|
|
412
|
+
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
413
|
+
const found = selectors.find((s) => s.type === "TextPositionSelector");
|
|
414
|
+
if (!found) return null;
|
|
415
|
+
return found.type === "TextPositionSelector" ? found : null;
|
|
416
|
+
}
|
|
417
|
+
function getSvgSelector(selector) {
|
|
418
|
+
if (!selector) return null;
|
|
419
|
+
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
420
|
+
const found = selectors.find((s) => s.type === "SvgSelector");
|
|
421
|
+
if (!found) return null;
|
|
422
|
+
return found.type === "SvgSelector" ? found : null;
|
|
423
|
+
}
|
|
424
|
+
function getFragmentSelector(selector) {
|
|
425
|
+
if (!selector) return null;
|
|
426
|
+
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
427
|
+
const found = selectors.find((s) => s.type === "FragmentSelector");
|
|
428
|
+
if (!found) return null;
|
|
429
|
+
return found.type === "FragmentSelector" ? found : null;
|
|
430
|
+
}
|
|
431
|
+
function validateSvgMarkup(svg) {
|
|
432
|
+
if (!svg.includes('xmlns="http://www.w3.org/2000/svg"')) {
|
|
433
|
+
return 'SVG must include xmlns="http://www.w3.org/2000/svg" attribute';
|
|
434
|
+
}
|
|
435
|
+
if (!svg.includes("<svg") || !svg.includes("</svg>")) {
|
|
436
|
+
return "SVG must have opening and closing tags";
|
|
437
|
+
}
|
|
438
|
+
const shapeElements = ["rect", "circle", "ellipse", "polygon", "polyline", "path", "line"];
|
|
439
|
+
const hasShape = shapeElements.some(
|
|
440
|
+
(shape) => svg.includes(`<${shape}`) || svg.includes(`<${shape} `)
|
|
441
|
+
);
|
|
442
|
+
if (!hasShape) {
|
|
443
|
+
return "SVG must contain at least one shape element (rect, circle, ellipse, polygon, polyline, path, or line)";
|
|
444
|
+
}
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
function generateAnnotationId(baseUrl2) {
|
|
448
|
+
if (!baseUrl2) {
|
|
449
|
+
throw new Error("baseUrl is required to generate annotation URIs");
|
|
450
|
+
}
|
|
451
|
+
const normalizedBase = baseUrl2.endsWith("/") ? baseUrl2.slice(0, -1) : baseUrl2;
|
|
452
|
+
return `${normalizedBase}/annotations/${generateUuid()}`;
|
|
453
|
+
}
|
|
454
|
+
function assembleAnnotation(request, creator, publicURL) {
|
|
455
|
+
const newAnnotationId = generateAnnotationId(publicURL);
|
|
456
|
+
const posSelector = getTextPositionSelector(request.target.selector);
|
|
457
|
+
const svgSelector = getSvgSelector(request.target.selector);
|
|
458
|
+
const fragmentSelector = getFragmentSelector(request.target.selector);
|
|
459
|
+
if (!posSelector && !svgSelector && !fragmentSelector) {
|
|
460
|
+
throw new Error("Either TextPositionSelector, SvgSelector, or FragmentSelector is required for creating annotations");
|
|
461
|
+
}
|
|
462
|
+
if (svgSelector) {
|
|
463
|
+
const svgError = validateSvgMarkup(svgSelector.value);
|
|
464
|
+
if (svgError) {
|
|
465
|
+
throw new Error(`Invalid SVG markup: ${svgError}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (!request.motivation) {
|
|
469
|
+
throw new Error("motivation is required");
|
|
470
|
+
}
|
|
471
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
472
|
+
const annotation = {
|
|
473
|
+
"@context": "http://www.w3.org/ns/anno.jsonld",
|
|
474
|
+
"type": "Annotation",
|
|
475
|
+
id: newAnnotationId,
|
|
476
|
+
motivation: request.motivation,
|
|
477
|
+
target: request.target,
|
|
478
|
+
body: request.body,
|
|
479
|
+
creator,
|
|
480
|
+
created: now,
|
|
481
|
+
modified: now
|
|
482
|
+
};
|
|
483
|
+
const bodyArray = Array.isArray(request.body) ? request.body : request.body ? [request.body] : [];
|
|
484
|
+
return { annotation, bodyArray };
|
|
485
|
+
}
|
|
486
|
+
function applyBodyOperations(body, operations) {
|
|
487
|
+
const bodyArray = Array.isArray(body) ? [...body] : [];
|
|
488
|
+
for (const op of operations) {
|
|
489
|
+
if (op.op === "add") {
|
|
490
|
+
const exists = bodyArray.some(
|
|
491
|
+
(item) => JSON.stringify(item) === JSON.stringify(op.item)
|
|
492
|
+
);
|
|
493
|
+
if (!exists) {
|
|
494
|
+
bodyArray.push(op.item);
|
|
495
|
+
}
|
|
496
|
+
} else if (op.op === "remove") {
|
|
497
|
+
const index = bodyArray.findIndex(
|
|
498
|
+
(item) => JSON.stringify(item) === JSON.stringify(op.item)
|
|
499
|
+
);
|
|
500
|
+
if (index !== -1) {
|
|
501
|
+
bodyArray.splice(index, 1);
|
|
502
|
+
}
|
|
503
|
+
} else if (op.op === "replace") {
|
|
504
|
+
const index = bodyArray.findIndex(
|
|
505
|
+
(item) => JSON.stringify(item) === JSON.stringify(op.oldItem)
|
|
506
|
+
);
|
|
507
|
+
if (index !== -1) {
|
|
508
|
+
bodyArray[index] = op.newItem;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return bodyArray;
|
|
513
|
+
}
|
|
404
514
|
|
|
405
515
|
// src/type-guards.ts
|
|
406
516
|
function isString(value) {
|
|
@@ -2375,6 +2485,6 @@ function getAllPlatformTypes() {
|
|
|
2375
2485
|
var CORE_TYPES_VERSION = "0.1.0";
|
|
2376
2486
|
var SDK_VERSION = "0.1.0";
|
|
2377
2487
|
|
|
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 };
|
|
2488
|
+
export { APIError, CORE_TYPES_VERSION, CREATION_METHODS, ConfigurationError, ConflictError, EventBus, NotFoundError, SDK_VERSION, ScopedEventBus, ScriptError, SemiontError, UnauthorizedError, ValidationError, accessToken, annotationId, annotationIdToURI, annotationUri, applyBodyOperations, assembleAnnotation, authCode, baseUrl, burstBuffer, cloneToken, createConfigLoader, deepMerge, didToAgent, displayConfiguration, email, entityType, extractResourceUriFromAnnotationUri, 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, resourceIdToURI, resourceUri, searchQuery, uriToAnnotationId, uriToAnnotationIdOrPassthrough, uriToResourceId, userDID, userId, userToAgent, userToDid, validateEnvironment, validateEnvironmentConfig, validateSemiontConfig, validateSiteConfig, validateSvgMarkup };
|
|
2379
2489
|
//# sourceMappingURL=index.js.map
|
|
2380
2490
|
//# sourceMappingURL=index.js.map
|