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

Add new examples: Chat API and websocket api #87

Draft
wants to merge 2 commits 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
37 changes: 37 additions & 0 deletions javascript/chat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { createServer } = require("@scramjet/api-server");


/** @type {import("@scramjet/types").ReadableApp} */
module.exports = [
{ requires: "chat-input" },
/** @this {import("@scramjet/types").AppContext} */
async function* (input, queueSize = "1000") {
this.logger.info("Starting chat server...");

const api = createServer();
const messages = [];
const output = Object.assign(new DataStream(), { contentType: "text/x-ndjson" });

input.on("data", (data) => {
messages.unshift({timestamp: Date.now(), data});
if (messages.length > queueSize) {
messages.length = queueSize;
}
});

api.get("/input", (req) => {
if (req.query.since) {
const since = +new Date(req.query.since);
const filtered = messages.filter((msg) => msg.timestamp > since);
return filtered.map((msg) => msg.data);
}
return messages.map((msg) => msg.data);
});
api.op("POST", "/output", (req) => {
output.write(messages[0]);
});
api.server.listen(1234);

return output;
}
]
201 changes: 201 additions & 0 deletions javascript/chat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions javascript/chat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@scramjet/js-transformer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"predeploy": "mkdir -p dist/ && cp index.js package.json dist/ && (cd dist && npm i --omit=dev)"
},
"engines": {
"node": ">=16"
},
"repository": {
"type": "git",
"url": "git+https://github.com/scramjetorg/create-sequence.git"
},
"bugs": {
"url": "https://github.com/scramjetorg/create-sequence/issues"
},
"homepage": "https://github.com/scramjetorg/create-sequence#readme",
"author": "",
"license": "ISC",
"dependencies": {
"@scramjet/api-server": "^0.39.3"
},
"devDependencies": {
"@scramjet/types": "^0.39.3"
}
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@scramjet/platform-samples",
"version": "1.0.1",
"private": true,
"description": "A repo with samples for the @scramjet/sth package",
"scripts": {
"postinstall": "cd scripts && yarn",
"build:packages": "scripts/run-script.js -v -p typescript -p python -p javascript build",
"cloc": "cloc . --fullpath --include-lang TypeScript,JavaScript,Python,Dockerfile,Markdown --match-d=\"(packages|bdd/step-definitions|bdd/lib)\" --not-match-d \"(node_modules|test|dist)\" --by-percent cmb",
"clean": "yarn clean:root && scripts/run-script.js clean",
"clean:root": "rm -rf ./dist/"
},
"author": "Scramjet <[email protected]>",
"license": "AGPL-3.0",
"repository": {
"type": "git",
"url": "https://github.com/scramjetorg/platform-samples.git"
},
"husky": {
"hooks": {
"pre-push": "yarn lint"
}
},
"dependencies": {
"minimist": "^1.2.7",
"npm": "8",
"scramjet": "^4.36.9"
}
}
Loading
Loading