ctx-core 5.2.2 → 5.3.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,4 +1,4 @@
1
- import type { be_sig_triple_T } from 'rmemo'
1
+ import type { be_sig_triple_T } from '../be_sig_triple/index.js'
2
2
  export declare const [
3
3
  CACHE_VERSION$_,
4
4
  CACHE_VERSION_,
@@ -1,4 +1,4 @@
1
- import type { be_sig_triple_T } from 'rmemo'
1
+ import type { be_sig_triple_T } from '../be_sig_triple/index.js'
2
2
  export declare const [
3
3
  NODE_ENV$_,
4
4
  NODE_ENV_,
@@ -1,4 +1,4 @@
1
- import type { be_sig_triple_T } from 'rmemo'
1
+ import type { be_sig_triple_T } from '../be_sig_triple/index.js'
2
2
  export declare const [
3
3
  VERSION$_,
4
4
  VERSION_,
@@ -1,4 +1,5 @@
1
1
  /// <reference lib="dom" />
2
+ import { process_release_name } from '../process_release_name/index.js'
2
3
  /**
3
4
  * @type {Crypto}
4
5
  * @see {@link https://stackoverflow.com/a/70981544/142571}
@@ -7,6 +8,5 @@ export const crypto = globalThis.crypto
7
8
  export async function crypto_() {
8
9
  return (
9
10
  crypto
10
- || import('node:crypto').then($=>
11
- $.webcrypto))
11
+ || process_release_name && import('node:crypto').then(mod=>mod.webcrypto))
12
12
  }
@@ -0,0 +1,9 @@
1
+ export declare function file_exists_(path:string):Promise<boolean|undefined>
2
+ export {
3
+ file_exists_ as path__exists_
4
+ }
5
+ export declare function file_exists__waitfor(
6
+ path:string,
7
+ timeout?:number,
8
+ period?:number
9
+ ):Promise<void>
@@ -0,0 +1,27 @@
1
+ import { process_release_name } from '../process_release_name/index.js'
2
+ import { waitfor } from '../waitfor/index.js'
3
+ /**
4
+ * @param {string}path
5
+ * @returns {Promise<boolean>}
6
+ * @private
7
+ */
8
+ export async function file_exists_(path) {
9
+ return (
10
+ (process_release_name ?? false)
11
+ && import('node:fs/promises').then(({ access, constants })=>
12
+ access(path, constants.F_OK)
13
+ .then(()=>true)
14
+ .catch(()=>false)))
15
+ }
16
+ export {
17
+ file_exists_ as path__exists_
18
+ }
19
+ /**
20
+ * @param {string}path
21
+ * @param {number}[timeout]
22
+ * @param {number}[period]
23
+ * @returns {Promise<void>}
24
+ */
25
+ export function file_exists__waitfor(path, timeout = 5000, period = 0) {
26
+ return waitfor(()=>file_exists_(path), timeout, period)
27
+ }
@@ -0,0 +1,38 @@
1
+ import esmock from 'esmock'
2
+ import { unlink, writeFile } from 'fs/promises'
3
+ import { test } from 'uvu'
4
+ import { equal } from 'uvu/assert'
5
+ import { tempfile_path_ } from '../tempfile_path/index.js'
6
+ import { file_exists_, file_exists__waitfor } from './index.js'
7
+ test('file_exists_ + file_exists__waitfor|server', async ()=>{
8
+ const tempfile_path = await tempfile_path_()
9
+ equal(await file_exists_(tempfile_path), false)
10
+ await writeFile(tempfile_path, 'test content')
11
+ try {
12
+ await file_exists__waitfor(tempfile_path)
13
+ equal(await file_exists_(tempfile_path), true)
14
+ } finally {
15
+ await unlink(tempfile_path)
16
+ }
17
+ })
18
+ test('file_exists_ + file_exists__waitfor|browser', async ()=>{
19
+ const { file_exists_, file_exists__waitfor } = await esmock('./index.js', {}, {
20
+ '../process_release_name/index.js': {
21
+ process_release_name: undefined
22
+ }
23
+ })
24
+ const tempfile_path = await tempfile_path_()
25
+ equal(await file_exists_(tempfile_path), false)
26
+ await writeFile(tempfile_path, 'test content')
27
+ let err:Error|undefined = undefined
28
+ try {
29
+ await file_exists__waitfor(tempfile_path, 10)
30
+ } catch(_err) {
31
+ err = _err as Error
32
+ } finally {
33
+ await unlink(tempfile_path)
34
+ }
35
+ equal(await file_exists_(tempfile_path), false)
36
+ equal(err?.message, 'Timeout 10ms')
37
+ })
38
+ test.run()
package/all/index.d.ts CHANGED
@@ -142,6 +142,7 @@ export * from './fetch_method/index.js'
142
142
  export * from './fetch_response__throw/index.js'
143
143
  export * from './fetch_response_pair/index.js'
144
144
  export * from './fibonacci_number/index.js'
145
+ export * from './file_exists/index.js'
145
146
  export * from './filter/index.js'
146
147
  export * from './filter_o/index.js'
147
148
  export * from './filter_o/index.js'
@@ -222,8 +223,10 @@ export * from './isNumber_or/index.js'
222
223
  export * from './isObject/index.js'
223
224
  export * from './isPrimitive/index.js'
224
225
  export * from './isPromiseLike/index.js'
226
+ export * from './is_browser/index.js'
225
227
  export * from './is_development/index.js'
226
228
  export * from './is_production/index.js'
229
+ export * from './is_server/index.js'
227
230
  export * from './is_staging/index.js'
228
231
  export * from './item_r_idx/index.js'
229
232
  export * from './js_html/index.js'
package/all/index.js CHANGED
@@ -142,6 +142,7 @@ export * from './fetch_method/index.js'
142
142
  export * from './fetch_response__throw/index.js'
143
143
  export * from './fetch_response_pair/index.js'
144
144
  export * from './fibonacci_number/index.js'
145
+ export * from './file_exists/index.js'
145
146
  export * from './filter/index.js'
146
147
  export * from './filter_o/index.js'
147
148
  export * from './filter_o/index.js'
@@ -222,8 +223,10 @@ export * from './isNumber_or/index.js'
222
223
  export * from './isObject/index.js'
223
224
  export * from './isPrimitive/index.js'
224
225
  export * from './isPromiseLike/index.js'
226
+ export * from './is_browser/index.js'
225
227
  export * from './is_development/index.js'
226
228
  export * from './is_production/index.js'
229
+ export * from './is_server/index.js'
227
230
  export * from './is_staging/index.js'
228
231
  export * from './item_r_idx/index.js'
229
232
  export * from './js_html/index.js'
@@ -0,0 +1 @@
1
+ export declare function is_server_():boolean
@@ -0,0 +1,3 @@
1
+ export function is_server_() {
2
+ return !!globalThis.process?.release?.name
3
+ }
@@ -1,4 +1,4 @@
1
- import type { be_memo_pair_T } from 'rmemo'
1
+ import type { be_memo_pair_T } from '../be_memo_pair/index.js'
2
2
  export declare const [
3
3
  is_development$_,
4
4
  is_development_,
@@ -1,4 +1,4 @@
1
- import type { be_memo_pair_T } from 'rmemo'
1
+ import type { be_memo_pair_T } from '../be_memo_pair/index.js'
2
2
  export declare const [
3
3
  is_production$_,
4
4
  is_production_,
@@ -0,0 +1 @@
1
+ export declare function is_server_():boolean
@@ -0,0 +1,3 @@
1
+ export function is_browser_() {
2
+ return !globalThis.process?.release?.name
3
+ }
@@ -1,4 +1,4 @@
1
- import type { be_memo_pair_T } from 'rmemo'
1
+ import type { be_memo_pair_T } from '../be_memo_pair/index.js'
2
2
  export const [
3
3
  is_staging$_,
4
4
  is_staging_,
@@ -0,0 +1 @@
1
+ export declare function process_release_name_():string|undefined
@@ -0,0 +1 @@
1
+ export const process_release_name = globalThis.process?.release?.name
@@ -7,10 +7,16 @@ import { sleep } from '../sleep/index.js'
7
7
  * @returns {Promise<void>}
8
8
  */
9
9
  export async function waitfor(fn, timeout, period = 0) {
10
- await promise_timeout(async ()=>{
11
- for (; ;) {
12
- if (await fn()) return
13
- await sleep(period)
14
- }
15
- }, timeout)
10
+ let cancel
11
+ try {
12
+ await promise_timeout(async ()=>{
13
+ for (; !cancel;) {
14
+ if (await fn()) return
15
+ await sleep(period)
16
+ }
17
+ }, timeout)
18
+ } catch (err) {
19
+ cancel = 1
20
+ throw err
21
+ }
16
22
  }
package/env/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  export * from '../all/CACHE_VERSION/index.js'
2
2
  export * from '../all/import_meta_env/index.js'
3
+ export * from '../all/is_browser/index.js'
3
4
  export * from '../all/is_development/index.js'
4
5
  export * from '../all/is_production/index.js'
6
+ export * from '../all/is_server/index.js'
5
7
  export * from '../all/is_staging/index.js'
6
8
  export * from '../all/missing_env__throw/index.js'
7
9
  export * from '../all/NODE_ENV/index.js'
10
+ export * from '../all/process_release_name/index.js'
8
11
  export * from '../all/VERSION/index.js'
package/env/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  export * from '../all/CACHE_VERSION/index.js'
2
2
  export * from '../all/import_meta_env/index.js'
3
+ export * from '../all/is_browser/index.js'
3
4
  export * from '../all/is_development/index.js'
4
5
  export * from '../all/is_production/index.js'
6
+ export * from '../all/is_server/index.js'
5
7
  export * from '../all/is_staging/index.js'
6
8
  export * from '../all/missing_env__throw/index.js'
7
9
  export * from '../all/NODE_ENV/index.js'
10
+ export * from '../all/process_release_name/index.js'
8
11
  export * from '../all/VERSION/index.js'
package/fs/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '../all/file_exists/index.js'
package/fs/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from '../all/file_exists/index.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ctx-core",
3
- "version": "5.2.2",
3
+ "version": "5.3.0",
4
4
  "description": "ctx-core core library",
5
5
  "keywords": [
6
6
  "ctx-core",
@@ -47,6 +47,7 @@
47
47
  "error",
48
48
  "fetch",
49
49
  "fibonacci",
50
+ "fs",
50
51
  "function",
51
52
  "functional",
52
53
  "html",
@@ -95,6 +96,7 @@
95
96
  "./error": "./error/index.js",
96
97
  "./fetch": "./fetch/index.js",
97
98
  "./fibonacci": "./fibonacci/index.js",
99
+ "./fs": "./fs/index.js",
98
100
  "./function": "./function/index.js",
99
101
  "./functional": "./functional/index.js",
100
102
  "./html": "./html/index.js",
@@ -127,6 +129,7 @@
127
129
  "c8": "^8.0.1",
128
130
  "check-dts": "^0.7.2",
129
131
  "esbuild": "^0.19.10",
132
+ "esmock": "^2.6.0",
130
133
  "sinon": "^17.0.1",
131
134
  "size-limit": "^11.0.1",
132
135
  "tsx": "^4.7.0",
@@ -217,7 +220,7 @@
217
220
  "test": "pnpm run /^test:/",
218
221
  "test:size": "size-limit",
219
222
  "test:type": "check-dts",
220
- "test:unit": "tsx node_modules/uvu/bin.js . '\\.test\\.(ts|js)$'",
223
+ "test:unit": "NODE_OPTIONS=--loader=esmock tsx node_modules/uvu/bin.js . '\\.test\\.(ts|js)$'",
221
224
  "disable:test:coverage": "c8 pnpm test-unit"
222
225
  }
223
226
  }