2020-06-27 02:24:21 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the 'License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-21 03:41:51 +02:00
|
|
|
import type * as channels from '@protocol/channels';
|
2022-04-06 23:57:14 +02:00
|
|
|
import { Dispatcher } from './dispatcher';
|
2022-08-25 20:58:41 +02:00
|
|
|
import type { DispatcherScope } from './dispatcher';
|
2021-02-09 23:44:48 +01:00
|
|
|
import { StreamDispatcher } from './streamDispatcher';
|
2021-02-11 15:36:15 +01:00
|
|
|
import fs from 'fs';
|
2022-04-08 05:18:22 +02:00
|
|
|
import { mkdirIfNeeded } from '../../utils/fileUtils';
|
2022-04-07 07:21:27 +02:00
|
|
|
import type { Artifact } from '../artifact';
|
2020-06-27 02:24:21 +02:00
|
|
|
|
2022-08-25 20:58:41 +02:00
|
|
|
export class ArtifactDispatcher extends Dispatcher<Artifact, channels.ArtifactChannel, DispatcherScope> implements channels.ArtifactChannel {
|
2021-11-18 00:26:01 +01:00
|
|
|
_type_Artifact = true;
|
2021-03-31 19:38:05 +02:00
|
|
|
constructor(scope: DispatcherScope, artifact: Artifact) {
|
|
|
|
|
super(scope, artifact, 'Artifact', {
|
|
|
|
|
absolutePath: artifact.localPath(),
|
2020-06-27 02:24:21 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 19:38:05 +02:00
|
|
|
async pathAfterFinished(): Promise<channels.ArtifactPathAfterFinishedResult> {
|
|
|
|
|
const path = await this._object.localPathAfterFinished();
|
2020-07-21 02:38:06 +02:00
|
|
|
return { value: path || undefined };
|
2020-06-27 02:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 19:38:05 +02:00
|
|
|
async saveAs(params: channels.ArtifactSaveAsParams): Promise<channels.ArtifactSaveAsResult> {
|
2020-08-26 21:46:30 +02:00
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
|
this._object.saveAs(async (localPath, error) => {
|
|
|
|
|
if (error !== undefined) {
|
2021-02-15 01:46:26 +01:00
|
|
|
reject(new Error(error));
|
2020-08-26 21:46:30 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await mkdirIfNeeded(params.path);
|
2021-06-03 18:55:33 +02:00
|
|
|
await fs.promises.copyFile(localPath, params.path);
|
2020-08-26 21:46:30 +02:00
|
|
|
resolve();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 19:38:05 +02:00
|
|
|
async saveAsStream(): Promise<channels.ArtifactSaveAsStreamResult> {
|
2020-08-26 21:46:30 +02:00
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
|
this._object.saveAs(async (localPath, error) => {
|
|
|
|
|
if (error !== undefined) {
|
2021-02-15 01:46:26 +01:00
|
|
|
reject(new Error(error));
|
2020-08-26 21:46:30 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const readable = fs.createReadStream(localPath);
|
2022-08-26 18:30:27 +02:00
|
|
|
const stream = new StreamDispatcher(this, readable);
|
2020-08-26 21:46:30 +02:00
|
|
|
// Resolve with a stream, so that client starts saving the data.
|
|
|
|
|
resolve({ stream });
|
2021-03-31 19:38:05 +02:00
|
|
|
// Block the Artifact until the stream is consumed.
|
2020-08-26 21:46:30 +02:00
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
|
readable.on('close', resolve);
|
|
|
|
|
readable.on('end', resolve);
|
|
|
|
|
readable.on('error', resolve);
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-07-22 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 19:38:05 +02:00
|
|
|
async stream(): Promise<channels.ArtifactStreamResult> {
|
|
|
|
|
const fileName = await this._object.localPathAfterFinished();
|
2020-08-26 21:46:30 +02:00
|
|
|
if (!fileName)
|
2020-07-21 02:38:06 +02:00
|
|
|
return {};
|
2020-08-26 21:46:30 +02:00
|
|
|
const readable = fs.createReadStream(fileName);
|
2022-08-26 18:30:27 +02:00
|
|
|
return { stream: new StreamDispatcher(this, readable) };
|
2020-07-14 19:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 19:38:05 +02:00
|
|
|
async failure(): Promise<channels.ArtifactFailureResult> {
|
|
|
|
|
const error = await this._object.failureError();
|
2020-07-21 02:38:06 +02:00
|
|
|
return { error: error || undefined };
|
2020-06-27 02:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 22:23:22 +02:00
|
|
|
async cancel(): Promise<void> {
|
|
|
|
|
await this._object.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 02:24:21 +02:00
|
|
|
async delete(): Promise<void> {
|
|
|
|
|
await this._object.delete();
|
2021-04-25 05:39:48 +02:00
|
|
|
this._dispose();
|
2020-06-27 02:24:21 +02:00
|
|
|
}
|
|
|
|
|
}
|