UNPKG

unix-dgram-socket

Version:

Unix datagram socket with abstract namespace support for local interprocess communication.

94 lines 3.53 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.UnixDgramSocket = void 0; var fs = require("fs"); var events_1 = require("events"); var SocketException_1 = require("./SocketException"); var lib = require('../build/Release/unix_dgram'); var UnixDgramSocket = (function (_super) { __extends(UnixDgramSocket, _super); function UnixDgramSocket() { var _this = _super.call(this) || this; _this.connected = false; _this.fd = lib.socket(lib.AF_UNIX, lib.SOCK_DGRAM, 0, function (size, data, path) { _this.emit('message', data, { dataSize: size, remoteSocket: path }); }, function () { _this.emit('writable'); }); if (_this.fd < 0) { throw new SocketException_1.SocketException(_this.fd, 'socket'); } return _this; } UnixDgramSocket.prototype.bind = function (socketPath) { var result = lib.bind(this.fd, this.cleanupSocketFile(socketPath)); if (result < 0) { this.emit('error', new SocketException_1.SocketException(result, 'bind')); } else { this.emit('listening', socketPath); } }; UnixDgramSocket.prototype.send = function (data, socketPath) { var result; if (typeof data === 'string') { data = Buffer.from(data, UnixDgramSocket.payloadEncoding); } if (socketPath) { result = lib.sendto(this.fd, data, 0, data.length, socketPath); } else { result = lib.send(this.fd, data); } if (result < 0) { this.emit('error', new SocketException_1.SocketException(result, 'send')); } else if (result === 1) { this.emit('congestion', data); } return (result >= 0); }; UnixDgramSocket.prototype.close = function () { var result = lib.close(this.fd); if (result < 0) { this.emit('error', new SocketException_1.SocketException(result, 'close')); } this.fd = -1; }; UnixDgramSocket.prototype.connect = function (socketPath) { var result = lib.connect(this.fd, socketPath); if (result < 0) { this.emit('error', new SocketException_1.SocketException(result, 'connect')); } else { this.connected = true; this.emit('connect', socketPath); } }; UnixDgramSocket.prototype.cleanupSocketFile = function (socketPath) { if (fs.existsSync(socketPath)) { var stat = fs.statSync(socketPath); if (stat.isSocket()) { fs.unlinkSync(fs.realpathSync(socketPath)); } } return socketPath; }; UnixDgramSocket.payloadEncoding = 'utf8'; return UnixDgramSocket; }(events_1.EventEmitter)); exports.UnixDgramSocket = UnixDgramSocket; //# sourceMappingURL=UnixDgramSocket.js.map