fix: multi-repo local dev (file:../ deps, package exports, missing deps)

This commit is contained in:
Raven Scott
2026-05-21 20:14:54 -04:00
parent 76e3272ced
commit b63275c6cc
802 changed files with 94636 additions and 4 deletions
+13
View File
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 4.0)
find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
project(bare_pipe C)
add_bare_module(bare_pipe)
target_sources(
${bare_pipe}
PRIVATE
binding.c
)
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+21
View File
@@ -0,0 +1,21 @@
# bare-pipe
Native I/O pipes for JavaScript.
```
npm i bare-pipe
```
## Usage
```js
const Pipe = require('bare-pipe')
const stdout = new Pipe(1)
stdout.write('Hello world!\n')
```
## License
Apache-2.0
+846
View File
@@ -0,0 +1,846 @@
#include <assert.h>
#include <bare.h>
#include <js.h>
#include <stdlib.h>
#include <string.h>
#include <utf.h>
#include <uv.h>
typedef struct {
uv_pipe_t handle;
struct {
uv_connect_t connect;
uv_write_t write;
uv_shutdown_t shutdown;
} requests;
uv_buf_t read;
js_env_t *env;
js_ref_t *ctx;
js_ref_t *on_connection;
js_ref_t *on_connect;
js_ref_t *on_write;
js_ref_t *on_end;
js_ref_t *on_read;
js_ref_t *on_close;
bool closing;
bool exiting;
js_deferred_teardown_t *teardown;
} bare_pipe_t;
enum {
bare_pipe_readable = 0x1,
bare_pipe_writable = 0x2
};
typedef utf8_t bare_pipe_path_t[4096 + 1 /* NULL */];
static void
bare_pipe__on_connection(uv_stream_t *server, int status) {
int err;
bare_pipe_t *pipe = (bare_pipe_t *) server;
if (pipe->exiting) return;
js_env_t *env = pipe->env;
js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
js_value_t *ctx;
err = js_get_reference_value(env, pipe->ctx, &ctx);
assert(err == 0);
js_value_t *on_connection;
err = js_get_reference_value(env, pipe->on_connection, &on_connection);
assert(err == 0);
js_value_t *argv[1];
if (status < 0) {
js_value_t *code;
err = js_create_string_utf8(env, (utf8_t *) uv_err_name(status), -1, &code);
assert(err == 0);
js_value_t *message;
err = js_create_string_utf8(env, (utf8_t *) uv_strerror(status), -1, &message);
assert(err == 0);
err = js_create_error(env, code, message, &argv[0]);
assert(err == 0);
} else {
err = js_get_null(env, &argv[0]);
assert(err == 0);
}
js_call_function(env, ctx, on_connection, 1, argv, NULL);
err = js_close_handle_scope(env, scope);
assert(err == 0);
}
static void
bare_pipe__on_connect(uv_connect_t *req, int status) {
int err;
bare_pipe_t *pipe = (bare_pipe_t *) req->data;
if (pipe->exiting) return;
js_env_t *env = pipe->env;
js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
js_value_t *ctx;
err = js_get_reference_value(env, pipe->ctx, &ctx);
assert(err == 0);
js_value_t *on_connect;
err = js_get_reference_value(env, pipe->on_connect, &on_connect);
assert(err == 0);
js_value_t *argv[1];
if (status < 0) {
js_value_t *code;
err = js_create_string_utf8(env, (utf8_t *) uv_err_name(status), -1, &code);
assert(err == 0);
js_value_t *message;
err = js_create_string_utf8(env, (utf8_t *) uv_strerror(status), -1, &message);
assert(err == 0);
err = js_create_error(env, code, message, &argv[0]);
assert(err == 0);
} else {
err = js_get_null(env, &argv[0]);
assert(err == 0);
}
js_call_function(env, ctx, on_connect, 1, argv, NULL);
err = js_close_handle_scope(env, scope);
assert(err == 0);
}
static void
bare_pipe__on_write(uv_write_t *req, int status) {
int err;
bare_pipe_t *pipe = (bare_pipe_t *) req->data;
if (pipe->exiting) return;
js_env_t *env = pipe->env;
js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
js_value_t *ctx;
err = js_get_reference_value(env, pipe->ctx, &ctx);
assert(err == 0);
js_value_t *on_write;
err = js_get_reference_value(env, pipe->on_write, &on_write);
assert(err == 0);
js_value_t *argv[1];
if (status < 0) {
js_value_t *code;
err = js_create_string_utf8(env, (utf8_t *) uv_err_name(status), -1, &code);
assert(err == 0);
js_value_t *message;
err = js_create_string_utf8(env, (utf8_t *) uv_strerror(status), -1, &message);
assert(err == 0);
err = js_create_error(env, code, message, &argv[0]);
assert(err == 0);
} else {
err = js_get_null(env, &argv[0]);
assert(err == 0);
}
js_call_function(env, ctx, on_write, 1, argv, NULL);
err = js_close_handle_scope(env, scope);
assert(err == 0);
}
static void
bare_pipe__on_shutdown(uv_shutdown_t *req, int status) {
int err;
bare_pipe_t *pipe = (bare_pipe_t *) req->data;
if (pipe->exiting) return;
js_env_t *env = pipe->env;
js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
js_value_t *ctx;
err = js_get_reference_value(env, pipe->ctx, &ctx);
assert(err == 0);
js_value_t *on_end;
err = js_get_reference_value(env, pipe->on_end, &on_end);
assert(err == 0);
js_value_t *argv[1];
if (status < 0) {
js_value_t *code;
err = js_create_string_utf8(env, (utf8_t *) uv_err_name(status), -1, &code);
assert(err == 0);
js_value_t *message;
err = js_create_string_utf8(env, (utf8_t *) uv_strerror(status), -1, &message);
assert(err == 0);
err = js_create_error(env, code, message, &argv[0]);
assert(err == 0);
} else {
err = js_get_null(env, &argv[0]);
assert(err == 0);
}
js_call_function(env, ctx, on_end, 1, argv, NULL);
err = js_close_handle_scope(env, scope);
assert(err == 0);
}
static void
bare_pipe__on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (nread == UV_EOF) nread = 0;
else if (nread == 0) return;
int err;
bare_pipe_t *pipe = (bare_pipe_t *) stream;
if (pipe->exiting) return;
js_env_t *env = pipe->env;
js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
js_value_t *ctx;
err = js_get_reference_value(env, pipe->ctx, &ctx);
assert(err == 0);
js_value_t *on_read;
err = js_get_reference_value(env, pipe->on_read, &on_read);
assert(err == 0);
js_value_t *argv[2];
if (nread < 0) {
js_value_t *code;
err = js_create_string_utf8(env, (utf8_t *) uv_err_name(nread), -1, &code);
assert(err == 0);
js_value_t *message;
err = js_create_string_utf8(env, (utf8_t *) uv_strerror(nread), -1, &message);
assert(err == 0);
err = js_create_error(env, code, message, &argv[0]);
assert(err == 0);
err = js_create_int32(env, 0, &argv[1]);
assert(err == 0);
} else {
err = js_get_null(env, &argv[0]);
assert(err == 0);
err = js_create_int32(env, nread, &argv[1]);
assert(err == 0);
}
js_call_function(env, ctx, on_read, 2, argv, NULL);
err = js_close_handle_scope(env, scope);
assert(err == 0);
}
static void
bare_pipe__on_close(uv_handle_t *handle) {
int err;
bare_pipe_t *pipe = (bare_pipe_t *) handle;
js_env_t *env = pipe->env;
js_deferred_teardown_t *teardown = pipe->teardown;
js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
js_value_t *ctx;
err = js_get_reference_value(env, pipe->ctx, &ctx);
assert(err == 0);
js_value_t *on_close;
err = js_get_reference_value(env, pipe->on_close, &on_close);
assert(err == 0);
err = js_delete_reference(env, pipe->on_connection);
assert(err == 0);
err = js_delete_reference(env, pipe->on_connect);
assert(err == 0);
err = js_delete_reference(env, pipe->on_write);
assert(err == 0);
err = js_delete_reference(env, pipe->on_end);
assert(err == 0);
err = js_delete_reference(env, pipe->on_read);
assert(err == 0);
err = js_delete_reference(env, pipe->on_close);
assert(err == 0);
err = js_delete_reference(env, pipe->ctx);
assert(err == 0);
if (!pipe->exiting) js_call_function(env, ctx, on_close, 0, NULL, NULL);
err = js_close_handle_scope(env, scope);
assert(err == 0);
err = js_finish_deferred_teardown_callback(teardown);
assert(err == 0);
}
static void
bare_pipe__on_teardown(js_deferred_teardown_t *handle, void *data) {
bare_pipe_t *pipe = (bare_pipe_t *) data;
pipe->exiting = true;
if (pipe->closing) return;
uv_close((uv_handle_t *) &pipe->handle, bare_pipe__on_close);
}
static void
bare_pipe__on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
bare_pipe_t *pipe = (bare_pipe_t *) handle;
*buf = pipe->read;
}
static js_value_t *
bare_pipe_init(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 8;
js_value_t *argv[8];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 8);
uv_loop_t *loop;
err = js_get_env_loop(env, &loop);
assert(err == 0);
js_value_t *handle;
bare_pipe_t *pipe;
err = js_create_arraybuffer(env, sizeof(bare_pipe_t), (void **) &pipe, &handle);
assert(err == 0);
err = uv_pipe_init(loop, &pipe->handle, 0);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
return NULL;
}
pipe->env = env;
pipe->closing = false;
pipe->exiting = false;
err = js_get_typedarray_info(env, argv[0], NULL, (void **) &pipe->read.base, (size_t *) &pipe->read.len, NULL, NULL);
assert(err == 0);
err = js_create_reference(env, argv[1], 1, &pipe->ctx);
assert(err == 0);
err = js_create_reference(env, argv[2], 1, &pipe->on_connection);
assert(err == 0);
err = js_create_reference(env, argv[3], 1, &pipe->on_connect);
assert(err == 0);
err = js_create_reference(env, argv[4], 1, &pipe->on_write);
assert(err == 0);
err = js_create_reference(env, argv[5], 1, &pipe->on_end);
assert(err == 0);
err = js_create_reference(env, argv[6], 1, &pipe->on_read);
assert(err == 0);
err = js_create_reference(env, argv[7], 1, &pipe->on_close);
assert(err == 0);
err = js_add_deferred_teardown_callback(env, bare_pipe__on_teardown, (void *) pipe, &pipe->teardown);
assert(err == 0);
return handle;
}
static js_value_t *
bare_pipe_connect(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 2;
js_value_t *argv[2];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 2);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
bare_pipe_path_t path;
err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_pipe_path_t), NULL);
assert(err == 0);
uv_connect_t *req = &pipe->requests.connect;
req->data = pipe;
err = uv_pipe_connect2(req, &pipe->handle, (char *) path, strlen((const char *) path), UV_PIPE_NO_TRUNCATE, bare_pipe__on_connect);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_open(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 2;
js_value_t *argv[2];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 2);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
uint32_t fd;
err = js_get_value_uint32(env, argv[1], &fd);
assert(err == 0);
err = uv_pipe_open(&pipe->handle, fd);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
return NULL;
}
uint32_t status = 0;
if (uv_is_readable((uv_stream_t *) &pipe->handle)) {
status |= bare_pipe_readable;
}
if (uv_is_writable((uv_stream_t *) &pipe->handle)) {
status |= bare_pipe_writable;
}
js_value_t *result;
err = js_create_uint32(env, status, &result);
assert(err == 0);
return result;
}
static js_value_t *
bare_pipe_bind(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 3;
js_value_t *argv[3];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 3);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
bare_pipe_path_t name;
err = js_get_value_string_utf8(env, argv[1], name, sizeof(bare_pipe_path_t), NULL);
assert(err == 0);
uint32_t backlog;
err = js_get_value_uint32(env, argv[2], &backlog);
assert(err == 0);
err = uv_pipe_bind2(&pipe->handle, (char *) name, strlen((const char *) name), UV_PIPE_NO_TRUNCATE);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
return NULL;
}
err = uv_listen((uv_stream_t *) &pipe->handle, backlog, bare_pipe__on_connection);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_accept(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 2;
js_value_t *argv[2];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 2);
bare_pipe_t *server;
err = js_get_arraybuffer_info(env, argv[0], (void **) &server, NULL);
assert(err == 0);
bare_pipe_t *client;
err = js_get_arraybuffer_info(env, argv[1], (void **) &client, NULL);
assert(err == 0);
err = uv_accept((uv_stream_t *) &server->handle, (uv_stream_t *) &client->handle);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_writev(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 2;
js_value_t *argv[2];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 2);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
js_value_t *arr = argv[1];
uint32_t bufs_len;
err = js_get_array_length(env, arr, &bufs_len);
assert(err == 0);
uv_buf_t *bufs = malloc(sizeof(uv_buf_t) * bufs_len);
js_value_t **elements = malloc(bufs_len * sizeof(js_value_t *));
err = js_get_array_elements(env, arr, elements, bufs_len, 0, NULL);
assert(err == 0);
for (uint32_t i = 0; i < bufs_len; i++) {
js_value_t *item = elements[i];
uv_buf_t *buf = &bufs[i];
err = js_get_typedarray_info(env, item, NULL, (void **) &buf->base, (size_t *) &buf->len, NULL, NULL);
assert(err == 0);
}
uv_write_t *req = &pipe->requests.write;
req->data = pipe;
err = uv_write(req, (uv_stream_t *) &pipe->handle, bufs, bufs_len, bare_pipe__on_write);
free(bufs);
free(elements);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_end(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 1;
js_value_t *argv[1];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 1);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
uv_shutdown_t *req = &pipe->requests.shutdown;
req->data = pipe;
err = uv_shutdown(req, (uv_stream_t *) &pipe->handle, bare_pipe__on_shutdown);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_resume(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 1;
js_value_t *argv[1];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 1);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
if (!uv_is_readable((uv_stream_t *) &pipe->handle)) return NULL;
err = uv_read_start((uv_stream_t *) &pipe->handle, bare_pipe__on_alloc, bare_pipe__on_read);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_pause(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 1;
js_value_t *argv[1];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 1);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
if (!uv_is_readable((uv_stream_t *) &pipe->handle)) return NULL;
err = uv_read_stop((uv_stream_t *) &pipe->handle);
if (err < 0) {
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
assert(err == 0);
}
return NULL;
}
static js_value_t *
bare_pipe_close(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 1;
js_value_t *argv[1];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 1);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
pipe->closing = true;
uv_close((uv_handle_t *) &pipe->handle, bare_pipe__on_close);
return NULL;
}
static js_value_t *
bare_pipe_ref(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 1;
js_value_t *argv[1];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 1);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
uv_ref((uv_handle_t *) &pipe->handle);
return NULL;
}
static js_value_t *
bare_pipe_unref(js_env_t *env, js_callback_info_t *info) {
int err;
size_t argc = 1;
js_value_t *argv[1];
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);
assert(argc == 1);
bare_pipe_t *pipe;
err = js_get_arraybuffer_info(env, argv[0], (void **) &pipe, NULL);
assert(err == 0);
uv_unref((uv_handle_t *) &pipe->handle);
return NULL;
}
static js_value_t *
bare_pipe_pipe(js_env_t *env, js_callback_info_t *info) {
int err;
uv_file fds[2];
err = uv_pipe(fds, UV_NONBLOCK_PIPE, UV_NONBLOCK_PIPE);
assert(err == 0);
js_value_t *result;
err = js_create_array_with_length(env, 2, &result);
assert(err == 0);
js_value_t *read;
err = js_create_int64(env, fds[0], &read);
assert(err == 0);
js_value_t *write;
err = js_create_int64(env, fds[1], &write);
assert(err == 0);
err = js_set_element(env, result, 0, read);
assert(err == 0);
err = js_set_element(env, result, 1, write);
assert(err == 0);
return result;
}
static js_value_t *
bare_pipe_exports(js_env_t *env, js_value_t *exports) {
int err;
#define V(name, fn) \
{ \
js_value_t *val; \
err = js_create_function(env, name, -1, fn, NULL, &val); \
assert(err == 0); \
err = js_set_named_property(env, exports, name, val); \
assert(err == 0); \
}
V("init", bare_pipe_init)
V("connect", bare_pipe_connect)
V("open", bare_pipe_open)
V("bind", bare_pipe_bind)
V("accept", bare_pipe_accept)
V("writev", bare_pipe_writev)
V("end", bare_pipe_end)
V("resume", bare_pipe_resume)
V("pause", bare_pipe_pause)
V("close", bare_pipe_close)
V("ref", bare_pipe_ref)
V("unref", bare_pipe_unref)
V("pipe", bare_pipe_pipe)
#undef V
#define V(name, n) \
{ \
js_value_t *val; \
err = js_create_uint32(env, n, &val); \
assert(err == 0); \
err = js_set_named_property(env, exports, name, val); \
assert(err == 0); \
}
V("READABLE", bare_pipe_readable)
V("WRITABLE", bare_pipe_writable)
#undef V
return exports;
}
BARE_MODULE(bare_pipe, bare_pipe_exports)
+1
View File
@@ -0,0 +1 @@
module.exports = require.addon()
+119
View File
@@ -0,0 +1,119 @@
import EventEmitter, { EventMap } from 'bare-events'
import { Duplex, DuplexEvents } from 'bare-stream'
import PipeError from './lib/errors'
import constants from './lib/constants'
interface PipeEvents extends DuplexEvents {
connect: []
}
interface PipeOptions {
allowHalfOpen?: boolean
eagerOpen?: boolean
readBufferSize?: number
}
interface PipeConnectOptions {
path?: string
}
interface Pipe<M extends PipeEvents = PipeEvents> extends Duplex<M> {
readonly connecting: boolean
readonly pending: boolean
readonly readyState: 'open' | 'readOnly' | 'writeOnly' | 'opening'
open(fd: number, opts?: { fd?: number }, onconnect?: () => void): this
open(fd: number, onconnect: () => void): this
open(opts: { fd: number }, onconnect?: () => void): this
connect(path: string, opts?: PipeConnectOptions, onconnect?: () => void): this
connect(path: string, onconnect: () => void): this
connect(opts: PipeConnectOptions, onconnect?: () => void): this
ref(): this
unref(): this
}
declare class Pipe<M extends PipeEvents = PipeEvents> extends Duplex<M> {
constructor(path: string | number, opts?: PipeOptions)
constructor(opts?: PipeOptions)
}
interface PipeServerEvents extends EventMap {
close: []
connection: [pipe: Pipe]
error: [err: Error]
listening: []
}
interface PipeServerOptions {
allowHalfOpen?: boolean
pauseOnConnect?: boolean
readBufferSize?: number
}
interface PipeServerListenOptions {
path?: string
backlog?: number
}
interface PipeServer<M extends PipeServerEvents = PipeServerEvents> extends EventEmitter<M> {
readonly listening: boolean
address(): string | null
listen(
path: string,
backlog?: number,
opts?: PipeServerListenOptions,
onlistening?: () => void
): this
listen(path: string, backlog: number, onlistening: () => void): this
listen(path: string, onlistening: () => void): this
listen(opts: PipeServerListenOptions): this
close(onclose?: (err?: Error) => void): this
ref(): this
unref(): this
}
declare class PipeServer<M extends PipeServerEvents = PipeServerEvents> extends EventEmitter<M> {
constructor(opts?: PipeServerOptions, onconnection?: () => void)
constructor(onconnection: () => void)
}
declare namespace Pipe {
export interface CreateConnectionOptions extends PipeOptions, PipeConnectOptions {}
export function createConnection(
path: string,
opts?: CreateConnectionOptions,
onconnect?: () => void
): Pipe
export function createConnection(path: string, onconnect: () => void): Pipe
export function createConnection(opts: CreateConnectionOptions, onconnect?: () => void): Pipe
export function createServer(opts?: PipeServerOptions, onconnection?: () => void): PipeServer
export function pipe(): [read: number, write: number]
export {
type PipeEvents,
type PipeOptions,
Pipe,
type PipeConnectOptions,
type PipeServerEvents,
type PipeServerOptions,
type PipeServerListenOptions,
type PipeServer,
PipeServer as Server,
type PipeError,
PipeError as errors,
constants
}
}
export = Pipe
+515
View File
@@ -0,0 +1,515 @@
const EventEmitter = require('bare-events')
const { Duplex } = require('bare-stream')
const binding = require('./binding')
const constants = require('./lib/constants')
const errors = require('./lib/errors')
const defaultReadBufferSize = 65536
const empty = Buffer.alloc(0)
module.exports = exports = class Pipe extends Duplex {
constructor(path, opts = {}) {
if (typeof path === 'object' && path !== null) {
opts = path
path = null
}
const { readBufferSize = defaultReadBufferSize, allowHalfOpen = true, eagerOpen = true } = opts
super({ eagerOpen })
this._state = 0
this._allowHalfOpen = allowHalfOpen
this._fd = -1
this._path = null
this._pendingOpen = null
this._pendingWrite = null
this._pendingFinal = null
this._pendingDestroy = null
this._buffer = Buffer.alloc(readBufferSize)
this._handle = binding.init(
this._buffer,
this,
noop,
this._onconnect,
this._onwrite,
this._onfinal,
this._onread,
this._onclose
)
if (typeof path === 'number') {
this.open(path)
} else if (typeof path === 'string') {
this.connect(path)
}
}
get connecting() {
return (this._state & constants.state.CONNECTING) !== 0
}
get pending() {
return (this._state & constants.state.CONNECTED) === 0
}
get readyState() {
if (this._state & constants.state.READABLE && this._state & constants.state.WRITABLE) {
return 'open'
}
if (this._state & constants.state.READABLE) {
return 'readOnly'
}
if (this._state & constants.state.WRITABLE) {
return 'writeOnly'
}
return 'opening'
}
open(fd, opts = {}, onconnect) {
if (typeof opts === 'function') {
onconnect = opts
opts = {}
}
if (typeof fd === 'object' && fd !== null) {
opts = fd || {}
fd = opts.fd
}
try {
const status = binding.open(this._handle, fd)
this._state |= constants.state.CONNECTED
this._fd = fd
if (status & binding.READABLE) {
this._state |= constants.state.READABLE
} else {
this.push(null)
}
if (status & binding.WRITABLE) {
this._state |= constants.state.WRITABLE
} else {
this.end()
}
if (onconnect) this.once('connect', onconnect)
queueMicrotask(() => this.emit('connect'))
} catch (err) {
queueMicrotask(() => {
if (this._pendingOpen) this._pendingOpen(err)
else this.destroy(err)
})
}
return this
}
connect(path, opts = {}, onconnect) {
if (this._state & constants.state.CONNECTING || this._state & constants.state.CONNECTED) {
throw errors.PIPE_ALREADY_CONNECTED('Pipe is already connected')
}
this._state |= constants.state.CONNECTING
if (typeof opts === 'function') {
onconnect = opts
opts = {}
}
if (typeof path === 'object' && path !== null) {
opts = path || {}
path = opts.path
}
try {
binding.connect(this._handle, path)
this._path = path
if (onconnect) this.once('connect', onconnect)
} catch (err) {
queueMicrotask(() => {
if (this._pendingOpen) this._pendingOpen(err)
else this.destroy(err)
})
}
return this
}
ref() {
binding.ref(this._handle)
return this
}
unref() {
binding.unref(this._handle)
return this
}
_open(cb) {
if (this._state & constants.state.CONNECTED) return cb(null)
this._pendingOpen = cb
}
_read() {
if ((this._state & constants.state.READING) === 0) {
this._state |= constants.state.READING
binding.resume(this._handle)
}
}
_writev(batch, cb) {
this._pendingWrite = [cb, batch]
binding.writev(
this._handle,
batch.map(({ chunk }) => chunk)
)
}
_final(cb) {
if (this._state & constants.state.READABLE && this._state & constants.state.WRITABLE) {
this._pendingFinal = cb
binding.end(this._handle)
} else {
cb(null)
}
}
_predestroy() {
if (this._state & constants.state.CLOSING) return
this._state |= constants.state.CLOSING
binding.close(this._handle)
}
_destroy(err, cb) {
if (this._state & constants.state.CLOSING) return cb(err)
this._state |= constants.state.CLOSING
this._pendingDestroy = cb
binding.close(this._handle)
}
_continueOpen(err) {
if (this._pendingOpen === null) return
const cb = this._pendingOpen
this._pendingOpen = null
cb(err)
}
_continueWrite(err) {
if (this._pendingWrite === null) return
const cb = this._pendingWrite[0]
this._pendingWrite = null
cb(err)
}
_continueFinal(err) {
if (this._pendingFinal === null) return
const cb = this._pendingFinal
this._pendingFinal = null
cb(err)
}
_continueDestroy() {
if (this._pendingDestroy === null) return
const cb = this._pendingDestroy
this._pendingDestroy = null
cb(null)
}
_onconnect(err) {
if (err) {
if (this._pendingOpen) this._continueOpen(err)
else this.destroy(err)
return
}
this._state |= constants.state.CONNECTED | constants.state.READABLE | constants.state.WRITABLE
this._state &= ~constants.state.CONNECTING
this._continueOpen()
this.emit('connect')
}
_onread(err, read) {
if (err) {
this.destroy(err)
return
}
if (read === 0) {
this.push(null)
if (this._allowHalfOpen === false) this.end()
return
}
const copy = Buffer.allocUnsafe(read)
copy.set(this._buffer.subarray(0, read))
if (this.push(copy) === false && this.destroying === false) {
this._state &= ~constants.state.READING
binding.pause(this._handle)
}
}
_onwrite(err) {
this._continueWrite(err)
}
_onfinal(err) {
this._continueFinal(err === null || err.code === 'ENOTCONN' ? null : err)
}
_onclose() {
this._continueDestroy()
}
_onspawn(readable, writable) {
this._state |= constants.state.CONNECTED
if (readable) {
this._state |= constants.state.READABLE
} else {
this.push(null)
}
if (writable) {
this._state |= constants.state.WRITABLE
} else {
this.end()
}
this._continueOpen()
}
}
exports.Pipe = exports
exports.pipe = function pipe() {
return binding.pipe()
}
exports.Server = class PipeServer extends EventEmitter {
constructor(opts = {}, onconnection) {
if (typeof opts === 'function') {
onconnection = opts
opts = {}
}
super()
const {
readBufferSize = defaultReadBufferSize,
allowHalfOpen = true,
pauseOnConnect = false
} = opts
this._state = 0
this._readBufferSize = readBufferSize
this._allowHalfOpen = allowHalfOpen
this._pauseOnConnect = pauseOnConnect
this._path = null
this._connections = new Set()
this._error = null
this._handle = null
if (onconnection) this.on('connection', onconnection)
}
get listening() {
return (this._state & constants.state.BOUND) !== 0
}
address() {
if ((this._state & constants.state.BOUND) === 0) {
return null
}
return this._path
}
listen(path, backlog = 511, opts = {}, onlistening) {
if (this._state & constants.state.BINDING || this._state & constants.state.BOUND) {
throw errors.SERVER_ALREADY_LISTENING('Server is already listening')
}
if (this._state & constants.state.CLOSING) {
throw errors.SERVER_IS_CLOSED('Server is closed')
}
this._state |= constants.state.BINDING
if (typeof backlog === 'function') {
onlistening = backlog
backlog = 511
} else if (typeof opts === 'function') {
onlistening = opts
opts = {}
}
if (typeof path === 'object' && path !== null) {
opts = path || {}
path = opts.path
backlog = opts.backlog || 511
}
this._handle = binding.init(
empty,
this,
this._onconnection,
noop,
noop,
noop,
noop,
this._onclose
)
if (this._state & constants.state.UNREFED) binding.unref(this._handle)
try {
binding.bind(this._handle, path, backlog)
this._path = path
this._state |= constants.state.BOUND
this._state &= ~constants.state.BINDING
if (onlistening) this.once('listening', onlistening)
queueMicrotask(() => this.emit('listening'))
} catch (err) {
this._error = err
binding.close(this._handle)
}
return this
}
close(onclose) {
if (onclose) this.once('close', onclose)
if (this._state & constants.state.CLOSING) return
this._state |= constants.state.CLOSING
this._closeMaybe()
return this
}
ref() {
this._state &= ~constants.state.UNREFED
if (this._handle !== null) binding.ref(this._handle)
return this
}
unref() {
this._state |= constants.state.UNREFED
if (this._handle !== null) binding.unref(this._handle)
return this
}
_closeMaybe() {
if (this._state & constants.state.CLOSING && this._connections.size === 0) {
if (this._handle !== null) binding.close(this._handle)
else queueMicrotask(() => this.emit('close'))
}
}
_onconnection(err) {
if (err) {
this.emit('error', err)
return
}
if (this._state & constants.state.CLOSING) return
const pipe = new exports.Pipe({
readBufferSize: this._readBufferSize,
allowHalfOpen: this._allowHalfOpen,
eagerOpen: !this._pauseOnConnect
})
try {
binding.accept(this._handle, pipe._handle)
pipe._path = this._path
pipe._state |= constants.state.CONNECTED | constants.state.READABLE | constants.state.WRITABLE
this._connections.add(pipe)
pipe.on('close', () => {
this._connections.delete(pipe)
this._closeMaybe()
})
this.emit('connection', pipe)
} catch (err) {
pipe.destroy()
throw err
}
}
_onclose() {
const err = this._error
this._state &= ~constants.state.BINDING
this._error = null
this._handle = null
if (err) this.emit('error', err)
else this.emit('close')
}
}
exports.constants = constants
exports.errors = errors
exports.createConnection = function createConnection(path, opts, onconnect) {
if (typeof opts === 'function') {
onconnect = opts
opts = {}
}
if (typeof path === 'object' && path !== null) {
opts = path || {}
path = opts.path
}
return new exports.Pipe(opts).connect(path, opts, onconnect)
}
exports.createServer = function createServer(opts, onconnection) {
return new exports.Server(opts, onconnection)
}
function noop() {}
+15
View File
@@ -0,0 +1,15 @@
declare const constants: {
state: {
CONNECTING: number
CONNECTED: number
BINDING: number
BOUND: number
READING: number
CLOSING: number
READABLE: number
WRITABLE: number
UNREFED: number
}
}
export = constants
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
state: {
CONNECTING: 0x1,
CONNECTED: 0x2,
BINDING: 0x4,
BOUND: 0x8,
READING: 0x10,
CLOSING: 0x20,
READABLE: 0x40,
WRITABLE: 0x80,
UNREFED: 0x100
}
}
+5
View File
@@ -0,0 +1,5 @@
declare class PipeError extends Error {
readonly code: string
}
export = PipeError
+26
View File
@@ -0,0 +1,26 @@
module.exports = class PipeError extends Error {
constructor(msg, fn = PipeError, code = fn.name) {
super(`${code}: ${msg}`)
this.code = code
if (Error.captureStackTrace) {
Error.captureStackTrace(this, fn)
}
}
get name() {
return 'PipeError'
}
static PIPE_ALREADY_CONNECTED(msg) {
return new PipeError(msg, PipeError.PIPE_ALREADY_CONNECTED)
}
static SERVER_ALREADY_LISTENING(msg) {
return new PipeError(msg, PipeError.SERVER_ALREADY_LISTENING)
}
static SERVER_IS_CLOSED(msg) {
return new PipeError(msg, PipeError.SERVER_IS_CLOSED)
}
}
+57
View File
@@ -0,0 +1,57 @@
{
"name": "bare-pipe",
"version": "4.1.5",
"description": "Native I/O pipes for JavaScript",
"exports": {
"./package": "./package.json",
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./constants": {
"types": "./lib/constants.d.ts",
"default": "./lib/constants.js"
},
"./errors": {
"types": "./lib/errors.d.ts",
"default": "./lib/errors.js"
}
},
"files": [
"index.js",
"index.d.ts",
"binding.c",
"binding.js",
"CMakeLists.txt",
"lib",
"prebuilds"
],
"addon": true,
"scripts": {
"test": "prettier . --check && bare test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/holepunchto/bare-pipe.git"
},
"author": "Holepunch",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/holepunchto/bare-pipe/issues"
},
"homepage": "https://github.com/holepunchto/bare-pipe#readme",
"engines": {
"bare": ">=1.16.0"
},
"dependencies": {
"bare-events": "^2.0.0",
"bare-stream": "^2.0.0"
},
"devDependencies": {
"bare-path": "^3.0.0",
"brittle": "^3.2.1",
"cmake-bare": "^1.1.6",
"prettier": "^3.4.1",
"prettier-config-holepunch": "^2.0.0"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.