async-ioctl
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -9,3 +9,8 @@ /// <reference types="node" /> | ||
export declare function ioctl(fd: MaybeBigInt, request: MaybeBigInt, data?: MaybeBufferPointer): Promise<number>; | ||
declare const BaseError: ErrorConstructor; | ||
export declare namespace ioctl { | ||
class Error extends BaseError { | ||
readonly code: number; | ||
constructor(code: number); | ||
} | ||
function batch(...calls: [ | ||
@@ -22,2 +27,3 @@ fd: MaybeBigInt, | ||
} | ||
export {}; | ||
//# sourceMappingURL=ioctl.d.ts.map |
@@ -17,3 +17,12 @@ "use strict"; | ||
exports.ioctl = ioctl; | ||
const BaseError = Error; | ||
(function (ioctl) { | ||
class Error extends BaseError { | ||
code; | ||
constructor(code) { | ||
super(`ioctl error ${code}`); | ||
this.code = code; | ||
} | ||
} | ||
ioctl.Error = Error; | ||
async function batch(...calls) { | ||
@@ -30,5 +39,3 @@ return await new Promise((resolve, reject) => { | ||
if (result == -1) { | ||
const err = new Error(`Error ${error} running ioctl`); | ||
err.code = error; | ||
throw err; | ||
throw new Error(error); | ||
} | ||
@@ -56,6 +63,12 @@ else { | ||
async function blocking(fd, request, data) { | ||
console.log('Calling blocking ioctl', { fd, request, data }); | ||
return await new Promise((resolve, reject) => { | ||
try { | ||
ioctl_node_1.default.blocking(BigInt(fd), BigInt(request), pointer(data), complete(resolve, reject)); | ||
ioctl_node_1.default.blocking(BigInt(fd), BigInt(request), pointer(data), (error, result) => { | ||
if (result == -1) { | ||
reject(new Error(error)); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}); | ||
} | ||
@@ -69,14 +82,2 @@ catch (error) { | ||
})(ioctl = exports.ioctl || (exports.ioctl = {})); | ||
function complete(resolve, reject) { | ||
return (error, result) => { | ||
if (result == -1) { | ||
const err = new Error(`Error ${error} running ioctl`); | ||
err.code = error; | ||
reject(err); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}; | ||
} | ||
function pointer(ptr) { | ||
@@ -83,0 +84,0 @@ if (ptr === undefined || ptr === null) { |
{ | ||
"name": "async-ioctl", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "build/ioctl.js", |
# node-async-ioctl | ||
Asynchronous `ioctl` for Node.js. Used for `node-nbd-client`, UNIX-only. | ||
Asynchronous `ioctl` for Node.js. Used for [`node-nbd-client`](https://github.com/fathyb/node-nbd-client), UNIX-only. | ||
## Usage | ||
Three APIs are provided, all return a `Promise`: | ||
- `ioctl(fd: number | bigint, request: number | bigint, value: number | bigint | Buffer): Promise<number>` | ||
- meant for `ioctl` calls returning immediately. Uses the libuv thread pool. | ||
- `ioctl.batch(batch: [fd: number | bigint, request: number | bigint, value: number | bigint | Buffer][]): Promise<number[]>` | ||
- same as `ioctl(fd, request, value)` except requests can be submitted in batches. | ||
- `ioctl.blocking(fd: number | bigint, request: number | bigint, value: number | bigint | Buffer): Promise<number>` | ||
- meant for `ioctl` calls blocking until something happens. Uses a dedicated thread so more memory and higher latency. | ||
- `ioctl(fd, request, value)`: meant for `ioctl` calls returning immediately. Uses the libuv thread pool. | ||
- `ioctl.batch()`: same as `ioctl(fd, request, value)` except requests can be submitted in batches, lowering latency between calls. | ||
- `ioctl.blocking()`: meant for `ioctl` calls blocking until something happens. Uses a dedicated thread so more memory and higher latency. | ||
```js | ||
@@ -19,4 +20,4 @@ import { open } from 'fs' | ||
// No argument | ||
const result = await ioctl(device.fd, 0x2000ab00) | ||
// Get number of rows for current terminal | ||
const rows = await ioctl(process.stdout.fd, 0x2000ab00) | ||
// Buffer argument | ||
@@ -23,0 +24,0 @@ const result = await ioctl(device.fd, 0x2000ab00, Buffer.from('test')) |
@@ -20,3 +20,11 @@ import native from '../native/build/Release/ioctl.node' | ||
const BaseError = Error | ||
export namespace ioctl { | ||
export class Error extends BaseError { | ||
constructor(public readonly code: number) { | ||
super(`ioctl error ${code}`) | ||
} | ||
} | ||
export async function batch( | ||
@@ -42,9 +50,3 @@ ...calls: [ | ||
if (result == -1) { | ||
const err: any = new Error( | ||
`Error ${error} running ioctl`, | ||
) | ||
err.code = error | ||
throw err | ||
throw new Error(error) | ||
} else { | ||
@@ -75,4 +77,2 @@ return result | ||
) { | ||
console.log('Calling blocking ioctl', { fd, request, data }) | ||
return await new Promise<number>((resolve, reject) => { | ||
@@ -84,3 +84,9 @@ try { | ||
pointer(data), | ||
complete(resolve, reject), | ||
(error: number, result: number) => { | ||
if (result == -1) { | ||
reject(new Error(error)) | ||
} else { | ||
resolve(result) | ||
} | ||
}, | ||
) | ||
@@ -94,19 +100,2 @@ } catch (error) { | ||
function complete( | ||
resolve: (result: number) => void, | ||
reject: (error: Error) => void, | ||
) { | ||
return (error: number, result: number) => { | ||
if (result == -1) { | ||
const err: any = new Error(`Error ${error} running ioctl`) | ||
err.code = error | ||
reject(err) | ||
} else { | ||
resolve(result) | ||
} | ||
} | ||
} | ||
function pointer(ptr: undefined | MaybeBufferPointer) { | ||
@@ -113,0 +102,0 @@ if (ptr === undefined || ptr === null) { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37929
27