tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

messaging-serialize-helpers.js (7656B)


      1 'use strict';
      2 
      3 // This script depends on the following script:
      4 //    /fs/resources/test-helpers.js
      5 
      6 // Serializes an array of FileSystemHandles where each element can be either a
      7 // FileSystemFileHandle or FileSystemDirectoryHandle.
      8 async function serialize_handles(handle_array) {
      9  const serialized_handle_array = [];
     10  for (let i = 0; i < handle_array.length; ++i) {
     11    serialized_handle_array.push(await serialize_handle(handle_array[i]));
     12  }
     13  return serialized_handle_array;
     14 }
     15 
     16 // Serializes either a FileSystemFileHandle or FileSystemDirectoryHandle.
     17 async function serialize_handle(handle) {
     18  switch (handle.kind) {
     19    case 'directory':
     20      return await serialize_file_system_directory_handle(handle);
     21    case 'file':
     22      return await serialize_file_system_file_handle(handle);
     23    default:
     24      throw 'Object is not a FileSystemFileHandle or ' +
     25          `FileSystemDirectoryHandle ${handle}`;
     26  }
     27 }
     28 
     29 // Creates a dictionary for a FileSystemHandle base, which contains
     30 // serialized properties shared by both FileSystemFileHandle and
     31 // FileSystemDirectoryHandle.
     32 async function serialize_file_system_handle(handle) {
     33  return {
     34    kind: handle.kind,
     35    name: handle.name
     36  };
     37 }
     38 
     39 // Create a dictionary with each property value in FileSystemFileHandle.
     40 // Also, reads the contents of the file to include with the returned
     41 // dictionary.  Example output:
     42 // {
     43 //   kind: "file",
     44 //   name: "example-file-name",
     45 //   contents: "example-file-contents"
     46 // }
     47 async function serialize_file_system_file_handle(file_handle) {
     48  const contents = await getFileContents(file_handle);
     49 
     50  const serialized_file_system_handle =
     51    await serialize_file_system_handle(file_handle);
     52 
     53  return Object.assign(serialized_file_system_handle, { contents });
     54 }
     55 
     56 // Create a dictionary with each property value in FileSystemDirectoryHandle.
     57 // Example output:
     58 // {
     59 //   kind: "directory",
     60 //   name: "example-directory-name",
     61 //   files: [<first serialized file>, ...]
     62 //   directories: [<first serialized subdirectory>, ...]
     63 // }
     64 async function serialize_file_system_directory_handle(directory_handle) {
     65  // Serialize the contents of the directory.
     66  const serialized_files = [];
     67  const serialized_directories = [];
     68  for await (const child_handle of directory_handle.values()) {
     69    const serialized_child_handle = await serialize_handle(child_handle);
     70    if (child_handle.kind === "directory") {
     71      serialized_directories.push(serialized_child_handle);
     72    } else {
     73      serialized_files.push(serialized_child_handle);
     74    }
     75  }
     76 
     77  // Order the serialized contents of the directory by name.
     78  serialized_files.sort((left, right) => {
     79    return left.name.localeCompare(right.name);
     80  });
     81  serialized_directories.sort((left, right) => {
     82    return left.name.localeCompare(right.name);
     83  });
     84 
     85  // Serialize the directory's common properties shared by all
     86  // FileSystemHandles.
     87  const serialized_file_system_handle =
     88    await serialize_file_system_handle(directory_handle);
     89 
     90  return Object.assign(
     91    serialized_file_system_handle,
     92    { files: serialized_files, directories: serialized_directories });
     93 }
     94 
     95 // Verifies |left_array| is a clone of |right_array| where each element
     96 // is a cloned FileSystemHandle with the same properties and contents.
     97 async function assert_equals_cloned_handles(left_array, right_array) {
     98  assert_equals(left_array.length, right_array.length,
     99    'Each array of FileSystemHandles must have the same length');
    100 
    101  for (let i = 0; i < left_array.length; ++i) {
    102    assert_not_equals(left_array[i], right_array[i],
    103      'Clones must create new FileSystemHandle instances.');
    104 
    105    const left_serialized = await serialize_handle(left_array[i]);
    106    const right_serialized = await serialize_handle(right_array[i]);
    107    assert_equals_serialized_handle(left_serialized, right_serialized);
    108  }
    109 }
    110 
    111 // Verifies |left_array| is the same as |right_array| where each element
    112 // is a serialized FileSystemHandle with the same properties.
    113 function assert_equals_serialized_handles(left_array, right_array) {
    114  assert_equals(left_array.length, right_array.length,
    115    'Each array of serialized handles must have the same length');
    116 
    117  for (let i = 0; i < left_array.length; ++i) {
    118    assert_equals_serialized_handle(left_array[i], right_array[i]);
    119  }
    120 }
    121 
    122 // Verifies each property of a serialized FileSystemFileHandle or
    123 // FileSystemDirectoryHandle.
    124 function assert_equals_serialized_handle(left, right) {
    125  switch (left.kind) {
    126    case 'directory':
    127      assert_equals_serialized_file_system_directory_handle(left, right);
    128      break;
    129    case 'file':
    130      assert_equals_serialized_file_system_file_handle(left, right);
    131      break;
    132    default:
    133      throw 'Object is not a FileSystemFileHandle or ' +
    134          `FileSystemDirectoryHandle ${left}`;
    135  }
    136 }
    137 
    138 // Compares the output of serialize_file_system_handle() for
    139 // two FileSystemHandles.
    140 function assert_equals_serialized_file_system_handle(left, right) {
    141  assert_equals(left.kind, right.kind,
    142    'Each FileSystemHandle instance must use the expected "kind".');
    143 
    144  assert_equals(left.name, right.name,
    145    'Each FileSystemHandle instance must use the expected "name" ' +
    146    ' property.');
    147 }
    148 
    149 // Compares the output of serialize_file_system_file_handle()
    150 // for two FileSystemFileHandle.
    151 function assert_equals_serialized_file_system_file_handle(left, right) {
    152  assert_equals_serialized_file_system_handle(left, right);
    153  assert_equals(left.contents, right.contents,
    154    'Each FileSystemFileHandle instance must have the same contents.');
    155 }
    156 
    157 // Compares the output of serialize_file_system_directory_handle()
    158 // for two FileSystemDirectoryHandles.
    159 function assert_equals_serialized_file_system_directory_handle(left, right) {
    160  assert_equals_serialized_file_system_handle(left, right);
    161 
    162  assert_equals(left.files.length, right.files.length,
    163    'Each FileSystemDirectoryHandle must contain the same number of ' +
    164    'file children');
    165 
    166  for (let i = 0; i < left.files.length; ++i) {
    167    assert_equals_serialized_file_system_file_handle(
    168      left.files[i], right.files[i]);
    169  }
    170 
    171  assert_equals(left.directories.length, right.directories.length,
    172    'Each FileSystemDirectoryHandle must contain the same number of ' +
    173    'directory children');
    174 
    175  for (let i = 0; i < left.directories.length; ++i) {
    176    assert_equals_serialized_file_system_directory_handle(
    177      left.directories[i], right.directories[i]);
    178  }
    179 }
    180 
    181 // Creates a dictionary with interesting property values from MessageEvent.
    182 function serialize_message_error_event(message_error_event) {
    183  return {
    184    data: message_error_event.data,
    185    origin: message_error_event.origin,
    186    last_event_id: message_error_event.lastEventId,
    187    has_source: (message_error_event.source !== null),
    188    ports_length: message_error_event.ports.length
    189  };
    190 }
    191 
    192 // Compares the output of serialize_message_error_event() with an
    193 // expected result.
    194 function assert_equals_serialized_message_error_event(
    195  serialized_event, expected_origin, expected_has_source) {
    196  assert_equals(serialized_event.data, null,
    197    'The message error event must set the "data" property to null.');
    198 
    199  assert_equals(serialized_event.origin, expected_origin,
    200    'The message error event must have the expected "origin" property.');
    201 
    202  assert_equals(serialized_event.last_event_id, "",
    203    'The message error event must set the "lastEventId" property to the empty string.');
    204 
    205  assert_equals(serialized_event.has_source, expected_has_source,
    206    'The message error event must have the expected "source" property.');
    207 
    208  assert_equals(serialized_event.ports_length, 0,
    209    'The message error event must not contain any message ports.');
    210 }