Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@hyperjump/browser
Advanced tools
The hyperjump browser is an experimental generic hypermedia client. It aims to provide a uniform interface for working with hypermedia enabled media types. When you use a web browser, you don't interact with HTML, you interact with the UI that the HTML represents. The hyperjump browser aims to do the same except with data. It abstracts away the hypermedia so you can work data as if it's just plain JSON data without having to leave the browser.
The hyperjump browser allows you to plug in support for different media types,
but it comes with support for application/reference+json
. This media type is
based on the JSON Reference I-D
with some additions and improvements.
npm install @hyperjump/browser --save
Run the tests
npm test
Run the tests with a continuous test runner
npm test -- --watch
The following is a quick set of examples with little explanation. See the JSON Reference section below for the theory behind JSON Reference.
GET http://json-reference.hyperjump.io/example1 HTTP/1.1
Accept: application/reference+json
HTTP/1.1 200 OK
Content-Type: application/reference+json
{
"foo": "bar",
"aaa": { "$ref": "#/foo" },
"ccc": { "$ref": "#/aaa" },
"ddd": {
"111": 111,
"222": { "$ref": "#/aaa/bbb" }
},
"eee": [333, { "$ref": "#/ddd/111" }],
"fff": {
"$id": "http://json-reference.hyperjump.io/example2",
"abc": 123
}
}
const JRef = require("@hyperjump/browser/json-reference");
(async () => {
// Get a document by absolute URL
const doc = await JRef.get("http://json-reference.hyperjump.io/example1", JRef.nil);
// Get a document with a relative URL using another document as the context
const aaa = await JRef.get("/aaa", doc);
// Get the value of a document
JRef.value(aaa); // => "bar"
// Get the JSON Pointer for the document
JRef.pointer(aaa); // => "/aaa"
// Map over a document whose value is an array
const eee = JRef.get("#/eee", doc);
const types = await JRef.map((item) => JRef.value(item) * 2, eee); // => [666, 222];
// Get the key/value pairs of a document whose value is an object
const ddd = JRef.get("#/ddd", doc);
await JRef.entries(ddd); // => [
// ["111", JRef.get("#/ddd/111", doc)],
// ["222", JRef.get("#/ddd/222", doc)]
// ]
// Apply operations as a pipeline that works with promises
const doubleEee = JRef.pipeline([
JRef.get("#/eee"),
JRef.map((items) => JRef.value(items) * 2)
]);
await doubleEee(doc); // => [666, 222]
}());
JSON Reference is best known for its role in JSON Schema. Although it had an author in common with JSON Schema, JSON Reference started as an independent, standalone specification. Both JSON Schema and JSON Reference were abandoned by their authors before reaching RFC status. In 2016, a new group picked up the JSON Schema specification and eventually folded JSON Reference into JSON Schema.
With this implementation, I use
JSON Reference draft-03
from the original authors as a starting point and evolve the concept from there.
Therefore, the $ref
and $id
in this implementation IS NOT the same $ref
and $id
used in recent drafts of JSON Schema.
To understand how this implementation works, you need to think about it like a document in a browser. Like HTML in a web browser, a JSON Reference document is identified by a URL and relative URLs within the document are resolved against that URL.
An HTTP message with Content-Type: application/reference+json
should be
interpreted as a JSON Reference document. This content is a JSON object that can
be parsed with any RFC-7150 compliant
JSON parser. The URL fragment used to identify the document should be
interpreted as a JSON Pointer (RFC-6901).
The "value" of a JSON Reference document is the result of applying the JSON
Pointer in the URL fragment to the JSON message body. In the following example,
the URL is http://json-reference.hyperjump.io/example#/foo
, which means the
fragment is /foo
, and the "value" is "bar"
.
Request:
GET http://json-reference.hyperjump.io/example#/foo HTTP/1.1
Accept: application/reference+json
Response:
HTTP/1.1 200 OK
Content-Type: application/reference+json
{
"foo": "bar"
}
In a JSON Reference document, the $ref
property defines a reference to another
document or a different part of the current document. The value of the $ref
property is a string that defines a relative or absolute URL as specified by
RFC-3986.
When the "value" is an object with a $ref
property, it should follow the
reference like following a link. In the following example the fragment points
/aaa
, which is a reference that points to /foo
, and thus the "value" is
"bar"
.
Request:
GET http://json-reference.hyperjump.io/example#/aaa HTTP/1.1
Accept: application/reference+json
Response:
HTTP/1.1 200 OK
Content-Type: application/reference+json
{
"foo": "bar",
"aaa": { "$ref": "#/foo" }
}
A $ref
is a document boundary that JSON Pointers should not cross. $ref
s
should not be followed in order to resolve the fragment's JSON Pointer.
In a JSON Reference document, the $id
property is a string that defines an
absolute URL that identifies a document within the parent document. It's the
inlined version of a $ref
. This is a little like the HTTP/2 server push
feature. It's sending additional documents with the request because we know the
client is just going to request those documents next.
In the example below, the "value" of the document is 111
.
Request:
GET http://json-reference.hyperjump.io/example#/foo HTTP/1.1
Accept: application/reference+json
Response:
HTTP/1.1 200 OK
Content-Type: application/reference+json
{
"foo": {
"$id": "http://json-reference.hyperjump.io/example2#/aaa",
"aaa": 111
}
}
An $id
is a document boundary that JSON Pointers should not cross. A JSON
Reference's fragment JSON Pointer should not point to a separate document
inlined with $id
.
The problem with inlining $ref
s with $id
is that we don't get the HTTP
headers that describe important things like caching. An optional $headers
keyword is being considered.
FAQs
Browse JSON-compatible data with hypermedia references
The npm package @hyperjump/browser receives a total of 3,292 weekly downloads. As such, @hyperjump/browser popularity was classified as popular.
We found that @hyperjump/browser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.