-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathexportFile.ts
168 lines (152 loc) · 4.47 KB
/
exportFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import * as fs from "fs";
import { LoaderHeader } from "@fluidframework/container-definitions/internal";
import {
loadExistingContainer,
type ILoaderProps,
} from "@fluidframework/container-loader/internal";
import { createLocalOdspDocumentServiceFactory } from "@fluidframework/odsp-driver/internal";
import {
ITelemetryLoggerExt,
PerformanceEvent,
} from "@fluidframework/telemetry-utils/internal";
import { IFluidFileConverter } from "./codeLoaderBundle.js";
import { FakeUrlResolver } from "./fakeUrlResolver.js";
/* eslint-disable import/no-internal-modules */
import { ITelemetryOptions } from "./logger/fileLogger.js";
import { createLogger, getTelemetryFileValidationError } from "./logger/loggerUtils.js";
import { getArgsValidationError, getSnapshotFileContent, timeoutPromise } from "./utils.js";
/* eslint-enable import/no-internal-modules */
/**
* @legacy
* @alpha
*/
export type IExportFileResponse = IExportFileResponseSuccess | IExportFileResponseFailure;
/**
* @legacy
* @alpha
*/
export interface IExportFileResponseSuccess {
success: true;
}
/**
* @legacy
* @alpha
*/
export interface IExportFileResponseFailure {
success: false;
eventName: string;
errorMessage: string;
error?: any;
}
const clientArgsValidationError = "Client_ArgsValidationError";
/**
* Execute code on Container based on ODSP snapshot and write result to file
* @internal
*/
export async function exportFile(
fluidFileConverter: IFluidFileConverter,
inputFile: string,
outputFile: string,
telemetryFile: string,
options?: string,
telemetryOptions?: ITelemetryOptions,
timeout?: number,
disableNetworkFetch?: boolean,
): Promise<IExportFileResponse> {
const telemetryArgError = getTelemetryFileValidationError(telemetryFile);
if (telemetryArgError) {
const eventName = clientArgsValidationError;
return { success: false, eventName, errorMessage: telemetryArgError };
}
const { fileLogger, logger } = createLogger(telemetryFile, telemetryOptions);
try {
return await PerformanceEvent.timedExecAsync(
logger,
{ eventName: "ExportFile" },
async () => {
const argsValidationError = getArgsValidationError(inputFile, outputFile, timeout);
if (argsValidationError) {
const eventName = clientArgsValidationError;
logger.sendErrorEvent({ eventName, message: argsValidationError });
return { success: false, eventName, errorMessage: argsValidationError };
}
fs.writeFileSync(
outputFile,
await createContainerAndExecute(
getSnapshotFileContent(inputFile),
fluidFileConverter,
logger,
options,
timeout,
disableNetworkFetch,
),
);
return { success: true };
},
);
} catch (error) {
const eventName = "Client_UnexpectedError";
logger.sendErrorEvent({ eventName }, error);
return { success: false, eventName, errorMessage: "Unexpected error", error };
} finally {
await fileLogger.close();
}
}
/**
* Create the container based on an ODSP snapshot and execute code on it
* @returns result of execution
* @internal
*/
export async function createContainerAndExecute(
localOdspSnapshot: string | Uint8Array,
fluidFileConverter: IFluidFileConverter,
logger: ITelemetryLoggerExt,
options?: string,
timeout?: number,
disableNetworkFetch: boolean = false,
): Promise<string> {
const fn = async () => {
if (disableNetworkFetch) {
global.fetch = async () => {
throw new Error("Network fetch is not allowed");
};
}
const loaderProps: ILoaderProps = {
urlResolver: new FakeUrlResolver(),
documentServiceFactory: createLocalOdspDocumentServiceFactory(localOdspSnapshot),
codeLoader: await fluidFileConverter.getCodeLoader(logger),
scope: await fluidFileConverter.getScope?.(logger),
logger,
};
const container = await loadExistingContainer({
...loaderProps,
request: {
url: "/fakeUrl/",
headers: {
[LoaderHeader.loadMode]: { opsBeforeReturn: "cached" },
},
},
});
return PerformanceEvent.timedExecAsync(logger, { eventName: "ExportFile" }, async () => {
try {
return await fluidFileConverter.execute(container, options);
} finally {
container.dispose();
}
});
};
// eslint-disable-next-line unicorn/prefer-ternary
if (timeout !== undefined) {
return timeoutPromise<string>((resolve, reject) => {
fn()
.then((value) => resolve(value))
.catch((error) => reject(error));
}, timeout);
} else {
return fn();
}
}