@wener/utils 1.1.12 → 1.1.13

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.
Files changed (70) hide show
  1. package/dist/cjs/globalThis-ee7c1669.js +2 -0
  2. package/dist/cjs/globalThis-ee7c1669.js.map +1 -0
  3. package/dist/cjs/index.cjs +2 -2
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs/server.cjs +1 -1
  6. package/dist/cjs/server.cjs.map +1 -1
  7. package/dist/esm/globalThis-000611c3.js +2 -0
  8. package/dist/esm/globalThis-000611c3.js.map +1 -0
  9. package/dist/esm/index.js +4 -4
  10. package/dist/esm/index.js.map +1 -1
  11. package/dist/esm/server.js +1 -1
  12. package/dist/esm/server.js.map +1 -1
  13. package/dist/system/globalThis-7bba6592.js +2 -0
  14. package/dist/system/globalThis-7bba6592.js.map +1 -0
  15. package/dist/system/index.js +4 -4
  16. package/dist/system/index.js.map +1 -1
  17. package/dist/system/server.js +1 -1
  18. package/dist/system/server.js.map +1 -1
  19. package/lib/asyncs/timeout.js +3 -1
  20. package/lib/asyncs/timeout.js.map +1 -1
  21. package/lib/browsers/copy.js.map +1 -1
  22. package/lib/browsers/download.js +3 -1
  23. package/lib/browsers/download.js.map +1 -1
  24. package/lib/browsers/loaders.js +6 -2
  25. package/lib/browsers/loaders.js.map +1 -1
  26. package/lib/crypto/getRandomValues.js +1 -0
  27. package/lib/crypto/getRandomValues.js.map +1 -1
  28. package/lib/crypto/randomUUID.js +2 -0
  29. package/lib/crypto/randomUUID.js.map +1 -1
  30. package/lib/i18n/createTranslate.js +2 -1
  31. package/lib/i18n/createTranslate.js.map +1 -1
  32. package/lib/index.js +1 -0
  33. package/lib/index.js.map +1 -1
  34. package/lib/io/ArrayBuffers.js +1 -0
  35. package/lib/io/ArrayBuffers.js.map +1 -1
  36. package/lib/io/isTransferable.js +2 -0
  37. package/lib/io/isTransferable.js.map +1 -1
  38. package/lib/isomorphics/getGlobalThis.js.map +1 -1
  39. package/lib/isomorphics/globalThis.js +6 -0
  40. package/lib/isomorphics/globalThis.js.map +1 -0
  41. package/lib/isomorphics/structuredClone.js +1 -0
  42. package/lib/isomorphics/structuredClone.js.map +1 -1
  43. package/lib/logging/createLogger.js +15 -5
  44. package/lib/logging/createLogger.js.map +1 -1
  45. package/lib/servers/createProxyFetch.js +2 -0
  46. package/lib/servers/createProxyFetch.js.map +1 -1
  47. package/lib/servers/polyfillCrypto.js +2 -0
  48. package/lib/servers/polyfillCrypto.js.map +1 -1
  49. package/lib/servers/polyfillJsDom.js +2 -0
  50. package/lib/servers/polyfillJsDom.js.map +1 -1
  51. package/package.json +1 -1
  52. package/src/asyncs/timeout.ts +1 -1
  53. package/src/browsers/copy.ts +1 -1
  54. package/src/browsers/download.ts +1 -1
  55. package/src/browsers/loaders.ts +2 -2
  56. package/src/crypto/getRandomValues.ts +1 -0
  57. package/src/crypto/randomUUID.ts +2 -0
  58. package/src/i18n/createTranslate.ts +2 -1
  59. package/src/index.ts +1 -0
  60. package/src/io/ArrayBuffers.ts +1 -0
  61. package/src/io/isTransferable.ts +2 -0
  62. package/src/isomorphics/getGlobalThis.ts +3 -1
  63. package/src/isomorphics/globalThis.ts +3 -0
  64. package/src/isomorphics/structuredClone.ts +1 -0
  65. package/src/logging/createLogger.ts +5 -5
  66. package/src/objects/set.test.ts +1 -1
  67. package/src/servers/createProxyFetch.ts +2 -1
  68. package/src/servers/polyfillCrypto.ts +3 -1
  69. package/src/servers/polyfillJsDom.ts +1 -0
  70. package/src/servers/polyfillWebSocket.ts +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wener/utils",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "type": "module",
5
5
  "description": "Utils for daily use",
6
6
  "repository": {
@@ -4,7 +4,7 @@ export function timeout<T = any>(v: Promise<T>, ms: number): Promise<T> {
4
4
  return Promise.race([
5
5
  v,
6
6
  new Promise((_resolve, reject) => {
7
- timeout = setTimeout(() => reject(error), ms);
7
+ timeout = setTimeout(() => { reject(error); }, ms);
8
8
  }),
9
9
  ]).then(
10
10
  (v) => {
@@ -1,6 +1,6 @@
1
1
  import type { MaybePromise } from '../asyncs/MaybePromise';
2
2
 
3
- let _copy: (s: any) => void;
3
+ let _copy: (s: any) => MaybePromise<void>;
4
4
 
5
5
  // https://gist.github.com/rproenca/64781c6a1329b48a455b645d361a9aa3
6
6
  function initCopy() {
@@ -26,7 +26,7 @@ export function download(filename: string, data: any, { type = 'application/octe
26
26
 
27
27
  if (data instanceof File || data instanceof Blob || data instanceof MediaSource) {
28
28
  a.href = URL.createObjectURL(data);
29
- closer = () => URL.revokeObjectURL(a.href);
29
+ closer = () => { URL.revokeObjectURL(a.href); };
30
30
  } else {
31
31
  console.error(`invalid download data`, data);
32
32
  throw new Error(`can not download ${Object.getPrototypeOf(data)}`);
@@ -4,13 +4,13 @@ function load(
4
4
  reject: (v: any) => void,
5
5
  options: { attributes: Record<string, string> } | undefined,
6
6
  ) {
7
- el.onload = () => resolve(el);
7
+ el.onload = () => { resolve(el); };
8
8
  el.onerror = (e) => {
9
9
  el.remove();
10
10
  reject(e);
11
11
  };
12
12
  const { attributes = {} } = options || {};
13
- Object.entries(attributes).forEach(([k, v]) => el.setAttribute(k, v));
13
+ Object.entries(attributes).forEach(([k, v]) => { el.setAttribute(k, v); });
14
14
 
15
15
  document.head.appendChild(el);
16
16
  }
@@ -1,5 +1,6 @@
1
1
  // eslint-disable-next-line @typescript-eslint/consistent-type-imports
2
2
  import type { TypedArray } from '../io/ArrayBuffers';
3
+ import { globalThis } from '../isomorphics/globalThis';
3
4
  import { getNodeCrypto } from './getNodeCrypto';
4
5
 
5
6
  export let getRandomValues: <T extends Exclude<TypedArray, Float32Array | Float64Array>>(typedArray: T) => T =
@@ -1,3 +1,5 @@
1
+ import { globalThis } from '../isomorphics/globalThis';
2
+
1
3
  /**
2
4
  * generate random UUIDv4
3
5
  */
@@ -53,11 +53,12 @@ export function createTranslate<T extends object>(obj?: Record<string, T>): Tran
53
53
  }
54
54
  if (process.env.NODE_ENV === 'development') {
55
55
  if (val == null) {
56
- return console.error(
56
+ console.error(
57
57
  `[Translate] Missing the "${[].concat(key as any).join('.')}" key within the "${
58
58
  lang || locale
59
59
  }" dictionary`,
60
60
  );
61
+ return;
61
62
  }
62
63
  }
63
64
  if (typeof val === 'function') return val(params);
package/src/index.ts CHANGED
@@ -72,6 +72,7 @@ export { getFileFromDataTransfer } from './browsers/getFileFromDataTransfer';
72
72
 
73
73
  // polyfills
74
74
  export { getGlobalThis } from './isomorphics/getGlobalThis';
75
+ export { globalThis } from './isomorphics/globalThis';
75
76
  export { structuredClone } from './isomorphics/structuredClone';
76
77
 
77
78
  // crypto
@@ -1,3 +1,4 @@
1
+ import { globalThis } from '../isomorphics/globalThis';
1
2
  import { classOf } from '../langs/classOf';
2
3
  import { decodeBase64ToArrayBuffer, encodeArrayBufferToBase64 } from './base64';
3
4
  import { isBuffer } from './isBuffer';
@@ -1,3 +1,5 @@
1
+ import { globalThis } from '../isomorphics/globalThis';
2
+
1
3
  /**
2
4
  * transferable object pass between workers, can work with structuredClone
3
5
  *
@@ -5,6 +5,8 @@ declare const global: typeof globalThis;
5
5
  *
6
6
  * globalThis supported by ff 65, Chrome 71, Node 12, babel
7
7
  *
8
+ * non-standard globalThis supported for Alipay Miniprogram
9
+ *
8
10
  * @see https://caniuse.com/#search=globalThis
9
11
  * @see https://v8.dev/features/globalthis
10
12
  */
@@ -13,6 +15,6 @@ export const getGlobalThis = (): typeof globalThis => {
13
15
  if (typeof self !== 'undefined') return self;
14
16
  if (typeof window !== 'undefined') return window;
15
17
  if (typeof global !== 'undefined') return global as any;
16
- // if (typeof this !== 'undefined') return this;
18
+ if (typeof this !== 'undefined') return this as any;
17
19
  throw new Error('Unable to locate global `this`');
18
20
  };
@@ -0,0 +1,3 @@
1
+ import { getGlobalThis } from './getGlobalThis';
2
+
3
+ export const globalThis = getGlobalThis();
@@ -1,5 +1,6 @@
1
1
  /* eslint no-proto:0 */
2
2
  import { classOf } from '../langs/classOf';
3
+ import { globalThis } from './globalThis';
3
4
 
4
5
  /**
5
6
  * Clone an object using structured cloning algorithm
@@ -12,11 +12,11 @@ export function createLogger(
12
12
  context: object = {},
13
13
  ): LoggerWithChild {
14
14
  return {
15
- trace: (...values) => write({ ...context, level: 'trace', values }),
16
- debug: (...values) => write({ ...context, level: 'debug', values }),
17
- info: (...values) => write({ ...context, level: 'info', values }),
18
- warn: (...values) => write({ ...context, level: 'warn', values }),
19
- error: (...values) => write({ ...context, level: 'error', values }),
15
+ trace: (...values) => { write({ ...context, level: 'trace', values }); },
16
+ debug: (...values) => { write({ ...context, level: 'debug', values }); },
17
+ info: (...values) => { write({ ...context, level: 'info', values }); },
18
+ warn: (...values) => { write({ ...context, level: 'warn', values }); },
19
+ error: (...values) => { write({ ...context, level: 'error', values }); },
20
20
  child: (ctx) => createLogger(write, { ...context, ...ctx }),
21
21
  };
22
22
  }
@@ -28,7 +28,7 @@ test('set objects', (t) => {
28
28
  const orig = set;
29
29
 
30
30
  function run(isMerge: boolean) {
31
- const set = (a: any, b: any, c: any) => orig(a, b, c, isMerge);
31
+ const set = (a: any, b: any, c: any) => { orig(a, b, c, isMerge); };
32
32
  const verb = isMerge ? 'merge' : 'overwrite';
33
33
  objects(`should ${verb} existing object value :: simple`, () => {
34
34
  const { input } = prepare({
@@ -1,4 +1,5 @@
1
- import { FetchLike } from '../fetch';
1
+ import { type FetchLike } from '../fetch';
2
+ import { globalThis } from '../isomorphics/globalThis';
2
3
 
3
4
  export function createProxyFetch(proxy?: string, fetch?: FetchLike): FetchLike {
4
5
  if (!proxy) {
@@ -1,7 +1,9 @@
1
+ import { globalThis } from '../isomorphics/globalThis';
2
+
1
3
  export async function polyfillCrypto() {
2
4
  if ('crypto' in globalThis) {
3
5
  return false;
4
6
  }
5
- globalThis.crypto = (await import('node:crypto')).webcrypto as Crypto;
7
+ (globalThis as any).crypto = (await import('node:crypto')).webcrypto as Crypto;
6
8
  return true;
7
9
  }
@@ -1,4 +1,5 @@
1
1
  import type { ConstructorOptions, ResourceLoaderConstructorOptions } from 'jsdom';
2
+ import { globalThis } from '../isomorphics/globalThis';
2
3
 
3
4
  export async function polyfillJsDom() {
4
5
  if (typeof window !== 'undefined') {
@@ -1,4 +1,5 @@
1
1
  import type { MaybePromise } from '../asyncs/MaybePromise';
2
+ import { globalThis } from '../isomorphics/globalThis';
2
3
 
3
4
  export function polyfillWebSocket(ws: any): boolean;
4
5
  export function polyfillWebSocket(ws?: undefined): Promise<boolean>;