2021-09-10 20:05:47 +02:00
|
|
|
import * as React from 'react';
|
2021-06-20 20:29:34 +02:00
|
|
|
import { NoteEntity, ResourceEntity } from './services/database/types';
|
2023-12-06 21:24:00 +02:00
|
|
|
import type FsDriverBase from './fs-driver-base';
|
2020-11-14 14:37:18 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
let isTestingEnv_ = false;
|
2020-11-19 18:38:44 +02:00
|
|
|
|
2021-09-10 20:05:47 +02:00
|
|
|
// We need to ensure that there's only one instance of React being used by all
|
|
|
|
// the packages. In particular, the lib might need React to define generic
|
|
|
|
// hooks, but it shouldn't have React in its dependencies as that would cause
|
|
|
|
// the following error:
|
2020-11-19 18:38:44 +02:00
|
|
|
//
|
|
|
|
// https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react
|
|
|
|
//
|
2021-09-10 20:05:47 +02:00
|
|
|
// So instead, the **applications** include React as a dependency, then pass it
|
|
|
|
// to any other packages using the shim. Essentially, only one package should
|
|
|
|
// require React, and in our case that should be one of the applications
|
|
|
|
// (app-desktop, app-mobile, etc.) since we are sure they won't be dependency to
|
|
|
|
// other packages (unlike the lib which can be included anywhere).
|
|
|
|
//
|
2022-07-10 15:54:31 +02:00
|
|
|
// Regarding the type - although we import React, we only use it as a type
|
2021-09-10 20:05:47 +02:00
|
|
|
// using `typeof React`. This is just to get types in hooks.
|
|
|
|
//
|
|
|
|
// https://stackoverflow.com/a/42816077/561309
|
|
|
|
let react_: typeof React = null;
|
2021-10-01 20:35:27 +02:00
|
|
|
let nodeSqlite_: any = null;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
const shim = {
|
2021-01-22 19:41:11 +02:00
|
|
|
Geolocation: null as any,
|
|
|
|
|
2021-10-01 20:35:27 +02:00
|
|
|
electronBridge: (): any => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
msleep_: (ms: number) => {
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-01-29 20:45:11 +02:00
|
|
|
return new Promise((resolve: Function) => {
|
|
|
|
shim.setTimeout(() => {
|
|
|
|
resolve(null);
|
|
|
|
}, ms);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
isNode: () => {
|
|
|
|
if (typeof process === 'undefined') return false;
|
|
|
|
if (shim.isElectron()) return true;
|
2023-10-12 14:27:48 +02:00
|
|
|
return !shim.mobilePlatform();
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
isReactNative: () => {
|
|
|
|
if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('ReactNativeDebugger') >= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !shim.isNode();
|
|
|
|
},
|
|
|
|
|
|
|
|
isLinux: () => {
|
|
|
|
return process && process.platform === 'linux';
|
|
|
|
},
|
|
|
|
|
2023-07-10 12:59:09 +02:00
|
|
|
isGNOME: () => {
|
|
|
|
// XDG_CURRENT_DESKTOP may be something like "ubuntu:GNOME" and not just "GNOME".
|
|
|
|
// Thus, we use .includes and not ===.
|
|
|
|
return (shim.isLinux() || shim.isFreeBSD())
|
|
|
|
&& process && (process.env['XDG_CURRENT_DESKTOP'] ?? '').includes('GNOME');
|
|
|
|
},
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
isFreeBSD: () => {
|
|
|
|
return process && process.platform === 'freebsd';
|
|
|
|
},
|
|
|
|
|
|
|
|
isWindows: () => {
|
|
|
|
return process && process.platform === 'win32';
|
|
|
|
},
|
|
|
|
|
|
|
|
isMac: () => {
|
|
|
|
return process && process.platform === 'darwin';
|
|
|
|
},
|
|
|
|
|
|
|
|
platformName: () => {
|
|
|
|
if (shim.isReactNative()) return shim.mobilePlatform();
|
|
|
|
if (shim.isMac()) return 'darwin';
|
|
|
|
if (shim.isWindows()) return 'win32';
|
|
|
|
if (shim.isLinux()) return 'linux';
|
|
|
|
if (shim.isFreeBSD()) return 'freebsd';
|
|
|
|
if (process && process.platform) return process.platform;
|
|
|
|
throw new Error('Cannot determine platform');
|
|
|
|
},
|
|
|
|
|
|
|
|
// "ios" or "android", or "" if not on mobile
|
|
|
|
mobilePlatform: () => {
|
|
|
|
return ''; // Default if we're not on mobile (React Native)
|
|
|
|
},
|
|
|
|
|
|
|
|
// https://github.com/cheton/is-electron
|
|
|
|
isElectron: () => {
|
|
|
|
// Renderer process
|
|
|
|
if (typeof window !== 'undefined' && typeof window.process === 'object' && (window.process as any).type === 'renderer') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main process
|
|
|
|
if (typeof process !== 'undefined' && typeof process.versions === 'object' && !!(process.versions as any).electron) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect the user agent when the `nodeIntegration` option is set to true
|
|
|
|
if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
isPortable: (): boolean => {
|
2020-10-09 19:35:46 +02:00
|
|
|
return typeof process !== 'undefined' && typeof process.env === 'object' && !!process.env.PORTABLE_EXECUTABLE_DIR;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Node requests can go wrong is so many different ways and with so
|
|
|
|
// many different error messages... This handler inspects the error
|
|
|
|
// and decides whether the request can safely be repeated or not.
|
2020-11-12 21:13:28 +02:00
|
|
|
fetchRequestCanBeRetried: (error: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!error) return false;
|
|
|
|
|
|
|
|
// Unfortunately the error 'Network request failed' doesn't have a type
|
|
|
|
// or error code, so hopefully that message won't change and is not localized
|
2022-07-23 09:31:32 +02:00
|
|
|
if (error.message === 'Network request failed') return true;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// request to https://public-ch3302....1fab24cb1bd5f.md failed, reason: socket hang up"
|
2022-07-23 09:31:32 +02:00
|
|
|
if (error.code === 'ECONNRESET') return true;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// OneDrive (or Node?) sometimes sends back a "not found" error for resources
|
|
|
|
// that definitely exist and in this case repeating the request works.
|
|
|
|
// Error is:
|
|
|
|
// request to https://graph.microsoft.com/v1.0/drive/special/approot failed, reason: getaddrinfo ENOTFOUND graph.microsoft.com graph.microsoft.com:443
|
2022-07-23 09:31:32 +02:00
|
|
|
if (error.code === 'ENOTFOUND') return true;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// network timeout at: https://public-ch3302...859f9b0e3ab.md
|
|
|
|
if (error.message && error.message.indexOf('network timeout') === 0) return true;
|
|
|
|
|
|
|
|
// name: 'FetchError',
|
|
|
|
// message: 'request to https://api.ipify.org/?format=json failed, reason: getaddrinfo EAI_AGAIN api.ipify.org:443',
|
|
|
|
// type: 'system',
|
|
|
|
// errno: 'EAI_AGAIN',
|
|
|
|
// code: 'EAI_AGAIN' } } reason: { FetchError: request to https://api.ipify.org/?format=json failed, reason: getaddrinfo EAI_AGAIN api.ipify.org:443
|
|
|
|
//
|
|
|
|
// It's a Microsoft error: "A temporary failure in name resolution occurred."
|
2022-07-23 09:31:32 +02:00
|
|
|
if (error.code === 'EAI_AGAIN') return true;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// request to https://public-...8fd8bc6bb68e9c4d17a.md failed, reason: connect ETIMEDOUT 204.79.197.213:443
|
|
|
|
// Code: ETIMEDOUT
|
|
|
|
if (error.code === 'ETIMEDOUT') return true;
|
|
|
|
|
|
|
|
// ECONNREFUSED is generally temporary
|
|
|
|
if (error.code === 'ECONNREFUSED') return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchMaxRetry_: 5,
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
fetchMaxRetrySet: (v: number) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
const previous = shim.fetchMaxRetry_;
|
|
|
|
shim.fetchMaxRetry_ = v;
|
|
|
|
return previous;
|
|
|
|
},
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
fetchWithRetry: async function(fetchFn: Function, options: any = null) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
if (!options.timeout) options.timeout = 1000 * 120; // ms
|
|
|
|
if (!('maxRetry' in options)) options.maxRetry = shim.fetchMaxRetry_;
|
|
|
|
|
|
|
|
let retryCount = 0;
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
const response = await fetchFn();
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
if (shim.fetchRequestCanBeRetried(error)) {
|
|
|
|
retryCount++;
|
|
|
|
if (retryCount > options.maxRetry) throw error;
|
2021-01-29 20:45:11 +02:00
|
|
|
await shim.msleep_(retryCount * 3000);
|
2020-10-09 19:35:46 +02:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-05-14 11:29:06 +02:00
|
|
|
fetch: (_url: string, _options: any = null): any => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2021-01-07 18:30:53 +02:00
|
|
|
fetchText: async (url: string, options: any = null): Promise<string> => {
|
|
|
|
const r = await shim.fetch(url, options || {});
|
|
|
|
if (!r.ok) throw new Error(`Could not fetch ${url}`);
|
|
|
|
return r.text();
|
|
|
|
},
|
|
|
|
|
2020-11-14 14:37:18 +02:00
|
|
|
createResourceFromPath: async (_filePath: string, _defaultProps: any = null, _options: any = null): Promise<ResourceEntity> => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
FormData: typeof FormData !== 'undefined' ? FormData : null,
|
|
|
|
|
2023-12-06 21:24:00 +02:00
|
|
|
fsDriver: (): FsDriverBase => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
FileApiDriverLocal: null as any,
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
readLocalFileBase64: (_path: string) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-12-28 13:48:47 +02:00
|
|
|
uploadBlob: (_url: string, _options: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
sjclModule: null as any,
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
randomBytes: async (_count: number) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
stringByteLength: (_s: string) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
detectAndSetLocale: null as Function,
|
|
|
|
|
2021-06-20 20:29:34 +02:00
|
|
|
attachFileToNote: async (_note: any, _filePath: string): Promise<NoteEntity> => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
attachFileToNoteBody: async (_body: string, _filePath: string, _position: number, _options: any): Promise<string> => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2023-06-30 10:11:26 +02:00
|
|
|
imageToDataUrl: async (_filePath: string, _maxSize = 0): Promise<string> => {
|
2022-02-06 18:42:00 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
imageFromDataUrl: async (_imageDataUrl: string, _filePath: string, _options: any = null) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2021-01-07 18:30:53 +02:00
|
|
|
fetchBlob: function(_url: string, _options: any = null): any {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
Buffer: null as any,
|
|
|
|
|
|
|
|
openUrl: () => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
httpAgent: () => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
openOrCreateFile: (_path: string, _defaultContents: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
waitForFrame: () => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
appVersion: () => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
injectedJs: (_name: string) => '',
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
isTestingEnv: () => {
|
|
|
|
return isTestingEnv_;
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
setIsTestingEnv: (v: boolean) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
isTestingEnv_ = v;
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
pathRelativeToCwd: (_path: string) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
showMessageBox: (_message: string, _options: any = null) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
writeImageToFile: (_image: any, _format: any, _filePath: string) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
|
|
|
// setTimeout/setInterval are in the shim so that in Electron renderer process
|
|
|
|
// we can force the use of the Node timers from the "timers" package. This is to go around
|
|
|
|
// a bug that happened while testing plugins (on WSL2 / X-Server, so same as Linux):
|
|
|
|
//
|
|
|
|
// - The plugin would change some text in the editor
|
|
|
|
// - The editor would schedule a save using setTimeout (via AsyncActionQueue)
|
|
|
|
// - The timer would never get fired (although setTimeout was definitely called)
|
|
|
|
//
|
|
|
|
// It's not clear whether it is due to the code path originating in the plugin script or
|
|
|
|
// some other bugs. But in any case, the issue was fixed using require('timers').setTimeout
|
|
|
|
// instead of the default window.setTimeout. Might be related to this Electron bug:
|
|
|
|
// https://github.com/electron/electron/issues/25311
|
|
|
|
// (although changing allowRendererProcessReuse solved nothing)
|
|
|
|
//
|
|
|
|
// These unreliable timers might also be the reason for hard to replicate bugs in file watching
|
|
|
|
// or the synchronisation freeze bug on Linux.
|
|
|
|
//
|
|
|
|
// Having the timers wrapped in that way would also make it easier to debug timing issue and
|
|
|
|
// find out what timers have been fired or not.
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
setTimeout: (_fn: Function, _interval: number) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
setInterval: (_fn: Function, _interval: number) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
clearTimeout: (_id: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
clearInterval: (_id: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
2020-11-05 18:58:23 +02:00
|
|
|
|
2021-10-01 20:35:27 +02:00
|
|
|
setNodeSqlite: (nodeSqlite: any) => {
|
|
|
|
nodeSqlite_ = nodeSqlite;
|
|
|
|
},
|
|
|
|
|
|
|
|
nodeSqlite: () => {
|
|
|
|
if (!nodeSqlite_) throw new Error('Trying to access nodeSqlite before it has been set!!!');
|
|
|
|
return nodeSqlite_;
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
setReact: (react: any) => {
|
2020-11-05 18:58:23 +02:00
|
|
|
react_ = react;
|
|
|
|
},
|
|
|
|
|
|
|
|
react: () => {
|
|
|
|
if (!react_) throw new Error('Trying to access React before it has been set!!!');
|
|
|
|
return react_;
|
|
|
|
},
|
|
|
|
|
2021-11-17 14:54:34 +02:00
|
|
|
dgram: () => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
platformSupportsKeyChain: () => {
|
|
|
|
// keytar throws an error when system keychain is not present; even
|
|
|
|
// when keytar itself is installed. try/catch to ensure system
|
|
|
|
// keychain is present and no error is thrown.
|
|
|
|
|
|
|
|
// For now, keychain support is disabled on Linux because when
|
|
|
|
// keytar is loaded it seems to cause the following error when
|
|
|
|
// loading Sharp:
|
|
|
|
//
|
|
|
|
// Something went wrong installing the "sharp" module
|
|
|
|
// /lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found
|
|
|
|
// (required by
|
|
|
|
// /home/travis/build/laurent22/joplin/packages/app-cli/node_modules/sharp/build/Release/../../vendor/lib/libpng16.so.16)
|
|
|
|
//
|
|
|
|
// See: https://travis-ci.org/github/laurent22/joplin/jobs/686222036
|
|
|
|
//
|
|
|
|
// Also disabled in portable mode obviously.
|
|
|
|
|
|
|
|
return (shim.isWindows() || shim.isMac()) && !shim.isPortable();
|
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
keytar: (): any => {
|
2020-11-05 18:58:23 +02:00
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
2020-12-20 09:52:28 +02:00
|
|
|
|
|
|
|
// In general all imports should be static, but for cases where dynamic
|
|
|
|
// require is needed, we should use the shim so that the code can build in
|
|
|
|
// React Native. In React Native that code path will throw an error, but at
|
|
|
|
// least it will build.
|
|
|
|
// https://stackoverflow.com/questions/55581073
|
|
|
|
requireDynamic: (_path: string): any => {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
},
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default shim;
|