11cb0ef41Sopenharmony_ci// Flags: --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Because registering a Blob URL requires generating a random
71cb0ef41Sopenharmony_ci// UUID, it can only be done if crypto support is enabled.
81cb0ef41Sopenharmony_ciif (!common.hasCrypto)
91cb0ef41Sopenharmony_ci  common.skip('missing crypto');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  URL,
131cb0ef41Sopenharmony_ci} = require('url');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst {
161cb0ef41Sopenharmony_ci  Blob,
171cb0ef41Sopenharmony_ci  resolveObjectURL,
181cb0ef41Sopenharmony_ci} = require('buffer');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst assert = require('assert');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci(async () => {
231cb0ef41Sopenharmony_ci  const blob = new Blob(['hello']);
241cb0ef41Sopenharmony_ci  const id = URL.createObjectURL(blob);
251cb0ef41Sopenharmony_ci  assert.strictEqual(typeof id, 'string');
261cb0ef41Sopenharmony_ci  const otherBlob = resolveObjectURL(id);
271cb0ef41Sopenharmony_ci  assert.strictEqual(otherBlob.size, 5);
281cb0ef41Sopenharmony_ci  assert.strictEqual(
291cb0ef41Sopenharmony_ci    Buffer.from(await otherBlob.arrayBuffer()).toString(),
301cb0ef41Sopenharmony_ci    'hello');
311cb0ef41Sopenharmony_ci  URL.revokeObjectURL(id);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // should do nothing
341cb0ef41Sopenharmony_ci  URL.revokeObjectURL(id);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  assert.strictEqual(resolveObjectURL(id), undefined);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  // Leaving a Blob registered should not cause an assert
391cb0ef41Sopenharmony_ci  // when Node.js exists
401cb0ef41Sopenharmony_ci  URL.createObjectURL(new Blob());
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci})().then(common.mustCall());
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci['not a url', undefined, 1, 'blob:nodedata:1:wrong', {}].forEach((i) => {
451cb0ef41Sopenharmony_ci  assert.strictEqual(resolveObjectURL(i), undefined);
461cb0ef41Sopenharmony_ci});
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci[undefined, 1, '', false, {}].forEach((i) => {
491cb0ef41Sopenharmony_ci  assert.throws(() => URL.createObjectURL(i), {
501cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
511cb0ef41Sopenharmony_ci  });
521cb0ef41Sopenharmony_ci});
53