Skip to content

Commit

Permalink
feat(wip): writablestreams for collector and logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
eser committed Jan 29, 2024
1 parent e2cd8c5 commit 523d401
Show file tree
Hide file tree
Showing 41 changed files with 491 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
args: ["--maxkb=1024"]
exclude: |
(?x)^(
bundler/esbuild_v0.19.11.wasm
bundler/esbuild_v0.20.0.wasm
)$
- id: check-case-conflict
- id: check-executables-have-shebangs
Expand Down Expand Up @@ -64,7 +64,7 @@ repos:
.git/COMMIT_EDITMSG|
appserver/README.md|
bundler/README.md|
bundler/esbuild_v0.19.11.wasm|
bundler/esbuild_v0.20.0.wasm|
collector/README.md|
di/README.md|
directives/README.md|
Expand Down
2 changes: 1 addition & 1 deletion _etc/tasks/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as walk from "https://deno.land/std@0.212.0/fs/walk.ts";
export * as walk from "https://deno.land/std@0.213.0/fs/walk.ts";
2 changes: 1 addition & 1 deletion appserver/appserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class AppServer {
awaits: Array<Promise<any>> = [];

constructor() {
this.runMode = runModes.RunMode.NotSet;
this.runMode = runModes.RunModes.NotSet;
this.events = events;
this.di = di;
this.channels = new Map<string, Channel>();
Expand Down
2 changes: 1 addition & 1 deletion appserver/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as semver from "https://deno.land/std@0.212.0/semver/mod.ts";
export * as semver from "https://deno.land/std@0.213.0/semver/mod.ts";
10 changes: 5 additions & 5 deletions bundler/deps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as colors from "https://deno.land/std@0.212.0/fmt/colors.ts";
export * as hex from "https://deno.land/std@0.212.0/encoding/hex.ts";
export * as path from "https://deno.land/std@0.212.0/path/mod.ts";
export * as regexpEscape from "https://deno.land/std@0.212.0/regexp/escape.ts";
export * as colors from "https://deno.land/std@0.213.0/fmt/colors.ts";
export * as hex from "https://deno.land/std@0.213.0/encoding/hex.ts";
export * as path from "https://deno.land/std@0.213.0/path/mod.ts";
export * as regexpEscape from "https://deno.land/std@0.213.0/regexp/escape.ts";

export * as esbuild from "https://deno.land/x/esbuild_deno_loader@0.8.3/mod.ts";
export * as esbuild from "https://deno.land/x/esbuild_deno_loader@0.8.5/mod.ts";
10 changes: 5 additions & 5 deletions bundler/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type BuildOptions,
type OnLoadOptions,
type Plugin,
} from "https://deno.land/x/esbuild@v0.19.11/mod.js";
} from "https://deno.land/x/esbuild@v0.20.0/mod.js";
import * as runtime from "../standards/runtime.ts";
import { esbuild, path, regexpEscape } from "./deps.ts";
import { Builder, BuildSnapshot } from "./mod.ts";
Expand Down Expand Up @@ -43,12 +43,12 @@ export class EsbuildBuilder implements Builder {
const portableBuilder = env["LIME_ESBUILD_LOADER"] === "portable";

// Lazily initialize esbuild
// @deno-types="https://deno.land/x/esbuild@v0.19.11/mod.d.ts"
// @deno-types="https://deno.land/x/esbuild@v0.20.0/mod.d.ts"
const esbuildInstance = isOnDenoDeploy || portableBuilder
? await import("https://deno.land/x/esbuild@v0.19.11/wasm.js")
: await import("https://deno.land/x/esbuild@v0.19.11/mod.js");
? await import("https://deno.land/x/esbuild@v0.20.0/wasm.js")
: await import("https://deno.land/x/esbuild@v0.20.0/mod.js");
const esbuildWasmURL =
new URL("./esbuild_v0.19.11.wasm", import.meta.url).href;
new URL("./esbuild_v0.20.0.wasm", import.meta.url).href;

if (isOnDenoDeploy) {
await esbuildInstance.initialize({
Expand Down
Binary file not shown.
10 changes: 8 additions & 2 deletions collector/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { path, walk } from "./deps.ts";
import * as patterns from "../standards/patterns.ts";
// import { runtime } from "$cool/standards/mod.ts";

export async function* walkFiles(
baseDir: string,
Expand Down Expand Up @@ -35,11 +36,12 @@ export async function* walkFiles(
export interface CollectExportsOptions {
baseDir: string;
globFilter?: string;
exportFilter?: (entries: [string, unknown][]) => [string, unknown][];
exportFilter?: (entries: [string, unknown][]) => Promise<[string, unknown][]>;
ignoreFilePattern?: RegExp;
}

export const collectExports = async (options: CollectExportsOptions) => {
// const mainModule = runtime.current.getMainModule();
const ignoreFilePattern = options.ignoreFilePattern ??
patterns.JS_TEST_FILE_PATTERN;

Expand All @@ -55,6 +57,10 @@ export const collectExports = async (options: CollectExportsOptions) => {
const entryUri = `${options.baseDir}/${entry}`;

try {
// if (`file://${entryUri}` === mainModule) {
// continue;
// }

const entryModule = await import(entryUri);
const moduleExports = Object.entries(entryModule);

Expand All @@ -63,7 +69,7 @@ export const collectExports = async (options: CollectExportsOptions) => {
continue;
}

const selectedExports = options.exportFilter(moduleExports);
const selectedExports = await options.exportFilter(moduleExports);

if (selectedExports.length === 0) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions collector/deps-dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as assert from "https://deno.land/std@0.212.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.212.0/testing/bdd.ts";
export * as assert from "https://deno.land/std@0.213.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.213.0/testing/bdd.ts";
6 changes: 3 additions & 3 deletions collector/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as path from "https://deno.land/std@0.212.0/path/mod.ts";
export * as posix from "https://deno.land/std@0.212.0/path/posix/mod.ts";
export * as walk from "https://deno.land/std@0.212.0/fs/walk.ts";
export * as path from "https://deno.land/std@0.213.0/path/mod.ts";
export * as posix from "https://deno.land/std@0.213.0/path/posix/mod.ts";
export * as walk from "https://deno.land/std@0.213.0/fs/walk.ts";
30 changes: 25 additions & 5 deletions collector/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { path, posix } from "./deps.ts";
import * as runtime from "../standards/runtime.ts";
import * as logger from "../logging/logger.ts";
import * as validatorIdentifier from "./validator-identifier/mod.ts";
import * as collector from "./collector.ts";
import * as formatter from "./formatter.ts";
Expand Down Expand Up @@ -138,23 +139,42 @@ export const manifest = ${manifestSerialized};
};

export const buildManifest = async (
target: WritableStream,
options: collector.CollectExportsOptions,
) => {
const collection = await collector.collectExports(options);

const manifestStr = await writeManifestToString(collection);
const manifestPath = path.join(options.baseDir, "./manifest.gen.ts");

await runtime.current.writeTextFile(manifestPath, manifestStr);
const outputWriter = target.getWriter();
await outputWriter.ready;

const encoded = new TextEncoder().encode(manifestStr);
await outputWriter.write(encoded);

outputWriter.releaseLock();

const exportModules = Object.values(collection);
const exportCount = exportModules.reduce((acc, [, moduleFns]) => {
acc += moduleFns.length;
return acc;
}, 0);

console.log(
`%cThe manifest file has been generated for ${exportCount} exports in ${exportModules.length} modules.`,
"color: blue",
logger.current.info(
`The manifest file has been generated for ${exportCount} exports in ${exportModules.length} modules.`,
);
};

export const buildManifestFile = async (
filepath: string,
options: collector.CollectExportsOptions,
) => {
const target = await runtime.current.open(filepath, {
create: true,
write: true,
});

await buildManifest(target.writable, options);

target.close();
};
4 changes: 3 additions & 1 deletion cool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const repl = async (_args: Array<string>, _options: ExecuteOptions) => {
{
args: [
"repl",
"--unstable",
"--unstable-cron",
"--unstable-kv",
"--unstable-temporal",
"--allow-all",
"--eval-file=https://deno.land/x/cool@0.7.13/repl-init.ts",
],
Expand Down
4 changes: 2 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
]
},
"tasks": {
"cleanup": "rm -rf ./_etc/coverage/ ./_etc/coverage.lcov ./deno.lock && deno cache --unstable --reload ./mod.ts",
"cleanup": "rm -rf ./_etc/coverage/ ./_etc/coverage.lcov ./deno.lock && deno cache --unstable-cron --unstable-kv --unstable-temporal --reload ./mod.ts",
"check:license": "deno run --allow-read --allow-write ./_etc/tasks/check-license.ts",
"check:mod": "deno run --check --reload ./mod.ts",
"doc:lint": "deno doc --lint ./mod.ts",
Expand All @@ -55,7 +55,7 @@
"test:coverage": "deno coverage ./_etc/coverage/ --exclude='\\.(j|t)sx$'",
"test:coverage-gen": "deno coverage ./_etc/coverage --exclude='\\.(j|t)sx$' --lcov --output=./_etc/coverage.lcov",
"ok": "deno fmt --check && deno lint && deno task check:license --check && deno task check:mod && deno task test:run",
"repl": "deno repl --unstable --allow-all --eval-file=./repl-init.ts",
"repl": "deno repl --unstable-cron --unstable-kv --unstable-temporal --allow-all --eval-file=./repl-init.ts",
"container:build": "docker build -t cool .",
"container:run": "docker run --interactive --tty --rm cool"
}
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as posix from "https://deno.land/std@0.212.0/path/posix/mod.ts";
export * as posix from "https://deno.land/std@0.213.0/path/posix/mod.ts";
23 changes: 12 additions & 11 deletions di/container.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

import { type Promisable } from "../standards/promises.ts";
import { type AnonymousFunction } from "../standards/functions.ts";
import { type GenericFunction } from "../standards/functions.ts";
import {
type PromisableBuilder,
type ServiceDescriptor,
type ServiceKey,
type ServiceRegistry,
type ServiceResolution,
type ServiceScope,
ServiceType,
ServiceTypes,
type ServiceValue,
} from "./primitives.ts";
import { invoke } from "./invoker.ts";
Expand All @@ -19,25 +19,25 @@ export class Registry<K = ServiceKey, V = ServiceValue>
descriptors = new Map<K, ServiceDescriptor<V>>();

set(token: K, value: Promisable<V>) {
this.descriptors.set(token, [ServiceType.Singleton, value]);
this.descriptors.set(token, [ServiceTypes.Singleton, value]);

return this;
}

setLazy(token: K, value: PromisableBuilder<V>) {
this.descriptors.set(token, [ServiceType.Lazy, value]);
this.descriptors.set(token, [ServiceTypes.Lazy, value]);

return this;
}

setScoped(token: K, value: PromisableBuilder<V>) {
this.descriptors.set(token, [ServiceType.Scoped, value]);
this.descriptors.set(token, [ServiceTypes.Scoped, value]);

return this;
}

setTransient(token: K, value: PromisableBuilder<V>) {
this.descriptors.set(token, [ServiceType.Transient, value]);
this.descriptors.set(token, [ServiceTypes.Transient, value]);

return this;
}
Expand Down Expand Up @@ -68,14 +68,15 @@ export class Scope<K = ServiceKey, V = ServiceValue>
return defaultValue;
}

if (descriptor[0] === ServiceType.Singleton) {
if (descriptor[0] === ServiceTypes.Singleton) {
return descriptor[1] as Promisable<V2>;
}

if (
descriptor[0] === ServiceType.Lazy || descriptor[0] === ServiceType.Scoped
descriptor[0] === ServiceTypes.Lazy ||
descriptor[0] === ServiceTypes.Scoped
) {
const targetScope = (descriptor[0] === ServiceType.Scoped)
const targetScope = (descriptor[0] === ServiceTypes.Scoped)
? this
: this.rootScope;
const stored = targetScope.items.get(token);
Expand All @@ -100,7 +101,7 @@ export class Scope<K = ServiceKey, V = ServiceValue>
return result as ServiceResolution<V2>;
}

// if (descriptor[0] === ServiceType.Transient) {
// if (descriptor[0] === ServiceTypes.Transient) {
const value = descriptor[1] as PromisableBuilder<V>;
const result = value(this);

Expand All @@ -112,7 +113,7 @@ export class Scope<K = ServiceKey, V = ServiceValue>
return tokens.map((token) => this.get(token));
}

invoke<T extends AnonymousFunction>(fn: T): ReturnType<T> {
invoke<T extends GenericFunction>(fn: T): ReturnType<T> {
return invoke(this, fn);
}

Expand Down
6 changes: 3 additions & 3 deletions di/deps-dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

export * as assert from "https://deno.land/std@0.212.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.212.0/testing/bdd.ts";
export * as mock from "https://deno.land/std@0.212.0/testing/mock.ts";
export * as assert from "https://deno.land/std@0.213.0/assert/mod.ts";
export * as bdd from "https://deno.land/std@0.213.0/testing/bdd.ts";
export * as mock from "https://deno.land/std@0.213.0/testing/mock.ts";
4 changes: 2 additions & 2 deletions di/fluent-api-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

import { type Promisable } from "../standards/promises.ts";
import { type AnonymousFunction } from "../standards/functions.ts";
import { type GenericFunction } from "../standards/functions.ts";
import {
type PromisableBuilder,
type ServiceKey,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const factory = <K = ServiceKey, V = ServiceValue>(

di.many = (...tokens: ReadonlyArray<K>) => services.getMany(...tokens);

di.invoke = <T extends AnonymousFunction>(fn: T): ReturnType<T> =>
di.invoke = <T extends GenericFunction>(fn: T): ReturnType<T> =>
services.invoke(fn);

di.scope = () => services.createScope();
Expand Down
6 changes: 3 additions & 3 deletions di/invoker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.

import { type AnonymousFunction } from "../standards/functions.ts";
import { type GenericFunction } from "../standards/functions.ts";
import {
type ServiceKey,
type ServiceScope,
Expand All @@ -17,7 +17,7 @@ const getFunctionParametersFromString = (fnSerialized: string) => {
return [];
};

const getFunctionParameters = (fn: AnonymousFunction) => {
const getFunctionParameters = (fn: GenericFunction) => {
return getFunctionParametersFromString(fn.toString());
};

Expand Down Expand Up @@ -56,7 +56,7 @@ const getFunctionParameters = (fn: AnonymousFunction) => {
// };

export const invoke = <
T extends AnonymousFunction,
T extends GenericFunction,
K = ServiceKey,
V = ServiceValue,
>(
Expand Down
Loading

0 comments on commit 523d401

Please sign in to comment.