Skip to content

Commit

Permalink
fix: 修复 electron 21 buffer 崩溃问题与 MacOS 13 崩溃问题 (#19)
Browse files Browse the repository at this point in the history
* fix: 修复 MacOS 13 崩溃问题 (#17)

* fix: 修复 electron 21 buffer 崩溃问题

0.0.13

* feat: 升级依赖

* feat: 关闭linux测试
  • Loading branch information
nashaofu authored Dec 4, 2022
1 parent 36020d5 commit aa22588
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 267 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Docker
name: docker-publish

on:
workflow_dispatch: # 手动触发
push:
branches: ['master']
# Publish semver tags as releases.
Expand Down
80 changes: 40 additions & 40 deletions .github/workflows/CI.yml → .github/workflows/napi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: napi
env:
DEBUG: napi:*
APP_NAME: node-screenshots
Expand Down Expand Up @@ -197,43 +197,43 @@ jobs:
shell: bash
- name: Test bindings
run: yarn test
test-linux-x64-gnu-binding:
name: Test bindings on Linux-x64-gnu - node@${{ matrix.node }}
needs:
- build
strategy:
fail-fast: false
matrix:
node:
- '14'
- '16'
- '18'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
check-latest: true
cache: yarn
- name: Cache NPM dependencies
uses: actions/cache@v3
with:
path: .yarn/cache
key: npm-cache-test-linux-x64-gnu-${{ matrix.node }}
- name: Install dependencies
run: yarn install
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: bindings-x86_64-unknown-linux-gnu
path: .
- name: List packages
run: ls -R .
shell: bash
- name: Test bindings
run: docker run --rm -v $(pwd):/build -w /build ghcr.io/nashaofu/node-screenshots/node-desktop:${{ matrix.node }}-slim yarn test
# test-linux-x64-gnu-binding:
# name: Test bindings on Linux-x64-gnu - node@${{ matrix.node }}
# needs:
# - build
# strategy:
# fail-fast: false
# matrix:
# node:
# - '14'
# - '16'
# - '18'
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - name: Setup node
# uses: actions/setup-node@v3
# with:
# node-version: ${{ matrix.node }}
# check-latest: true
# cache: yarn
# - name: Cache NPM dependencies
# uses: actions/cache@v3
# with:
# path: .yarn/cache
# key: npm-cache-test-linux-x64-gnu-${{ matrix.node }}
# - name: Install dependencies
# run: yarn install
# - name: Download artifacts
# uses: actions/download-artifact@v3
# with:
# name: bindings-x86_64-unknown-linux-gnu
# path: .
# - name: List packages
# run: ls -R .
# shell: bash
# - name: Test bindings
# run: docker run --rm -v $(pwd):/build -w /build ghcr.io/nashaofu/node-screenshots/node-desktop:${{ matrix.node }}-slim yarn test
test-linux-x64-musl-binding:
name: Test bindings on x86_64-unknown-linux-musl - node@${{ matrix.node }}
needs:
Expand Down Expand Up @@ -355,7 +355,7 @@ jobs:
runs-on: ubuntu-latest
needs:
- test-macOS-windows-binding
- test-linux-x64-gnu-binding
# - test-linux-x64-gnu-binding
- test-linux-x64-musl-binding
# - test-linux-aarch64-gnu-binding
# - test-linux-aarch64-musl-binding
Expand Down Expand Up @@ -398,4 +398,4 @@ jobs:
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.9.1", default-features = false, features = ["napi4"] }
napi = { version = "2.10.1", default-features = false, features = ["napi4"] }
napi-derive = "2.9.1"
screenshots = "0.4.4"
screenshots = "0.4.5"

[build-dependencies]
napi-build = "2.0.1"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-screenshots",
"version": "0.0.12",
"version": "0.0.13",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand Down Expand Up @@ -32,7 +32,7 @@
},
"packageManager": "[email protected]",
"devDependencies": {
"@napi-rs/cli": "^2.11.4",
"ava": "^4.3.0"
"@napi-rs/cli": "^2.13.0",
"ava": "^5.1.0"
}
}
40 changes: 25 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
extern crate napi_derive;

use napi::{
bindgen_prelude::{AsyncTask, Buffer, Error, Result},
Task,
bindgen_prelude::{AsyncTask, Error, Result},
Env, JsBuffer, Task,
};

use screenshots::Screen;
use screenshots::{Image, Screen};
use std::thread;

#[napi]
Expand All @@ -32,8 +32,8 @@ pub struct AsyncCapture {

#[napi]
impl Task for AsyncCapture {
type Output = Buffer;
type JsValue = Buffer;
type Output = Image;
type JsValue = JsBuffer;

fn compute(&mut self) -> Result<Self::Output> {
let AsyncCapture { screen, area } = *self;
Expand All @@ -44,22 +44,23 @@ impl Task for AsyncCapture {
screen.capture()?
};

Some(Buffer::from(image.buffer().clone()))
Some(image)
});

let capture_result = match handle.join() {
let capture_image = match handle.join() {
Ok(result) => result,
Err(_) => None,
};

match capture_result {
Some(buffer) => Ok(buffer),
match capture_image {
Some(image) => Ok(image),
None => Err(Error::from_reason(String::from("Capture failed"))),
}
}

fn resolve(&mut self, _env: napi::Env, output: Self::Output) -> Result<Self::JsValue> {
Ok(output)
fn resolve(&mut self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
let buffer = env.create_buffer_copy(output.buffer())?;
Ok(buffer.into_raw())
}
}

Expand Down Expand Up @@ -102,9 +103,10 @@ impl Screenshots {
}

#[napi]
pub fn capture_sync(&self) -> Option<Buffer> {
pub fn capture_sync(&self, env: Env) -> Option<JsBuffer> {
let image = self.screen.capture()?;
Some(Buffer::from(image.buffer().clone()))
let buffer = env.create_buffer_copy(image.buffer()).ok()?;
Some(buffer.into_raw())
}

#[napi]
Expand All @@ -116,9 +118,17 @@ impl Screenshots {
}

#[napi]
pub fn capture_area_sync(&self, x: i32, y: i32, width: u32, height: u32) -> Option<Buffer> {
pub fn capture_area_sync(
&self,
env: Env,
x: i32,
y: i32,
width: u32,
height: u32,
) -> Option<JsBuffer> {
let image = self.screen.capture_area(x, y, width, height)?;
Some(Buffer::from(image.buffer().clone()))
let buffer = env.create_buffer_copy(image.buffer()).ok()?;
Some(buffer.into_raw())
}

#[napi]
Expand Down
Loading

0 comments on commit aa22588

Please sign in to comment.