@milkio/stargate 1.0.0-alpha.0 → 1.0.0-alpha.1
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.
- package/.publish/publish.json +0 -0
- package/README.md +1 -1
- package/index.ts +40 -87
- package/package.json +1 -1
- package/tsconfig.json +0 -0
package/.publish/publish.json
CHANGED
|
File without changes
|
package/README.md
CHANGED
package/index.ts
CHANGED
|
@@ -3,15 +3,8 @@ import { TSON } from "@southern-aurora/tson";
|
|
|
3
3
|
export type MilkioStargateOptions = {
|
|
4
4
|
baseUrl: string | (() => string) | (() => Promise<string>);
|
|
5
5
|
timeout?: number;
|
|
6
|
-
// middlewares?: () => Array<MiddlewareOptions & { isMiddleware: true }>;
|
|
7
6
|
fetch?: typeof fetch;
|
|
8
7
|
abort?: typeof AbortController;
|
|
9
|
-
// storage?: {
|
|
10
|
-
// getItem: (key: string) => string | null | Promise<string | null>;
|
|
11
|
-
// setItem: (key: string, value: string) => void | Promise<void>;
|
|
12
|
-
// removeItem: (key: string) => void | Promise<void>;
|
|
13
|
-
// };
|
|
14
|
-
// memoryStorage?: boolean;
|
|
15
8
|
};
|
|
16
9
|
|
|
17
10
|
export type Mixin<T, U> = U & Omit<T, keyof U>;
|
|
@@ -52,39 +45,6 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
52
45
|
if (typeof baseUrl === "function") baseUrl = await baseUrl();
|
|
53
46
|
if (baseUrl.endsWith("/")) baseUrl = baseUrl.slice(0, -1);
|
|
54
47
|
|
|
55
|
-
// if (options.middlewares) {
|
|
56
|
-
// const middlewares = [...builtinMiddlewares, ...options.middlewares()];
|
|
57
|
-
|
|
58
|
-
// const push = (index: number, middlewares: Array<any>, middleware: any) => {
|
|
59
|
-
// const id = ++guid;
|
|
60
|
-
// middlewares.push({ id, index, middleware });
|
|
61
|
-
// return () =>
|
|
62
|
-
// middlewares.splice(
|
|
63
|
-
// middlewares.findIndex((v) => v.id === id),
|
|
64
|
-
// 1,
|
|
65
|
-
// );
|
|
66
|
-
// };
|
|
67
|
-
|
|
68
|
-
// const _middlewareHandler = (index: number, options: MiddlewareOptions) => {
|
|
69
|
-
// if (options.bootstrap) push(index, _bootstrapMiddlewares, options.bootstrap);
|
|
70
|
-
// if (options.beforeExecute) push(index, _beforeExecuteMiddlewares, options.beforeExecute);
|
|
71
|
-
// if (options.afterExecute) push(index, _afterExecuteMiddlewares, options.afterExecute);
|
|
72
|
-
// };
|
|
73
|
-
|
|
74
|
-
// for (let index = 0; index < middlewares.length; index++) {
|
|
75
|
-
// const middlewareOptions = middlewares[index];
|
|
76
|
-
// _middlewareHandler(index, middlewareOptions);
|
|
77
|
-
// }
|
|
78
|
-
|
|
79
|
-
// _bootstrapMiddlewares.sort((a, b) => a.index - b.index);
|
|
80
|
-
// _beforeExecuteMiddlewares.sort((a, b) => a.index - b.index);
|
|
81
|
-
// _afterExecuteMiddlewares.sort((a, b) => b.index - a.index);
|
|
82
|
-
|
|
83
|
-
// for (const m of _bootstrapMiddlewares) {
|
|
84
|
-
// await m.middleware({ storage: options.storage as ClientStorage });
|
|
85
|
-
// }
|
|
86
|
-
// }
|
|
87
|
-
|
|
88
48
|
return baseUrl;
|
|
89
49
|
};
|
|
90
50
|
|
|
@@ -126,10 +86,6 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
126
86
|
if (options.headers["Accept"] === undefined) options.headers["Accept"] = "application/json";
|
|
127
87
|
if (options.headers["Content-Type"] === undefined) options.headers["Content-Type"] = "application/json";
|
|
128
88
|
|
|
129
|
-
// for (const m of _beforeExecuteMiddlewares) {
|
|
130
|
-
// await m.middleware({ path: path as string, params: options.params, headers: options.headers, storage: options.storage as ClientStorage });
|
|
131
|
-
// }
|
|
132
|
-
|
|
133
89
|
const body = TSON.stringify(options.params) ?? "";
|
|
134
90
|
|
|
135
91
|
let result: { value: Record<any, any> };
|
|
@@ -159,10 +115,6 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
159
115
|
return [error, null, { executeId: "unknown" }];
|
|
160
116
|
}
|
|
161
117
|
|
|
162
|
-
// for (const m of _afterExecuteMiddlewares) {
|
|
163
|
-
// await m.middleware({ path: path as string, storage: options.storage as ClientStorage, result });
|
|
164
|
-
// }
|
|
165
|
-
|
|
166
118
|
return [null, result.value.data, { executeId: result.value.executeId }] as any;
|
|
167
119
|
} else {
|
|
168
120
|
// stream
|
|
@@ -173,6 +125,7 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
173
125
|
const stacks: Map<
|
|
174
126
|
number,
|
|
175
127
|
{
|
|
128
|
+
done: boolean;
|
|
176
129
|
promise: Promise<IteratorResult<any>>;
|
|
177
130
|
resolve: (value: IteratorResult<any>) => void;
|
|
178
131
|
reject: (reason: any) => void;
|
|
@@ -201,11 +154,14 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
201
154
|
return;
|
|
202
155
|
} else {
|
|
203
156
|
const index = ++stacksIndex;
|
|
204
|
-
if (stacks.has(index))
|
|
205
|
-
|
|
157
|
+
if (stacks.has(index)) {
|
|
158
|
+
const stack = stacks.get(index);
|
|
159
|
+
stack!.done = true;
|
|
160
|
+
stack!.resolve({ done: false, value: TSON.parse(event.data) });
|
|
161
|
+
} else {
|
|
206
162
|
const stack = withResolvers<IteratorResult<any>>();
|
|
207
163
|
stack.resolve({ done: false, value: TSON.parse(event.data) });
|
|
208
|
-
stacks.set(index, stack);
|
|
164
|
+
stacks.set(index, { ...stack, done: false });
|
|
209
165
|
}
|
|
210
166
|
}
|
|
211
167
|
};
|
|
@@ -214,12 +170,10 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
214
170
|
|
|
215
171
|
async function create() {
|
|
216
172
|
curRequestController = new $abort();
|
|
217
|
-
curRequestController.signal.addEventListener("abort", () =>
|
|
173
|
+
curRequestController.signal.addEventListener("abort", () => {
|
|
174
|
+
iterator.return();
|
|
175
|
+
});
|
|
218
176
|
try {
|
|
219
|
-
// for (const m of _beforeExecuteMiddlewares) {
|
|
220
|
-
// await m.middleware({ path: path as string, params: eventOptions.params, headers: eventOptions.headers!, storage: options.storage as ClientStorage });
|
|
221
|
-
// }
|
|
222
|
-
|
|
223
177
|
const response = await $fetch(url, {
|
|
224
178
|
method: "POST",
|
|
225
179
|
headers: options!.headers,
|
|
@@ -253,13 +207,17 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
253
207
|
next(): Promise<IteratorResult<unknown>> {
|
|
254
208
|
const index = ++iteratorIndex;
|
|
255
209
|
if (stacks.has(index - 2)) stacks.delete(index - 2);
|
|
256
|
-
if (stacks.has(index)) {
|
|
257
|
-
|
|
258
|
-
|
|
210
|
+
if (!stacks.has(index) && !curRequestController.signal.aborted) {
|
|
211
|
+
const stack = withResolvers<IteratorResult<any>>();
|
|
212
|
+
stacks.set(index, { ...stack, done: false });
|
|
213
|
+
return stack.promise;
|
|
214
|
+
}
|
|
215
|
+
if (!stacks.has(index) && curRequestController.signal.aborted) {
|
|
259
216
|
const stack = withResolvers<IteratorResult<any>>();
|
|
260
|
-
|
|
217
|
+
stack.resolve({ done: true, value: undefined });
|
|
261
218
|
return stack.promise;
|
|
262
219
|
}
|
|
220
|
+
return stacks.get(index)!.promise;
|
|
263
221
|
},
|
|
264
222
|
async return(): Promise<IteratorResult<void>> {
|
|
265
223
|
if (!curRequestController.signal.aborted) curRequestController.abort();
|
|
@@ -277,6 +235,11 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
277
235
|
data: err,
|
|
278
236
|
},
|
|
279
237
|
};
|
|
238
|
+
for (const [_index, stack] of stacks) {
|
|
239
|
+
if (stack.done) continue;
|
|
240
|
+
stack.done = true;
|
|
241
|
+
stack.resolve({ done: true, value: undefined });
|
|
242
|
+
}
|
|
280
243
|
if (!curRequestController.signal.aborted) curRequestController.abort();
|
|
281
244
|
for (const [_, iterator] of stacks) iterator.resolve({ done: true, value: undefined });
|
|
282
245
|
return { done: true, value: undefined };
|
|
@@ -307,6 +270,7 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
307
270
|
const stacks: Map<
|
|
308
271
|
number,
|
|
309
272
|
{
|
|
273
|
+
done: boolean;
|
|
310
274
|
promise: Promise<IteratorResult<any>>;
|
|
311
275
|
resolve: (value: IteratorResult<any>) => void;
|
|
312
276
|
reject: (reason: any) => void;
|
|
@@ -314,15 +278,16 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
314
278
|
> = new Map();
|
|
315
279
|
let stacksIndex: number = 0;
|
|
316
280
|
let iteratorIndex: number = 0;
|
|
317
|
-
let streamResult: any = undefined;
|
|
318
281
|
|
|
319
282
|
const onmessage = (event: EventSourceMessage) => {
|
|
320
283
|
const index = ++stacksIndex;
|
|
321
|
-
if (stacks.has(index))
|
|
322
|
-
|
|
284
|
+
if (stacks.has(index)) {
|
|
285
|
+
const stack = stacks.get(index);
|
|
286
|
+
stack!.resolve({ done: false, value: TSON.parse(event.data) });
|
|
287
|
+
} else {
|
|
323
288
|
const stack = withResolvers<IteratorResult<any>>();
|
|
324
289
|
stack.resolve({ done: false, value: TSON.parse(event.data) });
|
|
325
|
-
stacks.set(index, stack);
|
|
290
|
+
stacks.set(index, { ...stack, done: false });
|
|
326
291
|
}
|
|
327
292
|
};
|
|
328
293
|
|
|
@@ -330,7 +295,9 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
330
295
|
|
|
331
296
|
async function create() {
|
|
332
297
|
curRequestController = new $abort();
|
|
333
|
-
curRequestController.signal.addEventListener("abort", () =>
|
|
298
|
+
curRequestController.signal.addEventListener("abort", () => {
|
|
299
|
+
iterator.return();
|
|
300
|
+
});
|
|
334
301
|
try {
|
|
335
302
|
const response = await $fetch(`${baseUrl}/$subscribe`, {
|
|
336
303
|
method: "POST",
|
|
@@ -360,13 +327,17 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
360
327
|
next(): Promise<IteratorResult<unknown>> {
|
|
361
328
|
const index = ++iteratorIndex;
|
|
362
329
|
if (stacks.has(index - 2)) stacks.delete(index - 2);
|
|
363
|
-
if (stacks.has(index)) {
|
|
364
|
-
return stacks.get(index)!.promise;
|
|
365
|
-
} else {
|
|
330
|
+
if (!stacks.has(index) && !curRequestController.signal.aborted) {
|
|
366
331
|
const stack = withResolvers<IteratorResult<any>>();
|
|
367
|
-
stacks.set(index, stack);
|
|
332
|
+
stacks.set(index, { ...stack, done: false });
|
|
368
333
|
return stack.promise;
|
|
369
334
|
}
|
|
335
|
+
if (!stacks.has(index) && curRequestController.signal.aborted) {
|
|
336
|
+
const stack = withResolvers<IteratorResult<any>>();
|
|
337
|
+
stack.resolve({ done: true, value: undefined });
|
|
338
|
+
return stack.promise;
|
|
339
|
+
}
|
|
340
|
+
return stacks.get(index)!.promise;
|
|
370
341
|
},
|
|
371
342
|
async return(): Promise<IteratorResult<void>> {
|
|
372
343
|
if (!curRequestController.signal.aborted) curRequestController.abort();
|
|
@@ -374,15 +345,6 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
374
345
|
return { done: true, value: undefined };
|
|
375
346
|
},
|
|
376
347
|
async throw(err: any): Promise<IteratorResult<void>> {
|
|
377
|
-
streamResult = {
|
|
378
|
-
success: false,
|
|
379
|
-
fail: {
|
|
380
|
-
code: "NETWORK_ERROR",
|
|
381
|
-
message: "Network Error",
|
|
382
|
-
fromClient: true,
|
|
383
|
-
data: err,
|
|
384
|
-
},
|
|
385
|
-
};
|
|
386
348
|
if (!curRequestController.signal.aborted) curRequestController.abort();
|
|
387
349
|
for (const [_, iterator] of stacks) iterator.resolve({ done: true, value: undefined });
|
|
388
350
|
return { done: true, value: undefined };
|
|
@@ -430,15 +392,6 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
430
392
|
return stargate;
|
|
431
393
|
}
|
|
432
394
|
|
|
433
|
-
// export function defineMiddleware(options: MiddlewareOptions): () => MiddlewareOptions & { isMiddleware: true } {
|
|
434
|
-
// return () => ({
|
|
435
|
-
// ...options,
|
|
436
|
-
// isMiddleware: true,
|
|
437
|
-
// });
|
|
438
|
-
// }
|
|
439
|
-
|
|
440
|
-
let guid = 0;
|
|
441
|
-
|
|
442
395
|
export type ExecuteStreamOptions = {
|
|
443
396
|
headers?: Record<string, string>;
|
|
444
397
|
timeout?: number;
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
|
File without changes
|