mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-13 01:22:11 +00:00
feat: add support of cloudflare workers (#596)
This commit is contained in:
18
examples/cloudflare-worker/package.json
Normal file
18
examples/cloudflare-worker/package.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "cf-worker",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"deploy": "wrangler deploy",
|
||||||
|
"dev": "wrangler dev",
|
||||||
|
"start": "wrangler dev"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@cloudflare/workers-types": "^4.20240208.0",
|
||||||
|
"typescript": "^5.0.4",
|
||||||
|
"wrangler": "^3.0.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"youtubei.js": "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
19
examples/cloudflare-worker/src/index.ts
Normal file
19
examples/cloudflare-worker/src/index.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { Innertube } from "youtubei.js/cf-worker";
|
||||||
|
|
||||||
|
export interface Env {}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
async fetch(
|
||||||
|
request: Request,
|
||||||
|
env: Env,
|
||||||
|
ctx: ExecutionContext
|
||||||
|
): Promise<Response> {
|
||||||
|
// cannot initialize Innertube in global scope as it makes fetch requests
|
||||||
|
const yt = await Innertube.create();
|
||||||
|
|
||||||
|
const video = await yt.getInfo("jNQXAC9IVRw");
|
||||||
|
console.log("Video title is", video.basic_info.title);
|
||||||
|
|
||||||
|
return new Response("Hello World!");
|
||||||
|
},
|
||||||
|
};
|
||||||
19
examples/cloudflare-worker/tsconfig.json
Normal file
19
examples/cloudflare-worker/tsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2021",
|
||||||
|
"lib": ["es2021"],
|
||||||
|
"jsx": "react",
|
||||||
|
"module": "es2022",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"types": ["@cloudflare/workers-types/2023-07-01"],
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"allowJs": true,
|
||||||
|
"checkJs": false,
|
||||||
|
"noEmit": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true
|
||||||
|
}
|
||||||
|
}
|
||||||
3
examples/cloudflare-worker/wrangler.toml
Normal file
3
examples/cloudflare-worker/wrangler.toml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
name = "cf-worker-youtubei"
|
||||||
|
main = "src/index.ts"
|
||||||
|
compatibility_date = "2024-02-08"
|
||||||
10
package.json
10
package.json
@@ -17,6 +17,9 @@
|
|||||||
],
|
],
|
||||||
"web.bundle.min": [
|
"web.bundle.min": [
|
||||||
"./dist/src/platform/lib.d.ts"
|
"./dist/src/platform/lib.d.ts"
|
||||||
|
],
|
||||||
|
"cf-worker": [
|
||||||
|
"./dist/src/platform/lib.d.ts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -46,6 +49,10 @@
|
|||||||
"./web.bundle.min": {
|
"./web.bundle.min": {
|
||||||
"types": "./dist/src/platform/lib.d.ts",
|
"types": "./dist/src/platform/lib.d.ts",
|
||||||
"default": "./bundle/browser.min.js"
|
"default": "./bundle/browser.min.js"
|
||||||
|
},
|
||||||
|
"./cf-worker": {
|
||||||
|
"types": "./dist/src/platform/lib.d.ts",
|
||||||
|
"default": "./dist/src/platform/cf-worker.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"author": "LuanRT <luan.lrt4@gmail.com> (https://github.com/LuanRT)",
|
"author": "LuanRT <luan.lrt4@gmail.com> (https://github.com/LuanRT)",
|
||||||
@@ -68,7 +75,7 @@
|
|||||||
"test": "npx jest --verbose",
|
"test": "npx jest --verbose",
|
||||||
"lint": "npx eslint ./src",
|
"lint": "npx eslint ./src",
|
||||||
"lint:fix": "npx eslint --fix ./src",
|
"lint:fix": "npx eslint --fix ./src",
|
||||||
"build": "npm run build:parser-map && npm run build:proto && npm run build:esm && npm run bundle:node && npm run bundle:browser && npm run bundle:browser:prod",
|
"build": "npm run build:parser-map && npm run build:proto && npm run build:esm && npm run bundle:node && npm run bundle:browser && npm run bundle:browser:prod && npm run bundle:cf-worker",
|
||||||
"build:parser-map": "node ./scripts/gen-parser-map.mjs",
|
"build:parser-map": "node ./scripts/gen-parser-map.mjs",
|
||||||
"build:proto": "npx pb-gen-ts --entry-path=\"src/proto\" --out-dir=\"src/proto/generated\" --ext-in-import=\".js\"",
|
"build:proto": "npx pb-gen-ts --entry-path=\"src/proto\" --out-dir=\"src/proto/generated\" --ext-in-import=\".js\"",
|
||||||
"build:esm": "npx tspc",
|
"build:esm": "npx tspc",
|
||||||
@@ -76,6 +83,7 @@
|
|||||||
"bundle:node": "npx esbuild ./dist/src/platform/node.js --bundle --target=node10 --keep-names --format=cjs --platform=node --outfile=./bundle/node.cjs --external:jintr --external:undici --external:linkedom --external:tslib --sourcemap --banner:js=\"/* eslint-disable */\"",
|
"bundle:node": "npx esbuild ./dist/src/platform/node.js --bundle --target=node10 --keep-names --format=cjs --platform=node --outfile=./bundle/node.cjs --external:jintr --external:undici --external:linkedom --external:tslib --sourcemap --banner:js=\"/* eslint-disable */\"",
|
||||||
"bundle:browser": "npx esbuild ./dist/src/platform/web.js --banner:js=\"/* eslint-disable */\" --bundle --target=chrome58 --keep-names --format=esm --sourcemap --define:global=globalThis --conditions=module --outfile=./bundle/browser.js --platform=browser",
|
"bundle:browser": "npx esbuild ./dist/src/platform/web.js --banner:js=\"/* eslint-disable */\" --bundle --target=chrome58 --keep-names --format=esm --sourcemap --define:global=globalThis --conditions=module --outfile=./bundle/browser.js --platform=browser",
|
||||||
"bundle:browser:prod": "npm run bundle:browser -- --outfile=./bundle/browser.min.js --minify",
|
"bundle:browser:prod": "npm run bundle:browser -- --outfile=./bundle/browser.min.js --minify",
|
||||||
|
"bundle:cf-worker": "npx esbuild ./dist/src/platform/cf-worker.js --banner:js=\"/* eslint-disable */\" --bundle --target=es2020 --keep-names --format=esm --sourcemap --define:global=globalThis --conditions=module --outfile=./bundle/cf-worker.js --platform=node",
|
||||||
"prepare": "npm run build",
|
"prepare": "npm run build",
|
||||||
"watch": "npx tsc --watch"
|
"watch": "npx tsc --watch"
|
||||||
},
|
},
|
||||||
|
|||||||
71
src/platform/cf-worker.ts
Normal file
71
src/platform/cf-worker.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import type { ICache } from '../types/Cache.js';
|
||||||
|
import { Platform } from '../utils/Utils.js';
|
||||||
|
import evaluate from './jsruntime/jinter.js';
|
||||||
|
import { $INLINE_JSON } from 'ts-transformer-inline-file';
|
||||||
|
import sha1Hash from './polyfills/web-crypto.js';
|
||||||
|
|
||||||
|
const { homepage, version, bugs } = $INLINE_JSON('../../package.json');
|
||||||
|
const repo_url = homepage?.split('#')[0];
|
||||||
|
|
||||||
|
class Cache implements ICache {
|
||||||
|
#persistent_directory: string;
|
||||||
|
#persistent: boolean;
|
||||||
|
|
||||||
|
constructor(persistent = false, persistent_directory?: string) {
|
||||||
|
this.#persistent_directory = persistent_directory || '';
|
||||||
|
this.#persistent = persistent;
|
||||||
|
}
|
||||||
|
|
||||||
|
get cache_dir() {
|
||||||
|
return this.#persistent ? this.#persistent_directory : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(key: string) {
|
||||||
|
const cache = await caches.open('yt-api');
|
||||||
|
|
||||||
|
const response = await cache.match(key);
|
||||||
|
if (!response) return undefined;
|
||||||
|
|
||||||
|
return response.arrayBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
async set(key: string, value: ArrayBuffer) {
|
||||||
|
|
||||||
|
const cache = await caches.open('yt-api');
|
||||||
|
cache.put(key, new Response(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove(key: string) {
|
||||||
|
const cache = await caches.open('yt-api');
|
||||||
|
|
||||||
|
await cache.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Platform.load({
|
||||||
|
runtime: 'cf-worker',
|
||||||
|
info: {
|
||||||
|
version: version,
|
||||||
|
bugs_url: bugs?.url || `${repo_url}/issues`,
|
||||||
|
repo_url
|
||||||
|
},
|
||||||
|
server: true,
|
||||||
|
Cache: Cache,
|
||||||
|
sha1Hash,
|
||||||
|
uuidv4() {
|
||||||
|
return crypto.randomUUID();
|
||||||
|
},
|
||||||
|
eval: evaluate,
|
||||||
|
fetch: fetch.bind(globalThis),
|
||||||
|
Request: Request,
|
||||||
|
Response: Response,
|
||||||
|
Headers: Headers,
|
||||||
|
FormData: FormData,
|
||||||
|
File: File,
|
||||||
|
ReadableStream: ReadableStream,
|
||||||
|
CustomEvent: CustomEvent
|
||||||
|
});
|
||||||
|
|
||||||
|
export * from './lib.js';
|
||||||
|
import Innertube from './lib.js';
|
||||||
|
export default Innertube;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { ICacheConstructor } from './Cache.js';
|
import type { ICacheConstructor } from './Cache.js';
|
||||||
|
|
||||||
export type Runtime = 'deno' | 'node' | 'browser' | 'unknown';
|
export type Runtime = 'deno' | 'node' | 'browser' | 'cf-worker' | 'unknown';
|
||||||
|
|
||||||
export type FetchFunction = typeof fetch;
|
export type FetchFunction = typeof fetch;
|
||||||
|
|
||||||
|
|||||||
@@ -140,8 +140,8 @@ export default class HTTPClient {
|
|||||||
const response = await this.#fetch(request, {
|
const response = await this.#fetch(request, {
|
||||||
body: request_body,
|
body: request_body,
|
||||||
headers: request_headers,
|
headers: request_headers,
|
||||||
credentials: 'include',
|
redirect: input instanceof Platform.shim.Request ? input.redirect : init?.redirect || 'follow',
|
||||||
redirect: input instanceof Platform.shim.Request ? input.redirect : init?.redirect || 'follow'
|
...(Platform.shim.runtime !== 'cf-worker' ? { credentials: 'include' } : {})
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if 2xx
|
// Check if 2xx
|
||||||
|
|||||||
Reference in New Issue
Block a user