-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "loadPath" options to runtime API and server adapter options (
#696)
- Loading branch information
Showing
35 changed files
with
2,286 additions
and
2,037 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './constants'; | ||
export * from './enhancements'; | ||
export * from './error'; | ||
export * from './loader'; | ||
export * from './types'; | ||
export * from './validation'; | ||
export * from './version'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import path from 'path'; | ||
import { DEFAULT_RUNTIME_LOAD_PATH } from './constants'; | ||
import { ModelMeta, PolicyDef, ZodSchemas } from './enhancements'; | ||
|
||
/** | ||
* Load model metadata. | ||
* | ||
* @param loadPath The path to load model metadata from. If not provided, | ||
* will use default load path. | ||
*/ | ||
export function getDefaultModelMeta(loadPath: string | undefined): ModelMeta { | ||
const toLoad = loadPath ? path.resolve(loadPath, 'model-meta') : `${DEFAULT_RUNTIME_LOAD_PATH}/model-meta`; | ||
try { | ||
// normal load | ||
return require(toLoad).default; | ||
} catch { | ||
if (process.env.ZENSTACK_TEST === '1' && !path.isAbsolute(toLoad)) { | ||
try { | ||
// special handling for running as tests, try resolving relative to CWD | ||
return require(path.join(process.cwd(), 'node_modules', toLoad)).default; | ||
} catch { | ||
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.'); | ||
} | ||
} | ||
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.'); | ||
} | ||
} | ||
|
||
/** | ||
* Load access policies. | ||
* | ||
* @param loadPath The path to load access policies from. If not provided, | ||
* will use default load path. | ||
*/ | ||
export function getDefaultPolicy(loadPath: string | undefined): PolicyDef { | ||
const toLoad = loadPath ? path.resolve(loadPath, 'policy') : `${DEFAULT_RUNTIME_LOAD_PATH}/policy`; | ||
try { | ||
return require(toLoad).default; | ||
} catch { | ||
if (process.env.ZENSTACK_TEST === '1' && !path.isAbsolute(toLoad)) { | ||
try { | ||
// special handling for running as tests, try resolving relative to CWD | ||
return require(path.join(process.cwd(), 'node_modules', toLoad)).default; | ||
} catch { | ||
throw new Error( | ||
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.' | ||
); | ||
} | ||
} | ||
throw new Error( | ||
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Load zod schemas. | ||
* | ||
* @param loadPath The path to load zod schemas from. If not provided, | ||
* will use default load path. | ||
*/ | ||
export function getDefaultZodSchemas(loadPath: string | undefined): ZodSchemas | undefined { | ||
const toLoad = loadPath ? path.resolve(loadPath, 'zod') : `${DEFAULT_RUNTIME_LOAD_PATH}/zod`; | ||
try { | ||
return require(toLoad); | ||
} catch { | ||
if (process.env.ZENSTACK_TEST === '1' && !path.isAbsolute(toLoad)) { | ||
try { | ||
// special handling for running as tests, try resolving relative to CWD | ||
return require(path.join(process.cwd(), 'node_modules', toLoad)); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.