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

You don't need to use arraybuffer and await #3

Closed
gornostay25 opened this issue Jul 12, 2022 · 4 comments
Closed

You don't need to use arraybuffer and await #3

gornostay25 opened this issue Jul 12, 2022 · 4 comments

Comments

@gornostay25
Copy link
Contributor

gornostay25 commented Jul 12, 2022

if (file.isFile) {
return new Response(await file.blob.arrayBuffer(), {
headers: { ...options.headers, "Content-Type": `${getMimeType(file.blob)}; charset=${options.charset}` },
});
}
// If it is a folder and it has an index
if (options.index && indexFile.exists) {
return new Response(await indexFile.blob.arrayBuffer(), {
headers: {
...options.headers,
"Content-Type": `${getMimeType(indexFile.blob)}; charset=${options.charset}`,
},
});
}

		if (file.isFile) {
			return new Response(file.blob, {
				headers: { ...options.headers, "Content-Type": `${getMimeType(file.blob)}; charset=${options.charset}` },
			});
		}

		// If it is a folder and it has an index
		if (options.index && indexFile.exists) {
			return new Response(indexFile.blob, {
				headers: {
					...options.headers,
					"Content-Type": `${getMimeType(indexFile.blob)}; charset=${options.charset}`,
				},
			});
		}

You don't need to use arraybuffer and await. I used it in my code because oven-sh/bun#616

https://github.com/gornostay25/svelte-adapter-bun/blob/b3e7449100216d2e72564d137c6c682c42c01507/src/sirv.js#L99-L102

@jakobbouchard
Copy link
Owner

Since I am planning on adding support for Accept-Ranges, shouldn't I keep using array buffers anyway?

@gornostay25
Copy link
Contributor Author

gornostay25 commented Jul 12, 2022

first

export default {
    fetch: async (req)=>{
      let fb = await Bun.file("myfile").arrayBuffer()
      
      return new Response(fb,{status:200})
    }
}

second

export default {
    fetch: async (req)=>{
      let fb = Bun.file("myfile")
      
      return new Response(fb,{status:200})
    }
}
~/sveltekit$ time curl localhost:3000 --output 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:-  0     0    0     0    0     0      0      0 --:--:--  0:0  0     0    0     0    0     0      0      0 --:--:--  0:0  0     0    0     0    0     0      0      0 --:--:--  0:0 21  135M   21 28.7M    0     0  8792k      0  0:00:15  0:0100  135M  100  135M    0     0  33.9M      0  0:00:03  0:00:03 --:--:-- 33.9M

real    0m4.067s
user    0m0.015s
sys 0m0.290s
~/sveltekit$ time curl localhost:3000 --output 1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:-100  135M  100  135M    0     0   184M      0 --:--:-- --:--:-- --:--:--  185M

real    0m0.749s
user    0m0.021s
sys 0m0.234s
~/sveltekit$ 

file -rw-r--r-- 1 runner runner 141994670 Jul 12 21:26 build/myfile +/- 140 MB

@gornostay25
Copy link
Contributor Author

gornostay25 commented Jul 12, 2022

But, if you dont use await it work faster

export default {
    fetch: async (req) => {
        return new Promise(rs => {
            Bun.file("myfile").arrayBuffer().then(arbf => {
                return new Response(arbf, {
                    status: 200
                })
            }).then(rs)
        })
    }
}
~/sveltekit$ time curl localhost:3000 --output 4
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:-  0     0    0     0    0     0      0      0 --:--:-- --:- 54  135M   54 74.0M    0     0  68.5M      0  0:00:01  0:0100  135M  100  135M    0     0  91.4M      0  0:00:01  0:00:01 --:--:-- 91.3M

real    0m1.551s
user    0m0.012s
sys 0m0.323s

@jakobbouchard
Copy link
Owner

Thanks! I merged your PR :)

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

No branches or pull requests

2 participants