refactor: use Buffer.from and Buffer.alloc instead of new Buffer() (#957)

This commit is contained in:
Róbert Kiss
2019-05-29 21:56:01 +02:00
committed by László Monda
parent 999feea488
commit 82c9126d82
24 changed files with 47 additions and 47 deletions
+7 -7
View File
@@ -145,12 +145,12 @@ export class UhkHidDevice {
}
public async writeConfigToEeprom(configBufferId: ConfigBufferId): Promise<void> {
await this.write(new Buffer([UsbCommand.LaunchEepromTransfer, EepromOperation.write, configBufferId]));
await this.write(Buffer.from([UsbCommand.LaunchEepromTransfer, EepromOperation.write, configBufferId]));
await this.waitUntilKeyboardBusy();
}
public async enableUsbStackTest(): Promise<void> {
await this.write(new Buffer([UsbCommand.SetVariable, UsbVariables.testUsbStack, 1]));
await this.write(Buffer.from([UsbCommand.SetVariable, UsbVariables.testUsbStack, 1]));
await this.waitUntilKeyboardBusy();
}
@@ -169,7 +169,7 @@ export class UhkHidDevice {
public async waitUntilKeyboardBusy(): Promise<void> {
while (true) {
const buffer = await this.write(new Buffer([UsbCommand.GetDeviceState]));
const buffer = await this.write(Buffer.from([UsbCommand.GetDeviceState]));
if (buffer[1] === 0) {
break;
}
@@ -186,7 +186,7 @@ export class UhkHidDevice {
const reenumMode = EnumerationModes[enumerationMode].toString();
this.logService.debug(`[UhkHidDevice] Start reenumeration, mode: ${reenumMode}`);
const message = new Buffer([
const message = Buffer.from([
UsbCommand.Reenumerate,
enumerationMode,
BOOTLOADER_TIMEOUT_MS & 0xff,
@@ -240,16 +240,16 @@ export class UhkHidDevice {
const moduleName = kbootCommandName(module);
this.logService.debug(`[UhkHidDevice] USB[T]: Send KbootCommand ${moduleName} ${KbootCommands[command].toString()}`);
if (command === KbootCommands.idle) {
transfer = new Buffer([UsbCommand.SendKbootCommandToModule, command]);
transfer = Buffer.from([UsbCommand.SendKbootCommandToModule, command]);
} else {
transfer = new Buffer([UsbCommand.SendKbootCommandToModule, command, module]);
transfer = Buffer.from([UsbCommand.SendKbootCommandToModule, command, module]);
}
await retry(async () => await this.write(transfer), maxTry, this.logService);
}
async jumpToBootloaderModule(module: ModuleSlotToId): Promise<any> {
this.logService.debug(`[UhkHidDevice] USB[T]: Jump to bootloader. Module: ${ModuleSlotToId[module].toString()}`);
const transfer = new Buffer([UsbCommand.JumpToModuleBootloader, module]);
const transfer = Buffer.from([UsbCommand.JumpToModuleBootloader, module]);
await this.write(transfer);
}
+7 -7
View File
@@ -165,7 +165,7 @@ export class UhkOperations {
this.logService.debug(`[DeviceOperation] getConfigSize() configSize: ${configSize}`);
const chunkSize = 63;
let offset = 0;
let configBuffer = new Buffer(0);
let configBuffer = Buffer.alloc(0);
let firstRead = true;
this.logService.debug(`[DeviceOperation] USB[T]: Read ${configName} from keyboard`);
@@ -174,7 +174,7 @@ export class UhkOperations {
const writeBuffer = Buffer.from(
[UsbCommand.ReadConfig, configBufferId, chunkSizeToRead, offset & 0xff, offset >> 8]);
const readBuffer = await this.device.write(writeBuffer);
configBuffer = Buffer.concat([configBuffer, new Buffer(readBuffer.slice(1, chunkSizeToRead + 1))]);
configBuffer = Buffer.concat([configBuffer, Buffer.from(readBuffer.slice(1, chunkSizeToRead + 1))]);
offset += chunkSizeToRead;
if (firstRead && configBufferId !== ConfigBufferId.hardwareConfig) {
@@ -203,7 +203,7 @@ export class UhkOperations {
* @returns {Promise<number>}
*/
public async getConfigSizeFromKeyboard(configBufferId: ConfigBufferId): Promise<number> {
const buffer = await this.device.write(new Buffer([UsbCommand.GetProperty, DevicePropertyIds.ConfigSizes]));
const buffer = await this.device.write(Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.ConfigSizes]));
this.device.close();
const hardwareConfigSize = buffer[1] + (buffer[2] << 8);
const userConfigSize = buffer[3] + (buffer[4] << 8);
@@ -232,7 +232,7 @@ export class UhkOperations {
const timeoutTime = new Date(new Date().getTime() + 30000);
while (new Date() < timeoutTime) {
const buffer = await this.device.write(new Buffer([UsbCommand.GetProperty, DevicePropertyIds.CurrentKbootCommand]));
const buffer = await this.device.write(Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.CurrentKbootCommand]));
this.device.close();
if (buffer[1] === 0) {
@@ -252,7 +252,7 @@ export class UhkOperations {
try {
this.logService.debug('[DeviceOperation] USB[T]: Read left module version information');
const command = new Buffer([
const command = Buffer.from([
UsbCommand.GetModuleProperty,
ModuleSlotToId.leftHalf,
ModulePropertyId.protocolVersions
@@ -280,7 +280,7 @@ export class UhkOperations {
public async getRightModuleVersionInfo(): Promise<HardwareModuleInfo> {
this.logService.debug('[DeviceOperation] USB[T]: Read right module version information');
const command = new Buffer([UsbCommand.GetProperty, DevicePropertyIds.ProtocolVersions]);
const command = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.ProtocolVersions]);
const buffer = await this.device.write(command);
const uhkBuffer = UhkBuffer.fromArray(convertBufferToIntArray(buffer));
// skip the first byte
@@ -303,7 +303,7 @@ export class UhkOperations {
await this.device.write(fragment);
}
this.logService.debug('[DeviceOperation] USB[T]: Apply user configuration to keyboard');
const applyBuffer = new Buffer([UsbCommand.ApplyConfig]);
const applyBuffer = Buffer.from([UsbCommand.ApplyConfig]);
await this.device.write(applyBuffer);
}
+1 -1
View File
@@ -33,7 +33,7 @@ export function getTransferBuffers(usbCommand: UsbCommand, configBuffer: Buffer)
const length = offset + MAX_SENDING_PAYLOAD_SIZE < configBuffer.length
? MAX_SENDING_PAYLOAD_SIZE
: configBuffer.length - offset;
const header = new Buffer([usbCommand, length, offset & 0xFF, offset >> 8]);
const header = Buffer.from([usbCommand, length, offset & 0xFF, offset >> 8]);
fragments.push(Buffer.concat([header, configBuffer.slice(offset, offset + length)]));
}