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

fix: delegations model tries to handle if row.bytes is Array not Buffer (e.g. cloudflare) #478

Merged
merged 4 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions packages/access-api/src/models/delegations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
delegationsToBytes,
bytesToDelegations,
} from '@web3-storage/access/encoding'
import { isBuffer } from '../utils/common.js'

/**
* @typedef {import('@web3-storage/access/src/types').DelegationTable} DelegationRow
Expand Down Expand Up @@ -162,3 +163,20 @@ export function createDelegationRowUpdate(d) {
bytes: delegationsToBytes([d]),
}
}

/**
* @param {Array<number> | Buffer | unknown} sqlValue - value from kysely 'bytes' table - in node it could be a Buffer. In cloudflare it might be an Array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value from kysely 'bytes' table

Not sure I understand this comment! Isn't kysely the client? What is a "bytes" table?

* @returns {ArrayBuffer|undefined} - undefined if unable to convert
*/
export function delegationsTableBytesToArrayBuffer(sqlValue) {
if (ArrayBuffer.isView(sqlValue)) {
return new Uint8Array(
sqlValue.buffer,
sqlValue.byteOffset,
sqlValue.byteLength
)
}
if (Array.isArray(sqlValue)) {
return Uint8Array.from(sqlValue)
}
}
2 changes: 1 addition & 1 deletion packages/access-api/src/service/voucher-redeem.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function voucherRedeemProvider(ctx) {
)

if (error) {
if (error.code === 'SQLITE_CONSTRAINT_PRIMARYKEY') {
if ('code' in error && error.code === 'SQLITE_CONSTRAINT_PRIMARYKEY') {
return new Failure(`Space ${capability.nb.space} already registered.`)
} else {
throw error
Expand Down
13 changes: 11 additions & 2 deletions packages/access-api/src/utils/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { Validations } from '../models/validations.js'
import * as Email from './email.js'
import { createUploadApiConnection } from '../service/upload-api-proxy.js'
import { DID } from '@ucanto/core'
import { DbDelegationsStorage } from '../models/delegations.js'
import {
DbDelegationsStorage,
delegationsTableBytesToArrayBuffer,
} from '../models/delegations.js'
import { createD1Database } from './d1.js'

/**
Expand Down Expand Up @@ -64,7 +67,13 @@ export function getContext(request, env, ctx) {
config,
url,
models: {
delegations: new DbDelegationsStorage(createD1Database(config.DB)),
delegations: new DbDelegationsStorage(
createD1Database(config.DB, {
bytes: (v) => {
return delegationsTableBytesToArrayBuffer(v) ?? v
},
})
),
spaces: new Spaces(config.DB),
validations: new Validations(config.VALIDATIONS),
accounts: new Accounts(config.DB),
Expand Down
4 changes: 3 additions & 1 deletion packages/access-api/src/utils/d1.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ export class D1Error extends Error {
/**
* @template S
* @param {D1Database} d1
* @param {Record<string, (v: unknown) => unknown>} [resultTransforms]
* @returns {import('../types/database.js').Database<S>}
*/
export function createD1Database(d1) {
export function createD1Database(d1, resultTransforms = {}) {
/** @type {Kysely<S>} */
const kdb = new Kysely({
dialect: new D1Dialect({ database: d1 }),
Expand All @@ -154,6 +155,7 @@ export function createD1Database(d1) {
expires_at: (v) => (typeof v === 'string' ? new Date(v) : null),
inserted_at: (v) => new Date(v),
updated_at: (v) => new Date(v),
...resultTransforms,
}),
],
})
Expand Down