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.
@@ -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 if (request.request.method === 'GET' && fullurl.searchParams.get('params')) {
136
- params = JSON.parse(decodeURIComponent(fullurl.searchParams.get('params')!));
137
- } else {
138
- params = undefined;
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
- if (global?.Bun) {
181
- // bun
177
+ // Handshake failure (usually due to incorrect parameters)
178
+ if (resultsRaw.$type === "result" && resultsRaw.$result.success === false) {
182
179
  stream = new ReadableStream({
183
- type: "direct",
184
- async pull(controller: ReadableStreamDirectController) {
180
+ async pull(controller) {
185
181
  control = controller;
186
- try {
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
- } as unknown as UnderlyingByteSource);
188
+ });
206
189
  } else {
207
- // node.js or others
208
- stream = new ReadableStream({
209
- async pull(controller) {
210
- control = controller;
211
- try {
212
- for await (const value of generator) {
213
- if (!request.request.signal.aborted) {
214
- const result: string = JSON.stringify(TSON.encode(value));
215
- controller.enqueue(`data:${result}\n\n`);
216
- } else {
217
- generator.return(undefined);
218
- controller.close();
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
- throw error;
224
- }
225
- controller.close();
226
- },
227
- cancel() {
228
- control.close();
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";
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.2.16",
5
+ "version": "0.2.17",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },