refactor!: cleanup platform support (#306)

* refactor!: cleanup platform support

* chore: lint

* fix: web platform

* feat: provide UniversalCache

Provide UniversalCache as a wrapper around Platform.shim.Cache.

* fix: invalid import

* refactor: remove isolated-vm support

* fix: type info

* refactor: cleanup exports

* fix: mark jintr as external dependency

In the bundled CJS node build, mark jintr as external.

* chore: add additional exports

web exports provide a way to select web implementation manually without
relying on the bundler to select it correctly from the "exports" field

web points to src/platform/web.js
web.bundle points to bundle/browser.js
web.bundle.browser points to bundle/browser.min.js

agnostic exports provide users of the library to provide their own
platform implementation without first importing the default one.

agnostic points to src/platform/lib.ts

* fix: toDash on web

* revert: eval is synchronous

* fix: use serializeDOM in FormatUtils

* ci: automate releases with `release-please`

* chore: clean up workflow files

* ci: fix NPM publish action

---------

Co-authored-by: LuanRT <luan.lrt4@gmail.com>
This commit is contained in:
Daniel Wykerd
2023-02-12 09:21:44 +02:00
committed by GitHub
parent a69e43bf3a
commit 2ccbe2ce62
504 changed files with 11184 additions and 6279 deletions

View File

@@ -1,8 +1,13 @@
import Session, { Context } from '../core/Session';
import Constants from './Constants';
import { generateSidAuth, getRandomUserAgent, getStringBetweenStrings, InnertubeError, isServer } from './Utils';
export type FetchFunction = typeof fetch;
import Session, { Context } from '../core/Session.js';
import { FetchFunction } from '../types/PlatformShim.js';
import Constants from './Constants.js';
import {
Platform,
generateSidAuth,
getRandomUserAgent,
getStringBetweenStrings,
InnertubeError
} from './Utils.js';
export interface HTTPClientInit {
baseURL?: string;
@@ -16,7 +21,7 @@ export default class HTTPClient {
constructor(session: Session, cookie?: string, fetch?: FetchFunction) {
this.#session = session;
this.#cookie = cookie;
this.#fetch = fetch || globalThis.fetch;
this.#fetch = fetch || Platform.shim.fetch;
}
get fetch_function(): FetchFunction {
@@ -40,12 +45,12 @@ export default class HTTPClient {
const headers =
init?.headers ||
(input instanceof Request ? input.headers : new Headers()) ||
new Headers();
(input instanceof Platform.shim.Request ? input.headers : new Platform.shim.Headers()) ||
new Platform.shim.Headers();
const body = init?.body || (input instanceof Request ? input.body : undefined);
const body = init?.body || (input instanceof Platform.shim.Request ? input.body : undefined);
const request_headers = new Headers(headers);
const request_headers = new Platform.shim.Headers(headers);
request_headers.set('Accept', '*/*');
request_headers.set('Accept-Language', '*');
@@ -53,7 +58,7 @@ export default class HTTPClient {
request_headers.set('x-origin', request_url.origin);
request_headers.set('x-youtube-client-version', this.#session.context.client.clientVersion || '');
if (isServer()) {
if (Platform.shim.server) {
request_headers.set('User-Agent', getRandomUserAgent('desktop'));
request_headers.set('origin', request_url.origin);
}
@@ -115,13 +120,13 @@ export default class HTTPClient {
}
}
const request = new Request(request_url, input instanceof Request ? input : init);
const request = new Platform.shim.Request(request_url, input instanceof Platform.shim.Request ? input : init);
const response = await this.#fetch(request, {
body: request_body,
headers: request_headers,
credentials: 'include',
redirect: input instanceof Request ? input.redirect : init?.redirect || 'follow'
redirect: input instanceof Platform.shim.Request ? input.redirect : init?.redirect || 'follow'
});
// Check if 2xx