koajs / koa-range Goto Github PK
View Code? Open in Web Editor NEW[MAINTAINERS WANTED] range request implementation for koa, see http://tools.ietf.org/html/rfc7233
[MAINTAINERS WANTED] range request implementation for koa, see http://tools.ietf.org/html/rfc7233
This project has not been updated for 4 years, and it may not be updated again in the future. I suggest creating another branch for continued maintenance.
transfer to me and i can transfer it in if you can't transfer it in yourself
If you use"app.use(range);"multiple times, the range request fails.
import Koa from "koa";
const app = new Koa();
import range from "koa-range";
import serveIndex from "koa2-serve-index";
import { publicpath } from "./publicpath.js";
app.use(range);app.use(range);
app.use(serveIndex(publicpath, { hidden: true }));
GET / HTTP/1.1
Host: localhost:4000
Connection: keep-alive
sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"
cache-control: no-cache
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36
Accept: */*
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4000/
Accept-Encoding: identity
Accept-Language: zh-CN,zh;q=0.9
Range: bytes=200-500
HTTP/1.1 206 Partial Content
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
Vary: Origin, Accept-Encoding
Content-Type: text/html; charset=utf-8
Content-Length: 301
ETag: "253d-9gLiUOyMqeHE6kv7oLd9+m5DLjI"
Content-Range: bytes 200-500/301
Date: Tue, 22 Jun 2021 04:37:05 GMT
Connection: keep-alive
Keep-Alive: timeout=5
The browser reports an error as follows.
GET http://localhost:4000/ net::ERR_CONTENT_LENGTH_MISMATCH 206 (Partial Content)
If the starting point of the range request is greater than the maximum length of the content, the response status 416 shall be returned.
GET / HTTP/1.1
Host: localhost:4000
Connection: keep-alive
Cache-Control: no-cache
sec-ch-ua: " Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"
DNT: 1
accept-language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
upgrade-insecure-requests: 1
if-none-match: "253b-oDUrS8IQyJRM9EiG2CmtiW6ahlM"
range: bytes=9999999-
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4000/
Accept-Encoding: identity
HTTP/1.1 206 Partial Content
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
Vary: Origin, Accept-Encoding
Content-Type: text/html; charset=utf-8
Content-Length: -9990468
ETag: "253b-pRYhHemHVMhhBMOOkvWAjABIxIE"
Content-Range: bytes 9999999-9530/9531
Date: Tue, 22 Jun 2021 05:59:36 GMT
Connection: keep-alive
Keep-Alive: timeout=5
I found that some video player clients will send out requests for video files beyond the scope.
E.g. PotPlayer
As the title says, would be great to have a branch (and npm version published) with support for koa version 2.
I'm preparing a PR
With small video (mp4) files, chrome has no issues. However if I use a 34 mbish sort of a video file. It will sometimes play the first 2 secs, like (5%) of the file or just stay stuck in limbo and not respond to the request. The code I have is pretty simple:
import fs from 'fs';
import Koa from 'koa';
import koaLogger from 'koa-logger';
import koaRange from 'koa-range';
const app = new Koa();
const router = new koaRouter();
app
.use(koaLogger())
.use(koaRange)
/*
* TEST VIDEO STREAM
*/
router.get('/test', async (ctx, next) => {
ctx.type = 'video/mp4';
ctx.body = fs.createReadStream(__dirname + '/test.mp4');
});
app
.use(router.routes())
.use(router.allowedMethods())
.listen('3000');
It works fine on firefox, but I think firefox doesn't even use ranges for video. It also works fine when koaRange is removed as a middleware but then it would not seek, as expected. Using the latest version of koa and koa-range
I'm using koa-range via local-web-server to serve up small slices (e.g. 12kb) of a large (12GB) file. The response time is very slow - on the order of 30+ seconds.
I'm not at all familiar with node, but I'm somewhat suspicious of this line, which seems to read and discard from the very beginning of the file.
rawBody = rawBody.pipe(slice(start, end + 1));
That rawBody
seems to come from this line in koa-send
:
ctx.body = fs.createReadStream(path)
It might be better if we can find a way to stream only the requested portion of the data , rather than streaming the entire stream through the slice
function.
e.g. somewhere in koa-send:
ctx.body = fs.createReadStream(path, { start, end });
I'm using koa-range
with koa-better-static
, and the user is making a request with
if-modified-since:Fri, 01 Apr 2016 18:21:16 GMT
range:bytes=15110-6381395
koa-better-static
returns 304 NOT MODIFIED
with an empty body because the file has not been modified since that date, but now koa-range
tries to access this.body.length
which throws an error as its dereferencing undefined
I've read through rfc7233, and I'm going to attempt re-writing koa-range to be more compliant with the spec. There are a lot of weird edge cases having to do with handling the presence of other http headers (e.g., If-Match, If-Range, etc.)
I'd also like to modernize the stream handling since we're relying on ancient node-isms.
I'd also like to drop the old unsupported versions of node (<= v12) but I'm not sure when to do this. It would be really nice to have as many fixes in place before dropping node versions.
Hi,
As I saw the project needs maintainers. I have free time for it and related experience ( already 10 years with javascript ):
koajs/static#207
https://github.com/koajs/router/graphs/contributors
If everything is okay, how can I help the project?
> User-Agent: facebookexternalhit/1.1
> Accept: */*
> Accept-Encoding: deflate, gzip
> Range: bytes=0-524287
>
< HTTP/1.1 206 Partial Content
< Date: Sat, 24 Feb 2018 06:27:21 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 524288
< Connection: keep-alive
< Accept-Ranges: bytes
< Content-Range: bytes 0-524287/12338
< x-readtime: 52
The Content-Length
should be the content's real size(https://github.com/koajs/koa-range/blob/master/index.js#L80) will be better when full content is smaller than the requested Range.
Also see: restlet/restlet-framework-java#604
This middleware causes videos to fail to stream in Chrome and Firefox.
Here's a minimal repo for reproducing the bug:
https://github.com/jmeyers91/koa-range-broken-video-streaming
Run node broken.js
and node notBroken.js
then open http://localhost:8080 and http://localhost:8081
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.