@mittwald/flow-remote-core 0.2.0-alpha.895 → 0.2.0-alpha.897
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/js/index-node.mjs +13 -13
- package/dist/js/index.mjs +12 -12
- package/dist/js/node_modules/.pnpm/@quilted_events@2.1.5/node_modules/@quilted/events/build/esm/abort/NestedAbortController.mjs +38 -0
- package/dist/js/node_modules/.pnpm/@quilted_events@2.1.5/node_modules/@quilted/events/build/esm/abort/NestedAbortController.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/Thread.mjs +231 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/Thread.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/constants.mjs +10 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/constants.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/errors.mjs +8 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/errors.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/functions/ThreadFunctionsAutomatic.mjs +69 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/functions/ThreadFunctionsAutomatic.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/nanoid.mjs +11 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/nanoid.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/serialization/ThreadSerializationStructuredClone.mjs +212 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/serialization/ThreadSerializationStructuredClone.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/serialization/shared.mjs +11 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/serialization/shared.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/ThreadIframe.mjs +161 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/ThreadIframe.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/ThreadNestedIframe.mjs +86 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/ThreadNestedIframe.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/window/ThreadNestedWindow.mjs +140 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/window/ThreadNestedWindow.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/window/ThreadWindow.mjs +211 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/window/ThreadWindow.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/window/shared.mjs +5 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/window/shared.mjs.map +1 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/transfer.mjs +17 -0
- package/dist/js/node_modules/.pnpm/@quilted_threads@3.3.1_patch_hash_df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/transfer.mjs.map +1 -0
- package/dist/js/{connection → packages/remote-core/src/connection}/connectHostRenderRoot.mjs +1 -1
- package/dist/js/packages/remote-core/src/connection/connectHostRenderRoot.mjs.map +1 -0
- package/dist/js/{connection → packages/remote-core/src/connection}/connectRemoteIframe.mjs +1 -1
- package/dist/js/packages/remote-core/src/connection/connectRemoteIframe.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/connection/types.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/error.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/ext-bridge/getWithMergedHostConfig.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/ext-bridge/implementation.mjs.map +1 -0
- package/dist/js/{serialization → packages/remote-core/src/serialization}/FlowThreadSerialization.mjs +2 -1
- package/dist/js/packages/remote-core/src/serialization/FlowThreadSerialization.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/serialization/Serializer.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/serialization/serializers/dataTransferText.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/serialization/serializers/date.mjs.map +1 -0
- package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/file.mjs +1 -1
- package/dist/js/packages/remote-core/src/serialization/serializers/file.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/serialization/serializers/fileList.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/serialization/serializers/formData.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/serialization/serializers/passwordPolicy.mjs.map +1 -0
- package/dist/js/packages/remote-core/src/shim.mjs.map +1 -0
- package/package.json +6 -6
- package/dist/js/connection/connectHostRenderRoot.mjs.map +0 -1
- package/dist/js/connection/connectRemoteIframe.mjs.map +0 -1
- package/dist/js/connection/types.mjs.map +0 -1
- package/dist/js/error.mjs.map +0 -1
- package/dist/js/ext-bridge/getWithMergedHostConfig.mjs.map +0 -1
- package/dist/js/ext-bridge/implementation.mjs.map +0 -1
- package/dist/js/serialization/FlowThreadSerialization.mjs.map +0 -1
- package/dist/js/serialization/Serializer.mjs.map +0 -1
- package/dist/js/serialization/serializers/dataTransferText.mjs.map +0 -1
- package/dist/js/serialization/serializers/date.mjs.map +0 -1
- package/dist/js/serialization/serializers/file.mjs.map +0 -1
- package/dist/js/serialization/serializers/fileList.mjs.map +0 -1
- package/dist/js/serialization/serializers/formData.mjs.map +0 -1
- package/dist/js/serialization/serializers/passwordPolicy.mjs.map +0 -1
- package/dist/js/shim.mjs.map +0 -1
- /package/dist/js/{connection → packages/remote-core/src/connection}/types.mjs +0 -0
- /package/dist/js/{error.mjs → packages/remote-core/src/error.mjs} +0 -0
- /package/dist/js/{ext-bridge → packages/remote-core/src/ext-bridge}/getWithMergedHostConfig.mjs +0 -0
- /package/dist/js/{ext-bridge → packages/remote-core/src/ext-bridge}/implementation.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/Serializer.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/dataTransferText.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/date.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/fileList.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/formData.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/index.mjs +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/index.mjs.map +0 -0
- /package/dist/js/{serialization → packages/remote-core/src/serialization}/serializers/passwordPolicy.mjs +0 -0
- /package/dist/js/{shim.mjs → packages/remote-core/src/shim.mjs} +0 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { TRANSFERABLE, SERIALIZE_METHOD } from '../constants.mjs';
|
|
2
|
+
import { isIterator, isBasicObject } from './shared.mjs';
|
|
3
|
+
|
|
4
|
+
const FUNCTION = '_@f';
|
|
5
|
+
const ASYNC_ITERATOR = '_@i';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A thread encoder that can transfer supports a wide variety of JavaScript types by
|
|
9
|
+
* converting them to a format that the [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
|
10
|
+
* algorithm can process. This
|
|
11
|
+
*
|
|
12
|
+
* Additionally, this encoder will convert functions into a JSON-friendly representation.
|
|
13
|
+
* These functions will use a manual memory management technique to ensure they can be
|
|
14
|
+
* garbage collected by default, but can be configured to use a `WeakRef`-based automatic
|
|
15
|
+
* memory management system by setting the `garbageCollection: 'weak-ref'` option.
|
|
16
|
+
*/
|
|
17
|
+
class ThreadSerializationStructuredClone {
|
|
18
|
+
#customSerializer;
|
|
19
|
+
#customDeserializer;
|
|
20
|
+
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.#customSerializer = options?.serialize;
|
|
23
|
+
this.#customDeserializer = options?.deserialize;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Serializes a value into a structured cloning-compatible format that can be transferred between threads.
|
|
28
|
+
*/
|
|
29
|
+
async serialize(value, thread, transferable) {
|
|
30
|
+
return this.#serializeInternal(value, thread, transferable);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async #serializeInternal(value, thread, transferable, seen = new Map(), isApplyingDefault = false) {
|
|
34
|
+
if (value == null) return value;
|
|
35
|
+
if (seen.has(value)) return seen.get(value);
|
|
36
|
+
|
|
37
|
+
// Prevent circular references
|
|
38
|
+
seen.set(value, undefined);
|
|
39
|
+
|
|
40
|
+
if (typeof value === 'object') {
|
|
41
|
+
if (this.#customSerializer && !isApplyingDefault) {
|
|
42
|
+
const customValue = await this.#customSerializer(
|
|
43
|
+
value,
|
|
44
|
+
(value) => this.#serializeInternal(value, thread, transferable, seen, true),
|
|
45
|
+
thread,
|
|
46
|
+
transferable,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (customValue !== undefined) {
|
|
50
|
+
seen.set(value, customValue);
|
|
51
|
+
return customValue;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (value[TRANSFERABLE]) {
|
|
56
|
+
transferable?.push(value);
|
|
57
|
+
seen.set(value, value);
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const serializeValue = (value) => {
|
|
62
|
+
return this.#serializeInternal(value, thread, transferable, seen);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (typeof value[SERIALIZE_METHOD] === 'function') {
|
|
66
|
+
const result = await value[SERIALIZE_METHOD]({
|
|
67
|
+
serialize: serializeValue,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
seen.set(value, result);
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (Array.isArray(value)) {
|
|
75
|
+
const result = await Promise.all(value.map((item) => serializeValue(item)));
|
|
76
|
+
seen.set(value, result);
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (value instanceof Map) {
|
|
81
|
+
const entries = await Promise.all(
|
|
82
|
+
[...value.entries()].map(async ([key, value]) => {
|
|
83
|
+
return [await serializeValue(key), await serializeValue(value)];
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const result = new Map(entries);
|
|
88
|
+
seen.set(value, result);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (value instanceof Set) {
|
|
93
|
+
const entries = await Promise.all(
|
|
94
|
+
[...value].map((entry) => serializeValue(entry)),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const result = new Set(entries);
|
|
98
|
+
seen.set(value, result);
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const valueIsIterator = isIterator(value);
|
|
103
|
+
|
|
104
|
+
if (isBasicObject(value) || valueIsIterator) {
|
|
105
|
+
const result = {};
|
|
106
|
+
|
|
107
|
+
for (const key of Object.keys(value)) {
|
|
108
|
+
result[key] = await serializeValue(value[key]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (valueIsIterator) {
|
|
112
|
+
result.next ??= await serializeValue(value.next.bind(value));
|
|
113
|
+
result.return ??= await serializeValue(value.return.bind(value));
|
|
114
|
+
result.throw ??= await serializeValue(value.throw.bind(value));
|
|
115
|
+
result[ASYNC_ITERATOR] = true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
seen.set(value, result);
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (typeof value === 'function') {
|
|
124
|
+
const serialized = thread.functions.serialize(value, thread, transferable);
|
|
125
|
+
const result = { [FUNCTION]: serialized };
|
|
126
|
+
|
|
127
|
+
seen.set(value, result);
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
seen.set(value, value);
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Deserializes a structured cloning-compatible value from another thread.
|
|
137
|
+
*/
|
|
138
|
+
async deserialize(value, thread) {
|
|
139
|
+
return this.#deserializeInternal(value, thread);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async #deserializeInternal(value, thread, isApplyingDefault = false) {
|
|
143
|
+
if (value == null) return value;
|
|
144
|
+
|
|
145
|
+
if (typeof value === 'object') {
|
|
146
|
+
if (this.#customDeserializer && !isApplyingDefault) {
|
|
147
|
+
const customValue = await this.#customDeserializer(
|
|
148
|
+
value,
|
|
149
|
+
(value) => this.#deserializeInternal(value, thread, true),
|
|
150
|
+
thread,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
if (customValue !== undefined) {
|
|
154
|
+
return customValue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (value == null) {
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (Array.isArray(value)) {
|
|
163
|
+
return Promise.all(value.map((value) => this.#deserializeInternal(value, thread)));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (value instanceof Map) {
|
|
167
|
+
const entries = await Promise.all(
|
|
168
|
+
[...value].map(async ([key, value]) => [
|
|
169
|
+
await this.#deserializeInternal(key, thread),
|
|
170
|
+
await this.#deserializeInternal(value, thread),
|
|
171
|
+
]),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
return new Map(entries);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (value instanceof Set) {
|
|
178
|
+
const entries = await Promise.all(
|
|
179
|
+
[...value].map((entry) => this.#deserializeInternal(entry, thread)),
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
return new Set(entries);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (FUNCTION in value) {
|
|
186
|
+
const func = thread.functions.deserialize(value[FUNCTION], thread);
|
|
187
|
+
return func;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!isBasicObject(value)) {
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const result = {};
|
|
195
|
+
|
|
196
|
+
for (const key of Object.keys(value)) {
|
|
197
|
+
if (key === ASYNC_ITERATOR) {
|
|
198
|
+
result[Symbol.asyncIterator] = () => result;
|
|
199
|
+
} else {
|
|
200
|
+
result[key] = await this.#deserializeInternal(value[key], thread);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return value;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export { ThreadSerializationStructuredClone };
|
|
212
|
+
//# sourceMappingURL=ThreadSerializationStructuredClone.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThreadSerializationStructuredClone.mjs","sources":["../../../../../../../../../../../../../node_modules/.pnpm/@quilted+threads@3.3.1_patch_hash=df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/serialization/ThreadSerializationStructuredClone.mjs"],"sourcesContent":["import { TRANSFERABLE, SERIALIZE_METHOD } from '../constants.mjs';\nimport { isIterator, isBasicObject } from './shared.mjs';\n\nconst FUNCTION = '_@f';\nconst ASYNC_ITERATOR = '_@i';\n\n/**\n * A thread encoder that can transfer supports a wide variety of JavaScript types by\n * converting them to a format that the [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)\n * algorithm can process. This\n *\n * Additionally, this encoder will convert functions into a JSON-friendly representation.\n * These functions will use a manual memory management technique to ensure they can be\n * garbage collected by default, but can be configured to use a `WeakRef`-based automatic\n * memory management system by setting the `garbageCollection: 'weak-ref'` option.\n */\nclass ThreadSerializationStructuredClone {\n #customSerializer;\n #customDeserializer;\n\n constructor(options) {\n this.#customSerializer = options?.serialize;\n this.#customDeserializer = options?.deserialize;\n }\n\n /**\n * Serializes a value into a structured cloning-compatible format that can be transferred between threads.\n */\n async serialize(value, thread, transferable) {\n return this.#serializeInternal(value, thread, transferable);\n }\n\n async #serializeInternal(value, thread, transferable, seen = new Map(), isApplyingDefault = false) {\n if (value == null) return value;\n if (seen.has(value)) return seen.get(value);\n\n // Prevent circular references\n seen.set(value, undefined);\n\n if (typeof value === 'object') {\n if (this.#customSerializer && !isApplyingDefault) {\n const customValue = await this.#customSerializer(\n value,\n (value) => this.#serializeInternal(value, thread, transferable, seen, true),\n thread,\n transferable,\n );\n\n if (customValue !== undefined) {\n seen.set(value, customValue);\n return customValue;\n }\n }\n\n if (value[TRANSFERABLE]) {\n transferable?.push(value);\n seen.set(value, value);\n return value;\n }\n\n const serializeValue = (value) => {\n return this.#serializeInternal(value, thread, transferable, seen);\n };\n\n if (typeof value[SERIALIZE_METHOD] === 'function') {\n const result = await value[SERIALIZE_METHOD]({\n serialize: serializeValue,\n });\n\n seen.set(value, result);\n return result;\n }\n\n if (Array.isArray(value)) {\n const result = await Promise.all(value.map((item) => serializeValue(item)));\n seen.set(value, result);\n return result;\n }\n\n if (value instanceof Map) {\n const entries = await Promise.all(\n [...value.entries()].map(async ([key, value]) => {\n return [await serializeValue(key), await serializeValue(value)];\n }),\n );\n\n const result = new Map(entries);\n seen.set(value, result);\n return result;\n }\n\n if (value instanceof Set) {\n const entries = await Promise.all(\n [...value].map((entry) => serializeValue(entry)),\n );\n\n const result = new Set(entries);\n seen.set(value, result);\n return result;\n }\n\n const valueIsIterator = isIterator(value);\n\n if (isBasicObject(value) || valueIsIterator) {\n const result = {};\n\n for (const key of Object.keys(value)) {\n result[key] = await serializeValue(value[key]);\n }\n\n if (valueIsIterator) {\n result.next ??= await serializeValue(value.next.bind(value));\n result.return ??= await serializeValue(value.return.bind(value));\n result.throw ??= await serializeValue(value.throw.bind(value));\n result[ASYNC_ITERATOR] = true;\n }\n\n seen.set(value, result);\n return result;\n }\n }\n\n if (typeof value === 'function') {\n const serialized = thread.functions.serialize(value, thread, transferable);\n const result = { [FUNCTION]: serialized };\n\n seen.set(value, result);\n return result;\n }\n\n seen.set(value, value);\n return value;\n }\n\n /**\n * Deserializes a structured cloning-compatible value from another thread.\n */\n async deserialize(value, thread) {\n return this.#deserializeInternal(value, thread);\n }\n\n async #deserializeInternal(value, thread, isApplyingDefault = false) {\n if (value == null) return value;\n\n if (typeof value === 'object') {\n if (this.#customDeserializer && !isApplyingDefault) {\n const customValue = await this.#customDeserializer(\n value,\n (value) => this.#deserializeInternal(value, thread, true),\n thread,\n );\n\n if (customValue !== undefined) {\n return customValue;\n }\n }\n\n if (value == null) {\n return value;\n }\n\n if (Array.isArray(value)) {\n return Promise.all(value.map((value) => this.#deserializeInternal(value, thread)));\n }\n\n if (value instanceof Map) {\n const entries = await Promise.all(\n [...value].map(async ([key, value]) => [\n await this.#deserializeInternal(key, thread),\n await this.#deserializeInternal(value, thread),\n ]),\n );\n\n return new Map(entries);\n }\n\n if (value instanceof Set) {\n const entries = await Promise.all(\n [...value].map((entry) => this.#deserializeInternal(entry, thread)),\n );\n\n return new Set(entries);\n }\n\n if (FUNCTION in value) {\n const func = thread.functions.deserialize(value[FUNCTION], thread);\n return func;\n }\n\n if (!isBasicObject(value)) {\n return value;\n }\n\n const result = {};\n\n for (const key of Object.keys(value)) {\n if (key === ASYNC_ITERATOR) {\n result[Symbol.asyncIterator] = () => result;\n } else {\n result[key] = await this.#deserializeInternal(value[key], thread);\n }\n }\n\n return result;\n }\n\n return value;\n }\n}\n\nexport { ThreadSerializationStructuredClone };\n"],"names":[],"mappings":";;;AAGA,MAAM,QAAQ,GAAG,KAAK;AACtB,MAAM,cAAc,GAAG,KAAK;;AAE5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;AAC/B,MAAM;;AAEN,MAAM,IAAI,QAAQ,IAAI,KAAK,EAAE;AAC7B,QAAQ,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;AAC1E,QAAQ,OAAO,IAAI;AACnB,MAAM;;AAEN,MAAM,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE;AACjC,QAAQ,OAAO,KAAK;AACpB,MAAM;;AAEN,MAAM,MAAM,MAAM,GAAG,EAAE;;AAEvB,MAAM,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;AAC5C,QAAQ,IAAI,GAAG,KAAK,cAAc,EAAE;AACpC,UAAU,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,MAAM,MAAM;AACrD,QAAQ,CAAC,MAAM;AACf,UAAU,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;AAC3E,QAAQ;AACR,MAAM;;AAEN,MAAM,OAAO,MAAM;AACnB,IAAI;;AAEJ,IAAI,OAAO,KAAK;AAChB,EAAE;AACF;;;;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function isIterator(value) {
|
|
2
|
+
return value != null && (Symbol.asyncIterator in value || Symbol.iterator in value) && typeof value.next === 'function';
|
|
3
|
+
}
|
|
4
|
+
function isBasicObject(value) {
|
|
5
|
+
if (value == null || typeof value !== 'object') return false;
|
|
6
|
+
const prototype = Object.getPrototypeOf(value);
|
|
7
|
+
return prototype == null || prototype === Object.prototype;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { isBasicObject, isIterator };
|
|
11
|
+
//# sourceMappingURL=shared.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.mjs","sources":["../../../../../../../../../../../../../node_modules/.pnpm/@quilted+threads@3.3.1_patch_hash=df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/serialization/shared.mjs"],"sourcesContent":["function isIterator(value) {\n return value != null && (Symbol.asyncIterator in value || Symbol.iterator in value) && typeof value.next === 'function';\n}\nfunction isBasicObject(value) {\n if (value == null || typeof value !== 'object') return false;\n const prototype = Object.getPrototypeOf(value);\n return prototype == null || prototype === Object.prototype;\n}\n\nexport { isBasicObject, isIterator };\n"],"names":[],"mappings":"AAAA,SAAS,UAAU,CAAC,KAAK,EAAE;AAC3B,EAAE,OAAO,KAAK,IAAI,IAAI,KAAK,MAAM,CAAC,aAAa,IAAI,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU;AACzH;AACA,SAAS,aAAa,CAAC,KAAK,EAAE;AAC9B,EAAE,IAAI,KAAK,IAAI,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,OAAO,KAAK;AAC9D,EAAE,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC;AAChD,EAAE,OAAO,SAAS,IAAI,IAAI,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS;AAC5D;;;;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { Thread } from '../Thread.mjs';
|
|
2
|
+
import { windowToThreadTarget } from './window/ThreadWindow.mjs';
|
|
3
|
+
import { ThreadNestedIframe } from './ThreadNestedIframe.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a thread from an iframe element.
|
|
7
|
+
*
|
|
8
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
12
|
+
*
|
|
13
|
+
* const iframe = document.querySelector('iframe#thread-iframe');
|
|
14
|
+
* const thread = new ThreadIframe(iframe);
|
|
15
|
+
* await thread.imports.sendMessage('Hello world!');
|
|
16
|
+
*/
|
|
17
|
+
class ThreadIframe extends Thread {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a thread from an iframe element.
|
|
20
|
+
*
|
|
21
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
25
|
+
*
|
|
26
|
+
* const iframe = document.querySelector('iframe#thread-iframe');
|
|
27
|
+
* const thread = ThreadIframe.from(iframe);
|
|
28
|
+
* await thread.imports.sendMessage('Hello world!');
|
|
29
|
+
*/
|
|
30
|
+
static from(iframe, options) {
|
|
31
|
+
return new ThreadIframe(iframe, options);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Starts a thread wrapped around an iframe element, and returns the imports
|
|
36
|
+
* of the thread.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
41
|
+
*
|
|
42
|
+
* const {getMessage} = ThreadIframe.import(iframe);
|
|
43
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
44
|
+
*
|
|
45
|
+
* // In the nested window:
|
|
46
|
+
*
|
|
47
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
48
|
+
*
|
|
49
|
+
* ThreadIframe.parent.export({
|
|
50
|
+
* async getMessage() {
|
|
51
|
+
* return 'Hello, world!';
|
|
52
|
+
* },
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
static import(iframe, options) {
|
|
57
|
+
return new ThreadIframe(iframe, options).imports;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Starts a thread wrapped around an iframe element, providing the second
|
|
62
|
+
* argument as the exports of the thread.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
67
|
+
*
|
|
68
|
+
* ThreadIframe.export(iframe, {
|
|
69
|
+
* async getMessage() {
|
|
70
|
+
* return 'Hello, world!';
|
|
71
|
+
* },
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* // In the nested iframe:
|
|
75
|
+
*
|
|
76
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
77
|
+
*
|
|
78
|
+
* const {getMessage} = ThreadIframe.parent.import();
|
|
79
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static export(iframe, exports, options) {
|
|
83
|
+
new ThreadIframe(iframe, {
|
|
84
|
+
...options,
|
|
85
|
+
exports
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a thread from a `window.parent`, a reference to the parent window when rendering
|
|
91
|
+
* in a nested iframe.
|
|
92
|
+
*
|
|
93
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/parent
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
97
|
+
*
|
|
98
|
+
* const thread = ThreadIframe.parent();
|
|
99
|
+
* await thread.imports.sendMessage('Hello world!');
|
|
100
|
+
*/
|
|
101
|
+
static parent = (() => Object.assign(options => new ThreadNestedIframe(options), {
|
|
102
|
+
/**
|
|
103
|
+
* Starts a thread wrapped around a parent window, and returns the imports
|
|
104
|
+
* of the thread.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
109
|
+
*
|
|
110
|
+
* const {getMessage} = ThreadIframe.parent.import();
|
|
111
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
112
|
+
*
|
|
113
|
+
* // In the parent window:
|
|
114
|
+
*
|
|
115
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
116
|
+
*
|
|
117
|
+
* ThreadIframe.export(iframe, {
|
|
118
|
+
* async getMessage() {
|
|
119
|
+
* return 'Hello, world!';
|
|
120
|
+
* },
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
import: options => ThreadNestedIframe.import(options),
|
|
125
|
+
/**
|
|
126
|
+
* Starts a thread wrapped around a parent window, providing the second
|
|
127
|
+
* argument as the exports of the thread.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
132
|
+
*
|
|
133
|
+
* ThreadIframe.parent.export({
|
|
134
|
+
* async getMessage() {
|
|
135
|
+
* return 'Hello, world!';
|
|
136
|
+
* },
|
|
137
|
+
* });
|
|
138
|
+
*
|
|
139
|
+
* // In the parent window:
|
|
140
|
+
*
|
|
141
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
142
|
+
*
|
|
143
|
+
* const {getMessage} = ThreadIframe.import(iframe);
|
|
144
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
export: (exports, options) => ThreadNestedIframe.export(exports, options)
|
|
148
|
+
}))();
|
|
149
|
+
constructor(iframe, {
|
|
150
|
+
targetOrigin = '*',
|
|
151
|
+
...options
|
|
152
|
+
} = {}) {
|
|
153
|
+
super(windowToThreadTarget(iframe.contentWindow, {
|
|
154
|
+
targetOrigin
|
|
155
|
+
}), options);
|
|
156
|
+
this.iframe = iframe;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { ThreadIframe };
|
|
161
|
+
//# sourceMappingURL=ThreadIframe.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThreadIframe.mjs","sources":["../../../../../../../../../../../../../node_modules/.pnpm/@quilted+threads@3.3.1_patch_hash=df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/ThreadIframe.mjs"],"sourcesContent":["import { Thread } from '../Thread.mjs';\nimport { windowToThreadTarget } from './window/ThreadWindow.mjs';\nimport { ThreadNestedIframe } from './ThreadNestedIframe.mjs';\n\n/**\n * Creates a thread from an iframe element.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe\n *\n * @example\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const iframe = document.querySelector('iframe#thread-iframe');\n * const thread = new ThreadIframe(iframe);\n * await thread.imports.sendMessage('Hello world!');\n */\nclass ThreadIframe extends Thread {\n /**\n * Creates a thread from an iframe element.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe\n *\n * @example\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const iframe = document.querySelector('iframe#thread-iframe');\n * const thread = ThreadIframe.from(iframe);\n * await thread.imports.sendMessage('Hello world!');\n */\n static from(iframe, options) {\n return new ThreadIframe(iframe, options);\n }\n\n /**\n * Starts a thread wrapped around an iframe element, and returns the imports\n * of the thread.\n *\n * @example\n * ```ts\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const {getMessage} = ThreadIframe.import(iframe);\n * const message = await getMessage(); // 'Hello, world!'\n *\n * // In the nested window:\n *\n * import {ThreadIframe} from '@quilted/threads';\n *\n * ThreadIframe.parent.export({\n * async getMessage() {\n * return 'Hello, world!';\n * },\n * });\n * ```\n */\n static import(iframe, options) {\n return new ThreadIframe(iframe, options).imports;\n }\n\n /**\n * Starts a thread wrapped around an iframe element, providing the second\n * argument as the exports of the thread.\n *\n * @example\n * ```ts\n * import {ThreadIframe} from '@quilted/threads';\n *\n * ThreadIframe.export(iframe, {\n * async getMessage() {\n * return 'Hello, world!';\n * },\n * });\n *\n * // In the nested iframe:\n *\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const {getMessage} = ThreadIframe.parent.import();\n * const message = await getMessage(); // 'Hello, world!'\n * ```\n */\n static export(iframe, exports, options) {\n new ThreadIframe(iframe, {\n ...options,\n exports\n });\n }\n\n /**\n * Creates a thread from a `window.parent`, a reference to the parent window when rendering\n * in a nested iframe.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/parent\n *\n * @example\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const thread = ThreadIframe.parent();\n * await thread.imports.sendMessage('Hello world!');\n */\n static parent = (() => Object.assign(options => new ThreadNestedIframe(options), {\n /**\n * Starts a thread wrapped around a parent window, and returns the imports\n * of the thread.\n *\n * @example\n * ```ts\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const {getMessage} = ThreadIframe.parent.import();\n * const message = await getMessage(); // 'Hello, world!'\n *\n * // In the parent window:\n *\n * import {ThreadIframe} from '@quilted/threads';\n *\n * ThreadIframe.export(iframe, {\n * async getMessage() {\n * return 'Hello, world!';\n * },\n * });\n * ```\n */\n import: options => ThreadNestedIframe.import(options),\n /**\n * Starts a thread wrapped around a parent window, providing the second\n * argument as the exports of the thread.\n *\n * @example\n * ```ts\n * import {ThreadIframe} from '@quilted/threads';\n *\n * ThreadIframe.parent.export({\n * async getMessage() {\n * return 'Hello, world!';\n * },\n * });\n *\n * // In the parent window:\n *\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const {getMessage} = ThreadIframe.import(iframe);\n * const message = await getMessage(); // 'Hello, world!'\n * ```\n */\n export: (exports, options) => ThreadNestedIframe.export(exports, options)\n }))();\n constructor(iframe, {\n targetOrigin = '*',\n ...options\n } = {}) {\n super(windowToThreadTarget(iframe.contentWindow, {\n targetOrigin\n }), options);\n this.iframe = iframe;\n }\n}\n\nexport { ThreadIframe };\n"],"names":[],"mappings":";;;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,YAAY,SAAS,MAAM,CAAC;AAClC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE;AAC/B,IAAI,OAAO,IAAI,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC;AAC5C,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,EAAE;AACjC,IAAI,OAAO,IAAI,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,OAAO;AACpD,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE;AAC1C,IAAI,IAAI,YAAY,CAAC,MAAM,EAAE;AAC7B,MAAM,GAAG,OAAO;AAChB,MAAM;AACN,KAAK,CAAC;AACN,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,IAAI,kBAAkB,CAAC,OAAO,CAAC,EAAE;AACnF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,MAAM,EAAE,OAAO,IAAI,kBAAkB,CAAC,MAAM,CAAC,OAAO,CAAC;AACzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,kBAAkB,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO;AAC5E,GAAG,CAAC,GAAG;AACP,EAAE,WAAW,CAAC,MAAM,EAAE;AACtB,IAAI,YAAY,GAAG,GAAG;AACtB,IAAI,GAAG;AACP,GAAG,GAAG,EAAE,EAAE;AACV,IAAI,KAAK,CAAC,oBAAoB,CAAC,MAAM,CAAC,aAAa,EAAE;AACrD,MAAM;AACN,KAAK,CAAC,EAAE,OAAO,CAAC;AAChB,IAAI,IAAI,CAAC,MAAM,GAAG,MAAM;AACxB,EAAE;AACF;;;;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Thread } from '../Thread.mjs';
|
|
2
|
+
import { nestedWindowToThreadTarget } from './window/ThreadNestedWindow.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a thread from within an iframe nested in a top-level document.
|
|
6
|
+
*
|
|
7
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import {ThreadNestedIframe} from '@quilted/threads';
|
|
11
|
+
*
|
|
12
|
+
* const thread = new ThreadNestedIframe();
|
|
13
|
+
* await thread.imports.sendMessage('Hello world!');
|
|
14
|
+
*/
|
|
15
|
+
class ThreadNestedIframe extends Thread {
|
|
16
|
+
/**
|
|
17
|
+
* Starts a thread wrapped around a parent window, and returns the imports
|
|
18
|
+
* of the thread.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import {ThreadNestedIframe} from '@quilted/threads';
|
|
23
|
+
*
|
|
24
|
+
* const {getMessage} = ThreadNestedIframe.import();
|
|
25
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
26
|
+
*
|
|
27
|
+
* // In the parent window:
|
|
28
|
+
*
|
|
29
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
30
|
+
*
|
|
31
|
+
* ThreadIframe.export(iframe, {
|
|
32
|
+
* async getMessage() {
|
|
33
|
+
* return 'Hello, world!';
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
static import(options) {
|
|
39
|
+
return new ThreadNestedIframe(options).imports;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Starts a thread wrapped around a parent window, providing the second
|
|
44
|
+
* argument as the exports of the thread.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import {ThreadNestedIframe} from '@quilted/threads';
|
|
49
|
+
*
|
|
50
|
+
* ThreadNestedIframe.export({
|
|
51
|
+
* async getMessage() {
|
|
52
|
+
* return 'Hello, world!';
|
|
53
|
+
* },
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* // In the parent window:
|
|
57
|
+
*
|
|
58
|
+
* import {ThreadIframe} from '@quilted/threads';
|
|
59
|
+
*
|
|
60
|
+
* const {getMessage} = ThreadIframe.import(iframe);
|
|
61
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
static export(exports, options) {
|
|
65
|
+
new ThreadNestedIframe({
|
|
66
|
+
...options,
|
|
67
|
+
exports
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
constructor({
|
|
71
|
+
parent = globalThis.parent,
|
|
72
|
+
targetOrigin = '*',
|
|
73
|
+
...options
|
|
74
|
+
} = {}) {
|
|
75
|
+
if (typeof self === 'undefined' || parent == null) {
|
|
76
|
+
throw new Error('You are not inside an iframe, because there is no parent window.');
|
|
77
|
+
}
|
|
78
|
+
super(nestedWindowToThreadTarget(parent, {
|
|
79
|
+
targetOrigin
|
|
80
|
+
}), options);
|
|
81
|
+
this.parent = parent;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { ThreadNestedIframe };
|
|
86
|
+
//# sourceMappingURL=ThreadNestedIframe.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThreadNestedIframe.mjs","sources":["../../../../../../../../../../../../../node_modules/.pnpm/@quilted+threads@3.3.1_patch_hash=df15ead5d86def314ee16ab6a48f9e4faa765b52aa161b10d2848_aa11bff1669c76d0bae25976909ea4eb/node_modules/@quilted/threads/build/esm/threads/ThreadNestedIframe.mjs"],"sourcesContent":["import { Thread } from '../Thread.mjs';\nimport { nestedWindowToThreadTarget } from './window/ThreadNestedWindow.mjs';\n\n/**\n * Creates a thread from within an iframe nested in a top-level document.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe\n *\n * @example\n * import {ThreadNestedIframe} from '@quilted/threads';\n *\n * const thread = new ThreadNestedIframe();\n * await thread.imports.sendMessage('Hello world!');\n */\nclass ThreadNestedIframe extends Thread {\n /**\n * Starts a thread wrapped around a parent window, and returns the imports\n * of the thread.\n *\n * @example\n * ```ts\n * import {ThreadNestedIframe} from '@quilted/threads';\n *\n * const {getMessage} = ThreadNestedIframe.import();\n * const message = await getMessage(); // 'Hello, world!'\n *\n * // In the parent window:\n *\n * import {ThreadIframe} from '@quilted/threads';\n *\n * ThreadIframe.export(iframe, {\n * async getMessage() {\n * return 'Hello, world!';\n * },\n * });\n * ```\n */\n static import(options) {\n return new ThreadNestedIframe(options).imports;\n }\n\n /**\n * Starts a thread wrapped around a parent window, providing the second\n * argument as the exports of the thread.\n *\n * @example\n * ```ts\n * import {ThreadNestedIframe} from '@quilted/threads';\n *\n * ThreadNestedIframe.export({\n * async getMessage() {\n * return 'Hello, world!';\n * },\n * });\n *\n * // In the parent window:\n *\n * import {ThreadIframe} from '@quilted/threads';\n *\n * const {getMessage} = ThreadIframe.import(iframe);\n * const message = await getMessage(); // 'Hello, world!'\n * ```\n */\n static export(exports, options) {\n new ThreadNestedIframe({\n ...options,\n exports\n });\n }\n constructor({\n parent = globalThis.parent,\n targetOrigin = '*',\n ...options\n } = {}) {\n if (typeof self === 'undefined' || parent == null) {\n throw new Error('You are not inside an iframe, because there is no parent window.');\n }\n super(nestedWindowToThreadTarget(parent, {\n targetOrigin\n }), options);\n this.parent = parent;\n }\n}\n\nexport { ThreadNestedIframe };\n"],"names":[],"mappings":";;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,kBAAkB,SAAS,MAAM,CAAC;AACxC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,MAAM,CAAC,OAAO,EAAE;AACzB,IAAI,OAAO,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC,OAAO;AAClD,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE;AAClC,IAAI,IAAI,kBAAkB,CAAC;AAC3B,MAAM,GAAG,OAAO;AAChB,MAAM;AACN,KAAK,CAAC;AACN,EAAE;AACF,EAAE,WAAW,CAAC;AACd,IAAI,MAAM,GAAG,UAAU,CAAC,MAAM;AAC9B,IAAI,YAAY,GAAG,GAAG;AACtB,IAAI,GAAG;AACP,GAAG,GAAG,EAAE,EAAE;AACV,IAAI,IAAI,OAAO,IAAI,KAAK,WAAW,IAAI,MAAM,IAAI,IAAI,EAAE;AACvD,MAAM,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC;AACzF,IAAI;AACJ,IAAI,KAAK,CAAC,0BAA0B,CAAC,MAAM,EAAE;AAC7C,MAAM;AACN,KAAK,CAAC,EAAE,OAAO,CAAC;AAChB,IAAI,IAAI,CAAC,MAAM,GAAG,MAAM;AACxB,EAAE;AACF;;;;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Thread } from '../../Thread.mjs';
|
|
2
|
+
import { CHECK_MESSAGE, RESPONSE_MESSAGE } from './shared.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a thread from within a window created by a parent document (for example,
|
|
6
|
+
* an `iframe` or popup window).
|
|
7
|
+
*
|
|
8
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/opener
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import {ThreadNestedWindow} from '@quilted/threads';
|
|
12
|
+
*
|
|
13
|
+
* // Inside a document opened as a popup window
|
|
14
|
+
* const thread = new ThreadNestedWindow(window.opener);
|
|
15
|
+
* await thread.imports.sendMessage('Hello world!');
|
|
16
|
+
*/
|
|
17
|
+
class ThreadNestedWindow extends Thread {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a thread from within a window created by a parent document (for example,
|
|
20
|
+
* an `iframe` or popup window).
|
|
21
|
+
*
|
|
22
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/opener
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import {ThreadNestedWindow} from '@quilted/threads';
|
|
26
|
+
*
|
|
27
|
+
* // Inside a document opened as a popup window
|
|
28
|
+
* const thread = ThreadNestedWindow.from(window.opener);
|
|
29
|
+
* await thread.imports.sendMessage('Hello world!');
|
|
30
|
+
*/
|
|
31
|
+
static from(window, options) {
|
|
32
|
+
return new ThreadNestedWindow(window, options);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Starts a thread wrapped around a nested `window` object, and returns the imports
|
|
37
|
+
* of the thread.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import {ThreadNestedWindow} from '@quilted/threads';
|
|
42
|
+
*
|
|
43
|
+
* const {getMessage} = ThreadNestedWindow.import(window.opener);
|
|
44
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
45
|
+
*
|
|
46
|
+
* // In the parent window:
|
|
47
|
+
*
|
|
48
|
+
* import {ThreadWindow} from '@quilted/threads';
|
|
49
|
+
*
|
|
50
|
+
* ThreadWindow.export(window, {
|
|
51
|
+
* async getMessage() {
|
|
52
|
+
* return 'Hello, world!';
|
|
53
|
+
* },
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
static import(window, options) {
|
|
58
|
+
return new ThreadNestedWindow(window, options).imports;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Starts a thread wrapped around a nested `window` object, providing the second
|
|
63
|
+
* argument as the exports of the thread.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import {ThreadNestedWindow} from '@quilted/threads';
|
|
68
|
+
*
|
|
69
|
+
* ThreadNestedWindow.export(window.opener, {
|
|
70
|
+
* async getMessage() {
|
|
71
|
+
* return 'Hello, world!';
|
|
72
|
+
* },
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* // In the parent window:
|
|
76
|
+
*
|
|
77
|
+
* import {ThreadWindow} from '@quilted/threads';
|
|
78
|
+
*
|
|
79
|
+
* const {getMessage} = ThreadWindow.import(window);
|
|
80
|
+
* const message = await getMessage(); // 'Hello, world!'
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
static export(window, exports, options) {
|
|
84
|
+
new ThreadNestedWindow(window, {
|
|
85
|
+
...options,
|
|
86
|
+
exports
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
constructor(parent, {
|
|
90
|
+
targetOrigin = '*',
|
|
91
|
+
...options
|
|
92
|
+
} = {}) {
|
|
93
|
+
super(nestedWindowToThreadTarget(parent, {
|
|
94
|
+
targetOrigin
|
|
95
|
+
}), options);
|
|
96
|
+
this.parent = parent;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function nestedWindowToThreadTarget(parent, {
|
|
100
|
+
targetOrigin = '*'
|
|
101
|
+
} = {}) {
|
|
102
|
+
const ready = () => {
|
|
103
|
+
const respond = () => parent.postMessage(RESPONSE_MESSAGE, targetOrigin);
|
|
104
|
+
self.addEventListener('message', ({
|
|
105
|
+
data,
|
|
106
|
+
source
|
|
107
|
+
}) => {
|
|
108
|
+
if (source !== parent) return;
|
|
109
|
+
if (data === CHECK_MESSAGE) respond();
|
|
110
|
+
});
|
|
111
|
+
respond();
|
|
112
|
+
};
|
|
113
|
+
if (document.readyState === 'complete') {
|
|
114
|
+
ready();
|
|
115
|
+
} else {
|
|
116
|
+
document.addEventListener('readystatechange', () => {
|
|
117
|
+
if (document.readyState === 'complete') {
|
|
118
|
+
ready();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
send(message, transfer) {
|
|
124
|
+
return parent.postMessage(message, targetOrigin, transfer);
|
|
125
|
+
},
|
|
126
|
+
listen(listen, {
|
|
127
|
+
signal
|
|
128
|
+
}) {
|
|
129
|
+
self.addEventListener('message', event => {
|
|
130
|
+
if (event.data === CHECK_MESSAGE) return;
|
|
131
|
+
listen(event.data);
|
|
132
|
+
}, {
|
|
133
|
+
signal
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { ThreadNestedWindow, nestedWindowToThreadTarget };
|
|
140
|
+
//# sourceMappingURL=ThreadNestedWindow.mjs.map
|