feat: improved piping & accept stdin as image input for CLI#380
feat: improved piping & accept stdin as image input for CLI#380
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughAdds stdin support to the SQIP CLI: detect piped input via fstatSync(0).isFIFO(), read image data from stdin, treat stdin inputs as named "stdin" with MIME detection from image metadata, and route SVG output to stdout or an output file accordingly. Tests (unit and e2e) added/updated for these flows. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Shell
participant SQIP_CLI as SQIP CLI
participant SQIP_Core as sqip (processor)
participant FS as Filesystem
rect rgba(200,200,255,0.5)
User->>Shell: curl ... | sqip
Shell->>SQIP_CLI: piped stdin (fd 0)
SQIP_CLI->>SQIP_CLI: fstatSync(0).isFIFO() -> true
SQIP_CLI->>SQIP_CLI: read stdin bytes
SQIP_CLI->>SQIP_Core: processImage(buffer, config with outputFileName="stdin")
SQIP_Core->>SQIP_Core: sharp.metadata() -> determine mimeType
SQIP_Core-->>SQIP_CLI: SVG result
alt output to file (-o)
SQIP_CLI->>FS: write SVG to output path
FS-->>SQIP_CLI: write complete
SQIP_CLI->>Shell: (no stdout if silent)
else print to stdout (default for stdin)
SQIP_CLI->>Shell: write SVG to stdout
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
|
@xerc WDYT? :) |
| } | ||
|
|
||
| if (missing.length) { | ||
| if (!args.input) { |
There was a problem hiding this comment.
SQIP actually only used required for the input parameter, all the others have sane defaults.
So dropping the missing others check is totally fine here!
|
@axe312ger these could also be from other GLOBAL packages .. INFOs% node -v v20.20.0 % npm -g list /usr/local/lib ├── css-checker-kit@0.4.2 ├── glyphhanger@5.0.0 ├── npm-check@6.0.1 ├── npm@11.11.0 └── porffor@0.61.11 ERRORsnpm error code 1 npm error path /usr/local/lib/node_modules/sqip-plugin-data-uri/node_modules/sharp npm error command failed npm error command sh -c node install/check.js || npm run build npm error > sharp@0.34.5 build npm error > node install/build.js npm error npm error sharp: Attempting to build from source via node-gyp npm error sharp: See https://sharp.pixelplumbing.com/install#building-from-source npm error sharp: Found node-addon-api npm error sharp: Please add node-gyp to your dependencies DEPRECATEDnpm warn deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility npm warn deprecated q@1.5.1: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other. npm warn deprecated (For a CapTP with native promises, see @endo/eventual-send and @endo/captp) npm warn deprecated xmldom@0.1.31: Deprecated due to CVE-2021-21366 resolved in 0.5.0 npm warn deprecated prebuild-install@7.1.3: No longer maintained. Please contact the author of the relevant native addon; alternatives are available. npm warn deprecated argv@0.0.2: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info. npm warn deprecated svgo@0.7.2: This SVGO version is no longer supported. Upgrade to v2.x.x. npm warn deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x. |
|
@xerc did you run from this branch for sure? If you |
Allow piping image data via process.stdin, enabling usage like: curl -s https://example.com/image.jpg | sqip -p pixels cat photo.png | sqip -p pixels -o result.svg When stdin is detected (via !process.stdin.isTTY), the CLI reads all data into a buffer and passes it to the core sqip() function. Stdin mode defaults to print=true and silent=true for clean piping output. The --input flag takes precedence when both are provided. Also improves buffer input handling in the core library: - Detect mime type from buffer contents using sharp metadata - Allow print to work even when silent is true (for piping) Closes #334 Co-Authored-By: Mastra Code (anthropic/claude-opus-4-6) <noreply@mastra.ai>
abd4c37 to
9add34f
Compare
|
I rebased this branch now as well, so most of the dependency updates from yesterday should be in here now as well |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/sqip-cli/__tests__/unit/sqip-cli.test.ts (1)
54-54: Consider usingmockClear()instead ofmockRestore()for consistency.Using
mockRestore()onmockedFstatSyncresets it to the original implementation, which may cause the mock to behave unexpectedly in subsequent tests since the module-levelvi.mockalready wraps it. Consider usingmockClear()to reset call history while preserving the mock wrapper, or explicitly re-apply the mock behavior in tests that need it.♻️ Suggested change
- mockedFstatSync.mockRestore() + mockedFstatSync.mockClear()🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/sqip-cli/__tests__/unit/sqip-cli.test.ts` at line 54, The test currently calls mockedFstatSync.mockRestore(), which restores the original implementation and can interfere with module-level vi.mock wrappers; replace that call with mockedFstatSync.mockClear() to clear call history while preserving the mock wrapper (or, if the intention was to fully restore, re-apply the mock afterwards); locate the call to mockedFstatSync.mockRestore() in the test file and change it to mockedFstatSync.mockClear() (or add a re-mock step) to ensure consistent mock behavior across tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/sqip-cli/__tests__/unit/sqip-cli.test.ts`:
- Line 54: The test currently calls mockedFstatSync.mockRestore(), which
restores the original implementation and can interfere with module-level vi.mock
wrappers; replace that call with mockedFstatSync.mockClear() to clear call
history while preserving the mock wrapper (or, if the intention was to fully
restore, re-apply the mock afterwards); locate the call to
mockedFstatSync.mockRestore() in the test file and change it to
mockedFstatSync.mockClear() (or add a re-mock step) to ensure consistent mock
behavior across tests.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e34fe509-c26b-48bb-9fee-33d3763207b0
⛔ Files ignored due to path filters (3)
packages/sqip-cli/__tests__/e2e/__snapshots__/sqip-cli-e2e.test.ts.snapis excluded by!**/*.snappackages/sqip-cli/__tests__/unit/__snapshots__/sqip-cli.test.ts.snapis excluded by!**/*.snappackages/sqip/__tests__/unit/__snapshots__/sqip.test.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (4)
packages/sqip-cli/__tests__/e2e/sqip-cli-e2e.test.tspackages/sqip-cli/__tests__/unit/sqip-cli.test.tspackages/sqip-cli/src/sqip-cli.tspackages/sqip/src/sqip.ts
|
DEPRnpm warn deprecated git-raw-commits@3.0.0: This package is no longer maintained. For the JavaScript API, please use @conventional-changelog/git-client instead. npm warn deprecated git-semver-tags@5.0.1: This package is no longer maintained. For the JavaScript API, please use @conventional-changelog/git-client instead. npm warn deprecated whatwg-encoding@3.1.1: Use @exodus/bytes instead for a more spec-conformant and faster implementation npm warn deprecated glob@11.1.0: Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me INFOnpm error code 1 npm error path github-sqip/packages/sqip-plugin-primitive npm error command failed npm error command sh -c npm run build-vendor npm error > sqip-plugin-primitive@1.0.0-alpha.53 build-vendor npm error > node scripts/bundle-vendor.js npm error npm error npm error --- npm error npm error Unable to download and build primitive from https://github.com/hashbite/primitive. npm error npm error Is go installed? npm error npm error Some users might just want to download it from here: https://golang.org/dl/ npm error npm error Brew users: brew install go npm error npm error --- ERROR npm error code 1
npm error path github-sqip/node_modules/sharp
npm error command failed
npm error command sh -c node install/check.js || npm run build
npm error > sharp@0.34.5 build
npm error > node install/build.js
npm error
npm error sharp: Attempting to build from source via node-gyp
npm error sharp: See https://sharp.pixelplumbing.com/install#building-from-source
npm error sharp: Please add node-addon-api to your dependencies |
Summary
Implements #334 — allow the CLI to accept piped image data via
process.stdin.Usage
Changes
CLI (
sqip-cli)--inputis no longer required — omit it when piping via stdin!process.stdin.isTTY, reads data withprocess.stdin.toArray()--inputflag takes precedence when both stdin and flag are providedprint=true(output SVG to stdout),silent=true(no metadata tables)Core (
sqip)sharpmetadata (instead of returning'unknown')printnow works even whensilentis true, enabling clean piping workflowsTests
-o)Test Plan
Closes #334
Summary by CodeRabbit
New Features
cat image.jpg | sqip), with output file option (-o) and sensible defaults when reading from stdin.Tests