@wener/utils 1.1.13 → 1.1.15

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 (123) hide show
  1. package/dist/LICENSE.txt +1 -135
  2. package/dist/cjs/{globalThis-ee7c1669.js → getGlobalThis-304f74e0.js} +2 -2
  3. package/dist/cjs/getGlobalThis-304f74e0.js.map +1 -0
  4. package/dist/cjs/index.cjs +16 -5
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/server.cjs +1 -1
  7. package/dist/cjs/server.cjs.map +1 -1
  8. package/dist/esm/{globalThis-000611c3.js → getGlobalThis-b7ad0cf9.js} +2 -2
  9. package/dist/esm/getGlobalThis-b7ad0cf9.js.map +1 -0
  10. package/dist/esm/index.js +15 -4
  11. package/dist/esm/index.js.map +1 -1
  12. package/dist/esm/server.js +1 -1
  13. package/dist/esm/server.js.map +1 -1
  14. package/dist/system/getGlobalThis-8951eb0e.js +2 -0
  15. package/dist/system/getGlobalThis-8951eb0e.js.map +1 -0
  16. package/dist/system/index.js +16 -5
  17. package/dist/system/index.js.map +1 -1
  18. package/dist/system/server.js +1 -1
  19. package/dist/system/server.js.map +1 -1
  20. package/lib/asyncs/timeout.js.map +1 -1
  21. package/lib/browsers/download.js.map +1 -1
  22. package/lib/browsers/loaders.js.map +1 -1
  23. package/lib/crypto/getRandomValues.js +2 -1
  24. package/lib/crypto/getRandomValues.js.map +1 -1
  25. package/lib/crypto/randomUUID.js +2 -1
  26. package/lib/crypto/randomUUID.js.map +1 -1
  27. package/lib/fetch/createFetchWith.js +25 -0
  28. package/lib/fetch/createFetchWith.js.map +1 -0
  29. package/lib/fetch/createFetchWithLogging.js +21 -0
  30. package/lib/fetch/createFetchWithLogging.js.map +1 -0
  31. package/lib/fetch/dumpRequest.js +46 -0
  32. package/lib/fetch/dumpRequest.js.map +1 -0
  33. package/lib/fetch/dumpResponse.js +23 -0
  34. package/lib/fetch/dumpResponse.js.map +1 -0
  35. package/lib/index.js +4 -1
  36. package/lib/index.js.map +1 -1
  37. package/lib/io/ArrayBuffers.js +2 -2
  38. package/lib/io/ArrayBuffers.js.map +1 -1
  39. package/lib/io/isTransferable.js +2 -1
  40. package/lib/io/isTransferable.js.map +1 -1
  41. package/lib/isomorphics/structuredClone.js +2 -1
  42. package/lib/isomorphics/structuredClone.js.map +1 -1
  43. package/lib/logging/createLogger.js.map +1 -1
  44. package/lib/objects/get.js.map +1 -1
  45. package/lib/objects/set.js.map +1 -1
  46. package/lib/server.js +3 -1
  47. package/lib/server.js.map +1 -1
  48. package/lib/servers/createFetchWithProxy.js +12 -0
  49. package/lib/servers/createFetchWithProxy.js.map +1 -0
  50. package/lib/servers/{createProxyFetch.js → createFetchWithProxyByNodeFetch.js} +8 -4
  51. package/lib/servers/createFetchWithProxyByNodeFetch.js.map +1 -0
  52. package/lib/servers/createFetchWithProxyByUndici.js +28 -0
  53. package/lib/servers/createFetchWithProxyByUndici.js.map +1 -0
  54. package/lib/servers/polyfillCrypto.js +2 -1
  55. package/lib/servers/polyfillCrypto.js.map +1 -1
  56. package/lib/servers/polyfillJsDom.js +2 -1
  57. package/lib/servers/polyfillJsDom.js.map +1 -1
  58. package/package.json +39 -45
  59. package/src/asyncs/createLazyPromise.test.ts +21 -21
  60. package/src/asyncs/timeout.ts +3 -1
  61. package/src/browsers/download.ts +3 -1
  62. package/src/browsers/loaders.ts +6 -2
  63. package/src/crypto/getRandomValues.ts +2 -1
  64. package/src/crypto/hashing.test.ts +10 -12
  65. package/src/crypto/pem/__snapshots__/pem.test.ts.snap +18 -0
  66. package/src/crypto/pem/pem.test.ts +14 -17
  67. package/src/crypto/randomUUID.ts +3 -1
  68. package/src/crypto/ulid.test.ts +9 -9
  69. package/src/fetch/createFetchWith.ts +32 -0
  70. package/src/fetch/createFetchWithLogging.ts +20 -0
  71. package/src/fetch/dumpRequest.ts +51 -0
  72. package/src/fetch/dumpResponse.ts +28 -0
  73. package/src/fetch/index.ts +4 -0
  74. package/src/i18n/createTranslate.test.ts +76 -88
  75. package/src/index.ts +4 -2
  76. package/src/io/ArrayBuffer.test-d.ts +4 -2
  77. package/src/io/ArrayBuffers.base64.test.ts +17 -17
  78. package/src/io/ArrayBuffers.test.ts +8 -8
  79. package/src/io/ArrayBuffers.ts +2 -2
  80. package/src/io/Buffer.test.ts +4 -4
  81. package/src/io/isBuffer.test.ts +4 -4
  82. package/src/io/isTransferable.test.ts +7 -6
  83. package/src/io/isTransferable.ts +3 -1
  84. package/src/isomorphics/structuredClone.test.ts +4 -4
  85. package/src/isomorphics/structuredClone.ts +3 -1
  86. package/src/langs/deepEqual.test.ts +4 -4
  87. package/src/langs/langs.test.ts +3 -3
  88. package/src/libs/ms.test.ts +311 -0
  89. package/src/logging/createLogger.ts +15 -5
  90. package/src/logging/logger.test.ts +8 -8
  91. package/src/modules/parseModuleId.test.ts +3 -3
  92. package/src/objects/get.test-d.ts +30 -22
  93. package/src/objects/get.ts +1 -1
  94. package/src/objects/parseObjectPath.test.ts +3 -3
  95. package/src/objects/set.test.ts +98 -117
  96. package/src/objects/set.ts +1 -1
  97. package/src/server.ts +3 -1
  98. package/src/servers/createFetchWithProxy.ts +12 -0
  99. package/src/servers/{createProxyFetch.ts → createFetchWithProxyByNodeFetch.ts} +7 -3
  100. package/src/servers/createFetchWithProxyByUndici.ts +36 -0
  101. package/src/servers/polyfillBrowser.test.ts +9 -9
  102. package/src/servers/polyfillCrypto.ts +2 -1
  103. package/src/servers/polyfillJsDom.ts +3 -1
  104. package/src/servers/polyfillWebSocket.ts +2 -1
  105. package/src/strings/renderTemplate.test.ts +6 -9
  106. package/src/validations/parseTimestamp.test.ts +4 -4
  107. package/dist/cjs/globalThis-ee7c1669.js.map +0 -1
  108. package/dist/cjs/index-da9513d6.js +0 -13
  109. package/dist/cjs/index-da9513d6.js.map +0 -1
  110. package/dist/esm/globalThis-000611c3.js.map +0 -1
  111. package/dist/esm/index-c696799a.js +0 -13
  112. package/dist/esm/index-c696799a.js.map +0 -1
  113. package/dist/system/globalThis-7bba6592.js +0 -2
  114. package/dist/system/globalThis-7bba6592.js.map +0 -1
  115. package/dist/system/index-2dfef0f3.js +0 -13
  116. package/dist/system/index-2dfef0f3.js.map +0 -1
  117. package/lib/isomorphics/globalThis.js +0 -6
  118. package/lib/isomorphics/globalThis.js.map +0 -1
  119. package/lib/servers/createProxyFetch.js.map +0 -1
  120. package/src/crypto/pem/pem.test.ts.md +0 -24
  121. package/src/crypto/pem/pem.test.ts.snap +0 -0
  122. package/src/isomorphics/globalThis.ts +0 -3
  123. package/src/objects/get.test.ts +0 -63
@@ -0,0 +1,51 @@
1
+ export function dumpRequest({
2
+ url,
3
+ req = {},
4
+ log = console.log,
5
+ }: {
6
+ url: string;
7
+ req?: RequestInit;
8
+ log?: (s: string) => void;
9
+ }) {
10
+ const { method = 'GET' } = req;
11
+ let out = `-> ${method} ${url}
12
+ ${Array.from(new Headers(req.headers).entries())
13
+ .map(([k, v]) => `${k}: ${v}`)
14
+ .join('\n')}
15
+ `;
16
+
17
+ let done: Promise<string> | undefined;
18
+ if (req.body) {
19
+ if (req.body instanceof ReadableStream) {
20
+ const [a, b] = req.body.tee();
21
+ req.body = a;
22
+ const signal = req.signal;
23
+ done = Promise.resolve().then(async () => {
24
+ const reader = b.getReader();
25
+ log(out);
26
+ while (true) {
27
+ if (signal?.aborted) {
28
+ break;
29
+ }
30
+
31
+ let { done, value } = await reader.read();
32
+ value instanceof Uint8Array && (value = new TextDecoder().decode(value));
33
+ out += value;
34
+ log(value);
35
+ if (!done) {
36
+ break;
37
+ }
38
+ }
39
+ // maybe for archive
40
+ out += `\n`;
41
+ return out;
42
+ });
43
+ } else {
44
+ out += `
45
+ ${req.body}
46
+ `;
47
+ }
48
+ log(out);
49
+ }
50
+ return done || Promise.resolve(out);
51
+ }
@@ -0,0 +1,28 @@
1
+ export async function dumpResponse({
2
+ res,
3
+ url,
4
+ req,
5
+ log = console.log,
6
+ }: {
7
+ res: Response;
8
+ url: string;
9
+ req: RequestInit;
10
+ log?: (s: string) => void;
11
+ }) {
12
+ let out = `<- ${res.status} ${res.statusText} ${req.method} ${url}
13
+ ${Array.from(res.headers.entries())
14
+ .map(([k, v]) => `${k}: ${v}`)
15
+ .join('\n')}
16
+ `;
17
+ let contentType = res.headers.get('content-type');
18
+ // TODO text/event-stream
19
+ if (contentType?.includes('application/json') || contentType?.includes('text/plain')) {
20
+ const body = await res.text();
21
+ out += `\n${body}\n`;
22
+ res = new Response(body, res);
23
+ }
24
+
25
+ log(out);
26
+
27
+ return res;
28
+ }
@@ -1 +1,5 @@
1
1
  export type FetchLike = (url: string | Request, init?: RequestInit) => Promise<Response>;
2
+ export { createFetchWith } from './createFetchWith';
3
+ export { createFetchWithLogging } from './createFetchWithLogging';
4
+ export { dumpRequest } from './dumpRequest';
5
+ export { dumpResponse } from './dumpResponse';
@@ -1,80 +1,83 @@
1
- import test from 'ava';
1
+ import { assert, test } from 'vitest';
2
2
  import { createTranslate } from './createTranslate';
3
3
 
4
- test('exports', (t) => {
5
- const out = createTranslate();
6
- t.is(typeof out, 'object', 'returns an object');
7
- t.is(typeof out.t, 'function', '~> has "t" function');
8
- t.is(typeof out.dict, 'function', '~> has "set" function');
9
- t.is(typeof out.locale, 'function', '~> has "locale" function');
4
+ test('exports', () => {
5
+ assert.equal(typeof createTranslate, 'function', 'exports a function');
6
+
7
+ let out = createTranslate();
8
+ assert.equal(typeof out, 'object', 'returns an object');
9
+ assert.equal(typeof out.t, 'function', '~> has "t" function');
10
+ assert.equal(typeof out.dict, 'function', '~> has "dict" function');
11
+ assert.equal(typeof out.locale, 'function', '~> has "locale" function');
10
12
  });
11
13
 
12
- test('usage', (t) => {
13
- const ctx = createTranslate({
14
+ test('usage', () => {
15
+ let ctx = createTranslate({
14
16
  en: { hello: 'Hello, {{name}}!' },
15
17
  es: { hello: 'Hola {{name}}!' },
16
18
  pt: { foo: 'foo {{name}}~!' },
17
19
  });
18
20
 
19
- t.deepEqual(ctx.dict('en'), { hello: 'Hello, {{name}}!' });
21
+ assert.deepEqual(ctx.dict('en'), { hello: 'Hello, {{name}}!' });
22
+
23
+ assert.equal(ctx.dict('foobar'), undefined);
20
24
 
21
- t.true(ctx.dict('foobar') === undefined);
25
+ let foo = ctx.t('hello');
26
+ assert.equal(foo, '', '~> "" w/o locale');
22
27
 
23
- const foo = ctx.t('hello');
24
- t.is(foo, '', '~> "" w/o locale');
28
+ assert.equal(ctx.locale('en'), 'en', '>>> ctx.locale()');
25
29
 
26
- t.is(ctx.locale('en'), 'en', '>>> ctx.locale()');
30
+ assert.equal(ctx.locale(), 'en');
27
31
 
28
- t.is(ctx.locale(), 'en');
32
+ let bar = ctx.t('hello');
29
33
 
30
- const bar = ctx.t('hello');
31
- t.not(bar, '', '(en) found "hello" key');
32
- t.is(bar, 'Hello, !', '~> interpolations empty if missing param');
34
+ assert.notEqual(bar, '', '(en) found "hello" key');
35
+ assert.equal(bar, 'Hello, !', '~> interpolations empty if missing param');
33
36
 
34
- const baz = ctx.t('hello', { name: 'world' });
35
- t.is(baz, 'Hello, world!', '~> interpolations successful');
37
+ let baz = ctx.t('hello', { name: 'world' });
38
+ assert.equal(baz, 'Hello, world!', '~> interpolations successful');
36
39
 
37
- const bat = ctx.t('hello', { name: 'world' }, 'es');
38
- t.not(bat, '', '(es) found "hello" key');
39
- t.is(bat, 'Hola world!', '~> success');
40
+ let bat = ctx.t('hello', { name: 'world' }, 'es');
41
+ assert.notEqual(bat, '', '(es) found "hello" key');
42
+ assert.equal(bat, 'Hola world!', '~> success');
40
43
 
41
- t.is(ctx.t('hello', { name: 'world' }, 'pt'), '', '(pt) did NOT find "hello" key');
44
+ assert.equal(ctx.t('hello', { name: 'world' }, 'pt'), '', '(pt) did NOT find "hello" key');
42
45
 
43
- t.is(ctx.dict('pt', { hello: 'Oí {{name}}!' }), undefined, '>>> ctx.dict()');
46
+ assert.equal(ctx.dict('pt', { hello: 'Oí {{name}}!' }), undefined, '>>> ctx.set()');
44
47
 
45
- const quz = ctx.t('hello', { name: 'world' }, 'pt');
46
- t.not(quz, '', '(pt) found "hello" key');
47
- t.is(quz, 'Oí world!', '~> success');
48
+ let quz = ctx.t('hello', { name: 'world' }, 'pt');
49
+ assert.notEqual(quz, '', '(pt) found "hello" key');
50
+ assert.equal(quz, 'Oí world!', '~> success');
48
51
 
49
- const qut = ctx.t('foo', { name: 'bar' }, 'pt');
50
- t.not(qut, '', '(pt) found "foo" key');
51
- t.is(qut, 'foo bar~!', '~> success');
52
+ let qut = ctx.t('foo', { name: 'bar' }, 'pt');
53
+ assert.notEqual(qut, '', '(pt) found "foo" key');
54
+ assert.equal(qut, 'foo bar~!', '~> success');
52
55
 
53
- t.is(ctx.locale('es'), 'es', '>>> ctx.locale()');
56
+ assert.equal(ctx.locale('es'), 'es', '>>> ctx.locale()');
54
57
 
55
- t.is(ctx.locale(), 'es');
56
- t.is(ctx.locale(''), 'es');
57
- t.is(ctx.locale(false as any), 'es');
58
- t.is(ctx.locale(null as any), 'es');
59
- t.is(ctx.locale(0 as any), 'es');
58
+ assert.equal(ctx.locale(), 'es');
59
+ assert.equal(ctx.locale(''), 'es');
60
+ assert.equal(ctx.locale(false as any), 'es');
61
+ assert.equal(ctx.locale(null as any), 'es');
62
+ assert.equal(ctx.locale(0 as any), 'es');
60
63
 
61
- const qux = ctx.t('hello', { name: 'default' });
62
- t.not(qux, '', '(es) found "hello" key');
63
- t.is(qux, 'Hola default!', '~> success');
64
+ let qux = ctx.t('hello', { name: 'default' });
65
+ assert.notEqual(qux, '', '(es) found "hello" key');
66
+ assert.equal(qux, 'Hola default!', '~> success');
64
67
 
65
- t.is(ctx.t('hello', { name: 'world' }, 'de'), '', '(de) did NOT find "hello" key');
68
+ assert.equal(ctx.t('hello', { name: 'world' }, 'de'), '', '(de) did NOT find "hello" key');
66
69
 
67
- t.is(ctx.dict('de', { hello: 'Hallo {{name}}!' }), undefined, '>>> ctx.dict(de)');
70
+ assert.equal(ctx.dict('de', { hello: 'Hallo {{name}}!' }), undefined, '>>> ctx.set(de)');
68
71
 
69
- const qar = ctx.t('hello', { name: 'world' }, 'de');
70
- t.not(qar, '', '(de) found "hello" key');
71
- t.is(qar, 'Hallo world!', '~> success');
72
+ let qar = ctx.t('hello', { name: 'world' }, 'de');
73
+ assert.notEqual(qar, '', '(de) found "hello" key');
74
+ assert.equal(qar, 'Hallo world!', '~> success');
72
75
  });
73
76
 
74
- test('functional', (t) => {
75
- const ctx = createTranslate({
77
+ test('functional', () => {
78
+ let ctx = createTranslate({
76
79
  en: {
77
- hello(value: any) {
80
+ hello(value: string) {
78
81
  return `hello ${value || 'stranger'}~!`;
79
82
  },
80
83
  },
@@ -82,18 +85,18 @@ test('functional', (t) => {
82
85
 
83
86
  ctx.locale('en');
84
87
 
85
- const foo = ctx.t('hello');
86
- t.is(foo, 'hello stranger~!', '~> called function w/o param');
88
+ let foo = ctx.t('hello');
89
+ assert.equal(foo, 'hello stranger~!', '~> called function w/o param');
87
90
 
88
- const bar = ctx.t('hello', 'world' as any);
89
- t.is(bar, 'hello world~!', '~> called function w/ param (string)');
91
+ let bar = ctx.t('hello', 'world' as any);
92
+ assert.equal(bar, 'hello world~!', '~> called function w/ param (string)');
90
93
 
91
- const baz = ctx.t('hello', [1, 2, 3]);
92
- t.is(baz, 'hello 1,2,3~!', '~> called function w/ param (array)');
94
+ let baz = ctx.t('hello', [1, 2, 3]);
95
+ assert.equal(baz, 'hello 1,2,3~!', '~> called function w/ param (array)');
93
96
  });
94
97
 
95
- test('nested', (t) => {
96
- const ctx = createTranslate({
98
+ test('nested', () => {
99
+ let ctx = createTranslate({
97
100
  en: {
98
101
  fruits: {
99
102
  apple: 'apple',
@@ -111,22 +114,22 @@ test('nested', (t) => {
111
114
  });
112
115
 
113
116
  ctx.locale('en');
114
- t.is(ctx.t('fruits.apple'), 'apple', '(en) fruits.apple');
115
- t.is(ctx.t('fruits.orange'), 'orange', '(en) fruits.orange');
116
- t.is(ctx.t(['fruits', 'grape']), 'grape', '(en) ["fruits","grape"]');
117
- t.is(ctx.t('fruits.404'), '', '(en) fruits.404 ~> ""');
118
- t.is(ctx.t('error.404'), '', '(en) error.404 ~> ""');
117
+ assert.equal(ctx.t('fruits.apple'), 'apple', '(en) fruits.apple');
118
+ assert.equal(ctx.t('fruits.orange'), 'orange', '(en) fruits.orange');
119
+ assert.equal(ctx.t(['fruits', 'grape']), 'grape', '(en) ["fruits","grape"]');
120
+ assert.equal(ctx.t('fruits.404'), '', '(en) fruits.404 ~> ""');
121
+ assert.equal(ctx.t('error.404'), '', '(en) error.404 ~> ""');
119
122
 
120
123
  ctx.locale('es');
121
- t.is(ctx.t('fruits.apple'), 'manzana', '(es) fruits.apple');
122
- t.is(ctx.t('fruits.orange'), 'naranja', '(es) fruits.orange');
123
- t.is(ctx.t(['fruits', 'grape']), 'uva', '(es) ["fruits","grape"]');
124
- t.is(ctx.t('fruits.404'), '', '(es) fruits.404 ~> ""');
125
- t.is(ctx.t('error.404'), '', '(es) error.404 ~> ""');
124
+ assert.equal(ctx.t('fruits.apple'), 'manzana', '(es) fruits.apple');
125
+ assert.equal(ctx.t('fruits.orange'), 'naranja', '(es) fruits.orange');
126
+ assert.equal(ctx.t(['fruits', 'grape']), 'uva', '(es) ["fruits","grape"]');
127
+ assert.equal(ctx.t('fruits.404'), '', '(es) fruits.404 ~> ""');
128
+ assert.equal(ctx.t('error.404'), '', '(es) error.404 ~> ""');
126
129
  });
127
130
 
128
- test('arrays', (t) => {
129
- const ctx = createTranslate({
131
+ test('arrays', () => {
132
+ let ctx = createTranslate({
130
133
  en: {
131
134
  foo: '{{0}} + {{1}} = {{2}}',
132
135
  bar: [
@@ -139,32 +142,17 @@ test('arrays', (t) => {
139
142
 
140
143
  ctx.locale('en');
141
144
 
142
- t.is(ctx.t('foo', [1, 2, 3]), '1 + 2 = 3', '~> foo');
145
+ assert.equal(ctx.t('foo', [1, 2, 3]), '1 + 2 = 3', '~> foo');
143
146
 
144
- t.is(ctx.t('bar.0.baz', { colors: ['red', 'blue'] }), 'roses are red, violets are blue', '~> bar.0.baz');
147
+ assert.equal(ctx.t('bar.0.baz', { colors: ['red', 'blue'] }), 'roses are red, violets are blue', '~> bar.0.baz');
145
148
  });
146
149
 
147
- test('invalid value', (t) => {
148
- const ctx = createTranslate({
150
+ test('invalid value', () => {
151
+ let ctx = createTranslate({
149
152
  en: {
150
153
  foo: ['bar'],
151
154
  },
152
155
  });
153
156
 
154
- t.deepEqual(ctx.t('foo', null as any, 'en'), ['bar']);
155
- });
156
-
157
- test('fallback', (t) => {
158
- const ctx = createTranslate({
159
- en: {
160
- a: 'a',
161
- },
162
- 'en-US': {
163
- a: 'a-US',
164
- },
165
- });
166
-
167
- // t.deepEqual(ctx.t('a', undefined, 'en'), 'a');
168
- // t.deepEqual(ctx.t('a', undefined, 'en-US'), 'a-US');
169
- t.deepEqual(ctx.t('a', undefined, 'en-UK'), 'a');
157
+ assert.deepEqual(ctx.t('foo', null as never, 'en'), ['bar'] as any);
170
158
  });
package/src/index.ts CHANGED
@@ -72,8 +72,9 @@ export { getFileFromDataTransfer } from './browsers/getFileFromDataTransfer';
72
72
 
73
73
  // polyfills
74
74
  export { getGlobalThis } from './isomorphics/getGlobalThis';
75
- export { globalThis } from './isomorphics/globalThis';
76
75
  export { structuredClone } from './isomorphics/structuredClone';
76
+ // should not export as sideEffect
77
+ // export { globalThis } from './isomorphics/globalThis';
77
78
 
78
79
  // crypto
79
80
  export { randomUUID } from './crypto/randomUUID';
@@ -86,7 +87,8 @@ export { PEM } from './crypto/pem/pem';
86
87
  // misc
87
88
  export { createRandom } from './maths/random';
88
89
 
89
- export { type FetchLike } from './fetch';
90
+ // network
91
+ export { type FetchLike, createFetchWith, createFetchWithLogging, dumpResponse, dumpRequest } from './fetch';
90
92
 
91
93
  // bundled
92
94
  export { default as ms } from './libs/ms';
@@ -1,4 +1,6 @@
1
- import { expectType } from 'tsd';
1
+ import { expectTypeOf, test } from 'vitest';
2
2
  import { ArrayBuffers } from './ArrayBuffers';
3
3
 
4
- expectType<Buffer>(ArrayBuffers.asView(Buffer, new Uint8Array()));
4
+ test('my types work properly', () => {
5
+ expectTypeOf(ArrayBuffers.asView(Buffer, new Uint8Array())).toMatchTypeOf<Buffer>();
6
+ });
@@ -1,23 +1,23 @@
1
- import test from 'ava';
1
+ import { test, beforeAll, assert, expect } from 'vitest';
2
2
  import { ArrayBuffers } from './ArrayBuffers';
3
3
 
4
- test.before((t) => {
5
- t.true(ArrayBuffers.isNativeBufferAvailable());
4
+ beforeAll(() => {
5
+ expect(ArrayBuffers.isNativeBufferAvailable()).toBeTruthy();
6
6
  ArrayBuffers.setNativeBufferAllowed(false);
7
7
  });
8
8
 
9
- test('base64: ignore whitespace', function (t) {
9
+ test('base64: ignore whitespace', function () {
10
10
  const text = '\n YW9ldQ== ';
11
11
  const buf = ArrayBuffers.from(text, 'base64');
12
- t.is(ArrayBuffers.toString(buf), 'aoeu');
12
+ expect(ArrayBuffers.toString(buf)).toBe('aoeu');
13
13
  });
14
14
 
15
- test('base64: strings without padding', function (t) {
16
- t.is(ArrayBuffers.toString(ArrayBuffers.from('YW9ldQ', 'base64')), 'aoeu');
15
+ test('base64: strings without padding', function () {
16
+ expect(ArrayBuffers.toString(ArrayBuffers.from('YW9ldQ', 'base64'))).toBe('aoeu');
17
17
  });
18
18
 
19
- test('base64: newline in utf8 -- should not be an issue', function (t) {
20
- t.is(
19
+ test('base64: newline in utf8 -- should not be an issue', function () {
20
+ assert.equal(
21
21
  ArrayBuffers.toString(
22
22
  ArrayBuffers.from('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64'),
23
23
  'utf8',
@@ -26,8 +26,8 @@ test('base64: newline in utf8 -- should not be an issue', function (t) {
26
26
  );
27
27
  });
28
28
 
29
- test('base64: newline in base64 -- should get stripped', function (t) {
30
- t.is(
29
+ test('base64: newline in base64 -- should get stripped', function () {
30
+ assert.equal(
31
31
  ArrayBuffers.toString(
32
32
  ArrayBuffers.from(
33
33
  'LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt',
@@ -39,8 +39,8 @@ test('base64: newline in base64 -- should get stripped', function (t) {
39
39
  );
40
40
  });
41
41
 
42
- test('base64: tab characters in base64 - should get stripped', function (t) {
43
- t.is(
42
+ test('base64: tab characters in base64 - should get stripped', function () {
43
+ assert.equal(
44
44
  ArrayBuffers.toString(
45
45
  ArrayBuffers.from(
46
46
  'LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt',
@@ -52,11 +52,11 @@ test('base64: tab characters in base64 - should get stripped', function (t) {
52
52
  );
53
53
  });
54
54
 
55
- test('base64: invalid non-alphanumeric characters -- should be stripped', function (t) {
56
- t.is(ArrayBuffers.toString(ArrayBuffers.from('!"#$%&\'()*,.:;<=>?@[\\]^`{|}~', 'base64'), 'utf8'), '');
55
+ test('base64: invalid non-alphanumeric characters -- should be stripped', function () {
56
+ expect(ArrayBuffers.toString(ArrayBuffers.from('!"#$%&\'()*,.:;<=>?@[\\]^`{|}~', 'base64'), 'utf8')).toBe('');
57
57
  });
58
58
 
59
- test('base64: high byte', function (t) {
59
+ test('base64: high byte', function () {
60
60
  const highByte = ArrayBuffers.from([128]);
61
- t.deepEqual(ArrayBuffers.alloc(1, ArrayBuffers.toString(highByte, 'base64'), 'base64'), highByte);
61
+ assert.deepEqual(ArrayBuffers.alloc(1, ArrayBuffers.toString(highByte, 'base64'), 'base64'), highByte);
62
62
  });
@@ -1,23 +1,23 @@
1
- import test from 'ava';
1
+ import { test, expect } from 'vitest';
2
2
  import { ArrayBuffers } from './ArrayBuffers';
3
3
 
4
- test('concat', (t) => {
4
+ test('concat', () => {
5
5
  const check = (Type: any = Uint8Array, va = [1, 2, 3], vb = [4, 5, 6]) => {
6
6
  const a = new Type(va);
7
7
  const b = new Type(vb);
8
- t.deepEqual(ArrayBuffers.concat([a, b]), new Type([...va, ...vb]).buffer, `should concat ${Type}`);
8
+ expect(ArrayBuffers.concat([a, b]), `should concat ${Type}`).toStrictEqual(new Type([...va, ...vb]).buffer);
9
9
  };
10
10
  check(Uint8Array);
11
11
  check(Uint16Array, [0xff, 0xffff]);
12
12
  check(Uint32Array, [0xfffffff]);
13
13
  });
14
14
 
15
- test('asView for Buffer', (t) => {
15
+ test('asView for Buffer', () => {
16
16
  const buf = Buffer.from([1, 2, 3, 4, 5]);
17
- t.is(ArrayBuffers.asView(Buffer, buf), buf);
17
+ expect(ArrayBuffers.asView(Buffer, buf)).toBe(buf);
18
18
  const uint8 = ArrayBuffers.asView(Uint8Array, buf);
19
- t.is(ArrayBuffers.asView(Buffer, uint8).buffer, buf.buffer);
19
+ expect(ArrayBuffers.asView(Buffer, uint8).buffer).toBe(buf.buffer);
20
20
 
21
- t.deepEqual(uint8, buf);
22
- t.deepEqual(ArrayBuffers.asView(Buffer, uint8), buf);
21
+ expect(uint8).toEqual(buf);
22
+ expect(ArrayBuffers.asView(Buffer, uint8)).toEqual(buf);
23
23
  });
@@ -1,4 +1,4 @@
1
- import { globalThis } from '../isomorphics/globalThis';
1
+ import { getGlobalThis } from '../isomorphics/getGlobalThis';
2
2
  import { classOf } from '../langs/classOf';
3
3
  import { decodeBase64ToArrayBuffer, encodeArrayBufferToBase64 } from './base64';
4
4
  import { isBuffer } from './isBuffer';
@@ -74,7 +74,7 @@ export class ArrayBuffers {
74
74
 
75
75
  static isNativeBufferAvailable() {
76
76
  // eslint-disable-next-line no-return-assign
77
- return (this.#isBufferAvailable ??= !(globalThis.Buffer as any)?.isPollyfill?.());
77
+ return (this.#isBufferAvailable ??= !(getGlobalThis().Buffer as any)?.isPollyfill?.());
78
78
  }
79
79
 
80
80
  static isNativeBufferAllowed() {
@@ -1,12 +1,12 @@
1
- import test from 'ava';
1
+ import { test, expect } from 'vitest';
2
2
  import { Buffer } from './Buffer';
3
3
  import { isBuffer } from './isBuffer';
4
4
 
5
- test('basic', (t) => {
5
+ test('basic', () => {
6
6
  {
7
7
  const buf = new Buffer(0);
8
- t.true(Buffer.isBuffer(buf));
9
- t.true(isBuffer(buf));
8
+ expect(Buffer.isBuffer(buf)).toBeTruthy();
9
+ expect(isBuffer(buf)).toBeTruthy();
10
10
  }
11
11
  // const b = new Buffer(10)
12
12
  // t.is(b.length,10)
@@ -1,9 +1,9 @@
1
- import test from 'ava';
2
1
  import { Buffer } from 'node:buffer';
2
+ import { expect, test } from 'vitest';
3
3
  import { classOf } from '../langs/classOf';
4
4
  import { isBuffer } from './isBuffer';
5
5
 
6
- test('isBuffer', (t) => {
7
- t.true(isBuffer(Buffer.from('')));
8
- t.is(classOf(Buffer), 'Function');
6
+ test('isBuffer', () => {
7
+ expect(isBuffer(Buffer.from(''))).toBeTruthy();
8
+ expect(classOf(Buffer)).toBe('Function');
9
9
  });
@@ -1,10 +1,11 @@
1
- import test from 'ava';
2
1
  import { Buffer } from 'node:buffer';
2
+ import { expect, test } from 'vitest';
3
3
  import { isTransferable } from './isTransferable';
4
4
 
5
- test('isTransferable', (t) => {
6
- t.false(isTransferable(0));
7
- t.false(isTransferable(Buffer.from('')));
8
- t.true(new ArrayBuffer(0) instanceof ArrayBuffer);
9
- t.true(isTransferable(new ArrayBuffer(0)));
5
+ test('isTransferable', () => {
6
+ expect(isTransferable(0)).toBeFalsy();
7
+ expect(isTransferable(Buffer.from(''))).toBeFalsy();
8
+
9
+ expect(new ArrayBuffer(0) instanceof ArrayBuffer).toBeTruthy();
10
+ expect(isTransferable(new ArrayBuffer(0))).toBeTruthy();
10
11
  });
@@ -1,4 +1,6 @@
1
- import { globalThis } from '../isomorphics/globalThis';
1
+ import { getGlobalThis } from '../isomorphics/getGlobalThis';
2
+
3
+ const globalThis = getGlobalThis();
2
4
 
3
5
  /**
4
6
  * transferable object pass between workers, can work with structuredClone
@@ -1,14 +1,14 @@
1
- import test from 'ava';
1
+ import { expect, test } from 'vitest';
2
2
  import { classOf } from '../langs/classOf';
3
3
  import { _clone } from './structuredClone';
4
4
 
5
- test('structuredClone', (t) => {
5
+ test('structuredClone', () => {
6
6
  for (const [k, v] of [
7
7
  ['', ''],
8
8
  [Number(1), 1],
9
9
  ]) {
10
10
  const c = _clone(k);
11
- t.deepEqual(c, v);
12
- t.is(classOf(c), classOf(v));
11
+ expect(c).toEqual(v);
12
+ expect(classOf(c)).toBe(classOf(v));
13
13
  }
14
14
  });
@@ -1,6 +1,8 @@
1
1
  /* eslint no-proto:0 */
2
2
  import { classOf } from '../langs/classOf';
3
- import { globalThis } from './globalThis';
3
+ import { getGlobalThis } from './getGlobalThis';
4
+
5
+ const globalThis = getGlobalThis();
4
6
 
5
7
  /**
6
8
  * Clone an object using structured cloning algorithm
@@ -1,8 +1,8 @@
1
- import test from 'ava';
1
+ import { expect, test } from 'vitest';
2
2
  import { deepEqual } from './deepEqual';
3
3
 
4
- test('deep equal', (t) => {
5
- t.true(
4
+ test('deep equal', () => {
5
+ expect(
6
6
  deepEqual(
7
7
  {
8
8
  a: null,
@@ -13,5 +13,5 @@ test('deep equal', (t) => {
13
13
  b: Infinity,
14
14
  },
15
15
  ),
16
- );
16
+ ).toBeTruthy();
17
17
  });
@@ -1,7 +1,7 @@
1
- import test from 'ava';
1
+ import { expect, test } from 'vitest';
2
2
  import { classOf } from './classOf';
3
3
 
4
- test('classOf', (t) => {
4
+ test('classOf', () => {
5
5
  for (const [k, v] of [
6
6
  [0, 'Number'],
7
7
  ['', 'String'],
@@ -18,6 +18,6 @@ test('classOf', (t) => {
18
18
  [new DataView(new ArrayBuffer(0)), 'DataView'],
19
19
  [new Int8Array(0), 'Int8Array'],
20
20
  ]) {
21
- t.is(classOf(k), v as any);
21
+ expect(classOf(k)).toBe(v);
22
22
  }
23
23
  });