Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safe checking of payload data type to prevent undefined errors #23

Merged
merged 1 commit into from Oct 13, 2022

Conversation

danawoodman
Copy link
Collaborator

@danawoodman danawoodman commented Oct 12, 2022

This fixed issues where Blob/FormData are being reported as undefined. It also makes our auto content-type/JSON.stringify code work more reliably.

Also added checking for Uint8Array. May eventually need to support more content types.

Thanks to @Crisfole for the tip on using typeof FormData !== "undefined"!

  • Export all common types so consumers can use them if needed

Closes #16
Closes #21

…fined issues

Thanks to @Crisfole for the tip on using `typeof FormData !== "undefined"`!

- Export all common types so consumers can use them if needed

Closes #16
Closes #21
Closes #22
@danawoodman danawoodman marked this pull request as draft October 12, 2022 22:01
@danawoodman danawoodman removed the request for review from kwhitley October 12, 2022 22:01
@kwhitley
Copy link
Owner

…fined issues

Thanks to @Crisfole for the tip on using typeof FormData !== "undefined"!

Solid!!

@kwhitley
Copy link
Owner

This a WIP still? See it's back in draft mode...

@kwhitley
Copy link
Owner

Either way, killer job you two!

@danawoodman danawoodman marked this pull request as ready for review October 13, 2022 15:48
@danawoodman
Copy link
Collaborator Author

@kwhitley sorry yeah I think it's ready for review. Def need to improve the type checking to be more robust tho

@danawoodman danawoodman changed the title Add explicitly and safe checking of payload data type to prevent unde… Add safe checking of payload data type to prevent undefined errors Oct 13, 2022
const is_blob = typeof Blob !== 'undefined' && payload instanceof Blob
const is_arraybuffer = typeof Uint8Array !== 'undefined' && payload instanceof Uint8Array
const passthrough = is_formdata || is_blob || is_arraybuffer

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gulp.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Granted this should minify somewhat...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make this smaller by keying on 'raw' post content vs stringify post content:

fetcher.post('/whatever', raw(myBlobHere))

A tiny function raw could simply tag the object with a symbol to skip the stringification...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I figured out a better approach! Using Object.getPrototypeOf() we can sniff the type of the object. If it is of type Object we can stringify it otherwise we pass through. Think this could work? Feels more robust than what were doing now

C1C0A4E6-230C-42D7-8A31-1C5E0C94F0EB

@kwhitley kwhitley merged commit b9777f4 into v0.x Oct 13, 2022
@kwhitley
Copy link
Owner

Ok, live in 0.7.1, let's cross our fingers! Thanks (yet again) Dana!!

@danawoodman danawoodman deleted the 16-21-22-ensure-definition-of-builtins branch October 14, 2022 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Blob is not defined in SvelteKit hook FormData is not defined
3 participants