diff --git a/package.json b/package.json index b042637..4ba3567 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@root/std", + "name": "@root/http", "private": true, "type": "module", "version": "1.0.0", @@ -9,28 +9,29 @@ ], "license": "MIT", "scripts": { - "build": "tsc -b", + "build": "yarn yakumo build", "bump": "yarn yakumo version", "dep": "yarn yakumo upgrade", "pub": "yarn yakumo publish", "lint": "eslint --cache", - "test": "yarn yakumo test -r esbuild-register", + "test": "yarn yakumo test --import tsx", "test:text": "shx rm -rf coverage && c8 -r text yarn test", "test:json": "shx rm -rf coverage && c8 -r json yarn test", "test:html": "shx rm -rf coverage && c8 -r html yarn test" }, "devDependencies": { "@cordisjs/eslint-config": "^1.0.4", - "@sinonjs/fake-timers": "^6.0.1", "@types/chai": "^4.3.11", + "@types/chai-as-promised": "^7.1.8", "@types/node": "^20.10.2", - "@types/sinonjs__fake-timers": "^6.0.4", "c8": "^7.14.0", "chai": "^4.3.10", + "chai-as-promised": "^7.1.1", "esbuild": "^0.18.20", "esbuild-register": "^3.5.0", "eslint": "^8.55.0", "shx": "^0.3.4", + "tsx": "^4.7.0", "typescript": "^5.3.2", "yakumo": "^1.0.0-alpha.10", "yakumo-esbuild": "^1.0.0-alpha.2", diff --git a/packages/logger/package.json b/packages/logger/package.json deleted file mode 100644 index f198a67..0000000 --- a/packages/logger/package.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "name": "@cordisjs/logger", - "description": "Logger plugin for cordis", - "version": "0.1.3", - "main": "lib/index.js", - "module": "lib/index.mjs", - "types": "lib/index.d.ts", - "files": [ - "lib", - "src" - ], - "author": "Shigma ", - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/cordisjs/std.git", - "directory": "packages/logger" - }, - "bugs": { - "url": "https://github.com/cordisjs/std/issues" - }, - "homepage": "https://github.com/cordisjs/std", - "keywords": [ - "cordis", - "logger", - "service", - "plugin" - ], - "devDependencies": { - "cordis": "^3.5.1" - }, - "peerDependencies": { - "cordis": "^3.5.1" - }, - "dependencies": { - "reggol": "^1.6.3" - } -} diff --git a/packages/logger/readme.md b/packages/logger/readme.md deleted file mode 100644 index 5ac3ac3..0000000 --- a/packages/logger/readme.md +++ /dev/null @@ -1 +0,0 @@ -# @cordisjs/logger diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts deleted file mode 100644 index 0adf576..0000000 --- a/packages/logger/src/index.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Context } from 'cordis' -import Logger from 'reggol' - -export { Logger } - -declare module 'cordis' { - interface Context { - baseDir: string - logger: LoggerService - } -} - -interface LoggerService { - (name: string): Logger -} - -export function apply(ctx: Context) { - ctx.root.baseDir = globalThis.process?.cwd() || '' - - ctx.provide('logger', undefined, true) - - ctx.logger = function (name: string) { - return new Logger(name, { [Context.current]: this }) - } - - ctx.on('internal/error', function (format, ...args) { - this.logger('app').error(format, ...args) - }) - - ctx.on('internal/warning', function (format, ...args) { - this.logger('app').warn(format, ...args) - }) -} diff --git a/packages/logger/tsconfig.json b/packages/logger/tsconfig.json deleted file mode 100644 index 42cbe59..0000000 --- a/packages/logger/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../../tsconfig.base", - "compilerOptions": { - "rootDir": "src", - "outDir": "lib", - }, - "include": [ - "src", - ], -} diff --git a/packages/timer/package.json b/packages/timer/package.json deleted file mode 100644 index 149b107..0000000 --- a/packages/timer/package.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "name": "@cordisjs/timer", - "description": "Timer plugin for cordis", - "version": "0.2.0", - "main": "lib/index.js", - "module": "lib/index.mjs", - "types": "lib/index.d.ts", - "files": [ - "lib", - "src" - ], - "author": "Shigma ", - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/cordisjs/std.git", - "directory": "packages/timer" - }, - "bugs": { - "url": "https://github.com/cordisjs/std/issues" - }, - "homepage": "https://github.com/cordisjs/std", - "keywords": [ - "cordis", - "timer", - "service", - "plugin" - ], - "devDependencies": { - "cordis": "^3.5.1" - }, - "peerDependencies": { - "cordis": "^3.5.1" - } -} diff --git a/packages/timer/readme.md b/packages/timer/readme.md deleted file mode 100644 index 5ac3ac3..0000000 --- a/packages/timer/readme.md +++ /dev/null @@ -1 +0,0 @@ -# @cordisjs/logger diff --git a/packages/timer/src/index.ts b/packages/timer/src/index.ts deleted file mode 100644 index a0726c2..0000000 --- a/packages/timer/src/index.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { Context, Service } from 'cordis' -import { remove } from 'cosmokit' - -declare module 'cordis' { - interface Context { - timer: TimerService - setTimeout(callback: () => void, delay: number): () => void - setInterval(callback: () => void, delay: number): () => void - sleep(delay: number): Promise - throttle void>(callback: F, delay: number, noTrailing?: boolean): WithDispose - debounce void>(callback: F, delay: number): WithDispose - } -} - -type WithDispose = T & { dispose: () => void } - -class TimerService extends Service { - constructor(ctx: Context) { - super(ctx, 'timer', true) - ctx.mixin('timer', ['setTimeout', 'setInterval', 'sleep', 'throttle', 'debounce']) - } - - setTimeout(callback: () => void, delay: number) { - const dispose = this[Context.current].effect(() => { - const timer = setTimeout(() => { - dispose() - callback() - }, delay) - return () => clearTimeout(timer) - }) - return dispose - } - - setInterval(callback: () => void, delay: number) { - return this[Context.current].effect(() => { - const timer = setInterval(callback, delay) - return () => clearInterval(timer) - }) - } - - sleep(delay: number) { - const caller = this[Context.current] - return new Promise((resolve, reject) => { - const dispose1 = this.setTimeout(() => { - dispose1() - dispose2() - resolve() - }, delay) - const dispose2 = caller.on('dispose', () => { - dispose1() - dispose2() - reject(new Error('Context has been disposed')) - }) - }) - } - - private createWrapper(callback: (args: any[], check: () => boolean) => any, isDisposed = false) { - const caller = this[Context.current] - caller.scope.assertActive() - - let timer: number | NodeJS.Timeout | undefined - const dispose = () => { - isDisposed = true - remove(caller.scope.disposables, dispose) - clearTimeout(timer) - } - - const wrapper: any = (...args: any[]) => { - clearTimeout(timer) - timer = callback(args, () => !isDisposed && caller.scope.isActive) - } - wrapper.dispose = dispose - caller.scope.disposables.push(dispose) - return wrapper - } - - throttle void>(callback: F, delay: number, noTrailing?: boolean): WithDispose { - let lastCall = -Infinity - const execute = (...args: any[]) => { - lastCall = Date.now() - callback(...args) - } - return this.createWrapper((args, isActive) => { - const now = Date.now() - const remaining = delay - (now - lastCall) - if (remaining <= 0) { - execute(...args) - } else if (isActive()) { - return setTimeout(execute, remaining, ...args) - } - }, noTrailing) - } - - debounce void>(callback: F, delay: number): WithDispose { - return this.createWrapper((args, isActive) => { - if (!isActive()) return - return setTimeout(callback, delay, ...args) - }) - } -} - -export default TimerService diff --git a/packages/timer/tests/index.spec.ts b/packages/timer/tests/index.spec.ts deleted file mode 100644 index 45707a9..0000000 --- a/packages/timer/tests/index.spec.ts +++ /dev/null @@ -1,146 +0,0 @@ -import { describe, mock, test } from 'node:test' -import { FakeTimerInstallOpts, install, InstalledClock } from '@sinonjs/fake-timers' -import { Context } from 'cordis' -import assert from 'node:assert' -import Timer from '../src' - -declare module 'cordis' { - interface Context { - clock: InstalledClock - } -} - -function withContext(callback: (ctx: Context) => Promise, config?: FakeTimerInstallOpts) { - return () => new Promise((resolve, reject) => { - const ctx = new Context() - ctx.clock = install(config) - ctx.plugin(Timer) - ctx.plugin(() => { - callback(ctx).then(resolve, reject).finally(() => ctx.clock.uninstall()) - }) - }) -} - -describe('ctx.setTimeout()', () => { - test('basic support', withContext(async (ctx) => { - const callback = mock.fn() - ctx.setTimeout(callback, 1000) - assert.strictEqual(callback.mock.calls.length, 0) - await ctx.clock.tickAsync(1000) - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(1000) - assert.strictEqual(callback.mock.calls.length, 1) - })) - - test('dispose', withContext(async (ctx) => { - const callback = mock.fn() - const dispose = ctx.setTimeout(callback, 1000) - assert.strictEqual(callback.mock.calls.length, 0) - dispose() - await ctx.clock.tickAsync(2000) - assert.strictEqual(callback.mock.calls.length, 0) - })) -}) - -describe('ctx.setInterval()', () => { - test('basic support', withContext(async (ctx) => { - const callback = mock.fn() - const dispose = ctx.setInterval(callback, 1000) - assert.strictEqual(callback.mock.calls.length, 0) - await ctx.clock.tickAsync(1000) - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(1000) - assert.strictEqual(callback.mock.calls.length, 2) - dispose() - await ctx.clock.tickAsync(2000) - assert.strictEqual(callback.mock.calls.length, 2) - })) -}) - -describe('ctx.sleep()', () => { - test('basic support', withContext(async (ctx) => { - const resolve = mock.fn() - const reject = mock.fn() - ctx.sleep(1000).then(resolve, reject) - await ctx.clock.tickAsync(500) - assert.strictEqual(resolve.mock.calls.length, 0) - assert.strictEqual(reject.mock.calls.length, 0) - await ctx.clock.tickAsync(500) - assert.strictEqual(resolve.mock.calls.length, 1) - assert.strictEqual(reject.mock.calls.length, 0) - ctx.scope.dispose() - await ctx.clock.tickAsync(2000) - assert.strictEqual(resolve.mock.calls.length, 1) - assert.strictEqual(reject.mock.calls.length, 0) - })) -}) - -describe('ctx.throttle()', () => { - test('basic support', withContext(async (ctx) => { - const callback = mock.fn() - const throttled = ctx.throttle(callback, 1000) - throttled() - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(600) - throttled() - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(600) - throttled() - assert.strictEqual(callback.mock.calls.length, 2) - await ctx.clock.tickAsync(2000) - assert.strictEqual(callback.mock.calls.length, 3) - })) - - test('trailing mode', withContext(async (ctx) => { - const callback = mock.fn() - const throttled = ctx.throttle(callback, 1000) - throttled() - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(500) - throttled() - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(500) - assert.strictEqual(callback.mock.calls.length, 2) - await ctx.clock.tickAsync(2000) - assert.strictEqual(callback.mock.calls.length, 2) - })) - - test('disposed', withContext(async (ctx) => { - const callback = mock.fn() - const throttled = ctx.throttle(callback, 1000) - throttled.dispose() - throttled() - assert.strictEqual(callback.mock.calls.length, 1) - await ctx.clock.tickAsync(500) - throttled() - await ctx.clock.tickAsync(2000) - assert.strictEqual(callback.mock.calls.length, 1) - })) -}) - -describe('ctx.debounce()', () => { - test('basic support', withContext(async (ctx) => { - const callback = mock.fn() - const debounced = ctx.debounce(callback, 1000) - debounced() - assert.strictEqual(callback.mock.calls.length, 0) - await ctx.clock.tickAsync(400) - debounced() - assert.strictEqual(callback.mock.calls.length, 0) - await ctx.clock.tickAsync(400) - debounced() - assert.strictEqual(callback.mock.calls.length, 0) - await ctx.clock.tickAsync(1000) - assert.strictEqual(callback.mock.calls.length, 1) - })) - - test('disposed', withContext(async (ctx) => { - const callback = mock.fn() - const debounced = ctx.debounce(callback, 1000) - debounced.dispose() - debounced() - assert.strictEqual(callback.mock.calls.length, 0) - await ctx.clock.tickAsync(2000) - assert.strictEqual(callback.mock.calls.length, 0) - })) -}) diff --git a/packages/timer/tsconfig.json b/packages/timer/tsconfig.json deleted file mode 100644 index 42cbe59..0000000 --- a/packages/timer/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../../tsconfig.base", - "compilerOptions": { - "rootDir": "src", - "outDir": "lib", - }, - "include": [ - "src", - ], -}