38 lines
1.1 KiB
Dart
38 lines
1.1 KiB
Dart
import 'dart:js_interop';
|
|
|
|
import 'artifact_viewer_fetch.dart';
|
|
|
|
@JS('fetch')
|
|
external JSPromise<_FetchResponse> _fetch(JSString input, _FetchInit init);
|
|
|
|
extension type _FetchInit._(JSObject _) implements JSObject {
|
|
external factory _FetchInit({JSString credentials});
|
|
}
|
|
|
|
extension type _FetchResponse._(JSObject _) implements JSObject {
|
|
external int get status;
|
|
|
|
external JSPromise<JSArrayBuffer> arrayBuffer();
|
|
}
|
|
|
|
/// Web fetch for an already resolved same-origin artifact.
|
|
///
|
|
/// `credentials: 'same-origin'` attaches the browser's credentials only to this
|
|
/// server's origin; the viewer never asks for another origin. The status is
|
|
/// returned verbatim so a failed preview can report the HTTP failure.
|
|
Future<ArtifactFetchResult> fetchArtifact(Uri uri) async {
|
|
try {
|
|
final response = await _fetch(
|
|
uri.toString().toJS,
|
|
_FetchInit(credentials: 'same-origin'.toJS),
|
|
).toDart;
|
|
final buffer = await response.arrayBuffer().toDart;
|
|
return ArtifactFetchResult(
|
|
status: response.status,
|
|
bytes: buffer.toDart.asUint8List(),
|
|
);
|
|
} catch (error) {
|
|
throw ArtifactFetchException('$error');
|
|
}
|
|
}
|