Skip to content

Commit 255c341

Browse files
committed
refactor(src): rework runtime, context isolation per bundle id, prepare for origin isolated service worker
1 parent 4eecb05 commit 255c341

File tree

266 files changed

+31039
-8904
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+31039
-8904
lines changed

api/README.md

Lines changed: 175 additions & 58 deletions
Large diffs are not rendered by default.

api/application.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ export function getCurrentWindowIndex () {
238238
* @param {boolean=} [opts.headless=false] - whether the window will be headless or not (no frame)
239239
* @param {string=} [opts.userScript=null] - A user script that will be injected into the window (desktop only)
240240
* @param {string[]=} [opts.protocolHandlers] - An array of protocol handler schemes to register with the new window (requires service worker)
241+
* @param {string=} [opts.resourcesDirectory]
242+
* @param {boolean=} [opts.shouldPreferServiceWorker=false]
241243
* @return {Promise<ApplicationWindow>}
242244
*/
243245
export async function createWindow (opts) {
@@ -262,7 +264,9 @@ export async function createWindow (opts) {
262264
backgroundColorDark: opts.backgroundColorDark ?? '',
263265
backgroundColorLight: opts.backgroundColorLight ?? '',
264266
utility: opts.utility ?? false,
267+
resourcesDirectory: opts.resourcesDirectory ?? '',
265268
shouldExitApplicationOnClose: opts.shouldExitApplicationOnClose ?? false,
269+
shouldPreferServiceWorker: Boolean(opts.shouldPreferServiceWorker ?? false),
266270
/**
267271
* @private
268272
* @type {number}

api/child_process/worker.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const propagateWorkerError = err => parentPort.postMessage({
1919
if (process.stdin) {
2020
process.stdin.on('data', async (data) => {
2121
const { id } = state
22-
const result = await ipc.write('child_process.spawn', { id }, data)
22+
const result = await ipc.write('childProcess.spawn', { id }, data)
2323

2424
if (result.err) {
2525
propagateWorkerError(result.err)
@@ -42,7 +42,7 @@ parentPort.onmessage = async ({ data: { id, method, args } }) => {
4242
stderr: opts?.stderr !== false
4343
}
4444

45-
const result = await ipc.send('child_process.spawn', params)
45+
const result = await ipc.send('childProcess.spawn', params)
4646

4747
if (result.err) {
4848
return propagateWorkerError(result.err)
@@ -66,25 +66,25 @@ parentPort.onmessage = async ({ data: { id, method, args } }) => {
6666

6767
if (!data || BigInt(data.id) !== state.id) return
6868

69-
if (source === 'child_process.spawn' && data.source === 'stdout') {
69+
if (source === 'childProcess.spawn' && data.source === 'stdout') {
7070
if (process.stdout) {
7171
process.stdout.write(buffer)
7272
}
7373
}
7474

75-
if (source === 'child_process.spawn' && data.source === 'stderr') {
75+
if (source === 'childProcess.spawn' && data.source === 'stderr') {
7676
if (process.stderr) {
7777
process.stderr.write(buffer)
7878
}
7979
}
8080

81-
if (source === 'child_process.spawn' && data.status === 'close') {
81+
if (source === 'childProcess.spawn' && data.status === 'close') {
8282
state.exitCode = data.code
8383
state.lifecycle = 'close'
8484
parentPort.postMessage({ method: 'state', args: [state] })
8585
}
8686

87-
if (source === 'child_process.spawn' && data.status === 'exit') {
87+
if (source === 'childProcess.spawn' && data.status === 'exit') {
8888
state.exitCode = data.code
8989
state.lifecycle = 'exit'
9090
parentPort.postMessage({ method: 'state', args: [state] })
@@ -93,7 +93,7 @@ parentPort.onmessage = async ({ data: { id, method, args } }) => {
9393
}
9494

9595
if (method === 'kill') {
96-
const result = await ipc.send('child_process.kill', {
96+
const result = await ipc.send('childProcess.kill', {
9797
id: state.id,
9898
signal: signal.getCode(args[0])
9999
})

api/commonjs/loader.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import URL from '../url.js'
1313
import fs from '../fs.js'
1414
import os from '../os.js'
1515

16-
const RUNTIME_SERVICE_WORKER_FETCH_MODE = 'Runtime-ServiceWorker-Fetch-Mode'
16+
// TODO(@jwerle): investigate if we really need this header back
17+
// const RUNTIME_SERVICE_WORKER_FETCH_MODE = 'Runtime-ServiceWorker-Fetch-Mode'
1718
const RUNTIME_REQUEST_SOURCE_HEADER = 'Runtime-Request-Source'
1819
const textDecoder = new TextDecoder()
1920

@@ -209,7 +210,9 @@ export class RequestStatus {
209210
}
210211

211212
if (
213+
application.config.commonjs_fscheck !== false &&
212214
os.platform() !== 'android' &&
215+
/^(socket:|https:)/.test(this.id) &&
213216
this.#request.id.includes(`://${application.config.meta_bundle_identifier}`)
214217
) {
215218
try {
@@ -227,10 +230,6 @@ export class RequestStatus {
227230
request.open('HEAD', this.#request.id, false)
228231
request.setRequestHeader(RUNTIME_REQUEST_SOURCE_HEADER, 'module')
229232

230-
if (globalThis.isServiceWorkerScope) {
231-
request.setRequestHeader(RUNTIME_SERVICE_WORKER_FETCH_MODE, 'ignore')
232-
}
233-
234233
if (this.#request?.loader) {
235234
const entries = this.#request.loader.headers.entries()
236235
for (const entry of entries) {
@@ -442,6 +441,7 @@ export class Request {
442441
}
443442

444443
if (
444+
application.config.commonjs_fscheck !== false &&
445445
os.platform() !== 'android' &&
446446
/^(socket:|https:)/.test(this.id) &&
447447
this.id.includes(`//${application.config.meta_bundle_identifier}/`)
@@ -460,10 +460,6 @@ export class Request {
460460
request.open('GET', this.id, false)
461461
request.setRequestHeader(RUNTIME_REQUEST_SOURCE_HEADER, 'module')
462462

463-
if (globalThis.isServiceWorkerScope) {
464-
request.setRequestHeader(RUNTIME_SERVICE_WORKER_FETCH_MODE, 'ignore')
465-
}
466-
467463
if (typeof options?.responseType === 'string') {
468464
request.responseType = options.responseType
469465
}

api/hooks.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,37 +115,39 @@ function dispatchReadyEvent (target) {
115115

116116
function proxyGlobalEvents (global, target) {
117117
for (const type of GLOBAL_EVENTS) {
118-
const globalObject = GLOBAL_TOP_LEVEL_EVENTS.includes(type)
119-
? global.top ?? global
120-
: global
121-
122-
addEventListener(globalObject, type, (event) => {
123-
const { type, data, detail = null, error } = event
124-
const { origin } = location
125-
126-
if (type === 'applicationurl') {
127-
dispatchEvent(target, new ApplicationURLEvent(type, {
128-
origin,
129-
data: event.data,
130-
url: event.url.toString()
131-
}))
132-
} else if (type === 'error' || error) {
133-
const { message, filename = import.meta.url || globalThis.location.href } = error || {}
134-
dispatchEvent(target, new ErrorEvent(type, {
135-
message,
136-
filename,
137-
error,
138-
detail,
139-
origin
140-
}))
141-
} else if (data || type === 'message') {
142-
dispatchEvent(target, new MessageEvent(type, event))
143-
} else if (detail) {
144-
dispatchEvent(target, new CustomEvent(type, event))
145-
} else {
146-
dispatchEvent(target, new Event(type, event))
147-
}
148-
})
118+
try {
119+
const globalObject = GLOBAL_TOP_LEVEL_EVENTS.includes(type)
120+
? global.top ?? global
121+
: global
122+
123+
addEventListener(globalObject, type, (event) => {
124+
const { type, data, detail = null, error } = event
125+
const { origin } = location
126+
127+
if (type === 'applicationurl') {
128+
dispatchEvent(target, new ApplicationURLEvent(type, {
129+
origin,
130+
data: event.data,
131+
url: event.url.toString()
132+
}))
133+
} else if (type === 'error' || error) {
134+
const { message, filename = import.meta.url || globalThis.location.href } = error || {}
135+
dispatchEvent(target, new ErrorEvent(type, {
136+
message,
137+
filename,
138+
error,
139+
detail,
140+
origin
141+
}))
142+
} else if (data || type === 'message') {
143+
dispatchEvent(target, new MessageEvent(type, event))
144+
} else if (detail) {
145+
dispatchEvent(target, new CustomEvent(type, event))
146+
} else {
147+
dispatchEvent(target, new Event(type, event))
148+
}
149+
})
150+
} catch (err) {}
149151
}
150152
}
151153

api/index.d.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7884,6 +7884,8 @@ declare module "socket:application" {
78847884
* @param {boolean=} [opts.headless=false] - whether the window will be headless or not (no frame)
78857885
* @param {string=} [opts.userScript=null] - A user script that will be injected into the window (desktop only)
78867886
* @param {string[]=} [opts.protocolHandlers] - An array of protocol handler schemes to register with the new window (requires service worker)
7887+
* @param {string=} [opts.resourcesDirectory]
7888+
* @param {boolean=} [opts.shouldPreferServiceWorker=false]
78877889
* @return {Promise<ApplicationWindow>}
78887890
*/
78897891
export function createWindow(opts: {
@@ -7913,6 +7915,8 @@ declare module "socket:application" {
79137915
headless?: boolean | undefined;
79147916
userScript?: string | undefined;
79157917
protocolHandlers?: string[] | undefined;
7918+
resourcesDirectory?: string | undefined;
7919+
shouldPreferServiceWorker?: boolean | undefined;
79167920
}): Promise<ApplicationWindow>;
79177921
/**
79187922
* Returns the current screen size.
@@ -9434,7 +9438,7 @@ declare module "socket:internal/conduit" {
94349438
* @typedef {{ options: object, payload: Uint8Array }} ReceiveMessage
94359439
* @typedef {function(Error?, ReceiveCallback | undefined)} ReceiveCallback
94369440
* @typedef {{ isActive: boolean, handles: { ids: string[], count: number }}} ConduitDiagnostics
9437-
* @typedef {{ isActive: boolean, port: number }} ConduitStatus
9441+
* @typedef {{ isActive: boolean, port: number, sharedKey: string }} ConduitStatus
94389442
* @typedef {{
94399443
* id?: string|BigInt|number,
94409444
* sharedKey?: string
@@ -9476,6 +9480,17 @@ declare module "socket:internal/conduit" {
94769480
static waitForActiveState(options?: {
94779481
maxQueriesForStatus?: number;
94789482
} | undefined): Promise<any>;
9483+
/**
9484+
* Gets the current conduit shared key.
9485+
* @return {Promise<string>}
9486+
*/
9487+
static getSharedKey(): Promise<string>;
9488+
/**
9489+
* Sets the conduit shared key.
9490+
* @param {string} sharedKey
9491+
* @return {Promise<string>}
9492+
*/
9493+
static setSharedKey(sharedKey: string): Promise<string>;
94799494
/**
94809495
* Creates an instance of Conduit.
94819496
*
@@ -9503,9 +9518,9 @@ declare module "socket:internal/conduit" {
95039518
*/
95049519
port: number;
95059520
/**
9506-
* @type {number?}
9521+
* @type {string}
95079522
*/
9508-
id: number | null;
9523+
id: string;
95099524
/**
95109525
* @type {string}
95119526
*/
@@ -9611,6 +9626,7 @@ declare module "socket:internal/conduit" {
96119626
export type ConduitStatus = {
96129627
isActive: boolean;
96139628
port: number;
9629+
sharedKey: string;
96149630
};
96159631
export type ConduitOptions = {
96169632
id?: string | BigInt | number;
@@ -10571,7 +10587,6 @@ declare module "socket:internal/database" {
1057110587
delete(key: string, options?: (DatabaseDeleteOptions | undefined) | null): Promise<any>;
1057210588
/**
1057310589
* Gets a "readonly" value by `key` in the `Database` object storage.
10574-
* @param {string} key
1057510590
* @param {?DatabaseEntriesOptions|undefined} [options]
1057610591
* @return {Promise<object|object[]|null>}
1057710592
*/

0 commit comments

Comments
 (0)