Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSN Integration #94

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified backend/.yarn/install-state.gz
Binary file not shown.
53 changes: 53 additions & 0 deletions backend/migrations/20241114152906-dsn-intergration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20241114152906-dsn-intergration-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20241114152906-dsn-intergration-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Replace with your SQL commands */
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE nodes;

ALTER TABLE uploads.blockstore ADD COLUMN "status" TEXT;
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"pg-format": "^1.0.4",
"pizzip": "^3.1.7",
"uuid": "^10.0.0",
"websocket": "^1.0.35",
"zod": "^3.23.8"
},
"devDependencies": {
Expand All @@ -40,6 +41,7 @@
"@types/pg": "^8.11.10",
"@types/pg-format": "^1",
"@types/uuid": "^10",
"@types/websocket": "^1",
"@typescript-eslint/eslint-plugin": "^8.13.0",
"@typescript-eslint/parser": "^8.13.0",
"blockstore-core": "^5.0.2",
Expand Down
92 changes: 92 additions & 0 deletions backend/src/drivers/ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import Websocket from 'websocket'

type RPCMessage = {
jsonrpc: string
method: string
params: unknown
id: number
}

export type WS = {
send: (message: Omit<RPCMessage, 'id'>) => Promise<unknown>
on: (callback: (event: RPCMessage) => void) => void
off: (callback: (event: RPCMessage) => void) => void
}

export const createWS = (endpoint: string): WS => {
let ws: Websocket.w3cwebsocket
let onMessageCallbacks: ((event: RPCMessage) => void)[] = []

const handleConnection = () => {
ws = new Websocket.w3cwebsocket(endpoint)

ws.onerror = (event) => {
const errorDetails = {
readyState: ws.readyState,
url: endpoint,
message: event.message || 'Unknown error',
}

console.error('WebSocket connection error', errorDetails)

setTimeout(() => {
console.log(`Reconnecting to RPC Web Socket (${endpoint})`)
handleConnection()
}, 10_000)
}

ws.onmessage = (event) => {
console.debug(`Received message from WebSocket (${endpoint})`)
onMessageCallbacks.forEach((callback) =>
callback(JSON.parse(event.data.toString())),
)
}

ws.onclose = (event) => {
console.log(
`WebSocket connection closed (${event.code}) due to ${event.reason}.`,
)
}
}

handleConnection()

const connected: Promise<void> = new Promise((resolve) => {
ws.onopen = () => {
console.log(`Connected to RPC Web Socket (${endpoint})`)
resolve()
}
})

const send = async (message: Omit<RPCMessage, 'id'>) => {
await connected

const id = Math.floor(Math.random() * 65546)
const messageWithID = { ...message, id }

return new Promise((resolve, reject) => {
const cb = (event: RPCMessage) => {
try {
if (event.id === id) {
off(cb)
resolve(event)
}
} catch (error) {
reject(error)
}
}
on(cb)

ws.send(JSON.stringify(messageWithID))
})
}

const on = (callback: (event: RPCMessage) => void) => {
onMessageCallbacks.push(callback)
}
const off = (callback: (event: RPCMessage) => void) => {
onMessageCallbacks = onMessageCallbacks.filter((cb) => cb !== callback)
}

return { send, on, off }
}
8 changes: 4 additions & 4 deletions backend/src/models/uploads/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export enum UploadType {
}

export enum UploadStatus {
PENDING = 'pending',
UPLOADING = 'uploading',
UPLOADED = 'uploaded',
PUBLISHING = 'publishing',
ARCHIVING = 'archiving',
COMPLETED = 'completed',
MIGRATING = 'migrating',
CANCELLED = 'cancelled',
FAILED = 'failed',
}

export const uploadOptionsSchema = z.object({
Expand Down
1 change: 0 additions & 1 deletion backend/src/repositories/objects/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './interactions.js'
export * from './metadata.js'
export * from './nodes.js'
export * from './ownership.js'
export * from './transactionResults.js'
112 changes: 0 additions & 112 deletions backend/src/repositories/objects/nodes.ts

This file was deleted.

8 changes: 4 additions & 4 deletions backend/src/repositories/objects/transactionResults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getDatabase } from '../../drivers/pg.js'
import { TransactionResult } from '../../models/objects/transaction.js'
import { Node } from './nodes.js'
import { BlockstoreEntry } from '../uploads/blockstore.js'

export interface TransactionResultEntry {
cid: string
Expand Down Expand Up @@ -49,9 +49,9 @@ const getHeadTransactionResults = async (head_cid: string) => {
const getPendingUploads = async (limit: number = 100) => {
const db = await getDatabase()
const result = await db
.query<Node>(
.query<BlockstoreEntry>(
`
SELECT n.* FROM nodes n left join transaction_results tr on n.cid = tr.cid where tr.cid is null limit $1
SELECT n.* FROM uploads.blockstore n left join transaction_results tr on n.cid = tr.cid where tr.cid is null limit $1
`,
[limit],
)
Expand All @@ -78,7 +78,7 @@ const getUploadedNodesByRootCid = async (rootCid: string) => {
const db = await getDatabase()
const result = await db
.query<TransactionResultEntry>({
text: `select tr.* from transaction_results tr join nodes n on tr.cid = n.cid where n.root_cid = $1 and tr.transaction_result->>'blockNumber' is not null order by tr.transaction_result->>'blockNumber' asc
text: `select * from uploads.uploads u join uploads.blockstore b on u.id = b.upload_id and u.root_upload_id = u.id where b.cid = $1
`,
values: [rootCid],
})
Expand Down
Loading
Loading