Skip to content

Commit

Permalink
ref(ipos): /pinFile car file to v0 cid
Browse files Browse the repository at this point in the history
  • Loading branch information
hassnian committed Sep 27, 2024
1 parent 8aa6e5e commit b52f180
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"engines": {
"pnpm": ">=9",
"node": ">=20"
},
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
}
}
}
12 changes: 12 additions & 0 deletions patches/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/dist/esm/pack/index.js b/dist/esm/pack/index.js
index 97e262734c587f31b89aeaf370d11bb4760f33f0..fe0f34ce16a9a5a79174a0d9f7a33c6d63a9e637 100644
--- a/dist/esm/pack/index.js
+++ b/dist/esm/pack/index.js
@@ -13,6 +13,7 @@ export async function pack({ input, blockstore: userBlockstore, hasher, maxChunk
// Consume the source
const rootEntry = await last(pipe(getNormaliser(input), (source) => importer(source, blockstore, {
...unixfsImporterOptionsDefault,
+ cidVersion: 0,
hasher: hasher || unixfsImporterOptionsDefault.hasher,
maxChunkSize: maxChunkSize || unixfsImporterOptionsDefault.maxChunkSize,
maxChildrenPerNode: maxChildrenPerNode || unixfsImporterOptionsDefault.maxChildrenPerNode,
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion services/ipos/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"enabled": true
},
"files": {
"ignore": ["node_modules", ".wrangler", ".papi"]
"ignore": ["node_modules", ".wrangler", ".papi", "patches"]
},
"linter": {
"enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion services/ipos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@aws-sdk/client-s3": "^3.609.0",
"@hono/valibot-validator": "^0.2.5",
"hono": "^4.4.12",
"ipfs-car": "^0.9.0",
"ipfs-car": "0.9.2",
"ipfs-only-hash": "^4.0.0",
"ipfs-unixfs-importer": "^15.2.5",
"multiformats": "^13.1.3",
Expand Down
9 changes: 3 additions & 6 deletions services/ipos/src/routes/pinning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ app.post('/pinFile', vValidator('form', pinFileRequestSchema), async (c) => {
const s3 = getS3(c)

let cid: string
let file: Uint8Array | File
let size: number
let file: Blob | File
let type: string

if (files.length > 1) {
Expand All @@ -78,9 +77,8 @@ app.post('/pinFile', vValidator('form', pinFileRequestSchema), async (c) => {
})),
)

cid = root.toString() as string
cid = root.toString()
file = car
size = file.byteLength
type = 'directory'

await s3.putObject({
Expand All @@ -97,7 +95,6 @@ app.post('/pinFile', vValidator('form', pinFileRequestSchema), async (c) => {
cid = (await hashOf(content)).toV0().toString()

file = f
size = f.size
type = f.type

await s3.putObject({
Expand All @@ -114,7 +111,7 @@ app.post('/pinFile', vValidator('form', pinFileRequestSchema), async (c) => {
getPinResponse({
cid: cid,
type: type,
size: size,
size: file.size,
}),
)
})
Expand Down
19 changes: 5 additions & 14 deletions services/ipos/src/utils/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { MemoryBlockStore } from 'ipfs-car/blockstore/memory'
import { pack } from 'ipfs-car/pack'
// Patch: `ipfs-car` library has been patched to work with CID v0 which is not supported by the original implementation.
// see `import { pack } from 'ipfs-car/pack'`
import { packToBlob } from 'ipfs-car/pack/blob'
import type { CID } from 'multiformats'

export default async function toCar(
input: { path: string; content: Uint8Array }[],
) {
const { root, out } = await pack({
const { root, car } = await packToBlob({
input: input,
blockstore: new MemoryBlockStore(),
wrapWithDirectory: true,
})

const chunks = []
for await (const chunk of out) {
chunks.push(chunk)
}

const car = new Uint8Array(
chunks.reduce((acc, chunk) => acc.concat(Array.from(chunk)), []),
)

return {
root: root as CID,
car,
car: car as Blob,
}
}

0 comments on commit b52f180

Please sign in to comment.