weida 0.0.0-stage → 0.1.0-alpha.14

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.
package/lib/native.js ADDED
@@ -0,0 +1,18 @@
1
+ // The Node-API addon (src/addon.c) of this platform, from
2
+ // prebuilds/<platform>-<arch>/ (build.sh writes it there), loaded the one
3
+ // way Node, Bun and Deno all accept from an ES module.
4
+ import { existsSync } from "node:fs";
5
+ import { createRequire } from "node:module";
6
+ import process from "node:process";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ const target = `${process.platform}-${process.arch}`;
10
+ const path = fileURLToPath(new URL(`../prebuilds/${target}/weida.node`, import.meta.url));
11
+ if (!existsSync(path)) {
12
+ throw new Error(
13
+ `weida: no addon for ${target}: ${path} does not exist; the package carries linux-x64, linux-arm64, darwin-arm64 and win32-x64, and build.sh (build.ps1 on Windows) builds one from source`,
14
+ );
15
+ }
16
+ const native = createRequire(import.meta.url)(path);
17
+
18
+ export default native;
package/lib/wait.js ADDED
@@ -0,0 +1,82 @@
1
+ // Waiting on libweida from the JS thread (docs/c/README.md R3, R4, R11):
2
+ // every native call tries with timeout 0; on AGAIN the handle's descriptor
3
+ // is watched once through the addon's waiter thread and the call is tried
4
+ // again when it turns readable. R11 allows one stale AGAIN, which simply
5
+ // watches again.
6
+ import native from "./native.js";
7
+ import { closed } from "./errors.js";
8
+
9
+ /** The pending watches of a handle object: a Set of entries. */
10
+ export const WAITS = Symbol("weida.waits");
11
+
12
+ function watch(owner, fd, signal, weak) {
13
+ return new Promise((resolve, reject) => {
14
+ const entry = {};
15
+ const settle = () => {
16
+ owner[WAITS].delete(entry);
17
+ signal?.removeEventListener("abort", entry.abort);
18
+ };
19
+ entry.cancel = (reason) => {
20
+ native.unwatch(entry.id);
21
+ settle();
22
+ reject(reason);
23
+ };
24
+ entry.abort = () => entry.cancel(signal.reason);
25
+ entry.id = native.watch(
26
+ fd,
27
+ () => {
28
+ settle();
29
+ resolve();
30
+ },
31
+ weak === true,
32
+ );
33
+ owner[WAITS].add(entry);
34
+ signal?.addEventListener("abort", entry.abort, { once: true });
35
+ });
36
+ }
37
+
38
+ /**
39
+ * Runs `attempt()` until it returns something other than AGAIN, watching
40
+ * `fdOf()` in between. `owner` is the object whose `drop()` ends the wait;
41
+ * aborting `signal` ends it with `signal.reason`. A `weak` wait does not keep
42
+ * the process alive.
43
+ */
44
+ export async function call(owner, attempt, fdOf, signal, weak = false) {
45
+ signal?.throwIfAborted();
46
+ let rc = attempt();
47
+ while (rc === native.AGAIN) {
48
+ await watch(owner, fdOf(), signal, weak);
49
+ rc = attempt();
50
+ }
51
+ return rc;
52
+ }
53
+
54
+ /**
55
+ * Awaits the op `start()` returns (R3b) and drops it on every path, which
56
+ * cancels the operation when it did not finish (an abort, or `owner`
57
+ * dropped).
58
+ */
59
+ export async function op(owner, start, waitOf, fdOf, signal) {
60
+ signal?.throwIfAborted();
61
+ const handle = start();
62
+ try {
63
+ return await call(owner, () => waitOf(handle), () => fdOf(handle), signal);
64
+ } finally {
65
+ native.drop(handle);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Ends every pending wait of `owner` with WEIDA_ECLOSED, before its handle
71
+ * is dropped or consumed, so no descriptor is watched once it is closed.
72
+ */
73
+ export function cancelWaits(owner) {
74
+ const pending = owner[WAITS];
75
+ if (pending.size === 0) {
76
+ return;
77
+ }
78
+ const error = closed();
79
+ for (const entry of [...pending]) {
80
+ entry.cancel(error);
81
+ }
82
+ }
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "weida",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-alpha.14",
4
+ "description": "weida for JavaScript: a Node-API addon over libweida for Node, Bun and Deno",
5
+ "license": "MIT OR Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/tuco86/weida.git",
9
+ "directory": "crates/js/weida-js"
10
+ },
11
+ "homepage": "https://weida.doodleshnookie.net",
12
+ "keywords": [
13
+ "messaging",
14
+ "quic",
15
+ "broker",
16
+ "async",
17
+ "protocol"
18
+ ],
19
+ "type": "module",
20
+ "exports": "./lib/index.js",
21
+ "types": "./lib/index.d.ts",
22
+ "files": [
23
+ "lib",
24
+ "prebuilds"
25
+ ],
26
+ "publishConfig": {
27
+ "registry": "https://registry.npmjs.org/"
28
+ },
29
+ "engines": {
30
+ "node": ">=20"
31
+ }
32
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.