generated from argahsuknesib/TS-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds:the first version of the Notifications Cache Server
- Loading branch information
1 parent
5a04e8e
commit 9c726af
Showing
12 changed files
with
805 additions
and
69 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
Large diffs are not rendered by default.
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
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
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,91 @@ | ||
import { SubscribeNotification } from './SubscribeNotification'; | ||
import { extract_ldp_inbox, extract_subscription_server } from '../utils/Util'; | ||
|
||
jest.mock('../utils/Util', () => ({ | ||
extract_ldp_inbox: jest.fn(), | ||
extract_subscription_server: jest.fn(), | ||
})); | ||
|
||
describe('SubscribeNotification', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should subscribe successfully', async () => { | ||
const streams = ['stream1', 'stream2']; | ||
const subscribeNotification = new SubscribeNotification(streams); | ||
|
||
(extract_ldp_inbox as jest.Mock).mockResolvedValueOnce('inbox1'); | ||
(extract_subscription_server as jest.Mock).mockResolvedValueOnce({ | ||
location: 'http://subscription-server1', | ||
}); | ||
|
||
const fetchMock = jest.fn().mockResolvedValueOnce({ status: 200 }); | ||
global.fetch = fetchMock; | ||
|
||
const result = await subscribeNotification.subscribe(); | ||
|
||
expect(result).toBe(true); | ||
expect(extract_ldp_inbox).toHaveBeenCalledWith('stream1'); | ||
expect(extract_subscription_server).toHaveBeenCalledWith('inbox1'); | ||
expect(fetchMock).toHaveBeenCalledWith('http://subscription-server1', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/ld+json', | ||
}, | ||
body: JSON.stringify({ | ||
'@context': ['https://www.w3.org/ns/solid/notification/v1'], | ||
type: 'http://www.w3.org/ns/solid/notifications#WebhookChannel2023', | ||
topic: 'inbox1', | ||
sendTo: 'http://localhost:8085/', | ||
}), | ||
}); | ||
}); | ||
|
||
it('should throw an error if subscription server is undefined', async () => { | ||
const streams = ['stream1']; | ||
const subscribeNotification = new SubscribeNotification(streams); | ||
|
||
(extract_ldp_inbox as jest.Mock).mockResolvedValueOnce('inbox1'); | ||
(extract_subscription_server as jest.Mock).mockResolvedValueOnce(undefined); | ||
|
||
await expect(subscribeNotification.subscribe()).rejects.toThrow( | ||
'Subscription server is undefined.' | ||
); | ||
|
||
expect(extract_ldp_inbox).toHaveBeenCalledWith('stream1'); | ||
expect(extract_subscription_server).toHaveBeenCalledWith('inbox1'); | ||
}); | ||
|
||
it('should throw an error if subscription to the notification server fails', async () => { | ||
const streams = ['stream1']; | ||
const subscribeNotification = new SubscribeNotification(streams); | ||
|
||
(extract_ldp_inbox as jest.Mock).mockResolvedValueOnce('inbox1'); | ||
(extract_subscription_server as jest.Mock).mockResolvedValueOnce({ | ||
location: 'http://subscription-server1', | ||
}); | ||
|
||
const fetchMock = jest.fn().mockResolvedValueOnce({ status: 500 }); | ||
global.fetch = fetchMock; | ||
|
||
await expect(subscribeNotification.subscribe()).rejects.toThrow( | ||
'The subscription to the notification server failed.' | ||
); | ||
|
||
expect(extract_ldp_inbox).toHaveBeenCalledWith('stream1'); | ||
expect(extract_subscription_server).toHaveBeenCalledWith('inbox1'); | ||
expect(fetchMock).toHaveBeenCalledWith('http://subscription-server1', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/ld+json', | ||
}, | ||
body: JSON.stringify({ | ||
'@context': ['https://www.w3.org/ns/solid/notification/v1'], | ||
type: 'http://www.w3.org/ns/solid/notifications#WebhookChannel2023', | ||
topic: 'inbox1', | ||
sendTo: 'http://localhost:8085/', | ||
}), | ||
}); | ||
}); | ||
}); |
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,24 +1,51 @@ | ||
export class SubscribeNotification { | ||
public solid_pod_urls: string[]; | ||
import { extract_ldp_inbox, extract_subscription_server } from "../utils/Util"; | ||
|
||
constructor(solid_pod_urls: string[]) { | ||
this.solid_pod_urls = solid_pod_urls; | ||
/** | ||
* This class is used to subscribe to the notification server for real-time notifications. | ||
* @class SubscribeNotification | ||
*/ | ||
export class SubscribeNotification { | ||
private ldes_streams: string[]; | ||
/** | ||
* Creates an instance of SubscribeNotification. | ||
* @param {string[]} streams - An array of LDES streams to subscribe to, for real-time notifications. | ||
* @memberof SubscribeNotification | ||
*/ | ||
constructor(streams: string[]) { | ||
this.ldes_streams = streams; | ||
} | ||
|
||
public async subscribe() { | ||
console.log(`Subscribing to notifications for ${this.solid_pod_urls}...`); | ||
|
||
/** | ||
* Subscribes to the notification server for each LDES stream in the constructor, using the inbox and subscription server. | ||
* @returns {(Promise<boolean | undefined>)} - Returns a promise with a boolean or undefined. If the subscription is successful, it returns true. If the subscription fails, it throws an error. | ||
* @memberof SubscribeNotification | ||
*/ | ||
public async subscribe(): Promise<boolean | undefined> { | ||
for (const stream of this.ldes_streams) { | ||
const inbox = await extract_ldp_inbox(stream) as string; | ||
const subscription_server = await extract_subscription_server(inbox); | ||
if (subscription_server === undefined) { | ||
throw new Error("Subscription server is undefined."); | ||
} else { | ||
const response = await fetch(subscription_server.location, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/ld+json' | ||
}, | ||
body: JSON.stringify({ | ||
"@context": ["https://www.w3.org/ns/solid/notification/v1"], | ||
"type": "http://www.w3.org/ns/solid/notifications#WebhookChannel2023", | ||
"topic": inbox, | ||
"sendTo": "http://localhost:8085/" | ||
}) | ||
}); | ||
if (response.status === 200) { | ||
return true; | ||
} | ||
else { | ||
throw new Error("The subscription to the notification server failed."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
/** | ||
* sending the server `http://localhost:3000/.notifications/WebhookChannel2023/` | ||
{ | ||
"@context": [ "https://www.w3.org/ns/solid/notification/v1" ], | ||
"type": "http://www.w3.org/ns/solid/notifications#WebhookChannel2023", | ||
"topic": "http://localhost:3000/aggregation_pod/", | ||
"sendTo": "http://localhost:3001/" | ||
} | ||
*/ |
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,7 @@ | ||
export type SubscriptionServerNotification = { | ||
location: string, | ||
channelType: string, | ||
channelLocation: string | ||
} | ||
|
||
export type RedisStatus = "wait" | "reconnecting" | "connecting" | "connect" | "ready" | "close" | "end"; |
Oops, something went wrong.