nodejs_io_uring
Advanced tools
+3
-2
@@ -5,4 +5,5 @@ const { read } = require('./build/Release/io_uring.node'); | ||
| class FileReader extends Readable { | ||
| constructor({fd, offset, bufferSize}) { | ||
| super(); | ||
| constructor(options = {}) { | ||
| super(options); | ||
| const {fd, offset, bufferSize} = options; | ||
| this.fd = fd; | ||
@@ -9,0 +10,0 @@ this.offset = offset || 0; |
+50
-34
@@ -12,3 +12,2 @@ #include <fcntl.h> | ||
| #define QUEUE_DEPTH 1 | ||
| #define BLOCK_SZ 1024 | ||
@@ -18,4 +17,4 @@ // 管理一个文件读取请求的结构体 | ||
| int fd; | ||
| napi_env env; | ||
| // 回调 | ||
| napi_env env; | ||
| napi_ref func; | ||
@@ -25,3 +24,2 @@ // 字节大小 | ||
| int offset; | ||
| int buf_size; | ||
| int nvecs; | ||
@@ -74,5 +72,3 @@ // 数据 | ||
| napi_value callback; | ||
| napi_get_reference_value(req->env, req->func, &callback); | ||
| napi_status ret = napi_call_function(req->env, global, callback, 2, argv, nullptr); | ||
@@ -91,7 +87,17 @@ napi_delete_reference(req->env, req->func); | ||
| // 向内核提交一个请求 | ||
| int submit_read_request(int op, struct request* req, struct io_uring *ring) { | ||
| void submit_request(int op, struct request* req, struct io_uring *ring) { | ||
| // 获取一个io_uring的请求结构体 | ||
| struct io_uring_sqe *sqe = io_uring_get_sqe(ring); | ||
| // 填充请求 | ||
| io_uring_prep_readv(sqe, req->fd, req->iovecs, req->nvecs, req->offset); | ||
| switch (op) | ||
| { | ||
| case IORING_OP_READV: | ||
| io_uring_prep_readv(sqe, req->fd, req->iovecs, req->nvecs, req->offset); | ||
| break; | ||
| case IORING_OP_WRITEV: | ||
| io_uring_prep_writev(sqe, req->fd, req->iovecs, req->nvecs, req->offset); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| // 保存请求上下文,响应的时候用 | ||
@@ -108,23 +114,18 @@ io_uring_sqe_set_data(sqe, (void *)req); | ||
| io_uring_submit(ring); | ||
| return 0; | ||
| } | ||
| static napi_value read(napi_env env, napi_callback_info info) { | ||
| size_t argc = 5; | ||
| napi_value args[5]; | ||
| napi_get_cb_info(env, info, &argc, args, NULL, NULL); | ||
| int32_t fd; | ||
| napi_get_value_int32(env, args[0], &fd); | ||
| char *bufferData; | ||
| size_t bufferLength; | ||
| napi_get_buffer_info( | ||
| env, | ||
| args[1], | ||
| (void**)(&bufferData), | ||
| &bufferLength); | ||
| int32_t offset; | ||
| napi_get_value_int32(env, args[3], &offset); | ||
| void read_write_wrapper(int op, napi_env env, napi_callback_info info) { | ||
| size_t argc = 4; | ||
| napi_value args[4]; | ||
| napi_get_cb_info(env, info, &argc, args, NULL, NULL); | ||
| int32_t fd; | ||
| napi_get_value_int32(env, args[0], &fd); | ||
| char *bufferData; | ||
| size_t bufferLength; | ||
| napi_get_buffer_info(env, | ||
| args[1], | ||
| (void**)(&bufferData), | ||
| &bufferLength); | ||
| int32_t offset; | ||
| napi_get_value_int32(env, args[3], &offset); | ||
@@ -146,5 +147,12 @@ uv_loop_t* loop; | ||
| req->iovecs[0].iov_base = bufferData; | ||
| submit_request(op, req, &io_uring_data->ring); | ||
| } | ||
| static napi_value read(napi_env env, napi_callback_info info) { | ||
| read_write_wrapper(IORING_OP_READV, env, info); | ||
| return nullptr; | ||
| } | ||
| submit_read_request(IORING_OP_READV, req, &io_uring_data->ring); | ||
| return nullptr; | ||
| static napi_value write(napi_env env, napi_callback_info info) { | ||
| read_write_wrapper(IORING_OP_WRITEV, env, info); | ||
| return nullptr; | ||
| } | ||
@@ -158,3 +166,3 @@ | ||
| // 初始化io_uring | ||
| io_uring_queue_init(1, &io_uring_data->ring, 0); | ||
| io_uring_queue_init(QUEUE_DEPTH, &io_uring_data->ring, 0); | ||
| // 初始化poll handle,保存监听的fd | ||
@@ -165,4 +173,4 @@ uv_poll_init(loop, &io_uring_data->poll_handle, io_uring_data->ring.ring_fd); | ||
| loop->data = (void *)io_uring_data; | ||
| napi_value func; | ||
| napi_create_function(env, | ||
| napi_value readFunc; | ||
| napi_create_function(env, | ||
| NULL, | ||
@@ -172,7 +180,15 @@ NAPI_AUTO_LENGTH, | ||
| NULL, | ||
| &func); | ||
| napi_set_named_property(env, exports, "read", func); | ||
| return exports; | ||
| &readFunc); | ||
| napi_set_named_property(env, exports, "read", readFunc); | ||
| napi_value writeFunc; | ||
| napi_create_function(env, | ||
| NULL, | ||
| NAPI_AUTO_LENGTH, | ||
| write, | ||
| NULL, | ||
| &writeFunc); | ||
| napi_set_named_property(env, exports, "write", writeFunc); | ||
| return exports; | ||
| } | ||
| NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
+1
-1
| { | ||
| "name": "nodejs_io_uring", | ||
| "version": "1.0.5", | ||
| "version": "1.0.6", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "FileReader.js", |
+3
-2
@@ -1,2 +0,3 @@ | ||
| # node-io_uring | ||
| nodejs中的io_uring插件 | ||
| # nodejs_io_uring | ||
| nodejs中的io_uring插件(Linux5.1后的版本支持,可以通过uname -a查看,目前支持读取、写入)<br/> | ||
| npm:npm i nodejs_io_uring |
-7
| const { FileReader } = require('./FileReader'); | ||
| const fs = require('fs'); | ||
| const fd = fs.openSync('./1.txt'); | ||
| const fileRead = new FileReader({fd}); | ||
| fileRead.on('data', (buffer) => { | ||
| console.log(buffer); | ||
| }) |
-7
| const { FileReader } = require('./FileReader'); | ||
| const fs = require('fs'); | ||
| const fd = fs.openSync('./1.txt'); | ||
| const fileRead = new FileReader({fd}); | ||
| fileRead.on('data', (buffer) => { | ||
| console.log(buffer); | ||
| }) |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
164245
0.29%4
33.33%0
-100%11
-15.38%25
-19.35%