@conduit-client/salesforce-lightning-service-worker 3.2.0
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/LICENSE.txt +27 -0
- package/README.md +135 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/types/__tests__/index.spec.d.ts +1 -0
- package/dist/types/csrf.d.ts +8 -0
- package/dist/types/index.d.ts +10 -0
- package/package.json +37 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
*Attorney/Client Privileged + Confidential*
|
|
2
|
+
|
|
3
|
+
Terms of Use for Public Code (Non-OSS)
|
|
4
|
+
|
|
5
|
+
*NOTE:* Before publishing code under this license/these Terms of Use, please review https://salesforce.quip.com/WFfvAMKB18AL and confirm that you’ve completed all prerequisites described therein. *These Terms of Use may not be used or modified without input from IP and Product Legal.*
|
|
6
|
+
|
|
7
|
+
*Terms of Use*
|
|
8
|
+
|
|
9
|
+
Copyright 2022 Salesforce, Inc. All rights reserved.
|
|
10
|
+
|
|
11
|
+
These Terms of Use govern the download, installation, and/or use of this software provided by Salesforce, Inc. (“Salesforce”) (the “Software”), were last updated on April 15, 2022, ** and constitute a legally binding agreement between you and Salesforce. If you do not agree to these Terms of Use, do not install or use the Software.
|
|
12
|
+
|
|
13
|
+
Salesforce grants you a worldwide, non-exclusive, no-charge, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute the Software and derivative works subject to these Terms. These Terms shall be included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
Subject to the limited rights expressly granted hereunder, Salesforce reserves all rights, title, and interest in and to all intellectual property subsisting in the Software. No rights are granted to you hereunder other than as expressly set forth herein. Users residing in countries on the United States Office of Foreign Assets Control sanction list, or which are otherwise subject to a US export embargo, may not use the Software.
|
|
16
|
+
|
|
17
|
+
Implementation of the Software may require development work, for which you are responsible. The Software may contain bugs, errors and incompatibilities and is made available on an AS IS basis without support, updates, or service level commitments.
|
|
18
|
+
|
|
19
|
+
Salesforce reserves the right at any time to modify, suspend, or discontinue, the Software (or any part thereof) with or without notice. You agree that Salesforce shall not be liable to you or to any third party for any modification, suspension, or discontinuance.
|
|
20
|
+
|
|
21
|
+
You agree to defend Salesforce against any claim, demand, suit or proceeding made or brought against Salesforce by a third party arising out of or accruing from (a) your use of the Software, and (b) any application you develop with the Software that infringes any copyright, trademark, trade secret, trade dress, patent, or other intellectual property right of any person or defames any person or violates their rights of publicity or privacy (each a “Claim Against Salesforce”), and will indemnify Salesforce from any damages, attorney fees, and costs finally awarded against Salesforce as a result of, or for any amounts paid by Salesforce under a settlement approved by you in writing of, a Claim Against Salesforce, provided Salesforce (x) promptly gives you written notice of the Claim Against Salesforce, (y) gives you sole control of the defense and settlement of the Claim Against Salesforce (except that you may not settle any Claim Against Salesforce unless it unconditionally releases Salesforce of all liability), and (z) gives you all reasonable assistance, at your expense.
|
|
22
|
+
|
|
23
|
+
WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE SOFTWARE IS NOT SUPPORTED AND IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL SALESFORCE HAVE ANY LIABILITY FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES, OR DAMAGES BASED ON LOST PROFITS, DATA, OR USE, IN CONNECTION WITH THE SOFTWARE, HOWEVER CAUSED AND WHETHER IN CONTRACT, TORT, OR UNDER ANY OTHER THEORY OF LIABILITY, WHETHER OR NOT YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
24
|
+
|
|
25
|
+
These Terms of Use shall be governed exclusively by the internal laws of the State of California, without regard to its conflicts of laws rules. Each party hereby consents to the exclusive jurisdiction of the state and federal courts located in San Francisco County, California to adjudicate any dispute arising out of or relating to these Terms of Use and the download, installation, and/or use of the Software. Except as expressly stated herein, these Terms of Use constitute the entire agreement between the parties, and supersede all prior and contemporaneous agreements, proposals, or representations, written or oral, concerning their subject matter. No modification, amendment, or waiver of any provision of these Terms of Use shall be effective unless it is by an update to these Terms of Use that Salesforce makes available, or is in writing and signed by the party against whom the modification, amendment, or waiver is to be asserted.
|
|
26
|
+
|
|
27
|
+
_*Data Privacy*_: Salesforce may collect, process, and store device, system, and other information related to your use of the Software. This information includes, but is not limited to, IP address, user metrics, and other data (“Usage Data”). Salesforce may use Usage Data for analytics, product development, and marketing purposes. You acknowledge that files generated in conjunction with the Software may contain sensitive or confidential data, and you are solely responsible for anonymizing and protecting such data.
|
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# Salesforce Lightning Service Worker
|
|
2
|
+
|
|
3
|
+
A lightweight service worker utility for Salesforce Lightning applications that provides basic service worker registration and management functionality.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @conduit-client/salesforce-lightning-service-worker
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
This package exports two main functions that work together to implement service worker functionality in your application:
|
|
14
|
+
|
|
15
|
+
### 1. Register the Service Worker (App Code)
|
|
16
|
+
|
|
17
|
+
In your main application code, use the `registerServiceWorker` function to register your service worker:
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { registerServiceWorker } from '@conduit-client/salesforce-lightning-service-worker';
|
|
21
|
+
|
|
22
|
+
// Register service worker with module type support
|
|
23
|
+
const registration = await registerServiceWorker('./sw.js', { type: 'module' });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Parameters:**
|
|
27
|
+
|
|
28
|
+
- `scriptUrl`: The URL path to your service worker file
|
|
29
|
+
- `options`: Optional `RegistrationOptions` (e.g., `{ type: 'module' }` for ES6 module support)
|
|
30
|
+
|
|
31
|
+
### 2. Create the Service Worker File
|
|
32
|
+
|
|
33
|
+
You must configure your bundler to expose a statically named service worker file (without hash tokens) that calls the `createServiceWorker` function:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
// sw.js or similar
|
|
37
|
+
import { createServiceWorker } from '@conduit-client/salesforce-lightning-service-worker';
|
|
38
|
+
|
|
39
|
+
createServiceWorker();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Important Configuration Notes:**
|
|
43
|
+
|
|
44
|
+
1. **Static File Name**: The service worker file must have a static name (e.g., `sw.js`) without hash tokens to ensure consistent registration/updates
|
|
45
|
+
2. **Module Type**: If using ES6 imports in your service worker, register it with `{ type: 'module' }`
|
|
46
|
+
3. **Scope**: The path from which the service worker file is served establishes the scope for which the service worker will apply. For example:
|
|
47
|
+
- `/sw.js` → Controls the entire origin
|
|
48
|
+
- `/app/sw.js` → Only controls paths under `/app/`
|
|
49
|
+
|
|
50
|
+
## Service Worker Features
|
|
51
|
+
|
|
52
|
+
The `createServiceWorker` function sets up a basic service worker with:
|
|
53
|
+
|
|
54
|
+
- **Install Handler**: Skips waiting to activate immediately
|
|
55
|
+
- **Activate Handler**: Claims all clients immediately
|
|
56
|
+
- **Fetch Handler**: Intercepts and logs all `fetch` requests (currently passes through all requests)
|
|
57
|
+
|
|
58
|
+
## Example Implementation
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
// Main app code
|
|
62
|
+
import { registerServiceWorker } from '@conduit-client/salesforce-lightning-service-worker';
|
|
63
|
+
|
|
64
|
+
async function initializeApp() {
|
|
65
|
+
try {
|
|
66
|
+
const registration = await registerServiceWorker('./sw.js', { type: 'module' });
|
|
67
|
+
console.log('Service worker registered:', registration?.scope);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error('Service worker registration failed:', error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// sw.js (served statically by your bundler)
|
|
76
|
+
import { createServiceWorker } from '@conduit-client/salesforce-lightning-service-worker';
|
|
77
|
+
|
|
78
|
+
createServiceWorker();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Development
|
|
82
|
+
|
|
83
|
+
### Running the Development Server
|
|
84
|
+
|
|
85
|
+
Use the `dev` script to run and debug the service worker code:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm run dev
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
This will:
|
|
92
|
+
|
|
93
|
+
1. Build the service worker code
|
|
94
|
+
2. Start a development server on port 3000
|
|
95
|
+
3. Serve a test page with service worker registration
|
|
96
|
+
4. Automatically open your browser
|
|
97
|
+
|
|
98
|
+
### Development Features
|
|
99
|
+
|
|
100
|
+
- **Live Service Worker Updates**: The development server includes automatic service worker update detection and page reloading when you make changes to the source code
|
|
101
|
+
- **Test API Endpoint**: A dummy `/api/data` endpoint is configured to test fetch interception by the service worker
|
|
102
|
+
- **Debug Console**: The test page includes buttons to check service worker status and test API calls
|
|
103
|
+
- **TypeScript Transformation**: Service worker code is served as transformed TypeScript via Vite
|
|
104
|
+
|
|
105
|
+
### Development Workflow
|
|
106
|
+
|
|
107
|
+
1. Make changes to `src/index.ts` (service worker logic)
|
|
108
|
+
2. The page will automatically detect service worker updates and reload
|
|
109
|
+
3. Use the test page buttons to verify your changes:
|
|
110
|
+
- **Check SW Status**: View service worker registration details
|
|
111
|
+
- **Fetch API Data**: Test the `/api/data` endpoint through the service worker
|
|
112
|
+
- **Clear Output**: Clear the debug console
|
|
113
|
+
|
|
114
|
+
### Development File Structure
|
|
115
|
+
|
|
116
|
+
- `src/index.ts` - Main service worker code and registration utilities
|
|
117
|
+
- `scripts/dev.ts` - Development server with API mock and service worker serving
|
|
118
|
+
- `scripts/dev-service-worker.ts` - Entry point for the development service worker
|
|
119
|
+
- `scripts/dev-test-page.html` - Test page for debugging service worker functionality
|
|
120
|
+
|
|
121
|
+
### Building for Production
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
npm run build
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
This creates production-ready files in the `dist/` directory.
|
|
128
|
+
|
|
129
|
+
## Browser Support
|
|
130
|
+
|
|
131
|
+
Requires browsers with service worker support. The registration function includes feature detection and will gracefully handle unsupported browsers.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
This software is provided as-is with no support provided.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* For full license text, see the LICENSE.txt file
|
|
5
|
+
*/
|
|
6
|
+
function createServiceWorker({ debug } = {}) {
|
|
7
|
+
const scope = self;
|
|
8
|
+
scope.addEventListener("install", (event) => {
|
|
9
|
+
if (debug) console.log("[Service Worker] Installed");
|
|
10
|
+
event.waitUntil(scope.skipWaiting());
|
|
11
|
+
});
|
|
12
|
+
scope.addEventListener("activate", (event) => {
|
|
13
|
+
if (debug) console.log("[Service Worker] Activated");
|
|
14
|
+
event.waitUntil(scope.clients.claim());
|
|
15
|
+
});
|
|
16
|
+
scope.addEventListener("fetch", (event) => {
|
|
17
|
+
console.log("[Service Worker] Fetch intercepted for pass-through:", {
|
|
18
|
+
url: event.request.url,
|
|
19
|
+
method: event.request.method,
|
|
20
|
+
destination: event.request.destination,
|
|
21
|
+
mode: event.request.mode
|
|
22
|
+
});
|
|
23
|
+
event.respondWith(fetch(event.request));
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
createServiceWorker
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["type Config = {\n debug?: boolean;\n};\n\n/**\n * Adds event listeners for setting up service worker.\n *\n * @param debug\n */\nexport function createServiceWorker({ debug }: Config = {}) {\n const scope = self as any as ServiceWorkerGlobalScope;\n\n scope.addEventListener('install', (event) => {\n if (debug) console.log('[Service Worker] Installed');\n\n // Skip waiting to activate immediately\n event.waitUntil(scope.skipWaiting());\n });\n\n scope.addEventListener('activate', (event) => {\n if (debug) console.log('[Service Worker] Activated');\n\n // Claim all clients immediately\n event.waitUntil(scope.clients.claim());\n });\n\n scope.addEventListener('fetch', (event) => {\n console.log('[Service Worker] Fetch intercepted for pass-through:', {\n url: event.request.url,\n method: event.request.method,\n destination: event.request.destination,\n mode: event.request.mode,\n });\n\n // Simply forward for now\n event.respondWith(fetch(event.request));\n });\n}\n"],"names":[],"mappings":";;;;;AASO,SAAS,oBAAoB,EAAE,MAAA,IAAkB,IAAI;AACxD,QAAM,QAAQ;AAEd,QAAM,iBAAiB,WAAW,CAAC,UAAU;AACzC,QAAI,MAAO,SAAQ,IAAI,4BAA4B;AAGnD,UAAM,UAAU,MAAM,aAAa;AAAA,EACvC,CAAC;AAED,QAAM,iBAAiB,YAAY,CAAC,UAAU;AAC1C,QAAI,MAAO,SAAQ,IAAI,4BAA4B;AAGnD,UAAM,UAAU,MAAM,QAAQ,MAAA,CAAO;AAAA,EACzC,CAAC;AAED,QAAM,iBAAiB,SAAS,CAAC,UAAU;AACvC,YAAQ,IAAI,wDAAwD;AAAA,MAChE,KAAK,MAAM,QAAQ;AAAA,MACnB,QAAQ,MAAM,QAAQ;AAAA,MACtB,aAAa,MAAM,QAAQ;AAAA,MAC3B,MAAM,MAAM,QAAQ;AAAA,IAAA,CACvB;AAGD,UAAM,YAAY,MAAM,MAAM,OAAO,CAAC;AAAA,EAC1C,CAAC;AACL;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
/**
|
|
3
|
+
* Enforces a valid CSRF token for all requests that modify data
|
|
4
|
+
*
|
|
5
|
+
* @param targetClient - The client to add the interceptor to
|
|
6
|
+
* @param csrfClient - The client to use for fetching CSRF tokens (should not have interceptors to avoid circular dependencies)
|
|
7
|
+
*/
|
|
8
|
+
export declare function csrfInterceptor(targetClient: AxiosInstance, csrfClient: AxiosInstance): void;
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@conduit-client/salesforce-lightning-service-worker",
|
|
3
|
+
"version": "3.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Service worker for accessing Salesforce data",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/salesforce-experience-platform-emu/onestore",
|
|
10
|
+
"directory": "packages/@conduit-client/salesforce-lightning-service-worker"
|
|
11
|
+
},
|
|
12
|
+
"license": "SEE LICENSE IN LICENSE.txt",
|
|
13
|
+
"main": "dist/index.js",
|
|
14
|
+
"types": "dist/types/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "vite build && tsc --build --emitDeclarationOnly",
|
|
20
|
+
"clean": "rm -rf dist",
|
|
21
|
+
"dev": "npm run build && tsx scripts/dev.ts",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:size": "size-limit",
|
|
24
|
+
"watch": "npm run build --watch"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/express": "^4.17.17",
|
|
28
|
+
"express": "^4.18.2",
|
|
29
|
+
"tsx": "^4.7.0"
|
|
30
|
+
},
|
|
31
|
+
"size-limit": [
|
|
32
|
+
{
|
|
33
|
+
"path": "dist/index.js",
|
|
34
|
+
"limit": "344 B"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|