Compare commits

..

4 Commits

2 changed files with 30 additions and 15 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2022 Lume Web
Copyright (c) 2022 Hammer Technologies LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -445,7 +445,7 @@ async function createProtomuxChannel(aq: ActiveQuery) {
}
return (...args: any) => {
args = args.filter(
(item: any) => item.constructor.name.toLowerCase() !== "channel"
(item: any) => item?.constructor.name.toLowerCase() !== "channel"
);
if (name === "destroy") {
@ -464,14 +464,7 @@ async function createProtomuxChannel(aq: ActiveQuery) {
};
};
aq.setReceiveUpdate?.((data: any) => {
switch (data.action) {
case "open":
channel.open();
}
});
const channel = mux.createChannel({
let channel = mux.createChannel({
protocol: data?.protocol,
id: data?.id,
handshake: data?.handshake,
@ -485,6 +478,8 @@ async function createProtomuxChannel(aq: ActiveQuery) {
return;
}
channel.open();
const channelId = getChannelId();
connections.get(aq.callerInput.id)?.channels.set(channelId, channel);
@ -535,9 +530,11 @@ async function createProtomuxMessage(aq: ActiveQuery) {
});
const ret = await defers[action]?.promise;
if (ret[1]) {
args[0].buffer = Buffer.from(ret[1].buffer);
if (ret[1].buffer) {
args[0].buffer = b4a.from(ret[1].buffer);
}
args[0].start = ret[1].start;
args[0].end = ret[1].end;
}
@ -553,18 +550,34 @@ async function createProtomuxMessage(aq: ActiveQuery) {
return update("encode", args);
},
async decode(...args: any) {
return update("encode", args);
return update("decode", args);
},
};
};
aq.setReceiveUpdate?.((data) => {
defers[data.action]?.resolve(data.args[0]);
if (data.action === "send") {
message.send(...data.args);
}
defers[data.action]?.resolve(data.args);
});
if (data.onmessage) {
data.onmessage = (...args: any) => {
args = args.filter(
(item: any) => item?.constructor.name.toLowerCase() !== "channel"
);
aq.sendUpdate({
action: "onmessage",
args,
});
};
}
const message = channel.addMessage({
encoding: handleEncoding(data.encoding ?? false),
onmessage: data.encoding ?? undefined,
onmessage: data.onmessage ?? noop,
});
aq.sendUpdate({
@ -591,3 +604,5 @@ function getSwarmToSwarmId(swarm: any) {
return false;
}
function noop() {}