v1rtl
v1rtl
DDeno
Created by v1rtl on 11/29/2023 in #help
"A fetch response body was created during the test" when invoking a PUT request in a test?
I'm writing a Deno library for uploading files on IPFS. The code is very simple:
export const uploadCar = async ({ file, url }) => {
const res = await fetch(url, {
method: 'PUT',
body: file,
headers: { 'x-amz-meta-import': 'car' },
})

return res.headers.get('x-amz-meta-cid')!
}
export const uploadCar = async ({ file, url }) => {
const res = await fetch(url, {
method: 'PUT',
body: file,
headers: { 'x-amz-meta-import': 'car' },
})

return res.headers.get('x-amz-meta-cid')!
}
And this is the test file I have:
import { CAREncoderStream, createFileEncoderStream } from 'https://esm.sh/ipfs-car@1.0.0'
import { CID } from 'https://esm.sh/multiformats@12.1.3/cid'

async function streamToBlob(stream: ReadableStream): Promise<Blob> {
const reader = stream.getReader()
const chunks: Uint8Array[] = []

while (true) {
const { done, value } = await reader.read()
if (done) break
chunks.push(value)
}

return new Blob(chunks, { type: 'application/octet-stream' })
}

describe('uploadCar', () => {
it(
'should upload a CAR file',
async () => {
const stream = createFileEncoderStream(new Blob(['Hello ipfs-car!']))
.pipeThrough(
new TransformStream(),
)
.pipeThrough(new CAREncoderStream([placeholderCID]))

const blob = await streamToBlob(stream)

const file = new File([blob], 'file.car')

const cid = await uploadCar({ file, ...args })

assertEquals(cid, 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi')
},
)
})
import { CAREncoderStream, createFileEncoderStream } from 'https://esm.sh/ipfs-car@1.0.0'
import { CID } from 'https://esm.sh/multiformats@12.1.3/cid'

async function streamToBlob(stream: ReadableStream): Promise<Blob> {
const reader = stream.getReader()
const chunks: Uint8Array[] = []

while (true) {
const { done, value } = await reader.read()
if (done) break
chunks.push(value)
}

return new Blob(chunks, { type: 'application/octet-stream' })
}

describe('uploadCar', () => {
it(
'should upload a CAR file',
async () => {
const stream = createFileEncoderStream(new Blob(['Hello ipfs-car!']))
.pipeThrough(
new TransformStream(),
)
.pipeThrough(new CAREncoderStream([placeholderCID]))

const blob = await streamToBlob(stream)

const file = new File([blob], 'file.car')

const cid = await uploadCar({ file, ...args })

assertEquals(cid, 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi')
},
)
})
When running this test this is what Im getting:
uploadCar ... should upload a CAR file => https://deno.land/std@0.207.0/testing/_test_suite.ts:323:15
error: Leaking resources:
- A fetch response body (rid 18) was created during the test, but not consumed during the test. Consume or close the response body `ReadableStream`, e.g `await resp.text()` or `await resp.body.cancel()`.
uploadCar ... should upload a CAR file => https://deno.land/std@0.207.0/testing/_test_suite.ts:323:15
error: Leaking resources:
- A fetch response body (rid 18) was created during the test, but not consumed during the test. Consume or close the response body `ReadableStream`, e.g `await resp.text()` or `await resp.body.cancel()`.
I couldn't find much info about the issue - what could be a possible fix?
5 replies
DDeno
Created by v1rtl on 3/26/2023 in #help
error: BadResource: Bad resource ID
I'm writing tests for https://github.com/deno-libs/tinyhttp/pull/9.
import { describe, expect, it, run } from 'https://deno.land/x/tincan@1.0.1/mod.ts'
import { App } from '../../app.ts'

describe('Request properties', () => {
it.only('should have default HTTP Request properties', async () => {
const app = new App()
app.get('/', (req, res) => {
res.json({ url: req.url })
})
const listener = Deno.listen({ port: 8080, hostname: 'localhost' })

setTimeout(() => {
fetch(`http://localhost:8080`).then((res) => res.json()).then((json) => {
expect(json).toEqual({ url: `http://localhost:8080/` })
})
}, 0)

const conn = await listener.accept()
const requests = Deno.serveHttp(conn)
const { request, respondWith } = (await requests.nextRequest())!
const response = await app.handler(request, conn)
if (response) {
respondWith(response)
// Deno.close(conn.rid)

listener.close()
}
})
})
import { describe, expect, it, run } from 'https://deno.land/x/tincan@1.0.1/mod.ts'
import { App } from '../../app.ts'

describe('Request properties', () => {
it.only('should have default HTTP Request properties', async () => {
const app = new App()
app.get('/', (req, res) => {
res.json({ url: req.url })
})
const listener = Deno.listen({ port: 8080, hostname: 'localhost' })

setTimeout(() => {
fetch(`http://localhost:8080`).then((res) => res.json()).then((json) => {
expect(json).toEqual({ url: `http://localhost:8080/` })
})
}, 0)

const conn = await listener.accept()
const requests = Deno.serveHttp(conn)
const { request, respondWith } = (await requests.nextRequest())!
const response = await app.handler(request, conn)
if (response) {
respondWith(response)
// Deno.close(conn.rid)

listener.close()
}
})
})
When running this test I get either this:
Request properties > should have default HTTP Request properties => https://deno.land/x/tincan@1.0.1/src/runner.ts:166:10
error: BadResource: Bad resource ID
Deno.close(conn.rid)
^
at Object.close (ext:core/01_core.js:396:25)
at ItNode.fn (file:///home/v1rtl/Coding/deno-libs/tinyhttp/tests/core/request.test.ts:29:12)
at async wrappedFn (https://deno.land/x/tincan@1.0.1/src/runner.ts:141:9)
Request properties > should have default HTTP Request properties => https://deno.land/x/tincan@1.0.1/src/runner.ts:166:10
error: BadResource: Bad resource ID
Deno.close(conn.rid)
^
at Object.close (ext:core/01_core.js:396:25)
at ItNode.fn (file:///home/v1rtl/Coding/deno-libs/tinyhttp/tests/core/request.test.ts:29:12)
at async wrappedFn (https://deno.land/x/tincan@1.0.1/src/runner.ts:141:9)
or this, if I uncomment that line:
error: AssertionError: Test case is leaking 1 resource:
- An inbound HTTP connection (rid 8) was accepted during the test, but not closed during the test. Close the inbound HTTP connection by calling `httpConn.close()`.
at assert (ext:deno_web/00_infra.js:353:11)
at resourceSanitizer (ext:cli/40_testing.js:414:5)
at async Object.exitSanitizer [as fn] (ext:cli/40_testing.js:432:7)
error: AssertionError: Test case is leaking 1 resource:
- An inbound HTTP connection (rid 8) was accepted during the test, but not closed during the test. Close the inbound HTTP connection by calling `httpConn.close()`.
at assert (ext:deno_web/00_infra.js:353:11)
at resourceSanitizer (ext:cli/40_testing.js:414:5)
at async Object.exitSanitizer [as fn] (ext:cli/40_testing.js:432:7)
10 replies
DDeno
Created by v1rtl on 2/27/2023 in #help
could not find npm package for
I'm trying to make Hardhat CLI work with Deno but I stumbled upon this error which comes from Deno: I initialized a new hh project pnpx hardhat init then installed hh as a deno binary:
deno install -Afq npm:hardhat
deno install -Afq npm:hardhat
and got this:
HARDHAT_EXPERIMENTAL_ALLOW_NON_LOCAL_INSTALLATION=true hardhat --help
Error 'could not find npm package for 'file:///tmp/hh/node_modules/@nomicfoundation/hardhat-toolbox/dist/src/index.js'' contains boxed error of unknown type:
"could not find npm package for 'file:///tmp/hh/node_modules/@nomicfoundation/hardhat-toolbox/dist/src/index.js'"
An unexpected error occurred:

Error: could not find npm package for 'file:///tmp/hh/node_modules/@nomicfoundation/hardhat-toolbox/dist/src/index.js'
at Object.Module._extensions..js (internal:ext/node/02_require.js:766:28)
at Module.load (internal:ext/node/02_require.js:658:34)
at Function.Module._load (internal:ext/node/02_require.js:515:14)
at Module.require (internal:ext/node/02_require.js:680:21)
at require (internal:ext/node/02_require.js:820:18)
at Object.<anonymous> (file:///tmp/hh/hardhat.config.js:1:253)
at Object.<anonymous> (file:///tmp/hh/hardhat.config.js:8:4)
at Module._compile (internal:ext/node/02_require.js:747:36)
at Object.Module._extensions..js (internal:ext/node/02_require.js:780:12)
at Module.load (internal:ext/node/02_require.js:658:34)
HARDHAT_EXPERIMENTAL_ALLOW_NON_LOCAL_INSTALLATION=true hardhat --help
Error 'could not find npm package for 'file:///tmp/hh/node_modules/@nomicfoundation/hardhat-toolbox/dist/src/index.js'' contains boxed error of unknown type:
"could not find npm package for 'file:///tmp/hh/node_modules/@nomicfoundation/hardhat-toolbox/dist/src/index.js'"
An unexpected error occurred:

Error: could not find npm package for 'file:///tmp/hh/node_modules/@nomicfoundation/hardhat-toolbox/dist/src/index.js'
at Object.Module._extensions..js (internal:ext/node/02_require.js:766:28)
at Module.load (internal:ext/node/02_require.js:658:34)
at Function.Module._load (internal:ext/node/02_require.js:515:14)
at Module.require (internal:ext/node/02_require.js:680:21)
at require (internal:ext/node/02_require.js:820:18)
at Object.<anonymous> (file:///tmp/hh/hardhat.config.js:1:253)
at Object.<anonymous> (file:///tmp/hh/hardhat.config.js:8:4)
at Module._compile (internal:ext/node/02_require.js:747:36)
at Object.Module._extensions..js (internal:ext/node/02_require.js:780:12)
at Module.load (internal:ext/node/02_require.js:658:34)
p.s. I use that flag cuz here: https://github.com/NomicFoundation/hardhat/blob/main/packages/hardhat-core/src/internal/cli/cli.ts#L175
2 replies
DDeno
Created by v1rtl on 1/28/2023 in #help
Republish a /x/ module from a deleted repo
Is it possible to publish an old module from a new repo? i'v deleted an old one and created a new repo with module's source.
3 replies
DDeno
Created by v1rtl on 11/7/2022 in #help
Closing a websocket server in a unit test
Hello, I'm an author of rpc lib and I'm currently writing unit tests for it. I am facing a problem of closing a server. How should I do it without deno hanging forever? my test case:
import { send, App } from './mod.ts'

Deno.test('send', async (t) => {
await t.step('should send a message with websocket', async () => {
const app = new App()
await app.listen({ port: 8881, hostname: 'localhost' })

const socket = new WebSocket('ws://localhost:8881')
socket.onopen = () => {
send(socket, { hello: 'world' })
socket.close()
app.close()
}
})
})
import { send, App } from './mod.ts'

Deno.test('send', async (t) => {
await t.step('should send a message with websocket', async () => {
const app = new App()
await app.listen({ port: 8881, hostname: 'localhost' })

const socket = new WebSocket('ws://localhost:8881')
socket.onopen = () => {
send(socket, { hello: 'world' })
socket.close()
app.close()
}
})
})
app.listen here both starts the Deno.listener and Deno.serveHttp
1 replies