Skip to content

Commit

Permalink
Implements client-side http transport
Browse files Browse the repository at this point in the history
* Adds support of node 20 to package.json
  • Loading branch information
rgbabaev committed Sep 1, 2024
1 parent 9e894c4 commit e2ee3c2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 44 deletions.
49 changes: 33 additions & 16 deletions JavaScript/9-logger/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const http = require('node:http');

const answerNotFound = (res, headers) => {
res.writeHead(404, headers);
res.end('Not found');
};

const receiveArgs = async (req) => {
const buffers = [];
for await (const chunk of req) buffers.push(chunk);
Expand All @@ -10,22 +15,34 @@ const receiveArgs = async (req) => {
};

module.exports = (routing, port, console) => {
http.createServer(async (req, res) => {
const { url, socket } = req;
const [name, method, id] = url.substring(1).split('/');
const entity = routing[name];
if (!entity) return void res.end('Not found');
const handler = entity[method];
if (!handler) return void res.end('Not found');
const src = handler.toString();
const signature = src.substring(0, src.indexOf(')'));
const args = [];
if (signature.includes('(id')) args.push(id);
if (signature.includes('{')) args.push(await receiveArgs(req));
console.log(`${socket.remoteAddress} ${method} ${url}`);
const result = await handler(...args);
res.end(JSON.stringify(result.rows));
}).listen(port);
http
.createServer(async (req, res) => {
const headers = {
'Access-Control-Allow-Origin':
'*' /* @dev First, read about security */,
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Max-Age': 2592000, // 30 days
/** add other headers as per requirement */
};

const { url, socket } = req;
const [name, method, id] = url.substring(1).split('/');
const entity = routing[name];
if (!entity) return void answerNotFound(res, headers);
const handler = entity[method];
if (!handler) return void answerNotFound(res, headers);

res.writeHead(200, headers);
const src = handler.toString();
const signature = src.substring(0, src.indexOf(')'));
const args = [];
if (signature.includes('(id')) args.push(id);
args.push(...(await receiveArgs(req)));
console.log(`${socket.remoteAddress} ${req.method} ${url}`);
const result = await handler(...args);
res.end(JSON.stringify(result.rows));
})
.listen(port);

console.log(`API on port ${port}`);
};
2 changes: 1 addition & 1 deletion JavaScript/9-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"main": "main.js",
"engines": {
"node": "14 || 16 || 18"
"node": "14 || 16 || 18 || 20"
},
"scripts": {
"start": "node --experimental-vm-modules main.js"
Expand Down
92 changes: 65 additions & 27 deletions JavaScript/9-logger/static/client.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,82 @@
'use strict';

const socket = new WebSocket('ws://127.0.0.1:8001/');
const HTTP_API_URL = 'http://localhost:8001';
let socket;

const scaffold = (structure) => {
const transports = {
ws: (name, method) => {
if (!socket) socket = new WebSocket('ws://127.0.0.1:8001/');
return (...args) => {
return new Promise((resolve) => {
const packet = { name, method, args };
socket.send(JSON.stringify(packet));
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
resolve(data);
};
});
};
},

http:
(name, method, methodArgs) =>
(...args) => {
const urlParts = [HTTP_API_URL, name, method];
if (methodArgs[0] === 'id') urlParts.push(args.shift());

return fetch(urlParts.join('/'), {
method: 'POST',
body: JSON.stringify(args),
}).then((response) => response.json());
},
};

const scaffold = (structure, transport) => {
const api = {};
const services = Object.keys(structure);
for (const serviceName of services) {
api[serviceName] = {};
const service = structure[serviceName];
const methods = Object.keys(service);
for (const methodName of methods) {
api[serviceName][methodName] = (...args) => new Promise((resolve) => {
const packet = { name: serviceName, method: methodName, args };
socket.send(JSON.stringify(packet));
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
resolve(data);
};
});
api[serviceName][methodName] = transport(
serviceName,
methodName,
service[methodName],
);
}
}
return api;
};

const api = scaffold({
user: {
create: ['record'],
read: ['id'],
update: ['id', 'record'],
delete: ['id'],
find: ['mask'],
const api = scaffold(
{
user: {
create: ['record'],
read: ['id'],
update: ['id', 'record'],
delete: ['id'],
find: ['mask'],
},
city: {
read: ['id'],
create: ['record'],
update: ['id', 'record'],
delete: ['id'],
},
country: {
read: ['id'],
create: ['record'],
update: ['id', 'record'],
delete: ['id'],
},
},
country: {
read: ['id'],
delete: ['id'],
find: ['mask'],
},
});
transports.http,
);

// socket.addEventListener('open', async () => {
// const data = await api.user.read(3);
// console.dir({ data });
// });

socket.addEventListener('open', async () => {
const data = await api.user.read(3);
console.dir({ data });
});
(async () => console.log(await api.user.read(3)))();

0 comments on commit e2ee3c2

Please sign in to comment.