dsh-open-model 1.0.0 → 1.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.
- package/README.md +68 -16
- package/dist/client.js +2137 -0
- package/dist/index.js +27974 -0
- package/models.json +169 -0
- package/native/credential-key +0 -0
- package/notices/LICENSE +54 -0
- package/notices/THIRD_PARTY_NOTICES.md +424 -0
- package/package.json +41 -9
- package/catalog.js +0 -182
- package/client.js +0 -709
- package/index.js +0 -96
- package/model-availability.js +0 -99
- package/model-management.js +0 -111
- package/stream.js +0 -382
- package/updates.js +0 -141
package/updates.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import manifest from './package.json' with { type: 'json' };
|
|
2
|
-
|
|
3
|
-
const packageUrl = `https://registry.npmjs.org/${manifest.name.replace('/', '%2f')}`;
|
|
4
|
-
const minimumReleaseAge = 24 * 60 * 60 * 1000;
|
|
5
|
-
const queryTimeout = 5_000;
|
|
6
|
-
const maxLatestBytes = 256 * 1024;
|
|
7
|
-
const maxHistoryBytes = 8 * 1024 * 1024;
|
|
8
|
-
const versionPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
9
|
-
const numericIdentifier = /^\d+$/;
|
|
10
|
-
const releaseTimestamp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
11
|
-
const cacheTime = {
|
|
12
|
-
update_available: 6 * 60 * 60 * 1000,
|
|
13
|
-
release_pending: 6 * 60 * 60 * 1000,
|
|
14
|
-
up_to_date: 6 * 60 * 60 * 1000,
|
|
15
|
-
unpublished: 30 * 60 * 1000,
|
|
16
|
-
unreachable: 5 * 60 * 1000,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function parseVersion(value) {
|
|
20
|
-
if (typeof value !== 'string') return undefined;
|
|
21
|
-
const match = versionPattern.exec(value);
|
|
22
|
-
if (match?.[0] !== value) return undefined;
|
|
23
|
-
const prerelease = match[4]?.split('.') ?? [];
|
|
24
|
-
if (prerelease.some(part => /^0\d+$/.test(part))) return undefined;
|
|
25
|
-
return { core: match.slice(1, 4), prerelease };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function compareIdentifiers(left, right) {
|
|
29
|
-
if (left === right) return 0;
|
|
30
|
-
const leftNumeric = numericIdentifier.test(left);
|
|
31
|
-
const rightNumeric = numericIdentifier.test(right);
|
|
32
|
-
if (leftNumeric !== rightNumeric) return leftNumeric ? -1 : 1;
|
|
33
|
-
if (leftNumeric && left.length !== right.length) return left.length - right.length;
|
|
34
|
-
return left < right ? -1 : 1;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function isNewer(latest, current) {
|
|
38
|
-
for (let index = 0; index < latest.core.length; index += 1) {
|
|
39
|
-
const precedence = compareIdentifiers(latest.core[index], current.core[index]);
|
|
40
|
-
if (precedence) return precedence > 0;
|
|
41
|
-
}
|
|
42
|
-
if (!latest.prerelease.length) return current.prerelease.length > 0;
|
|
43
|
-
if (!current.prerelease.length) return false;
|
|
44
|
-
const sharedLength = Math.min(latest.prerelease.length, current.prerelease.length);
|
|
45
|
-
for (let index = 0; index < sharedLength; index += 1) {
|
|
46
|
-
const precedence = compareIdentifiers(latest.prerelease[index], current.prerelease[index]);
|
|
47
|
-
if (precedence) return precedence > 0;
|
|
48
|
-
}
|
|
49
|
-
return latest.prerelease.length > current.prerelease.length;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async function readMetadata(response, maxBytes) {
|
|
53
|
-
let bytes = 0;
|
|
54
|
-
let json = '';
|
|
55
|
-
const decoder = new TextDecoder();
|
|
56
|
-
for await (const chunk of response.body) {
|
|
57
|
-
bytes += chunk.byteLength;
|
|
58
|
-
if (bytes > maxBytes) throw new Error('npm registry metadata exceeded the request limit.');
|
|
59
|
-
json += decoder.decode(chunk, { stream: true });
|
|
60
|
-
}
|
|
61
|
-
return JSON.parse(json + decoder.decode());
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function createUpdateChecker({
|
|
65
|
-
currentVersion = manifest.version,
|
|
66
|
-
fetch: fetchRegistry = globalThis.fetch,
|
|
67
|
-
now = Date.now,
|
|
68
|
-
signal,
|
|
69
|
-
} = {}) {
|
|
70
|
-
const current = parseVersion(currentVersion);
|
|
71
|
-
const unreachable = { status: 'unreachable', currentVersion };
|
|
72
|
-
let cached;
|
|
73
|
-
let expiresAt = 0;
|
|
74
|
-
let pending;
|
|
75
|
-
|
|
76
|
-
async function query() {
|
|
77
|
-
if (!current) return unreachable;
|
|
78
|
-
const timeout = AbortSignal.timeout(queryTimeout);
|
|
79
|
-
const options = {
|
|
80
|
-
headers: { accept: 'application/json' },
|
|
81
|
-
credentials: 'omit',
|
|
82
|
-
redirect: 'error',
|
|
83
|
-
signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
|
|
84
|
-
};
|
|
85
|
-
try {
|
|
86
|
-
const response = await fetchRegistry(`${packageUrl}/latest`, options);
|
|
87
|
-
if (!response.ok) {
|
|
88
|
-
await response.body?.cancel().catch(() => undefined);
|
|
89
|
-
return response.status === 404 ? { status: 'unpublished', currentVersion } : unreachable;
|
|
90
|
-
}
|
|
91
|
-
const { name, version: latestVersion } = await readMetadata(response, maxLatestBytes);
|
|
92
|
-
options.signal.throwIfAborted();
|
|
93
|
-
const latest = parseVersion(latestVersion);
|
|
94
|
-
if (name !== manifest.name || !latest) return unreachable;
|
|
95
|
-
if (!isNewer(latest, current)) return { status: 'up_to_date', currentVersion, latestVersion };
|
|
96
|
-
|
|
97
|
-
const history = await fetchRegistry(packageUrl, options);
|
|
98
|
-
if (!history.ok) {
|
|
99
|
-
await history.body?.cancel().catch(() => undefined);
|
|
100
|
-
return unreachable;
|
|
101
|
-
}
|
|
102
|
-
const metadata = await readMetadata(history, maxHistoryBytes);
|
|
103
|
-
options.signal.throwIfAborted();
|
|
104
|
-
if (metadata?.name !== name || metadata['dist-tags']?.latest !== latestVersion) return unreachable;
|
|
105
|
-
const timestamp = metadata.time?.[latestVersion];
|
|
106
|
-
const publishedAt = typeof timestamp === 'string' && releaseTimestamp.test(timestamp) ? Date.parse(timestamp) : NaN;
|
|
107
|
-
if (!Number.isFinite(publishedAt)) return unreachable;
|
|
108
|
-
const eligibleAt = publishedAt + minimumReleaseAge;
|
|
109
|
-
if (now() < eligibleAt) return { status: 'release_pending', currentVersion, latestVersion, eligibleAt };
|
|
110
|
-
return { status: 'update_available', currentVersion, latestVersion };
|
|
111
|
-
} catch {
|
|
112
|
-
return unreachable;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return async function checkUpdate() {
|
|
117
|
-
if (signal?.aborted) return unreachable;
|
|
118
|
-
if (cached && now() < expiresAt) return cached;
|
|
119
|
-
if (pending) return pending;
|
|
120
|
-
pending = query().then(({ eligibleAt, ...result }) => {
|
|
121
|
-
cached = Object.freeze(result);
|
|
122
|
-
expiresAt = Math.min(now() + cacheTime[result.status], eligibleAt ?? Infinity);
|
|
123
|
-
return cached;
|
|
124
|
-
}).finally(() => { pending = undefined; });
|
|
125
|
-
return pending;
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function mountUpdates(ctx) {
|
|
130
|
-
const lifetime = new AbortController();
|
|
131
|
-
ctx.effect(() => () => lifetime.abort(), 'dsh-free-model: update check lifetime');
|
|
132
|
-
const checkUpdate = createUpdateChecker({ signal: lifetime.signal });
|
|
133
|
-
ctx.connection.fetch.register({
|
|
134
|
-
path: '/api/dsh-free-model/update',
|
|
135
|
-
methods: ['GET'],
|
|
136
|
-
requestBody: 'buffered',
|
|
137
|
-
async fetch() {
|
|
138
|
-
return Response.json(await checkUpdate(), { headers: { 'cache-control': 'no-store' } });
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
}
|