-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLinkLanguageFactory.ts
More file actions
74 lines (61 loc) · 2.9 KB
/
LinkLanguageFactory.ts
File metadata and controls
74 lines (61 loc) · 2.9 KB
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 fs from 'fs'
import path from 'path'
import multihashing from 'multihashing'
import baseX from 'base-x'
import SharedPerspective, { SharingType } from "../ad4m/SharedPerspective";
import type AgentService from "./agent/AgentService";
import type Language from "../ad4m/Language";
import type { PublicSharing } from "../ad4m/Language";
import type LanguageRef from "../ad4m/LanguageRef";
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const bs58 = baseX(BASE58)
const templates = {
permissionless: './src/languages/ipfs-links/build/bundle.js'
}
export default class LinkLanguageFactory {
#agentService: AgentService
#languageLanguage: Language
constructor(agentService: AgentService, languageLanguage: Language) {
if(!languageLanguage.languageAdapter)
throw new Error(`Error creating LinkLanguageFactory! Not a Language Language: ${JSON.stringify(languageLanguage)}`)
this.#languageLanguage = languageLanguage
this.#agentService = agentService
}
async createLinkLanguageForSharedPerspective(sharedPerspective: SharedPerspective): Promise<LanguageRef> {
console.debug("LinkLanguageFactory: creating new link language for shared perspective:", sharedPerspective.name)
const name = `${sharedPerspective.name}-${sharedPerspective.type}-LinkLanguage`
const templateInfo = JSON.stringify(this.#agentService.createSignedExpression(sharedPerspective))
const UUID = bs58.encode(multihashing(templateInfo, 'sha2-256'))
const injection = `var TEMPLATE_INFO=${templateInfo}; var TEMPLATE_UUID="${UUID};"`
let template
switch(sharedPerspective.type) {
case SharingType.Permissionless:
console.debug("LinkLanguageFactory: Permissionless language")
const templateFilePath = path.join(process.env.PWD, templates.permissionless)
console.debug("LinkLanguageFactor: reading template file", templateFilePath)
template = fs.readFileSync(templateFilePath).toString()
break;
default:
throw new Error(`SharingType ${sharedPerspective.type} not yet implementent`)
}
const lines = template.split('\n')
lines.splice(1, 0, injection)
const code = lines.join('\n')
const newLanguageObj = {
name,
description: `UUID: ${UUID}`,
bundleFile: code.toString()
}
try {
const address = await (this.#languageLanguage.expressionAdapter.putAdapter as PublicSharing).createPublic(newLanguageObj)
console.debug("LinkLanguageFactory: new Language address:", address)
return {
address,
name: "",
} as LanguageRef
} catch(e) {
console.error("LinkLanguageFactory: ERROR creating new language:", e)
throw e
}
}
}