Skip to content

Commit ce29f69

Browse files
chearonzbjornson
authored andcommitted
port to node-addon-api (remove NAN, v8, libuv)
1 parent adf73ee commit ce29f69

38 files changed

+1920
-2101
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1414
* Avoid calling virtual methods in constructors/destructors to avoid bypassing virtual dispatch. (#2229)
1515
* Remove unused private field `backend` in the `Backend` class. (#2229)
1616
* Add Node.js v20 to CI. (#2237)
17+
* Migrated to N-API (by way of node-addon-api) and removed libuv and v8 dependencies
1718
### Added
1819
* Added string tags to support class detection
1920
### Fixed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ npm install canvas
1313

1414
By default, binaries for macOS, Linux and Windows will be downloaded. If you want to build from source, use `npm install --build-from-source` and see the **Compiling** section below.
1515

16-
The minimum version of Node.js required is **6.0.0**.
16+
The minimum version of Node.js required is **10.20.0**.
1717

1818
### Compiling
1919

binding.gyp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
},
5858
{
5959
'target_name': 'canvas',
60-
'include_dirs': ["<!(node -e \"require('nan')\")"],
60+
'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"],
61+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS', 'NODE_ADDON_API_ENABLE_MAYBE' ],
6162
'sources': [
6263
'src/backend/Backend.cc',
6364
'src/backend/ImageBackend.cc',
@@ -142,7 +143,9 @@
142143
'cflags_cc!': ['-fno-exceptions']
143144
}],
144145
['OS=="mac"', {
146+
'cflags+': ['-fvisibility=hidden'],
145147
'xcode_settings': {
148+
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
146149
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
147150
}
148151
}],

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const PDFStream = require('./lib/pdfstream')
1111
const JPEGStream = require('./lib/jpegstream')
1212
const { DOMPoint, DOMMatrix } = require('./lib/DOMMatrix')
1313

14+
bindings.setDOMMatrix(DOMMatrix)
15+
bindings.setParseFont(parseFont)
16+
1417
function createCanvas (width, height, type) {
1518
return new Canvas(width, height, type)
1619
}

lib/context2d.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77
*/
88

99
const bindings = require('./bindings')
10-
const parseFont = require('./parse-font')
11-
const { DOMMatrix } = require('./DOMMatrix')
1210

13-
bindings.CanvasRenderingContext2dInit(DOMMatrix, parseFont)
1411
module.exports = bindings.CanvasRenderingContext2d

lib/pattern.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*/
88

99
const bindings = require('./bindings')
10-
const { DOMMatrix } = require('./DOMMatrix')
1110

12-
bindings.CanvasPatternInit(DOMMatrix)
1311
module.exports = bindings.CanvasPattern
1412

1513
bindings.CanvasPattern.prototype.toString = function () {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"types": "types/index.d.ts",
5252
"dependencies": {
5353
"@mapbox/node-pre-gyp": "^1.0.0",
54-
"nan": "^2.17.0",
54+
"node-addon-api": "^7.0.0",
5555
"simple-get": "^3.0.3"
5656
},
5757
"devDependencies": {
@@ -66,7 +66,7 @@
6666
"typescript": "^4.2.2"
6767
},
6868
"engines": {
69-
"node": ">=6"
69+
"node": ">=10.20.0"
7070
},
7171
"license": "MIT"
7272
}

src/Backends.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
#include "backend/PdfBackend.h"
55
#include "backend/SvgBackend.h"
66

7-
using namespace v8;
7+
using namespace Napi;
88

9-
void Backends::Initialize(Local<Object> target) {
10-
Nan::HandleScope scope;
9+
void
10+
Backends::Initialize(Napi::Env env, Napi::Object exports) {
11+
Napi::Object obj = Napi::Object::New(env);
1112

12-
Local<Object> obj = Nan::New<Object>();
1313
ImageBackend::Initialize(obj);
1414
PdfBackend::Initialize(obj);
1515
SvgBackend::Initialize(obj);
1616

17-
Nan::Set(target, Nan::New<String>("Backends").ToLocalChecked(), obj).Check();
17+
exports.Set("Backends", obj);
1818
}

src/Backends.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#pragma once
22

33
#include "backend/Backend.h"
4-
#include <nan.h>
5-
#include <v8.h>
4+
#include <napi.h>
65

7-
class Backends : public Nan::ObjectWrap {
6+
class Backends : public Napi::ObjectWrap<Backends> {
87
public:
9-
static void Initialize(v8::Local<v8::Object> target);
8+
static void Initialize(Napi::Env env, Napi::Object exports);
109
};

0 commit comments

Comments
 (0)