milkio 0.2.16 → 0.2.17
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/defines/define-http-handler.ts +66 -47
- package/package.json +1 -1
|
@@ -132,11 +132,10 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
132
132
|
let params: any;
|
|
133
133
|
try {
|
|
134
134
|
if (rawbody) params = JSON.parse(rawbody);
|
|
135
|
-
else
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
135
|
+
else params = undefined;
|
|
136
|
+
// Doing so may pose security risks, as attackers may trigger your requests through link redirection without being protected by browser CORS.
|
|
137
|
+
// Therefore, this feature was commented out after v0.3.0.
|
|
138
|
+
// else if (request.request.method === 'GET' && fullurl.searchParams.get('params')) { params = JSON.parse(decodeURIComponent(fullurl.searchParams.get('params')!)); }
|
|
140
139
|
} catch (error) {
|
|
141
140
|
const logger = useLogger(executeId);
|
|
142
141
|
logger.log("TIP: body is not json, the content is not empty, but the content is not in a valid JSON format. The original content value can be retrieved via request.request.text()");
|
|
@@ -172,62 +171,82 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
if (mode === "stream") {
|
|
175
|
-
const generator = (resultsRaw as any).$generator as AsyncGenerator;
|
|
176
174
|
let stream: ReadableStream;
|
|
177
175
|
let control: ReadableStreamDirectController | ReadableStreamDefaultController;
|
|
178
|
-
// SSE has a default timeout, which helps prevent memory leaks, especially when you are writing code with a while (true) loop
|
|
179
176
|
|
|
180
|
-
|
|
181
|
-
|
|
177
|
+
// Handshake failure (usually due to incorrect parameters)
|
|
178
|
+
if (resultsRaw.$type === "result" && resultsRaw.$result.success === false) {
|
|
182
179
|
stream = new ReadableStream({
|
|
183
|
-
|
|
184
|
-
async pull(controller: ReadableStreamDirectController) {
|
|
180
|
+
async pull(controller) {
|
|
185
181
|
control = controller;
|
|
186
|
-
|
|
187
|
-
for await (const value of generator) {
|
|
188
|
-
if (!request.request.signal.aborted) {
|
|
189
|
-
const result: string = JSON.stringify(TSON.encode(value));
|
|
190
|
-
controller.write(`data:${result}\n\n`);
|
|
191
|
-
} else {
|
|
192
|
-
generator.return(undefined);
|
|
193
|
-
controller.close();
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
} catch (error) {
|
|
197
|
-
controller.close();
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
182
|
+
controller.enqueue(`data:$MILKIO_FAIL@${JSON.stringify(TSON.encode(resultsRaw.$result))}\n\n`);
|
|
200
183
|
controller.close();
|
|
201
184
|
},
|
|
202
185
|
cancel() {
|
|
203
186
|
control.close();
|
|
204
187
|
},
|
|
205
|
-
}
|
|
188
|
+
});
|
|
206
189
|
} else {
|
|
207
|
-
//
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
190
|
+
// Handshake successful
|
|
191
|
+
const generator = (resultsRaw as any).$generator as AsyncGenerator;
|
|
192
|
+
// SSE has a default timeout, which helps prevent memory leaks, especially when you are writing code with a while (true) loop
|
|
193
|
+
|
|
194
|
+
if (global?.Bun) {
|
|
195
|
+
// bun
|
|
196
|
+
stream = new ReadableStream({
|
|
197
|
+
type: "direct",
|
|
198
|
+
async pull(controller: ReadableStreamDirectController) {
|
|
199
|
+
control = controller;
|
|
200
|
+
try {
|
|
201
|
+
controller.write(`data:$MILKIO_SUCC@${JSON.stringify(TSON.encode(resultsRaw.$result))}\n\n`);
|
|
202
|
+
for await (const value of generator) {
|
|
203
|
+
if (!request.request.signal.aborted) {
|
|
204
|
+
const result: string = JSON.stringify(TSON.encode(value));
|
|
205
|
+
controller.write(`data:${result}\n\n`);
|
|
206
|
+
} else {
|
|
207
|
+
generator.return(undefined);
|
|
208
|
+
controller.close();
|
|
209
|
+
}
|
|
219
210
|
}
|
|
211
|
+
} catch (error) {
|
|
212
|
+
const result = handleCatchError(error, executeId);
|
|
213
|
+
controller.write(`data:$MILKIO_FAIL@${JSON.stringify(TSON.encode(result))}\n\n`);
|
|
214
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
220
215
|
}
|
|
221
|
-
} catch (error) {
|
|
222
216
|
controller.close();
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
217
|
+
},
|
|
218
|
+
cancel() {
|
|
219
|
+
control.close();
|
|
220
|
+
},
|
|
221
|
+
} as unknown as UnderlyingByteSource);
|
|
222
|
+
} else {
|
|
223
|
+
// node.js or others
|
|
224
|
+
stream = new ReadableStream({
|
|
225
|
+
async pull(controller) {
|
|
226
|
+
control = controller;
|
|
227
|
+
try {
|
|
228
|
+
controller.enqueue(`data:$MILKIO_SUCC@${JSON.stringify(TSON.encode(resultsRaw.$result))}\n\n`);
|
|
229
|
+
for await (const value of generator) {
|
|
230
|
+
if (!request.request.signal.aborted) {
|
|
231
|
+
const result: string = JSON.stringify(TSON.encode(value));
|
|
232
|
+
controller.enqueue(`data:${result}\n\n`);
|
|
233
|
+
} else {
|
|
234
|
+
generator.return(undefined);
|
|
235
|
+
controller.close();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
} catch (error) {
|
|
239
|
+
const result = handleCatchError(error, executeId);
|
|
240
|
+
controller.enqueue(`data:$MILKIO_FAIL@${JSON.stringify(TSON.encode(result))}\n\n`);
|
|
241
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
242
|
+
}
|
|
243
|
+
controller.close();
|
|
244
|
+
},
|
|
245
|
+
cancel() {
|
|
246
|
+
control.close();
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
}
|
|
231
250
|
}
|
|
232
251
|
detail.response.headers["Content-Type"] = "text/event-stream";
|
|
233
252
|
detail.response.headers["Cache-Control"] = "no-cache";
|