From ba149856ab5f34121b605e02df5c4fe1b5cf7b8d Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Wed, 23 Oct 2024 11:48:58 +0200 Subject: [PATCH 1/4] feat: export RPC types --- package.json | 14 +++++++------- src/common/msg/rpc.ts | 10 +++++----- tsconfig.json | 4 +++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index ac8b01c..3314d7d 100644 --- a/package.json +++ b/package.json @@ -69,19 +69,19 @@ "watch": "nr build -- --watch src" }, "devDependencies": { - "@antfu/eslint-config": "^3.7", + "@antfu/eslint-config": "^3.8", "@antfu/ni": "^0.23.0", - "@types/node": "^22.7.5", + "@types/node": "^22.7.8", "@vitejs/plugin-vue": "^5.1.4", - "@vitest/browser": "^2.1.2", - "@vitest/coverage-v8": "^2.1.2", + "@vitest/browser": "^2.1.3", + "@vitest/coverage-v8": "^2.1.3", "esbuild": "^0.24.0", "eslint": "^9", - "playwright": "^1.48.0", + "playwright": "^1.48.1", "tsup": "^8.3.0", "typescript": "^5.6.3", - "vite": "^5.4.8", - "vitest": "^2.1.2" + "vite": "^5.4.9", + "vitest": "^2.1.3" }, "pnpm": { "overrides": { diff --git a/src/common/msg/rpc.ts b/src/common/msg/rpc.ts index a21411b..4ef8d04 100644 --- a/src/common/msg/rpc.ts +++ b/src/common/msg/rpc.ts @@ -45,17 +45,17 @@ export type RPCReturn = { let rpcCounter = 1 -enum RPCMode { +export enum RPCMode { request = 1, event = 2, resolve = 3, reject = 4, } -type RPCMessage = [ - RPCMode, - number, - string | number | any, +export type RPCMessage = [ + mode: RPCMode, + id: number, + method: string | number | any, ...any, ] diff --git a/tsconfig.json b/tsconfig.json index 29ef9b7..8226f94 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,9 @@ "resolveJsonModule": true, "types": [ "node", - "vitest/globals" + "vitest/globals", + "@vitest/browser/providers/webdriverio", + "@vitest/browser/providers/playwright" ], "allowJs": true, "strict": true, From 0efc668f92841994e65b0bbc40704c18d2d0d61c Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sat, 26 Oct 2024 12:40:32 +0200 Subject: [PATCH 2/4] fix: separate renderMessages --- package.json | 8 +++---- src/common/data/convert.spec.ts | 26 ++------------------ src/common/data/convert.ts | 42 --------------------------------- src/common/data/index.ts | 1 + src/common/data/message.spec.ts | 28 ++++++++++++++++++++++ src/common/data/message.ts | 42 +++++++++++++++++++++++++++++++++ src/common/eslint-defaults.js | 2 +- src/node/log/log-file.ts | 2 +- src/node/log/log-node.ts | 3 ++- 9 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 src/common/data/message.spec.ts create mode 100644 src/common/data/message.ts diff --git a/package.json b/package.json index 3314d7d..702f1eb 100644 --- a/package.json +++ b/package.json @@ -71,16 +71,16 @@ "devDependencies": { "@antfu/eslint-config": "^3.8", "@antfu/ni": "^0.23.0", - "@types/node": "^22.7.8", + "@types/node": "^22.8.1", "@vitejs/plugin-vue": "^5.1.4", "@vitest/browser": "^2.1.3", "@vitest/coverage-v8": "^2.1.3", "esbuild": "^0.24.0", "eslint": "^9", - "playwright": "^1.48.1", - "tsup": "^8.3.0", + "playwright": "^1.48.2", + "tsup": "^8.3.5", "typescript": "^5.6.3", - "vite": "^5.4.9", + "vite": "^5.4.10", "vitest": "^2.1.3" }, "pnpm": { diff --git a/src/common/data/convert.spec.ts b/src/common/data/convert.spec.ts index 196b4ce..462ef2d 100644 --- a/src/common/data/convert.spec.ts +++ b/src/common/data/convert.spec.ts @@ -1,4 +1,4 @@ -import { renderMessages, valueToBoolean, valueToBooleanNotFalse, valueToInteger, valueToString } from './convert' +import { valueToBoolean, valueToBooleanNotFalse, valueToInteger, valueToString } from './convert' describe('convert', () => { it('should convert', () => { @@ -7,29 +7,7 @@ describe('convert', () => { expect(valueToInteger('123')).toBe(123) expect(valueToInteger(true)).toBe(1) }) - - it('should print errors', () => { - expect(renderMessages(['Message', new Error('Hello')])).toContain( - 'Message Error: Hello', - ) - expect( - renderMessages(['Message', new Error('Hello')], { trace: false }), - ).toBe('Message Error: Hello') - expect( - renderMessages([ - 'Message', - { a: 1, b: null }, - null, - undefined, - Number.NaN, - 1e2, - 'string', - ]), - ).toEqual(`Message { - \"a\": 1, - \"b\": null -} null undefined NaN 100 string`) - }) + it('should have some bias', async () => { expect(valueToBoolean('123')).toBe(false) diff --git a/src/common/data/convert.ts b/src/common/data/convert.ts index 12a2261..c1215ce 100644 --- a/src/common/data/convert.ts +++ b/src/common/data/convert.ts @@ -90,48 +90,6 @@ export const toInt = valueToInteger export const toString = valueToString export const toBool = valueToBoolean -// Strings - -export interface RenderMessagesOptions { - trace?: boolean // = true - pretty?: boolean // = true -} - -export function formatMessages( - messages: any[], - opt: RenderMessagesOptions = {}, -): any[] { - const { trace = true, pretty = true } = opt - return messages.map((obj) => { - if (obj && typeof obj === 'object') { - if (pretty && (obj instanceof Uint8Array || obj instanceof ArrayBuffer)) - return `\n${Uint8ArrayToHexDump(obj)}\n` - if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) { - obj = obj.error - } - if (obj instanceof Error) { - if (!trace) - return `${obj.name || 'Error'}: ${obj.message}` - return `${obj.name || 'Error'}: ${obj.message}\n${obj.stack}` - } - try { - obj = objectPlain(obj) - return pretty ? JSON.stringify(obj, null, 2) : JSON.stringify(obj) - } - catch (err) {} - } - return String(obj) - }) -} - -export function renderMessages( - messages: any[], - opt: RenderMessagesOptions = {}, -): string { - return formatMessages(messages, opt).join(' ') -} - -// // Awesome trick from https://stackoverflow.com/a/5396742/140927 export function fixBrokenUtf8String(brokenString: string): string { diff --git a/src/common/data/index.ts b/src/common/data/index.ts index d79d9f4..f7f90af 100644 --- a/src/common/data/index.ts +++ b/src/common/data/index.ts @@ -16,6 +16,7 @@ export * from './is' export * from './json' export * from './list' export * from './math' +export * from './message' export * from './object-changes' export * from './object' export * from './orderby' diff --git a/src/common/data/message.spec.ts b/src/common/data/message.spec.ts new file mode 100644 index 0000000..6c0bd6f --- /dev/null +++ b/src/common/data/message.spec.ts @@ -0,0 +1,28 @@ +import { renderMessages } from './message' + +describe('message', () => { + + it('should print errors', () => { + expect(renderMessages(['Message', new Error('Hello')])).toContain( + 'Message Error: Hello', + ) + expect( + renderMessages(['Message', new Error('Hello')], { trace: false }), + ).toBe('Message Error: Hello') + expect( + renderMessages([ + 'Message', + { a: 1, b: null }, + null, + undefined, + Number.NaN, + 1e2, + 'string', + ]), + ).toEqual(`Message { + \"a\": 1, + \"b\": null +} null undefined NaN 100 string`) + }) + +}) diff --git a/src/common/data/message.ts b/src/common/data/message.ts new file mode 100644 index 0000000..fba2ee6 --- /dev/null +++ b/src/common/data/message.ts @@ -0,0 +1,42 @@ +import { Uint8ArrayToHexDump } from "./bin" +import { objectPlain } from "./object" + +export interface RenderMessagesOptions { + trace?: boolean // = true + pretty?: boolean // = true +} + +export function formatMessages( + messages: any[], + opt: RenderMessagesOptions = {}, +): any[] { + const { trace = true, pretty = true } = opt + return messages.map((obj) => { + if (obj && typeof obj === 'object') { + if (pretty && (obj instanceof Uint8Array || obj instanceof ArrayBuffer)) + return `\n${Uint8ArrayToHexDump(obj)}\n` + if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) { + obj = obj.error + } + if (obj instanceof Error) { + if (!trace) + return `${obj.name || 'Error'}: ${obj.message}` + return `${obj.name || 'Error'}: ${obj.message}\n${obj.stack}` + } + try { + obj = objectPlain(obj) + return pretty ? JSON.stringify(obj, null, 2) : JSON.stringify(obj) + } + catch (err) {} + } + return String(obj) + }) +} + +export function renderMessages( + messages: any[], + opt: RenderMessagesOptions = {}, +): string { + return formatMessages(messages, opt).join(' ') +} + diff --git a/src/common/eslint-defaults.js b/src/common/eslint-defaults.js index d18feac..ff498cc 100644 --- a/src/common/eslint-defaults.js +++ b/src/common/eslint-defaults.js @@ -61,7 +61,7 @@ export function eslintDefaults(opt) { return { vue: true, typescript: true, - // regexp: false, + regexp: true, ...rest, diff --git a/src/node/log/log-file.ts b/src/node/log/log-file.ts index 90b94da..c8c3c6d 100644 --- a/src/node/log/log-file.ts +++ b/src/node/log/log-file.ts @@ -2,7 +2,7 @@ import type { LogHandlerOptions, LogMessage } from '../../common/log/log-base' import { createWriteStream, mkdirSync } from 'node:fs' import { dirname, resolve } from 'node:path' import process from 'node:process' -import { renderMessages } from '../../common/data/convert' +import { renderMessages } from '../../common/data/message' import { LogLevelError, LogLevelInfo, LogLevelWarn } from '../../common/log/log-base' import { useLevelFilter, useNamespaceFilter } from '../../common/log/log-filter' diff --git a/src/node/log/log-node.ts b/src/node/log/log-node.ts index 13d5f2d..a84dfea 100644 --- a/src/node/log/log-node.ts +++ b/src/node/log/log-node.ts @@ -1,7 +1,8 @@ import type { LogHandler, LogHandlerOptions, LogMessage } from '../../common/log/log-base' import process from 'node:process' import tty from 'node:tty' -import { renderMessages, valueToBoolean } from '../../common/data/convert' +import { valueToBoolean } from '../../common/data/convert' +import { renderMessages } from '../../common/data/message' import { LogLevelError, LogLevelInfo, LogLevelWarn } from '../../common/log/log-base' import { useLevelFilter, useNamespaceFilter } from '../../common/log/log-filter' import { formatMilliseconds, getTimestamp } from '../../common/time' From 88787722c783c17c765c98ecd9c0890a3df0727e Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sat, 26 Oct 2024 15:11:54 +0200 Subject: [PATCH 3/4] fix: renderMessges error handling --- src/common/data/message.spec.ts | 36 +++++++++++++++++++++------------ src/common/data/message.ts | 22 ++++++++++++-------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/common/data/message.spec.ts b/src/common/data/message.spec.ts index 6c0bd6f..52c3768 100644 --- a/src/common/data/message.spec.ts +++ b/src/common/data/message.spec.ts @@ -1,14 +1,12 @@ import { renderMessages } from './message' describe('message', () => { - + it('should print errors', () => { - expect(renderMessages(['Message', new Error('Hello')])).toContain( - 'Message Error: Hello', - ) - expect( - renderMessages(['Message', new Error('Hello')], { trace: false }), - ).toBe('Message Error: Hello') + expect(renderMessages(['Message', new Error('Hello')], { trace: false })).toMatchInlineSnapshot(`"Message Error: Hello"`) + + expect(renderMessages(['Message', new Error('Hello')], { trace: false }),).toMatchInlineSnapshot(`"Message Error: Hello"`) + expect( renderMessages([ 'Message', @@ -18,11 +16,23 @@ describe('message', () => { Number.NaN, 1e2, 'string', - ]), - ).toEqual(`Message { - \"a\": 1, - \"b\": null -} null undefined NaN 100 string`) + ], { trace: false }), + ).toMatchInlineSnapshot(` + "Message { + "a": 1, + "b": null + } null undefined NaN 100 string" + `) + + // it('should handle promise rejection', async () => { + // let promise = new Promise((_, reject) => reject(new Error('Hello'))) + // let myRejectionEvent = new PromiseRejectionEvent("unhandledrejection", { + // promise, + // reason: "My house is on fire", + // }) + + // expect(renderMessages(myRejectionEvent)).toMatchInlineSnapshot() + }) - + }) diff --git a/src/common/data/message.ts b/src/common/data/message.ts index fba2ee6..e6b1217 100644 --- a/src/common/data/message.ts +++ b/src/common/data/message.ts @@ -1,4 +1,5 @@ import { Uint8ArrayToHexDump } from "./bin" +import { isArray } from "./is" import { objectPlain } from "./object" export interface RenderMessagesOptions { @@ -15,14 +16,20 @@ export function formatMessages( if (obj && typeof obj === 'object') { if (pretty && (obj instanceof Uint8Array || obj instanceof ArrayBuffer)) return `\n${Uint8ArrayToHexDump(obj)}\n` + + if (obj instanceof Error) { + return `${obj.name}: ${obj.message}` + (trace ? `\n${obj.stack}` : '') + } if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) { - obj = obj.error + return `${obj.error.name || 'ErrorEvent'}: ${obj.error.message}` + (trace ? `\n${obj.error.stack}` : '') } - if (obj instanceof Error) { - if (!trace) - return `${obj.name || 'Error'}: ${obj.message}` - return `${obj.name || 'Error'}: ${obj.message}\n${obj.stack}` + if (typeof DOMException !== 'undefined' && obj instanceof DOMException) { + return `${obj.name || 'DOMException'}: ${obj.message}` + (trace ? `\n${obj.stack}` : '') } + if (obj && typeof obj === 'object' && 'reason' in obj) { + return `PromiseRejection ${obj.type}: ${obj.reason}` + (trace ? `\n${obj.stack}` : '') + } + try { obj = objectPlain(obj) return pretty ? JSON.stringify(obj, null, 2) : JSON.stringify(obj) @@ -34,9 +41,8 @@ export function formatMessages( } export function renderMessages( - messages: any[], + messages: any | any[], opt: RenderMessagesOptions = {}, ): string { - return formatMessages(messages, opt).join(' ') + return formatMessages(isArray(messages) ? messages : [messages], opt).join(' ') } - From ddc9ac082d5baa9bf5985b8f169a4bd4a147f592 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sat, 26 Oct 2024 15:11:57 +0200 Subject: [PATCH 4/4] 0.25.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 702f1eb..33639b8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.25.3", + "version": "0.25.4", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick",