1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const { Blob } = require('buffer'); 6const { inspect } = require('util'); 7const { MessageChannel } = require('worker_threads'); 8 9{ 10 const b = new Blob(); 11 assert.strictEqual(b.size, 0); 12 assert.strictEqual(b.type, ''); 13} 14 15assert.throws(() => new Blob(false), { 16 code: 'ERR_INVALID_ARG_TYPE' 17}); 18 19assert.throws(() => new Blob('hello'), { 20 code: 'ERR_INVALID_ARG_TYPE' 21}); 22 23assert.throws(() => new Blob({}), { 24 code: 'ERR_INVALID_ARG_TYPE' 25}); 26 27{ 28 const b = new Blob([]); 29 assert(b); 30 assert.strictEqual(b.size, 0); 31 assert.strictEqual(b.type, ''); 32 33 b.arrayBuffer().then(common.mustCall((ab) => { 34 assert.deepStrictEqual(ab, new ArrayBuffer(0)); 35 })); 36 b.text().then(common.mustCall((text) => { 37 assert.strictEqual(text, ''); 38 })); 39 const c = b.slice(); 40 assert.strictEqual(c.size, 0); 41} 42 43{ 44 assert.strictEqual(new Blob([], { type: 1 }).type, '1'); 45 assert.strictEqual(new Blob([], { type: false }).type, 'false'); 46 assert.strictEqual(new Blob([], { type: {} }).type, '[object object]'); 47} 48 49{ 50 const b = new Blob(['616263'], { encoding: 'hex', type: 'foo' }); 51 assert.strictEqual(b.size, 3); 52 assert.strictEqual(b.type, 'foo'); 53 b.text().then(common.mustCall((text) => { 54 assert.strictEqual(text, 'abc'); 55 })); 56} 57 58{ 59 const b = new Blob([Buffer.from('abc')]); 60 assert.strictEqual(b.size, 3); 61 b.text().then(common.mustCall((text) => { 62 assert.strictEqual(text, 'abc'); 63 })); 64} 65 66{ 67 const b = new Blob([new ArrayBuffer(3)]); 68 assert.strictEqual(b.size, 3); 69 b.text().then(common.mustCall((text) => { 70 assert.strictEqual(text, '\0\0\0'); 71 })); 72} 73 74{ 75 const b = new Blob([new Uint8Array(3)]); 76 assert.strictEqual(b.size, 3); 77 b.text().then(common.mustCall((text) => { 78 assert.strictEqual(text, '\0\0\0'); 79 })); 80} 81 82{ 83 const b = new Blob([new Blob(['abc'])]); 84 assert.strictEqual(b.size, 3); 85 b.text().then(common.mustCall((text) => { 86 assert.strictEqual(text, 'abc'); 87 })); 88} 89 90{ 91 const b = new Blob(['hello', Buffer.from('world')]); 92 assert.strictEqual(b.size, 10); 93 b.text().then(common.mustCall((text) => { 94 assert.strictEqual(text, 'helloworld'); 95 })); 96} 97 98{ 99 const b = new Blob( 100 [ 101 'h', 102 'e', 103 'l', 104 'lo', 105 Buffer.from('world'), 106 ]); 107 assert.strictEqual(b.size, 10); 108 b.text().then(common.mustCall((text) => { 109 assert.strictEqual(text, 'helloworld'); 110 })); 111} 112 113{ 114 const b = new Blob(['hello', Buffer.from('world')]); 115 assert.strictEqual(b.size, 10); 116 assert.strictEqual(b.type, ''); 117 118 const c = b.slice(1, -1, 'foo'); 119 assert.strictEqual(c.type, 'foo'); 120 c.text().then(common.mustCall((text) => { 121 assert.strictEqual(text, 'elloworl'); 122 })); 123 124 const d = c.slice(1, -1); 125 d.text().then(common.mustCall((text) => { 126 assert.strictEqual(text, 'llowor'); 127 })); 128 129 const e = d.slice(1, -1); 130 e.text().then(common.mustCall((text) => { 131 assert.strictEqual(text, 'lowo'); 132 })); 133 134 const f = e.slice(1, -1); 135 f.text().then(common.mustCall((text) => { 136 assert.strictEqual(text, 'ow'); 137 })); 138 139 const g = f.slice(1, -1); 140 assert.strictEqual(g.type, ''); 141 g.text().then(common.mustCall((text) => { 142 assert.strictEqual(text, ''); 143 })); 144 145 const h = b.slice(-1, 1); 146 assert.strictEqual(h.size, 0); 147 148 const i = b.slice(1, 100); 149 assert.strictEqual(i.size, 9); 150 151 const j = b.slice(1, 2, false); 152 assert.strictEqual(j.type, 'false'); 153 154 assert.strictEqual(b.size, 10); 155 assert.strictEqual(b.type, ''); 156} 157 158{ 159 const b = new Blob([Buffer.from('hello'), Buffer.from('world')]); 160 const mc = new MessageChannel(); 161 mc.port1.onmessage = common.mustCall(({ data }) => { 162 data.text().then(common.mustCall((text) => { 163 assert.strictEqual(text, 'helloworld'); 164 })); 165 mc.port1.close(); 166 }); 167 mc.port2.postMessage(b); 168 b.text().then(common.mustCall((text) => { 169 assert.strictEqual(text, 'helloworld'); 170 })); 171} 172 173{ 174 const b = new Blob(['hello'], { type: '\x01' }); 175 assert.strictEqual(b.type, ''); 176} 177 178{ 179 const descriptor = 180 Object.getOwnPropertyDescriptor(Blob.prototype, Symbol.toStringTag); 181 assert.deepStrictEqual(descriptor, { 182 configurable: true, 183 enumerable: false, 184 value: 'Blob', 185 writable: false 186 }); 187} 188 189{ 190 const b = new Blob(['test', 42]); 191 b.text().then(common.mustCall((text) => { 192 assert.strictEqual(text, 'test42'); 193 })); 194} 195 196{ 197 const b = new Blob(); 198 assert.strictEqual(inspect(b, { depth: null }), 199 'Blob { size: 0, type: \'\' }'); 200 assert.strictEqual(inspect(b, { depth: -1 }), '[Blob]'); 201} 202