11cb0ef41Sopenharmony_ci// Flags: --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { Blob } = require('buffer');
71cb0ef41Sopenharmony_ciconst { inspect } = require('util');
81cb0ef41Sopenharmony_ciconst { EOL } = require('os');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const b = new Blob();
121cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 0);
131cb0ef41Sopenharmony_ci  assert.strictEqual(b.type, '');
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciassert.throws(() => new Blob(false), {
171cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE'
181cb0ef41Sopenharmony_ci});
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciassert.throws(() => new Blob('hello'), {
211cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE'
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciassert.throws(() => new Blob({}), {
251cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE'
261cb0ef41Sopenharmony_ci});
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const b = new Blob([]);
301cb0ef41Sopenharmony_ci  assert(b);
311cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 0);
321cb0ef41Sopenharmony_ci  assert.strictEqual(b.type, '');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  b.arrayBuffer().then(common.mustCall((ab) => {
351cb0ef41Sopenharmony_ci    assert.deepStrictEqual(ab, new ArrayBuffer(0));
361cb0ef41Sopenharmony_ci  }));
371cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
381cb0ef41Sopenharmony_ci    assert.strictEqual(text, '');
391cb0ef41Sopenharmony_ci  }));
401cb0ef41Sopenharmony_ci  const c = b.slice();
411cb0ef41Sopenharmony_ci  assert.strictEqual(c.size, 0);
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  assert.strictEqual(new Blob([], { type: 1 }).type, '1');
461cb0ef41Sopenharmony_ci  assert.strictEqual(new Blob([], { type: false }).type, 'false');
471cb0ef41Sopenharmony_ci  assert.strictEqual(new Blob([], { type: {} }).type, '[object object]');
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  const b = new Blob([Buffer.from('abc')]);
521cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 3);
531cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
541cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'abc');
551cb0ef41Sopenharmony_ci  }));
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  const b = new Blob([new ArrayBuffer(3)]);
601cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 3);
611cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
621cb0ef41Sopenharmony_ci    assert.strictEqual(text, '\0\0\0');
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  const b = new Blob([new Uint8Array(3)]);
681cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 3);
691cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
701cb0ef41Sopenharmony_ci    assert.strictEqual(text, '\0\0\0');
711cb0ef41Sopenharmony_ci  }));
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  const b = new Blob([new Blob(['abc'])]);
761cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 3);
771cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
781cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'abc');
791cb0ef41Sopenharmony_ci  }));
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci{
831cb0ef41Sopenharmony_ci  const b = new Blob(['hello', Buffer.from('world')]);
841cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 10);
851cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
861cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'helloworld');
871cb0ef41Sopenharmony_ci  }));
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci{
911cb0ef41Sopenharmony_ci  const b = new Blob(['hello', new Uint8Array([0xed, 0xa0, 0x88])]);
921cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 8);
931cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
941cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'hello\ufffd\ufffd\ufffd');
951cb0ef41Sopenharmony_ci    assert.strictEqual(text.length, 8);
961cb0ef41Sopenharmony_ci  }));
971cb0ef41Sopenharmony_ci}
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci{
1001cb0ef41Sopenharmony_ci  const b = new Blob(
1011cb0ef41Sopenharmony_ci    [
1021cb0ef41Sopenharmony_ci      'h',
1031cb0ef41Sopenharmony_ci      'e',
1041cb0ef41Sopenharmony_ci      'l',
1051cb0ef41Sopenharmony_ci      'lo',
1061cb0ef41Sopenharmony_ci      Buffer.from('world'),
1071cb0ef41Sopenharmony_ci    ]);
1081cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 10);
1091cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
1101cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'helloworld');
1111cb0ef41Sopenharmony_ci  }));
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci{
1151cb0ef41Sopenharmony_ci  const b = new Blob(['hello', Buffer.from('world')]);
1161cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 10);
1171cb0ef41Sopenharmony_ci  assert.strictEqual(b.type, '');
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  const c = b.slice(1, -1, 'foo');
1201cb0ef41Sopenharmony_ci  assert.strictEqual(c.type, 'foo');
1211cb0ef41Sopenharmony_ci  c.text().then(common.mustCall((text) => {
1221cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'elloworl');
1231cb0ef41Sopenharmony_ci  }));
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  const d = c.slice(1, -1);
1261cb0ef41Sopenharmony_ci  d.text().then(common.mustCall((text) => {
1271cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'llowor');
1281cb0ef41Sopenharmony_ci  }));
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  const e = d.slice(1, -1);
1311cb0ef41Sopenharmony_ci  e.text().then(common.mustCall((text) => {
1321cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'lowo');
1331cb0ef41Sopenharmony_ci  }));
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  const f = e.slice(1, -1);
1361cb0ef41Sopenharmony_ci  f.text().then(common.mustCall((text) => {
1371cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'ow');
1381cb0ef41Sopenharmony_ci  }));
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  const g = f.slice(1, -1);
1411cb0ef41Sopenharmony_ci  assert.strictEqual(g.type, '');
1421cb0ef41Sopenharmony_ci  g.text().then(common.mustCall((text) => {
1431cb0ef41Sopenharmony_ci    assert.strictEqual(text, '');
1441cb0ef41Sopenharmony_ci  }));
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  const h = b.slice(-1, 1);
1471cb0ef41Sopenharmony_ci  assert.strictEqual(h.size, 0);
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  const i = b.slice(1, 100);
1501cb0ef41Sopenharmony_ci  assert.strictEqual(i.size, 9);
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  const j = b.slice(1, 2, false);
1531cb0ef41Sopenharmony_ci  assert.strictEqual(j.type, 'false');
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, 10);
1561cb0ef41Sopenharmony_ci  assert.strictEqual(b.type, '');
1571cb0ef41Sopenharmony_ci}
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci{
1601cb0ef41Sopenharmony_ci  const b = new Blob([Buffer.from('hello'), Buffer.from('world')]);
1611cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
1621cb0ef41Sopenharmony_ci  mc.port1.onmessage = common.mustCall(({ data }) => {
1631cb0ef41Sopenharmony_ci    data.text().then(common.mustCall((text) => {
1641cb0ef41Sopenharmony_ci      assert.strictEqual(text, 'helloworld');
1651cb0ef41Sopenharmony_ci    }));
1661cb0ef41Sopenharmony_ci    mc.port1.close();
1671cb0ef41Sopenharmony_ci  });
1681cb0ef41Sopenharmony_ci  mc.port2.postMessage(b);
1691cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
1701cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'helloworld');
1711cb0ef41Sopenharmony_ci  }));
1721cb0ef41Sopenharmony_ci}
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci{
1751cb0ef41Sopenharmony_ci  const b = new Blob(['hello'], { type: '\x01' });
1761cb0ef41Sopenharmony_ci  assert.strictEqual(b.type, '');
1771cb0ef41Sopenharmony_ci}
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci{
1801cb0ef41Sopenharmony_ci  const descriptor =
1811cb0ef41Sopenharmony_ci      Object.getOwnPropertyDescriptor(Blob.prototype, Symbol.toStringTag);
1821cb0ef41Sopenharmony_ci  assert.deepStrictEqual(descriptor, {
1831cb0ef41Sopenharmony_ci    configurable: true,
1841cb0ef41Sopenharmony_ci    enumerable: false,
1851cb0ef41Sopenharmony_ci    value: 'Blob',
1861cb0ef41Sopenharmony_ci    writable: false
1871cb0ef41Sopenharmony_ci  });
1881cb0ef41Sopenharmony_ci}
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci{
1911cb0ef41Sopenharmony_ci  const descriptors = Object.getOwnPropertyDescriptors(Blob.prototype);
1921cb0ef41Sopenharmony_ci  const enumerable = [
1931cb0ef41Sopenharmony_ci    'size',
1941cb0ef41Sopenharmony_ci    'type',
1951cb0ef41Sopenharmony_ci    'slice',
1961cb0ef41Sopenharmony_ci    'stream',
1971cb0ef41Sopenharmony_ci    'text',
1981cb0ef41Sopenharmony_ci    'arrayBuffer',
1991cb0ef41Sopenharmony_ci  ];
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  for (const prop of enumerable) {
2021cb0ef41Sopenharmony_ci    assert.notStrictEqual(descriptors[prop], undefined);
2031cb0ef41Sopenharmony_ci    assert.strictEqual(descriptors[prop].enumerable, true);
2041cb0ef41Sopenharmony_ci  }
2051cb0ef41Sopenharmony_ci}
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci{
2081cb0ef41Sopenharmony_ci  const b = new Blob(['test', 42]);
2091cb0ef41Sopenharmony_ci  b.text().then(common.mustCall((text) => {
2101cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'test42');
2111cb0ef41Sopenharmony_ci  }));
2121cb0ef41Sopenharmony_ci}
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci{
2151cb0ef41Sopenharmony_ci  const b = new Blob();
2161cb0ef41Sopenharmony_ci  assert.strictEqual(inspect(b, { depth: null }),
2171cb0ef41Sopenharmony_ci                     'Blob { size: 0, type: \'\' }');
2181cb0ef41Sopenharmony_ci  assert.strictEqual(inspect(b, { depth: 1 }),
2191cb0ef41Sopenharmony_ci                     'Blob { size: 0, type: \'\' }');
2201cb0ef41Sopenharmony_ci  assert.strictEqual(inspect(b, { depth: -1 }), '[Blob]');
2211cb0ef41Sopenharmony_ci}
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci{
2241cb0ef41Sopenharmony_ci  // The Blob has to be over a specific size for the data to
2251cb0ef41Sopenharmony_ci  // be copied asynchronously..
2261cb0ef41Sopenharmony_ci  const b = new Blob(['hello', 'there'.repeat(820)]);
2271cb0ef41Sopenharmony_ci  assert.strictEqual(b.arrayBuffer(), b.arrayBuffer());
2281cb0ef41Sopenharmony_ci  b.arrayBuffer().then(common.mustCall());
2291cb0ef41Sopenharmony_ci}
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci(async () => {
2321cb0ef41Sopenharmony_ci  const b = new Blob(['hello']);
2331cb0ef41Sopenharmony_ci  const reader = b.stream().getReader();
2341cb0ef41Sopenharmony_ci  let res = await reader.read();
2351cb0ef41Sopenharmony_ci  assert.strictEqual(res.value.byteLength, 5);
2361cb0ef41Sopenharmony_ci  assert(!res.done);
2371cb0ef41Sopenharmony_ci  res = await reader.read();
2381cb0ef41Sopenharmony_ci  assert(res.done);
2391cb0ef41Sopenharmony_ci})().then(common.mustCall());
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci{
2421cb0ef41Sopenharmony_ci  const b = new Blob(['hello\n'], { endings: 'native' });
2431cb0ef41Sopenharmony_ci  assert.strictEqual(b.size, EOL.length + 5);
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci  [1, {}, 'foo'].forEach((endings) => {
2461cb0ef41Sopenharmony_ci    assert.throws(() => new Blob([], { endings }), {
2471cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_VALUE',
2481cb0ef41Sopenharmony_ci    });
2491cb0ef41Sopenharmony_ci  });
2501cb0ef41Sopenharmony_ci}
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci{
2531cb0ef41Sopenharmony_ci  assert.throws(() => Reflect.get(Blob.prototype, 'type', {}), {
2541cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2551cb0ef41Sopenharmony_ci  });
2561cb0ef41Sopenharmony_ci  assert.throws(() => Reflect.get(Blob.prototype, 'size', {}), {
2571cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2581cb0ef41Sopenharmony_ci  });
2591cb0ef41Sopenharmony_ci  assert.throws(() => Blob.prototype.slice(Blob.prototype, 0, 1), {
2601cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2611cb0ef41Sopenharmony_ci  });
2621cb0ef41Sopenharmony_ci  assert.throws(() => Blob.prototype.stream.call(), {
2631cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2641cb0ef41Sopenharmony_ci  });
2651cb0ef41Sopenharmony_ci}
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci(async () => {
2681cb0ef41Sopenharmony_ci  assert.rejects(async () => Blob.prototype.arrayBuffer.call(), {
2691cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2701cb0ef41Sopenharmony_ci  });
2711cb0ef41Sopenharmony_ci  assert.rejects(async () => Blob.prototype.text.call(), {
2721cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
2731cb0ef41Sopenharmony_ci  });
2741cb0ef41Sopenharmony_ci})().then(common.mustCall());
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci(async () => {
2771cb0ef41Sopenharmony_ci  const blob = new Blob([
2781cb0ef41Sopenharmony_ci    new Uint8Array([0x50, 0x41, 0x53, 0x53]),
2791cb0ef41Sopenharmony_ci    new Int8Array([0x50, 0x41, 0x53, 0x53]),
2801cb0ef41Sopenharmony_ci    new Uint16Array([0x4150, 0x5353]),
2811cb0ef41Sopenharmony_ci    new Int16Array([0x4150, 0x5353]),
2821cb0ef41Sopenharmony_ci    new Uint32Array([0x53534150]),
2831cb0ef41Sopenharmony_ci    new Int32Array([0x53534150]),
2841cb0ef41Sopenharmony_ci    new Float32Array([0xD341500000]),
2851cb0ef41Sopenharmony_ci  ]);
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci  assert.strictEqual(blob.size, 28);
2881cb0ef41Sopenharmony_ci  assert.strictEqual(blob.type, '');
2891cb0ef41Sopenharmony_ci})().then(common.mustCall());
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ci{
2921cb0ef41Sopenharmony_ci  // Testing the defaults
2931cb0ef41Sopenharmony_ci  [undefined, null, Object.create(null), { type: undefined }, {
2941cb0ef41Sopenharmony_ci    get type() {}, // eslint-disable-line getter-return
2951cb0ef41Sopenharmony_ci  }].forEach((options) => {
2961cb0ef41Sopenharmony_ci    assert.strictEqual(
2971cb0ef41Sopenharmony_ci      new Blob([], options).type,
2981cb0ef41Sopenharmony_ci      new Blob([]).type,
2991cb0ef41Sopenharmony_ci    );
3001cb0ef41Sopenharmony_ci  });
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci  Reflect.defineProperty(Object.prototype, 'type', {
3031cb0ef41Sopenharmony_ci    __proto__: null,
3041cb0ef41Sopenharmony_ci    configurable: true,
3051cb0ef41Sopenharmony_ci    get: common.mustCall(() => 3, 7),
3061cb0ef41Sopenharmony_ci  });
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci  [{}, [], () => {}, Number, new Number(), new String(), new Boolean()].forEach(
3091cb0ef41Sopenharmony_ci    (options) => {
3101cb0ef41Sopenharmony_ci      assert.strictEqual(new Blob([], options).type, '3');
3111cb0ef41Sopenharmony_ci    },
3121cb0ef41Sopenharmony_ci  );
3131cb0ef41Sopenharmony_ci  [0, '', true, Symbol(), 0n].forEach((options) => {
3141cb0ef41Sopenharmony_ci    assert.throws(() => new Blob([], options), { code: 'ERR_INVALID_ARG_TYPE' });
3151cb0ef41Sopenharmony_ci  });
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci  delete Object.prototype.type;
3181cb0ef41Sopenharmony_ci}
319