Skip to content

Commit

Permalink
fix: using import() to load the fs (#435)
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
  • Loading branch information
nodece authored Feb 24, 2023
1 parent 5d5a7f1 commit e90a8bf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
20 changes: 19 additions & 1 deletion src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -467,6 +467,24 @@ export class Enforcer extends ManagementEnforcer {
}

export async function newEnforcerWithClass<T extends Enforcer>(enforcer: new () => T, ...params: any[]): Promise<T> {
// 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;
Expand Down
14 changes: 0 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion test/config/config.test.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
6 changes: 0 additions & 6 deletions test/model/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion test/persist/fileSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e90a8bf

Please sign in to comment.