Skip to content

Commit

Permalink
Use native teardown API
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Nov 26, 2024
1 parent c2423df commit a8a330b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
build
prebuilds
sandbox
package-lock.json
52 changes: 27 additions & 25 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
typedef struct {
SSL_CTX *ssl;
BIO_METHOD *io;

js_env_t *env;
js_ref_t *ctx;
} bare_tls_context_t;

typedef struct {
Expand Down Expand Up @@ -128,8 +131,24 @@ bare_tls__on_ctrl(BIO *io, int cmd, long argc, void *argv) {
}
}

static void
bare_tls__on_teardown(void *data) {
int err;

bare_tls_context_t *context = (bare_tls_context_t *) data;

js_env_t *env = context->env;

SSL_CTX_free(context->ssl);

BIO_meth_free(context->io);

err = js_delete_reference(env, context->ctx);
assert(err == 0);
}

static js_value_t *
bare_tls_init_context(js_env_t *env, js_callback_info_t *info) {
bare_tls_context(js_env_t *env, js_callback_info_t *info) {
int err;

js_value_t *handle;
Expand Down Expand Up @@ -160,33 +179,18 @@ bare_tls_init_context(js_env_t *env, js_callback_info_t *info) {
err = SSL_CTX_set_min_proto_version(ssl, TLS1_3_VERSION);
assert(err == 1);

return handle;

err:
js_throw_error(env, ERR_reason_symbol_name(ERR_peek_last_error()), "Context initialisation failed");
return NULL;
}

static js_value_t *
bare_tls_destroy_context(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
js_value_t *argv[1];
context->env = env;

err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
err = js_add_teardown_callback(env, bare_tls__on_teardown, (void *) context);
assert(err == 0);

assert(argc == 1);

bare_tls_context_t *context;
err = js_get_arraybuffer_info(env, argv[0], (void **) &context, NULL);
err = js_create_reference(env, handle, 1, &context->ctx);
assert(err == 0);

SSL_CTX_free(context->ssl);

BIO_meth_free(context->io);
return handle;

err:
js_throw_error(env, ERR_reason_symbol_name(ERR_peek_last_error()), "Context initialisation failed");
return NULL;
}

Expand Down Expand Up @@ -560,9 +564,7 @@ bare_tls_exports(js_env_t *env, js_value_t *exports) {
assert(err == 0); \
}

V("initContext", bare_tls_init_context);
V("destroyContext", bare_tls_destroy_context);

V("context", bare_tls_context);
V("init", bare_tls_init);
V("destroy", bare_tls_destroy);
V("handshake", bare_tls_handshake);
Expand Down
16 changes: 1 addition & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const errors = require('./lib/errors')

const readBufferSize = 65536

const context = binding.initContext()
const context = binding.context()

exports.Socket = class TLSSocket extends Duplex {
static _buffer = Buffer.alloc(readBufferSize)
Expand Down Expand Up @@ -45,8 +45,6 @@ exports.Socket = class TLSSocket extends Duplex {
this._onread,
this._onwrite
)

TLSSocket._sockets.add(this)
}

get socket() {
Expand Down Expand Up @@ -196,30 +194,18 @@ exports.Socket = class TLSSocket extends Duplex {
_predestroy() {
binding.destroy(this._handle)
this._handle = null
TLSSocket._sockets.delete(this)
}

_destroy(err, cb) {
if (this._handle) {
binding.destroy(this._handle)
this._handle = null
TLSSocket._sockets.delete(this)
}
cb(err)
}

static _sockets = new Set()
}

exports.TLSSocket = exports.Socket // For Node.js compatibility

exports.constants = constants
exports.errors = errors

Bare.on('exit', () => {
for (const socket of exports.Socket._sockets) {
socket.destroy()
}

binding.destroyContext(context)
})
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bare-tls",
"version": "1.2.1",
"version": "2.0.0-0",
"description": "Transport Layer Security (TLS) streams for JavaScript",
"exports": {
".": "./index.js",
Expand Down Expand Up @@ -30,6 +30,9 @@
"url": "https://github.com/holepunchto/bare-tls/issues"
},
"homepage": "https://github.com/holepunchto/bare-tls#readme",
"engines": {
"bare": ">=1.7.0"
},
"dependencies": {
"bare-stream": "^2.0.0"
},
Expand Down

0 comments on commit a8a330b

Please sign in to comment.