-
Notifications
You must be signed in to change notification settings - Fork 6k
feat(storageState): add OPFS support #41420
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,8 @@ export type SerializedValue = | |
| { ref: number } | | ||
| { h: number } | | ||
| { ta: { b: string, k: TypedArrayKind } } | | ||
| { ab: { b: string } }; | ||
| { ab: { b: string } } | | ||
| { f: { b: string, n: string, t: string, m: number } }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to serialize and restore opfs without this. |
||
|
|
||
| type HandleOrValue = { h: number } | { fallThrough: any }; | ||
|
|
||
|
|
@@ -87,6 +88,14 @@ function isArrayBuffer(obj: any): obj is ArrayBuffer { | |
| } | ||
| } | ||
|
|
||
| function isFile(obj: any): obj is File { | ||
| try { | ||
| return obj instanceof File || Object.prototype.toString.call(obj) === '[object File]'; | ||
| } catch (error) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| const typedArrayConstructors: Record<TypedArrayKind, Function> = { | ||
| i8: Int8Array, | ||
| ui8: Uint8Array, | ||
|
|
@@ -181,6 +190,16 @@ export function parseEvaluationResultValue(value: SerializedValue, handles: any[ | |
| return base64ToTypedArray(value.ta.b, typedArrayConstructors[value.ta.k]); | ||
| if ('ab' in value) | ||
| return base64ToTypedArray(value.ab.b, Uint8Array).buffer; | ||
| if ('f' in value) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did it like this so that we would get File object support in IndexedDB as a side effect of the PR, but yeah it's not strictly necessary |
||
| return new File( | ||
| [base64ToTypedArray(value.f.b, Uint8Array)], | ||
| value.f.n, | ||
| { | ||
| lastModified: value.f.m, | ||
| type: value.f.t | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|
|
@@ -189,6 +208,18 @@ export function serializeAsCallArgument(value: any, handleSerializer: (value: an | |
| return serialize(value, handleSerializer, { visited: new Map(), lastId: 0 }); | ||
| } | ||
|
|
||
| // Getting a File object's contents requires async | ||
| export async function serializeFile(value: File): Promise<Extract<SerializedValue, { f: any; }>> { | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
| return { | ||
| f: { | ||
| b: typedArrayToBase64(await value.bytes()), | ||
| n: value.name, | ||
| m: value.lastModified, | ||
| t: value.type | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function serialize(value: any, handleSerializer: (value: any) => HandleOrValue, visitorInfo: VisitorInfo): SerializedValue { | ||
| if (value && typeof value === 'object') { | ||
| // eslint-disable-next-line no-restricted-globals | ||
|
|
@@ -257,6 +288,8 @@ function innerSerialize(value: any, handleSerializer: (value: any) => HandleOrVa | |
| } | ||
| if (isArrayBuffer(value)) | ||
| return { ab: { b: typedArrayToBase64(new Uint8Array(value)) } }; | ||
| if (isFile(value)) | ||
| throw new Error('File serialization is asynchronous and must be done separately from serializeAsCallArgument'); | ||
|
|
||
| const id = visitorInfo.visited.get(value); | ||
| if (id) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -336,7 +336,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel | |
| } | ||
|
|
||
| async storageState(params: channels.BrowserContextStorageStateParams, progress: Progress): Promise<channels.BrowserContextStorageStateResult> { | ||
| return await this._context.storageState(progress, params.indexedDB, params.credentials); | ||
| return await this._context.storageState(progress, params.indexedDB, params.credentials, params.opfs); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pass params as object |
||
| } | ||
|
|
||
| async setStorageState(params: channels.BrowserContextSetStorageStateParams, progress: Progress): Promise<void> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,7 +212,7 @@ export class APIRequestContextDispatcher extends Dispatcher<APIRequestContext, c | |
| } | ||
|
|
||
| async storageState(params: channels.APIRequestContextStorageStateParams, progress: Progress): Promise<channels.APIRequestContextStorageStateResult> { | ||
| return await this._object.storageState(progress, params.indexedDB); | ||
| return await this._object.storageState(progress, params.indexedDB, params.opfs); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: params object here |
||
| } | ||
|
|
||
| async dispose(params: channels.APIRequestContextDisposeParams, progress: Progress): Promise<void> { | ||
|
|
||
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.
Let's use conservative types: