2023-02-16 16:48:28 -08:00
|
|
|
/**
|
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import type { TestStore } from '../types/test';
|
|
|
|
|
import { currentConfig } from './common/globals';
|
2023-03-01 08:49:31 -08:00
|
|
|
import { mime } from 'playwright-core/lib/utilsBundle';
|
|
|
|
|
import { isJsonMimeType, isString, isTextualMimeType } from 'playwright-core/lib/utils';
|
2023-02-16 16:48:28 -08:00
|
|
|
|
|
|
|
|
class JsonStore implements TestStore {
|
|
|
|
|
async delete(name: string) {
|
|
|
|
|
const file = this.path(name);
|
|
|
|
|
await fs.promises.rm(file, { force: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get<T>(name: string) {
|
|
|
|
|
const file = this.path(name);
|
|
|
|
|
try {
|
2023-03-01 08:49:31 -08:00
|
|
|
const type = contentType(name);
|
|
|
|
|
if (type === 'binary')
|
|
|
|
|
return await fs.promises.readFile(file) as T;
|
|
|
|
|
const text = await fs.promises.readFile(file, 'utf-8');
|
|
|
|
|
if (type === 'json')
|
|
|
|
|
return JSON.parse(text) as T;
|
|
|
|
|
return text as T;
|
2023-02-16 16:48:28 -08:00
|
|
|
} catch (e) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path(name: string): string {
|
|
|
|
|
return path.join(this.root(), name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root(): string {
|
|
|
|
|
const config = currentConfig();
|
|
|
|
|
if (!config)
|
|
|
|
|
throw new Error('Cannot access store before config is loaded');
|
|
|
|
|
return config._internal.storeDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async set<T>(name: string, value: T | undefined) {
|
|
|
|
|
const file = this.path(name);
|
|
|
|
|
if (value === undefined) {
|
|
|
|
|
await fs.promises.rm(file, { force: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-01 08:49:31 -08:00
|
|
|
let data: string | Buffer = '';
|
|
|
|
|
switch (contentType(name)) {
|
|
|
|
|
case 'json': {
|
|
|
|
|
if (Buffer.isBuffer(value))
|
|
|
|
|
throw new Error('JSON value must be an Object');
|
|
|
|
|
data = JSON.stringify(value, undefined, 2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'text': {
|
|
|
|
|
if (!isString(value))
|
|
|
|
|
throw new Error('Textual value must be a string');
|
|
|
|
|
data = value as string;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'binary': {
|
|
|
|
|
if (!Buffer.isBuffer(value))
|
|
|
|
|
throw new Error('Binary value must be a Buffer');
|
|
|
|
|
data = value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-16 16:48:28 -08:00
|
|
|
await fs.promises.mkdir(path.dirname(file), { recursive: true });
|
|
|
|
|
await fs.promises.writeFile(file, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 08:49:31 -08:00
|
|
|
function contentType(name: string): 'json'|'text'|'binary' {
|
|
|
|
|
const mimeType = mime.getType(path.basename(name)) ?? 'application/octet-string';
|
|
|
|
|
if (isJsonMimeType(mimeType))
|
|
|
|
|
return 'json';
|
|
|
|
|
if (isTextualMimeType(mimeType))
|
|
|
|
|
return 'text';
|
|
|
|
|
return 'binary';
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 16:48:28 -08:00
|
|
|
export const store = new JsonStore();
|