-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement validators into schema validation service
- Loading branch information
Showing
14 changed files
with
323 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,97 @@ | ||
import { deterministicHash } from '@nostrwatch/nip66/utils/hash'; | ||
import type { NostrEvent } from 'nostr-tools'; | ||
import { get } from 'svelte/store'; | ||
import { EventEmitter } from 'tseep' | ||
|
||
export type SchemaValidationServiceRequest = { | ||
type: 'nip11' | 'message' | 'note', | ||
json: string, | ||
subject?: string, | ||
slug?: string, | ||
hash?: string | ||
} | ||
|
||
export type SchemaValidationServiceResponse = { | ||
status: 'success' | 'error', | ||
result: string, | ||
hash: string; | ||
error?: any | ||
} | ||
|
||
export class SchemaValidationService { | ||
worker: Worker; | ||
private _subIds: Set<string> = new Set(); | ||
private emitter = new EventEmitter(); | ||
|
||
constructor(){ | ||
this.worker = new Worker(new URL('./schemavalidation.worker.ts', import.meta.url), { type: 'module' }); | ||
this.worker.onmessage = this.onmessage.bind(this); | ||
} | ||
|
||
get subIds(): Set<string> { | ||
return this._subIds; | ||
} | ||
|
||
emitterKey(hash: string) { | ||
return `schemaValidation:${hash}` | ||
} | ||
|
||
async respond(hash: string): Promise<SchemaValidationServiceResponse> { | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout( () => reject({ status: 'error', hash, error: 'Request timed out, worker may have been terminated.' }), 5000) | ||
this.emitter.once(this.emitterKey(hash), (response: SchemaValidationServiceResponse) => { | ||
clearTimeout(timeout) | ||
const { result, error } = response; | ||
if(error) { | ||
reject(response) | ||
} | ||
else { | ||
resolve(response) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
async validate(request: SchemaValidationServiceRequest, hash?: string): Promise<SchemaValidationServiceResponse> { | ||
hash = hash ?? deterministicHash(request.json) | ||
this._subIds.add(hash) | ||
this.worker.postMessage(request) | ||
return this.respond(hash) | ||
} | ||
|
||
constructor(){} | ||
async validateNip11(nip11: string): Promise<SchemaValidationServiceResponse> { | ||
const request: SchemaValidationServiceRequest = { | ||
type: 'nip11', | ||
json: nip11 | ||
} | ||
return this.validate(request) | ||
} | ||
|
||
async validateMessage(json: string, subject: string, slug: string): Promise<SchemaValidationServiceResponse> { | ||
const request: SchemaValidationServiceRequest = { | ||
type: 'message', | ||
subject, | ||
slug, | ||
json | ||
} | ||
return this.validate(request) | ||
} | ||
|
||
async validateNote(json: any): Promise<SchemaValidationServiceResponse> { | ||
let hash: string | undefined; | ||
if(json?.id) { | ||
hash = json.id; | ||
} | ||
const request: SchemaValidationServiceRequest = { | ||
type: 'note', | ||
json, | ||
hash | ||
} | ||
return this.validate(request, hash) | ||
} | ||
|
||
private onmessage(message: MessageEvent<SchemaValidationServiceResponse>){ | ||
const { hash } = message.data; | ||
this.emitter.emit(this.emitterKey(hash), message.data) | ||
} | ||
} |
55 changes: 39 additions & 16 deletions
55
apps/gui/src/lib/services/SchemaValidationService/schemavalidation.worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
import Ajv from 'ajv' | ||
|
||
const validate = async (relay: string, checks: string[]): Promise<any> => { | ||
const nocap = new Nocap(relay) | ||
return nocap.check(checks) | ||
} | ||
import { deterministicHash } from '@nostrwatch/nip66/utils/hash'; | ||
import { type SchemaValidatorResult } from '@nostrwatch/schemata-js-ajv' | ||
|
||
self.onmessage = ({ data }) => { | ||
const { relay, checks } = data as NocapRequestMessage; | ||
check(relay, checks) | ||
.then( (results: any) => { | ||
const message: NocapResultMessage = {relay, results} | ||
self.postMessage(message) | ||
}) | ||
.catch( (error: any) => { | ||
const message: NocapResultMessage = {relay, error} | ||
self.postMessage(message) | ||
}) | ||
import('@nostrwatch/schemata-js-ajv').then( ({validateNip11, validateMessage, validateNote}) => { | ||
const { json, type, subject, slug } = data as any; | ||
let { hash } = data as any; | ||
let result: SchemaValidatorResult; | ||
let error: string = ''; | ||
if(!hash) { | ||
hash = deterministicHash(json) | ||
} | ||
if(type === 'nip11') { | ||
result = validateNip11(json) | ||
} | ||
else if(type === 'message') { | ||
if(subject && slug) { | ||
result = validateMessage(json, subject, slug) | ||
} | ||
else { | ||
error = 'Both subject and slug are required for message validation (for example subject "relay" and slug "ok"' | ||
} | ||
} | ||
else if(type === 'note') { | ||
result = validateNote(json) | ||
} | ||
if(result) { | ||
self.postMessage({ | ||
status: 'success', | ||
hash, | ||
result | ||
}) | ||
} | ||
else if(error){ | ||
self.postMessage({ | ||
status: 'error', | ||
hash, | ||
error | ||
}) | ||
} | ||
}); | ||
} |
Oops, something went wrong.