• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --no-warnings
2'use strict';
3
4const common = require('../common');
5
6// Because registering a Blob URL requires generating a random
7// UUID, it can only be done if crypto support is enabled.
8if (!common.hasCrypto)
9  common.skip('missing crypto');
10
11const {
12  URL,
13} = require('url');
14
15const {
16  Blob,
17  resolveObjectURL,
18} = require('buffer');
19
20const assert = require('assert');
21
22(async () => {
23  const blob = new Blob(['hello']);
24  const id = URL.createObjectURL(blob);
25  assert.strictEqual(typeof id, 'string');
26  const otherBlob = resolveObjectURL(id);
27  assert.strictEqual(otherBlob.size, 5);
28  assert.strictEqual(
29    Buffer.from(await otherBlob.arrayBuffer()).toString(),
30    'hello');
31  URL.revokeObjectURL(id);
32
33  // should do nothing
34  URL.revokeObjectURL(id);
35
36  assert.strictEqual(resolveObjectURL(id), undefined);
37
38  // Leaving a Blob registered should not cause an assert
39  // when Node.js exists
40  URL.createObjectURL(new Blob());
41
42})().then(common.mustCall());
43
44['not a url', undefined, 1, 'blob:nodedata:1:wrong', {}].forEach((i) => {
45  assert.strictEqual(resolveObjectURL(i), undefined);
46});
47
48[undefined, 1, '', false, {}].forEach((i) => {
49  assert.throws(() => URL.createObjectURL(i), {
50    code: 'ERR_INVALID_ARG_TYPE',
51  });
52});
53