Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skipsocial #431

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"examples/hackernews/front-end/"
],
"devDependencies": {
"@types/node": "^22.6.0",
"@skiplabs/eslint-config": "^1.0.0",
"@skiplabs/tsconfig": "^1.0.0",
"@types/node": "^22.6.0",
"@types/uuid": "^10.0.0",
"eslint": "^9.9.0",
"prettier": "^3.3.3",
"typescript": "^5.6.2"
Expand All @@ -31,5 +32,8 @@
"clean": "npm run clean --workspaces --if-present",
"lint": "npm run lint --workspaces --if-present",
"test": "npm run test --workspaces --if-present"
},
"dependencies": {
"uuid": "^10.0.0"
}
}
8 changes: 7 additions & 1 deletion skiplang/prelude/src/skstore/Context.sk
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,13 @@ mutable class Context{
| Some(child @ EagerDir _) ->
this.!toReset = this.toReset.set(parentName);
parentMaps = child.parents.maybeGet(parentName) match {
| None() -> invariant_violation("Could not find parent")
| None() ->
invariant_violation(
"Could not find parent: " +
parentName +
" for child " +
childName,
)
| Some(f) -> f
};
EagerDir::update(
Expand Down
2 changes: 2 additions & 0 deletions skipruntime-ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type {
Creds,
};

import WebSocket from "ws";

import * as Protocol from "./protocol.js";

export { Protocol };
Expand Down
247 changes: 247 additions & 0 deletions skipruntime-ts/examples/skipsocial-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
import {
type ReactiveResponse,
} from "@skipruntime/api";
import {
fetchJSON,
} from "@skipruntime/helpers/rest.js";
import {
parseReactiveResponse,
} from "@skipruntime/helpers";
import { connect, Protocol } from "@skipruntime/client";
import type { TJSON } from "@skipruntime/client/protocol.js";
import { newID } from "./skipsocial-utils.js";

/*
This is the client simulator of skipsocial example
*/

/*
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
*/
const replication = 8081;
const port = 8082;
const url = `http://localhost:${port.toString()}`;
const creds = await Protocol.generateCredentials();

const publicKey = new Uint8Array(await Protocol.exportKey(creds.publicKey));

const header = {
"X-Reactive-Auth": Buffer.from(publicKey.buffer).toString("base64"),
};
const [_e, headers] = await fetchJSON<ReactiveResponse>(
`${url}/auth/users`,
"HEAD",
header,
);

const reactive = parseReactiveResponse(headers);

if (!reactive) {
throw new Error("Reactive response must be supplied.");
}

const client = await connect(`ws://localhost:${replication.toString()}`, creds);

client.subscribe(
reactive.collection,
reactive.watermark,
(updates: [string, TJSON[]][], isInit: boolean) => {
console.log("Update", Object.fromEntries(updates), isInit);
},
);

/*****************************************************************************/
// Testing friend requests
/*****************************************************************************/

const userID1 = "users-1";
const userID2 = "users-2";
const userID3 = "users-3";

await fetchJSON(`${url}/user/${userID1}`, "PUT", {}, { country: "FR" });

await fetchJSON(`${url}/user/${userID2}`, "PUT", {}, { country: "UK" });

await fetchJSON(`${url}/user/${userID3}`, "PUT", {}, { country: "UK" });

await fetchJSON(
`${url}/friend-request/${newID("friendRequests")}`,
"PUT",
{},
{ from: userID2, to: userID1 },
);

await fetchJSON(
`${url}/friend-request/${newID("friendRequests")}`,
"PUT",
{},
{ from: userID1, to: userID2 },
);

await fetchJSON(
`${url}/friend-request/${newID("friendRequests")}`,
"PUT",
{},
{ from: userID3, to: userID1 },
);

await fetchJSON(
`${url}/friend-request/${newID("friendRequests")}`,
"PUT",
{},
{ from: userID1, to: userID3 },
);

const friends2 = await fetchJSON(`${url}/friend/${userID1}`, "GET", header);
const areFriends = await fetchJSON(
`${url}/friend-index/${userID1}:${userID2}`,
"GET",
header,
);

console.log(friends2);
console.log(areFriends);

/*****************************************************************************/
/* Testing messages */
/*****************************************************************************/

await fetchJSON(
`${url}/message/${newID("messages")}`,
"PUT",
{},
{ from: userID3, to: userID1, content: "Hello from userID3" },
);

await fetchJSON(
`${url}/message/${newID("messages")}`,
"PUT",
{},
{ from: userID2, to: userID1, content: "Hello from userID2" },
);

await fetchJSON(
`${url}/message/${newID("messages")}`,
"PUT",
{},
{ from: userID2, to: userID1, content: "Another hello from userID2" },
);

const params = new URLSearchParams({
userID: userID1,
});

console.log(
await fetchJSON(`${url}/messages/${userID1}?${params}`, "GET", header),
);

/*****************************************************************************/
/* Testing groups */
/*****************************************************************************/

/*
const groupID = "groupid-1";

await fetchJSON(
`${url}/group/${groupID}`,
"PUT",
{},
{ name: "this is my first group!", owner: userID1 },
);

await fetchJSON(
`${url}/group-member/${groupID}`,
"PUT",
{},
{ groupID, userID: userID2 },
);

*/

/*
await fetchJSON(
`${url}/message/${userID1}`,
"PUT",
{},
{ from: 'id-user-2', content: "Salut!"},
);

console.log(await fetchJSON(
`${url}/friend/${userID2}`,
"GET",
header,
));


console.log(await fetchJSON(
`${url}/group/${groupID}`,
"GET",
header,
));
*/

/*
const postID = "post-816"
const commentID = "comment-817"

await fetchJSON(
`${url}/post/${postID}`,
"PUT",
{},
{ content: "First post from julien!", userID: "123" },
);
console.log("PUT POST DONE");

console.log("FETCH" + await fetchJSON(
`${url}/post/${postID}`,
"GET",
header,
));
console.log("FETCH POST DONE");

await fetchJSON(
`${url}/comment/${commentID}`,
"PUT",
{},
{ postID, content: "First comment from julien!", userID: "123" },
);

console.log("PUT COMMENT DONE");



await sleep(1000);


console.log(
"Get /post/" + postID,
(await fetchJSON(`${url}/post/${postID}`, "GET", header))[0],
);
*/

/*

console.log('Set /user/123 to { name: "daniel", country: "UK" }');

await fetchJSON(
`${url}/user/123`,
"PUT",
{},
{ name: "daniel", country: "UK" },
);

await sleep(1000);
console.log("Delete /user/123");

await fetchJSON(`${url}/user/123`, "DELETE", {});

console.log(
"Get /user/123",
(await fetchJSON(`${url}/user/123`, "GET", header))[0],
);

await sleep(1000);
*/
client.close();
Loading