ipcInterface.ts 1.3 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3

4
import { IpcInterface } from './tuner_command_channel/common';
5
export { IpcInterface } from './tuner_command_channel/common';
6
7
8
9
10
11
12
13
14
15
16
17
import * as shim from './tuner_command_channel/shim';

let tunerDisabled: boolean = false;

export async function createDispatcherInterface(): Promise<IpcInterface> {
    if (!tunerDisabled) {
        return await shim.createDispatcherInterface();
    } else {
        return new DummyIpcInterface();
    }
}

18
19
20
21
22
23
24
25
26
27
28
29
30
export function encodeCommand(commandType: string, content: string): Buffer {
    const contentBuffer: Buffer = Buffer.from(content);
    const contentLengthBuffer: Buffer = Buffer.from(contentBuffer.length.toString().padStart(14, '0'));
    return Buffer.concat([Buffer.from(commandType), contentLengthBuffer, contentBuffer]);
}

class DummyIpcInterface implements IpcInterface {
    public async init(): Promise<void> { /* empty */ }
    public sendCommand(_commandType: string, _content?: string): void { /* empty */ }
    public onCommand(_listener: (commandType: string, content: string) => void): void { /* empty */ }
    public onError(_listener: (error: Error) => void): void { /* empty */ }
}

31
32
33
34
35
export namespace UnitTestHelpers {
    export function disableTuner(): void {
        tunerDisabled = true;
    }
}