face-api/src/Platform.ts

27 lines
706 B
TypeScript
Raw Normal View History

2020-10-11 18:41:17 +02:00
export class PlatformBrowser {
private textEncoder: TextEncoder;
2020-12-27 00:35:17 +01:00
// eslint-disable-next-line no-undef
2020-10-11 18:41:17 +02:00
fetch(path: string, init?: RequestInit): Promise<Response> {
return fetch(path, init);
}
now(): number {
return performance.now();
}
encode(text: string, encoding: string): Uint8Array {
if (encoding !== 'utf-8' && encoding !== 'utf8') {
2020-12-27 00:35:17 +01:00
throw new Error(`Browser's encoder only supports utf-8, but got ${encoding}`);
2020-10-11 18:41:17 +02:00
}
if (this.textEncoder == null) {
this.textEncoder = new TextEncoder();
}
return this.textEncoder.encode(text);
}
2020-12-27 00:35:17 +01:00
2020-10-11 18:41:17 +02:00
decode(bytes: Uint8Array, encoding: string): string {
return new TextDecoder(encoding).decode(bytes);
}
}