@webreflection/utils 0.1.0 → 0.1.2

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.
@@ -15,7 +15,7 @@ jobs:
15
15
  node-version: [22]
16
16
 
17
17
  steps:
18
- - uses: actions/checkout@v4
18
+ - uses: actions/checkout@v5
19
19
  - name: Use Node.js ${{ matrix.node-version }}
20
20
  uses: actions/setup-node@v4
21
21
  with:
package/README.md CHANGED
@@ -8,6 +8,9 @@
8
8
  A [collection](./src/) of utility functions.
9
9
 
10
10
  ```js
11
+ // example: a shim based on ArrayBuffer if native is `false`
12
+ import { SharedArrayBuffer, native } from 'https://esm.run/@webreflection/utils/shared-array-buffer';
13
+
11
14
  // example: self bound Promise.withResolvers()
12
15
  import withResolvers from 'https://esm.run/@webreflection/utils/with-resolvers';
13
16
 
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "exports": {
6
+ "./shared-array-buffer": "./src/shared-array-buffer.js",
6
7
  "./with-resolvers": "./src/with-resolvers.js",
7
8
  "./package.json": "./package.json"
8
9
  },
9
10
  "tests": [
11
+ "shared-array-buffer",
10
12
  "with-resolvers"
11
13
  ],
12
14
  "scripts": {
@@ -0,0 +1,28 @@
1
+ //@ts-check
2
+
3
+ let { SharedArrayBuffer: SAB } = globalThis, native = true;
4
+
5
+ try {
6
+ //@ts-ignore due valid options not recognized
7
+ new SAB(4, { maxByteLength: 8 });
8
+ }
9
+ catch (_) {
10
+ native = false;
11
+ SAB = /** @type {SharedArrayBufferConstructor} */(
12
+ /** @type {unknown} */(
13
+ class SharedArrayBuffer extends ArrayBuffer {
14
+ get growable() {
15
+ //@ts-ignore due valid property not recognized
16
+ return super.resizable;
17
+ }
18
+ /** @param {number} newLength */
19
+ grow(newLength) {
20
+ //@ts-ignore due valid method not recognized
21
+ super.resize(newLength);
22
+ }
23
+ }
24
+ )
25
+ );
26
+ }
27
+
28
+ export { SAB as SharedArrayBuffer, native };
@@ -5,8 +5,15 @@
5
5
  * @typedef {{promise: Promise<T>, resolve: (value: T) => void, reject: (reason?: any) => void}} Resolvers
6
6
  */
7
7
 
8
+ // fallback for Android WebView
8
9
  //@ts-ignore
9
- const withResolvers = Promise.withResolvers;
10
+ const withResolvers = Promise.withResolvers || function withResolvers() {
11
+ var a, b, c = new this((resolve, reject) => {
12
+ a = resolve;
13
+ b = reject;
14
+ });
15
+ return {resolve: a, reject: b, promise: c};
16
+ };
10
17
 
11
18
  /**
12
19
  * @template T
@@ -0,0 +1,22 @@
1
+ const { SharedArrayBuffer: SAB } = globalThis;
2
+
3
+ globalThis.SharedArrayBuffer = null;
4
+
5
+ const { SharedArrayBuffer, native } = await import("../src/shared-array-buffer.js");
6
+
7
+ globalThis.SharedArrayBuffer = SAB;
8
+
9
+ console.assert(!native);
10
+
11
+ const gsab = new SharedArrayBuffer(4, { maxByteLength: 8 });
12
+
13
+ console.assert(gsab.byteLength === 4);
14
+ console.assert(gsab.growable);
15
+
16
+ gsab.grow(8);
17
+
18
+ console.assert(gsab.byteLength === 8);
19
+
20
+ const fsab = new SharedArrayBuffer(6);
21
+ console.assert(fsab.byteLength === 6);
22
+ console.assert(!fsab.growable);
@@ -1,4 +1,6 @@
1
- import withResolvers from '../src/with-resolvers.js';
1
+ Promise.withResolvers = undefined;
2
+
3
+ const { default: withResolvers } = await import('../src/with-resolvers.js');
2
4
 
3
5
  const { promise, resolve, reject } = withResolvers();
4
6