Skip to content

Commit

Permalink
get rid of proper-lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Mar 8, 2023
1 parent 0225d5d commit 23bb8df
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
2 changes: 0 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@types/lodash.debounce": "4.0.3",
"@types/lodash.throttle": "^4.1.3",
"@types/markdown-it": "^12.2.3",
"@types/proper-lockfile": "^4.1.2",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@types/route-parser": "^0.1.1",
Expand Down Expand Up @@ -60,7 +59,6 @@
"nsfw": "^2.2.4",
"p-debounce": "^2.1.0",
"perfect-scrollbar": "^1.3.0",
"proper-lockfile": "^4.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-tooltip": "^4.2.21",
Expand Down
28 changes: 12 additions & 16 deletions packages/core/src/node/filesystem-locking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

import { Mutex } from 'async-mutex';
import { injectable, interfaces } from 'inversify';
import lockfile = require('proper-lockfile');
import type { URI } from '../common';
import { FileUri } from './file-uri';
import path = require('path');
import * as fs from 'fs';
import * as path from 'path';

export const FileSystemLocking = Symbol('FileSystemLocking') as symbol & interfaces.Abstract<FileSystemLocking>;
/**
Expand All @@ -32,27 +30,25 @@ export interface FileSystemLocking {
* @param transaction The job to do while having exclusive access.
* @param thisArg `this` argument used when calling `transaction`.
*/
lockPath<T>(lockPath: string | URI, transaction: (lockPath: string) => T | Promise<T>, thisArg?: unknown): Promise<T>;
lockPath<T>(lockPath: string, transaction: (lockPath: string) => T | Promise<T>, thisArg?: unknown): Promise<T>;
}

@injectable()
export class FileSystemLockingImpl implements FileSystemLocking {

lockPath<T>(lockPath: string | URI, transaction: (lockPath: string) => T | Promise<T>, thisArg?: unknown): Promise<T> {
lockPath<T>(lockPath: string, transaction: (lockPath: string) => T | Promise<T>, thisArg?: unknown): Promise<T> {
const resolvedLockPath = this.resolveLockPath(lockPath);
return this.getLock(resolvedLockPath).runExclusive(async () => {
const releaseFilelock = await lockfile.lock(resolvedLockPath);
try {
return await transaction.call(thisArg, resolvedLockPath);
} finally {
releaseFilelock();
}
});
return this.getLock(resolvedLockPath).runExclusive(async () => transaction.call(thisArg, resolvedLockPath));
}

protected resolveLockPath(lockPath: string | URI): string {
protected resolveLockPath(lockPath: string): string {
// try to normalize the path to avoid two paths pointing to the same file
return path.resolve(FileUri.fsPath(lockPath));
return path.resolve(lockPath);
}

protected async touchFile(filePath: string): Promise<void> {
const file = await fs.promises.open(filePath, fs.constants.O_CREAT);
await file.close();
}

protected getLocks(): Map<string, Mutex> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import 'reflect-metadata';
import { expect } from 'chai';
import { Container } from '@theia/core/shared/inversify';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
Expand All @@ -30,7 +32,9 @@ import * as temp from 'temp';
const GlobalStorageKind = undefined;

describe('Plugins Key Value Storage', () => {

let container: Container;

beforeEach(async () => {
container = new Container();
container.bind(PluginsKeyValueStorage).toSelf().inSingletonScope();
Expand All @@ -43,6 +47,10 @@ describe('Plugins Key Value Storage', () => {
expect(await getNumEntries(storage), 'Expected that storage should initially be empty').to.equal(0);
});

afterEach(() => {
container.get(PluginsKeyValueStorage)['dispose']();
});

it('Should be able to set and overwrite a storage entry', async () => {
const aKey = 'akey';
const aValue = { 'this is a test': 'abc' };
Expand Down
21 changes: 14 additions & 7 deletions packages/plugin-ext/src/main/node/plugins-key-value-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class PluginsKeyValueStorage {

private stores: Record<string, KeysToKeysToAnyValue> = Object.create(null);
private storesToSync = new Map<string, KeysToKeysToAnyValue>();
private syncStoresTimeout: NodeJS.Timeout;
private syncStoresTimeout?: NodeJS.Timeout;

private deferredGlobalDataPath = new Deferred<string | undefined>();

Expand All @@ -51,7 +51,7 @@ export class PluginsKeyValueStorage {
console.error('Failed to initialize global state path:', error);
return undefined;
}));
process.once('beforeExit', () => clearTimeout(this.syncStoresTimeout));
process.once('beforeExit', () => this.dispose());
this.syncStores();
}

Expand Down Expand Up @@ -106,16 +106,18 @@ export class PluginsKeyValueStorage {

private syncStores(): void {
this.syncStoresTimeout = setTimeout(async () => {
await Promise.all(Array.from(this.storesToSync, async ([dataPath, store]) => {
await this.fsLocking.lockPath(dataPath, async resolved => {
const storeOnDisk = await this.readFromFile(dataPath);
await Promise.all(Array.from(this.storesToSync, async ([storePath, store]) => {
await this.fsLocking.lockPath(storePath, async resolved => {
const storeOnDisk = await this.readFromFile(storePath);
const updatedStore = deepmerge(storeOnDisk, store);
this.stores[dataPath] = updatedStore;
this.stores[storePath] = updatedStore;
await this.writeToFile(resolved, updatedStore);
});
}));
this.storesToSync.clear();
this.syncStores();
if (this.syncStoresTimeout) {
this.syncStores();
}
}, 60_000);
}

Expand Down Expand Up @@ -145,4 +147,9 @@ export class PluginsKeyValueStorage {
await fs.ensureDir(path.dirname(pathToFile));
await fs.writeJSON(pathToFile, data);
}

private dispose(): void {
clearTimeout(this.syncStoresTimeout);
this.syncStoresTimeout = undefined;
}
}
21 changes: 0 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2092,13 +2092,6 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==

"@types/proper-lockfile@^4.1.2":
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/proper-lockfile/-/proper-lockfile-4.1.2.tgz#49537cee7134055ee13a1833b76a1c298f39bb26"
integrity sha512-kd4LMvcnpYkspDcp7rmXKedn8iJSCoa331zRRamUp5oanKt/CefbEGPQP7G89enz7sKD4bvsr8mHSsC8j5WOvA==
dependencies:
"@types/retry" "*"

"@types/proxy-from-env@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.1.tgz#b5f3e99230ca4518af196c18267055fc51f892b7"
Expand Down Expand Up @@ -2161,11 +2154,6 @@
dependencies:
"@types/node" "*"

"@types/retry@*":
version "0.12.2"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.2.tgz#ed279a64fa438bb69f2480eda44937912bb7480a"
integrity sha512-XISRgDJ2Tc5q4TRqvgJtzsRkFYNJzZrhTdtMoGVBttwzzQJkPnS3WWTFc7kuDRoPtPakl+T+OfdEUjYJj7Jbow==

"@types/rimraf@^2.0.2":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-2.0.5.tgz#368fb04d59630b727fc05a74d2ca557f64a8ef98"
Expand Down Expand Up @@ -9191,15 +9179,6 @@ prop-types@^15.5.6, prop-types@^15.6.1, prop-types@^15.8.1:
object-assign "^4.1.1"
react-is "^16.13.1"

proper-lockfile@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/proper-lockfile/-/proper-lockfile-4.1.2.tgz#c8b9de2af6b2f1601067f98e01ac66baa223141f"
integrity sha512-TjNPblN4BwAWMXU8s9AEz4JmQxnD1NNL7bNOY/AKUzyamc379FWASUhc/K1pL2noVb+XmZKLL68cjzLsiOAMaA==
dependencies:
graceful-fs "^4.2.4"
retry "^0.12.0"
signal-exit "^3.0.2"

properties@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/properties/-/properties-1.2.1.tgz#0ee97a7fc020b1a2a55b8659eda4aa8d869094bd"
Expand Down

0 comments on commit 23bb8df

Please sign in to comment.