Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(identifierType: NodeIdType, value: any, namespace?: number) {
this.identifierType = identifierType;
this.value = value;
this.namespace = namespace || 0;
// namespace shall be a UInt16
assert(this.namespace >= 0 && this.namespace <= 0xFFFF);
assert(this.identifierType !== NodeIdType.NUMERIC || (this.value >= 0 && this.value <= 0xFFFFFFFF));
assert(this.identifierType !== NodeIdType.GUID || isValidGuid(this.value as string));
assert(this.identifierType !== NodeIdType.STRING || typeof this.value === "string");
}
export function makeNodeId(value: string | Buffer | number, namespace?: number) {
value = value || 0;
namespace = namespace || 0;
let identifierType = NodeIdType.NUMERIC;
if (typeof value === "string") {
if (value.match(/^(s|g|b|i)=/)) {
throw new Error("please use coerce NodeId instead");
}
// 1 2 3
// 012345678901234567890123456789012345
// "72962B91-FA75-4AE6-8D28-B404DC7DAF63"
if (isValidGuid(value)) {
identifierType = NodeIdType.GUID;
} else {
identifierType = NodeIdType.STRING;
// detect accidental string of form "ns=x;x";
assert(value.indexOf("ns=") === -1, " makeNodeId(string) ? did you mean using coerceNodeId instead? ");
}
} else if (value instanceof Buffer) {
identifierType = NodeIdType.BYTESTRING;
}
const nodeId = new NodeId(identifierType, value, namespace);
assert(nodeId.hasOwnProperty("identifierType"));
return nodeId;
}
export function encodeGuid(guid: Guid, stream: BinaryStream): void {
if (!isValidGuid(guid)) {
throw new Error(" Invalid GUID : '" + JSON.stringify(guid) + "'");
}
// 1 2 3
// 012345678901234567890123456789012345
// | | | | | | | | | | |
// 12345678-1234-1234-ABCD-0123456789AB
// 00000000-0000-0000-0000-000000000000";
function write_UInt32(starts: number[]) {
const n = starts.length;
for (let i = 0; i < n; i++) {
const start = starts[i];
stream.writeUInt32(parseInt(guid.substr(start, 8), 16));
}
}
function write_UInt16(starts: number[]) {
} else if (twoFirst === "s=") {
identifierType = NodeIdType.STRING;
value = value.substr(2);
} else if (twoFirst === "b=") {
identifierType = NodeIdType.BYTESTRING;
value = Buffer.from(value.substr(2), "hex");
} else if (twoFirst === "g=") {
identifierType = NodeIdType.GUID;
value = value.substr(2);
} else if (isValidGuid(value)) {
identifierType = NodeIdType.GUID;
} else if ((matches = regexNamespaceI.exec(value)) !== null) {
identifierType = NodeIdType.NUMERIC;
namespace = parseInt(matches[1], 10);
value = parseInt(matches[2], 10);
} else if ((matches = regexNamespaceS.exec(value)) !== null) {
identifierType = NodeIdType.STRING;
namespace = parseInt(matches[1], 10);
value = matches[2];
} else if ((matches = regexNamespaceB.exec(value)) !== null) {
identifierType = NodeIdType.BYTESTRING;