@semapps/semantic-data-provider 0.6.5 → 0.6.7

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.cjs.js CHANGED
@@ -31,6 +31,9 @@ $parcel$export(module.exports, "useDataServers", () => $c9933a88e2acc4da$export$
31
31
  $parcel$export(module.exports, "FilterHandler", () => $f763906f9b20f2d8$export$2e2bcd8739ae039);
32
32
  $parcel$export(module.exports, "GroupedReferenceHandler", () => $b4703fef6d6af456$export$2e2bcd8739ae039);
33
33
  $parcel$export(module.exports, "ReificationArrayInput", () => $030f1232f6810456$export$2e2bcd8739ae039);
34
+ $parcel$export(module.exports, "createWsChannel", () => $84ab912646919f8c$export$28772ab4c256e709);
35
+ $parcel$export(module.exports, "getOrCreateWsChannel", () => $84ab912646919f8c$export$8d60734939c59ced);
36
+ $parcel$export(module.exports, "createSolidNotificationChannel", () => $84ab912646919f8c$export$3edfe18db119b920);
34
37
 
35
38
 
36
39
  const $3db7a4510a668a04$var$fetchResource = async (resourceUri, config)=>{
@@ -366,7 +369,7 @@ const $8c999cc29c8d6a6c$var$fetchContainers = async (containers, params, { httpC
366
369
  const results = await Promise.all(fetchPromises);
367
370
  let resources = results.flat();
368
371
  resources = resources.map((resource)=>{
369
- resource.id ||= resource["@id"];
372
+ resource.id = resource.id || resource["@id"];
370
373
  return resource;
371
374
  });
372
375
  // Apply filter to results
@@ -949,6 +952,44 @@ var $e5e279a608b8e6b1$export$2e2bcd8739ae039 = $e5e279a608b8e6b1$var$getManyRefe
949
952
 
950
953
 
951
954
 
955
+ const $fda69bf2752eb49a$var$generator = new (0, $bkNnK$sparqljs.Generator)();
956
+ const $fda69bf2752eb49a$var$patchMethod = (config)=>async (resourceId, params)=>{
957
+ const { httpClient: httpClient } = config;
958
+ const sparqlUpdate = {
959
+ type: "update",
960
+ prefixes: {},
961
+ updates: []
962
+ };
963
+ if (params.triplesToAdd) sparqlUpdate.updates.push({
964
+ updateType: "insert",
965
+ insert: [
966
+ {
967
+ type: "bgp",
968
+ triples: params.triplesToAdd
969
+ }
970
+ ]
971
+ });
972
+ if (params.triplesToRemove) sparqlUpdate.updates.push({
973
+ updateType: "delete",
974
+ delete: [
975
+ {
976
+ type: "bgp",
977
+ triples: params.triplesToRemove
978
+ }
979
+ ]
980
+ });
981
+ await httpClient(`${params.id}`, {
982
+ method: "PATCH",
983
+ headers: new Headers({
984
+ "Content-Type": "application/sparql-update"
985
+ }),
986
+ body: $fda69bf2752eb49a$var$generator.stringify(sparqlUpdate)
987
+ });
988
+ };
989
+ var $fda69bf2752eb49a$export$2e2bcd8739ae039 = $fda69bf2752eb49a$var$patchMethod;
990
+
991
+
992
+
952
993
  const $ceaafb56f75454f0$var$updateMethod = (config)=>async (resourceId, params)=>{
953
994
  const { httpClient: httpClient, jsonContext: jsonContext } = config;
954
995
  // Upload files, if there are any
@@ -1159,6 +1200,7 @@ const $7f6a16d0025dc83a$var$dataProvider = (config)=>{
1159
1200
  delete: waitForConfig((0, $566b5adde94810fa$export$2e2bcd8739ae039)(config)),
1160
1201
  deleteMany: waitForConfig((0, $f170294dd29d8bf8$export$2e2bcd8739ae039)(config)),
1161
1202
  // Custom methods
1203
+ patch: waitForConfig((0, $fda69bf2752eb49a$export$2e2bcd8739ae039)(config)),
1162
1204
  getDataModels: waitForConfig((0, $241c41c6f6021c7a$export$2e2bcd8739ae039)(config)),
1163
1205
  getDataServers: waitForConfig((0, $b16131432127b07b$export$2e2bcd8739ae039)(config)),
1164
1206
  getLocalDataServers: (0, $b16131432127b07b$export$2e2bcd8739ae039)(config),
@@ -1565,5 +1607,96 @@ var $030f1232f6810456$export$2e2bcd8739ae039 = $030f1232f6810456$var$Reification
1565
1607
 
1566
1608
 
1567
1609
 
1610
+ /**
1611
+ * Find the solid notification description resource for a given resource URI.
1612
+ */ const $84ab912646919f8c$var$findDescriptionResource = async (fetch, resourceUri)=>{
1613
+ const { headers: headers } = await fetch(resourceUri, {
1614
+ method: "HEAD"
1615
+ });
1616
+ const linkHeader = headers.get("Link");
1617
+ const matches = linkHeader?.match(/<([^>]+)>;\s*rel="(?:describedby|http:\/\/www\.w3\.org\/ns\/solid\/terms#storageDescription)"/);
1618
+ if (!matches?.[1]) return undefined;
1619
+ const { json: descriptionResource } = await fetch(matches[1]);
1620
+ return descriptionResource;
1621
+ };
1622
+ const $84ab912646919f8c$export$3edfe18db119b920 = async (fetch, resourceUri, options = {
1623
+ type: "WebSocketChannel2023"
1624
+ })=>{
1625
+ const { type: type, closeAfter: closeAfter, startIn: startIn, rate: rate } = options;
1626
+ let { startAt: startAt, endAt: endAt } = options;
1627
+ if (startIn && !startAt) startAt = new Date(Date.now() + startIn).toISOString();
1628
+ if (closeAfter && !endAt) endAt = new Date(Date.now() + closeAfter).toISOString();
1629
+ const descriptionResource = await $84ab912646919f8c$var$findDescriptionResource(fetch, resourceUri);
1630
+ // TODO: use a json-ld parser / ldo in the future for this...
1631
+ // Get solid notification subscription service for the given type.
1632
+ const subscriptionService = (await Promise.all(// Get the subscription service resources (that describe a channel type).
1633
+ (0, $e6fbab1f303bdb93$export$2e2bcd8739ae039)(descriptionResource.subscription || descriptionResource["notify:subscription"]).map(async (subscriptionServiceOrUri)=>{
1634
+ // They might not be resolved...
1635
+ if (typeof subscriptionServiceOrUri === "string") {
1636
+ const { json: json } = await fetch(subscriptionServiceOrUri);
1637
+ return json;
1638
+ }
1639
+ return subscriptionServiceOrUri;
1640
+ }))).find((service)=>{
1641
+ // Find for the correct channel type (e.g. web socket).
1642
+ const channelType = service.channelType ?? service["notify:channelType"];
1643
+ return channelType === type || channelType === `notify:${type}`;
1644
+ });
1645
+ if (!subscriptionService) throw new Error(`No solid notification subscription service found for type ${type}`);
1646
+ // Create a new channel.
1647
+ const { json: channel } = await fetch(subscriptionService.id || subscriptionService["@id"], {
1648
+ method: "POST",
1649
+ body: JSON.stringify({
1650
+ "@context": "https://www.w3.org/ns/solid/notification/v1",
1651
+ type: "WebSocketChannel2023",
1652
+ topic: resourceUri,
1653
+ startAt: startAt,
1654
+ endAt: endAt,
1655
+ rate: rate
1656
+ })
1657
+ });
1658
+ return channel;
1659
+ };
1660
+ const $84ab912646919f8c$export$28772ab4c256e709 = async (fetch, resourceUri, options)=>{
1661
+ const channel = await $84ab912646919f8c$export$3edfe18db119b920(fetch, resourceUri, options);
1662
+ const receiveFrom = channel.receiveFrom || channel["notify:receiveFrom"];
1663
+ return new WebSocket(receiveFrom);
1664
+ };
1665
+ const $84ab912646919f8c$var$registeredWebSockets = new Map();
1666
+ /**
1667
+ * @param fetch A react admin fetch function.
1668
+ * @param resourceUri The resource to subscribe to
1669
+ * @param options Options to pass to @see createSolidNotificationChannel, if the channel does not exist yet.
1670
+ * @returns {WebSocket} A new or existing web socket that subscribed to the given resource.
1671
+ */ const $84ab912646919f8c$export$8d60734939c59ced = async (fetch, resourceUri, options = {
1672
+ type: "WebSocketChannel2023",
1673
+ closeAfter: 3600000
1674
+ })=>{
1675
+ const socket = $84ab912646919f8c$var$registeredWebSockets.get(resourceUri);
1676
+ if (socket) // Will resolve or is resolved already.
1677
+ return socket;
1678
+ // Create a promise, to return immediately and set the sockets cache.
1679
+ // This prevents racing conditions that create multiple channels.
1680
+ const wsPromise = $84ab912646919f8c$export$28772ab4c256e709(fetch, resourceUri, {
1681
+ ...options,
1682
+ type: "WebSocketChannel2023"
1683
+ }).then((ws)=>{
1684
+ // Remove the promise from the cache, if it closes.
1685
+ ws.addEventListener("close", (e)=>{
1686
+ $84ab912646919f8c$var$registeredWebSockets.delete(resourceUri);
1687
+ });
1688
+ // Close the socket, if the endAt / closeAfter time is reached.
1689
+ const closeIn = options.closeAfter ?? (options.endAt && new Date(options.endAt).getTime() - Date.now());
1690
+ if (closeIn) setTimeout(()=>{
1691
+ ws.close();
1692
+ }, closeIn);
1693
+ return ws;
1694
+ });
1695
+ $84ab912646919f8c$var$registeredWebSockets.set(resourceUri, wsPromise);
1696
+ return wsPromise;
1697
+ };
1698
+
1699
+
1700
+
1568
1701
 
1569
1702
  //# sourceMappingURL=index.cjs.js.map