Skip to content

Commit

Permalink
[discovery] first draft of DiscoveryClient
Browse files Browse the repository at this point in the history
  • Loading branch information
elf-pavlik committed Jun 13, 2023
1 parent eea36e0 commit 8d9e5ac
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/discovery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
"jest": "^29.5.0",
"solid-test-utils": "^0.0.0",
"ts-jest": "^29.1.0"
},
"dependencies": {
"@janeirodigital/interop-utils": "^1.0.0-rc.20",
"n3": "^1.16.4"
}
}
81 changes: 81 additions & 0 deletions packages/discovery/src/client.ts
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'))
}
}
8 changes: 0 additions & 8 deletions packages/discovery/src/getDescribedByUri.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/discovery/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./getDescribedByUri";
export * from "./client";
7 changes: 6 additions & 1 deletion packages/types/src/NotificationChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export enum ChannelType {
WebhookChannel2023 = 'http://www.w3.org/ns/solid/notifications#WebhookChannel2023',
WebSocketChannel2023 = 'http://www.w3.org/ns/solid/notifications#WebSocketChannel2023',
}

export interface NotificationChannel {
id: string;
type: string;
type: ChannelType; // TODO channel types should be extendible
topic: string | string[];
receiveFrom: string;
sendTo?: string;
Expand Down

0 comments on commit 8d9e5ac

Please sign in to comment.