Skip to content

Instantly share code, notes, and snippets.

@aricart
Last active January 11, 2023 19:16
Show Gist options
  • Save aricart/e20012b96747b04c564269629ac327ba to your computer and use it in GitHub Desktop.
Save aricart/e20012b96747b04c564269629ac327ba to your computer and use it in GitHub Desktop.
os put large file
mkdir test; cd test; npm init -y; npm install nats.ws@latest
nats-server -c server.conf &
deno run -A os.js

nats object ls
╭────────────────────────────────────────────────────────────────────────────────────╮
│                                Object Store Buckets                                │
├────────────────────────┬─────────────┬─────────────────────┬─────────┬─────────────┤
│ Bucket                 │ Description │ Created             │ Size    │ Last Update │
├────────────────────────┼─────────────┼─────────────────────┼─────────┼─────────────┤
│ 7KNV4JG4XIKDNUBZBRAGAX │             │ 2023-01-11 19:01:16 │ 8.0 MiB │ 6.43s       │
╰────────────────────────┴─────────────┴─────────────────────┴─────────┴─────────────╯

# now try via a browser
deno run --allow-all --unstable https://deno.land/std@0.136.0/http/file_server.ts .
# find simple.html and click on the link
# show developer tools:``
connected to NATS!
nats.js:10090  >> jetstream's materialized views object store functionality in nats.ws is beta functionality 
simple.html:17 loading 8MB
simple.html:17 {
 "info": {
  "bucket": "UXA5IF33QWF7MRWM4D0IYB",
  "nuid": "UXA5IF33QWF7MRWM4D0JIN",
  "size": 8192192,
  "chunks": 63,
  "name": "hello",
  "description": "test",
  "options": {
   "max_chunk_size": 131072
  },
  "mtime": "2023-01-11T19:11:46.264Z",
  "digest": "SHA-256=krXdR3LFlwZEE3hfE1L2cSvJE3jaya-vpxTdf0YPJG0=",
  "deleted": false
 }
}
simple.html:17 closed the connection
import { connect, StringCodec, nuid } from "./node_modules/nats.ws/esm/nats.js";
// add an entry to the document
function addEntry(s) {
console.log(s);
}
const sc = StringCodec();
const init = async function () {
// create a connection
const nc = await connect({ servers: "ws://localhost:9222" });
addEntry("connected to NATS!");
const js = nc.jetstream();
const os = await js.views.os(nuid.next());
function readableStreamFrom(data) {
return new ReadableStream({
pull(controller) {
// the readable stream adds data
controller.enqueue(data);
controller.close();
},
});
}
addEntry("loading 8MB")
const data = new Uint8Array(1024*1024*8);
const info = await os.put({name: "hello", description: "test"}, readableStreamFrom(data))
addEntry(JSON.stringify(info, null, " "));
// close the connection
nc.close();
addEntry("closed the connection");
};
init();
port: 4222
websocket: {
port: 9222
no_tls: true
compression: true
}
jetstream: {
store_dir: "/tmp/js_data",
max_file_store: -1,
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
crossorigin="anonymous">
</head>
<body>
<!-- a place to record messages -->
<div id="messages" class="container"></div>
<!-- load a script -->
<script type="module">
import { connect, nuid } from "./node_modules/nats.ws/esm/nats.js";
function addEntry(s) {
console.log(s);
}
const init = async function () {
// create a connection
const nc = await connect({ servers: "ws://localhost:9222" });
addEntry("connected to NATS!");
const js = nc.jetstream();
const os = await js.views.os(nuid.next());
function readableStreamFrom(data) {
return new ReadableStream({
pull(controller) {
// the readable stream adds data
controller.enqueue(data);
controller.close();
},
});
}
addEntry("loading 8MB")
const data = new Uint8Array(1024024*8);
const info = await os.put({name: "hello", description: "test"}, readableStreamFrom(data))
addEntry(JSON.stringify(info, null, " "));
// close the connection
nc.close();
addEntry("closed the connection");
};
init();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment