-
Notifications
You must be signed in to change notification settings - Fork 15k
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
feat: add protocol.handle #36674
Merged
Merged
feat: add protocol.handle #36674
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
02d618d
feat: add protocol.registerProtocol
nornagon eec9404
feat: net.request() supports custom protocols
nornagon dcd3c5b
add missing fixture
nornagon 1a47cc3
net requests can follow cross-scheme redirects
nornagon 6d716bf
fix request body
nornagon 5e9d142
fix redirects not being properly aborted, add tests
nornagon 47f04ef
wip
nornagon e92da94
remove wip handleProtocol code
nornagon a8d14df
lint
nornagon 17543eb
Merge branch 'protocol-free' into protocol-handle
nornagon c7534f4
merge net-headers
nornagon 2c7df1a
feat: implement protocol.handle
nornagon 8d8d0aa
Merge branch 'main' into protocol-handle
nornagon 1ba6c4c
Merge branch 'main' into protocol-handle
nornagon cb8eddf
undo changes to yarn.lock from merge
nornagon c4e51fc
Merge remote-tracking branch 'origin/protocol-handle' into protocol-h…
nornagon d60a769
Merge remote-tracking branch 'origin/main' into protocol-handle
nornagon 613de94
remove logging
nornagon 64ee235
use Request/Response in protocol.handle
nornagon 373e715
Merge remote-tracking branch 'origin/main' into protocol-handle
nornagon 3e8f598
remove old structure
nornagon 422ea88
fix filenames.auto.gni
nornagon b4c66bd
add const_cast note
nornagon f2d7c0a
add isProtocolHandled
nornagon 0044604
update docs
nornagon f99ceab
catch errors in handler body
nornagon c768632
fix test
nornagon f65ea4c
support bypassing custom handlers
nornagon e423401
Merged in branch 'main' to fix mergability
electron-patch-conflict-fixer[bot] f770a1b
fix patches
nornagon d5d9671
extract kBypassCustomProtocolHandlers to a constant
nornagon d6656a6
clean up tests
nornagon 7da0ea8
net.fetch doesn't send accept header
nornagon 0a8ab74
wipish: support passing on sniffed mime type
nornagon edd9f96
add perf test
nornagon 5b5da4a
document bypassCustomProtocolHandlers
nornagon 2d29368
more URL tests
nornagon 1d95273
fix webRequest/protocol.handle conflict on streaming upload
nornagon b72ee6a
remove hostAndPort test
nornagon ab63c10
disable perf test on linux
nornagon File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,12 @@ | ||
# HandleProtocolResponse Object | ||
|
||
* `error` Integer (optional) - When assigned, the request will fail with the | ||
error defined by `error`. For the available error numbers you can use, see | ||
the [net error list][net-error]. | ||
* `statusCode` number (optional) - The HTTP response code. If not specified, | ||
the response will be sent with status code 200. | ||
* `headers` Record<string, string | string[]> (optional) - The response headers. | ||
* `body` string | Buffer | ReadableStream | null (optional) - the body of the | ||
response. If null or undefined, the body will be empty. | ||
nornagon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[net-error]: https://source.chromium.org/chromium/chromium/src/+/main:net/base/net_error_list.h |
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 |
---|---|---|
@@ -1,33 +1,56 @@ | ||
import { app, session } from 'electron/main'; | ||
import { session } from 'electron/main'; | ||
|
||
// Global protocol APIs. | ||
const protocol = process._linkedBinding('electron_browser_protocol'); | ||
|
||
// Fallback protocol APIs of default session. | ||
Object.setPrototypeOf(protocol, new Proxy({}, { | ||
get (_target, property) { | ||
if (!app.isReady()) return; | ||
|
||
const protocol = session.defaultSession!.protocol; | ||
if (!Object.prototype.hasOwnProperty.call(protocol, property)) return; | ||
|
||
// Returning a native function directly would throw error. | ||
return (...args: any[]) => (protocol[property as keyof Electron.Protocol] as Function)(...args); | ||
}, | ||
|
||
ownKeys () { | ||
if (!app.isReady()) return []; | ||
return Reflect.ownKeys(session.defaultSession!.protocol); | ||
}, | ||
|
||
has: (target, property: string) => { | ||
if (!app.isReady()) return false; | ||
return Reflect.has(session.defaultSession!.protocol, property); | ||
}, | ||
|
||
getOwnPropertyDescriptor () { | ||
return { configurable: true, enumerable: true }; | ||
} | ||
})); | ||
const { registerSchemesAsPrivileged, getStandardSchemes, Protocol } = process._linkedBinding('electron_browser_protocol'); | ||
|
||
const ERR_UNEXPECTED = -9; | ||
|
||
const isBuiltInScheme = (scheme: string) => scheme === 'http' || scheme === 'https'; | ||
|
||
Protocol.prototype.handle = function (this: Electron.Protocol, scheme: string, handler: (req: any) => Promise<any>) { | ||
const register = isBuiltInScheme(scheme) ? this.interceptProtocol : this.registerProtocol; | ||
const success = register.call(this, scheme, async (req: any, cb: any) => { | ||
const res = await handler(req); | ||
if (!res || typeof res !== 'object') { | ||
return cb({ error: ERR_UNEXPECTED }); | ||
} | ||
const { error, body, headers, statusCode } = res; | ||
cb({ | ||
data: body, | ||
headers, | ||
error, | ||
statusCode | ||
}); | ||
}); | ||
if (!success) throw new Error(`Failed to register protocol: ${scheme}`); | ||
}; | ||
|
||
Protocol.prototype.unhandle = function (this: Electron.Protocol, scheme: string) { | ||
const unregister = isBuiltInScheme(scheme) ? this.uninterceptProtocol : this.unregisterProtocol; | ||
if (!unregister.call(this, scheme)) { throw new Error(`Failed to unhandle protocol: ${scheme}`); } | ||
}; | ||
|
||
const protocol = { | ||
registerSchemesAsPrivileged, | ||
getStandardSchemes, | ||
registerStringProtocol: (...args) => session.defaultSession.protocol.registerStringProtocol(...args), | ||
registerBufferProtocol: (...args) => session.defaultSession.protocol.registerBufferProtocol(...args), | ||
registerStreamProtocol: (...args) => session.defaultSession.protocol.registerStreamProtocol(...args), | ||
registerFileProtocol: (...args) => session.defaultSession.protocol.registerFileProtocol(...args), | ||
registerHttpProtocol: (...args) => session.defaultSession.protocol.registerHttpProtocol(...args), | ||
registerProtocol: (...args) => session.defaultSession.protocol.registerProtocol(...args), | ||
unregisterProtocol: (...args) => session.defaultSession.protocol.unregisterProtocol(...args), | ||
isProtocolRegistered: (...args) => session.defaultSession.protocol.isProtocolRegistered(...args), | ||
interceptStringProtocol: (...args) => session.defaultSession.protocol.interceptStringProtocol(...args), | ||
interceptBufferProtocol: (...args) => session.defaultSession.protocol.interceptBufferProtocol(...args), | ||
interceptStreamProtocol: (...args) => session.defaultSession.protocol.interceptStreamProtocol(...args), | ||
interceptFileProtocol: (...args) => session.defaultSession.protocol.interceptFileProtocol(...args), | ||
interceptHttpProtocol: (...args) => session.defaultSession.protocol.interceptHttpProtocol(...args), | ||
interceptProtocol: (...args) => session.defaultSession.protocol.interceptProtocol(...args), | ||
uninterceptProtocol: (...args) => session.defaultSession.protocol.uninterceptProtocol(...args), | ||
isProtocolIntercepted: (...args) => session.defaultSession.protocol.isProtocolIntercepted(...args), | ||
handle: (...args) => session.defaultSession.protocol.handle(...args), | ||
unhandle: (...args) => session.defaultSession.protocol.unhandle(...args) | ||
} as typeof Electron.protocol; | ||
|
||
export default protocol; |
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
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.
This change is incidental to this PR, but it happens to be true (because
ClientRequest
inherits fromWritable
).