-
Notifications
You must be signed in to change notification settings - Fork 1
/
preloader.mjs
74 lines (67 loc) · 2.45 KB
/
preloader.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { DeepClient } from '@deep-foundation/deeplinks/imports/client.js';
import { generateApolloClient } from '@deep-foundation/hasura/client.js';
import fs from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
let path, ssl = true;
try { path = process.env.GQL || process.env.NEXT_PUBLIC_GRAPHQL_URL || 'https://deeplinks.deep.foundation/gql' } catch(e) {}
try { ssl = new URL(path).protocol === "https:" } catch(e) {}
path = path.replace(/(^\w+:|^)\/\//, '');
const secret = process.env.SECRET || process.env.DEEPLINKS_HASURA_SECRET;
const token = process.env.TOKEN || process.env.NEXT_PUBLIC_DEEP_TOKEN;
const deep = new DeepClient({
apolloClient: generateApolloClient({
path: path,
ssl: ssl,
secret: secret,
token: token,
}),
})
var init = async () => {
const { plainLinks: packages } = await deep.select({
type_id: { _nin: [
deep.idLocal('@deep-foundation/core', 'Promise'),
deep.idLocal('@deep-foundation/core', 'Then'),
deep.idLocal('@deep-foundation/core', 'Rejected'),
deep.idLocal('@deep-foundation/core', 'Resolved'),
deep.idLocal('@deep-foundation/core', 'PromiseResult'),
] },
up: {
tree_id: { _eq: deep.idLocal('@deep-foundation/core', 'containTree') },
parent: {
type_id: { _eq: deep.idLocal('@deep-foundation/core', 'Package') },
string: { value: { _neq: 'deep' } },
},
},
return: {
_version: {
relation: 'in',
type_id: deep.idLocal('@deep-foundation/core', 'PackageVersion')
},
},
}, { apply: 'packages' });
const { data: handlers } = await deep.select({
execution_provider_id: { _eq: deep.idLocal('@deep-foundation/core', 'JSExecutionProvider') },
return: {
dist: { relation: 'dist' }
},
}, { table: 'handlers' });
console.log('preloader packages', packages?.length || 0);
console.log('preloader handlers', handlers?.length || 0);
if (!packages?.length || !handlers?.length) {
console.log('preloader ignored');
return;
}
deep.minilinks.apply(packages, 'packages');
const preloaded = deep.minilinks.links.map(l => l.toPlain());
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
fs.writeFileSync(__dirname+'/imports/preloaded.js', `export default ${JSON.stringify({
packages: preloaded,
handlers,
}, null, 2)}`);
console.log('preloader success', __dirname+'/imports/preloaded.js');
}
if (secret || token) {
init();
}