From e90a8bf064963dd448cfa67da5af0f9aad957999 Mon Sep 17 00:00:00 2001 From: Zixuan Liu Date: Fri, 24 Feb 2023 23:10:57 +0800 Subject: [PATCH] fix: using import() to load the fs (#435) Signed-off-by: Zixuan Liu --- src/enforcer.ts | 20 +++++++++++++++++++- src/index.ts | 14 -------------- test/config/config.test.ts | 3 ++- test/model/model.test.ts | 6 ------ test/persist/fileSystem.test.ts | 2 +- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/enforcer.ts b/src/enforcer.ts index 7b301e2..832aa7f 100644 --- a/src/enforcer.ts +++ b/src/enforcer.ts @@ -14,7 +14,7 @@ import { ManagementEnforcer } from './managementEnforcer'; import { Model, newModelFromFile } from './model'; -import { Adapter, FileAdapter, StringAdapter } from './persist'; +import { Adapter, FileAdapter, getDefaultFileSystem, setDefaultFileSystem, StringAdapter } from './persist'; import { getLogger } from './log'; import { arrayRemoveDuplicates } from './util'; import { FieldIndex } from './constants'; @@ -467,6 +467,24 @@ export class Enforcer extends ManagementEnforcer { } export async function newEnforcerWithClass(enforcer: new () => T, ...params: any[]): Promise { + // inject the FS + if (!getDefaultFileSystem()) { + try { + if (typeof process !== 'undefined' && process?.versions?.node) { + const fs = await import('fs'); + const defaultFileSystem = { + readFileSync(path: string, encoding?: string) { + return fs.readFileSync(path, { encoding }); + }, + writeFileSync(path: string, text: string, encoding?: string) { + return fs.writeFileSync(path, text, encoding); + }, + }; + setDefaultFileSystem(defaultFileSystem); + } + } catch (ignored) {} + } + const e = new enforcer(); let parsedParamLen = 0; diff --git a/src/index.ts b/src/index.ts index 0e827bd..6658ac2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,17 +26,3 @@ export * from './rbac'; export * from './log'; export * from './frontend'; export { Util }; - -if (typeof process !== 'undefined' && process?.versions?.node) { - const requireFunc = typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require; - const fs = requireFunc('fs'); - const defaultFileSystem = { - readFileSync(path: string, encoding?: string) { - return fs.readFileSync(path, { encoding }); - }, - writeFileSync(path: string, text: string, encoding?: string) { - return fs.writeFileSync(path, text, encoding); - }, - }; - setDefaultFileSystem(defaultFileSystem); -} diff --git a/test/config/config.test.ts b/test/config/config.test.ts index 553d0fe..9bdae02 100644 --- a/test/config/config.test.ts +++ b/test/config/config.test.ts @@ -1,6 +1,7 @@ import { Config } from '../../src'; +import { readFileSync } from 'fs'; -const config = Config.newConfig('test/config/testini.ini'); +const config = Config.newConfigFromText(readFileSync('test/config/testini.ini').toString()); describe('multi-line test', () => { it('should config.get("multi1::name") to equal r.sub==p.sub&&r.obj==p.obj', function () { diff --git a/test/model/model.test.ts b/test/model/model.test.ts index 43a3451..7dedd59 100644 --- a/test/model/model.test.ts +++ b/test/model/model.test.ts @@ -43,12 +43,6 @@ test('TestNewModel', () => { expect(m !== null).toBe(true); }); -test('TestNewModelFromFile', () => { - const m = newModelFromFile(basicExample); - - expect(m !== null).toBe(true); -}); - test('TestNewModelFromString', () => { const m = newModelFromString(readFileSync(basicExample).toString()); diff --git a/test/persist/fileSystem.test.ts b/test/persist/fileSystem.test.ts index 84db508..2747324 100644 --- a/test/persist/fileSystem.test.ts +++ b/test/persist/fileSystem.test.ts @@ -16,7 +16,7 @@ import { FileSystem, getDefaultFileSystem, setDefaultFileSystem } from '../../sr test('get an set the default system', async () => { let fs = getDefaultFileSystem(); - expect(fs).toBeDefined(); + expect(fs).toBeUndefined(); const defaultFileSystem: FileSystem = { readFileSync(path: string, encoding?: string): Buffer { // noop