Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scoped Binding #71

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/lunox-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/auth",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Authentication",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-build/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/build",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox helper for rollup",
"main": "index.js",
"files": [
Expand Down
1 change: 1 addition & 0 deletions packages/lunox-core/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dist/
client/
bin/
.tsup/
test/
1 change: 1 addition & 0 deletions packages/lunox-core/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ src/View/Vite*.ts
*.yaml
coverage/
storage/
test/
2 changes: 1 addition & 1 deletion packages/lunox-core/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />
/// <reference types="@lunoxjs/test/global" />
/// <reference types="../lunox-test/global" />

import type {
Application,
Expand Down
4 changes: 2 additions & 2 deletions packages/lunox-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/core",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Laravel-Flavoured NodeJs framework",
"bin": {
"lunox": "./bin/lunox.cjs"
Expand Down Expand Up @@ -101,4 +101,4 @@
"vitest": "^0.34.6"
},
"type": "module"
}
}
4 changes: 2 additions & 2 deletions packages/lunox-core/src/Console/Kernel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {version} from "../../package.json"
import { version } from "../../package.json";
import LoadConfiguration from "../Foundation/Bootstrap/LoadConfiguration";
import type { Bootstrapper } from "../Contracts/Foundation/Bootstrapper";
import type Application from "../Foundation/Application";
Expand Down Expand Up @@ -105,7 +105,7 @@ class Kernel {
// register all commands to artisan
await Promise.all(
files.map(async (f) => {
const _command = (await import(pathToFileURL(f).href, {with: {type: "macro"}}))
const _command = (await import(pathToFileURL(f).href))
.default as Class<Command>;
const commandInstance = new _command();
this.registerCommand(commandInstance);
Expand Down
23 changes: 22 additions & 1 deletion packages/lunox-core/src/Container/Container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CallBack, Concrete } from "../Contracts";
import { Als } from "../Support/Facades";

interface Binding {
concrete: Concrete;
Expand All @@ -12,6 +13,8 @@ class Container {
/** The container's bindings. */
protected bindings: Record<string | symbol, Binding> = {};

protected scopedInstances: (string | symbol)[] = [];

/** Register a binding with the container. */
public bind(abstract: string | symbol, concrete: Concrete, shared = false) {
this.bindings[abstract] = { concrete, shared };
Expand All @@ -22,6 +25,14 @@ class Container {
this.bind(abstract, concrete, true);
}

/** Register scoped binding in container.
* it's just like singleton but scoped to current request
*/
public scoped(abstract: string | symbol, concrete: Concrete) {
this.scopedInstances.push(abstract);
this.singleton(abstract, concrete);
}

/** Instantiate a concrete instance of the given type. */
build<T>(abstract: string | symbol, params: Record<string, any> = {}): T {
const concrete = this.bindings[abstract].concrete;
Expand All @@ -38,9 +49,13 @@ class Container {
} else {
instance = (concrete as CallBack)();
}
if (this.bindings[abstract].shared) {
const isScoped = this.scopedInstances.includes(abstract);
if (this.bindings[abstract].shared && !isScoped) {
this.instances[abstract] = instance;
}
if (isScoped) {
Als.getStore()?.set(abstract, instance);
}
return instance as T;
}

Expand All @@ -50,6 +65,12 @@ class Container {
if (this.instances[abstract] && Object.keys(params).length == 0) {
return this.instances[abstract] as T;
}
if (this.scopedInstances.includes(abstract)) {
const scopedInstance = Als.getStore()?.get(abstract);
if (scopedInstance) {
return scopedInstance;
}
}
return this.build<T>(abstract, params);
} catch (error) {
let message = "";
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-core/src/Encryption/Encrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Encrypter {
const serializedValue = needSerialize ? JSON.stringify(value) : value;
try {
const ivBuffer = crypto.randomBytes(
Encrypter.supportedCiphers[this.cipher].ivLength
Encrypter.supportedCiphers[this.cipher].ivLength,
);
const iv = Encrypter.base64Encode(ivBuffer);

Expand Down
1 change: 1 addition & 0 deletions packages/lunox-core/src/Foundation/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Application extends Container {
protected _basePath!: string;

protected isBooted = false;
protected configFiles: Record<string, any> = {};

public get config(): Repository {
return this.make(Repository.symbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ class LoadConfiguration implements Bootstrapper {
files.map(async (f) => {
repository.set(
f.replace(app.getExt(), "").replace(path.sep, "."),
(
await import(pathToFileURL(path.join(configPath, f)).href, {
with: { type: "macro" },
})
).default || {},
(await import(pathToFileURL(path.join(configPath, f)).href))
.default || {},
);
}),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-core/src/Foundation/Http/Kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ const parseFormData = async (req: ServerRequest, request: Request) => {
(prev, key) => {
const file = files[key];
if (!file) return prev;
if(!key.endsWith("[]") && file.length == 1){
if (!key.endsWith("[]") && file.length == 1) {
prev[key] = new UploadedFile(file[0]);
return prev;
}
Expand Down
30 changes: 28 additions & 2 deletions packages/lunox-core/test/Feature/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ describe("Request Test", () => {
agent.get("/api/request-1"),
agent.get("/api/request-2"),
]);
expect(responses[0].body).toMatchObject({});
expect(responses[1].body).toMatchObject({ foo: "bar" });
expect(responses[0].body).toMatchObject({count:{
bind: {
init:0,
last:0
},
singleton:{
init:-10,
last:-15
},
scoped:{
init:0,
last:-10
}
}});
expect(responses[1].body).toMatchObject({ foo: "bar", count:{
bind: {
init:0,
last:0
},
singleton:{
init:-10,
last:-5
},
scoped:{
init:0,
last: 5
}
}});
});
});
13 changes: 13 additions & 0 deletions packages/lunox-core/test/app/Counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Counter {
protected count = 0;
increment(step: number = 1) {
this.count += step;
}
decrement(step: number = 1) {
this.count -= step;
}
getCount() {
return this.count;
}
}
export default Counter;
11 changes: 11 additions & 0 deletions packages/lunox-core/test/app/Providers/ScopeServecProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ServiceProvider } from "../../../src";
import Counter from "../Counter";

class ScopeServiceProvider extends ServiceProvider {
async register(): Promise<void> {
this.app.scoped("scopedCounter", Counter)
this.app.bind("counter", Counter)
this.app.singleton("singletonCounter", Counter)
}
}
export default ScopeServiceProvider;
2 changes: 2 additions & 0 deletions packages/lunox-core/test/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EncryptionServiceProvider } from "../../src";
import type { AppConfig } from "../../src/Contracts/Config";
import RouteServiceProvider from "../app/Providers/RouteServiceProvider";
import ScopeServiceProvider from "../app/Providers/ScopeServecProvider";

const app: AppConfig = {
name: "Lunox App",
Expand All @@ -12,6 +13,7 @@ const app: AppConfig = {
EncryptionServiceProvider,
// app service providers
RouteServiceProvider,
ScopeServiceProvider
],
};
export default app;
51 changes: 49 additions & 2 deletions packages/lunox-core/test/routes/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request } from "../../src";
import { Response, Route } from "../../src/Support/Facades";
import fs from "fs";
import Counter from "../app/Counter";

Route.get("/", () => {
return Response.make({
Expand All @@ -10,13 +11,59 @@ Route.get("/", () => {
});

Route.get("/request-1", async () => {
const count = {
bind: {
init:0,
last:0
},
singleton: {
init:0,
last:0
},
scoped: {
init:0,
last:0
}
}
count.bind.init = app<Counter>("counter").getCount();
count.singleton.init = app<Counter>("singletonCounter").getCount();
count.scoped.init = app<Counter>("scopedCounter").getCount();
await wait(500);
return Response.make(request().all());
app<Counter>("counter").decrement(10);
app<Counter>("singletonCounter").decrement(10);
app<Counter>("scopedCounter").decrement(10);
count.bind.last = app<Counter>("counter").getCount();
count.singleton.last = app<Counter>("singletonCounter").getCount();
count.scoped.last = app<Counter>("scopedCounter").getCount();
return Response.make({ ...request().all(), count });
});

Route.get("/request-2", async () => {
const count = {
bind: {
init:0,
last:0
},
singleton: {
init:0,
last:0
},
scoped: {
init:0,
last:0
}
}
count.bind.init = app<Counter>("counter").getCount();
count.singleton.init = app<Counter>("singletonCounter").getCount();
count.scoped.init = app<Counter>("scopedCounter").getCount();
app<Counter>("counter").increment(5);
app<Counter>("singletonCounter").increment(5);
app<Counter>("scopedCounter").increment(5);
count.bind.last = app<Counter>("counter").getCount();
count.singleton.last = app<Counter>("singletonCounter").getCount();
count.scoped.last = app<Counter>("scopedCounter").getCount();
request().merge({ foo: "bar" });
return Response.make(request().all());
return Response.make({ ...request().all(), count });
});
Route.post("/", () => "halo");
Route.post("/upload", handleUpload);
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-create/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-lunox-app",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Create Lunox app",
"main": "index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/drizzle",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Database Manager using Drizzle",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-eloquent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/eloquent",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Database ORM using Objection and Knex",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-event-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/event-drizzle",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Drizzle Connection for @lunoxjs/event",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-event-typeorm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/event-typeorm",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Typeorm Connection for @lunoxjs/event",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-event/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/event",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Event Manager",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-filesystem/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/filesystem",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Filesystem",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-mail/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/mail",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Mail Manager",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-prisma/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/prisma",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Database Manager using Prisma ORM",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-session/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/session",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Session",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/test",
"version": "2.0.0-beta.3.6.5",
"version": "2.0.0-beta.3.6.5.1",
"description": "Lunox Unit Test",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading