Skip to content

Commit

Permalink
Merge pull request #278 from codecrafters-io/add-stage-2-solutions
Browse files Browse the repository at this point in the history
Add stage 2 solutions for js and ts
  • Loading branch information
andy1li authored Nov 22, 2024
2 parents 536c761 + 278da75 commit 1db83cb
Show file tree
Hide file tree
Showing 25 changed files with 295 additions and 0 deletions.
11 changes: 11 additions & 0 deletions solutions/javascript/02-rg2/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

# (This file is empty since JavaScript programs don't use a compile step)
11 changes: 11 additions & 0 deletions solutions/javascript/02-rg2/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec node app/main.js "$@"
1 change: 1 addition & 0 deletions solutions/javascript/02-rg2/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions solutions/javascript/02-rg2/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
33 changes: 33 additions & 0 deletions solutions/javascript/02-rg2/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for JavaScript solutions to the
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).

In this challenge, you'll build a toy Redis clone that's capable of handling
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
event loops, the Redis protocol and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Redis implementation is in `app/main.js`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `node (21)` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`app/main.js`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
8 changes: 8 additions & 0 deletions solutions/javascript/02-rg2/code/app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const net = require("net");

const server = net.createServer((connection) => {
// Handle connection
connection.write(`+PONG\r\n`);
});

server.listen(6379, "127.0.0.1");
11 changes: 11 additions & 0 deletions solutions/javascript/02-rg2/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the JavaScript version used to run your code
# on Codecrafters.
#
# Available versions: nodejs-21
language_pack: nodejs-21
12 changes: 12 additions & 0 deletions solutions/javascript/02-rg2/code/package-lock.json

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

5 changes: 5 additions & 0 deletions solutions/javascript/02-rg2/code/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@codecrafters/redis",
"version": "1.0.0",
"dependencies": {}
}
15 changes: 15 additions & 0 deletions solutions/javascript/02-rg2/code/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec node app/main.js "$@"
9 changes: 9 additions & 0 deletions solutions/javascript/02-rg2/diff/app/main.js.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@@ -1,7 +1,8 @@
const net = require("net");

const server = net.createServer((connection) => {
// Handle connection
+ connection.write(`+PONG\r\n`);
});

server.listen(6379, "127.0.0.1");
20 changes: 20 additions & 0 deletions solutions/javascript/02-rg2/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The entry point for your Redis implementation is in `app/main.js`.

Study and uncomment the relevant code:

```javascript
// Uncomment this block to pass the first stage
const server = net.createServer((connection) => {
// Handle connection
});

server.listen(6379, "127.0.0.1");
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
11 changes: 11 additions & 0 deletions solutions/typescript/02-rg2/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

# (This file is empty since TypeScript programs don't use a compile step)
11 changes: 11 additions & 0 deletions solutions/typescript/02-rg2/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec bun run $(dirname $0)/app/main.ts "$@"
1 change: 1 addition & 0 deletions solutions/typescript/02-rg2/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions solutions/typescript/02-rg2/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
33 changes: 33 additions & 0 deletions solutions/typescript/02-rg2/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for TypeScript solutions to the
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).

In this challenge, you'll build a toy Redis clone that's capable of handling
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
event loops, the Redis protocol and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Redis implementation is in `app/main.ts`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `bun (1.1)` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`app/main.ts`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
8 changes: 8 additions & 0 deletions solutions/typescript/02-rg2/code/app/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as net from "net";

const server: net.Server = net.createServer((connection: net.Socket) => {
// Handle connection
connection.write(`+PONG\r\n`);
});

server.listen(6379, "127.0.0.1");
Binary file added solutions/typescript/02-rg2/code/bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions solutions/typescript/02-rg2/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the TypeScript version used to run your code
# on Codecrafters.
#
# Available versions: bun-1.1
language_pack: bun-1.1
11 changes: 11 additions & 0 deletions solutions/typescript/02-rg2/code/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@codecrafters/redis",
"description": "Build your own Redis challenge, from CodeCrafters",
"type": "module",
"scripts": {
"dev": "bun run app/main.ts"
},
"devDependencies": {
"@types/bun": "latest"
}
}
27 changes: 27 additions & 0 deletions solutions/typescript/02-rg2/code/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
15 changes: 15 additions & 0 deletions solutions/typescript/02-rg2/code/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec bun run $(dirname $0)/app/main.ts "$@"
9 changes: 9 additions & 0 deletions solutions/typescript/02-rg2/diff/app/main.ts.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@@ -1,7 +1,8 @@
import * as net from "net";

const server: net.Server = net.createServer((connection: net.Socket) => {
// Handle connection
+ connection.write(`+PONG\r\n`);
});

server.listen(6379, "127.0.0.1");
20 changes: 20 additions & 0 deletions solutions/typescript/02-rg2/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The entry point for your Redis implementation is in `app/main.ts`.

Study and uncomment the relevant code:

```typescript
// Uncomment this block to pass the first stage
const server: net.Server = net.createServer((connection: net.Socket) => {
// Handle connection
});

server.listen(6379, "127.0.0.1");
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

0 comments on commit 1db83cb

Please sign in to comment.