@atlaspack/query 2.12.1-canary.3354

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,61 @@
1
+ // @flow
2
+ /* eslint-disable monorepo/no-internal-import */
3
+ import typeof AssetGraph from '@atlaspack/core/src/AssetGraph.js';
4
+ import typeof BundleGraph, {
5
+ bundleGraphEdgeTypes,
6
+ } from '@atlaspack/core/src/BundleGraph.js';
7
+ import typeof RequestTracker, {
8
+ RequestGraph,
9
+ readAndDeserializeRequestGraph,
10
+ } from '@atlaspack/core/src/RequestTracker.js';
11
+ import typeof {requestGraphEdgeTypes} from '@atlaspack/core/src/RequestTracker.js';
12
+ import typeof {LMDBCache} from '@atlaspack/cache/src/LMDBCache.js';
13
+ import typeof {Priority} from '@atlaspack/core/src/types.js';
14
+ import typeof {fromProjectPathRelative} from '@atlaspack/core/src/projectPath.js';
15
+
16
+ const v =
17
+ process.env.ATLASPACK_BUILD_ENV === 'production'
18
+ ? {
19
+ // Split up require specifier to outsmart packages/dev/babel-register/babel-plugin-module-translate.js
20
+ // $FlowFixMe(unsupported-syntax)
21
+ AssetGraph: require('@atlaspack/core' + '/lib/AssetGraph.js').default,
22
+ // $FlowFixMe(unsupported-syntax)
23
+ BundleGraph: require('@atlaspack/core' + '/lib/BundleGraph.js'),
24
+ // $FlowFixMe(unsupported-syntax)
25
+ RequestTracker: require('@atlaspack/core' + '/lib/RequestTracker.js'),
26
+ // $FlowFixMe(unsupported-syntax)
27
+ LMDBCache: require('@atlaspack/cache' + '/lib/LMDBCache.js').LMDBCache,
28
+ // $FlowFixMe(unsupported-syntax)
29
+ Priority: require('@atlaspack/core' + '/lib/types.js').Priority,
30
+ // $FlowFixMe(unsupported-syntax)
31
+ fromProjectPathRelative: require('@atlaspack/core' +
32
+ '/lib/projectPath.js').fromProjectPathRelative,
33
+ }
34
+ : {
35
+ AssetGraph: require('@atlaspack/core/src/AssetGraph.js').default,
36
+ BundleGraph: require('@atlaspack/core/src/BundleGraph.js'),
37
+ RequestTracker: require('@atlaspack/core/src/RequestTracker.js'),
38
+ LMDBCache: require('@atlaspack/cache/src/LMDBCache.js').LMDBCache,
39
+ Priority: require('@atlaspack/core/src/types.js').Priority,
40
+ fromProjectPathRelative: require('@atlaspack/core/src/projectPath.js')
41
+ .fromProjectPathRelative,
42
+ };
43
+
44
+ module.exports = (v: {|
45
+ AssetGraph: AssetGraph,
46
+ BundleGraph: {
47
+ default: BundleGraph,
48
+ bundleGraphEdgeTypes: bundleGraphEdgeTypes,
49
+ ...
50
+ },
51
+ RequestTracker: {
52
+ default: RequestTracker,
53
+ readAndDeserializeRequestGraph: readAndDeserializeRequestGraph,
54
+ RequestGraph: RequestGraph,
55
+ requestGraphEdgeTypes: requestGraphEdgeTypes,
56
+ ...
57
+ },
58
+ LMDBCache: LMDBCache,
59
+ Priority: Priority,
60
+ fromProjectPathRelative: fromProjectPathRelative,
61
+ |});
package/src/index.js ADDED
@@ -0,0 +1,185 @@
1
+ // @flow strict-local
2
+ /* eslint-disable no-console, monorepo/no-internal-import */
3
+ import type {ContentKey, NodeId} from '@atlaspack/graph';
4
+ import type {PackagedBundleInfo} from '@atlaspack/core/src/types';
5
+
6
+ import fs from 'fs';
7
+ import path from 'path';
8
+ import v8 from 'v8';
9
+ import nullthrows from 'nullthrows';
10
+ import invariant from 'assert';
11
+
12
+ const {
13
+ AssetGraph,
14
+ BundleGraph: {default: BundleGraph},
15
+ RequestTracker: {
16
+ default: RequestTracker,
17
+ readAndDeserializeRequestGraph,
18
+ requestGraphEdgeTypes,
19
+ },
20
+ LMDBCache,
21
+ } = require('./deep-imports.js');
22
+
23
+ export async function loadGraphs(cacheDir: string): Promise<{|
24
+ assetGraph: ?AssetGraph,
25
+ bundleGraph: ?BundleGraph,
26
+ requestTracker: ?RequestTracker,
27
+ bundleInfo: ?Map<ContentKey, PackagedBundleInfo>,
28
+ cacheInfo: ?Map<string, Array<string | number>>,
29
+ |}> {
30
+ function getMostRecentCacheBlobs() {
31
+ let files = fs.readdirSync(cacheDir);
32
+
33
+ let result = {};
34
+
35
+ let blobsToFind: Array<{|
36
+ name: string,
37
+ check: (v: string) => boolean,
38
+ mtime?: Date,
39
+ |}> = [
40
+ {
41
+ name: 'requestGraphBlob',
42
+ check: basename =>
43
+ basename.startsWith('requestGraph-') &&
44
+ !basename.startsWith('requestGraph-nodes'),
45
+ },
46
+ {
47
+ name: 'bundleGraphBlob',
48
+ check: basename => basename.endsWith('BundleGraph-0'),
49
+ },
50
+ {
51
+ name: 'assetGraphBlob',
52
+ check: basename => basename.endsWith('AssetGraph-0'),
53
+ },
54
+ ];
55
+
56
+ for (let file of files) {
57
+ let basename = path.basename(file);
58
+ let match = blobsToFind.find(({check}) => check(basename));
59
+
60
+ if (match) {
61
+ let stat = fs.statSync(path.join(cacheDir, file));
62
+
63
+ if (!match.mtime || stat.mtime > match.mtime) {
64
+ match.mtime = stat.mtime;
65
+ result[match.name] = file;
66
+ }
67
+ }
68
+ }
69
+
70
+ return result;
71
+ }
72
+
73
+ let cacheInfo: Map<string, Array<string | number>> = new Map();
74
+
75
+ let {requestGraphBlob, bundleGraphBlob, assetGraphBlob} =
76
+ getMostRecentCacheBlobs();
77
+ const cache = new LMDBCache(cacheDir);
78
+
79
+ // Get requestTracker
80
+ let requestTracker;
81
+ if (requestGraphBlob) {
82
+ try {
83
+ let requestGraphKey = requestGraphBlob.slice(0, -'-0'.length);
84
+ let date = Date.now();
85
+ let {requestGraph, bufferLength} = await readAndDeserializeRequestGraph(
86
+ cache,
87
+ requestGraphKey,
88
+ requestGraphKey.replace('requestGraph-', ''),
89
+ );
90
+
91
+ requestTracker = new RequestTracker({
92
+ graph: requestGraph,
93
+ // $FlowFixMe
94
+ farm: null,
95
+ // $FlowFixMe
96
+ options: null,
97
+ });
98
+ let timeToDeserialize = Date.now() - date;
99
+ cacheInfo.set('RequestGraph', [bufferLength]);
100
+ cacheInfo.get('RequestGraph')?.push(timeToDeserialize);
101
+ } catch (e) {
102
+ console.log('Error loading Request Graph\n', e);
103
+ }
104
+ }
105
+
106
+ // Get bundleGraph
107
+ let bundleGraph;
108
+ if (bundleGraphBlob) {
109
+ try {
110
+ let file = await cache.getLargeBlob(
111
+ path.basename(bundleGraphBlob).slice(0, -'-0'.length),
112
+ );
113
+
114
+ let timeToDeserialize = Date.now();
115
+ let obj = v8.deserialize(file);
116
+ invariant(obj.bundleGraph != null);
117
+ bundleGraph = BundleGraph.deserialize(obj.bundleGraph.value);
118
+ timeToDeserialize = Date.now() - timeToDeserialize;
119
+
120
+ cacheInfo.set('BundleGraph', [Buffer.byteLength(file)]);
121
+ cacheInfo.get('BundleGraph')?.push(timeToDeserialize);
122
+ } catch (e) {
123
+ console.log('Error loading Bundle Graph\n', e);
124
+ }
125
+ }
126
+
127
+ // Get assetGraph
128
+ let assetGraph;
129
+ if (assetGraphBlob) {
130
+ try {
131
+ let file = await cache.getLargeBlob(
132
+ path.basename(assetGraphBlob).slice(0, -'-0'.length),
133
+ );
134
+
135
+ let timeToDeserialize = Date.now();
136
+ let obj = v8.deserialize(file);
137
+ invariant(obj.assetGraph != null);
138
+ assetGraph = AssetGraph.deserialize(obj.assetGraph.value);
139
+ timeToDeserialize = Date.now() - timeToDeserialize;
140
+
141
+ cacheInfo.set('AssetGraph', [Buffer.byteLength(file)]);
142
+ cacheInfo.get('AssetGraph')?.push(timeToDeserialize);
143
+ } catch (e) {
144
+ console.log('Error loading Asset Graph\n', e);
145
+ }
146
+ }
147
+
148
+ function getSubRequests(id: NodeId) {
149
+ return requestTracker.graph
150
+ .getNodeIdsConnectedFrom(id, requestGraphEdgeTypes.subrequest)
151
+ .map(n => nullthrows(requestTracker.graph.getNode(n)));
152
+ }
153
+
154
+ // Load graphs by finding the main subrequests and loading their results
155
+ let bundleInfo;
156
+ try {
157
+ invariant(requestTracker);
158
+ let buildRequestId = requestTracker.graph.getNodeIdByContentKey(
159
+ 'atlaspack_build_request',
160
+ );
161
+ let buildRequestNode = nullthrows(
162
+ requestTracker.graph.getNode(buildRequestId),
163
+ );
164
+ invariant(
165
+ buildRequestNode.type === 1 && buildRequestNode.requestType === 1,
166
+ );
167
+ let buildRequestSubRequests = getSubRequests(buildRequestId);
168
+
169
+ let writeBundlesRequest = buildRequestSubRequests.find(
170
+ n => n.type === 1 && n.requestType === 11,
171
+ );
172
+ if (writeBundlesRequest != null) {
173
+ invariant(writeBundlesRequest.type === 1);
174
+ // $FlowFixMe[incompatible-cast]
175
+ bundleInfo = (nullthrows(writeBundlesRequest.result): Map<
176
+ ContentKey,
177
+ PackagedBundleInfo,
178
+ >);
179
+ }
180
+ } catch (e) {
181
+ console.log('Error loading bundleInfo\n', e);
182
+ }
183
+
184
+ return {assetGraph, bundleGraph, requestTracker, bundleInfo, cacheInfo};
185
+ }