@doki-land/live2d-loader 0.0.23 → 0.0.24

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.d.ts CHANGED
@@ -1,23 +1,27 @@
1
1
  import { ModelFormat, ModelSettings, AssetKey, AssetResolver } from '@doki-land/live2d-core';
2
2
 
3
3
  /**
4
- * Fetch helpers with optional byte-level progress.
4
+ * Fetch helpers with optional byte-level progress and AbortSignal.
5
5
  */
6
6
  type FetchProgressHandler = (update: {
7
7
  bytesLoaded: number;
8
8
  bytesTotal: number | null;
9
9
  }) => void;
10
+ type FetchInit = {
11
+ signal?: AbortSignal;
12
+ };
10
13
  /**
11
14
  * Fetch bytes with progress. Uses `arrayBuffer()` when streaming is unavailable
12
15
  * or when Content-Length is known (avoids stuck ReadableStream readers in some
13
16
  * embedded Chromium hosts during rapid remounts).
14
17
  */
15
- declare function fetchArrayBufferWithProgress(url: string, onProgress?: FetchProgressHandler): Promise<ArrayBuffer>;
16
- declare function fetchJsonWithProgress(url: string, onProgress?: FetchProgressHandler): Promise<unknown>;
18
+ declare function fetchArrayBufferWithProgress(url: string, onProgress?: FetchProgressHandler, init?: FetchInit): Promise<ArrayBuffer>;
19
+ declare function fetchJsonWithProgress(url: string, onProgress?: FetchProgressHandler, init?: FetchInit): Promise<unknown>;
17
20
 
18
21
  /** Resolve a relative asset URL against the model settings URL. */
19
22
  declare function resolveAssetUrl(baseUrl: string, relative: string): string;
20
23
  interface UrlAssetResolverOptions {
24
+ signal?: AbortSignal;
21
25
  onBytesProgress?: (key: AssetKey, update: {
22
26
  bytesLoaded: number;
23
27
  bytesTotal: number | null;
@@ -36,7 +40,9 @@ declare function runLoadPipeline(ctx: LoadPipelineContext, middlewares: LoadMidd
36
40
  declare function fetchModelJson(url: string, onProgress?: (update: {
37
41
  bytesLoaded: number;
38
42
  bytesTotal: number | null;
39
- }) => void): Promise<unknown>;
43
+ }) => void, init?: {
44
+ signal?: AbortSignal;
45
+ }): Promise<unknown>;
40
46
  /** Detect moc2 vs moc3 from settings JSON shape (core helper; throws if unknown). */
41
47
  declare function detectModelFormat(json: unknown): ModelFormat;
42
48
  /** Normalize raw model3.json / model.json into ModelSettings. */
package/dist/index.js CHANGED
@@ -6,8 +6,8 @@ function safeProgress(onProgress, update) {
6
6
  } catch {
7
7
  }
8
8
  }
9
- async function fetchArrayBufferWithProgress(url, onProgress) {
10
- const res = await fetch(url);
9
+ async function fetchArrayBufferWithProgress(url, onProgress, init) {
10
+ const res = await fetch(url, { signal: init?.signal });
11
11
  if (!res.ok) {
12
12
  throw new Error(
13
13
  `@doki-land/live2d-loader: failed to fetch bytes (${res.status}): ${url}`
@@ -49,8 +49,8 @@ async function fetchArrayBufferWithProgress(url, onProgress) {
49
49
  });
50
50
  return out.buffer;
51
51
  }
52
- async function fetchJsonWithProgress(url, onProgress) {
53
- const bytes = await fetchArrayBufferWithProgress(url, onProgress);
52
+ async function fetchJsonWithProgress(url, onProgress, init) {
53
+ const bytes = await fetchArrayBufferWithProgress(url, onProgress, init);
54
54
  const text = new TextDecoder().decode(bytes);
55
55
  return JSON.parse(text);
56
56
  }
@@ -77,15 +77,23 @@ function createUrlAssetResolver(baseUrl, options = {}) {
77
77
  },
78
78
  async fetchJson(key) {
79
79
  const url = resolveAssetUrl(baseUrl, key);
80
- return fetchJsonWithProgress(url, (update) => {
81
- options.onBytesProgress?.(key, update);
82
- });
80
+ return fetchJsonWithProgress(
81
+ url,
82
+ (update) => {
83
+ options.onBytesProgress?.(key, update);
84
+ },
85
+ { signal: options.signal }
86
+ );
83
87
  },
84
88
  async fetchBytes(key) {
85
89
  const url = resolveAssetUrl(baseUrl, key);
86
- return fetchArrayBufferWithProgress(url, (update) => {
87
- options.onBytesProgress?.(key, update);
88
- });
90
+ return fetchArrayBufferWithProgress(
91
+ url,
92
+ (update) => {
93
+ options.onBytesProgress?.(key, update);
94
+ },
95
+ { signal: options.signal }
96
+ );
89
97
  }
90
98
  };
91
99
  }
@@ -99,8 +107,8 @@ async function runLoadPipeline(ctx, middlewares) {
99
107
  await dispatch();
100
108
  return ctx;
101
109
  }
102
- async function fetchModelJson(url, onProgress) {
103
- return fetchJsonWithProgress(url, onProgress);
110
+ async function fetchModelJson(url, onProgress, init) {
111
+ return fetchJsonWithProgress(url, onProgress, init);
104
112
  }
105
113
  function detectModelFormat(json) {
106
114
  if (!json || typeof json !== "object") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doki-land/live2d-loader",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "Fetch and normalize Live2D model.json/model3.json, resolve npm: URLs, AssetResolver for moc/textures.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  "test": "vitest run"
43
43
  },
44
44
  "dependencies": {
45
- "@doki-land/live2d-core": "0.0.23"
45
+ "@doki-land/live2d-core": "0.0.24"
46
46
  },
47
47
  "sideEffects": false
48
48
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Fetch helpers with optional byte-level progress.
2
+ * Fetch helpers with optional byte-level progress and AbortSignal.
3
3
  */
4
4
 
5
5
  export type FetchProgressHandler = (update: {
@@ -7,6 +7,10 @@ export type FetchProgressHandler = (update: {
7
7
  bytesTotal: number | null;
8
8
  }) => void;
9
9
 
10
+ export type FetchInit = {
11
+ signal?: AbortSignal;
12
+ };
13
+
10
14
  function safeProgress(
11
15
  onProgress: FetchProgressHandler | undefined,
12
16
  update: { bytesLoaded: number; bytesTotal: number | null },
@@ -27,8 +31,9 @@ function safeProgress(
27
31
  export async function fetchArrayBufferWithProgress(
28
32
  url: string,
29
33
  onProgress?: FetchProgressHandler,
34
+ init?: FetchInit,
30
35
  ): Promise<ArrayBuffer> {
31
- const res = await fetch(url);
36
+ const res = await fetch(url, { signal: init?.signal });
32
37
  if (!res.ok) {
33
38
  throw new Error(
34
39
  `@doki-land/live2d-loader: failed to fetch bytes (${res.status}): ${url}`,
@@ -82,8 +87,9 @@ export async function fetchArrayBufferWithProgress(
82
87
  export async function fetchJsonWithProgress(
83
88
  url: string,
84
89
  onProgress?: FetchProgressHandler,
90
+ init?: FetchInit,
85
91
  ): Promise<unknown> {
86
- const bytes = await fetchArrayBufferWithProgress(url, onProgress);
92
+ const bytes = await fetchArrayBufferWithProgress(url, onProgress, init);
87
93
  const text = new TextDecoder().decode(bytes);
88
94
  return JSON.parse(text) as unknown;
89
95
  }
@@ -28,6 +28,7 @@ export function resolveAssetUrl(baseUrl: string, relative: string): string {
28
28
  }
29
29
 
30
30
  export interface UrlAssetResolverOptions {
31
+ signal?: AbortSignal;
31
32
  onBytesProgress?: (
32
33
  key: AssetKey,
33
34
  update: { bytesLoaded: number; bytesTotal: number | null },
@@ -45,15 +46,23 @@ export function createUrlAssetResolver(
45
46
  },
46
47
  async fetchJson(key: AssetKey) {
47
48
  const url = resolveAssetUrl(baseUrl, key);
48
- return fetchJsonWithProgress(url, (update) => {
49
- options.onBytesProgress?.(key, update);
50
- });
49
+ return fetchJsonWithProgress(
50
+ url,
51
+ (update) => {
52
+ options.onBytesProgress?.(key, update);
53
+ },
54
+ { signal: options.signal },
55
+ );
51
56
  },
52
57
  async fetchBytes(key: AssetKey) {
53
58
  const url = resolveAssetUrl(baseUrl, key);
54
- return fetchArrayBufferWithProgress(url, (update) => {
55
- options.onBytesProgress?.(key, update);
56
- });
59
+ return fetchArrayBufferWithProgress(
60
+ url,
61
+ (update) => {
62
+ options.onBytesProgress?.(key, update);
63
+ },
64
+ { signal: options.signal },
65
+ );
57
66
  },
58
67
  };
59
68
  }
@@ -91,8 +100,9 @@ export async function fetchModelJson(
91
100
  bytesLoaded: number;
92
101
  bytesTotal: number | null;
93
102
  }) => void,
103
+ init?: { signal?: AbortSignal },
94
104
  ): Promise<unknown> {
95
- return fetchJsonWithProgress(url, onProgress);
105
+ return fetchJsonWithProgress(url, onProgress, init);
96
106
  }
97
107
 
98
108
  /** Detect moc2 vs moc3 from settings JSON shape (core helper; throws if unknown). */