downloadIpc.ts 1.3 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { URI } from 'vs/base/common/uri';
import * as fs from 'fs';
8
import { IServerChannel } from 'vs/base/parts/ipc/node/ipc';
J
Joao Moreno 已提交
9
import { Event, Emitter } from 'vs/base/common/event';
S
Sandeep Somavarapu 已提交
10

11
type UploadResponse = Buffer | string | undefined;
S
Sandeep Somavarapu 已提交
12

13
function upload(uri: URI): Event<UploadResponse> {
S
Sandeep Somavarapu 已提交
14 15 16 17
	const stream = new Emitter<UploadResponse>();
	const readstream = fs.createReadStream(uri.fsPath);
	readstream.on('data', data => stream.fire(data));
	readstream.on('error', error => stream.fire(error.toString()));
18
	readstream.on('close', () => stream.fire(undefined));
S
Sandeep Somavarapu 已提交
19 20 21
	return stream.event;
}

J
Joao Moreno 已提交
22
export class DownloadServiceChannel implements IServerChannel {
S
Sandeep Somavarapu 已提交
23 24 25

	constructor() { }

J
Joao Moreno 已提交
26
	listen(_, event: string, arg?: any): Event<any> {
S
Sandeep Somavarapu 已提交
27
		switch (event) {
J
Joao Moreno 已提交
28
			case 'upload': return Event.buffer(upload(URI.revive(arg)));
S
Sandeep Somavarapu 已提交
29
		}
J
Joao Moreno 已提交
30 31

		throw new Error(`Event not found: ${event}`);
S
Sandeep Somavarapu 已提交
32 33
	}

J
Johannes Rieken 已提交
34
	call(_, command: string): Promise<any> {
J
Joao Moreno 已提交
35
		throw new Error(`Call not found: ${command}`);
S
Sandeep Somavarapu 已提交
36 37
	}
}