@milkio/stargate 1.0.0-alpha.94 → 1.0.0-alpha.97

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 (2) hide show
  1. package/index.ts +1 -200
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -331,7 +331,7 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
331
331
  }
332
332
  }
333
333
  },
334
- cookbook: {
334
+ __cookbook: {
335
335
  subscribe: async (baseUrl: string) => {
336
336
  const headers = {
337
337
  "Content-Type": "application/json",
@@ -461,205 +461,6 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
461
461
  }
462
462
  });
463
463
  },
464
- async requestRaw<
465
- T,
466
- Options extends {
467
- method: string;
468
- type?: "action" | "stream";
469
- params?: Record<any, any> | string;
470
- headers?: Record<string, string>;
471
- timeout?: number;
472
- },
473
- >(
474
- path: string,
475
- options: Options,
476
- ): Promise<
477
- Options["type"] extends "stream"
478
- ? // stream
479
- [Partial<Generated["rejectCode"]>, null, ExecuteResultsOption] | [null, AsyncGenerator<[Partial<Generated["rejectCode"]>, null] | [null, T], ExecuteResultsOption>]
480
- : // action
481
- [Partial<Generated["rejectCode"]>, null, ExecuteResultsOption] | [null, T, ExecuteResultsOption]
482
- > {
483
- if (options.headers === undefined) options.headers = {};
484
-
485
- const url = path;
486
-
487
- if (options.type !== "stream") {
488
- // action
489
- if (options.headers.Accept === undefined) options.headers.Accept = "application/json";
490
- if (options.headers["Content-Type"] === undefined) options.headers["Content-Type"] = "application/json";
491
- let result: { value: Record<any, any> };
492
-
493
- try {
494
- const body = TSON.stringify(options.params) ?? "";
495
- // biome-ignore lint/suspicious/noAsyncPromiseExecutor: <explanation>
496
- const response = await new Promise<string>(async (resolve, reject) => {
497
- const timeout = options?.timeout ?? options?.timeout ?? 6000;
498
- const timer = setTimeout(() => {
499
- reject([{ REQUEST_TIMEOUT: { timeout, message: `Execute timeout after ${timeout}ms.` } }, null]);
500
- }, timeout);
501
-
502
- try {
503
- const value = await (await $fetch(url, { method: "POST", body, headers: options.headers })).text();
504
- clearTimeout(timer);
505
- resolve(value);
506
- } catch (error) {
507
- reject(error);
508
- }
509
- });
510
- result = { value: TSON.parse(response) };
511
- } catch (error: any) {
512
- if (error?.[0]?.REQUEST_TIMEOUT) {
513
- return error;
514
- }
515
- const errorPined = { REQUEST_FAIL: error };
516
- return [errorPined, null, { executeId: "unknown" }];
517
- }
518
- if (result.value.success !== true) {
519
- const error: any = {};
520
- error[result.value.code] = result.value.reject ?? null;
521
- return [error, null, { executeId: "unknown" }];
522
- }
523
-
524
- return [null, result.value.data, { executeId: result.value.executeId }] as any;
525
- } else {
526
- // stream
527
- if (options.headers.Accept === undefined) options.headers.Accept = "text/event-stream";
528
- if (options.headers["Content-Type"] === undefined) options.headers["Content-Type"] = "application/json";
529
-
530
- const stacks: Map<
531
- number,
532
- {
533
- done: boolean;
534
- promise: Promise<IteratorResult<any>>;
535
- resolve: (value: IteratorResult<any>) => void;
536
- reject: (reason: any) => void;
537
- }
538
- > = new Map();
539
- let stacksIndex = 0;
540
- let iteratorIndex = 0;
541
- let streamResult: any;
542
- const streamResultFetched = withResolvers<undefined>();
543
-
544
- const timeout = stargateOptions?.timeout ?? options?.timeout ?? 6000;
545
- const timer = setTimeout(() => {
546
- streamResultFetched.reject([{ REQUEST_TIMEOUT: { timeout, message: `Execute timeout after ${timeout}ms.` } }, null, { executeId: "unknown" }]);
547
- }, timeout);
548
-
549
- const onmessage = (event: EventSourceMessage) => {
550
- if (event.data.startsWith("@")) {
551
- try {
552
- streamResult = TSON.parse(event.data.slice(1));
553
- streamResultFetched.resolve(undefined);
554
- clearTimeout(timer);
555
- } catch (error) {
556
- streamResultFetched.reject([{ REQUEST_FAIL: error }, null, { executeId: "unknown" }]);
557
- clearTimeout(timer);
558
- }
559
- } else {
560
- const index = ++stacksIndex;
561
- if (stacks.has(index)) {
562
- const stack = stacks.get(index);
563
- stack!.done = true;
564
- stack!.resolve({ done: false, value: TSON.parse(event.data) });
565
- } else {
566
- const stack = withResolvers<IteratorResult<any>>();
567
- stack.resolve({ done: false, value: TSON.parse(event.data) });
568
- stacks.set(index, { ...stack, done: false });
569
- }
570
- }
571
- };
572
-
573
- let curRequestController: AbortController;
574
-
575
- async function create() {
576
- curRequestController = new $abort();
577
- curRequestController.signal.addEventListener("abort", () => {
578
- iterator.return();
579
- });
580
- try {
581
- const body = TSON.stringify(options!.params) ?? "";
582
- const response = await $fetch(url, {
583
- method: "POST",
584
- headers: options!.headers,
585
- body,
586
- signal: curRequestController.signal,
587
- });
588
-
589
- const contentType = response.headers.get("Content-Type");
590
- if (!contentType?.startsWith("text/event-stream")) {
591
- throw new Error(`Expected content-type to be ${"text/event-stream"}, Actual: ${contentType}`);
592
- }
593
-
594
- await getBytes(response.body!, getLines(getMessages(onmessage)));
595
-
596
- await iterator.return();
597
- } catch (err) {
598
- if (!curRequestController.signal.aborted) curRequestController.abort();
599
- const error = { REQUEST_FAIL: err };
600
- await iterator.throw(err);
601
- streamResultFetched.reject([error, null, { executeId: "unknown" }]);
602
- }
603
- }
604
-
605
- void create();
606
-
607
- const iterator = {
608
- ...({
609
- next(): Promise<IteratorResult<unknown>> {
610
- const index = ++iteratorIndex;
611
- if (stacks.has(index - 2)) stacks.delete(index - 2);
612
- if (!stacks.has(index) && !curRequestController.signal.aborted) {
613
- const stack = withResolvers<IteratorResult<any>>();
614
- stacks.set(index, { ...stack, done: false });
615
- return stack.promise;
616
- }
617
- if (!stacks.has(index) && curRequestController.signal.aborted) {
618
- const stack = withResolvers<IteratorResult<any>>();
619
- stack.resolve({ done: true, value: undefined });
620
- return stack.promise;
621
- }
622
- return stacks.get(index)!.promise;
623
- },
624
- async return(): Promise<IteratorResult<void>> {
625
- if (!curRequestController.signal.aborted) curRequestController.abort();
626
- for (const [_, iterator] of stacks) iterator.resolve({ done: true, value: undefined });
627
- return { done: true, value: undefined };
628
- },
629
- async throw(err: any): Promise<IteratorResult<void>> {
630
- streamResult = {
631
- success: false,
632
- executeId: streamResult?.executeId ?? "",
633
- fail: {
634
- code: "NETWORK_ERROR",
635
- message: "Network Error",
636
- fromClient: true,
637
- data: err,
638
- },
639
- };
640
- for (const [_index, stack] of stacks) {
641
- if (stack.done) continue;
642
- stack.done = true;
643
- stack.resolve({ done: true, value: undefined });
644
- }
645
- if (!curRequestController.signal.aborted) curRequestController.abort();
646
- for (const [_, iterator] of stacks) iterator.resolve({ done: true, value: undefined });
647
- return { done: true, value: undefined };
648
- },
649
- } satisfies AsyncIterator<unknown>),
650
- [Symbol.asyncIterator]() {
651
- return this;
652
- },
653
- };
654
-
655
- try {
656
- await streamResultFetched.promise;
657
- return [null, iterator, { executeId: streamResult.executeId }] as any;
658
- } catch (error) {
659
- return error as any;
660
- }
661
- }
662
- },
663
464
  };
664
465
 
665
466
  return stargate;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@milkio/stargate",
3
3
  "type": "module",
4
- "version": "1.0.0-alpha.94",
4
+ "version": "1.0.0-alpha.97",
5
5
  "module": "index.ts",
6
6
  "dependencies": {
7
7
  "@southern-aurora/tson": "^2.0.2"