-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[discovery] first draft of DiscoveryClient
- Loading branch information
1 parent
eea36e0
commit 8d9e5ac
Showing
5 changed files
with
92 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { DataFactory } from 'n3' | ||
import { parseTurtle, getDescriptionResource, getOneMatchingQuad, getAllMatchingQuads, NOTIFY } from '@janeirodigital/interop-utils' | ||
import type { DatasetCore } from '@rdfjs/types' | ||
import { ChannelType, SubscriptionService } from '@solid-notifications/types' | ||
|
||
// TODO remove once fetching from CSS | ||
const snippet = ` | ||
@prefix notify: <http://www.w3.org/ns/solid/notifications#>. | ||
<http://localhost:3000/.well-known/solid> | ||
a <http://www.w3.org/ns/pim/space#Storage> ; | ||
notify:subscription <http://localhost:3000/.notifications/WebSocketChannel2023/> , | ||
<http://localhost:3000/.notifications/WebhookChannel2023/> . | ||
<http://localhost:3000/.notifications/WebSocketChannel2023/> | ||
notify:channelType notify:WebSocketChannel2023 ; | ||
notify:feature notify:accept , | ||
notify:endAt , | ||
notify:rate , | ||
notify:startAt , | ||
notify:state . | ||
<http://localhost:3000/.notifications/WebhookChannel2023/> | ||
notify:channelType notify:WebhookChannel2023; | ||
notify:feature notify:accept , | ||
notify:endAt , | ||
notify:rate , | ||
notify:startAt , | ||
notify:state . | ||
` | ||
|
||
export class DiscoveryClient { | ||
|
||
private parser: { | ||
contentType: 'text/turtle' | 'application/ld+json', | ||
parse: typeof parseTurtle | ||
} | ||
constructor(private authnFetch: typeof fetch) { | ||
// TODO pass Turtle or JSON-LD parser and set accordignly | ||
this.parser = { | ||
contentType: 'text/turtle', | ||
parse: parseTurtle | ||
} | ||
} | ||
|
||
async findService(resourceUri: string, channelType: ChannelType): Promise<SubscriptionService | null> { | ||
|
||
// const storageDescription = this.fetchStorageDescription(resourceUri) | ||
|
||
const storageDescription = await parseTurtle(snippet) | ||
|
||
// TODO handle multiple matching services | ||
const serviceNode = getOneMatchingQuad(storageDescription, null, NOTIFY.channelType, DataFactory.namedNode(channelType))?.subject | ||
if (!serviceNode) return null | ||
const features = getAllMatchingQuads(storageDescription, serviceNode, NOTIFY.feature).map(quad => quad.object.value) | ||
return { | ||
id: serviceNode.value, | ||
channelType, | ||
feature: features | ||
} | ||
} | ||
|
||
// TODO use some rdf-fetch util | ||
async fetchStorageDescription(resourceUri): Promise<DatasetCore> { | ||
const storageDescriptionUri = await this.discoverStorageDescription(resourceUri) | ||
const response = await this.authnFetch(storageDescriptionUri, { | ||
headers: { | ||
'Accept': this.parser.contentType | ||
} | ||
}) | ||
return this.parser.parse(await response.text(), response.url) | ||
} | ||
|
||
async discoverStorageDescription(resourceUri: string): Promise<string> { | ||
const response = await this.authnFetch(resourceUri, { method: 'head' }) | ||
return getDescriptionResource(response.headers.get('Link')) // TODO add getStorageDescription to utils | ||
} | ||
|
||
async discoverResourceDescription(resourceUri: string): Promise<string> { | ||
const response = await this.authnFetch(resourceUri, { method: 'head' }) | ||
return getDescriptionResource(response.headers.get('Link')) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./getDescribedByUri"; | ||
export * from "./client"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters