-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
26 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import { Blob } from "rcompat/fs"; | ||
import { URL } from "rcompat/http"; | ||
import { Blob, s_streamable } from "rcompat/fs"; | ||
import { URL, Response } from "rcompat/http"; | ||
import { identity } from "rcompat/function"; | ||
import { text, json, stream, redirect } from "primate"; | ||
import is_response_duck from "./duck.js"; | ||
|
||
const is_text = value => { | ||
if (typeof value === "string") { | ||
return text(value); | ||
} | ||
const not_found = value => { | ||
throw new Error(`no handler found for ${value}`); | ||
}; | ||
|
||
const is_text = value => typeof value === "string"; | ||
const is_non_null_object = value => typeof value === "object" && value !== null; | ||
const is_object = value => is_non_null_object(value) | ||
? json(value) : is_text(value); | ||
const is_response = value => is_response_duck(value) | ||
? _ => value : is_object(value); | ||
const is_stream = value => value instanceof ReadableStream | ||
? stream(value) : is_response(value); | ||
const is_blob = value => value instanceof Blob | ||
? stream(value.stream()) : is_stream(value); | ||
const is_URL = value => value instanceof URL | ||
? redirect(value.href) : is_blob(value); | ||
const guess = value => is_URL(value); | ||
const is_instance = of => value => value instanceof of; | ||
const is_response = is_instance(Response); | ||
const is_global_response = is_instance(globalThis.Response); | ||
const is_streamable = | ||
value => value instanceof Blob || value?.streamable === s_streamable; | ||
|
||
// [if, then] | ||
const guesses = [ | ||
[is_instance(URL), redirect], | ||
[is_streamable, value => stream(value.stream())], | ||
[is_instance(ReadableStream), stream], | ||
[value => is_response(value) || is_global_response(value), identity], | ||
[is_non_null_object, json], | ||
[is_text, text], | ||
[not_found, identity], | ||
]; | ||
|
||
const guess = value => guesses.find(([check]) => check(value))?.[1](value); | ||
|
||
export default result => typeof result === "function" ? result : guess(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters