From 2c492a4e20a42d63370dee42ba6041cf7e2f6d5e Mon Sep 17 00:00:00 2001 From: ferzu13 Date: Fri, 28 Mar 2025 17:06:05 +0100 Subject: [PATCH 1/2] Config fields representing new mapping --- __test__/index.test.ts | 72 +++++++++++++++--------------------------- bin/k21.d.ts | 6 +--- k21/k21.ts | 41 +++++++++--------------- src/capture.rs | 36 +++++++++------------ 4 files changed, 57 insertions(+), 98 deletions(-) diff --git a/__test__/index.test.ts b/__test__/index.test.ts index 0276821..724c35c 100644 --- a/__test__/index.test.ts +++ b/__test__/index.test.ts @@ -36,9 +36,7 @@ describe('K21Pipeline', () => { test('should set basic config correctly', () => { const config = { fps: 1, - recordLengthInSeconds: 5, - saveVideo: false, - saveScreenshot: false + duration: 5, } pipeline.setCapturer(config) expect(pipeline['capturer']).toEqual({ @@ -53,11 +51,9 @@ describe('K21Pipeline', () => { const config = { fps: 1, - recordLengthInSeconds: 5, - saveVideo: true, - outputDirVideo: videoDir, - videoChunkDurationInSeconds: 10, - saveScreenshot: false + duration: 5, + saveVideoTo: videoDir, + videoChunkDuration: 10, } pipeline.setCapturer(config) expect(pipeline['capturer']).toEqual({ @@ -73,10 +69,8 @@ describe('K21Pipeline', () => { const config = { fps: 1, - recordLengthInSeconds: 5, - saveVideo: false, - saveScreenshot: true, - outputDirScreenshot: screenshotDir + duration: 5, + saveScreenshotTo: screenshotDir } pipeline.setCapturer(config) expect(pipeline['capturer']).toEqual({ @@ -94,12 +88,10 @@ describe('K21Pipeline', () => { const config = { fps: 1, - recordLengthInSeconds: 5, - saveVideo: true, - outputDirVideo: videoDir, - videoChunkDurationInSeconds: 10, - saveScreenshot: true, - outputDirScreenshot: screenshotDir + duration: 5, + saveScreenshotTo: screenshotDir, + saveVideoTo: videoDir, + videoChunkDuration: 10, } pipeline.setCapturer(config) expect(pipeline['capturer']).toEqual({ @@ -114,10 +106,8 @@ describe('K21Pipeline', () => { pipeline.setUploader('test-uploader') expect(() => pipeline.setCapturer({ fps: 1, - recordLengthInSeconds: 5, - saveVideo: false, - saveScreenshot: false - })).toThrow('Cannot set Capturer when Uploader is already set') + duration: 5 + })).toThrow('Cannot set Capturer when Uploader is already set') }) }) @@ -125,9 +115,7 @@ describe('K21Pipeline', () => { test('should throw error when setting uploader with capturer present', () => { pipeline.setCapturer({ fps: 1, - recordLengthInSeconds: 5, - saveVideo: false, - saveScreenshot: false + duration: 5 }) expect(() => pipeline.setUploader('test-uploader')).toThrow( 'Cannot set Uploader when Capturer is already set' @@ -145,9 +133,7 @@ describe('K21Pipeline', () => { test('should run with basic capture config', async () => { const config = { fps: 1, - recordLengthInSeconds: 5, - saveVideo: false, - saveScreenshot: false + duration: 5 } pipeline.setCapturer(config) expect(pipeline.run()).resolves.not.toThrow() @@ -157,20 +143,18 @@ describe('K21Pipeline', () => { const videoDir = path.join(tempDir, 'videos'); fs.mkdirSync(videoDir, { recursive: true }); - const recordLengthInSeconds = 5 - const videoChunkDurationInSeconds = 10 + const duration = 5 + const videoChunkDuration = 10 - const fullChunks = Math.floor(recordLengthInSeconds / videoChunkDurationInSeconds) - const extraChunk = recordLengthInSeconds % videoChunkDurationInSeconds > 0 ? 1 : 0 + const fullChunks = Math.floor(duration / videoChunkDuration) + const extraChunk = duration % videoChunkDuration > 0 ? 1 : 0 const expectedNumberOfMp4Files = fullChunks + extraChunk const config = { fps: 1, - recordLengthInSeconds, - saveVideo: true, - outputDirVideo: videoDir, - videoChunkDurationInSeconds, - saveScreenshot: false + duration: duration, + saveVideoTo: videoDir, + videoChunkDuration: videoChunkDuration, } pipeline.setCapturer(config) await expect(pipeline.run()).resolves.not.toThrow() @@ -197,11 +181,9 @@ describe('K21Pipeline', () => { const config = { fps, - recordLengthInSeconds, - saveVideo: false, - saveScreenshot: true, - outputDirScreenshot: screenshotDir - } + duration: recordLengthInSeconds, + saveScreenshotTo: screenshotDir + } pipeline.setCapturer(config) await expect(pipeline.run()).resolves.not.toThrow(); @@ -214,9 +196,7 @@ describe('K21Pipeline', () => { test('should return empty array for basic capture without processor', async () => { const config = { fps: 1, - recordLengthInSeconds: 5, - saveVideo: false, - saveScreenshot: false + duration: 5 } pipeline.setCapturer(config) const result = await pipeline.run() @@ -226,7 +206,7 @@ describe('K21Pipeline', () => { test('should return processed images when processor is set', async () => { const config = { fps: 1, - recordLengthInSeconds: 2, + duration: 2, } pipeline.setCapturer(config) pipeline.setProcessor({}) // Add mock processor diff --git a/bin/k21.d.ts b/bin/k21.d.ts index d974a64..60e0d24 100644 --- a/bin/k21.d.ts +++ b/bin/k21.d.ts @@ -22,15 +22,11 @@ interface CaptureConfig { /** Frames per second for capture. Default: 1 */ fps?: number; /** Total duration of capture in seconds. Default: 10 */ - recordLengthInSeconds?: number; + duration?: number; /** Whether to save screenshots during capture. Default: false */ saveScreenshot?: boolean; - /** Directory path for saving screenshots. Required if saveScreenshot is true */ - outputDirScreenshot?: string; /** Whether to save video during capture. Default: false */ saveVideo?: boolean; - /** Directory path for saving video files. Required if saveVideo is true */ - outputDirVideo?: string; /** Duration of each video chunk in seconds. Required if saveVideo is true. Default: 60 */ videoChunkDurationInSeconds?: number; } diff --git a/k21/k21.ts b/k21/k21.ts index 3007458..a5806fa 100644 --- a/k21/k21.ts +++ b/k21/k21.ts @@ -2,22 +2,20 @@ import k21 from './k21_internal' /** * Configuration options for screen capture - * Controls capture parameters, file saving behavior, - * and output locations for video and screenshots + * Controls capture parameters and output locations for video and screenshots * @example - * // Basic capture without saving + * // Basic capture configuration * const config: CaptureConfig = { * fps: 1, - * recordLengthInSeconds: 60 + * duration: 60 * } * - * // Capture with video saving + * // Capture with saving options * const config: CaptureConfig = { * fps: 1, - * recordLengthInSeconds: 60, - * saveVideo: true, - * outputDirVideo: '/path/to/videos', - * videoChunkDurationInSeconds: 60 + * duration: 60, + * saveVideoTo: '/path/to/videos', + * videoChunkDuration: 60 * } */ @@ -25,17 +23,13 @@ interface CaptureConfig { /** Frames per second for capture. Default: 1 */ fps?: number; /** Total duration of capture in seconds. Default: 10 */ - recordLengthInSeconds?: number; - /** Whether to save screenshots during capture. Default: false */ - saveScreenshot?: boolean; - /** Directory path for saving screenshots. Required if saveScreenshot is true */ - outputDirScreenshot?: string; - /** Whether to save video during capture. Default: false */ - saveVideo?: boolean; - /** Directory path for saving video files. Required if saveVideo is true */ - outputDirVideo?: string; - /** Duration of each video chunk in seconds. Required if saveVideo is true. Default: 60 */ - videoChunkDurationInSeconds?: number; + duration?: number; + /** Path where screenshots should be saved. If not provided, screenshots won't be saved */ + saveScreenshotTo?: string; + /** Path where video should be saved. If not provided, video won't be saved */ + saveVideoTo?: string; + /** Duration of each video chunk in seconds. Default: 60 */ + videoChunkDuration?: number; } /** Configuration for vision-based processing using external vision APIs */ @@ -96,12 +90,7 @@ class K21Pipeline { private processor: any; private defaultCaptureConfig = { fps: 1, - recordLengthInSeconds: 10, - saveVideo: false, - outputDirVideo: '', - videoChunkDurationInSeconds: 60, - saveScreenshot: false, - outputDirScreenshot: '', + duration: 10, }; private defaultProcessorConfig = { diff --git a/src/capture.rs b/src/capture.rs index 732683b..b85c36a 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -3,25 +3,21 @@ use k21::capture::ScreenCaptureConfig; #[napi(object)] pub struct JsScreenCaptureConfig { - pub fps: f64, - pub video_chunk_duration_in_seconds: u32, - pub save_screenshot: bool, - pub save_video: bool, - pub record_length_in_seconds: u32, - pub output_dir_video: Option, - pub output_dir_screenshot: Option, + pub fps: Option, + pub duration: Option, + pub save_screenshot_to: Option, + pub save_video_to: Option, + pub video_chunk_duration: Option, } impl From for JsScreenCaptureConfig { fn from(config: ScreenCaptureConfig) -> Self { Self { - fps: config.fps as f64, - video_chunk_duration_in_seconds: config.video_chunk_duration_in_seconds as u32, - save_screenshot: config.save_screenshot, - save_video: config.save_video, - record_length_in_seconds: config.record_length_in_seconds as u32, - output_dir_video: config.output_dir_video, - output_dir_screenshot: config.output_dir_screenshot, + fps: config.fps.map(|f| f as f64), + duration: config.duration.map(|d| d as u32), + save_screenshot_to: config.save_screenshot_to, + save_video_to: config.save_video_to, + video_chunk_duration: config.video_chunk_duration.map(|d| d as u32), } } } @@ -29,13 +25,11 @@ impl From for JsScreenCaptureConfig { impl From for ScreenCaptureConfig { fn from(config: JsScreenCaptureConfig) -> Self { Self { - fps: config.fps as f32, - video_chunk_duration_in_seconds: config.video_chunk_duration_in_seconds as u64, - save_screenshot: config.save_screenshot, - save_video: config.save_video, - record_length_in_seconds: config.record_length_in_seconds as u64, - output_dir_video: config.output_dir_video, - output_dir_screenshot: config.output_dir_screenshot, + fps: config.fps.map(|f| f as f32), + duration: config.duration.map(|d| d as u64), + save_screenshot_to: config.save_screenshot_to, + save_video_to: config.save_video_to, + video_chunk_duration: config.video_chunk_duration.map(|d| d as u64), } } } From fc9b8290236386aba3d4afbefd30e1a197acf846 Mon Sep 17 00:00:00 2001 From: ferzu13 Date: Fri, 28 Mar 2025 17:11:03 +0100 Subject: [PATCH 2/2] renamed K21Pipeline to K21 --- __test__/index.test.ts | 78 +++++++++++++++++++++--------------------- bin/index.d.ts | 4 +-- bin/index.js | 4 +-- bin/k21.d.ts | 32 ++++++++--------- bin/k21.js | 13 +++---- k21/index.ts | 4 +-- k21/k21.ts | 4 +-- 7 files changed, 66 insertions(+), 73 deletions(-) diff --git a/__test__/index.test.ts b/__test__/index.test.ts index 724c35c..4397eae 100644 --- a/__test__/index.test.ts +++ b/__test__/index.test.ts @@ -1,17 +1,17 @@ // import k21 from '@k21' -import { K21Pipeline } from '@k21' +import { K21 } from '@k21' import fs from 'fs'; import path from 'path'; import os from 'os'; jest.setTimeout(20000); // Set global timeout -describe('K21Pipeline', () => { - let pipeline: K21Pipeline +describe('K21', () => { + let k21: K21 let tempDir: string; beforeEach(() => { - pipeline = new K21Pipeline() + k21 = new K21() // Create unique temp directory for each test tempDir = path.join(os.tmpdir(), `k21-test-${Date.now()}`); fs.mkdirSync(tempDir, { recursive: true }); @@ -26,9 +26,9 @@ describe('K21Pipeline', () => { describe('initialization', () => { test('should initialize with null values', () => { - expect(pipeline['capturer']).toBeNull() - expect(pipeline['uploader']).toBeNull() - expect(pipeline['processor']).toBeNull() + expect(k21['capturer']).toBeNull() + expect(k21['uploader']).toBeNull() + expect(k21['processor']).toBeNull() }) }) @@ -38,9 +38,9 @@ describe('K21Pipeline', () => { fps: 1, duration: 5, } - pipeline.setCapturer(config) - expect(pipeline['capturer']).toEqual({ - ...pipeline['defaultConfig'], + k21.setCapturer(config) + expect(k21['capturer']).toEqual({ + ...k21['defaultConfig'], ...config }) }) @@ -55,10 +55,10 @@ describe('K21Pipeline', () => { saveVideoTo: videoDir, videoChunkDuration: 10, } - pipeline.setCapturer(config) - expect(pipeline['capturer']).toEqual({ - ...pipeline['defaultConfig'], - ...pipeline['defaultConfigSaveVideo'], + k21.setCapturer(config) + expect(k21['capturer']).toEqual({ + ...k21['defaultConfig'], + ...k21['defaultConfigSaveVideo'], ...config }) }) @@ -72,10 +72,10 @@ describe('K21Pipeline', () => { duration: 5, saveScreenshotTo: screenshotDir } - pipeline.setCapturer(config) - expect(pipeline['capturer']).toEqual({ - ...pipeline['defaultConfig'], - ...pipeline['defaultConfigSaveScreenshot'], + k21.setCapturer(config) + expect(k21['capturer']).toEqual({ + ...k21['defaultConfig'], + ...k21['defaultConfigSaveScreenshot'], ...config }) }) @@ -93,18 +93,18 @@ describe('K21Pipeline', () => { saveVideoTo: videoDir, videoChunkDuration: 10, } - pipeline.setCapturer(config) - expect(pipeline['capturer']).toEqual({ - ...pipeline['defaultConfig'], - ...pipeline['defaultConfigSaveVideo'], - ...pipeline['defaultConfigSaveScreenshot'], + k21.setCapturer(config) + expect(k21['capturer']).toEqual({ + ...k21['defaultConfig'], + ...k21['defaultConfigSaveVideo'], + ...k21['defaultConfigSaveScreenshot'], ...config }) }) test('should throw error when setting capturer with uploader present', () => { - pipeline.setUploader('test-uploader') - expect(() => pipeline.setCapturer({ + k21.setUploader('test-uploader') + expect(() => k21.setCapturer({ fps: 1, duration: 5 })).toThrow('Cannot set Capturer when Uploader is already set') @@ -113,11 +113,11 @@ describe('K21Pipeline', () => { describe('setUploader', () => { test('should throw error when setting uploader with capturer present', () => { - pipeline.setCapturer({ + k21.setCapturer({ fps: 1, duration: 5 }) - expect(() => pipeline.setUploader('test-uploader')).toThrow( + expect(() => k21.setUploader('test-uploader')).toThrow( 'Cannot set Uploader when Capturer is already set' ) }) @@ -125,7 +125,7 @@ describe('K21Pipeline', () => { describe('run', () => { test('should throw error when neither capturer nor uploader is set', async () => { - expect(pipeline.run()).rejects.toThrow( + expect(k21.run()).rejects.toThrow( 'Either Capturer or Uploader must be set' ) }) @@ -135,8 +135,8 @@ describe('K21Pipeline', () => { fps: 1, duration: 5 } - pipeline.setCapturer(config) - expect(pipeline.run()).resolves.not.toThrow() + k21.setCapturer(config) + expect(k21.run()).resolves.not.toThrow() }) test('should run with video capture config', async () => { @@ -156,8 +156,8 @@ describe('K21Pipeline', () => { saveVideoTo: videoDir, videoChunkDuration: videoChunkDuration, } - pipeline.setCapturer(config) - await expect(pipeline.run()).resolves.not.toThrow() + k21.setCapturer(config) + await expect(k21.run()).resolves.not.toThrow() // Check for video file @@ -184,8 +184,8 @@ describe('K21Pipeline', () => { duration: recordLengthInSeconds, saveScreenshotTo: screenshotDir } - pipeline.setCapturer(config) - await expect(pipeline.run()).resolves.not.toThrow(); + k21.setCapturer(config) + await expect(k21.run()).resolves.not.toThrow(); // Check number of screenshots const files = fs.readdirSync(screenshotDir); @@ -198,8 +198,8 @@ describe('K21Pipeline', () => { fps: 1, duration: 5 } - pipeline.setCapturer(config) - const result = await pipeline.run() + k21.setCapturer(config) + const result = await k21.run() expect(result).toEqual([]) }) @@ -208,10 +208,10 @@ describe('K21Pipeline', () => { fps: 1, duration: 2, } - pipeline.setCapturer(config) - pipeline.setProcessor({}) // Add mock processor + k21.setCapturer(config) + k21.setProcessor({}) // Add mock processor - const result = await pipeline.run() + const result = await k21.run() expect(Array.isArray(result)).toBe(true) expect(result.length).toBeGreaterThan(0) diff --git a/bin/index.d.ts b/bin/index.d.ts index 6aa6ffb..c6e57e3 100644 --- a/bin/index.d.ts +++ b/bin/index.d.ts @@ -1,2 +1,2 @@ -import { K21Pipeline } from './k21'; -export { K21Pipeline }; +import { K21 } from './k21'; +export { K21 }; diff --git a/bin/index.js b/bin/index.js index a55ab11..f1c67b2 100644 --- a/bin/index.js +++ b/bin/index.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.K21Pipeline = void 0; +exports.K21 = void 0; // import * as lib from './lib' const k21_1 = require("./k21"); -Object.defineProperty(exports, "K21Pipeline", { enumerable: true, get: function () { return k21_1.K21Pipeline; } }); +Object.defineProperty(exports, "K21", { enumerable: true, get: function () { return k21_1.K21; } }); diff --git a/bin/k21.d.ts b/bin/k21.d.ts index 60e0d24..654023e 100644 --- a/bin/k21.d.ts +++ b/bin/k21.d.ts @@ -1,21 +1,19 @@ /** * Configuration options for screen capture - * Controls capture parameters, file saving behavior, - * and output locations for video and screenshots + * Controls capture parameters and output locations for video and screenshots * @example - * // Basic capture without saving + * // Basic capture configuration * const config: CaptureConfig = { * fps: 1, - * recordLengthInSeconds: 60 + * duration: 60 * } * - * // Capture with video saving + * // Capture with saving options * const config: CaptureConfig = { * fps: 1, - * recordLengthInSeconds: 60, - * saveVideo: true, - * outputDirVideo: '/path/to/videos', - * videoChunkDurationInSeconds: 60 + * duration: 60, + * saveVideoTo: '/path/to/videos', + * videoChunkDuration: 60 * } */ interface CaptureConfig { @@ -23,12 +21,12 @@ interface CaptureConfig { fps?: number; /** Total duration of capture in seconds. Default: 10 */ duration?: number; - /** Whether to save screenshots during capture. Default: false */ - saveScreenshot?: boolean; - /** Whether to save video during capture. Default: false */ - saveVideo?: boolean; - /** Duration of each video chunk in seconds. Required if saveVideo is true. Default: 60 */ - videoChunkDurationInSeconds?: number; + /** Path where screenshots should be saved. If not provided, screenshots won't be saved */ + saveScreenshotTo?: string; + /** Path where video should be saved. If not provided, video won't be saved */ + saveVideoTo?: string; + /** Duration of each video chunk in seconds. Default: 60 */ + videoChunkDuration?: number; } /** Configuration for vision-based processing using external vision APIs */ interface VisionConfig { @@ -78,7 +76,7 @@ interface ImageData { /** Type of processing applied to the frame (e.g., "OCR", "CLASSIFICATION") */ processingType: string; } -declare class K21Pipeline { +declare class K21 { private capturer; private uploader; private processor; @@ -128,4 +126,4 @@ declare class K21Pipeline { */ run(): Promise; } -export { K21Pipeline, ImageData, CaptureConfig }; +export { K21, ImageData, CaptureConfig }; diff --git a/bin/k21.js b/bin/k21.js index 58ce0e5..464db9b 100644 --- a/bin/k21.js +++ b/bin/k21.js @@ -3,20 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.K21Pipeline = void 0; +exports.K21 = void 0; const k21_internal_1 = __importDefault(require("./k21_internal")); -class K21Pipeline { +class K21 { capturer; uploader; processor; defaultCaptureConfig = { fps: 1, - recordLengthInSeconds: 10, - saveVideo: false, - outputDirVideo: '', - videoChunkDurationInSeconds: 60, - saveScreenshot: false, - outputDirScreenshot: '', + duration: 10, }; defaultProcessorConfig = { processingType: 'OCR', @@ -127,4 +122,4 @@ class K21Pipeline { } } } -exports.K21Pipeline = K21Pipeline; +exports.K21 = K21; diff --git a/k21/index.ts b/k21/index.ts index 2ee46ea..0ce566a 100644 --- a/k21/index.ts +++ b/k21/index.ts @@ -1,3 +1,3 @@ // import * as lib from './lib' -import { K21Pipeline } from './k21' -export { K21Pipeline } +import { K21 } from './k21' +export { K21 } diff --git a/k21/k21.ts b/k21/k21.ts index a5806fa..c997ee2 100644 --- a/k21/k21.ts +++ b/k21/k21.ts @@ -84,7 +84,7 @@ interface ImageData { processingType: string; } -class K21Pipeline { +class K21 { private capturer: any; private uploader: any; private processor: any; @@ -214,4 +214,4 @@ class K21Pipeline { } // Export default instance -export { K21Pipeline, ImageData, CaptureConfig } +export { K21, ImageData, CaptureConfig }