-
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 cdc40f8
Showing
8 changed files
with
139 additions
and
27 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,66 @@ | ||
import { DataFactory } from 'n3' | ||
import { parseTurtle, getDescriptionResource, getOneMatchingQuad, getAllMatchingQuads, NOTIFY, getStorageDescription } from '@janeirodigital/interop-utils' | ||
import type { DatasetCore } from '@rdfjs/types' | ||
import { ChannelType, SubscriptionService } from '@solid-notifications/types' | ||
|
||
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 = await this.fetchStorageDescription(resourceUri) | ||
console.log([...storageDescription]) | ||
|
||
// 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 fetchResource(resourceUri): Promise<DatasetCore> { | ||
const response = await this.authnFetch(resourceUri, { | ||
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 getStorageDescription(response.headers.get('Link')) | ||
} | ||
|
||
async fetchStorageDescription(resourceUri): Promise<DatasetCore> { | ||
const storageDescriptionUri = await this.discoverStorageDescription(resourceUri) | ||
return this.fetchResource(storageDescriptionUri) | ||
} | ||
|
||
async discoverDescriptionResource(resourceUri: string): Promise<string> { | ||
const response = await this.authnFetch(resourceUri, { method: 'head' }) | ||
return getDescriptionResource(response.headers.get('Link')) | ||
} | ||
|
||
async fetchDescriptionResource(resourceUri): Promise<DatasetCore> { | ||
const resourceDescriptionUri = await this.discoverDescriptionResource(resourceUri) | ||
return this.fetchResource(resourceDescriptionUri) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import SolidTestUtils from "solid-test-utils"; | ||
import { DiscoveryClient } from "../src/client"; | ||
import { DC, NOTIFY } from "@janeirodigital/interop-utils"; | ||
|
||
describe("discovery", () => { | ||
const stu = new SolidTestUtils(); | ||
beforeAll(async () => stu.beforeAll()); | ||
afterAll(async () => stu.afterAll()); | ||
|
||
const cardUri = "http://localhost:3001/example/profile/card" | ||
|
||
test("discoverStorageDescription", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
const storageDescriptionUri = await client.discoverStorageDescription(cardUri) | ||
expect(storageDescriptionUri).toBe('http://localhost:3001/example/.well-known/solid'); | ||
}); | ||
|
||
test("fetchStorageDescription", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
const dataset = await client.fetchStorageDescription(cardUri) | ||
expect(dataset.match(null, NOTIFY.subscription, NOTIFY.WebhookChannel2023)).toBeTruthy() | ||
}); | ||
|
||
test("discoverDescriptionResource", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
const resourceDescriptionUri = await client.discoverDescriptionResource(cardUri) | ||
expect(resourceDescriptionUri).toBe('http://localhost:3001/example/profile/card.meta'); | ||
}); | ||
|
||
test("fetchDescriptionResource", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
const dataset = await client.fetchDescriptionResource(cardUri) | ||
expect(dataset.match(null, DC.modified)).toBeTruthy() | ||
}); | ||
|
||
test("find Webhook service", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
const service = await client.findService(cardUri, NOTIFY.WebhookChannel2023.value) | ||
expect(service).toEqual(expect.objectContaining({ | ||
id: 'http://localhost:3001/.notifications/WebhookChannel2023/', | ||
channelType: NOTIFY.WebhookChannel2023.value | ||
})) | ||
}); | ||
|
||
test("find Web Socket service", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
const service = await client.findService(cardUri, NOTIFY.WebSocketChannel2023.value) | ||
expect(service).toEqual(expect.objectContaining({ | ||
id: 'http://localhost:3001/.notifications/WebSocketChannel2023/', | ||
channelType: NOTIFY.WebSocketChannel2023.value | ||
})) | ||
}); | ||
|
||
test("find non existing service", async () => { | ||
const client = new DiscoveryClient(stu.authFetch) | ||
//@ts-ignore | ||
const service = await client.findService(cardUri, 'https://fake.example/SomeChannel') | ||
expect(service).toBeNull() | ||
}); | ||
}); |
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
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