Skip to content

Commit

Permalink
fix: removes legacy file config-accessor (BREAKING CHANGE)
Browse files Browse the repository at this point in the history
  • Loading branch information
Figedi committed Aug 27, 2023
1 parent 9c3af3e commit 9838d59
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 69 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ Base svc-framework for µ-services.
## TODO
Replace them with non nodejs related versions for edge compat
- axios <-- replace with isomorphic fetch
- split k8s + app + utils + interfaces into separate packages, use turbo for management
- make jsondecryptor optional in remoteConfig
- update readme
- remoteConfigFactory infer more default values from app-builder (e.g. serviceName, logger, poll, etc)

## Features

This framework is the base for most of my µ-services. Some functionalities include:
Expand Down
5 changes: 0 additions & 5 deletions src/app/ApplicationBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ describe("ApplicationBuilder", function AppBuilderTest() {
.addConfig(({ $env }) => ({
a: {
deep: {
fileVal: $env.file(
({ app }) => `${app.rootPath}/resources/example.json`,
// for testability, the file is being parsed as json
fileContent => ({ parsedContent: JSON.parse(fileContent.toString("utf-8")) }),
),
bool: $env.bool(),
overwriteable: $env.str(),
str: $env.str({ choices: ["example_string", "whatup"] }),
Expand Down
25 changes: 1 addition & 24 deletions src/app/ApplicationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import type {
Command,
ResolveRegisterFnArgs,
BaseRegisterFnArgs,
FileTransformFn,
FileTransformConfig,
EnvalidTransformer,
AnyTransformStrict,
InferredOptionType,
Expand Down Expand Up @@ -49,7 +47,7 @@ import { ShutdownHandle, ErrorHandle, REF_SYMBOLS, REF_TYPES, isTransformer } fr
import buildOptions from "minimist-options";
import minimist from "minimist";
import { MissingCommandArgsError } from "./errors";
import { getRootDir, safeReadFile } from "./utils/util";
import { getRootDir } from "./utils/util";
import type { BaseRemoteSource } from "./remoteConfig/remoteSource/BaseRemoteSource";
import { DynamicConfigSource } from "./remoteConfig/remoteSource/DynamicConfigSource";
import _debug from "debug";
Expand Down Expand Up @@ -80,13 +78,6 @@ const ref: RefTransformFn = (referenceValue, refTransformFn) => ({
__sym: REF_SYMBOLS.REF,
});

const file: FileTransformFn = (filePath, fileTransformFn) => ({
filePath,
fileTransformFn,
__type: REF_TYPES.FILE,
__sym: REF_SYMBOLS.FILE,
});

const once: DynamicOnceTransformFn<any> = propGetter => ({
propGetter,
__type: REF_TYPES.DYNAMIC_ONCE,
Expand Down Expand Up @@ -118,7 +109,6 @@ const $env: EnvalidTransformer = {
port,
url,
json,
file,
any: env,
ref,
};
Expand Down Expand Up @@ -321,19 +311,6 @@ export class ApplicationBuilder<Config> {
}

private configTransformers: TreeNodeTransformerConfig[] = [
{
predicate: value => !!value && value.__type === REF_TYPES.FILE,
transform: async ({ filePath, fileTransformFn }: FileTransformConfig) => {
const resolvedPath = typeof filePath === "function" ? filePath({ app: this.app }) : filePath;
// @todo guard clause for non node envs for better errors
const fileContent = await safeReadFile(resolvedPath);
if (fileTransformFn) {
const result = await fileTransformFn?.(fileContent);
return result;
}
return fileContent;
},
},
{
predicate: value => !!value && value.__type === REF_TYPES.ENV,
transform: ({ transformFn, defaultValue }: EnvTransformConfig, path) => {
Expand Down
18 changes: 0 additions & 18 deletions src/app/types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export type EnvFn<Config extends Record<string, any>> = (
export type EnvalidTransformer = {
any: EnvTransformFn;
ref: RefTransformFn;
file: FileTransformFn;
str: <T extends string = string>(spec?: Spec<T>) => ValidatorSpec<string>;
host: <T extends string = string>(spec?: Spec<T>) => ValidatorSpec<string>;
url: <T extends string = string>(spec?: Spec<T>) => ValidatorSpec<string>;
Expand All @@ -73,7 +72,6 @@ export type EnvalidTransformer = {
export const REF_TYPES = {
ENV: 0,
REF: 1,
FILE: 2,
DYNAMIC_ONCE: 3,
DYNAMIC_STREAMED: 4,
DYNAMIC_PROMISE: 5,
Expand All @@ -83,7 +81,6 @@ export const REF_TYPES = {
export const REF_SYMBOLS = {
ENV: Symbol.for("@figedi/svc-transform-env"),
REF: Symbol.for("@figedi/svc-transform-ref"),
FILE: Symbol.for("@figedi/svc-transform-file"),
DYNAMIC_ONCE: Symbol.for("@figedi/svc-transform-dynamic-once"),
DYNAMIC_STREAMED: Symbol.for("@figedi/svc-transform-dynamic-streamed"),
DYNAMIC_PROMISE: Symbol.for("@figedi/svc-transform-dynamic-promise"),
Expand All @@ -100,11 +97,6 @@ export type RefTransformFn = <ReturnValue = string>(
refTransformFn?: (value?: string) => ReturnValue,
) => RefTransformConfig<ReturnValue>;

export type FileTransformFn = <ReturnValue = Buffer>(
filePath: string | ((env: Omit<DependencyArgs, "$env">) => string),
fileTransformFn?: (value: Buffer) => ReturnValue,
) => FileTransformConfig<ReturnValue>;

export type DynamicOnceTransformFn<RemoteConfig> = <ReturnValue = string>(
propGetter?: (config: RemoteConfig) => ReturnValue,
) => DynamicOnceTransformConfig<RemoteConfig, ReturnValue>;
Expand Down Expand Up @@ -135,13 +127,6 @@ export interface RefTransformConfig<ReturnValue = string> {
refTransformFn?: (value?: string) => ReturnValue;
}

export interface FileTransformConfig<ReturnValue = Buffer> {
__type: typeof REF_TYPES.FILE;
__sym: symbol;
filePath: string | ((env: Omit<DependencyArgs, "$env">) => string);
fileTransformFn?: (fileBuffer: Buffer) => ReturnValue | Promise<ReturnValue>;
}

export interface DynamicOnceTransformConfig<Config, ReturnValue = string> {
__type: typeof REF_TYPES.DYNAMIC_ONCE;
__sym: symbol;
Expand All @@ -166,7 +151,6 @@ export interface DynamicObservableTransformConfig<ReturnValue = string> {

export type UnpackEnvConfig<T> = T extends EnvTransformConfig<infer V> ? V : never;
export type UnpackRefConfig<T> = T extends RefTransformConfig<infer V> ? V : never;
export type UnpackFileConfig<T> = T extends FileTransformConfig<infer V> ? Promise<V> : never;
export type UnpackValidatorSpec<T> = T extends ValidatorSpec<infer V> ? V : never;
export type UnpackDynamicOnceConfig<T> = T extends DynamicOnceTransformConfig<infer V, infer K>
? IOnceRemoteConfigValue<V, K>
Expand All @@ -182,7 +166,6 @@ export type UnpackDynamicObservableConfig<T> = T extends DynamicObservableTransf
type Unpacked<T> =
| UnpackRefConfig<T>
| UnpackEnvConfig<T>
| UnpackFileConfig<T>
| UnpackValidatorSpec<T>
| UnpackDynamicOnceConfig<T>
| UnpackDynamicStreamedConfig<T>
Expand All @@ -192,7 +175,6 @@ type Unpacked<T> =
export type InternalTransform<T, TSchema = any> =
| EnvTransformConfig<T>
| RefTransformConfig<T>
| FileTransformConfig<T>
| DynamicOnceTransformConfig<TSchema, T>
| DynamicStreamedTransformConfig<TSchema, T>
| DynamicPromiseTransformConfig<T>
Expand Down
9 changes: 0 additions & 9 deletions src/app/utils/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
export const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;

// @todo wait for https://nodejs.org/api/esm.html#importmetaresolvespecifier-parent
export const safeReadFile = async (filePath: string): Promise<Buffer> => {
if (isNode) {
return import("node:fs/promises").then(mod => mod.readFile(filePath));
}
// @todo implement
throw new Error("edge-runtime not supported yet");
};

const getFirstPartFromNodeModules = (resolved: string): string | null => {
if (resolved.indexOf("/node_modules") !== -1) {
const parts = resolved.split("/node_modules");
Expand Down
5 changes: 0 additions & 5 deletions test-edge-runtime-env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ ApplicationBuilder.create({
.addConfig(({ $env }) => ({
a: {
deep: {
fileVal: $env.file(
({ app }) => app.rootPath + '/resources/example.json',
// for testability, the file is being parsed as json
fileContent => ({ parsedContent: JSON.parse(fileContent.toString("utf-8")) }),
),
bool: $env.bool(),
str: $env.str({ choices: ["example_string", "whatup"] }),
json: $env.json(),
Expand Down
5 changes: 0 additions & 5 deletions test-worker-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ ApplicationBuilder.create({
.addConfig(({ $env }) => ({
a: {
deep: {
fileVal: $env.file(
({ app }) => `${app.rootPath}/resources/example.json`,
// for testability, the file is being parsed as json
fileContent => ({ parsedContent: JSON.parse(fileContent.toString("utf-8")) }),
),
bool: $env.bool(),
str: $env.str({ choices: ["example_string", "whatup"] }),
json: $env.json(),
Expand Down
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
outDir: "dist",
sourcemap: false,
dts: true,
shims: true,
shims: false,
clean: true,
treeshake: true,
});

0 comments on commit 9838d59

Please sign in to comment.