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