-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Platform): Add support for
react-native
platform (#593)
- Loading branch information
Showing
5 changed files
with
190 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
Making the `Youtube.js` work in React-Native involves polyfilling number of APIs and using MMKV as an underlying storage for cache. | ||
Below configuration is tested with `"react-native": "0.73.2` | ||
|
||
Following polyfills are required to be installed: | ||
|
||
``` | ||
"base-64": "^1.0.0", | ||
"event-target-polyfill": "^0.0.4", | ||
"react-native-mmkv": "^2.11.0", | ||
"react-native-url-polyfill": "^2.0.0", | ||
"text-encoding-polyfill": "^0.6.7", | ||
"web-streams-polyfill": "^3.3.2" | ||
``` | ||
|
||
And following `devDependencies`: | ||
``` | ||
"@types/base-64": "^1.0.2", | ||
"@babel/plugin-syntax-import-attributes": "^7.23.3", | ||
"@babel/plugin-transform-export-namespace-from": "^7.23.4", | ||
``` | ||
|
||
Adding `unstable_enablePackageExports: true` flag to Metro is required as well, because `Youtube.js` uses package exports feature in `package.json`. | ||
|
||
`metro.config.js`: | ||
```js | ||
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config'); | ||
|
||
const config = { | ||
resolver: { | ||
sourceExts: ['jsx', 'js', 'ts', 'tsx', 'cjs', 'json', 'd.ts'], | ||
unstable_enablePackageExports: true, | ||
}, | ||
}; | ||
|
||
module.exports = mergeConfig(getDefaultConfig(__dirname), config); | ||
``` | ||
|
||
`babel.config.js`: | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
['@babel/plugin-syntax-import-attributes', {deprecatedAssertSyntax: true}], | ||
'@babel/plugin-transform-export-namespace-from', | ||
], | ||
presets: ['module:@react-native/babel-preset'], | ||
}; | ||
``` | ||
|
||
Below is the sample file that loads all of the polyfills, makes `MMKV` storage globally available (to be used by `Cache` in `Youtube.js`) and inits the `Innertube` instance. | ||
|
||
```typescript | ||
// === START === Making Youtube.js work | ||
import 'event-target-polyfill'; | ||
import 'web-streams-polyfill'; | ||
import 'text-encoding-polyfill'; | ||
import 'react-native-url-polyfill/auto'; | ||
import {decode, encode} from 'base-64'; | ||
|
||
if (!global.btoa) { | ||
global.btoa = encode; | ||
} | ||
|
||
if (!global.atob) { | ||
global.atob = decode; | ||
} | ||
|
||
import {MMKV} from 'react-native-mmkv'; | ||
// @ts-expect-error to avoid typings' fuss | ||
global.mmkvStorage = MMKV as any; | ||
|
||
// See https://github.com/nodejs/node/issues/40678#issuecomment-1126944677 | ||
class CustomEvent extends Event { | ||
#detail; | ||
|
||
constructor(type: string, options?: CustomEventInit<any[]>) { | ||
super(type, options); | ||
this.#detail = options?.detail ?? null; | ||
} | ||
|
||
get detail() { | ||
return this.#detail; | ||
} | ||
} | ||
|
||
global.CustomEvent = CustomEvent as any; | ||
|
||
// === END === Making Youtube.js work | ||
|
||
import Innertube, {UniversalCache} from 'youtubei.js'; | ||
|
||
let innertube: Promise<Innertube> = Innertube.create({ | ||
cache: new UniversalCache(false), | ||
generate_session_locally: true, | ||
}); | ||
|
||
export default innertube; | ||
|
||
``` |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// React Native Platform Support | ||
import type { ICache } from '../types/Cache.js'; | ||
import { Platform } from '../utils/Utils.js'; | ||
import sha1Hash from './polyfills/web-crypto.js'; | ||
import package_json from '../../package.json' assert { type: 'json' }; | ||
import evaluate from './jsruntime/jinter.js'; | ||
|
||
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 : ''; | ||
} | ||
|
||
#getStorage() { | ||
const storage = new ((globalThis as any).mmkvStorage as any)({ id: 'InnertubeCache' }); | ||
return storage; | ||
} | ||
|
||
async get(key: string) { | ||
const storage = this.#getStorage(); | ||
return storage.getBuffer(key)?.buffer; | ||
} | ||
|
||
async set(key: string, value: ArrayBuffer) { | ||
const storage = this.#getStorage(); | ||
storage.set(key, new Uint8Array(value)); | ||
} | ||
|
||
async remove(key: string) { | ||
const storage = this.#getStorage(); | ||
storage.delete(key); | ||
} | ||
} | ||
|
||
Platform.load({ | ||
runtime: 'react-native', | ||
server: false, | ||
info: { | ||
version: package_json.version, | ||
bugs_url: package_json.bugs.url, | ||
repo_url: package_json.homepage.split('#')[0] | ||
}, | ||
Cache: Cache, | ||
sha1Hash, | ||
uuidv4() { | ||
if (globalThis.crypto?.randomUUID()) { | ||
return globalThis.crypto.randomUUID(); | ||
} | ||
|
||
// See https://stackoverflow.com/a/2117523 | ||
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (cc) => { | ||
const c = parseInt(cc); | ||
return ( | ||
c ^ | ||
(window.crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))) | ||
).toString(16); | ||
}); | ||
}, | ||
eval: evaluate, | ||
fetch: globalThis.fetch, | ||
Request: globalThis.Request, | ||
Response: globalThis.Response, | ||
Headers: globalThis.Headers, | ||
FormData: globalThis.FormData, | ||
File: globalThis.File, | ||
ReadableStream: globalThis.ReadableStream, | ||
CustomEvent: globalThis.CustomEvent | ||
}); | ||
|
||
export * from './lib.js'; | ||
import Innertube from './lib.js'; | ||
export default Innertube; |
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