@bytecodealliance/preview2-shim 0.14.2 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +0,0 @@
1
- export function cappedUint32(value) {
2
- // Note: cap the value to the highest possible BigInt value that can be represented as a
3
- // unsigned 32-bit integer.
4
- const width = 32n;
5
- return BigInt.asUintN(Number(width), value);
6
- }
7
-
8
- export function noop() {}
9
-
10
- function tupleToIPv6(arr) {
11
- if (arr.length !== 8) {
12
- return null;
13
- }
14
- return arr.map((segment) => segment.toString(16)).join(":");
15
- }
16
-
17
- function tupleToIpv4(arr) {
18
- if (arr.length !== 4) {
19
- return null;
20
- }
21
- return arr.map((segment) => segment.toString(10)).join(".");
22
- }
23
-
24
- // TODO: write a better (faste?) parser for ipv6
25
- function ipv6ToTuple(ipv6) {
26
- if (ipv6 === "::1") {
27
- return [0, 0, 0, 0, 0, 0, 0, 1];
28
- } else if (ipv6 === "::") {
29
- return [0, 0, 0, 0, 0, 0, 0, 0];
30
- } else if (ipv6.includes("::")) {
31
- const [head, tail] = ipv6.split("::");
32
- const headSegments = head.split(":").map((segment) => parseInt(segment, 16));
33
- const tailSegments = tail.split(":").map((segment) => parseInt(segment, 16));
34
- const missingSegments = 8 - headSegments.length - tailSegments.length;
35
- const middleSegments = Array(missingSegments).fill(0);
36
- return headSegments.concat(middleSegments).concat(tailSegments);
37
- }
38
- return ipv6.split(":").map((segment) => parseInt(segment, 16));
39
- }
40
-
41
- function ipv4ToTuple(ipv4) {
42
- return ipv4.split(".").map((segment) => parseInt(segment, 10));
43
- }
44
-
45
- export function serializeIpAddress(addr = undefined, includePort = false) {
46
- if (addr === undefined) {
47
- return undefined;
48
- }
49
-
50
- const family = addr.tag;
51
-
52
- let { address } = addr.val;
53
- if (family.toLocaleLowerCase() === "ipv4") {
54
- address = tupleToIpv4(address);
55
- } else if (family.toLocaleLowerCase() === "ipv6") {
56
- address = tupleToIPv6(address);
57
- }
58
-
59
- if (includePort) {
60
- address = `${address}:${addr.val.port}`;
61
- }
62
-
63
- return address;
64
- }
65
-
66
- export function deserializeIpAddress(addr, family) {
67
- let address = [];
68
- if (family.toLocaleLowerCase() === "ipv4") {
69
- address = ipv4ToTuple(addr);
70
- } else if (family.toLocaleLowerCase() === "ipv6") {
71
- address = ipv6ToTuple(addr);
72
- }
73
- return address;
74
- }
75
-
76
- export function findUnusedLocalAddress(
77
- family,
78
- { iPv4MappedAddress = false } = {}
79
- ) {
80
- let address = [127, 0, 0, 1];
81
- if (family.toLocaleLowerCase() === "ipv6") {
82
- if (iPv4MappedAddress) {
83
- address = [0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x0001];
84
- } else {
85
- address = [0, 0, 0, 0, 0, 0, 0, 1];
86
- }
87
- }
88
- return {
89
- tag: family,
90
- val: {
91
- address,
92
- port: 0,
93
- },
94
- };
95
- }
96
-
97
- export function isUnicastIpAddress(ipSocketAddress) {
98
- return !isMulticastIpAddress(ipSocketAddress) && !isBroadcastIpAddress(ipSocketAddress);
99
- }
100
-
101
- export function isMulticastIpAddress(ipSocketAddress) {
102
- // ipv6: [0xff00, 0, 0, 0, 0, 0, 0, 0]
103
- // ipv4: [224, 0, 0, 0]
104
- return ipSocketAddress.val.address[0] === 224 || ipSocketAddress.val.address[0] === 0xff00;
105
- }
106
-
107
- export function isBroadcastIpAddress(ipSocketAddress) {
108
- // ipv4: [255, 255, 255, 255]
109
- return (
110
- ipSocketAddress.val.address[0] === 0xff && // 255
111
- ipSocketAddress.val.address[1] === 0xff && // 255
112
- ipSocketAddress.val.address[2] === 0xff && // 255
113
- ipSocketAddress.val.address[3] === 0xff // 255
114
- );
115
- }
116
-
117
- export function isIPv4MappedAddress(ipSocketAddress) {
118
- // ipv6: [0, 0, 0, 0, 0, 0xffff, 0, 0]
119
- if (ipSocketAddress.val.address.length !== 8) {
120
- return false;
121
- }
122
- return ipSocketAddress.val.address[5] === 0xffff;
123
- }
124
-
125
- export function isWildcardAddress(ipSocketAddress) {
126
- // ipv6: [0, 0, 0, 0, 0, 0, 0, 0]
127
- // ipv4: [0, 0, 0, 0]
128
- return ipSocketAddress.val.address.every((segment) => segment === 0);
129
- }
@@ -1,94 +0,0 @@
1
- import { platform } from "node:os";
2
- import { _errnoException } from "node:util";
3
- import { types, refType } from "ref-napi";
4
- import { Library, errno as _errno } from "ffi-napi";
5
-
6
- const tryGetUV = (() => {
7
- let UV = null;
8
- return () => {
9
- if (UV === null) {
10
- try {
11
- UV = typeof process.binding === "function" ? process.binding("uv") : undefined;
12
- } catch (ex) {
13
- // Continue regardless
14
- }
15
- }
16
- return UV;
17
- };
18
- })();
19
-
20
- const uvErrName = (errno) => {
21
- const UV = tryGetUV();
22
- return UV && UV.errname ? UV.errname(errno) : "UNKNOWN";
23
- };
24
-
25
- const errnoException = (errno, syscall, original) => {
26
- if (_errnoException) {
27
- return _errnoException(-errno, syscall, original);
28
- }
29
-
30
- const errname = uvErrName(-errno),
31
- message = original ? `${syscall} ${errname} (${errno}) ${original}` : `${syscall} ${errname} (${errno})`;
32
-
33
- const e = new Error(message);
34
- e.code = errname;
35
- e.errno = errname;
36
- e.syscall = syscall;
37
- return e;
38
- };
39
-
40
- const createFFI = () => {
41
- const cInt = types.int;
42
- const cVoid = types.void;
43
-
44
- return Library(null, {
45
- //name ret 1 2 3 4 5
46
- setsockopt: [cInt, [cInt, cInt, cInt, refType(cVoid), cInt]],
47
- getsockopt: [cInt, [cInt, cInt, cInt, refType(cVoid), refType(cInt)]],
48
- });
49
- };
50
-
51
- const ffi = (() => {
52
- let instance;
53
- return () => {
54
- if (!instance) {
55
- instance = createFFI();
56
- }
57
- return instance;
58
- };
59
- })();
60
-
61
- const _setsockopt = (fd, level, name, value, valueLength) => {
62
- if (fd == null) {
63
- return false;
64
- }
65
-
66
- const err = ffi().setsockopt(fd, level, name, value, valueLength);
67
-
68
- if (err !== 0) {
69
- const errno = _errno();
70
- throw errnoException(errno, "setsockopt");
71
- }
72
-
73
- return true;
74
- };
75
-
76
- const _getsockopt = (fd, level, name, value, valueLength) => {
77
- if (fd == null) {
78
- return false;
79
- }
80
-
81
- const err = ffi().getsockopt(fd, level, name, value, valueLength);
82
-
83
- if (err !== 0) {
84
- const errno = _errno();
85
- throw errnoException(errno, "getsockopt");
86
- }
87
- return true;
88
- };
89
-
90
- const noop = () => false;
91
- const isWin32 = platform() === "win32";
92
-
93
- export const setsockopt = isWin32 ? noop : _setsockopt;
94
- export const getsockopt = isWin32 ? noop : _getsockopt;