Skip to content

Commit

Permalink
fix: update content type and use kind type index
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Aug 21, 2024
1 parent f90e3c3 commit 8342c30
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 575 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"license": "GPL-3.0",
"devDependencies": {
"typescript": "latest",
"vitest": "^1.2.2"
"vitest": "^2.0.5"
},
"dependencies": {
"@types/node": "^20.11.10",
"json5": "2.2.3",
"prettier": "3.2.4",
"prettier": "3.3.3",
"rosetta": "1.1.0"
},
"repository": {
Expand Down
11 changes: 9 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import { OperatorCode, OutputType, Kind } from './types.js'
export const DEFAULT_OPERATOR = OperatorCode.ArrayCount
export const DEFAULT_INPUT_TYPE = OutputType.Array
export const DEFAULT_SCRIPT_FIRST_TYPE = OutputType.String
export const KIND_OPTIONS = [Kind.HttpGet, Kind.RNG, Kind.HttpPost]
export const KIND_OPTIONS = {
[Kind.HttpGet]: 'HTTP-GET',
[Kind.HttpPost]: 'HTTP-POST',
[Kind.RNG]: 'RNG',
// TODO: Support HTTP-HEAD
// [Kind.HttpHead]: 'HTTP-HEAD',
}
export const CONTENT_TYPE_OPTIONS = {
[Kind.HttpGet]: 'JSON API',
[Kind.HttpHead]: 'JSON API',
[Kind.HttpPost]: 'JSON API',
[Kind.RNG]: 'Binary file',
// TODO: Support HTTP-HEAD
// [Kind.HttpHead]: 'JSON API',
}
export const DEFAULT_KIND_OPTION = Kind.HttpGet
6 changes: 2 additions & 4 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import rosetta, { Rosetta } from 'rosetta'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const en = require('../src/locales/en.json')
const es = require('../src/locales/es.json')
import en from '../src/locales/en.json' assert { type: 'json' }
import es from '../src/locales/es.json' assert { type: 'json' }

export type Locale = 'en' | 'es'

Expand Down
16 changes: 3 additions & 13 deletions src/source.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
MarkupSource,
MirSource,
OutputType,
Context,
KindOptions,
Kind,
EventEmitter,
ContentTypeOptions,
} from './types.js'
import { MarkupSource, MirSource, OutputType, Context, Kind, EventEmitter } from './types.js'
import { KIND_OPTIONS, CONTENT_TYPE_OPTIONS } from './constants.js'
import { Cache } from './structures.js'
import { Script } from './script.js'
Expand All @@ -16,10 +7,10 @@ import { OutgoingHttpHeaders } from 'http'

export class Source {
public kind: Kind
public kindOptions: KindOptions
public kindOptions: { [key in Kind]: string }
public url: string
public contentType: string
public contentTypeOptions: ContentTypeOptions
public contentTypeOptions: { [key in Kind]: string }
public headers: OutgoingHttpHeaders
public body?: object
public script: Script
Expand Down Expand Up @@ -89,7 +80,6 @@ export class Source {

public updateSourceType(sourceType: Kind) {
this.kind = sourceType
this.contentType = CONTENT_TYPE_OPTIONS[sourceType]
this.script.updateSourceType(sourceType)
if (this.kind === Kind.RNG) {
this.url = ''
Expand Down
24 changes: 9 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ export type MarkupArgumentScript = {

export type MarkupSource = {
kind: Kind
kindOptions: KindOptions
kindOptions: { [key in Kind]: string }
headers: OutgoingHttpHeaders
body?: object
url: string
contentType: string
contentTypeOptions: ContentTypeOptions
contentTypeOptions: { [key in Kind]: string }
script: MarkupScript
scriptId: number
}
Expand All @@ -216,13 +216,12 @@ export type Markup = {
radRequest: MarkupRequest
}

export type KindOptions = Array<Kind>

export enum Kind {
HttpGet = 'HTTP-GET',
HttpPost = 'HTTP-POST',
HttpHead = 'HTTP-HEAD',
RNG = 'RNG',
HttpGet = 0x01,
HttpPost = 0x02,
RNG = 0x03,
// TODO: suppport Http Head requests
// HttpHead = 0x04,
}

export enum OperatorCode {
Expand Down Expand Up @@ -375,18 +374,13 @@ export type MirSource = {
kind: Kind
headers: OutgoingHttpHeaders
body?: object
kindOptions: KindOptions
kindOptions: { [key in Kind]: string }
url: string
contentType: string
contentTypeOptions: ContentTypeOptions
contentTypeOptions: { [key in Kind]: string }
script: MirScript
}

export type ContentTypeOptions = {
[Kind.HttpGet]: string | Array<string>
[Kind.RNG]: string
}

export type MirRequest = {
timelock: number
retrieve: Array<MirSource>
Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as prettier from 'prettier'
import { format } from 'prettier/standalone'
import * as prettierPluginBabel from 'prettier/plugins/babel'
import prettierPluginEstree from 'prettier/plugins/estree'
import {
ArgumentInfo,
MarkupInputType,
Expand Down Expand Up @@ -57,7 +59,11 @@ export function fromOutputTypeToType(type: OutputType): Type | null {
}

export async function formatJs(source: string): Promise<string> {
return prettier.format(source, { semi: false, parser: 'babel' })
return await format(source, {
semi: false,
parser: 'babel',
plugins: [prettierPluginBabel, prettierPluginEstree],
})
}

export function getEnumNames(e: any): Array<any> {
Expand Down
42 changes: 21 additions & 21 deletions test/src/createRngRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[DEFAULT_KIND_OPTION],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [OperatorCode.StringAsBoolean, OperatorCode.BooleanNegate],
},
Expand All @@ -31,7 +31,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_2',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[DEFAULT_KIND_OPTION],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [OperatorCode.StringAsBoolean, OperatorCode.BooleanNegate],
},
Expand All @@ -55,7 +55,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.retrieve[0].script.operators.length).toBe(0)
})
Expand All @@ -100,7 +100,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.aggregate.filters.length).toBe(0)
})
Expand All @@ -110,7 +110,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.retrieve[0].url).toBe('')
})
Expand All @@ -125,7 +125,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMarkup().retrieve[0].script.length).toBe(0)
})
Expand All @@ -170,7 +170,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMarkup().aggregate.filters.length).toBe(0)
})
Expand All @@ -180,7 +180,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMarkup().retrieve[0].url).toBe('')
})
Expand All @@ -195,7 +195,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand All @@ -233,7 +233,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand All @@ -252,9 +252,9 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.HttpGet,
url: '',
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.HttpGet],
})
expect(radon.getMir().retrieve[0].kind).toBe('HTTP-GET')
expect(radon.getMir().retrieve[0].kind).toBe(1)
})
it('creates an aggregation/tally script with RNG source', () => {
const mirScript: MirAggregationTallyScript = {
Expand All @@ -271,7 +271,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().retrieve[0].script.length).toBe(0)
})
Expand All @@ -281,7 +281,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().retrieve.length).toBe(1)
})
Expand All @@ -291,7 +291,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().aggregate.filters.length).toBe(0)
})
Expand All @@ -301,7 +301,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().retrieve[0].url).toBe('')
})
Expand All @@ -316,7 +316,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'RNG',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
const expected = await formatJsTest(`import * as Witnet from "witnet-requests"
const request = new Witnet.Request()
Expand Down
5 changes: 3 additions & 2 deletions test/src/radon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3610,10 +3610,11 @@ describe('Radon', () => {
}

const radon = new Radon(mirRequest)
radon.updateSource(0, { kind: 'new_kind', url: 'new_url' })
radon.updateSource(0, { kind: 2, url: 'new_url', contentType: 'new_content_type' })
const updatedSource = radon.retrieve[0]
expect(updatedSource.url).toBe('new_url')
expect(updatedSource.kind).toBe('new_kind')
expect(updatedSource.kind).toBe(2)
expect(updatedSource.contentType).toBe('new_content_type')
})
})

Expand Down
Loading

0 comments on commit 8342c30

Please sign in to comment.