forked from storacha/w3up
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
47 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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createServer } from 'node:http' | ||
import { | ||
createUcantoServer, | ||
getContentServeMockService, | ||
} from '../mocks/service.js' | ||
import { gateway } from '../../../upload-api/test/helpers/utils.js' | ||
|
||
const port = 5001 | ||
|
||
const server = createServer(async (req, res) => { | ||
res.setHeader('Access-Control-Allow-Origin', '*') | ||
res.setHeader('Access-Control-Allow-Methods', '*') | ||
res.setHeader('Access-Control-Allow-Headers', '*') | ||
if (req.method === 'OPTIONS') return res.end() | ||
|
||
if (req.method === 'POST') { | ||
const service = getContentServeMockService() | ||
const server = createUcantoServer(gateway, service) | ||
|
||
const bodyBuffer = Buffer.concat(await collect(req)) | ||
|
||
const reqHeaders = /** @type {Record<string, string>} */ ( | ||
Object.fromEntries(Object.entries(req.headers)) | ||
) | ||
|
||
const { headers, body, status } = await server.request({ | ||
body: new Uint8Array( | ||
bodyBuffer.buffer, | ||
bodyBuffer.byteOffset, | ||
bodyBuffer.byteLength | ||
), | ||
headers: reqHeaders, | ||
}) | ||
|
||
for (const [key, value] of Object.entries(headers)) { | ||
res.setHeader(key, value) | ||
} | ||
res.writeHead(status ?? 200) | ||
res.end(body) | ||
} | ||
res.end() | ||
}) | ||
|
||
/** @param {import('node:stream').Readable} stream */ | ||
const collect = (stream) => { | ||
return /** @type {Promise<Buffer[]>} */ ( | ||
new Promise((resolve, reject) => { | ||
const chunks = /** @type {Buffer[]} */ ([]) | ||
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk))) | ||
stream.on('error', (err) => reject(err)) | ||
stream.on('end', () => resolve(chunks)) | ||
}) | ||
) | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
server.listen(port, () => | ||
console.log(`[Mock] Gateway Server Listening on :${port}`) | ||
) | ||
|
||
process.on('SIGTERM', () => process.exit(0)) |
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