Skip to content
Open
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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\test\\fileStreamer\\index.js"
"program": "${workspaceFolder}\\test\\command\\external.ws.ws\\simple.js"
}
]
}
19 changes: 1 addition & 18 deletions Models/Connection/ConnectionInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,5 @@ export default class ConnectionInfo {
loadPageAsync(pageName, rawCommand, pageSize, domainId, cancellationToken) {
throw new Error("ConnectionInfo.loadPageAsync() method not implemented.");
}
/**
*
* @param {string} jsonString
* @returns {DataSourceCollection}
*/
convertJSONToDataSet(content) {
if (content?.sources && Array.isArray(content?.sources)) {
let retVal = [];
content.sources.forEach((source) => {
retVal.push(source.data);
});
return new DataSourceCollection(retVal);
} else {
throw new WebServerException(
"Error from Edge Connection ;the sources are not available."
);
}
}

}
20 changes: 20 additions & 0 deletions Models/Connection/EdgeConnectionInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,24 @@ export default class EdgeConnectionInfo extends ConnectionInfo {
});
});
}
/**
*
* @param {string} jsonString
* @returns {DataSourceCollection}
*/
convertJSONToDataSet(content) {
if (content?.sources && Array.isArray(content?.sources)) {
let retVal = [];
content.sources.forEach((source) => {
retVal.push(source.data);
});
return new DataSourceCollection(retVal);
} else {
throw new WebServerException(
"Error from " +
this.name +
" Connection ;the sources are not available."
);
}
}
}
89 changes: 50 additions & 39 deletions Models/Connection/SocketConnectionInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import net from "net";
import ConnectionInfo from "./ConnectionInfo.js";
import DataSourceCollection from "../../renderEngine/Source/DataSourceCollection.js";
import SocketSettingData from "./SocketSettingData.js";
import WebServerException from "../Exceptions/WebServerException.js";

/**
* @typedef {Object} LoadDataRequest
Expand All @@ -12,24 +11,69 @@ import WebServerException from "../Exceptions/WebServerException.js";
*/
export default class SocketConnectionInfo extends ConnectionInfo {
/**@type {string} */
endPoint;
host;
/**@type {string} */
port;
/**
* @param {string} name
* @param {SocketSettingData} settings
*/
constructor(name, settings) {
super(name);
this.endPoint = settings.endPoint;
[this.host, this.port] = settings.endPoint.split(":");
}

/**
* @param {NodeJS.Dict<object|string|number>} parameters
* @param {CancellationToken} cancellationToken
* @returns {Promise<DataSourceCollection>}
*/
async LoadDataAsync(cancellationToken, parameters) {
const retVal = await this.sendAsync(parameters);
return this.convertJSONToDataSet(retVal);
async loadDataAsync(parameters, cancellationToken) {
const byteMessage = parameters.hasOwnProperty("byteMessage")
? parameters["byteMessage"]
: null;
const mySocket = new net.Socket();

await new Promise((resolve, reject) => {
mySocket.connect(
{
host: this.host,
port: this.port,
},
() => {
resolve();
}
);
mySocket.on("error", (err) => {
reject(err);
});
});
const networkStream = mySocket;
if (byteMessage) {
await new Promise((resolve, reject) => {
networkStream.write(byteMessage, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
let result = await new Promise((resolve, reject) => {
/** @type {Buffer[]}*/
const buffer = [];
networkStream.on("data", (data) => buffer.push(data));
networkStream.on("error", (err) => {
reject(err);
});
networkStream.on("end", () => {
const data = Buffer.concat(buffer);
const retVal = data.toString()
resolve( new DataSourceCollection([[{ result: retVal }]]));
});
});
return result;
}
/**
* @param {Request} request
Expand All @@ -48,37 +92,4 @@ export default class SocketConnectionInfo extends ConnectionInfo {
}
return result;
}
async sendAsync(parameters) {
const byteMessage = parameters.hasOwnProperty("byteMessage")
? parameters["byteMessage"]
: null;
const mySocket = new net.Socket();
await new Promise((resolve, reject) => {
mySocket.connect(this.EndPoint, () => {
resolve();
});
mySocket.on("error", (err) => {
reject(err);
});
});
const networkStream = mySocket;
await new Promise((resolve, reject) => {
networkStream.write(byteMessage, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return await new Promise((resolve, reject) => {
networkStream.read((err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
}
Binary file added b.test.db-journal
Binary file not shown.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
"index4": "node test\\index4\\simple.js",
"unknown": "node test\\command\\UnknownCommand\\simple.js",
"fileStreamer": "node test\\fileStreamer\\index.js",
"bug": "node --trace-warnings --trace-deprecation test\\bug.js"
"bug": "node --trace-warnings --trace-deprecation test\\bug.js",
"socket-server": "node test\\connection\\socket\\app.js",
"socket": "node test\\connection\\socket\\connection.js",
"ws": "node test\\command\\external.ws.ws\\simple.js"
},
"type": "module",
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions renderEngine/Command/Source/BaseClasses/SourceCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class SourceCommand extends CommandBase {
async _executeCommandAsync(context) {
if (this.members?.items.length > 0) {
const name = await this.name.getValueAsync(context);
const dataSet = await this.#loadDataAsync(name, context);
const dataSet = await this._loadDataAsync(name, context);
context.cancellation.throwIfCancellationRequested();
if (dataSet.items.length != this.members.items.length) {
throw new BasisCoreException(
Expand All @@ -63,7 +63,7 @@ export default class SourceCommand extends CommandBase {
* @param {IContext} context
* @returns {Promise<DataSourceCollection>}
*/
async #loadDataAsync(sourceName, context) {
async _loadDataAsync(sourceName, context) {
const [connectionName, command, paramList] = await Promise.all([
this.connectionName.getValueAsync(context),
this.toCustomFormatHtmlAsync(context),
Expand Down
45 changes: 45 additions & 0 deletions renderEngine/Command/Source/BaseClasses/WsMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import IContext from "../../../Context/IContext.js";
import Member from "./Member.js";
import CommandElement from "../../CommandElement.js";
import TokenUtil from "../../../Token/TokenUtil.js";

export default class WsMember extends Member {
/** @type {IToken} */
method;
/**@type {IToken} */
query;
/**
* @param {object} memberIL
*/
constructor(memberIL) {
super(memberIL);
this.method = TokenUtil.getFiled(memberIL, "method");
this.query = TokenUtil.getFiled(memberIL, "query");
}

/**
* @param {IContext} context
* @returns {Promise<CommandElement>}
*/
async createHtmlElementAsync(context) {
const tag = new CommandElement("member");
tag.addAttributeIfExist("name", this.name);
await Promise.all([
tag.addAttributeIfExistAsync("preview", this.preview, context),
tag.addAttributeIfExistAsync("sort", this.sort, context),
tag.addAttributeIfExistAsync("method", this.method, context),
tag.addAttributeIfExistAsync("query", this.query, context),
]);
if (this.extraAttributes) {
if (this.extraAttributes) {
await Promise.all(
Object.entries(this.extraAttributes).map((pair) =>
tag.addAttributeIfExistAsync(pair[0], pair[1], context)
)
);
}
}
await tag.addRawContentIfExistAsync(this.rawContent, context);
return tag;
}
Comment on lines +24 to +44

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use and extend (override) base class implementation of createHtmlElementAsync() for remove existing part of method body.

}
19 changes: 19 additions & 0 deletions renderEngine/Command/Source/BaseClasses/WsMemberCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import JoinMember from "./JoinMember.js";
import Member from "./Member.js";
import MemberCollection from "./MemberCollection.js";
import WsMember from "./wsmember.js";

export default class WSMemberCollection extends MemberCollection {
/** @param {object[]} membersIl */
constructor(membersIl) {
super(membersIl);
}

/**
* @param {object} memberIl
* @returns {Member}
*/
_createMember(memberIl) {
return new WsMember(memberIl);
}
}
1 change: 0 additions & 1 deletion renderEngine/Command/Source/InlineSourceCommand.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import VoidResult from "../../Models/VoidResult.js";
import { InMemoryMemberCollection } from "./BaseClasses/InMemoryMemberCollection.js";
import SourceCommand from "./BaseClasses/SourceCommand.js";
import IToken from "../../Token/IToken.js";
import ICommandResult from "../../Models/ICommandResult.js";
import IContext from "../../Context/IContext.js";
export default class InlineSourceCommand extends SourceCommand {
Expand Down
19 changes: 15 additions & 4 deletions renderEngine/Command/Source/ws.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import SourceCommand from "./BaseClasses/SourceCommand.js";
import ICommandResult from "../../Models/ICommandResult.js";
import VoidResult from "../../Models/VoidResult.js";
import IContext from "../../Context/IContext.js";
import DataSourceCollection from "../../Source/DataSourceCollection.js";
import WSMemberCollection from "./BaseClasses/WsMemberCollection.js";
import MemberCollection from "./BaseClasses/MemberCollection.js";
export default class WsCommand extends SourceCommand {
/**
* @param {object} wsIl
*/
constructor(wsIl) {
super(wsIl);
}

/**
* @param {object[]} membersIl
* @returns {MemberCollection}
*/
createMemberCollection(membersIl) {
return new WSMemberCollection(membersIl);
}
/**
* @param {string} sourceName
* @param {IContext} context
* @returns {Promise<ICommandResult>}
* @returns {Promise<DataSourceCollection>}
*/
async #loadDataAsync(sourceName, context) {
async _loadDataAsync(sourceName, context) {
const [connectionName, command] = await Promise.all([
this.connectionName.getValueAsync(context),
this.toCustomFormatHtmlAsync(context),
Expand All @@ -25,7 +36,7 @@ export default class WsCommand extends SourceCommand {
const encoder = new TextEncoder();
const byteMessage = encoder.encode(JSON.stringify(inputs));
const parameters = {
byteMessage,
byteMessage: byteMessage,
};
return await context.loadDataAsync(sourceName, connectionName, parameters);
}
Expand Down
5 changes: 5 additions & 0 deletions test/command/CommandUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import UnknownCommand from "../../renderEngine/Command/UnknownCommand.js";
import RepeaterCommand from "../../renderEngine/Command/Collection/RepeaterCommand.js";
import CookieCommand from "../../renderEngine/Command/CookieCommand.js";
import ClientComponent from "../../renderEngine/Command/ClientComponent.js";
import WsCommand from "../../renderEngine/Command/Source/ws.js";

export default class CommandUtil {
/**
Expand Down Expand Up @@ -76,6 +77,10 @@ export default class CommandUtil {
retVal = new ClientComponent(commandIl);
break;
}
case "external.ws.ws":{
retVal = new WsCommand(commandIl)
break
}
default: {
retVal = new UnknownCommand(commandIl);
break;
Expand Down
Loading