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

src, tests, docs: update and add missing docs #56

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This worker is auto-deployed by Github Actions. The workflow is defined [here](.

## Staging

This worker is deployed to staging every time a pr is merged into the main branch.
This worker is deployed to staging every time a pull request is merged into the main branch.

## Prod

Expand Down
6 changes: 2 additions & 4 deletions docs/dev-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ Spin up a Workerd instance on your machine that serves this worker

Run `wrangler login`

### R2 Buckets
### R2 Bucket

Depending on the environment you're running in, you will need a different R2 bucket on your account.
For dev (the default), it's `dist-dev`. For staging, `dist-staging`. For prod, `dist-prod`.
These buckets will either need to have a copy of Node's dist folder in them or something mimicing the folder there.
Create a R2 bucket named `dist-prod`. This is the bucket that the worker read from. It will either need to have a copy of Node's dist folder in it or something mimicing the folder there.

### Starting the Local Server

Expand Down
2 changes: 2 additions & 0 deletions scripts/update-redirect-links.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3';
import { Linker } from 'nodejs-latest-linker/common.js';
import { writeFile } from 'node:fs/promises';
Expand Down
4 changes: 4 additions & 0 deletions src/handlers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const getHandler: Handler = async (request, env, ctx, cache) => {

let response: Response;
if (bucketPath in VIRTUAL_DIRS) {
// Path requested is to be treated as a symlink to a directory,
// list the directory it points to
response = renderDirectoryListing(
requestUrl,
request,
Expand All @@ -67,8 +69,10 @@ const getHandler: Handler = async (request, env, ctx, cache) => {
env
);
} else if (isPathADirectory) {
// List the directory
response = await listDirectory(requestUrl, request, bucketPath, env);
} else {
// Fetch the file
response = await getFile(requestUrl, request, bucketPath, env);
}

Expand Down
6 changes: 4 additions & 2 deletions src/handlers/strategies/directoryListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export function renderDirectoryListing(
objects: _Object[],
env: Env
): Response {
// Holds all the html for each directory and file we're listing
// Holds the contents of the listing (directories and files)
const tableElements = [];

// Add the default parent directory listing
// There shouldn't really be a case where we're listing the root
// directory (/) when this is deployed, so always add the option
// to go up a directory
tableElements.push({
href: '../',
name: '../',
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/strategies/serveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function getFile(
headers: {
etag: file.httpEtag,
'accept-range': 'bytes',
// @TODO: Explain why JSON files can be accessed anywhere
// https://github.com/nodejs/build/blob/e3df25d6a23f033db317a53ab1e904c953ba1f00/ansible/www-standalone/resources/config/nodejs.org?plain=1#L194-L196
'access-control-allow-origin': url.pathname.endsWith('.json')
? '*'
: '',
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { readFile } from 'node:fs/promises';
import http from 'http';
import { Miniflare } from 'miniflare';

/**
* We make use of the S3 api for directory listing due to
* a size limit in the responses that bindings can return.
* So, we need to mock the ListObjectsV2 call that we send
* to the S3 api.
*/
async function startS3Mock(): Promise<http.Server> {
const server = http.createServer((req, res) => {
const url = new URL(req.url!, `http://${req.headers.host}`);
Expand Down
Loading