pax_global_header00006660000000000000000000000064134710744520014521gustar00rootroot0000000000000052 comment=df9bef0a16d793050c107740ff1f65e322e40283 jsonld.js-1.6.2/000077500000000000000000000000001347107445200134335ustar00rootroot00000000000000jsonld.js-1.6.2/.editorconfig000066400000000000000000000003731347107445200161130ustar00rootroot00000000000000# http://editorconfig.org root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.{js,json,jsonld,yaml,yml}] indent_style = space indent_size = 2 [*.idl] indent_style = space indent_size = 4 jsonld.js-1.6.2/.eslintrc.js000066400000000000000000000002211347107445200156650ustar00rootroot00000000000000module.exports = { env: { browser: true, commonjs: true, node: true }, extends: 'eslint-config-digitalbazaar', root: true }; jsonld.js-1.6.2/.gitignore000066400000000000000000000002711347107445200154230ustar00rootroot00000000000000*.sublime-project *.sublime-workspace *.sw[nop] *~ .DS_Store .cdtproject .classpath .cproject .project .settings TAGS coverage dist node_modules npm-debug.log tests/webidl/*-new v8.log jsonld.js-1.6.2/.jscsrc000066400000000000000000000041011347107445200147170ustar00rootroot00000000000000{ "excludeFiles": [ "./tests/setImmediate.js" ], "disallowKeywords": ["with"], "disallowKeywordsOnNewLine": ["else", "catch"], // FIXME: enable this? //"disallowImplicitTypeConversion": ["string"], "disallowMixedSpacesAndTabs": true, "disallowMultipleLineBreaks": true, // FIXME: enable this or do we prefer to // use w/angular directive templates? //"disallowMultipleLineStrings": true, "disallowNewlineBeforeBlockStatements": true, "disallowSpaceAfterObjectKeys": true, "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"], "disallowSpaceBeforeBinaryOperators": [","], "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"], "disallowSpacesInAnonymousFunctionExpression": { "beforeOpeningRoundBrace": true }, "disallowSpacesInFunctionDeclaration": { "beforeOpeningRoundBrace": true }, "disallowSpacesInNamedFunctionExpression": { "beforeOpeningRoundBrace": true }, "disallowSpacesInsideParentheses": true, "disallowTrailingComma": true, "disallowTrailingWhitespace": true, "requireCommaBeforeLineBreak": true, "requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"], "requireLineFeedAtFileEnd": true, "requireSpaceAfterBinaryOperators": ["?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"], "requireSpaceBeforeBinaryOperators": ["?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"], "requireSpaceAfterKeywords": [ "else", "do", "return", "try" ], "requireSpaceBeforeBlockStatements": true, "requireSpacesInConditionalExpression": { "afterTest": true, "beforeConsequent": true, "afterConsequent": true, "beforeAlternate": true }, "requireSpacesInFunction": { "beforeOpeningCurlyBrace": true }, "safeContextKeyword": "self", "validateLineBreaks": "LF", // FIXME: enable doc checks (update to use newer jscs jsdoc module) //"validateJSDoc": { // "checkParamNames": true, // "requireParamTypes": true //}, "validateParameterSeparator": ", " } jsonld.js-1.6.2/.travis.yml000066400000000000000000000007421347107445200155470ustar00rootroot00000000000000language: node_js node_js: - "6" - "8" - "10" - "12" - "node" sudo: false install: - npm install - npm run fetch-test-suites script: - if [ "x$BUNDLER" = "x" ]; then npm run test; fi - if [ "x$BUNDLER" != "x" ]; then npm run test-karma; fi # only run karma tests for one node version matrix: include: - node_js: "10" env: BUNDLER=webpack # - node_js: "10" # env: BUNDLER=browserify notifications: email: on_success: change on_failure: change jsonld.js-1.6.2/CHANGELOG.md000066400000000000000000000353471347107445200152600ustar00rootroot00000000000000# jsonld ChangeLog ## 1.6.2 - 2019-05-21 ### Fixed - Allow overriding of protected terms when redefining to the same definition, modulo the `protected` flag itself. - Fix type-scoped context application: - Ensure values and subject references are expanded and compacted using type-scoped contexts, if available. - Ensure `@type` values are evaluated against the previous context, not the type-scoped context. ## 1.6.1 - 2019-05-13 ### Fixed - Ensure `@type`-scoped terms are limited to their `@type`-scoped object. ## 1.6.0 - 2019-04-17 ### Fixed - Testing: Use explicit id and description skipping regexes. - Usage of JavaScript Object property names in data. - **NOTE**: A class of bugs was causing term names such as `toString`, `valueOf`, and others to be dropped or produce bogus output. The fix could cause output triples to differ from previous versions if those special names were used. - Specifically, the problem was using `x in obj` instead of `obj.hasOwnProperty(x)` or a `Map`. - Fixed usage in contexts for expand and compact. - Attempted fixes in other parts of the code with similar `x in obj` usage. Finding actual realistic failing test cases proved difficult. ### Changed - Testing: Improve skip logging. ### Added - Testing: `skip` and `only` flags in manifests. - Testing: `VERBOSE_SKIP=true` env var to debug skipping. - Support `@protected`. - Support experimental non-standard `protectedMode` option. ## 1.5.4 - 2019-02-28 ### Fixed - Handle ` ` triple. ## 1.5.3 - 2019-02-21 ### Fixed - Improve handling of canonize test cases. - Update rdf-canonize dependency to address N-Quads parsing bug. ## 1.5.2 - 2019-02-20 ### Changed - Switch to eslint. - Optimize ensuring value is an array. ## 1.5.1 - 2019-02-01 ### Fixed - Update canonize docs. ## 1.5.0 - 2019-01-24 ### Changed - [rdf-canonize][] updated: - **BREAKING**: A fix was applied that makes the canonical output format properly match the N-Triples canoncial format. This fixes the format to no longer escape tabs in literals. This may cause canonical output from `jsonld.normalize()`/`jsonld.canonize()` to differ from previous versions depending on your literal data. If a backwards compatibility mode is needed please use 1.4.x and file an issue. - **BREAKING**: [rdf-canonize-native][] was removed as an indirect optional dependency and the JavaScript implemenation is now the default. The former `usePureJavaScript` flag was removed and a new `useNative` flag was added to force use of the native bindings. Higher level applications must explicitly install `rdf-canonize-native` to use this mode. Note that in many cases the JavaScript implemenation will be *faster*. Apps should be benchmarked before using the specialized native mode. - **NOTE**: The Travis-CI C++11 compiler update fixes are no longer needed when using jsonld.js! [rdf-canonize-native][] was updated to not use C++11 features and is also no longer a direct or indirect dependency of jsonld.js. ### Fixed - `rdfn:Urgna2012EvalTest` and `rdfn:Urdna2015EvalTest` tests should compare with expected output. ## 1.4.0 - 2019-01-05 ### Changed - PhantomJS is deprecated, now using headless Chrome with Karma. - **NOTE**: Using headless Chrome vs PhantomJS may cause newer JS features to slip into releases without proper support for older runtimes and browsers. Please report such issues and they will be addressed. - Update webpack and babel. - Use CommonJS style in main file. - **NOTE**: This change *might* cause problems if code somehow was still using the long deprecated `jsonldjs` global. Any dependants on this feature should update to use bundler tools such as webpack or use `jsonld` in the distributed bundle. ## 1.3.0 - 2019-01-04 ### Fixed - Use rdf-canonize to compare n-quads test results. - Maintain multiple graphs. - Sort `@type` when looking for scoped contexts. ### Changed - Use JSON-LD WG tests. - Categorize and skip failing tests. ## 1.2.1 - 2018-12-11 ### Fixed - Fix source map generation. ## 1.2.0 - 2018-12-11 ### Notes - The updated [rdf-canonize][] extracted out native support into [rdf-canonize-native][] and now has an *optional* dependency on this new module. If you have build tools available it will still build and use native support otherwise it will fallback to less performant JS code. - If you wish to *require* the native `rdf-canonize` bindings, add a dependency in your code to `rdf-canonize-native` to insure it is installed. - Some systems such as [Travis CI](https://travis-ci.org) currently only have ancient compilers installed by default. Users of `rdf-canonize`, and hence `jsonld.js`, previously required special setup so the `rdf-canonize` native bindings would be installable. If CI testing is not performance critical you can now simplify your CI config, let those bindings fail to install, and use the JS fallback code. ### Changed - Update `rdf-canonize` dependency to 0.3. ### Added - Initial support for benchmarking. - Basic callback interface tests. - Add README note about running json-ld.org test suite. ### Removed - Callback version of every test. - Callback interface tests added to catch callback API errors. - Avoids duplication of running every test for promises and callbacks. - Simplifies testing code and makes async/await conversion easier. ## 1.1.0 - 2018-09-05 ### Added - Add `skipExpansion` flag to `toRdf` and `canonize`. ## 1.0.4 - 2018-08-17 ### Fixed - Fix `_findContextUrls` refactoring bug from 1.0.3. ## 1.0.3 - 2018-08-16 ### Changed - Improve performance of active context cache and find context urls: - Use Map/Set. - Cache initial contexts based on options. - Reduce lookups. - Update webpack/karma core-js usage: - Add Map, Set, and Array.from support. ## 1.0.2 - 2018-05-22 ### Fixed - Handle compactArrays option in `@graph` compaction. - Many eslint fixes. - Add missing await to createNodeMap() and merge(). ## 1.0.1 - 2018-03-01 ### Fixed - Don't always use arrays for `@graph`. Fixes 1.0 compatibility issue. ## 1.0.0 - 2018-02-28 ### Notes - **1.0.0**! - [Semantic Versioning](https://semver.org/) is now past the "initial development" 0.x.y stage (after 7+ years!). - [Conformance](README.md#conformance): - JSON-LD 1.0 + JSON-LD 1.0 errata - JSON-LD 1.1 drafts - Thanks to the JSON-LD and related communities and the many many people over the years who contributed ideas, code, bug reports, and support! ### Added - Expansion and Compaction using scoped contexts on property and `@type` terms. - Expansion and Compaction of nested properties. - Index graph containers using `@id` and `@index`, with `@set` variations. - Index node objects using `@id` and `@type`, with `@set` variations. - Framing default and named graphs in addition to merged graph. - Value patterns when framing, allowing a subset of values to appear in the output. ## 0.5.21 - 2018-02-22 ### Fixed - ES2018 features are being used. Update version check to use generated Node.js 6 code when using Node.js earlier than 8.6.0. ## 0.5.20 - 2018-02-10 ### Fixed - Typo handling legacy N-Quads dataset format. ## 0.5.19 - 2018-02-09 ### Fixed - Include String startsWith() compatibility code. ## 0.5.18 - 2018-01-26 ### Changed - Use the W3C standard MIME type for N-Quads of "application/n-quads". Accept "application/nquads" for compatibility. ### Fixed - Fix fromRdf with input triple having a nil subject. ## 0.5.17 - 2018-01-25 ### Changed - **BREAKING**: Release 0.5.x as latest branch. See the many many changes below since 0.4.x including many potential breaking changes. ## 0.5.16 - 2018-01-25 ### Removed - **BREAKING**: Remove `jsonld.version` API and `pkginfo` dependency. This feature added complexity and browser issues and the use case is likely handled by semantic versioning and using a proper dependency. ### Fixed - Do not use native types to create IRIs in value expansion. - Improved error detection for `@container` variations. - Handle empty and relative `@base`. - Remove shortcut from compactIri when IRI is a keyword (fixes compact-0073). ### Changed - Set processingMode from options or first encountered context. - Use array representation of `@container` in processing. - **BREAKING**: Check for keys in term definition outside that expected: `@container`, `@id`, `@language`, `@reverse`, and `@type`. This also sets up for additional keywords in 1.1. ## 0.5.15 - 2017-10-16 ### Changed - **BREAKING**: Use RDF JS (rdf.js.org) interfaces for internal representation of dataset and quads. This should only break code that was using undocumented internal data structures, backwards-compat code exists to handle external RDF parsers. - Update `rdf-canonize` to dependency with native support. ## 0.5.14 - 2017-10-11 ### Fixed - Allow empty lists to be compacted to any `@list` container term. Fixes compact-0074 test. ## 0.5.13 - 2017-10-05 ### Fixed - Remote context retrieval bug. ### Removed - **BREAKING**: Remove `promisify` API. ## 0.5.12 - 2017-10-05 ### Changed - **BREAKING**: Remove top-layer errors. ## 0.5.11 - 2017-09-28 ### Removed - **BREAKING**: Remove deprecated extensions API, including `jsonld.request`. ## 0.5.10 - 2017-09-21 ### Added - Add `expansionMap` and `compactionMap` options. These functions may be provided that will be called when an unmapped value or property will be dropped during expansion or compaction, respectively. The function map return either `undefined` to cause the default behavior, some other value to use instead of the default expanded/compacted value, or it may throw an error to stop expansion/compaction. ### Removed - **BREAKING**: Remove deprecated `objectify` and `prependBase` APIs. Now `objectify` can be achieved via the `@link` option in framing and `prependBase` can be found via `url.prependBase`. - **BREAKING**: Remove deprecated `namer` option from all public APIs, use `issuer` instead. - **BREAKING**: Last active context used is no longer returned as an optional parameter to the `compact` callback. - **BREAKING**: Do not expose deprecated `DocumentCache`. ### Changed - **BREAKING**: Change default canonicalization algorithm to `URDNA2015`. ## 0.5.9 - 2017-09-21 ### Fixed - Callbackify bugs. - Document loaders. - Request queue. - Handling of exceptions in callbacks. ### Added - Various toRDF tests. ### Changed - Move tests from test/ to tests/. ## 0.5.8 - 2017-09-20 ### Changed - Run all test-suite tests with promises and callbacks. ### Fixed - Use Node.js "global" or webpack polyfill. ## 0.5.7 - 2017-09-20 ### Fixed - Distribute all js files, for real this time. ## 0.5.6 - 2017-09-20 ### Fixed - Fix `toRDF()`. ## 0.5.5 - 2017-09-20 ### Fixed - Distribute all js files. ## 0.5.4 - 2017-09-20 ### Fixed - Generate all js files for Node.js 6. ## 0.5.3 - 2017-09-20 ### Changed - Significant code reorganization and splitting into multiple files. ### Removed - **BREAKING**: Explicit IE8 support. Webpack, babel, and/or polyfills may be of help if support is still needed. - **BREAKING**: jQuery document loader. Use the XHR loader. - `Object.keys` polyfill. Other tools can provide this. ### Fixed - Handling of "global". ## 0.5.2 - 2017-09-19 ### Fixed - Distribute browser files. ## 0.5.1 - 2017-09-19 ### Fixed - Distribute unminified bundle. ## 0.5.0 - 2017-09-18 ### Added - Add .editorconfig support. - `fetch-test-suites` and related `fetch-*-test-suite` NPM scripts. - Support for `@graph` `@container`. ### Removed - Bower support. Use NPM, a NPM proxy site, or build your own bundle. - Makefile. Use NPM script targets. ### Changed - Update url parser to remove default ports from URLs. - Skip spec version 1.1 tests. - **BREAKING**: Only support Node.js 6.x and later with ES2015 features. - Build and use custom Node.js 6.x output so async/await/etc can be used. - **BREAKING**: Move `js/jsonld.js` to `lib/jsonld.js`. - **BREAKING**: Switch to CommonJS. - **BREAKING**: Fixes to allow RFC3986 tests to pass. Some URI edge cases and certain base URIs with dot segments may cause different URI outputs. - Switch to Karma for browser testing. - Switch to webpack to build browser bundles. - Add explicit feature compatibility libs to browser bundles. - Use async APIs for test generation. - Done to allow testing in Node.js and browsers. - Required major testing changes to make everything async. - Workarounds added to get async generated mocha tests working. - Improved support for loading various types of tests. - Can load local files, test manifests, or plain js files (in Node.js). - Use ES2015 in tests and babel/webpack to support older platforms. - Use rdf-canonize library, remove local implementation. ## 0.4.12 - 2017-04-24 ### Fixed - Fix `promises.compact` API when called with only 2 parameters. ## 0.4.11 - 2016-04-24 ### Changed - Add optimization for finding best CURIE matches. ## 0.4.10 - 2016-04-24 ### Changed - Add optimization for compacting keywords. ## 0.4.9 - 2016-04-23 ### Changed - Add optimizations for \_compactIri. ## 0.4.8 - 2016-04-14 ### Fixed - Revert es6-promise dependency to 2.x to avoid auto-polyfill behavior. ## 0.4.7 - 2016-04-14 ### Fixed - Testing document loader. ## 0.4.6 - 2016-03-02 ### Added - Add `headers` and `request` option for node doc loader. ### Changed - Include local tests. ## 0.4.5 - 2016-01-19 ### Fixed - N-Quads comments pattern. - Local tests. ## 0.4.4 - 2016-01-08 ### Fixed - Document cache in default node document loader is broken; disable until HTTP caching is implemented. ## 0.4.3 - 2016-01-05 ### Fixed - N-Quads may contain comments. ## 0.4.2 - 2015-10-12 ### Added - Add inputFormat and algorithm options to normalize. ### Changed - Add support for normalization test suite. - Support URDNA2015 normalization algorithm. - Add async scheduling of normalization algorithms. ### Fixed - Ignore null values in language maps. ## 0.4.1 - 2015-09-12 ### Changed - Ignore jsonld-request and pkginfo for browserify. ## 0.4.0 - 2015-09-12 ### Breaking Changes - "request" extension moved to [jsonld-request][]. This was done to simplify the core JSON-LD processing library. In particular, the extension pulled in RDFa processing code and related dependencies. The old method of using this extension of `jsonld.use('request')` is deprecated in favor of using the new module directly. - The `jsonld` tool moved to [jsonld-cli][]. This was also done to simplify the core JSON-LD processing library and because it uses the [jsonld-request][] module. ## 0.3.26 - 2015-09-01 ## Before 0.3.26 - See git history for changes. [jsonld-cli]: https://github.com/digitalbazaar/jsonld-cli [jsonld-request]: https://github.com/digitalbazaar/jsonld-request [rdf-canonize]: https://github.com/digitalbazaar/rdf-canonize [rdf-canonize-native]: https://github.com/digitalbazaar/rdf-canonize-native jsonld.js-1.6.2/LICENSE000066400000000000000000000044401347107445200144420ustar00rootroot00000000000000You may use the jsonld.js project under the terms of the BSD License. You are free to use this project in commercial projects as long as the copyright header is left intact. If you are a commercial entity and use this set of libraries in your commercial software then reasonable payment to Digital Bazaar, if you can afford it, is not required but is expected and would be appreciated. If this library saves you time, then it's saving you money. The cost of developing JSON-LD was on the order of several months of work and tens of thousands of dollars. We are attempting to strike a balance between helping the development community while not being taken advantage of by lucrative commercial entities for our efforts. ------------------------------------------------------------------------------- New BSD License (3-clause) Copyright (c) 2010, Digital Bazaar, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Digital Bazaar, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL BAZAAR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jsonld.js-1.6.2/README.md000066400000000000000000000354241347107445200147220ustar00rootroot00000000000000jsonld.js ========= [![Build status](https://img.shields.io/travis/digitalbazaar/jsonld.js.svg)](https://travis-ci.org/digitalbazaar/jsonld.js) [![Dependency Status](https://img.shields.io/david/digitalbazaar/jsonld.js.svg)](https://david-dm.org/digitalbazaar/jsonld.js) Introduction ------------ This library is an implementation of the [JSON-LD][] specification in JavaScript. JSON, as specified in [RFC7159][], is a simple language for representing objects on the Web. Linked Data is a way of describing content across different documents or Web sites. Web resources are described using IRIs, and typically are dereferencable entities that may be used to find more information, creating a "Web of Knowledge". [JSON-LD][] is intended to be a simple publishing method for expressing not only Linked Data in JSON, but for adding semantics to existing JSON. JSON-LD is designed as a light-weight syntax that can be used to express Linked Data. It is primarily intended to be a way to express Linked Data in JavaScript and other Web-based programming environments. It is also useful when building interoperable Web Services and when storing Linked Data in JSON-based document storage engines. It is practical and designed to be as simple as possible, utilizing the large number of JSON parsers and existing code that is in use today. It is designed to be able to express key-value pairs, RDF data, [RDFa][] data, [Microformats][] data, and [Microdata][]. That is, it supports every major Web-based structured data model in use today. The syntax does not require many applications to change their JSON, but easily add meaning by adding context in a way that is either in-band or out-of-band. The syntax is designed to not disturb already deployed systems running on JSON, but provide a smooth migration path from JSON to JSON with added semantics. Finally, the format is intended to be fast to parse, fast to generate, stream-based and document-based processing compatible, and require a very small memory footprint in order to operate. Conformance ----------- This library aims to conform with the following: * [JSON-LD 1.0][], W3C Recommendation, 2014-01-16, and any [errata][] * [JSON-LD 1.0 Processing Algorithms and API][JSON-LD 1.0 API], W3C Recommendation, 2014-01-16, and any [errata][] * [JSON-LD 1.0 Framing][JSON-LD 1.0 Framing], Unofficial Draft, 2012-08-30 * [JSON-LD 1.1][JSON-LD CG 1.1], Draft Community Group Report, 2018-06-07 or [newer][JSON-LD CG latest] * [JSON-LD 1.1 Processing Algorithms and API][JSON-LD CG 1.1 API], Draft Community Group Report, 2018-06-07 or [newer][JSON-LD CG API latest] * [JSON-LD 1.1 Framing][JSON-LD CG 1.1 Framing], Draft Community Group Report, 2018-06-07 or [newer][JSON-LD CG Framing latest] * Community Group [test suite][] The [JSON-LD Working Group][JSON-LD WG] is now developing JSON-LD 1.1. Library updates to conform with newer specifications will happen as features stabilize and development time and resources permit. * [JSON-LD 1.1][JSON-LD WG 1.1], W3C Working Draft, 2018-12-14 or [newer][JSON-LD WG latest] * [JSON-LD 1.1 Processing Algorithms and API][JSON-LD WG 1.1 API], W3C Working Draft, 2018-12-14 or [newer][JSON-LD WG API latest] * [JSON-LD 1.1 Framing][JSON-LD WG 1.1 Framing], W3C Working Draft, 2018-12-14 or [newer][JSON-LD WG Framing latest] * Working Group [test suite][WG test suite] The [test runner][] is often updated to note or skip newer tests that are not yet supported. Installation ------------ ### node.js + npm ``` npm install jsonld ``` ```js const jsonld = require('jsonld'); ``` ### Browser (AMD) + npm ``` npm install jsonld ``` Use your favorite technology to load `node_modules/dist/jsonld.min.js`. ### CDNJS CDN To use [CDNJS](https://cdnjs.com/) include this script tag: ```html ``` Check https://cdnjs.com/libraries/jsonld for the latest available version. ### jsDeliver CDN To use [jsDeliver](https://www.jsdelivr.com/) include this script tag: ```html ``` See https://www.jsdelivr.com/package/npm/jsonld for the latest available version. ### unpkg CDN To use [unpkg](https://unpkg.com/) include this script tag: ```html ``` See https://unpkg.com/jsonld/ for the latest available version. ### JSPM ``` jspm install npm:jsonld ``` ``` js import * as jsonld from 'jsonld'; // or import {promises} from 'jsonld'; // or import {JsonLdProcessor} from 'jsonld'; ``` ### node.js native canonize bindings For specialized use cases there is an optional [rdf-canonize-native][] package available which provides a native implementation for `canonize()`. It is used by installing the package and setting the `useNative` option of `canonize()` to `true`. Before using this mode it is **highly recommended** to run benchmarks since the JavaScript implementation is often faster and the bindings add toolchain complexity. ``` npm install jsonld npm install rdf-canonize-native ``` Examples -------- Example data and context used throughout examples below: ```js const doc = { "http://schema.org/name": "Manu Sporny", "http://schema.org/url": {"@id": "http://manu.sporny.org/"}, "http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"} }; const context = { "name": "http://schema.org/name", "homepage": {"@id": "http://schema.org/url", "@type": "@id"}, "image": {"@id": "http://schema.org/image", "@type": "@id"} }; ``` ### [compact](http://json-ld.org/spec/latest/json-ld/#compacted-document-form) ```js // compact a document according to a particular context jsonld.compact(doc, context, function(err, compacted) { console.log(JSON.stringify(compacted, null, 2)); /* Output: { "@context": {...}, "name": "Manu Sporny", "homepage": "http://manu.sporny.org/", "image": "http://manu.sporny.org/images/manu.png" } */ }); // compact using URLs jsonld.compact('http://example.org/doc', 'http://example.org/context', ...); // or using promises const compacted = await jsonld.compact(doc, context); ``` ### [expand](http://json-ld.org/spec/latest/json-ld/#expanded-document-form) ```js // expand a document, removing its context jsonld.expand(compacted, function(err, expanded) { /* Output: { "http://schema.org/name": [{"@value": "Manu Sporny"}], "http://schema.org/url": [{"@id": "http://manu.sporny.org/"}], "http://schema.org/image": [{"@id": "http://manu.sporny.org/images/manu.png"}] } */ }); // expand using URLs jsonld.expand('http://example.org/doc', ...); // or using promises const expanded = await jsonld.expand(doc); ``` ### [flatten](http://json-ld.org/spec/latest/json-ld/#flattened-document-form) ```js // flatten a document jsonld.flatten(doc, (err, flattened) => { // all deep-level trees flattened to the top-level }); // or using promises const flattened = await jsonld.flatten(doc); ``` ### [frame](http://json-ld.org/spec/latest/json-ld-framing/#introduction) ```js // frame a document jsonld.frame(doc, frame, (err, framed) => { // document transformed into a particular tree structure per the given frame }); // or using promises const framed = await jsonld.frame(doc, frame); ``` ### [canonize](http://json-ld.github.io/normalization/spec/) (normalize) ```js // canonize (normalize) a document using the RDF Dataset Normalization Algorithm // (URDNA2015), see: jsonld.canonize(doc, { algorithm: 'URDNA2015', format: 'application/n-quads' }, (err, canonized) => { // canonized is a string that is a canonical representation of the document // that can be used for hashing, comparison, etc. }); // or using promises const canonized = await jsonld.canonize(doc, {format: 'application/n-quads'}); ``` ### toRDF (N-Quads) ```js // serialize a document to N-Quads (RDF) jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, nquads) => { // nquads is a string of N-Quads }); // or using promises const rdf = await jsonld.toRDF(doc, {format: 'application/n-quads'}); ``` ### fromRDF (N-Quads) ```js // deserialize N-Quads (RDF) to JSON-LD jsonld.fromRDF(nquads, {format: 'application/n-quads'}, (err, doc) => { // doc is JSON-LD }); // or using promises const doc = await jsonld.fromRDF(nquads, {format: 'application/n-quads'}); ``` ### Custom RDF Parser ```js // register a custom async-callback-based RDF parser jsonld.registerRDFParser(contentType, (input, callback) => { // parse input to a jsonld.js RDF dataset object... callback(err, dataset); }); // register a custom synchronous RDF parser jsonld.registerRDFParser(contentType, input => { // parse input to a jsonld.js RDF dataset object... and return it return dataset; }); // register a custom promise-based RDF parser jsonld.registerRDFParser(contentType, async input => { // parse input into a jsonld.js RDF dataset object... return new Promise(...); }); ``` ### Custom Document Loader ```js // how to override the default document loader with a custom one -- for // example, one that uses pre-loaded contexts: // define a mapping of context URL => context doc const CONTEXTS = { "http://example.com": { "@context": ... }, ... }; // grab the built-in node.js doc loader const nodeDocumentLoader = jsonld.documentLoaders.node(); // or grab the XHR one: jsonld.documentLoaders.xhr() // change the default document loader using the callback API // (you can also do this using the promise-based API, return a promise instead // of using a callback) const customLoader = (url, callback) => { if(url in CONTEXTS) { return callback( null, { contextUrl: null, // this is for a context via a link header document: CONTEXTS[url], // this is the actual document that was loaded documentUrl: url // this is the actual context URL after redirects }); } // call the underlining documentLoader using the callback API. nodeDocumentLoader(url, callback); /* Note: By default, the node.js document loader uses a callback, but browser-based document loaders (xhr or jquery) return promises if they are supported (or polyfilled) in the browser. This behavior can be controlled with the 'usePromise' option when constructing the document loader. For example: jsonld.documentLoaders.xhr({usePromise: false}); */ }; jsonld.documentLoader = customLoader; // alternatively, pass the custom loader for just a specific call: const compacted = await jsonld.compact( doc, context, {documentLoader: customLoader}); ``` Related Modules --------------- * [jsonld-cli][]: A command line interface tool called `jsonld` that exposes most of the basic jsonld.js API. * [jsonld-request][]: A module that can read data from stdin, URLs, and files and in various formats and return JSON-LD. Commercial Support ------------------ Commercial support for this library is available upon request from [Digital Bazaar][]: support@digitalbazaar.com Source ------ The source code for the JavaScript implementation of the JSON-LD API is available at: http://github.com/digitalbazaar/jsonld.js Tests ----- This library includes a sample testing utility which may be used to verify that changes to the processor maintain the correct output. The main test suites are included in external repositories. Check out each of the following: https://github.com/w3c/json-ld-api https://github.com/w3c/json-ld-framing https://github.com/json-ld/json-ld.org https://github.com/json-ld/normalization They should be sibling directories of the jsonld.js directory or in a `test-suites` dir. To clone shallow copies into the `test-suites` dir you can use the following: npm run fetch-test-suites Node.js tests can be run with a simple command: npm test If you installed the test suites elsewhere, or wish to run other tests, use the `JSONLD_TESTS` environment var: JSONLD_TESTS="/tmp/org/test-suites /tmp/norm/tests" npm test This feature can be used to run the older json-ld.org test suite: JSONLD_TESTS=/tmp/json-ld.org/test-suite npm test Browser testing can be done with Karma: npm run test-karma npm run test-karma -- --browsers Firefox,Chrome Code coverage of node tests can be generated in `coverage/`: npm run coverage To display a full coverage report on the console from coverage data: npm run coverage-report The Mocha output reporter can be changed to min, dot, list, nyan, etc: REPORTER=dot npm test Remote context tests are also available: # run the context server in the background or another terminal node tests/remote-context-server.js JSONLD_TESTS=./tests npm test To generate earl reports: # generate the earl report for node.js EARL=earl-node.jsonld npm test # generate the earl report for the browser EARL=earl-firefox.jsonld npm run test-karma -- --browser Firefox Benchmarks ---------- Benchmarks can be created from any manifest that the test system supports. Use a command line with a test suite and a benchmark flag: JSONLD_TESTS=/tmp/benchmark-manifest.jsonld JSONLD_BENCHMARK=1 npm test [Digital Bazaar]: https://digitalbazaar.com/ [JSON-LD 1.0 API]: http://www.w3.org/TR/2014/REC-json-ld-api-20140116/ [JSON-LD 1.0 Framing]: https://json-ld.org/spec/ED/json-ld-framing/20120830/ [JSON-LD 1.0]: http://www.w3.org/TR/2014/REC-json-ld-20140116/ [JSON-LD CG 1.1 API]: https://json-ld.org/spec/FCGS/json-ld-api/20180607/ [JSON-LD CG 1.1 Framing]: https://json-ld.org/spec/FCGS/json-ld-framing/20180607/ [JSON-LD CG 1.1]: https://json-ld.org/spec/FCGS/json-ld/20180607/ [JSON-LD CG API latest]: https://json-ld.org/spec/latest/json-ld-api/ [JSON-LD CG Framing latest]: https://json-ld.org/spec/latest/json-ld-framing/ [JSON-LD CG latest]: https://json-ld.org/spec/latest/json-ld/ [JSON-LD WG 1.1 API]: https://www.w3.org/TR/json-ld11-api/ [JSON-LD WG 1.1 Framing]: https://www.w3.org/TR/json-ld11-framing/ [JSON-LD WG 1.1]: https://www.w3.org/TR/json-ld11/ [JSON-LD WG API latest]: https://w3c.github.io/json-ld-api/ [JSON-LD WG Framing latest]: https://w3c.github.io/json-ld-framing/ [JSON-LD WG latest]: https://w3c.github.io/json-ld-syntax/ [JSON-LD WG]: https://www.w3.org/2018/json-ld-wg/ [JSON-LD]: https://json-ld.org/ [Microdata]: http://www.w3.org/TR/microdata/ [Microformats]: http://microformats.org/ [RDFa]: http://www.w3.org/TR/rdfa-core/ [RFC7159]: http://tools.ietf.org/html/rfc7159 [WG test suite]: https://github.com/w3c/json-ld-api/tree/master/tests [errata]: http://www.w3.org/2014/json-ld-errata [jsonld-cli]: https://github.com/digitalbazaar/jsonld-cli [jsonld-request]: https://github.com/digitalbazaar/jsonld-request [rdf-canonize-native]: https://github.com/digitalbazaar/rdf-canonize-native [test runner]: https://github.com/digitalbazaar/jsonld.js/blob/master/tests/test-common.js [test suite]: https://github.com/json-ld/json-ld.org/tree/master/test-suite jsonld.js-1.6.2/karma.conf.js000066400000000000000000000111671347107445200160160ustar00rootroot00000000000000/** * Karam configuration for jsonld.js. * * Set dirs, manifests, or js to run: * JSONLD_TESTS="f1 f2 ..." * Output an EARL report: * EARL=filename * Bail with tests fail: * BAIL=true * * @author Dave Longley * @author David I. Lehn * * Copyright (c) 2011-2017 Digital Bazaar, Inc. All rights reserved. */ const webpack = require('webpack'); module.exports = function(config) { // bundler to test: webpack, browserify const bundler = process.env.BUNDLER || 'webpack'; const frameworks = ['mocha', 'server-side']; // main bundle preprocessors const preprocessors = ['babel']; if(bundler === 'browserify') { frameworks.push(bundler); preprocessors.push(bundler); } else if(bundler === 'webpack') { preprocessors.push(bundler); preprocessors.push('sourcemap'); } else { throw Error('Unknown bundler'); } config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks, // list of files / patterns to load in the browser files: [ { pattern: 'tests/test-karma.js', watched: false, served: true, included: true } ], // list of files to exclude exclude: [ ], // preprocess matching files before serving them to the browser // available preprocessors: // https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { //'tests/*.js': ['webpack', 'babel'] //preprocessors 'tests/*.js': preprocessors }, webpack: { mode: 'production', devtool: 'inline-source-map', plugins: [ new webpack.DefinePlugin({ 'process.env.BAIL': JSON.stringify(process.env.BAIL), 'process.env.EARL': JSON.stringify(process.env.EARL), 'process.env.JSONLD_BENCHMARK': JSON.stringify(process.env.JSONLD_BENCHMARK), 'process.env.JSONLD_TESTS': JSON.stringify(process.env.JSONLD_TESTS), 'process.env.TEST_ROOT_DIR': JSON.stringify(__dirname), 'process.env.VERBOSE_SKIP': JSON.stringify(process.env.VERBOSE_SKIP) }) ], module: { rules: [ { test: /\.js$/, include: [{ // exclude node_modules by default exclude: /(node_modules)/ }, { // include rdf-canonize include: /(node_modules\/rdf-canonize)/ }], use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], plugins: [ [ '@babel/plugin-proposal-object-rest-spread', {useBuiltIns: true} ], '@babel/plugin-transform-modules-commonjs', '@babel/plugin-transform-runtime' ] } } } ] }, node: { Buffer: false, process: false, crypto: false, setImmediate: false } }, browserify: { debug: true //transform: ['uglifyify'] }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter //reporters: ['progress'], reporters: ['mocha'], // web server port port: 9876, // enable / disable colors in the output (reporters and logs) colors: true, // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || // config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file // changes autoWatch: false, // start these browsers // available browser launchers: // https://npmjs.org/browse/keyword/karma-launcher //browsers: ['ChromeHeadless', 'Chrome', 'Firefox', 'Safari'], browsers: ['ChromeHeadless'], customLaunchers: { IE9: { base: 'IE', 'x-ua-compatible': 'IE=EmulateIE9' }, IE8: { base: 'IE', 'x-ua-compatible': 'IE=EmulateIE8' } }, // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: true, // Concurrency level // how many browser should be started simultaneous concurrency: Infinity, // Mocha client: { mocha: { // increase from default 2s timeout: 10000, reporter: 'html', delay: true } }, // Proxied paths proxies: {} }); }; jsonld.js-1.6.2/lib/000077500000000000000000000000001347107445200142015ustar00rootroot00000000000000jsonld.js-1.6.2/lib/ActiveContextCache.js000066400000000000000000000020511347107445200202410ustar00rootroot00000000000000/* * Copyright (c) 2017-2018 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {clone} = require('./util'); module.exports = class ActiveContextCache { /** * Creates an active context cache. * * @param size the maximum size of the cache. */ constructor(size = 100) { this.order = []; this.cache = new Map(); this.size = size; } get(activeCtx, localCtx) { const level1 = this.cache.get(activeCtx); if(level1) { const key = JSON.stringify(localCtx); const result = level1.get(key); return result || null; } return null; } set(activeCtx, localCtx, result) { if(this.order.length === this.size) { const entry = this.order.shift(); this.cache.get(entry.activeCtx).delete(entry.localCtx); } const key = JSON.stringify(localCtx); this.order.push({activeCtx, localCtx: key}); let level1 = this.cache.get(activeCtx); if(!level1) { level1 = new Map(); this.cache.set(activeCtx, level1); } level1.set(key, clone(result)); } }; jsonld.js-1.6.2/lib/DocumentCache.js000066400000000000000000000021051347107445200172370ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; module.exports = class DocumentCache { /** * Creates a simple document cache that retains documents for a short * period of time. * * FIXME: Implement simple HTTP caching instead. * * @param options the options to use: * [size] the maximum size of the cache (default: 50). * [expires] expiration time for each entry in ms (default: 30000). */ constructor({size = 50, expires = 30000}) { this.order = []; this.cache = {}; this.size = size; this.expires = expires; } get(url) { if(url in this.cache) { const entry = this.cache[url]; if(entry.expires >= Date.now()) { return entry.ctx; } delete this.cache[url]; this.order.splice(this.order.indexOf(url), 1); } return null; } set(url, ctx) { if(this.order.length === this.size) { delete this.cache[this.order.shift()]; } this.order.push(url); this.cache[url] = {ctx, expires: (Date.now() + this.expires)}; } }; jsonld.js-1.6.2/lib/JsonLdError.js000066400000000000000000000010021347107445200167330ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; module.exports = class JsonLdError extends Error { /** * Creates a JSON-LD Error. * * @param msg the error message. * @param type the error type. * @param details the error details. */ constructor( message = 'An unspecified JSON-LD error occurred.', name = 'jsonld.Error', details = {}) { super(message); this.name = name; this.message = message; this.details = details; } }; jsonld.js-1.6.2/lib/JsonLdProcessor.js000066400000000000000000000027151347107445200176350ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; module.exports = jsonld => { class JsonLdProcessor { toString() { return '[object JsonLdProcessor]'; } } Object.defineProperty(JsonLdProcessor, 'prototype', { writable: false, enumerable: false }); Object.defineProperty(JsonLdProcessor.prototype, 'constructor', { writable: true, enumerable: false, configurable: true, value: JsonLdProcessor }); // The Web IDL test harness will check the number of parameters defined in // the functions below. The number of parameters must exactly match the // required (non-optional) parameters of the JsonLdProcessor interface as // defined here: // https://www.w3.org/TR/json-ld-api/#the-jsonldprocessor-interface JsonLdProcessor.compact = function(input, ctx) { if(arguments.length < 2) { return Promise.reject( new TypeError('Could not compact, too few arguments.')); } return jsonld.compact(input, ctx); }; JsonLdProcessor.expand = function(input) { if(arguments.length < 1) { return Promise.reject( new TypeError('Could not expand, too few arguments.')); } return jsonld.expand(input); }; JsonLdProcessor.flatten = function(input) { if(arguments.length < 1) { return Promise.reject( new TypeError('Could not flatten, too few arguments.')); } return jsonld.flatten(input); }; return JsonLdProcessor; }; jsonld.js-1.6.2/lib/NQuads.js000066400000000000000000000002611347107445200157310ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; // TODO: move `NQuads` to its own package module.exports = require('rdf-canonize').NQuads; jsonld.js-1.6.2/lib/Rdfa.js000066400000000000000000000073571347107445200154270ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ /* global Node, XMLSerializer */ 'use strict'; const { RDF_LANGSTRING, RDF_PLAIN_LITERAL, RDF_OBJECT, RDF_XML_LITERAL, XSD_STRING, } = require('./constants'); let _Node; if(typeof Node !== 'undefined') { _Node = Node; } else { _Node = { ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5, ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12 }; } module.exports = class Rdfa { /** * Parses the RDF dataset found via the data object from the RDFa API. * * @param data the RDFa API data object. * * @return the RDF dataset. */ parse(data) { const dataset = {}; dataset['@default'] = []; const subjects = data.getSubjects(); for(let si = 0; si < subjects.length; ++si) { const subject = subjects[si]; if(subject === null) { continue; } // get all related triples const triples = data.getSubjectTriples(subject); if(triples === null) { continue; } const predicates = triples.predicates; for(const predicate in predicates) { // iterate over objects const objects = predicates[predicate].objects; for(let oi = 0; oi < objects.length; ++oi) { const object = objects[oi]; // create RDF triple const triple = {}; // add subject if(subject.indexOf('_:') === 0) { triple.subject = {type: 'blank node', value: subject}; } else { triple.subject = {type: 'IRI', value: subject}; } // add predicate if(predicate.indexOf('_:') === 0) { triple.predicate = {type: 'blank node', value: predicate}; } else { triple.predicate = {type: 'IRI', value: predicate}; } // serialize XML literal let value = object.value; if(object.type === RDF_XML_LITERAL) { // initialize XMLSerializer const XMLSerializer = getXMLSerializerClass(); const serializer = new XMLSerializer(); value = ''; for(let x = 0; x < object.value.length; x++) { if(object.value[x].nodeType === _Node.ELEMENT_NODE) { value += serializer.serializeToString(object.value[x]); } else if(object.value[x].nodeType === _Node.TEXT_NODE) { value += object.value[x].nodeValue; } } } // add object triple.object = {}; // object is an IRI if(object.type === RDF_OBJECT) { if(object.value.indexOf('_:') === 0) { triple.object.type = 'blank node'; } else { triple.object.type = 'IRI'; } } else { // object is a literal triple.object.type = 'literal'; if(object.type === RDF_PLAIN_LITERAL) { if(object.language) { triple.object.datatype = RDF_LANGSTRING; triple.object.language = object.language; } else { triple.object.datatype = XSD_STRING; } } else { triple.object.datatype = object.type; } } triple.object.value = value; // add triple to dataset in default graph dataset['@default'].push(triple); } } } return dataset; } }; function getXMLSerializerClass() { if(typeof XMLSerializer === 'undefined') { return require('xmldom').XMLSerializer; } return XMLSerializer; } jsonld.js-1.6.2/lib/RequestQueue.js000066400000000000000000000016341347107445200172000ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {callbackify, normalizeDocumentLoader} = require('./util'); module.exports = class RequestQueue { /** * Creates a simple queue for requesting documents. */ constructor() { this._requests = {}; this.add = callbackify(this.add.bind(this)); } wrapLoader(loader) { const self = this; self._loader = normalizeDocumentLoader(loader); return function(/* url */) { return self.add.apply(self, arguments); }; } async add(url) { const self = this; let promise = self._requests[url]; if(promise) { // URL already queued, wait for it to load return Promise.resolve(promise); } // queue URL and load it promise = self._requests[url] = self._loader(url); try { return await promise; } finally { delete self._requests[url]; } } }; jsonld.js-1.6.2/lib/compact.js000066400000000000000000001057351347107445200162000ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const JsonLdError = require('./JsonLdError'); const { isArray: _isArray, isObject: _isObject, isString: _isString, isUndefined: _isUndefined } = require('./types'); const { isList: _isList, isValue: _isValue, isGraph: _isGraph, isSimpleGraph: _isSimpleGraph, isSubjectReference: _isSubjectReference } = require('./graphTypes'); const { expandIri: _expandIri, getContextValue: _getContextValue, isKeyword: _isKeyword, process: _processContext } = require('./context'); const { removeBase: _removeBase } = require('./url'); const { addValue: _addValue, asArray: _asArray, compareShortestLeast: _compareShortestLeast } = require('./util'); const api = {}; module.exports = api; /** * Recursively compacts an element using the given active context. All values * must be in expanded form before this method is called. * * @param activeCtx the active context to use. * @param activeProperty the compacted property associated with the element * to compact, null for none. * @param element the element to compact. * @param options the compaction options. * @param compactionMap the compaction map to use. * * @return the compacted value. */ api.compact = ({ activeCtx, activeProperty = null, element, options = {}, compactionMap = () => undefined }) => { // recursively compact array if(_isArray(element)) { let rval = []; for(let i = 0; i < element.length; ++i) { // compact, dropping any null values unless custom mapped let compacted = api.compact({ activeCtx, activeProperty, element: element[i], options, compactionMap }); if(compacted === null) { // TODO: use `await` to support async compacted = compactionMap({ unmappedValue: element[i], activeCtx, activeProperty, parent: element, index: i, options }); if(compacted === undefined) { continue; } } rval.push(compacted); } if(options.compactArrays && rval.length === 1) { // use single element if no container is specified const container = _getContextValue( activeCtx, activeProperty, '@container') || []; if(container.length === 0) { rval = rval[0]; } } return rval; } // use any scoped context on activeProperty const ctx = _getContextValue(activeCtx, activeProperty, '@context'); if(!_isUndefined(ctx)) { // Note: spec's `from term` var is named `isPropertyTermScopedContext` activeCtx = _processContext({ activeCtx, localCtx: ctx, isPropertyTermScopedContext: true, options }); } // recursively compact object if(_isObject(element)) { if(options.link && '@id' in element && options.link.hasOwnProperty(element['@id'])) { // check for a linked element to reuse const linked = options.link[element['@id']]; for(let i = 0; i < linked.length; ++i) { if(linked[i].expanded === element) { return linked[i].compacted; } } } // do value compaction on @values and subject references if(_isValue(element) || _isSubjectReference(element)) { const rval = api.compactValue({activeCtx, activeProperty, value: element, options}); if(options.link && _isSubjectReference(element)) { // store linked element if(!(options.link.hasOwnProperty(element['@id']))) { options.link[element['@id']] = []; } options.link[element['@id']].push({expanded: element, compacted: rval}); } return rval; } // FIXME: avoid misuse of active property as an expanded property? const insideReverse = (activeProperty === '@reverse'); const rval = {}; // revert type scoped context activeCtx = activeCtx.revertTypeScopedContext(); if(options.link && '@id' in element) { // store linked element if(!options.link.hasOwnProperty(element['@id'])) { options.link[element['@id']] = []; } options.link[element['@id']].push({expanded: element, compacted: rval}); } // apply any context defined on an alias of @type // if key is @type and any compacted value is a term having a local // context, overlay that context let types = element['@type'] || []; if(types.length > 1) { types = Array.from(types).sort(); } // find all type-scoped contexts based on current context, prior to // updating it const typeContext = activeCtx; for(const type of types) { const compactedType = api.compactIri( {activeCtx: typeContext, iri: type, relativeTo: {vocab: true}}); // Use any type-scoped context defined on this value const ctx = _getContextValue(typeContext, compactedType, '@context'); if(!_isUndefined(ctx)) { activeCtx = _processContext({ activeCtx, localCtx: ctx, options, isTypeScopedContext: true }); } } // process element keys in order const keys = Object.keys(element).sort(); for(const expandedProperty of keys) { const expandedValue = element[expandedProperty]; // compact @id and @type(s) if(expandedProperty === '@id' || expandedProperty === '@type') { // if using a type-scoped context, resolve type values against previous // context const isType = expandedProperty === '@type'; const valueContext = isType ? (activeCtx.previousContext || activeCtx) : activeCtx; let compactedValue = _asArray(expandedValue).map( expandedIri => api.compactIri({ activeCtx: valueContext, iri: expandedIri, relativeTo: {vocab: isType} })); if(compactedValue.length === 1) { compactedValue = compactedValue[0]; } // use keyword alias and add value const alias = api.compactIri( {activeCtx, iri: expandedProperty, relativeTo: {vocab: true}}); const isArray = _isArray(compactedValue) && expandedValue.length === 0; _addValue(rval, alias, compactedValue, {propertyIsArray: isArray}); continue; } // handle @reverse if(expandedProperty === '@reverse') { // recursively compact expanded value const compactedValue = api.compact({ activeCtx, activeProperty: '@reverse', element: expandedValue, options, compactionMap }); // handle double-reversed properties for(const compactedProperty in compactedValue) { if(activeCtx.mappings.has(compactedProperty) && activeCtx.mappings.get(compactedProperty).reverse) { const value = compactedValue[compactedProperty]; const container = _getContextValue( activeCtx, compactedProperty, '@container') || []; const useArray = ( container.includes('@set') || !options.compactArrays); _addValue( rval, compactedProperty, value, {propertyIsArray: useArray}); delete compactedValue[compactedProperty]; } } if(Object.keys(compactedValue).length > 0) { // use keyword alias and add value const alias = api.compactIri({ activeCtx, iri: expandedProperty, relativeTo: {vocab: true} }); _addValue(rval, alias, compactedValue); } continue; } if(expandedProperty === '@preserve') { // compact using activeProperty const compactedValue = api.compact({ activeCtx, activeProperty, element: expandedValue, options, compactionMap}); if(!(_isArray(compactedValue) && compactedValue.length === 0)) { _addValue(rval, expandedProperty, compactedValue); } continue; } // handle @index property if(expandedProperty === '@index') { // drop @index if inside an @index container const container = _getContextValue( activeCtx, activeProperty, '@container') || []; if(container.includes('@index')) { continue; } // use keyword alias and add value const alias = api.compactIri({ activeCtx, iri: expandedProperty, relativeTo: {vocab: true} }); _addValue(rval, alias, expandedValue); continue; } // skip array processing for keywords that aren't @graph or @list if(expandedProperty !== '@graph' && expandedProperty !== '@list' && _isKeyword(expandedProperty)) { // use keyword alias and add value as is const alias = api.compactIri({ activeCtx, iri: expandedProperty, relativeTo: {vocab: true} }); _addValue(rval, alias, expandedValue); continue; } // Note: expanded value must be an array due to expansion algorithm. if(!_isArray(expandedValue)) { throw new JsonLdError( 'JSON-LD expansion error; expanded value must be an array.', 'jsonld.SyntaxError'); } // preserve empty arrays if(expandedValue.length === 0) { const itemActiveProperty = api.compactIri({ activeCtx, iri: expandedProperty, value: expandedValue, relativeTo: {vocab: true}, reverse: insideReverse }); const nestProperty = activeCtx.mappings.has(itemActiveProperty) ? activeCtx.mappings.get(itemActiveProperty)['@nest'] : null; let nestResult = rval; if(nestProperty) { _checkNestProperty(activeCtx, nestProperty, options); if(!_isObject(rval[nestProperty])) { rval[nestProperty] = {}; } nestResult = rval[nestProperty]; } _addValue( nestResult, itemActiveProperty, expandedValue, { propertyIsArray: true }); } // recusively process array values for(const expandedItem of expandedValue) { // compact property and get container type const itemActiveProperty = api.compactIri({ activeCtx, iri: expandedProperty, value: expandedItem, relativeTo: {vocab: true}, reverse: insideReverse }); // if itemActiveProperty is a @nest property, add values to nestResult, // otherwise rval const nestProperty = activeCtx.mappings.has(itemActiveProperty) ? activeCtx.mappings.get(itemActiveProperty)['@nest'] : null; let nestResult = rval; if(nestProperty) { _checkNestProperty(activeCtx, nestProperty, options); if(!_isObject(rval[nestProperty])) { rval[nestProperty] = {}; } nestResult = rval[nestProperty]; } const container = _getContextValue( activeCtx, itemActiveProperty, '@container') || []; // get simple @graph or @list value if appropriate const isGraph = _isGraph(expandedItem); const isList = _isList(expandedItem); let inner; if(isList) { inner = expandedItem['@list']; } else if(isGraph) { inner = expandedItem['@graph']; } // recursively compact expanded item let compactedItem = api.compact({ activeCtx, activeProperty: itemActiveProperty, element: (isList || isGraph) ? inner : expandedItem, options, compactionMap }); // handle @list if(isList) { // ensure @list value is an array if(!_isArray(compactedItem)) { compactedItem = [compactedItem]; } if(!container.includes('@list')) { // wrap using @list alias compactedItem = { [api.compactIri({ activeCtx, iri: '@list', relativeTo: {vocab: true} })]: compactedItem }; // include @index from expanded @list, if any if('@index' in expandedItem) { compactedItem[api.compactIri({ activeCtx, iri: '@index', relativeTo: {vocab: true} })] = expandedItem['@index']; } } else if(nestResult.hasOwnProperty(itemActiveProperty)) { // can't use @list container for more than 1 list throw new JsonLdError( 'JSON-LD compact error; property has a "@list" @container ' + 'rule but there is more than a single @list that matches ' + 'the compacted term in the document. Compaction might mix ' + 'unwanted items into the list.', 'jsonld.SyntaxError', {code: 'compaction to list of lists'}); } } // Graph object compaction cases if(isGraph) { if(container.includes('@graph') && (container.includes('@id') || container.includes('@index') && _isSimpleGraph(expandedItem))) { // get or create the map object let mapObject; if(nestResult.hasOwnProperty(itemActiveProperty)) { mapObject = nestResult[itemActiveProperty]; } else { nestResult[itemActiveProperty] = mapObject = {}; } // index on @id or @index or alias of @none const key = (container.includes('@id') ? expandedItem['@id'] : expandedItem['@index']) || api.compactIri({activeCtx, iri: '@none', vocab: true}); // add compactedItem to map, using value of `@id` or a new blank // node identifier _addValue( mapObject, key, compactedItem, { propertyIsArray: (!options.compactArrays || container.includes('@set')) }); } else if(container.includes('@graph') && _isSimpleGraph(expandedItem)) { // container includes @graph but not @id or @index and value is a // simple graph object add compact value _addValue( nestResult, itemActiveProperty, compactedItem, { propertyIsArray: (!options.compactArrays || container.includes('@set')) }); } else { // wrap using @graph alias, remove array if only one item and // compactArrays not set if(_isArray(compactedItem) && compactedItem.length === 1 && options.compactArrays) { compactedItem = compactedItem[0]; } compactedItem = { [api.compactIri({ activeCtx, iri: '@graph', relativeTo: {vocab: true} })]: compactedItem }; // include @id from expanded graph, if any if('@id' in expandedItem) { compactedItem[api.compactIri({ activeCtx, iri: '@id', relativeTo: {vocab: true} })] = expandedItem['@id']; } // include @index from expanded graph, if any if('@index' in expandedItem) { compactedItem[api.compactIri({ activeCtx, iri: '@index', relativeTo: {vocab: true} })] = expandedItem['@index']; } _addValue( nestResult, itemActiveProperty, compactedItem, { propertyIsArray: (!options.compactArrays || container.includes('@set')) }); } } else if(container.includes('@language') || container.includes('@index') || container.includes('@id') || container.includes('@type')) { // handle language and index maps // get or create the map object let mapObject; if(nestResult.hasOwnProperty(itemActiveProperty)) { mapObject = nestResult[itemActiveProperty]; } else { nestResult[itemActiveProperty] = mapObject = {}; } let key; if(container.includes('@language')) { // if container is a language map, simplify compacted value to // a simple string if(_isValue(compactedItem)) { compactedItem = compactedItem['@value']; } key = expandedItem['@language']; } else if(container.includes('@index')) { key = expandedItem['@index']; } else if(container.includes('@id')) { const idKey = api.compactIri({activeCtx, iri: '@id', vocab: true}); key = compactedItem[idKey]; delete compactedItem[idKey]; } else if(container.includes('@type')) { const typeKey = api.compactIri({ activeCtx, iri: '@type', vocab: true }); let types; [key, ...types] = _asArray(compactedItem[typeKey] || []); switch(types.length) { case 0: delete compactedItem[typeKey]; break; case 1: compactedItem[typeKey] = types[0]; break; default: compactedItem[typeKey] = types; break; } } // if compacting this value which has no key, index on @none if(!key) { key = api.compactIri({activeCtx, iri: '@none', vocab: true}); } // add compact value to map object using key from expanded value // based on the container type _addValue( mapObject, key, compactedItem, { propertyIsArray: container.includes('@set') }); } else { // use an array if: compactArrays flag is false, // @container is @set or @list , value is an empty // array, or key is @graph const isArray = (!options.compactArrays || container.includes('@set') || container.includes('@list') || (_isArray(compactedItem) && compactedItem.length === 0) || expandedProperty === '@list' || expandedProperty === '@graph'); // add compact value _addValue( nestResult, itemActiveProperty, compactedItem, {propertyIsArray: isArray}); } } } return rval; } // only primitives remain which are already compact return element; }; /** * Compacts an IRI or keyword into a term or prefix if it can be. If the * IRI has an associated value it may be passed. * * @param activeCtx the active context to use. * @param iri the IRI to compact. * @param value the value to check or null. * @param relativeTo options for how to compact IRIs: * vocab: true to split after @vocab, false not to. * @param reverse true if a reverse property is being compacted, false if not. * * @return the compacted term, prefix, keyword alias, or the original IRI. */ api.compactIri = ({ activeCtx, iri, value = null, relativeTo = {vocab: false}, reverse = false }) => { // can't compact null if(iri === null) { return iri; } // if context is from a property term scoped context composed with a // type-scoped context, then use the previous context instead if(activeCtx.isPropertyTermScoped && activeCtx.previousContext) { activeCtx = activeCtx.previousContext; } const inverseCtx = activeCtx.getInverse(); // if term is a keyword, it may be compacted to a simple alias if(_isKeyword(iri) && iri in inverseCtx && '@none' in inverseCtx[iri] && '@type' in inverseCtx[iri]['@none'] && '@none' in inverseCtx[iri]['@none']['@type']) { return inverseCtx[iri]['@none']['@type']['@none']; } // use inverse context to pick a term if iri is relative to vocab if(relativeTo.vocab && iri in inverseCtx) { const defaultLanguage = activeCtx['@language'] || '@none'; // prefer @index if available in value const containers = []; if(_isObject(value) && '@index' in value && !('@graph' in value)) { containers.push('@index', '@index@set'); } // if value is a preserve object, use its value if(_isObject(value) && '@preserve' in value) { value = value['@preserve'][0]; } // prefer most specific container including @graph, prefering @set // variations if(_isGraph(value)) { // favor indexmap if the graph is indexed if('@index' in value) { containers.push( '@graph@index', '@graph@index@set', '@index', '@index@set'); } // favor idmap if the graph is has an @id if('@id' in value) { containers.push( '@graph@id', '@graph@id@set'); } containers.push('@graph', '@graph@set', '@set'); // allow indexmap if the graph is not indexed if(!('@index' in value)) { containers.push( '@graph@index', '@graph@index@set', '@index', '@index@set'); } // allow idmap if the graph does not have an @id if(!('@id' in value)) { containers.push('@graph@id', '@graph@id@set'); } } else if(_isObject(value) && !_isValue(value)) { containers.push('@id', '@id@set', '@type', '@set@type'); } // defaults for term selection based on type/language let typeOrLanguage = '@language'; let typeOrLanguageValue = '@null'; if(reverse) { typeOrLanguage = '@type'; typeOrLanguageValue = '@reverse'; containers.push('@set'); } else if(_isList(value)) { // choose the most specific term that works for all elements in @list // only select @list containers if @index is NOT in value if(!('@index' in value)) { containers.push('@list'); } const list = value['@list']; if(list.length === 0) { // any empty list can be matched against any term that uses the // @list container regardless of @type or @language typeOrLanguage = '@any'; typeOrLanguageValue = '@none'; } else { let commonLanguage = (list.length === 0) ? defaultLanguage : null; let commonType = null; for(let i = 0; i < list.length; ++i) { const item = list[i]; let itemLanguage = '@none'; let itemType = '@none'; if(_isValue(item)) { if('@language' in item) { itemLanguage = item['@language']; } else if('@type' in item) { itemType = item['@type']; } else { // plain literal itemLanguage = '@null'; } } else { itemType = '@id'; } if(commonLanguage === null) { commonLanguage = itemLanguage; } else if(itemLanguage !== commonLanguage && _isValue(item)) { commonLanguage = '@none'; } if(commonType === null) { commonType = itemType; } else if(itemType !== commonType) { commonType = '@none'; } // there are different languages and types in the list, so choose // the most generic term, no need to keep iterating the list if(commonLanguage === '@none' && commonType === '@none') { break; } } commonLanguage = commonLanguage || '@none'; commonType = commonType || '@none'; if(commonType !== '@none') { typeOrLanguage = '@type'; typeOrLanguageValue = commonType; } else { typeOrLanguageValue = commonLanguage; } } } else { if(_isValue(value)) { if('@language' in value && !('@index' in value)) { containers.push('@language', '@language@set'); typeOrLanguageValue = value['@language']; } else if('@type' in value) { typeOrLanguage = '@type'; typeOrLanguageValue = value['@type']; } } else { typeOrLanguage = '@type'; typeOrLanguageValue = '@id'; } containers.push('@set'); } // do term selection containers.push('@none'); // an index map can be used to index values using @none, so add as a low // priority if(_isObject(value) && !('@index' in value)) { // allow indexing even if no @index present containers.push('@index', '@index@set'); } // values without type or language can use @language map if(_isValue(value) && Object.keys(value).length === 1) { // allow indexing even if no @index present containers.push('@language', '@language@set'); } const term = _selectTerm( activeCtx, iri, value, containers, typeOrLanguage, typeOrLanguageValue); if(term !== null) { return term; } } // no term match, use @vocab if available if(relativeTo.vocab) { if('@vocab' in activeCtx) { // determine if vocab is a prefix of the iri const vocab = activeCtx['@vocab']; if(iri.indexOf(vocab) === 0 && iri !== vocab) { // use suffix as relative iri if it is not a term in the active context const suffix = iri.substr(vocab.length); if(!activeCtx.mappings.has(suffix)) { return suffix; } } } } // no term or @vocab match, check for possible CURIEs let choice = null; // TODO: make FastCurieMap a class with a method to do this lookup const partialMatches = []; let iriMap = activeCtx.fastCurieMap; // check for partial matches of against `iri`, which means look until // iri.length - 1, not full length const maxPartialLength = iri.length - 1; for(let i = 0; i < maxPartialLength && iri[i] in iriMap; ++i) { iriMap = iriMap[iri[i]]; if('' in iriMap) { partialMatches.push(iriMap[''][0]); } } // check partial matches in reverse order to prefer longest ones first for(let i = partialMatches.length - 1; i >= 0; --i) { const entry = partialMatches[i]; const terms = entry.terms; for(const term of terms) { // a CURIE is usable if: // 1. it has no mapping, OR // 2. value is null, which means we're not compacting an @value, AND // the mapping matches the IRI const curie = term + ':' + iri.substr(entry.iri.length); const isUsableCurie = (activeCtx.mappings.get(term)._prefix && (!activeCtx.mappings.has(curie) || (value === null && activeCtx.mappings.get(curie)['@id'] === iri))); // select curie if it is shorter or the same length but lexicographically // less than the current choice if(isUsableCurie && (choice === null || _compareShortestLeast(curie, choice) < 0)) { choice = curie; } } } // return chosen curie if(choice !== null) { return choice; } // compact IRI relative to base if(!relativeTo.vocab) { return _removeBase(activeCtx['@base'], iri); } // return IRI as is return iri; }; /** * Performs value compaction on an object with '@value' or '@id' as the only * property. * * @param activeCtx the active context. * @param activeProperty the active property that points to the value. * @param value the value to compact. * @param {Object} [options] - processing options. * * @return the compaction result. */ api.compactValue = ({activeCtx, activeProperty, value, options}) => { // value is a @value if(_isValue(value)) { // get context rules const type = _getContextValue(activeCtx, activeProperty, '@type'); const language = _getContextValue(activeCtx, activeProperty, '@language'); const container = _getContextValue(activeCtx, activeProperty, '@container') || []; // whether or not the value has an @index that must be preserved const preserveIndex = '@index' in value && !container.includes('@index'); // if there's no @index to preserve ... if(!preserveIndex) { // matching @type or @language specified in context, compact value if(value['@type'] === type || value['@language'] === language) { return value['@value']; } } // return just the value of @value if all are true: // 1. @value is the only key or @index isn't being preserved // 2. there is no default language or @value is not a string or // the key has a mapping with a null @language const keyCount = Object.keys(value).length; const isValueOnlyKey = (keyCount === 1 || (keyCount === 2 && '@index' in value && !preserveIndex)); const hasDefaultLanguage = ('@language' in activeCtx); const isValueString = _isString(value['@value']); const hasNullMapping = (activeCtx.mappings.has(activeProperty) && activeCtx.mappings.get(activeProperty)['@language'] === null); if(isValueOnlyKey && (!hasDefaultLanguage || !isValueString || hasNullMapping)) { return value['@value']; } const rval = {}; // preserve @index if(preserveIndex) { rval[api.compactIri({ activeCtx, iri: '@index', relativeTo: {vocab: true} })] = value['@index']; } if('@type' in value) { // compact @type IRI rval[api.compactIri({ activeCtx, iri: '@type', relativeTo: {vocab: true} })] = api.compactIri( {activeCtx, iri: value['@type'], relativeTo: {vocab: true}}); } else if('@language' in value) { // alias @language rval[api.compactIri({ activeCtx, iri: '@language', relativeTo: {vocab: true} })] = value['@language']; } // alias @value rval[api.compactIri({ activeCtx, iri: '@value', relativeTo: {vocab: true} })] = value['@value']; return rval; } // value is a subject reference const expandedProperty = _expandIri(activeCtx, activeProperty, {vocab: true}, options); const type = _getContextValue(activeCtx, activeProperty, '@type'); const compacted = api.compactIri( {activeCtx, iri: value['@id'], relativeTo: {vocab: type === '@vocab'}}); // compact to scalar if(type === '@id' || type === '@vocab' || expandedProperty === '@graph') { return compacted; } return { [api.compactIri({ activeCtx, iri: '@id', relativeTo: {vocab: true} })]: compacted }; }; /** * Removes the @preserve keywords as the last step of the compaction * algorithm when it is running on framed output. * * @param ctx the active context used to compact the input. * @param input the framed, compacted output. * @param options the compaction options used. * * @return the resulting output. */ api.removePreserve = (ctx, input, options) => { // recurse through arrays if(_isArray(input)) { const output = []; for(let i = 0; i < input.length; ++i) { const result = api.removePreserve(ctx, input[i], options); // drop nulls from arrays if(result !== null) { output.push(result); } } input = output; } else if(_isObject(input)) { // remove @preserve if('@preserve' in input) { if(input['@preserve'] === '@null') { return null; } return input['@preserve']; } // skip @values if(_isValue(input)) { return input; } // recurse through @lists if(_isList(input)) { input['@list'] = api.removePreserve(ctx, input['@list'], options); return input; } // handle in-memory linked nodes const idAlias = api.compactIri({ activeCtx: ctx, iri: '@id', relativeTo: {vocab: true} }); if(input.hasOwnProperty(idAlias)) { const id = input[idAlias]; if(options.link.hasOwnProperty(id)) { const idx = options.link[id].indexOf(input); if(idx !== -1) { // already visited return options.link[id][idx]; } // prevent circular visitation options.link[id].push(input); } else { // prevent circular visitation options.link[id] = [input]; } } // recurse through properties const graphAlias = api.compactIri({ activeCtx: ctx, iri: '@graph', relativeTo: {vocab: true} }); for(const prop in input) { // potentially remove the id, if it is an unreference bnode if(prop === idAlias && options.bnodesToClear.includes(input[prop])) { delete input[idAlias]; continue; } let result = api.removePreserve(ctx, input[prop], options); const container = _getContextValue(ctx, prop, '@container') || []; if(options.compactArrays && _isArray(result) && result.length === 1 && container.length === 0 && prop !== graphAlias) { result = result[0]; } input[prop] = result; } } return input; }; /** * Picks the preferred compaction term from the given inverse context entry. * * @param activeCtx the active context. * @param iri the IRI to pick the term for. * @param value the value to pick the term for. * @param containers the preferred containers. * @param typeOrLanguage either '@type' or '@language'. * @param typeOrLanguageValue the preferred value for '@type' or '@language'. * * @return the preferred term. */ function _selectTerm( activeCtx, iri, value, containers, typeOrLanguage, typeOrLanguageValue) { if(typeOrLanguageValue === null) { typeOrLanguageValue = '@null'; } // preferences for the value of @type or @language const prefs = []; // determine prefs for @id based on whether or not value compacts to a term if((typeOrLanguageValue === '@id' || typeOrLanguageValue === '@reverse') && _isSubjectReference(value)) { // prefer @reverse first if(typeOrLanguageValue === '@reverse') { prefs.push('@reverse'); } // try to compact value to a term const term = api.compactIri( {activeCtx, iri: value['@id'], relativeTo: {vocab: true}}); if(activeCtx.mappings.has(term) && activeCtx.mappings.get(term) && activeCtx.mappings.get(term)['@id'] === value['@id']) { // prefer @vocab prefs.push.apply(prefs, ['@vocab', '@id']); } else { // prefer @id prefs.push.apply(prefs, ['@id', '@vocab']); } } else { prefs.push(typeOrLanguageValue); } prefs.push('@none'); const containerMap = activeCtx.inverse[iri]; for(let ci = 0; ci < containers.length; ++ci) { // if container not available in the map, continue const container = containers[ci]; if(!(container in containerMap)) { continue; } const typeOrLanguageValueMap = containerMap[container][typeOrLanguage]; for(let pi = 0; pi < prefs.length; ++pi) { // if type/language option not available in the map, continue const pref = prefs[pi]; if(!(pref in typeOrLanguageValueMap)) { continue; } // select term return typeOrLanguageValueMap[pref]; } } return null; } /** * The value of `@nest` in the term definition must either be `@nest`, or a term * which resolves to `@nest`. * * @param activeCtx the active context. * @param nestProperty a term in the active context or `@nest`. * @param {Object} [options] - processing options. */ function _checkNestProperty(activeCtx, nestProperty, options) { if(_expandIri(activeCtx, nestProperty, {vocab: true}, options) !== '@nest') { throw new JsonLdError( 'JSON-LD compact error; nested property must have an @nest value ' + 'resolving to @nest.', 'jsonld.SyntaxError', {code: 'invalid @nest value'}); } } jsonld.js-1.6.2/lib/constants.js000066400000000000000000000012701347107445200165530ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; const XSD = 'http://www.w3.org/2001/XMLSchema#'; module.exports = { LINK_HEADER_REL: 'http://www.w3.org/ns/json-ld#context', RDF, RDF_LIST: RDF + 'List', RDF_FIRST: RDF + 'first', RDF_REST: RDF + 'rest', RDF_NIL: RDF + 'nil', RDF_TYPE: RDF + 'type', RDF_PLAIN_LITERAL: RDF + 'PlainLiteral', RDF_XML_LITERAL: RDF + 'XMLLiteral', RDF_OBJECT: RDF + 'object', RDF_LANGSTRING: RDF + 'langString', XSD, XSD_BOOLEAN: XSD + 'boolean', XSD_DOUBLE: XSD + 'double', XSD_INTEGER: XSD + 'integer', XSD_STRING: XSD + 'string', }; jsonld.js-1.6.2/lib/context.js000066400000000000000000001224671347107445200162370ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const util = require('./util'); const ActiveContextCache = require('./ActiveContextCache'); const JsonLdError = require('./JsonLdError'); const { isArray: _isArray, isObject: _isObject, isString: _isString, isUndefined: _isUndefined } = require('./types'); const { isAbsolute: _isAbsoluteIri, isRelative: _isRelativeIri, prependBase, parse: parseUrl } = require('./url'); const { asArray: _asArray, compareShortestLeast: _compareShortestLeast } = require('./util'); const MAX_CONTEXT_URLS = 10; const INITIAL_CONTEXT_CACHE = new Map(); const INITIAL_CONTEXT_CACHE_MAX_SIZE = 10000; const api = {}; module.exports = api; api.cache = new ActiveContextCache(); /** * Processes a local context and returns a new active context. * * @param activeCtx the current active context. * @param localCtx the local context to process. * @param options the context processing options. * @param isPropertyTermScopedContext `true` if `localCtx` is a scoped context * from a property term. * @param isTypeScopedContext `true` if `localCtx` is a scoped context * from a type. * * @return the new active context. */ api.process = ({ activeCtx, localCtx, options, isPropertyTermScopedContext = false, isTypeScopedContext = false }) => { // normalize local context to an array of @context objects if(_isObject(localCtx) && '@context' in localCtx && _isArray(localCtx['@context'])) { localCtx = localCtx['@context']; } const ctxs = _asArray(localCtx); // no contexts in array, return current active context w/o changes if(ctxs.length === 0) { return activeCtx; } // track the previous context const previousContext = activeCtx.previousContext || activeCtx; // if context is property scoped and there's a previous context, amend it, // not the current one if(isPropertyTermScopedContext && activeCtx.previousContext) { // TODO: consider optimizing to a shallow copy activeCtx = activeCtx.clone(); activeCtx.isPropertyTermScoped = true; activeCtx.previousContext = api.process({ activeCtx: activeCtx.previousContext, localCtx: ctxs, options, isPropertyTermScopedContext }); return activeCtx; } // process each context in order, update active context // on each iteration to ensure proper caching let rval = activeCtx; for(let i = 0; i < ctxs.length; ++i) { let ctx = ctxs[i]; // update active context to one computed from last iteration activeCtx = rval; // reset to initial context if(ctx === null) { // We can't nullify if there are protected terms and we're // not processing a property term scoped context if(!isPropertyTermScopedContext && Object.keys(activeCtx.protected).length !== 0) { const protectedMode = (options && options.protectedMode) || 'error'; if(protectedMode === 'error') { throw new JsonLdError( 'Tried to nullify a context with protected terms outside of ' + 'a term definition.', 'jsonld.SyntaxError', {code: 'invalid context nullification'}); } else if(protectedMode === 'warn') { // FIXME: remove logging and use a handler console.warn('WARNING: invalid context nullification'); const oldActiveCtx = activeCtx; // copy all protected term definitions to fresh initial context rval = activeCtx = api.getInitialContext(options).clone(); for(const [term, _protected] of Object.entries(oldActiveCtx.protected)) { if(_protected) { activeCtx.mappings[term] = util.clone(oldActiveCtx.mappings[term]); } } activeCtx.protected = util.clone(oldActiveCtx.protected); // cache result if(api.cache) { api.cache.set(oldActiveCtx, ctx, rval); } continue; } throw new JsonLdError( 'Invalid protectedMode.', 'jsonld.SyntaxError', {code: 'invalid protected mode', context: localCtx, protectedMode}); } rval = activeCtx = api.getInitialContext(options).clone(); // if context is type-scoped, ensure previous context has been set if(isTypeScopedContext) { rval.previousContext = previousContext.clone(); } continue; } // get context from cache if available if(api.cache) { const cached = api.cache.get(activeCtx, ctx); if(cached) { rval = activeCtx = cached; continue; } } // dereference @context key if present if(_isObject(ctx) && '@context' in ctx) { ctx = ctx['@context']; } // context must be an object by now, all URLs retrieved before this call if(!_isObject(ctx)) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context must be an object.', 'jsonld.SyntaxError', {code: 'invalid local context', context: ctx}); } // TODO: there is likely a `preivousContext` cloning optimization that // could be applied here (no need to copy it under certain conditions) // clone context before updating it rval = rval.clone(); // define context mappings for keys in local context const defined = new Map(); // handle @version if('@version' in ctx) { if(ctx['@version'] !== 1.1) { throw new JsonLdError( 'Unsupported JSON-LD version: ' + ctx['@version'], 'jsonld.UnsupportedVersion', {code: 'invalid @version value', context: ctx}); } if(activeCtx.processingMode && activeCtx.processingMode === 'json-ld-1.0') { throw new JsonLdError( '@version: ' + ctx['@version'] + ' not compatible with ' + activeCtx.processingMode, 'jsonld.ProcessingModeConflict', {code: 'processing mode conflict', context: ctx}); } rval.processingMode = 'json-ld-1.1'; rval['@version'] = ctx['@version']; defined.set('@version', true); } // if not set explicitly, set processingMode to "json-ld-1.0" rval.processingMode = rval.processingMode || activeCtx.processingMode || 'json-ld-1.0'; // handle @base if('@base' in ctx) { let base = ctx['@base']; if(base === null) { // no action } else if(_isAbsoluteIri(base)) { base = parseUrl(base); } else if(_isRelativeIri(base)) { base = parseUrl(prependBase(activeCtx['@base'].href, base)); } else { throw new JsonLdError( 'Invalid JSON-LD syntax; the value of "@base" in a ' + '@context must be an absolute IRI, a relative IRI, or null.', 'jsonld.SyntaxError', {code: 'invalid base IRI', context: ctx}); } rval['@base'] = base; defined.set('@base', true); } // handle @vocab if('@vocab' in ctx) { const value = ctx['@vocab']; if(value === null) { delete rval['@vocab']; } else if(!_isString(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; the value of "@vocab" in a ' + '@context must be a string or null.', 'jsonld.SyntaxError', {code: 'invalid vocab mapping', context: ctx}); } else if(!_isAbsoluteIri(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; the value of "@vocab" in a ' + '@context must be an absolute IRI.', 'jsonld.SyntaxError', {code: 'invalid vocab mapping', context: ctx}); } else { rval['@vocab'] = value; } defined.set('@vocab', true); } // handle @language if('@language' in ctx) { const value = ctx['@language']; if(value === null) { delete rval['@language']; } else if(!_isString(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; the value of "@language" in a ' + '@context must be a string or null.', 'jsonld.SyntaxError', {code: 'invalid default language', context: ctx}); } else { rval['@language'] = value.toLowerCase(); } defined.set('@language', true); } // handle @protected; determine whether this sub-context is declaring // all its terms to be "protected" (exceptions can be made on a // per-definition basis) defined.set('@protected', ctx['@protected'] || false); // process all other keys for(const key in ctx) { api.createTermDefinition( rval, ctx, key, defined, options, isPropertyTermScopedContext); } // if context is type-scoped, ensure previous context has been set if(isTypeScopedContext && !rval.previousContext) { rval.previousContext = previousContext.clone(); } // cache result if(api.cache) { api.cache.set(activeCtx, ctx, rval); } } return rval; }; /** * Creates a term definition during context processing. * * @param activeCtx the current active context. * @param localCtx the local context being processed. * @param term the term in the local context to define the mapping for. * @param defined a map of defining/defined keys to detect cycles and prevent * double definitions. * @param {Object} [options] - creation options. * @param {string} [options.protectedMode="error"] - "error" to throw error * on `@protected` constraint violation, "warn" to allow violations and * signal a warning. * @param isPropertyTermScopedContext `true` if `localCtx` is a scoped context * from a property term. */ api.createTermDefinition = ( activeCtx, localCtx, term, defined, options, isPropertyTermScopedContext = false) => { if(defined.has(term)) { // term already defined if(defined.get(term)) { return; } // cycle detected throw new JsonLdError( 'Cyclical context definition detected.', 'jsonld.CyclicalContext', {code: 'cyclic IRI mapping', context: localCtx, term}); } // now defining term defined.set(term, false); if(api.isKeyword(term)) { throw new JsonLdError( 'Invalid JSON-LD syntax; keywords cannot be overridden.', 'jsonld.SyntaxError', {code: 'keyword redefinition', context: localCtx, term}); } if(term === '') { throw new JsonLdError( 'Invalid JSON-LD syntax; a term cannot be an empty string.', 'jsonld.SyntaxError', {code: 'invalid term definition', context: localCtx}); } // keep reference to previous mapping for potential `@protected` check const previousMapping = activeCtx.mappings.get(term); // remove old mapping if(activeCtx.mappings.has(term)) { activeCtx.mappings.delete(term); } // get context term value let value; if(localCtx.hasOwnProperty(term)) { value = localCtx[term]; } // clear context entry if(value === null || (_isObject(value) && value['@id'] === null)) { activeCtx.mappings.set(term, null); defined.set(term, true); return; } // convert short-hand value to object w/@id let simpleTerm = false; if(_isString(value)) { simpleTerm = true; value = {'@id': value}; } if(!_isObject(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context term values must be ' + 'strings or objects.', 'jsonld.SyntaxError', {code: 'invalid term definition', context: localCtx}); } // create new mapping const mapping = {}; activeCtx.mappings.set(term, mapping); mapping.reverse = false; // make sure term definition only has expected keywords const validKeys = ['@container', '@id', '@language', '@reverse', '@type']; // JSON-LD 1.1 support if(api.processingMode(activeCtx, 1.1)) { validKeys.push('@context', '@nest', '@prefix', '@protected'); } for(const kw in value) { if(!validKeys.includes(kw)) { throw new JsonLdError( 'Invalid JSON-LD syntax; a term definition must not contain ' + kw, 'jsonld.SyntaxError', {code: 'invalid term definition', context: localCtx}); } } // always compute whether term has a colon as an optimization for // _compactIri const colon = term.indexOf(':'); mapping._termHasColon = (colon !== -1); if('@reverse' in value) { if('@id' in value) { throw new JsonLdError( 'Invalid JSON-LD syntax; a @reverse term definition must not ' + 'contain @id.', 'jsonld.SyntaxError', {code: 'invalid reverse property', context: localCtx}); } if('@nest' in value) { throw new JsonLdError( 'Invalid JSON-LD syntax; a @reverse term definition must not ' + 'contain @nest.', 'jsonld.SyntaxError', {code: 'invalid reverse property', context: localCtx}); } const reverse = value['@reverse']; if(!_isString(reverse)) { throw new JsonLdError( 'Invalid JSON-LD syntax; a @context @reverse value must be a string.', 'jsonld.SyntaxError', {code: 'invalid IRI mapping', context: localCtx}); } // expand and add @id mapping const id = _expandIri( activeCtx, reverse, {vocab: true, base: false}, localCtx, defined, options); if(!_isAbsoluteIri(id)) { throw new JsonLdError( 'Invalid JSON-LD syntax; a @context @reverse value must be an ' + 'absolute IRI or a blank node identifier.', 'jsonld.SyntaxError', {code: 'invalid IRI mapping', context: localCtx}); } mapping['@id'] = id; mapping.reverse = true; } else if('@id' in value) { let id = value['@id']; if(!_isString(id)) { throw new JsonLdError( 'Invalid JSON-LD syntax; a @context @id value must be an array ' + 'of strings or a string.', 'jsonld.SyntaxError', {code: 'invalid IRI mapping', context: localCtx}); } if(id !== term) { // expand and add @id mapping id = _expandIri( activeCtx, id, {vocab: true, base: false}, localCtx, defined, options); if(!_isAbsoluteIri(id) && !api.isKeyword(id)) { throw new JsonLdError( 'Invalid JSON-LD syntax; a @context @id value must be an ' + 'absolute IRI, a blank node identifier, or a keyword.', 'jsonld.SyntaxError', {code: 'invalid IRI mapping', context: localCtx}); } mapping['@id'] = id; // indicate if this term may be used as a compact IRI prefix mapping._prefix = (!mapping._termHasColon && id.match(/[:\/\?#\[\]@]$/) && (simpleTerm || api.processingMode(activeCtx, 1.0))); } } if(!('@id' in mapping)) { // see if the term has a prefix if(mapping._termHasColon) { const prefix = term.substr(0, colon); if(localCtx.hasOwnProperty(prefix)) { // define parent prefix api.createTermDefinition(activeCtx, localCtx, prefix, defined, options); } if(activeCtx.mappings.has(prefix)) { // set @id based on prefix parent const suffix = term.substr(colon + 1); mapping['@id'] = activeCtx.mappings.get(prefix)['@id'] + suffix; } else { // term is an absolute IRI mapping['@id'] = term; } } else { // non-IRIs *must* define @ids if @vocab is not available if(!('@vocab' in activeCtx)) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context terms must define an @id.', 'jsonld.SyntaxError', {code: 'invalid IRI mapping', context: localCtx, term}); } // prepend vocab to term mapping['@id'] = activeCtx['@vocab'] + term; } } // Handle term protection if(value['@protected'] === true || (defined.get('@protected') === true && value['@protected'] !== false)) { activeCtx.protected[term] = true; mapping.protected = true; } // IRI mapping now defined defined.set(term, true); if('@type' in value) { let type = value['@type']; if(!_isString(type)) { throw new JsonLdError( 'Invalid JSON-LD syntax; an @context @type value must be a string.', 'jsonld.SyntaxError', {code: 'invalid type mapping', context: localCtx}); } if(type !== '@id' && type !== '@vocab') { // expand @type to full IRI type = _expandIri( activeCtx, type, {vocab: true, base: false}, localCtx, defined, options); if(!_isAbsoluteIri(type)) { throw new JsonLdError( 'Invalid JSON-LD syntax; an @context @type value must be an ' + 'absolute IRI.', 'jsonld.SyntaxError', {code: 'invalid type mapping', context: localCtx}); } if(type.indexOf('_:') === 0) { throw new JsonLdError( 'Invalid JSON-LD syntax; an @context @type value must be an IRI, ' + 'not a blank node identifier.', 'jsonld.SyntaxError', {code: 'invalid type mapping', context: localCtx}); } } // add @type to mapping mapping['@type'] = type; } if('@container' in value) { // normalize container to an array form const container = _isString(value['@container']) ? [value['@container']] : (value['@container'] || []); const validContainers = ['@list', '@set', '@index', '@language']; let isValid = true; const hasSet = container.includes('@set'); // JSON-LD 1.1 support if(api.processingMode(activeCtx, 1.1)) { validContainers.push('@graph', '@id', '@type'); // check container length if(container.includes('@list')) { if(container.length !== 1) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @container with @list must ' + 'have no other values', 'jsonld.SyntaxError', {code: 'invalid container mapping', context: localCtx}); } } else if(container.includes('@graph')) { if(container.some(key => key !== '@graph' && key !== '@id' && key !== '@index' && key !== '@set')) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @container with @graph must ' + 'have no other values other than @id, @index, and @set', 'jsonld.SyntaxError', {code: 'invalid container mapping', context: localCtx}); } } else { // otherwise, container may also include @set isValid &= container.length <= (hasSet ? 2 : 1); } } else { // in JSON-LD 1.0, container must not be an array (it must be a string, // which is one of the validContainers) isValid &= !_isArray(value['@container']); // check container length isValid &= container.length <= 1; } // check against valid containers isValid &= container.every(c => validContainers.includes(c)); // @set not allowed with @list isValid &= !(hasSet && container.includes('@list')); if(!isValid) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @container value must be ' + 'one of the following: ' + validContainers.join(', '), 'jsonld.SyntaxError', {code: 'invalid container mapping', context: localCtx}); } if(mapping.reverse && !container.every(c => ['@index', '@set'].includes(c))) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @container value for a @reverse ' + 'type definition must be @index or @set.', 'jsonld.SyntaxError', {code: 'invalid reverse property', context: localCtx}); } // add @container to mapping mapping['@container'] = container; } // scoped contexts if('@context' in value) { mapping['@context'] = value['@context']; } if('@language' in value && !('@type' in value)) { let language = value['@language']; if(language !== null && !_isString(language)) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @language value must be ' + 'a string or null.', 'jsonld.SyntaxError', {code: 'invalid language mapping', context: localCtx}); } // add @language to mapping if(language !== null) { language = language.toLowerCase(); } mapping['@language'] = language; } // term may be used as a prefix if('@prefix' in value) { if(mapping._termHasColon) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @prefix used on a compact IRI term', 'jsonld.SyntaxError', {code: 'invalid term definition', context: localCtx}); } if(typeof value['@prefix'] === 'boolean') { mapping._prefix = value['@prefix'] === true; } else { throw new JsonLdError( 'Invalid JSON-LD syntax; @context value for @prefix must be boolean', 'jsonld.SyntaxError', {code: 'invalid @prefix value', context: localCtx}); } } if('@nest' in value) { const nest = value['@nest']; if(!_isString(nest) || (nest !== '@nest' && nest.indexOf('@') === 0)) { throw new JsonLdError( 'Invalid JSON-LD syntax; @context @nest value must be ' + 'a string which is not a keyword other than @nest.', 'jsonld.SyntaxError', {code: 'invalid @nest value', context: localCtx}); } mapping['@nest'] = nest; } // disallow aliasing @context and @preserve const id = mapping['@id']; if(id === '@context' || id === '@preserve') { throw new JsonLdError( 'Invalid JSON-LD syntax; @context and @preserve cannot be aliased.', 'jsonld.SyntaxError', {code: 'invalid keyword alias', context: localCtx}); } // FIXME if(1.1) ... ? if(previousMapping && previousMapping.protected && !isPropertyTermScopedContext) { // force new term to continue to be protected and see if the mappings would // be equal activeCtx.protected[term] = true; mapping.protected = true; if(!_deepCompare(previousMapping, mapping)) { const protectedMode = (options && options.protectedMode) || 'error'; if(protectedMode === 'error') { throw new JsonLdError( 'Invalid JSON-LD syntax; tried to redefine a protected term.', 'jsonld.SyntaxError', {code: 'protected term redefinition', context: localCtx, term}); } else if(protectedMode === 'warn') { // FIXME: remove logging and use a handler console.warn('WARNING: protected term redefinition', {term}); return; } throw new JsonLdError( 'Invalid protectedMode.', 'jsonld.SyntaxError', {code: 'invalid protected mode', context: localCtx, term, protectedMode}); } } }; /** * Expands a string to a full IRI. The string may be a term, a prefix, a * relative IRI, or an absolute IRI. The associated absolute IRI will be * returned. * * @param activeCtx the current active context. * @param value the string to expand. * @param relativeTo options for how to resolve relative IRIs: * base: true to resolve against the base IRI, false not to. * vocab: true to concatenate after @vocab, false not to. * @param {Object} [options] - processing options. * * @return the expanded value. */ api.expandIri = (activeCtx, value, relativeTo, options) => { return _expandIri(activeCtx, value, relativeTo, undefined, undefined, options); }; /** * Expands a string to a full IRI. The string may be a term, a prefix, a * relative IRI, or an absolute IRI. The associated absolute IRI will be * returned. * * @param activeCtx the current active context. * @param value the string to expand. * @param relativeTo options for how to resolve relative IRIs: * base: true to resolve against the base IRI, false not to. * vocab: true to concatenate after @vocab, false not to. * @param localCtx the local context being processed (only given if called * during context processing). * @param defined a map for tracking cycles in context definitions (only given * if called during context processing). * @param {Object} [options] - processing options. * * @return the expanded value. */ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) { // already expanded if(value === null || !_isString(value) || api.isKeyword(value)) { return value; } // define term dependency if not defined if(localCtx && localCtx.hasOwnProperty(value) && defined.get(value) !== true) { api.createTermDefinition(activeCtx, localCtx, value, defined, options); } // if context is from a property term scoped context composed with a // type-scoped context, then use previous context instead if(activeCtx.isPropertyTermScoped && activeCtx.previousContext) { activeCtx = activeCtx.previousContext; } relativeTo = relativeTo || {}; if(relativeTo.vocab) { const mapping = activeCtx.mappings.get(value); // value is explicitly ignored with a null mapping if(mapping === null) { return null; } if(mapping) { // value is a term return mapping['@id']; } } // split value into prefix:suffix const colon = value.indexOf(':'); if(colon !== -1) { const prefix = value.substr(0, colon); const suffix = value.substr(colon + 1); // do not expand blank nodes (prefix of '_') or already-absolute // IRIs (suffix of '//') if(prefix === '_' || suffix.indexOf('//') === 0) { return value; } // prefix dependency not defined, define it if(localCtx && localCtx.hasOwnProperty(prefix)) { api.createTermDefinition(activeCtx, localCtx, prefix, defined, options); } // use mapping if prefix is defined if(activeCtx.mappings.has(prefix)) { const mapping = activeCtx.mappings.get(prefix); return mapping['@id'] + suffix; } // already absolute IRI return value; } // prepend vocab if(relativeTo.vocab && '@vocab' in activeCtx) { return activeCtx['@vocab'] + value; } // prepend base if(relativeTo.base) { return prependBase(activeCtx['@base'], value); } return value; } /** * Gets the initial context. * * @param options the options to use: * [base] the document base IRI. * * @return the initial context. */ api.getInitialContext = options => { const base = parseUrl(options.base || ''); const key = JSON.stringify({base, processingMode: options.processingMode}); const cached = INITIAL_CONTEXT_CACHE.get(key); if(cached) { return cached; } const initialContext = { '@base': base, processingMode: options.processingMode, mappings: new Map(), inverse: null, getInverse: _createInverseContext, clone: _cloneActiveContext, revertTypeScopedContext: _revertTypeScopedContext, protected: {} }; // TODO: consider using LRU cache instead if(INITIAL_CONTEXT_CACHE.size === INITIAL_CONTEXT_CACHE_MAX_SIZE) { // clear whole cache -- assumes scenario where the cache fills means // the cache isn't being used very efficiently anyway INITIAL_CONTEXT_CACHE.clear(); } INITIAL_CONTEXT_CACHE.set(key, initialContext); return initialContext; /** * Generates an inverse context for use in the compaction algorithm, if * not already generated for the given active context. * * @return the inverse context. */ function _createInverseContext() { const activeCtx = this; // lazily create inverse if(activeCtx.inverse) { return activeCtx.inverse; } const inverse = activeCtx.inverse = {}; // variables for building fast CURIE map const fastCurieMap = activeCtx.fastCurieMap = {}; const irisToTerms = {}; // handle default language const defaultLanguage = activeCtx['@language'] || '@none'; // create term selections for each mapping in the context, ordered by // shortest and then lexicographically least const mappings = activeCtx.mappings; const terms = [...mappings.keys()].sort(_compareShortestLeast); for(const term of terms) { const mapping = mappings.get(term); if(mapping === null) { continue; } let container = mapping['@container'] || '@none'; container = [].concat(container).sort().join(''); // iterate over every IRI in the mapping const ids = _asArray(mapping['@id']); for(const iri of ids) { let entry = inverse[iri]; const isKeyword = api.isKeyword(iri); if(!entry) { // initialize entry inverse[iri] = entry = {}; if(!isKeyword && !mapping._termHasColon) { // init IRI to term map and fast CURIE prefixes irisToTerms[iri] = [term]; const fastCurieEntry = {iri, terms: irisToTerms[iri]}; if(iri[0] in fastCurieMap) { fastCurieMap[iri[0]].push(fastCurieEntry); } else { fastCurieMap[iri[0]] = [fastCurieEntry]; } } } else if(!isKeyword && !mapping._termHasColon) { // add IRI to term match irisToTerms[iri].push(term); } // add new entry if(!entry[container]) { entry[container] = { '@language': {}, '@type': {}, '@any': {} }; } entry = entry[container]; _addPreferredTerm(term, entry['@any'], '@none'); if(mapping.reverse) { // term is preferred for values using @reverse _addPreferredTerm(term, entry['@type'], '@reverse'); } else if('@type' in mapping) { // term is preferred for values using specific type _addPreferredTerm(term, entry['@type'], mapping['@type']); } else if('@language' in mapping) { // term is preferred for values using specific language const language = mapping['@language'] || '@null'; _addPreferredTerm(term, entry['@language'], language); } else { // term is preferred for values w/default language or no type and // no language // add an entry for the default language _addPreferredTerm(term, entry['@language'], defaultLanguage); // add entries for no type and no language _addPreferredTerm(term, entry['@type'], '@none'); _addPreferredTerm(term, entry['@language'], '@none'); } } } // build fast CURIE map for(const key in fastCurieMap) { _buildIriMap(fastCurieMap, key, 1); } return inverse; } /** * Runs a recursive algorithm to build a lookup map for quickly finding * potential CURIEs. * * @param iriMap the map to build. * @param key the current key in the map to work on. * @param idx the index into the IRI to compare. */ function _buildIriMap(iriMap, key, idx) { const entries = iriMap[key]; const next = iriMap[key] = {}; let iri; let letter; for(const entry of entries) { iri = entry.iri; if(idx >= iri.length) { letter = ''; } else { letter = iri[idx]; } if(letter in next) { next[letter].push(entry); } else { next[letter] = [entry]; } } for(const key in next) { if(key === '') { continue; } _buildIriMap(next, key, idx + 1); } } /** * Adds the term for the given entry if not already added. * * @param term the term to add. * @param entry the inverse context typeOrLanguage entry to add to. * @param typeOrLanguageValue the key in the entry to add to. */ function _addPreferredTerm(term, entry, typeOrLanguageValue) { if(!entry.hasOwnProperty(typeOrLanguageValue)) { entry[typeOrLanguageValue] = term; } } /** * Clones an active context, creating a child active context. * * @return a clone (child) of the active context. */ function _cloneActiveContext() { const child = {}; child['@base'] = this['@base']; child.mappings = util.clone(this.mappings); child.clone = this.clone; child.inverse = null; child.getInverse = this.getInverse; child.protected = util.clone(this.protected); if(this.previousContext) { child.isPropertyTermScoped = this.previousContext.isPropertyTermScoped; child.previousContext = this.previousContext.clone(); } child.revertTypeScopedContext = this.revertTypeScopedContext; if('@language' in this) { child['@language'] = this['@language']; } if('@vocab' in this) { child['@vocab'] = this['@vocab']; } return child; } /** * Reverts any type-scoped context in this active context to the previous * context. */ function _revertTypeScopedContext() { if(!this.previousContext) { return this; } return this.previousContext.clone(); } }; /** * Gets the value for the given active context key and type, null if none is * set or undefined if none is set and type is '@context'. * * @param ctx the active context. * @param key the context key. * @param [type] the type of value to get (eg: '@id', '@type'), if not * specified gets the entire entry for a key, null if not found. * * @return the value, null, or undefined. */ api.getContextValue = (ctx, key, type) => { // invalid key if(key === null) { if(type === '@context') { return undefined; } return null; } // get specific entry information if(ctx.mappings.has(key)) { const entry = ctx.mappings.get(key); if(_isUndefined(type)) { // return whole entry return entry; } if(entry.hasOwnProperty(type)) { // return entry value for type return entry[type]; } } // get default language if(type === '@language' && ctx.hasOwnProperty(type)) { return ctx[type]; } if(type === '@context') { return undefined; } return null; }; /** * Retrieves external @context URLs using the given document loader. Every * instance of @context in the input that refers to a URL will be replaced * with the JSON @context found at that URL. * * @param input the JSON-LD input with possible contexts. * @param options the options to use: * documentLoader(url, [callback(err, remoteDoc)]) the document loader. * @param callback(err, input) called once the operation completes. */ api.getAllContexts = async (input, options) => { return _retrieveContextUrls(input, options); }; /** * Processing Mode check. * * @param activeCtx the current active context. * @param version the string or numeric version to check. * * @return boolean. */ api.processingMode = (activeCtx, version) => { if(version.toString() >= '1.1') { return activeCtx.processingMode && activeCtx.processingMode >= 'json-ld-' + version.toString(); } else { return !activeCtx.processingMode || activeCtx.processingMode === 'json-ld-1.0'; } }; /** * Returns whether or not the given value is a keyword. * * @param v the value to check. * * @return true if the value is a keyword, false if not. */ api.isKeyword = v => { if(!_isString(v)) { return false; } switch(v) { case '@base': case '@container': case '@context': case '@default': case '@embed': case '@explicit': case '@graph': case '@id': case '@index': case '@language': case '@list': case '@nest': case '@none': case '@omitDefault': case '@prefix': case '@preserve': case '@protected': case '@requireAll': case '@reverse': case '@set': case '@type': case '@value': case '@version': case '@vocab': return true; } return false; }; async function _retrieveContextUrls(input, options) { const documentLoader = util.normalizeDocumentLoader(options.documentLoader); // retrieve all @context URLs in input await retrieve(input, new Set(), documentLoader); return input; // recursive function that will retrieve all @context URLs in documents async function retrieve(doc, cycles, documentLoader) { if(cycles.size > MAX_CONTEXT_URLS) { throw new JsonLdError( 'Maximum number of @context URLs exceeded.', 'jsonld.ContextUrlError', {code: 'loading remote context failed', max: MAX_CONTEXT_URLS}); } // find all URLs in the given document const urls = new Map(); _findContextUrls(doc, urls, false, options.base); if(urls.size === 0) { return; } // queue all unretrieved URLs const queue = [...urls.keys()].filter(u => urls.get(u) === false); // retrieve URLs in queue return Promise.all(queue.map(async url => { // check for context URL cycle if(cycles.has(url)) { throw new JsonLdError( 'Cyclical @context URLs detected.', 'jsonld.ContextUrlError', {code: 'recursive context inclusion', url}); } const _cycles = new Set(cycles); _cycles.add(url); let remoteDoc; let ctx; try { remoteDoc = await documentLoader(url); ctx = remoteDoc.document || null; // parse string context as JSON if(_isString(ctx)) { ctx = JSON.parse(ctx); } } catch(e) { throw new JsonLdError( 'Dereferencing a URL did not result in a valid JSON-LD object. ' + 'Possible causes are an inaccessible URL perhaps due to ' + 'a same-origin policy (ensure the server uses CORS if you are ' + 'using client-side JavaScript), too many redirects, a ' + 'non-JSON response, or more than one HTTP Link Header was ' + 'provided for a remote context.', 'jsonld.InvalidUrl', {code: 'loading remote context failed', url, cause: e}); } // ensure ctx is an object if(!_isObject(ctx)) { throw new JsonLdError( 'Dereferencing a URL did not result in a JSON object. The ' + 'response was valid JSON, but it was not a JSON object.', 'jsonld.InvalidUrl', {code: 'invalid remote context', url}); } // use empty context if no @context key is present if(!('@context' in ctx)) { ctx = {'@context': {}}; } else { ctx = {'@context': ctx['@context']}; } // append @context URL to context if given if(remoteDoc.contextUrl) { if(!_isArray(ctx['@context'])) { ctx['@context'] = [ctx['@context']]; } ctx['@context'].push(remoteDoc.contextUrl); } // recurse await retrieve(ctx, _cycles, documentLoader); // store retrieved context w/replaced @context URLs urls.set(url, ctx['@context']); // replace all @context URLs in the document _findContextUrls(doc, urls, true, options.base); })); } } /** * Finds all @context URLs in the given JSON-LD input. * * @param input the JSON-LD input. * @param urls a map of URLs (url => false/@contexts). * @param replace true to replace the URLs in the given input with the * @contexts from the urls map, false not to. * @param base the base IRI to use to resolve relative IRIs. * * @return true if new URLs to retrieve were found, false if not. */ function _findContextUrls(input, urls, replace, base) { if(_isArray(input)) { for(const element of input) { _findContextUrls(element, urls, replace, base); } return; } if(!_isObject(input)) { // no @context URLs can be found in non-object input return; } // input is an object for(const key in input) { if(key !== '@context') { _findContextUrls(input[key], urls, replace, base); continue; } // get @context const ctx = input[key]; if(_isArray(ctx)) { // array @context let length = ctx.length; for(let i = 0; i < length; ++i) { const _ctx = ctx[i]; if(_isString(_ctx)) { const prepended = prependBase(base, _ctx); const resolved = urls.get(prepended); // replace w/@context if requested if(replace) { if(_isArray(resolved)) { // add flattened context Array.prototype.splice.apply(ctx, [i, 1].concat(resolved)); i += resolved.length - 1; length = ctx.length; } else if(resolved !== false) { ctx[i] = resolved; } } else if(resolved === undefined) { // @context URL found urls.set(prepended, false); } } else { // look for scoped context for(const key in _ctx) { if(_isObject(_ctx[key])) { _findContextUrls(_ctx[key], urls, replace, base); } } } } } else if(_isString(ctx)) { // string @context const prepended = prependBase(base, ctx); const resolved = urls.get(prepended); // replace w/@context if requested if(replace) { if(resolved !== false) { input[key] = resolved; } } else if(resolved === undefined) { // @context URL found urls.set(prepended, false); } } else { // look for scoped context for(const key in ctx) { if(_isObject(ctx[key])) { _findContextUrls(ctx[key], urls, replace, base); } } } } } function _deepCompare(x1, x2) { // compare `null` or primitive types directly if((!(x1 && typeof x1 === 'object')) || (!(x2 && typeof x2 === 'object'))) { return x1 === x2; } // x1 and x2 are objects (also potentially arrays) const x1Array = Array.isArray(x1); if(x1Array !== Array.isArray(x2)) { return false; } if(x1Array) { if(x1.length !== x2.length) { return false; } for(let i = 0; i < x1.length; ++i) { if(!_deepCompare(x1[i], x2[i])) { return false; } } return true; } // x1 and x2 are non-array objects const k1s = Object.keys(x1); const k2s = Object.keys(x2); if(k1s.length !== k2s.length) { return false; } for(const k1 in x1) { let v1 = x1[k1]; let v2 = x2[k1]; // special case: `@container` can be in any order if(k1 === '@container') { if(Array.isArray(v1) && Array.isArray(v2)) { v1 = v1.slice().sort(); v2 = v2.slice().sort(); } } if(!_deepCompare(v1, v2)) { return false; } } return true; } jsonld.js-1.6.2/lib/documentLoaders/000077500000000000000000000000001347107445200173315ustar00rootroot00000000000000jsonld.js-1.6.2/lib/documentLoaders/node.js000066400000000000000000000117171347107445200206230ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {parseLinkHeader, buildHeaders} = require('../util'); const {LINK_HEADER_REL} = require('../constants'); const JsonLdError = require('../JsonLdError'); const RequestQueue = require('../RequestQueue'); /** * Creates a built-in node document loader. * * @param options the options to use: * secure: require all URLs to use HTTPS. * strictSSL: true to require SSL certificates to be valid, * false not to (default: true). * maxRedirects: the maximum number of redirects to permit, none by * default. * request: the object which will make the request, default is * provided by `https://www.npmjs.com/package/request`. * headers: an object (map) of headers which will be passed as request * headers for the requested document. Accept is not allowed. * * @return the node document loader. */ module.exports = ({ secure, strictSSL = true, maxRedirects = -1, request, headers = {} } = {strictSSL: true, maxRedirects: -1, headers: {}}) => { headers = buildHeaders(headers); // TODO: use `r2` request = request || require('request'); const http = require('http'); // TODO: disable cache until HTTP caching implemented //const cache = new DocumentCache(); const queue = new RequestQueue(); return queue.wrapLoader(function(url) { return loadDocument(url, []); }); async function loadDocument(url, redirects) { if(url.indexOf('http:') !== 0 && url.indexOf('https:') !== 0) { throw new JsonLdError( 'URL could not be dereferenced; only "http" and "https" URLs are ' + 'supported.', 'jsonld.InvalidUrl', {code: 'loading document failed', url}); } if(secure && url.indexOf('https') !== 0) { throw new JsonLdError( 'URL could not be dereferenced; secure mode is enabled and ' + 'the URL\'s scheme is not "https".', 'jsonld.InvalidUrl', {code: 'loading document failed', url}); } // TODO: disable cache until HTTP caching implemented let doc = null;//cache.get(url); if(doc !== null) { return doc; } let result; try { result = await _request(request, { url, headers, strictSSL, followRedirect: false }); } catch(e) { throw new JsonLdError( 'URL could not be dereferenced, an error occurred.', 'jsonld.LoadDocumentError', {code: 'loading document failed', url, cause: e}); } const {res, body} = result; doc = {contextUrl: null, documentUrl: url, document: body || null}; // handle error const statusText = http.STATUS_CODES[res.statusCode]; if(res.statusCode >= 400) { throw new JsonLdError( 'URL could not be dereferenced: ' + statusText, 'jsonld.InvalidUrl', { code: 'loading document failed', url, httpStatusCode: res.statusCode }); } // handle Link Header if(res.headers.link && res.headers['content-type'] !== 'application/ld+json') { // only 1 related link header permitted const linkHeader = parseLinkHeader(res.headers.link)[LINK_HEADER_REL]; if(Array.isArray(linkHeader)) { throw new JsonLdError( 'URL could not be dereferenced, it has more than one associated ' + 'HTTP Link Header.', 'jsonld.InvalidUrl', {code: 'multiple context link headers', url}); } if(linkHeader) { doc.contextUrl = linkHeader.target; } } // handle redirect if(res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { if(redirects.length === maxRedirects) { throw new JsonLdError( 'URL could not be dereferenced; there were too many redirects.', 'jsonld.TooManyRedirects', { code: 'loading document failed', url, httpStatusCode: res.statusCode, redirects }); } if(redirects.indexOf(url) !== -1) { throw new JsonLdError( 'URL could not be dereferenced; infinite redirection was detected.', 'jsonld.InfiniteRedirectDetected', { code: 'recursive context inclusion', url, httpStatusCode: res.statusCode, redirects }); } redirects.push(url); return loadDocument(res.headers.location, redirects); } // cache for each redirected URL redirects.push(url); // TODO: disable cache until HTTP caching implemented /* for(let i = 0; i < redirects.length; ++i) { cache.set( redirects[i], {contextUrl: null, documentUrl: redirects[i], document: body}); } */ return doc; } }; function _request(request, options) { return new Promise((resolve, reject) => { request(options, (err, res, body) => { if(err) { reject(err); } else { resolve({res, body}); } }); }); } jsonld.js-1.6.2/lib/documentLoaders/xhr.js000066400000000000000000000063461347107445200205010ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {parseLinkHeader, buildHeaders} = require('../util'); const {LINK_HEADER_REL} = require('../constants'); const JsonLdError = require('../JsonLdError'); const RequestQueue = require('../RequestQueue'); const REGEX_LINK_HEADER = /(^|(\r\n))link:/i; /** * Creates a built-in XMLHttpRequest document loader. * * @param options the options to use: * secure: require all URLs to use HTTPS. * headers: an object (map) of headers which will be passed as request * headers for the requested document. Accept is not allowed. * [xhr]: the XMLHttpRequest API to use. * * @return the XMLHttpRequest document loader. */ module.exports = ({ secure, headers = {}, xhr } = {headers: {}}) => { headers = buildHeaders(headers); const queue = new RequestQueue(); return queue.wrapLoader(loader); async function loader(url) { if(url.indexOf('http:') !== 0 && url.indexOf('https:') !== 0) { throw new JsonLdError( 'URL could not be dereferenced; only "http" and "https" URLs are ' + 'supported.', 'jsonld.InvalidUrl', {code: 'loading document failed', url}); } if(secure && url.indexOf('https') !== 0) { throw new JsonLdError( 'URL could not be dereferenced; secure mode is enabled and ' + 'the URL\'s scheme is not "https".', 'jsonld.InvalidUrl', {code: 'loading document failed', url}); } let req; try { req = await _get(xhr, url, headers); } catch(e) { throw new JsonLdError( 'URL could not be dereferenced, an error occurred.', 'jsonld.LoadDocumentError', {code: 'loading document failed', url, cause: e}); } if(req.status >= 400) { throw new JsonLdError( 'URL could not be dereferenced: ' + req.statusText, 'jsonld.LoadDocumentError', { code: 'loading document failed', url, httpStatusCode: req.status }); } const doc = {contextUrl: null, documentUrl: url, document: req.response}; // handle Link Header (avoid unsafe header warning by existence testing) const contentType = req.getResponseHeader('Content-Type'); let linkHeader; if(REGEX_LINK_HEADER.test(req.getAllResponseHeaders())) { linkHeader = req.getResponseHeader('Link'); } if(linkHeader && contentType !== 'application/ld+json') { // only 1 related link header permitted linkHeader = parseLinkHeader(linkHeader)[LINK_HEADER_REL]; if(Array.isArray(linkHeader)) { throw new JsonLdError( 'URL could not be dereferenced, it has more than one ' + 'associated HTTP Link Header.', 'jsonld.InvalidUrl', {code: 'multiple context link headers', url}); } if(linkHeader) { doc.contextUrl = linkHeader.target; } } return doc; } }; function _get(xhr, url, headers) { xhr = xhr || XMLHttpRequest; const req = new xhr(); return new Promise((resolve, reject) => { req.onload = () => resolve(req); req.onerror = err => reject(err); req.open('GET', url, true); for(const k in headers) { req.setRequestHeader(k, headers[k]); } req.send(); }); } jsonld.js-1.6.2/lib/expand.js000066400000000000000000000715711347107445200160310ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const JsonLdError = require('./JsonLdError'); const { isArray: _isArray, isObject: _isObject, isEmptyObject: _isEmptyObject, isString: _isString, isUndefined: _isUndefined } = require('./types'); const { isList: _isList, isValue: _isValue, isGraph: _isGraph } = require('./graphTypes'); const { expandIri: _expandIri, getContextValue: _getContextValue, isKeyword: _isKeyword, process: _processContext } = require('./context'); const { isAbsolute: _isAbsoluteIri } = require('./url'); const { addValue: _addValue, asArray: _asArray, getValues: _getValues, validateTypeValue: _validateTypeValue } = require('./util'); const api = {}; module.exports = api; /** * Recursively expands an element using the given context. Any context in * the element will be removed. All context URLs must have been retrieved * before calling this method. * * @param activeCtx the context to use. * @param activeProperty the property for the element, null for none. * @param element the element to expand. * @param options the expansion options. * @param insideList true if the element is a list, false if not. * @param insideIndex true if the element is inside an index container, * false if not. * @param typeScopedContext an optional type-scoped active context for * expanding values of nodes that were expressed according to * a type-scoped context. * @param expansionMap(info) a function that can be used to custom map * unmappable values (or to throw an error when they are detected); * if this function returns `undefined` then the default behavior * will be used. * * @return a Promise that resolves to the expanded value. */ api.expand = ({ activeCtx, activeProperty = null, element, options = {}, insideList = false, insideIndex = false, typeScopedContext = null, expansionMap = () => undefined }) => { // nothing to expand if(element === null || element === undefined) { return null; } // disable framing if activeProperty is @default if(activeProperty === '@default') { options = Object.assign({}, options, {isFrame: false}); } if(!_isArray(element) && !_isObject(element)) { // drop free-floating scalars that are not in lists unless custom mapped if(!insideList && (activeProperty === null || _expandIri(activeCtx, activeProperty, {vocab: true}, options) === '@graph')) { // TODO: use `await` to support async const mapped = expansionMap({ unmappedValue: element, activeCtx, activeProperty, options, insideList }); if(mapped === undefined) { return null; } return mapped; } // expand element according to value expansion rules return _expandValue({activeCtx, activeProperty, value: element, options}); } // recursively expand array if(_isArray(element)) { let rval = []; const container = _getContextValue( activeCtx, activeProperty, '@container') || []; insideList = insideList || container.includes('@list'); for(let i = 0; i < element.length; ++i) { // expand element let e = api.expand({ activeCtx, activeProperty, element: element[i], options, expansionMap, insideIndex, typeScopedContext }); if(insideList && (_isArray(e) || _isList(e))) { // lists of lists are illegal throw new JsonLdError( 'Invalid JSON-LD syntax; lists of lists are not permitted.', 'jsonld.SyntaxError', {code: 'list of lists'}); } if(e === null) { // TODO: add `await` for async support e = expansionMap({ unmappedValue: element[i], activeCtx, activeProperty, parent: element, index: i, options, expandedParent: rval, insideList }); if(e === undefined) { continue; } } if(_isArray(e)) { rval = rval.concat(e); } else { rval.push(e); } } return rval; } // recursively expand object: // first, expand the active property const expandedActiveProperty = _expandIri( activeCtx, activeProperty, {vocab: true}, options); // second, determine if any type-scoped context should be reverted; it // should only be reverted when the following are all true: // 1. `element` is not a value or subject reference // 2. `insideIndex` is false typeScopedContext = typeScopedContext || (activeCtx.previousContext ? activeCtx : null); let keys = Object.keys(element).sort(); let mustRevert = !insideIndex; if(mustRevert && typeScopedContext && keys.length <= 2 && !keys.includes('@context')) { for(const key of keys) { const expandedProperty = _expandIri( typeScopedContext, key, {vocab: true}, options); if(expandedProperty === '@value') { // value found, ensure type-scoped context is used to expand it mustRevert = false; activeCtx = typeScopedContext; break; } if(expandedProperty === '@id' && keys.length === 1) { // subject reference found, do not revert mustRevert = false; break; } } } if(mustRevert) { // revert type scoped context activeCtx = activeCtx.revertTypeScopedContext(); } // if element has a context, process it if('@context' in element) { activeCtx = _processContext( {activeCtx, localCtx: element['@context'], options}); } // look for scoped contexts on `@type` for(const key of keys) { const expandedProperty = _expandIri(activeCtx, key, {vocab: true}, options); if(expandedProperty === '@type') { // set scoped contexts from @type // avoid sorting if possible const value = element[key]; const types = Array.isArray(value) ? (value.length > 1 ? value.slice().sort() : value) : [value]; for(const type of types) { const ctx = _getContextValue( activeCtx.previousContext || activeCtx, type, '@context'); if(!_isUndefined(ctx)) { activeCtx = _processContext({ activeCtx, localCtx: ctx, options, isTypeScopedContext: true }); } } } } // process each key and value in element, ignoring @nest content let rval = {}; _expandObject({ activeCtx, activeProperty, expandedActiveProperty, element, expandedParent: rval, options, insideList, typeScopedContext, expansionMap}); // get property count on expanded output keys = Object.keys(rval); let count = keys.length; if('@value' in rval) { // @value must only have @language or @type if('@type' in rval && '@language' in rval) { throw new JsonLdError( 'Invalid JSON-LD syntax; an element containing "@value" may not ' + 'contain both "@type" and "@language".', 'jsonld.SyntaxError', {code: 'invalid value object', element: rval}); } let validCount = count - 1; if('@type' in rval) { validCount -= 1; } if('@index' in rval) { validCount -= 1; } if('@language' in rval) { validCount -= 1; } if(validCount !== 0) { throw new JsonLdError( 'Invalid JSON-LD syntax; an element containing "@value" may only ' + 'have an "@index" property and at most one other property ' + 'which can be "@type" or "@language".', 'jsonld.SyntaxError', {code: 'invalid value object', element: rval}); } const values = rval['@value'] === null ? [] : _asArray(rval['@value']); const types = _getValues(rval, '@type'); // drop null @values unless custom mapped if(values.length === 0) { // TODO: use `await` to support async const mapped = expansionMap({ unmappedValue: rval, activeCtx, activeProperty, element, options, insideList }); if(mapped !== undefined) { rval = mapped; } else { rval = null; } } else if(!values.every(v => (_isString(v) || _isEmptyObject(v))) && '@language' in rval) { // if @language is present, @value must be a string throw new JsonLdError( 'Invalid JSON-LD syntax; only strings may be language-tagged.', 'jsonld.SyntaxError', {code: 'invalid language-tagged value', element: rval}); } else if(!types.every(t => (_isAbsoluteIri(t) && !(_isString(t) && t.indexOf('_:') === 0) || _isEmptyObject(t)))) { throw new JsonLdError( 'Invalid JSON-LD syntax; an element containing "@value" and "@type" ' + 'must have an absolute IRI for the value of "@type".', 'jsonld.SyntaxError', {code: 'invalid typed value', element: rval}); } } else if('@type' in rval && !_isArray(rval['@type'])) { // convert @type to an array rval['@type'] = [rval['@type']]; } else if('@set' in rval || '@list' in rval) { // handle @set and @list if(count > 1 && !(count === 2 && '@index' in rval)) { throw new JsonLdError( 'Invalid JSON-LD syntax; if an element has the property "@set" ' + 'or "@list", then it can have at most one other property that is ' + '"@index".', 'jsonld.SyntaxError', {code: 'invalid set or list object', element: rval}); } // optimize away @set if('@set' in rval) { rval = rval['@set']; keys = Object.keys(rval); count = keys.length; } } else if(count === 1 && '@language' in rval) { // drop objects with only @language unless custom mapped // TODO: use `await` to support async const mapped = expansionMap(rval, { unmappedValue: rval, activeCtx, activeProperty, element, options, insideList }); if(mapped !== undefined) { rval = mapped; } else { rval = null; } } // drop certain top-level objects that do not occur in lists, unless custom // mapped if(_isObject(rval) && !options.keepFreeFloatingNodes && !insideList && (activeProperty === null || expandedActiveProperty === '@graph')) { // drop empty object, top-level @value/@list, or object with only @id if(count === 0 || '@value' in rval || '@list' in rval || (count === 1 && '@id' in rval)) { // TODO: use `await` to support async const mapped = expansionMap({ unmappedValue: rval, activeCtx, activeProperty, element, options, insideList }); if(mapped !== undefined) { rval = mapped; } else { rval = null; } } } return rval; }; /** * Expand each key and value of element adding to result * * @param activeCtx the context to use. * @param activeProperty the property for the element. * @param expandedActiveProperty the expansion of activeProperty * @param element the element to expand. * @param expandedParent the expanded result into which to add values. * @param options the expansion options. * @param insideList true if the element is a list, false if not. * @param expansionMap(info) a function that can be used to custom map * unmappable values (or to throw an error when they are detected); * if this function returns `undefined` then the default behavior * will be used. */ function _expandObject({ activeCtx, activeProperty, expandedActiveProperty, element, expandedParent, options = {}, insideList, expansionMap }) { const keys = Object.keys(element).sort(); const nests = []; for(const key of keys) { let value = element[key]; let expandedValue; // skip @context if(key === '@context') { continue; } // expand property let expandedProperty = _expandIri(activeCtx, key, {vocab: true}, options); // drop non-absolute IRI keys that aren't keywords unless custom mapped if(expandedProperty === null || !(_isAbsoluteIri(expandedProperty) || _isKeyword(expandedProperty))) { // TODO: use `await` to support async expandedProperty = expansionMap({ unmappedProperty: key, activeCtx, activeProperty, parent: element, options, insideList, value, expandedParent }); if(expandedProperty === undefined) { continue; } } if(_isKeyword(expandedProperty)) { if(expandedActiveProperty === '@reverse') { throw new JsonLdError( 'Invalid JSON-LD syntax; a keyword cannot be used as a @reverse ' + 'property.', 'jsonld.SyntaxError', {code: 'invalid reverse property map', value}); } if(expandedProperty in expandedParent) { throw new JsonLdError( 'Invalid JSON-LD syntax; colliding keywords detected.', 'jsonld.SyntaxError', {code: 'colliding keywords', keyword: expandedProperty}); } } // syntax error if @id is not a string if(expandedProperty === '@id') { if(!_isString(value)) { if(!options.isFrame) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@id" value must a string.', 'jsonld.SyntaxError', {code: 'invalid @id value', value}); } if(_isObject(value)) { // empty object is a wildcard if(!_isEmptyObject(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@id" value an empty object or array ' + 'of strings, if framing', 'jsonld.SyntaxError', {code: 'invalid @id value', value}); } } else if(_isArray(value)) { if(!value.every(v => _isString(v))) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@id" value an empty object or array ' + 'of strings, if framing', 'jsonld.SyntaxError', {code: 'invalid @id value', value}); } } else { throw new JsonLdError( 'Invalid JSON-LD syntax; "@id" value an empty object or array ' + 'of strings, if framing', 'jsonld.SyntaxError', {code: 'invalid @id value', value}); } } _addValue( expandedParent, '@id', _asArray(value).map(v => _isString(v) ? _expandIri(activeCtx, v, {base: true}, options) : v), {propertyIsArray: options.isFrame}); continue; } if(expandedProperty === '@type') { _validateTypeValue(value); _addValue( expandedParent, '@type', _asArray(value).map(v => _isString(v) ? _expandIri(activeCtx.previousContext || activeCtx, v, {base: true, vocab: true}, options) : v), {propertyIsArray: options.isFrame}); continue; } // @graph must be an array or an object if(expandedProperty === '@graph' && !(_isObject(value) || _isArray(value))) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@graph" value must not be an ' + 'object or an array.', 'jsonld.SyntaxError', {code: 'invalid @graph value', value}); } // @value must not be an object or an array (unless framing) if(expandedProperty === '@value') { if((_isObject(value) || _isArray(value)) && !options.isFrame) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@value" value must not be an ' + 'object or an array.', 'jsonld.SyntaxError', {code: 'invalid value object value', value}); } _addValue( expandedParent, '@value', value, {propertyIsArray: options.isFrame}); continue; } // @language must be a string if(expandedProperty === '@language') { if(value === null) { // drop null @language values, they expand as if they didn't exist continue; } if(!_isString(value) && !options.isFrame) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@language" value must be a string.', 'jsonld.SyntaxError', {code: 'invalid language-tagged string', value}); } // ensure language value is lowercase value = _asArray(value).map(v => _isString(v) ? v.toLowerCase() : v); _addValue( expandedParent, '@language', value, {propertyIsArray: options.isFrame}); continue; } // @index must be a string if(expandedProperty === '@index') { if(!_isString(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@index" value must be a string.', 'jsonld.SyntaxError', {code: 'invalid @index value', value}); } _addValue(expandedParent, '@index', value); continue; } // @reverse must be an object if(expandedProperty === '@reverse') { if(!_isObject(value)) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@reverse" value must be an object.', 'jsonld.SyntaxError', {code: 'invalid @reverse value', value}); } expandedValue = api.expand({ activeCtx, activeProperty: '@reverse', element: value, options, expansionMap }); // properties double-reversed if('@reverse' in expandedValue) { for(const property in expandedValue['@reverse']) { _addValue( expandedParent, property, expandedValue['@reverse'][property], {propertyIsArray: true}); } } // FIXME: can this be merged with code below to simplify? // merge in all reversed properties let reverseMap = expandedParent['@reverse'] || null; for(const property in expandedValue) { if(property === '@reverse') { continue; } if(reverseMap === null) { reverseMap = expandedParent['@reverse'] = {}; } _addValue(reverseMap, property, [], {propertyIsArray: true}); const items = expandedValue[property]; for(let ii = 0; ii < items.length; ++ii) { const item = items[ii]; if(_isValue(item) || _isList(item)) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@reverse" value must not be a ' + '@value or an @list.', 'jsonld.SyntaxError', {code: 'invalid reverse property value', value: expandedValue}); } _addValue(reverseMap, property, item, {propertyIsArray: true}); } } continue; } // nested keys if(expandedProperty === '@nest') { nests.push(key); continue; } // use potential scoped context for key let termCtx = activeCtx; const ctx = _getContextValue(activeCtx, key, '@context'); if(!_isUndefined(ctx)) { // Note: spec's `from term` var is named `isPropertyTermScopedContext` termCtx = _processContext({ activeCtx, localCtx: ctx, isPropertyTermScopedContext: true, options }); } const container = _getContextValue(termCtx, key, '@container') || []; if(container.includes('@language') && _isObject(value)) { // handle language map container (skip if value is not an object) expandedValue = _expandLanguageMap(termCtx, value, options); } else if(container.includes('@index') && _isObject(value)) { // handle index container (skip if value is not an object) const asGraph = container.includes('@graph'); expandedValue = _expandIndexMap({ activeCtx: termCtx, options, activeProperty: key, value, expansionMap, asGraph, indexKey: '@index' }); } else if(container.includes('@id') && _isObject(value)) { // handle id container (skip if value is not an object) const asGraph = container.includes('@graph'); expandedValue = _expandIndexMap({ activeCtx: termCtx, options, activeProperty: key, value, expansionMap, asGraph, indexKey: '@id' }); } else if(container.includes('@type') && _isObject(value)) { // handle type container (skip if value is not an object) expandedValue = _expandIndexMap({ // since container is `@type`, revert type scoped context when expanding activeCtx: termCtx.revertTypeScopedContext(), options, activeProperty: key, value, expansionMap, asGraph: false, indexKey: '@type' }); } else { // recurse into @list or @set const isList = (expandedProperty === '@list'); if(isList || expandedProperty === '@set') { let nextActiveProperty = activeProperty; if(isList && expandedActiveProperty === '@graph') { nextActiveProperty = null; } expandedValue = api.expand({ activeCtx: termCtx, activeProperty: nextActiveProperty, element: value, options, insideList: isList, expansionMap }); if(isList && _isList(expandedValue)) { throw new JsonLdError( 'Invalid JSON-LD syntax; lists of lists are not permitted.', 'jsonld.SyntaxError', {code: 'list of lists'}); } } else { // recursively expand value with key as new active property expandedValue = api.expand({ activeCtx: termCtx, activeProperty: key, element: value, options, insideList: false, expansionMap }); } } // drop null values if property is not @value if(expandedValue === null && expandedProperty !== '@value') { // TODO: use `await` to support async expandedValue = expansionMap({ unmappedValue: value, expandedProperty, activeCtx: termCtx, activeProperty, parent: element, options, insideList, key, expandedParent }); if(expandedValue === undefined) { continue; } } // convert expanded value to @list if container specifies it if(expandedProperty !== '@list' && !_isList(expandedValue) && container.includes('@list')) { // ensure expanded value in @list is an array expandedValue = {'@list': _asArray(expandedValue)}; } // convert expanded value to @graph if container specifies it // and value is not, itself, a graph // index cases handled above if(container.includes('@graph') && !container.some(key => key === '@id' || key === '@index')) { // ensure expanded values are arrays expandedValue = _asArray(expandedValue) .map(v => _isGraph(v) ? v : {'@graph': _asArray(v)}); } // FIXME: can this be merged with code above to simplify? // merge in reverse properties if(termCtx.mappings.has(key) && termCtx.mappings.get(key).reverse) { const reverseMap = expandedParent['@reverse'] = expandedParent['@reverse'] || {}; expandedValue = _asArray(expandedValue); for(let ii = 0; ii < expandedValue.length; ++ii) { const item = expandedValue[ii]; if(_isValue(item) || _isList(item)) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@reverse" value must not be a ' + '@value or an @list.', 'jsonld.SyntaxError', {code: 'invalid reverse property value', value: expandedValue}); } _addValue(reverseMap, expandedProperty, item, {propertyIsArray: true}); } continue; } // add value for property // use an array except for certain keywords const useArray = !['@index', '@id', '@type', '@value', '@language'] .includes(expandedProperty); _addValue(expandedParent, expandedProperty, expandedValue, { propertyIsArray: useArray }); } // expand each nested key for(const key of nests) { const nestedValues = _isArray(element[key]) ? element[key] : [element[key]]; for(const nv of nestedValues) { if(!_isObject(nv) || Object.keys(nv).some(k => _expandIri(activeCtx, k, {vocab: true}, options) === '@value')) { throw new JsonLdError( 'Invalid JSON-LD syntax; nested value must be a node object.', 'jsonld.SyntaxError', {code: 'invalid @nest value', value: nv}); } _expandObject({ activeCtx, activeProperty, expandedActiveProperty, element: nv, expandedParent, options, insideList, expansionMap}); } } } /** * Expands the given value by using the coercion and keyword rules in the * given context. * * @param activeCtx the active context to use. * @param activeProperty the active property the value is associated with. * @param value the value to expand. * @param {Object} [options] - processing options. * * @return the expanded value. */ function _expandValue({activeCtx, activeProperty, value, options}) { // nothing to expand if(value === null || value === undefined) { return null; } // special-case expand @id and @type (skips '@id' expansion) const expandedProperty = _expandIri( activeCtx, activeProperty, {vocab: true}, options); if(expandedProperty === '@id') { return _expandIri(activeCtx, value, {base: true}, options); } else if(expandedProperty === '@type') { return _expandIri(activeCtx, value, {vocab: true, base: true}, options); } // get type definition from context const type = _getContextValue(activeCtx, activeProperty, '@type'); // do @id expansion (automatic for @graph) if((type === '@id' || expandedProperty === '@graph') && _isString(value)) { return {'@id': _expandIri(activeCtx, value, {base: true}, options)}; } // do @id expansion w/vocab if(type === '@vocab' && _isString(value)) { return { '@id': _expandIri(activeCtx, value, {vocab: true, base: true}, options) }; } // do not expand keyword values if(_isKeyword(expandedProperty)) { return value; } const rval = {}; if(type && !['@id', '@vocab'].includes(type)) { // other type rval['@type'] = type; } else if(_isString(value)) { // check for language tagging for strings const language = _getContextValue(activeCtx, activeProperty, '@language'); if(language !== null) { rval['@language'] = language; } } // do conversion of values that aren't basic JSON types to strings if(!['boolean', 'number', 'string'].includes(typeof value)) { value = value.toString(); } rval['@value'] = value; return rval; } /** * Expands a language map. * * @param activeCtx the active context to use. * @param languageMap the language map to expand. * @param {Object} [options] - processing options. * * @return the expanded language map. */ function _expandLanguageMap(activeCtx, languageMap, options) { const rval = []; const keys = Object.keys(languageMap).sort(); for(const key of keys) { const expandedKey = _expandIri(activeCtx, key, {vocab: true}, options); let val = languageMap[key]; if(!_isArray(val)) { val = [val]; } for(const item of val) { if(item === null) { // null values are allowed (8.5) but ignored (3.1) continue; } if(!_isString(item)) { throw new JsonLdError( 'Invalid JSON-LD syntax; language map values must be strings.', 'jsonld.SyntaxError', {code: 'invalid language map value', languageMap}); } const val = {'@value': item}; if(expandedKey !== '@none') { val['@language'] = key.toLowerCase(); } rval.push(val); } } return rval; } function _expandIndexMap( {activeCtx, options, activeProperty, value, expansionMap, asGraph, indexKey}) { const rval = []; const keys = Object.keys(value).sort(); const isTypeIndex = indexKey === '@type'; for(let key of keys) { // if indexKey is @type, there may be a context defined for it if(isTypeIndex) { const ctx = _getContextValue(activeCtx, key, '@context'); if(!_isUndefined(ctx)) { activeCtx = _processContext({ activeCtx, localCtx: ctx, isTypeScopedContext: true, options }); } } let val = value[key]; if(!_isArray(val)) { val = [val]; } // expand for @type, but also for @none const expandedKey = _expandIri(activeCtx, key, {vocab: true}, options); if(indexKey === '@id') { // expand document relative key = _expandIri(activeCtx, key, {base: true}, options); } else if(isTypeIndex) { key = expandedKey; } val = api.expand({ activeCtx, activeProperty, element: val, options, insideList: false, insideIndex: true, expansionMap }); for(let item of val) { // If this is also a @graph container, turn items into graphs if(asGraph && !_isGraph(item)) { item = {'@graph': [item]}; } if(indexKey === '@type') { if(expandedKey === '@none') { // ignore @none } else if(item['@type']) { item['@type'] = [key].concat(item['@type']); } else { item['@type'] = [key]; } } else if(expandedKey !== '@none' && !(indexKey in item)) { item[indexKey] = key; } rval.push(item); } } return rval; } jsonld.js-1.6.2/lib/flatten.js000066400000000000000000000014631347107445200162000ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const { isSubjectReference: _isSubjectReference } = require('./graphTypes'); const { createMergedNodeMap: _createMergedNodeMap } = require('./nodeMap'); const api = {}; module.exports = api; /** * Performs JSON-LD flattening. * * @param input the expanded JSON-LD to flatten. * * @return the flattened output. */ api.flatten = input => { const defaultGraph = _createMergedNodeMap(input); // produce flattened output const flattened = []; const keys = Object.keys(defaultGraph).sort(); for(let ki = 0; ki < keys.length; ++ki) { const node = defaultGraph[keys[ki]]; // only add full subjects to top-level if(!_isSubjectReference(node)) { flattened.push(node); } } return flattened; }; jsonld.js-1.6.2/lib/frame.js000066400000000000000000000472721347107445200156450ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {isKeyword} = require('./context'); const graphTypes = require('./graphTypes'); const types = require('./types'); const util = require('./util'); const JsonLdError = require('./JsonLdError'); const { createNodeMap: _createNodeMap, mergeNodeMapGraphs: _mergeNodeMapGraphs } = require('./nodeMap'); const api = {}; module.exports = api; /** * Performs JSON-LD `merged` framing. * * @param input the expanded JSON-LD to frame. * @param frame the expanded JSON-LD frame to use. * @param options the framing options. * * @return the framed output. */ api.frameMergedOrDefault = (input, frame, options) => { // create framing state const state = { options, graph: '@default', graphMap: {'@default': {}}, graphStack: [], subjectStack: [], link: {}, bnodeMap: {} }; // produce a map of all graphs and name each bnode // FIXME: currently uses subjects from @merged graph only const issuer = new util.IdentifierIssuer('_:b'); _createNodeMap(input, state.graphMap, '@default', issuer); if(options.merged) { state.graphMap['@merged'] = _mergeNodeMapGraphs(state.graphMap); state.graph = '@merged'; } state.subjects = state.graphMap[state.graph]; // frame the subjects const framed = []; api.frame(state, Object.keys(state.subjects).sort(), frame, framed); // If pruning blank nodes, find those to prune if(options.pruneBlankNodeIdentifiers) { // remove all blank nodes appearing only once, done in compaction options.bnodesToClear = Object.keys(state.bnodeMap).filter(id => state.bnodeMap[id].length === 1); } return framed; }; /** * Frames subjects according to the given frame. * * @param state the current framing state. * @param subjects the subjects to filter. * @param frame the frame. * @param parent the parent subject or top-level array. * @param property the parent property, initialized to null. */ api.frame = (state, subjects, frame, parent, property = null) => { // validate the frame _validateFrame(frame); frame = frame[0]; // get flags for current frame const options = state.options; const flags = { embed: _getFrameFlag(frame, options, 'embed'), explicit: _getFrameFlag(frame, options, 'explicit'), requireAll: _getFrameFlag(frame, options, 'requireAll') }; // filter out subjects that match the frame const matches = _filterSubjects(state, subjects, frame, flags); // add matches to output const ids = Object.keys(matches).sort(); for(const id of ids) { const subject = matches[id]; if(flags.embed === '@link' && id in state.link) { // TODO: may want to also match an existing linked subject against // the current frame ... so different frames could produce different // subjects that are only shared in-memory when the frames are the same // add existing linked subject _addFrameOutput(parent, property, state.link[id]); continue; } /* Note: In order to treat each top-level match as a compartmentalized result, clear the unique embedded subjects map when the property is null, which only occurs at the top-level. */ if(property === null) { state.uniqueEmbeds = {[state.graph]: {}}; } else { state.uniqueEmbeds[state.graph] = state.uniqueEmbeds[state.graph] || {}; } // start output for subject const output = {}; output['@id'] = id; if(id.indexOf('_:') === 0) { util.addValue(state.bnodeMap, id, output, {propertyIsArray: true}); } state.link[id] = output; // if embed is @never or if a circular reference would be created by an // embed, the subject cannot be embedded, just add the reference; // note that a circular reference won't occur when the embed flag is // `@link` as the above check will short-circuit before reaching this point if(flags.embed === '@never' || _createsCircularReference(subject, state.graph, state.subjectStack)) { _addFrameOutput(parent, property, output); continue; } // if only the last match should be embedded if(flags.embed === '@last') { // remove any existing embed if(id in state.uniqueEmbeds[state.graph]) { _removeEmbed(state, id); } state.uniqueEmbeds[state.graph][id] = {parent, property}; } // push matching subject onto stack to enable circular embed checks state.subjectStack.push({subject, graph: state.graph}); // subject is also the name of a graph if(id in state.graphMap) { let recurse = false; let subframe = null; if(!('@graph' in frame)) { recurse = state.graph !== '@merged'; subframe = {}; } else { subframe = frame['@graph'][0]; if(!types.isObject(subframe)) { subframe = {}; } recurse = !(id === '@merged' || id === '@default'); } if(recurse) { state.graphStack.push(state.graph); state.graph = id; // recurse into graph api.frame( state, Object.keys(state.graphMap[id]).sort(), [subframe], output, '@graph'); state.graph = state.graphStack.pop; } } // iterate over subject properties for(const prop of Object.keys(subject).sort()) { // copy keywords to output if(isKeyword(prop)) { output[prop] = util.clone(subject[prop]); if(prop === '@type') { // count bnode values of @type for(const type of subject['@type']) { if(type.indexOf('_:') === 0) { util.addValue( state.bnodeMap, type, output, {propertyIsArray: true}); } } } continue; } // explicit is on and property isn't in the frame, skip processing if(flags.explicit && !(prop in frame)) { continue; } // add objects for(let o of subject[prop]) { const subframe = (prop in frame ? frame[prop] : _createImplicitFrame(flags)); // recurse into list if(graphTypes.isList(o)) { // add empty list const list = {'@list': []}; _addFrameOutput(output, prop, list); // add list objects const src = o['@list']; for(const n in src) { o = src[n]; if(graphTypes.isSubjectReference(o)) { const subframe = (prop in frame ? frame[prop][0]['@list'] : _createImplicitFrame(flags)); // recurse into subject reference api.frame(state, [o['@id']], subframe, list, '@list'); } else { // include other values automatically _addFrameOutput(list, '@list', util.clone(o)); } } continue; } if(graphTypes.isSubjectReference(o)) { // recurse into subject reference api.frame(state, [o['@id']], subframe, output, prop); } else if(_valueMatch(subframe[0], o)) { // include other values, if they match _addFrameOutput(output, prop, util.clone(o)); } } } // handle defaults for(const prop of Object.keys(frame).sort()) { // skip keywords if(isKeyword(prop)) { continue; } // if omit default is off, then include default values for properties // that appear in the next frame but are not in the matching subject const next = frame[prop][0] || {}; const omitDefaultOn = _getFrameFlag(next, options, 'omitDefault'); if(!omitDefaultOn && !(prop in output)) { let preserve = '@null'; if('@default' in next) { preserve = util.clone(next['@default']); } if(!types.isArray(preserve)) { preserve = [preserve]; } output[prop] = [{'@preserve': preserve}]; } } // if embed reverse values by finding nodes having this subject as a value // of the associated property if('@reverse' in frame) { for(const reverseProp of Object.keys(frame['@reverse']).sort()) { const subframe = frame['@reverse'][reverseProp]; for(const subject of Object.keys(state.subjects)) { const nodeValues = util.getValues(state.subjects[subject], reverseProp); if(nodeValues.some(v => v['@id'] === id)) { // node has property referencing this subject, recurse output['@reverse'] = output['@reverse'] || {}; util.addValue( output['@reverse'], reverseProp, [], {propertyIsArray: true}); api.frame( state, [subject], subframe, output['@reverse'][reverseProp], property); } } } } // add output to parent _addFrameOutput(parent, property, output); // pop matching subject from circular ref-checking stack state.subjectStack.pop(); } }; /** * Creates an implicit frame when recursing through subject matches. If * a frame doesn't have an explicit frame for a particular property, then * a wildcard child frame will be created that uses the same flags that the * parent frame used. * * @param flags the current framing flags. * * @return the implicit frame. */ function _createImplicitFrame(flags) { const frame = {}; for(const key in flags) { if(flags[key] !== undefined) { frame['@' + key] = [flags[key]]; } } return [frame]; } /** * Checks the current subject stack to see if embedding the given subject * would cause a circular reference. * * @param subjectToEmbed the subject to embed. * @param graph the graph the subject to embed is in. * @param subjectStack the current stack of subjects. * * @return true if a circular reference would be created, false if not. */ function _createsCircularReference(subjectToEmbed, graph, subjectStack) { for(let i = subjectStack.length - 1; i >= 0; --i) { const subject = subjectStack[i]; if(subject.graph === graph && subject.subject['@id'] === subjectToEmbed['@id']) { return true; } } return false; } /** * Gets the frame flag value for the given flag name. * * @param frame the frame. * @param options the framing options. * @param name the flag name. * * @return the flag value. */ function _getFrameFlag(frame, options, name) { const flag = '@' + name; let rval = (flag in frame ? frame[flag][0] : options[name]); if(name === 'embed') { // default is "@last" // backwards-compatibility support for "embed" maps: // true => "@last" // false => "@never" if(rval === true) { rval = '@last'; } else if(rval === false) { rval = '@never'; } else if(rval !== '@always' && rval !== '@never' && rval !== '@link') { rval = '@last'; } } return rval; } /** * Validates a JSON-LD frame, throwing an exception if the frame is invalid. * * @param frame the frame to validate. */ function _validateFrame(frame) { if(!types.isArray(frame) || frame.length !== 1 || !types.isObject(frame[0])) { throw new JsonLdError( 'Invalid JSON-LD syntax; a JSON-LD frame must be a single object.', 'jsonld.SyntaxError', {frame}); } } /** * Returns a map of all of the subjects that match a parsed frame. * * @param state the current framing state. * @param subjects the set of subjects to filter. * @param frame the parsed frame. * @param flags the frame flags. * * @return all of the matched subjects. */ function _filterSubjects(state, subjects, frame, flags) { // filter subjects in @id order const rval = {}; for(const id of subjects) { const subject = state.graphMap[state.graph][id]; if(_filterSubject(state, subject, frame, flags)) { rval[id] = subject; } } return rval; } /** * Returns true if the given subject matches the given frame. * * Matches either based on explicit type inclusion where the node has any * type listed in the frame. If the frame has empty types defined matches * nodes not having a @type. If the frame has a type of {} defined matches * nodes having any type defined. * * Otherwise, does duck typing, where the node must have all of the * properties defined in the frame. * * @param state the current framing state. * @param subject the subject to check. * @param frame the frame to check. * @param flags the frame flags. * * @return true if the subject matches, false if not. */ function _filterSubject(state, subject, frame, flags) { // check ducktype let wildcard = true; let matchesSome = false; for(const key in frame) { let matchThis = false; const nodeValues = util.getValues(subject, key); const isEmpty = util.getValues(frame, key).length === 0; if(isKeyword(key)) { // skip non-@id and non-@type if(key !== '@id' && key !== '@type') { continue; } wildcard = false; // check @id for a specific @id value if(key === '@id') { // if @id is not a wildcard and is not empty, then match or not on // specific value if(frame['@id'].length >= 0 && !types.isEmptyObject(frame['@id'][0])) { return frame['@id'].includes(nodeValues[0]); } matchThis = true; continue; } // check @type (object value means 'any' type, fall through to ducktyping) if('@type' in frame) { if(isEmpty) { if(nodeValues.length > 0) { // don't match on no @type return false; } matchThis = true; } else if(frame['@type'].length === 1 && types.isEmptyObject(frame['@type'][0])) { // match on wildcard @type matchThis = nodeValues.length > 0; } else { // match on a specific @type for(const type of frame['@type']) { if(nodeValues.some(tt => tt === type)) { return true; } } return false; } } } // Forc a copy of this frame entry so it can be manipulated const thisFrame = util.getValues(frame, key)[0]; let hasDefault = false; if(thisFrame) { _validateFrame([thisFrame]); hasDefault = '@default' in thisFrame; } // no longer a wildcard pattern if frame has any non-keyword properties wildcard = false; // skip, but allow match if node has no value for property, and frame has a // default value if(nodeValues.length === 0 && hasDefault) { continue; } // if frame value is empty, don't match if subject has any value if(nodeValues.length > 0 && isEmpty) { return false; } if(thisFrame === undefined) { // node does not match if values is not empty and the value of property // in frame is match none. if(nodeValues.length > 0) { return false; } matchThis = true; } else if(types.isObject(thisFrame)) { // node matches if values is not empty and the value of property in frame // is wildcard matchThis = nodeValues.length > 0; } else { if(graphTypes.isValue(thisFrame)) { // match on any matching value matchThis = nodeValues.some(nv => _valueMatch(thisFrame, nv)); } else if(graphTypes.isSubject(thisFrame) || graphTypes.isSubjectReference(thisFrame)) { matchThis = nodeValues.some(nv => _nodeMatch(state, thisFrame, nv, flags)); } else if(graphTypes.isList(thisFrame)) { const listValue = thisFrame['@list'][0]; if(graphTypes.isList(nodeValues[0])) { const nodeListValues = nodeValues[0]['@list']; if(graphTypes.isValue(listValue)) { // match on any matching value matchThis = nodeListValues.some(lv => _valueMatch(listValue, lv)); } else if(graphTypes.isSubject(listValue) || graphTypes.isSubjectReference(listValue)) { matchThis = nodeListValues.some(lv => _nodeMatch( state, listValue, lv, flags)); } } else { // value must be a list to match matchThis = false; } } } // all non-defaulted values must match if requireAll is set if(!matchThis && flags.requireAll) { return false; } matchesSome = matchesSome || matchThis; } // return true if wildcard or subject matches some properties return wildcard || matchesSome; } /** * Removes an existing embed. * * @param state the current framing state. * @param id the @id of the embed to remove. */ function _removeEmbed(state, id) { // get existing embed const embeds = state.uniqueEmbeds[state.graph]; const embed = embeds[id]; const parent = embed.parent; const property = embed.property; // create reference to replace embed const subject = {'@id': id}; // remove existing embed if(types.isArray(parent)) { // replace subject with reference for(let i = 0; i < parent.length; ++i) { if(util.compareValues(parent[i], subject)) { parent[i] = subject; break; } } } else { // replace subject with reference const useArray = types.isArray(parent[property]); util.removeValue(parent, property, subject, {propertyIsArray: useArray}); util.addValue(parent, property, subject, {propertyIsArray: useArray}); } // recursively remove dependent dangling embeds const removeDependents = id => { // get embed keys as a separate array to enable deleting keys in map const ids = Object.keys(embeds); for(const next of ids) { if(next in embeds && types.isObject(embeds[next].parent) && embeds[next].parent['@id'] === id) { delete embeds[next]; removeDependents(next); } } }; removeDependents(id); } /** * Adds framing output to the given parent. * * @param parent the parent to add to. * @param property the parent property. * @param output the output to add. */ function _addFrameOutput(parent, property, output) { if(types.isObject(parent)) { util.addValue(parent, property, output, {propertyIsArray: true}); } else { parent.push(output); } } /** * Node matches if it is a node, and matches the pattern as a frame. * * @param state the current framing state. * @param pattern used to match value * @param value to check * @param flags the frame flags. */ function _nodeMatch(state, pattern, value, flags) { if(!('@id' in value)) { return false; } const nodeObject = state.subjects[value['@id']]; return nodeObject && _filterSubject(state, nodeObject, pattern, flags); } /** * Value matches if it is a value and matches the value pattern * * * `pattern` is empty * * @values are the same, or `pattern[@value]` is a wildcard, and * * @types are the same or `value[@type]` is not null * and `pattern[@type]` is `{}`, or `value[@type]` is null * and `pattern[@type]` is null or `[]`, and * * @languages are the same or `value[@language]` is not null * and `pattern[@language]` is `{}`, or `value[@language]` is null * and `pattern[@language]` is null or `[]`. * * @param pattern used to match value * @param value to check */ function _valueMatch(pattern, value) { const v1 = value['@value']; const t1 = value['@type']; const l1 = value['@language']; const v2 = pattern['@value'] ? (types.isArray(pattern['@value']) ? pattern['@value'] : [pattern['@value']]) : []; const t2 = pattern['@type'] ? (types.isArray(pattern['@type']) ? pattern['@type'] : [pattern['@type']]) : []; const l2 = pattern['@language'] ? (types.isArray(pattern['@language']) ? pattern['@language'] : [pattern['@language']]) : []; if(v2.length === 0 && t2.length === 0 && l2.length === 0) { return true; } if(!(v2.includes(v1) || types.isEmptyObject(v2[0]))) { return false; } if(!(!t1 && t2.length === 0 || t2.includes(t1) || t1 && types.isEmptyObject(t2[0]))) { return false; } if(!(!l1 && l2.length === 0 || l2.includes(l1) || l1 && types.isEmptyObject(l2[0]))) { return false; } return true; } jsonld.js-1.6.2/lib/fromRdf.js000066400000000000000000000217511347107445200161440ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const graphTypes = require('./graphTypes'); const types = require('./types'); const util = require('./util'); // constants const { // RDF, RDF_LIST, RDF_FIRST, RDF_REST, RDF_NIL, RDF_TYPE, // RDF_PLAIN_LITERAL, // RDF_XML_LITERAL, // RDF_OBJECT, // RDF_LANGSTRING, // XSD, XSD_BOOLEAN, XSD_DOUBLE, XSD_INTEGER, XSD_STRING, } = require('./constants'); const api = {}; module.exports = api; /** * Converts an RDF dataset to JSON-LD. * * @param dataset the RDF dataset. * @param options the RDF serialization options. * * @return a Promise that resolves to the JSON-LD output. */ api.fromRDF = async ( dataset, {useRdfType = false, useNativeTypes = false}) => { const defaultGraph = {}; const graphMap = {'@default': defaultGraph}; const referencedOnce = {}; for(const quad of dataset) { // TODO: change 'name' to 'graph' const name = (quad.graph.termType === 'DefaultGraph') ? '@default' : quad.graph.value; if(!(name in graphMap)) { graphMap[name] = {}; } if(name !== '@default' && !(name in defaultGraph)) { defaultGraph[name] = {'@id': name}; } const nodeMap = graphMap[name]; // get subject, predicate, object const s = quad.subject.value; const p = quad.predicate.value; const o = quad.object; if(!(s in nodeMap)) { nodeMap[s] = {'@id': s}; } const node = nodeMap[s]; const objectIsNode = o.termType.endsWith('Node'); if(objectIsNode && !(o.value in nodeMap)) { nodeMap[o.value] = {'@id': o.value}; } if(p === RDF_TYPE && !useRdfType && objectIsNode) { util.addValue(node, '@type', o.value, {propertyIsArray: true}); continue; } const value = _RDFToObject(o, useNativeTypes); util.addValue(node, p, value, {propertyIsArray: true}); // object may be an RDF list/partial list node but we can't know easily // until all triples are read if(objectIsNode) { if(o.value === RDF_NIL) { // track rdf:nil uniquely per graph const object = nodeMap[o.value]; if(!('usages' in object)) { object.usages = []; } object.usages.push({ node, property: p, value }); } else if(o.value in referencedOnce) { // object referenced more than once referencedOnce[o.value] = false; } else { // keep track of single reference referencedOnce[o.value] = { node, property: p, value }; } } } /* for(let name in dataset) { const graph = dataset[name]; if(!(name in graphMap)) { graphMap[name] = {}; } if(name !== '@default' && !(name in defaultGraph)) { defaultGraph[name] = {'@id': name}; } const nodeMap = graphMap[name]; for(let ti = 0; ti < graph.length; ++ti) { const triple = graph[ti]; // get subject, predicate, object const s = triple.subject.value; const p = triple.predicate.value; const o = triple.object; if(!(s in nodeMap)) { nodeMap[s] = {'@id': s}; } const node = nodeMap[s]; const objectIsId = (o.type === 'IRI' || o.type === 'blank node'); if(objectIsId && !(o.value in nodeMap)) { nodeMap[o.value] = {'@id': o.value}; } if(p === RDF_TYPE && !useRdfType && objectIsId) { util.addValue(node, '@type', o.value, {propertyIsArray: true}); continue; } const value = _RDFToObject(o, useNativeTypes); util.addValue(node, p, value, {propertyIsArray: true}); // object may be an RDF list/partial list node but we can't know easily // until all triples are read if(objectIsId) { if(o.value === RDF_NIL) { // track rdf:nil uniquely per graph const object = nodeMap[o.value]; if(!('usages' in object)) { object.usages = []; } object.usages.push({ node: node, property: p, value: value }); } else if(o.value in referencedOnce) { // object referenced more than once referencedOnce[o.value] = false; } else { // keep track of single reference referencedOnce[o.value] = { node: node, property: p, value: value }; } } } }*/ // convert linked lists to @list arrays for(const name in graphMap) { const graphObject = graphMap[name]; // no @lists to be converted, continue if(!(RDF_NIL in graphObject)) { continue; } // iterate backwards through each RDF list const nil = graphObject[RDF_NIL]; if(!nil.usages) { continue; } for(let usage of nil.usages) { let node = usage.node; let property = usage.property; let head = usage.value; const list = []; const listNodes = []; // ensure node is a well-formed list node; it must: // 1. Be referenced only once. // 2. Have an array for rdf:first that has 1 item. // 3. Have an array for rdf:rest that has 1 item. // 4. Have no keys other than: @id, rdf:first, rdf:rest, and, // optionally, @type where the value is rdf:List. let nodeKeyCount = Object.keys(node).length; while(property === RDF_REST && types.isObject(referencedOnce[node['@id']]) && types.isArray(node[RDF_FIRST]) && node[RDF_FIRST].length === 1 && types.isArray(node[RDF_REST]) && node[RDF_REST].length === 1 && (nodeKeyCount === 3 || (nodeKeyCount === 4 && types.isArray(node['@type']) && node['@type'].length === 1 && node['@type'][0] === RDF_LIST))) { list.push(node[RDF_FIRST][0]); listNodes.push(node['@id']); // get next node, moving backwards through list usage = referencedOnce[node['@id']]; node = usage.node; property = usage.property; head = usage.value; nodeKeyCount = Object.keys(node).length; // if node is not a blank node, then list head found if(!graphTypes.isBlankNode(node)) { break; } } // the list is nested in another list if(property === RDF_FIRST) { // empty list if(node['@id'] === RDF_NIL) { // can't convert rdf:nil to a @list object because it would // result in a list of lists which isn't supported continue; } // preserve list head if(RDF_REST in graphObject[head['@id']]) { head = graphObject[head['@id']][RDF_REST][0]; } list.pop(); listNodes.pop(); } // transform list into @list object delete head['@id']; head['@list'] = list.reverse(); for(const listNode of listNodes) { delete graphObject[listNode]; } } delete nil.usages; } const result = []; const subjects = Object.keys(defaultGraph).sort(); for(const subject of subjects) { const node = defaultGraph[subject]; if(subject in graphMap) { const graph = node['@graph'] = []; const graphObject = graphMap[subject]; const graphSubjects = Object.keys(graphObject).sort(); for(const graphSubject of graphSubjects) { const node = graphObject[graphSubject]; // only add full subjects to top-level if(!graphTypes.isSubjectReference(node)) { graph.push(node); } } } // only add full subjects to top-level if(!graphTypes.isSubjectReference(node)) { result.push(node); } } return result; }; /** * Converts an RDF triple object to a JSON-LD object. * * @param o the RDF triple object to convert. * @param useNativeTypes true to output native types, false not to. * * @return the JSON-LD object. */ function _RDFToObject(o, useNativeTypes) { // convert NamedNode/BlankNode object to JSON-LD if(o.termType.endsWith('Node')) { return {'@id': o.value}; } // convert literal to JSON-LD const rval = {'@value': o.value}; // add language if(o.language) { rval['@language'] = o.language; } else { let type = o.datatype.value; if(!type) { type = XSD_STRING; } // use native types for certain xsd types if(useNativeTypes) { if(type === XSD_BOOLEAN) { if(rval['@value'] === 'true') { rval['@value'] = true; } else if(rval['@value'] === 'false') { rval['@value'] = false; } } else if(types.isNumeric(rval['@value'])) { if(type === XSD_INTEGER) { const i = parseInt(rval['@value'], 10); if(i.toFixed(0) === rval['@value']) { rval['@value'] = i; } } else if(type === XSD_DOUBLE) { rval['@value'] = parseFloat(rval['@value']); } } // do not add native type if(![XSD_BOOLEAN, XSD_INTEGER, XSD_DOUBLE, XSD_STRING].includes(type)) { rval['@type'] = type; } } else if(type !== XSD_STRING) { rval['@type'] = type; } } return rval; } jsonld.js-1.6.2/lib/graphTypes.js000066400000000000000000000062541347107445200166740ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const types = require('./types'); const api = {}; module.exports = api; /** * Returns true if the given value is a subject with properties. * * @param v the value to check. * * @return true if the value is a subject with properties, false if not. */ api.isSubject = v => { // Note: A value is a subject if all of these hold true: // 1. It is an Object. // 2. It is not a @value, @set, or @list. // 3. It has more than 1 key OR any existing key is not @id. if(types.isObject(v) && !(('@value' in v) || ('@set' in v) || ('@list' in v))) { const keyCount = Object.keys(v).length; return (keyCount > 1 || !('@id' in v)); } return false; }; /** * Returns true if the given value is a subject reference. * * @param v the value to check. * * @return true if the value is a subject reference, false if not. */ api.isSubjectReference = v => // Note: A value is a subject reference if all of these hold true: // 1. It is an Object. // 2. It has a single key: @id. (types.isObject(v) && Object.keys(v).length === 1 && ('@id' in v)); /** * Returns true if the given value is a @value. * * @param v the value to check. * * @return true if the value is a @value, false if not. */ api.isValue = v => // Note: A value is a @value if all of these hold true: // 1. It is an Object. // 2. It has the @value property. types.isObject(v) && ('@value' in v); /** * Returns true if the given value is a @list. * * @param v the value to check. * * @return true if the value is a @list, false if not. */ api.isList = v => // Note: A value is a @list if all of these hold true: // 1. It is an Object. // 2. It has the @list property. types.isObject(v) && ('@list' in v); /** * Returns true if the given value is a @graph. * * @return true if the value is a @graph, false if not. */ api.isGraph = v => { // Note: A value is a graph if all of these hold true: // 1. It is an object. // 2. It has an `@graph` key. // 3. It may have '@id' or '@index' return types.isObject(v) && '@graph' in v && Object.keys(v) .filter(key => key !== '@id' && key !== '@index').length === 1; }; /** * Returns true if the given value is a simple @graph. * * @return true if the value is a simple @graph, false if not. */ api.isSimpleGraph = v => { // Note: A value is a simple graph if all of these hold true: // 1. It is an object. // 2. It has an `@graph` key. // 3. It has only 1 key or 2 keys where one of them is `@index`. return api.isGraph(v) && !('@id' in v); }; /** * Returns true if the given value is a blank node. * * @param v the value to check. * * @return true if the value is a blank node, false if not. */ api.isBlankNode = v => { // Note: A value is a blank node if all of these hold true: // 1. It is an Object. // 2. If it has an @id key its value begins with '_:'. // 3. It has no keys OR is not a @value, @set, or @list. if(types.isObject(v)) { if('@id' in v) { return (v['@id'].indexOf('_:') === 0); } return (Object.keys(v).length === 0 || !(('@value' in v) || ('@set' in v) || ('@list' in v))); } return false; }; jsonld.js-1.6.2/lib/index.js000066400000000000000000000004111347107445200156420ustar00rootroot00000000000000/** * jsonld.js library. * * @author Dave Longley * * Copyright 2010-2017 Digital Bazaar, Inc. */ if(require('semver').gte(process.version, '8.6.0')) { module.exports = require('./jsonld'); } else { module.exports = require('../dist/node6/lib/jsonld'); } jsonld.js-1.6.2/lib/jsonld.js000066400000000000000000001022161347107445200160320ustar00rootroot00000000000000/** * A JavaScript implementation of the JSON-LD API. * * @author Dave Longley * * @license BSD 3-Clause License * Copyright (c) 2011-2017 Digital Bazaar, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the Digital Bazaar, Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ const canonize = require('rdf-canonize'); const util = require('./util'); const IdentifierIssuer = util.IdentifierIssuer; const JsonLdError = require('./JsonLdError'); const NQuads = require('./NQuads'); const Rdfa = require('./Rdfa'); const {expand: _expand} = require('./expand'); const {flatten: _flatten} = require('./flatten'); const {fromRDF: _fromRDF} = require('./fromRdf'); const {toRDF: _toRDF} = require('./toRdf'); const { frameMergedOrDefault: _frameMergedOrDefault } = require('./frame'); const { isArray: _isArray, isObject: _isObject, isString: _isString } = require('./types'); const { isSubjectReference: _isSubjectReference, } = require('./graphTypes'); const { getInitialContext: _getInitialContext, process: _processContext, getAllContexts: _getAllContexts } = require('./context'); const { compact: _compact, compactIri: _compactIri, removePreserve: _removePreserve } = require('./compact'); const { createNodeMap: _createNodeMap, createMergedNodeMap: _createMergedNodeMap, mergeNodeMaps: _mergeNodeMaps } = require('./nodeMap'); // determine if in-browser or using node.js const _nodejs = ( typeof process !== 'undefined' && process.versions && process.versions.node); const _browser = !_nodejs && (typeof window !== 'undefined' || typeof self !== 'undefined'); /* eslint-disable indent */ // attaches jsonld API to the given object const wrapper = function(jsonld) { /* Core API */ /** * Performs JSON-LD compaction. * * @param input the JSON-LD input to compact. * @param ctx the context to compact with. * @param [options] options to use: * [base] the base IRI to use. * [compactArrays] true to compact arrays to single values when * appropriate, false not to (default: true). * [compactToRelative] true to compact IRIs to be relative to document * base, false to keep absolute (default: true) * [graph] true to always output a top-level graph (default: false). * [expandContext] a context to expand with. * [skipExpansion] true to assume the input is expanded and skip * expansion, false not to, defaults to false. * [documentLoader(url, callback(err, remoteDoc))] the document loader. * [expansionMap(info)] a function that can be used to custom map * unmappable values (or to throw an error when they are detected); * if this function returns `undefined` then the default behavior * will be used. * [framing] true if compaction is occuring during a framing operation. * [compactionMap(info)] a function that can be used to custom map * unmappable values (or to throw an error when they are detected); * if this function returns `undefined` then the default behavior * will be used. * @param [callback(err, compacted)] called once the operation completes. * * @return a Promise that resolves to the compacted output. */ jsonld.compact = util.callbackify(async function(input, ctx, options) { if(arguments.length < 2) { throw new TypeError('Could not compact, too few arguments.'); } if(ctx === null) { throw new JsonLdError( 'The compaction context must not be null.', 'jsonld.CompactError', {code: 'invalid local context'}); } // nothing to compact if(input === null) { return null; } // set default options options = _setDefaults(options, { base: _isString(input) ? input : '', compactArrays: true, compactToRelative: true, graph: false, skipExpansion: false, link: false, issuer: new IdentifierIssuer('_:b') }); if(options.link) { // force skip expansion when linking, "link" is not part of the public // API, it should only be called from framing options.skipExpansion = true; } if(!options.compactToRelative) { delete options.base; } // expand input let expanded; if(options.skipExpansion) { expanded = input; } else { expanded = await jsonld.expand(input, options); } // process context const activeCtx = await jsonld.processContext( _getInitialContext(options), ctx, options); // do compaction let compacted = _compact({ activeCtx, element: expanded, options, compactionMap: options.compactionMap }); // perform clean up if(options.compactArrays && !options.graph && _isArray(compacted)) { if(compacted.length === 1) { // simplify to a single item compacted = compacted[0]; } else if(compacted.length === 0) { // simplify to an empty object compacted = {}; } } else if(options.graph && _isObject(compacted)) { // always use array if graph option is on compacted = [compacted]; } // follow @context key if(_isObject(ctx) && '@context' in ctx) { ctx = ctx['@context']; } // build output context ctx = util.clone(ctx); if(!_isArray(ctx)) { ctx = [ctx]; } // remove empty contexts const tmp = ctx; ctx = []; for(let i = 0; i < tmp.length; ++i) { if(!_isObject(tmp[i]) || Object.keys(tmp[i]).length > 0) { ctx.push(tmp[i]); } } // remove array if only one context const hasContext = (ctx.length > 0); if(ctx.length === 1) { ctx = ctx[0]; } // add context and/or @graph if(_isArray(compacted)) { // use '@graph' keyword const graphAlias = _compactIri({ activeCtx, iri: '@graph', relativeTo: {vocab: true} }); const graph = compacted; compacted = {}; if(hasContext) { compacted['@context'] = ctx; } compacted[graphAlias] = graph; } else if(_isObject(compacted) && hasContext) { // reorder keys so @context is first const graph = compacted; compacted = {'@context': ctx}; for(const key in graph) { compacted[key] = graph[key]; } } if(options.framing) { // get graph alias const graph = _compactIri({ activeCtx, iri: '@graph', relativeTo: {vocab: true} }); // remove @preserve from results options.link = {}; compacted[graph] = _removePreserve(activeCtx, compacted[graph], options); } return compacted; }); /** * Performs JSON-LD expansion. * * @param input the JSON-LD input to expand. * @param [options] the options to use: * [base] the base IRI to use. * [expandContext] a context to expand with. * [keepFreeFloatingNodes] true to keep free-floating nodes, * false not to, defaults to false. * [documentLoader(url, callback(err, remoteDoc))] the document loader. * [expansionMap(info)] a function that can be used to custom map * unmappable values (or to throw an error when they are detected); * if this function returns `undefined` then the default behavior * will be used. * @param [callback(err, expanded)] called once the operation completes. * * @return a Promise that resolves to the expanded output. */ jsonld.expand = util.callbackify(async function(input, options) { if(arguments.length < 1) { throw new TypeError('Could not expand, too few arguments.'); } // set default options options = _setDefaults(options, { keepFreeFloatingNodes: false }); if(options.expansionMap === false) { options.expansionMap = undefined; } // build set of objects that may have @contexts to resolve const toResolve = {}; // build set of contexts to process prior to expansion const contextsToProcess = []; // if an `expandContext` has been given ensure it gets resolved if('expandContext' in options) { const expandContext = util.clone(options.expandContext); if(_isObject(expandContext) && '@context' in expandContext) { toResolve.expandContext = expandContext; } else { toResolve.expandContext = {'@context': expandContext}; } contextsToProcess.push(toResolve.expandContext); } // if input is a string, attempt to dereference remote document let defaultBase; if(!_isString(input)) { // input is not a URL, do not need to retrieve it first toResolve.input = util.clone(input); } else { // load remote doc const remoteDoc = await jsonld.get(input, options); defaultBase = remoteDoc.documentUrl; toResolve.input = remoteDoc.document; if(remoteDoc.contextUrl) { // context included in HTTP link header and must be resolved toResolve.remoteContext = {'@context': remoteDoc.contextUrl}; contextsToProcess.push(toResolve.remoteContext); } } // set default base if(!('base' in options)) { options.base = defaultBase || ''; } // get all contexts in `toResolve` await _getAllContexts(toResolve, options); // process any additional contexts let activeCtx = _getInitialContext(options); contextsToProcess.forEach(localCtx => { activeCtx = _processContext({activeCtx, localCtx, options}); }); // expand resolved input let expanded = _expand({ activeCtx, element: toResolve.input, options, expansionMap: options.expansionMap }); // optimize away @graph with no other properties if(_isObject(expanded) && ('@graph' in expanded) && Object.keys(expanded).length === 1) { expanded = expanded['@graph']; } else if(expanded === null) { expanded = []; } // normalize to an array if(!_isArray(expanded)) { expanded = [expanded]; } return expanded; }); /** * Performs JSON-LD flattening. * * @param input the JSON-LD to flatten. * @param ctx the context to use to compact the flattened output, or null. * @param [options] the options to use: * [base] the base IRI to use. * [expandContext] a context to expand with. * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, flattened)] called once the operation completes. * * @return a Promise that resolves to the flattened output. */ jsonld.flatten = util.callbackify(async function(input, ctx, options) { if(arguments.length < 1) { return new TypeError('Could not flatten, too few arguments.'); } if(typeof ctx === 'function') { ctx = null; } else { ctx = ctx || null; } // set default options options = _setDefaults(options, { base: _isString(input) ? input : '' }); // expand input const expanded = await jsonld.expand(input, options); // do flattening const flattened = _flatten(expanded); if(ctx === null) { // no compaction required return flattened; } // compact result (force @graph option to true, skip expansion) options.graph = true; options.skipExpansion = true; const compacted = await jsonld.compact(flattened, ctx, options); return compacted; }); /** * Performs JSON-LD framing. * * @param input the JSON-LD input to frame. * @param frame the JSON-LD frame to use. * @param [options] the framing options. * [base] the base IRI to use. * [expandContext] a context to expand with. * [embed] default @embed flag: '@last', '@always', '@never', '@link' * (default: '@last'). * [explicit] default @explicit flag (default: false). * [requireAll] default @requireAll flag (default: true). * [omitDefault] default @omitDefault flag (default: false). * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, framed)] called once the operation completes. * * @return a Promise that resolves to the framed output. */ jsonld.frame = util.callbackify(async function(input, frame, options) { if(arguments.length < 2) { throw new TypeError('Could not frame, too few arguments.'); } // set default options options = _setDefaults(options, { base: _isString(input) ? input : '', embed: '@last', explicit: false, requireAll: true, omitDefault: false, pruneBlankNodeIdentifiers: true, bnodesToClear: [] }); // if frame is a string, attempt to dereference remote document if(_isString(frame)) { // load remote doc const remoteDoc = await jsonld.get(frame, options); frame = remoteDoc.document; if(remoteDoc.contextUrl) { // inject link header @context into frame let ctx = frame['@context']; if(!ctx) { ctx = remoteDoc.contextUrl; } else if(_isArray(ctx)) { ctx.push(remoteDoc.contextUrl); } else { ctx = [ctx, remoteDoc.contextUrl]; } frame['@context'] = ctx; } } const frameContext = frame ? frame['@context'] || {} : {}; // expand input const expanded = await jsonld.expand(input, options); // expand frame const opts = util.clone(options); opts.isFrame = true; opts.keepFreeFloatingNodes = true; const expandedFrame = await jsonld.expand(frame, opts); // if the unexpanded frame includes a key expanding to @graph, frame the // default graph, otherwise, the merged graph let framed; // FIXME should look for aliases of @graph opts.merged = !('@graph' in frame); // do framing framed = _frameMergedOrDefault(expanded, expandedFrame, opts); // compact result (force @graph option to true, skip expansion, // check for linked embeds) opts.graph = true; opts.skipExpansion = true; opts.link = {}; opts.framing = true; const compacted = await jsonld.compact(framed, frameContext, opts); return compacted; }); /** * **Experimental** * * Links a JSON-LD document's nodes in memory. * * @param input the JSON-LD document to link. * @param [ctx] the JSON-LD context to apply. * @param [options] the options to use: * [base] the base IRI to use. * [expandContext] a context to expand with. * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, linked)] called once the operation completes. * * @return a Promise that resolves to the linked output. */ jsonld.link = util.callbackify(async function(input, ctx, options) { // API matches running frame with a wildcard frame and embed: '@link' // get arguments const frame = {}; if(ctx) { frame['@context'] = ctx; } frame['@embed'] = '@link'; return jsonld.frame(input, frame, options); }); /** * Performs RDF dataset normalization on the given input. The input is JSON-LD * unless the 'inputFormat' option is used. The output is an RDF dataset * unless the 'format' option is used. * * @param input the input to normalize as JSON-LD or as a format specified by * the 'inputFormat' option. * @param [options] the options to use: * [algorithm] the normalization algorithm to use, `URDNA2015` or * `URGNA2012` (default: `URDNA2015`). * [base] the base IRI to use. * [expandContext] a context to expand with. * [skipExpansion] true to assume the input is expanded and skip * expansion, false not to, defaults to false. * [inputFormat] the format if input is not JSON-LD: * 'application/n-quads' for N-Quads. * [format] the format if output is a string: * 'application/n-quads' for N-Quads. * [documentLoader(url, callback(err, remoteDoc))] the document loader. * [useNative] true to use a native canonize algorithm * @param [callback(err, normalized)] called once the operation completes. * * @return a Promise that resolves to the normalized output. */ jsonld.normalize = jsonld.canonize = util.callbackify(async function( input, options) { if(arguments.length < 1) { throw new TypeError('Could not canonize, too few arguments.'); } // set default options options = _setDefaults(options, { base: _isString(input) ? input : '', algorithm: 'URDNA2015', skipExpansion: false }); if('inputFormat' in options) { if(options.inputFormat !== 'application/n-quads' && options.inputFormat !== 'application/nquads') { throw new JsonLdError( 'Unknown canonicalization input format.', 'jsonld.CanonizeError'); } // TODO: `await` for async parsers const parsedInput = NQuads.parse(input); // do canonicalization return canonize.canonize(parsedInput, options); } // convert to RDF dataset then do normalization const opts = util.clone(options); delete opts.format; opts.produceGeneralizedRdf = false; const dataset = await jsonld.toRDF(input, opts); // do canonicalization return canonize.canonize(dataset, options); }); /** * Converts an RDF dataset to JSON-LD. * * @param dataset a serialized string of RDF in a format specified by the * format option or an RDF dataset to convert. * @param [options] the options to use: * [format] the format if dataset param must first be parsed: * 'application/n-quads' for N-Quads (default). * [rdfParser] a custom RDF-parser to use to parse the dataset. * [useRdfType] true to use rdf:type, false to use @type * (default: false). * [useNativeTypes] true to convert XSD types into native types * (boolean, integer, double), false not to (default: false). * @param [callback(err, output)] called once the operation completes. * * @return a Promise that resolves to the JSON-LD document. */ jsonld.fromRDF = util.callbackify(async function(dataset, options) { if(arguments.length < 1) { throw new TypeError('Could not convert from RDF, too few arguments.'); } // set default options options = _setDefaults(options, { format: _isString(dataset) ? 'application/n-quads' : undefined }); let {format, rdfParser} = options; // handle special format if(format) { // check supported formats rdfParser = rdfParser || _rdfParsers[format]; if(!rdfParser) { throw new JsonLdError( 'Unknown input format.', 'jsonld.UnknownFormat', {format}); } } else { // no-op parser, assume dataset already parsed rdfParser = () => dataset; } // TODO: call `normalizeAsyncFn` on parser fn // rdfParser can be callback, promise-based, or synchronous let parsedDataset; if(rdfParser.length > 1) { // convert callback-based rdf parser to promise-based parsedDataset = new Promise((resolve, reject) => { rdfParser(dataset, (err, dataset) => { if(err) { reject(err); } else { resolve(dataset); } }); }); } else { parsedDataset = Promise.resolve(rdfParser(dataset)); } parsedDataset = await parsedDataset; // back-compat with old parsers that produced legacy dataset format if(!Array.isArray(parsedDataset)) { parsedDataset = NQuads.legacyDatasetToQuads(parsedDataset); } return _fromRDF(parsedDataset, options); }); /** * Outputs the RDF dataset found in the given JSON-LD object. * * @param input the JSON-LD input. * @param [options] the options to use: * [base] the base IRI to use. * [expandContext] a context to expand with. * [skipExpansion] true to assume the input is expanded and skip * expansion, false not to, defaults to false. * [format] the format to use to output a string: * 'application/n-quads' for N-Quads. * [produceGeneralizedRdf] true to output generalized RDF, false * to produce only standard RDF (default: false). * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, dataset)] called once the operation completes. * * @return a Promise that resolves to the RDF dataset. */ jsonld.toRDF = util.callbackify(async function(input, options) { if(arguments.length < 1) { throw new TypeError('Could not convert to RDF, too few arguments.'); } // set default options options = _setDefaults(options, { base: _isString(input) ? input : '', skipExpansion: false }); // TODO: support toRDF custom map? let expanded; if(options.skipExpansion) { expanded = input; } else { // expand input expanded = await jsonld.expand(input, options); } // output RDF dataset const dataset = _toRDF(expanded, options); if(options.format) { if(options.format === 'application/n-quads' || options.format === 'application/nquads') { return await NQuads.serialize(dataset); } throw new JsonLdError( 'Unknown output format.', 'jsonld.UnknownFormat', {format: options.format}); } return dataset; }); /** * **Experimental** * * Recursively flattens the nodes in the given JSON-LD input into a merged * map of node ID => node. All graphs will be merged into the default graph. * * @param input the JSON-LD input. * @param [options] the options to use: * [base] the base IRI to use. * [expandContext] a context to expand with. * [issuer] a jsonld.IdentifierIssuer to use to label blank nodes. * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, nodeMap)] called once the operation completes. * * @return a Promise that resolves to the merged node map. */ jsonld.createNodeMap = util.callbackify(async function(input, options) { if(arguments.length < 1) { throw new TypeError('Could not create node map, too few arguments.'); } // set default options options = _setDefaults(options, { base: _isString(input) ? input : '' }); // expand input const expanded = await jsonld.expand(input, options); return _createMergedNodeMap(expanded, options); }); /** * **Experimental** * * Merges two or more JSON-LD documents into a single flattened document. * * @param docs the JSON-LD documents to merge together. * @param ctx the context to use to compact the merged result, or null. * @param [options] the options to use: * [base] the base IRI to use. * [expandContext] a context to expand with. * [issuer] a jsonld.IdentifierIssuer to use to label blank nodes. * [mergeNodes] true to merge properties for nodes with the same ID, * false to ignore new properties for nodes with the same ID once * the ID has been defined; note that this may not prevent merging * new properties where a node is in the `object` position * (default: true). * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, merged)] called once the operation completes. * * @return a Promise that resolves to the merged output. */ jsonld.merge = util.callbackify(async function(docs, ctx, options) { if(arguments.length < 1) { throw new TypeError('Could not merge, too few arguments.'); } if(!_isArray(docs)) { throw new TypeError('Could not merge, "docs" must be an array.'); } if(typeof ctx === 'function') { ctx = null; } else { ctx = ctx || null; } // set default options options = _setDefaults(options, {}); // expand all documents const expanded = await Promise.all(docs.map(doc => { const opts = Object.assign({}, options); return jsonld.expand(doc, opts); })); let mergeNodes = true; if('mergeNodes' in options) { mergeNodes = options.mergeNodes; } const issuer = options.issuer || new IdentifierIssuer('_:b'); const graphs = {'@default': {}}; for(let i = 0; i < expanded.length; ++i) { // uniquely relabel blank nodes const doc = util.relabelBlankNodes(expanded[i], { issuer: new IdentifierIssuer('_:b' + i + '-') }); // add nodes to the shared node map graphs if merging nodes, to a // separate graph set if not const _graphs = (mergeNodes || i === 0) ? graphs : {'@default': {}}; _createNodeMap(doc, _graphs, '@default', issuer); if(_graphs !== graphs) { // merge document graphs but don't merge existing nodes for(const graphName in _graphs) { const _nodeMap = _graphs[graphName]; if(!(graphName in graphs)) { graphs[graphName] = _nodeMap; continue; } const nodeMap = graphs[graphName]; for(const key in _nodeMap) { if(!(key in nodeMap)) { nodeMap[key] = _nodeMap[key]; } } } } } // add all non-default graphs to default graph const defaultGraph = _mergeNodeMaps(graphs); // produce flattened output const flattened = []; const keys = Object.keys(defaultGraph).sort(); for(let ki = 0; ki < keys.length; ++ki) { const node = defaultGraph[keys[ki]]; // only add full subjects to top-level if(!_isSubjectReference(node)) { flattened.push(node); } } if(ctx === null) { return flattened; } // compact result (force @graph option to true, skip expansion) options.graph = true; options.skipExpansion = true; const compacted = await jsonld.compact(flattened, ctx, options); return compacted; }); /** * The default document loader for external documents. If the environment * is node.js, a callback-continuation-style document loader is used; otherwise, * a promises-style document loader is used. * * @param url the URL to load. * @param callback(err, remoteDoc) called once the operation completes, * if using a non-promises API. * * @return a promise, if using a promises API. */ Object.defineProperty(jsonld, 'documentLoader', { get: () => jsonld._documentLoader, set: v => jsonld._documentLoader = util.normalizeDocumentLoader(v) }); // default document loader not implemented jsonld.documentLoader = async url => { throw new JsonLdError( 'Could not retrieve a JSON-LD document from the URL. URL ' + 'dereferencing not implemented.', 'jsonld.LoadDocumentError', {code: 'loading document failed', url}); }; /** * Deprecated default document loader. Do not use or override. */ jsonld.loadDocument = util.callbackify(async function() { return jsonld.documentLoader.apply(null, arguments); }); /** * Gets a remote JSON-LD document using the default document loader or * one given in the passed options. * * @param url the URL to fetch. * @param [options] the options to use: * [documentLoader] the document loader to use. * @param [callback(err, remoteDoc)] called once the operation completes. * * @return a Promise that resolves to the retrieved remote document. */ jsonld.get = util.callbackify(async function(url, options) { let load; if(typeof options.documentLoader === 'function') { load = util.normalizeDocumentLoader(options.documentLoader); } else { load = jsonld.documentLoader; } const remoteDoc = await load(url); // TODO: can this be moved into `normalizeDocumentLoader`? try { if(!remoteDoc.document) { throw new JsonLdError( 'No remote document found at the given URL.', 'jsonld.NullRemoteDocument'); } if(_isString(remoteDoc.document)) { remoteDoc.document = JSON.parse(remoteDoc.document); } } catch(e) { throw new JsonLdError( 'Could not retrieve a JSON-LD document from the URL.', 'jsonld.LoadDocumentError', { code: 'loading document failed', cause: e, remoteDoc }); } return remoteDoc; }); /** * Processes a local context, resolving any URLs as necessary, and returns a * new active context in its callback. * * @param activeCtx the current active context. * @param localCtx the local context to process. * @param [options] the options to use: * [documentLoader(url, callback(err, remoteDoc))] the document loader. * @param [callback(err, activeCtx)] called once the operation completes. * * @return a Promise that resolves to the new active context. */ jsonld.processContext = util.callbackify(async function( activeCtx, localCtx, options) { // set default options options = _setDefaults(options, { base: '' }); // return initial context early for null context if(localCtx === null) { return _getInitialContext(options); } // get URLs in localCtx localCtx = util.clone(localCtx); if(!(_isObject(localCtx) && '@context' in localCtx)) { localCtx = {'@context': localCtx}; } const ctx = await _getAllContexts(localCtx, options); return _processContext({activeCtx, localCtx: ctx, options}); }); // backwards compatibility jsonld.getContextValue = require('./context').getContextValue; /** * Document loaders. */ jsonld.documentLoaders = {}; jsonld.documentLoaders.node = require('./documentLoaders/node'); jsonld.documentLoaders.xhr = require('./documentLoaders/xhr'); /** * Assigns the default document loader for external document URLs to a built-in * default. Supported types currently include: 'xhr' and 'node'. * * @param type the type to set. * @param [params] the parameters required to use the document loader. */ jsonld.useDocumentLoader = function(type) { if(!(type in jsonld.documentLoaders)) { throw new JsonLdError( 'Unknown document loader type: "' + type + '"', 'jsonld.UnknownDocumentLoader', {type}); } // set document loader jsonld.documentLoader = jsonld.documentLoaders[type].apply( jsonld, Array.prototype.slice.call(arguments, 1)); }; /** Registered RDF dataset parsers hashed by content-type. */ const _rdfParsers = {}; /** * Registers an RDF dataset parser by content-type, for use with * jsonld.fromRDF. An RDF dataset parser will always be given two parameters, * a string of input and a callback. An RDF dataset parser can be synchronous * or asynchronous. * * If the parser function returns undefined or null then it will be assumed to * be asynchronous w/a continuation-passing style and the callback parameter * given to the parser MUST be invoked. * * If it returns a Promise, then it will be assumed to be asynchronous, but the * callback parameter MUST NOT be invoked. It should instead be ignored. * * If it returns an RDF dataset, it will be assumed to be synchronous and the * callback parameter MUST NOT be invoked. It should instead be ignored. * * @param contentType the content-type for the parser. * @param parser(input, callback(err, dataset)) the parser function (takes a * string as a parameter and either returns null/undefined and uses * the given callback, returns a Promise, or returns an RDF dataset). */ jsonld.registerRDFParser = function(contentType, parser) { _rdfParsers[contentType] = parser; }; /** * Unregisters an RDF dataset parser by content-type. * * @param contentType the content-type for the parser. */ jsonld.unregisterRDFParser = function(contentType) { delete _rdfParsers[contentType]; }; // register the N-Quads RDF parser jsonld.registerRDFParser('application/n-quads', NQuads.parse); jsonld.registerRDFParser('application/nquads', NQuads.parse); // register the RDFa API RDF parser jsonld.registerRDFParser('rdfa-api', Rdfa.parse); /* URL API */ jsonld.url = require('./url'); /* Utility API */ jsonld.util = util; // backwards compatibility Object.assign(jsonld, util); // reexpose API as jsonld.promises for backwards compatability jsonld.promises = jsonld; // backwards compatibility jsonld.RequestQueue = require('./RequestQueue'); /* WebIDL API */ jsonld.JsonLdProcessor = require('./JsonLdProcessor')(jsonld); // setup browser global JsonLdProcessor if(_browser && typeof global.JsonLdProcessor === 'undefined') { Object.defineProperty(global, 'JsonLdProcessor', { writable: true, enumerable: false, configurable: true, value: jsonld.JsonLdProcessor }); } // set platform-specific defaults/APIs if(_nodejs) { // use node document loader by default jsonld.useDocumentLoader('node'); } else if(typeof XMLHttpRequest !== 'undefined') { // use xhr document loader by default jsonld.useDocumentLoader('xhr'); } function _setDefaults(options, { documentLoader = jsonld.documentLoader, ...defaults }) { return Object.assign({}, {documentLoader}, defaults, options); } // end of jsonld API `wrapper` factory return jsonld; }; // external APIs: // used to generate a new jsonld API instance const factory = function() { return wrapper(function() { return factory(); }); }; // wrap the main jsonld API instance wrapper(factory); // export API module.exports = factory; jsonld.js-1.6.2/lib/nodeMap.js000066400000000000000000000175751347107445200161410ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {isKeyword} = require('./context'); const graphTypes = require('./graphTypes'); const types = require('./types'); const util = require('./util'); const JsonLdError = require('./JsonLdError'); const api = {}; module.exports = api; /** * Creates a merged JSON-LD node map (node ID => node). * * @param input the expanded JSON-LD to create a node map of. * @param [options] the options to use: * [issuer] a jsonld.IdentifierIssuer to use to label blank nodes. * * @return the node map. */ api.createMergedNodeMap = (input, options) => { options = options || {}; // produce a map of all subjects and name each bnode const issuer = options.issuer || new util.IdentifierIssuer('_:b'); const graphs = {'@default': {}}; api.createNodeMap(input, graphs, '@default', issuer); // add all non-default graphs to default graph return api.mergeNodeMaps(graphs); }; /** * Recursively flattens the subjects in the given JSON-LD expanded input * into a node map. * * @param input the JSON-LD expanded input. * @param graphs a map of graph name to subject map. * @param graph the name of the current graph. * @param issuer the blank node identifier issuer. * @param name the name assigned to the current input if it is a bnode. * @param list the list to append to, null for none. */ api.createNodeMap = (input, graphs, graph, issuer, name, list) => { // recurse through array if(types.isArray(input)) { for(let i = 0; i < input.length; ++i) { api.createNodeMap(input[i], graphs, graph, issuer, undefined, list); } return; } // add non-object to list if(!types.isObject(input)) { if(list) { list.push(input); } return; } // add values to list if(graphTypes.isValue(input)) { if('@type' in input) { let type = input['@type']; // rename @type blank node if(type.indexOf('_:') === 0) { input['@type'] = type = issuer.getId(type); } } if(list) { list.push(input); } return; } // Note: At this point, input must be a subject. // spec requires @type to be named first, so assign names early if('@type' in input) { const types = input['@type']; for(let i = 0; i < types.length; ++i) { const type = types[i]; if(type.indexOf('_:') === 0) { issuer.getId(type); } } } // get name for subject if(types.isUndefined(name)) { name = graphTypes.isBlankNode(input) ? issuer.getId(input['@id']) : input['@id']; } // add subject reference to list if(list) { list.push({'@id': name}); } // create new subject or merge into existing one const subjects = graphs[graph]; const subject = subjects[name] = subjects[name] || {}; subject['@id'] = name; const properties = Object.keys(input).sort(); for(let pi = 0; pi < properties.length; ++pi) { let property = properties[pi]; // skip @id if(property === '@id') { continue; } // handle reverse properties if(property === '@reverse') { const referencedNode = {'@id': name}; const reverseMap = input['@reverse']; for(const reverseProperty in reverseMap) { const items = reverseMap[reverseProperty]; for(let ii = 0; ii < items.length; ++ii) { const item = items[ii]; let itemName = item['@id']; if(graphTypes.isBlankNode(item)) { itemName = issuer.getId(itemName); } api.createNodeMap(item, graphs, graph, issuer, itemName); util.addValue( subjects[itemName], reverseProperty, referencedNode, {propertyIsArray: true, allowDuplicate: false}); } } continue; } // recurse into graph if(property === '@graph') { // add graph subjects map entry if(!(name in graphs)) { graphs[name] = {}; } api.createNodeMap(input[property], graphs, name, issuer); continue; } // copy non-@type keywords if(property !== '@type' && isKeyword(property)) { if(property === '@index' && property in subject && (input[property] !== subject[property] || input[property]['@id'] !== subject[property]['@id'])) { throw new JsonLdError( 'Invalid JSON-LD syntax; conflicting @index property detected.', 'jsonld.SyntaxError', {code: 'conflicting indexes', subject}); } subject[property] = input[property]; continue; } // iterate over objects const objects = input[property]; // if property is a bnode, assign it a new id if(property.indexOf('_:') === 0) { property = issuer.getId(property); } // ensure property is added for empty arrays if(objects.length === 0) { util.addValue(subject, property, [], {propertyIsArray: true}); continue; } for(let oi = 0; oi < objects.length; ++oi) { let o = objects[oi]; if(property === '@type') { // rename @type blank nodes o = (o.indexOf('_:') === 0) ? issuer.getId(o) : o; } // handle embedded subject or subject reference if(graphTypes.isSubject(o) || graphTypes.isSubjectReference(o)) { // relabel blank node @id const id = graphTypes.isBlankNode(o) ? issuer.getId(o['@id']) : o['@id']; // add reference and recurse util.addValue( subject, property, {'@id': id}, {propertyIsArray: true, allowDuplicate: false}); api.createNodeMap(o, graphs, graph, issuer, id); } else if(graphTypes.isList(o)) { // handle @list const _list = []; api.createNodeMap(o['@list'], graphs, graph, issuer, name, _list); o = {'@list': _list}; util.addValue( subject, property, o, {propertyIsArray: true, allowDuplicate: false}); } else { // handle @value api.createNodeMap(o, graphs, graph, issuer, name); util.addValue( subject, property, o, {propertyIsArray: true, allowDuplicate: false}); } } } }; /** * Merge separate named graphs into a single merged graph including * all nodes from the default graph and named graphs. * * @param graphs a map of graph name to subject map. * * @return the merged graph map. */ api.mergeNodeMapGraphs = graphs => { const merged = {}; for(const name of Object.keys(graphs).sort()) { for(const id of Object.keys(graphs[name]).sort()) { const node = graphs[name][id]; if(!(id in merged)) { merged[id] = {'@id': id}; } const mergedNode = merged[id]; for(const property of Object.keys(node).sort()) { if(isKeyword(property)) { // copy keywords mergedNode[property] = util.clone(node[property]); } else { // merge objects for(const value of node[property]) { util.addValue( mergedNode, property, util.clone(value), {propertyIsArray: true, allowDuplicate: false}); } } } } } return merged; }; api.mergeNodeMaps = graphs => { // add all non-default graphs to default graph const defaultGraph = graphs['@default']; const graphNames = Object.keys(graphs).sort(); for(let i = 0; i < graphNames.length; ++i) { const graphName = graphNames[i]; if(graphName === '@default') { continue; } const nodeMap = graphs[graphName]; let subject = defaultGraph[graphName]; if(!subject) { defaultGraph[graphName] = subject = { '@id': graphName, '@graph': [] }; } else if(!('@graph' in subject)) { subject['@graph'] = []; } const graph = subject['@graph']; const ids = Object.keys(nodeMap).sort(); for(let ii = 0; ii < ids.length; ++ii) { const node = nodeMap[ids[ii]]; // only add full subjects if(!graphTypes.isSubjectReference(node)) { graph.push(node); } } } return defaultGraph; }; jsonld.js-1.6.2/lib/toRdf.js000066400000000000000000000154471347107445200156300ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const {createNodeMap} = require('./nodeMap'); const {isKeyword} = require('./context'); const graphTypes = require('./graphTypes'); const types = require('./types'); const util = require('./util'); const { // RDF, // RDF_LIST, RDF_FIRST, RDF_REST, RDF_NIL, RDF_TYPE, // RDF_PLAIN_LITERAL, // RDF_XML_LITERAL, // RDF_OBJECT, RDF_LANGSTRING, // XSD, XSD_BOOLEAN, XSD_DOUBLE, XSD_INTEGER, XSD_STRING, } = require('./constants'); const { isAbsolute: _isAbsoluteIri } = require('./url'); const api = {}; module.exports = api; /** * Outputs an RDF dataset for the expanded JSON-LD input. * * @param input the expanded JSON-LD input. * @param options the RDF serialization options. * * @return the RDF dataset. */ api.toRDF = (input, options) => { // create node map for default graph (and any named graphs) const issuer = new util.IdentifierIssuer('_:b'); const nodeMap = {'@default': {}}; createNodeMap(input, nodeMap, '@default', issuer); const dataset = []; const graphNames = Object.keys(nodeMap).sort(); for(const graphName of graphNames) { let graphTerm; if(graphName === '@default') { graphTerm = {termType: 'DefaultGraph', value: ''}; } else if(_isAbsoluteIri(graphName)) { if(graphName.startsWith('_:')) { graphTerm = {termType: 'BlankNode'}; } else { graphTerm = {termType: 'NamedNode'}; } graphTerm.value = graphName; } else { // skip relative IRIs (not valid RDF) continue; } _graphToRDF(dataset, nodeMap[graphName], graphTerm, issuer, options); } return dataset; }; /** * Adds RDF quads for a particular graph to the given dataset. * * @param dataset the dataset to append RDF quads to. * @param graph the graph to create RDF quads for. * @param graphTerm the graph term for each quad. * @param issuer a IdentifierIssuer for assigning blank node names. * @param options the RDF serialization options. * * @return the array of RDF triples for the given graph. */ function _graphToRDF(dataset, graph, graphTerm, issuer, options) { const ids = Object.keys(graph).sort(); for(let i = 0; i < ids.length; ++i) { const id = ids[i]; const node = graph[id]; const properties = Object.keys(node).sort(); for(let property of properties) { const items = node[property]; if(property === '@type') { property = RDF_TYPE; } else if(isKeyword(property)) { continue; } for(const item of items) { // RDF subject const subject = { termType: id.startsWith('_:') ? 'BlankNode' : 'NamedNode', value: id }; // skip relative IRI subjects (not valid RDF) if(!_isAbsoluteIri(id)) { continue; } // RDF predicate const predicate = { termType: property.startsWith('_:') ? 'BlankNode' : 'NamedNode', value: property }; // skip relative IRI predicates (not valid RDF) if(!_isAbsoluteIri(property)) { continue; } // skip blank node predicates unless producing generalized RDF if(predicate.termType === 'BlankNode' && !options.produceGeneralizedRdf) { continue; } // convert @list to triples if(graphTypes.isList(item)) { _listToRDF( item['@list'], issuer, subject, predicate, dataset, graphTerm); } else { // convert value or node object to triple const object = _objectToRDF(item); // skip null objects (they are relative IRIs) if(object) { dataset.push({ subject, predicate, object, graph: graphTerm }); } } } } } } /** * Converts a @list value into linked list of blank node RDF quads * (an RDF collection). * * @param list the @list value. * @param issuer a IdentifierIssuer for assigning blank node names. * @param subject the subject for the head of the list. * @param predicate the predicate for the head of the list. * @param dataset the array of quads to append to. * @param graphTerm the graph term for each quad. */ function _listToRDF(list, issuer, subject, predicate, dataset, graphTerm) { const first = {termType: 'NamedNode', value: RDF_FIRST}; const rest = {termType: 'NamedNode', value: RDF_REST}; const nil = {termType: 'NamedNode', value: RDF_NIL}; for(const item of list) { const blankNode = {termType: 'BlankNode', value: issuer.getId()}; dataset.push({ subject, predicate, object: blankNode, graph: graphTerm }); subject = blankNode; predicate = first; const object = _objectToRDF(item); // skip null objects (they are relative IRIs) if(object) { dataset.push({ subject, predicate, object, graph: graphTerm }); } predicate = rest; } dataset.push({ subject, predicate, object: nil, graph: graphTerm }); } /** * Converts a JSON-LD value object to an RDF literal or a JSON-LD string or * node object to an RDF resource. * * @param item the JSON-LD value or node object. * * @return the RDF literal or RDF resource. */ function _objectToRDF(item) { const object = {}; // convert value object to RDF if(graphTypes.isValue(item)) { object.termType = 'Literal'; object.value = undefined; object.datatype = { termType: 'NamedNode' }; let value = item['@value']; const datatype = item['@type'] || null; // convert to XSD datatypes as appropriate if(types.isBoolean(value)) { object.value = value.toString(); object.datatype.value = datatype || XSD_BOOLEAN; } else if(types.isDouble(value) || datatype === XSD_DOUBLE) { if(!types.isDouble(value)) { value = parseFloat(value); } // canonical double representation object.value = value.toExponential(15).replace(/(\d)0*e\+?/, '$1E'); object.datatype.value = datatype || XSD_DOUBLE; } else if(types.isNumber(value)) { object.value = value.toFixed(0); object.datatype.value = datatype || XSD_INTEGER; } else if('@language' in item) { object.value = value; object.datatype.value = datatype || RDF_LANGSTRING; object.language = item['@language']; } else { object.value = value; object.datatype.value = datatype || XSD_STRING; } } else { // convert string/node object to RDF const id = types.isObject(item) ? item['@id'] : item; object.termType = id.startsWith('_:') ? 'BlankNode' : 'NamedNode'; object.value = id; } // skip relative IRIs, not valid RDF if(object.termType === 'NamedNode' && !_isAbsoluteIri(object.value)) { return null; } return object; } jsonld.js-1.6.2/lib/types.js000066400000000000000000000042031347107445200157020ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const api = {}; module.exports = api; /** * Returns true if the given value is an Array. * * @param v the value to check. * * @return true if the value is an Array, false if not. */ api.isArray = Array.isArray; /** * Returns true if the given value is a Boolean. * * @param v the value to check. * * @return true if the value is a Boolean, false if not. */ api.isBoolean = v => (typeof v === 'boolean' || Object.prototype.toString.call(v) === '[object Boolean]'); /** * Returns true if the given value is a double. * * @param v the value to check. * * @return true if the value is a double, false if not. */ api.isDouble = v => api.isNumber(v) && String(v).indexOf('.') !== -1; /** * Returns true if the given value is an empty Object. * * @param v the value to check. * * @return true if the value is an empty Object, false if not. */ api.isEmptyObject = v => api.isObject(v) && Object.keys(v).length === 0; /** * Returns true if the given value is a Number. * * @param v the value to check. * * @return true if the value is a Number, false if not. */ api.isNumber = v => (typeof v === 'number' || Object.prototype.toString.call(v) === '[object Number]'); /** * Returns true if the given value is numeric. * * @param v the value to check. * * @return true if the value is numeric, false if not. */ api.isNumeric = v => !isNaN(parseFloat(v)) && isFinite(v); /** * Returns true if the given value is an Object. * * @param v the value to check. * * @return true if the value is an Object, false if not. */ api.isObject = v => Object.prototype.toString.call(v) === '[object Object]'; /** * Returns true if the given value is a String. * * @param v the value to check. * * @return true if the value is a String, false if not. */ api.isString = v => (typeof v === 'string' || Object.prototype.toString.call(v) === '[object String]'); /** * Returns true if the given value is undefined. * * @param v the value to check. * * @return true if the value is undefined, false if not. */ api.isUndefined = v => typeof v === 'undefined'; jsonld.js-1.6.2/lib/url.js000066400000000000000000000157751347107445200153600ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const types = require('./types'); const api = {}; module.exports = api; // define URL parser // parseUri 1.2.2 // (c) Steven Levithan // MIT License // with local jsonld.js modifications api.parsers = { simple: { // RFC 3986 basic parts keys: [ 'href', 'scheme', 'authority', 'path', 'query', 'fragment' ], regex: /^(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/ }, full: { keys: [ 'href', 'protocol', 'scheme', 'authority', 'auth', 'user', 'password', 'hostname', 'port', 'path', 'directory', 'file', 'query', 'fragment' ], regex: /^(([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?(?:(((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/ } }; api.parse = (str, parser) => { const parsed = {}; const o = api.parsers[parser || 'full']; const m = o.regex.exec(str); let i = o.keys.length; while(i--) { parsed[o.keys[i]] = (m[i] === undefined) ? null : m[i]; } // remove default ports in found in URLs if((parsed.scheme === 'https' && parsed.port === '443') || (parsed.scheme === 'http' && parsed.port === '80')) { parsed.href = parsed.href.replace(':' + parsed.port, ''); parsed.authority = parsed.authority.replace(':' + parsed.port, ''); parsed.port = null; } parsed.normalizedPath = api.removeDotSegments(parsed.path); return parsed; }; /** * Prepends a base IRI to the given relative IRI. * * @param base the base IRI. * @param iri the relative IRI. * * @return the absolute IRI. */ api.prependBase = (base, iri) => { // skip IRI processing if(base === null) { return iri; } // already an absolute IRI if(iri.indexOf(':') !== -1) { return iri; } // parse base if it is a string if(types.isString(base)) { base = api.parse(base || ''); } // parse given IRI const rel = api.parse(iri); // per RFC3986 5.2.2 const transform = { protocol: base.protocol || '' }; if(rel.authority !== null) { transform.authority = rel.authority; transform.path = rel.path; transform.query = rel.query; } else { transform.authority = base.authority; if(rel.path === '') { transform.path = base.path; if(rel.query !== null) { transform.query = rel.query; } else { transform.query = base.query; } } else { if(rel.path.indexOf('/') === 0) { // IRI represents an absolute path transform.path = rel.path; } else { // merge paths let path = base.path; // append relative path to the end of the last directory from base path = path.substr(0, path.lastIndexOf('/') + 1); if(path.length > 0 && path.substr(-1) !== '/') { path += '/'; } path += rel.path; transform.path = path; } transform.query = rel.query; } } if(rel.path !== '') { // remove slashes and dots in path transform.path = api.removeDotSegments(transform.path); } // construct URL let rval = transform.protocol; if(transform.authority !== null) { rval += '//' + transform.authority; } rval += transform.path; if(transform.query !== null) { rval += '?' + transform.query; } if(rel.fragment !== null) { rval += '#' + rel.fragment; } // handle empty base if(rval === '') { rval = './'; } return rval; }; /** * Removes a base IRI from the given absolute IRI. * * @param base the base IRI. * @param iri the absolute IRI. * * @return the relative IRI if relative to base, otherwise the absolute IRI. */ api.removeBase = (base, iri) => { // skip IRI processing if(base === null) { return iri; } if(types.isString(base)) { base = api.parse(base || ''); } // establish base root let root = ''; if(base.href !== '') { root += (base.protocol || '') + '//' + (base.authority || ''); } else if(iri.indexOf('//')) { // support network-path reference with empty base root += '//'; } // IRI not relative to base if(iri.indexOf(root) !== 0) { return iri; } // remove root from IRI and parse remainder const rel = api.parse(iri.substr(root.length)); // remove path segments that match (do not remove last segment unless there // is a hash or query) const baseSegments = base.normalizedPath.split('/'); const iriSegments = rel.normalizedPath.split('/'); const last = (rel.fragment || rel.query) ? 0 : 1; while(baseSegments.length > 0 && iriSegments.length > last) { if(baseSegments[0] !== iriSegments[0]) { break; } baseSegments.shift(); iriSegments.shift(); } // use '../' for each non-matching base segment let rval = ''; if(baseSegments.length > 0) { // don't count the last segment (if it ends with '/' last path doesn't // count and if it doesn't end with '/' it isn't a path) baseSegments.pop(); for(let i = 0; i < baseSegments.length; ++i) { rval += '../'; } } // prepend remaining segments rval += iriSegments.join('/'); // add query and hash if(rel.query !== null) { rval += '?' + rel.query; } if(rel.fragment !== null) { rval += '#' + rel.fragment; } // handle empty base if(rval === '') { rval = './'; } return rval; }; /** * Removes dot segments from a URL path. * * @param path the path to remove dot segments from. */ api.removeDotSegments = path => { // RFC 3986 5.2.4 (reworked) // empty path shortcut if(path.length === 0) { return ''; } const input = path.split('/'); const output = []; while(input.length > 0) { const next = input.shift(); const done = input.length === 0; if(next === '.') { if(done) { // ensure output has trailing / output.push(''); } continue; } if(next === '..') { output.pop(); if(done) { // ensure output has trailing / output.push(''); } continue; } output.push(next); } // ensure output has leading / if(output.length > 0 && output[0] !== '') { output.unshift(''); } if(output.length === 1 && output[0] === '') { return '/'; } return output.join('/'); }; // TODO: time better isAbsolute/isRelative checks using full regexes: // http://jmrware.com/articles/2009/uri_regexp/URI_regex.html // regex to check for absolute IRI (starting scheme and ':') or blank node IRI const isAbsoluteRegex = /^([A-Za-z][A-Za-z0-9+-.]*|_):/; /** * Returns true if the given value is an absolute IRI or blank node IRI, false * if not. * Note: This weak check only checks for a correct starting scheme. * * @param v the value to check. * * @return true if the value is an absolute IRI, false if not. */ api.isAbsolute = v => types.isString(v) && isAbsoluteRegex.test(v); /** * Returns true if the given value is a relative IRI, false if not. * Note: this is a weak check. * * @param v the value to check. * * @return true if the value is a relative IRI, false if not. */ api.isRelative = v => types.isString(v); jsonld.js-1.6.2/lib/util.js000066400000000000000000000327051347107445200155230ustar00rootroot00000000000000/* * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; const graphTypes = require('./graphTypes'); const types = require('./types'); // TODO: move `IdentifierIssuer` to its own package const IdentifierIssuer = require('rdf-canonize').IdentifierIssuer; const JsonLdError = require('./JsonLdError'); // constants const REGEX_LINK_HEADERS = /(?:<[^>]*?>|"[^"]*?"|[^,])+/g; const REGEX_LINK_HEADER = /\s*<([^>]*?)>\s*(?:;\s*(.*))?/; const REGEX_LINK_HEADER_PARAMS = /(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/g; const DEFAULTS = { headers: { accept: 'application/ld+json, application/json' } }; const api = {}; module.exports = api; api.IdentifierIssuer = IdentifierIssuer; // define setImmediate and nextTick // // nextTick implementation with browser-compatible fallback // // // from https://github.com/caolan/async/blob/master/lib/async.js // capture the global reference to guard against fakeTimer mocks const _setImmediate = typeof setImmediate === 'function' && setImmediate; const _delay = _setImmediate ? // not a direct alias (for IE10 compatibility) fn => _setImmediate(fn) : fn => setTimeout(fn, 0); if(typeof process === 'object' && typeof process.nextTick === 'function') { api.nextTick = process.nextTick; } else { api.nextTick = _delay; } api.setImmediate = _setImmediate ? _delay : api.nextTick; /** * Clones an object, array, Map, Set, or string/number. If a typed JavaScript * object is given, such as a Date, it will be converted to a string. * * @param value the value to clone. * * @return the cloned value. */ api.clone = function(value) { if(value && typeof value === 'object') { let rval; if(types.isArray(value)) { rval = []; for(let i = 0; i < value.length; ++i) { rval[i] = api.clone(value[i]); } } else if(value instanceof Map) { rval = new Map(); for(const [k, v] of value) { rval.set(k, api.clone(v)); } } else if(value instanceof Set) { rval = new Set(); for(const v of value) { rval.add(api.clone(v)); } } else if(types.isObject(value)) { rval = {}; for(const key in value) { rval[key] = api.clone(value[key]); } } else { rval = value.toString(); } return rval; } return value; }; /** * Ensure a value is an array. If the value is an array, it is returned. * Otherwise, it is wrapped in an array. * * @param value the value to return as an array. * * @return the value as an array. */ api.asArray = function(value) { return Array.isArray(value) ? value : [value]; }; /** * Builds an HTTP headers object for making a JSON-LD request from custom * headers and asserts the `accept` header isn't overridden. * * @param headers an object of headers with keys as header names and values * as header values. * * @return an object of headers with a valid `accept` header. */ api.buildHeaders = (headers = {}) => { const hasAccept = Object.keys(headers).some( h => h.toLowerCase() === 'accept'); if(hasAccept) { throw new RangeError( 'Accept header may not be specified; only "' + DEFAULTS.headers.accept + '" is supported.'); } return Object.assign({Accept: DEFAULTS.headers.accept}, headers); }; /** * Parses a link header. The results will be key'd by the value of "rel". * * Link: ; * rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json" * * Parses as: { * 'http://www.w3.org/ns/json-ld#context': { * target: http://json-ld.org/contexts/person.jsonld, * type: 'application/ld+json' * } * } * * If there is more than one "rel" with the same IRI, then entries in the * resulting map for that "rel" will be arrays. * * @param header the link header to parse. */ api.parseLinkHeader = header => { const rval = {}; // split on unbracketed/unquoted commas const entries = header.match(REGEX_LINK_HEADERS); for(let i = 0; i < entries.length; ++i) { let match = entries[i].match(REGEX_LINK_HEADER); if(!match) { continue; } const result = {target: match[1]}; const params = match[2]; while((match = REGEX_LINK_HEADER_PARAMS.exec(params))) { result[match[1]] = (match[2] === undefined) ? match[3] : match[2]; } const rel = result['rel'] || ''; if(Array.isArray(rval[rel])) { rval[rel].push(result); } else if(rval.hasOwnProperty(rel)) { rval[rel] = [rval[rel], result]; } else { rval[rel] = result; } } return rval; }; /** * Throws an exception if the given value is not a valid @type value. * * @param v the value to check. */ api.validateTypeValue = v => { // can be a string or an empty object if(types.isString(v) || types.isEmptyObject(v)) { return; } // must be an array let isValid = false; if(types.isArray(v)) { // must contain only strings isValid = true; for(let i = 0; i < v.length; ++i) { if(!(types.isString(v[i]))) { isValid = false; break; } } } if(!isValid) { throw new JsonLdError( 'Invalid JSON-LD syntax; "@type" value must a string, an array of ' + 'strings, or an empty object.', 'jsonld.SyntaxError', {code: 'invalid type value', value: v}); } }; /** * Returns true if the given subject has the given property. * * @param subject the subject to check. * @param property the property to look for. * * @return true if the subject has the given property, false if not. */ api.hasProperty = (subject, property) => { if(subject.hasOwnProperty(property)) { const value = subject[property]; return (!types.isArray(value) || value.length > 0); } return false; }; /** * Determines if the given value is a property of the given subject. * * @param subject the subject to check. * @param property the property to check. * @param value the value to check. * * @return true if the value exists, false if not. */ api.hasValue = (subject, property, value) => { if(api.hasProperty(subject, property)) { let val = subject[property]; const isList = graphTypes.isList(val); if(types.isArray(val) || isList) { if(isList) { val = val['@list']; } for(let i = 0; i < val.length; ++i) { if(api.compareValues(value, val[i])) { return true; } } } else if(!types.isArray(value)) { // avoid matching the set of values with an array value parameter return api.compareValues(value, val); } } return false; }; /** * Adds a value to a subject. If the value is an array, all values in the * array will be added. * * @param subject the subject to add the value to. * @param property the property that relates the value to the subject. * @param value the value to add. * @param [options] the options to use: * [propertyIsArray] true if the property is always an array, false * if not (default: false). * [allowDuplicate] true to allow duplicates, false not to (uses a * simple shallow comparison of subject ID or value) (default: true). */ api.addValue = (subject, property, value, options) => { options = options || {}; if(!('propertyIsArray' in options)) { options.propertyIsArray = false; } if(!('allowDuplicate' in options)) { options.allowDuplicate = true; } if(types.isArray(value)) { if(value.length === 0 && options.propertyIsArray && !subject.hasOwnProperty(property)) { subject[property] = []; } for(let i = 0; i < value.length; ++i) { api.addValue(subject, property, value[i], options); } } else if(subject.hasOwnProperty(property)) { // check if subject already has value if duplicates not allowed const hasValue = (!options.allowDuplicate && api.hasValue(subject, property, value)); // make property an array if value not present or always an array if(!types.isArray(subject[property]) && (!hasValue || options.propertyIsArray)) { subject[property] = [subject[property]]; } // add new value if(!hasValue) { subject[property].push(value); } } else { // add new value as set or single value subject[property] = options.propertyIsArray ? [value] : value; } }; /** * Gets all of the values for a subject's property as an array. * * @param subject the subject. * @param property the property. * * @return all of the values for a subject's property as an array. */ api.getValues = (subject, property) => [].concat(subject[property] || []); /** * Removes a property from a subject. * * @param subject the subject. * @param property the property. */ api.removeProperty = (subject, property) => { delete subject[property]; }; /** * Removes a value from a subject. * * @param subject the subject. * @param property the property that relates the value to the subject. * @param value the value to remove. * @param [options] the options to use: * [propertyIsArray] true if the property is always an array, false * if not (default: false). */ api.removeValue = (subject, property, value, options) => { options = options || {}; if(!('propertyIsArray' in options)) { options.propertyIsArray = false; } // filter out value const values = api.getValues(subject, property).filter( e => !api.compareValues(e, value)); if(values.length === 0) { api.removeProperty(subject, property); } else if(values.length === 1 && !options.propertyIsArray) { subject[property] = values[0]; } else { subject[property] = values; } }; /** * Relabels all blank nodes in the given JSON-LD input. * * @param input the JSON-LD input. * @param [options] the options to use: * [issuer] an IdentifierIssuer to use to label blank nodes. */ api.relabelBlankNodes = (input, options) => { options = options || {}; const issuer = options.issuer || new IdentifierIssuer('_:b'); return _labelBlankNodes(issuer, input); }; /** * Compares two JSON-LD values for equality. Two JSON-LD values will be * considered equal if: * * 1. They are both primitives of the same type and value. * 2. They are both @values with the same @value, @type, @language, * and @index, OR * 3. They both have @ids they are the same. * * @param v1 the first value. * @param v2 the second value. * * @return true if v1 and v2 are considered equal, false if not. */ api.compareValues = (v1, v2) => { // 1. equal primitives if(v1 === v2) { return true; } // 2. equal @values if(graphTypes.isValue(v1) && graphTypes.isValue(v2) && v1['@value'] === v2['@value'] && v1['@type'] === v2['@type'] && v1['@language'] === v2['@language'] && v1['@index'] === v2['@index']) { return true; } // 3. equal @ids if(types.isObject(v1) && ('@id' in v1) && types.isObject(v2) && ('@id' in v2)) { return v1['@id'] === v2['@id']; } return false; }; /** * Compares two strings first based on length and then lexicographically. * * @param a the first string. * @param b the second string. * * @return -1 if a < b, 1 if a > b, 0 if a === b. */ api.compareShortestLeast = (a, b) => { if(a.length < b.length) { return -1; } if(b.length < a.length) { return 1; } if(a === b) { return 0; } return (a < b) ? -1 : 1; }; api.normalizeDocumentLoader = fn => { if(fn.length < 2) { return api.callbackify(fn); } return async function(url) { const callback = arguments[1]; return new Promise((resolve, reject) => { try { fn(url, (err, remoteDoc) => { if(typeof callback === 'function') { return _invokeCallback(callback, err, remoteDoc); } else if(err) { reject(err); } else { resolve(remoteDoc); } }); } catch(e) { if(typeof callback === 'function') { return _invokeCallback(callback, e); } reject(e); } }); }; }; api.callbackify = fn => { return async function(...args) { const callback = args[args.length - 1]; if(typeof callback === 'function') { args.pop(); } let result; try { result = await fn.apply(null, args); } catch(e) { if(typeof callback === 'function') { return _invokeCallback(callback, e); } throw e; } if(typeof callback === 'function') { return _invokeCallback(callback, null, result); } return result; }; }; function _invokeCallback(callback, err, result) { // execute on next tick to prevent "unhandled rejected promise" // and simulate what would have happened in a promiseless API api.nextTick(() => callback(err, result)); } /** * Labels the blank nodes in the given value using the given IdentifierIssuer. * * @param issuer the IdentifierIssuer to use. * @param element the element with blank nodes to rename. * * @return the element. */ function _labelBlankNodes(issuer, element) { if(types.isArray(element)) { for(let i = 0; i < element.length; ++i) { element[i] = _labelBlankNodes(issuer, element[i]); } } else if(graphTypes.isList(element)) { element['@list'] = _labelBlankNodes(issuer, element['@list']); } else if(types.isObject(element)) { // relabel blank node if(graphTypes.isBlankNode(element)) { element['@id'] = issuer.getId(element['@id']); } // recursively apply to all keys const keys = Object.keys(element).sort(); for(let ki = 0; ki < keys.length; ++ki) { const key = keys[ki]; if(key !== '@id') { element[key] = _labelBlankNodes(issuer, element[key]); } } } return element; } jsonld.js-1.6.2/package.json000066400000000000000000000103531347107445200157230ustar00rootroot00000000000000{ "name": "jsonld", "version": "1.6.2", "description": "A JSON-LD Processor and API implementation in JavaScript.", "homepage": "https://github.com/digitalbazaar/jsonld.js", "author": { "name": "Digital Bazaar, Inc.", "email": "support@digitalbazaar.com", "url": "https://digitalbazaar.com/" }, "contributors": [ "Dave Longley ", "David I. Lehn " ], "repository": { "type": "git", "url": "https://github.com/digitalbazaar/jsonld.js" }, "bugs": { "url": "https://github.com/digitalbazaar/jsonld.js/issues", "email": "support@digitalbazaar.com" }, "license": "BSD-3-Clause", "main": "lib/index.js", "files": [ "dist/*.js", "dist/*.js.map", "dist/node6/**/*.js", "lib/*.js", "lib/**/*.js" ], "dependencies": { "rdf-canonize": "^1.0.2", "request": "^2.88.0", "semver": "^5.6.0", "xmldom": "0.1.19" }, "devDependencies": { "@babel/cli": "^7.2.3", "@babel/core": "^7.3.3", "@babel/plugin-proposal-object-rest-spread": "^7.3.2", "@babel/plugin-transform-modules-commonjs": "^7.2.0", "@babel/plugin-transform-runtime": "^7.2.0", "@babel/preset-env": "^7.3.1", "@babel/register": "^7.0.0", "@babel/runtime": "^7.3.1", "babel-loader": "^8.0.5", "benchmark": "^2.1.4", "browserify": "^16.2.3", "chai": "^4.2.0", "commander": "^2.19.0", "core-js": "^2.6.5", "cors": "^2.7.1", "cross-env": "^5.2.0", "eslint": "^5.14.1", "eslint-config-digitalbazaar": "^1.6.0", "express": "^4.16.4", "fs-extra": "^7.0.0", "join-path-js": "0.0.0", "karma": "^3.1.1", "karma-babel-preprocessor": "^8.0.0", "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", "karma-edge-launcher": "^0.4.2", "karma-firefox-launcher": "^1.1.0", "karma-ie-launcher": "^1.0.0", "karma-mocha": "^1.3.0", "karma-mocha-reporter": "^2.2.5", "karma-safari-launcher": "^1.0.0", "karma-server-side": "^1.7.0", "karma-sourcemap-loader": "^0.3.7", "karma-tap-reporter": "0.0.6", "karma-webpack": "^3.0.5", "mocha": "^5.2.0", "mocha-lcov-reporter": "^1.3.0", "nyc": "^13.3.0", "webpack": "^4.29.5", "webpack-cli": "^3.2.3", "webpack-merge": "^4.2.1" }, "engines": { "node": ">=6" }, "keywords": [ "JSON", "Linked Data", "JSON-LD", "RDF", "Semantic Web", "jsonld" ], "scripts": { "prepublish": "npm run build", "build": "npm run build-webpack && npm run build-node6", "build-webpack": "webpack", "build-node6": "BROWSERSLIST='node 6' babel --no-babelrc lib --out-dir dist/node6/lib --presets=@babel/preset-env", "fetch-test-suites": "npm run fetch-json-ld-wg-test-suite && npm run fetch-json-ld-org-test-suite && npm run fetch-normalization-test-suite", "fetch-json-ld-wg-test-suite": "npm run fetch-json-ld-api-test-suite && npm run fetch-json-ld-framing-test-suite", "fetch-json-ld-api-test-suite": "if [ ! -e test-suites/json-wg-api ]; then git clone --depth 1 https://github.com/w3c/json-ld-api.git test-suites/json-ld-api; fi", "fetch-json-ld-framing-test-suite": "if [ ! -e test-suites/json-wg-framing ]; then git clone --depth 1 https://github.com/w3c/json-ld-framing.git test-suites/json-ld-framing; fi", "fetch-json-ld-org-test-suite": "if [ ! -e test-suites/json-ld.org ]; then git clone --depth 1 https://github.com/json-ld/json-ld.org.git test-suites/json-ld.org; fi", "fetch-normalization-test-suite": "if [ ! -e test-suites/normalization ]; then git clone --depth 1 https://github.com/json-ld/normalization.git test-suites/normalization; fi", "test": "cross-env NODE_ENV=test mocha --delay -t 30000 -A -R ${REPORTER:-spec} tests/test.js", "test-karma": "cross-env NODE_ENV=test karma start", "coverage": "nyc --reporter=lcov --reporter=text-summary npm test", "coverage-report": "nyc report", "lint": "eslint *.js lib/**.js tests/**.js" }, "nyc": { "exclude": [ "lib/documentLoaders/xhr.js", "tests" ] }, "browser": { "lib/index.js": "./lib/jsonld.js", "crypto": false, "http": false, "jsonld-request": false, "request": false, "url": false, "util": false, "xmldom": false } } jsonld.js-1.6.2/tests/000077500000000000000000000000001347107445200145755ustar00rootroot00000000000000jsonld.js-1.6.2/tests/callbacks.js000066400000000000000000000105211347107445200170510ustar00rootroot00000000000000/** * Test the callback interface. */ const jsonld = require('..'); const assert = require('assert'); describe('callback API', () => { // common data const doc = {}; const ctx = {}; const frame = {}; const options = {}; it('should compact', done => { jsonld.compact(doc, ctx, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {}); done(); }); }); it('should compact with options', done => { jsonld.compact(doc, ctx, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {}); done(); }); }); it('should expand', done => { jsonld.expand(doc, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, []); done(); }); }); it('should expand with options', done => { jsonld.expand(doc, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, []); done(); }); }); it('should flatten', done => { jsonld.flatten(doc, ctx, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should flatten with options', done => { jsonld.flatten(doc, ctx, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should frame', done => { jsonld.frame(doc, frame, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should frame with options', done => { jsonld.frame(doc, frame, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should link', done => { jsonld.link(doc, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should link with context', done => { jsonld.link(doc, ctx, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should link with context and options', done => { jsonld.link(doc, ctx, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should normalize', done => { jsonld.normalize(doc, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, ''); done(); }); }); it('should normalize with options', done => { jsonld.normalize(doc, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, ''); done(); }); }); it('should convert from RDF', done => { jsonld.fromRDF('', (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, []); done(); }); }); it('should convert from RDF with options', done => { jsonld.fromRDF('', options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, []); done(); }); }); it('should convert to RDF', done => { jsonld.toRDF(doc, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, []); done(); }); }); it('should convert to RDF with options', done => { jsonld.toRDF(doc, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, []); done(); }); }); it('should create node map', done => { jsonld.createNodeMap(doc, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {}); done(); }); }); it('should create node map with options', done => { jsonld.createNodeMap(doc, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {}); done(); }); }); it('should merge', done => { jsonld.merge([doc, doc], ctx, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); it('should merge with options', done => { jsonld.merge([doc, doc], ctx, options, (err, result) => { assert.ifError(err); assert.deepStrictEqual(result, {'@graph': []}); done(); }); }); // TODO //it('should load document'); //it('should get document'); //it('should process context'); }); jsonld.js-1.6.2/tests/contexts/000077500000000000000000000000001347107445200164445ustar00rootroot00000000000000jsonld.js-1.6.2/tests/contexts/context-1.jsonld000066400000000000000000000000771347107445200215050ustar00rootroot00000000000000{ "@context": { "term1": "http://example.org/term1" } }jsonld.js-1.6.2/tests/contexts/context-2.jsonld000066400000000000000000000000721347107445200215010ustar00rootroot00000000000000{ "@context": "http://localhost:8000/context-1.jsonld" }jsonld.js-1.6.2/tests/contexts/context-3.jsonld000066400000000000000000000000721347107445200215020ustar00rootroot00000000000000{ "@context": "http://localhost:8000/context-2.jsonld" }jsonld.js-1.6.2/tests/contexts/context-4.jsonld000066400000000000000000000001421347107445200215010ustar00rootroot00000000000000{ "@context": [ "/context-1.jsonld", "/context-2.jsonld", "/context-3.jsonld" ] }jsonld.js-1.6.2/tests/contexts/context-5.jsonld000066400000000000000000000000441347107445200215030ustar00rootroot00000000000000{ "@context": "context-1.jsonld" }jsonld.js-1.6.2/tests/earl-report.js000066400000000000000000000054141347107445200173730ustar00rootroot00000000000000/** * EARL Report * * @author Dave Longley * * Copyright (c) 2011-2017 Digital Bazaar, Inc. All rights reserved. */ /** * Create an EARL Reporter. * * @param options {Object} reporter options * id: {String} report id */ function EarlReport(options) { let today = new Date(); today = today.getFullYear() + '-' + (today.getMonth() < 9 ? '0' + (today.getMonth() + 1) : today.getMonth() + 1) + '-' + (today.getDate() < 10 ? '0' + today.getDate() : today.getDate()); this.id = options.id; this._report = { '@context': { 'doap': 'http://usefulinc.com/ns/doap#', 'foaf': 'http://xmlns.com/foaf/0.1/', 'dc': 'http://purl.org/dc/terms/', 'earl': 'http://www.w3.org/ns/earl#', 'xsd': 'http://www.w3.org/2001/XMLSchema#', 'doap:homepage': {'@type': '@id'}, 'doap:license': {'@type': '@id'}, 'dc:creator': {'@type': '@id'}, 'foaf:homepage': {'@type': '@id'}, 'subjectOf': {'@reverse': 'earl:subject'}, 'earl:assertedBy': {'@type': '@id'}, 'earl:mode': {'@type': '@id'}, 'earl:test': {'@type': '@id'}, 'earl:outcome': {'@type': '@id'}, 'dc:date': {'@type': 'xsd:date'} }, '@id': 'https://github.com/digitalbazaar/jsonld.js', '@type': [ 'doap:Project', 'earl:TestSubject', 'earl:Software' ], 'doap:name': 'jsonld.js', 'dc:title': 'jsonld.js', 'doap:homepage': 'https://github.com/digitalbazaar/jsonld.js', 'doap:license': 'https://github.com/digitalbazaar/jsonld.js/blob/master/LICENSE', 'doap:description': 'A JSON-LD processor for JavaScript', 'doap:programming-language': 'JavaScript', 'dc:creator': 'https://github.com/dlongley', 'doap:developer': { '@id': 'https://github.com/dlongley', '@type': [ 'foaf:Person', 'earl:Assertor' ], 'foaf:name': 'Dave Longley', 'foaf:homepage': 'https://github.com/dlongley' }, 'dc:date': { '@value': today, '@type': 'xsd:date' }, 'subjectOf': [] }; this._report['@id'] += '#' + this.id; this._report['doap:name'] += ' ' + this.id; this._report['dc:title'] += ' ' + this.id; } EarlReport.prototype.addAssertion = function(test, pass) { this._report.subjectOf.push({ '@type': 'earl:Assertion', 'earl:assertedBy': this._report['doap:developer']['@id'], 'earl:mode': 'earl:automatic', 'earl:test': test['@id'], 'earl:result': { '@type': 'earl:TestResult', 'dc:date': new Date().toISOString(), 'earl:outcome': pass ? 'earl:passed' : 'earl:failed' } }); return this; }; EarlReport.prototype.report = function() { return this._report; }; EarlReport.prototype.reportJson = function() { return JSON.stringify(this._report, null, 2); }; module.exports = EarlReport; jsonld.js-1.6.2/tests/fromRdf-0001-in.nq000066400000000000000000000007151347107445200175210ustar00rootroot00000000000000 . # $5 comment . "Plain" . # Another comment "2012-05-12"^^ . "English"@en . jsonld.js-1.6.2/tests/fromRdf-0001-out.jsonld000066400000000000000000000005471347107445200206000ustar00rootroot00000000000000[ { "@id": "http://example.com/Subj1", "@type": ["http://example.com/Type"], "http://example.com/prop1": [{"@id": "http://example.com/Obj1"}], "http://example.com/prop2": [ {"@value": "Plain"}, {"@value": "2012-05-12", "@type": "http://www.w3.org/2001/XMLSchema#date"}, {"@value": "English", "@language": "en"} ] } ] jsonld.js-1.6.2/tests/graph-container.js000066400000000000000000000063051347107445200202200ustar00rootroot00000000000000/** * Temporary graph-container tests. */ // disable so tests can be copy & pasted /* eslint-disable quotes, quote-props */ const jsonld = require('..'); const assert = require('assert'); describe('@graph container', () => { it('should expand @graph container', done => { const doc = { "@context": { "@version": 1.1, "input": {"@id": "foo:input", "@container": "@graph"}, "value": "foo:value" }, "input": { "value": "x" } }; jsonld.expand(doc, (err, expanded) => { assert.ifError(err); assert.deepEqual(expanded, [{ "foo:input": [{ "@graph": [{ "foo:value": [{ "@value": "x" }] }] }] }]); done(); }); }); it('should expand ["@graph", "@set"] container', done => { const doc = { "@context": { "@version": 1.1, "input": {"@id": "foo:input", "@container": ["@graph", "@set"]}, "value": "foo:value" }, "input": [{ "value": "x" }] }; jsonld.expand(doc, (err, expanded) => { assert.ifError(err); assert.deepEqual(expanded, [{ "foo:input": [{ "@graph": [{ "foo:value": [{ "@value": "x" }] }] }] }]); done(); }); }); it('should expand and then compact @graph container', done => { const doc = { "@context": { "@version": 1.1, "input": {"@id": "foo:input", "@container": "@graph"}, "value": "foo:value" }, "input": { "value": "x" } }; jsonld.expand(doc, (err, expanded) => { assert.ifError(err); jsonld.compact(expanded, doc['@context'], (err, compacted) => { assert.ifError(err); assert.deepEqual(compacted, { "@context": { "@version": 1.1, "input": { "@id": "foo:input", "@container": "@graph" }, "value": "foo:value" }, "input": { "value": "x" } }); done(); }); }); }); it('should expand and then compact @graph container into a @set', done => { const doc = { "@context": { "@version": 1.1, "input": {"@id": "foo:input", "@container": "@graph"}, "value": "foo:value" }, "input": { "value": "x" } }; const newContext = { "@context": { "@version": 1.1, "input": {"@id": "foo:input", "@container": ["@graph", "@set"]}, "value": "foo:value" } }; jsonld.expand(doc, (err, expanded) => { assert.ifError(err); jsonld.compact(expanded, newContext, (err, compacted) => { assert.ifError(err); assert.deepEqual(compacted, { "@context": { "@version": 1.1, "input": { "@id": "foo:input", "@container": [ "@graph", "@set" ] }, "value": "foo:value" }, "input": [ { "value": "x" } ] }); done(); }); }); }); }); jsonld.js-1.6.2/tests/manifest.jsonld000066400000000000000000000032661347107445200176250ustar00rootroot00000000000000{ "@context": "http://json-ld.org/test-suite/context.jsonld", "@id": "", "@type": "mf:Manifest", "name": "Custom jsonld.js Tests", "description": "Custom jsonld.js Tests", "baseIri": "./", "sequence": [{ "@id": "#t0001", "@type": ["jld:PositiveEvaluationTest", "jld:ExpandTest"], "name": "resolve single URL", "input": "remote-0001-in.jsonld", "expect": "remote-0001-out.jsonld" }, { "@id": "#t0002", "@type": ["jld:PositiveEvaluationTest", "jld:ExpandTest"], "name": "resolve recursive URL", "input": "remote-0002-in.jsonld", "expect": "remote-0002-out.jsonld" }, { "@id": "#t0003", "@type": ["jld:PositiveEvaluationTest", "jld:ExpandTest"], "name": "resolve doubly recursive URL", "input": "remote-0003-in.jsonld", "expect": "remote-0003-out.jsonld" }, { "@id": "#t0004", "@type": ["jld:PositiveEvaluationTest", "jld:ExpandTest"], "name": "resolve array context", "input": "remote-0004-in.jsonld", "expect": "remote-0004-out.jsonld" }, { "@id": "#t0005", "@type": ["jld:PositiveEvaluationTest", "jld:ExpandTest"], "name": "resolve relative URL", "input": "remote-0005-in.jsonld", "expect": "remote-0005-out.jsonld" }, { "@id": "#t0006", "@type": ["jld:PositiveEvaluationTest", "jld:ExpandTest"], "name": "follow redirect", "input": "remote-0006-in.jsonld", "expect": "remote-0006-out.jsonld" }, { "@id": "#t0007", "@type": ["jld:PositiveEvaluationTest", "jld:FromRDFTest"], "name": "allow comments in N-Quads", "purpose": "RDF serialized in N-Quads may contain comments", "input": "fromRdf-0001-in.nq", "expect": "fromRdf-0001-out.jsonld" }] } jsonld.js-1.6.2/tests/misc.js000066400000000000000000000266631347107445200161030ustar00rootroot00000000000000/** * Misc tests. */ // disable so tests can be copy & pasted /* eslint-disable quotes, quote-props */ const jsonld = require('..'); const assert = require('assert'); // TODO: need more tests for jsonld.link and jsonld.merge describe('link tests', () => { const doc = { "@id": "ex:1", "a:foo": { "@id": "ex:1" } }; it('should create a circular link', done => { jsonld.link(doc, {}, (err, output) => { assert.ifError(err); output = output['@graph'][0]; assert.equal(output, output['a:foo']); done(); }); }); }); describe('merge tests', () => { const docA = {"@id": "ex:1", "a:foo": [{"@value": 1}]}; const docB = {"@id": "ex:1", "b:foo": [{"@value": 2}]}; const merged = [Object.assign({}, docA, docB)]; const context = {}; const ctxMerged = {"@graph": [{"@id": "ex:1", "a:foo": 1, "b:foo": 2}]}; it('should merge nodes from two different documents', done => { jsonld.merge([docA, docB], (err, output) => { assert.ifError(err); assert.deepEqual(output, merged); done(); }); }); it('should merge nodes from two different documents with context', done => { jsonld.merge([docA, docB], context, (err, output) => { assert.ifError(err); assert.deepEqual(output, ctxMerged); done(); }); }); }); describe('createNodeMap', () => { const doc = {"@id": "ex:1", "a:property": [{"@id": "ex:2"}]}; it('should create a flattened node hashmap', () => { const expected = { "ex:1": { "@id": "ex:1", "a:property": [ {"@id": "ex:2"} ] }, "ex:2": {"@id": "ex:2"} }; return jsonld.createNodeMap(doc).then(map => { assert.deepEqual(map, expected); }); }); }); describe('other toRDF tests', () => { const emptyRdf = []; it('should process with options and callback', done => { jsonld.toRDF({}, {}, (err, output) => { assert.ifError(err); assert.deepEqual(output, emptyRdf); done(); }); }); it('should process with no options and callback', done => { jsonld.toRDF({}, (err, output) => { assert.ifError(err); assert.deepEqual(output, emptyRdf); done(); }); }); it('should process with options and promise', done => { const p = jsonld.toRDF({}, {}); assert(p instanceof Promise); p.catch(e => { assert.fail(); }).then(output => { assert.deepEqual(output, emptyRdf); done(); }); }); it('should process with no options and promise', done => { const p = jsonld.toRDF({}); assert(p instanceof Promise); p.catch(e => { assert.fail(); }).then(output => { assert.deepEqual(output, emptyRdf); done(); }); }); it('should fail with no args and callback', done => { jsonld.toRDF((err, output) => { assert(err); done(); }); }); it('should fail with no args and promise', done => { const p = jsonld.toRDF(); assert(p instanceof Promise); p.then(output => { assert.fail(); }).catch(e => { assert(e); done(); }); }); it('should fail for bad format and callback', done => { jsonld.toRDF({}, {format: 'bogus'}, (err, output) => { assert(err); assert.equal(err.name, 'jsonld.UnknownFormat'); done(); }); }); it('should fail for bad format and promise', done => { const p = jsonld.toRDF({}, {format: 'bogus'}); assert(p instanceof Promise); p.then(() => { assert.fail(); }).catch(e => { assert(e); assert.equal(e.name, 'jsonld.UnknownFormat'); done(); }); }); it('should handle N-Quads format', done => { const doc = { "@id": "https://example.com/", "https://example.com/test": "test" }; jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, output) => { assert.ifError(err); assert.equal( output, ' "test" .\n'); done(); }); }); it('should handle deprecated N-Quads format', done => { const doc = { "@id": "https://example.com/", "https://example.com/test": "test" }; jsonld.toRDF(doc, {format: 'application/nquads'}, (err, output) => { assert.ifError(err); assert.equal( output, ' "test" .\n'); done(); }); }); }); describe('other fromRDF tests', () => { const emptyNQuads = ''; const emptyRdf = []; it('should process with options and callback', done => { jsonld.fromRDF('', {}, (err, output) => { assert.ifError(err); assert.deepEqual(output, emptyRdf); done(); }); }); it('should process with no options and callback', done => { jsonld.fromRDF(emptyNQuads, (err, output) => { assert.ifError(err); assert.deepEqual(output, emptyRdf); done(); }); }); it('should process with options and promise', done => { const p = jsonld.fromRDF(emptyNQuads, {}); assert(p instanceof Promise); p.catch(e => { assert.fail(); }).then(output => { assert.deepEqual(output, emptyRdf); done(); }); }); it('should process with no options and promise', done => { const p = jsonld.fromRDF(emptyNQuads); assert(p instanceof Promise); p.catch(e => { assert.fail(); }).then(output => { assert.deepEqual(output, emptyRdf); done(); }); }); it('should fail with no args and callback', done => { jsonld.fromRDF((err, output) => { assert(err); done(); }); }); it('should fail with no args and promise', done => { const p = jsonld.fromRDF(); assert(p instanceof Promise); p.then(output => { assert.fail(); }).catch(e => { assert(e); done(); }); }); it('should fail for bad format and callback', done => { jsonld.fromRDF(emptyNQuads, {format: 'bogus'}, (err, output) => { assert(err); assert.equal(err.name, 'jsonld.UnknownFormat'); done(); }); }); it('should fail for bad format and promise', done => { const p = jsonld.fromRDF(emptyNQuads, {format: 'bogus'}); assert(p instanceof Promise); p.then(() => { assert.fail(); }).catch(e => { assert(e); assert.equal(e.name, 'jsonld.UnknownFormat'); done(); }); }); it('should handle N-Quads format', done => { const nq = ' "test" .\n'; jsonld.fromRDF(nq, {format: 'application/n-quads'}, (err, output) => { assert.ifError(err); assert.deepEqual( output, [{ "@id": "https://example.com/", "https://example.com/test": [{ "@value": "test" }] }]); done(); }); }); it('should handle deprecated N-Quads format', done => { const nq = ' "test" .\n'; jsonld.fromRDF(nq, {format: 'application/nquads'}, (err, output) => { assert.ifError(err); assert.deepEqual( output, [{ "@id": "https://example.com/", "https://example.com/test": [{ "@value": "test" }] }]); done(); }); }); }); describe('loading multiple levels of contexts', () => { const documentLoader = url => { if(url === 'https://example.com/context1') { return { document: { "@context": { "ex": "https://example.com/#" } }, contextUrl: null, documentUrl: url }; } if(url === 'https://example.com/context2') { return { document: { "@context": { "ex": "https://example.com/#" } }, contextUrl: null, documentUrl: url }; } }; const doc = { "@context": "https://example.com/context1", "ex:foo": { "@context": "https://example.com/context2", "ex:bar": "test" } }; const expected = [{ "https://example.com/#foo": [{ "https://example.com/#bar": [{ "@value": "test" }] }] }]; it('should handle loading multiple levels of contexts (promise)', () => { return jsonld.expand(doc, {documentLoader}).then(output => { assert.deepEqual(output, expected); }); }); it('should handle loading multiple levels of contexts (callback)', done => { jsonld.expand(doc, {documentLoader}, (err, output) => { assert.ifError(err); assert.deepEqual(output, expected); done(); }); }); }); describe('url tests', () => { it('should detect absolute IRIs', done => { // absolute IRIs assert(jsonld.url.isAbsolute('a:')); assert(jsonld.url.isAbsolute('a:b')); assert(jsonld.url.isAbsolute('a:b:c')); // blank nodes assert(jsonld.url.isAbsolute('_:')); assert(jsonld.url.isAbsolute('_:a')); assert(jsonld.url.isAbsolute('_:a:b')); // not absolute or blank node assert(!jsonld.url.isAbsolute(':')); assert(!jsonld.url.isAbsolute('a')); assert(!jsonld.url.isAbsolute('/:')); assert(!jsonld.url.isAbsolute('/a:')); assert(!jsonld.url.isAbsolute('/a:b')); assert(!jsonld.url.isAbsolute('_')); done(); }); }); describe('js keywords', () => { it('expand js valueOf/toString keywords (top ctx)', async () => { const d = { "@context": { "valueOf": "http://example.org/valueOf", "toString": "http://example.org/toString" }, "valueOf": "first", "toString": "second" } ; const ex = [ { "http://example.org/toString": [ { "@value": "second" } ], "http://example.org/valueOf": [ { "@value": "first" } ] } ] ; const e = await jsonld.expand(d); assert.deepStrictEqual(e, ex); }); it('expand js valueOf/toString keywords (sub ctx)', async () => { const d = { "@context": { "@version": 1.1, "ex:thing": { "@context": { "valueOf": "http://example.org/valueOf", "toString": "http://example.org/toString" } } }, "ex:thing": { "valueOf": "first", "toString": "second" } } ; const ex = [ { "ex:thing": [ { "http://example.org/toString": [ { "@value": "second" } ], "http://example.org/valueOf": [ { "@value": "first" } ] } ] } ] ; const e = await jsonld.expand(d); assert.deepStrictEqual(e, ex); }); it('compact js valueOf/toString keywords', async () => { const d = { "@context": { "valueOf": "http://example.org/valueOf", "toString": "http://example.org/toString" }, "valueOf": "first", "toString": "second" } ; const ctx = { "@context": { "valueOf": "http://example.org/valueOf", "toString": "http://example.org/toString" } } ; const ex = { "@context": { "valueOf": "http://example.org/valueOf", "toString": "http://example.org/toString" }, "valueOf": "first", "toString": "second" } ; const e = await jsonld.compact(d, ctx); assert.deepStrictEqual(e, ex); }); it('frame js valueOf/toString keywords', async () => { const d = { "@context": { "@vocab": "http://example.org/" }, "toString": { "valueOf": "thing" } } ; const frame = { "@context": { "@vocab": "http://example.org/" }, "toString": {} } ; const ex = { "@context": { "@vocab": "http://example.org/" }, "@graph": [ { "toString": { "valueOf": "thing" } } ] } ; const e = await jsonld.frame(d, frame); assert.deepStrictEqual(e, ex); }); }); jsonld.js-1.6.2/tests/new-embed-api/000077500000000000000000000000001347107445200172075ustar00rootroot00000000000000jsonld.js-1.6.2/tests/new-embed-api/frame-0001-frame.jsonld000066400000000000000000000006111347107445200231600ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@embed": "@always", "ex:contains": { "@type": "ex:Chapter", "@embed": "@always" }, "ex:bookmark": { "@type": "ex:Chapter", "@embed": "@always" } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0001-in.jsonld000066400000000000000000000013611347107445200224770ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:contains": {"@type": "@id"}, "ex:bookmark": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book", "ex:contains": "http://example.org/test#chapter", "ex:bookmark": "http://example.org/test#chapter" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One" } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0001-out.jsonld000066400000000000000000000013221347107445200226750ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@graph": [{ "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book", "ex:contains": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One" }, "ex:bookmark": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One" } } }] }jsonld.js-1.6.2/tests/new-embed-api/frame-0002-frame.jsonld000066400000000000000000000006061347107445200231650ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@embed": "@always", "ex:contains": { "@type": "ex:Chapter", "@embed": "@always" }, "ex:topic": { "@type": "ex:Library", "@embed": "@always" } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0002-in.jsonld000066400000000000000000000013741347107445200225040ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:contains": {"@type": "@id"}, "ex:topic": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": "http://example.org/test#chapter", "ex:topic": "http://example.org/test/#library" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One" } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0002-out.jsonld000066400000000000000000000011751347107445200227040ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@graph": [{ "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One" }, "ex:topic": { "@id": "http://example.org/test/#library" } } }] }jsonld.js-1.6.2/tests/new-embed-api/frame-0003-frame.jsonld000066400000000000000000000006161347107445200231670ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@embed": "@always", "ex:contains": { "@type": "ex:Chapter", "@embed": "@always", "ex:topic": { "@type": "ex:Library", "@embed": "@always" } } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0003-in.jsonld000066400000000000000000000013741347107445200225050ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:contains": {"@type": "@id"}, "ex:topic": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": "http://example.org/test#chapter" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": "http://example.org/test/#library" } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0003-out.jsonld000066400000000000000000000012031347107445200226750ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@graph": [{ "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": { "@id": "http://example.org/test/#library" } } } }] }jsonld.js-1.6.2/tests/new-embed-api/frame-0004-frame.jsonld000066400000000000000000000006521347107445200231700ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@omitDefault": "true", "@embed": "@always", "ex:contains": { "@type": "ex:Chapter", "@embed": "@always", "ex:topic": { "@type": "ex:Library", "@embed": "@always" } } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0004-in.jsonld000066400000000000000000000017771347107445200225150ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:contains": {"@type": "@id"}, "ex:topic": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:name": "My local library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test/#library2", "@type": "ex:Library", "ex:name": "Another library" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": "http://example.org/test#chapter" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": { "@set": [ "http://example.org/test/#library", "http://example.org/test/#library2" ] } } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0004-out.jsonld000066400000000000000000000017221347107445200227040ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@graph": [{ "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:name": "My local library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": [ { "@id": "http://example.org/test/#library" }, { "@id": "http://example.org/test/#library2", "@type": "ex:Library", "ex:name": "Another library" } ] } } }, { "@id": "http://example.org/test/#library2", "@type": "ex:Library", "ex:name": "Another library" }] }jsonld.js-1.6.2/tests/new-embed-api/frame-0005-frame.jsonld000066400000000000000000000007601347107445200231710ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@type": "ex:Town", "@embed": "@always", "ex:hasLibrary": { "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@embed": "@always", "ex:contains": { "@type": "ex:Chapter", "@embed": "@always", "ex:topic": { "@type": "ex:Library", "@embed": "@always" } } } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0005-in.jsonld000066400000000000000000000024171347107445200225060ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:contains": {"@type": "@id"}, "ex:topic": {"@type": "@id"}, "ex:hasLibrary": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/town/#123", "@type": "ex:Town", "ex:name": "My town", "ex:hasLibrary": [ "http://example.org/test/#library", "http://example.org/test/#library2" ] }, { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:name": "My local library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test/#library2", "@type": "ex:Library", "ex:name": "Another library" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": "http://example.org/test#chapter" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": { "@set": [ "http://example.org/test/#library", "http://example.org/test/#library2" ] } } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0005-out.jsonld000066400000000000000000000025071347107445200227070ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@graph": [ { "@id": "http://example.org/town/#123", "@type": "ex:Town", "ex:name": "My town", "ex:hasLibrary": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:name": "My local library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book about a library", "ex:contains": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": [ { "@id": "http://example.org/test/#library" }, { "@id": "http://example.org/test/#library2", "@type": "ex:Library", "ex:name": "Another library" } ] } } }, { "@id": "http://example.org/test/#library2", "@type": "ex:Library", "ex:contains": null, "ex:name": "Another library" } ] } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0006-frame.jsonld000066400000000000000000000007401347107445200231700ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@embed": "@always", "ex:bookmark": { "@type": "ex:Chapter", "@embed": "@always" }, "ex:contains": { "@type": "ex:Chapter", "@embed": "@always", "ex:topic" : { "@type": "ex:Topic", "@embed": "@always" } } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0006-in.jsonld000066400000000000000000000036241347107445200225100ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:contains": {"@type": "@id"}, "ex:bookmark": {"@type": "@id"}, "ex:topic": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book", "ex:contains": [ "http://example.org/test#chapter", "http://example.org/test#chapter2" ], "ex:bookmark": "http://example.org/test#chapter" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": [ "http://example.org/test#subject1", "http://example.org/test#subject2", "http://example.org/test#subject3" ] }, { "@id": "http://example.org/test#chapter2", "@type": "ex:Chapter", "dc:description": "More Fun", "dc:title": "Chapter Two", "ex:topic": [ "http://example.org/test#subject1", "http://example.org/test#subject4", "http://example.org/test#subject5" ] }, { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1" }, { "@id": "http://example.org/test#subject2", "@type": "ex:Topic", "dc:description": "Topic 2" }, { "@id": "http://example.org/test#subject3", "@type": "ex:Topic", "dc:description": "Topic 3" }, { "@id": "http://example.org/test#subject4", "@type": "ex:Topic", "dc:description": "Topic 4" }, { "@id": "http://example.org/test#subject5", "@type": "ex:Topic", "dc:description": "Topic 5" }] }jsonld.js-1.6.2/tests/new-embed-api/frame-0006-out.jsonld000066400000000000000000000051221347107445200227040ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#" }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book", "ex:contains": [ { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": [ { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1" }, { "@id": "http://example.org/test#subject2", "@type": "ex:Topic", "dc:description": "Topic 2" }, { "@id": "http://example.org/test#subject3", "@type": "ex:Topic", "dc:description": "Topic 3" } ] }, { "@id": "http://example.org/test#chapter2", "@type": "ex:Chapter", "dc:description": "More Fun", "dc:title": "Chapter Two", "ex:topic": [ { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1" }, { "@id": "http://example.org/test#subject4", "@type": "ex:Topic", "dc:description": "Topic 4" }, { "@id": "http://example.org/test#subject5", "@type": "ex:Topic", "dc:description": "Topic 5" } ] } ], "ex:bookmark": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "ex:topic": [ { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1" }, { "@id": "http://example.org/test#subject2", "@type": "ex:Topic", "dc:description": "Topic 2" }, { "@id": "http://example.org/test#subject3", "@type": "ex:Topic", "dc:description": "Topic 3" } ] } } } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0007-frame.jsonld000066400000000000000000000014071347107445200231720ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:relatesTo": {"@type": "@id"} }, "@type": "ex:Library", "@embed": "@always", "ex:contains": { "@type": "ex:Book", "@embed": "@always", "ex:bookmark": { "@type": "ex:Chapter", "@embed": "@always" }, "ex:contains": { "@type": "ex:Chapter", "@embed": "@always", "dc:subject": { "@omitDefault": "true", "@embed": "@always", "@type": "ex:Library" }, "ex:topic" : { "@type": "ex:Topic", "@embed": "@always", "ex:relatesTo" : { "@omitDefault": "true", "@embed": "@always", "@type": "ex:Library" } } } } }jsonld.js-1.6.2/tests/new-embed-api/frame-0007-in.jsonld000066400000000000000000000041201347107445200225010ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "dc:subject": {"@type": "@id"}, "ex:contains": {"@type": "@id"}, "ex:bookmark": {"@type": "@id"}, "ex:topic": {"@type": "@id"}, "ex:relatesTo": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": "http://example.org/test#book" }, { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book", "ex:contains": [ "http://example.org/test#chapter", "http://example.org/test#chapter2" ], "ex:bookmark": "http://example.org/test#chapter" }, { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "dc:subject": "http://example.org/test/#library", "ex:topic": [ "http://example.org/test#subject1", "http://example.org/test#subject2", "http://example.org/test#subject3" ] }, { "@id": "http://example.org/test#chapter2", "@type": "ex:Chapter", "dc:description": "More Fun", "dc:title": "Chapter Two", "ex:topic": [ "http://example.org/test#subject1", "http://example.org/test#subject4", "http://example.org/test#subject5" ] }, { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1", "ex:relatesTo": "http://example.org/test/#library" }, { "@id": "http://example.org/test#subject2", "@type": "ex:Topic", "dc:description": "Topic 2" }, { "@id": "http://example.org/test#subject3", "@type": "ex:Topic", "dc:description": "Topic 3" }, { "@id": "http://example.org/test#subject4", "@type": "ex:Topic", "dc:description": "Topic 4" }, { "@id": "http://example.org/test#subject5", "@type": "ex:Topic", "dc:description": "Topic 5" }] }jsonld.js-1.6.2/tests/new-embed-api/frame-0007-out.jsonld000066400000000000000000000057161347107445200227160ustar00rootroot00000000000000{ "@context": { "dc": "http://purl.org/dc/elements/1.1/", "ex": "http://example.org/vocab#", "ex:relatesTo": {"@type": "@id"} }, "@graph": [ { "@id": "http://example.org/test/#library", "@type": "ex:Library", "ex:contains": { "@id": "http://example.org/test#book", "@type": "ex:Book", "dc:contributor": "Writer", "dc:title": "My Book", "ex:contains": [ { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "dc:subject": {"@id": "http://example.org/test/#library"}, "ex:topic": [ { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1", "ex:relatesTo": "http://example.org/test/#library" }, { "@id": "http://example.org/test#subject2", "@type": "ex:Topic", "dc:description": "Topic 2" }, { "@id": "http://example.org/test#subject3", "@type": "ex:Topic", "dc:description": "Topic 3" } ] }, { "@id": "http://example.org/test#chapter2", "@type": "ex:Chapter", "dc:description": "More Fun", "dc:title": "Chapter Two", "ex:topic": [ { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1", "ex:relatesTo": "http://example.org/test/#library" }, { "@id": "http://example.org/test#subject4", "@type": "ex:Topic", "dc:description": "Topic 4" }, { "@id": "http://example.org/test#subject5", "@type": "ex:Topic", "dc:description": "Topic 5" } ] } ], "ex:bookmark": { "@id": "http://example.org/test#chapter", "@type": "ex:Chapter", "dc:description": "Fun", "dc:title": "Chapter One", "dc:subject": {"@id": "http://example.org/test/#library"}, "ex:topic": [ { "@id": "http://example.org/test#subject1", "@type": "ex:Topic", "dc:description": "Topic 1", "ex:relatesTo": "http://example.org/test/#library" }, { "@id": "http://example.org/test#subject2", "@type": "ex:Topic", "dc:description": "Topic 2" }, { "@id": "http://example.org/test#subject3", "@type": "ex:Topic", "dc:description": "Topic 3" } ] } } } ] }jsonld.js-1.6.2/tests/new-embed-api/frame-0008-frame.jsonld000066400000000000000000000004221347107445200231670ustar00rootroot00000000000000{ "@context": { "ex": "http://example.com/", "embed": { "@id": "ex:embed", "@container": "@set" }, "shouldExist": "ex:shouldExist" }, "@type": "http://example.com/TreeRoot", "@embed": "@always", "embed": { "@embed": "@always" } } jsonld.js-1.6.2/tests/new-embed-api/frame-0008-in.jsonld000066400000000000000000000015311347107445200225050ustar00rootroot00000000000000{ "@context": { "ex": "http://example.com/", "embed": { "@id": "ex:embed", "@container": "@set" }, "shouldExist": "ex:shouldExist" }, "@graph": [ { "@id": "ex:root", "@type": "ex:TreeRoot", "embed": [ { "@id": "ex:node-d1-with-leaf" }, { "@id": "ex:node-d1-with-node" } ] }, { "@id": "ex:node-d1-with-leaf", "embed": [ { "@id": "ex:leaf" } ] }, { "@id": "ex:node-d1-with-node", "embed": [ { "@id": "ex:node-d2-with-leaf" } ] }, { "@id": "ex:node-d2-with-leaf", "embed": [ { "@id": "ex:leaf" } ] }, { "@id": "ex:leaf", "shouldExist": "shows when embedded" } ] } jsonld.js-1.6.2/tests/new-embed-api/frame-0008-out.jsonld000066400000000000000000000014551347107445200227130ustar00rootroot00000000000000{ "@context": { "ex": "http://example.com/", "embed": { "@id": "ex:embed", "@container": "@set" },"shouldExist": "ex:shouldExist" }, "@graph": [ { "@id": "ex:root", "@type": "ex:TreeRoot", "embed": [ { "@id": "ex:node-d1-with-leaf", "embed": [ { "@id": "ex:leaf", "shouldExist": "shows when embedded" } ] }, { "@id": "ex:node-d1-with-node", "embed": [ { "@id": "ex:node-d2-with-leaf", "embed": [ { "@id": "ex:leaf", "shouldExist": "shows when embedded" } ] } ] } ] } ] } jsonld.js-1.6.2/tests/new-embed-api/manifest.jsonld000066400000000000000000000043521347107445200222340ustar00rootroot00000000000000{ "@context": "http://json-ld.org/test-suite/context.jsonld", "@id": "", "@type": "mf:Manifest", "name": "New Embed API Framing", "description": "JSON-LD framing tests use object comparison.", "baseIri": "", "sequence": [{ "@id": "#t0001", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Simple embed", "input": "frame-0001-in.jsonld", "frame": "frame-0001-frame.jsonld", "expect": "frame-0001-out.jsonld" }, { "@id": "#t0002", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Embed with direct circular reference", "input": "frame-0002-in.jsonld", "frame": "frame-0002-frame.jsonld", "expect": "frame-0002-out.jsonld" }, { "@id": "#t0003", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Embed with indirect circular reference", "input": "frame-0003-in.jsonld", "frame": "frame-0003-frame.jsonld", "expect": "frame-0003-out.jsonld" }, { "@id": "#t0004", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Embed with indirect circular reference via set", "input": "frame-0004-in.jsonld", "frame": "frame-0004-frame.jsonld", "expect": "frame-0004-out.jsonld" }, { "@id": "#t0005", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Embed with nested indirect circular reference via set", "input": "frame-0005-in.jsonld", "frame": "frame-0005-frame.jsonld", "expect": "frame-0005-out.jsonld" }, { "@id": "#t0006", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Multi-level simple embeds", "input": "frame-0006-in.jsonld", "frame": "frame-0006-frame.jsonld", "expect": "frame-0006-out.jsonld" }, { "@id": "#t0007", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "A tangle of nastiness", "input": "frame-0007-in.jsonld", "frame": "frame-0007-frame.jsonld", "expect": "frame-0007-out.jsonld" },{ "@id": "#t0008", "@type": ["jld:PositiveEvaluationTest", "jld:FrameTest"], "name": "Recursive property embed w/o circular reference", "input": "frame-0008-in.jsonld", "frame": "frame-0008-frame.jsonld", "expect": "frame-0008-out.jsonld" }] } jsonld.js-1.6.2/tests/node-document-loader-tests.js000066400000000000000000000065231347107445200223060ustar00rootroot00000000000000/** * Local tests for the node.js document loader * * @author goofballLogic */ const jsonld = require('..'); const assert = require('assert'); describe('For the node.js document loader', function() { const documentLoaderType = 'node'; const requestMock = function(options, callback) { // store these for later inspection requestMock.calls.push([].slice.call(arguments, 0)); callback(null, {headers: {}}, ''); }; describe('When built with no options specified', function() { it('loading should work', function(done) { jsonld.useDocumentLoader(documentLoaderType); jsonld.expand('http://schema.org/', function(err, expanded) { assert.ifError(err); done(); }); }); }); describe('When built with no explicit headers', function() { const options = {request: requestMock}; it('loading should pass just the ld Accept header', function(done) { jsonld.useDocumentLoader(documentLoaderType, options); requestMock.calls = []; const iri = 'http://some.thing.test.com/my-thing.jsonld'; jsonld.documentLoader(iri, function(err) { if(err) { return done(err); } const actualOptions = (requestMock.calls[0] || {})[0] || {}; const actualHeaders = actualOptions.headers; const expectedHeaders = { 'Accept': 'application/ld+json, application/json' }; assert.deepEqual(actualHeaders, expectedHeaders); done(); }); }); }); describe('When built using options containing a headers object', function() { const options = {request: requestMock}; options.headers = { 'x-test-header-1': 'First value', 'x-test-two': 2.34, 'Via': '1.0 fred, 1.1 example.com (Apache/1.1)', 'Authorization': 'Bearer d783jkjaods9f87o83hj' }; it('loading should pass the headers through on the request', function(done) { jsonld.useDocumentLoader(documentLoaderType, options); requestMock.calls = []; const iri = 'http://some.thing.test.com/my-thing.jsonld'; jsonld.documentLoader(iri, function(err) { if(err) { return done(err); } const actualOptions = (requestMock.calls[0] || {})[0] || {}; const actualHeaders = actualOptions.headers; const expectedHeaders = { 'Accept': 'application/ld+json, application/json' }; for(const k in options.headers) { expectedHeaders[k] = options.headers[k]; } assert.deepEqual(actualHeaders, expectedHeaders); done(); }); }); }); describe('When built using headers that already contain an Accept header', function() { const options = {request: requestMock}; options.headers = { 'x-test-header-3': 'Third value', 'Accept': 'video/mp4' }; it('constructing the document loader should fail', function(done) { const expectedMessage = 'Accept header may not be specified as an option; ' + 'only "application/ld+json, application/json" is supported.'; assert.throws( jsonld.useDocumentLoader.bind(jsonld, documentLoaderType, options), function(err) { assert.ok( err instanceof RangeError, 'A range error should be thrown'); assert.equal(err.message, expectedMessage); return true; }); done(); }); }); }); jsonld.js-1.6.2/tests/remote-0001-in.jsonld000066400000000000000000000001151347107445200202620ustar00rootroot00000000000000{ "@context": "http://localhost:8000/context-1.jsonld", "term1": "foo" } jsonld.js-1.6.2/tests/remote-0001-out.jsonld000066400000000000000000000000771347107445200204720ustar00rootroot00000000000000[ { "http://example.org/term1": [{"@value": "foo"}] } ]jsonld.js-1.6.2/tests/remote-0002-in.jsonld000066400000000000000000000001151347107445200202630ustar00rootroot00000000000000{ "@context": "http://localhost:8000/context-2.jsonld", "term1": "foo" } jsonld.js-1.6.2/tests/remote-0002-out.jsonld000066400000000000000000000000771347107445200204730ustar00rootroot00000000000000[ { "http://example.org/term1": [{"@value": "foo"}] } ]jsonld.js-1.6.2/tests/remote-0003-in.jsonld000066400000000000000000000001151347107445200202640ustar00rootroot00000000000000{ "@context": "http://localhost:8000/context-3.jsonld", "term1": "foo" } jsonld.js-1.6.2/tests/remote-0003-out.jsonld000066400000000000000000000000771347107445200204740ustar00rootroot00000000000000[ { "http://example.org/term1": [{"@value": "foo"}] } ]jsonld.js-1.6.2/tests/remote-0004-in.jsonld000066400000000000000000000001771347107445200202750ustar00rootroot00000000000000{ "@context": [ {"term1": "http://notexample.org"}, "http://localhost:8000/context-4.jsonld" ], "term1": "foo" } jsonld.js-1.6.2/tests/remote-0004-out.jsonld000066400000000000000000000000771347107445200204750ustar00rootroot00000000000000[ { "http://example.org/term1": [{"@value": "foo"}] } ]jsonld.js-1.6.2/tests/remote-0005-in.jsonld000066400000000000000000000001151347107445200202660ustar00rootroot00000000000000{ "@context": "http://localhost:8000/context-5.jsonld", "term1": "foo" } jsonld.js-1.6.2/tests/remote-0005-out.jsonld000066400000000000000000000000771347107445200204760ustar00rootroot00000000000000[ { "http://example.org/term1": [{"@value": "foo"}] } ]jsonld.js-1.6.2/tests/remote-0006-in.jsonld000066400000000000000000000001051347107445200202660ustar00rootroot00000000000000{ "@context": "https://w3id.org/payswarm/v1", "comment": "foo" } jsonld.js-1.6.2/tests/remote-0006-out.jsonld000066400000000000000000000001231347107445200204670ustar00rootroot00000000000000[ { "http://www.w3.org/2000/01/rdf-schema#comment": [{"@value": "foo"}] } ]jsonld.js-1.6.2/tests/remote-context-server.js000066400000000000000000000005261347107445200214170ustar00rootroot00000000000000const cors = require('cors'); const express = require('express'); const path = require('path'); const app = express(); app.use(cors()); app.use(express.static(path.resolve(path.join(__dirname, 'contexts')))); const port = 8000; app.listen(port, function() { console.log('Remote context test server running on port ' + port + '...'); }); jsonld.js-1.6.2/tests/setImmediate.js000066400000000000000000000226511347107445200175530ustar00rootroot00000000000000/* Copyright (c) 2012 Barnesandnoble.com, llc, Donavon West, and Domenic Denicola Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function (global, undefined) { "use strict"; var tasks = (function () { function Task(handler, args) { this.handler = handler; this.args = args; } Task.prototype.run = function () { // See steps in section 5 of the spec. if (typeof this.handler === "function") { // Choice of `thisArg` is not in the setImmediate spec; `undefined` is in the setTimeout spec though: // http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html this.handler.apply(undefined, this.args); } else { var scriptSource = "" + this.handler; /*jshint evil: true */ eval(scriptSource); } }; var nextHandle = 1; // Spec says greater than zero var tasksByHandle = {}; var currentlyRunningATask = false; return { addFromSetImmediateArguments: function (args) { var handler = args[0]; var argsToHandle = Array.prototype.slice.call(args, 1); var task = new Task(handler, argsToHandle); var thisHandle = nextHandle++; tasksByHandle[thisHandle] = task; return thisHandle; }, runIfPresent: function (handle) { // From the spec: "Wait until any invocations of this algorithm started before this one have completed." // So if we're currently running a task, we'll need to delay this invocation. if (!currentlyRunningATask) { var task = tasksByHandle[handle]; if (task) { currentlyRunningATask = true; try { task.run(); } finally { delete tasksByHandle[handle]; currentlyRunningATask = false; } } } else { // Delay by doing a setTimeout. setImmediate was tried instead, but in Firefox 7 it generated a // "too much recursion" error. global.setTimeout(function () { tasks.runIfPresent(handle); }, 0); } }, remove: function (handle) { delete tasksByHandle[handle]; } }; }()); function canUseNextTick() { // Don't get fooled by e.g. browserify environments. return typeof process === "object" && Object.prototype.toString.call(process) === "[object process]"; } function canUseMessageChannel() { return !!global.MessageChannel; } function canUsePostMessage() { // The test against `importScripts` prevents this implementation from being installed inside a web worker, // where `global.postMessage` means something completely different and can't be used for this purpose. if (!global.postMessage || global.importScripts) { return false; } var postMessageIsAsynchronous = true; var oldOnMessage = global.onmessage; global.onmessage = function () { postMessageIsAsynchronous = false; }; global.postMessage("", "*"); global.onmessage = oldOnMessage; return postMessageIsAsynchronous; } function canUseReadyStateChange() { return "document" in global && "onreadystatechange" in global.document.createElement("script"); } function installNextTickImplementation(attachTo) { attachTo.setImmediate = function () { var handle = tasks.addFromSetImmediateArguments(arguments); process.nextTick(function () { tasks.runIfPresent(handle); }); return handle; }; } function installMessageChannelImplementation(attachTo) { var channel = new global.MessageChannel(); channel.port1.onmessage = function (event) { var handle = event.data; tasks.runIfPresent(handle); }; attachTo.setImmediate = function () { var handle = tasks.addFromSetImmediateArguments(arguments); channel.port2.postMessage(handle); return handle; }; } function installPostMessageImplementation(attachTo) { // Installs an event handler on `global` for the `message` event: see // * https://developer.mozilla.org/en/DOM/window.postMessage // * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages var MESSAGE_PREFIX = "com.bn.NobleJS.setImmediate" + Math.random(); function isStringAndStartsWith(string, putativeStart) { return typeof string === "string" && string.substring(0, putativeStart.length) === putativeStart; } function onGlobalMessage(event) { // This will catch all incoming messages (even from other windows!), so we need to try reasonably hard to // avoid letting anyone else trick us into firing off. We test the origin is still this window, and that a // (randomly generated) unpredictable identifying prefix is present. if (event.source === global && isStringAndStartsWith(event.data, MESSAGE_PREFIX)) { var handle = event.data.substring(MESSAGE_PREFIX.length); tasks.runIfPresent(handle); } } if (global.addEventListener) { global.addEventListener("message", onGlobalMessage, false); } else { global.attachEvent("onmessage", onGlobalMessage); } attachTo.setImmediate = function () { var handle = tasks.addFromSetImmediateArguments(arguments); // Make `global` post a message to itself with the handle and identifying prefix, thus asynchronously // invoking our onGlobalMessage listener above. global.postMessage(MESSAGE_PREFIX + handle, "*"); return handle; }; } function installReadyStateChangeImplementation(attachTo) { attachTo.setImmediate = function () { var handle = tasks.addFromSetImmediateArguments(arguments); // Create a