diff --git a/README.md b/README.md deleted file mode 100644 index 57a481c..0000000 --- a/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# structrail-design - -To install dependencies: - -```bash -bun install -``` - -To run: - -```bash -bun run index.ts -``` - -This project was created using `bun init` in bun v1.2.23. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. diff --git a/bun.lock b/bun.lock deleted file mode 100644 index f9444e4..0000000 --- a/bun.lock +++ /dev/null @@ -1,29 +0,0 @@ -{ - "lockfileVersion": 1, - "workspaces": { - "": { - "name": "structrail-design", - "devDependencies": { - "@types/bun": "latest", - }, - "peerDependencies": { - "typescript": "^5", - }, - }, - }, - "packages": { - "@types/bun": ["@types/bun@1.2.23", "", { "dependencies": { "bun-types": "1.2.23" } }, "sha512-le8ueOY5b6VKYf19xT3McVbXqLqmxzPXHsQT/q9JHgikJ2X22wyTW3g3ohz2ZMnp7dod6aduIiq8A14Xyimm0A=="], - - "@types/node": ["@types/node@24.7.1", "", { "dependencies": { "undici-types": "~7.14.0" } }, "sha512-CmyhGZanP88uuC5GpWU9q+fI61j2SkhO3UGMUdfYRE6Bcy0ccyzn1Rqj9YAB/ZY4kOXmNf0ocah5GtphmLMP6Q=="], - - "@types/react": ["@types/react@19.2.2", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-6mDvHUFSjyT2B2yeNx2nUgMxh9LtOWvkhIU3uePn2I2oyNymUAX1NIsdgviM4CH+JSrp2D2hsMvJOkxY+0wNRA=="], - - "bun-types": ["bun-types@1.2.23", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-R9f0hKAZXgFU3mlrA0YpE/fiDvwV0FT9rORApt2aQVWSuJDzZOyB5QLc0N/4HF57CS8IXJ6+L5E4W1bW6NS2Aw=="], - - "csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="], - - "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="], - - "undici-types": ["undici-types@7.14.0", "", {}, "sha512-QQiYxHuyZ9gQUIrmPo3IA+hUl4KYk8uSA7cHrcKd/l3p1OTpZcM0Tbp9x7FAtXdAYhlasd60ncPpgu6ihG6TOA=="], - } -} diff --git a/index.ts b/index.ts deleted file mode 100644 index fd293be..0000000 --- a/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ArrayTracer } from "./src/tracers.ts"; - -const arrayTracer = ArrayTracer.define({ - description: "Array Tracer", -}); - -arrayTracer.preset([1, 2, 3]); diff --git a/tracers.ts/.gitignore b/tracers.ts/.gitignore new file mode 100644 index 0000000..a14702c --- /dev/null +++ b/tracers.ts/.gitignore @@ -0,0 +1,34 @@ +# dependencies (bun install) +node_modules + +# output +out +dist +*.tgz + +# code coverage +coverage +*.lcov + +# logs +logs +_.log +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# caches +.eslintcache +.cache +*.tsbuildinfo + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/tracers.ts/README.md b/tracers.ts/README.md new file mode 100644 index 0000000..7b32915 --- /dev/null +++ b/tracers.ts/README.md @@ -0,0 +1,15 @@ +# tracers.ts + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run src/index.ts +``` + +This project was created using `bun init` in bun v1.3.4. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. diff --git a/tracers.ts/bun.lock b/tracers.ts/bun.lock new file mode 100644 index 0000000..0d9354a --- /dev/null +++ b/tracers.ts/bun.lock @@ -0,0 +1,26 @@ +{ + "lockfileVersion": 1, + "configVersion": 1, + "workspaces": { + "": { + "name": "tracers.ts", + "devDependencies": { + "@types/bun": "latest", + }, + "peerDependencies": { + "typescript": "^5", + }, + }, + }, + "packages": { + "@types/bun": ["@types/bun@1.3.8", "", { "dependencies": { "bun-types": "1.3.8" } }, "sha512-3LvWJ2q5GerAXYxO2mffLTqOzEu5qnhEAlh48Vnu8WQfnmSwbgagjGZV6BoHKJztENYEDn6QmVd949W4uESRJA=="], + + "@types/node": ["@types/node@25.2.0", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-DZ8VwRFUNzuqJ5khrvwMXHmvPe+zGayJhr2CDNiKB1WBE1ST8Djl00D0IC4vvNmHMdj6DlbYRIaFE7WHjlDl5w=="], + + "bun-types": ["bun-types@1.3.8", "", { "dependencies": { "@types/node": "*" } }, "sha512-fL99nxdOWvV4LqjmC+8Q9kW3M4QTtTR1eePs94v5ctGqU8OeceWrSUaRw3JYb7tU3FkMIAjkueehrHPPPGKi5Q=="], + + "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="], + + "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="], + } +} diff --git a/package.json b/tracers.ts/package.json similarity index 73% rename from package.json rename to tracers.ts/package.json index 7dc984c..fa0ec26 100644 --- a/package.json +++ b/tracers.ts/package.json @@ -1,6 +1,6 @@ { - "name": "structrail-design", - "module": "index.ts", + "name": "tracers.ts", + "module": "src/index.ts", "type": "module", "private": true, "devDependencies": { diff --git a/tracers.ts/src/context/index.ts b/tracers.ts/src/context/index.ts new file mode 100644 index 0000000..61b8bf3 --- /dev/null +++ b/tracers.ts/src/context/index.ts @@ -0,0 +1 @@ +export * from './tracer-context'; diff --git a/tracers.ts/src/context/tracer-context.ts b/tracers.ts/src/context/tracer-context.ts new file mode 100644 index 0000000..1a7c713 --- /dev/null +++ b/tracers.ts/src/context/tracer-context.ts @@ -0,0 +1,22 @@ +import type { TracerCommand } from '../types'; + +const createTracerContext = () => { + const commands: TracerCommand[] = []; + + const getTracerContext = () => { + const command = (command: TracerCommand) => { + commands.push(command); + }; + + return { + commands, + command, + }; + }; + + return { + getTracerContext, + }; +}; + +export const { getTracerContext } = createTracerContext(); diff --git a/tracers.ts/src/index.ts b/tracers.ts/src/index.ts new file mode 100644 index 0000000..a6de801 --- /dev/null +++ b/tracers.ts/src/index.ts @@ -0,0 +1,20 @@ +import { getTracerContext } from './context'; +import { + createArrayTracer, + createControlTracer, + createLogTracer, +} from './tracers'; + +const logTracer = createLogTracer({ description: 'LogTracer' }); +const controlTracer = createControlTracer({ description: 'ControlTracer' }); + +const arrayTracer = createArrayTracer({ + description: 'ArrayTracer', + array: [1, 2, 3], +}); + +arrayTracer.patch(0, 100); + +controlTracer.step(); + +console.log(getTracerContext().commands); diff --git a/tracers.ts/src/tracers/array-tracer.ts b/tracers.ts/src/tracers/array-tracer.ts new file mode 100644 index 0000000..b54c28d --- /dev/null +++ b/tracers.ts/src/tracers/array-tracer.ts @@ -0,0 +1,90 @@ +import { getTracerContext } from '../context'; +import type { JsonValue } from '../types'; + +interface ArrayTracerCreateOptions { + description?: string; + array?: T; +} + +export const createArrayTracer = ( + options: ArrayTracerCreateOptions, +) => { + const { description = 'ArrayTracer', array } = options; + const tracer = crypto.randomUUID(); + + const { command } = getTracerContext(); + + command({ + type: 'ArrayTracer', + tracer: tracer, + action: 'create', + params: { + description: description, + array: array, + }, + }); + + const scale = (size: number) => { + command({ + type: 'ArrayTracer', + tracer: tracer, + action: 'scale', + params: { + size: size, + }, + }); + }; + + const pick = (index: number) => { + command({ + type: 'ArrayTracer', + tracer: tracer, + action: 'pick', + params: { + index: index, + }, + }); + }; + + const drop = (index: number) => { + command({ + type: 'ArrayTracer', + tracer: tracer, + action: 'drop', + params: { + index: index, + }, + }); + }; + + const patch = (index: number, value: T[number]) => { + command({ + type: 'ArrayTracer', + tracer: tracer, + action: 'patch', + params: { + index: index, + value: value, + }, + }); + }; + + const unset = (index: number) => { + command({ + type: 'ArrayTracer', + tracer: tracer, + action: 'unset', + params: { + index: index, + }, + }); + }; + + return { + scale, + pick, + drop, + patch, + unset, + }; +}; diff --git a/tracers.ts/src/tracers/control-tracer.ts b/tracers.ts/src/tracers/control-tracer.ts new file mode 100644 index 0000000..6832a18 --- /dev/null +++ b/tracers.ts/src/tracers/control-tracer.ts @@ -0,0 +1,81 @@ +import { getTracerContext } from '../context'; + +export const getLocation = (targetFn?: Function) => { + const stackObject: { stack: string } = { stack: '' }; + Error.captureStackTrace(stackObject, targetFn ?? getLocation); + const { stack } = stackObject; + // D:\Projects\structrail-sdk\tracers.ts\src\index.ts:23:18 + const location = stack.split('\n').at(1)?.trim().split(' ').at(1)?.trim(); + // console.log(location); + if (!location) return null; + + const matches = location?.match(/:(\d+):?(\d+)?\)?$/); + // console.log(matches); + const file = location?.replace(matches?.at(0) ?? '', ''); + // console.log(file); + const line = parseInt(matches?.at(1) ?? '0'); + // console.log(line); + + return { + file, + line, + }; +}; + +interface ControlTracerCreateOptions { + description?: string; +} + +export const createControlTracer = (options: ControlTracerCreateOptions) => { + const { description = 'ControlTracer' } = options; + const tracer = crypto.randomUUID(); + + const { command } = getTracerContext(); + + command({ + type: 'ControlTracer', + tracer: tracer, + action: 'create', + params: { + description: description, + }, + }); + + const step = (...range: (number | [number, number])[]) => { + const { line: currentLine } = getLocation(step) ?? {}; + if (!currentLine) return; + // console.log(currentLine); + + const linesSet = new Set([currentLine]); + range.forEach((item) => { + if (Array.isArray(item) && item.length === 2) { + const [offsetStart, offsetEnd] = item; + const lineStart = currentLine - offsetStart; + const lineEnd = currentLine - offsetEnd; + const lineMin = Math.min(lineStart, lineEnd); + const lineMax = Math.max(lineStart, lineEnd); + for (let line = lineMin; line <= lineMax; line++) { + linesSet.add(line); + } + } + if (typeof item === 'number') { + const line = item; + linesSet.add(currentLine - line); + } + }); + const lines = Array.from(linesSet).sort((line1, line2) => line1 - line2); + + command({ + type: 'ControlTracer', + tracer: tracer, + action: 'step', + params: { + lines: lines, + }, + }); + }; + + return { + step, + }; +}; diff --git a/tracers.ts/src/tracers/index.ts b/tracers.ts/src/tracers/index.ts new file mode 100644 index 0000000..f8e480d --- /dev/null +++ b/tracers.ts/src/tracers/index.ts @@ -0,0 +1,3 @@ +export * from './array-tracer'; +export * from './control-tracer'; +export * from './log-tracer'; diff --git a/tracers.ts/src/tracers/log-tracer.ts b/tracers.ts/src/tracers/log-tracer.ts new file mode 100644 index 0000000..29073f6 --- /dev/null +++ b/tracers.ts/src/tracers/log-tracer.ts @@ -0,0 +1,44 @@ +import { getTracerContext } from '../context'; + +interface LogTracerCreateOptions { + description?: string; +} + +export const createLogTracer = (options: LogTracerCreateOptions) => { + const { description = 'LogTracer' } = options; + const tracer = crypto.randomUUID(); + + const { command } = getTracerContext(); + + command({ + type: 'LogTracer', + tracer: tracer, + action: 'create', + params: { + description: description, + }, + }); + + const log = (...args: unknown[]) => { + const parsed = args.map((arg) => { + if (typeof arg === 'string') { + return arg; + } + return JSON.stringify(arg); + }); + const message = parsed.join(' '); + + command({ + type: 'LogTracer', + tracer: tracer, + action: 'log', + params: { + message: message, + }, + }); + }; + + return { + log, + }; +}; diff --git a/tracers.ts/src/types/array-tracer.ts b/tracers.ts/src/types/array-tracer.ts new file mode 100644 index 0000000..944a693 --- /dev/null +++ b/tracers.ts/src/types/array-tracer.ts @@ -0,0 +1,57 @@ +import type { BaseTracerCommand, JsonValue } from './common'; + +type BaseArrayTracerCommand = BaseTracerCommand & { + type: 'ArrayTracer'; +}; + +type ArrayTracerCreateCommand = BaseArrayTracerCommand & { + action: 'create'; + params: { + description: string; + array?: JsonValue[]; + }; +}; + +type ArrayTracerScaleCommand = BaseArrayTracerCommand & { + action: 'scale'; + params: { + size: number; + }; +}; + +type ArrayTracerPickCommand = BaseArrayTracerCommand & { + action: 'pick'; + params: { + index: number; + }; +}; + +type ArrayTracerDropCommand = BaseArrayTracerCommand & { + action: 'drop'; + params: { + index: number; + }; +}; + +type ArrayTracerPatchCommand = BaseArrayTracerCommand & { + action: 'patch'; + params: { + index: number; + value: JsonValue; + }; +}; + +type ArrayTracerUnsetCommand = BaseArrayTracerCommand & { + action: 'unset'; + params: { + index: number; + }; +}; + +export type ArrayTracerCommand = + | ArrayTracerCreateCommand + | ArrayTracerScaleCommand + | ArrayTracerPickCommand + | ArrayTracerDropCommand + | ArrayTracerPatchCommand + | ArrayTracerUnsetCommand; diff --git a/tracers.ts/src/types/command.ts b/tracers.ts/src/types/command.ts new file mode 100644 index 0000000..e312e20 --- /dev/null +++ b/tracers.ts/src/types/command.ts @@ -0,0 +1,8 @@ +import type { ArrayTracerCommand } from './array-tracer'; +import type { ControlTracerCommand } from './control-tracer'; +import type { LogTracerCommand } from './log-tracer'; + +export type TracerCommand = + | ArrayTracerCommand + | LogTracerCommand + | ControlTracerCommand; diff --git a/tracers.ts/src/types/common.ts b/tracers.ts/src/types/common.ts new file mode 100644 index 0000000..9fc9bfc --- /dev/null +++ b/tracers.ts/src/types/common.ts @@ -0,0 +1,16 @@ +export type JsonValue = + | string + | number + | boolean + | null + | JsonValue[] + | { [key: string]: JsonValue }; + +export type TracerType = 'ArrayTracer' | 'LogTracer' | 'ControlTracer'; + +export type TracerId = ReturnType; + +export type BaseTracerCommand = { + type: TracerType; + tracer: TracerId; +}; diff --git a/tracers.ts/src/types/control-tracer.ts b/tracers.ts/src/types/control-tracer.ts new file mode 100644 index 0000000..1109a3b --- /dev/null +++ b/tracers.ts/src/types/control-tracer.ts @@ -0,0 +1,23 @@ +import type { BaseTracerCommand } from './common'; + +type BaseControlTracerCommand = BaseTracerCommand & { + type: 'ControlTracer'; +}; + +type ControlTracerCreateCommand = BaseControlTracerCommand & { + action: 'create'; + params: { + description: string; + }; +}; + +type ControlTracerStepCommand = BaseControlTracerCommand & { + action: 'step'; + params: { + lines: number[]; + }; +}; + +export type ControlTracerCommand = + | ControlTracerCreateCommand + | ControlTracerStepCommand; diff --git a/tracers.ts/src/types/index.ts b/tracers.ts/src/types/index.ts new file mode 100644 index 0000000..39b1734 --- /dev/null +++ b/tracers.ts/src/types/index.ts @@ -0,0 +1,5 @@ +export * from './array-tracer'; +export * from './command'; +export * from './common'; +export * from './control-tracer'; +export * from './log-tracer'; diff --git a/tracers.ts/src/types/log-tracer.ts b/tracers.ts/src/types/log-tracer.ts new file mode 100644 index 0000000..ac5cee7 --- /dev/null +++ b/tracers.ts/src/types/log-tracer.ts @@ -0,0 +1,21 @@ +import type { BaseTracerCommand } from "./common"; + +type BaseLogTracerCommand = BaseTracerCommand & { + type: 'LogTracer'; +}; + +type LogTracerCreateCommand = BaseLogTracerCommand & { + action: 'create'; + params: { + description: string; + }; +}; + +type LogTracerLogCommand = BaseLogTracerCommand & { + action: 'log'; + params: { + message: string; + }; +}; + +export type LogTracerCommand = LogTracerCreateCommand | LogTracerLogCommand; diff --git a/tsconfig.json b/tracers.ts/tsconfig.json similarity index 100% rename from tsconfig.json rename to tracers.ts/tsconfig.json