Skip to content

Commit

Permalink
Merge pull request #413 from coasys/centralized-link-language
Browse files Browse the repository at this point in the history
New Link Langauges
  • Loading branch information
jdeepee authored Oct 20, 2023
2 parents 33c1402 + 20dff3f commit b22cebb
Show file tree
Hide file tree
Showing 81 changed files with 14,283 additions and 26 deletions.
20 changes: 20 additions & 0 deletions bootstrap-languages/centralized-p-diff-sync/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.cargo
target
hc-dna/workdir/perspective-diff-sync.dna
hc-dna/zomes/tests/node_modules
hc-dna/zomes/tests/out.log
node_modules
.hc*
perspect-diff-sync.dna
*.log
.turbo

*.js
*.js.map
!rollup.config*
!.dna.js
!integration-test.js
!customHttpDownloader.js

.ad4m-test
ad4m-test-*
3 changes: 3 additions & 0 deletions bootstrap-languages/centralized-p-diff-sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Perspective Diff Sync

Git like holochain syncing DNA for sharing of mutation to a shared perspective.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
let setup = ({onResolve, onLoad}) => {
onResolve({filter: /^https:\/\/github.com/}, resolveFile)
onResolve({filter: /.*/, namespace: 'http-fetch'}, resolveUrl)
onLoad({filter: /.*/, namespace: 'http-fetch'}, loadSource)
}

let resolveFile = ({path}) => {
return {
path: path,
namespace: 'http-fetch'
}
}

export let resolveUrl = ({path, importer}) => {
return {
path: new URL(path, importer).href,
namespace: 'imports'
}
}

export let loadSource = async ({path}) => {
let source;
if (path.includes('perspect3vism')) {
const commit = path.split("#");

const url = `${commit[0].replace("https://github.com", "https://raw.githubusercontent.com")}/${commit[1]}/lib/bundle.js`

path = url;
}

source = await fetch(path)

if (!source.ok) {
let message = `GET ${path} failed: status ${source.status}`
throw new Error(message)
}

let contents = await source.text()
let pattern = /\/\/# sourceMappingURL=(\S+)/
let match = contents.match(pattern)
if (match) {
let url = new URL(match[1], source.url)
let dataurl = await loadMap(url)
let comment = `//# sourceMappingURL=${dataurl}`
contents = contents.replace(pattern, comment)
}



let {pathname} = new URL(source.url)
let loader = pathname.match(/[^.]+$/)[0]

loader = loader === 'mjs' ? 'js' : loader
return {contents, loader}
}

let loadMap = async url => {
let map = await fetch(url)
let type = map.headers.get('content-type').replace(/\s/g, '')
let buffer = await map.arrayBuffer()
let blob = new Blob([buffer], {type})
let reader = new FileReader()
return new Promise(cb => {
reader.onload = e => cb(e.target.result)
reader.readAsDataURL(blob)
})
}

export default {
name: "custom-http-fetch",
setup
};
39 changes: 39 additions & 0 deletions bootstrap-languages/centralized-p-diff-sync/esbuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
// Import the WASM build on platforms where running subprocesses is not
// permitted, such as Deno Deploy, or when running without `--allow-run`.
// import * as esbuild from "https://deno.land/x/[email protected]/wasm.js";

import { denoPlugins } from "https://deno.land/x/[email protected]/mod.ts";
import { loadSource, resolveUrl } from "./customHttpDownloader.js";

const result = await esbuild.build({
plugins: [
{
name: `buffer-alias`,
setup(build) {
build.onResolve({ filter: new RegExp(`^buffer$`) }, (args) => {
return { path: `https://deno.land/[email protected]/node/buffer.ts`, namespace: 'imports' };
});

build.onResolve({filter: /.*/, namespace: 'imports'}, resolveUrl)

build.onLoad({filter: /.*/, namespace: 'imports'}, (args) => {
return loadSource(args)
})
},
},
...denoPlugins()
],
entryPoints: ['index.ts'],
outfile: 'build/bundle.js',
bundle: true,
platform: 'node',
target: 'deno1.32.4',
format: 'esm',
globalName: 'centralized.perspective.diff.sync.language',
charset: 'ascii',
legalComments: 'inline'
});
console.log(result.outputFiles);

esbuild.stop();
34 changes: 34 additions & 0 deletions bootstrap-languages/centralized-p-diff-sync/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Address, Language, Interaction, HolochainLanguageDelegate, LanguageContext, AgentService } from "https://esm.sh/@perspect3vism/[email protected]";
import { LinkAdapter } from "./linksAdapter.ts";
import { TelepresenceAdapterImplementation } from "./telepresenceAdapter.ts";
import { io } from "https://esm.sh/[email protected]";

function interactions(expression: Address): Interaction[] {
return [];
}

//!@ad4m-template-variable
const name = "centralized-perspective-diff-sync";

//!@ad4m-template-variable
const uid = "centralized-perspective-diff-sync-uuid";

export default async function create(context: LanguageContext): Promise<Language> {
let socketClient = io("https://socket.ad4m.dev", {
transports: ['websocket', 'polling'],
autoConnect: true,
query: { did: context.agent.did, linkLanguageUUID: uid }
});
console.log("Created socket connection");

const linksAdapter = new LinkAdapter(context, uid, socketClient);
const telepresenceAdapter = new TelepresenceAdapterImplementation(context, uid, socketClient);

//@ts-ignore
return {
name,
linksAdapter,
interactions,
telepresenceAdapter
} as Language;
}
20 changes: 20 additions & 0 deletions bootstrap-languages/centralized-p-diff-sync/integration-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { spawnLinkAgent } = require("@perspect3vism/ad4m-test/helpers");

describe("Link", () => {
it("Create Link", async () => {
const agent = await spawnLinkAgent();

const all = await agent.queryLinks({});

expect(all.length).toBe(0)

const link = await agent.addLink({source:"root", predicate: "soic://test", target:"QmYVsrMpiFmV9S7bTWNAkUzSqjRJskQ8g4TWKKwKrHAPqL://QmSsCCtXMDAZXMpyiNLzwjGEU4hLmhG7fphidhEEodQ4Wy"})

const all1 = await agent.queryLinks({});

expect(all1.length).toBe(1)
expect(all1[0].data.source).toBe(link.data.source)
expect(all1[0].data.predicate).toBe(link.data.predicate)
expect(all1[0].data.target).toBe(link.data.target)
});
})
Loading

0 comments on commit b22cebb

Please sign in to comment.