cozy-iiif 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.
@@ -0,0 +1,2 @@
1
+ export * from './crop-region';
2
+ export * from './get-thumbnail';
@@ -0,0 +1,85 @@
1
+ import pThrottle from 'p-throttle';
2
+
3
+ interface ThrottleConfig {
4
+
5
+ limit: number;
6
+
7
+ interval: number;
8
+
9
+ }
10
+
11
+ interface ThrottledLoaderOpts {
12
+
13
+ callsPerSecond?: number;
14
+
15
+ }
16
+
17
+ const createThrottleManager = () => {
18
+
19
+ // Default config: 5 calls/second
20
+ let config: ThrottleConfig = {
21
+ limit: 5,
22
+ interval: 1000
23
+ };
24
+
25
+ let instance = pThrottle(config);
26
+
27
+ const getInstance = () => instance;
28
+
29
+ const setConfig = (updated: Partial<ThrottleConfig>) => {
30
+ config = {...config, ...updated };
31
+ instance = pThrottle(config);
32
+ }
33
+
34
+ const getConfig = () => ({ ...config });
35
+
36
+ return {
37
+ getInstance,
38
+ getConfig,
39
+ setConfig
40
+ };
41
+
42
+ }
43
+
44
+ // Singleton instance
45
+ const throttleManager = createThrottleManager();
46
+
47
+ const updateThrottleConfig = (opts?: Partial<ThrottleConfig>) => {
48
+ if (!opts) return;
49
+
50
+ const current = throttleManager.getConfig();
51
+
52
+ if (
53
+ (opts.limit && opts.limit !== current.limit) ||
54
+ (opts.interval && opts.interval !== current.interval)
55
+ ) {
56
+ console.log('updating throttle config!');
57
+ throttleManager.setConfig({ ...current, ...opts });
58
+ }
59
+ }
60
+
61
+ export const getThrottledLoader = (
62
+ opts?: ThrottledLoaderOpts
63
+ ) => {
64
+ if (opts) {
65
+ const throttleOpts = {
66
+ limit: opts.callsPerSecond || 5,
67
+ interval: 1000
68
+ }
69
+
70
+ updateThrottleConfig(throttleOpts);
71
+ }
72
+
73
+ const throttle = throttleManager.getInstance();
74
+
75
+ const loadImage = throttle((url: string) => new Promise<HTMLImageElement>((resolve, reject) => {
76
+ const img = new Image();
77
+ img.crossOrigin = 'anonymous';
78
+ img.onload = () => resolve(img);
79
+ img.onerror = reject;
80
+ img.src = url;
81
+ }));
82
+
83
+ return { loadImage };
84
+
85
+ }
@@ -0,0 +1,37 @@
1
+ export interface ImageInfo {
2
+
3
+ id: string;
4
+
5
+ width: number;
6
+
7
+ height: number;
8
+
9
+ tiles: TileInfo[];
10
+
11
+ }
12
+
13
+ export type Size = { width: number; height: number; };
14
+
15
+ export interface TileInfo {
16
+
17
+ width: number;
18
+
19
+ height?: number;
20
+
21
+ scaleFactors: number[];
22
+
23
+ }
24
+
25
+ export interface Tile {
26
+
27
+ x: number;
28
+
29
+ y: number;
30
+
31
+ width: number;
32
+
33
+ height: number;
34
+
35
+ url: string;
36
+
37
+ }
package/src/types.ts ADDED
@@ -0,0 +1,137 @@
1
+ import type { Manifest, Canvas, ImageService2, ImageService3, IIIFExternalWebResource } from '@iiif/presentation-3';
2
+
3
+ export type CozyParseResult =
4
+ | { type: 'manifest'; url: string, resource: CozyManifest }
5
+ | { type: 'iiif-image'; url: string, resource: CozyImageResource }
6
+ | { type: 'plain-image'; url: string }
7
+ | { type: 'webpage'; url: string }
8
+ | {
9
+ type: 'error';
10
+ code: 'INVALID_URL' | 'INVALID_HTTP_RESPONSE' | 'FETCH_ERROR' | 'INVALID_MANIFEST' | 'UNSUPPORTED_FORMAT';
11
+ message: string;
12
+ };
13
+
14
+ export interface CozyManifest {
15
+
16
+ readonly majorVersion: number;
17
+
18
+ readonly source: Manifest;
19
+
20
+ readonly id: string;
21
+
22
+ readonly canvases: CozyCanvas[];
23
+
24
+ getLabel(locale?: string): string;
25
+
26
+ getMetadata(locale?: string): CozyMetadata[];
27
+
28
+ }
29
+
30
+ export interface CozyCanvas {
31
+
32
+ readonly source: Canvas;
33
+
34
+ readonly id: string;
35
+
36
+ readonly width: number;
37
+
38
+ readonly height: number;
39
+
40
+ readonly images: CozyImageResource[];
41
+
42
+ getLabel(locale?: string): string;
43
+
44
+ getMetadata(locale?: string): CozyMetadata[];
45
+
46
+ getThumbnailURL(minSize?: number): string;
47
+
48
+ }
49
+
50
+ export interface CozyMetadata {
51
+
52
+ label: string;
53
+
54
+ value: string;
55
+
56
+ }
57
+
58
+ export type CozyImageResource =
59
+ | StaticImageResource
60
+ | ImageServiceResource;
61
+
62
+ export type ImageServiceResource =
63
+ | DynamicImageServiceResource
64
+ | Level0ImageServiceResource;
65
+
66
+ interface BaseImageResource {
67
+
68
+ readonly source: IIIFExternalWebResource;
69
+
70
+ type: 'static' | 'dynamic' | 'level0';
71
+
72
+ width: number;
73
+
74
+ height: number;
75
+
76
+ }
77
+
78
+ export interface StaticImageResource extends BaseImageResource {
79
+
80
+ type: 'static';
81
+
82
+ url: string;
83
+
84
+ }
85
+
86
+ export interface DynamicImageServiceResource extends BaseImageResource {
87
+
88
+ type: 'dynamic';
89
+
90
+ service: ImageService2 | ImageService3;
91
+
92
+ serviceUrl: string;
93
+
94
+ majorVersion: number;
95
+
96
+ getRegionURL(bounds: Bounds, minSize?: number): string;
97
+
98
+ }
99
+
100
+ export interface Level0ImageServiceResource extends BaseImageResource {
101
+
102
+ type: 'level0';
103
+
104
+ majorVersion: number;
105
+
106
+ service: ImageService2 | ImageService3;
107
+
108
+ serviceUrl: string;
109
+
110
+ }
111
+
112
+
113
+ export interface ImageRequestOptions {
114
+
115
+ width?: number;
116
+
117
+ height?: number;
118
+
119
+ region?: 'full' | 'square' | { x: number; y: number; width: number; height: number };
120
+
121
+ quality?: 'default' | 'color' | 'gray' | 'bitonal';
122
+
123
+ format?: 'jpg' | 'png' | 'gif' | 'webp';
124
+
125
+ }
126
+
127
+ export interface Bounds {
128
+
129
+ x: number;
130
+
131
+ y: number;
132
+
133
+ w: number;
134
+
135
+ h: number;
136
+
137
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowSyntheticDefaultImports": true,
4
+ "baseUrl": ".",
5
+ "isolatedModules": true,
6
+ "lib": ["ES2021", "DOM", "DOM.Iterable"],
7
+ "module": "ESNext",
8
+ "moduleResolution": "Bundler",
9
+ "noEmit": true,
10
+ "noFallthroughCasesInSwitch": true,
11
+ "noUnusedLocals": false,
12
+ "noUnusedParameters": false,
13
+ "resolveJsonModule": true,
14
+ "skipLibCheck": true,
15
+ "strict": false,
16
+ "target": "ES6",
17
+ "useDefineForClassFields": true
18
+ },
19
+ "include": ["src"]
20
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from 'vite';
2
+ import { resolve } from 'path';
3
+ import dts from 'vite-plugin-dts';
4
+
5
+ export default defineConfig({
6
+ plugins: [dts()],
7
+ build: {
8
+ lib: {
9
+ entry: {
10
+ 'index': resolve(__dirname, 'src/index.ts'),
11
+ 'level-0/index': resolve(__dirname, 'src/level-0/index.ts'),
12
+ },
13
+ formats: ['es']
14
+ },
15
+ rollupOptions: {
16
+ output: {
17
+ preserveModules: true
18
+ }
19
+ }
20
+ }
21
+ });