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

fix: Fix null/undefined check in javascript Convert Arg #13

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
runs-on: ubuntu-latest
name: Build/Deploy .NET Core
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.x'
- name: Install dependencies
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/dotnet-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v0.9.6
uses: gittools/actions/gitversion/setup@v3.0
with:
versionSpec: '5.x'
- name: Use GitVersion
id: gitversion # step id used as reference for output values
uses: gittools/actions/gitversion/execute@v0.9.6
uses: gittools/actions/gitversion/execute@v3.0
with:
additionalArguments: '/updateAssemblyInfo'
updateAssemblyInfo: true
- run: |
echo "NuGetVersionV2: ${{ steps.gitversion.outputs.NuGetVersionV2 }}"
echo "FullSemVer: ${{ steps.gitversion.outputs.fullSemVer }}"
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
- name: Build with dotnet
run: dotnet build --configuration Release ./EventHorizon.Blazor.Server.Interop/EventHorizon.Blazor.Server.Interop.csproj
- name: Pack with dotnet
run: dotnet pack EventHorizon.Blazor.Server.Interop/EventHorizon.Blazor.Server.Interop.csproj --output nuget-packages --configuration Release -p:PackageVersion=${{ steps.gitversion.outputs.NuGetVersionV2 }}
run: dotnet pack EventHorizon.Blazor.Server.Interop/EventHorizon.Blazor.Server.Interop.csproj --output nuget-packages --configuration Release -p:PackageVersion=${{ steps.gitversion.outputs.fullSemVer }}
- name: Push with dotnet
run: dotnet nuget push nuget-packages/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
10 changes: 5 additions & 5 deletions .github/workflows/main-tag-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ jobs:
with:
fetch-depth: '0'
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v0.9.6
uses: gittools/actions/gitversion/setup@v3.0
with:
versionSpec: '5.x'
- name: Use GitVersion
id: gitversion # step id used as reference for output values
uses: gittools/actions/gitversion/execute@v0.9.6
uses: gittools/actions/gitversion/execute@v3.0
- run: |
echo "NuGetVersionV2: ${{ steps.gitversion.outputs.NuGetVersionV2 }}"
echo "SemVer: ${{ steps.gitversion.outputs.semVer }}"
- name: Bump version and push tag
uses: anothrNick/github-tag-action@1.17.2
uses: anothrNick/github-tag-action@1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# We want to push to nuget the same version we tag
CUSTOM_TAG: ${{ steps.gitversion.outputs.NuGetVersionV2 }}
CUSTOM_TAG: ${{ steps.gitversion.outputs.semVer }}
RELEASE_BRANCHES: main
133 changes: 78 additions & 55 deletions EventHorizon.Blazor.Server.Interop/wwwroot/server-interop-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
argumentCache,
methodCache,
};
const CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
const CHARS =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
""
);
const guid = () => {
var chars = CHARS,
uuid = new Array(36),
Expand All @@ -20,7 +23,9 @@
} else if (i === 14) {
uuid[i] = "4";
} else {
if (rnd <= 0x02) { rnd = (0x2000000 + Math.random() * 0x1000000) | 0; }
if (rnd <= 0x02) {
rnd = (0x2000000 + Math.random() * 0x1000000) | 0;
}
r = rnd & 0xf;
rnd = rnd >> 4;
uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
Expand Down Expand Up @@ -49,44 +54,59 @@
/**
* Check argument for existing in argumentCache and if actionResultCallbackType and if actionCallbackType.
* Returns argValue if not part argumentCache or actionCallbackType.
*
*
* @param {any} argValue
*/
const convertArg = (argValue) => {
if (!argValue) {
return null;
if (!argValue == null || argValue == undefined) {
return undefined;
}
if (argValue[cacheKey] && argumentCache.has(argValue[cacheKey])) {
return argumentCache.get(argValue[cacheKey]);
} else if (argValue[typeKey] && argValue[typeKey] === actionResultCallbackType) {
} else if (
argValue[typeKey] &&
argValue[typeKey] === actionResultCallbackType
) {
const invokableReference = argValue["invokableReference"];
const method = argValue["method"];
return async function () {
console.log({ invokableReference, method, arguments, convertedARgs: convertCallbackArguments(arguments) })
var result = await invokableReference.invokeMethodAsync(method, ...convertCallbackArguments(arguments));
console.log({ result })
var result = await invokableReference.invokeMethodAsync(
method,
...convertCallbackArguments(arguments)
);
return !!result.result ? result.result : result;
};
} else if (argValue[typeKey] && argValue[typeKey] === actionCallbackType) {
} else if (
argValue[typeKey] &&
argValue[typeKey] === actionCallbackType
) {
const invokableReference = argValue["invokableReference"];
const method = argValue["method"];
return async function () {
await invokableReference.invokeMethodAsync(method, ...convertCallbackArguments(arguments));
await invokableReference.invokeMethodAsync(
method,
...convertCallbackArguments(arguments)
);
};
}
return argValue;
};
/**
* Loop through all the argumentArray items and convert the args to usable references.
*
*
* @param {any} argumentArray
*/
const convertArgs = (argumentArray) => {
const args = [];
for (var i = 1; i < argumentArray.length; i++) {
const arg = convertArg(argumentArray[i]);

if (arg && typeof (arg) === "object" && !arg[cacheKey] && !Array.isArray(arg)) {
if (
arg &&
typeof arg === "object" &&
!arg[cacheKey] &&
!Array.isArray(arg)
) {
// Object literal: { prop: "hi", prop2: { ___type: "action_callback" } }
const newArg = {};
for (const key in arg) {
Expand All @@ -110,9 +130,7 @@
const convertCallbackArguments = (callbackArguments) => {
const args = [];
for (var arg of callbackArguments) {
if (typeof (arg) === "object"
&& !Array.isArray(arg)
) {
if (typeof arg === "object" && !Array.isArray(arg)) {
args.push(cacheEntity(arg));
} else if (Array.isArray(arg)) {
args.push(arg.map(cacheEntity));
Expand All @@ -130,28 +148,28 @@
let numStr = String(num);

if (Math.abs(num) < 1.0) {
let e = parseInt(num.toString().split('e-')[1]);
let e = parseInt(num.toString().split("e-")[1]);
if (e) {
let negative = num < 0;
if (negative) num *= -1
if (negative) num *= -1;
num *= Math.pow(10, e - 1);
numStr = '0.' + (new Array(e)).join('0') + num.toString().substring(2);
numStr =
"0." + new Array(e).join("0") + num.toString().substring(2);
if (negative) numStr = "-" + numStr;
}
}
else {
let e = parseInt(num.toString().split('+')[1]);
} else {
let e = parseInt(num.toString().split("+")[1]);
if (e > 20) {
e -= 20;
num /= Math.pow(10, e);
numStr = num.toString() + (new Array(e + 1)).join('0');
numStr = num.toString() + new Array(e + 1).join("0");
}
}

return numStr;
};

const isPromise = (obj) => typeof (obj?.then) === "function";
const isPromise = (obj) => typeof obj?.then === "function";

const cacheKey = "___guid";
const typeKey = "___type";
Expand All @@ -177,12 +195,15 @@
value = value[identifier[i]];
}

if (typeof (value) === "number") {
if (typeof value === "number") {
value = numberToString(value);
} else if (value == undefined || value == null) {
return null;
}

return value.toString();
} catch (ex) {
console.log("error", { ex, args });
console.log("error", { ex, root, identifier });
}
},
/**
Expand Down Expand Up @@ -301,7 +322,7 @@
},
/**
* This will set a the passed in value on the identifier starting at the root.
*
*
* @param root Property Name from window or Cached Entity GUID
* @param identifier Property to get from Root
* @param value The value to set at the root.identifier
Expand Down Expand Up @@ -339,7 +360,11 @@
var args = [];
for (var i = 2; i < arguments.length; i++) {
var arg = arguments[i];
if (arg && arg[cacheKey] && argumentCache.has(arg[cacheKey])) {
if (
arg &&
arg[cacheKey] &&
argumentCache.has(arg[cacheKey])
) {
args.push(argumentCache.get(arg[cacheKey]));
} else {
args.push(arg);
Expand Down Expand Up @@ -374,10 +399,10 @@
newObject[cacheKey] = guid();
argumentCache.set(newObject[cacheKey], newObject);
return {
[cacheKey]: newObject[cacheKey]
[cacheKey]: newObject[cacheKey],
};
} catch (ex) {
console.log("error", { ex, arguments });;
console.log("error", { ex, arguments });
}
throw { code: "invalid_call" };
},
Expand Down Expand Up @@ -428,9 +453,10 @@
result = await result;
}
let newCacheKey = result[cacheKey];
if (typeof (result) === "object"
&& !Array.isArray(result)
&& !newCacheKey
if (
typeof result === "object" &&
!Array.isArray(result) &&
!newCacheKey
) {
newCacheKey = guid();
result[cacheKey] = newCacheKey;
Expand Down Expand Up @@ -565,8 +591,9 @@
obj = obj[identifier[i]];
}
var newObject = await obj.call(context, ...args);
if (typeof (newObject) === "object"
&& !Array.isArray(newObject)
if (
typeof newObject === "object" &&
!Array.isArray(newObject)
) {
const newCacheKey = guid();
newObject[cacheKey] = newCacheKey;
Expand Down Expand Up @@ -663,14 +690,14 @@
"$args",
methodRunner.script
);
methodCache.set(
methodRunner.methodName,
script
);
methodCache.set(methodRunner.methodName, script);
}
script({
argumentCache,
}, methodRunner.args);
script(
{
argumentCache,
},
methodRunner.args
);
},
/**
* This will create a callback function and trigger the args
Expand All @@ -685,41 +712,37 @@

const cachedEntity = argumentCache.get(entity);
cachedEntity[funcCallbackName](function () {
invokableReference.invokeMethodAsync(referenceMethod, ...convertCallbackArguments(arguments));
invokableReference.invokeMethodAsync(
referenceMethod,
...convertCallbackArguments(arguments)
);
});
},
/**
* This will create a callback function and trigger the assembly callback
**/
assemblyFuncCallback: (
identifier,
assemblyName,
referenceCallback
) => {
assemblyFuncCallback: (identifier, assemblyName, referenceCallback) => {
var identifier = identifier.split(".");
var func = window[identifier[0]];
for (var i = 1; i < identifier.length; i++) {
func = func[identifier[i]];
}

func(function (/* TODO: Support passing back props */) {
DotNet.invokeMethodAsync(assemblyName, referenceCallback)
DotNet.invokeMethodAsync(assemblyName, referenceCallback);
});
},
/**
* This will create a cachedEntity from the prop on the passed in entity.
**/
cacheEntity: (
identifier,
prop
) => {
cacheEntity: (identifier, prop) => {
const cachedEntity = argumentCache.get(identifier);
var newObject = cachedEntity[prop];
newObject[cacheKey] = guid();
argumentCache.set(newObject[cacheKey], newObject);
return {
[cacheKey]: newObject[cacheKey]
[cacheKey]: newObject[cacheKey],
};
},
};
})();
})();
Loading