ranges.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. const mocha = require('mocha');
  20. const expect = require('chai').expect;
  21. const mock_http = require('node-mocks-http');
  22. const stream_buffers = require('stream-buffers');
  23. const ranges = require('@joystream/storage-utils/ranges');
  24. describe('util/ranges', function()
  25. {
  26. describe('parse()', function()
  27. {
  28. it('should parse a full range', function()
  29. {
  30. // Range with unit
  31. var range = ranges.parse('bytes=0-100');
  32. expect(range.unit).to.equal('bytes');
  33. expect(range.range_str).to.equal('0-100');
  34. expect(range.ranges[0][0]).to.equal(0);
  35. expect(range.ranges[0][1]).to.equal(100);
  36. // Range without unit
  37. var range = ranges.parse('0-100');
  38. expect(range.unit).to.equal('bytes');
  39. expect(range.range_str).to.equal('0-100');
  40. expect(range.ranges[0][0]).to.equal(0);
  41. expect(range.ranges[0][1]).to.equal(100);
  42. // Range with custom unit
  43. //
  44. var range = ranges.parse('foo=0-100');
  45. expect(range.unit).to.equal('foo');
  46. expect(range.range_str).to.equal('0-100');
  47. expect(range.ranges[0][0]).to.equal(0);
  48. expect(range.ranges[0][1]).to.equal(100);
  49. });
  50. it('should error out on malformed strings', function()
  51. {
  52. expect(() => ranges.parse('foo')).to.throw();
  53. expect(() => ranges.parse('foo=bar')).to.throw();
  54. expect(() => ranges.parse('foo=100')).to.throw();
  55. expect(() => ranges.parse('foo=100-0')).to.throw();
  56. });
  57. it('should parse a range without end', function()
  58. {
  59. var range = ranges.parse('0-');
  60. expect(range.unit).to.equal('bytes');
  61. expect(range.range_str).to.equal('0-');
  62. expect(range.ranges[0][0]).to.equal(0);
  63. expect(range.ranges[0][1]).to.be.undefined;
  64. });
  65. it('should parse a range without start', function()
  66. {
  67. var range = ranges.parse('-100');
  68. expect(range.unit).to.equal('bytes');
  69. expect(range.range_str).to.equal('-100');
  70. expect(range.ranges[0][0]).to.be.undefined;
  71. expect(range.ranges[0][1]).to.equal(100);
  72. });
  73. it('should parse multiple ranges', function()
  74. {
  75. var range = ranges.parse('0-10,30-40,60-80');
  76. expect(range.unit).to.equal('bytes');
  77. expect(range.range_str).to.equal('0-10,30-40,60-80');
  78. expect(range.ranges[0][0]).to.equal(0);
  79. expect(range.ranges[0][1]).to.equal(10);
  80. expect(range.ranges[1][0]).to.equal(30);
  81. expect(range.ranges[1][1]).to.equal(40);
  82. expect(range.ranges[2][0]).to.equal(60);
  83. expect(range.ranges[2][1]).to.equal(80);
  84. });
  85. it('should merge overlapping ranges', function()
  86. {
  87. // Two overlapping ranges
  88. var range = ranges.parse('0-20,10-30');
  89. expect(range.unit).to.equal('bytes');
  90. expect(range.range_str).to.equal('0-20,10-30');
  91. expect(range.ranges).to.have.lengthOf(1);
  92. expect(range.ranges[0][0]).to.equal(0);
  93. expect(range.ranges[0][1]).to.equal(30);
  94. // Three overlapping ranges
  95. var range = ranges.parse('0-15,10-25,20-30');
  96. expect(range.unit).to.equal('bytes');
  97. expect(range.range_str).to.equal('0-15,10-25,20-30');
  98. expect(range.ranges).to.have.lengthOf(1);
  99. expect(range.ranges[0][0]).to.equal(0);
  100. expect(range.ranges[0][1]).to.equal(30);
  101. // Three overlapping ranges, reverse order
  102. var range = ranges.parse('20-30,10-25,0-15');
  103. expect(range.unit).to.equal('bytes');
  104. expect(range.range_str).to.equal('20-30,10-25,0-15');
  105. expect(range.ranges).to.have.lengthOf(1);
  106. expect(range.ranges[0][0]).to.equal(0);
  107. expect(range.ranges[0][1]).to.equal(30);
  108. // Adjacent ranges
  109. var range = ranges.parse('0-10,11-20');
  110. expect(range.unit).to.equal('bytes');
  111. expect(range.range_str).to.equal('0-10,11-20');
  112. expect(range.ranges).to.have.lengthOf(1);
  113. expect(range.ranges[0][0]).to.equal(0);
  114. expect(range.ranges[0][1]).to.equal(20);
  115. });
  116. it('should sort ranges', function()
  117. {
  118. var range = ranges.parse('10-30,0-5');
  119. expect(range.unit).to.equal('bytes');
  120. expect(range.range_str).to.equal('10-30,0-5');
  121. expect(range.ranges).to.have.lengthOf(2);
  122. expect(range.ranges[0][0]).to.equal(0);
  123. expect(range.ranges[0][1]).to.equal(5);
  124. expect(range.ranges[1][0]).to.equal(10);
  125. expect(range.ranges[1][1]).to.equal(30);
  126. });
  127. });
  128. describe('send()', function()
  129. {
  130. it('should send full files on request', function(done)
  131. {
  132. var res = mock_http.createResponse({});
  133. var in_stream = new stream_buffers.ReadableStreamBuffer({});
  134. // End-of-stream callback
  135. var opts = {
  136. name: 'test.file',
  137. type: 'application/test',
  138. };
  139. ranges.send(res, in_stream, opts, function(err) {
  140. expect(err).to.not.exist;
  141. // HTTP handling
  142. expect(res.statusCode).to.equal(200);
  143. expect(res.getHeader('content-type')).to.equal('application/test');
  144. expect(res.getHeader('content-disposition')).to.equal('inline');
  145. // Data/stream handling
  146. expect(res._isEndCalled()).to.be.true;
  147. expect(res._getBuffer().toString()).to.equal('Hello, world!');
  148. // Notify mocha that we're done.
  149. done();
  150. });
  151. // Simulate file stream
  152. in_stream.emit('open');
  153. in_stream.put('Hello, world!');
  154. in_stream.stop();
  155. });
  156. it('should send a range spanning the entire file on request', function(done)
  157. {
  158. var res = mock_http.createResponse({});
  159. var in_stream = new stream_buffers.ReadableStreamBuffer({});
  160. // End-of-stream callback
  161. var opts = {
  162. name: 'test.file',
  163. type: 'application/test',
  164. ranges: {
  165. ranges: [[0, 12]],
  166. }
  167. };
  168. ranges.send(res, in_stream, opts, function(err) {
  169. expect(err).to.not.exist;
  170. // HTTP handling
  171. expect(res.statusCode).to.equal(206);
  172. expect(res.getHeader('content-type')).to.equal('application/test');
  173. expect(res.getHeader('content-disposition')).to.equal('inline');
  174. expect(res.getHeader('content-range')).to.equal('bytes 0-12/*');
  175. expect(res.getHeader('content-length')).to.equal('13');
  176. // Data/stream handling
  177. expect(res._isEndCalled()).to.be.true;
  178. expect(res._getBuffer().toString()).to.equal('Hello, world!');
  179. // Notify mocha that we're done.
  180. done();
  181. });
  182. // Simulate file stream
  183. in_stream.emit('open');
  184. in_stream.put('Hello, world!');
  185. in_stream.stop();
  186. });
  187. it('should send a small range on request', function(done)
  188. {
  189. var res = mock_http.createResponse({});
  190. var in_stream = new stream_buffers.ReadableStreamBuffer({});
  191. // End-of-stream callback
  192. var opts = {
  193. name: 'test.file',
  194. type: 'application/test',
  195. ranges: {
  196. ranges: [[1, 11]], // Cut off first and last letter
  197. }
  198. };
  199. ranges.send(res, in_stream, opts, function(err) {
  200. expect(err).to.not.exist;
  201. // HTTP handling
  202. expect(res.statusCode).to.equal(206);
  203. expect(res.getHeader('content-type')).to.equal('application/test');
  204. expect(res.getHeader('content-disposition')).to.equal('inline');
  205. expect(res.getHeader('content-range')).to.equal('bytes 1-11/*');
  206. expect(res.getHeader('content-length')).to.equal('11');
  207. // Data/stream handling
  208. expect(res._isEndCalled()).to.be.true;
  209. expect(res._getBuffer().toString()).to.equal('ello, world');
  210. // Notify mocha that we're done.
  211. done();
  212. });
  213. // Simulate file stream
  214. in_stream.emit('open');
  215. in_stream.put('Hello, world!');
  216. in_stream.stop();
  217. });
  218. it('should send ranges crossing buffer boundaries', function(done)
  219. {
  220. var res = mock_http.createResponse({});
  221. var in_stream = new stream_buffers.ReadableStreamBuffer({
  222. chunkSize: 3, // Setting a chunk size smaller than the range should
  223. // not impact the test.
  224. });
  225. // End-of-stream callback
  226. var opts = {
  227. name: 'test.file',
  228. type: 'application/test',
  229. ranges: {
  230. ranges: [[1, 11]], // Cut off first and last letter
  231. }
  232. };
  233. ranges.send(res, in_stream, opts, function(err) {
  234. expect(err).to.not.exist;
  235. // HTTP handling
  236. expect(res.statusCode).to.equal(206);
  237. expect(res.getHeader('content-type')).to.equal('application/test');
  238. expect(res.getHeader('content-disposition')).to.equal('inline');
  239. expect(res.getHeader('content-range')).to.equal('bytes 1-11/*');
  240. expect(res.getHeader('content-length')).to.equal('11');
  241. // Data/stream handling
  242. expect(res._isEndCalled()).to.be.true;
  243. expect(res._getBuffer().toString()).to.equal('ello, world');
  244. // Notify mocha that we're done.
  245. done();
  246. });
  247. // Simulate file stream
  248. in_stream.emit('open');
  249. in_stream.put('Hello, world!');
  250. in_stream.stop();
  251. });
  252. it('should send multiple ranges', function(done)
  253. {
  254. var res = mock_http.createResponse({});
  255. var in_stream = new stream_buffers.ReadableStreamBuffer({});
  256. // End-of-stream callback
  257. var opts = {
  258. name: 'test.file',
  259. type: 'application/test',
  260. ranges: {
  261. ranges: [[1, 3], [5, 7]], // Slice two ranges out
  262. }
  263. };
  264. ranges.send(res, in_stream, opts, function(err) {
  265. expect(err).to.not.exist;
  266. // HTTP handling
  267. expect(res.statusCode).to.equal(206);
  268. expect(res.getHeader('content-type')).to.satisfy((str) => str.startsWith('multipart/byteranges'));
  269. expect(res.getHeader('content-disposition')).to.equal('inline');
  270. // Data/stream handling
  271. expect(res._isEndCalled()).to.be.true;
  272. // The buffer should contain both ranges, but with all the That would be
  273. // "ell" and ", w".
  274. // It's pretty elaborate having to parse the entire multipart response
  275. // body, so we'll restrict ourselves to finding lines within it.
  276. var body = res._getBuffer().toString();
  277. expect(body).to.contain('\r\nContent-Range: bytes 1-3/*\r\n');
  278. expect(body).to.contain('\r\nell\r\n');
  279. expect(body).to.contain('\r\nContent-Range: bytes 5-7/*\r\n');
  280. expect(body).to.contain('\r\n, w');
  281. // Notify mocha that we're done.
  282. done();
  283. });
  284. // Simulate file stream
  285. in_stream.emit('open');
  286. in_stream.put('Hello, world!');
  287. in_stream.stop();
  288. });
  289. it('should deal with ranges without end', function(done)
  290. {
  291. var res = mock_http.createResponse({});
  292. var in_stream = new stream_buffers.ReadableStreamBuffer({});
  293. // End-of-stream callback
  294. var opts = {
  295. name: 'test.file',
  296. type: 'application/test',
  297. ranges: {
  298. ranges: [[5, undefined]], // Skip the first part, but read until end
  299. }
  300. };
  301. ranges.send(res, in_stream, opts, function(err) {
  302. expect(err).to.not.exist;
  303. // HTTP handling
  304. expect(res.statusCode).to.equal(206);
  305. expect(res.getHeader('content-type')).to.equal('application/test');
  306. expect(res.getHeader('content-disposition')).to.equal('inline');
  307. expect(res.getHeader('content-range')).to.equal('bytes 5-/*');
  308. // Data/stream handling
  309. expect(res._isEndCalled()).to.be.true;
  310. expect(res._getBuffer().toString()).to.equal(', world!');
  311. // Notify mocha that we're done.
  312. done();
  313. });
  314. // Simulate file stream
  315. in_stream.emit('open');
  316. in_stream.put('Hello, world!');
  317. in_stream.stop();
  318. });
  319. it('should ignore ranges without start', function(done)
  320. {
  321. var res = mock_http.createResponse({});
  322. var in_stream = new stream_buffers.ReadableStreamBuffer({});
  323. // End-of-stream callback
  324. var opts = {
  325. name: 'test.file',
  326. type: 'application/test',
  327. ranges: {
  328. ranges: [[undefined, 5]], // Only last five
  329. }
  330. };
  331. ranges.send(res, in_stream, opts, function(err) {
  332. expect(err).to.not.exist;
  333. // HTTP handling
  334. expect(res.statusCode).to.equal(200);
  335. expect(res.getHeader('content-type')).to.equal('application/test');
  336. expect(res.getHeader('content-disposition')).to.equal('inline');
  337. // Data/stream handling
  338. expect(res._isEndCalled()).to.be.true;
  339. expect(res._getBuffer().toString()).to.equal('Hello, world!');
  340. // Notify mocha that we're done.
  341. done();
  342. });
  343. // Simulate file stream
  344. in_stream.emit('open');
  345. in_stream.put('Hello, world!');
  346. in_stream.stop();
  347. });
  348. });
  349. });