@microscope-js/core 0.1.0 → 0.1.4
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/dist/index.cjs +23 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +24 -24
- package/dist/index.js.map +1 -1
- package/package.json +11 -2
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
var utils = require('@microscope-js/utils');
|
|
4
4
|
|
|
5
|
-
// src/
|
|
5
|
+
// src/index.ts
|
|
6
|
+
async function mount(opts) {
|
|
7
|
+
const { source, container, registry, options, signal, t, rendererId } = opts;
|
|
8
|
+
if (!container) {
|
|
9
|
+
throw new utils.MicroscopeError("mount() requires a container element", "INVALID_SOURCE");
|
|
10
|
+
}
|
|
11
|
+
if (signal?.aborted) {
|
|
12
|
+
throw new utils.MicroscopeError("aborted before start", "ABORTED");
|
|
13
|
+
}
|
|
14
|
+
const normalized = await utils.normalizeSource(source);
|
|
15
|
+
if (!normalized.mime) {
|
|
16
|
+
normalized.mime = await utils.sniffMime(normalized.blob);
|
|
17
|
+
}
|
|
18
|
+
const renderer = rendererId ? registry.get(rendererId) : await registry.match(normalized);
|
|
19
|
+
if (!renderer) {
|
|
20
|
+
throw new utils.MicroscopeError(
|
|
21
|
+
`No renderer registered for source (mime=${normalized.mime ?? "unknown"}, name=${normalized.name ?? "unknown"})`,
|
|
22
|
+
"UNSUPPORTED"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
utils.clearContainer(container);
|
|
26
|
+
return renderer.render({ source: normalized, container, options, signal, t });
|
|
27
|
+
}
|
|
6
28
|
function createRegistry(renderers) {
|
|
7
29
|
const entries = renderers.map(
|
|
8
30
|
(r, i) => "renderer" in r ? r : { renderer: r, priority: i }
|
|
@@ -49,28 +71,6 @@ function composeRegistries(...regs) {
|
|
|
49
71
|
for (const r of regs) merged.push(...r.entries);
|
|
50
72
|
return createRegistry(merged);
|
|
51
73
|
}
|
|
52
|
-
async function mount(opts) {
|
|
53
|
-
const { source, container, registry, options, signal, t, rendererId } = opts;
|
|
54
|
-
if (!container) {
|
|
55
|
-
throw new utils.MicroscopeError("mount() requires a container element", "INVALID_SOURCE");
|
|
56
|
-
}
|
|
57
|
-
if (signal?.aborted) {
|
|
58
|
-
throw new utils.MicroscopeError("aborted before start", "ABORTED");
|
|
59
|
-
}
|
|
60
|
-
const normalized = await utils.normalizeSource(source);
|
|
61
|
-
if (!normalized.mime) {
|
|
62
|
-
normalized.mime = await utils.sniffMime(normalized.blob);
|
|
63
|
-
}
|
|
64
|
-
const renderer = rendererId ? registry.get(rendererId) : await registry.match(normalized);
|
|
65
|
-
if (!renderer) {
|
|
66
|
-
throw new utils.MicroscopeError(
|
|
67
|
-
`No renderer registered for source (mime=${normalized.mime ?? "unknown"}, name=${normalized.name ?? "unknown"})`,
|
|
68
|
-
"UNSUPPORTED"
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
utils.clearContainer(container);
|
|
72
|
-
return renderer.render({ source: normalized, container, options, signal, t });
|
|
73
|
-
}
|
|
74
74
|
|
|
75
75
|
Object.defineProperty(exports, "MicroscopeError", {
|
|
76
76
|
enumerable: true,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/mount.ts","../src/registry.ts"],"names":["MicroscopeError","normalizeSource","sniffMime","clearContainer","extOf","mimeMatches"],"mappings":";;;;;AASA,eAAsB,MAAM,IAAA,EAA2C;AACrE,EAAA,MAAM,EAAE,QAAQ,SAAA,EAAW,QAAA,EAAU,SAAS,MAAA,EAAQ,CAAA,EAAG,YAAW,GAAI,IAAA;AAExE,EAAA,IAAI,CAAC,SAAA,EAAW;AACd,IAAA,MAAM,IAAIA,qBAAA,CAAgB,sCAAA,EAAwC,gBAAgB,CAAA;AA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,CAAE,UAAA,CAAW,QAAA,CAAS,GAAG,GAAG,OAAO,IAAA;AAC9C,EAAA,OAAO,KAAA;AACT;AAMO,SAAS,qBAAqB,IAAA,EAAyC;AAC5E,EAAA,MAAM,SAA0B,EAAC;AACjC,EAAA,KAAA,MAAW,KAAK,IAAA,EAAM,MAAA,CAAO,IAAA,CAAK,GAAG,EAAE,OAAO,CAAA;AAC9C,EAAA,OAAO,eAAe,MAAM,CAAA;AAC9B","file":"index.cjs","sourcesContent":["import { clearContainer, MicroscopeError, normalizeSource, sniffMime } from '@microscope-js/utils';\nimport type { MountOptions, RenderHandle } from './types.js';\n\n/**\n * The single entry point most callers will use. Normalizes the source,\n * sniffs MIME if unknown, picks a renderer from the registry, and renders.\n *\n * Returns a handle whose `destroy()` MUST be called by the caller.\n */\nexport async function mount(opts: MountOptions): Promise<RenderHandle> {\n const { source, container, registry, options, signal, t, rendererId } = opts;\n\n if (!container) {\n throw new MicroscopeError('mount() requires a container element', 'INVALID_SOURCE');\n }\n if (signal?.aborted) {\n throw new MicroscopeError('aborted before start', 'ABORTED');\n }\n\n const normalized = await normalizeSource(source);\n if (!normalized.mime) {\n normalized.mime = await sniffMime(normalized.blob);\n }\n\n const renderer = rendererId ? registry.get(rendererId) : await registry.match(normalized);\n\n if (!renderer) {\n throw new MicroscopeError(\n `No renderer registered for source (mime=${normalized.mime ?? 'unknown'}, name=${normalized.name ?? 'unknown'})`,\n 'UNSUPPORTED',\n );\n }\n\n clearContainer(container);\n return renderer.render({ source: normalized, container, options, signal, t });\n}\n","import { extOf, mimeMatches } from '@microscope-js/utils';\nimport type { Registry, RegistryEntry, Renderer } from './types.js';\n\n/**\n * Build a registry from a list of renderers. Later renderers in the list win\n * unless an explicit priority is given via `{ renderer, priority }`.\n *\n * The registry is intentionally tiny — picking a renderer is just:\n * 1. Run `canRender` overrides first (custom byte-sniffing wins).\n * 2. Otherwise match by MIME, then by extension, then by sniffed MIME.\n * 3. Highest priority wins.\n */\nexport function createRegistry(renderers: ReadonlyArray<Renderer | RegistryEntry>): Registry {\n const entries: RegistryEntry[] = renderers.map((r, i) =>\n 'renderer' in r ? r : { renderer: r, priority: i },\n );\n\n const byId = new Map<string, Renderer>();\n for (const { renderer } of entries) {\n byId.set(renderer.id, renderer);\n }\n\n return {\n entries,\n get(id) {\n return byId.get(id) ?? null;\n },\n async match(source) {\n const ext = extOf(source.name);\n const mime = source.mime;\n\n // First pass — let any renderer with a custom `canRender` claim the source.\n // This is how byte-sniffing renderers (ZIP-based formats) override MIME.\n const candidates: RegistryEntry[] = [];\n for (const entry of entries) {\n const r = entry.renderer;\n if (r.canRender) {\n const ok = await r.canRender(source);\n if (ok) candidates.push(entry);\n continue;\n }\n if (claimsByMeta(r, mime, ext)) candidates.push(entry);\n }\n\n if (candidates.length === 0) return null;\n candidates.sort((a, b) => b.priority - a.priority);\n return candidates[0]?.renderer ?? null;\n },\n };\n}\n\nfunction claimsByMeta(r: Renderer, mime: string | null, ext: string | null): boolean {\n if (mime) {\n for (const m of r.mimes) {\n if (mimeMatches(mime, m)) return true;\n }\n }\n if (ext && r.extensions.includes(ext)) return true;\n return false;\n}\n\n/**\n * Compose two registries — useful when an app wants its own custom renderers\n * layered on top of the default ones without losing tree-shakability.\n */\nexport function composeRegistries(...regs: ReadonlyArray<Registry>): Registry {\n const merged: RegistryEntry[] = [];\n for (const r of regs) merged.push(...r.entries);\n return createRegistry(merged);\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -69,6 +69,14 @@ interface Registry {
|
|
|
69
69
|
get(id: string): Renderer | null;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* The single entry point most callers will use. Normalizes the source,
|
|
74
|
+
* sniffs MIME if unknown, picks a renderer from the registry, and renders.
|
|
75
|
+
*
|
|
76
|
+
* Returns a handle whose `destroy()` MUST be called by the caller.
|
|
77
|
+
*/
|
|
78
|
+
declare function mount(opts: MountOptions): Promise<RenderHandle>;
|
|
79
|
+
|
|
72
80
|
/**
|
|
73
81
|
* Build a registry from a list of renderers. Later renderers in the list win
|
|
74
82
|
* unless an explicit priority is given via `{ renderer, priority }`.
|
|
@@ -85,12 +93,4 @@ declare function createRegistry(renderers: ReadonlyArray<Renderer | RegistryEntr
|
|
|
85
93
|
*/
|
|
86
94
|
declare function composeRegistries(...regs: ReadonlyArray<Registry>): Registry;
|
|
87
95
|
|
|
88
|
-
/**
|
|
89
|
-
* The single entry point most callers will use. Normalizes the source,
|
|
90
|
-
* sniffs MIME if unknown, picks a renderer from the registry, and renders.
|
|
91
|
-
*
|
|
92
|
-
* Returns a handle whose `destroy()` MUST be called by the caller.
|
|
93
|
-
*/
|
|
94
|
-
declare function mount(opts: MountOptions): Promise<RenderHandle>;
|
|
95
|
-
|
|
96
96
|
export { type MountOptions, type Registry, type RegistryEntry, type RenderContext, type RenderHandle, type Renderer, type RendererMeta, composeRegistries, createRegistry, mount };
|
package/dist/index.d.ts
CHANGED
|
@@ -69,6 +69,14 @@ interface Registry {
|
|
|
69
69
|
get(id: string): Renderer | null;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* The single entry point most callers will use. Normalizes the source,
|
|
74
|
+
* sniffs MIME if unknown, picks a renderer from the registry, and renders.
|
|
75
|
+
*
|
|
76
|
+
* Returns a handle whose `destroy()` MUST be called by the caller.
|
|
77
|
+
*/
|
|
78
|
+
declare function mount(opts: MountOptions): Promise<RenderHandle>;
|
|
79
|
+
|
|
72
80
|
/**
|
|
73
81
|
* Build a registry from a list of renderers. Later renderers in the list win
|
|
74
82
|
* unless an explicit priority is given via `{ renderer, priority }`.
|
|
@@ -85,12 +93,4 @@ declare function createRegistry(renderers: ReadonlyArray<Renderer | RegistryEntr
|
|
|
85
93
|
*/
|
|
86
94
|
declare function composeRegistries(...regs: ReadonlyArray<Registry>): Registry;
|
|
87
95
|
|
|
88
|
-
/**
|
|
89
|
-
* The single entry point most callers will use. Normalizes the source,
|
|
90
|
-
* sniffs MIME if unknown, picks a renderer from the registry, and renders.
|
|
91
|
-
*
|
|
92
|
-
* Returns a handle whose `destroy()` MUST be called by the caller.
|
|
93
|
-
*/
|
|
94
|
-
declare function mount(opts: MountOptions): Promise<RenderHandle>;
|
|
95
|
-
|
|
96
96
|
export { type MountOptions, type Registry, type RegistryEntry, type RenderContext, type RenderHandle, type Renderer, type RendererMeta, composeRegistries, createRegistry, mount };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MicroscopeError, normalizeSource, sniffMime, clearContainer, extOf, mimeMatches } from '@microscope-js/utils';
|
|
2
2
|
export { MicroscopeError, extOf, normalizeSource, sniffMime } from '@microscope-js/utils';
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/index.ts
|
|
5
|
+
async function mount(opts) {
|
|
6
|
+
const { source, container, registry, options, signal, t, rendererId } = opts;
|
|
7
|
+
if (!container) {
|
|
8
|
+
throw new MicroscopeError("mount() requires a container element", "INVALID_SOURCE");
|
|
9
|
+
}
|
|
10
|
+
if (signal?.aborted) {
|
|
11
|
+
throw new MicroscopeError("aborted before start", "ABORTED");
|
|
12
|
+
}
|
|
13
|
+
const normalized = await normalizeSource(source);
|
|
14
|
+
if (!normalized.mime) {
|
|
15
|
+
normalized.mime = await sniffMime(normalized.blob);
|
|
16
|
+
}
|
|
17
|
+
const renderer = rendererId ? registry.get(rendererId) : await registry.match(normalized);
|
|
18
|
+
if (!renderer) {
|
|
19
|
+
throw new MicroscopeError(
|
|
20
|
+
`No renderer registered for source (mime=${normalized.mime ?? "unknown"}, name=${normalized.name ?? "unknown"})`,
|
|
21
|
+
"UNSUPPORTED"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
clearContainer(container);
|
|
25
|
+
return renderer.render({ source: normalized, container, options, signal, t });
|
|
26
|
+
}
|
|
5
27
|
function createRegistry(renderers) {
|
|
6
28
|
const entries = renderers.map(
|
|
7
29
|
(r, i) => "renderer" in r ? r : { renderer: r, priority: i }
|
|
@@ -48,28 +70,6 @@ function composeRegistries(...regs) {
|
|
|
48
70
|
for (const r of regs) merged.push(...r.entries);
|
|
49
71
|
return createRegistry(merged);
|
|
50
72
|
}
|
|
51
|
-
async function mount(opts) {
|
|
52
|
-
const { source, container, registry, options, signal, t, rendererId } = opts;
|
|
53
|
-
if (!container) {
|
|
54
|
-
throw new MicroscopeError("mount() requires a container element", "INVALID_SOURCE");
|
|
55
|
-
}
|
|
56
|
-
if (signal?.aborted) {
|
|
57
|
-
throw new MicroscopeError("aborted before start", "ABORTED");
|
|
58
|
-
}
|
|
59
|
-
const normalized = await normalizeSource(source);
|
|
60
|
-
if (!normalized.mime) {
|
|
61
|
-
normalized.mime = await sniffMime(normalized.blob);
|
|
62
|
-
}
|
|
63
|
-
const renderer = rendererId ? registry.get(rendererId) : await registry.match(normalized);
|
|
64
|
-
if (!renderer) {
|
|
65
|
-
throw new MicroscopeError(
|
|
66
|
-
`No renderer registered for source (mime=${normalized.mime ?? "unknown"}, name=${normalized.name ?? "unknown"})`,
|
|
67
|
-
"UNSUPPORTED"
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
clearContainer(container);
|
|
71
|
-
return renderer.render({ source: normalized, container, options, signal, t });
|
|
72
|
-
}
|
|
73
73
|
|
|
74
74
|
export { composeRegistries, createRegistry, mount };
|
|
75
75
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/mount.ts","../src/registry.ts"],"names":[],"mappings":";;;;AASA,eAAsB,MAAM,IAAA,EAA2C;AACrE,EAAA,MAAM,EAAE,QAAQ,SAAA,EAAW,QAAA,EAAU,SAAS,MAAA,EAAQ,CAAA,EAAG,YAAW,GAAI,IAAA;AAExE,EAAA,IAAI,CAAC,SAAA,EAAW;AACd,IAAA,MAAM,IAAI,eAAA,CAAgB,sCAAA,EAAwC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AAA,CAAE,UAAA,CAAW,QAAA,CAAS,GAAG,GAAG,OAAO,IAAA;AAC9C,EAAA,OAAO,KAAA;AACT;AAMO,SAAS,qBAAqB,IAAA,EAAyC;AAC5E,EAAA,MAAM,SAA0B,EAAC;AACjC,EAAA,KAAA,MAAW,KAAK,IAAA,EAAM,MAAA,CAAO,IAAA,CAAK,GAAG,EAAE,OAAO,CAAA;AAC9C,EAAA,OAAO,eAAe,MAAM,CAAA;AAC9B","file":"index.js","sourcesContent":["import { clearContainer, MicroscopeError, normalizeSource, sniffMime } from '@microscope-js/utils';\nimport type { MountOptions, RenderHandle } from './types.js';\n\n/**\n * The single entry point most callers will use. Normalizes the source,\n * sniffs MIME if unknown, picks a renderer from the registry, and renders.\n *\n * Returns a handle whose `destroy()` MUST be called by the caller.\n */\nexport async function mount(opts: MountOptions): Promise<RenderHandle> {\n const { source, container, registry, options, signal, t, rendererId } = opts;\n\n if (!container) {\n throw new MicroscopeError('mount() requires a container element', 'INVALID_SOURCE');\n }\n if (signal?.aborted) {\n throw new MicroscopeError('aborted before start', 'ABORTED');\n }\n\n const normalized = await normalizeSource(source);\n if (!normalized.mime) {\n normalized.mime = await sniffMime(normalized.blob);\n }\n\n const renderer = rendererId ? registry.get(rendererId) : await registry.match(normalized);\n\n if (!renderer) {\n throw new MicroscopeError(\n `No renderer registered for source (mime=${normalized.mime ?? 'unknown'}, name=${normalized.name ?? 'unknown'})`,\n 'UNSUPPORTED',\n );\n }\n\n clearContainer(container);\n return renderer.render({ source: normalized, container, options, signal, t });\n}\n","import { extOf, mimeMatches } from '@microscope-js/utils';\nimport type { Registry, RegistryEntry, Renderer } from './types.js';\n\n/**\n * Build a registry from a list of renderers. Later renderers in the list win\n * unless an explicit priority is given via `{ renderer, priority }`.\n *\n * The registry is intentionally tiny — picking a renderer is just:\n * 1. Run `canRender` overrides first (custom byte-sniffing wins).\n * 2. Otherwise match by MIME, then by extension, then by sniffed MIME.\n * 3. Highest priority wins.\n */\nexport function createRegistry(renderers: ReadonlyArray<Renderer | RegistryEntry>): Registry {\n const entries: RegistryEntry[] = renderers.map((r, i) =>\n 'renderer' in r ? r : { renderer: r, priority: i },\n );\n\n const byId = new Map<string, Renderer>();\n for (const { renderer } of entries) {\n byId.set(renderer.id, renderer);\n }\n\n return {\n entries,\n get(id) {\n return byId.get(id) ?? null;\n },\n async match(source) {\n const ext = extOf(source.name);\n const mime = source.mime;\n\n // First pass — let any renderer with a custom `canRender` claim the source.\n // This is how byte-sniffing renderers (ZIP-based formats) override MIME.\n const candidates: RegistryEntry[] = [];\n for (const entry of entries) {\n const r = entry.renderer;\n if (r.canRender) {\n const ok = await r.canRender(source);\n if (ok) candidates.push(entry);\n continue;\n }\n if (claimsByMeta(r, mime, ext)) candidates.push(entry);\n }\n\n if (candidates.length === 0) return null;\n candidates.sort((a, b) => b.priority - a.priority);\n return candidates[0]?.renderer ?? null;\n },\n };\n}\n\nfunction claimsByMeta(r: Renderer, mime: string | null, ext: string | null): boolean {\n if (mime) {\n for (const m of r.mimes) {\n if (mimeMatches(mime, m)) return true;\n }\n }\n if (ext && r.extensions.includes(ext)) return true;\n return false;\n}\n\n/**\n * Compose two registries — useful when an app wants its own custom renderers\n * layered on top of the default ones without losing tree-shakability.\n */\nexport function composeRegistries(...regs: ReadonlyArray<Registry>): Registry {\n const merged: RegistryEntry[] = [];\n for (const r of regs) merged.push(...r.entries);\n return createRegistry(merged);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microscope-js/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Framework-agnostic core for microscope-js: Renderer interface + registry + mount() entry point",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"README.md"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@microscope-js/utils": "0.1.
|
|
23
|
+
"@microscope-js/utils": "0.1.4"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"tsup": "^8.3.5",
|
|
@@ -30,6 +30,15 @@
|
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/shubham8550/microscope-js.git",
|
|
36
|
+
"directory": "packages/core"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/shubham8550/microscope-js/tree/main/packages/core#readme",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/shubham8550/microscope-js/issues"
|
|
41
|
+
},
|
|
33
42
|
"scripts": {
|
|
34
43
|
"build": "tsup",
|
|
35
44
|
"dev": "tsup --watch",
|