@shgysk8zer0/importmap 1.4.88 → 1.5.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/index.js CHANGED
@@ -1,10 +1,12 @@
1
- import { writeFile } from 'node:fs/promises';
1
+ import { readFile, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
2
3
  import '@shgysk8zer0/polyfills';
3
4
  import { readYAMLFile, writeYAMLFile } from '@shgysk8zer0/npm-utils/yaml.js';
4
5
  import { readJSONFile, writeJSONFile } from '@shgysk8zer0/npm-utils/json.js';
5
6
  import { getFileURL } from '@shgysk8zer0/npm-utils/path.js';
6
7
 
7
8
  const imports$1 = {
9
+ "@node/": "/node_modules/",
8
10
  "@shgysk8zer0/kazoo/": "https://unpkg.com/@shgysk8zer0/kazoo@1.0.10/",
9
11
  "@shgysk8zer0/konami": "https://unpkg.com/@shgysk8zer0/konami@1.1.1/konami.js",
10
12
  "@shgysk8zer0/polyfills": "https://unpkg.com/@shgysk8zer0/polyfills@0.5.1/browser.min.js",
@@ -43,8 +45,8 @@ const imports$1 = {
43
45
  "@aegisjsproject/aegis-md/": "https://unpkg.com/@aegisjsproject/aegis-md@0.0.4/",
44
46
  "@aegisjsproject/url": "https://unpkg.com/@aegisjsproject/url@1.0.3/url.mjs",
45
47
  "@aegisjsproject/url/": "https://unpkg.com/@aegisjsproject/url@1.0.3/",
46
- "@aegisjsproject/idb/": "https://unpkg.com/@aegisjsproject/idb@1.0.4/",
47
- "@aegisjsproject/idb": "https://unpkg.com/@aegisjsproject/idb@1.0.4/idb.min.js",
48
+ "@aegisjsproject/idb/": "https://unpkg.com/@aegisjsproject/idb@1.0.5/",
49
+ "@aegisjsproject/idb": "https://unpkg.com/@aegisjsproject/idb@1.0.5/idb.min.js",
48
50
  "@aegisjsproject/otp/": "https://unpkg.com/@aegisjsproject/otp@1.0.1/",
49
51
  "@aegisjsproject/otp": "https://unpkg.com/@aegisjsproject/otp@1.0.1/otp.min.js",
50
52
  "@aegisjsproject/md-editor": "https://unpkg.com/@aegisjsproject/md-editor@1.0.0/md-editor.js",
@@ -66,7 +68,7 @@ const imports$1 = {
66
68
  "@aegisjsproject/firebase-account-routes": "https://unpkg.com/@aegisjsproject/firebase-account-routes@0.0.5/main.js",
67
69
  "@aegisjsproject/firebase-account-routes/": "https://unpkg.com/@aegisjsproject/firebase-account-routes@0.0.5/",
68
70
  "@aegisjsproject/secret-store/": "https://unpkg.com/@aegisjsproject/secret-store@1.0.1/",
69
- "@kernvalley/components/": "https://unpkg.com/@kernvalley/components@2.0.8/",
71
+ "@kernvalley/components/": "https://unpkg.com/@kernvalley/components@2.0.9/",
70
72
  "@webcomponents/custom-elements": "https://unpkg.com/@webcomponents/custom-elements@1.6.0/custom-elements.min.js",
71
73
  leaflet: "https://unpkg.com/leaflet@1.9.4/dist/leaflet-src.esm.js",
72
74
  "leaflet/": "https://unpkg.com/leaflet@1.9.4/dist/",
@@ -75,10 +77,10 @@ const imports$1 = {
75
77
  "highlight.js/": "https://unpkg.com/@highlightjs/cdn-assets@11.11.1/es/",
76
78
  "@highlightjs/cdn-assets": "https://unpkg.com/@highlightjs/cdn-assets@11.11.1/es/core.min.js",
77
79
  "@highlightjs/cdn-assets/": "https://unpkg.com/@highlightjs/cdn-assets@11.11.1/es/",
78
- marked: "https://unpkg.com/marked@17.0.0/lib/marked.esm.js",
80
+ marked: "https://unpkg.com/marked@17.0.1/lib/marked.esm.js",
79
81
  "marked-highlight": "https://unpkg.com/marked-highlight@2.2.3/src/index.js",
80
- yaml: "https://unpkg.com/yaml@2.8.1/browser/dist/index.js",
81
- "yaml/": "https://unpkg.com/yaml@2.8.1/browser/dist/",
82
+ yaml: "https://unpkg.com/yaml@2.8.2/browser/dist/index.js",
83
+ "yaml/": "https://unpkg.com/yaml@2.8.2/browser/dist/",
82
84
  "firebase/": "https://www.gstatic.com/firebasejs/10.12.1/",
83
85
  "firebase/app": "https://www.gstatic.com/firebasejs/10.12.1/firebase-app.js",
84
86
  "firebase/app-check": "https://www.gstatic.com/firebasejs/10.12.1/firebase-app-check.js",
@@ -92,16 +94,16 @@ const imports$1 = {
92
94
  "firebase/storage": "https://www.gstatic.com/firebasejs/10.12.1/firebase-storage.js",
93
95
  "firebase/analytics": "https://www.gstatic.com/firebasejs/10.12.1/firebase-analytics.js"
94
96
  };
95
- const scope$1 = {
97
+ const scopes$1 = {
98
+ };
99
+ var json = {
100
+ imports: imports$1,
101
+ scopes: scopes$1
96
102
  };
97
103
 
98
- var importmap = /*#__PURE__*/Object.freeze({
99
- __proto__: null,
100
- imports: imports$1,
101
- scope: scope$1
102
- });
103
-
104
+ const SHA256 = 'SHA-256';
104
105
  const SHA384 = 'SHA-384';
106
+ const SHA512 = 'SHA-512';
105
107
  const DEFAULT_ALGO = SHA384;
106
108
 
107
109
  const BASE64 = 'base64';
@@ -138,6 +140,132 @@ async function hash(data, { algo = DEFAULT_ALGO, output = BUFFER } = {}) {
138
140
 
139
141
  const sri = async(data, { algo = SHA384 } = {}) => hash(data, { algo, output: SRI });
140
142
 
143
+ const { imports, scopes } = json;
144
+
145
+ class Importmap {
146
+ #imports = {};
147
+ #scopes = {};
148
+
149
+ constructor({ imports: i = imports, scopes: s = scopes } = {}) {
150
+ this.#imports = i;
151
+ this.#scopes = s;
152
+ }
153
+
154
+ get imports() {
155
+ return structuredClone(this.#imports);
156
+ }
157
+
158
+ get scopes() {
159
+ return structuredClone(this.#scopes);
160
+ }
161
+
162
+ delete(key) {
163
+ return Reflect.deleteProperty(this.#imports, key);
164
+ }
165
+
166
+ get(key) {
167
+ return Reflect.get(this.#imports, key);
168
+ }
169
+
170
+ has(key) {
171
+ return Reflect.has(this.#imports, key);
172
+ }
173
+
174
+ set(key, newValue) {
175
+ return Reflect.set(this.#imports, key, newValue);
176
+ }
177
+
178
+ toJSON() {
179
+ return { imports: this.#imports, scopes: this.#scopes };
180
+ }
181
+
182
+ toString() {
183
+ return JSON.stringify(this);
184
+ }
185
+
186
+ async importLocalPackage(name = 'package.json', { signal } = {}) {
187
+ const path = join(process.cwd(), name);
188
+ const pkg = await readFile(path, { encoding: 'utf8', signal });
189
+
190
+ return this.setLocalPackage(JSON.parse(pkg));
191
+ }
192
+
193
+ setLocalPackage({ name, module, exports = {} }) {
194
+ if (typeof name !== 'string') {
195
+ return false;
196
+ } else if (typeof exports === 'string' ) {
197
+ this.set(name, exports);
198
+ return true;
199
+ } else if (typeof exports === 'object') {
200
+ Object.entries(exports).forEach(([key, value]) => {
201
+ if (key.startsWith('.')) {
202
+ const importKey = key === '.' ? name : `${name}${key.substring(1)}`;
203
+ const resolved = typeof value === 'string' ? value : value.import ?? value.default;
204
+
205
+ if (typeof resolved === 'string' && resolved.startsWith('./')) {
206
+ if (importKey.includes('*') && resolved.includes('*')) {
207
+ const [prefix, suffix] = importKey.split('*');
208
+
209
+ if (resolved.endsWith('*' + suffix)) {
210
+ this.set(prefix, resolved.substring(1).replace('*' + suffix, ''));
211
+ }
212
+ } else {
213
+ this.set(importKey, resolved.substring(1));
214
+ }
215
+ }
216
+ }
217
+ });
218
+
219
+ return true;
220
+ } else if (typeof module === 'string') {
221
+ this.set(name, module);
222
+ return true;
223
+ } else {
224
+ this.set(name, '/');
225
+ return true;
226
+ }
227
+ }
228
+
229
+ async getScript({ algo = DEFAULT_ALGO, alphabet = BASE64, signal } = {}) {
230
+ const integrity = await this.getIntegrity({ algo, alphabet, signal });
231
+
232
+ return `<script type="importmap" integrity="${integrity}">${JSON.stringify(this)}</script>`;
233
+ }
234
+
235
+ async getIntegrity({ algo = DEFAULT_ALGO, alphabet = BASE64, signal } = {}) {
236
+ if (signal instanceof AbortSignal && signal.aborted) {
237
+ throw signal.reason;
238
+ } else {
239
+ const prefix = algo.toLowerCase().replace('-', '') + '-';
240
+ const encoded = new TextEncoder().encode(this);
241
+ const hash = await crypto.subtle.digest(algo, encoded);
242
+
243
+ return prefix + new Uint8Array(hash).toBase64({ alphabet });
244
+ }
245
+ }
246
+
247
+ static get SHA256() {
248
+ return SHA256;
249
+ }
250
+
251
+ static get SHA384() {
252
+ return SHA384;
253
+ }
254
+
255
+ static get SHA512() {
256
+ return SHA512;
257
+ }
258
+
259
+ static async importFromFile(path = 'importmap.json', { signal } = {}) {
260
+ const fullPath = join(process.cwd(), path);
261
+ const importmap = await readFile(fullPath, { encoding: 'utf8', signal });
262
+
263
+ return new Importmap(JSON.parse(importmap));
264
+ }
265
+ }
266
+
267
+ const importmap = new Importmap({ imports, scopes });
268
+
141
269
  const UNPKG = 'https://unpkg.com/';
142
270
 
143
271
  const cache = new Map();
@@ -263,18 +391,17 @@ var unpkg = /*#__PURE__*/Object.freeze({
263
391
  updateYAML: updateYAML
264
392
  });
265
393
 
266
- const { imports, scope } = importmap;
267
394
  const ENCODING = 'utf8';
268
395
 
269
- function mergeWithImportmap({ imports = {}, scope = {}}) {
396
+ function mergeWithImportmap({ imports = {}, scopes = {}}) {
270
397
  return {
271
- imports: { ...imports$1, ...imports },
272
- scope: { ...scope$1, ...scope },
398
+ imports: { ...importmap.imports, ...imports },
399
+ scopes: { ...importmap.scope, ...scopes },
273
400
  };
274
401
  }
275
402
 
276
403
  async function createImportmapJSON(path = 'importmap.json', {
277
- importmap = { imports, scope },
404
+ importmap = { imports, scopes },
278
405
  spaces = 2,
279
406
  signal,
280
407
  } = {}) {
@@ -282,18 +409,18 @@ async function createImportmapJSON(path = 'importmap.json', {
282
409
  }
283
410
 
284
411
  async function getImportmapIntegrity({
285
- importmap = { imports, scope },
412
+ importmap = { imports, scopes },
286
413
  algo = DEFAULT_ALGO,
287
414
  } = {}) {
288
415
  return await sri(JSON.stringify(importmap), { algo });
289
416
  }
290
417
 
291
418
  async function getImportmapScript({
292
- importmap = { imports, scope },
419
+ importmap = { imports, scopes },
293
420
  algo = DEFAULT_ALGO,
294
421
  } = {}) {
295
422
  const integrity = await getImportmapIntegrity({ importmap, algo });
296
423
  return `<script type="importmap" integrity="${integrity}">${JSON.stringify(importmap)}</script>`;
297
424
  }
298
425
 
299
- export { ENCODING, createImportmapJSON, getImportmapIntegrity, getImportmapScript, importmap, imports, mergeWithImportmap, scope, unpkg };
426
+ export { ENCODING, Importmap, createImportmapJSON, getImportmapIntegrity, getImportmapScript, importmap, imports, mergeWithImportmap, scopes, unpkg };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@shgysk8zer0/importmap",
3
- "version": "1.4.88",
3
+ "version": "1.5.1",
4
4
  "engines": {
5
- "node": ">=18.0.0"
5
+ "node": ">=20.10.0"
6
6
  },
7
7
  "description": "Front-End dependencies based on `<script type=\"importmap\">`",
8
8
  "type": "module",
@@ -14,6 +14,9 @@
14
14
  "import": "./index.js",
15
15
  "require": "./index.cjs"
16
16
  },
17
+ "./importmap": {
18
+ "import": "./importmap.js"
19
+ },
17
20
  "./*.js": {
18
21
  "import": "./*.js",
19
22
  "require": "./*.cjs"