@pairbo/ui-kit 0.0.6 → 0.1.1
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/README.md +185 -2
- package/dist/mockServiceWorker.js +302 -0
- package/dist/pairbo.es.js +6086 -3217
- package/dist/pairbo.umd.js +1775 -432
- package/dist/public/Greeting Card from Pairbo.png +0 -0
- package/dist/public/mockServiceWorker.js +302 -0
- package/dist/src/components/button/button.component.d.ts +26 -3
- package/dist/src/components/button-group/button-group.component.d.ts +19 -14
- package/dist/src/components/card-selection/card-selection.component.d.ts +1 -2
- package/dist/src/components/category/category.component.d.ts +1 -0
- package/dist/src/components/drawer/drawer.component.d.ts +3 -0
- package/dist/src/components/editor/editor.component.d.ts +15 -5
- package/dist/src/components/{image-slider → editor-card-slider}/editor-card-slider.component.d.ts +26 -6
- package/dist/src/components/editor-selector/editor-selector.component.d.ts +14 -0
- package/dist/src/components/editor-selector/editor-selector.d.ts +8 -0
- package/dist/src/components/editor-selector/editor-selector.styles.d.ts +2 -0
- package/dist/src/components/fabric-example/fabric-example.component.d.ts +8 -3
- package/dist/src/components/gift-options/gift-options.component.d.ts +39 -0
- package/dist/src/components/gift-options/gift-options.d.ts +8 -0
- package/dist/src/components/gift-options/gift-options.styles.d.ts +2 -0
- package/dist/src/components/handwritten-canvas/handwritten-canvas.component.d.ts +60 -0
- package/dist/src/components/handwritten-canvas/handwritten-canvas.d.ts +8 -0
- package/dist/src/components/handwritten-canvas/handwritten-canvas.styles.d.ts +2 -0
- package/dist/src/components/handwritten-form/handwritten-form.component.d.ts +50 -0
- package/dist/src/components/handwritten-form/handwritten-form.d.ts +8 -0
- package/dist/src/components/handwritten-form/handwritten-form.styles.d.ts +2 -0
- package/dist/src/components/main.d.ts +7 -1
- package/dist/src/components/page-manager/page-manager.component.d.ts +5 -4
- package/dist/src/components/radio-button/radio-button.component.d.ts +5 -3
- package/dist/src/components/radio-group/radio-group.component.d.ts +10 -4
- package/dist/src/components/selector/selector.component.d.ts +3 -1
- package/dist/src/components/textarea/textarea.component.d.ts +2 -0
- package/dist/src/components/type-canvas/type-canvas.component.d.ts +52 -0
- package/dist/src/components/type-canvas/type-canvas.d.ts +8 -0
- package/dist/src/components/type-canvas/type-canvas.styles.d.ts +2 -0
- package/dist/src/components/type-form/type-form.component.d.ts +24 -0
- package/dist/src/components/zoomed-preview/zoomed-preview.component.d.ts +33 -0
- package/dist/src/components/zoomed-preview/zoomed-preview.d.ts +8 -0
- package/dist/src/components/zoomed-preview/zoomed-preview.styles.d.ts +2 -0
- package/dist/src/core/pbo-bridge/PboBridge.d.ts +32 -0
- package/dist/src/core/pbo-bridge/createPboBridge.d.ts +20 -0
- package/dist/src/core/pbo-bridge/index.d.ts +2 -0
- package/dist/src/core/pbo-bridge/types.d.ts +69 -0
- package/dist/src/demo.d.ts +1 -0
- package/dist/src/events/events.d.ts +1 -0
- package/dist/src/events/handwritten-image-dimensions-changed.d.ts +13 -0
- package/dist/src/internal/form.d.ts +54 -0
- package/dist/src/mocks/browser.d.ts +1 -0
- package/dist/src/mocks/handlers.d.ts +1 -0
- package/dist/src/mocks/init.d.ts +1 -0
- package/dist/src/mocks/shopifyHandler.d.ts +2 -0
- package/dist/src/themes/default.css +171 -67
- package/dist/src/utilities/mobile.d.ts +3 -0
- package/package.json +7 -1
- /package/dist/src/components/{image-slider → editor-card-slider}/editor-card-slider.d.ts +0 -0
- /package/dist/src/components/{image-slider → editor-card-slider}/editor-card-slider.styles.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,3 +1,186 @@
|
|
|
1
|
-
|
|
1
|
+
## **🚀 V1 To-Do List**
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
### **🔧 Pending Setup**
|
|
4
|
+
|
|
5
|
+
- [ ] **11ty setup is incomplete.**
|
|
6
|
+
- [ ] **Mock API needed** for:
|
|
7
|
+
- Fetching and submitting cards
|
|
8
|
+
- Processing handwritten images
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## **🎨 Global Theme ([src/themes](src/themes))**
|
|
13
|
+
|
|
14
|
+
- [ ] Define global theme styles
|
|
15
|
+
- Identify `--sl-` global CSS variables in component styles, and replace with `--pbo-`
|
|
16
|
+
- Declare variables in [`default.css`](src/themes/default.css)
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## **📌 Components Overview**
|
|
21
|
+
|
|
22
|
+
### **🗂️ Drawer ([src/components/drawer](src/components/drawer))**
|
|
23
|
+
|
|
24
|
+
- [x] Implement **window-like title bar** with:
|
|
25
|
+
- **Left:** Back icon (if not on Card Selection page)
|
|
26
|
+
- **Center:** Current page name (**Card Selection** / **Card Editing**)
|
|
27
|
+
- **Right:** Close icon to dismiss drawer
|
|
28
|
+
- [x] Make styles more flexible:
|
|
29
|
+
- Support top/bottom/left/right drawers
|
|
30
|
+
- Allow setting drawer size (`30%`, `50%`, `60%`, `100%`)
|
|
31
|
+
- Add opening animations
|
|
32
|
+
- [ ] _(Optional)_ Customizable themes (background, colors, etc.)
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
### **✍️ Editor ([src/components/editor](src/components/editor))**
|
|
37
|
+
|
|
38
|
+
#### 🖥️ **Desktop Layout**
|
|
39
|
+
|
|
40
|
+
- [x] Implement responsive grid layout for desktop
|
|
41
|
+
- Grid rows: `1`
|
|
42
|
+
- Grid cols: `12`
|
|
43
|
+
- Image Slider:
|
|
44
|
+
- `sm`: col start `2`, end `7`
|
|
45
|
+
- `2xl`: col start `3`, end `7`
|
|
46
|
+
- Message Editor:
|
|
47
|
+
- `sm`: col start `7`, end `12`
|
|
48
|
+
- `2xl`: col start `7`, end `11`
|
|
49
|
+
|
|
50
|
+
#### 📱 **Mobile Layout**
|
|
51
|
+
|
|
52
|
+
- [x] Implement responsive flex layout for mobile
|
|
53
|
+
- `display: flex`
|
|
54
|
+
- `flex-direction: column`
|
|
55
|
+
- `gap: 1rem`
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## **📝 Form Elements**
|
|
60
|
+
|
|
61
|
+
### **📄 Type Form ([src/components/type-form](src/components/type-form))**
|
|
62
|
+
|
|
63
|
+
- [x] Add gap between form elements
|
|
64
|
+
- [ ] Implement submit logic:
|
|
65
|
+
- If message is empty → CTA button turns **red** and shows `Skip` link
|
|
66
|
+
- **❓ Where to implement validation logic?** (At type form level or button level?)
|
|
67
|
+
- [x] **Editor ↔️ Image Slider Communication**
|
|
68
|
+
- Auto-focus image slider when user types in textarea
|
|
69
|
+
- Improve communication logic between image slider and type form
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
### **🔠 Font Selector**
|
|
74
|
+
|
|
75
|
+
- [ ] **Extract** `font-selector` as a separate reusable component
|
|
76
|
+
- [ ] Optimize font variable passing
|
|
77
|
+
- [ ] Display font preview in dropdown
|
|
78
|
+
- [ ] Enable global font imports
|
|
79
|
+
- [ ] Improve font variable handling in `<pbo-selector>`
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### **📝 Textarea ([src/components/textarea](src/components/textarea))**
|
|
84
|
+
|
|
85
|
+
- [x] Show **current/max characters** indicator
|
|
86
|
+
- [ ] _(Optional)_ Show **current/max lines** indicator
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## **🎛️ Form Controls**
|
|
91
|
+
|
|
92
|
+
### **️ Radio Button, Group, and Button Enhancements**
|
|
93
|
+
|
|
94
|
+
#### **✅ Submit Button ([src/components/button](src/components/button))**
|
|
95
|
+
|
|
96
|
+
- [ ] **Submit Button Logic**
|
|
97
|
+
- Add validation to check form inputs
|
|
98
|
+
- Support `pre-submit` & `post-submit` callbacks:
|
|
99
|
+
- `pre-submit`: E.g. validate stock before adding a card
|
|
100
|
+
- `post-submit`: E.g. update cart after adding a card
|
|
101
|
+
|
|
102
|
+
#### **🎛️ Button Group ([src/components/button-group](src/components/button-group))**
|
|
103
|
+
|
|
104
|
+
- [x] Define group styles
|
|
105
|
+
- [ ] Implement submit validation & error handling
|
|
106
|
+
|
|
107
|
+
#### **🔘 Radio Button ([src/components/radio-button](src/components/radio-button))**
|
|
108
|
+
|
|
109
|
+
- [x] Enhance styling and logic based on button component
|
|
110
|
+
|
|
111
|
+
#### **📻 Radio Group ([src/components/radio-group](src/components/radio-group))**
|
|
112
|
+
|
|
113
|
+
- [x] Define group styles
|
|
114
|
+
- [ ] Implement validation & error handling
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## **📂 Category & Category Image Components**
|
|
119
|
+
|
|
120
|
+
### **📦 Category ([src/components/category](src/components/category))**
|
|
121
|
+
|
|
122
|
+
- [x] Fix **image aspect ratio** (currently responsive)
|
|
123
|
+
- [x] Ensure **responsive slider**:
|
|
124
|
+
- **Desktop:** Show **4** cards
|
|
125
|
+
- **Mobile:** Show **2** cards
|
|
126
|
+
- Hide arrows if fewer than needed cards
|
|
127
|
+
- [x] Add **hover animations**
|
|
128
|
+
- [x] Improve **category title styling**
|
|
129
|
+
|
|
130
|
+
## Roadmap
|
|
131
|
+
|
|
132
|
+
### Preprepare
|
|
133
|
+
|
|
134
|
+
- [x] **Live Documentation for Development and Future Reference**
|
|
135
|
+
_Status_: The basic structure has been initialized using **11ty**. However, the bundling process for `src/components` is not fully configured. Additionally, rendering inline `` `html` `` within Markdown to actual HTML in **11ty** is not yet fully functional.
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### Phase 1: Core Card Editor
|
|
140
|
+
|
|
141
|
+
- [x] **Font Dropdown Selector**
|
|
142
|
+
- [x] **Editor Dropdown Selector**
|
|
143
|
+
- [x] **Text Alignment Button Group**
|
|
144
|
+
- [x] **Text Color Block**
|
|
145
|
+
- [x] **Live Preview (Zoomed View)**
|
|
146
|
+
- [x] **Typing Live Preview**
|
|
147
|
+
- [ ] **Handwritten Live Preview**
|
|
148
|
+
- [x] **Image Slider**
|
|
149
|
+
- [ ] **CTA Button**
|
|
150
|
+
- [ ] **Handwritten Description Section**
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### Phase 2: Core Card Selection
|
|
155
|
+
|
|
156
|
+
- [x] **Image Slider for Card Covers**
|
|
157
|
+
_(4 columns on desktop, 2 columns on mobile)_
|
|
158
|
+
- [x] **Category Selection**
|
|
159
|
+
- [x] **Image Components (Optional)**
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
### Phase 3: Integration Components
|
|
164
|
+
|
|
165
|
+
- [x] **Card Selection - Main Content**
|
|
166
|
+
- [x] **Card Editor (Typing) - Right Content Group**
|
|
167
|
+
- [ ] **Card Editor (Handwritten) - Right Content Group**
|
|
168
|
+
- [ ] **Card Editor (Handwritten) - Content Section**
|
|
169
|
+
- [x] **Card Editor (Typing) - Content Section**
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
### Phase 4: Modal/Pop-up
|
|
174
|
+
|
|
175
|
+
- [ ] **Checkboxes**
|
|
176
|
+
- [x] **Basic Modal Implementation**
|
|
177
|
+
- [x] **Sectioned Form in Modal**
|
|
178
|
+
- [ ] **Modal Submission API**
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
### Phase 5: Abstract Internal Logic
|
|
183
|
+
|
|
184
|
+
- **TBD** _(Detailed planning will be decided later.)_
|
|
185
|
+
|
|
186
|
+
This phase will focus on abstracting internal logic, such as retrieving card data and synchronizing editing information across different components.
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/* tslint:disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Mock Service Worker.
|
|
6
|
+
* @see https://github.com/mswjs/msw
|
|
7
|
+
* - Please do NOT modify this file.
|
|
8
|
+
* - Please do NOT serve this file on production.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const PACKAGE_VERSION = "2.7.3";
|
|
12
|
+
const INTEGRITY_CHECKSUM = "00729d72e3b82faf54ca8b9621dbb96f";
|
|
13
|
+
const IS_MOCKED_RESPONSE = Symbol("isMockedResponse");
|
|
14
|
+
const activeClientIds = new Set();
|
|
15
|
+
|
|
16
|
+
self.addEventListener("install", function () {
|
|
17
|
+
self.skipWaiting();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
self.addEventListener("activate", function (event) {
|
|
21
|
+
event.waitUntil(self.clients.claim());
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
self.addEventListener("message", async function (event) {
|
|
25
|
+
const clientId = event.source.id;
|
|
26
|
+
|
|
27
|
+
if (!clientId || !self.clients) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const client = await self.clients.get(clientId);
|
|
32
|
+
|
|
33
|
+
if (!client) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const allClients = await self.clients.matchAll({
|
|
38
|
+
type: "window",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
switch (event.data) {
|
|
42
|
+
case "KEEPALIVE_REQUEST": {
|
|
43
|
+
sendToClient(client, {
|
|
44
|
+
type: "KEEPALIVE_RESPONSE",
|
|
45
|
+
});
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
case "INTEGRITY_CHECK_REQUEST": {
|
|
50
|
+
sendToClient(client, {
|
|
51
|
+
type: "INTEGRITY_CHECK_RESPONSE",
|
|
52
|
+
payload: {
|
|
53
|
+
packageVersion: PACKAGE_VERSION,
|
|
54
|
+
checksum: INTEGRITY_CHECKSUM,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
case "MOCK_ACTIVATE": {
|
|
61
|
+
activeClientIds.add(clientId);
|
|
62
|
+
|
|
63
|
+
sendToClient(client, {
|
|
64
|
+
type: "MOCKING_ENABLED",
|
|
65
|
+
payload: {
|
|
66
|
+
client: {
|
|
67
|
+
id: client.id,
|
|
68
|
+
frameType: client.frameType,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
case "MOCK_DEACTIVATE": {
|
|
76
|
+
activeClientIds.delete(clientId);
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
case "CLIENT_CLOSED": {
|
|
81
|
+
activeClientIds.delete(clientId);
|
|
82
|
+
|
|
83
|
+
const remainingClients = allClients.filter(client => {
|
|
84
|
+
return client.id !== clientId;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Unregister itself when there are no more clients
|
|
88
|
+
if (remainingClients.length === 0) {
|
|
89
|
+
self.registration.unregister();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
self.addEventListener("fetch", function (event) {
|
|
98
|
+
const { request } = event;
|
|
99
|
+
|
|
100
|
+
// Bypass navigation requests.
|
|
101
|
+
if (request.mode === "navigate") {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Opening the DevTools triggers the "only-if-cached" request
|
|
106
|
+
// that cannot be handled by the worker. Bypass such requests.
|
|
107
|
+
if (request.cache === "only-if-cached" && request.mode !== "same-origin") {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Bypass all requests when there are no active clients.
|
|
112
|
+
// Prevents the self-unregistered worked from handling requests
|
|
113
|
+
// after it's been deleted (still remains active until the next reload).
|
|
114
|
+
if (activeClientIds.size === 0) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Generate unique request ID.
|
|
119
|
+
const requestId = crypto.randomUUID();
|
|
120
|
+
event.respondWith(handleRequest(event, requestId));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
async function handleRequest(event, requestId) {
|
|
124
|
+
const client = await resolveMainClient(event);
|
|
125
|
+
const response = await getResponse(event, client, requestId);
|
|
126
|
+
|
|
127
|
+
// Send back the response clone for the "response:*" life-cycle events.
|
|
128
|
+
// Ensure MSW is active and ready to handle the message, otherwise
|
|
129
|
+
// this message will pend indefinitely.
|
|
130
|
+
if (client && activeClientIds.has(client.id)) {
|
|
131
|
+
(async function () {
|
|
132
|
+
const responseClone = response.clone();
|
|
133
|
+
|
|
134
|
+
sendToClient(
|
|
135
|
+
client,
|
|
136
|
+
{
|
|
137
|
+
type: "RESPONSE",
|
|
138
|
+
payload: {
|
|
139
|
+
requestId,
|
|
140
|
+
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
|
141
|
+
type: responseClone.type,
|
|
142
|
+
status: responseClone.status,
|
|
143
|
+
statusText: responseClone.statusText,
|
|
144
|
+
body: responseClone.body,
|
|
145
|
+
headers: Object.fromEntries(responseClone.headers.entries()),
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
[responseClone.body]
|
|
149
|
+
);
|
|
150
|
+
})();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return response;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Resolve the main client for the given event.
|
|
157
|
+
// Client that issues a request doesn't necessarily equal the client
|
|
158
|
+
// that registered the worker. It's with the latter the worker should
|
|
159
|
+
// communicate with during the response resolving phase.
|
|
160
|
+
async function resolveMainClient(event) {
|
|
161
|
+
const client = await self.clients.get(event.clientId);
|
|
162
|
+
|
|
163
|
+
if (activeClientIds.has(event.clientId)) {
|
|
164
|
+
return client;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (client?.frameType === "top-level") {
|
|
168
|
+
return client;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const allClients = await self.clients.matchAll({
|
|
172
|
+
type: "window",
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
return allClients
|
|
176
|
+
.filter(client => {
|
|
177
|
+
// Get only those clients that are currently visible.
|
|
178
|
+
return client.visibilityState === "visible";
|
|
179
|
+
})
|
|
180
|
+
.find(client => {
|
|
181
|
+
// Find the client ID that's recorded in the
|
|
182
|
+
// set of clients that have registered the worker.
|
|
183
|
+
return activeClientIds.has(client.id);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function getResponse(event, client, requestId) {
|
|
188
|
+
const { request } = event;
|
|
189
|
+
|
|
190
|
+
// Clone the request because it might've been already used
|
|
191
|
+
// (i.e. its body has been read and sent to the client).
|
|
192
|
+
const requestClone = request.clone();
|
|
193
|
+
|
|
194
|
+
function passthrough() {
|
|
195
|
+
// Cast the request headers to a new Headers instance
|
|
196
|
+
// so the headers can be manipulated with.
|
|
197
|
+
const headers = new Headers(requestClone.headers);
|
|
198
|
+
|
|
199
|
+
// Remove the "accept" header value that marked this request as passthrough.
|
|
200
|
+
// This prevents request alteration and also keeps it compliant with the
|
|
201
|
+
// user-defined CORS policies.
|
|
202
|
+
const acceptHeader = headers.get("accept");
|
|
203
|
+
if (acceptHeader) {
|
|
204
|
+
const values = acceptHeader.split(",").map(value => value.trim());
|
|
205
|
+
const filteredValues = values.filter(value => value !== "msw/passthrough");
|
|
206
|
+
|
|
207
|
+
if (filteredValues.length > 0) {
|
|
208
|
+
headers.set("accept", filteredValues.join(", "));
|
|
209
|
+
} else {
|
|
210
|
+
headers.delete("accept");
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return fetch(requestClone, { headers });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Bypass mocking when the client is not active.
|
|
218
|
+
if (!client) {
|
|
219
|
+
return passthrough();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Bypass initial page load requests (i.e. static assets).
|
|
223
|
+
// The absence of the immediate/parent client in the map of the active clients
|
|
224
|
+
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
|
|
225
|
+
// and is not ready to handle requests.
|
|
226
|
+
if (!activeClientIds.has(client.id)) {
|
|
227
|
+
return passthrough();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Notify the client that a request has been intercepted.
|
|
231
|
+
const requestBuffer = await request.arrayBuffer();
|
|
232
|
+
const clientMessage = await sendToClient(
|
|
233
|
+
client,
|
|
234
|
+
{
|
|
235
|
+
type: "REQUEST",
|
|
236
|
+
payload: {
|
|
237
|
+
id: requestId,
|
|
238
|
+
url: request.url,
|
|
239
|
+
mode: request.mode,
|
|
240
|
+
method: request.method,
|
|
241
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
242
|
+
cache: request.cache,
|
|
243
|
+
credentials: request.credentials,
|
|
244
|
+
destination: request.destination,
|
|
245
|
+
integrity: request.integrity,
|
|
246
|
+
redirect: request.redirect,
|
|
247
|
+
referrer: request.referrer,
|
|
248
|
+
referrerPolicy: request.referrerPolicy,
|
|
249
|
+
body: requestBuffer,
|
|
250
|
+
keepalive: request.keepalive,
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
[requestBuffer]
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
switch (clientMessage.type) {
|
|
257
|
+
case "MOCK_RESPONSE": {
|
|
258
|
+
return respondWithMock(clientMessage.data);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
case "PASSTHROUGH": {
|
|
262
|
+
return passthrough();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return passthrough();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function sendToClient(client, message, transferrables = []) {
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
const channel = new MessageChannel();
|
|
272
|
+
|
|
273
|
+
channel.port1.onmessage = event => {
|
|
274
|
+
if (event.data && event.data.error) {
|
|
275
|
+
return reject(event.data.error);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
resolve(event.data);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
client.postMessage(message, [channel.port2].concat(transferrables.filter(Boolean)));
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function respondWithMock(response) {
|
|
286
|
+
// Setting response status code to 0 is a no-op.
|
|
287
|
+
// However, when responding with a "Response.error()", the produced Response
|
|
288
|
+
// instance will have status code set to 0. Since it's not possible to create
|
|
289
|
+
// a Response instance with status code 0, handle that use-case separately.
|
|
290
|
+
if (response.status === 0) {
|
|
291
|
+
return Response.error();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const mockedResponse = new Response(response.body, response);
|
|
295
|
+
|
|
296
|
+
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
|
|
297
|
+
value: true,
|
|
298
|
+
enumerable: true,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
return mockedResponse;
|
|
302
|
+
}
|