Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli,napi-derive): re-export types from shared crate #1531

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"./crates/napi",
"./crates/sys",
"./examples/napi",
"./examples/napi-shared",
"./examples/napi-compat-mode",
"./examples/binary",
"./bench",
Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@napi-rs/cli",
"version": "2.15.0",
"version": "2.15.1-alpha.0",
"description": "Cli tools for napi-rs",
"keywords": [
"cli",
Expand Down
12 changes: 12 additions & 0 deletions cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import * as chalk from 'colorette'
import envPaths from 'env-paths'
import { groupBy } from 'lodash-es'

import { version } from '../package.json'

import { ARM_FEATURES_H } from './arm-features.h'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
Expand Down Expand Up @@ -493,6 +495,7 @@ export class BuildCommand extends Command {
}
const cwdSha = createHash('sha256')
.update(process.cwd())
.update(version)
.digest('hex')
.substring(0, 8)
const intermediateTypeFile = join(
Expand Down Expand Up @@ -698,6 +701,15 @@ async function processIntermediateTypeFile(
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
.map((line) => {
// compatible with old version
if (line.startsWith('{')) {
return line
} else {
const [_crateName, ...rest] = line.split(':')
return rest.join(':')
}
})

if (!lines.length) {
return idents
Expand Down
4 changes: 3 additions & 1 deletion crates/backend/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn escape_json(src: &str) -> String {

impl ToString for TypeDef {
fn to_string(&self) -> String {
let pkg_name = std::env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME is not set");
let js_mod = if let Some(js_mod) = &self.js_mod {
format!(", \"js_mod\": \"{}\"", js_mod)
} else {
Expand All @@ -103,7 +104,8 @@ impl ToString for TypeDef {
"".to_owned()
};
format!(
r#"{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
r#"{}:{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
pkg_name,
self.kind,
self.name,
escape_json(&self.js_doc),
Expand Down
39 changes: 37 additions & 2 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
{
// logic on first macro expansion
#[cfg(feature = "type-def")]
if let Ok(type_def_file) = env::var("TYPE_DEF_TMP_PATH") {
if let Err(_e) = fs::remove_file(type_def_file) {
if let Ok(ref type_def_file) = env::var("TYPE_DEF_TMP_PATH") {
if let Err(_e) = remove_existed_type_def(type_def_file) {
#[cfg(debug_assertions)]
{
println!("Failed to manipulate type def file: {:?}", _e);
Expand Down Expand Up @@ -432,3 +432,38 @@ fn replace_napi_attr_in_mod(
None
}
}

#[cfg(all(feature = "type-def", not(feature = "noop")))]
fn remove_existed_type_def(type_def_file: &str) -> std::io::Result<()> {
use std::io::{BufRead, BufReader};

let pkg_name = std::env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME is not set");
if let Ok(content) = std::fs::File::open(type_def_file) {
let reader = BufReader::new(content);
let cleaned_content = reader
.lines()
.filter_map(|line| {
if let Ok(line) = line {
if let Some((package_name, _)) = line.split_once(':') {
if pkg_name == package_name {
return None;
}
}
Some(line)
} else {
None
}
})
.collect::<Vec<String>>()
.join("\n");
let mut content = std::fs::OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.open(type_def_file)?;

content.write_all(cleaned_content.as_bytes())?;
content.write_all(b"\n")?;
}
Ok(())
}
22 changes: 22 additions & 0 deletions examples/napi-shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
edition = "2021"
name = "napi-shared"
publish = false
version = "0.1.0"

[dependencies]
napi = { path = "../../crates/napi", default-features = false, features = [
"tokio_fs",
"napi8",
"tokio_rt",
"serde-json",
"async",
"experimental",
"latin1",
"chrono_date",
] }
napi-derive = { path = "../../crates/macro", features = ["type-def"] }

[build-dependencies]
napi-build = { path = "../../crates/build" }
6 changes: 6 additions & 0 deletions examples/napi-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use napi_derive::napi;

#[napi(object)]
pub struct Shared {
pub value: u32,
}
1 change: 1 addition & 0 deletions examples/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ napi = { path = "../../crates/napi", default-features = false, features = [
"chrono_date",
] }
napi-derive = { path = "../../crates/macro", features = ["type-def"] }
napi-shared = { path = "../napi-shared" }
serde = "1"
serde_derive = "1"
serde_json = "1"
Expand Down
4 changes: 4 additions & 0 deletions examples/napi/__test__/typegen.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Generated by [AVA](https://avajs.dev).
[K: symbol]: T␊
}␊
}␊
export interface Shared {␊
value: number␊
}␊
/** This is a const */␊
export const DEFAULT_COST: number␊
export function getWords(): Array<string>␊
Expand Down Expand Up @@ -192,6 +195,7 @@ Generated by [AVA](https://avajs.dev).
export function readPackageJson(): PackageJson␊
export function getPackageJsonName(packageJson: PackageJson): string␊
export function testSerdeRoundtrip(data: any): any␊
export function returnFromSharedCrate(): Shared␊
export function contains(source: string, target: string): boolean␊
export function concatStr(s: string): string␊
export function concatUtf16(s: string): string␊
Expand Down
Binary file modified examples/napi/__test__/typegen.spec.ts.snap
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/napi/__test__/values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import {
runScript,
tsfnReturnPromise,
tsfnReturnPromiseTimeout,
returnFromSharedCrate,
} from '../'

test('export const', (t) => {
Expand Down Expand Up @@ -650,6 +651,12 @@ test('should be able to run script', async (t) => {
t.is(await runScript(`Promise.resolve(1)`), 1)
})

test('should be able to return object from shared crate', (t) => {
t.deepEqual(returnFromSharedCrate(), {
value: 42,
})
})

const AbortSignalTest =
typeof AbortController !== 'undefined' ? test : test.skip

Expand Down
4 changes: 4 additions & 0 deletions examples/napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export class ExternalObject<T> {
[K: symbol]: T
}
}
export interface Shared {
value: number
}
/** This is a const */
export const DEFAULT_COST: number
export function getWords(): Array<string>
Expand Down Expand Up @@ -182,6 +185,7 @@ export interface PackageJson {
export function readPackageJson(): PackageJson
export function getPackageJsonName(packageJson: PackageJson): string
export function testSerdeRoundtrip(data: any): any
export function returnFromSharedCrate(): Shared
export function contains(source: string, target: string): boolean
export function concatStr(s: string): string
export function concatUtf16(s: string): string
Expand Down
1 change: 1 addition & 0 deletions examples/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod object;
mod promise;
mod reference;
mod serde;
mod shared;
mod string;
mod symbol;
mod task;
Expand Down
7 changes: 7 additions & 0 deletions examples/napi/src/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use napi_derive::napi;
use napi_shared::Shared;

#[napi]
pub fn return_from_shared_crate() -> Shared {
Shared { value: 42 }
}
4 changes: 2 additions & 2 deletions examples/napi/src/threadsafe_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ pub async fn tsfn_return_promise_timeout(func: ThreadsafeFunction<u32>) -> Resul
let sleep = time::sleep(Duration::from_nanos(1));
tokio::select! {
_ = sleep => {
return Err(Error::new(Status::GenericFailure, "Timeout".to_owned()));
Err(Error::new(Status::GenericFailure, "Timeout".to_owned()))
}
value = promise => {
return Ok(value? + 2);
Ok(value? + 2)
}
}
}
2 changes: 1 addition & 1 deletion memory-testing/test-util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export async function createSuite(testFile, maxMemoryUsage) {
stats.on('data', (d) => {
const { memory_stats } = JSON.parse(d.toString('utf8'))
if (Date.now() - initialDate > 10000 && !shouldAssertMemoryUsage) {
resolve()
initialMemoryUsage = memory_stats.usage
shouldAssertMemoryUsage = true
resolve()
}
if (shouldAssertMemoryUsage && memory_stats?.usage) {
const memoryGrowth = memory_stats.usage - initialMemoryUsage
Expand Down