@webreflection/utils 0.1.1 → 0.2.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.
- package/LICENSE +21 -0
- package/package.json +24 -4
- package/src/bound.js +25 -0
- package/src/with-resolvers.js +18 -6
- package/types/bound.d.ts +4 -0
- package/types/shared-array-buffer.d.ts +3 -0
- package/types/with-resolvers.d.ts +7 -0
- package/.github/dependabot.yml +0 -13
- package/.github/workflows/node.js.yml +0 -31
- package/test/shared-array-buffer.js +0 -22
- package/test/with-resolvers.js +0 -11
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2025-today, Andrea Giammarchi, @WebReflection
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software
|
|
10
|
+
is furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included
|
|
13
|
+
in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,20 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webreflection/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
|
-
"./
|
|
7
|
-
|
|
6
|
+
"./bound": {
|
|
7
|
+
"types": "./types/bound.d.ts",
|
|
8
|
+
"import": "./src/bound.js"
|
|
9
|
+
},
|
|
10
|
+
"./shared-array-buffer": {
|
|
11
|
+
"types": "./types/shared-array-buffer.d.ts",
|
|
12
|
+
"import": "./src/shared-array-buffer.js"
|
|
13
|
+
},
|
|
14
|
+
"./with-resolvers": {
|
|
15
|
+
"types": "./types/with-resolvers.d.ts",
|
|
16
|
+
"import": "./src/with-resolvers.js"
|
|
17
|
+
},
|
|
8
18
|
"./package.json": "./package.json"
|
|
9
19
|
},
|
|
10
20
|
"tests": [
|
|
21
|
+
"bound",
|
|
11
22
|
"shared-array-buffer",
|
|
12
23
|
"with-resolvers"
|
|
13
24
|
],
|
|
14
25
|
"scripts": {
|
|
26
|
+
"build": "npm run build:types && npm run test",
|
|
27
|
+
"build:types": "tsc --allowJs --checkJs --declaration --emitDeclarationOnly --outDir types --target es2022 --lib es2024 --module nodenext --moduleResolution nodenext src/*.js",
|
|
15
28
|
"coverage": "mkdir -p ./coverage; c8 report --reporter=text-lcov > ./coverage/lcov.info",
|
|
16
29
|
"test": "c8 node -e 'let q=Promise.resolve();for(const t of require(`./package.json`).tests)q=q.then(()=>import(`./test/${t}.js`));'"
|
|
17
30
|
},
|
|
31
|
+
"files": [
|
|
32
|
+
"src",
|
|
33
|
+
"types",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
18
37
|
"keywords": [
|
|
19
38
|
"shared",
|
|
20
39
|
"utility",
|
|
@@ -32,6 +51,7 @@
|
|
|
32
51
|
},
|
|
33
52
|
"homepage": "https://github.com/WebReflection/utils#readme",
|
|
34
53
|
"devDependencies": {
|
|
35
|
-
"c8": "^
|
|
54
|
+
"c8": "^11.0.0",
|
|
55
|
+
"typescript": "^6.0.3"
|
|
36
56
|
}
|
|
37
57
|
}
|
package/src/bound.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @template F
|
|
5
|
+
* @typedef {unknown extends ThisParameterType<F> ? F : F extends (this: any, ...args: infer A) => infer R ? (...args: A) => R : never} BoundMethod
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @template T
|
|
10
|
+
* @typedef {{[K in keyof T as T[K] extends Function ? K : never]: BoundMethod<T[K]>}} Bound
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @type {ProxyHandler<any>}
|
|
15
|
+
*/
|
|
16
|
+
const handler = {
|
|
17
|
+
get: (target, prop) => target[prop].bind(target),
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @template {object} T
|
|
22
|
+
* @param {T} target
|
|
23
|
+
* @returns {Bound<T>}
|
|
24
|
+
*/
|
|
25
|
+
export default target => new Proxy(target, handler);
|
package/src/with-resolvers.js
CHANGED
|
@@ -2,14 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @template T
|
|
5
|
-
* @typedef {{promise: Promise<T>, resolve: (value: T) => void, reject: (reason?: any) => void}} Resolvers
|
|
5
|
+
* @typedef {{promise: Promise<T>, resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void}} Resolvers
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
const withResolvers = Promise.withResolvers;
|
|
10
|
-
|
|
8
|
+
// fallback for Android WebView
|
|
11
9
|
/**
|
|
12
10
|
* @template T
|
|
13
|
-
* @
|
|
11
|
+
* @this {PromiseConstructor}
|
|
12
|
+
* @returns {Resolvers<T>}
|
|
14
13
|
*/
|
|
15
|
-
|
|
14
|
+
function withResolvers() {
|
|
15
|
+
var a, b, c = new this((resolve, reject) => {
|
|
16
|
+
a = resolve;
|
|
17
|
+
b = reject;
|
|
18
|
+
});
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
return { resolve: a, reject: b, promise: c };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export default /** @type {<T>() => Resolvers<T>} */((
|
|
25
|
+
/** @type {PromiseConstructor & {withResolvers?: <T>() => Resolvers<T>}} */ (Promise).withResolvers ||
|
|
26
|
+
withResolvers
|
|
27
|
+
).bind(Promise));
|
package/types/bound.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
declare function _default<T extends object>(target: T): Bound<T>;
|
|
2
|
+
export default _default;
|
|
3
|
+
export type BoundMethod<F> = unknown extends ThisParameterType<F> ? F : F extends (this: any, ...args: infer A) => infer R ? (...args: A) => R : never;
|
|
4
|
+
export type Bound<T_1> = { [K in keyof T_1 as T_1[K] extends Function ? K : never]: BoundMethod<T_1[K]>; };
|
package/.github/dependabot.yml
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# Keep GitHub Actions up to date with GitHub's Dependabot...
|
|
2
|
-
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
|
|
3
|
-
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem
|
|
4
|
-
version: 2
|
|
5
|
-
updates:
|
|
6
|
-
- package-ecosystem: github-actions
|
|
7
|
-
directory: /
|
|
8
|
-
groups:
|
|
9
|
-
github-actions:
|
|
10
|
-
patterns:
|
|
11
|
-
- "*" # Group all Actions updates into a single larger pull request
|
|
12
|
-
schedule:
|
|
13
|
-
interval: weekly
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
|
3
|
-
|
|
4
|
-
name: build
|
|
5
|
-
|
|
6
|
-
on: [push, pull_request]
|
|
7
|
-
|
|
8
|
-
jobs:
|
|
9
|
-
build:
|
|
10
|
-
|
|
11
|
-
runs-on: ubuntu-latest
|
|
12
|
-
|
|
13
|
-
strategy:
|
|
14
|
-
matrix:
|
|
15
|
-
node-version: [22]
|
|
16
|
-
|
|
17
|
-
steps:
|
|
18
|
-
- uses: actions/checkout@v4
|
|
19
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
20
|
-
uses: actions/setup-node@v4
|
|
21
|
-
with:
|
|
22
|
-
node-version: ${{ matrix.node-version }}
|
|
23
|
-
cache: 'npm'
|
|
24
|
-
- run: npm ci
|
|
25
|
-
- run: npm run build --if-present
|
|
26
|
-
- run: npm run test
|
|
27
|
-
- run: npm run coverage --if-present
|
|
28
|
-
- name: Coveralls
|
|
29
|
-
uses: coverallsapp/github-action@master
|
|
30
|
-
with:
|
|
31
|
-
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -1,22 +0,0 @@
|
|
|
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);
|
package/test/with-resolvers.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import withResolvers from '../src/with-resolvers.js';
|
|
2
|
-
|
|
3
|
-
const { promise, resolve, reject } = withResolvers();
|
|
4
|
-
|
|
5
|
-
console.assert(typeof reject === 'function');
|
|
6
|
-
console.assert(typeof resolve === 'function');
|
|
7
|
-
console.assert(promise instanceof Promise);
|
|
8
|
-
|
|
9
|
-
resolve('OK');
|
|
10
|
-
|
|
11
|
-
console.log(await promise);
|