Skip to content

Commit

Permalink
Merge pull request #6 from remix-run/jacob/root-of-clone-issue
Browse files Browse the repository at this point in the history
fix: fix root cause of clone issue
  • Loading branch information
mjackson authored May 10, 2022
2 parents 8a92b6f + 412eb2e commit 565f94e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
6 changes: 1 addition & 5 deletions packages/fetch/src/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,7 @@ class AsyncIterablePump {
controller.close();
break;
} else {
if (typeof next.value === 'string') {
controller.enqueue(new TextEncoder().encode(next.value));
} else {
controller.enqueue(next.value);
}
controller.enqueue(next.value);
}
}
} catch (error) {
Expand Down
9 changes: 5 additions & 4 deletions packages/fetch/src/utils/form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ export const getBoundary = () => randomBytes(8).toString('hex');
* @param {string} boundary
*/
export async function * formDataIterator(form, boundary) {
const encoder = new TextEncoder();
for (const [name, value] of form) {
yield getHeader(boundary, name, value);
yield encoder.encode(getHeader(boundary, name, value));

if (isBlob(value)) {
// @ts-ignore - we know our streams implement aysnc iteration
yield * value.stream();
} else {
yield value;
yield encoder.encode(value);
}

yield carriage;
yield encoder.encode(carriage);
}

yield getFooter(boundary);
yield encoder.encode(getFooter(boundary));
}

/**
Expand Down
40 changes: 37 additions & 3 deletions packages/fetch/test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('Request', () => {
});
});

it('should read formData after clone with FormData body',async () => {
it('should read formData after clone with web FormData body',async () => {
const ogFormData = new WebFormData();
ogFormData.append('a', 1);
ogFormData.append('b', 2);
Expand All @@ -303,11 +303,45 @@ describe('Request', () => {
});
const clonedRequest = request.clone();

return clonedRequest.formData().then(clonedFormData => {
return clonedRequest.formData().then(async clonedFormData => {
expect(clonedFormData.get('a')).to.equal("1");
expect(clonedFormData.get('b')).to.equal("2");
const file = clonedFormData.get('file')
expect(typeof file).to.equal("object");
if (typeof file !== "object") {
throw new Error("File is not an object");
}
expect(file.name).to.equal("file.txt");
expect(file.type).to.equal("application/octet-stream");
expect(file.size).to.equal(7);
expect(await file.text()).to.equal("content");
expect(file.lastModified).to.be.a('number');
});
});

it('should read formData after clone with node FormData body',async () => {
const ogFormData = new FormData();
ogFormData.append('a', '1');
ogFormData.append('b', '2');
ogFormData.append('file', Buffer.from('content'), { filename: "file.txt" });

const request = new Request(base, {
method: 'POST',
body: ogFormData,
});
const clonedRequest = request.clone();

return clonedRequest.formData().then(async clonedFormData => {
expect(clonedFormData.get('a')).to.equal("1");
expect(clonedFormData.get('b')).to.equal("2");
const file = clonedFormData.get('file')
if (typeof file !== "object") {
throw new Error("File is not an object");
}
expect(file.name).to.equal("file.txt");
expect(file.type).to.equal("text/plain");
expect(file.size).to.equal(7);
expect(await file.text()).to.equal("content");
expect(file.lastModified).to.be.a('number');
});
});
});

0 comments on commit 565f94e

Please sign in to comment.