storage.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 chai = require('chai');
  21. const chai_as_promised = require('chai-as-promised');
  22. chai.use(chai_as_promised);
  23. const expect = chai.expect;
  24. const fs = require('fs');
  25. const { Storage } = require('@joystream/storage-node-backend');
  26. const IPFS_CID_REGEX = /^Qm[1-9A-HJ-NP-Za-km-z]{44}$/;
  27. function write(store, content_id, contents, callback)
  28. {
  29. store.open(content_id, 'w')
  30. .then((stream) => {
  31. stream.on('finish', () => {
  32. stream.commit();
  33. });
  34. stream.on('committed', callback);
  35. stream.write(contents);
  36. stream.end();
  37. })
  38. .catch((err) => {
  39. expect.fail(err);
  40. });
  41. }
  42. function read_all (stream) {
  43. return new Promise((resolve, reject) => {
  44. const chunks = []
  45. stream.on('data', chunk => chunks.push(chunk))
  46. stream.on('end', () => resolve(Buffer.concat(chunks)))
  47. stream.on('error', err => reject(err))
  48. stream.resume()
  49. })
  50. }
  51. function create_known_object(content_id, contents, callback)
  52. {
  53. var hash;
  54. const store = Storage.create({
  55. resolve_content_id: () => {
  56. return hash;
  57. },
  58. })
  59. write(store, content_id, contents, (the_hash) => {
  60. hash = the_hash;
  61. callback(store, hash);
  62. });
  63. }
  64. describe('storage/storage', () => {
  65. var storage;
  66. before(async () => {
  67. storage = await Storage.create({ timeout: 1900 });
  68. });
  69. describe('open()', () => {
  70. it('can write a stream', (done) => {
  71. write(storage, 'foobar', 'test-content', (hash) => {
  72. expect(hash).to.not.be.undefined;
  73. expect(hash).to.match(IPFS_CID_REGEX)
  74. done();
  75. });
  76. });
  77. it('detects the MIME type of a write stream', (done) => {
  78. const contents = fs.readFileSync('../../storage-node_new.svg');
  79. create_known_object('foobar', contents, (store, hash) => {
  80. var file_info;
  81. store.open('mime-test', 'w')
  82. .then((stream) => {
  83. stream.on('file_info', (info) => {
  84. // Could filter & abort here now, but we're just going to set this,
  85. // and expect it to be set later...
  86. file_info = info;
  87. });
  88. stream.on('finish', () => {
  89. stream.commit();
  90. });
  91. stream.on('committed', (hash) => {
  92. // ... if file_info is not set here, there's an issue.
  93. expect(file_info).to.have.property('mime_type', 'application/xml');
  94. expect(file_info).to.have.property('ext', 'xml');
  95. done();
  96. });
  97. stream.write(contents);
  98. stream.end();
  99. })
  100. .catch((err) => {
  101. expect.fail(err);
  102. });
  103. });
  104. });
  105. it('can read a stream', (done) => {
  106. const contents = 'test-for-reading';
  107. create_known_object('foobar', contents, (store, hash) => {
  108. store.open('foobar', 'r')
  109. .then(async (stream) => {
  110. const data = await read_all(stream);
  111. expect(Buffer.compare(data, Buffer.from(contents))).to.equal(0);
  112. done();
  113. })
  114. .catch((err) => {
  115. expect.fail(err);
  116. });
  117. });
  118. });
  119. it('detects the MIME type of a read stream', (done) => {
  120. const contents = fs.readFileSync('../../storage-node_new.svg');
  121. create_known_object('foobar', contents, (store, hash) => {
  122. store.open('foobar', 'r')
  123. .then(async (stream) => {
  124. const data = await read_all(stream);
  125. expect(contents.length).to.equal(data.length);
  126. expect(Buffer.compare(data, contents)).to.equal(0);
  127. expect(stream).to.have.property('file_info');
  128. // application/xml+svg would be better, but this is good-ish.
  129. expect(stream.file_info).to.have.property('mime_type', 'application/xml');
  130. expect(stream.file_info).to.have.property('ext', 'xml');
  131. done();
  132. })
  133. .catch((err) => {
  134. expect.fail(err);
  135. });
  136. });
  137. });
  138. it('provides default MIME type for read streams', (done) => {
  139. const contents = 'test-for-reading';
  140. create_known_object('foobar', contents, (store, hash) => {
  141. store.open('foobar', 'r')
  142. .then(async (stream) => {
  143. const data = await read_all(stream);
  144. expect(Buffer.compare(data, Buffer.from(contents))).to.equal(0);
  145. expect(stream.file_info).to.have.property('mime_type', 'application/octet-stream');
  146. expect(stream.file_info).to.have.property('ext', 'bin');
  147. done();
  148. })
  149. .catch((err) => {
  150. expect.fail(err);
  151. });
  152. });
  153. });
  154. });
  155. describe('stat()', () => {
  156. it('times out for unknown content', async () => {
  157. const content = Buffer.from('this-should-not-exist');
  158. const x = await storage.ipfs.add(content, { onlyHash: true });
  159. const hash = x[0].hash;
  160. // Try to stat this entry, it should timeout.
  161. expect(storage.stat(hash)).to.eventually.be.rejectedWith('timed out');
  162. });
  163. it('returns stats for a known object', (done) => {
  164. const content = 'stat-test';
  165. const expected_size = content.length;
  166. create_known_object('foobar', 'stat-test', (store, hash) => {
  167. expect(store.stat(hash)).to.eventually.have.property('size', expected_size);
  168. done();
  169. });
  170. });
  171. });
  172. describe('size()', () => {
  173. it('times out for unknown content', async () => {
  174. const content = Buffer.from('this-should-not-exist');
  175. const x = await storage.ipfs.add(content, { onlyHash: true });
  176. const hash = x[0].hash;
  177. // Try to stat this entry, it should timeout.
  178. expect(storage.size(hash)).to.eventually.be.rejectedWith('timed out');
  179. });
  180. it('returns the size of a known object', (done) => {
  181. create_known_object('foobar', 'stat-test', (store, hash) => {
  182. expect(store.size(hash)).to.eventually.equal(15);
  183. done();
  184. });
  185. });
  186. });
  187. });