@dataships/launcher 0.0.0-stage → 0.1.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 +202 -0
- package/README.md +66 -2
- package/dist/integrity.json +4 -0
- package/dist/launcher.js +229 -0
- package/dist/modules/alia-0.1.0.js +5 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 DATASHIPS LIMITED
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dataships/launcher
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Dataships Launcher is the one script a storefront needs. It reads your
|
|
4
|
+
public key from its own script tag, asks the Dataships API what to render on
|
|
5
|
+
this storefront, and loads only the code that is needed for it. Unless the API
|
|
6
|
+
says yes, it renders nothing and logs nothing.
|
|
7
|
+
|
|
8
|
+
```html
|
|
9
|
+
<script
|
|
10
|
+
src="https://sdk.dataships.io/launcher/launcher.js"
|
|
11
|
+
data-public-key="ds_pk_live_…"
|
|
12
|
+
defer
|
|
13
|
+
></script>
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Place it on every page of the storefront. There is nothing to call and nothing
|
|
17
|
+
to configure in the page: what is rendered, and where, is set up in Dataships.
|
|
18
|
+
|
|
19
|
+
## Script tag attributes
|
|
20
|
+
|
|
21
|
+
| Attribute | Required | Meaning |
|
|
22
|
+
|---|---|---|
|
|
23
|
+
| `data-public-key` | yes | Your Dataships public key (`ds_pk_…`). Never a server key. |
|
|
24
|
+
| `data-preview-mode` | no | `"true"` renders every element with sample content and makes no API calls, for styling in a theme editor. Nothing is recorded. |
|
|
25
|
+
| `data-base-url` | no | API base URL. Defaults to `https://api.dataships.io`. |
|
|
26
|
+
|
|
27
|
+
The Shopify theme editor (`Shopify.designMode`) turns preview mode on by itself.
|
|
28
|
+
|
|
29
|
+
## What it does
|
|
30
|
+
|
|
31
|
+
1. Reads `data-public-key`. Without it, it stops.
|
|
32
|
+
2. Calls `GET /v1/elements` with the key as `x-api-key`. The storefront's domain
|
|
33
|
+
must be on your list of allowed domains.
|
|
34
|
+
3. Keeps the answer for 5 minutes in the tab's `sessionStorage`, so the API is
|
|
35
|
+
called at most once per 5 minutes per tab.
|
|
36
|
+
4. If the API refuses (403), cannot be reached, or lists nothing to render, the
|
|
37
|
+
launcher stops: nothing is added to the page and nothing is written to the
|
|
38
|
+
console.
|
|
39
|
+
5. Otherwise it loads one module per kind of element, and only when the form
|
|
40
|
+
provider that element belongs to is on the page:
|
|
41
|
+
|
|
42
|
+
| Element | Module | Loaded when |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `ALIA_FORM` | `modules/alia-<version>.js` | Alia is on the page |
|
|
45
|
+
|
|
46
|
+
Modules are loaded with a dynamic `import()` from the same location as
|
|
47
|
+
`launcher.js`. If your site sends a Content-Security-Policy, allow
|
|
48
|
+
`https://sdk.dataships.io` in `script-src` and `https://api.dataships.io` in
|
|
49
|
+
`connect-src`.
|
|
50
|
+
|
|
51
|
+
## CDN
|
|
52
|
+
|
|
53
|
+
| URL | Serves |
|
|
54
|
+
|---|---|
|
|
55
|
+
| `https://sdk.dataships.io/launcher/launcher.js` | the latest stable release |
|
|
56
|
+
| `https://sdk.dataships.io/launcher/<version>/launcher.js` | that exact release |
|
|
57
|
+
| `https://sdk.dataships.io/launcher/next/launcher.js` | the latest release candidate |
|
|
58
|
+
| `https://sdk.dataships.io/launcher/<version>/integrity.json` | SHA-384 of each script |
|
|
59
|
+
|
|
60
|
+
## Browser support
|
|
61
|
+
|
|
62
|
+
Browsers with ES2020 and dynamic `import()`: every current version of Chrome,
|
|
63
|
+
Edge, Firefox and Safari.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
Apache-2.0
|
package/dist/launcher.js
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var __typeError = (msg) => {
|
|
5
|
+
throw TypeError(msg);
|
|
6
|
+
};
|
|
7
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
8
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
9
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
10
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
11
|
+
|
|
12
|
+
// src/elementsApi.ts
|
|
13
|
+
var ELEMENTS_PATH = "/v1/elements";
|
|
14
|
+
var isNonEmptyString = (value) => typeof value === "string" && value.length > 0;
|
|
15
|
+
var parseElements = (body) => {
|
|
16
|
+
if (typeof body !== "object" || body === null) return null;
|
|
17
|
+
const { datashipsSessionId, token, elements } = body;
|
|
18
|
+
if (!isNonEmptyString(datashipsSessionId) || !isNonEmptyString(token) || !Array.isArray(elements))
|
|
19
|
+
return null;
|
|
20
|
+
const refs = [];
|
|
21
|
+
for (const element of elements) {
|
|
22
|
+
if (typeof element !== "object" || element === null) return null;
|
|
23
|
+
const { type, id } = element;
|
|
24
|
+
if (!isNonEmptyString(type) || !isNonEmptyString(id)) return null;
|
|
25
|
+
refs.push({ type, id });
|
|
26
|
+
}
|
|
27
|
+
return { datashipsSessionId, token, elements: refs };
|
|
28
|
+
};
|
|
29
|
+
var idsOfType = (data, type) => data.elements.filter((element) => element.type === type).map((element) => element.id);
|
|
30
|
+
var fetchElements = async (fetchFn, baseUrl, publicKey) => {
|
|
31
|
+
try {
|
|
32
|
+
const response = await fetchFn(`${baseUrl}${ELEMENTS_PATH}`, {
|
|
33
|
+
method: "GET",
|
|
34
|
+
headers: { "x-api-key": publicKey },
|
|
35
|
+
credentials: "omit"
|
|
36
|
+
});
|
|
37
|
+
if (response.status === 403) return { status: "refused" };
|
|
38
|
+
if (response.status !== 200) return { status: "failed" };
|
|
39
|
+
const data = parseElements(await response.json());
|
|
40
|
+
if (!data) return { status: "failed" };
|
|
41
|
+
return data.elements.length === 0 ? { status: "refused" } : { status: "ok", data };
|
|
42
|
+
} catch {
|
|
43
|
+
return { status: "failed" };
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/cache.ts
|
|
48
|
+
var CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
49
|
+
var KEY_PREFIX = "dataships:launcher:elements:";
|
|
50
|
+
var _key, _win;
|
|
51
|
+
var ElementsCache = class {
|
|
52
|
+
constructor(win, publicKey) {
|
|
53
|
+
__privateAdd(this, _key);
|
|
54
|
+
__privateAdd(this, _win);
|
|
55
|
+
__privateSet(this, _win, win);
|
|
56
|
+
__privateSet(this, _key, `${KEY_PREFIX}${publicKey}`);
|
|
57
|
+
}
|
|
58
|
+
read(now) {
|
|
59
|
+
try {
|
|
60
|
+
const raw = __privateGet(this, _win).sessionStorage.getItem(__privateGet(this, _key));
|
|
61
|
+
if (!raw) return null;
|
|
62
|
+
const entry = JSON.parse(raw);
|
|
63
|
+
if (typeof entry.at !== "number" || now < entry.at || now - entry.at >= CACHE_TTL_MS)
|
|
64
|
+
return null;
|
|
65
|
+
if (entry.status === "refused") return { status: "refused" };
|
|
66
|
+
const data = entry.status === "ok" ? parseElements(entry.data) : null;
|
|
67
|
+
return data && data.elements.length > 0 ? { status: "ok", data } : null;
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
write(answer, now) {
|
|
73
|
+
try {
|
|
74
|
+
__privateGet(this, _win).sessionStorage.setItem(
|
|
75
|
+
__privateGet(this, _key),
|
|
76
|
+
JSON.stringify({ at: now, ...answer })
|
|
77
|
+
);
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
_key = new WeakMap();
|
|
83
|
+
_win = new WeakMap();
|
|
84
|
+
|
|
85
|
+
// src/config.ts
|
|
86
|
+
var PRODUCTION_BASE_URL = "https://api.dataships.io";
|
|
87
|
+
var readConfig = (script, win) => {
|
|
88
|
+
if (!script) return null;
|
|
89
|
+
const publicKey = script.dataset.publicKey?.trim();
|
|
90
|
+
if (!publicKey || !script.src) return null;
|
|
91
|
+
return {
|
|
92
|
+
publicKey,
|
|
93
|
+
baseUrl: (script.dataset.baseUrl?.trim() || PRODUCTION_BASE_URL).replace(
|
|
94
|
+
/\/+$/,
|
|
95
|
+
""
|
|
96
|
+
),
|
|
97
|
+
// The Shopify theme editor sets designMode even when the embed forgot the attribute.
|
|
98
|
+
preview: script.dataset.previewMode === "true" || win.Shopify?.designMode === true,
|
|
99
|
+
scriptUrl: script.src
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/modules/alia/detect.ts
|
|
104
|
+
var ALIA_EVENTS = [
|
|
105
|
+
"alia:campaignMatched",
|
|
106
|
+
"alia:popupView",
|
|
107
|
+
"alia:signup",
|
|
108
|
+
"alia:popupClose"
|
|
109
|
+
];
|
|
110
|
+
var isAliaOnPage = (win) => win.alia !== void 0 || win.document.querySelector('[id^="alia-root-"]') !== null;
|
|
111
|
+
var watchAlia = (win) => {
|
|
112
|
+
const events = [];
|
|
113
|
+
let markPresent;
|
|
114
|
+
const present = new Promise((resolve) => markPresent = resolve);
|
|
115
|
+
const onEvent = (event) => {
|
|
116
|
+
events.push(event);
|
|
117
|
+
markPresent();
|
|
118
|
+
};
|
|
119
|
+
for (const name of ALIA_EVENTS) win.document.addEventListener(name, onEvent);
|
|
120
|
+
if (isAliaOnPage(win)) markPresent();
|
|
121
|
+
return {
|
|
122
|
+
present,
|
|
123
|
+
events,
|
|
124
|
+
stop: () => {
|
|
125
|
+
for (const name of ALIA_EVENTS)
|
|
126
|
+
win.document.removeEventListener(name, onEvent);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// src/modules/registry.ts
|
|
132
|
+
var MODULES = [
|
|
133
|
+
{
|
|
134
|
+
type: "ALIA_FORM",
|
|
135
|
+
file: `modules/alia-${"0.1.0"}.js`,
|
|
136
|
+
watch: watchAlia
|
|
137
|
+
}
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
// src/boot.ts
|
|
141
|
+
var defaultImport = (url) => import(
|
|
142
|
+
/* @vite-ignore */
|
|
143
|
+
url
|
|
144
|
+
);
|
|
145
|
+
var boot = async (env) => {
|
|
146
|
+
try {
|
|
147
|
+
await run(env);
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
var run = async (env) => {
|
|
152
|
+
const win = env.window;
|
|
153
|
+
const config = readConfig(env.script, win);
|
|
154
|
+
if (!config) return;
|
|
155
|
+
const guard = win;
|
|
156
|
+
if (guard.__datashipsLauncher) return;
|
|
157
|
+
guard.__datashipsLauncher = { version: "0.1.0" };
|
|
158
|
+
const modules = env.modules ?? MODULES;
|
|
159
|
+
const importModule = env.importModule ?? defaultImport;
|
|
160
|
+
const load = (definition, context, beforeStart) => startModule(importModule, config, definition, context, beforeStart);
|
|
161
|
+
if (config.preview) {
|
|
162
|
+
const context = {
|
|
163
|
+
publicKey: config.publicKey,
|
|
164
|
+
baseUrl: config.baseUrl,
|
|
165
|
+
preview: true,
|
|
166
|
+
token: null,
|
|
167
|
+
datashipsSessionId: null,
|
|
168
|
+
campaignIds: [],
|
|
169
|
+
events: []
|
|
170
|
+
};
|
|
171
|
+
await Promise.all(modules.map((definition) => load(definition, context)));
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const watched = modules.map((definition) => ({
|
|
175
|
+
definition,
|
|
176
|
+
watch: definition.watch(win)
|
|
177
|
+
}));
|
|
178
|
+
const outcome = await resolveElements(env, config);
|
|
179
|
+
for (const { definition, watch } of watched) {
|
|
180
|
+
const campaignIds = outcome.status === "ok" ? idsOfType(outcome.data, definition.type) : [];
|
|
181
|
+
if (outcome.status !== "ok" || campaignIds.length === 0) {
|
|
182
|
+
watch.stop();
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const context = {
|
|
186
|
+
publicKey: config.publicKey,
|
|
187
|
+
baseUrl: config.baseUrl,
|
|
188
|
+
preview: false,
|
|
189
|
+
token: outcome.data.token,
|
|
190
|
+
datashipsSessionId: outcome.data.datashipsSessionId,
|
|
191
|
+
campaignIds,
|
|
192
|
+
events: watch.events
|
|
193
|
+
};
|
|
194
|
+
void watch.present.then(() => load(definition, context, watch.stop));
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
var resolveElements = async (env, config) => {
|
|
198
|
+
const now = env.now ?? Date.now;
|
|
199
|
+
const cache = new ElementsCache(env.window, config.publicKey);
|
|
200
|
+
const cached = cache.read(now());
|
|
201
|
+
if (cached) return cached;
|
|
202
|
+
const fetchFn = env.fetch ?? ((url, init) => env.window.fetch(url, init));
|
|
203
|
+
const outcome = await fetchElements(
|
|
204
|
+
fetchFn,
|
|
205
|
+
config.baseUrl,
|
|
206
|
+
config.publicKey
|
|
207
|
+
);
|
|
208
|
+
if (outcome.status !== "failed") cache.write(outcome, now());
|
|
209
|
+
return outcome;
|
|
210
|
+
};
|
|
211
|
+
var startModule = async (importModule, config, definition, context, beforeStart) => {
|
|
212
|
+
try {
|
|
213
|
+
const module = await importModule(
|
|
214
|
+
new URL(definition.file, config.scriptUrl).href
|
|
215
|
+
);
|
|
216
|
+
beforeStart?.();
|
|
217
|
+
await module.start(context);
|
|
218
|
+
} catch {
|
|
219
|
+
beforeStart?.();
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// src/index.ts
|
|
224
|
+
void boot({
|
|
225
|
+
script: document.currentScript,
|
|
226
|
+
window
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dataships/launcher",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dataships Launcher — the one storefront script. Reads the public key from its script tag, asks the Dataships API what to render and loads the matching module.",
|
|
5
|
+
"private": false,
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "Dataships",
|
|
8
|
+
"homepage": "https://dataships.io",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://gitlab.com/dataships/launcher.git"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"main": "./dist/launcher.js",
|
|
20
|
+
"unpkg": "./dist/launcher.js",
|
|
21
|
+
"jsdelivr": "./dist/launcher.js",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "rm -rf dist && tsup && node scripts/integrity.mjs",
|
|
24
|
+
"dev": "tsup --watch",
|
|
25
|
+
"serve": "python3 -m http.server 5180 --directory .",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
28
|
+
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
29
|
+
"test": "vitest",
|
|
30
|
+
"test:once": "vitest run",
|
|
31
|
+
"test:ci": "vitest run --coverage.enabled --reporter=junit --outputFile=./junit.xml",
|
|
32
|
+
"check:package": "sh scripts/check-package.sh",
|
|
33
|
+
"smoke:cdn": "node scripts/smoke-cdn.mjs"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^20.12.11",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
38
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
39
|
+
"@vitest/coverage-v8": "^3.2.6",
|
|
40
|
+
"eslint": "^8.57.0",
|
|
41
|
+
"jsdom": "^24.0.0",
|
|
42
|
+
"prettier": "^3.3.0",
|
|
43
|
+
"tsup": "^8.3.0",
|
|
44
|
+
"typescript": "^5.4.0",
|
|
45
|
+
"vitest": "^3.2.6"
|
|
46
|
+
},
|
|
47
|
+
"packageManager": "yarn@1.22.22",
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public",
|
|
50
|
+
"registry": "https://registry.npmjs.org/"
|
|
51
|
+
}
|
|
52
|
+
}
|