nodejs / nodejs/node-addon-api

Unify handling of string-like arguments across the API

Open
#1,745 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

SemVer-major
Dominant language
C++
Stars
2.4k
Forks
499
Avg merge
2d 11h
Merged PRs (30d)
2

Description

Follow-up to #1741 / #1742.

Several APIs still carry multiple overloads for different string types. Now that the library requires C++17, we can modernize by preferring std::string_view and removing the redundant UTF-8 overloads, replacing them with a single constrained template, as a semver-major change.

For String::New, Symbol::New, and Symbol::For, collapse the const std::string& + std::string_view pair into:

template <typename T,
          std::enable_if_t<
              std::is_convertible_v<const T&, std::string_view> &&
              !std::is_convertible_v<const T&, const char*>, int> = 0>
static String New(napi_env env, const T& t);

This matches the C++17 standard-library idiom (std::string's find/append/compare/… members). The !is_convertible_v<const T&, const char*> clause keeps const char* and literals on the dedicated overload and filters nullptr (avoiding a UB string_view(nullptr)).

The enable_if alias is shareable across APIs e.g. enable_if_string_view_like_t<T>.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the declarations and implementations of String::New, Symbol::New, and Symbol::For, then compare their existing std::string and std::string_view overloads. Done means the redundant overloads are unified through the constrained string_view-like template while const char* and literals retain their dedicated handling and nullptr remains excluded.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.