@modern-js/plugin 1.3.2 → 1.3.5

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 (90) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/js/modern/farrow-pipeline/asyncHooks.node.js +74 -0
  3. package/dist/js/modern/farrow-pipeline/asyncHooksInterface.js +12 -0
  4. package/dist/js/modern/farrow-pipeline/context.js +99 -0
  5. package/dist/js/modern/farrow-pipeline/counter.js +18 -0
  6. package/dist/js/modern/farrow-pipeline/hook.js +42 -0
  7. package/dist/js/modern/farrow-pipeline/index.js +19 -0
  8. package/dist/js/modern/farrow-pipeline/pipeline.js +93 -0
  9. package/dist/js/modern/index.js +1 -1
  10. package/dist/js/modern/manager/async.js +1 -1
  11. package/dist/js/modern/manager/runner.js +1 -1
  12. package/dist/js/modern/manager/sync.js +2 -3
  13. package/dist/js/modern/waterfall/async.js +5 -9
  14. package/dist/js/modern/waterfall/sync.js +5 -9
  15. package/dist/js/modern/workflow/async.js +2 -5
  16. package/dist/js/modern/workflow/parallel.js +3 -4
  17. package/dist/js/modern/workflow/sync.js +2 -2
  18. package/dist/js/node/farrow-pipeline/asyncHooks.node.js +93 -0
  19. package/dist/js/node/farrow-pipeline/asyncHooksInterface.js +26 -0
  20. package/dist/js/node/farrow-pipeline/context.js +120 -0
  21. package/dist/js/node/farrow-pipeline/counter.js +27 -0
  22. package/dist/js/node/farrow-pipeline/hook.js +52 -0
  23. package/dist/js/node/farrow-pipeline/index.js +18 -0
  24. package/dist/js/node/farrow-pipeline/pipeline.js +129 -0
  25. package/dist/js/node/index.js +1 -1
  26. package/dist/js/node/manager/async.js +1 -1
  27. package/dist/js/node/manager/runner.js +1 -1
  28. package/dist/js/node/manager/sync.js +2 -3
  29. package/dist/js/node/waterfall/async.js +5 -9
  30. package/dist/js/node/waterfall/sync.js +5 -9
  31. package/dist/js/node/workflow/async.js +2 -5
  32. package/dist/js/node/workflow/parallel.js +3 -4
  33. package/dist/js/node/workflow/sync.js +2 -2
  34. package/dist/js/treeshaking/farrow-pipeline/asyncHooks.node.js +74 -0
  35. package/dist/js/treeshaking/farrow-pipeline/asyncHooksInterface.js +12 -0
  36. package/dist/js/treeshaking/farrow-pipeline/context.js +103 -0
  37. package/dist/js/treeshaking/farrow-pipeline/counter.js +23 -0
  38. package/dist/js/treeshaking/farrow-pipeline/hook.js +46 -0
  39. package/dist/js/treeshaking/farrow-pipeline/index.js +19 -0
  40. package/dist/js/treeshaking/farrow-pipeline/pipeline.js +100 -0
  41. package/dist/js/treeshaking/index.js +1 -1
  42. package/dist/js/treeshaking/manager/async.js +5 -26
  43. package/dist/js/treeshaking/manager/runner.js +1 -1
  44. package/dist/js/treeshaking/manager/sync.js +6 -25
  45. package/dist/js/treeshaking/waterfall/async.js +13 -37
  46. package/dist/js/treeshaking/waterfall/sync.js +12 -32
  47. package/dist/js/treeshaking/workflow/async.js +7 -29
  48. package/dist/js/treeshaking/workflow/parallel.js +7 -27
  49. package/dist/js/treeshaking/workflow/sync.js +6 -25
  50. package/dist/types/farrow-pipeline/asyncHooks.node.d.ts +2 -0
  51. package/dist/types/farrow-pipeline/asyncHooksInterface.d.ts +19 -0
  52. package/dist/types/farrow-pipeline/context.d.ts +28 -0
  53. package/dist/types/farrow-pipeline/counter.d.ts +11 -0
  54. package/dist/types/farrow-pipeline/hook.d.ts +9 -0
  55. package/dist/types/farrow-pipeline/index.d.ts +19 -0
  56. package/dist/types/farrow-pipeline/pipeline.d.ts +38 -0
  57. package/dist/types/index.d.ts +1 -1
  58. package/dist/types/manager/runner.d.ts +1 -1
  59. package/dist/types/manager/sync.d.ts +1 -1
  60. package/dist/types/manager/types.d.ts +1 -1
  61. package/dist/types/waterfall/async.d.ts +1 -1
  62. package/dist/types/waterfall/sync.d.ts +1 -1
  63. package/dist/types/workflow/async.d.ts +1 -1
  64. package/dist/types/workflow/sync.d.ts +1 -1
  65. package/package.json +37 -8
  66. package/jest.config.js +0 -8
  67. package/modern.config.js +0 -2
  68. package/node.d.ts +0 -1
  69. package/node.js +0 -1
  70. package/tests/.eslintrc.js +0 -6
  71. package/tests/async.test.ts +0 -738
  72. package/tests/fixtures/async/base/bar.ts +0 -22
  73. package/tests/fixtures/async/base/foo.ts +0 -20
  74. package/tests/fixtures/async/base/fooManager.ts +0 -47
  75. package/tests/fixtures/async/core/index.ts +0 -174
  76. package/tests/fixtures/async/dynamic/bar.ts +0 -18
  77. package/tests/fixtures/async/dynamic/foo.ts +0 -27
  78. package/tests/fixtures/sync/base/bar.ts +0 -21
  79. package/tests/fixtures/sync/base/foo.ts +0 -20
  80. package/tests/fixtures/sync/base/fooManager.ts +0 -47
  81. package/tests/fixtures/sync/core/index.ts +0 -171
  82. package/tests/fixtures/sync/dynamic/bar.ts +0 -22
  83. package/tests/fixtures/sync/dynamic/foo.ts +0 -27
  84. package/tests/helpers.ts +0 -4
  85. package/tests/pipeline.test.ts +0 -607
  86. package/tests/sync.test.ts +0 -701
  87. package/tests/tsconfig.json +0 -13
  88. package/tests/waterfall.test.ts +0 -172
  89. package/tests/workflow.test.ts +0 -111
  90. package/tsconfig.json +0 -14
@@ -1,607 +0,0 @@
1
- // eslint-disable-next-line eslint-comments/disable-enable-pair
2
- /* eslint-disable max-lines */
3
- import * as asyncHooksImpl from 'farrow-pipeline/asyncHooks.node';
4
- import {
5
- createContext,
6
- createContainer,
7
- createPipeline,
8
- createAsyncPipeline,
9
- usePipeline,
10
- useContainer,
11
- isPipeline,
12
- } from '../src';
13
- import { sleep } from './helpers';
14
-
15
- describe('createPipeline', () => {
16
- it('basic usage', async () => {
17
- type Input = {
18
- count: number;
19
- };
20
- type Output = PromiseLike<number> | number;
21
-
22
- const pipeline = createPipeline<Input, Output>();
23
-
24
- let list: number[] = [];
25
-
26
- pipeline.use((input, next) => {
27
- list.push(1);
28
- return next(input);
29
- });
30
-
31
- pipeline.use((input, next) => {
32
- list.push(2);
33
- return next(input);
34
- });
35
-
36
- pipeline.use((input, next) => {
37
- list.push(3);
38
- return next(input);
39
- });
40
-
41
- pipeline.use((input, next) => {
42
- if (input.count < 10) {
43
- return input.count + 1;
44
- }
45
-
46
- return next(input);
47
- });
48
-
49
- pipeline.use(input => {
50
- list.push(4);
51
- return input.count + 2;
52
- });
53
-
54
- const result0 = await pipeline.run({ count: 0 });
55
-
56
- expect(result0).toEqual(1);
57
- expect(list).toEqual([1, 2, 3]);
58
-
59
- list = [];
60
-
61
- const result1 = await pipeline.run({ count: 10 });
62
-
63
- expect(result1).toEqual(12);
64
- expect(list).toEqual([1, 2, 3, 4]);
65
- });
66
-
67
- it('can change input and output', async () => {
68
- const pipeline = createPipeline<number, Promise<number>>();
69
-
70
- let list: number[] = [];
71
-
72
- pipeline.use(async (input, next) => {
73
- list.push(input);
74
- const result = await next(input + 1);
75
- list.push(result);
76
- return result + 1;
77
- });
78
-
79
- pipeline.use(input => {
80
- list.push(input);
81
- return Promise.resolve(input + 1);
82
- });
83
-
84
- const result0 = await pipeline.run(0);
85
-
86
- expect(result0).toEqual(3);
87
- expect(list).toEqual([0, 1, 2]);
88
-
89
- list = [];
90
-
91
- const result1 = await pipeline.run(11);
92
-
93
- expect(result1).toEqual(14);
94
- expect(list).toEqual([11, 12, 13]);
95
- });
96
-
97
- it('supports hooks in async middleware', async () => {
98
- const Context0 = createContext(0);
99
-
100
- const pipeline = createAsyncPipeline<number, number>();
101
-
102
- const list: number[] = [];
103
-
104
- pipeline.use(async (input, next) => {
105
- const Context = Context0.use();
106
-
107
- list.push(Context.value);
108
-
109
- Context.value += 1;
110
-
111
- const result = await next(input);
112
-
113
- list.push(Context.value);
114
-
115
- return result;
116
- });
117
-
118
- pipeline.use(async (input, next) => {
119
- const Context = Context0.use();
120
-
121
- list.push(Context.value);
122
-
123
- Context.value += 2;
124
-
125
- const result = await next(input);
126
-
127
- list.push(Context.value);
128
-
129
- Context.value += 3;
130
-
131
- return result;
132
- });
133
-
134
- pipeline.use(input => {
135
- const Context = Context0.use();
136
- list.push(Context.value);
137
- Context.value += 1;
138
- return Promise.resolve(input + Context.value);
139
- });
140
-
141
- const result = await pipeline.run(10);
142
-
143
- expect(result).toEqual(14);
144
- expect(list).toEqual([0, 1, 3, 4, 7]);
145
- });
146
-
147
- it('can inject context', async () => {
148
- const TestContext = createContext(10);
149
-
150
- const pipeline = createPipeline<number, PromiseLike<number> | number>({
151
- contexts: { count: TestContext.create(100) },
152
- });
153
-
154
- pipeline.use(input => {
155
- const Context = TestContext.use();
156
- Context.value += input;
157
- return Context.value;
158
- });
159
-
160
- const result0 = await pipeline.run(20);
161
-
162
- expect(result0).toEqual(120);
163
-
164
- const container = createContainer({ count: TestContext.create(10) });
165
-
166
- const rseult1 = await pipeline.run(30, { container });
167
-
168
- expect(rseult1).toEqual(40);
169
-
170
- expect(container.read(TestContext)).toEqual(40);
171
- });
172
-
173
- it('should throw error if there are no middlewares in pipeline', async () => {
174
- const pipeline = createPipeline<number, PromiseLike<number> | number>();
175
-
176
- let error: unknown = null;
177
-
178
- try {
179
- await pipeline.run(1);
180
- } catch (e) {
181
- error = e;
182
- }
183
-
184
- expect(error === null).toBe(false);
185
- });
186
-
187
- it('should throw error if there are no middlewares returning value', async () => {
188
- const pipeline = createPipeline<number, PromiseLike<number> | number>();
189
-
190
- pipeline.use((input, next) => next(input));
191
-
192
- pipeline.use((input, next) => next(input));
193
-
194
- pipeline.use((input, next) => next(input));
195
-
196
- pipeline.use((input, next) => next(input));
197
-
198
- let error: unknown = null;
199
-
200
- try {
201
- await pipeline.run(1);
202
- } catch (e) {
203
- error = e;
204
- }
205
-
206
- expect(error === null).toBe(false);
207
- });
208
-
209
- it('should invoke onLast if there are no middlewares returned value', async () => {
210
- const pipeline = createPipeline<number, PromiseLike<number> | number>();
211
-
212
- const list: number[] = [];
213
-
214
- pipeline.use((input, next) => {
215
- list.push(1);
216
- return next(input);
217
- });
218
-
219
- pipeline.use((input, next) => {
220
- list.push(2);
221
- return next(input);
222
- });
223
-
224
- pipeline.use((input, next) => {
225
- list.push(3);
226
- return next(input);
227
- });
228
-
229
- pipeline.use((input, next) => {
230
- list.push(4);
231
- return next(input);
232
- });
233
-
234
- const result = await pipeline.run(1, { onLast: input => input + 4 });
235
-
236
- expect(result).toEqual(5);
237
- expect(list).toEqual([1, 2, 3, 4]);
238
- });
239
-
240
- it('can access current context in pipeline', () => {
241
- const Context0 = createContext(0);
242
- const Context1 = createContext(1);
243
-
244
- const pipeline = createPipeline<number, number>({
245
- contexts: {
246
- count0: Context0.create(10),
247
- count1: Context1.create(20),
248
- },
249
- });
250
-
251
- const list: boolean[] = [];
252
-
253
- pipeline.use(input => {
254
- const container = useContainer();
255
- const count0 = Context0.use().value;
256
- const count1 = Context1.use().value;
257
-
258
- list.push(container.read(Context0) === count0);
259
- list.push(container.read(Context1) === count1);
260
-
261
- return input;
262
- });
263
-
264
- const result = pipeline.run(0);
265
-
266
- expect(result).toEqual(0);
267
- expect(list).toEqual([true, true]);
268
- });
269
-
270
- it('should support multiple middlewares in pipeline.use', () => {
271
- const pipeline = createPipeline<number, number>();
272
-
273
- pipeline.use(
274
- (input, next) => next(input + 1),
275
- (input, next) => next(input + 1),
276
- (input, next) => next(input + 1),
277
- (input, next) => next(input + 1),
278
- input => input + 1,
279
- );
280
-
281
- const result = pipeline.run(0);
282
-
283
- expect(result).toBe(5);
284
- });
285
-
286
- it('should support the shape of { middleware } as arguments in pipeline.use', () => {
287
- const pipeline = createPipeline<number, number>();
288
-
289
- pipeline.use(
290
- { middleware: (input, next) => next(input + 1) },
291
- { middleware: (input, next) => next(input + 1) },
292
- { middleware: (input, next) => next(input + 1) },
293
- { middleware: (input, next) => next(input + 1) },
294
- input => input + 1,
295
- );
296
-
297
- const result = pipeline.run(0);
298
-
299
- expect(result).toBe(5);
300
- });
301
-
302
- it('should support pipeline.use(anotherPipeline) if their type is matched', () => {
303
- const StepContext = createContext(1);
304
-
305
- const pipeline0 = createPipeline<number, number>();
306
-
307
- const pipeline1 = createPipeline<number, number>();
308
-
309
- const steps = [] as number[];
310
-
311
- pipeline0.use((input, next) => {
312
- const step = StepContext.use();
313
- return next(input + step.value++);
314
- });
315
-
316
- pipeline0.use(pipeline1);
317
-
318
- pipeline1.use(input => {
319
- const step = StepContext.use();
320
- steps.push(step.value);
321
- return input + step.value;
322
- });
323
-
324
- const result0 = pipeline1.run(0);
325
- const result1 = pipeline0.run(0);
326
-
327
- expect(result0).toEqual(1);
328
- expect(result1).toEqual(3);
329
- expect(steps).toEqual([1, 2]);
330
- });
331
-
332
- describe('sync', () => {
333
- it('should support pipeline.use(anotherPipeline) if their type is matched', () => {
334
- const StepContext = createContext(1);
335
-
336
- const pipeline0 = createPipeline<number, number>();
337
-
338
- const pipeline1 = createPipeline<number, number>();
339
-
340
- const steps = [] as number[];
341
-
342
- pipeline0.use((input, next) => {
343
- const step = StepContext.use();
344
- return next(input + step.value++);
345
- });
346
-
347
- pipeline0.use(pipeline1);
348
-
349
- pipeline1.use(input => {
350
- const step = StepContext.use();
351
- steps.push(step.value);
352
- return input + step.value;
353
- });
354
-
355
- const result0 = pipeline1.run(0);
356
- const result1 = pipeline0.run(0);
357
-
358
- expect(result0).toEqual(1);
359
- expect(result1).toEqual(3);
360
- expect(steps).toEqual([1, 2]);
361
- });
362
-
363
- it('should throw error when add illegal middleware', () => {
364
- const pipeline = createPipeline<number, number>();
365
- expect(() => pipeline.use({} as any)).toThrowError();
366
- });
367
-
368
- it('can usePipeline in another pipeline', () => {
369
- const pipeline0 = createPipeline<string, string>();
370
- const pipeline1 = createPipeline<string, string>();
371
-
372
- pipeline0.use(input => `${input} from pipeline0`);
373
-
374
- pipeline1.use(input => {
375
- const runPipeline1 = usePipeline(pipeline0);
376
-
377
- const text = runPipeline1(' pipeline1');
378
-
379
- return input + text;
380
- });
381
-
382
- const result = pipeline1.run('run');
383
-
384
- expect(result).toEqual(`run pipeline1 from pipeline0`);
385
- });
386
-
387
- it('isPipeline', () => {
388
- const pipeline = createPipeline();
389
-
390
- expect(isPipeline(pipeline)).toBeTruthy();
391
- expect(isPipeline({})).toBeFalsy();
392
- expect(isPipeline('test')).toBeFalsy();
393
- expect(isPipeline(null)).toBeFalsy();
394
- });
395
-
396
- it('support hooks', async () => {
397
- const Context0 = createContext(0);
398
-
399
- const pipeline = createPipeline<number, PromiseLike<number> | number>();
400
-
401
- const list: number[] = [];
402
-
403
- pipeline.use((input, next) => {
404
- const Context = Context0.use();
405
-
406
- list.push(Context.value);
407
-
408
- Context.value += 1;
409
-
410
- return next(input);
411
- });
412
-
413
- pipeline.use((input, next) => {
414
- const Context = Context0.use();
415
-
416
- list.push(Context.value);
417
-
418
- Context.value += 2;
419
-
420
- return next(input);
421
- });
422
-
423
- pipeline.use(input => {
424
- const Context = Context0.use();
425
- list.push(Context.value);
426
- return input + Context.value;
427
- });
428
-
429
- const result = await pipeline.run(10);
430
-
431
- expect(result).toEqual(13);
432
- expect(list).toEqual([0, 1, 3]);
433
- });
434
-
435
- it('should throw error when all middlewares calling next() and onLast is not exist', () => {
436
- const pipeline = createPipeline<number, number>();
437
-
438
- pipeline.use((input, next) => next(input));
439
-
440
- expect(() => pipeline.run(0)).toThrowError();
441
- });
442
-
443
- it('should calling onLast when all middlewares calling next() and onLast is exist', () => {
444
- const pipeline = createPipeline<number, number>();
445
-
446
- pipeline.use((input, next) => next(input));
447
-
448
- expect(pipeline.run(0, { onLast: () => 10 })).toBe(10);
449
- });
450
- });
451
-
452
- describe('async', () => {
453
- it('support async pipeline', async () => {
454
- const pipeline = createAsyncPipeline<number, number>();
455
-
456
- pipeline.use((input, next) => next(input + 1));
457
-
458
- let i = 0;
459
-
460
- pipeline.use((input, next) => {
461
- const count = ++i;
462
- return next(input + count);
463
- });
464
-
465
- pipeline.use(async (input, next) => {
466
- // eslint-disable-next-line @typescript-eslint/await-thenable
467
- const count = await ++i;
468
- return next(input + count);
469
- });
470
-
471
- pipeline.use(input => input);
472
-
473
- expect(i).toBe(0);
474
-
475
- const result0 = await pipeline.run(0);
476
-
477
- expect(result0).toBe(4);
478
- expect(i).toBe(2);
479
-
480
- const result1 = await pipeline.run(-4);
481
-
482
- expect(result1).toBe(4);
483
- expect(i).toBe(4);
484
- });
485
-
486
- it('should support pipeline.use(anotherPipeline) if their type is matched', async () => {
487
- const StepContext = createContext(1);
488
-
489
- const pipeline0 = createAsyncPipeline<number, number>();
490
-
491
- const pipeline1 = createAsyncPipeline<number, number>();
492
-
493
- const steps = [] as number[];
494
-
495
- pipeline0.use(async (input, next) => {
496
- await sleep(0);
497
- const step = StepContext.use();
498
- return next(input + step.value++);
499
- });
500
-
501
- pipeline0.use(pipeline1);
502
-
503
- pipeline1.use(async input => {
504
- const step = StepContext.use();
505
- steps.push(step.value);
506
- await sleep(0);
507
- return input + step.value;
508
- });
509
-
510
- asyncHooksImpl.enable();
511
- const result0 = await pipeline1.run(0);
512
- const result1 = await pipeline0.run(0);
513
- asyncHooksImpl.disable();
514
-
515
- expect(result0).toEqual(1);
516
- expect(result1).toEqual(3);
517
- expect(steps).toEqual([1, 2]);
518
- });
519
-
520
- it('should throw error when add illegal middleware', () => {
521
- const pipeline = createAsyncPipeline<number, number>();
522
- expect(() => pipeline.use({} as any)).toThrowError();
523
- });
524
-
525
- it('can useAsyncPipeline in another pipeline', async () => {
526
- const pipeline0 = createAsyncPipeline<string, string>();
527
- const pipeline1 = createAsyncPipeline<string, string>();
528
-
529
- pipeline0.use(async input => `${input} from pipeline0`);
530
-
531
- pipeline1.use(async input => {
532
- const runPipeline1 = usePipeline(pipeline0);
533
-
534
- const text = await runPipeline1(' pipeline1');
535
-
536
- return input + text;
537
- });
538
-
539
- const result = await pipeline1.run('run');
540
-
541
- expect(result).toEqual(`run pipeline1 from pipeline0`);
542
- });
543
-
544
- it('support hooks', async () => {
545
- const pipeline = createAsyncPipeline<number, number>();
546
-
547
- const Count = createContext({ count: 10 });
548
-
549
- const incre = async () => {
550
- await sleep(0);
551
- Count.set({ count: Count.assert().count + 1 });
552
- };
553
-
554
- const list = [] as { count: number }[];
555
-
556
- pipeline.use(async (input, next) => {
557
- const before = Count.get();
558
- const result = await next(input);
559
-
560
- await incre();
561
-
562
- const after = Count.get();
563
-
564
- list.push(before, after);
565
-
566
- return result;
567
- });
568
-
569
- pipeline.use(async input => {
570
- await sleep(0);
571
- await incre();
572
-
573
- return input + Count.get().count;
574
- });
575
-
576
- const container = createContainer({ count: Count });
577
-
578
- asyncHooksImpl.enable();
579
-
580
- const result0 = await pipeline.run(10, { container });
581
-
582
- asyncHooksImpl.disable();
583
-
584
- expect(result0).toEqual(21);
585
- expect(list).toEqual([{ count: 10 }, { count: 12 }]);
586
- });
587
-
588
- it('should throw error when all middlewares calling next() and onLast is not exist', async () => {
589
- const pipeline = createAsyncPipeline<number, number>();
590
-
591
- // eslint-disable-next-line promise/prefer-await-to-then
592
- pipeline.use((input, next) => Promise.resolve().then(() => next(input)));
593
-
594
- await expect(() => pipeline.run(0)).rejects.toThrowError();
595
- });
596
-
597
- it('should calling onLast when all middlewares calling next() and onLast is exist', async () => {
598
- const pipeline = createAsyncPipeline<number, number>();
599
-
600
- pipeline.use((input, next) => next(input));
601
-
602
- const result = await pipeline.run(0, { onLast: () => 10 });
603
-
604
- expect(result).toBe(10);
605
- });
606
- });
607
- });