+238
-0
@@ -298,3 +298,157 @@ #include <assert.h> | ||
| static int | ||
| bare_os__population_count(uint32_t num) { | ||
| int result = 0; | ||
| while (num != 0) { | ||
| result++; | ||
| num &= num - 1; | ||
| } | ||
| return result; | ||
| } | ||
| static js_value_t * | ||
| bare_os_network_interfaces(js_env_t *env, js_callback_info_t *info) { | ||
| int err; | ||
| uv_interface_address_t *addresses; | ||
| int len; | ||
| err = uv_interface_addresses(&addresses, &len); | ||
| if (err < 0) { | ||
| js_throw_error(env, uv_err_name(err), uv_strerror(err)); | ||
| return NULL; | ||
| } | ||
| js_value_t *result; | ||
| err = js_create_array_with_length(env, len, &result); | ||
| assert(err == 0); | ||
| for (size_t i = 0, n = len; i < n; i++) { | ||
| uv_interface_address_t address = addresses[i]; | ||
| uint32_t family = address.address.address4.sin_family; | ||
| if (family != AF_INET && family != AF_INET6) { | ||
| continue; | ||
| } | ||
| js_value_t *item; | ||
| err = js_create_object(env, &item); | ||
| assert(err == 0); | ||
| js_value_t *val; | ||
| err = js_create_string_utf8(env, (utf8_t *) address.name, -1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "name", val); | ||
| assert(err == 0); | ||
| char address_ip[INET6_ADDRSTRLEN]; | ||
| char netmask_ip[INET6_ADDRSTRLEN]; | ||
| char family_string[5]; | ||
| char cidr[INET6_ADDRSTRLEN + 8]; | ||
| uint32_t cidr_value = 0; | ||
| if (family == AF_INET) { | ||
| err = uv_ip4_name(&address.address.address4, address_ip, sizeof(address_ip)); | ||
| assert(err == 0); | ||
| err = uv_ip4_name(&address.netmask.netmask4, netmask_ip, sizeof(netmask_ip)); | ||
| assert(err == 0); | ||
| strcpy(family_string, "IPv4"); | ||
| cidr_value = bare_os__population_count(address.netmask.netmask4.sin_addr.s_addr); | ||
| } else { | ||
| err = uv_ip6_name(&address.address.address6, address_ip, sizeof(address_ip)); | ||
| assert(err == 0); | ||
| err = uv_ip6_name(&address.netmask.netmask6, netmask_ip, sizeof(netmask_ip)); | ||
| assert(err == 0); | ||
| strcpy(family_string, "IPv6"); | ||
| for (size_t i = 0; i < 16; i++) { | ||
| uint32_t count = bare_os__population_count(address.netmask.netmask6.sin6_addr.s6_addr[i]); | ||
| if (count == 0) break; | ||
| cidr_value += count; | ||
| } | ||
| } | ||
| err = js_create_string_utf8(env, (utf8_t *) address_ip, -1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "address", val); | ||
| assert(err == 0); | ||
| err = js_create_string_utf8(env, (utf8_t *) netmask_ip, -1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "netmask", val); | ||
| assert(err == 0); | ||
| err = js_create_string_utf8(env, (utf8_t *) family_string, -1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "family", val); | ||
| assert(err == 0); | ||
| err = sprintf(cidr, "%s/%d", address_ip, cidr_value); | ||
| assert(err > 0); | ||
| err = js_create_string_utf8(env, (utf8_t *) cidr, -1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "cidr", val); | ||
| assert(err == 0); | ||
| char mac[18]; | ||
| err = snprintf( | ||
| mac, | ||
| sizeof(mac), | ||
| "%02x:%02x:%02x:%02x:%02x:%02x", | ||
| (unsigned char) address.phys_addr[0], | ||
| (unsigned char) address.phys_addr[1], | ||
| (unsigned char) address.phys_addr[2], | ||
| (unsigned char) address.phys_addr[3], | ||
| (unsigned char) address.phys_addr[4], | ||
| (unsigned char) address.phys_addr[5] | ||
| ); | ||
| assert(err > 0); | ||
| err = js_create_string_utf8(env, (utf8_t *) mac, -1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "mac", val); | ||
| assert(err == 0); | ||
| err = js_get_boolean(env, address.is_internal == 1, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "internal", val); | ||
| assert(err == 0); | ||
| if (family == AF_INET6) { | ||
| err = js_create_uint32(env, address.address.address6.sin6_scope_id, &val); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, item, "scopeid", val); | ||
| assert(err == 0); | ||
| } | ||
| err = js_set_element(env, result, i, item); | ||
| assert(err == 0); | ||
| } | ||
| uv_free_interface_addresses(addresses, len); | ||
| return result; | ||
| } | ||
| static js_value_t * | ||
| bare_os_kill(js_env_t *env, js_callback_info_t *info) { | ||
@@ -674,2 +828,60 @@ int err; | ||
| static js_value_t * | ||
| bare_os_get_priority(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); | ||
| uv_pid_t pid; | ||
| err = js_get_value_int32(env, argv[0], &pid); | ||
| assert(err == 0); | ||
| int priority; | ||
| err = uv_os_getpriority(pid, &priority); | ||
| if (err < 0) { | ||
| js_throw_error(env, uv_err_name(err), uv_strerror(err)); | ||
| return NULL; | ||
| } | ||
| js_value_t *result; | ||
| err = js_create_int32(env, priority, &result); | ||
| assert(err == 0); | ||
| return result; | ||
| } | ||
| static js_value_t * | ||
| bare_os_set_priority(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); | ||
| uv_pid_t pid; | ||
| err = js_get_value_int32(env, argv[0], &pid); | ||
| assert(err == 0); | ||
| int priority; | ||
| err = js_get_value_int32(env, argv[1], &priority); | ||
| assert(err == 0); | ||
| err = uv_os_setpriority(pid, priority); | ||
| if (err < 0) { | ||
| js_throw_error(env, uv_err_name(err), uv_strerror(err)); | ||
| } | ||
| return NULL; | ||
| } | ||
| static js_value_t * | ||
| bare_os_get_env_keys(js_env_t *env, js_callback_info_t *info) { | ||
@@ -950,2 +1162,4 @@ int err; | ||
| V("setProcessTitle", bare_os_set_process_title) | ||
| V("getPriority", bare_os_get_priority) | ||
| V("setPriority", bare_os_set_priority) | ||
| V("getEnvKeys", bare_os_get_env_keys) | ||
@@ -957,2 +1171,3 @@ V("getEnv", bare_os_get_env) | ||
| V("userInfo", bare_os_user_info) | ||
| V("networkInterfaces", bare_os_network_interfaces) | ||
| #undef V | ||
@@ -1120,2 +1335,25 @@ | ||
| js_value_t *priority; | ||
| err = js_create_object(env, &priority); | ||
| assert(err == 0); | ||
| err = js_set_named_property(env, exports, "priority", priority); | ||
| assert(err == 0); | ||
| #define V(name) \ | ||
| { \ | ||
| js_value_t *val; \ | ||
| err = js_create_int32(env, UV_##name, &val); \ | ||
| assert(err == 0); \ | ||
| err = js_set_named_property(env, priority, #name, val); \ | ||
| assert(err == 0); \ | ||
| } | ||
| V(PRIORITY_LOW); | ||
| V(PRIORITY_BELOW_NORMAL); | ||
| V(PRIORITY_NORMAL); | ||
| V(PRIORITY_ABOVE_NORMAL); | ||
| V(PRIORITY_HIGH); | ||
| V(PRIORITY_HIGHEST); | ||
| return exports; | ||
@@ -1122,0 +1360,0 @@ } |
+28
-0
| export const constants: { | ||
| signals: Record<string, number> | ||
| errnos: Record<string, number> | ||
| priority: Record<string, number> | ||
| } | ||
@@ -36,4 +37,26 @@ | ||
| export interface NetworkInterface { | ||
| address: string | ||
| netmask: string | ||
| family: 'IPv4' | 'IPv6' | ||
| cidr: string | ||
| mac: string | ||
| internal: boolean | ||
| scopeid?: number | ||
| } | ||
| export function networkInterfaces(): Record<string, NetworkInterface[]> | ||
| export function kill(pid: number, signal?: string | number): void | ||
| export interface UserInfo { | ||
| uid: number | ||
| gid: number | ||
| username: string | ||
| homedir: string | ||
| shell: string | null | ||
| } | ||
| export function userInfo(): UserInfo | ||
| export function endianness(): 'LE' | 'BE' | ||
@@ -102,2 +125,7 @@ | ||
| export function getPriority(pid?: number): number | ||
| export function setPriority(priority: number): void | ||
| export function setPriority(pid: number, priority: number): void | ||
| export function getEnvKeys(): string[] | ||
@@ -104,0 +132,0 @@ |
+26
-0
@@ -31,2 +31,15 @@ const binding = require('./binding') | ||
| exports.networkInterfaces = function networkInterfaces() { | ||
| const interfaces = binding.networkInterfaces() | ||
| return interfaces.reduce((result, entry) => { | ||
| const { name, ...interface } = entry | ||
| if (result[name]) result[name].push(interface) | ||
| else result[name] = [interface] | ||
| return result | ||
| }, {}) | ||
| } | ||
| exports.kill = function kill(pid, signal = constants.signals.SIGTERM) { | ||
@@ -96,2 +109,15 @@ if (typeof signal === 'string') { | ||
| exports.getPriority = function getPriority(pid = 0) { | ||
| return binding.getPriority(pid) | ||
| } | ||
| exports.setPriority = function setPriority(pid, priority) { | ||
| if (priority === undefined) { | ||
| priority = pid | ||
| pid = 0 | ||
| } | ||
| binding.setPriority(pid, priority) | ||
| } | ||
| exports.getEnvKeys = binding.getEnvKeys | ||
@@ -98,0 +124,0 @@ exports.getEnv = binding.getEnv |
+2
-1
@@ -5,3 +5,4 @@ const binding = require('../binding') | ||
| signals: binding.signals, | ||
| errnos: binding.errnos | ||
| errnos: binding.errnos, | ||
| priority: binding.priority | ||
| } |
+2
-2
| { | ||
| "name": "bare-os", | ||
| "version": "3.6.2", | ||
| "version": "3.7.0", | ||
| "description": "Operating system utilities for Javascript", | ||
@@ -44,4 +44,4 @@ "exports": { | ||
| "prettier": "^3.4.2", | ||
| "prettier-config-standard": "^7.0.0" | ||
| "prettier-config-holepunch": "^2.0.0" | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
892200
14.52%217
24%