-
-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for react-native
platform
#593
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth throwing an error with an explanation when
mmkvStorage
is missing, that will prevent any cryptic errors further down the line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@absidue makes sense! I've just left a #593 (comment) and if this is the only issue that is blocking as per @LuanRT consideration – I will get it fixed asap