chrome-devtools-frontend
Version:
Chrome DevTools UI
76 lines (59 loc) • 2.44 kB
text/typescript
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {DebugId, SourceMapV3} from './SourceMap.js';
/** A thin wrapper around the Cache API to store source map JSONs keyed on Debug IDs */
export class SourceMapCache {
static readonly
static instance(): SourceMapCache {
if (typeof window === 'undefined') {
// TODO(crbug.com/451502260): Move this behind a `HostRuntime` interface.
return IN_MEMORY_INSTANCE as unknown as
SourceMapCache; // TS doesn't like that our in-memory class doesn't have the same private fields.
}
return this.
}
static createForTest(name: string): SourceMapCache {
return new SourceMapCache(name);
}
readonly
private constructor(name: string) {
this.
}
async set(debugId: DebugId, sourceMap: SourceMapV3): Promise<void> {
const cache = await this.
await cache.put(SourceMapCache.
}
async get(debugId: DebugId): Promise<SourceMapV3|null> {
const cache = await this.
const response = await cache.match(SourceMapCache.
return await response?.json() ?? null;
}
async
if (this.
return await this.
}
this.
return await this.
}
/** The Cache API only allows URL as keys, so we construct a simple one. Given that we have our own cache, we have no risk of conflicting URLs */
static
return 'http://debug.id/' + encodeURIComponent(debugId);
}
async disposeForTest(): Promise<void> {
await window.caches.delete(this.
}
}
const IN_MEMORY_INSTANCE = new (class implements Pick<SourceMapCache, 'get'|'set'|'disposeForTest'> {
readonly
async set(debugId: DebugId, sourceMap: SourceMapV3): Promise<void> {
this.
}
async get(debugId: DebugId): Promise<SourceMapV3|null> {
return this.
}
async disposeForTest(): Promise<void> {
// Do nothing.
}
})();