@drsamonline/kits 0.0.0-stage → 0.12.3

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.
Files changed (3) hide show
  1. package/package.json +23 -4
  2. package/src/index.js +136 -0
  3. package/README.md +0 -3
package/package.json CHANGED
@@ -1,6 +1,25 @@
1
1
  {
2
2
  "name": "@drsamonline/kits",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.12.3",
4
+ "description": "Vendored drsamonline kits (crypto, ml, search, tokens, compress) loaded in an isolated VM context.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "main": "./src/index.js",
8
+ "exports": {
9
+ ".": "./src/index.js",
10
+ "./src/*": "./src/*"
11
+ },
12
+ "files": [
13
+ "src"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git://github.com/drsamonline/sui.git",
18
+ "directory": "packages/kits"
19
+ },
20
+ "homepage": "https://github.com/drsamonline/sui#readme",
21
+ "publishConfig": {
22
+ "access": "public",
23
+ "registry": "https://registry.npmjs.org/"
24
+ }
25
+ }
package/src/index.js ADDED
@@ -0,0 +1,136 @@
1
+ /**
2
+ * @drsamonline/kits — the vendored drsamonline kits, loaded safely.
3
+ *
4
+ * Each kit is a single-file, dependency-free IIFE that attaches itself to a
5
+ * global (`CryptoKit`, `MLKit`, `Search`, `Tokens`, `Compress`, `Export`,
6
+ * `Loom`). They were written for the browser, so they are evaluated in an
7
+ * isolated `node:vm` context rather than imported: no global pollution, no
8
+ * ESM/CJS mismatch, and a broken kit cannot take the gateway down with it.
9
+ *
10
+ * Licence: each kit carries its own LICENSE file in vendor/kits/. The owner of
11
+ * those repositories has authorised their use in this MIT project; see
12
+ * docs/legal/LICENSE_INVENTORY.md for the authorisation and the outstanding
13
+ * upstream licence-file update.
14
+ */
15
+
16
+ import { existsSync, readFileSync } from 'node:fs';
17
+ import { dirname, join } from 'node:path';
18
+ import { fileURLToPath } from 'node:url';
19
+ import vm from 'node:vm';
20
+
21
+ const HERE = dirname(fileURLToPath(import.meta.url));
22
+ const KITS_DIR = join(HERE, '..', '..', '..', 'vendor', 'kits');
23
+
24
+ /** file name -> the global the kit attaches to */
25
+ export const KIT_GLOBALS = {
26
+ 'crypto-kit': 'CryptoKit',
27
+ 'ml-kit': 'MLKit',
28
+ 'search-kit': 'Search',
29
+ 'tokens-kit': 'Tokens',
30
+ 'compress-kit': 'Compress',
31
+ 'export-kit': 'Export',
32
+ 'web-kit': 'Loom',
33
+ };
34
+
35
+ /** @type {Map<string, any>} */
36
+ const cache = new Map();
37
+
38
+ /**
39
+ * Load a kit by short name (`crypto-kit`, `ml-kit`, …).
40
+ * Throws with a plain message if the file is missing, so a trimmed checkout says
41
+ * what is missing instead of failing later inside a feature.
42
+ * @param {string} name
43
+ * @returns {any}
44
+ */
45
+ export function loadKit(name) {
46
+ if (cache.has(name)) return cache.get(name);
47
+
48
+ const globalName = KIT_GLOBALS[name];
49
+ if (!globalName) {
50
+ throw new Error(`Unknown kit "${name}". Known kits: ${Object.keys(KIT_GLOBALS).join(', ')}.`);
51
+ }
52
+
53
+ const file = join(KITS_DIR, `${name}.js`);
54
+ if (!existsSync(file)) {
55
+ throw new Error(`The ${name} file is missing from vendor/kits. Re-run \`npm run kits\` or restore vendor/kits/${name}.js.`);
56
+ }
57
+
58
+ const code = readFileSync(file, 'utf8');
59
+ const sandbox = {
60
+ console,
61
+ Math,
62
+ Date,
63
+ JSON,
64
+ Number,
65
+ String,
66
+ Object,
67
+ Array,
68
+ RegExp,
69
+ Error,
70
+ Map,
71
+ Set,
72
+ Promise,
73
+ Uint8Array,
74
+ ArrayBuffer,
75
+ DataView,
76
+ TextEncoder,
77
+ TextDecoder,
78
+ // compress-kit is built on Compression Streams; Node 18+ has them globally,
79
+ // but the sandbox has to be told.
80
+ CompressionStream: globalThis.CompressionStream,
81
+ DecompressionStream: globalThis.DecompressionStream,
82
+ ReadableStream: globalThis.ReadableStream,
83
+ WritableStream: globalThis.WritableStream,
84
+ Blob: globalThis.Blob,
85
+ Response: globalThis.Response,
86
+ Request: globalThis.Request,
87
+ Headers: globalThis.Headers,
88
+ FormData: globalThis.FormData,
89
+ btoa: globalThis.btoa ?? ((s) => Buffer.from(s, 'binary').toString('base64')),
90
+ atob: globalThis.atob ?? ((s) => Buffer.from(s, 'base64').toString('binary')),
91
+ Buffer,
92
+ crypto: globalThis.crypto,
93
+ setTimeout,
94
+ clearTimeout,
95
+ structuredClone,
96
+ URL,
97
+ localStorage: undefined,
98
+ document: undefined,
99
+ navigator: undefined,
100
+ };
101
+ // The kits pick `globalThis` (or `this`) as their root; point both at the sandbox.
102
+ sandbox.globalThis = sandbox;
103
+ sandbox.window = sandbox;
104
+ sandbox.self = sandbox;
105
+
106
+ const context = vm.createContext(sandbox);
107
+ try {
108
+ vm.runInContext(code, context, { filename: `${name}.js`, timeout: 5000 });
109
+ } catch (err) {
110
+ throw new Error(`The ${name} kit failed to load: ${/** @type {any} */ (err).message}`);
111
+ }
112
+
113
+ const kit = sandbox[globalName];
114
+ if (!kit) {
115
+ throw new Error(`The ${name} kit loaded but did not define ${globalName}. The vendored file may be truncated.`);
116
+ }
117
+ cache.set(name, kit);
118
+ return kit;
119
+ }
120
+
121
+ /** Which kits are present, and at what version. Used by `sui doctor`. */
122
+ export function kitStatus() {
123
+ return Object.keys(KIT_GLOBALS).map((name) => {
124
+ try {
125
+ const kit = loadKit(name);
126
+ return { name, loaded: true, version: kit.version ?? null, error: null };
127
+ } catch (err) {
128
+ return { name, loaded: false, version: null, error: /** @type {any} */ (err).message };
129
+ }
130
+ });
131
+ }
132
+
133
+ /** The catalogue source data that ships with the fleet's app-endpoints repo. */
134
+ export function appEndpointsPath() {
135
+ return join(KITS_DIR, 'app-endpoints.apis.json');
136
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.