-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathconfig.ts
140 lines (121 loc) · 3.87 KB
/
config.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
/// <reference path="../../../node_modules/@types/mocha/index.d.ts" />
import {
ensureNotFalsy,
isPromise,
randomCouchString
} from '../utils/index.ts';
import {
enforceOptions as broadcastChannelEnforceOptions
} from 'broadcast-channel';
import events from 'node:events';
import * as path from 'node:path';
import url from 'node:url';
import type { RxStorage, RxTestStorage } from '../../types';
import { wrappedKeyEncryptionCryptoJsStorage } from '../encryption-crypto-js/index.ts';
export type TestConfig = {
storage: RxTestStorage;
};
export const isDeno = typeof window !== 'undefined' && 'Deno' in window;
export const isBun = typeof process !== 'undefined' && !!process.versions.bun;
export const isNode = !isDeno && !isBun && typeof window === 'undefined';
let config: TestConfig;
export function setConfig(newConfig: TestConfig) {
config = newConfig;
}
let initDone = false;
export function getConfig() {
if (!initDone) {
initTestEnvironment();
initDone = true;
}
return ensureNotFalsy(config, 'testConfig not set')
}
declare const Deno: any;
function getEnvVariables() {
if (isDeno) {
const ret: any = {};
[
'DEFAULT_STORAGE',
'NODE_ENV'
].forEach(k => {
ret[k] = Deno.env.get(k);
});
return ret;
}
return isBun || isNode ? process.env : (window as any).__karma__.config.env;
}
export const ENV_VARIABLES = getEnvVariables();
export const DEFAULT_STORAGE = ENV_VARIABLES.DEFAULT_STORAGE as string;
export function isFastMode(): boolean {
try {
return ENV_VARIABLES.NODE_ENV === 'fast';
} catch (err) {
return false;
}
}
export function initTestEnvironment() {
if (ENV_VARIABLES.NODE_ENV === 'fast') {
broadcastChannelEnforceOptions({
type: 'simulate'
});
}
/**
* Overwrite the console for easier debugging
*/
const oldConsoleLog = console.log.bind(console);
const oldConsoleDir = console.dir.bind(console);
function newLog(this: typeof console, value: any) {
if (isPromise(value)) {
oldConsoleDir(value);
throw new Error('cannot log Promise(), you should await it first');
}
if (typeof value === 'string' || typeof value === 'number') {
oldConsoleLog(value);
return;
}
try {
JSON.stringify(value);
oldConsoleLog(JSON.stringify(value, null, 4));
} catch (err) {
oldConsoleDir(value);
}
}
console.log = newLog.bind(console);
console.dir = newLog.bind(console);
console.log('DEFAULT_STORAGE: ' + DEFAULT_STORAGE);
if (isNode) {
process.setMaxListeners(100);
events.EventEmitter.defaultMaxListeners = 100;
/**
* Add a global function to process, so we can debug timings
*/
(process as any).startTime = performance.now();
(process as any).logTime = (msg: string = '') => {
const diff = performance.now() - (process as any).startTime;
console.log('process logTime(' + msg + ') ' + diff + 'ms');
};
}
}
export function getEncryptedStorage(baseStorage = getConfig().storage.getStorage()): RxStorage<any, any> {
const ret = config.storage.hasEncryption ?
baseStorage :
wrappedKeyEncryptionCryptoJsStorage({
storage: baseStorage
});
return ret;
}
export function isNotOneOfTheseStorages(storageNames: string[]) {
const isName = getConfig().storage.name;
if (storageNames.includes(isName)) {
return false;
} else {
return true;
}
}
export function getPassword(): Promise<string> {
if (getConfig().storage.hasEncryption) {
return ensureNotFalsy(getConfig().storage.hasEncryption)();
} else {
return Promise.resolve('test-password-' + randomCouchString(10));
}
}