Skip to content

Commit

Permalink
fix(connect): properly add protocol tags on message and spawn #1059
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Oct 31, 2024
1 parent e563123 commit ea9c8f4
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 94 deletions.
12 changes: 12 additions & 0 deletions connect/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"@permaweb/ao-scheduler-utils": "~0.0.23",
"@permaweb/protocol-tag-utils": "~0.0.1",
"buffer": "^6.0.3",
"debug": "^4.3.6",
"hyper-async": "^1.1.2",
Expand Down
31 changes: 19 additions & 12 deletions connect/src/lib/message/upload-message.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Resolved, fromPromise, of } from 'hyper-async'
import { z } from 'zod'
import { __, always, append, assoc, concat, defaultTo, ifElse, pipe, prop } from 'ramda'
import { __, always, assoc, curry, defaultTo, ifElse, pipe, prop } from 'ramda'
import { proto } from '@permaweb/protocol-tag-utils'

import { deployMessageSchema, signerSchema } from '../../dal.js'
import { removeTagsByNameMaybeValue } from '../utils.js'

const aoProto = proto('ao')
const removeAoProtoByName = curry(aoProto.removeAllByName)
const concatAoProto = curry(aoProto.concat)
const concatUnassoc = curry(aoProto.concatUnassoc)

const tagSchema = z.array(z.object({
name: z.string(),
Expand Down Expand Up @@ -36,15 +41,11 @@ function buildTagsWith () {
return (ctx) => {
return of(ctx.tags)
.map(defaultTo([]))
.map(removeTagsByNameMaybeValue('Data-Protocol', 'ao'))
.map(removeTagsByNameMaybeValue('Variant'))
.map(removeTagsByNameMaybeValue('Type'))
.map(removeTagsByNameMaybeValue('SDK'))
.map(concat(__, [
{ name: 'Data-Protocol', value: 'ao' },
.map(removeAoProtoByName('Variant'))
.map(removeAoProtoByName('Type'))
.map(concatAoProto([
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Message' },
{ name: 'SDK', value: 'aoconnect' }
{ name: 'Type', value: 'Message' }
]))
.map(tagSchema.parse)
.map(assoc('tags', __, ctx))
Expand Down Expand Up @@ -79,13 +80,19 @@ function buildDataWith ({ logger }) {
.map(
(ctx) => pipe(
prop('tags'),
removeTagsByNameMaybeValue('Content-Type'),
append({ name: 'Content-Type', value: 'text/plain' }),
concatUnassoc([{ name: 'Content-Type', value: 'text/plain' }]),
assoc('tags', __, ctx)
)(ctx)
)
.map(logger.tap('added pseudo-random string as message "data"'))
))
.map(
(ctx) => pipe(
prop('tags'),
concatUnassoc([{ name: 'SDK', value: 'aoconnect' }]),
assoc('tags', __, ctx)
)(ctx)
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions connect/src/lib/message/upload-message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe('upload-message', () => {
assert.equal(processId, 'process-asdf')
assert.deepStrictEqual(tags, [
{ name: 'foo', value: 'bar' },
{ name: 'Content-Type', value: 'text/plain' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Message' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Content-Type', value: 'text/plain' }
{ name: 'Type', value: 'Message' }
])
assert.equal(anchor, 'idempotent-123')
assert.ok(signer)
Expand Down
35 changes: 21 additions & 14 deletions connect/src/lib/spawn/upload-process.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { fromPromise, of, Resolved } from 'hyper-async'
import { z } from 'zod'
import { __, always, append, assoc, concat, defaultTo, ifElse, pipe, prop } from 'ramda'
import { __, always, assoc, curry, defaultTo, ifElse, pipe, prop } from 'ramda'
import { proto } from '@permaweb/protocol-tag-utils'

import { deployProcessSchema, signerSchema } from '../../dal.js'
import { removeTagsByNameMaybeValue } from '../utils.js'

const aoProto = proto('ao')
const removeAoProtoByName = curry(aoProto.removeAllByName)
const concatAoProto = curry(aoProto.concat)
const concatUnassoc = curry(aoProto.concatUnassoc)

const tagSchema = z.array(z.object({
name: z.string(),
Expand All @@ -30,19 +35,15 @@ function buildTagsWith () {
return of(ctx)
.map(prop('tags'))
.map(defaultTo([]))
.map(removeTagsByNameMaybeValue('Data-Protocol', 'ao'))
.map(removeTagsByNameMaybeValue('Variant'))
.map(removeTagsByNameMaybeValue('Type'))
.map(removeTagsByNameMaybeValue('Module'))
.map(removeTagsByNameMaybeValue('Scheduler'))
.map(removeTagsByNameMaybeValue('SDK'))
.map(concat(__, [
{ name: 'Data-Protocol', value: 'ao' },
.map(removeAoProtoByName('Variant'))
.map(removeAoProtoByName('Type'))
.map(removeAoProtoByName('Module'))
.map(removeAoProtoByName('Scheduler'))
.map(concatAoProto([
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Process' },
{ name: 'Module', value: ctx.module },
{ name: 'Scheduler', value: ctx.scheduler },
{ name: 'SDK', value: 'aoconnect' }
{ name: 'Scheduler', value: ctx.scheduler }
]))
.map(tagSchema.parse)
.map(assoc('tags', __, ctx))
Expand Down Expand Up @@ -70,13 +71,19 @@ function buildDataWith ({ logger }) {
.map(
(ctx) => pipe(
prop('tags'),
removeTagsByNameMaybeValue('Content-Type'),
append({ name: 'Content-Type', value: 'text/plain' }),
concatUnassoc([{ name: 'Content-Type', value: 'text/plain' }]),
assoc('tags', __, ctx)
)(ctx)
)
.map(logger.tap('added pseudo-random string as process "data"'))
))
.map(
(ctx) => pipe(
prop('tags'),
concatUnassoc([{ name: 'SDK', value: 'aoconnect' }]),
assoc('tags', __, ctx)
)(ctx)
)
}
}

Expand Down
16 changes: 8 additions & 8 deletions connect/src/lib/spawn/upload-process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ describe('upload-process', () => {
assert.ok(data)
assert.deepStrictEqual(tags, [
{ name: 'foo', value: 'bar' },
{ name: 'Content-Type', value: 'text/plain' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Process' },
{ name: 'Module', value: 'module-id-123' },
{ name: 'Scheduler', value: 'zVkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Content-Type', value: 'text/plain' }
{ name: 'Scheduler', value: 'zVkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro' }
])
assert.ok(signer)

Expand Down Expand Up @@ -53,13 +53,13 @@ describe('upload-process', () => {
const uploadProcess = uploadProcessWith({
deployProcess: async ({ tags }) => {
assert.deepStrictEqual(tags, [
{ name: 'Content-Type', value: 'text/plain' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Process' },
{ name: 'Module', value: 'module-id-123' },
{ name: 'Scheduler', value: 'zVkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Content-Type', value: 'text/plain' }
{ name: 'Scheduler', value: 'zVkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro' }
])

return { res: 'foobar', processId: 'process-id-123' }
Expand All @@ -84,12 +84,12 @@ describe('upload-process', () => {
*/
assert.deepStrictEqual(tags, [
{ name: 'foo', value: 'bar' },
{ name: 'SDK', value: 'aoconnect' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Process' },
{ name: 'Module', value: 'module-id-123' },
{ name: 'Scheduler', value: 'zVkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro' },
{ name: 'SDK', value: 'aoconnect' }
{ name: 'Scheduler', value: 'zVkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro' }
])
return {
res: 'foobar',
Expand Down
25 changes: 2 additions & 23 deletions connect/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
F, T, __, allPass, always, append, assoc, chain, concat, cond, defaultTo,
equals, has, ifElse, includes, is, join, map, pipe, propEq, propOr, reduce, reject
F, T, __, append, assoc, chain, concat, cond, defaultTo,
equals, has, includes, is, join, map, pipe, propOr, reduce
} from 'ramda'
import { ZodError, ZodIssueCode } from 'zod'

Expand Down Expand Up @@ -46,27 +46,6 @@ export function parseTags (rawTags) {
)(rawTags)
}

/**
* Remove tags from the array by name. If value is provided,
* then only remove tags whose both name and value matches.
*
* @param {string} name - the name of the tags to be removed
* @param {string} [value] - the value of the tags to be removed
*/
export function removeTagsByNameMaybeValue (name, value) {
return (tags) => reject(
allPass([
propEq(name, 'name'),
ifElse(
always(value),
propEq(value, 'value'),
T
)
]),
tags
)
}

export function eqOrIncludes (val) {
return cond([
[is(String), equals(val)],
Expand Down
35 changes: 1 addition & 34 deletions connect/src/lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as assert from 'node:assert'

import { z } from 'zod'

import { errFrom, joinUrl, removeTagsByNameMaybeValue } from './utils.js'
import { errFrom, joinUrl } from './utils.js'

describe('utils', () => {
describe('joinUrl', () => {
Expand Down Expand Up @@ -71,37 +71,4 @@ describe('utils', () => {
assert.equal(err.message, 'An error occurred')
})
})

describe('removeTagsByNameMaybeValue', () => {
const tags = [
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Message' },
{ name: 'Type', value: 'Foo' },
{ name: 'SDK', value: 'aoconnect' }
]

test('should remove the tags by name', () => {
assert.deepStrictEqual(
removeTagsByNameMaybeValue('Type')(tags),
[
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'SDK', value: 'aoconnect' }
]
)
})

test('should remove the tags by name and value', () => {
assert.deepStrictEqual(
removeTagsByNameMaybeValue('Type', 'Foo')(tags),
[
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Message' },
{ name: 'SDK', value: 'aoconnect' }
]
)
})
})
})

0 comments on commit ea9c8f4

Please sign in to comment.