@ricsam/isolate-fetch 0.1.1 → 0.1.3

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.
@@ -1,212 +0,0 @@
1
- import { test, describe, beforeEach, afterEach } from "node:test";
2
- import assert from "node:assert";
3
- import ivm from "isolated-vm";
4
- import { setupFetch, clearAllInstanceState, type FetchHandle } from "./index.ts";
5
- import { setupCore } from "@ricsam/isolate-core";
6
-
7
- describe("FormData Integration", () => {
8
- let isolate: ivm.Isolate;
9
- let context: ivm.Context;
10
- let fetchHandle: FetchHandle;
11
-
12
- beforeEach(async () => {
13
- isolate = new ivm.Isolate();
14
- context = await isolate.createContext();
15
- clearAllInstanceState();
16
- // Setup core first (provides Blob, File, etc.)
17
- await setupCore(context);
18
- fetchHandle = await setupFetch(context);
19
- });
20
-
21
- afterEach(() => {
22
- fetchHandle.dispose();
23
- context.release();
24
- isolate.dispose();
25
- });
26
-
27
- test("FormData handler works with fetch", async () => {
28
- context.evalSync(`
29
- serve({
30
- async fetch(request, server) {
31
- const formData = await request.formData();
32
- const file = formData.get("file");
33
- if (!file || typeof file === "string") {
34
- return Response.json({ error: "No file provided" }, { status: 400 });
35
- }
36
- return Response.json({
37
- success: true,
38
- name: file.name,
39
- size: file.size,
40
- type: file.type
41
- });
42
- }
43
- });
44
- `);
45
-
46
- // Create multipart/form-data request
47
- const boundary = "----TestBoundary123";
48
- const body = [
49
- `------TestBoundary123`,
50
- `Content-Disposition: form-data; name="file"; filename="test.txt"`,
51
- `Content-Type: text/plain`,
52
- ``,
53
- `Hello World`,
54
- `------TestBoundary123--`,
55
- ].join("\r\n");
56
-
57
- const response = await fetchHandle.dispatchRequest(
58
- new Request("http://localhost/api/upload", {
59
- method: "POST",
60
- headers: {
61
- "Content-Type": `multipart/form-data; boundary=${boundary}`,
62
- },
63
- body: body,
64
- })
65
- );
66
-
67
- assert.strictEqual(response.status, 200);
68
- const data = await response.json();
69
- assert.strictEqual(data.success, true);
70
- assert.strictEqual(data.name, "test.txt");
71
- });
72
-
73
- test("FormData with text fields", async () => {
74
- context.evalSync(`
75
- serve({
76
- async fetch(request, server) {
77
- const formData = await request.formData();
78
- const name = formData.get("name");
79
- const email = formData.get("email");
80
-
81
- return Response.json({
82
- name,
83
- email
84
- });
85
- }
86
- });
87
- `);
88
-
89
- // Create multipart/form-data request with text fields
90
- const boundary = "----TestBoundary456";
91
- const body = [
92
- `------TestBoundary456`,
93
- `Content-Disposition: form-data; name="name"`,
94
- ``,
95
- `John Doe`,
96
- `------TestBoundary456`,
97
- `Content-Disposition: form-data; name="email"`,
98
- ``,
99
- `john@example.com`,
100
- `------TestBoundary456--`,
101
- ].join("\r\n");
102
-
103
- const response = await fetchHandle.dispatchRequest(
104
- new Request("http://localhost/api/submit", {
105
- method: "POST",
106
- headers: {
107
- "Content-Type": `multipart/form-data; boundary=${boundary}`,
108
- },
109
- body: body,
110
- })
111
- );
112
-
113
- assert.strictEqual(response.status, 200);
114
- const data = await response.json();
115
- assert.strictEqual(data.name, "John Doe");
116
- assert.strictEqual(data.email, "john@example.com");
117
- });
118
-
119
- test("FormData with multiple files", async () => {
120
- context.evalSync(`
121
- serve({
122
- async fetch(request, server) {
123
- const formData = await request.formData();
124
- const files = formData.getAll("files");
125
-
126
- return Response.json({
127
- count: files.length,
128
- names: files.map(f => typeof f === 'string' ? f : f.name)
129
- });
130
- }
131
- });
132
- `);
133
-
134
- // Create multipart/form-data request with multiple files
135
- const boundary = "----TestBoundary789";
136
- const body = [
137
- `------TestBoundary789`,
138
- `Content-Disposition: form-data; name="files"; filename="file1.txt"`,
139
- `Content-Type: text/plain`,
140
- ``,
141
- `Content of file 1`,
142
- `------TestBoundary789`,
143
- `Content-Disposition: form-data; name="files"; filename="file2.txt"`,
144
- `Content-Type: text/plain`,
145
- ``,
146
- `Content of file 2`,
147
- `------TestBoundary789--`,
148
- ].join("\r\n");
149
-
150
- const response = await fetchHandle.dispatchRequest(
151
- new Request("http://localhost/api/upload-multiple", {
152
- method: "POST",
153
- headers: {
154
- "Content-Type": `multipart/form-data; boundary=${boundary}`,
155
- },
156
- body: body,
157
- })
158
- );
159
-
160
- assert.strictEqual(response.status, 200);
161
- const data = await response.json();
162
- assert.strictEqual(data.count, 2);
163
- assert.deepStrictEqual(data.names, ["file1.txt", "file2.txt"]);
164
- });
165
-
166
- test("FormData file content can be read", async () => {
167
- context.evalSync(`
168
- serve({
169
- async fetch(request, server) {
170
- const formData = await request.formData();
171
- const file = formData.get("file");
172
- if (!file || typeof file === "string") {
173
- return Response.json({ error: "No file provided" }, { status: 400 });
174
- }
175
-
176
- const content = await file.text();
177
-
178
- return Response.json({
179
- name: file.name,
180
- content: content
181
- });
182
- }
183
- });
184
- `);
185
-
186
- // Create multipart/form-data request
187
- const boundary = "----TestBoundaryContent";
188
- const body = [
189
- `------TestBoundaryContent`,
190
- `Content-Disposition: form-data; name="file"; filename="message.txt"`,
191
- `Content-Type: text/plain`,
192
- ``,
193
- `Hello from file content!`,
194
- `------TestBoundaryContent--`,
195
- ].join("\r\n");
196
-
197
- const response = await fetchHandle.dispatchRequest(
198
- new Request("http://localhost/api/read", {
199
- method: "POST",
200
- headers: {
201
- "Content-Type": `multipart/form-data; boundary=${boundary}`,
202
- },
203
- body: body,
204
- })
205
- );
206
-
207
- assert.strictEqual(response.status, 200);
208
- const data = await response.json();
209
- assert.strictEqual(data.name, "message.txt");
210
- assert.strictEqual(data.content, "Hello from file content!");
211
- });
212
- });