Skip to content

Commit 863b4b2

Browse files
committed
refactor(runtime,api): more progress towards supporting multiple origins
1 parent 255c341 commit 863b4b2

File tree

138 files changed

+6052
-5201
lines changed

Some content is hidden

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

138 files changed

+6052
-5201
lines changed

api/application.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function serializeConfig (config) {
5959
export { client, menu }
6060

6161
// get this from constant value in runtime
62-
export const MAX_WINDOWS = 32
62+
export const MAX_WINDOWS = 64
6363

6464
export class ApplicationWindowList {
6565
#list = []
@@ -88,8 +88,8 @@ export class ApplicationWindowList {
8888
return this.length
8989
}
9090

91-
get [Symbol.iterator] () {
92-
return this.#list[Symbol.iterator]
91+
[Symbol.iterator] () {
92+
return this.#list[Symbol.iterator]()
9393
}
9494

9595
forEach (callback, thisArg) {
@@ -267,6 +267,8 @@ export async function createWindow (opts) {
267267
resourcesDirectory: opts.resourcesDirectory ?? '',
268268
shouldExitApplicationOnClose: opts.shouldExitApplicationOnClose ?? false,
269269
shouldPreferServiceWorker: Boolean(opts.shouldPreferServiceWorker ?? false),
270+
// @ts-ignore
271+
reserved: Boolean(opts.reserved),
270272
/**
271273
* @private
272274
* @type {number}

api/ipc.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ export function sendSync (command, value = '', options = null, buffer = null) {
11471147

11481148
let response = null
11491149
try {
1150+
console.log({ uri })
11501151
response = globalThis.__global_ipc_extension_handler(uri)
11511152
} catch (err) {
11521153
return Result.from(null, err)
@@ -1778,6 +1779,18 @@ export function findIPCMessageTransfers (transfers, object) {
17781779
)
17791780
) {
17801781
const port = IPCMessagePort.create(object)
1782+
object.addEventListener('message', function onMessage (event) {
1783+
if (port.closed === true) {
1784+
port.onmessage = null
1785+
event.preventDefault()
1786+
event.stopImmediatePropagation()
1787+
object.removeEventListener('message', onMessage)
1788+
return false
1789+
}
1790+
1791+
port.dispatchEvent(new MessageEvent('message', event))
1792+
})
1793+
17811794

17821795
port.onmessage = (event) => {
17831796
if (port.closed === true) {
@@ -2067,19 +2080,20 @@ export class IPCMessageChannel extends MessageChannel {
20672080
constructor (options = null) {
20682081
super()
20692082
this.#id = String(options?.id ?? rand64())
2070-
this.#channel = options?.channel ?? new IPCBroadcastChannel(this.#id)
2083+
this.#channel = options?.channel ?? new BroadcastChannel(this.#id)
20712084

20722085
this.#port1 = IPCMessagePort.create(options?.port1)
20732086
this.#port2 = IPCMessagePort.create(options?.port2)
20742087

20752088
this.port1[Symbol.for('socket.runtime.ipc.MessagePort.handlePostMessage')] = (message, options) => {
2076-
this.port2.postMessage(message, options)
2089+
this.port2.channel.postMessage(message, options)
20772090
return false
20782091
}
20792092

2080-
this.port2.addEventListener('message', (e) => {
2081-
this.port1.postMessage(e.data)
2082-
})
2093+
this.port2[Symbol.for('socket.runtime.ipc.MessagePort.handlePostMessage')] = (message, options) => {
2094+
this.port2.channel.postMessage(message, options)
2095+
return false
2096+
}
20832097
}
20842098

20852099
get id () {

api/process/signal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import os from '../os.js'
1212

1313
export { constants }
1414

15-
export const channel = new ipc.IPCBroadcastChannel('socket.runtime.signal')
15+
export const channel = new BroadcastChannel('socket.runtime.signal')
1616

1717
export const SIGHUP = constants.SIGHUP
1818
export const SIGINT = constants.SIGINT

api/service-worker/container.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ServiceWorkerContainerInternalState {
2424
currentWindow = null
2525
controller = null
2626
sharedWorker = null
27-
channel = new ipc.IPCBroadcastChannel('socket.runtime.ServiceWorkerContainer')
27+
channel = new BroadcastChannel('socket.runtime.ServiceWorkerContainer')
2828
ready = new Deferred()
2929
init = new Deferred()
3030

api/service-worker/index.html

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,124 @@
2222
value: true
2323
})
2424
</script>
25-
<script type="module" src="./main.js"></script>
25+
<script type="module">
26+
import application from 'socket:application'
27+
import process from 'socket:process'
28+
29+
Object.assign(globalThis, {
30+
async openExternalLink (href) {
31+
const currentWindow = await application.getCurrentWindow()
32+
await currentWindow.openExternal(href)
33+
}
34+
})
35+
36+
document.title = `Service Worker Debugger - v${process.version}`
37+
</script>
38+
<script type="module" src="./init.js"></script>
2639
<style type="text/css" media="all">
40+
* {
41+
box-sizing: border-box;
42+
}
43+
2744
body {
28-
display: flex;
29-
width: 100%;
45+
background: rgba(40, 40, 40, 1);
46+
color: rgba(255, 255, 255, 1);
47+
font-family: 'Inter-Light', sans-serif;
48+
font-size: 14px;
3049
margin: 0;
31-
padding: 0;
50+
position: absolute; top: 0px; left: 0; right:0; bottom: 0px;
3251
}
3352

34-
iframe {
35-
border: none;
53+
a {
54+
color: inherit;
55+
text-decoration: none;
56+
transition: all 0.2s ease;
57+
58+
&.with-hover:hover {
59+
border-bottom: 2px solid rgba(255, 255, 255, 1);
60+
}
61+
}
62+
63+
p {
64+
background: rgba(14, 85, 152, .25);
65+
border-radius: 2px;
66+
display: inline-block;
67+
font: 12px/26px 'Inter-Light', sans-serif;
68+
margin: 0;
69+
text-align: center;
3670
width: 100%;
37-
height: 100vh;
71+
72+
&.message {
73+
display: block;
74+
overflow: hidden;
75+
padding: 0 8px;
76+
position: relative;
77+
text-overflow: ellipsis;
78+
white-space: nowrap;
79+
width: 100%;
80+
}
3881
}
82+
83+
pre#log {
84+
background: rgba(0, 0, 0, 1);
85+
overflow-y: scroll;
86+
padding: 16px;
87+
margin: 0;
88+
position: absolute; top: 0px; left: 0; right:0; bottom: 0px;
89+
90+
& span {
91+
opacity: 0.8;
92+
transition: all 0.2s ease;
93+
94+
&:hover {
95+
opacity: 1;
96+
transition: all 0.05s ease;
97+
}
98+
99+
& code {
100+
color: rgb(215, 215, 215);
101+
display: block;
102+
font-size: 12px;
103+
line-break: anywhere;
104+
margin-bottom: 8px;
105+
opacity: 0.8;
106+
white-space: wrap;
107+
transition: all 0.1s ease;
108+
109+
&:hover {
110+
color: rgb(225, 225, 225);
111+
opacity: 1;
112+
transition: all 0.025s ease;
113+
}
114+
115+
& span {
116+
&.red {
117+
color: red;
118+
}
119+
}
120+
}
121+
122+
& details {
123+
&[open] span {
124+
opacity: 1;
125+
transition: all 0.05s ease;
126+
& code {
127+
color: rgb(225, 225, 225);
128+
opacity: 1;
129+
transition: all 0.025s ease;
130+
}
131+
}
132+
133+
& > summary {
134+
cursor: pointer;
135+
list-style: none;
136+
}
137+
}
138+
}
39139
}
40140
</style>
41141
</head>
42142
<body>
143+
<pre id="log"></pre>
43144
</body>
44145
</html>

api/service-worker/init.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { Notification } from '../notification.js'
55
import { sleep } from '../timers.js'
66
import globals from '../internal/globals.js'
77
import crypto from '../crypto.js'
8+
import hooks from '../hooks.js'
89
import ipc from '../ipc.js'
910

1011
export const workers = new Map()
11-
export const channel = new ipc.IPCBroadcastChannel('socket.runtime.serviceWorker')
1212

1313
globals.register('ServiceWorkerContext.workers', workers)
1414
globals.register('ServiceWorkerContext.info', new Map())
@@ -24,7 +24,7 @@ sharedWorker.port.onmessage = (event) => {
2424
}
2525
}
2626
} else if (event.data?.showNotification && event.data.registration?.id) {
27-
onShowNotification(event, sharedWorker.port)
27+
onNotificationShow(event, sharedWorker.port)
2828
} else if (event.data?.getNotifications && event.data.registration?.id) {
2929
onGetNotifications(event, sharedWorker.port)
3030
}
@@ -37,12 +37,17 @@ export class ServiceWorkerInstance extends Worker {
3737
constructor (filename, options) {
3838
options = { ...options }
3939
const info = options.info ?? null
40-
4140
if (info.serializedWorkerArgs) {
42-
options.args = JSON.parse(decodeURIComponent(info.serializedWorkerArgs))
43-
options.args.index = globalThis.__args.index
41+
try {
42+
options.args = JSON.parse(info.serializedWorkerArgs)
43+
} catch {
44+
try {
45+
options.args = JSON.parse(decodeURIComponent(info.serializedWorkerArgs))
46+
} catch {
47+
options.args = JSON.parse(decodeURIComponent(decodeURIComponent(info.serializedWorkerArgs)))
48+
}
49+
}
4450
}
45-
4651
super(filename, {
4752
name: `ServiceWorker (${options?.info?.pathname ?? filename})`,
4853
...options,
@@ -106,7 +111,7 @@ export class ServiceWorkerInstance extends Worker {
106111
log.scrollTop = log.scrollHeight
107112
}
108113
} else if (event.data?.showNotification && event.data.registration?.id) {
109-
onShowNotification(event, this)
114+
onNotificationShow(event, this)
110115
} else if (event.data?.getNotifications && event.data.registration?.id) {
111116
onGetNotifications(event, this)
112117
} else if (event.data?.notificationclose?.id) {
@@ -120,7 +125,6 @@ export class ServiceWorkerInfo {
120125
url = null
121126
hash = null
122127
scope = null
123-
scheme = null
124128
scriptURL = null
125129
serializedWorkerArgs = null
126130

@@ -134,7 +138,7 @@ export class ServiceWorkerInfo {
134138

135139
const url = new URL(this.scriptURL)
136140
this.url = url.toString()
137-
this.hash = crypto.murmur3(this.scheme + url.pathname + (this.scope || ''))
141+
this.hash = crypto.murmur3(url.pathname + (this.scope || ''))
138142
}
139143

140144
get pathname () {
@@ -205,7 +209,7 @@ export async function onFetch (event) {
205209
}
206210
})(),
207211
new Promise((resolve) => {
208-
globalThis.addEventListener(
212+
globalThis.top.addEventListener(
209213
'serviceWorker.activate',
210214
async function (event) {
211215
// @ts-ignore
@@ -250,7 +254,7 @@ export async function onFetch (event) {
250254
worker.postMessage({ fetch: { ...info, client, request } })
251255
}
252256

253-
export function onShowNotification (event, target) {
257+
export function onNotificationShow (event, target) {
254258
for (const worker of workers.values()) {
255259
if (worker.info.id === event.data.registration.id) {
256260
let notification = null
@@ -353,14 +357,22 @@ export function onGetNotifications (event, target) {
353357

354358
export default null
355359

356-
globalThis.addEventListener('serviceWorker.register', onRegister)
357-
globalThis.addEventListener('serviceWorker.unregister', onUnregister)
358-
globalThis.addEventListener('serviceWorker.skipWaiting', onSkipWaiting)
359-
globalThis.addEventListener('serviceWorker.activate', onActivate)
360-
globalThis.addEventListener('serviceWorker.fetch', onFetch)
360+
globalThis.top.addEventListener('serviceWorker.register', onRegister)
361+
globalThis.top.addEventListener('serviceWorker.unregister', onUnregister)
362+
globalThis.top.addEventListener('serviceWorker.skipWaiting', onSkipWaiting)
363+
globalThis.top.addEventListener('serviceWorker.activate', onActivate)
364+
globalThis.top.addEventListener('serviceWorker.fetch', onFetch)
365+
366+
hooks.onReady(async () => {
367+
// notify top frame that the service worker init module is ready
368+
globalThis.top.postMessage({
369+
__service_worker_frame_init: true
370+
})
361371

362-
channel.addEventListener('message', (e) => {
363-
if (e.data?.type?.startsWith('serviceWorker.')) {
364-
globalThis.dispatchEvent(new CustomEvent(e.data.type, e.data))
372+
const result = await ipc.request('serviceWorker.getRegistrations')
373+
if (Array.isArray(result.data)) {
374+
for (const info of result.data) {
375+
await navigator.serviceWorker.register(info.scriptURL, info)
376+
}
365377
}
366378
})

api/service-worker/instance.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function createServiceWorker (
3232
}
3333
}
3434

35-
const channel = new ipc.IPCBroadcastChannel('socket.runtime.serviceWorker.state')
35+
const channel = new BroadcastChannel('socket.runtime.serviceWorker.state')
3636

3737
// events
3838
const eventTarget = new EventTarget()
@@ -77,7 +77,14 @@ export function createServiceWorker (
7777
state: {
7878
configurable: true,
7979
enumerable: false,
80-
get: () => currentState === null ? state.serviceWorker.state : currentState
80+
get: () => currentState === null ? state.serviceWorker.state : currentState,
81+
},
82+
83+
[Symbol.for('socket.runtime.serviceWorker.state')]: {
84+
configurable: false,
85+
enumerable: false,
86+
get: () => currentState === null ? state.serviceWorker.state : currentState,
87+
set: (state) => { currentState = state }
8188
},
8289

8390
scriptURL: {

0 commit comments

Comments
 (0)