-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(w3up-client): add default gateway authorization (#1604)
### Context While updating the `web3-storage` docs, `w3cli`, and `@storacha/client`, I found it inconvenient to repeatedly define the gateway DID and URL every time I created a space. Also, if we don't provide the `skipGatewayAuthorization=true`, it will throw an error saying it is required to provide the gateway services to authorize. ### Changes Following @travis suggestion (see: storacha/docs#21 (comment)), I've updated the client to automatically authorize the Storacha Gateway (Production) by default if `skipGatewayAuthorization` is not set to `true` and `authorizeGatewayServices` is not provided or is empty. Once approved, this change will need to be ported to [upload-service/w3up-client](https://github.com/storacha/upload-service/tree/main/packages/w3up-client). And the PR storacha/freeway#135 needs to be merged before this change gets released.
- Loading branch information
Showing
6 changed files
with
122 additions
and
34 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
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