1
1
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
2
2
// SPDX-License-Identifier: Elastic-2.0
3
3
4
- import { toFileUrl } from "std/path/mod.ts" ;
5
4
import { ComputeStage } from "../../engine/query_engine.ts" ;
6
5
import { TypeGraphDS , TypeMaterializer } from "../../typegraph/mod.ts" ;
7
6
import { Runtime } from "../Runtime.ts" ;
8
7
import { Resolver , RuntimeInitParams } from "../../types.ts" ;
9
- import { DenoRuntimeData } from "../../typegraph/types.ts" ;
8
+ import { Artifact , DenoRuntimeData } from "../../typegraph/types.ts" ;
10
9
import * as ast from "graphql/ast" ;
11
10
import { InternalAuth } from "../../services/auth/protocols/internal.ts" ;
12
11
import { DenoMessenger } from "./deno_messenger.ts" ;
13
12
import { Task } from "./shared_types.ts" ;
14
- import { structureRepr , uncompress } from "../../utils.ts" ;
15
13
import { path } from "compress/deps.ts" ;
16
14
import config from "../../config.ts" ;
17
15
import { getLogger } from "../../log.ts" ;
@@ -39,8 +37,14 @@ export class DenoRuntime extends Runtime {
39
37
static async init (
40
38
params : RuntimeInitParams ,
41
39
) : Promise < Runtime > {
42
- const { typegraph : tg , typegraphName, args, materializers, secretManager } =
43
- params as RuntimeInitParams < DenoRuntimeData > ;
40
+ const {
41
+ typegraph : tg ,
42
+ typegraphName,
43
+ args,
44
+ materializers,
45
+ secretManager,
46
+ typegate,
47
+ } = params as RuntimeInitParams < DenoRuntimeData > ;
44
48
45
49
const { worker : name } = args as unknown as DenoRuntimeData ;
46
50
if ( name == null ) {
@@ -56,6 +60,7 @@ export class DenoRuntime extends Runtime {
56
60
}
57
61
}
58
62
63
+ // maps from the module code to the op number/id
59
64
const registry = new Map < string , number > ( ) ;
60
65
const ops = new Map < number , Task > ( ) ;
61
66
@@ -64,21 +69,9 @@ export class DenoRuntime extends Runtime {
64
69
// => (gate) tmp/scripts/{tgname}/deno/*
65
70
const basePath = path . join (
66
71
config . tmp_dir ,
67
- "scripts" ,
68
- typegraphName ,
69
- uuid ,
70
- "deno" ,
71
- name . replaceAll ( " " , "_" ) , // TODO: improve sanitization
72
+ "artifacts" , // TODO: improve sanitization
72
73
) ;
73
74
74
- try {
75
- // clean up old files
76
- // logger.debug(`removes files at ${basePath}`);
77
- await Deno . remove ( basePath , { recursive : true } ) ;
78
- } catch {
79
- // ignore non-existent files
80
- }
81
-
82
75
let registryCount = 0 ;
83
76
for ( const mat of materializers ) {
84
77
if ( mat . name === "function" ) {
@@ -92,29 +85,50 @@ export class DenoRuntime extends Runtime {
92
85
registry . set ( code , registryCount ) ;
93
86
registryCount += 1 ;
94
87
} else if ( mat . name === "module" ) {
95
- const code = mat . data . code as string ;
88
+ const matData = mat . data ;
89
+ const denoArtifact = matData . denoArtifact as Artifact ;
90
+ const depArtifacts = matData . depsMeta as Artifact [ ] ;
91
+
92
+ const moduleMeta = {
93
+ typegraphName : typegraphName ,
94
+ relativePath : denoArtifact . path ,
95
+ hash : denoArtifact . hash ,
96
+ sizeInBytes : denoArtifact . size ,
97
+ } ;
96
98
97
- const repr = await structureRepr ( code ) ;
98
- const outDir = path . join ( basePath , repr . hashes . entryPoint ) ;
99
- const entries = await uncompress (
100
- outDir ,
101
- repr . base64 ,
99
+ const depMetas = depArtifacts . map (
100
+ ( dep ) => {
101
+ return {
102
+ typegraphName : typegraphName ,
103
+ relativePath : dep . path ,
104
+ hash : dep . hash ,
105
+ sizeInBytes : dep . size ,
106
+ } ;
107
+ } ,
102
108
) ;
103
109
104
- logger . info ( `uncompressed ${ entries . join ( ", " ) } at ${ outDir } ` ) ;
110
+ // Note:
111
+ // Worker destruction seems to have no effect on the import cache? (deinit() => stop(worker))
112
+ // hence the use of contentHash
113
+ const entryModulePath = await typegate . artifactStore . getLocalPath (
114
+ moduleMeta ,
115
+ depMetas ,
116
+ ) ;
117
+
118
+ logger . info ( `Resloved runtimes artifacts at ${ basePath } ` ) ;
105
119
106
120
// Note:
107
121
// Worker destruction seems to have no effect on the import cache? (deinit() => stop(worker))
108
122
// hence the use of contentHash
109
123
ops . set ( registryCount , {
110
124
type : "register_import_func" ,
111
- modulePath : toFileUrl (
112
- path . resolve ( outDir , repr . entryPoint ) ,
113
- ) . toString ( ) + `?hash=${ repr . hashes . content } ` ,
125
+ modulePath : entryModulePath ,
114
126
op : registryCount ,
115
127
verbose : config . debug ,
116
128
} ) ;
117
- registry . set ( code , registryCount ) ;
129
+
130
+ // TODO: can a single aritfact be used by multiple materializers?
131
+ registry . set ( denoArtifact . hash , registryCount ) ;
118
132
registryCount += 1 ;
119
133
}
120
134
}
@@ -216,7 +230,9 @@ export class DenoRuntime extends Runtime {
216
230
217
231
if ( mat . name === "import_function" ) {
218
232
const modMat = this . tg . materializers [ mat . data . mod as number ] ;
219
- const op = this . registry . get ( modMat . data . code as string ) ! ;
233
+ const denoAritfact = modMat . data . denoArtifact as Artifact ;
234
+ const op = this . registry . get ( denoAritfact . hash ) ! ;
235
+
220
236
return async (
221
237
{ _ : { context, parent, info : { url, headers } } , ...args } ,
222
238
) => {
0 commit comments