aboutsummaryrefslogtreecommitdiff
path: root/ui/src/common/upload_utils.ts
blob: 0c979217822e9b292a7b1d1c6ce5e02ede4ec625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {RecordConfig} from '../controller/record_config_types';

export const BUCKET_NAME = 'perfetto-ui-data';
import {v4 as uuidv4} from 'uuid';
import {State} from './state';

export async function saveTrace(trace: File|ArrayBuffer): Promise<string> {
  // TODO(hjd): This should probably also be a hash but that requires
  // trace processor support.
  const name = uuidv4();
  const url = 'https://www.googleapis.com/upload/storage/v1/b/' +
      `${BUCKET_NAME}/o?uploadType=media` +
      `&name=${name}&predefinedAcl=publicRead`;
  const response = await fetch(url, {
    method: 'post',
    headers: {'Content-Type': 'application/octet-stream;'},
    body: trace,
  });
  await response.json();
  return `https://storage.googleapis.com/${BUCKET_NAME}/${name}`;
}

// Bigint's are not serializable using JSON.stringify, so we use a special
// object when serialising
export type SerializedBigint = {
  __kind: 'bigint',
  value: string
};

// Check if a value looks like a serialized bigint
export function isSerializedBigint(value: unknown): value is SerializedBigint {
  if (value === null) {
    return false;
  }
  if (typeof value !== 'object') {
    return false;
  }
  if ('__kind' in value && 'value' in value) {
    return value.__kind === 'bigint' && typeof value.value === 'string';
  }
  return false;
}

export function serializeStateObject(object: unknown): string {
  const json = JSON.stringify(object, (key, value) => {
    if (typeof value === 'bigint') {
      return {
        __kind: 'bigint',
        value: value.toString(),
      };
    }
    return key === 'nonSerializableState' ? undefined : value;
  });
  return json;
}

export function deserializeStateObject(json: string): any {
  const object = JSON.parse(json, (_key, value) => {
    if (isSerializedBigint(value)) {
      return BigInt(value.value);
    }
    return value;
  });
  return object;
}

export async function saveState(stateOrConfig: State|
                                RecordConfig): Promise<string> {
  const text = serializeStateObject(stateOrConfig);
  const hash = await toSha256(text);
  const url = 'https://www.googleapis.com/upload/storage/v1/b/' +
      `${BUCKET_NAME}/o?uploadType=media` +
      `&name=${hash}&predefinedAcl=publicRead`;
  const response = await fetch(url, {
    method: 'post',
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
    },
    body: text,
  });
  await response.json();
  return hash;
}

// This has a bug:
// x.toString(16) doesn't zero pad so if the digest is:
// [23, 7, 42, ...]
// You get:
// ['17', '7', '2a', ...] = 1772a...
// Rather than:
// ['17', '07', '2a', ...] = 17072a...
// As you ought to (and as the hexdigest is computed by e.g. Python).
// Unfortunately there are a lot of old permalinks out there so we
// still need this broken implementation to check their hashes.
export async function buggyToSha256(str: string): Promise<string> {
  const buffer = new TextEncoder().encode(str);
  const digest = await crypto.subtle.digest('SHA-256', buffer);
  return Array.from(new Uint8Array(digest)).map((x) => x.toString(16)).join('');
}

export async function toSha256(str: string): Promise<string> {
  const buffer = new TextEncoder().encode(str);
  const digest = await crypto.subtle.digest('SHA-256', buffer);
  return Array.from(new Uint8Array(digest))
      .map((x) => x.toString(16).padStart(2, '0'))
      .join('');
}