UNPKG

@softchef/cdk-iot-device-management

Version:

IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.

207 lines (206 loc) 7.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.strictParseByte = exports.strictParseShort = exports.strictParseInt32 = exports.strictParseInt = exports.strictParseLong = exports.limitedParseFloat32 = exports.limitedParseFloat = exports.handleFloat = exports.limitedParseDouble = exports.strictParseFloat32 = exports.strictParseFloat = exports.strictParseDouble = exports.expectUnion = exports.expectString = exports.expectObject = exports.expectNonNull = exports.expectByte = exports.expectShort = exports.expectInt32 = exports.expectInt = exports.expectLong = exports.expectFloat32 = exports.expectNumber = exports.expectBoolean = exports.parseBoolean = void 0; const parseBoolean = (value) => { switch (value) { case "true": return true; case "false": return false; default: throw new Error(`Unable to parse boolean value "${value}"`); } }; exports.parseBoolean = parseBoolean; const expectBoolean = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "boolean") { return value; } throw new TypeError(`Expected boolean, got ${typeof value}`); }; exports.expectBoolean = expectBoolean; const expectNumber = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "number") { return value; } throw new TypeError(`Expected number, got ${typeof value}`); }; exports.expectNumber = expectNumber; const MAX_FLOAT = Math.ceil(2 ** 127 * (2 - 2 ** -23)); const expectFloat32 = (value) => { const expected = exports.expectNumber(value); if (expected !== undefined && !Number.isNaN(expected) && expected !== Infinity && expected !== -Infinity) { if (Math.abs(expected) > MAX_FLOAT) { throw new TypeError(`Expected 32-bit float, got ${value}`); } } return expected; }; exports.expectFloat32 = expectFloat32; const expectLong = (value) => { if (value === null || value === undefined) { return undefined; } if (Number.isInteger(value) && !Number.isNaN(value)) { return value; } throw new TypeError(`Expected integer, got ${typeof value}`); }; exports.expectLong = expectLong; exports.expectInt = exports.expectLong; const expectInt32 = (value) => expectSizedInt(value, 32); exports.expectInt32 = expectInt32; const expectShort = (value) => expectSizedInt(value, 16); exports.expectShort = expectShort; const expectByte = (value) => expectSizedInt(value, 8); exports.expectByte = expectByte; const expectSizedInt = (value, size) => { const expected = exports.expectLong(value); if (expected !== undefined && castInt(expected, size) !== expected) { throw new TypeError(`Expected ${size}-bit integer, got ${value}`); } return expected; }; const castInt = (value, size) => { switch (size) { case 32: return Int32Array.of(value)[0]; case 16: return Int16Array.of(value)[0]; case 8: return Int8Array.of(value)[0]; } }; const expectNonNull = (value, location) => { if (value === null || value === undefined) { if (location) { throw new TypeError(`Expected a non-null value for ${location}`); } throw new TypeError("Expected a non-null value"); } return value; }; exports.expectNonNull = expectNonNull; const expectObject = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "object" && !Array.isArray(value)) { return value; } throw new TypeError(`Expected object, got ${typeof value}`); }; exports.expectObject = expectObject; const expectString = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "string") { return value; } throw new TypeError(`Expected string, got ${typeof value}`); }; exports.expectString = expectString; const expectUnion = (value) => { if (value === null || value === undefined) { return undefined; } const asObject = exports.expectObject(value); const setKeys = Object.entries(asObject) .filter(([_, v]) => v !== null && v !== undefined) .map(([k, _]) => k); if (setKeys.length === 0) { throw new TypeError(`Unions must have exactly one non-null member`); } if (setKeys.length > 1) { throw new TypeError(`Unions must have exactly one non-null member. Keys ${setKeys} were not null.`); } return asObject; }; exports.expectUnion = expectUnion; const strictParseDouble = (value) => { if (typeof value == "string") { return exports.expectNumber(parseNumber(value)); } return exports.expectNumber(value); }; exports.strictParseDouble = strictParseDouble; exports.strictParseFloat = exports.strictParseDouble; const strictParseFloat32 = (value) => { if (typeof value == "string") { return exports.expectFloat32(parseNumber(value)); } return exports.expectFloat32(value); }; exports.strictParseFloat32 = strictParseFloat32; const NUMBER_REGEX = /(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)|(-?Infinity)|(NaN)/g; const parseNumber = (value) => { const matches = value.match(NUMBER_REGEX); if (matches === null || matches[0].length !== value.length) { throw new TypeError(`Expected real number, got implicit NaN`); } return parseFloat(value); }; const limitedParseDouble = (value) => { if (typeof value == "string") { return parseFloatString(value); } return exports.expectNumber(value); }; exports.limitedParseDouble = limitedParseDouble; exports.handleFloat = exports.limitedParseDouble; exports.limitedParseFloat = exports.limitedParseDouble; const limitedParseFloat32 = (value) => { if (typeof value == "string") { return parseFloatString(value); } return exports.expectFloat32(value); }; exports.limitedParseFloat32 = limitedParseFloat32; const parseFloatString = (value) => { switch (value) { case "NaN": return NaN; case "Infinity": return Infinity; case "-Infinity": return -Infinity; default: throw new Error(`Unable to parse float value: ${value}`); } }; const strictParseLong = (value) => { if (typeof value === "string") { return exports.expectLong(parseNumber(value)); } return exports.expectLong(value); }; exports.strictParseLong = strictParseLong; exports.strictParseInt = exports.strictParseLong; const strictParseInt32 = (value) => { if (typeof value === "string") { return exports.expectInt32(parseNumber(value)); } return exports.expectInt32(value); }; exports.strictParseInt32 = strictParseInt32; const strictParseShort = (value) => { if (typeof value === "string") { return exports.expectShort(parseNumber(value)); } return exports.expectShort(value); }; exports.strictParseShort = strictParseShort; const strictParseByte = (value) => { if (typeof value === "string") { return exports.expectByte(parseNumber(value)); } return exports.expectByte(value); }; exports.strictParseByte = strictParseByte;